xref: /OK3568_Linux_fs/kernel/Documentation/cdrom/ide-cd.rst (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593SmuzhiyunIDE-CD driver documentation
2*4882a593Smuzhiyun===========================
3*4882a593Smuzhiyun
4*4882a593Smuzhiyun:Originally by: scott snyder  <snyder@fnald0.fnal.gov> (19 May 1996)
5*4882a593Smuzhiyun:Carrying on the torch is: Erik Andersen <andersee@debian.org>
6*4882a593Smuzhiyun:New maintainers (19 Oct 1998): Jens Axboe <axboe@image.dk>
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun1. Introduction
9*4882a593Smuzhiyun---------------
10*4882a593Smuzhiyun
11*4882a593SmuzhiyunThe ide-cd driver should work with all ATAPI ver 1.2 to ATAPI 2.6 compliant
12*4882a593SmuzhiyunCDROM drives which attach to an IDE interface.  Note that some CDROM vendors
13*4882a593Smuzhiyun(including Mitsumi, Sony, Creative, Aztech, and Goldstar) have made
14*4882a593Smuzhiyunboth ATAPI-compliant drives and drives which use a proprietary
15*4882a593Smuzhiyuninterface.  If your drive uses one of those proprietary interfaces,
16*4882a593Smuzhiyunthis driver will not work with it (but one of the other CDROM drivers
17*4882a593Smuzhiyunprobably will).  This driver will not work with `ATAPI` drives which
18*4882a593Smuzhiyunattach to the parallel port.  In addition, there is at least one drive
19*4882a593Smuzhiyun(CyCDROM CR520ie) which attaches to the IDE port but is not ATAPI;
20*4882a593Smuzhiyunthis driver will not work with drives like that either (but see the
21*4882a593Smuzhiyunaztcd driver).
22*4882a593Smuzhiyun
23*4882a593SmuzhiyunThis driver provides the following features:
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun - Reading from data tracks, and mounting ISO 9660 filesystems.
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun - Playing audio tracks.  Most of the CDROM player programs floating
28*4882a593Smuzhiyun   around should work; I usually use Workman.
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun - Multisession support.
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun - On drives which support it, reading digital audio data directly
33*4882a593Smuzhiyun   from audio tracks.  The program cdda2wav can be used for this.
34*4882a593Smuzhiyun   Note, however, that only some drives actually support this.
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun - There is now support for CDROM changers which comply with the
37*4882a593Smuzhiyun   ATAPI 2.6 draft standard (such as the NEC CDR-251).  This additional
38*4882a593Smuzhiyun   functionality includes a function call to query which slot is the
39*4882a593Smuzhiyun   currently selected slot, a function call to query which slots contain
40*4882a593Smuzhiyun   CDs, etc. A sample program which demonstrates this functionality is
41*4882a593Smuzhiyun   appended to the end of this file.  The Sanyo 3-disc changer
42*4882a593Smuzhiyun   (which does not conform to the standard) is also now supported.
43*4882a593Smuzhiyun   Please note the driver refers to the first CD as slot # 0.
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun2. Installation
47*4882a593Smuzhiyun---------------
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun0. The ide-cd relies on the ide disk driver.  See
50*4882a593Smuzhiyun   Documentation/ide/ide.rst for up-to-date information on the ide
51*4882a593Smuzhiyun   driver.
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun1. Make sure that the ide and ide-cd drivers are compiled into the
54*4882a593Smuzhiyun   kernel you're using.  When configuring the kernel, in the section
55*4882a593Smuzhiyun   entitled "Floppy, IDE, and other block devices", say either `Y`
56*4882a593Smuzhiyun   (which will compile the support directly into the kernel) or `M`
57*4882a593Smuzhiyun   (to compile support as a module which can be loaded and unloaded)
58*4882a593Smuzhiyun   to the options::
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun      ATA/ATAPI/MFM/RLL support
61*4882a593Smuzhiyun      Include IDE/ATAPI CDROM support
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun   Depending on what type of IDE interface you have, you may need to
64*4882a593Smuzhiyun   specify additional configuration options.  See
65*4882a593Smuzhiyun   Documentation/ide/ide.rst.
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun2. You should also ensure that the iso9660 filesystem is either
68*4882a593Smuzhiyun   compiled into the kernel or available as a loadable module.  You
69*4882a593Smuzhiyun   can see if a filesystem is known to the kernel by catting
70*4882a593Smuzhiyun   /proc/filesystems.
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun3. The CDROM drive should be connected to the host on an IDE
73*4882a593Smuzhiyun   interface.  Each interface on a system is defined by an I/O port
74*4882a593Smuzhiyun   address and an IRQ number, the standard assignments being
75*4882a593Smuzhiyun   0x1f0 and 14 for the primary interface and 0x170 and 15 for the
76*4882a593Smuzhiyun   secondary interface.  Each interface can control up to two devices,
77*4882a593Smuzhiyun   where each device can be a hard drive, a CDROM drive, a floppy drive,
78*4882a593Smuzhiyun   or a tape drive.  The two devices on an interface are called `master`
79*4882a593Smuzhiyun   and `slave`; this is usually selectable via a jumper on the drive.
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun   Linux names these devices as follows.  The master and slave devices
82*4882a593Smuzhiyun   on the primary IDE interface are called `hda` and `hdb`,
83*4882a593Smuzhiyun   respectively.  The drives on the secondary interface are called
84*4882a593Smuzhiyun   `hdc` and `hdd`.  (Interfaces at other locations get other letters
85*4882a593Smuzhiyun   in the third position; see Documentation/ide/ide.rst.)
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun   If you want your CDROM drive to be found automatically by the
88*4882a593Smuzhiyun   driver, you should make sure your IDE interface uses either the
89*4882a593Smuzhiyun   primary or secondary addresses mentioned above.  In addition, if
90*4882a593Smuzhiyun   the CDROM drive is the only device on the IDE interface, it should
91*4882a593Smuzhiyun   be jumpered as `master`.  (If for some reason you cannot configure
92*4882a593Smuzhiyun   your system in this manner, you can probably still use the driver.
93*4882a593Smuzhiyun   You may have to pass extra configuration information to the kernel
94*4882a593Smuzhiyun   when you boot, however.  See Documentation/ide/ide.rst for more
95*4882a593Smuzhiyun   information.)
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun4. Boot the system.  If the drive is recognized, you should see a
98*4882a593Smuzhiyun   message which looks like::
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun     hdb: NEC CD-ROM DRIVE:260, ATAPI CDROM drive
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun   If you do not see this, see section 5 below.
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun5. You may want to create a symbolic link /dev/cdrom pointing to the
105*4882a593Smuzhiyun   actual device.  You can do this with the command::
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun     ln -s  /dev/hdX  /dev/cdrom
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun   where X should be replaced by the letter indicating where your
110*4882a593Smuzhiyun   drive is installed.
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun6. You should be able to see any error messages from the driver with
113*4882a593Smuzhiyun   the `dmesg` command.
114*4882a593Smuzhiyun
115*4882a593Smuzhiyun
116*4882a593Smuzhiyun3. Basic usage
117*4882a593Smuzhiyun--------------
118*4882a593Smuzhiyun
119*4882a593SmuzhiyunAn ISO 9660 CDROM can be mounted by putting the disc in the drive and
120*4882a593Smuzhiyuntyping (as root)::
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun  mount -t iso9660 /dev/cdrom /mnt/cdrom
123*4882a593Smuzhiyun
124*4882a593Smuzhiyunwhere it is assumed that /dev/cdrom is a link pointing to the actual
125*4882a593Smuzhiyundevice (as described in step 5 of the last section) and /mnt/cdrom is
126*4882a593Smuzhiyunan empty directory.  You should now be able to see the contents of the
127*4882a593SmuzhiyunCDROM under the /mnt/cdrom directory.  If you want to eject the CDROM,
128*4882a593Smuzhiyunyou must first dismount it with a command like::
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun  umount /mnt/cdrom
131*4882a593Smuzhiyun
132*4882a593SmuzhiyunNote that audio CDs cannot be mounted.
133*4882a593Smuzhiyun
134*4882a593SmuzhiyunSome distributions set up /etc/fstab to always try to mount a CDROM
135*4882a593Smuzhiyunfilesystem on bootup.  It is not required to mount the CDROM in this
136*4882a593Smuzhiyunmanner, though, and it may be a nuisance if you change CDROMs often.
137*4882a593SmuzhiyunYou should feel free to remove the cdrom line from /etc/fstab and
138*4882a593Smuzhiyunmount CDROMs manually if that suits you better.
139*4882a593Smuzhiyun
140*4882a593SmuzhiyunMultisession and photocd discs should work with no special handling.
141*4882a593SmuzhiyunThe hpcdtoppm package (ftp.gwdg.de:/pub/linux/hpcdtoppm/) may be
142*4882a593Smuzhiyunuseful for reading photocds.
143*4882a593Smuzhiyun
144*4882a593SmuzhiyunTo play an audio CD, you should first unmount and remove any data
145*4882a593SmuzhiyunCDROM.  Any of the CDROM player programs should then work (workman,
146*4882a593Smuzhiyunworkbone, cdplayer, etc.).
147*4882a593Smuzhiyun
148*4882a593SmuzhiyunOn a few drives, you can read digital audio directly using a program
149*4882a593Smuzhiyunsuch as cdda2wav.  The only types of drive which I've heard support
150*4882a593Smuzhiyunthis are Sony and Toshiba drives.  You will get errors if you try to
151*4882a593Smuzhiyunuse this function on a drive which does not support it.
152*4882a593Smuzhiyun
153*4882a593SmuzhiyunFor supported changers, you can use the `cdchange` program (appended to
154*4882a593Smuzhiyunthe end of this file) to switch between changer slots.  Note that the
155*4882a593Smuzhiyundrive should be unmounted before attempting this.  The program takes
156*4882a593Smuzhiyuntwo arguments:  the CDROM device, and the slot number to which you wish
157*4882a593Smuzhiyunto change.  If the slot number is -1, the drive is unloaded.
158*4882a593Smuzhiyun
159*4882a593Smuzhiyun
160*4882a593Smuzhiyun4. Common problems
161*4882a593Smuzhiyun------------------
162*4882a593Smuzhiyun
163*4882a593SmuzhiyunThis section discusses some common problems encountered when trying to
164*4882a593Smuzhiyunuse the driver, and some possible solutions.  Note that if you are
165*4882a593Smuzhiyunexperiencing problems, you should probably also review
166*4882a593SmuzhiyunDocumentation/ide/ide.rst for current information about the underlying
167*4882a593SmuzhiyunIDE support code.  Some of these items apply only to earlier versions
168*4882a593Smuzhiyunof the driver, but are mentioned here for completeness.
169*4882a593Smuzhiyun
170*4882a593SmuzhiyunIn most cases, you should probably check with `dmesg` for any errors
171*4882a593Smuzhiyunfrom the driver.
172*4882a593Smuzhiyun
173*4882a593Smuzhiyuna. Drive is not detected during booting.
174*4882a593Smuzhiyun
175*4882a593Smuzhiyun   - Review the configuration instructions above and in
176*4882a593Smuzhiyun     Documentation/ide/ide.rst, and check how your hardware is
177*4882a593Smuzhiyun     configured.
178*4882a593Smuzhiyun
179*4882a593Smuzhiyun   - If your drive is the only device on an IDE interface, it should
180*4882a593Smuzhiyun     be jumpered as master, if at all possible.
181*4882a593Smuzhiyun
182*4882a593Smuzhiyun   - If your IDE interface is not at the standard addresses of 0x170
183*4882a593Smuzhiyun     or 0x1f0, you'll need to explicitly inform the driver using a
184*4882a593Smuzhiyun     lilo option.  See Documentation/ide/ide.rst.  (This feature was
185*4882a593Smuzhiyun     added around kernel version 1.3.30.)
186*4882a593Smuzhiyun
187*4882a593Smuzhiyun   - If the autoprobing is not finding your drive, you can tell the
188*4882a593Smuzhiyun     driver to assume that one exists by using a lilo option of the
189*4882a593Smuzhiyun     form `hdX=cdrom`, where X is the drive letter corresponding to
190*4882a593Smuzhiyun     where your drive is installed.  Note that if you do this and you
191*4882a593Smuzhiyun     see a boot message like::
192*4882a593Smuzhiyun
193*4882a593Smuzhiyun       hdX: ATAPI cdrom (?)
194*4882a593Smuzhiyun
195*4882a593Smuzhiyun     this does _not_ mean that the driver has successfully detected
196*4882a593Smuzhiyun     the drive; rather, it means that the driver has not detected a
197*4882a593Smuzhiyun     drive, but is assuming there's one there anyway because you told
198*4882a593Smuzhiyun     it so.  If you actually try to do I/O to a drive defined at a
199*4882a593Smuzhiyun     nonexistent or nonresponding I/O address, you'll probably get
200*4882a593Smuzhiyun     errors with a status value of 0xff.
201*4882a593Smuzhiyun
202*4882a593Smuzhiyun   - Some IDE adapters require a nonstandard initialization sequence
203*4882a593Smuzhiyun     before they'll function properly.  (If this is the case, there
204*4882a593Smuzhiyun     will often be a separate MS-DOS driver just for the controller.)
205*4882a593Smuzhiyun     IDE interfaces on sound cards often fall into this category.
206*4882a593Smuzhiyun
207*4882a593Smuzhiyun     Support for some interfaces needing extra initialization is
208*4882a593Smuzhiyun     provided in later 1.3.x kernels.  You may need to turn on
209*4882a593Smuzhiyun     additional kernel configuration options to get them to work;
210*4882a593Smuzhiyun     see Documentation/ide/ide.rst.
211*4882a593Smuzhiyun
212*4882a593Smuzhiyun     Even if support is not available for your interface, you may be
213*4882a593Smuzhiyun     able to get it to work with the following procedure.  First boot
214*4882a593Smuzhiyun     MS-DOS and load the appropriate drivers.  Then warm-boot linux
215*4882a593Smuzhiyun     (i.e., without powering off).  If this works, it can be automated
216*4882a593Smuzhiyun     by running loadlin from the MS-DOS autoexec.
217*4882a593Smuzhiyun
218*4882a593Smuzhiyun
219*4882a593Smuzhiyunb. Timeout/IRQ errors.
220*4882a593Smuzhiyun
221*4882a593Smuzhiyun  - If you always get timeout errors, interrupts from the drive are
222*4882a593Smuzhiyun    probably not making it to the host.
223*4882a593Smuzhiyun
224*4882a593Smuzhiyun  - IRQ problems may also be indicated by the message
225*4882a593Smuzhiyun    `IRQ probe failed (<n>)` while booting.  If <n> is zero, that
226*4882a593Smuzhiyun    means that the system did not see an interrupt from the drive when
227*4882a593Smuzhiyun    it was expecting one (on any feasible IRQ).  If <n> is negative,
228*4882a593Smuzhiyun    that means the system saw interrupts on multiple IRQ lines, when
229*4882a593Smuzhiyun    it was expecting to receive just one from the CDROM drive.
230*4882a593Smuzhiyun
231*4882a593Smuzhiyun  - Double-check your hardware configuration to make sure that the IRQ
232*4882a593Smuzhiyun    number of your IDE interface matches what the driver expects.
233*4882a593Smuzhiyun    (The usual assignments are 14 for the primary (0x1f0) interface
234*4882a593Smuzhiyun    and 15 for the secondary (0x170) interface.)  Also be sure that
235*4882a593Smuzhiyun    you don't have some other hardware which might be conflicting with
236*4882a593Smuzhiyun    the IRQ you're using.  Also check the BIOS setup for your system;
237*4882a593Smuzhiyun    some have the ability to disable individual IRQ levels, and I've
238*4882a593Smuzhiyun    had one report of a system which was shipped with IRQ 15 disabled
239*4882a593Smuzhiyun    by default.
240*4882a593Smuzhiyun
241*4882a593Smuzhiyun  - Note that many MS-DOS CDROM drivers will still function even if
242*4882a593Smuzhiyun    there are hardware problems with the interrupt setup; they
243*4882a593Smuzhiyun    apparently don't use interrupts.
244*4882a593Smuzhiyun
245*4882a593Smuzhiyun  - If you own a Pioneer DR-A24X, you _will_ get nasty error messages
246*4882a593Smuzhiyun    on boot such as "irq timeout: status=0x50 { DriveReady SeekComplete }"
247*4882a593Smuzhiyun    The Pioneer DR-A24X CDROM drives are fairly popular these days.
248*4882a593Smuzhiyun    Unfortunately, these drives seem to become very confused when we perform
249*4882a593Smuzhiyun    the standard Linux ATA disk drive probe. If you own one of these drives,
250*4882a593Smuzhiyun    you can bypass the ATA probing which confuses these CDROM drives, by
251*4882a593Smuzhiyun    adding `append="hdX=noprobe hdX=cdrom"` to your lilo.conf file and running
252*4882a593Smuzhiyun    lilo (again where X is the drive letter corresponding to where your drive
253*4882a593Smuzhiyun    is installed.)
254*4882a593Smuzhiyun
255*4882a593Smuzhiyunc. System hangups.
256*4882a593Smuzhiyun
257*4882a593Smuzhiyun  - If the system locks up when you try to access the CDROM, the most
258*4882a593Smuzhiyun    likely cause is that you have a buggy IDE adapter which doesn't
259*4882a593Smuzhiyun    properly handle simultaneous transactions on multiple interfaces.
260*4882a593Smuzhiyun    The most notorious of these is the CMD640B chip.  This problem can
261*4882a593Smuzhiyun    be worked around by specifying the `serialize` option when
262*4882a593Smuzhiyun    booting.  Recent kernels should be able to detect the need for
263*4882a593Smuzhiyun    this automatically in most cases, but the detection is not
264*4882a593Smuzhiyun    foolproof.  See Documentation/ide/ide.rst for more information
265*4882a593Smuzhiyun    about the `serialize` option and the CMD640B.
266*4882a593Smuzhiyun
267*4882a593Smuzhiyun  - Note that many MS-DOS CDROM drivers will work with such buggy
268*4882a593Smuzhiyun    hardware, apparently because they never attempt to overlap CDROM
269*4882a593Smuzhiyun    operations with other disk activity.
270*4882a593Smuzhiyun
271*4882a593Smuzhiyun
272*4882a593Smuzhiyund. Can't mount a CDROM.
273*4882a593Smuzhiyun
274*4882a593Smuzhiyun  - If you get errors from mount, it may help to check `dmesg` to see
275*4882a593Smuzhiyun    if there are any more specific errors from the driver or from the
276*4882a593Smuzhiyun    filesystem.
277*4882a593Smuzhiyun
278*4882a593Smuzhiyun  - Make sure there's a CDROM loaded in the drive, and that's it's an
279*4882a593Smuzhiyun    ISO 9660 disc.  You can't mount an audio CD.
280*4882a593Smuzhiyun
281*4882a593Smuzhiyun  - With the CDROM in the drive and unmounted, try something like::
282*4882a593Smuzhiyun
283*4882a593Smuzhiyun      cat /dev/cdrom | od | more
284*4882a593Smuzhiyun
285*4882a593Smuzhiyun    If you see a dump, then the drive and driver are probably working
286*4882a593Smuzhiyun    OK, and the problem is at the filesystem level (i.e., the CDROM is
287*4882a593Smuzhiyun    not ISO 9660 or has errors in the filesystem structure).
288*4882a593Smuzhiyun
289*4882a593Smuzhiyun  - If you see `not a block device` errors, check that the definitions
290*4882a593Smuzhiyun    of the device special files are correct.  They should be as
291*4882a593Smuzhiyun    follows::
292*4882a593Smuzhiyun
293*4882a593Smuzhiyun      brw-rw----   1 root     disk       3,   0 Nov 11 18:48 /dev/hda
294*4882a593Smuzhiyun      brw-rw----   1 root     disk       3,  64 Nov 11 18:48 /dev/hdb
295*4882a593Smuzhiyun      brw-rw----   1 root     disk      22,   0 Nov 11 18:48 /dev/hdc
296*4882a593Smuzhiyun      brw-rw----   1 root     disk      22,  64 Nov 11 18:48 /dev/hdd
297*4882a593Smuzhiyun
298*4882a593Smuzhiyun    Some early Slackware releases had these defined incorrectly.  If
299*4882a593Smuzhiyun    these are wrong, you can remake them by running the script
300*4882a593Smuzhiyun    scripts/MAKEDEV.ide.  (You may have to make it executable
301*4882a593Smuzhiyun    with chmod first.)
302*4882a593Smuzhiyun
303*4882a593Smuzhiyun    If you have a /dev/cdrom symbolic link, check that it is pointing
304*4882a593Smuzhiyun    to the correct device file.
305*4882a593Smuzhiyun
306*4882a593Smuzhiyun    If you hear people talking of the devices `hd1a` and `hd1b`, these
307*4882a593Smuzhiyun    were old names for what are now called hdc and hdd.  Those names
308*4882a593Smuzhiyun    should be considered obsolete.
309*4882a593Smuzhiyun
310*4882a593Smuzhiyun  - If mount is complaining that the iso9660 filesystem is not
311*4882a593Smuzhiyun    available, but you know it is (check /proc/filesystems), you
312*4882a593Smuzhiyun    probably need a newer version of mount.  Early versions would not
313*4882a593Smuzhiyun    always give meaningful error messages.
314*4882a593Smuzhiyun
315*4882a593Smuzhiyun
316*4882a593Smuzhiyune. Directory listings are unpredictably truncated, and `dmesg` shows
317*4882a593Smuzhiyun   `buffer botch` error messages from the driver.
318*4882a593Smuzhiyun
319*4882a593Smuzhiyun  - There was a bug in the version of the driver in 1.2.x kernels
320*4882a593Smuzhiyun    which could cause this.  It was fixed in 1.3.0.  If you can't
321*4882a593Smuzhiyun    upgrade, you can probably work around the problem by specifying a
322*4882a593Smuzhiyun    blocksize of 2048 when mounting.  (Note that you won't be able to
323*4882a593Smuzhiyun    directly execute binaries off the CDROM in that case.)
324*4882a593Smuzhiyun
325*4882a593Smuzhiyun    If you see this in kernels later than 1.3.0, please report it as a
326*4882a593Smuzhiyun    bug.
327*4882a593Smuzhiyun
328*4882a593Smuzhiyun
329*4882a593Smuzhiyunf. Data corruption.
330*4882a593Smuzhiyun
331*4882a593Smuzhiyun  - Random data corruption was occasionally observed with the Hitachi
332*4882a593Smuzhiyun    CDR-7730 CDROM. If you experience data corruption, using "hdx=slow"
333*4882a593Smuzhiyun    as a command line parameter may work around the problem, at the
334*4882a593Smuzhiyun    expense of low system performance.
335*4882a593Smuzhiyun
336*4882a593Smuzhiyun
337*4882a593Smuzhiyun5. cdchange.c
338*4882a593Smuzhiyun-------------
339*4882a593Smuzhiyun
340*4882a593Smuzhiyun::
341*4882a593Smuzhiyun
342*4882a593Smuzhiyun  /*
343*4882a593Smuzhiyun   * cdchange.c  [-v]  <device>  [<slot>]
344*4882a593Smuzhiyun   *
345*4882a593Smuzhiyun   * This loads a CDROM from a specified slot in a changer, and displays
346*4882a593Smuzhiyun   * information about the changer status.  The drive should be unmounted before
347*4882a593Smuzhiyun   * using this program.
348*4882a593Smuzhiyun   *
349*4882a593Smuzhiyun   * Changer information is displayed if either the -v flag is specified
350*4882a593Smuzhiyun   * or no slot was specified.
351*4882a593Smuzhiyun   *
352*4882a593Smuzhiyun   * Based on code originally from Gerhard Zuber <zuber@berlin.snafu.de>.
353*4882a593Smuzhiyun   * Changer status information, and rewrite for the new Uniform CDROM driver
354*4882a593Smuzhiyun   * interface by Erik Andersen <andersee@debian.org>.
355*4882a593Smuzhiyun   */
356*4882a593Smuzhiyun
357*4882a593Smuzhiyun  #include <stdio.h>
358*4882a593Smuzhiyun  #include <stdlib.h>
359*4882a593Smuzhiyun  #include <errno.h>
360*4882a593Smuzhiyun  #include <string.h>
361*4882a593Smuzhiyun  #include <unistd.h>
362*4882a593Smuzhiyun  #include <fcntl.h>
363*4882a593Smuzhiyun  #include <sys/ioctl.h>
364*4882a593Smuzhiyun  #include <linux/cdrom.h>
365*4882a593Smuzhiyun
366*4882a593Smuzhiyun
367*4882a593Smuzhiyun  int
368*4882a593Smuzhiyun  main (int argc, char **argv)
369*4882a593Smuzhiyun  {
370*4882a593Smuzhiyun	char *program;
371*4882a593Smuzhiyun	char *device;
372*4882a593Smuzhiyun	int fd;           /* file descriptor for CD-ROM device */
373*4882a593Smuzhiyun	int status;       /* return status for system calls */
374*4882a593Smuzhiyun	int verbose = 0;
375*4882a593Smuzhiyun	int slot=-1, x_slot;
376*4882a593Smuzhiyun	int total_slots_available;
377*4882a593Smuzhiyun
378*4882a593Smuzhiyun	program = argv[0];
379*4882a593Smuzhiyun
380*4882a593Smuzhiyun	++argv;
381*4882a593Smuzhiyun	--argc;
382*4882a593Smuzhiyun
383*4882a593Smuzhiyun	if (argc < 1 || argc > 3) {
384*4882a593Smuzhiyun		fprintf (stderr, "usage: %s [-v] <device> [<slot>]\n",
385*4882a593Smuzhiyun			 program);
386*4882a593Smuzhiyun		fprintf (stderr, "       Slots are numbered 1 -- n.\n");
387*4882a593Smuzhiyun		exit (1);
388*4882a593Smuzhiyun	}
389*4882a593Smuzhiyun
390*4882a593Smuzhiyun       if (strcmp (argv[0], "-v") == 0) {
391*4882a593Smuzhiyun                verbose = 1;
392*4882a593Smuzhiyun                ++argv;
393*4882a593Smuzhiyun                --argc;
394*4882a593Smuzhiyun        }
395*4882a593Smuzhiyun
396*4882a593Smuzhiyun	device = argv[0];
397*4882a593Smuzhiyun
398*4882a593Smuzhiyun	if (argc == 2)
399*4882a593Smuzhiyun		slot = atoi (argv[1]) - 1;
400*4882a593Smuzhiyun
401*4882a593Smuzhiyun	/* open device */
402*4882a593Smuzhiyun	fd = open(device, O_RDONLY | O_NONBLOCK);
403*4882a593Smuzhiyun	if (fd < 0) {
404*4882a593Smuzhiyun		fprintf (stderr, "%s: open failed for `%s`: %s\n",
405*4882a593Smuzhiyun			 program, device, strerror (errno));
406*4882a593Smuzhiyun		exit (1);
407*4882a593Smuzhiyun	}
408*4882a593Smuzhiyun
409*4882a593Smuzhiyun	/* Check CD player status */
410*4882a593Smuzhiyun	total_slots_available = ioctl (fd, CDROM_CHANGER_NSLOTS);
411*4882a593Smuzhiyun	if (total_slots_available <= 1 ) {
412*4882a593Smuzhiyun		fprintf (stderr, "%s: Device `%s` is not an ATAPI "
413*4882a593Smuzhiyun			"compliant CD changer.\n", program, device);
414*4882a593Smuzhiyun		exit (1);
415*4882a593Smuzhiyun	}
416*4882a593Smuzhiyun
417*4882a593Smuzhiyun	if (slot >= 0) {
418*4882a593Smuzhiyun		if (slot >= total_slots_available) {
419*4882a593Smuzhiyun			fprintf (stderr, "Bad slot number.  "
420*4882a593Smuzhiyun				 "Should be 1 -- %d.\n",
421*4882a593Smuzhiyun				 total_slots_available);
422*4882a593Smuzhiyun			exit (1);
423*4882a593Smuzhiyun		}
424*4882a593Smuzhiyun
425*4882a593Smuzhiyun		/* load */
426*4882a593Smuzhiyun		slot=ioctl (fd, CDROM_SELECT_DISC, slot);
427*4882a593Smuzhiyun		if (slot<0) {
428*4882a593Smuzhiyun			fflush(stdout);
429*4882a593Smuzhiyun				perror ("CDROM_SELECT_DISC ");
430*4882a593Smuzhiyun			exit(1);
431*4882a593Smuzhiyun		}
432*4882a593Smuzhiyun	}
433*4882a593Smuzhiyun
434*4882a593Smuzhiyun	if (slot < 0 || verbose) {
435*4882a593Smuzhiyun
436*4882a593Smuzhiyun		status=ioctl (fd, CDROM_SELECT_DISC, CDSL_CURRENT);
437*4882a593Smuzhiyun		if (status<0) {
438*4882a593Smuzhiyun			fflush(stdout);
439*4882a593Smuzhiyun			perror (" CDROM_SELECT_DISC");
440*4882a593Smuzhiyun			exit(1);
441*4882a593Smuzhiyun		}
442*4882a593Smuzhiyun		slot=status;
443*4882a593Smuzhiyun
444*4882a593Smuzhiyun		printf ("Current slot: %d\n", slot+1);
445*4882a593Smuzhiyun		printf ("Total slots available: %d\n",
446*4882a593Smuzhiyun			total_slots_available);
447*4882a593Smuzhiyun
448*4882a593Smuzhiyun		printf ("Drive status: ");
449*4882a593Smuzhiyun                status = ioctl (fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
450*4882a593Smuzhiyun                if (status<0) {
451*4882a593Smuzhiyun                  perror(" CDROM_DRIVE_STATUS");
452*4882a593Smuzhiyun                } else switch(status) {
453*4882a593Smuzhiyun		case CDS_DISC_OK:
454*4882a593Smuzhiyun			printf ("Ready.\n");
455*4882a593Smuzhiyun			break;
456*4882a593Smuzhiyun		case CDS_TRAY_OPEN:
457*4882a593Smuzhiyun			printf ("Tray Open.\n");
458*4882a593Smuzhiyun			break;
459*4882a593Smuzhiyun		case CDS_DRIVE_NOT_READY:
460*4882a593Smuzhiyun			printf ("Drive Not Ready.\n");
461*4882a593Smuzhiyun			break;
462*4882a593Smuzhiyun		default:
463*4882a593Smuzhiyun			printf ("This Should not happen!\n");
464*4882a593Smuzhiyun			break;
465*4882a593Smuzhiyun		}
466*4882a593Smuzhiyun
467*4882a593Smuzhiyun		for (x_slot=0; x_slot<total_slots_available; x_slot++) {
468*4882a593Smuzhiyun			printf ("Slot %2d: ", x_slot+1);
469*4882a593Smuzhiyun			status = ioctl (fd, CDROM_DRIVE_STATUS, x_slot);
470*4882a593Smuzhiyun			if (status<0) {
471*4882a593Smuzhiyun			     perror(" CDROM_DRIVE_STATUS");
472*4882a593Smuzhiyun			} else switch(status) {
473*4882a593Smuzhiyun			case CDS_DISC_OK:
474*4882a593Smuzhiyun				printf ("Disc present.");
475*4882a593Smuzhiyun				break;
476*4882a593Smuzhiyun			case CDS_NO_DISC:
477*4882a593Smuzhiyun				printf ("Empty slot.");
478*4882a593Smuzhiyun				break;
479*4882a593Smuzhiyun			case CDS_TRAY_OPEN:
480*4882a593Smuzhiyun				printf ("CD-ROM tray open.\n");
481*4882a593Smuzhiyun				break;
482*4882a593Smuzhiyun			case CDS_DRIVE_NOT_READY:
483*4882a593Smuzhiyun				printf ("CD-ROM drive not ready.\n");
484*4882a593Smuzhiyun				break;
485*4882a593Smuzhiyun			case CDS_NO_INFO:
486*4882a593Smuzhiyun				printf ("No Information available.");
487*4882a593Smuzhiyun				break;
488*4882a593Smuzhiyun			default:
489*4882a593Smuzhiyun				printf ("This Should not happen!\n");
490*4882a593Smuzhiyun				break;
491*4882a593Smuzhiyun			}
492*4882a593Smuzhiyun		  if (slot == x_slot) {
493*4882a593Smuzhiyun                  status = ioctl (fd, CDROM_DISC_STATUS);
494*4882a593Smuzhiyun                  if (status<0) {
495*4882a593Smuzhiyun			perror(" CDROM_DISC_STATUS");
496*4882a593Smuzhiyun                  }
497*4882a593Smuzhiyun		  switch (status) {
498*4882a593Smuzhiyun			case CDS_AUDIO:
499*4882a593Smuzhiyun				printf ("\tAudio disc.\t");
500*4882a593Smuzhiyun				break;
501*4882a593Smuzhiyun			case CDS_DATA_1:
502*4882a593Smuzhiyun			case CDS_DATA_2:
503*4882a593Smuzhiyun				printf ("\tData disc type %d.\t", status-CDS_DATA_1+1);
504*4882a593Smuzhiyun				break;
505*4882a593Smuzhiyun			case CDS_XA_2_1:
506*4882a593Smuzhiyun			case CDS_XA_2_2:
507*4882a593Smuzhiyun				printf ("\tXA data disc type %d.\t", status-CDS_XA_2_1+1);
508*4882a593Smuzhiyun				break;
509*4882a593Smuzhiyun			default:
510*4882a593Smuzhiyun				printf ("\tUnknown disc type 0x%x!\t", status);
511*4882a593Smuzhiyun				break;
512*4882a593Smuzhiyun			}
513*4882a593Smuzhiyun			}
514*4882a593Smuzhiyun			status = ioctl (fd, CDROM_MEDIA_CHANGED, x_slot);
515*4882a593Smuzhiyun			if (status<0) {
516*4882a593Smuzhiyun				perror(" CDROM_MEDIA_CHANGED");
517*4882a593Smuzhiyun			}
518*4882a593Smuzhiyun			switch (status) {
519*4882a593Smuzhiyun			case 1:
520*4882a593Smuzhiyun				printf ("Changed.\n");
521*4882a593Smuzhiyun				break;
522*4882a593Smuzhiyun			default:
523*4882a593Smuzhiyun				printf ("\n");
524*4882a593Smuzhiyun				break;
525*4882a593Smuzhiyun			}
526*4882a593Smuzhiyun		}
527*4882a593Smuzhiyun	}
528*4882a593Smuzhiyun
529*4882a593Smuzhiyun	/* close device */
530*4882a593Smuzhiyun	status = close (fd);
531*4882a593Smuzhiyun	if (status != 0) {
532*4882a593Smuzhiyun		fprintf (stderr, "%s: close failed for `%s`: %s\n",
533*4882a593Smuzhiyun			 program, device, strerror (errno));
534*4882a593Smuzhiyun		exit (1);
535*4882a593Smuzhiyun	}
536*4882a593Smuzhiyun
537*4882a593Smuzhiyun	exit (0);
538*4882a593Smuzhiyun  }
539