File: /usr/src/linux/drivers/ide/ide-cd.c

1     /*
2      * linux/drivers/ide/ide-cd.c
3      *
4      * Copyright (C) 1994, 1995, 1996  scott snyder  <snyder@fnald0.fnal.gov>
5      * Copyright (C) 1996-1998  Erik Andersen <andersee@debian.org>
6      * Copyright (C) 1998-2000  Jens Axboe <axboe@suse.de>
7      *
8      * May be copied or modified under the terms of the GNU General Public
9      * License.  See linux/COPYING for more information.
10      *
11      * ATAPI CD-ROM driver.  To be used with ide.c.
12      * See Documentation/cdrom/ide-cd for usage information.
13      *
14      * Suggestions are welcome. Patches that work are more welcome though. ;-)
15      * For those wishing to work on this driver, please be sure you download
16      * and comply with the latest Mt. Fuji (SFF8090 version 4) and ATAPI 
17      * (SFF-8020i rev 2.6) standards. These documents can be obtained by 
18      * anonymous ftp from:
19      * ftp://fission.dt.wdc.com/pub/standards/SFF_atapi/spec/SFF8020-r2.6/PS/8020r26.ps
20      * ftp://ftp.avc-pioneer.com/Mtfuji4/Spec/Fuji4r10.pdf
21      *
22      * Drives that deviate from these standards will be accomodated as much
23      * as possible via compile time or command-line options.  Since I only have
24      * a few drives, you generally need to send me patches...
25      *
26      * ----------------------------------
27      * TO DO LIST:
28      * -Make it so that Pioneer CD DR-A24X and friends don't get screwed up on
29      *   boot
30      *
31      * ----------------------------------
32      * 1.00  Oct 31, 1994 -- Initial version.
33      * 1.01  Nov  2, 1994 -- Fixed problem with starting request in
34      *                       cdrom_check_status.
35      * 1.03  Nov 25, 1994 -- leaving unmask_intr[] as a user-setting (as for disks)
36      * (from mlord)       -- minor changes to cdrom_setup()
37      *                    -- renamed ide_dev_s to ide_drive_t, enable irq on command
38      * 2.00  Nov 27, 1994 -- Generalize packet command interface;
39      *                       add audio ioctls.
40      * 2.01  Dec  3, 1994 -- Rework packet command interface to handle devices
41      *                       which send an interrupt when ready for a command.
42      * 2.02  Dec 11, 1994 -- Cache the TOC in the driver.
43      *                       Don't use SCMD_PLAYAUDIO_TI; it's not included
44      *                       in the current version of ATAPI.
45      *                       Try to use LBA instead of track or MSF addressing
46      *                       when possible.
47      *                       Don't wait for READY_STAT.
48      * 2.03  Jan 10, 1995 -- Rewrite block read routines to handle block sizes
49      *                       other than 2k and to move multiple sectors in a
50      *                       single transaction.
51      * 2.04  Apr 21, 1995 -- Add work-around for Creative Labs CD220E drives.
52      *                       Thanks to Nick Saw <cwsaw@pts7.pts.mot.com> for
53      *                       help in figuring this out.  Ditto for Acer and
54      *                       Aztech drives, which seem to have the same problem.
55      * 2.04b May 30, 1995 -- Fix to match changes in ide.c version 3.16 -ml
56      * 2.05  Jun  8, 1995 -- Don't attempt to retry after an illegal request
57      *                        or data protect error.
58      *                       Use HWIF and DEV_HWIF macros as in ide.c.
59      *                       Always try to do a request_sense after
60      *                        a failed command.
61      *                       Include an option to give textual descriptions
62      *                        of ATAPI errors.
63      *                       Fix a bug in handling the sector cache which
64      *                        showed up if the drive returned data in 512 byte
65      *                        blocks (like Pioneer drives).  Thanks to
66      *                        Richard Hirst <srh@gpt.co.uk> for diagnosing this.
67      *                       Properly supply the page number field in the
68      *                        MODE_SELECT command.
69      *                       PLAYAUDIO12 is broken on the Aztech; work around it.
70      * 2.05x Aug 11, 1995 -- lots of data structure renaming/restructuring in ide.c
71      *                       (my apologies to Scott, but now ide-cd.c is independent)
72      * 3.00  Aug 22, 1995 -- Implement CDROMMULTISESSION ioctl.
73      *                       Implement CDROMREADAUDIO ioctl (UNTESTED).
74      *                       Use input_ide_data() and output_ide_data().
75      *                       Add door locking.
76      *                       Fix usage count leak in cdrom_open, which happened
77      *                        when a read-write mount was attempted.
78      *                       Try to load the disk on open.
79      *                       Implement CDROMEJECT_SW ioctl (off by default).
80      *                       Read total cdrom capacity during open.
81      *                       Rearrange logic in cdrom_decode_status.  Issue
82      *                        request sense commands for failed packet commands
83      *                        from here instead of from cdrom_queue_packet_command.
84      *                        Fix a race condition in retrieving error information.
85      *                       Suppress printing normal unit attention errors and
86      *                        some drive not ready errors.
87      *                       Implement CDROMVOLREAD ioctl.
88      *                       Implement CDROMREADMODE1/2 ioctls.
89      *                       Fix race condition in setting up interrupt handlers
90      *                        when the `serialize' option is used.
91      * 3.01  Sep  2, 1995 -- Fix ordering of reenabling interrupts in
92      *                        cdrom_queue_request.
93      *                       Another try at using ide_[input,output]_data.
94      * 3.02  Sep 16, 1995 -- Stick total disk capacity in partition table as well.
95      *                       Make VERBOSE_IDE_CD_ERRORS dump failed command again.
96      *                       Dump out more information for ILLEGAL REQUEST errs.
97      *                       Fix handling of errors occurring before the
98      *                        packet command is transferred.
99      *                       Fix transfers with odd bytelengths.
100      * 3.03  Oct 27, 1995 -- Some Creative drives have an id of just `CD'.
101      *                       `DCI-2S10' drives are broken too.
102      * 3.04  Nov 20, 1995 -- So are Vertos drives.
103      * 3.05  Dec  1, 1995 -- Changes to go with overhaul of ide.c and ide-tape.c
104      * 3.06  Dec 16, 1995 -- Add support needed for partitions.
105      *                       More workarounds for Vertos bugs (based on patches
106      *                        from Holger Dietze <dietze@aix520.informatik.uni-leipzig.de>).
107      *                       Try to eliminate byteorder assumptions.
108      *                       Use atapi_cdrom_subchnl struct definition.
109      *                       Add STANDARD_ATAPI compilation option.
110      * 3.07  Jan 29, 1996 -- More twiddling for broken drives: Sony 55D,
111      *                        Vertos 300.
112      *                       Add NO_DOOR_LOCKING configuration option.
113      *                       Handle drive_cmd requests w/NULL args (for hdparm -t).
114      *                       Work around sporadic Sony55e audio play problem.
115      * 3.07a Feb 11, 1996 -- check drive->id for NULL before dereferencing, to fix
116      *                        problem with "hde=cdrom" with no drive present.  -ml
117      * 3.08  Mar  6, 1996 -- More Vertos workarounds.
118      * 3.09  Apr  5, 1996 -- Add CDROMCLOSETRAY ioctl.
119      *                       Switch to using MSF addressing for audio commands.
120      *                       Reformat to match kernel tabbing style.
121      *                       Add CDROM_GET_UPC ioctl.
122      * 3.10  Apr 10, 1996 -- Fix compilation error with STANDARD_ATAPI.
123      * 3.11  Apr 29, 1996 -- Patch from Heiko Eissfeldt <heiko@colossus.escape.de>
124      *                       to remove redundant verify_area calls.
125      * 3.12  May  7, 1996 -- Rudimentary changer support.  Based on patches
126      *                        from Gerhard Zuber <zuber@berlin.snafu.de>.
127      *                       Let open succeed even if there's no loaded disc.
128      * 3.13  May 19, 1996 -- Fixes for changer code.
129      * 3.14  May 29, 1996 -- Add work-around for Vertos 600.
130      *                        (From Hennus Bergman <hennus@sky.ow.nl>.)
131      * 3.15  July 2, 1996 -- Added support for Sanyo 3 CD changers
132      *                        from Ben Galliart <bgallia@luc.edu> with 
133      *                        special help from Jeff Lightfoot 
134      *                        <jeffml@pobox.com>
135      * 3.15a July 9, 1996 -- Improved Sanyo 3 CD changer identification
136      * 3.16  Jul 28, 1996 -- Fix from Gadi to reduce kernel stack usage for ioctl.
137      * 3.17  Sep 17, 1996 -- Tweak audio reads for some drives.
138      *                       Start changing CDROMLOADFROMSLOT to CDROM_SELECT_DISC.
139      * 3.18  Oct 31, 1996 -- Added module and DMA support.
140      *                       
141      *                       
142      * 4.00  Nov 5, 1996   -- New ide-cd maintainer,
143      *                                 Erik B. Andersen <andersee@debian.org>
144      *                     -- Newer Creative drives don't always set the error
145      *                          register correctly.  Make sure we see media changes
146      *                          regardless.
147      *                     -- Integrate with generic cdrom driver.
148      *                     -- CDROMGETSPINDOWN and CDROMSETSPINDOWN ioctls, based on
149      *                          a patch from Ciro Cattuto <>.
150      *                     -- Call set_device_ro.
151      *                     -- Implement CDROMMECHANISMSTATUS and CDROMSLOTTABLE
152      *                          ioctls, based on patch by Erik Andersen
153      *                     -- Add some probes of drive capability during setup.
154      *
155      * 4.01  Nov 11, 1996  -- Split into ide-cd.c and ide-cd.h
156      *                     -- Removed CDROMMECHANISMSTATUS and CDROMSLOTTABLE 
157      *                          ioctls in favor of a generalized approach 
158      *                          using the generic cdrom driver.
159      *                     -- Fully integrated with the 2.1.X kernel.
160      *                     -- Other stuff that I forgot (lots of changes)
161      *
162      * 4.02  Dec 01, 1996  -- Applied patch from Gadi Oxman <gadio@netvision.net.il>
163      *                          to fix the drive door locking problems.
164      *
165      * 4.03  Dec 04, 1996  -- Added DSC overlap support.
166      * 4.04  Dec 29, 1996  -- Added CDROMREADRAW ioclt based on patch 
167      *                          by Ales Makarov (xmakarov@sun.felk.cvut.cz)
168      *
169      * 4.05  Nov 20, 1997  -- Modified to print more drive info on init
170      *                        Minor other changes
171      *                        Fix errors on CDROMSTOP (If you have a "Dolphin",
172      *                          you must define IHAVEADOLPHIN)
173      *                        Added identifier so new Sanyo CD-changer works
174      *                        Better detection if door locking isn't supported
175      *
176      * 4.06  Dec 17, 1997  -- fixed endless "tray open" messages  -ml
177      * 4.07  Dec 17, 1997  -- fallback to set pc->stat on "tray open"
178      * 4.08  Dec 18, 1997  -- spew less noise when tray is empty
179      *                     -- fix speed display for ACER 24X, 18X
180      * 4.09  Jan 04, 1998  -- fix handling of the last block so we return
181      *                         an end of file instead of an I/O error (Gadi)
182      * 4.10  Jan 24, 1998  -- fixed a bug so now changers can change to a new
183      *                         slot when there is no disc in the current slot.
184      *                     -- Fixed a memory leak where info->changer_info was
185      *                         malloc'ed but never free'd when closing the device.
186      *                     -- Cleaned up the global namespace a bit by making more
187      *                         functions static that should already have been.
188      * 4.11  Mar 12, 1998  -- Added support for the CDROM_SELECT_SPEED ioctl
189      *                         based on a patch for 2.0.33 by Jelle Foks 
190      *                         <jelle@scintilla.utwente.nl>, a patch for 2.0.33
191      *                         by Toni Giorgino <toni@pcape2.pi.infn.it>, the SCSI
192      *                         version, and my own efforts.  -erik
193      *                     -- Fixed a stupid bug which egcs was kind enough to
194      *                         inform me of where "Illegal mode for this track"
195      *                         was never returned due to a comparison on data
196      *                         types of limited range.
197      * 4.12  Mar 29, 1998  -- Fixed bug in CDROM_SELECT_SPEED so write speed is 
198      *                         now set ionly for CD-R and CD-RW drives.  I had 
199      *                         removed this support because it produced errors.
200      *                         It produced errors _only_ for non-writers. duh.
201      * 4.13  May 05, 1998  -- Suppress useless "in progress of becoming ready"
202      *                         messages, since this is not an error.
203      *                     -- Change error messages to be const
204      *                     -- Remove a "\t" which looks ugly in the syslogs
205      * 4.14  July 17, 1998 -- Change to pointing to .ps version of ATAPI spec
206      *                         since the .pdf version doesn't seem to work...
207      *                     -- Updated the TODO list to something more current.
208      *
209      * 4.15  Aug 25, 1998  -- Updated ide-cd.h to respect mechine endianess, 
210      *                         patch thanks to "Eddie C. Dost" <ecd@skynet.be>
211      *
212      * 4.50  Oct 19, 1998  -- New maintainers!
213      *                         Jens Axboe <axboe@image.dk>
214      *                         Chris Zwilling <chris@cloudnet.com>
215      *
216      * 4.51  Dec 23, 1998  -- Jens Axboe <axboe@image.dk>
217      *                      - ide_cdrom_reset enabled since the ide subsystem
218      *                         handles resets fine now. <axboe@image.dk>
219      *                      - Transfer size fix for Samsung CD-ROMs, thanks to
220      *                        "Ville Hallik" <ville.hallik@mail.ee>.
221      *                      - other minor stuff.
222      *
223      * 4.52  Jan 19, 1999  -- Jens Axboe <axboe@image.dk>
224      *                      - Detect DVD-ROM/RAM drives
225      *
226      * 4.53  Feb 22, 1999   - Include other model Samsung and one Goldstar
227      *                         drive in transfer size limit.
228      *                      - Fix the I/O error when doing eject without a medium
229      *                         loaded on some drives.
230      *                      - CDROMREADMODE2 is now implemented through
231      *                         CDROMREADRAW, since many drives don't support
232      *                         MODE2 (even though ATAPI 2.6 says they must).
233      *                      - Added ignore parameter to ide-cd (as a module), eg
234      *                         	insmod ide-cd ignore='hda hdb'
235      *                         Useful when using ide-cd in conjunction with
236      *                         ide-scsi. TODO: non-modular way of doing the
237      *                         same.
238      *
239      * 4.54  Aug 5, 1999	- Support for MMC2 class commands through the generic
240      *			  packet interface to cdrom.c.
241      *			- Unified audio ioctl support, most of it.
242      *			- cleaned up various deprecated verify_area().
243      *			- Added ide_cdrom_packet() as the interface for
244      *			  the Uniform generic_packet().
245      *			- bunch of other stuff, will fill in logs later.
246      *			- report 1 slot for non-changers, like the other
247      *			  cd-rom drivers. don't report select disc for
248      *			  non-changers as well.
249      *			- mask out audio playing, if the device can't do it.
250      *
251      * 4.55  Sep 1, 1999	- Eliminated the rest of the audio ioctls, except
252      *			  for CDROMREADTOC[ENTRY|HEADER]. Some of the drivers
253      *			  use this independently of the actual audio handling.
254      *			  They will disappear later when I get the time to
255      *			  do it cleanly.
256      *			- Minimize the TOC reading - only do it when we
257      *			  know a media change has occurred.
258      *			- Moved all the CDROMREADx ioctls to the Uniform layer.
259      *			- Heiko Eissfeldt <heiko@colossus.escape.de> supplied
260      *			  some fixes for CDI.
261      *			- CD-ROM leaving door locked fix from Andries
262      *			  Brouwer <Andries.Brouwer@cwi.nl>
263      *			- Erik Andersen <andersen@xmission.com> unified
264      *			  commands across the various drivers and how
265      *			  sense errors are handled.
266      *
267      * 4.56  Sep 12, 1999	- Removed changer support - it is now in the
268      *			  Uniform layer.
269      *			- Added partition based multisession handling.
270      *			- Mode sense and mode select moved to the
271      *			  Uniform layer.
272      *			- Fixed a problem with WPI CDS-32X drive - it
273      *			  failed the capabilities 
274      *
275      * 4.57  Apr 7, 2000	- Fixed sense reporting.
276      *			- Fixed possible oops in ide_cdrom_get_last_session()
277      *			- Fix locking mania and make ide_cdrom_reset relock
278      *			- Stop spewing errors to log when magicdev polls with
279      *			  TEST_UNIT_READY on some drives.
280      *			- Various fixes from Tobias Ringstrom:
281      *			  tray if it was locked prior to the reset.
282      *			  - cdrom_read_capacity returns one frame too little.
283      *			  - Fix real capacity reporting.
284      *
285      * 4.58  May 1, 2000	- Clean up ACER50 stuff.
286      *			- Fix small problem with ide_cdrom_capacity
287      *
288      * 4.59  Aug 11, 2000	- Fix changer problem in cdrom_read_toc, we weren't
289      *			  correctly sensing a disc change.
290      *			- Rearranged some code
291      *			- Use extended sense on drives that support it for
292      *			  correctly reporting tray status -- from
293      *			  Michael D Johnson <johnsom@orst.edu>
294      *
295      *************************************************************************/
296      
297     #define IDECD_VERSION "4.59"
298     
299     #include <linux/config.h>
300     #include <linux/module.h>
301     #include <linux/types.h>
302     #include <linux/kernel.h>
303     #include <linux/delay.h>
304     #include <linux/timer.h>
305     #include <linux/slab.h>
306     #include <linux/interrupt.h>
307     #include <linux/errno.h>
308     #include <linux/cdrom.h>
309     #include <linux/ide.h>
310     #include <linux/completion.h>
311     
312     #include <asm/irq.h>
313     #include <asm/io.h>
314     #include <asm/byteorder.h>
315     #include <asm/uaccess.h>
316     #include <asm/unaligned.h>
317     
318     #include "ide-cd.h"
319     
320     /****************************************************************************
321      * Generic packet command support and error handling routines.
322      */
323     
324     /* Mark that we've seen a media change, and invalidate our internal
325        buffers. */
326     static void cdrom_saw_media_change (ide_drive_t *drive)
327     {
328     	struct cdrom_info *info = drive->driver_data;
329     	
330     	CDROM_STATE_FLAGS (drive)->media_changed = 1;
331     	CDROM_STATE_FLAGS (drive)->toc_valid = 0;
332     	info->nsectors_buffered = 0;
333     }
334     
335     static int cdrom_log_sense(ide_drive_t *drive, struct packet_command *pc,
336     			   struct request_sense *sense)
337     {
338     	int log = 0;
339     
340     	if (sense == NULL || pc == NULL || pc->quiet)
341     		return 0;
342     
343     	switch (sense->sense_key) {
344     		case NO_SENSE: case RECOVERED_ERROR:
345     			break;
346     		case NOT_READY:
347     			/*
348     			 * don't care about tray state messages for
349     			 * e.g. capacity commands or in-progress or
350     			 * becoming ready
351     			 */
352     			if (sense->asc == 0x3a || sense->asc == 0x04)
353     				break;
354     			log = 1;
355     			break;
356     		case UNIT_ATTENTION:
357     			/*
358     			 * Make good and sure we've seen this potential media
359     			 * change. Some drives (i.e. Creative) fail to present
360     			 * the correct sense key in the error register.
361     			 */
362     			cdrom_saw_media_change(drive);
363     			break;
364     		default:
365     			log = 1;
366     			break;
367     	}
368     	return log;
369     }
370     
371     static
372     void cdrom_analyze_sense_data(ide_drive_t *drive,
373     			      struct packet_command *failed_command,
374     			      struct request_sense *sense)
375     {
376     
377     	if (!cdrom_log_sense(drive, failed_command, sense))
378     		return;
379     
380     	/*
381     	 * If a read toc is executed for a CD-R or CD-RW medium where
382     	 * the first toc has not been recorded yet, it will fail with
383     	 * 05/24/00 (which is a confusing error)
384     	 */
385     	if (failed_command && failed_command->c[0] == GPCMD_READ_TOC_PMA_ATIP)
386     		if (sense->sense_key == 0x05 && sense->asc == 0x24)
387     			return;
388     
389     #if VERBOSE_IDE_CD_ERRORS
390     	{
391     		int i;
392     		const char *s;
393     		char buf[80];
394     
395     		printk ("ATAPI device %s:\n", drive->name);
396     		if (sense->error_code==0x70)
397     			printk("  Error: ");
398     		else if (sense->error_code==0x71)
399     			printk("  Deferred Error: ");
400     		else if (sense->error_code == 0x7f)
401     			printk("  Vendor-specific Error: ");
402     		else
403     			printk("  Unknown Error Type: ");
404     
405     		if (sense->sense_key < ARY_LEN(sense_key_texts))
406     			s = sense_key_texts[sense->sense_key];
407     		else
408     			s = "bad sense key!";
409     
410     		printk("%s -- (Sense key=0x%02x)\n", s, sense->sense_key);
411     
412     		if (sense->asc == 0x40) {
413     			sprintf(buf, "Diagnostic failure on component 0x%02x",
414     				 sense->ascq);
415     			s = buf;
416     		} else {
417     			int lo = 0, mid, hi = ARY_LEN(sense_data_texts);
418     			unsigned long key = (sense->sense_key << 16);
419     			key |= (sense->asc << 8);
420     			if (!(sense->ascq >= 0x80 && sense->ascq <= 0xdd))
421     				key |= sense->ascq;
422     			s = NULL;
423     
424     			while (hi > lo) {
425     				mid = (lo + hi) / 2;
426     				if (sense_data_texts[mid].asc_ascq == key ||
427     				    sense_data_texts[mid].asc_ascq == (0xff0000|key)) {
428     					s = sense_data_texts[mid].text;
429     					break;
430     				}
431     				else if (sense_data_texts[mid].asc_ascq > key)
432     					hi = mid;
433     				else
434     					lo = mid+1;
435     			}
436     		}
437     
438     		if (s == NULL) {
439     			if (sense->asc > 0x80)
440     				s = "(vendor-specific error)";
441     			else
442     				s = "(reserved error code)";
443     		}
444     
445     		printk("  %s -- (asc=0x%02x, ascq=0x%02x)\n",
446     			s, sense->asc, sense->ascq);
447     
448     		if (failed_command != NULL) {
449     
450     			int lo=0, mid, hi= ARY_LEN (packet_command_texts);
451     			s = NULL;
452     
453     			while (hi > lo) {
454     				mid = (lo + hi) / 2;
455     				if (packet_command_texts[mid].packet_command ==
456     				    failed_command->c[0]) {
457     					s = packet_command_texts[mid].text;
458     					break;
459     				}
460     				if (packet_command_texts[mid].packet_command >
461     				    failed_command->c[0])
462     					hi = mid;
463     				else
464     					lo = mid+1;
465     			}
466     
467     			printk ("  The failed \"%s\" packet command was: \n  \"", s);
468     			for (i=0; i<sizeof (failed_command->c); i++)
469     				printk ("%02x ", failed_command->c[i]);
470     			printk ("\"\n");
471     		}
472     
473     		/* The SKSV bit specifies validity of the sense_key_specific
474     		 * in the next two commands. It is bit 7 of the first byte.
475     		 * In the case of NOT_READY, if SKSV is set the drive can
476     		 * give us nice ETA readings.
477     		 */
478     		if (sense->sense_key == NOT_READY && (sense->sks[0] & 0x80)) {
479     			int progress = (sense->sks[1] << 8 | sense->sks[2]) * 100;
480     			printk("  Command is %02d%% complete\n", progress / 0xffff);
481     
482     		}
483     
484     		if (sense->sense_key == ILLEGAL_REQUEST &&
485     		    (sense->sks[0] & 0x80) != 0) {
486     			printk("  Error in %s byte %d",
487     				(sense->sks[0] & 0x40) != 0 ?
488     				"command packet" : "command data",
489     				(sense->sks[1] << 8) + sense->sks[2]);
490     
491     			if ((sense->sks[0] & 0x40) != 0)
492     				printk (" bit %d", sense->sks[0] & 0x07);
493     
494     			printk ("\n");
495     		}
496     	}
497     
498     #else /* not VERBOSE_IDE_CD_ERRORS */
499     
500     	/* Suppress printing unit attention and `in progress of becoming ready'
501     	   errors when we're not being verbose. */
502     
503     	if (sense->sense_key == UNIT_ATTENTION ||
504     	    (sense->sense_key == NOT_READY && (sense->asc == 4 ||
505     						sense->asc == 0x3a)))
506     		return;
507     
508     	printk("%s: error code: 0x%02x  sense_key: 0x%02x  asc: 0x%02x  ascq: 0x%02x\n",
509     		drive->name,
510     		sense->error_code, sense->sense_key,
511     		sense->asc, sense->ascq);
512     #endif /* not VERBOSE_IDE_CD_ERRORS */
513     }
514     
515     static void cdrom_queue_request_sense(ide_drive_t *drive, 
516     				      struct completion *wait,
517     				      struct request_sense *sense,
518     				      struct packet_command *failed_command)
519     {
520     	struct cdrom_info *info		= drive->driver_data;
521     	struct packet_command *pc	= &info->request_sense_pc;
522     	struct request *rq;
523     
524     	if (sense == NULL)
525     		sense = &info->sense_data;
526     
527     	memset(pc, 0, sizeof(struct packet_command));
528     	pc->c[0] = GPCMD_REQUEST_SENSE;
529     	pc->c[4] = pc->buflen = 18;
530     	pc->buffer = (char *) sense;
531     	pc->sense = (struct request_sense *) failed_command;
532     
533     	/* stuff the sense request in front of our current request */
534     	rq = &info->request_sense_request;
535     	ide_init_drive_cmd(rq);
536     	rq->cmd = REQUEST_SENSE_COMMAND;
537     	rq->buffer = (char *) pc;
538     	rq->waiting = wait;
539     	(void) ide_do_drive_cmd(drive, rq, ide_preempt);
540     }
541     
542     
543     static void cdrom_end_request (int uptodate, ide_drive_t *drive)
544     {
545     	struct request *rq = HWGROUP(drive)->rq;
546     
547     	if (rq->cmd == REQUEST_SENSE_COMMAND && uptodate) {
548     		struct packet_command *pc = (struct packet_command *) rq->buffer;
549     		cdrom_analyze_sense_data(drive,
550     			(struct packet_command *) pc->sense,
551     			(struct request_sense *) (pc->buffer - pc->c[4]));
552     	}
553     	if (rq->cmd == READ || rq->cmd == WRITE)
554     		if (!rq->current_nr_sectors)
555     			uptodate = 1;
556     
557     	ide_end_request (uptodate, HWGROUP(drive));
558     }
559     
560     
561     /* Returns 0 if the request should be continued.
562        Returns 1 if the request was ended. */
563     static int cdrom_decode_status (ide_startstop_t *startstop, ide_drive_t *drive,
564     				int good_stat, int *stat_ret)
565     {
566     	struct request *rq = HWGROUP(drive)->rq;
567     	int stat, err, sense_key;
568     	struct packet_command *pc;
569     	
570     	/* Check for errors. */
571     	stat = GET_STAT();
572     	*stat_ret = stat;
573     
574     	if (OK_STAT (stat, good_stat, BAD_R_STAT))
575     		return 0;
576     
577     	/* Get the IDE error register. */
578     	err = GET_ERR();
579     	sense_key = err >> 4;
580     
581     	if (rq == NULL) {
582     		printk("%s: missing rq in cdrom_decode_status\n", drive->name);
583     		*startstop = ide_stopped;
584     		return 1;
585     	}
586     
587     	if (rq->cmd == REQUEST_SENSE_COMMAND) {
588     		/* We got an error trying to get sense info
589     		   from the drive (probably while trying
590     		   to recover from a former error).  Just give up. */
591     
592     		pc = (struct packet_command *) rq->buffer;
593     		pc->stat = 1;
594     		cdrom_end_request (1, drive);
595     		*startstop = ide_error (drive, "request sense failure", stat);
596     		return 1;
597     
598     	} else if (rq->cmd == PACKET_COMMAND) {
599     		/* All other functions, except for READ. */
600     		struct completion *wait = NULL;
601     		pc = (struct packet_command *) rq->buffer;
602     
603     		/* Check for tray open. */
604     		if (sense_key == NOT_READY) {
605     			cdrom_saw_media_change (drive);
606     		} else if (sense_key == UNIT_ATTENTION) {
607     			/* Check for media change. */
608     			cdrom_saw_media_change (drive);
609     			/*printk("%s: media changed\n",drive->name);*/
610     			return 0;
611     		} else if (!pc->quiet) {
612     			/* Otherwise, print an error. */
613     			ide_dump_status(drive, "packet command error", stat);
614     		}
615     		
616     		/* Set the error flag and complete the request.
617     		   Then, if we have a CHECK CONDITION status,
618     		   queue a request sense command.  We must be careful,
619     		   though: we don't want the thread in
620     		   cdrom_queue_packet_command to wake up until
621     		   the request sense has completed.  We do this
622     		   by transferring the semaphore from the packet
623     		   command request to the request sense request. */
624     
625     		if ((stat & ERR_STAT) != 0) {
626     			wait = rq->waiting;
627     			rq->waiting = NULL;
628     		}
629     
630     		pc->stat = 1;
631     		cdrom_end_request (1, drive);
632     
633     		if ((stat & ERR_STAT) != 0)
634     			cdrom_queue_request_sense(drive, wait, pc->sense, pc);
635     	} else {
636     		/* Handle errors from READ and WRITE requests. */
637     
638     		if (sense_key == NOT_READY) {
639     			/* Tray open. */
640     			cdrom_saw_media_change (drive);
641     
642     			/* Fail the request. */
643     			printk ("%s: tray open\n", drive->name);
644     			cdrom_end_request (0, drive);
645     		} else if (sense_key == UNIT_ATTENTION) {
646     			/* Media change. */
647     			cdrom_saw_media_change (drive);
648     
649     			/* Arrange to retry the request.
650     			   But be sure to give up if we've retried
651     			   too many times. */
652     			if (++rq->errors > ERROR_MAX)
653     				cdrom_end_request (0, drive);
654     		} else if (sense_key == ILLEGAL_REQUEST ||
655     			   sense_key == DATA_PROTECT) {
656     			/* No point in retrying after an illegal
657     			   request or data protect error.*/
658     			ide_dump_status (drive, "command error", stat);
659     			cdrom_end_request (0, drive);
660     		} else if ((err & ~ABRT_ERR) != 0) {
661     			/* Go to the default handler
662     			   for other errors. */
663     			*startstop = ide_error (drive, "cdrom_decode_status", stat);
664     			return 1;
665     		} else if ((++rq->errors > ERROR_MAX)) {
666     			/* We've racked up too many retries.  Abort. */
667     			cdrom_end_request (0, drive);
668     		}
669     
670     		/* If we got a CHECK_CONDITION status,
671     		   queue a request sense command. */
672     		if ((stat & ERR_STAT) != 0)
673     			cdrom_queue_request_sense(drive, NULL, NULL, NULL);
674     	}
675     
676     	/* Retry, or handle the next request. */
677     	*startstop = ide_stopped;
678     	return 1;
679     }
680     
681     static int cdrom_timer_expiry(ide_drive_t *drive)
682     {
683     	struct request *rq = HWGROUP(drive)->rq;
684     	struct packet_command *pc = (struct packet_command *) rq->buffer;
685     	unsigned long wait = 0;
686     
687     	/*
688     	 * Some commands are *slow* and normally take a long time to
689     	 * complete. Usually we can use the ATAPI "disconnect" to bypass
690     	 * this, but not all commands/drives support that. Let
691     	 * ide_timer_expiry keep polling us for these.
692     	 */
693     	switch (pc->c[0]) {
694     		case GPCMD_BLANK:
695     		case GPCMD_FORMAT_UNIT:
696     		case GPCMD_RESERVE_RZONE_TRACK:
697     			wait = WAIT_CMD;
698     			break;
699     		default:
700     			wait = 0;
701     			break;
702     	}
703     	return wait;
704     }
705     
706     /* Set up the device registers for transferring a packet command on DEV,
707        expecting to later transfer XFERLEN bytes.  HANDLER is the routine
708        which actually transfers the command to the drive.  If this is a
709        drq_interrupt device, this routine will arrange for HANDLER to be
710        called when the interrupt from the drive arrives.  Otherwise, HANDLER
711        will be called immediately after the drive is prepared for the transfer. */
712     
713     static ide_startstop_t cdrom_start_packet_command(ide_drive_t *drive,
714     						  int xferlen,
715     						  ide_handler_t *handler)
716     {
717     	ide_startstop_t startstop;
718     	struct cdrom_info *info = drive->driver_data;
719     
720     	/* Wait for the controller to be idle. */
721     	if (ide_wait_stat(&startstop, drive, 0, BUSY_STAT, WAIT_READY))
722     		return startstop;
723     
724     	if (info->dma) {
725     		if (info->cmd == READ) {
726     			info->dma = !HWIF(drive)->dmaproc(ide_dma_read, drive);
727     		} else if (info->cmd == WRITE) {
728     			info->dma = !HWIF(drive)->dmaproc(ide_dma_write, drive);
729     		} else {
730     			printk("ide-cd: DMA set, but not allowed\n");
731     		}
732     	}
733     
734     	/* Set up the controller registers. */
735     	OUT_BYTE (info->dma, IDE_FEATURE_REG);
736     	OUT_BYTE (0, IDE_NSECTOR_REG);
737     	OUT_BYTE (0, IDE_SECTOR_REG);
738     
739     	OUT_BYTE (xferlen & 0xff, IDE_LCYL_REG);
740     	OUT_BYTE (xferlen >> 8  , IDE_HCYL_REG);
741     	if (IDE_CONTROL_REG)
742     		OUT_BYTE (drive->ctl, IDE_CONTROL_REG);
743      
744     	if (info->dma)
745     		(void) (HWIF(drive)->dmaproc(ide_dma_begin, drive));
746     
747     	if (CDROM_CONFIG_FLAGS (drive)->drq_interrupt) {
748     		ide_set_handler (drive, handler, WAIT_CMD, cdrom_timer_expiry);
749     		OUT_BYTE (WIN_PACKETCMD, IDE_COMMAND_REG); /* packet command */
750     		return ide_started;
751     	} else {
752     		OUT_BYTE (WIN_PACKETCMD, IDE_COMMAND_REG); /* packet command */
753     		return (*handler) (drive);
754     	}
755     }
756     
757     /* Send a packet command to DRIVE described by CMD_BUF and CMD_LEN.
758        The device registers must have already been prepared
759        by cdrom_start_packet_command.
760        HANDLER is the interrupt handler to call when the command completes
761        or there's data ready. */
762     /*
763      * changed 5 parameters to 3 for dvd-ram
764      * struct packet_command *pc; now packet_command_t *pc;
765      */
766     static ide_startstop_t cdrom_transfer_packet_command (ide_drive_t *drive,
767     					  struct packet_command *pc,
768     					  ide_handler_t *handler)
769     {
770     	unsigned char *cmd_buf	= pc->c;
771     	int cmd_len		= sizeof(pc->c);
772     	unsigned int timeout	= pc->timeout;
773     	ide_startstop_t startstop;
774     
775     	if (CDROM_CONFIG_FLAGS (drive)->drq_interrupt) {
776     		/* Here we should have been called after receiving an interrupt
777     		   from the device.  DRQ should how be set. */
778     		int stat_dum;
779     
780     		/* Check for errors. */
781     		if (cdrom_decode_status (&startstop, drive, DRQ_STAT, &stat_dum))
782     			return startstop;
783     	} else {
784     		/* Otherwise, we must wait for DRQ to get set. */
785     		if (ide_wait_stat (&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY))
786     			return startstop;
787     	}
788     
789     	/* Arm the interrupt handler. */
790     	ide_set_handler (drive, handler, timeout, cdrom_timer_expiry);
791     
792     	/* Send the command to the device. */
793     	atapi_output_bytes (drive, cmd_buf, cmd_len);
794     	return ide_started;
795     }
796     
797     /****************************************************************************
798      * Block read functions.
799      */
800     
801     /*
802      * Buffer up to SECTORS_TO_TRANSFER sectors from the drive in our sector
803      * buffer.  Once the first sector is added, any subsequent sectors are
804      * assumed to be continuous (until the buffer is cleared).  For the first
805      * sector added, SECTOR is its sector number.  (SECTOR is then ignored until
806      * the buffer is cleared.)
807      */
808     static void cdrom_buffer_sectors (ide_drive_t *drive, unsigned long sector,
809                                       int sectors_to_transfer)
810     {
811     	struct cdrom_info *info = drive->driver_data;
812     
813     	/* Number of sectors to read into the buffer. */
814     	int sectors_to_buffer = MIN (sectors_to_transfer,
815     				     (SECTOR_BUFFER_SIZE >> SECTOR_BITS) -
816     				       info->nsectors_buffered);
817     
818     	char *dest;
819     
820     	/* If we couldn't get a buffer, don't try to buffer anything... */
821     	if (info->buffer == NULL)
822     		sectors_to_buffer = 0;
823     
824     	/* If this is the first sector in the buffer, remember its number. */
825     	if (info->nsectors_buffered == 0)
826     		info->sector_buffered = sector;
827     
828     	/* Read the data into the buffer. */
829     	dest = info->buffer + info->nsectors_buffered * SECTOR_SIZE;
830     	while (sectors_to_buffer > 0) {
831     		atapi_input_bytes (drive, dest, SECTOR_SIZE);
832     		--sectors_to_buffer;
833     		--sectors_to_transfer;
834     		++info->nsectors_buffered;
835     		dest += SECTOR_SIZE;
836     	}
837     
838     	/* Throw away any remaining data. */
839     	while (sectors_to_transfer > 0) {
840     		char dum[SECTOR_SIZE];
841     		atapi_input_bytes (drive, dum, sizeof (dum));
842     		--sectors_to_transfer;
843     	}
844     }
845     
846     /*
847      * Check the contents of the interrupt reason register from the cdrom
848      * and attempt to recover if there are problems.  Returns  0 if everything's
849      * ok; nonzero if the request has been terminated.
850      */
851     static inline
852     int cdrom_read_check_ireason (ide_drive_t *drive, int len, int ireason)
853     {
854     	ireason &= 3;
855     	if (ireason == 2) return 0;
856     
857     	if (ireason == 0) {
858     		/* Whoops... The drive is expecting to receive data from us! */
859     		printk ("%s: cdrom_read_intr: "
860     			"Drive wants to transfer data the wrong way!\n",
861     			drive->name);
862     
863     		/* Throw some data at the drive so it doesn't hang
864     		   and quit this request. */
865     		while (len > 0) {
866     			int dum = 0;
867     			atapi_output_bytes (drive, &dum, sizeof (dum));
868     			len -= sizeof (dum);
869     		}
870     	} else  if (ireason == 1) {
871     		/* Some drives (ASUS) seem to tell us that status
872     		 * info is available. just get it and ignore.
873     		 */
874     		GET_STAT();
875     		return 0;
876     	} else {
877     		/* Drive wants a command packet, or invalid ireason... */
878     		printk ("%s: cdrom_read_intr: bad interrupt reason %d\n",
879     			drive->name, ireason);
880     	}
881     
882     	cdrom_end_request (0, drive);
883     	return -1;
884     }
885     
886     /*
887      * Interrupt routine.  Called when a read request has completed.
888      */
889     static ide_startstop_t cdrom_read_intr (ide_drive_t *drive)
890     {
891     	int stat;
892     	int ireason, len, sectors_to_transfer, nskip;
893     	struct cdrom_info *info = drive->driver_data;
894     	int i, dma = info->dma, dma_error = 0;
895     	ide_startstop_t startstop;
896     
897     	struct request *rq = HWGROUP(drive)->rq;
898     
899     	/* Check for errors. */
900     	if (dma) {
901     		info->dma = 0;
902     		if ((dma_error = HWIF(drive)->dmaproc(ide_dma_end, drive)))
903     			HWIF(drive)->dmaproc(ide_dma_off, drive);
904     	}
905     
906     	if (cdrom_decode_status (&startstop, drive, 0, &stat))
907     		return startstop;
908      
909     	if (dma) {
910     		if (!dma_error) {
911     			for (i = rq->nr_sectors; i > 0;) {
912     				i -= rq->current_nr_sectors;
913     				ide_end_request(1, HWGROUP(drive));
914     			}
915     			return ide_stopped;
916     		} else
917     			return ide_error (drive, "dma error", stat);
918     	}
919     
920     	/* Read the interrupt reason and the transfer length. */
921     	ireason = IN_BYTE (IDE_NSECTOR_REG);
922     	len = IN_BYTE (IDE_LCYL_REG) + 256 * IN_BYTE (IDE_HCYL_REG);
923     
924     	/* If DRQ is clear, the command has completed. */
925     	if ((stat & DRQ_STAT) == 0) {
926     		/* If we're not done filling the current buffer, complain.
927     		   Otherwise, complete the command normally. */
928     		if (rq->current_nr_sectors > 0) {
929     			printk ("%s: cdrom_read_intr: data underrun (%ld blocks)\n",
930     				drive->name, rq->current_nr_sectors);
931     			cdrom_end_request (0, drive);
932     		} else
933     			cdrom_end_request (1, drive);
934     		return ide_stopped;
935     	}
936     
937     	/* Check that the drive is expecting to do the same thing we are. */
938     	if (cdrom_read_check_ireason (drive, len, ireason))
939     		return ide_stopped;
940     
941     	/* Assume that the drive will always provide data in multiples
942     	   of at least SECTOR_SIZE, as it gets hairy to keep track
943     	   of the transfers otherwise. */
944     	if ((len % SECTOR_SIZE) != 0) {
945     		printk ("%s: cdrom_read_intr: Bad transfer size %d\n",
946     			drive->name, len);
947     		if (CDROM_CONFIG_FLAGS (drive)->limit_nframes)
948     			printk ("  This drive is not supported by this version of the driver\n");
949     		else {
950     			printk ("  Trying to limit transfer sizes\n");
951     			CDROM_CONFIG_FLAGS (drive)->limit_nframes = 1;
952     		}
953     		cdrom_end_request (0, drive);
954     		return ide_stopped;
955     	}
956     
957     	/* The number of sectors we need to read from the drive. */
958     	sectors_to_transfer = len / SECTOR_SIZE;
959     
960     	/* First, figure out if we need to bit-bucket
961     	   any of the leading sectors. */
962     	nskip = MIN ((int)(rq->current_nr_sectors - (rq->bh->b_size >> SECTOR_BITS)),
963     		     sectors_to_transfer);
964     
965     	while (nskip > 0) {
966     		/* We need to throw away a sector. */
967     		char dum[SECTOR_SIZE];
968     		atapi_input_bytes (drive, dum, sizeof (dum));
969     
970     		--rq->current_nr_sectors;
971     		--nskip;
972     		--sectors_to_transfer;
973     	}
974     
975     	/* Now loop while we still have data to read from the drive. */
976     	while (sectors_to_transfer > 0) {
977     		int this_transfer;
978     
979     		/* If we've filled the present buffer but there's another
980     		   chained buffer after it, move on. */
981     		if (rq->current_nr_sectors == 0 && rq->nr_sectors)
982     			cdrom_end_request (1, drive);
983     
984     		/* If the buffers are full, cache the rest of the data in our
985     		   internal buffer. */
986     		if (rq->current_nr_sectors == 0) {
987     			cdrom_buffer_sectors(drive, rq->sector, sectors_to_transfer);
988     			sectors_to_transfer = 0;
989     		} else {
990     			/* Transfer data to the buffers.
991     			   Figure out how many sectors we can transfer
992     			   to the current buffer. */
993     			this_transfer = MIN (sectors_to_transfer,
994     					     rq->current_nr_sectors);
995     
996     			/* Read this_transfer sectors
997     			   into the current buffer. */
998     			while (this_transfer > 0) {
999     				atapi_input_bytes(drive, rq->buffer, SECTOR_SIZE);
1000     				rq->buffer += SECTOR_SIZE;
1001     				--rq->nr_sectors;
1002     				--rq->current_nr_sectors;
1003     				++rq->sector;
1004     				--this_transfer;
1005     				--sectors_to_transfer;
1006     			}
1007     		}
1008     	}
1009     
1010     	/* Done moving data!
1011     	   Wait for another interrupt. */
1012     	ide_set_handler(drive, &cdrom_read_intr, WAIT_CMD, NULL);
1013     	return ide_started;
1014     }
1015     
1016     /*
1017      * Try to satisfy some of the current read request from our cached data.
1018      * Returns nonzero if the request has been completed, zero otherwise.
1019      */
1020     static int cdrom_read_from_buffer (ide_drive_t *drive)
1021     {
1022     	struct cdrom_info *info = drive->driver_data;
1023     	struct request *rq = HWGROUP(drive)->rq;
1024     
1025     	/* Can't do anything if there's no buffer. */
1026     	if (info->buffer == NULL) return 0;
1027     
1028     	/* Loop while this request needs data and the next block is present
1029     	   in our cache. */
1030     	while (rq->nr_sectors > 0 &&
1031     	       rq->sector >= info->sector_buffered &&
1032     	       rq->sector < info->sector_buffered + info->nsectors_buffered) {
1033     		if (rq->current_nr_sectors == 0)
1034     			cdrom_end_request (1, drive);
1035     
1036     		memcpy (rq->buffer,
1037     			info->buffer +
1038     			(rq->sector - info->sector_buffered) * SECTOR_SIZE,
1039     			SECTOR_SIZE);
1040     		rq->buffer += SECTOR_SIZE;
1041     		--rq->current_nr_sectors;
1042     		--rq->nr_sectors;
1043     		++rq->sector;
1044     	}
1045     
1046     	/* If we've satisfied the current request,
1047     	   terminate it successfully. */
1048     	if (rq->nr_sectors == 0) {
1049     		cdrom_end_request (1, drive);
1050     		return -1;
1051     	}
1052     
1053     	/* Move on to the next buffer if needed. */
1054     	if (rq->current_nr_sectors == 0)
1055     		cdrom_end_request (1, drive);
1056     
1057     	/* If this condition does not hold, then the kluge i use to
1058     	   represent the number of sectors to skip at the start of a transfer
1059     	   will fail.  I think that this will never happen, but let's be
1060     	   paranoid and check. */
1061     	if (rq->current_nr_sectors < (rq->bh->b_size >> SECTOR_BITS) &&
1062     	    (rq->sector % SECTORS_PER_FRAME) != 0) {
1063     		printk ("%s: cdrom_read_from_buffer: buffer botch (%ld)\n",
1064     			drive->name, rq->sector);
1065     		cdrom_end_request (0, drive);
1066     		return -1;
1067     	}
1068     
1069     	return 0;
1070     }
1071     
1072     /*
1073      * Routine to send a read packet command to the drive.
1074      * This is usually called directly from cdrom_start_read.
1075      * However, for drq_interrupt devices, it is called from an interrupt
1076      * when the drive is ready to accept the command.
1077      */
1078     static ide_startstop_t cdrom_start_read_continuation (ide_drive_t *drive)
1079     {
1080     	struct packet_command pc;
1081     	struct request *rq = HWGROUP(drive)->rq;
1082     	int nsect, sector, nframes, frame, nskip;
1083     
1084     	/* Number of sectors to transfer. */
1085     	nsect = rq->nr_sectors;
1086     
1087     	/* Starting sector. */
1088     	sector = rq->sector;
1089     
1090     	/* If the requested sector doesn't start on a cdrom block boundary,
1091     	   we must adjust the start of the transfer so that it does,
1092     	   and remember to skip the first few sectors.
1093     	   If the CURRENT_NR_SECTORS field is larger than the size
1094     	   of the buffer, it will mean that we're to skip a number
1095     	   of sectors equal to the amount by which CURRENT_NR_SECTORS
1096     	   is larger than the buffer size. */
1097     	nskip = (sector % SECTORS_PER_FRAME);
1098     	if (nskip > 0) {
1099     		/* Sanity check... */
1100     		if (rq->current_nr_sectors != (rq->bh->b_size >> SECTOR_BITS) &&
1101     			(rq->sector % CD_FRAMESIZE != 0)) {
1102     			printk ("%s: cdrom_start_read_continuation: buffer botch (%lu)\n",
1103     				drive->name, rq->current_nr_sectors);
1104     			cdrom_end_request (0, drive);
1105     			return ide_stopped;
1106     		}
1107     		sector -= nskip;
1108     		nsect += nskip;
1109     		rq->current_nr_sectors += nskip;
1110     	}
1111     
1112     	/* Convert from sectors to cdrom blocks, rounding up the transfer
1113     	   length if needed. */
1114     	nframes = (nsect + SECTORS_PER_FRAME-1) / SECTORS_PER_FRAME;
1115     	frame = sector / SECTORS_PER_FRAME;
1116     
1117     	/* Largest number of frames was can transfer at once is 64k-1. For
1118     	   some drives we need to limit this even more. */
1119     	nframes = MIN (nframes, (CDROM_CONFIG_FLAGS (drive)->limit_nframes) ?
1120     		(65534 / CD_FRAMESIZE) : 65535);
1121     
1122     	/* Set up the command */
1123     	memset (&pc.c, 0, sizeof (pc.c));
1124     	pc.c[0] = GPCMD_READ_10;
1125     	pc.c[7] = (nframes >> 8);
1126     	pc.c[8] = (nframes & 0xff);
1127     	put_unaligned(cpu_to_be32(frame), (unsigned int *) &pc.c[2]);
1128     	pc.timeout = WAIT_CMD;
1129     
1130     	/* Send the command to the drive and return. */
1131     	return cdrom_transfer_packet_command(drive, &pc, &cdrom_read_intr);
1132     }
1133     
1134     
1135     #define IDECD_SEEK_THRESHOLD	(1000)			/* 1000 blocks */
1136     #define IDECD_SEEK_TIMER	(5 * WAIT_MIN_SLEEP)	/* 100 ms */
1137     #define IDECD_SEEK_TIMEOUT     WAIT_CMD			/* 10 sec */
1138     
1139     static ide_startstop_t cdrom_seek_intr (ide_drive_t *drive)
1140     {
1141     	struct cdrom_info *info = drive->driver_data;
1142     	int stat;
1143     	static int retry = 10;
1144     	ide_startstop_t startstop;
1145     
1146     	if (cdrom_decode_status (&startstop, drive, 0, &stat))
1147     		return startstop;
1148     	CDROM_CONFIG_FLAGS(drive)->seeking = 1;
1149     
1150     	if (retry && jiffies - info->start_seek > IDECD_SEEK_TIMER) {
1151     		if (--retry == 0) {
1152     			/*
1153     			 * this condition is far too common, to bother
1154     			 * users about it
1155     			 */
1156     #if 0
1157     			printk("%s: disabled DSC seek overlap\n", drive->name);
1158     #endif
1159     			drive->dsc_overlap = 0;
1160     		}
1161     	}
1162     	return ide_stopped;
1163     }
1164     
1165     static ide_startstop_t cdrom_start_seek_continuation (ide_drive_t *drive)
1166     {
1167     	struct packet_command pc;
1168     	struct request *rq = HWGROUP(drive)->rq;
1169     	int sector, frame, nskip;
1170     
1171     	sector = rq->sector;
1172     	nskip = (sector % SECTORS_PER_FRAME);
1173     	if (nskip > 0)
1174     		sector -= nskip;
1175     	frame = sector / SECTORS_PER_FRAME;
1176     
1177     	memset (&pc.c, 0, sizeof (pc.c));
1178     	pc.c[0] = GPCMD_SEEK;
1179     	put_unaligned(cpu_to_be32(frame), (unsigned int *) &pc.c[2]);
1180     
1181     	pc.timeout = WAIT_CMD;
1182     	return cdrom_transfer_packet_command(drive, &pc, &cdrom_seek_intr);
1183     }
1184     
1185     static ide_startstop_t cdrom_start_seek (ide_drive_t *drive, unsigned int block)
1186     {
1187     	struct cdrom_info *info = drive->driver_data;
1188     
1189     	info->dma = 0;
1190     	info->cmd = 0;
1191     	info->start_seek = jiffies;
1192     	return cdrom_start_packet_command (drive, 0, cdrom_start_seek_continuation);
1193     }
1194     
1195     static inline int cdrom_merge_requests(struct request *rq, struct request *nxt)
1196     {
1197     	int ret = 1;
1198     
1199     	/*
1200     	 * partitions not really working, but better check anyway...
1201     	 */
1202     	if (rq->cmd == nxt->cmd && rq->rq_dev == nxt->rq_dev) {
1203     		rq->nr_sectors += nxt->nr_sectors;
1204     		rq->hard_nr_sectors += nxt->nr_sectors;
1205     		rq->bhtail->b_reqnext = nxt->bh;
1206     		rq->bhtail = nxt->bhtail;
1207     		list_del(&nxt->queue);
1208     		blkdev_release_request(nxt);
1209     		ret = 0;
1210     	}
1211     
1212     	return ret;
1213     }
1214     
1215     /*
1216      * the current request will always be the first one on the list
1217      */
1218     static void cdrom_attempt_remerge(ide_drive_t *drive, struct request *rq)
1219     {
1220     	struct list_head *entry;
1221     	struct request *nxt;
1222     	unsigned long flags;
1223     
1224     	spin_lock_irqsave(&io_request_lock, flags);
1225     
1226     	while (1) {
1227     		entry = rq->queue.next;
1228     		if (entry == &drive->queue.queue_head)
1229     			break;
1230     
1231     		nxt = blkdev_entry_to_request(entry);
1232     		if (rq->sector + rq->nr_sectors != nxt->sector)
1233     			break;
1234     		else if (rq->nr_sectors + nxt->nr_sectors > SECTORS_MAX)
1235     			break;
1236     
1237     		if (cdrom_merge_requests(rq, nxt))
1238     			break;
1239     	}
1240     
1241     	spin_unlock_irqrestore(&io_request_lock, flags);
1242     }
1243     
1244     /* Fix up a possibly partially-processed request so that we can
1245        start it over entirely, or even put it back on the request queue. */
1246     static void restore_request (struct request *rq)
1247     {
1248     	if (rq->buffer != rq->bh->b_data) {
1249     		int n = (rq->buffer - rq->bh->b_data) / SECTOR_SIZE;
1250     		rq->buffer = rq->bh->b_data;
1251     		rq->nr_sectors += n;
1252     		rq->sector -= n;
1253     	}
1254     	rq->current_nr_sectors = rq->bh->b_size >> SECTOR_BITS;
1255     	rq->hard_nr_sectors = rq->nr_sectors;
1256     	rq->hard_sector = rq->sector;
1257     }
1258     
1259     /*
1260      * Start a read request from the CD-ROM.
1261      */
1262     static ide_startstop_t cdrom_start_read (ide_drive_t *drive, unsigned int block)
1263     {
1264     	struct cdrom_info *info = drive->driver_data;
1265     	struct request *rq = HWGROUP(drive)->rq;
1266     	int minor = MINOR (rq->rq_dev);
1267     
1268     	/* If the request is relative to a partition, fix it up to refer to the
1269     	   absolute address.  */
1270     	if (minor & PARTN_MASK) {
1271     		rq->sector = block;
1272     		minor &= ~PARTN_MASK;
1273     		rq->rq_dev = MKDEV(MAJOR(rq->rq_dev), minor);
1274     	}
1275     
1276     	/* We may be retrying this request after an error.  Fix up
1277     	   any weirdness which might be present in the request packet. */
1278     	restore_request(rq);
1279     
1280     	/* Satisfy whatever we can of this request from our cached sector. */
1281     	if (cdrom_read_from_buffer(drive))
1282     		return ide_stopped;
1283     
1284     	cdrom_attempt_remerge(drive, rq);
1285     
1286     	/* Clear the local sector buffer. */
1287     	info->nsectors_buffered = 0;
1288     
1289     	/* use dma, if possible. */
1290     	if (drive->using_dma && (rq->sector % SECTORS_PER_FRAME == 0) &&
1291     				(rq->nr_sectors % SECTORS_PER_FRAME == 0))
1292     		info->dma = 1;
1293     	else
1294     		info->dma = 0;
1295     
1296     	info->cmd = READ;
1297     	/* Start sending the read request to the drive. */
1298     	return cdrom_start_packet_command(drive, 32768, cdrom_start_read_continuation);
1299     }
1300     
1301     /****************************************************************************
1302      * Execute all other packet commands.
1303      */
1304     
1305     /* Forward declarations. */
1306     static int cdrom_lockdoor(ide_drive_t *drive, int lockflag,
1307     			  struct request_sense *sense);
1308     
1309     /* Interrupt routine for packet command completion. */
1310     static ide_startstop_t cdrom_pc_intr (ide_drive_t *drive)
1311     {
1312     	int ireason, len, stat, thislen;
1313     	struct request *rq = HWGROUP(drive)->rq;
1314     	struct packet_command *pc = (struct packet_command *)rq->buffer;
1315     	ide_startstop_t startstop;
1316     
1317     	/* Check for errors. */
1318     	if (cdrom_decode_status (&startstop, drive, 0, &stat))
1319     		return startstop;
1320     
1321     	/* Read the interrupt reason and the transfer length. */
1322     	ireason = IN_BYTE (IDE_NSECTOR_REG);
1323     	len = IN_BYTE (IDE_LCYL_REG) + 256 * IN_BYTE (IDE_HCYL_REG);
1324     
1325     	/* If DRQ is clear, the command has completed.
1326     	   Complain if we still have data left to transfer. */
1327     	if ((stat & DRQ_STAT) == 0) {
1328     		/* Some of the trailing request sense fields are optional, and
1329     		   some drives don't send them.  Sigh. */
1330     		if (pc->c[0] == GPCMD_REQUEST_SENSE &&
1331     		    pc->buflen > 0 &&
1332     		    pc->buflen <= 5) {
1333     			while (pc->buflen > 0) {
1334     				*pc->buffer++ = 0;
1335     				--pc->buflen;
1336     			}
1337     		}
1338     
1339     		if (pc->buflen == 0)
1340     			cdrom_end_request (1, drive);
1341     		else {
1342     			/* Comment this out, because this always happens 
1343     			   right after a reset occurs, and it is annoying to 
1344     			   always print expected stuff.  */
1345     			/*
1346     			printk ("%s: cdrom_pc_intr: data underrun %d\n",
1347     				drive->name, pc->buflen);
1348     			*/
1349     			pc->stat = 1;
1350     			cdrom_end_request (1, drive);
1351     		}
1352     		return ide_stopped;
1353     	}
1354     
1355     	/* Figure out how much data to transfer. */
1356     	thislen = pc->buflen;
1357     	if (thislen > len) thislen = len;
1358     
1359     	/* The drive wants to be written to. */
1360     	if ((ireason & 3) == 0) {
1361     		/* Transfer the data. */
1362     		atapi_output_bytes (drive, pc->buffer, thislen);
1363     
1364     		/* If we haven't moved enough data to satisfy the drive,
1365     		   add some padding. */
1366     		while (len > thislen) {
1367     			int dum = 0;
1368     			atapi_output_bytes (drive, &dum, sizeof (dum));
1369     			len -= sizeof (dum);
1370     		}
1371     
1372     		/* Keep count of how much data we've moved. */
1373     		pc->buffer += thislen;
1374     		pc->buflen -= thislen;
1375     	}
1376     
1377     	/* Same drill for reading. */
1378     	else if ((ireason & 3) == 2) {
1379     
1380     		/* Transfer the data. */
1381     		atapi_input_bytes (drive, pc->buffer, thislen);
1382     
1383     		/* If we haven't moved enough data to satisfy the drive,
1384     		   add some padding. */
1385     		while (len > thislen) {
1386     			int dum = 0;
1387     			atapi_input_bytes (drive, &dum, sizeof (dum));
1388     			len -= sizeof (dum);
1389     		}
1390     
1391     		/* Keep count of how much data we've moved. */
1392     		pc->buffer += thislen;
1393     		pc->buflen -= thislen;
1394     	} else {
1395     		printk ("%s: cdrom_pc_intr: The drive "
1396     			"appears confused (ireason = 0x%2x)\n",
1397     			drive->name, ireason);
1398     		pc->stat = 1;
1399     	}
1400     
1401     	/* Now we wait for another interrupt. */
1402     	ide_set_handler (drive, &cdrom_pc_intr, WAIT_CMD, cdrom_timer_expiry);
1403     	return ide_started;
1404     }
1405     
1406     
1407     static ide_startstop_t cdrom_do_pc_continuation (ide_drive_t *drive)
1408     {
1409     	struct request *rq = HWGROUP(drive)->rq;
1410     	struct packet_command *pc = (struct packet_command *)rq->buffer;
1411     
1412     	if (!pc->timeout)
1413     		pc->timeout = WAIT_CMD;
1414     
1415     	/* Send the command to the drive and return. */
1416     	return cdrom_transfer_packet_command(drive, pc, &cdrom_pc_intr);
1417     }
1418     
1419     
1420     static ide_startstop_t cdrom_do_packet_command (ide_drive_t *drive)
1421     {
1422     	int len;
1423     	struct request *rq = HWGROUP(drive)->rq;
1424     	struct packet_command *pc = (struct packet_command *)rq->buffer;
1425     	struct cdrom_info *info = drive->driver_data;
1426     
1427     	info->dma = 0;
1428     	info->cmd = 0;
1429     	pc->stat = 0;
1430     	len = pc->buflen;
1431     
1432     	/* Start sending the command to the drive. */
1433     	return cdrom_start_packet_command (drive, len, cdrom_do_pc_continuation);
1434     }
1435     
1436     
1437     /* Sleep for TIME jiffies.
1438        Not to be called from an interrupt handler. */
1439     static
1440     void cdrom_sleep (int time)
1441     {
1442     	int sleep = time;
1443     
1444     	do {
1445     		set_current_state(TASK_INTERRUPTIBLE);
1446     		sleep = schedule_timeout(sleep);
1447     	} while (sleep);
1448     }
1449     
1450     static
1451     int cdrom_queue_packet_command(ide_drive_t *drive, struct packet_command *pc)
1452     {
1453     	struct request_sense sense;
1454     	struct request req;
1455     	int retries = 10;
1456     
1457     	if (pc->sense == NULL)
1458     		pc->sense = &sense;
1459     
1460     	/* Start of retry loop. */
1461     	do {
1462     		ide_init_drive_cmd (&req);
1463     		req.cmd = PACKET_COMMAND;
1464     		req.buffer = (char *)pc;
1465     		if (ide_do_drive_cmd (drive, &req, ide_wait)) {
1466     			printk("%s: do_drive_cmd returned stat=%02x,err=%02x\n",
1467     				drive->name, req.buffer[0], req.buffer[1]);
1468     			/* FIXME: we should probably abort/retry or something */
1469     		}
1470     		if (pc->stat != 0) {
1471     			/* The request failed.  Retry if it was due to a unit
1472     			   attention status
1473     			   (usually means media was changed). */
1474     			struct request_sense *reqbuf = pc->sense;
1475     
1476     			if (reqbuf->sense_key == UNIT_ATTENTION)
1477     				cdrom_saw_media_change (drive);
1478     			else if (reqbuf->sense_key == NOT_READY &&
1479     				 reqbuf->asc == 4 && reqbuf->ascq != 4) {
1480     				/* The drive is in the process of loading
1481     				   a disk.  Retry, but wait a little to give
1482     				   the drive time to complete the load. */
1483     				cdrom_sleep(2 * HZ);
1484     			} else {
1485     				/* Otherwise, don't retry. */
1486     				retries = 0;
1487     			}
1488     			--retries;
1489     		}
1490     
1491     		/* End of retry loop. */
1492     	} while (pc->stat != 0 && retries >= 0);
1493     
1494     	/* Return an error if the command failed. */
1495     	return pc->stat ? -EIO : 0;
1496     }
1497     
1498     /*
1499      * Write handling
1500      */
1501     static inline int cdrom_write_check_ireason(ide_drive_t *drive, int len, int ireason)
1502     {
1503     	/* Two notes about IDE interrupt reason here - 0 means that
1504     	 * the drive wants to receive data from us, 2 means that
1505     	 * the drive is expecting data from us.
1506     	 */
1507     	ireason &= 3;
1508     
1509     	if (ireason == 2) {
1510     		/* Whoops... The drive wants to send data. */
1511     		printk("%s: cdrom_write_intr: wrong transfer direction!\n",
1512     			drive->name);
1513     
1514     		/* Throw some data at the drive so it doesn't hang
1515     		   and quit this request. */
1516     		while (len > 0) {
1517     			int dum = 0;
1518     			atapi_output_bytes(drive, &dum, sizeof(dum));
1519     			len -= sizeof(dum);
1520     		}
1521     	} else {
1522     		/* Drive wants a command packet, or invalid ireason... */
1523     		printk("%s: cdrom_write_intr: bad interrupt reason %d\n",
1524     			drive->name, ireason);
1525     	}
1526     
1527     	cdrom_end_request(0, drive);
1528     	return 1;
1529     }
1530     
1531     static ide_startstop_t cdrom_write_intr(ide_drive_t *drive)
1532     {
1533     	int stat, ireason, len, sectors_to_transfer, uptodate;
1534     	struct cdrom_info *info = drive->driver_data;
1535     	int i, dma_error = 0, dma = info->dma;
1536     	ide_startstop_t startstop;
1537     
1538     	struct request *rq = HWGROUP(drive)->rq;
1539     
1540     	/* Check for errors. */
1541     	if (dma) {
1542     		info->dma = 0;
1543     		if ((dma_error = HWIF(drive)->dmaproc(ide_dma_end, drive))) {
1544     			printk("ide-cd: write dma error\n");
1545     			HWIF(drive)->dmaproc(ide_dma_off, drive);
1546     		}
1547     	}
1548     
1549     	if (cdrom_decode_status(&startstop, drive, 0, &stat)) {
1550     		printk("ide-cd: write_intr decode_status bad\n");
1551     		return startstop;
1552     	}
1553      
1554     	/*
1555     	 * using dma, transfer is complete now
1556     	 */
1557     	if (dma) {
1558     		if (dma_error)
1559     			return ide_error(drive, "dma error", stat);
1560     
1561     		rq = HWGROUP(drive)->rq;
1562     		for (i = rq->nr_sectors; i > 0;) {
1563     			i -= rq->current_nr_sectors;
1564     			ide_end_request(1, HWGROUP(drive));
1565     		}
1566     		return ide_stopped;
1567     	}
1568     
1569     	/* Read the interrupt reason and the transfer length. */
1570     	ireason = IN_BYTE(IDE_NSECTOR_REG);
1571     	len = IN_BYTE(IDE_LCYL_REG) + 256 * IN_BYTE(IDE_HCYL_REG);
1572     
1573     	/* If DRQ is clear, the command has completed. */
1574     	if ((stat & DRQ_STAT) == 0) {
1575     		/* If we're not done writing, complain.
1576     		 * Otherwise, complete the command normally.
1577     		 */
1578     		uptodate = 1;
1579     		if (rq->current_nr_sectors > 0) {
1580     			printk("%s: write_intr: data underrun (%ld blocks)\n",
1581     			drive->name, rq->current_nr_sectors);
1582     			uptodate = 0;
1583     		}
1584     		cdrom_end_request(uptodate, drive);
1585     		return ide_stopped;
1586     	}
1587     
1588     	/* Check that the drive is expecting to do the same thing we are. */
1589     	if (ireason & 3)
1590     		if (cdrom_write_check_ireason(drive, len, ireason))
1591     			return ide_stopped;
1592     
1593     	sectors_to_transfer = len / SECTOR_SIZE;
1594     
1595     	/*
1596     	 * now loop and write out the data
1597     	 */
1598     	while (sectors_to_transfer > 0) {
1599     		int this_transfer;
1600     
1601     		if (!rq->current_nr_sectors) {
1602     			printk("ide-cd: write_intr: oops\n");
1603     			break;
1604     		}
1605     
1606     		/*
1607     		 * Figure out how many sectors we can transfer
1608     		 */
1609     		this_transfer = MIN(sectors_to_transfer,rq->current_nr_sectors);
1610     
1611     		while (this_transfer > 0) {
1612     			atapi_output_bytes(drive, rq->buffer, SECTOR_SIZE);
1613     			rq->buffer += SECTOR_SIZE;
1614     			--rq->nr_sectors;
1615     			--rq->current_nr_sectors;
1616     			++rq->sector;
1617     			--this_transfer;
1618     			--sectors_to_transfer;
1619     		}
1620     
1621     		/*
1622     		 * current buffer complete, move on
1623     		 */
1624     		if (rq->current_nr_sectors == 0 && rq->nr_sectors)
1625     			cdrom_end_request (1, drive);
1626     	}
1627     
1628     	/* re-arm handler */
1629     	ide_set_handler(drive, &cdrom_write_intr, 5 * WAIT_CMD, NULL);
1630     	return ide_started;
1631     }
1632     
1633     static ide_startstop_t cdrom_start_write_cont(ide_drive_t *drive)
1634     {
1635     	struct packet_command pc;	/* packet_command_t pc; */
1636     	struct request *rq = HWGROUP(drive)->rq;
1637     	unsigned nframes, frame;
1638     
1639     	nframes = rq->nr_sectors >> 2;
1640     	frame = rq->sector >> 2;
1641     
1642     	memset(&pc.c, 0, sizeof(pc.c));
1643     	/*
1644     	 * we might as well use WRITE_12, but none of the device I have
1645     	 * support the streaming feature anyway, so who cares.
1646     	 */
1647     	pc.c[0] = GPCMD_WRITE_10;
1648     #if 0	/* the immediate bit */
1649     	pc.c[1] = 1 << 3;
1650     #endif
1651     	pc.c[7] = (nframes >> 8) & 0xff;
1652     	pc.c[8] = nframes & 0xff;
1653     	put_unaligned(cpu_to_be32(frame), (unsigned int *)&pc.c[2]);
1654     	pc.timeout = 2 * WAIT_CMD;
1655     
1656     	return cdrom_transfer_packet_command(drive, &pc, cdrom_write_intr);
1657     }
1658     
1659     static ide_startstop_t cdrom_start_write(ide_drive_t *drive, struct request *rq)
1660     {
1661     	struct cdrom_info *info = drive->driver_data;
1662     
1663     	/*
1664     	 * writes *must* be 2kB frame aligned
1665     	 */
1666     	if ((rq->nr_sectors & 3) || (rq->sector & 3)) {
1667     		cdrom_end_request(0, drive);
1668     		return ide_stopped;
1669     	}
1670     
1671     	/*
1672     	 * for dvd-ram and such media, it's a really big deal to get
1673     	 * big writes all the time. so scour the queue and attempt to
1674     	 * remerge requests, often the plugging will not have had time
1675     	 * to do this properly
1676     	 */
1677     	cdrom_attempt_remerge(drive, rq);
1678     
1679     	info->nsectors_buffered = 0;
1680     
1681             /* use dma, if possible. we don't need to check more, since we
1682     	 * know that the transfer is always (at least!) 2KB aligned */
1683     	info->dma = drive->using_dma ? 1 : 0;
1684     	info->cmd = WRITE;
1685     
1686     	/* Start sending the read request to the drive. */
1687     	return cdrom_start_packet_command(drive, 32768, cdrom_start_write_cont);
1688     }
1689     
1690     /****************************************************************************
1691      * cdrom driver request routine.
1692      */
1693     static ide_startstop_t
1694     ide_do_rw_cdrom (ide_drive_t *drive, struct request *rq, unsigned long block)
1695     {
1696     	ide_startstop_t action;
1697     	struct cdrom_info *info = drive->driver_data;
1698     
1699     	switch (rq->cmd) {
1700     		case WRITE:
1701     		case READ: {
1702     			if (CDROM_CONFIG_FLAGS(drive)->seeking) {
1703     				unsigned long elpased = jiffies - info->start_seek;
1704     				int stat = GET_STAT();
1705     
1706     				if ((stat & SEEK_STAT) != SEEK_STAT) {
1707     					if (elpased < IDECD_SEEK_TIMEOUT) {
1708     						ide_stall_queue(drive, IDECD_SEEK_TIMER);
1709     						return ide_stopped;
1710     					}
1711     					printk ("%s: DSC timeout\n", drive->name);
1712     				}
1713     				CDROM_CONFIG_FLAGS(drive)->seeking = 0;
1714     			}
1715     			if (IDE_LARGE_SEEK(info->last_block, block, IDECD_SEEK_THRESHOLD) && drive->dsc_overlap)
1716     				action = cdrom_start_seek (drive, block);
1717     			else {
1718     				if (rq->cmd == READ)
1719     					action = cdrom_start_read(drive, block);
1720     				else
1721     					action = cdrom_start_write(drive, rq);
1722     			}
1723     			info->last_block = block;
1724     			return action;
1725     		}
1726     
1727     		case PACKET_COMMAND:
1728     		case REQUEST_SENSE_COMMAND: {
1729     			return cdrom_do_packet_command(drive);
1730     		}
1731     
1732     		case RESET_DRIVE_COMMAND: {
1733     			cdrom_end_request(1, drive);
1734     			return ide_do_reset(drive);
1735     		}
1736     
1737     		default: {
1738     			printk("ide-cd: bad cmd %d\n", rq->cmd);
1739     			cdrom_end_request(0, drive);
1740     			return ide_stopped;
1741     		}
1742     	}
1743     }
1744     
1745     
1746     
1747     /****************************************************************************
1748      * Ioctl handling.
1749      *
1750      * Routines which queue packet commands take as a final argument a pointer
1751      * to a request_sense struct.  If execution of the command results
1752      * in an error with a CHECK CONDITION status, this structure will be filled
1753      * with the results of the subsequent request sense command.  The pointer
1754      * can also be NULL, in which case no sense information is returned.
1755      */
1756     
1757     #if ! STANDARD_ATAPI
1758     static inline
1759     int bin2bcd (int x)
1760     {
1761     	return (x%10) | ((x/10) << 4);
1762     }
1763     
1764     
1765     static inline
1766     int bcd2bin (int x)
1767     {
1768     	return (x >> 4) * 10 + (x & 0x0f);
1769     }
1770     
1771     static
1772     void msf_from_bcd (struct atapi_msf *msf)
1773     {
1774     	msf->minute = bcd2bin (msf->minute);
1775     	msf->second = bcd2bin (msf->second);
1776     	msf->frame  = bcd2bin (msf->frame);
1777     }
1778     
1779     #endif /* not STANDARD_ATAPI */
1780     
1781     
1782     static inline
1783     void lba_to_msf (int lba, byte *m, byte *s, byte *f)
1784     {
1785     	lba += CD_MSF_OFFSET;
1786     	lba &= 0xffffff;  /* negative lbas use only 24 bits */
1787     	*m = lba / (CD_SECS * CD_FRAMES);
1788     	lba %= (CD_SECS * CD_FRAMES);
1789     	*s = lba / CD_FRAMES;
1790     	*f = lba % CD_FRAMES;
1791     }
1792     
1793     
1794     static inline
1795     int msf_to_lba (byte m, byte s, byte f)
1796     {
1797     	return (((m * CD_SECS) + s) * CD_FRAMES + f) - CD_MSF_OFFSET;
1798     }
1799     
1800     static int cdrom_check_status(ide_drive_t *drive, struct request_sense *sense)
1801     {
1802     	struct packet_command pc;
1803     	struct cdrom_info *info = drive->driver_data;
1804     	struct cdrom_device_info *cdi = &info->devinfo;
1805     
1806     	memset(&pc, 0, sizeof(pc));
1807     	pc.sense = sense;
1808     
1809     	pc.c[0] = GPCMD_TEST_UNIT_READY;
1810     
1811     #if ! STANDARD_ATAPI
1812             /* the Sanyo 3 CD changer uses byte 7 of TEST_UNIT_READY to 
1813                switch CDs instead of supporting the LOAD_UNLOAD opcode   */
1814     
1815             pc.c[7] = cdi->sanyo_slot % 3;
1816     #endif /* not STANDARD_ATAPI */
1817     
1818     	return cdrom_queue_packet_command(drive, &pc);
1819     }
1820     
1821     
1822     /* Lock the door if LOCKFLAG is nonzero; unlock it otherwise. */
1823     static int
1824     cdrom_lockdoor(ide_drive_t *drive, int lockflag, struct request_sense *sense)
1825     {
1826     	struct request_sense my_sense;
1827     	struct packet_command pc;
1828     	int stat;
1829     
1830     	if (sense == NULL)
1831     		sense = &my_sense;
1832     
1833     	/* If the drive cannot lock the door, just pretend. */
1834     	if (CDROM_CONFIG_FLAGS(drive)->no_doorlock) {
1835     		stat = 0;
1836     	} else {
1837     		memset(&pc, 0, sizeof(pc));
1838     		pc.sense = sense;
1839     		pc.c[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
1840     		pc.c[4] = lockflag ? 1 : 0;
1841     		stat = cdrom_queue_packet_command (drive, &pc);
1842     	}
1843     
1844     	/* If we got an illegal field error, the drive
1845     	   probably cannot lock the door. */
1846     	if (stat != 0 &&
1847     	    sense->sense_key == ILLEGAL_REQUEST &&
1848     	    (sense->asc == 0x24 || sense->asc == 0x20)) {
1849     		printk ("%s: door locking not supported\n",
1850     			drive->name);
1851     		CDROM_CONFIG_FLAGS (drive)->no_doorlock = 1;
1852     		stat = 0;
1853     	}
1854     	
1855     	/* no medium, that's alright. */
1856     	if (stat != 0 && sense->sense_key == NOT_READY && sense->asc == 0x3a)
1857     		stat = 0;
1858     
1859     	if (stat == 0)
1860     		CDROM_STATE_FLAGS (drive)->door_locked = lockflag;
1861     
1862     	return stat;
1863     }
1864     
1865     
1866     /* Eject the disk if EJECTFLAG is 0.
1867        If EJECTFLAG is 1, try to reload the disk. */
1868     static int cdrom_eject(ide_drive_t *drive, int ejectflag,
1869     		       struct request_sense *sense)
1870     {
1871     	struct packet_command pc;
1872     
1873     	if (CDROM_CONFIG_FLAGS(drive)->no_eject && !ejectflag)
1874     		return -EDRIVE_CANT_DO_THIS;
1875     	
1876     	/* reload fails on some drives, if the tray is locked */
1877     	if (CDROM_STATE_FLAGS(drive)->door_locked && ejectflag)
1878     		return 0;
1879     
1880     	memset(&pc, 0, sizeof (pc));
1881     	pc.sense = sense;
1882     
1883     	pc.c[0] = GPCMD_START_STOP_UNIT;
1884     	pc.c[4] = 0x02 + (ejectflag != 0);
1885     	return cdrom_queue_packet_command (drive, &pc);
1886     }
1887     
1888     static int cdrom_read_capacity(ide_drive_t *drive, unsigned long *capacity,
1889     			       struct request_sense *sense)
1890     {
1891     	struct {
1892     		__u32 lba;
1893     		__u32 blocklen;
1894     	} capbuf;
1895     
1896     	int stat;
1897     	struct packet_command pc;
1898     
1899     	memset(&pc, 0, sizeof(pc));
1900     	pc.sense = sense;
1901     
1902     	pc.c[0] = GPCMD_READ_CDVD_CAPACITY;
1903     	pc.buffer = (char *)&capbuf;
1904     	pc.buflen = sizeof(capbuf);
1905     
1906     	stat = cdrom_queue_packet_command(drive, &pc);
1907     	if (stat == 0)
1908     		*capacity = 1 + be32_to_cpu(capbuf.lba);
1909     
1910     	return stat;
1911     }
1912     
1913     static int cdrom_read_tocentry(ide_drive_t *drive, int trackno, int msf_flag,
1914     				int format, char *buf, int buflen,
1915     				struct request_sense *sense)
1916     {
1917     	struct packet_command pc;
1918     
1919     	memset(&pc, 0, sizeof(pc));
1920     	pc.sense = sense;
1921     
1922     	pc.buffer =  buf;
1923     	pc.buflen = buflen;
1924     	pc.quiet = 1;
1925     	pc.c[0] = GPCMD_READ_TOC_PMA_ATIP;
1926     	pc.c[6] = trackno;
1927     	pc.c[7] = (buflen >> 8);
1928     	pc.c[8] = (buflen & 0xff);
1929     	pc.c[9] = (format << 6);
1930     
1931     	if (msf_flag)
1932     		pc.c[1] = 2;
1933     
1934     	return cdrom_queue_packet_command (drive, &pc);
1935     }
1936     
1937     
1938     /* Try to read the entire TOC for the disk into our internal buffer. */
1939     static int cdrom_read_toc(ide_drive_t *drive, struct request_sense *sense)
1940     {
1941     	int minor, stat, ntracks, i;
1942     	kdev_t dev;
1943     	struct cdrom_info *info = drive->driver_data;
1944     	struct atapi_toc *toc = info->toc;
1945     	struct {
1946     		struct atapi_toc_header hdr;
1947     		struct atapi_toc_entry  ent;
1948     	} ms_tmp;
1949     
1950     	if (toc == NULL) {
1951     		/* Try to allocate space. */
1952     		toc = (struct atapi_toc *) kmalloc (sizeof (struct atapi_toc),
1953     						    GFP_KERNEL);
1954     		info->toc = toc;
1955     		if (toc == NULL) {
1956     			printk ("%s: No cdrom TOC buffer!\n", drive->name);
1957     			return -ENOMEM;
1958     		}
1959     	}
1960     
1961     	/* Check to see if the existing data is still valid.
1962     	   If it is, just return. */
1963     	(void) cdrom_check_status(drive, sense);
1964     
1965     	if (CDROM_STATE_FLAGS(drive)->toc_valid)
1966     		return 0;
1967     
1968     	/* First read just the header, so we know how long the TOC is. */
1969     	stat = cdrom_read_tocentry(drive, 0, 1, 0, (char *) &toc->hdr,
1970     				    sizeof(struct atapi_toc_header), sense);
1971     	if (stat) return stat;
1972     
1973     #if ! STANDARD_ATAPI
1974     	if (CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd) {
1975     		toc->hdr.first_track = bcd2bin (toc->hdr.first_track);
1976     		toc->hdr.last_track  = bcd2bin (toc->hdr.last_track);
1977     	}
1978     #endif  /* not STANDARD_ATAPI */
1979     
1980     	ntracks = toc->hdr.last_track - toc->hdr.first_track + 1;
1981     	if (ntracks <= 0)
1982     		return -EIO;
1983     	if (ntracks > MAX_TRACKS)
1984     		ntracks = MAX_TRACKS;
1985     
1986     	/* Now read the whole schmeer. */
1987     	stat = cdrom_read_tocentry(drive, toc->hdr.first_track, 1, 0,
1988     				  (char *)&toc->hdr,
1989     				   sizeof(struct atapi_toc_header) +
1990     				   (ntracks + 1) *
1991     				   sizeof(struct atapi_toc_entry), sense);
1992     
1993     	if (stat && toc->hdr.first_track > 1) {
1994     		/* Cds with CDI tracks only don't have any TOC entries,
1995     		   despite of this the returned values are
1996     		   first_track == last_track = number of CDI tracks + 1,
1997     		   so that this case is indistinguishable from the same
1998     		   layout plus an additional audio track.
1999     		   If we get an error for the regular case, we assume
2000     		   a CDI without additional audio tracks. In this case
2001     		   the readable TOC is empty (CDI tracks are not included)
2002     		   and only holds the Leadout entry. Heiko Eißfeldt */
2003     		ntracks = 0;
2004     		stat = cdrom_read_tocentry(drive, CDROM_LEADOUT, 1, 0,
2005     					   (char *)&toc->hdr,
2006     					   sizeof(struct atapi_toc_header) +
2007     					   (ntracks + 1) *
2008     					   sizeof(struct atapi_toc_entry),
2009     					   sense);
2010     		if (stat) {
2011     			return stat;
2012     		}
2013     #if ! STANDARD_ATAPI
2014     		if (CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd) {
2015     			toc->hdr.first_track = bin2bcd(CDROM_LEADOUT);
2016     			toc->hdr.last_track = bin2bcd(CDROM_LEADOUT);
2017     		} else
2018     #endif  /* not STANDARD_ATAPI */
2019     		{
2020     			toc->hdr.first_track = CDROM_LEADOUT;
2021     			toc->hdr.last_track = CDROM_LEADOUT;
2022     		}
2023     	}
2024     
2025     	if (stat)
2026     		return stat;
2027     
2028     	toc->hdr.toc_length = ntohs (toc->hdr.toc_length);
2029     
2030     #if ! STANDARD_ATAPI
2031     	if (CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd) {
2032     		toc->hdr.first_track = bcd2bin (toc->hdr.first_track);
2033     		toc->hdr.last_track  = bcd2bin (toc->hdr.last_track);
2034     	}
2035     #endif  /* not STANDARD_ATAPI */
2036     
2037     	for (i=0; i<=ntracks; i++) {
2038     #if ! STANDARD_ATAPI
2039     		if (CDROM_CONFIG_FLAGS (drive)->tocaddr_as_bcd) {
2040     			if (CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd)
2041     				toc->ent[i].track = bcd2bin (toc->ent[i].track);
2042     			msf_from_bcd (&toc->ent[i].addr.msf);
2043     		}
2044     #endif  /* not STANDARD_ATAPI */
2045     		toc->ent[i].addr.lba = msf_to_lba (toc->ent[i].addr.msf.minute,
2046     						   toc->ent[i].addr.msf.second,
2047     						   toc->ent[i].addr.msf.frame);
2048     	}
2049     
2050     	/* Read the multisession information. */
2051     	if (toc->hdr.first_track != CDROM_LEADOUT) {
2052     		/* Read the multisession information. */
2053     		stat = cdrom_read_tocentry(drive, 0, 1, 1, (char *)&ms_tmp,
2054     					   sizeof(ms_tmp), sense);
2055     		if (stat) return stat;
2056     	} else {
2057     		ms_tmp.ent.addr.msf.minute = 0;
2058     		ms_tmp.ent.addr.msf.second = 2;
2059     		ms_tmp.ent.addr.msf.frame  = 0;
2060     		ms_tmp.hdr.first_track = ms_tmp.hdr.last_track = CDROM_LEADOUT;
2061     	}
2062     
2063     #if ! STANDARD_ATAPI
2064     	if (CDROM_CONFIG_FLAGS (drive)->tocaddr_as_bcd)
2065     		msf_from_bcd (&ms_tmp.ent.addr.msf);
2066     #endif  /* not STANDARD_ATAPI */
2067     
2068     	toc->last_session_lba = msf_to_lba (ms_tmp.ent.addr.msf.minute,
2069     					    ms_tmp.ent.addr.msf.second,
2070     					    ms_tmp.ent.addr.msf.frame);
2071     
2072     	toc->xa_flag = (ms_tmp.hdr.first_track != ms_tmp.hdr.last_track);
2073     
2074     	/* Now try to get the total cdrom capacity. */
2075     	minor = (drive->select.b.unit) << PARTN_BITS;
2076     	dev = MKDEV(HWIF(drive)->major, minor);
2077     	stat = cdrom_get_last_written(dev, &toc->capacity);
2078     	if (stat)
2079     		stat = cdrom_read_capacity(drive, &toc->capacity, sense);
2080     	if (stat)
2081     		toc->capacity = 0x1fffff;
2082     
2083     	HWIF(drive)->gd->sizes[drive->select.b.unit << PARTN_BITS] = (toc->capacity * SECTORS_PER_FRAME) >> (BLOCK_SIZE_BITS - 9);
2084     	drive->part[0].nr_sects = toc->capacity * SECTORS_PER_FRAME;
2085     
2086     	/* Remember that we've read this stuff. */
2087     	CDROM_STATE_FLAGS (drive)->toc_valid = 1;
2088     
2089     	return 0;
2090     }
2091     
2092     
2093     static int cdrom_read_subchannel(ide_drive_t *drive, int format, char *buf,
2094     				 int buflen, struct request_sense *sense)
2095     {
2096     	struct packet_command pc;
2097     
2098     	memset(&pc, 0, sizeof(pc));
2099     	pc.sense = sense;
2100     
2101     	pc.buffer = buf;
2102     	pc.buflen = buflen;
2103     	pc.c[0] = GPCMD_READ_SUBCHANNEL;
2104     	pc.c[1] = 2;     /* MSF addressing */
2105     	pc.c[2] = 0x40;  /* request subQ data */
2106     	pc.c[3] = format;
2107     	pc.c[7] = (buflen >> 8);
2108     	pc.c[8] = (buflen & 0xff);
2109     	return cdrom_queue_packet_command(drive, &pc);
2110     }
2111     
2112     /* ATAPI cdrom drives are free to select the speed you request or any slower
2113        rate :-( Requesting too fast a speed will _not_ produce an error. */
2114     static int cdrom_select_speed(ide_drive_t *drive, int speed,
2115     			      struct request_sense *sense)
2116     {
2117     	struct packet_command pc;
2118     	memset(&pc, 0, sizeof(pc));
2119     	pc.sense = sense;
2120     
2121     	if (speed == 0)
2122     		speed = 0xffff; /* set to max */
2123     	else
2124     		speed *= 177;   /* Nx to kbytes/s */
2125     
2126     	pc.c[0] = GPCMD_SET_SPEED;
2127     	/* Read Drive speed in kbytes/second MSB */
2128     	pc.c[2] = (speed >> 8) & 0xff;	
2129     	/* Read Drive speed in kbytes/second LSB */
2130     	pc.c[3] = speed & 0xff;
2131     	if (CDROM_CONFIG_FLAGS(drive)->cd_r ||
2132     	    CDROM_CONFIG_FLAGS(drive)->cd_rw ||
2133     	    CDROM_CONFIG_FLAGS(drive)->dvd_r) {
2134     		/* Write Drive speed in kbytes/second MSB */
2135     		pc.c[4] = (speed >> 8) & 0xff;
2136     		/* Write Drive speed in kbytes/second LSB */
2137     		pc.c[5] = speed & 0xff;
2138            }
2139     
2140     	return cdrom_queue_packet_command(drive, &pc);
2141     }
2142     
2143     static int cdrom_play_audio(ide_drive_t *drive, int lba_start, int lba_end)
2144     {
2145     	struct request_sense sense;
2146     	struct packet_command pc;
2147     
2148     	memset(&pc, 0, sizeof (pc));
2149     	pc.sense = &sense;
2150     
2151     	pc.c[0] = GPCMD_PLAY_AUDIO_MSF;
2152     	lba_to_msf(lba_start, &pc.c[3], &pc.c[4], &pc.c[5]);
2153     	lba_to_msf(lba_end-1, &pc.c[6], &pc.c[7], &pc.c[8]);
2154     
2155     	return cdrom_queue_packet_command(drive, &pc);
2156     }
2157     
2158     static int cdrom_get_toc_entry(ide_drive_t *drive, int track,
2159     				struct atapi_toc_entry **ent)
2160     {
2161     	struct cdrom_info *info = drive->driver_data;
2162     	struct atapi_toc *toc = info->toc;
2163     	int ntracks;
2164     
2165     	/*
2166     	 * don't serve cached data, if the toc isn't valid
2167     	 */
2168     	if (!CDROM_STATE_FLAGS(drive)->toc_valid)
2169     		return -EINVAL;
2170     
2171     	/* Check validity of requested track number. */
2172     	ntracks = toc->hdr.last_track - toc->hdr.first_track + 1;
2173     	if (toc->hdr.first_track == CDROM_LEADOUT) ntracks = 0;
2174     	if (track == CDROM_LEADOUT)
2175     		*ent = &toc->ent[ntracks];
2176     	else if (track < toc->hdr.first_track ||
2177     		 track > toc->hdr.last_track)
2178     		return -EINVAL;
2179     	else
2180     		*ent = &toc->ent[track - toc->hdr.first_track];
2181     
2182     	return 0;
2183     }
2184     
2185     /* the generic packet interface to cdrom.c */
2186     static int ide_cdrom_packet(struct cdrom_device_info *cdi,
2187     			    struct cdrom_generic_command *cgc)
2188     {
2189     	struct packet_command pc;
2190     	ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2191     
2192     	if (cgc->timeout <= 0)
2193     		cgc->timeout = WAIT_CMD;
2194     
2195     	/* here we queue the commands from the uniform CD-ROM
2196     	   layer. the packet must be complete, as we do not
2197     	   touch it at all. */
2198     	memset(&pc, 0, sizeof(pc));
2199     	memcpy(pc.c, cgc->cmd, CDROM_PACKET_SIZE);
2200     	pc.buffer = cgc->buffer;
2201     	pc.buflen = cgc->buflen;
2202     	pc.quiet = cgc->quiet;
2203     	pc.timeout = cgc->timeout;
2204     	pc.sense = cgc->sense;
2205     	return cgc->stat = cdrom_queue_packet_command(drive, &pc);
2206     }
2207     
2208     static
2209     int ide_cdrom_dev_ioctl (struct cdrom_device_info *cdi,
2210     			 unsigned int cmd, unsigned long arg)
2211     {
2212     	struct cdrom_generic_command cgc;
2213     	char buffer[16];
2214     	int stat;
2215     
2216     	init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_UNKNOWN);
2217     
2218     	/* These will be moved into the Uniform layer shortly... */
2219     	switch (cmd) {
2220      	case CDROMSETSPINDOWN: {
2221      		char spindown;
2222      
2223      		if (copy_from_user(&spindown, (void *) arg, sizeof(char)))
2224     			return -EFAULT;
2225      
2226                     if ((stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CDROM_PAGE, 0)))
2227     			return stat;
2228     
2229      		buffer[11] = (buffer[11] & 0xf0) | (spindown & 0x0f);
2230     
2231      		return cdrom_mode_select(cdi, &cgc);
2232      	} 
2233      
2234      	case CDROMGETSPINDOWN: {
2235      		char spindown;
2236      
2237                     if ((stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CDROM_PAGE, 0)))
2238     			return stat;
2239      
2240      		spindown = buffer[11] & 0x0f;
2241      
2242     		if (copy_to_user((void *) arg, &spindown, sizeof (char)))
2243     			return -EFAULT;
2244      
2245      		return 0;
2246      	}
2247       
2248     	default:
2249     		return -EINVAL;
2250     	}
2251     
2252     }
2253     
2254     static
2255     int ide_cdrom_audio_ioctl (struct cdrom_device_info *cdi,
2256     			   unsigned int cmd, void *arg)
2257     			   
2258     {
2259     	ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2260     	struct cdrom_info *info = drive->driver_data;
2261     	int stat;
2262     
2263     	switch (cmd) {
2264     	/*
2265     	 * emulate PLAY_AUDIO_TI command with PLAY_AUDIO_10, since
2266     	 * atapi doesn't support it
2267     	 */
2268     	case CDROMPLAYTRKIND: {
2269     		unsigned long lba_start, lba_end;
2270     		struct cdrom_ti *ti = (struct cdrom_ti *)arg;
2271     		struct atapi_toc_entry *first_toc, *last_toc;
2272     
2273     		stat = cdrom_get_toc_entry(drive, ti->cdti_trk0, &first_toc);
2274     		if (stat)
2275     			return stat;
2276     
2277     		stat = cdrom_get_toc_entry(drive, ti->cdti_trk1, &last_toc);
2278     		if (stat)
2279     			return stat;
2280     
2281     		if (ti->cdti_trk1 != CDROM_LEADOUT)
2282     			++last_toc;
2283     		lba_start = first_toc->addr.lba;
2284     		lba_end   = last_toc->addr.lba;
2285     
2286     		if (lba_end <= lba_start)
2287     			return -EINVAL;
2288     
2289     		return cdrom_play_audio(drive, lba_start, lba_end);
2290     	}
2291     
2292     	case CDROMREADTOCHDR: {
2293     		struct cdrom_tochdr *tochdr = (struct cdrom_tochdr *) arg;
2294     		struct atapi_toc *toc;
2295     
2296     		/* Make sure our saved TOC is valid. */
2297     		stat = cdrom_read_toc(drive, NULL);
2298     		if (stat) return stat;
2299     
2300     		toc = info->toc;
2301     		tochdr->cdth_trk0 = toc->hdr.first_track;
2302     		tochdr->cdth_trk1 = toc->hdr.last_track;
2303     
2304     		return 0;
2305     	}
2306     
2307     	case CDROMREADTOCENTRY: {
2308     		struct cdrom_tocentry *tocentry = (struct cdrom_tocentry*) arg;
2309     		struct atapi_toc_entry *toce;
2310     
2311     		stat = cdrom_get_toc_entry (drive, tocentry->cdte_track, &toce);
2312     		if (stat) return stat;
2313     
2314     		tocentry->cdte_ctrl = toce->control;
2315     		tocentry->cdte_adr  = toce->adr;
2316     		if (tocentry->cdte_format == CDROM_MSF) {
2317     			lba_to_msf (toce->addr.lba,
2318     				   &tocentry->cdte_addr.msf.minute,
2319     				   &tocentry->cdte_addr.msf.second,
2320     				   &tocentry->cdte_addr.msf.frame);
2321     		} else
2322     			tocentry->cdte_addr.lba = toce->addr.lba;
2323     
2324     		return 0;
2325     	}
2326     
2327     	default:
2328     		return -EINVAL;
2329     	}
2330     }
2331     
2332     static
2333     int ide_cdrom_reset (struct cdrom_device_info *cdi)
2334     {
2335     	ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2336     	struct request_sense sense;
2337     	struct request req;
2338     	int ret;
2339     
2340     	ide_init_drive_cmd (&req);
2341     	req.cmd = RESET_DRIVE_COMMAND;
2342     	ret = ide_do_drive_cmd(drive, &req, ide_wait);
2343     
2344     	/*
2345     	 * A reset will unlock the door. If it was previously locked,
2346     	 * lock it again.
2347     	 */
2348     	if (CDROM_STATE_FLAGS(drive)->door_locked)
2349     		(void) cdrom_lockdoor(drive, 1, &sense);
2350     
2351     	return ret;
2352     }
2353     
2354     
2355     static
2356     int ide_cdrom_tray_move (struct cdrom_device_info *cdi, int position)
2357     {
2358     	ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2359     	struct request_sense sense;
2360     
2361     	if (position) {
2362     		int stat = cdrom_lockdoor(drive, 0, &sense);
2363     		if (stat) return stat;
2364     	}
2365     
2366     	return cdrom_eject(drive, !position, &sense);
2367     }
2368     
2369     static
2370     int ide_cdrom_lock_door (struct cdrom_device_info *cdi, int lock)
2371     {
2372     	ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2373     	return cdrom_lockdoor(drive, lock, NULL);
2374     }
2375     
2376     static
2377     int ide_cdrom_select_speed (struct cdrom_device_info *cdi, int speed)
2378     {
2379     	ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2380     	struct request_sense sense;
2381     	int stat;
2382     
2383     	if ((stat = cdrom_select_speed (drive, speed, &sense)) < 0)
2384     		return stat;
2385     
2386             cdi->speed = CDROM_STATE_FLAGS (drive)->current_speed;
2387             return 0;
2388     }
2389     
2390     static
2391     int ide_cdrom_drive_status (struct cdrom_device_info *cdi, int slot_nr)
2392     {
2393     	ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2394     
2395     	if (slot_nr == CDSL_CURRENT) {
2396     		struct request_sense sense;
2397     		int stat = cdrom_check_status(drive, &sense);
2398     		if (stat == 0 || sense.sense_key == UNIT_ATTENTION)
2399     			return CDS_DISC_OK;
2400     
2401     		if (sense.sense_key == NOT_READY && sense.asc == 0x04 &&
2402     		    sense.ascq == 0x04)
2403     			return CDS_DISC_OK;
2404     
2405     
2406     		/*
2407     		 * If not using Mt Fuji extended media tray reports,
2408     		 * just return TRAY_OPEN since ATAPI doesn't provide
2409     		 * any other way to detect this...
2410     		 */
2411     		if (sense.sense_key == NOT_READY) {
2412     			if (sense.asc == 0x3a && (!sense.ascq||sense.ascq == 1))
2413     				return CDS_NO_DISC;
2414     			else
2415     				return CDS_TRAY_OPEN;
2416     		}
2417     
2418     		return CDS_DRIVE_NOT_READY;
2419     	}
2420     	return -EINVAL;
2421     }
2422     
2423     static
2424     int ide_cdrom_get_last_session (struct cdrom_device_info *cdi,
2425     				struct cdrom_multisession *ms_info)
2426     {
2427     	struct atapi_toc *toc;
2428     	ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2429     	struct cdrom_info *info = drive->driver_data;
2430     	struct request_sense sense;
2431     	int ret;
2432     
2433     	if (!CDROM_STATE_FLAGS(drive)->toc_valid || info->toc == NULL)
2434     		if ((ret = cdrom_read_toc(drive, &sense)))
2435     			return ret;
2436     
2437     	toc = info->toc;
2438     	ms_info->addr.lba = toc->last_session_lba;
2439     	ms_info->xa_flag = toc->xa_flag;
2440     
2441     	return 0;
2442     }
2443     
2444     static
2445     int ide_cdrom_get_mcn (struct cdrom_device_info *cdi,
2446     		       struct cdrom_mcn *mcn_info)
2447     {
2448     	int stat;
2449     	char mcnbuf[24];
2450     	ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2451     
2452     /* get MCN */
2453     	if ((stat = cdrom_read_subchannel(drive, 2, mcnbuf, sizeof (mcnbuf), NULL)))
2454     		return stat;
2455     
2456     	memcpy (mcn_info->medium_catalog_number, mcnbuf+9,
2457     		sizeof (mcn_info->medium_catalog_number)-1);
2458     	mcn_info->medium_catalog_number[sizeof (mcn_info->medium_catalog_number)-1]
2459     		= '\0';
2460     
2461     	return 0;
2462     }
2463     
2464     
2465     
2466     /****************************************************************************
2467      * Other driver requests (open, close, check media change).
2468      */
2469     
2470     static
2471     int ide_cdrom_check_media_change_real (struct cdrom_device_info *cdi,
2472     				       int slot_nr)
2473     {
2474     	ide_drive_t *drive = (ide_drive_t*) cdi->handle;
2475     	int retval;
2476     	
2477     	if (slot_nr == CDSL_CURRENT) {
2478     		(void) cdrom_check_status(drive, NULL);
2479     		retval = CDROM_STATE_FLAGS (drive)->media_changed;
2480     		CDROM_STATE_FLAGS (drive)->media_changed = 0;
2481     		return retval;
2482     	} else {
2483     		return -EINVAL;
2484     	}
2485     }
2486     
2487     
2488     static
2489     int ide_cdrom_open_real (struct cdrom_device_info *cdi, int purpose)
2490     {
2491     	return 0;
2492     }
2493     
2494     
2495     /*
2496      * Close down the device.  Invalidate all cached blocks.
2497      */
2498     
2499     static
2500     void ide_cdrom_release_real (struct cdrom_device_info *cdi)
2501     {
2502     }
2503     
2504     
2505     
2506     /****************************************************************************
2507      * Device initialization.
2508      */
2509     static struct cdrom_device_ops ide_cdrom_dops = {
2510     	open:			ide_cdrom_open_real,
2511     	release:		ide_cdrom_release_real,
2512     	drive_status:		ide_cdrom_drive_status,
2513     	media_changed:		ide_cdrom_check_media_change_real,
2514     	tray_move:		ide_cdrom_tray_move,
2515     	lock_door:		ide_cdrom_lock_door,
2516     	select_speed:		ide_cdrom_select_speed,
2517     	get_last_session:	ide_cdrom_get_last_session,
2518     	get_mcn:		ide_cdrom_get_mcn,
2519     	reset:			ide_cdrom_reset,
2520     	audio_ioctl:		ide_cdrom_audio_ioctl,
2521     	dev_ioctl:		ide_cdrom_dev_ioctl,
2522     	capability:		CDC_CLOSE_TRAY | CDC_OPEN_TRAY | CDC_LOCK |
2523     				CDC_SELECT_SPEED | CDC_SELECT_DISC |
2524     				CDC_MULTI_SESSION | CDC_MCN |
2525     				CDC_MEDIA_CHANGED | CDC_PLAY_AUDIO | CDC_RESET |
2526     				CDC_IOCTLS | CDC_DRIVE_STATUS | CDC_CD_R |
2527     				CDC_CD_RW | CDC_DVD | CDC_DVD_R| CDC_DVD_RAM |
2528     				CDC_GENERIC_PACKET,
2529     	generic_packet:		ide_cdrom_packet,
2530     };
2531     
2532     static int ide_cdrom_register (ide_drive_t *drive, int nslots)
2533     {
2534     	struct cdrom_info *info = drive->driver_data;
2535     	struct cdrom_device_info *devinfo = &info->devinfo;
2536     	int minor = (drive->select.b.unit) << PARTN_BITS;
2537     
2538     	devinfo->dev = MKDEV (HWIF(drive)->major, minor);
2539     	devinfo->ops = &ide_cdrom_dops;
2540     	devinfo->mask = 0;
2541     	*(int *)&devinfo->speed = CDROM_STATE_FLAGS (drive)->current_speed;
2542     	*(int *)&devinfo->capacity = nslots;
2543     	devinfo->handle = (void *) drive;
2544     	strcpy(devinfo->name, drive->name);
2545     	
2546     	/* set capability mask to match the probe. */
2547     	if (!CDROM_CONFIG_FLAGS (drive)->cd_r)
2548     		devinfo->mask |= CDC_CD_R;
2549     	if (!CDROM_CONFIG_FLAGS (drive)->cd_rw)
2550     		devinfo->mask |= CDC_CD_RW;
2551     	if (!CDROM_CONFIG_FLAGS (drive)->dvd)
2552     		devinfo->mask |= CDC_DVD;
2553     	if (!CDROM_CONFIG_FLAGS (drive)->dvd_r)
2554     		devinfo->mask |= CDC_DVD_R;
2555     	if (!CDROM_CONFIG_FLAGS (drive)->dvd_ram)
2556     		devinfo->mask |= CDC_DVD_RAM;
2557     	if (!CDROM_CONFIG_FLAGS (drive)->is_changer)
2558     		devinfo->mask |= CDC_SELECT_DISC;
2559     	if (!CDROM_CONFIG_FLAGS (drive)->audio_play)
2560     		devinfo->mask |= CDC_PLAY_AUDIO;
2561     	if (!CDROM_CONFIG_FLAGS (drive)->close_tray)
2562     		devinfo->mask |= CDC_CLOSE_TRAY;
2563     
2564     	devinfo->de = devfs_register(drive->de, "cd", DEVFS_FL_DEFAULT,
2565     				     HWIF(drive)->major, minor,
2566     				     S_IFBLK | S_IRUGO | S_IWUGO,
2567     				     ide_fops, NULL);
2568     
2569     	return register_cdrom(devinfo);
2570     }
2571     
2572     static
2573     int ide_cdrom_get_capabilities(ide_drive_t *drive, struct atapi_capabilities_page *cap)
2574     {
2575     	struct cdrom_info *info = drive->driver_data;
2576     	struct cdrom_device_info *cdi = &info->devinfo;
2577     	struct cdrom_generic_command cgc;
2578     	int stat, attempts = 3, size = sizeof(*cap);
2579     
2580     	/*
2581     	 * ACER50 (and others?) require the full spec length mode sense
2582     	 * page capabilities size, but older drives break.
2583     	 */
2584     	if (drive->id) {
2585     		if (!(!strcmp(drive->id->model, "ATAPI CD ROM DRIVE 50X MAX") ||
2586     		    !strcmp(drive->id->model, "WPI CDS-32X")))
2587     			size -= sizeof(cap->pad);
2588     	}
2589     
2590     	/* we have to cheat a little here. the packet will eventually
2591     	 * be queued with ide_cdrom_packet(), which extracts the
2592     	 * drive from cdi->handle. Since this device hasn't been
2593     	 * registered with the Uniform layer yet, it can't do this.
2594     	 * Same goes for cdi->ops.
2595     	 */
2596     	cdi->handle = (ide_drive_t *) drive;
2597     	cdi->ops = &ide_cdrom_dops;
2598     	init_cdrom_command(&cgc, cap, size, CGC_DATA_UNKNOWN);
2599     	do { /* we seem to get stat=0x01,err=0x00 the first time (??) */
2600     		stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CAPABILITIES_PAGE, 0);
2601     		if (!stat)
2602     			break;
2603     	} while (--attempts);
2604     	return stat;
2605     }
2606     
2607     static
2608     int ide_cdrom_probe_capabilities (ide_drive_t *drive)
2609     {
2610     	struct cdrom_info *info = drive->driver_data;
2611     	struct cdrom_device_info *cdi = &info->devinfo;
2612     	struct atapi_capabilities_page cap;
2613     	int nslots = 1;
2614     
2615     	if (CDROM_CONFIG_FLAGS (drive)->nec260) {
2616     		CDROM_CONFIG_FLAGS (drive)->no_eject = 0;                       
2617     		CDROM_CONFIG_FLAGS (drive)->audio_play = 1;       
2618     		return nslots;
2619     	}
2620     
2621     	if (ide_cdrom_get_capabilities(drive, &cap))
2622     		return 0;
2623     
2624     	if (cap.lock == 0)
2625     		CDROM_CONFIG_FLAGS (drive)->no_doorlock = 1;
2626     	if (cap.eject)
2627     		CDROM_CONFIG_FLAGS (drive)->no_eject = 0;
2628     	if (cap.cd_r_write)
2629     		CDROM_CONFIG_FLAGS (drive)->cd_r = 1;
2630     	if (cap.cd_rw_write)
2631     		CDROM_CONFIG_FLAGS (drive)->cd_rw = 1;
2632     	if (cap.test_write)
2633     		CDROM_CONFIG_FLAGS (drive)->test_write = 1;
2634     	if (cap.dvd_ram_read || cap.dvd_r_read || cap.dvd_rom)
2635     		CDROM_CONFIG_FLAGS (drive)->dvd = 1;
2636     	if (cap.dvd_ram_write)
2637     		CDROM_CONFIG_FLAGS (drive)->dvd_ram = 1;
2638     	if (cap.dvd_r_write)
2639     		CDROM_CONFIG_FLAGS (drive)->dvd_r = 1;
2640     	if (cap.audio_play)
2641     		CDROM_CONFIG_FLAGS (drive)->audio_play = 1;
2642     	if (cap.mechtype == mechtype_caddy || cap.mechtype == mechtype_popup)
2643     		CDROM_CONFIG_FLAGS (drive)->close_tray = 0;
2644     
2645     #if ! STANDARD_ATAPI
2646     	if (cdi->sanyo_slot > 0) {
2647     		CDROM_CONFIG_FLAGS (drive)->is_changer = 1;
2648     		nslots = 3;
2649     	}
2650     
2651     	else
2652     #endif /* not STANDARD_ATAPI */
2653     	if (cap.mechtype == mechtype_individual_changer ||
2654     	    cap.mechtype == mechtype_cartridge_changer) {
2655     		if ((nslots = cdrom_number_of_slots(cdi)) > 1) {
2656     			CDROM_CONFIG_FLAGS (drive)->is_changer = 1;
2657     			CDROM_CONFIG_FLAGS (drive)->supp_disc_present = 1;
2658     		}
2659     	}
2660     
2661     	/* The ACER/AOpen 24X cdrom has the speed fields byte-swapped */
2662     	if (drive->id && !drive->id->model[0] && !strncmp(drive->id->fw_rev, "241N", 4)) {
2663     		CDROM_STATE_FLAGS (drive)->current_speed  = 
2664     			(((unsigned int)cap.curspeed) + (176/2)) / 176;
2665     		CDROM_CONFIG_FLAGS (drive)->max_speed = 
2666     			(((unsigned int)cap.maxspeed) + (176/2)) / 176;
2667     	} else {
2668     		CDROM_STATE_FLAGS (drive)->current_speed  = 
2669     			(ntohs(cap.curspeed) + (176/2)) / 176;
2670     		CDROM_CONFIG_FLAGS (drive)->max_speed = 
2671     			(ntohs(cap.maxspeed) + (176/2)) / 176;
2672     	}
2673     
2674     	/* don't print speed if the drive reported 0.
2675     	 */
2676     	printk("%s: ATAPI", drive->name);
2677     	if (CDROM_CONFIG_FLAGS(drive)->max_speed)
2678     		printk(" %dX", CDROM_CONFIG_FLAGS(drive)->max_speed);
2679     	printk(" %s", CDROM_CONFIG_FLAGS(drive)->dvd ? "DVD-ROM" : "CD-ROM");
2680     
2681     	if (CDROM_CONFIG_FLAGS (drive)->dvd_r|CDROM_CONFIG_FLAGS (drive)->dvd_ram)
2682             	printk (" DVD%s%s", 
2683             	(CDROM_CONFIG_FLAGS (drive)->dvd_r)? "-R" : "", 
2684             	(CDROM_CONFIG_FLAGS (drive)->dvd_ram)? "-RAM" : "");
2685     
2686             if (CDROM_CONFIG_FLAGS (drive)->cd_r|CDROM_CONFIG_FLAGS (drive)->cd_rw) 
2687             	printk (" CD%s%s", 
2688             	(CDROM_CONFIG_FLAGS (drive)->cd_r)? "-R" : "", 
2689             	(CDROM_CONFIG_FLAGS (drive)->cd_rw)? "/RW" : "");
2690     
2691             if (CDROM_CONFIG_FLAGS (drive)->is_changer) 
2692             	printk (" changer w/%d slots", nslots);
2693             else 	
2694             	printk (" drive");
2695     
2696     	printk (", %dkB Cache", be16_to_cpu(cap.buffer_size));
2697     
2698     #ifdef CONFIG_BLK_DEV_IDEDMA
2699     	if (drive->using_dma)
2700     		(void) HWIF(drive)->dmaproc(ide_dma_verbose, drive);
2701     #endif /* CONFIG_BLK_DEV_IDEDMA */
2702     	printk("\n");
2703     
2704     	return nslots;
2705     }
2706     
2707     static void ide_cdrom_add_settings(ide_drive_t *drive)
2708     {
2709     	int major = HWIF(drive)->major;
2710     	int minor = drive->select.b.unit << PARTN_BITS;
2711     
2712     	ide_add_setting(drive,	"breada_readahead",	SETTING_RW, BLKRAGET, BLKRASET, TYPE_INT, 0, 255, 1, 2, &read_ahead[major], NULL);
2713     	ide_add_setting(drive,	"file_readahead",	SETTING_RW, BLKFRAGET, BLKFRASET, TYPE_INTA, 0, INT_MAX, 1, 1024, &max_readahead[major][minor],	NULL);
2714     	ide_add_setting(drive,	"max_kb_per_request",	SETTING_RW, BLKSECTGET, BLKSECTSET, TYPE_INTA, 1, 255, 1, 2, &max_sectors[major][minor], NULL);
2715     	ide_add_setting(drive,	"dsc_overlap",		SETTING_RW, -1, -1, TYPE_BYTE, 0, 1, 1,	1, &drive->dsc_overlap, NULL);
2716     }
2717     
2718     static
2719     int ide_cdrom_setup (ide_drive_t *drive)
2720     {
2721     	struct cdrom_info *info = drive->driver_data;
2722     	struct cdrom_device_info *cdi = &info->devinfo;
2723     	int minor = drive->select.b.unit << PARTN_BITS;
2724     	int nslots;
2725     
2726     	/*
2727     	 * default to read-only always and fix latter at the bottom
2728     	 */
2729     	set_device_ro(MKDEV(HWIF(drive)->major, minor), 1);
2730     	set_blocksize(MKDEV(HWIF(drive)->major, minor), CD_FRAMESIZE);
2731     
2732     	drive->special.all	= 0;
2733     	drive->ready_stat	= 0;
2734     
2735     	CDROM_STATE_FLAGS (drive)->media_changed = 1;
2736     	CDROM_STATE_FLAGS (drive)->toc_valid     = 0;
2737     	CDROM_STATE_FLAGS (drive)->door_locked   = 0;
2738     
2739     #if NO_DOOR_LOCKING
2740     	CDROM_CONFIG_FLAGS (drive)->no_doorlock = 1;
2741     #else
2742     	CDROM_CONFIG_FLAGS (drive)->no_doorlock = 0;
2743     #endif
2744     
2745     	if (drive->id != NULL)
2746     		CDROM_CONFIG_FLAGS (drive)->drq_interrupt =
2747     			((drive->id->config & 0x0060) == 0x20);
2748     	else
2749     		CDROM_CONFIG_FLAGS (drive)->drq_interrupt = 0;
2750     
2751     	CDROM_CONFIG_FLAGS (drive)->is_changer = 0;
2752     	CDROM_CONFIG_FLAGS (drive)->cd_r = 0;
2753     	CDROM_CONFIG_FLAGS (drive)->cd_rw = 0;
2754     	CDROM_CONFIG_FLAGS (drive)->test_write = 0;
2755     	CDROM_CONFIG_FLAGS (drive)->dvd = 0;
2756     	CDROM_CONFIG_FLAGS (drive)->dvd_r = 0;
2757     	CDROM_CONFIG_FLAGS (drive)->dvd_ram = 0;
2758     	CDROM_CONFIG_FLAGS (drive)->no_eject = 1;
2759     	CDROM_CONFIG_FLAGS (drive)->supp_disc_present = 0;
2760     	CDROM_CONFIG_FLAGS (drive)->audio_play = 0;
2761     	CDROM_CONFIG_FLAGS (drive)->close_tray = 1;
2762     	
2763     	/* limit transfer size per interrupt. */
2764     	CDROM_CONFIG_FLAGS (drive)->limit_nframes = 0;
2765     	if (drive->id != NULL) {
2766     		/* a testament to the nice quality of Samsung drives... */
2767     		if (!strcmp(drive->id->model, "SAMSUNG CD-ROM SCR-2430"))
2768     			CDROM_CONFIG_FLAGS (drive)->limit_nframes = 1;
2769     		else if (!strcmp(drive->id->model, "SAMSUNG CD-ROM SCR-2432"))
2770     			CDROM_CONFIG_FLAGS (drive)->limit_nframes = 1;
2771     		/* the 3231 model does not support the SET_CD_SPEED command */
2772     		else if (!strcmp(drive->id->model, "SAMSUNG CD-ROM SCR-3231"))
2773     			cdi->mask |= CDC_SELECT_SPEED;
2774     	}
2775     
2776     #if ! STANDARD_ATAPI
2777     	/* by default Sanyo 3 CD changer support is turned off and
2778                ATAPI Rev 2.2+ standard support for CD changers is used */
2779     	cdi->sanyo_slot = 0;
2780     
2781     	CDROM_CONFIG_FLAGS (drive)->nec260 = 0;
2782     	CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd = 0;
2783     	CDROM_CONFIG_FLAGS (drive)->tocaddr_as_bcd = 0;
2784     	CDROM_CONFIG_FLAGS (drive)->playmsf_as_bcd = 0;
2785     	CDROM_CONFIG_FLAGS (drive)->subchan_as_bcd = 0;
2786     
2787     	if (drive->id != NULL) {
2788     		if (strcmp (drive->id->model, "V003S0DS") == 0 &&
2789     		    drive->id->fw_rev[4] == '1' &&
2790     		    drive->id->fw_rev[6] <= '2') {
2791     			/* Vertos 300.
2792     			   Some versions of this drive like to talk BCD. */
2793     			CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd = 1;
2794     			CDROM_CONFIG_FLAGS (drive)->tocaddr_as_bcd = 1;
2795     			CDROM_CONFIG_FLAGS (drive)->playmsf_as_bcd = 1;
2796     			CDROM_CONFIG_FLAGS (drive)->subchan_as_bcd = 1;
2797     		}
2798     
2799     		else if (strcmp (drive->id->model, "V006E0DS") == 0 &&
2800     		    drive->id->fw_rev[4] == '1' &&
2801     		    drive->id->fw_rev[6] <= '2') {
2802     			/* Vertos 600 ESD. */
2803     			CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd = 1;
2804     		}
2805     
2806     		else if (strcmp (drive->id->model,
2807     				 "NEC CD-ROM DRIVE:260") == 0 &&
2808     			 strncmp (drive->id->fw_rev, "1.01", 4) == 0) { /* FIXME */
2809     			/* Old NEC260 (not R).
2810     			   This drive was released before the 1.2 version
2811     			   of the spec. */
2812     			CDROM_CONFIG_FLAGS (drive)->tocaddr_as_bcd = 1;
2813     			CDROM_CONFIG_FLAGS (drive)->playmsf_as_bcd = 1;
2814     			CDROM_CONFIG_FLAGS (drive)->subchan_as_bcd = 1;
2815     			CDROM_CONFIG_FLAGS (drive)->nec260         = 1;
2816     		}
2817     
2818     		else if (strcmp (drive->id->model, "WEARNES CDD-120") == 0 &&
2819     			 strncmp (drive->id->fw_rev, "A1.1", 4) == 0) { /* FIXME */
2820     			/* Wearnes */
2821     			CDROM_CONFIG_FLAGS (drive)->playmsf_as_bcd = 1;
2822     			CDROM_CONFIG_FLAGS (drive)->subchan_as_bcd = 1;
2823     		}
2824     
2825                     /* Sanyo 3 CD changer uses a non-standard command
2826                         for CD changing */
2827                      else if ((strcmp(drive->id->model, "CD-ROM CDR-C3 G") == 0) ||
2828                              (strcmp(drive->id->model, "CD-ROM CDR-C3G") == 0) ||
2829                              (strcmp(drive->id->model, "CD-ROM CDR_C36") == 0)) {
2830                             /* uses CD in slot 0 when value is set to 3 */
2831                             cdi->sanyo_slot = 3;
2832                     }
2833     
2834     
2835     	}
2836     #endif /* not STANDARD_ATAPI */
2837     
2838     	info->toc		= NULL;
2839     	info->buffer		= NULL;
2840     	info->sector_buffered	= 0;
2841     	info->nsectors_buffered	= 0;
2842     	info->changer_info      = NULL;
2843     	info->last_block	= 0;
2844     	info->start_seek	= 0;
2845     
2846     	nslots = ide_cdrom_probe_capabilities (drive);
2847     
2848     	if (CDROM_CONFIG_FLAGS(drive)->dvd_ram)
2849     		set_device_ro(MKDEV(HWIF(drive)->major, minor), 0);
2850     
2851     	if (ide_cdrom_register (drive, nslots)) {
2852     		printk ("%s: ide_cdrom_setup failed to register device with the cdrom driver.\n", drive->name);
2853     		info->devinfo.handle = NULL;
2854     		return 1;
2855     	}
2856     	ide_cdrom_add_settings(drive);
2857     	return 0;
2858     }
2859     
2860     /* Forwarding functions to generic routines. */
2861     static
2862     int ide_cdrom_ioctl (ide_drive_t *drive,
2863     		     struct inode *inode, struct file *file,
2864     		     unsigned int cmd, unsigned long arg)
2865     {
2866     	return cdrom_fops.ioctl (inode, file, cmd, arg);
2867     }
2868     
2869     static
2870     int ide_cdrom_open (struct inode *ip, struct file *fp, ide_drive_t *drive)
2871     {
2872     	struct cdrom_info *info = drive->driver_data;
2873     	int rc = -ENOMEM;
2874     
2875     	MOD_INC_USE_COUNT;
2876     	if (info->buffer == NULL)
2877     		info->buffer = (char *) kmalloc(SECTOR_BUFFER_SIZE, GFP_KERNEL);
2878             if ((info->buffer == NULL) || (rc = cdrom_fops.open(ip, fp))) {
2879     		drive->usage--;
2880     		MOD_DEC_USE_COUNT;
2881     	}
2882     	return rc;
2883     }
2884     
2885     static
2886     void ide_cdrom_release (struct inode *inode, struct file *file,
2887     			ide_drive_t *drive)
2888     {
2889     	cdrom_fops.release (inode, file);
2890     	MOD_DEC_USE_COUNT;
2891     }
2892     
2893     static
2894     int ide_cdrom_check_media_change (ide_drive_t *drive)
2895     {
2896     	return cdrom_fops.check_media_change(MKDEV (HWIF (drive)->major,
2897     			(drive->select.b.unit) << PARTN_BITS));
2898     }
2899     
2900     static
2901     void ide_cdrom_revalidate (ide_drive_t *drive)
2902     {
2903     	struct cdrom_info *info = drive->driver_data;
2904     	struct atapi_toc *toc;
2905     	int minor = drive->select.b.unit << PARTN_BITS;
2906     	struct request_sense sense;
2907     
2908     	cdrom_read_toc(drive, &sense);
2909     
2910     	if (!CDROM_STATE_FLAGS(drive)->toc_valid)
2911     		return;
2912     
2913     	toc = info->toc;
2914     
2915     	/* for general /dev/cdrom like mounting, one big disc */
2916     	drive->part[0].nr_sects = toc->capacity * SECTORS_PER_FRAME;
2917     	HWIF(drive)->gd->sizes[minor] = toc->capacity * BLOCKS_PER_FRAME;
2918     
2919     	/*
2920     	 * reset block size, ide_revalidate_disk incorrectly sets it to
2921     	 * 1024 even for CDROM's
2922     	 */
2923     	blk_size[HWIF(drive)->major] = HWIF(drive)->gd->sizes;
2924     	set_blocksize(MKDEV(HWIF(drive)->major, minor), CD_FRAMESIZE);
2925     }
2926     
2927     static
2928     unsigned long ide_cdrom_capacity (ide_drive_t *drive)
2929     {
2930     	unsigned long capacity;
2931     
2932     	if (cdrom_read_capacity(drive, &capacity, NULL))
2933     		return 0;
2934     
2935     	return capacity * SECTORS_PER_FRAME;
2936     }
2937     
2938     static
2939     int ide_cdrom_cleanup(ide_drive_t *drive)
2940     {
2941     	struct cdrom_info *info = drive->driver_data;
2942     	struct cdrom_device_info *devinfo = &info->devinfo;
2943     
2944     	if (ide_unregister_subdriver (drive))
2945     		return 1;
2946     	if (info->buffer != NULL)
2947     		kfree(info->buffer);
2948     	if (info->toc != NULL)
2949     		kfree(info->toc);
2950     	if (info->changer_info != NULL)
2951     		kfree(info->changer_info);
2952     	if (devinfo->handle == drive && unregister_cdrom (devinfo))
2953     		printk ("%s: ide_cdrom_cleanup failed to unregister device from the cdrom driver.\n", drive->name);
2954     	kfree(info);
2955     	drive->driver_data = NULL;
2956     	return 0;
2957     }
2958     
2959     static
2960     int ide_cdrom_reinit (ide_drive_t *drive)
2961     {
2962     	return 0;
2963     }
2964     
2965     static ide_driver_t ide_cdrom_driver = {
2966     	name:			"ide-cdrom",
2967     	version:		IDECD_VERSION,
2968     	media:			ide_cdrom,
2969     	busy:			0,
2970     	supports_dma:		1,
2971     	supports_dsc_overlap:	1,
2972     	cleanup:		ide_cdrom_cleanup,
2973     	do_request:		ide_do_rw_cdrom,
2974     	end_request:		NULL,
2975     	ioctl:			ide_cdrom_ioctl,
2976     	open:			ide_cdrom_open,
2977     	release:		ide_cdrom_release,
2978     	media_change:		ide_cdrom_check_media_change,
2979     	revalidate:		ide_cdrom_revalidate,
2980     	pre_reset:		NULL,
2981     	capacity:		ide_cdrom_capacity,
2982     	special:		NULL,
2983     	proc:			NULL,
2984     	driver_reinit:		ide_cdrom_reinit,
2985     };
2986     
2987     int ide_cdrom_init(void);
2988     static ide_module_t ide_cdrom_module = {
2989     	IDE_DRIVER_MODULE,
2990     	ide_cdrom_init,
2991     	&ide_cdrom_driver,
2992     	NULL
2993     };
2994     
2995     /* options */
2996     char *ignore = NULL;
2997     
2998     MODULE_PARM(ignore, "s");
2999     MODULE_DESCRIPTION("ATAPI CD-ROM Driver");
3000     
3001     static void __exit ide_cdrom_exit(void)
3002     {
3003     	ide_drive_t *drive;
3004     	int failed = 0;
3005     
3006     	while ((drive = ide_scan_devices (ide_cdrom, ide_cdrom_driver.name, &ide_cdrom_driver, failed)) != NULL)
3007     		if (ide_cdrom_cleanup (drive)) {
3008     			printk ("%s: cleanup_module() called while still busy\n", drive->name);
3009     			failed++;
3010     		}
3011     	ide_unregister_module (&ide_cdrom_module);
3012     }
3013      
3014     int ide_cdrom_init(void)
3015     {
3016     	ide_drive_t *drive;
3017     	struct cdrom_info *info;
3018     	int failed = 0;
3019     
3020     	MOD_INC_USE_COUNT;
3021     	while ((drive = ide_scan_devices (ide_cdrom, ide_cdrom_driver.name, NULL, failed++)) != NULL) {
3022     		/* skip drives that we were told to ignore */
3023     		if (ignore != NULL) {
3024     			if (strstr(ignore, drive->name)) {
3025     				printk("ide-cd: ignoring drive %s\n", drive->name);
3026     				continue;
3027     			}
3028     		}
3029     		if (drive->scsi) {
3030     			printk("ide-cd: passing drive %s to ide-scsi emulation.\n", drive->name);
3031     			continue;
3032     		}
3033     		info = (struct cdrom_info *) kmalloc (sizeof (struct cdrom_info), GFP_KERNEL);
3034     		if (info == NULL) {
3035     			printk ("%s: Can't allocate a cdrom structure\n", drive->name);
3036     			continue;
3037     		}
3038     		if (ide_register_subdriver (drive, &ide_cdrom_driver, IDE_SUBDRIVER_VERSION)) {
3039     			printk ("%s: Failed to register the driver with ide.c\n", drive->name);
3040     			kfree (info);
3041     			continue;
3042     		}
3043     		memset (info, 0, sizeof (struct cdrom_info));
3044     		drive->driver_data = info;
3045     		DRIVER(drive)->busy++;
3046     		if (ide_cdrom_setup (drive)) {
3047     			DRIVER(drive)->busy--;
3048     			if (ide_cdrom_cleanup (drive))
3049     				printk ("%s: ide_cdrom_cleanup failed in ide_cdrom_init\n", drive->name);
3050     			continue;
3051     		}
3052     		DRIVER(drive)->busy--;
3053     		failed--;
3054     	}
3055     	ide_register_module(&ide_cdrom_module);
3056     	MOD_DEC_USE_COUNT;
3057     	return 0;
3058     }
3059     
3060     module_init(ide_cdrom_init);
3061     module_exit(ide_cdrom_exit);
3062