1
2
3#define IDECD_VERSION "4.61"
4
5#include <linux/module.h>
6#include <linux/types.h>
7#include <linux/kernel.h>
8#include <linux/delay.h>
9#include <linux/timer.h>
10#include <linux/slab.h>
11#include <linux/interrupt.h>
12#include <linux/errno.h>
13#include <linux/cdrom.h>
14#include <linux/ide.h>
15#include <linux/completion.h>
16#include <linux/mutex.h>
17
18#include <scsi/scsi.h>	/* For SCSI -> ATAPI command conversion */
19
20#include <asm/irq.h>
21#include <asm/io.h>
22#include <asm/byteorder.h>
23#include <asm/uaccess.h>
24#include <asm/unaligned.h>
25
26#include "ide-cd.h"
27
28static DEFINE_MUTEX(idecd_ref_mutex);
29
30#define to_ide_cd(obj) container_of(obj, struct cdrom_info, kref)
31
32#define ide_cd_g(disk) \
33	container_of((disk)->private_data, struct cdrom_info, driver)
34
35static struct cdrom_info *ide_cd_get(struct gendisk *disk)
36{
37	struct cdrom_info *cd = NULL;
38
39	mutex_lock(&idecd_ref_mutex);
40	cd = ide_cd_g(disk);
41	if (cd)
42		kref_get(&cd->kref);
43	mutex_unlock(&idecd_ref_mutex);
44	return cd;
45}
46
47static void ide_cd_release(struct kref *);
48
49static void ide_cd_put(struct cdrom_info *cd)
50{
51	mutex_lock(&idecd_ref_mutex);
52	kref_put(&cd->kref, ide_cd_release);
53	mutex_unlock(&idecd_ref_mutex);
54}
55
56/****************************************************************************
57 * Generic packet command support and error handling routines.
58 */
59
60/* Mark that we've seen a media change, and invalidate our internal
61   buffers. */
62static void cdrom_saw_media_change (ide_drive_t *drive)
63{
64	struct cdrom_info *info = drive->driver_data;
65
66	CDROM_STATE_FLAGS (drive)->media_changed = 1;
67	CDROM_STATE_FLAGS (drive)->toc_valid = 0;
68	info->nsectors_buffered = 0;
69}
70
71static int cdrom_log_sense(ide_drive_t *drive, struct request *rq,
72			   struct request_sense *sense)
73{
74	int log = 0;
75
76	if (!sense || !rq || (rq->cmd_flags & REQ_QUIET))
77		return 0;
78
79	switch (sense->sense_key) {
80		case NO_SENSE: case RECOVERED_ERROR:
81			break;
82		case NOT_READY:
83			/*
84			 * don't care about tray state messages for
85			 * e.g. capacity commands or in-progress or
86			 * becoming ready
87			 */
88			if (sense->asc == 0x3a || sense->asc == 0x04)
89				break;
90			log = 1;
91			break;
92		case ILLEGAL_REQUEST:
93			/*
94			 * don't log START_STOP unit with LoEj set, since
95			 * we cannot reliably check if drive can auto-close
96			 */
97			if (rq->cmd[0] == GPCMD_START_STOP_UNIT && sense->asc == 0x24)
98				break;
99			log = 1;
100			break;
101		case UNIT_ATTENTION:
102			/*
103			 * Make good and sure we've seen this potential media
104			 * change. Some drives (i.e. Creative) fail to present
105			 * the correct sense key in the error register.
106			 */
107			cdrom_saw_media_change(drive);
108			break;
109		default:
110			log = 1;
111			break;
112	}
113	return log;
114}
115
116static
117void cdrom_analyze_sense_data(ide_drive_t *drive,
118			      struct request *failed_command,
119			      struct request_sense *sense)
120{
121	unsigned long sector;
122	unsigned long bio_sectors;
123	unsigned long valid;
124	struct cdrom_info *info = drive->driver_data;
125
126	if (!cdrom_log_sense(drive, failed_command, sense))
127		return;
128
129	/*
130	 * If a read toc is executed for a CD-R or CD-RW medium where
131	 * the first toc has not been recorded yet, it will fail with
132	 * 05/24/00 (which is a confusing error)
133	 */
134	if (failed_command && failed_command->cmd[0] == GPCMD_READ_TOC_PMA_ATIP)
135		if (sense->sense_key == 0x05 && sense->asc == 0x24)
136			return;
137
138 	if (sense->error_code == 0x70) {	/* Current Error */
139 		switch(sense->sense_key) {
140		case MEDIUM_ERROR:
141		case VOLUME_OVERFLOW:
142		case ILLEGAL_REQUEST:
143			if (!sense->valid)
144				break;
145			if (failed_command == NULL ||
146					!blk_fs_request(failed_command))
147				break;
148			sector = (sense->information[0] << 24) |
149				 (sense->information[1] << 16) |
150				 (sense->information[2] <<  8) |
151				 (sense->information[3]);
152
153			bio_sectors = bio_sectors(failed_command->bio);
154			if (bio_sectors < 4)
155				bio_sectors = 4;
156			if (drive->queue->hardsect_size == 2048)
157				sector <<= 2;	/* Device sector size is 2K */
158			sector &= ~(bio_sectors -1);
159			valid = (sector - failed_command->sector) << 9;
160
161			if (valid < 0)
162				valid = 0;
163			if (sector < get_capacity(info->disk) &&
164				drive->probed_capacity - sector < 4 * 75) {
165				set_capacity(info->disk, sector);
166			}
167 		}
168 	}
169#if VERBOSE_IDE_CD_ERRORS
170	{
171		int i;
172		const char *s = "bad sense key!";
173		char buf[80];
174
175		printk ("ATAPI device %s:\n", drive->name);
176		if (sense->error_code==0x70)
177			printk("  Error: ");
178		else if (sense->error_code==0x71)
179			printk("  Deferred Error: ");
180		else if (sense->error_code == 0x7f)
181			printk("  Vendor-specific Error: ");
182		else
183			printk("  Unknown Error Type: ");
184
185		if (sense->sense_key < ARY_LEN(sense_key_texts))
186			s = sense_key_texts[sense->sense_key];
187
188		printk("%s -- (Sense key=0x%02x)\n", s, sense->sense_key);
189
190		if (sense->asc == 0x40) {
191			sprintf(buf, "Diagnostic failure on component 0x%02x",
192				 sense->ascq);
193			s = buf;
194		} else {
195			int lo = 0, mid, hi = ARY_LEN(sense_data_texts);
196			unsigned long key = (sense->sense_key << 16);
197			key |= (sense->asc << 8);
198			if (!(sense->ascq >= 0x80 && sense->ascq <= 0xdd))
199				key |= sense->ascq;
200			s = NULL;
201
202			while (hi > lo) {
203				mid = (lo + hi) / 2;
204				if (sense_data_texts[mid].asc_ascq == key ||
205				    sense_data_texts[mid].asc_ascq == (0xff0000|key)) {
206					s = sense_data_texts[mid].text;
207					break;
208				}
209				else if (sense_data_texts[mid].asc_ascq > key)
210					hi = mid;
211				else
212					lo = mid+1;
213			}
214		}
215
216		if (s == NULL) {
217			if (sense->asc > 0x80)
218				s = "(vendor-specific error)";
219			else
220				s = "(reserved error code)";
221		}
222
223		printk(KERN_ERR "  %s -- (asc=0x%02x, ascq=0x%02x)\n",
224			s, sense->asc, sense->ascq);
225
226		if (failed_command != NULL) {
227
228			int lo=0, mid, hi= ARY_LEN (packet_command_texts);
229			s = NULL;
230
231			while (hi > lo) {
232				mid = (lo + hi) / 2;
233				if (packet_command_texts[mid].packet_command ==
234				    failed_command->cmd[0]) {
235					s = packet_command_texts[mid].text;
236					break;
237				}
238				if (packet_command_texts[mid].packet_command >
239				    failed_command->cmd[0])
240					hi = mid;
241				else
242					lo = mid+1;
243			}
244
245			printk (KERN_ERR "  The failed \"%s\" packet command was: \n  \"", s);
246			for (i=0; i<sizeof (failed_command->cmd); i++)
247				printk ("%02x ", failed_command->cmd[i]);
248			printk ("\"\n");
249		}
250
251		/* The SKSV bit specifies validity of the sense_key_specific
252		 * in the next two commands. It is bit 7 of the first byte.
253		 * In the case of NOT_READY, if SKSV is set the drive can
254		 * give us nice ETA readings.
255		 */
256		if (sense->sense_key == NOT_READY && (sense->sks[0] & 0x80)) {
257			int progress = (sense->sks[1] << 8 | sense->sks[2]) * 100;
258			printk(KERN_ERR "  Command is %02d%% complete\n", progress / 0xffff);
259
260		}
261
262		if (sense->sense_key == ILLEGAL_REQUEST &&
263		    (sense->sks[0] & 0x80) != 0) {
264			printk(KERN_ERR "  Error in %s byte %d",
265				(sense->sks[0] & 0x40) != 0 ?
266				"command packet" : "command data",
267				(sense->sks[1] << 8) + sense->sks[2]);
268
269			if ((sense->sks[0] & 0x40) != 0)
270				printk (" bit %d", sense->sks[0] & 0x07);
271
272			printk ("\n");
273		}
274	}
275
276#else /* not VERBOSE_IDE_CD_ERRORS */
277
278	/* Suppress printing unit attention and `in progress of becoming ready'
279	   errors when we're not being verbose. */
280
281	if (sense->sense_key == UNIT_ATTENTION ||
282	    (sense->sense_key == NOT_READY && (sense->asc == 4 ||
283						sense->asc == 0x3a)))
284		return;
285
286	printk(KERN_ERR "%s: error code: 0x%02x  sense_key: 0x%02x  asc: 0x%02x  ascq: 0x%02x\n",
287		drive->name,
288		sense->error_code, sense->sense_key,
289		sense->asc, sense->ascq);
290#endif /* not VERBOSE_IDE_CD_ERRORS */
291}
292
293/*
294 * Initialize a ide-cd packet command request
295 */
296static void cdrom_prepare_request(ide_drive_t *drive, struct request *rq)
297{
298	struct cdrom_info *cd = drive->driver_data;
299
300	ide_init_drive_cmd(rq);
301	rq->cmd_type = REQ_TYPE_ATA_PC;
302	rq->rq_disk = cd->disk;
303}
304
305static void cdrom_queue_request_sense(ide_drive_t *drive, void *sense,
306				      struct request *failed_command)
307{
308	struct cdrom_info *info		= drive->driver_data;
309	struct request *rq		= &info->request_sense_request;
310
311	if (sense == NULL)
312		sense = &info->sense_data;
313
314	/* stuff the sense request in front of our current request */
315	cdrom_prepare_request(drive, rq);
316
317	rq->data = sense;
318	rq->cmd[0] = GPCMD_REQUEST_SENSE;
319	rq->cmd[4] = rq->data_len = 18;
320
321	rq->cmd_type = REQ_TYPE_SENSE;
322
323	/* NOTE! Save the failed command in "rq->buffer" */
324	rq->buffer = (void *) failed_command;
325
326	(void) ide_do_drive_cmd(drive, rq, ide_preempt);
327}
328
329static void cdrom_end_request (ide_drive_t *drive, int uptodate)
330{
331	struct request *rq = HWGROUP(drive)->rq;
332	int nsectors = rq->hard_cur_sectors;
333
334	if (blk_sense_request(rq) && uptodate) {
335		/*
336		 * For REQ_TYPE_SENSE, "rq->buffer" points to the original
337		 * failed request
338		 */
339		struct request *failed = (struct request *) rq->buffer;
340		struct cdrom_info *info = drive->driver_data;
341		void *sense = &info->sense_data;
342		unsigned long flags;
343
344		if (failed) {
345			if (failed->sense) {
346				sense = failed->sense;
347				failed->sense_len = rq->sense_len;
348			}
349			cdrom_analyze_sense_data(drive, failed, sense);
350			/*
351			 * now end failed request
352			 */
353			if (blk_fs_request(failed)) {
354				if (ide_end_dequeued_request(drive, failed, 0,
355						failed->hard_nr_sectors))
356					BUG();
357			} else {
358				spin_lock_irqsave(&ide_lock, flags);
359				end_that_request_chunk(failed, 0,
360							failed->data_len);
361				end_that_request_last(failed, 0);
362				spin_unlock_irqrestore(&ide_lock, flags);
363			}
364		} else
365			cdrom_analyze_sense_data(drive, NULL, sense);
366	}
367
368	if (!rq->current_nr_sectors && blk_fs_request(rq))
369		uptodate = 1;
370	/* make sure it's fully ended */
371	if (blk_pc_request(rq))
372		nsectors = (rq->data_len + 511) >> 9;
373	if (!nsectors)
374		nsectors = 1;
375
376	ide_end_request(drive, uptodate, nsectors);
377}
378
379static void ide_dump_status_no_sense(ide_drive_t *drive, const char *msg, u8 stat)
380{
381	if (stat & 0x80)
382		return;
383	ide_dump_status(drive, msg, stat);
384}
385
386/* Returns 0 if the request should be continued.
387   Returns 1 if the request was ended. */
388static int cdrom_decode_status(ide_drive_t *drive, int good_stat, int *stat_ret)
389{
390	struct request *rq = HWGROUP(drive)->rq;
391	int stat, err, sense_key;
392
393	/* Check for errors. */
394	stat = HWIF(drive)->INB(IDE_STATUS_REG);
395	if (stat_ret)
396		*stat_ret = stat;
397
398	if (OK_STAT(stat, good_stat, BAD_R_STAT))
399		return 0;
400
401	/* Get the IDE error register. */
402	err = HWIF(drive)->INB(IDE_ERROR_REG);
403	sense_key = err >> 4;
404
405	if (rq == NULL) {
406		printk("%s: missing rq in cdrom_decode_status\n", drive->name);
407		return 1;
408	}
409
410	if (blk_sense_request(rq)) {
411		/* We got an error trying to get sense info
412		   from the drive (probably while trying
413		   to recover from a former error).  Just give up. */
414
415		rq->cmd_flags |= REQ_FAILED;
416		cdrom_end_request(drive, 0);
417		ide_error(drive, "request sense failure", stat);
418		return 1;
419
420	} else if (blk_pc_request(rq) || rq->cmd_type == REQ_TYPE_ATA_PC) {
421		/* All other functions, except for READ. */
422		unsigned long flags;
423
424		/*
425		 * if we have an error, pass back CHECK_CONDITION as the
426		 * scsi status byte
427		 */
428		if (blk_pc_request(rq) && !rq->errors)
429			rq->errors = SAM_STAT_CHECK_CONDITION;
430
431		/* Check for tray open. */
432		if (sense_key == NOT_READY) {
433			cdrom_saw_media_change (drive);
434		} else if (sense_key == UNIT_ATTENTION) {
435			/* Check for media change. */
436			cdrom_saw_media_change (drive);
437			/*printk("%s: media changed\n",drive->name);*/
438			return 0;
439 		} else if ((sense_key == ILLEGAL_REQUEST) &&
440 			   (rq->cmd[0] == GPCMD_START_STOP_UNIT)) {
441 			/*
442 			 * Don't print error message for this condition--
443 			 * SFF8090i indicates that 5/24/00 is the correct
444 			 * response to a request to close the tray if the
445 			 * drive doesn't have that capability.
446 			 * cdrom_log_sense() knows this!
447 			 */
448		} else if (!(rq->cmd_flags & REQ_QUIET)) {
449			/* Otherwise, print an error. */
450			ide_dump_status(drive, "packet command error", stat);
451		}
452
453		rq->cmd_flags |= REQ_FAILED;
454
455		/*
456		 * instead of playing games with moving completions around,
457		 * remove failed request completely and end it when the
458		 * request sense has completed
459		 */
460		if (stat & ERR_STAT) {
461			spin_lock_irqsave(&ide_lock, flags);
462			blkdev_dequeue_request(rq);
463			HWGROUP(drive)->rq = NULL;
464			spin_unlock_irqrestore(&ide_lock, flags);
465
466			cdrom_queue_request_sense(drive, rq->sense, rq);
467		} else
468			cdrom_end_request(drive, 0);
469
470	} else if (blk_fs_request(rq)) {
471		int do_end_request = 0;
472
473		/* Handle errors from READ and WRITE requests. */
474
475		if (blk_noretry_request(rq))
476			do_end_request = 1;
477
478		if (sense_key == NOT_READY) {
479			/* Tray open. */
480			if (rq_data_dir(rq) == READ) {
481				cdrom_saw_media_change (drive);
482
483				/* Fail the request. */
484				printk ("%s: tray open\n", drive->name);
485				do_end_request = 1;
486			} else {
487				struct cdrom_info *info = drive->driver_data;
488
489				/* allow the drive 5 seconds to recover, some
490				 * devices will return this error while flushing
491				 * data from cache */
492				if (!rq->errors)
493					info->write_timeout = jiffies + ATAPI_WAIT_WRITE_BUSY;
494				rq->errors = 1;
495				if (time_after(jiffies, info->write_timeout))
496					do_end_request = 1;
497				else {
498					unsigned long flags;
499
500					/*
501					 * take a breather relying on the
502					 * unplug timer to kick us again
503					 */
504					spin_lock_irqsave(&ide_lock, flags);
505					blk_plug_device(drive->queue);
506					spin_unlock_irqrestore(&ide_lock,flags);
507					return 1;
508				}
509			}
510		} else if (sense_key == UNIT_ATTENTION) {
511			/* Media change. */
512			cdrom_saw_media_change (drive);
513
514			/* Arrange to retry the request.
515			   But be sure to give up if we've retried
516			   too many times. */
517			if (++rq->errors > ERROR_MAX)
518				do_end_request = 1;
519		} else if (sense_key == ILLEGAL_REQUEST ||
520			   sense_key == DATA_PROTECT) {
521			/* No point in retrying after an illegal
522			   request or data protect error.*/
523			ide_dump_status_no_sense (drive, "command error", stat);
524			do_end_request = 1;
525		} else if (sense_key == MEDIUM_ERROR) {
526			/* No point in re-trying a zillion times on a bad
527			 * sector...  If we got here the error is not correctable */
528			ide_dump_status_no_sense (drive, "media error (bad sector)", stat);
529			do_end_request = 1;
530		} else if (sense_key == BLANK_CHECK) {
531			/* Disk appears blank ?? */
532			ide_dump_status_no_sense (drive, "media error (blank)", stat);
533			do_end_request = 1;
534		} else if ((err & ~ABRT_ERR) != 0) {
535			/* Go to the default handler
536			   for other errors. */
537			ide_error(drive, "cdrom_decode_status", stat);
538			return 1;
539		} else if ((++rq->errors > ERROR_MAX)) {
540			/* We've racked up too many retries.  Abort. */
541			do_end_request = 1;
542		}
543
544		/* End a request through request sense analysis when we have
545		   sense data. We need this in order to perform end of media
546		   processing */
547
548		if (do_end_request) {
549			if (stat & ERR_STAT) {
550				unsigned long flags;
551				spin_lock_irqsave(&ide_lock, flags);
552				blkdev_dequeue_request(rq);
553				HWGROUP(drive)->rq = NULL;
554				spin_unlock_irqrestore(&ide_lock, flags);
555
556				cdrom_queue_request_sense(drive, rq->sense, rq);
557			} else
558				cdrom_end_request(drive, 0);
559		} else {
560			/* If we got a CHECK_CONDITION status,
561			   queue a request sense command. */
562			if (stat & ERR_STAT)
563				cdrom_queue_request_sense(drive, NULL, NULL);
564		}
565	} else {
566		blk_dump_rq_flags(rq, "ide-cd: bad rq");
567		cdrom_end_request(drive, 0);
568	}
569
570	/* Retry, or handle the next request. */
571	return 1;
572}
573
574static int cdrom_timer_expiry(ide_drive_t *drive)
575{
576	struct request *rq = HWGROUP(drive)->rq;
577	unsigned long wait = 0;
578
579	/*
580	 * Some commands are *slow* and normally take a long time to
581	 * complete. Usually we can use the ATAPI "disconnect" to bypass
582	 * this, but not all commands/drives support that. Let
583	 * ide_timer_expiry keep polling us for these.
584	 */
585	switch (rq->cmd[0]) {
586		case GPCMD_BLANK:
587		case GPCMD_FORMAT_UNIT:
588		case GPCMD_RESERVE_RZONE_TRACK:
589		case GPCMD_CLOSE_TRACK:
590		case GPCMD_FLUSH_CACHE:
591			wait = ATAPI_WAIT_PC;
592			break;
593		default:
594			if (!(rq->cmd_flags & REQ_QUIET))
595				printk(KERN_INFO "ide-cd: cmd 0x%x timed out\n", rq->cmd[0]);
596			wait = 0;
597			break;
598	}
599	return wait;
600}
601
602/* Set up the device registers for transferring a packet command on DEV,
603   expecting to later transfer XFERLEN bytes.  HANDLER is the routine
604   which actually transfers the command to the drive.  If this is a
605   drq_interrupt device, this routine will arrange for HANDLER to be
606   called when the interrupt from the drive arrives.  Otherwise, HANDLER
607   will be called immediately after the drive is prepared for the transfer. */
608
609static ide_startstop_t cdrom_start_packet_command(ide_drive_t *drive,
610						  int xferlen,
611						  ide_handler_t *handler)
612{
613	ide_startstop_t startstop;
614	struct cdrom_info *info = drive->driver_data;
615	ide_hwif_t *hwif = drive->hwif;
616
617	/* Wait for the controller to be idle. */
618	if (ide_wait_stat(&startstop, drive, 0, BUSY_STAT, WAIT_READY))
619		return startstop;
620
621	if (info->dma)
622		info->dma = !hwif->dma_setup(drive);
623
624	/* Set up the controller registers. */
625	HWIF(drive)->OUTB(info->dma, IDE_FEATURE_REG);
626	HWIF(drive)->OUTB(0, IDE_IREASON_REG);
627	HWIF(drive)->OUTB(0, IDE_SECTOR_REG);
628
629	HWIF(drive)->OUTB(xferlen & 0xff, IDE_BCOUNTL_REG);
630	HWIF(drive)->OUTB(xferlen >> 8  , IDE_BCOUNTH_REG);
631	if (IDE_CONTROL_REG)
632		HWIF(drive)->OUTB(drive->ctl, IDE_CONTROL_REG);
633
634	if (CDROM_CONFIG_FLAGS (drive)->drq_interrupt) {
635		/* waiting for CDB interrupt, not DMA yet. */
636		if (info->dma)
637			drive->waiting_for_dma = 0;
638
639		/* packet command */
640		ide_execute_command(drive, WIN_PACKETCMD, handler, ATAPI_WAIT_PC, cdrom_timer_expiry);
641		return ide_started;
642	} else {
643		unsigned long flags;
644
645		/* packet command */
646		spin_lock_irqsave(&ide_lock, flags);
647		hwif->OUTBSYNC(drive, WIN_PACKETCMD, IDE_COMMAND_REG);
648		ndelay(400);
649		spin_unlock_irqrestore(&ide_lock, flags);
650
651		return (*handler) (drive);
652	}
653}
654
655/* Send a packet command to DRIVE described by CMD_BUF and CMD_LEN.
656   The device registers must have already been prepared
657   by cdrom_start_packet_command.
658   HANDLER is the interrupt handler to call when the command completes
659   or there's data ready. */
660/*
661 * changed 5 parameters to 3 for dvd-ram
662 * struct packet_command *pc; now packet_command_t *pc;
663 */
664#define ATAPI_MIN_CDB_BYTES 12
665static ide_startstop_t cdrom_transfer_packet_command (ide_drive_t *drive,
666					  struct request *rq,
667					  ide_handler_t *handler)
668{
669	ide_hwif_t *hwif = drive->hwif;
670	int cmd_len;
671	struct cdrom_info *info = drive->driver_data;
672	ide_startstop_t startstop;
673
674	if (CDROM_CONFIG_FLAGS(drive)->drq_interrupt) {
675		/* Here we should have been called after receiving an interrupt
676		   from the device.  DRQ should how be set. */
677
678		/* Check for errors. */
679		if (cdrom_decode_status(drive, DRQ_STAT, NULL))
680			return ide_stopped;
681
682		/* Ok, next interrupt will be DMA interrupt. */
683		if (info->dma)
684			drive->waiting_for_dma = 1;
685	} else {
686		/* Otherwise, we must wait for DRQ to get set. */
687		if (ide_wait_stat(&startstop, drive, DRQ_STAT,
688				BUSY_STAT, WAIT_READY))
689			return startstop;
690	}
691
692	/* Arm the interrupt handler. */
693	ide_set_handler(drive, handler, rq->timeout, cdrom_timer_expiry);
694
695	/* ATAPI commands get padded out to 12 bytes minimum */
696	cmd_len = COMMAND_SIZE(rq->cmd[0]);
697	if (cmd_len < ATAPI_MIN_CDB_BYTES)
698		cmd_len = ATAPI_MIN_CDB_BYTES;
699
700	/* Send the command to the device. */
701	HWIF(drive)->atapi_output_bytes(drive, rq->cmd, cmd_len);
702
703	/* Start the DMA if need be */
704	if (info->dma)
705		hwif->dma_start(drive);
706
707	return ide_started;
708}
709
710/****************************************************************************
711 * Block read functions.
712 */
713
714/*
715 * Buffer up to SECTORS_TO_TRANSFER sectors from the drive in our sector
716 * buffer.  Once the first sector is added, any subsequent sectors are
717 * assumed to be continuous (until the buffer is cleared).  For the first
718 * sector added, SECTOR is its sector number.  (SECTOR is then ignored until
719 * the buffer is cleared.)
720 */
721static void cdrom_buffer_sectors (ide_drive_t *drive, unsigned long sector,
722                                  int sectors_to_transfer)
723{
724	struct cdrom_info *info = drive->driver_data;
725
726	/* Number of sectors to read into the buffer. */
727	int sectors_to_buffer = min_t(int, sectors_to_transfer,
728				     (SECTOR_BUFFER_SIZE >> SECTOR_BITS) -
729				       info->nsectors_buffered);
730
731	char *dest;
732
733	/* If we couldn't get a buffer, don't try to buffer anything... */
734	if (info->buffer == NULL)
735		sectors_to_buffer = 0;
736
737	/* If this is the first sector in the buffer, remember its number. */
738	if (info->nsectors_buffered == 0)
739		info->sector_buffered = sector;
740
741	/* Read the data into the buffer. */
742	dest = info->buffer + info->nsectors_buffered * SECTOR_SIZE;
743	while (sectors_to_buffer > 0) {
744		HWIF(drive)->atapi_input_bytes(drive, dest, SECTOR_SIZE);
745		--sectors_to_buffer;
746		--sectors_to_transfer;
747		++info->nsectors_buffered;
748		dest += SECTOR_SIZE;
749	}
750
751	/* Throw away any remaining data. */
752	while (sectors_to_transfer > 0) {
753		static char dum[SECTOR_SIZE];
754		HWIF(drive)->atapi_input_bytes(drive, dum, sizeof (dum));
755		--sectors_to_transfer;
756	}
757}
758
759/*
760 * Check the contents of the interrupt reason register from the cdrom
761 * and attempt to recover if there are problems.  Returns  0 if everything's
762 * ok; nonzero if the request has been terminated.
763 */
764static
765int cdrom_read_check_ireason (ide_drive_t *drive, int len, int ireason)
766{
767	if (ireason == 2)
768		return 0;
769	else if (ireason == 0) {
770		/* Whoops... The drive is expecting to receive data from us! */
771		printk(KERN_ERR "%s: read_intr: Drive wants to transfer data the "
772						"wrong way!\n", drive->name);
773
774		/* Throw some data at the drive so it doesn't hang
775		   and quit this request. */
776		while (len > 0) {
777			int dum = 0;
778			HWIF(drive)->atapi_output_bytes(drive, &dum, sizeof (dum));
779			len -= sizeof (dum);
780		}
781	} else  if (ireason == 1) {
782		/* Some drives (ASUS) seem to tell us that status
783		 * info is available. just get it and ignore.
784		 */
785		(void) HWIF(drive)->INB(IDE_STATUS_REG);
786		return 0;
787	} else {
788		/* Drive wants a command packet, or invalid ireason... */
789		printk(KERN_ERR "%s: read_intr: bad interrupt reason %x\n", drive->name,
790								ireason);
791	}
792
793	cdrom_end_request(drive, 0);
794	return -1;
795}
796
797/*
798 * Interrupt routine.  Called when a read request has completed.
799 */
800static ide_startstop_t cdrom_read_intr (ide_drive_t *drive)
801{
802	int stat;
803	int ireason, len, sectors_to_transfer, nskip;
804	struct cdrom_info *info = drive->driver_data;
805	u8 lowcyl = 0, highcyl = 0;
806	int dma = info->dma, dma_error = 0;
807
808	struct request *rq = HWGROUP(drive)->rq;
809
810	/*
811	 * handle dma case
812	 */
813	if (dma) {
814		info->dma = 0;
815		if ((dma_error = HWIF(drive)->ide_dma_end(drive)))
816			ide_dma_off(drive);
817	}
818
819	if (cdrom_decode_status(drive, 0, &stat))
820		return ide_stopped;
821
822	if (dma) {
823		if (!dma_error) {
824			ide_end_request(drive, 1, rq->nr_sectors);
825			return ide_stopped;
826		} else
827			return ide_error(drive, "dma error", stat);
828	}
829
830	/* Read the interrupt reason and the transfer length. */
831	ireason = HWIF(drive)->INB(IDE_IREASON_REG) & 0x3;
832	lowcyl  = HWIF(drive)->INB(IDE_BCOUNTL_REG);
833	highcyl = HWIF(drive)->INB(IDE_BCOUNTH_REG);
834
835	len = lowcyl + (256 * highcyl);
836
837	/* If DRQ is clear, the command has completed. */
838	if ((stat & DRQ_STAT) == 0) {
839		/* If we're not done filling the current buffer, complain.
840		   Otherwise, complete the command normally. */
841		if (rq->current_nr_sectors > 0) {
842			printk (KERN_ERR "%s: cdrom_read_intr: data underrun (%d blocks)\n",
843				drive->name, rq->current_nr_sectors);
844			rq->cmd_flags |= REQ_FAILED;
845			cdrom_end_request(drive, 0);
846		} else
847			cdrom_end_request(drive, 1);
848		return ide_stopped;
849	}
850
851	/* Check that the drive is expecting to do the same thing we are. */
852	if (cdrom_read_check_ireason (drive, len, ireason))
853		return ide_stopped;
854
855	/* Assume that the drive will always provide data in multiples
856	   of at least SECTOR_SIZE, as it gets hairy to keep track
857	   of the transfers otherwise. */
858	if ((len % SECTOR_SIZE) != 0) {
859		printk (KERN_ERR "%s: cdrom_read_intr: Bad transfer size %d\n",
860			drive->name, len);
861		if (CDROM_CONFIG_FLAGS(drive)->limit_nframes)
862			printk (KERN_ERR "  This drive is not supported by this version of the driver\n");
863		else {
864			printk (KERN_ERR "  Trying to limit transfer sizes\n");
865			CDROM_CONFIG_FLAGS(drive)->limit_nframes = 1;
866		}
867		cdrom_end_request(drive, 0);
868		return ide_stopped;
869	}
870
871	/* The number of sectors we need to read from the drive. */
872	sectors_to_transfer = len / SECTOR_SIZE;
873
874	/* First, figure out if we need to bit-bucket
875	   any of the leading sectors. */
876	nskip = min_t(int, rq->current_nr_sectors - bio_cur_sectors(rq->bio), sectors_to_transfer);
877
878	while (nskip > 0) {
879		/* We need to throw away a sector. */
880		static char dum[SECTOR_SIZE];
881		HWIF(drive)->atapi_input_bytes(drive, dum, sizeof (dum));
882
883		--rq->current_nr_sectors;
884		--nskip;
885		--sectors_to_transfer;
886	}
887
888	/* Now loop while we still have data to read from the drive. */
889	while (sectors_to_transfer > 0) {
890		int this_transfer;
891
892		/* If we've filled the present buffer but there's another
893		   chained buffer after it, move on. */
894		if (rq->current_nr_sectors == 0 && rq->nr_sectors)
895			cdrom_end_request(drive, 1);
896
897		/* If the buffers are full, cache the rest of the data in our
898		   internal buffer. */
899		if (rq->current_nr_sectors == 0) {
900			cdrom_buffer_sectors(drive, rq->sector, sectors_to_transfer);
901			sectors_to_transfer = 0;
902		} else {
903			/* Transfer data to the buffers.
904			   Figure out how many sectors we can transfer
905			   to the current buffer. */
906			this_transfer = min_t(int, sectors_to_transfer,
907					     rq->current_nr_sectors);
908
909			/* Read this_transfer sectors
910			   into the current buffer. */
911			while (this_transfer > 0) {
912				HWIF(drive)->atapi_input_bytes(drive, rq->buffer, SECTOR_SIZE);
913				rq->buffer += SECTOR_SIZE;
914				--rq->nr_sectors;
915				--rq->current_nr_sectors;
916				++rq->sector;
917				--this_transfer;
918				--sectors_to_transfer;
919			}
920		}
921	}
922
923	/* Done moving data!  Wait for another interrupt. */
924	ide_set_handler(drive, &cdrom_read_intr, ATAPI_WAIT_PC, NULL);
925	return ide_started;
926}
927
928/*
929 * Try to satisfy some of the current read request from our cached data.
930 * Returns nonzero if the request has been completed, zero otherwise.
931 */
932static int cdrom_read_from_buffer (ide_drive_t *drive)
933{
934	struct cdrom_info *info = drive->driver_data;
935	struct request *rq = HWGROUP(drive)->rq;
936	unsigned short sectors_per_frame;
937
938	sectors_per_frame = queue_hardsect_size(drive->queue) >> SECTOR_BITS;
939
940	/* Can't do anything if there's no buffer. */
941	if (info->buffer == NULL) return 0;
942
943	/* Loop while this request needs data and the next block is present
944	   in our cache. */
945	while (rq->nr_sectors > 0 &&
946	       rq->sector >= info->sector_buffered &&
947	       rq->sector < info->sector_buffered + info->nsectors_buffered) {
948		if (rq->current_nr_sectors == 0)
949			cdrom_end_request(drive, 1);
950
951		memcpy (rq->buffer,
952			info->buffer +
953			(rq->sector - info->sector_buffered) * SECTOR_SIZE,
954			SECTOR_SIZE);
955		rq->buffer += SECTOR_SIZE;
956		--rq->current_nr_sectors;
957		--rq->nr_sectors;
958		++rq->sector;
959	}
960
961	/* If we've satisfied the current request,
962	   terminate it successfully. */
963	if (rq->nr_sectors == 0) {
964		cdrom_end_request(drive, 1);
965		return -1;
966	}
967
968	/* Move on to the next buffer if needed. */
969	if (rq->current_nr_sectors == 0)
970		cdrom_end_request(drive, 1);
971
972	/* If this condition does not hold, then the kluge i use to
973	   represent the number of sectors to skip at the start of a transfer
974	   will fail.  I think that this will never happen, but let's be
975	   paranoid and check. */
976	if (rq->current_nr_sectors < bio_cur_sectors(rq->bio) &&
977	    (rq->sector & (sectors_per_frame - 1))) {
978		printk(KERN_ERR "%s: cdrom_read_from_buffer: buffer botch (%ld)\n",
979			drive->name, (long)rq->sector);
980		cdrom_end_request(drive, 0);
981		return -1;
982	}
983
984	return 0;
985}
986
987/*
988 * Routine to send a read packet command to the drive.
989 * This is usually called directly from cdrom_start_read.
990 * However, for drq_interrupt devices, it is called from an interrupt
991 * when the drive is ready to accept the command.
992 */
993static ide_startstop_t cdrom_start_read_continuation (ide_drive_t *drive)
994{
995	struct request *rq = HWGROUP(drive)->rq;
996	unsigned short sectors_per_frame;
997	int nskip;
998
999	sectors_per_frame = queue_hardsect_size(drive->queue) >> SECTOR_BITS;
1000
1001	/* If the requested sector doesn't start on a cdrom block boundary,
1002	   we must adjust the start of the transfer so that it does,
1003	   and remember to skip the first few sectors.
1004	   If the CURRENT_NR_SECTORS field is larger than the size
1005	   of the buffer, it will mean that we're to skip a number
1006	   of sectors equal to the amount by which CURRENT_NR_SECTORS
1007	   is larger than the buffer size. */
1008	nskip = rq->sector & (sectors_per_frame - 1);
1009	if (nskip > 0) {
1010		/* Sanity check... */
1011		if (rq->current_nr_sectors != bio_cur_sectors(rq->bio) &&
1012			(rq->sector & (sectors_per_frame - 1))) {
1013			printk(KERN_ERR "%s: cdrom_start_read_continuation: buffer botch (%u)\n",
1014				drive->name, rq->current_nr_sectors);
1015			cdrom_end_request(drive, 0);
1016			return ide_stopped;
1017		}
1018		rq->current_nr_sectors += nskip;
1019	}
1020
1021	/* Set up the command */
1022	rq->timeout = ATAPI_WAIT_PC;
1023
1024	/* Send the command to the drive and return. */
1025	return cdrom_transfer_packet_command(drive, rq, &cdrom_read_intr);
1026}
1027
1028
1029#define IDECD_SEEK_THRESHOLD	(1000)			/* 1000 blocks */
1030#define IDECD_SEEK_TIMER	(5 * WAIT_MIN_SLEEP)	/* 100 ms */
1031#define IDECD_SEEK_TIMEOUT	(2 * WAIT_CMD)		/* 20 sec */
1032
1033static ide_startstop_t cdrom_seek_intr (ide_drive_t *drive)
1034{
1035	struct cdrom_info *info = drive->driver_data;
1036	int stat;
1037	static int retry = 10;
1038
1039	if (cdrom_decode_status(drive, 0, &stat))
1040		return ide_stopped;
1041	CDROM_CONFIG_FLAGS(drive)->seeking = 1;
1042
1043	if (retry && time_after(jiffies, info->start_seek + IDECD_SEEK_TIMER)) {
1044		if (--retry == 0) {
1045			/*
1046			 * this condition is far too common, to bother
1047			 * users about it
1048			 */
1049			/* printk("%s: disabled DSC seek overlap\n", drive->name);*/
1050			drive->dsc_overlap = 0;
1051		}
1052	}
1053	return ide_stopped;
1054}
1055
1056static ide_startstop_t cdrom_start_seek_continuation (ide_drive_t *drive)
1057{
1058	struct request *rq = HWGROUP(drive)->rq;
1059	sector_t frame = rq->sector;
1060
1061	sector_div(frame, queue_hardsect_size(drive->queue) >> SECTOR_BITS);
1062
1063	memset(rq->cmd, 0, sizeof(rq->cmd));
1064	rq->cmd[0] = GPCMD_SEEK;
1065	put_unaligned(cpu_to_be32(frame), (unsigned int *) &rq->cmd[2]);
1066
1067	rq->timeout = ATAPI_WAIT_PC;
1068	return cdrom_transfer_packet_command(drive, rq, &cdrom_seek_intr);
1069}
1070
1071static ide_startstop_t cdrom_start_seek (ide_drive_t *drive, unsigned int block)
1072{
1073	struct cdrom_info *info = drive->driver_data;
1074
1075	info->dma = 0;
1076	info->start_seek = jiffies;
1077	return cdrom_start_packet_command(drive, 0, cdrom_start_seek_continuation);
1078}
1079
1080/* Fix up a possibly partially-processed request so that we can
1081   start it over entirely, or even put it back on the request queue. */
1082static void restore_request (struct request *rq)
1083{
1084	if (rq->buffer != bio_data(rq->bio)) {
1085		sector_t n = (rq->buffer - (char *) bio_data(rq->bio)) / SECTOR_SIZE;
1086
1087		rq->buffer = bio_data(rq->bio);
1088		rq->nr_sectors += n;
1089		rq->sector -= n;
1090	}
1091	rq->hard_cur_sectors = rq->current_nr_sectors = bio_cur_sectors(rq->bio);
1092	rq->hard_nr_sectors = rq->nr_sectors;
1093	rq->hard_sector = rq->sector;
1094	rq->q->prep_rq_fn(rq->q, rq);
1095}
1096
1097/*
1098 * Start a read request from the CD-ROM.
1099 */
1100static ide_startstop_t cdrom_start_read (ide_drive_t *drive, unsigned int block)
1101{
1102	struct cdrom_info *info = drive->driver_data;
1103	struct request *rq = HWGROUP(drive)->rq;
1104	unsigned short sectors_per_frame;
1105
1106	sectors_per_frame = queue_hardsect_size(drive->queue) >> SECTOR_BITS;
1107
1108	/* We may be retrying this request after an error.  Fix up
1109	   any weirdness which might be present in the request packet. */
1110	restore_request(rq);
1111
1112	/* Satisfy whatever we can of this request from our cached sector. */
1113	if (cdrom_read_from_buffer(drive))
1114		return ide_stopped;
1115
1116	/* Clear the local sector buffer. */
1117	info->nsectors_buffered = 0;
1118
1119	/* use dma, if possible. */
1120	info->dma = drive->using_dma;
1121	if ((rq->sector & (sectors_per_frame - 1)) ||
1122	    (rq->nr_sectors & (sectors_per_frame - 1)))
1123		info->dma = 0;
1124
1125	/* Start sending the read request to the drive. */
1126	return cdrom_start_packet_command(drive, 32768, cdrom_start_read_continuation);
1127}
1128
1129/****************************************************************************
1130 * Execute all other packet commands.
1131 */
1132
1133/* Interrupt routine for packet command completion. */
1134static ide_startstop_t cdrom_pc_intr (ide_drive_t *drive)
1135{
1136	int ireason, len, thislen;
1137	struct request *rq = HWGROUP(drive)->rq;
1138	u8 lowcyl = 0, highcyl = 0;
1139	int stat;
1140
1141	/* Check for errors. */
1142	if (cdrom_decode_status(drive, 0, &stat))
1143		return ide_stopped;
1144
1145	/* Read the interrupt reason and the transfer length. */
1146	ireason = HWIF(drive)->INB(IDE_IREASON_REG);
1147	lowcyl  = HWIF(drive)->INB(IDE_BCOUNTL_REG);
1148	highcyl = HWIF(drive)->INB(IDE_BCOUNTH_REG);
1149
1150	len = lowcyl + (256 * highcyl);
1151
1152	/* If DRQ is clear, the command has completed.
1153	   Complain if we still have data left to transfer. */
1154	if ((stat & DRQ_STAT) == 0) {
1155		/* Some of the trailing request sense fields are optional, and
1156		   some drives don't send them.  Sigh. */
1157		if (rq->cmd[0] == GPCMD_REQUEST_SENSE &&
1158		    rq->data_len > 0 &&
1159		    rq->data_len <= 5) {
1160			while (rq->data_len > 0) {
1161				*(unsigned char *)rq->data++ = 0;
1162				--rq->data_len;
1163			}
1164		}
1165
1166		if (rq->data_len == 0)
1167			cdrom_end_request(drive, 1);
1168		else {
1169			/* Comment this out, because this always happens
1170			   right after a reset occurs, and it is annoying to
1171			   always print expected stuff.  */
1172			/*
1173			printk ("%s: cdrom_pc_intr: data underrun %d\n",
1174				drive->name, pc->buflen);
1175			*/
1176			rq->cmd_flags |= REQ_FAILED;
1177			cdrom_end_request(drive, 0);
1178		}
1179		return ide_stopped;
1180	}
1181
1182	/* Figure out how much data to transfer. */
1183	thislen = rq->data_len;
1184	if (thislen > len) thislen = len;
1185
1186	/* The drive wants to be written to. */
1187	if ((ireason & 3) == 0) {
1188		if (!rq->data) {
1189			blk_dump_rq_flags(rq, "cdrom_pc_intr, write");
1190			goto confused;
1191		}
1192		/* Transfer the data. */
1193		HWIF(drive)->atapi_output_bytes(drive, rq->data, thislen);
1194
1195		/* If we haven't moved enough data to satisfy the drive,
1196		   add some padding. */
1197		while (len > thislen) {
1198			int dum = 0;
1199			HWIF(drive)->atapi_output_bytes(drive, &dum, sizeof(dum));
1200			len -= sizeof(dum);
1201		}
1202
1203		/* Keep count of how much data we've moved. */
1204		rq->data += thislen;
1205		rq->data_len -= thislen;
1206	}
1207
1208	/* Same drill for reading. */
1209	else if ((ireason & 3) == 2) {
1210		if (!rq->data) {
1211			blk_dump_rq_flags(rq, "cdrom_pc_intr, write");
1212			goto confused;
1213		}
1214		/* Transfer the data. */
1215		HWIF(drive)->atapi_input_bytes(drive, rq->data, thislen);
1216
1217		/* If we haven't moved enough data to satisfy the drive,
1218		   add some padding. */
1219		while (len > thislen) {
1220			int dum = 0;
1221			HWIF(drive)->atapi_input_bytes(drive, &dum, sizeof(dum));
1222			len -= sizeof(dum);
1223		}
1224
1225		/* Keep count of how much data we've moved. */
1226		rq->data += thislen;
1227		rq->data_len -= thislen;
1228
1229		if (blk_sense_request(rq))
1230			rq->sense_len += thislen;
1231	} else {
1232confused:
1233		printk (KERN_ERR "%s: cdrom_pc_intr: The drive "
1234			"appears confused (ireason = 0x%02x). "
1235			"Trying to recover by ending request.\n",
1236			drive->name, ireason);
1237		rq->cmd_flags |= REQ_FAILED;
1238		cdrom_end_request(drive, 0);
1239		return ide_stopped;
1240	}
1241
1242	/* Now we wait for another interrupt. */
1243	ide_set_handler(drive, &cdrom_pc_intr, ATAPI_WAIT_PC, cdrom_timer_expiry);
1244	return ide_started;
1245}
1246
1247static ide_startstop_t cdrom_do_pc_continuation (ide_drive_t *drive)
1248{
1249	struct request *rq = HWGROUP(drive)->rq;
1250
1251	if (!rq->timeout)
1252		rq->timeout = ATAPI_WAIT_PC;
1253
1254	/* Send the command to the drive and return. */
1255	return cdrom_transfer_packet_command(drive, rq, &cdrom_pc_intr);
1256}
1257
1258
1259static ide_startstop_t cdrom_do_packet_command (ide_drive_t *drive)
1260{
1261	int len;
1262	struct request *rq = HWGROUP(drive)->rq;
1263	struct cdrom_info *info = drive->driver_data;
1264
1265	info->dma = 0;
1266	rq->cmd_flags &= ~REQ_FAILED;
1267	len = rq->data_len;
1268
1269	/* Start sending the command to the drive. */
1270	return cdrom_start_packet_command(drive, len, cdrom_do_pc_continuation);
1271}
1272
1273
1274static int cdrom_queue_packet_command(ide_drive_t *drive, struct request *rq)
1275{
1276	struct request_sense sense;
1277	int retries = 10;
1278	unsigned int flags = rq->cmd_flags;
1279
1280	if (rq->sense == NULL)
1281		rq->sense = &sense;
1282
1283	/* Start of retry loop. */
1284	do {
1285		int error;
1286		unsigned long time = jiffies;
1287		rq->cmd_flags = flags;
1288
1289		error = ide_do_drive_cmd(drive, rq, ide_wait);
1290		time = jiffies - time;
1291
1292		if (rq->cmd_flags & REQ_FAILED) {
1293			/* The request failed.  Retry if it was due to a unit
1294			   attention status
1295			   (usually means media was changed). */
1296			struct request_sense *reqbuf = rq->sense;
1297
1298			if (reqbuf->sense_key == UNIT_ATTENTION)
1299				cdrom_saw_media_change(drive);
1300			else if (reqbuf->sense_key == NOT_READY &&
1301				 reqbuf->asc == 4 && reqbuf->ascq != 4) {
1302				/* The drive is in the process of loading
1303				   a disk.  Retry, but wait a little to give
1304				   the drive time to complete the load. */
1305				ssleep(2);
1306			} else {
1307				/* Otherwise, don't retry. */
1308				retries = 0;
1309			}
1310			--retries;
1311		}
1312
1313		/* End of retry loop. */
1314	} while ((rq->cmd_flags & REQ_FAILED) && retries >= 0);
1315
1316	/* Return an error if the command failed. */
1317	return (rq->cmd_flags & REQ_FAILED) ? -EIO : 0;
1318}
1319
1320/*
1321 * Write handling
1322 */
1323static int cdrom_write_check_ireason(ide_drive_t *drive, int len, int ireason)
1324{
1325	/* Two notes about IDE interrupt reason here - 0 means that
1326	 * the drive wants to receive data from us, 2 means that
1327	 * the drive is expecting to transfer data to us.
1328	 */
1329	if (ireason == 0)
1330		return 0;
1331	else if (ireason == 2) {
1332		/* Whoops... The drive wants to send data. */
1333		printk(KERN_ERR "%s: write_intr: wrong transfer direction!\n",
1334							drive->name);
1335
1336		while (len > 0) {
1337			int dum = 0;
1338			HWIF(drive)->atapi_input_bytes(drive, &dum, sizeof(dum));
1339			len -= sizeof(dum);
1340		}
1341	} else {
1342		/* Drive wants a command packet, or invalid ireason... */
1343		printk(KERN_ERR "%s: write_intr: bad interrupt reason %x\n",
1344							drive->name, ireason);
1345	}
1346
1347	cdrom_end_request(drive, 0);
1348	return 1;
1349}
1350
1351static void post_transform_command(struct request *req)
1352{
1353	u8 *c = req->cmd;
1354	char *ibuf;
1355
1356	if (!blk_pc_request(req))
1357		return;
1358
1359	if (req->bio)
1360		ibuf = bio_data(req->bio);
1361	else
1362		ibuf = req->data;
1363
1364	if (!ibuf)
1365		return;
1366
1367	/*
1368	 * set ansi-revision and response data as atapi
1369	 */
1370	if (c[0] == GPCMD_INQUIRY) {
1371		ibuf[2] |= 2;
1372		ibuf[3] = (ibuf[3] & 0xf0) | 2;
1373	}
1374}
1375
1376typedef void (xfer_func_t)(ide_drive_t *, void *, u32);
1377
1378/*
1379 * best way to deal with dma that is not sector aligned right now... note
1380 * that in this path we are not using ->data or ->buffer at all. this irs
1381 * can replace cdrom_pc_intr, cdrom_read_intr, and cdrom_write_intr in the
1382 * future.
1383 */
1384static ide_startstop_t cdrom_newpc_intr(ide_drive_t *drive)
1385{
1386	struct cdrom_info *info = drive->driver_data;
1387	struct request *rq = HWGROUP(drive)->rq;
1388	int dma_error, dma, stat, ireason, len, thislen;
1389	u8 lowcyl, highcyl;
1390	xfer_func_t *xferfunc;
1391	unsigned long flags;
1392
1393	/* Check for errors. */
1394	dma_error = 0;
1395	dma = info->dma;
1396	if (dma) {
1397		info->dma = 0;
1398		dma_error = HWIF(drive)->ide_dma_end(drive);
1399	}
1400
1401	if (cdrom_decode_status(drive, 0, &stat))
1402		return ide_stopped;
1403
1404	/*
1405	 * using dma, transfer is complete now
1406	 */
1407	if (dma) {
1408		if (dma_error) {
1409			printk(KERN_ERR "ide-cd: dma error\n");
1410			ide_dma_off(drive);
1411			return ide_error(drive, "dma error", stat);
1412		}
1413
1414		end_that_request_chunk(rq, 1, rq->data_len);
1415		rq->data_len = 0;
1416		goto end_request;
1417	}
1418
1419	/*
1420	 * ok we fall to pio :/
1421	 */
1422	ireason = HWIF(drive)->INB(IDE_IREASON_REG) & 0x3;
1423	lowcyl  = HWIF(drive)->INB(IDE_BCOUNTL_REG);
1424	highcyl = HWIF(drive)->INB(IDE_BCOUNTH_REG);
1425
1426	len = lowcyl + (256 * highcyl);
1427	thislen = rq->data_len;
1428	if (thislen > len)
1429		thislen = len;
1430
1431	/*
1432	 * If DRQ is clear, the command has completed.
1433	 */
1434	if ((stat & DRQ_STAT) == 0)
1435		goto end_request;
1436
1437	/*
1438	 * check which way to transfer data
1439	 */
1440	if (rq_data_dir(rq) == WRITE) {
1441		/*
1442		 * write to drive
1443		 */
1444		if (cdrom_write_check_ireason(drive, len, ireason))
1445			return ide_stopped;
1446
1447		xferfunc = HWIF(drive)->atapi_output_bytes;
1448	} else  {
1449		/*
1450		 * read from drive
1451		 */
1452		if (cdrom_read_check_ireason(drive, len, ireason))
1453			return ide_stopped;
1454
1455		xferfunc = HWIF(drive)->atapi_input_bytes;
1456	}
1457
1458	/*
1459	 * transfer data
1460	 */
1461	while (thislen > 0) {
1462		int blen = blen = rq->data_len;
1463		char *ptr = rq->data;
1464
1465		/*
1466		 * bio backed?
1467		 */
1468		if (rq->bio) {
1469			ptr = bio_data(rq->bio);
1470			blen = bio_iovec(rq->bio)->bv_len;
1471		}
1472
1473		if (!ptr) {
1474			printk(KERN_ERR "%s: confused, missing data\n", drive->name);
1475			break;
1476		}
1477
1478		if (blen > thislen)
1479			blen = thislen;
1480
1481		xferfunc(drive, ptr, blen);
1482
1483		thislen -= blen;
1484		len -= blen;
1485		rq->data_len -= blen;
1486
1487		if (rq->bio)
1488			end_that_request_chunk(rq, 1, blen);
1489		else
1490			rq->data += blen;
1491	}
1492
1493	/*
1494	 * pad, if necessary
1495	 */
1496	if (len > 0) {
1497		while (len > 0) {
1498			int pad = 0;
1499
1500			xferfunc(drive, &pad, sizeof(pad));
1501			len -= sizeof(pad);
1502		}
1503	}
1504
1505	BUG_ON(HWGROUP(drive)->handler != NULL);
1506
1507	ide_set_handler(drive, cdrom_newpc_intr, rq->timeout, NULL);
1508	return ide_started;
1509
1510end_request:
1511	if (!rq->data_len)
1512		post_transform_command(rq);
1513
1514	spin_lock_irqsave(&ide_lock, flags);
1515	blkdev_dequeue_request(rq);
1516	end_that_request_last(rq, 1);
1517	HWGROUP(drive)->rq = NULL;
1518	spin_unlock_irqrestore(&ide_lock, flags);
1519	return ide_stopped;
1520}
1521
1522static ide_startstop_t cdrom_write_intr(ide_drive_t *drive)
1523{
1524	int stat, ireason, len, sectors_to_transfer, uptodate;
1525	struct cdrom_info *info = drive->driver_data;
1526	int dma_error = 0, dma = info->dma;
1527	u8 lowcyl = 0, highcyl = 0;
1528
1529	struct request *rq = HWGROUP(drive)->rq;
1530
1531	/* Check for errors. */
1532	if (dma) {
1533		info->dma = 0;
1534		if ((dma_error = HWIF(drive)->ide_dma_end(drive))) {
1535			printk(KERN_ERR "ide-cd: write dma error\n");
1536			ide_dma_off(drive);
1537		}
1538	}
1539
1540	if (cdrom_decode_status(drive, 0, &stat))
1541		return ide_stopped;
1542
1543	/*
1544	 * using dma, transfer is complete now
1545	 */
1546	if (dma) {
1547		if (dma_error)
1548			return ide_error(drive, "dma error", stat);
1549
1550		ide_end_request(drive, 1, rq->nr_sectors);
1551		return ide_stopped;
1552	}
1553
1554	/* Read the interrupt reason and the transfer length. */
1555	ireason = HWIF(drive)->INB(IDE_IREASON_REG);
1556	lowcyl  = HWIF(drive)->INB(IDE_BCOUNTL_REG);
1557	highcyl = HWIF(drive)->INB(IDE_BCOUNTH_REG);
1558
1559	len = lowcyl + (256 * highcyl);
1560
1561	/* If DRQ is clear, the command has completed. */
1562	if ((stat & DRQ_STAT) == 0) {
1563		/* If we're not done writing, complain.
1564		 * Otherwise, complete the command normally.
1565		 */
1566		uptodate = 1;
1567		if (rq->current_nr_sectors > 0) {
1568			printk(KERN_ERR "%s: write_intr: data underrun (%d blocks)\n",
1569			drive->name, rq->current_nr_sectors);
1570			uptodate = 0;
1571		}
1572		cdrom_end_request(drive, uptodate);
1573		return ide_stopped;
1574	}
1575
1576	/* Check that the drive is expecting to do the same thing we are. */
1577	if (cdrom_write_check_ireason(drive, len, ireason))
1578		return ide_stopped;
1579
1580	sectors_to_transfer = len / SECTOR_SIZE;
1581
1582	/*
1583	 * now loop and write out the data
1584	 */
1585	while (sectors_to_transfer > 0) {
1586		int this_transfer;
1587
1588		if (!rq->current_nr_sectors) {
1589			printk(KERN_ERR "ide-cd: write_intr: oops\n");
1590			break;
1591		}
1592
1593		/*
1594		 * Figure out how many sectors we can transfer
1595		 */
1596		this_transfer = min_t(int, sectors_to_transfer, rq->current_nr_sectors);
1597
1598		while (this_transfer > 0) {
1599			HWIF(drive)->atapi_output_bytes(drive, rq->buffer, SECTOR_SIZE);
1600			rq->buffer += SECTOR_SIZE;
1601			--rq->nr_sectors;
1602			--rq->current_nr_sectors;
1603			++rq->sector;
1604			--this_transfer;
1605			--sectors_to_transfer;
1606		}
1607
1608		/*
1609		 * current buffer complete, move on
1610		 */
1611		if (rq->current_nr_sectors == 0 && rq->nr_sectors)
1612			cdrom_end_request(drive, 1);
1613	}
1614
1615	/* re-arm handler */
1616	ide_set_handler(drive, &cdrom_write_intr, ATAPI_WAIT_PC, NULL);
1617	return ide_started;
1618}
1619
1620static ide_startstop_t cdrom_start_write_cont(ide_drive_t *drive)
1621{
1622	struct request *rq = HWGROUP(drive)->rq;
1623
1624	rq->timeout = ATAPI_WAIT_PC;
1625
1626	return cdrom_transfer_packet_command(drive, rq, cdrom_write_intr);
1627}
1628
1629static ide_startstop_t cdrom_start_write(ide_drive_t *drive, struct request *rq)
1630{
1631	struct cdrom_info *info = drive->driver_data;
1632	struct gendisk *g = info->disk;
1633	unsigned short sectors_per_frame = queue_hardsect_size(drive->queue) >> SECTOR_BITS;
1634
1635	/*
1636	 * writes *must* be hardware frame aligned
1637	 */
1638	if ((rq->nr_sectors & (sectors_per_frame - 1)) ||
1639	    (rq->sector & (sectors_per_frame - 1))) {
1640		cdrom_end_request(drive, 0);
1641		return ide_stopped;
1642	}
1643
1644	/*
1645	 * disk has become write protected
1646	 */
1647	if (g->policy) {
1648		cdrom_end_request(drive, 0);
1649		return ide_stopped;
1650	}
1651
1652	info->nsectors_buffered = 0;
1653
1654	/* use dma, if possible. we don't need to check more, since we
1655	 * know that the transfer is always (at least!) frame aligned */
1656	info->dma = drive->using_dma ? 1 : 0;
1657
1658	info->devinfo.media_written = 1;
1659
1660	/* Start sending the write request to the drive. */
1661	return cdrom_start_packet_command(drive, 32768, cdrom_start_write_cont);
1662}
1663
1664static ide_startstop_t cdrom_do_newpc_cont(ide_drive_t *drive)
1665{
1666	struct request *rq = HWGROUP(drive)->rq;
1667
1668	if (!rq->timeout)
1669		rq->timeout = ATAPI_WAIT_PC;
1670
1671	return cdrom_transfer_packet_command(drive, rq, cdrom_newpc_intr);
1672}
1673
1674static ide_startstop_t cdrom_do_block_pc(ide_drive_t *drive, struct request *rq)
1675{
1676	struct cdrom_info *info = drive->driver_data;
1677
1678	rq->cmd_flags |= REQ_QUIET;
1679
1680	info->dma = 0;
1681
1682	/*
1683	 * sg request
1684	 */
1685	if (rq->bio) {
1686		int mask = drive->queue->dma_alignment;
1687		unsigned long addr = (unsigned long) page_address(bio_page(rq->bio));
1688
1689		info->dma = drive->using_dma;
1690
1691		/*
1692		 * check if dma is safe
1693		 *
1694		 * NOTE! The "len" and "addr" checks should possibly have
1695		 * separate masks.
1696		 */
1697		if ((rq->data_len & 15) || (addr & mask))
1698			info->dma = 0;
1699	}
1700
1701	/* Start sending the command to the drive. */
1702	return cdrom_start_packet_command(drive, rq->data_len, cdrom_do_newpc_cont);
1703}
1704
1705/****************************************************************************
1706 * cdrom driver request routine.
1707 */
1708static ide_startstop_t
1709ide_do_rw_cdrom (ide_drive_t *drive, struct request *rq, sector_t block)
1710{
1711	ide_startstop_t action;
1712	struct cdrom_info *info = drive->driver_data;
1713
1714	if (blk_fs_request(rq)) {
1715		if (CDROM_CONFIG_FLAGS(drive)->seeking) {
1716			unsigned long elapsed = jiffies - info->start_seek;
1717			int stat = HWIF(drive)->INB(IDE_STATUS_REG);
1718
1719			if ((stat & SEEK_STAT) != SEEK_STAT) {
1720				if (elapsed < IDECD_SEEK_TIMEOUT) {
1721					ide_stall_queue(drive, IDECD_SEEK_TIMER);
1722					return ide_stopped;
1723				}
1724				printk (KERN_ERR "%s: DSC timeout\n", drive->name);
1725			}
1726			CDROM_CONFIG_FLAGS(drive)->seeking = 0;
1727		}
1728		if ((rq_data_dir(rq) == READ) && IDE_LARGE_SEEK(info->last_block, block, IDECD_SEEK_THRESHOLD) && drive->dsc_overlap) {
1729			action = cdrom_start_seek(drive, block);
1730		} else {
1731			if (rq_data_dir(rq) == READ)
1732				action = cdrom_start_read(drive, block);
1733			else
1734				action = cdrom_start_write(drive, rq);
1735		}
1736		info->last_block = block;
1737		return action;
1738	} else if (rq->cmd_type == REQ_TYPE_SENSE ||
1739		   rq->cmd_type == REQ_TYPE_ATA_PC) {
1740		return cdrom_do_packet_command(drive);
1741	} else if (blk_pc_request(rq)) {
1742		return cdrom_do_block_pc(drive, rq);
1743	} else if (blk_special_request(rq)) {
1744		/*
1745		 * right now this can only be a reset...
1746		 */
1747		cdrom_end_request(drive, 1);
1748		return ide_stopped;
1749	}
1750
1751	blk_dump_rq_flags(rq, "ide-cd bad flags");
1752	cdrom_end_request(drive, 0);
1753	return ide_stopped;
1754}
1755
1756
1757
1758/****************************************************************************
1759 * Ioctl handling.
1760 *
1761 * Routines which queue packet commands take as a final argument a pointer
1762 * to a request_sense struct.  If execution of the command results
1763 * in an error with a CHECK CONDITION status, this structure will be filled
1764 * with the results of the subsequent request sense command.  The pointer
1765 * can also be NULL, in which case no sense information is returned.
1766 */
1767
1768#if ! STANDARD_ATAPI
1769static inline
1770int bin2bcd (int x)
1771{
1772	return (x%10) | ((x/10) << 4);
1773}
1774
1775
1776static inline
1777int bcd2bin (int x)
1778{
1779	return (x >> 4) * 10 + (x & 0x0f);
1780}
1781
1782static
1783void msf_from_bcd (struct atapi_msf *msf)
1784{
1785	msf->minute = bcd2bin (msf->minute);
1786	msf->second = bcd2bin (msf->second);
1787	msf->frame  = bcd2bin (msf->frame);
1788}
1789
1790#endif /* not STANDARD_ATAPI */
1791
1792
1793static inline
1794void lba_to_msf (int lba, byte *m, byte *s, byte *f)
1795{
1796	lba += CD_MSF_OFFSET;
1797	lba &= 0xffffff;  /* negative lbas use only 24 bits */
1798	*m = lba / (CD_SECS * CD_FRAMES);
1799	lba %= (CD_SECS * CD_FRAMES);
1800	*s = lba / CD_FRAMES;
1801	*f = lba % CD_FRAMES;
1802}
1803
1804
1805static inline
1806int msf_to_lba (byte m, byte s, byte f)
1807{
1808	return (((m * CD_SECS) + s) * CD_FRAMES + f) - CD_MSF_OFFSET;
1809}
1810
1811static int cdrom_check_status(ide_drive_t *drive, struct request_sense *sense)
1812{
1813	struct request req;
1814	struct cdrom_info *info = drive->driver_data;
1815	struct cdrom_device_info *cdi = &info->devinfo;
1816
1817	cdrom_prepare_request(drive, &req);
1818
1819	req.sense = sense;
1820	req.cmd[0] = GPCMD_TEST_UNIT_READY;
1821	req.cmd_flags |= REQ_QUIET;
1822
1823#if ! STANDARD_ATAPI
1824        /* the Sanyo 3 CD changer uses byte 7 of TEST_UNIT_READY to
1825           switch CDs instead of supporting the LOAD_UNLOAD opcode   */
1826
1827	req.cmd[7] = cdi->sanyo_slot % 3;
1828#endif /* not STANDARD_ATAPI */
1829
1830	return cdrom_queue_packet_command(drive, &req);
1831}
1832
1833
1834/* Lock the door if LOCKFLAG is nonzero; unlock it otherwise. */
1835static int
1836cdrom_lockdoor(ide_drive_t *drive, int lockflag, struct request_sense *sense)
1837{
1838	struct request_sense my_sense;
1839	struct request req;
1840	int stat;
1841
1842	if (sense == NULL)
1843		sense = &my_sense;
1844
1845	/* If the drive cannot lock the door, just pretend. */
1846	if (CDROM_CONFIG_FLAGS(drive)->no_doorlock) {
1847		stat = 0;
1848	} else {
1849		cdrom_prepare_request(drive, &req);
1850		req.sense = sense;
1851		req.cmd[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
1852		req.cmd[4] = lockflag ? 1 : 0;
1853		stat = cdrom_queue_packet_command(drive, &req);
1854	}
1855
1856	/* If we got an illegal field error, the drive
1857	   probably cannot lock the door. */
1858	if (stat != 0 &&
1859	    sense->sense_key == ILLEGAL_REQUEST &&
1860	    (sense->asc == 0x24 || sense->asc == 0x20)) {
1861		printk (KERN_ERR "%s: door locking not supported\n",
1862			drive->name);
1863		CDROM_CONFIG_FLAGS(drive)->no_doorlock = 1;
1864		stat = 0;
1865	}
1866
1867	/* no medium, that's alright. */
1868	if (stat != 0 && sense->sense_key == NOT_READY && sense->asc == 0x3a)
1869		stat = 0;
1870
1871	if (stat == 0)
1872		CDROM_STATE_FLAGS(drive)->door_locked = lockflag;
1873
1874	return stat;
1875}
1876
1877
1878/* Eject the disk if EJECTFLAG is 0.
1879   If EJECTFLAG is 1, try to reload the disk. */
1880static int cdrom_eject(ide_drive_t *drive, int ejectflag,
1881		       struct request_sense *sense)
1882{
1883	struct request req;
1884	char loej = 0x02;
1885
1886	if (CDROM_CONFIG_FLAGS(drive)->no_eject && !ejectflag)
1887		return -EDRIVE_CANT_DO_THIS;
1888
1889	/* reload fails on some drives, if the tray is locked */
1890	if (CDROM_STATE_FLAGS(drive)->door_locked && ejectflag)
1891		return 0;
1892
1893	cdrom_prepare_request(drive, &req);
1894
1895	/* only tell drive to close tray if open, if it can do that */
1896	if (ejectflag && !CDROM_CONFIG_FLAGS(drive)->close_tray)
1897		loej = 0;
1898
1899	req.sense = sense;
1900	req.cmd[0] = GPCMD_START_STOP_UNIT;
1901	req.cmd[4] = loej | (ejectflag != 0);
1902	return cdrom_queue_packet_command(drive, &req);
1903}
1904
1905static int cdrom_read_capacity(ide_drive_t *drive, unsigned long *capacity,
1906			       unsigned long *sectors_per_frame,
1907			       struct request_sense *sense)
1908{
1909	struct {
1910		__u32 lba;
1911		__u32 blocklen;
1912	} capbuf;
1913
1914	int stat;
1915	struct request req;
1916
1917	cdrom_prepare_request(drive, &req);
1918
1919	req.sense = sense;
1920	req.cmd[0] = GPCMD_READ_CDVD_CAPACITY;
1921	req.data = (char *)&capbuf;
1922	req.data_len = sizeof(capbuf);
1923	req.cmd_flags |= REQ_QUIET;
1924
1925	stat = cdrom_queue_packet_command(drive, &req);
1926	if (stat == 0) {
1927		*capacity = 1 + be32_to_cpu(capbuf.lba);
1928		*sectors_per_frame =
1929			be32_to_cpu(capbuf.blocklen) >> SECTOR_BITS;
1930	}
1931
1932	return stat;
1933}
1934
1935static int cdrom_read_tocentry(ide_drive_t *drive, int trackno, int msf_flag,
1936				int format, char *buf, int buflen,
1937				struct request_sense *sense)
1938{
1939	struct request req;
1940
1941	cdrom_prepare_request(drive, &req);
1942
1943	req.sense = sense;
1944	req.data =  buf;
1945	req.data_len = buflen;
1946	req.cmd_flags |= REQ_QUIET;
1947	req.cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
1948	req.cmd[6] = trackno;
1949	req.cmd[7] = (buflen >> 8);
1950	req.cmd[8] = (buflen & 0xff);
1951	req.cmd[9] = (format << 6);
1952
1953	if (msf_flag)
1954		req.cmd[1] = 2;
1955
1956	return cdrom_queue_packet_command(drive, &req);
1957}
1958
1959
1960/* Try to read the entire TOC for the disk into our internal buffer. */
1961static int cdrom_read_toc(ide_drive_t *drive, struct request_sense *sense)
1962{
1963	int stat, ntracks, i;
1964	struct cdrom_info *info = drive->driver_data;
1965	struct cdrom_device_info *cdi = &info->devinfo;
1966	struct atapi_toc *toc = info->toc;
1967	struct {
1968		struct atapi_toc_header hdr;
1969		struct atapi_toc_entry  ent;
1970	} ms_tmp;
1971	long last_written;
1972	unsigned long sectors_per_frame = SECTORS_PER_FRAME;
1973
1974	if (toc == NULL) {
1975		/* Try to allocate space. */
1976		toc = kmalloc(sizeof(struct atapi_toc), GFP_KERNEL);
1977		if (toc == NULL) {
1978			printk (KERN_ERR "%s: No cdrom TOC buffer!\n", drive->name);
1979			return -ENOMEM;
1980		}
1981		info->toc = toc;
1982	}
1983
1984	/* Check to see if the existing data is still valid.
1985	   If it is, just return. */
1986	(void) cdrom_check_status(drive, sense);
1987
1988	if (CDROM_STATE_FLAGS(drive)->toc_valid)
1989		return 0;
1990
1991	/* Try to get the total cdrom capacity and sector size. */
1992	stat = cdrom_read_capacity(drive, &toc->capacity, &sectors_per_frame,
1993				   sense);
1994	if (stat)
1995		toc->capacity = 0x1fffff;
1996
1997	set_capacity(info->disk, toc->capacity * sectors_per_frame);
1998	/* Save a private copy of te TOC capacity for error handling */
1999	drive->probed_capacity = toc->capacity * sectors_per_frame;
2000
2001	blk_queue_hardsect_size(drive->queue,
2002				sectors_per_frame << SECTOR_BITS);
2003
2004	/* First read just the header, so we know how long the TOC is. */
2005	stat = cdrom_read_tocentry(drive, 0, 1, 0, (char *) &toc->hdr,
2006				    sizeof(struct atapi_toc_header), sense);
2007	if (stat)
2008		return stat;
2009
2010#if ! STANDARD_ATAPI
2011	if (CDROM_CONFIG_FLAGS(drive)->toctracks_as_bcd) {
2012		toc->hdr.first_track = bcd2bin(toc->hdr.first_track);
2013		toc->hdr.last_track  = bcd2bin(toc->hdr.last_track);
2014	}
2015#endif  /* not STANDARD_ATAPI */
2016
2017	ntracks = toc->hdr.last_track - toc->hdr.first_track + 1;
2018	if (ntracks <= 0)
2019		return -EIO;
2020	if (ntracks > MAX_TRACKS)
2021		ntracks = MAX_TRACKS;
2022
2023	/* Now read the whole schmeer. */
2024	stat = cdrom_read_tocentry(drive, toc->hdr.first_track, 1, 0,
2025				  (char *)&toc->hdr,
2026				   sizeof(struct atapi_toc_header) +
2027				   (ntracks + 1) *
2028				   sizeof(struct atapi_toc_entry), sense);
2029
2030	if (stat && toc->hdr.first_track > 1) {
2031		/* Cds with CDI tracks only don't have any TOC entries,
2032		   despite of this the returned values are
2033		   first_track == last_track = number of CDI tracks + 1,
2034		   so that this case is indistinguishable from the same
2035		   layout plus an additional audio track.
2036		   If we get an error for the regular case, we assume
2037		   a CDI without additional audio tracks. In this case
2038		   the readable TOC is empty (CDI tracks are not included)
2039		   and only holds the Leadout entry. Heiko Ei�feldt */
2040		ntracks = 0;
2041		stat = cdrom_read_tocentry(drive, CDROM_LEADOUT, 1, 0,
2042					   (char *)&toc->hdr,
2043					   sizeof(struct atapi_toc_header) +
2044					   (ntracks + 1) *
2045					   sizeof(struct atapi_toc_entry),
2046					   sense);
2047		if (stat) {
2048			return stat;
2049		}
2050#if ! STANDARD_ATAPI
2051		if (CDROM_CONFIG_FLAGS(drive)->toctracks_as_bcd) {
2052			toc->hdr.first_track = bin2bcd(CDROM_LEADOUT);
2053			toc->hdr.last_track = bin2bcd(CDROM_LEADOUT);
2054		} else
2055#endif  /* not STANDARD_ATAPI */
2056		{
2057			toc->hdr.first_track = CDROM_LEADOUT;
2058			toc->hdr.last_track = CDROM_LEADOUT;
2059		}
2060	}
2061
2062	if (stat)
2063		return stat;
2064
2065	toc->hdr.toc_length = ntohs (toc->hdr.toc_length);
2066
2067#if ! STANDARD_ATAPI
2068	if (CDROM_CONFIG_FLAGS(drive)->toctracks_as_bcd) {
2069		toc->hdr.first_track = bcd2bin(toc->hdr.first_track);
2070		toc->hdr.last_track  = bcd2bin(toc->hdr.last_track);
2071	}
2072#endif  /* not STANDARD_ATAPI */
2073
2074	for (i=0; i<=ntracks; i++) {
2075#if ! STANDARD_ATAPI
2076		if (CDROM_CONFIG_FLAGS(drive)->tocaddr_as_bcd) {
2077			if (CDROM_CONFIG_FLAGS(drive)->toctracks_as_bcd)
2078				toc->ent[i].track = bcd2bin(toc->ent[i].track);
2079			msf_from_bcd(&toc->ent[i].addr.msf);
2080		}
2081#endif  /* not STANDARD_ATAPI */
2082		toc->ent[i].addr.lba = msf_to_lba (toc->ent[i].addr.msf.minute,
2083						   toc->ent[i].addr.msf.second,
2084						   toc->ent[i].addr.msf.frame);
2085	}
2086
2087	/* Read the multisession information. */
2088	if (toc->hdr.first_track != CDROM_LEADOUT) {
2089		/* Read the multisession information. */
2090		stat = cdrom_read_tocentry(drive, 0, 0, 1, (char *)&ms_tmp,
2091					   sizeof(ms_tmp), sense);
2092		if (stat)
2093			return stat;
2094
2095		toc->last_session_lba = be32_to_cpu(ms_tmp.ent.addr.lba);
2096	} else {
2097		ms_tmp.hdr.first_track = ms_tmp.hdr.last_track = CDROM_LEADOUT;
2098		toc->last_session_lba = msf_to_lba(0, 2, 0); /* 0m 2s 0f */
2099	}
2100
2101#if ! STANDARD_ATAPI
2102	if (CDROM_CONFIG_FLAGS(drive)->tocaddr_as_bcd) {
2103		/* Re-read multisession information using MSF format */
2104		stat = cdrom_read_tocentry(drive, 0, 1, 1, (char *)&ms_tmp,
2105					   sizeof(ms_tmp), sense);
2106		if (stat)
2107			return stat;
2108
2109		msf_from_bcd (&ms_tmp.ent.addr.msf);
2110		toc->last_session_lba = msf_to_lba(ms_tmp.ent.addr.msf.minute,
2111					  	   ms_tmp.ent.addr.msf.second,
2112						   ms_tmp.ent.addr.msf.frame);
2113	}
2114#endif  /* not STANDARD_ATAPI */
2115
2116	toc->xa_flag = (ms_tmp.hdr.first_track != ms_tmp.hdr.last_track);
2117
2118	/* Now try to get the total cdrom capacity. */
2119	stat = cdrom_get_last_written(cdi, &last_written);
2120	if (!stat && (last_written > toc->capacity)) {
2121		toc->capacity = last_written;
2122		set_capacity(info->disk, toc->capacity * sectors_per_frame);
2123		drive->probed_capacity = toc->capacity * sectors_per_frame;
2124	}
2125
2126	/* Remember that we've read this stuff. */
2127	CDROM_STATE_FLAGS(drive)->toc_valid = 1;
2128
2129	return 0;
2130}
2131
2132
2133static int cdrom_read_subchannel(ide_drive_t *drive, int format, char *buf,
2134				 int buflen, struct request_sense *sense)
2135{
2136	struct request req;
2137
2138	cdrom_prepare_request(drive, &req);
2139
2140	req.sense = sense;
2141	req.data = buf;
2142	req.data_len = buflen;
2143	req.cmd[0] = GPCMD_READ_SUBCHANNEL;
2144	req.cmd[1] = 2;     /* MSF addressing */
2145	req.cmd[2] = 0x40;  /* request subQ data */
2146	req.cmd[3] = format;
2147	req.cmd[7] = (buflen >> 8);
2148	req.cmd[8] = (buflen & 0xff);
2149	return cdrom_queue_packet_command(drive, &req);
2150}
2151
2152/* ATAPI cdrom drives are free to select the speed you request or any slower
2153   rate :-( Requesting too fast a speed will _not_ produce an error. */
2154static int cdrom_select_speed(ide_drive_t *drive, int speed,
2155			      struct request_sense *sense)
2156{
2157	struct request req;
2158	cdrom_prepare_request(drive, &req);
2159
2160	req.sense = sense;
2161	if (speed == 0)
2162		speed = 0xffff; /* set to max */
2163	else
2164		speed *= 177;   /* Nx to kbytes/s */
2165
2166	req.cmd[0] = GPCMD_SET_SPEED;
2167	/* Read Drive speed in kbytes/second MSB */
2168	req.cmd[2] = (speed >> 8) & 0xff;
2169	/* Read Drive speed in kbytes/second LSB */
2170	req.cmd[3] = speed & 0xff;
2171	if (CDROM_CONFIG_FLAGS(drive)->cd_r ||
2172	    CDROM_CONFIG_FLAGS(drive)->cd_rw ||
2173	    CDROM_CONFIG_FLAGS(drive)->dvd_r) {
2174		/* Write Drive speed in kbytes/second MSB */
2175		req.cmd[4] = (speed >> 8) & 0xff;
2176		/* Write Drive speed in kbytes/second LSB */
2177		req.cmd[5] = speed & 0xff;
2178       }
2179
2180	return cdrom_queue_packet_command(drive, &req);
2181}
2182
2183static int cdrom_play_audio(ide_drive_t *drive, int lba_start, int lba_end)
2184{
2185	struct request_sense sense;
2186	struct request req;
2187
2188	cdrom_prepare_request(drive, &req);
2189
2190	req.sense = &sense;
2191	req.cmd[0] = GPCMD_PLAY_AUDIO_MSF;
2192	lba_to_msf(lba_start, &req.cmd[3], &req.cmd[4], &req.cmd[5]);
2193	lba_to_msf(lba_end-1, &req.cmd[6], &req.cmd[7], &req.cmd[8]);
2194
2195	return cdrom_queue_packet_command(drive, &req);
2196}
2197
2198static int cdrom_get_toc_entry(ide_drive_t *drive, int track,
2199				struct atapi_toc_entry **ent)
2200{
2201	struct cdrom_info *info = drive->driver_data;
2202	struct atapi_toc *toc = info->toc;
2203	int ntracks;
2204
2205	/*
2206	 * don't serve cached data, if the toc isn't valid
2207	 */
2208	if (!CDROM_STATE_FLAGS(drive)->toc_valid)
2209		return -EINVAL;
2210
2211	/* Check validity of requested track number. */
2212	ntracks = toc->hdr.last_track - toc->hdr.first_track + 1;
2213	if (toc->hdr.first_track == CDROM_LEADOUT) ntracks = 0;
2214	if (track == CDROM_LEADOUT)
2215		*ent = &toc->ent[ntracks];
2216	else if (track < toc->hdr.first_track ||
2217		 track > toc->hdr.last_track)
2218		return -EINVAL;
2219	else
2220		*ent = &toc->ent[track - toc->hdr.first_track];
2221
2222	return 0;
2223}
2224
2225/* the generic packet interface to cdrom.c */
2226static int ide_cdrom_packet(struct cdrom_device_info *cdi,
2227			    struct packet_command *cgc)
2228{
2229	struct request req;
2230	ide_drive_t *drive = cdi->handle;
2231
2232	if (cgc->timeout <= 0)
2233		cgc->timeout = ATAPI_WAIT_PC;
2234
2235	/* here we queue the commands from the uniform CD-ROM
2236	   layer. the packet must be complete, as we do not
2237	   touch it at all. */
2238	cdrom_prepare_request(drive, &req);
2239	memcpy(req.cmd, cgc->cmd, CDROM_PACKET_SIZE);
2240	if (cgc->sense)
2241		memset(cgc->sense, 0, sizeof(struct request_sense));
2242	req.data = cgc->buffer;
2243	req.data_len = cgc->buflen;
2244	req.timeout = cgc->timeout;
2245
2246	if (cgc->quiet)
2247		req.cmd_flags |= REQ_QUIET;
2248
2249	req.sense = cgc->sense;
2250	cgc->stat = cdrom_queue_packet_command(drive, &req);
2251	if (!cgc->stat)
2252		cgc->buflen -= req.data_len;
2253	return cgc->stat;
2254}
2255
2256static
2257int ide_cdrom_audio_ioctl (struct cdrom_device_info *cdi,
2258			   unsigned int cmd, void *arg)
2259
2260{
2261	ide_drive_t *drive = cdi->handle;
2262	struct cdrom_info *info = drive->driver_data;
2263	int stat;
2264
2265	switch (cmd) {
2266	/*
2267	 * emulate PLAY_AUDIO_TI command with PLAY_AUDIO_10, since
2268	 * atapi doesn't support it
2269	 */
2270	case CDROMPLAYTRKIND: {
2271		unsigned long lba_start, lba_end;
2272		struct cdrom_ti *ti = arg;
2273		struct atapi_toc_entry *first_toc, *last_toc;
2274
2275		stat = cdrom_get_toc_entry(drive, ti->cdti_trk0, &first_toc);
2276		if (stat)
2277			return stat;
2278
2279		stat = cdrom_get_toc_entry(drive, ti->cdti_trk1, &last_toc);
2280		if (stat)
2281			return stat;
2282
2283		if (ti->cdti_trk1 != CDROM_LEADOUT)
2284			++last_toc;
2285		lba_start = first_toc->addr.lba;
2286		lba_end   = last_toc->addr.lba;
2287
2288		if (lba_end <= lba_start)
2289			return -EINVAL;
2290
2291		return cdrom_play_audio(drive, lba_start, lba_end);
2292	}
2293
2294	case CDROMREADTOCHDR: {
2295		struct cdrom_tochdr *tochdr = arg;
2296		struct atapi_toc *toc;
2297
2298		/* Make sure our saved TOC is valid. */
2299		stat = cdrom_read_toc(drive, NULL);
2300		if (stat)
2301			return stat;
2302
2303		toc = info->toc;
2304		tochdr->cdth_trk0 = toc->hdr.first_track;
2305		tochdr->cdth_trk1 = toc->hdr.last_track;
2306
2307		return 0;
2308	}
2309
2310	case CDROMREADTOCENTRY: {
2311		struct cdrom_tocentry *tocentry = arg;
2312		struct atapi_toc_entry *toce;
2313
2314		stat = cdrom_get_toc_entry(drive, tocentry->cdte_track, &toce);
2315		if (stat)
2316			return stat;
2317
2318		tocentry->cdte_ctrl = toce->control;
2319		tocentry->cdte_adr  = toce->adr;
2320		if (tocentry->cdte_format == CDROM_MSF) {
2321			lba_to_msf (toce->addr.lba,
2322				   &tocentry->cdte_addr.msf.minute,
2323				   &tocentry->cdte_addr.msf.second,
2324				   &tocentry->cdte_addr.msf.frame);
2325		} else
2326			tocentry->cdte_addr.lba = toce->addr.lba;
2327
2328		return 0;
2329	}
2330
2331	default:
2332		return -EINVAL;
2333	}
2334}
2335
2336static
2337int ide_cdrom_reset (struct cdrom_device_info *cdi)
2338{
2339	ide_drive_t *drive = cdi->handle;
2340	struct request_sense sense;
2341	struct request req;
2342	int ret;
2343
2344	cdrom_prepare_request(drive, &req);
2345	req.cmd_type = REQ_TYPE_SPECIAL;
2346	req.cmd_flags = REQ_QUIET;
2347	ret = ide_do_drive_cmd(drive, &req, ide_wait);
2348
2349	/*
2350	 * A reset will unlock the door. If it was previously locked,
2351	 * lock it again.
2352	 */
2353	if (CDROM_STATE_FLAGS(drive)->door_locked)
2354		(void) cdrom_lockdoor(drive, 1, &sense);
2355
2356	return ret;
2357}
2358
2359
2360static
2361int ide_cdrom_tray_move (struct cdrom_device_info *cdi, int position)
2362{
2363	ide_drive_t *drive = cdi->handle;
2364	struct request_sense sense;
2365
2366	if (position) {
2367		int stat = cdrom_lockdoor(drive, 0, &sense);
2368		if (stat)
2369			return stat;
2370	}
2371
2372	return cdrom_eject(drive, !position, &sense);
2373}
2374
2375static
2376int ide_cdrom_lock_door (struct cdrom_device_info *cdi, int lock)
2377{
2378	ide_drive_t *drive = cdi->handle;
2379	return cdrom_lockdoor(drive, lock, NULL);
2380}
2381
2382static
2383int ide_cdrom_get_capabilities(ide_drive_t *drive, struct atapi_capabilities_page *cap)
2384{
2385	struct cdrom_info *info = drive->driver_data;
2386	struct cdrom_device_info *cdi = &info->devinfo;
2387	struct packet_command cgc;
2388	int stat, attempts = 3, size = sizeof(*cap);
2389
2390	/*
2391	 * ACER50 (and others?) require the full spec length mode sense
2392	 * page capabilities size, but older drives break.
2393	 */
2394	if (!(!strcmp(drive->id->model, "ATAPI CD ROM DRIVE 50X MAX") ||
2395	    !strcmp(drive->id->model, "WPI CDS-32X")))
2396		size -= sizeof(cap->pad);
2397
2398	init_cdrom_command(&cgc, cap, size, CGC_DATA_UNKNOWN);
2399	do { /* we seem to get stat=0x01,err=0x00 the first time (??) */
2400		stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CAPABILITIES_PAGE, 0);
2401		if (!stat)
2402			break;
2403	} while (--attempts);
2404	return stat;
2405}
2406
2407static
2408void ide_cdrom_update_speed (ide_drive_t *drive, struct atapi_capabilities_page *cap)
2409{
2410	/* The ACER/AOpen 24X cdrom has the speed fields byte-swapped */
2411	if (!drive->id->model[0] &&
2412	    !strncmp(drive->id->fw_rev, "241N", 4)) {
2413		CDROM_STATE_FLAGS(drive)->current_speed  =
2414			(((unsigned int)cap->curspeed) + (176/2)) / 176;
2415		CDROM_CONFIG_FLAGS(drive)->max_speed =
2416			(((unsigned int)cap->maxspeed) + (176/2)) / 176;
2417	} else {
2418		CDROM_STATE_FLAGS(drive)->current_speed  =
2419			(ntohs(cap->curspeed) + (176/2)) / 176;
2420		CDROM_CONFIG_FLAGS(drive)->max_speed =
2421			(ntohs(cap->maxspeed) + (176/2)) / 176;
2422	}
2423}
2424
2425static
2426int ide_cdrom_select_speed (struct cdrom_device_info *cdi, int speed)
2427{
2428	ide_drive_t *drive = cdi->handle;
2429	struct request_sense sense;
2430	struct atapi_capabilities_page cap;
2431	int stat;
2432
2433	if ((stat = cdrom_select_speed(drive, speed, &sense)) < 0)
2434		return stat;
2435
2436	if (!ide_cdrom_get_capabilities(drive, &cap)) {
2437		ide_cdrom_update_speed(drive, &cap);
2438		cdi->speed = CDROM_STATE_FLAGS(drive)->current_speed;
2439	}
2440        return 0;
2441}
2442
2443/*
2444 * add logic to try GET_EVENT command first to check for media and tray
2445 * status. this should be supported by newer cd-r/w and all DVD etc
2446 * drives
2447 */
2448static
2449int ide_cdrom_drive_status (struct cdrom_device_info *cdi, int slot_nr)
2450{
2451	ide_drive_t *drive = cdi->handle;
2452	struct media_event_desc med;
2453	struct request_sense sense;
2454	int stat;
2455
2456	if (slot_nr != CDSL_CURRENT)
2457		return -EINVAL;
2458
2459	stat = cdrom_check_status(drive, &sense);
2460	if (!stat || sense.sense_key == UNIT_ATTENTION)
2461		return CDS_DISC_OK;
2462
2463	if (!cdrom_get_media_event(cdi, &med)) {
2464		if (med.media_present)
2465			return CDS_DISC_OK;
2466		else if (med.door_open)
2467			return CDS_TRAY_OPEN;
2468		else
2469			return CDS_NO_DISC;
2470	}
2471
2472	if (sense.sense_key == NOT_READY && sense.asc == 0x04 && sense.ascq == 0x04)
2473		return CDS_DISC_OK;
2474
2475	/*
2476	 * If not using Mt Fuji extended media tray reports,
2477	 * just return TRAY_OPEN since ATAPI doesn't provide
2478	 * any other way to detect this...
2479	 */
2480	if (sense.sense_key == NOT_READY) {
2481		if (sense.asc == 0x3a && sense.ascq == 1)
2482			return CDS_NO_DISC;
2483		else
2484			return CDS_TRAY_OPEN;
2485	}
2486	return CDS_DRIVE_NOT_READY;
2487}
2488
2489static
2490int ide_cdrom_get_last_session (struct cdrom_device_info *cdi,
2491				struct cdrom_multisession *ms_info)
2492{
2493	struct atapi_toc *toc;
2494	ide_drive_t *drive = cdi->handle;
2495	struct cdrom_info *info = drive->driver_data;
2496	struct request_sense sense;
2497	int ret;
2498
2499	if (!CDROM_STATE_FLAGS(drive)->toc_valid || info->toc == NULL)
2500		if ((ret = cdrom_read_toc(drive, &sense)))
2501			return ret;
2502
2503	toc = info->toc;
2504	ms_info->addr.lba = toc->last_session_lba;
2505	ms_info->xa_flag = toc->xa_flag;
2506
2507	return 0;
2508}
2509
2510static
2511int ide_cdrom_get_mcn (struct cdrom_device_info *cdi,
2512		       struct cdrom_mcn *mcn_info)
2513{
2514	int stat;
2515	char mcnbuf[24];
2516	ide_drive_t *drive = cdi->handle;
2517
2518/* get MCN */
2519	if ((stat = cdrom_read_subchannel(drive, 2, mcnbuf, sizeof (mcnbuf), NULL)))
2520		return stat;
2521
2522	memcpy (mcn_info->medium_catalog_number, mcnbuf+9,
2523		sizeof (mcn_info->medium_catalog_number)-1);
2524	mcn_info->medium_catalog_number[sizeof (mcn_info->medium_catalog_number)-1]
2525		= '\0';
2526
2527	return 0;
2528}
2529
2530
2531
2532/****************************************************************************
2533 * Other driver requests (open, close, check media change).
2534 */
2535
2536static
2537int ide_cdrom_check_media_change_real (struct cdrom_device_info *cdi,
2538				       int slot_nr)
2539{
2540	ide_drive_t *drive = cdi->handle;
2541	int retval;
2542
2543	if (slot_nr == CDSL_CURRENT) {
2544		(void) cdrom_check_status(drive, NULL);
2545		retval = CDROM_STATE_FLAGS(drive)->media_changed;
2546		CDROM_STATE_FLAGS(drive)->media_changed = 0;
2547		return retval;
2548	} else {
2549		return -EINVAL;
2550	}
2551}
2552
2553
2554static
2555int ide_cdrom_open_real (struct cdrom_device_info *cdi, int purpose)
2556{
2557	return 0;
2558}
2559
2560/*
2561 * Close down the device.  Invalidate all cached blocks.
2562 */
2563
2564static
2565void ide_cdrom_release_real (struct cdrom_device_info *cdi)
2566{
2567	ide_drive_t *drive = cdi->handle;
2568
2569	if (!cdi->use_count)
2570		CDROM_STATE_FLAGS(drive)->toc_valid = 0;
2571}
2572
2573
2574
2575/****************************************************************************
2576 * Device initialization.
2577 */
2578static struct cdrom_device_ops ide_cdrom_dops = {
2579	.open			= ide_cdrom_open_real,
2580	.release		= ide_cdrom_release_real,
2581	.drive_status		= ide_cdrom_drive_status,
2582	.media_changed		= ide_cdrom_check_media_change_real,
2583	.tray_move		= ide_cdrom_tray_move,
2584	.lock_door		= ide_cdrom_lock_door,
2585	.select_speed		= ide_cdrom_select_speed,
2586	.get_last_session	= ide_cdrom_get_last_session,
2587	.get_mcn		= ide_cdrom_get_mcn,
2588	.reset			= ide_cdrom_reset,
2589	.audio_ioctl		= ide_cdrom_audio_ioctl,
2590	.capability		= CDC_CLOSE_TRAY | CDC_OPEN_TRAY | CDC_LOCK |
2591				CDC_SELECT_SPEED | CDC_SELECT_DISC |
2592				CDC_MULTI_SESSION | CDC_MCN |
2593				CDC_MEDIA_CHANGED | CDC_PLAY_AUDIO | CDC_RESET |
2594				CDC_DRIVE_STATUS | CDC_CD_R |
2595				CDC_CD_RW | CDC_DVD | CDC_DVD_R| CDC_DVD_RAM |
2596				CDC_GENERIC_PACKET | CDC_MO_DRIVE | CDC_MRW |
2597				CDC_MRW_W | CDC_RAM,
2598	.generic_packet		= ide_cdrom_packet,
2599};
2600
2601static int ide_cdrom_register (ide_drive_t *drive, int nslots)
2602{
2603	struct cdrom_info *info = drive->driver_data;
2604	struct cdrom_device_info *devinfo = &info->devinfo;
2605
2606	devinfo->ops = &ide_cdrom_dops;
2607	devinfo->mask = 0;
2608	devinfo->speed = CDROM_STATE_FLAGS(drive)->current_speed;
2609	devinfo->capacity = nslots;
2610	devinfo->handle = drive;
2611	strcpy(devinfo->name, drive->name);
2612
2613	/* set capability mask to match the probe. */
2614	if (!CDROM_CONFIG_FLAGS(drive)->cd_r)
2615		devinfo->mask |= CDC_CD_R;
2616	if (!CDROM_CONFIG_FLAGS(drive)->cd_rw)
2617		devinfo->mask |= CDC_CD_RW;
2618	if (!CDROM_CONFIG_FLAGS(drive)->dvd)
2619		devinfo->mask |= CDC_DVD;
2620	if (!CDROM_CONFIG_FLAGS(drive)->dvd_r)
2621		devinfo->mask |= CDC_DVD_R;
2622	if (!CDROM_CONFIG_FLAGS(drive)->dvd_ram)
2623		devinfo->mask |= CDC_DVD_RAM;
2624	if (!CDROM_CONFIG_FLAGS(drive)->is_changer)
2625		devinfo->mask |= CDC_SELECT_DISC;
2626	if (!CDROM_CONFIG_FLAGS(drive)->audio_play)
2627		devinfo->mask |= CDC_PLAY_AUDIO;
2628	if (!CDROM_CONFIG_FLAGS(drive)->close_tray)
2629		devinfo->mask |= CDC_CLOSE_TRAY;
2630	if (!CDROM_CONFIG_FLAGS(drive)->mo_drive)
2631		devinfo->mask |= CDC_MO_DRIVE;
2632	if (!CDROM_CONFIG_FLAGS(drive)->ram)
2633		devinfo->mask |= CDC_RAM;
2634
2635	devinfo->disk = info->disk;
2636	return register_cdrom(devinfo);
2637}
2638
2639static
2640int ide_cdrom_probe_capabilities (ide_drive_t *drive)
2641{
2642	struct cdrom_info *info = drive->driver_data;
2643	struct cdrom_device_info *cdi = &info->devinfo;
2644	struct atapi_capabilities_page cap;
2645	int nslots = 1;
2646
2647	if (drive->media == ide_optical) {
2648		CDROM_CONFIG_FLAGS(drive)->mo_drive = 1;
2649		CDROM_CONFIG_FLAGS(drive)->ram = 1;
2650		printk(KERN_ERR "%s: ATAPI magneto-optical drive\n", drive->name);
2651		return nslots;
2652	}
2653
2654	if (CDROM_CONFIG_FLAGS(drive)->nec260 ||
2655	    !strcmp(drive->id->model,"STINGRAY 8422 IDE 8X CD-ROM 7-27-95")) {
2656		CDROM_CONFIG_FLAGS(drive)->no_eject = 0;
2657		CDROM_CONFIG_FLAGS(drive)->audio_play = 1;
2658		return nslots;
2659	}
2660
2661	/*
2662	 * we have to cheat a little here. the packet will eventually
2663	 * be queued with ide_cdrom_packet(), which extracts the
2664	 * drive from cdi->handle. Since this device hasn't been
2665	 * registered with the Uniform layer yet, it can't do this.
2666	 * Same goes for cdi->ops.
2667	 */
2668	cdi->handle = drive;
2669	cdi->ops = &ide_cdrom_dops;
2670
2671	if (ide_cdrom_get_capabilities(drive, &cap))
2672		return 0;
2673
2674	if (cap.lock == 0)
2675		CDROM_CONFIG_FLAGS(drive)->no_doorlock = 1;
2676	if (cap.eject)
2677		CDROM_CONFIG_FLAGS(drive)->no_eject = 0;
2678	if (cap.cd_r_write)
2679		CDROM_CONFIG_FLAGS(drive)->cd_r = 1;
2680	if (cap.cd_rw_write) {
2681		CDROM_CONFIG_FLAGS(drive)->cd_rw = 1;
2682		CDROM_CONFIG_FLAGS(drive)->ram = 1;
2683	}
2684	if (cap.test_write)
2685		CDROM_CONFIG_FLAGS(drive)->test_write = 1;
2686	if (cap.dvd_ram_read || cap.dvd_r_read || cap.dvd_rom)
2687		CDROM_CONFIG_FLAGS(drive)->dvd = 1;
2688	if (cap.dvd_ram_write) {
2689		CDROM_CONFIG_FLAGS(drive)->dvd_ram = 1;
2690		CDROM_CONFIG_FLAGS(drive)->ram = 1;
2691	}
2692	if (cap.dvd_r_write)
2693		CDROM_CONFIG_FLAGS(drive)->dvd_r = 1;
2694	if (cap.audio_play)
2695		CDROM_CONFIG_FLAGS(drive)->audio_play = 1;
2696	if (cap.mechtype == mechtype_caddy || cap.mechtype == mechtype_popup)
2697		CDROM_CONFIG_FLAGS(drive)->close_tray = 0;
2698
2699	/* Some drives used by Apple don't advertise audio play
2700	 * but they do support reading TOC & audio datas
2701	 */
2702	if (strcmp(drive->id->model, "MATSHITADVD-ROM SR-8187") == 0 ||
2703	    strcmp(drive->id->model, "MATSHITADVD-ROM SR-8186") == 0 ||
2704	    strcmp(drive->id->model, "MATSHITADVD-ROM SR-8176") == 0 ||
2705	    strcmp(drive->id->model, "MATSHITADVD-ROM SR-8174") == 0)
2706		CDROM_CONFIG_FLAGS(drive)->audio_play = 1;
2707
2708#if ! STANDARD_ATAPI
2709	if (cdi->sanyo_slot > 0) {
2710		CDROM_CONFIG_FLAGS(drive)->is_changer = 1;
2711		nslots = 3;
2712	}
2713
2714	else
2715#endif /* not STANDARD_ATAPI */
2716	if (cap.mechtype == mechtype_individual_changer ||
2717	    cap.mechtype == mechtype_cartridge_changer) {
2718		if ((nslots = cdrom_number_of_slots(cdi)) > 1) {
2719			CDROM_CONFIG_FLAGS(drive)->is_changer = 1;
2720			CDROM_CONFIG_FLAGS(drive)->supp_disc_present = 1;
2721		}
2722	}
2723
2724	ide_cdrom_update_speed(drive, &cap);
2725	/* don't print speed if the drive reported 0.
2726	 */
2727	printk(KERN_INFO "%s: ATAPI", drive->name);
2728	if (CDROM_CONFIG_FLAGS(drive)->max_speed)
2729		printk(" %dX", CDROM_CONFIG_FLAGS(drive)->max_speed);
2730	printk(" %s", CDROM_CONFIG_FLAGS(drive)->dvd ? "DVD-ROM" : "CD-ROM");
2731
2732	if (CDROM_CONFIG_FLAGS(drive)->dvd_r|CDROM_CONFIG_FLAGS(drive)->dvd_ram)
2733        	printk(" DVD%s%s",
2734        	(CDROM_CONFIG_FLAGS(drive)->dvd_r)? "-R" : "",
2735        	(CDROM_CONFIG_FLAGS(drive)->dvd_ram)? "-RAM" : "");
2736
2737        if (CDROM_CONFIG_FLAGS(drive)->cd_r|CDROM_CONFIG_FLAGS(drive)->cd_rw)
2738        	printk(" CD%s%s",
2739        	(CDROM_CONFIG_FLAGS(drive)->cd_r)? "-R" : "",
2740        	(CDROM_CONFIG_FLAGS(drive)->cd_rw)? "/RW" : "");
2741
2742        if (CDROM_CONFIG_FLAGS(drive)->is_changer)
2743        	printk(" changer w/%d slots", nslots);
2744        else
2745        	printk(" drive");
2746
2747	printk(", %dkB Cache", be16_to_cpu(cap.buffer_size));
2748
2749	if (drive->using_dma)
2750		ide_dma_verbose(drive);
2751
2752	printk("\n");
2753
2754	return nslots;
2755}
2756
2757#ifdef CONFIG_IDE_PROC_FS
2758static void ide_cdrom_add_settings(ide_drive_t *drive)
2759{
2760	ide_add_setting(drive, "dsc_overlap", SETTING_RW, TYPE_BYTE, 0, 1, 1, 1, &drive->dsc_overlap, NULL);
2761}
2762#else
2763static inline void ide_cdrom_add_settings(ide_drive_t *drive) { ; }
2764#endif
2765
2766/*
2767 * standard prep_rq_fn that builds 10 byte cmds
2768 */
2769static int ide_cdrom_prep_fs(request_queue_t *q, struct request *rq)
2770{
2771	int hard_sect = queue_hardsect_size(q);
2772	long block = (long)rq->hard_sector / (hard_sect >> 9);
2773	unsigned long blocks = rq->hard_nr_sectors / (hard_sect >> 9);
2774
2775	memset(rq->cmd, 0, sizeof(rq->cmd));
2776
2777	if (rq_data_dir(rq) == READ)
2778		rq->cmd[0] = GPCMD_READ_10;
2779	else
2780		rq->cmd[0] = GPCMD_WRITE_10;
2781
2782	/*
2783	 * fill in lba
2784	 */
2785	rq->cmd[2] = (block >> 24) & 0xff;
2786	rq->cmd[3] = (block >> 16) & 0xff;
2787	rq->cmd[4] = (block >>  8) & 0xff;
2788	rq->cmd[5] = block & 0xff;
2789
2790	/*
2791	 * and transfer length
2792	 */
2793	rq->cmd[7] = (blocks >> 8) & 0xff;
2794	rq->cmd[8] = blocks & 0xff;
2795	rq->cmd_len = 10;
2796	return BLKPREP_OK;
2797}
2798
2799/*
2800 * Most of the SCSI commands are supported directly by ATAPI devices.
2801 * This transform handles the few exceptions.
2802 */
2803static int ide_cdrom_prep_pc(struct request *rq)
2804{
2805	u8 *c = rq->cmd;
2806
2807	/*
2808	 * Transform 6-byte read/write commands to the 10-byte version
2809	 */
2810	if (c[0] == READ_6 || c[0] == WRITE_6) {
2811		c[8] = c[4];
2812		c[5] = c[3];
2813		c[4] = c[2];
2814		c[3] = c[1] & 0x1f;
2815		c[2] = 0;
2816		c[1] &= 0xe0;
2817		c[0] += (READ_10 - READ_6);
2818		rq->cmd_len = 10;
2819		return BLKPREP_OK;
2820	}
2821
2822	/*
2823	 * it's silly to pretend we understand 6-byte sense commands, just
2824	 * reject with ILLEGAL_REQUEST and the caller should take the
2825	 * appropriate action
2826	 */
2827	if (c[0] == MODE_SENSE || c[0] == MODE_SELECT) {
2828		rq->errors = ILLEGAL_REQUEST;
2829		return BLKPREP_KILL;
2830	}
2831
2832	return BLKPREP_OK;
2833}
2834
2835static int ide_cdrom_prep_fn(request_queue_t *q, struct request *rq)
2836{
2837	if (blk_fs_request(rq))
2838		return ide_cdrom_prep_fs(q, rq);
2839	else if (blk_pc_request(rq))
2840		return ide_cdrom_prep_pc(rq);
2841
2842	return 0;
2843}
2844
2845static
2846int ide_cdrom_setup (ide_drive_t *drive)
2847{
2848	struct cdrom_info *info = drive->driver_data;
2849	struct cdrom_device_info *cdi = &info->devinfo;
2850	int nslots;
2851
2852	blk_queue_prep_rq(drive->queue, ide_cdrom_prep_fn);
2853	blk_queue_dma_alignment(drive->queue, 31);
2854	drive->queue->unplug_delay = (1 * HZ) / 1000;
2855	if (!drive->queue->unplug_delay)
2856		drive->queue->unplug_delay = 1;
2857
2858	drive->special.all	= 0;
2859
2860	CDROM_STATE_FLAGS(drive)->media_changed = 1;
2861	CDROM_STATE_FLAGS(drive)->toc_valid     = 0;
2862	CDROM_STATE_FLAGS(drive)->door_locked   = 0;
2863
2864#if NO_DOOR_LOCKING
2865	CDROM_CONFIG_FLAGS(drive)->no_doorlock = 1;
2866#else
2867	CDROM_CONFIG_FLAGS(drive)->no_doorlock = 0;
2868#endif
2869
2870	CDROM_CONFIG_FLAGS(drive)->drq_interrupt = ((drive->id->config & 0x0060) == 0x20);
2871	CDROM_CONFIG_FLAGS(drive)->is_changer = 0;
2872	CDROM_CONFIG_FLAGS(drive)->cd_r = 0;
2873	CDROM_CONFIG_FLAGS(drive)->cd_rw = 0;
2874	CDROM_CONFIG_FLAGS(drive)->test_write = 0;
2875	CDROM_CONFIG_FLAGS(drive)->dvd = 0;
2876	CDROM_CONFIG_FLAGS(drive)->dvd_r = 0;
2877	CDROM_CONFIG_FLAGS(drive)->dvd_ram = 0;
2878	CDROM_CONFIG_FLAGS(drive)->no_eject = 1;
2879	CDROM_CONFIG_FLAGS(drive)->supp_disc_present = 0;
2880	CDROM_CONFIG_FLAGS(drive)->audio_play = 0;
2881	CDROM_CONFIG_FLAGS(drive)->close_tray = 1;
2882
2883	/* limit transfer size per interrupt. */
2884	CDROM_CONFIG_FLAGS(drive)->limit_nframes = 0;
2885	/* a testament to the nice quality of Samsung drives... */
2886	if (!strcmp(drive->id->model, "SAMSUNG CD-ROM SCR-2430"))
2887		CDROM_CONFIG_FLAGS(drive)->limit_nframes = 1;
2888	else if (!strcmp(drive->id->model, "SAMSUNG CD-ROM SCR-2432"))
2889		CDROM_CONFIG_FLAGS(drive)->limit_nframes = 1;
2890	/* the 3231 model does not support the SET_CD_SPEED command */
2891	else if (!strcmp(drive->id->model, "SAMSUNG CD-ROM SCR-3231"))
2892		cdi->mask |= CDC_SELECT_SPEED;
2893
2894#if ! STANDARD_ATAPI
2895	/* by default Sanyo 3 CD changer support is turned off and
2896           ATAPI Rev 2.2+ standard support for CD changers is used */
2897	cdi->sanyo_slot = 0;
2898
2899	CDROM_CONFIG_FLAGS(drive)->nec260 = 0;
2900	CDROM_CONFIG_FLAGS(drive)->toctracks_as_bcd = 0;
2901	CDROM_CONFIG_FLAGS(drive)->tocaddr_as_bcd = 0;
2902	CDROM_CONFIG_FLAGS(drive)->playmsf_as_bcd = 0;
2903	CDROM_CONFIG_FLAGS(drive)->subchan_as_bcd = 0;
2904
2905	if (strcmp (drive->id->model, "V003S0DS") == 0 &&
2906	    drive->id->fw_rev[4] == '1' &&
2907	    drive->id->fw_rev[6] <= '2') {
2908		/* Vertos 300.
2909		   Some versions of this drive like to talk BCD. */
2910		CDROM_CONFIG_FLAGS(drive)->toctracks_as_bcd = 1;
2911		CDROM_CONFIG_FLAGS(drive)->tocaddr_as_bcd = 1;
2912		CDROM_CONFIG_FLAGS(drive)->playmsf_as_bcd = 1;
2913		CDROM_CONFIG_FLAGS(drive)->subchan_as_bcd = 1;
2914	}
2915
2916	else if (strcmp (drive->id->model, "V006E0DS") == 0 &&
2917	    drive->id->fw_rev[4] == '1' &&
2918	    drive->id->fw_rev[6] <= '2') {
2919		/* Vertos 600 ESD. */
2920		CDROM_CONFIG_FLAGS(drive)->toctracks_as_bcd = 1;
2921	}
2922	else if (strcmp(drive->id->model, "NEC CD-ROM DRIVE:260") == 0 &&
2923		 strncmp(drive->id->fw_rev, "1.01", 4) == 0) {
2924		/* Old NEC260 (not R).
2925		   This drive was released before the 1.2 version
2926		   of the spec. */
2927		CDROM_CONFIG_FLAGS(drive)->tocaddr_as_bcd = 1;
2928		CDROM_CONFIG_FLAGS(drive)->playmsf_as_bcd = 1;
2929		CDROM_CONFIG_FLAGS(drive)->subchan_as_bcd = 1;
2930		CDROM_CONFIG_FLAGS(drive)->nec260         = 1;
2931	}
2932	else if (strcmp(drive->id->model, "WEARNES CDD-120") == 0 &&
2933		 strncmp(drive->id->fw_rev, "A1.1", 4) == 0) {
2934		/* Wearnes */
2935		CDROM_CONFIG_FLAGS(drive)->playmsf_as_bcd = 1;
2936		CDROM_CONFIG_FLAGS(drive)->subchan_as_bcd = 1;
2937	}
2938        /* Sanyo 3 CD changer uses a non-standard command
2939           for CD changing */
2940        else if ((strcmp(drive->id->model, "CD-ROM CDR-C3 G") == 0) ||
2941                 (strcmp(drive->id->model, "CD-ROM CDR-C3G") == 0) ||
2942                 (strcmp(drive->id->model, "CD-ROM CDR_C36") == 0)) {
2943                 /* uses CD in slot 0 when value is set to 3 */
2944                 cdi->sanyo_slot = 3;
2945        }
2946#endif /* not STANDARD_ATAPI */
2947
2948	info->toc		= NULL;
2949	info->buffer		= NULL;
2950	info->sector_buffered	= 0;
2951	info->nsectors_buffered	= 0;
2952	info->changer_info      = NULL;
2953	info->last_block	= 0;
2954	info->start_seek	= 0;
2955
2956	nslots = ide_cdrom_probe_capabilities (drive);
2957
2958	/*
2959	 * set correct block size
2960	 */
2961	blk_queue_hardsect_size(drive->queue, CD_FRAMESIZE);
2962
2963	if (drive->autotune == IDE_TUNE_DEFAULT ||
2964	    drive->autotune == IDE_TUNE_AUTO)
2965		drive->dsc_overlap = (drive->next != drive);
2966
2967	if (ide_cdrom_register(drive, nslots)) {
2968		printk (KERN_ERR "%s: ide_cdrom_setup failed to register device with the cdrom driver.\n", drive->name);
2969		info->devinfo.handle = NULL;
2970		return 1;
2971	}
2972	ide_cdrom_add_settings(drive);
2973	return 0;
2974}
2975
2976#ifdef CONFIG_IDE_PROC_FS
2977static
2978sector_t ide_cdrom_capacity (ide_drive_t *drive)
2979{
2980	unsigned long capacity, sectors_per_frame;
2981
2982	if (cdrom_read_capacity(drive, &capacity, &sectors_per_frame, NULL))
2983		return 0;
2984
2985	return capacity * sectors_per_frame;
2986}
2987#endif
2988
2989static void ide_cd_remove(ide_drive_t *drive)
2990{
2991	struct cdrom_info *info = drive->driver_data;
2992
2993	ide_proc_unregister_driver(drive, info->driver);
2994
2995	del_gendisk(info->disk);
2996
2997	ide_cd_put(info);
2998}
2999
3000static void ide_cd_release(struct kref *kref)
3001{
3002	struct cdrom_info *info = to_ide_cd(kref);
3003	struct cdrom_device_info *devinfo = &info->devinfo;
3004	ide_drive_t *drive = info->drive;
3005	struct gendisk *g = info->disk;
3006
3007	kfree(info->buffer);
3008	kfree(info->toc);
3009	kfree(info->changer_info);
3010	if (devinfo->handle == drive && unregister_cdrom(devinfo))
3011		printk(KERN_ERR "%s: %s failed to unregister device from the cdrom "
3012				"driver.\n", __FUNCTION__, drive->name);
3013	drive->dsc_overlap = 0;
3014	drive->driver_data = NULL;
3015	blk_queue_prep_rq(drive->queue, NULL);
3016	g->private_data = NULL;
3017	put_disk(g);
3018	kfree(info);
3019}
3020
3021static int ide_cd_probe(ide_drive_t *);
3022
3023#ifdef CONFIG_IDE_PROC_FS
3024static int proc_idecd_read_capacity
3025	(char *page, char **start, off_t off, int count, int *eof, void *data)
3026{
3027	ide_drive_t *drive = data;
3028	int len;
3029
3030	len = sprintf(page,"%llu\n", (long long)ide_cdrom_capacity(drive));
3031	PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
3032}
3033
3034static ide_proc_entry_t idecd_proc[] = {
3035	{ "capacity", S_IFREG|S_IRUGO, proc_idecd_read_capacity, NULL },
3036	{ NULL, 0, NULL, NULL }
3037};
3038#endif
3039
3040static ide_driver_t ide_cdrom_driver = {
3041	.gen_driver = {
3042		.owner		= THIS_MODULE,
3043		.name		= "ide-cdrom",
3044		.bus		= &ide_bus_type,
3045	},
3046	.probe			= ide_cd_probe,
3047	.remove			= ide_cd_remove,
3048	.version		= IDECD_VERSION,
3049	.media			= ide_cdrom,
3050	.supports_dsc_overlap	= 1,
3051	.do_request		= ide_do_rw_cdrom,
3052	.end_request		= ide_end_request,
3053	.error			= __ide_error,
3054	.abort			= __ide_abort,
3055#ifdef CONFIG_IDE_PROC_FS
3056	.proc			= idecd_proc,
3057#endif
3058};
3059
3060static int idecd_open(struct inode * inode, struct file * file)
3061{
3062	struct gendisk *disk = inode->i_bdev->bd_disk;
3063	struct cdrom_info *info;
3064	int rc = -ENOMEM;
3065
3066	if (!(info = ide_cd_get(disk)))
3067		return -ENXIO;
3068
3069	if (!info->buffer)
3070		info->buffer = kmalloc(SECTOR_BUFFER_SIZE, GFP_KERNEL|__GFP_REPEAT);
3071
3072	if (info->buffer)
3073		rc = cdrom_open(&info->devinfo, inode, file);
3074
3075	if (rc < 0)
3076		ide_cd_put(info);
3077
3078	return rc;
3079}
3080
3081static int idecd_release(struct inode * inode, struct file * file)
3082{
3083	struct gendisk *disk = inode->i_bdev->bd_disk;
3084	struct cdrom_info *info = ide_cd_g(disk);
3085
3086	cdrom_release (&info->devinfo, file);
3087
3088	ide_cd_put(info);
3089
3090	return 0;
3091}
3092
3093static int idecd_set_spindown(struct cdrom_device_info *cdi, unsigned long arg)
3094{
3095	struct packet_command cgc;
3096	char buffer[16];
3097	int stat;
3098	char spindown;
3099
3100	if (copy_from_user(&spindown, (void __user *)arg, sizeof(char)))
3101		return -EFAULT;
3102
3103	init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_UNKNOWN);
3104
3105	stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CDROM_PAGE, 0);
3106	if (stat)
3107		return stat;
3108
3109	buffer[11] = (buffer[11] & 0xf0) | (spindown & 0x0f);
3110	return cdrom_mode_select(cdi, &cgc);
3111}
3112
3113static int idecd_get_spindown(struct cdrom_device_info *cdi, unsigned long arg)
3114{
3115	struct packet_command cgc;
3116	char buffer[16];
3117	int stat;
3118 	char spindown;
3119
3120	init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_UNKNOWN);
3121
3122	stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CDROM_PAGE, 0);
3123	if (stat)
3124		return stat;
3125
3126	spindown = buffer[11] & 0x0f;
3127	if (copy_to_user((void __user *)arg, &spindown, sizeof (char)))
3128		return -EFAULT;
3129	return 0;
3130}
3131
3132static int idecd_ioctl (struct inode *inode, struct file *file,
3133			unsigned int cmd, unsigned long arg)
3134{
3135	struct block_device *bdev = inode->i_bdev;
3136	struct cdrom_info *info = ide_cd_g(bdev->bd_disk);
3137	int err;
3138
3139	switch (cmd) {
3140 	case CDROMSETSPINDOWN:
3141		return idecd_set_spindown(&info->devinfo, arg);
3142 	case CDROMGETSPINDOWN:
3143		return idecd_get_spindown(&info->devinfo, arg);
3144	default:
3145		break;
3146 	}
3147
3148	err = generic_ide_ioctl(info->drive, file, bdev, cmd, arg);
3149	if (err == -EINVAL)
3150		err = cdrom_ioctl(file, &info->devinfo, inode, cmd, arg);
3151
3152	return err;
3153}
3154
3155static int idecd_media_changed(struct gendisk *disk)
3156{
3157	struct cdrom_info *info = ide_cd_g(disk);
3158	return cdrom_media_changed(&info->devinfo);
3159}
3160
3161static int idecd_revalidate_disk(struct gendisk *disk)
3162{
3163	struct cdrom_info *info = ide_cd_g(disk);
3164	struct request_sense sense;
3165	cdrom_read_toc(info->drive, &sense);
3166	return  0;
3167}
3168
3169static struct block_device_operations idecd_ops = {
3170	.owner		= THIS_MODULE,
3171	.open		= idecd_open,
3172	.release	= idecd_release,
3173	.ioctl		= idecd_ioctl,
3174	.media_changed	= idecd_media_changed,
3175	.revalidate_disk= idecd_revalidate_disk
3176};
3177
3178/* options */
3179static char *ignore = NULL;
3180
3181module_param(ignore, charp, 0400);
3182MODULE_DESCRIPTION("ATAPI CD-ROM Driver");
3183
3184static int ide_cd_probe(ide_drive_t *drive)
3185{
3186	struct cdrom_info *info;
3187	struct gendisk *g;
3188	struct request_sense sense;
3189
3190	if (!strstr("ide-cdrom", drive->driver_req))
3191		goto failed;
3192	if (!drive->present)
3193		goto failed;
3194	if (drive->media != ide_cdrom && drive->media != ide_optical)
3195		goto failed;
3196	/* skip drives that we were told to ignore */
3197	if (ignore != NULL) {
3198		if (strstr(ignore, drive->name)) {
3199			printk(KERN_INFO "ide-cd: ignoring drive %s\n", drive->name);
3200			goto failed;
3201		}
3202	}
3203	if (drive->scsi) {
3204		printk(KERN_INFO "ide-cd: passing drive %s to ide-scsi emulation.\n", drive->name);
3205		goto failed;
3206	}
3207	info = kzalloc(sizeof(struct cdrom_info), GFP_KERNEL);
3208	if (info == NULL) {
3209		printk(KERN_ERR "%s: Can't allocate a cdrom structure\n", drive->name);
3210		goto failed;
3211	}
3212
3213	g = alloc_disk(1 << PARTN_BITS);
3214	if (!g)
3215		goto out_free_cd;
3216
3217	ide_init_disk(g, drive);
3218
3219	ide_proc_register_driver(drive, &ide_cdrom_driver);
3220
3221	kref_init(&info->kref);
3222
3223	info->drive = drive;
3224	info->driver = &ide_cdrom_driver;
3225	info->disk = g;
3226
3227	g->private_data = &info->driver;
3228
3229	drive->driver_data = info;
3230
3231	g->minors = 1;
3232	g->driverfs_dev = &drive->gendev;
3233	g->flags = GENHD_FL_CD | GENHD_FL_REMOVABLE;
3234	if (ide_cdrom_setup(drive)) {
3235		struct cdrom_device_info *devinfo = &info->devinfo;
3236		ide_proc_unregister_driver(drive, &ide_cdrom_driver);
3237		kfree(info->buffer);
3238		kfree(info->toc);
3239		kfree(info->changer_info);
3240		if (devinfo->handle == drive && unregister_cdrom(devinfo))
3241			printk (KERN_ERR "%s: ide_cdrom_cleanup failed to unregister device from the cdrom driver.\n", drive->name);
3242		kfree(info);
3243		drive->driver_data = NULL;
3244		goto failed;
3245	}
3246
3247	cdrom_read_toc(drive, &sense);
3248	g->fops = &idecd_ops;
3249	g->flags |= GENHD_FL_REMOVABLE;
3250	add_disk(g);
3251	return 0;
3252
3253out_free_cd:
3254	kfree(info);
3255failed:
3256	return -ENODEV;
3257}
3258
3259static void __exit ide_cdrom_exit(void)
3260{
3261	driver_unregister(&ide_cdrom_driver.gen_driver);
3262}
3263
3264static int __init ide_cdrom_init(void)
3265{
3266	return driver_register(&ide_cdrom_driver.gen_driver);
3267}
3268
3269MODULE_ALIAS("ide:*m-cdrom*");
3270module_init(ide_cdrom_init);
3271module_exit(ide_cdrom_exit);
3272MODULE_LICENSE("GPL");
3273