]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/ide/ide-cd.c
19ccadead5e83bce5dbdb815b0e4532d57372cd6
[linux-2.6-omap-h63xx.git] / drivers / ide / ide-cd.c
1 /*
2  * ATAPI CD-ROM driver.
3  *
4  * Copyright (C) 1994-1996   Scott Snyder <snyder@fnald0.fnal.gov>
5  * Copyright (C) 1996-1998   Erik Andersen <andersee@debian.org>
6  * Copyright (C) 1998-2000   Jens Axboe <axboe@suse.de>
7  * Copyright (C) 2005, 2007  Bartlomiej Zolnierkiewicz
8  *
9  * May be copied or modified under the terms of the GNU General Public
10  * License.  See linux/COPYING for more information.
11  *
12  * See Documentation/cdrom/ide-cd for usage information.
13  *
14  * Suggestions are welcome. Patches that work are more welcome though. ;-)
15  *
16  * Documentation:
17  *      Mt. Fuji (SFF8090 version 4) and ATAPI (SFF-8020i rev 2.6) standards.
18  *
19  * For historical changelog please see:
20  *      Documentation/ide/ChangeLog.ide-cd.1994-2004
21  */
22
23 #define DRV_NAME "ide-cd"
24 #define PFX DRV_NAME ": "
25
26 #define IDECD_VERSION "5.00"
27
28 #include <linux/module.h>
29 #include <linux/types.h>
30 #include <linux/kernel.h>
31 #include <linux/delay.h>
32 #include <linux/timer.h>
33 #include <linux/slab.h>
34 #include <linux/interrupt.h>
35 #include <linux/errno.h>
36 #include <linux/cdrom.h>
37 #include <linux/ide.h>
38 #include <linux/completion.h>
39 #include <linux/mutex.h>
40 #include <linux/bcd.h>
41
42 /* For SCSI -> ATAPI command conversion */
43 #include <scsi/scsi.h>
44
45 #include <linux/irq.h>
46 #include <linux/io.h>
47 #include <asm/byteorder.h>
48 #include <linux/uaccess.h>
49 #include <asm/unaligned.h>
50
51 #include "ide-cd.h"
52
53 static DEFINE_MUTEX(idecd_ref_mutex);
54
55 static void ide_cd_release(struct device *);
56
57 static struct cdrom_info *ide_cd_get(struct gendisk *disk)
58 {
59         struct cdrom_info *cd = NULL;
60
61         mutex_lock(&idecd_ref_mutex);
62         cd = ide_drv_g(disk, cdrom_info);
63         if (cd) {
64                 if (ide_device_get(cd->drive))
65                         cd = NULL;
66                 else
67                         get_device(&cd->dev);
68
69         }
70         mutex_unlock(&idecd_ref_mutex);
71         return cd;
72 }
73
74 static void ide_cd_put(struct cdrom_info *cd)
75 {
76         ide_drive_t *drive = cd->drive;
77
78         mutex_lock(&idecd_ref_mutex);
79         put_device(&cd->dev);
80         ide_device_put(drive);
81         mutex_unlock(&idecd_ref_mutex);
82 }
83
84 /*
85  * Generic packet command support and error handling routines.
86  */
87
88 /* Mark that we've seen a media change and invalidate our internal buffers. */
89 static void cdrom_saw_media_change(ide_drive_t *drive)
90 {
91         drive->dev_flags |= IDE_DFLAG_MEDIA_CHANGED;
92         drive->atapi_flags &= ~IDE_AFLAG_TOC_VALID;
93 }
94
95 static int cdrom_log_sense(ide_drive_t *drive, struct request *rq,
96                            struct request_sense *sense)
97 {
98         int log = 0;
99
100         ide_debug_log(IDE_DBG_SENSE, "sense_key: 0x%x", sense->sense_key);
101
102         if (!sense || !rq || (rq->cmd_flags & REQ_QUIET))
103                 return 0;
104
105         switch (sense->sense_key) {
106         case NO_SENSE:
107         case RECOVERED_ERROR:
108                 break;
109         case NOT_READY:
110                 /*
111                  * don't care about tray state messages for e.g. capacity
112                  * commands or in-progress or becoming ready
113                  */
114                 if (sense->asc == 0x3a || sense->asc == 0x04)
115                         break;
116                 log = 1;
117                 break;
118         case ILLEGAL_REQUEST:
119                 /*
120                  * don't log START_STOP unit with LoEj set, since we cannot
121                  * reliably check if drive can auto-close
122                  */
123                 if (rq->cmd[0] == GPCMD_START_STOP_UNIT && sense->asc == 0x24)
124                         break;
125                 log = 1;
126                 break;
127         case UNIT_ATTENTION:
128                 /*
129                  * Make good and sure we've seen this potential media change.
130                  * Some drives (i.e. Creative) fail to present the correct sense
131                  * key in the error register.
132                  */
133                 cdrom_saw_media_change(drive);
134                 break;
135         default:
136                 log = 1;
137                 break;
138         }
139         return log;
140 }
141
142 static void cdrom_analyze_sense_data(ide_drive_t *drive,
143                               struct request *failed_command,
144                               struct request_sense *sense)
145 {
146         unsigned long sector;
147         unsigned long bio_sectors;
148         struct cdrom_info *info = drive->driver_data;
149
150         ide_debug_log(IDE_DBG_SENSE, "error_code: 0x%x, sense_key: 0x%x",
151                                      sense->error_code, sense->sense_key);
152
153         if (failed_command)
154                 ide_debug_log(IDE_DBG_SENSE, "failed cmd: 0x%x",
155                                              failed_command->cmd[0]);
156
157         if (!cdrom_log_sense(drive, failed_command, sense))
158                 return;
159
160         /*
161          * If a read toc is executed for a CD-R or CD-RW medium where the first
162          * toc has not been recorded yet, it will fail with 05/24/00 (which is a
163          * confusing error)
164          */
165         if (failed_command && failed_command->cmd[0] == GPCMD_READ_TOC_PMA_ATIP)
166                 if (sense->sense_key == 0x05 && sense->asc == 0x24)
167                         return;
168
169         /* current error */
170         if (sense->error_code == 0x70) {
171                 switch (sense->sense_key) {
172                 case MEDIUM_ERROR:
173                 case VOLUME_OVERFLOW:
174                 case ILLEGAL_REQUEST:
175                         if (!sense->valid)
176                                 break;
177                         if (failed_command == NULL ||
178                                         !blk_fs_request(failed_command))
179                                 break;
180                         sector = (sense->information[0] << 24) |
181                                  (sense->information[1] << 16) |
182                                  (sense->information[2] <<  8) |
183                                  (sense->information[3]);
184
185                         if (drive->queue->hardsect_size == 2048)
186                                 /* device sector size is 2K */
187                                 sector <<= 2;
188
189                         bio_sectors = max(bio_sectors(failed_command->bio), 4U);
190                         sector &= ~(bio_sectors - 1);
191
192                         /*
193                          * The SCSI specification allows for the value
194                          * returned by READ CAPACITY to be up to 75 2K
195                          * sectors past the last readable block.
196                          * Therefore, if we hit a medium error within the
197                          * last 75 2K sectors, we decrease the saved size
198                          * value.
199                          */
200                         if (sector < get_capacity(info->disk) &&
201                             drive->probed_capacity - sector < 4 * 75)
202                                 set_capacity(info->disk, sector);
203                 }
204         }
205
206         ide_cd_log_error(drive->name, failed_command, sense);
207 }
208
209 static void cdrom_queue_request_sense(ide_drive_t *drive, void *sense,
210                                       struct request *failed_command)
211 {
212         struct cdrom_info *info         = drive->driver_data;
213         struct request *rq              = &drive->request_sense_rq;
214
215         ide_debug_log(IDE_DBG_SENSE, "enter");
216
217         if (sense == NULL)
218                 sense = &info->sense_data;
219
220         /* stuff the sense request in front of our current request */
221         blk_rq_init(NULL, rq);
222         rq->cmd_type = REQ_TYPE_ATA_PC;
223         rq->rq_disk = info->disk;
224
225         rq->data = sense;
226         rq->cmd[0] = GPCMD_REQUEST_SENSE;
227         rq->cmd[4] = 18;
228         rq->data_len = 18;
229
230         rq->cmd_type = REQ_TYPE_SENSE;
231         rq->cmd_flags |= REQ_PREEMPT;
232
233         /* NOTE! Save the failed command in "rq->buffer" */
234         rq->buffer = (void *) failed_command;
235
236         if (failed_command)
237                 ide_debug_log(IDE_DBG_SENSE, "failed_cmd: 0x%x",
238                                              failed_command->cmd[0]);
239
240         drive->hwif->rq = NULL;
241
242         elv_add_request(drive->queue, rq, ELEVATOR_INSERT_FRONT, 0);
243 }
244
245 static void cdrom_end_request(ide_drive_t *drive, int uptodate)
246 {
247         struct request *rq = drive->hwif->rq;
248         int nsectors = rq->hard_cur_sectors;
249
250         ide_debug_log(IDE_DBG_FUNC, "cmd: 0x%x, uptodate: 0x%x, nsectors: %d",
251                                     rq->cmd[0], uptodate, nsectors);
252
253         if (blk_sense_request(rq) && uptodate) {
254                 /*
255                  * For REQ_TYPE_SENSE, "rq->buffer" points to the original
256                  * failed request
257                  */
258                 struct request *failed = (struct request *) rq->buffer;
259                 struct cdrom_info *info = drive->driver_data;
260                 void *sense = &info->sense_data;
261
262                 if (failed) {
263                         if (failed->sense) {
264                                 sense = failed->sense;
265                                 failed->sense_len = rq->sense_len;
266                         }
267                         cdrom_analyze_sense_data(drive, failed, sense);
268
269                         if (ide_end_rq(drive, failed, -EIO,
270                                        blk_rq_bytes(failed)))
271                                 BUG();
272                 } else
273                         cdrom_analyze_sense_data(drive, NULL, sense);
274         }
275
276         if (!rq->current_nr_sectors && blk_fs_request(rq))
277                 uptodate = 1;
278         /* make sure it's fully ended */
279         if (blk_pc_request(rq))
280                 nsectors = (rq->data_len + 511) >> 9;
281         if (!nsectors)
282                 nsectors = 1;
283
284         ide_debug_log(IDE_DBG_FUNC, "uptodate: 0x%x, nsectors: %d",
285                                     uptodate, nsectors);
286
287         if (blk_fs_request(rq) == 0 && uptodate <= 0 && rq->errors == 0)
288                 rq->errors = -EIO;
289
290         ide_complete_rq(drive, uptodate ? 0 : -EIO, nsectors << 9);
291 }
292
293 /*
294  * Returns:
295  * 0: if the request should be continued.
296  * 1: if the request was ended.
297  */
298 static int cdrom_decode_status(ide_drive_t *drive, int good_stat, int *stat_ret)
299 {
300         ide_hwif_t *hwif = drive->hwif;
301         struct request *rq = hwif->rq;
302         int stat, err, sense_key;
303
304         /* check for errors */
305         stat = hwif->tp_ops->read_status(hwif);
306
307         if (stat_ret)
308                 *stat_ret = stat;
309
310         if (OK_STAT(stat, good_stat, BAD_R_STAT))
311                 return 0;
312
313         /* get the IDE error register */
314         err = ide_read_error(drive);
315         sense_key = err >> 4;
316
317         ide_debug_log(IDE_DBG_RQ, "stat: 0x%x, good_stat: 0x%x, cmd[0]: 0x%x, "
318                                   "rq->cmd_type: 0x%x, err: 0x%x",
319                                   stat, good_stat, rq->cmd[0], rq->cmd_type,
320                                   err);
321
322         if (blk_sense_request(rq)) {
323                 /*
324                  * We got an error trying to get sense info from the drive
325                  * (probably while trying to recover from a former error).
326                  * Just give up.
327                  */
328                 rq->cmd_flags |= REQ_FAILED;
329                 cdrom_end_request(drive, 0);
330                 ide_error(drive, "request sense failure", stat);
331                 return 1;
332
333         } else if (blk_pc_request(rq) || rq->cmd_type == REQ_TYPE_ATA_PC) {
334                 /* All other functions, except for READ. */
335
336                 /*
337                  * if we have an error, pass back CHECK_CONDITION as the
338                  * scsi status byte
339                  */
340                 if (blk_pc_request(rq) && !rq->errors)
341                         rq->errors = SAM_STAT_CHECK_CONDITION;
342
343                 /* check for tray open */
344                 if (sense_key == NOT_READY) {
345                         cdrom_saw_media_change(drive);
346                 } else if (sense_key == UNIT_ATTENTION) {
347                         /* check for media change */
348                         cdrom_saw_media_change(drive);
349                         return 0;
350                 } else if (sense_key == ILLEGAL_REQUEST &&
351                            rq->cmd[0] == GPCMD_START_STOP_UNIT) {
352                         /*
353                          * Don't print error message for this condition--
354                          * SFF8090i indicates that 5/24/00 is the correct
355                          * response to a request to close the tray if the
356                          * drive doesn't have that capability.
357                          * cdrom_log_sense() knows this!
358                          */
359                 } else if (!(rq->cmd_flags & REQ_QUIET)) {
360                         /* otherwise, print an error */
361                         ide_dump_status(drive, "packet command error", stat);
362                 }
363
364                 rq->cmd_flags |= REQ_FAILED;
365
366                 /*
367                  * instead of playing games with moving completions around,
368                  * remove failed request completely and end it when the
369                  * request sense has completed
370                  */
371                 goto end_request;
372
373         } else if (blk_fs_request(rq)) {
374                 int do_end_request = 0;
375
376                 /* handle errors from READ and WRITE requests */
377
378                 if (blk_noretry_request(rq))
379                         do_end_request = 1;
380
381                 if (sense_key == NOT_READY) {
382                         /* tray open */
383                         if (rq_data_dir(rq) == READ) {
384                                 cdrom_saw_media_change(drive);
385
386                                 /* fail the request */
387                                 printk(KERN_ERR PFX "%s: tray open\n",
388                                                 drive->name);
389                                 do_end_request = 1;
390                         } else {
391                                 struct cdrom_info *info = drive->driver_data;
392
393                                 /*
394                                  * Allow the drive 5 seconds to recover, some
395                                  * devices will return this error while flushing
396                                  * data from cache.
397                                  */
398                                 if (!rq->errors)
399                                         info->write_timeout = jiffies +
400                                                         ATAPI_WAIT_WRITE_BUSY;
401                                 rq->errors = 1;
402                                 if (time_after(jiffies, info->write_timeout))
403                                         do_end_request = 1;
404                                 else {
405                                         struct request_queue *q = drive->queue;
406                                         unsigned long flags;
407
408                                         /*
409                                          * take a breather relying on the unplug
410                                          * timer to kick us again
411                                          */
412                                         spin_lock_irqsave(q->queue_lock, flags);
413                                         blk_plug_device(q);
414                                         spin_unlock_irqrestore(q->queue_lock, flags);
415
416                                         return 1;
417                                 }
418                         }
419                 } else if (sense_key == UNIT_ATTENTION) {
420                         /* media change */
421                         cdrom_saw_media_change(drive);
422
423                         /*
424                          * Arrange to retry the request but be sure to give up
425                          * if we've retried too many times.
426                          */
427                         if (++rq->errors > ERROR_MAX)
428                                 do_end_request = 1;
429                 } else if (sense_key == ILLEGAL_REQUEST ||
430                            sense_key == DATA_PROTECT) {
431                         /*
432                          * No point in retrying after an illegal request or data
433                          * protect error.
434                          */
435                         ide_dump_status(drive, "command error", stat);
436                         do_end_request = 1;
437                 } else if (sense_key == MEDIUM_ERROR) {
438                         /*
439                          * No point in re-trying a zillion times on a bad
440                          * sector. If we got here the error is not correctable.
441                          */
442                         ide_dump_status(drive, "media error (bad sector)",
443                                         stat);
444                         do_end_request = 1;
445                 } else if (sense_key == BLANK_CHECK) {
446                         /* disk appears blank ?? */
447                         ide_dump_status(drive, "media error (blank)", stat);
448                         do_end_request = 1;
449                 } else if ((err & ~ATA_ABORTED) != 0) {
450                         /* go to the default handler for other errors */
451                         ide_error(drive, "cdrom_decode_status", stat);
452                         return 1;
453                 } else if ((++rq->errors > ERROR_MAX)) {
454                         /* we've racked up too many retries, abort */
455                         do_end_request = 1;
456                 }
457
458                 /*
459                  * End a request through request sense analysis when we have
460                  * sense data. We need this in order to perform end of media
461                  * processing.
462                  */
463                 if (do_end_request)
464                         goto end_request;
465
466                 /*
467                  * If we got a CHECK_CONDITION status, queue
468                  * a request sense command.
469                  */
470                 if (stat & ATA_ERR)
471                         cdrom_queue_request_sense(drive, NULL, NULL);
472         } else {
473                 blk_dump_rq_flags(rq, PFX "bad rq");
474                 cdrom_end_request(drive, 0);
475         }
476
477         /* retry, or handle the next request */
478         return 1;
479
480 end_request:
481         if (stat & ATA_ERR) {
482                 struct request_queue *q = drive->queue;
483                 unsigned long flags;
484
485                 spin_lock_irqsave(q->queue_lock, flags);
486                 blkdev_dequeue_request(rq);
487                 spin_unlock_irqrestore(q->queue_lock, flags);
488
489                 hwif->rq = NULL;
490
491                 cdrom_queue_request_sense(drive, rq->sense, rq);
492         } else
493                 cdrom_end_request(drive, 0);
494
495         return 1;
496 }
497
498 /*
499  * Check the contents of the interrupt reason register from the cdrom
500  * and attempt to recover if there are problems.  Returns  0 if everything's
501  * ok; nonzero if the request has been terminated.
502  */
503 static int ide_cd_check_ireason(ide_drive_t *drive, struct request *rq,
504                                 int len, int ireason, int rw)
505 {
506         ide_hwif_t *hwif = drive->hwif;
507
508         ide_debug_log(IDE_DBG_FUNC, "ireason: 0x%x, rw: 0x%x", ireason, rw);
509
510         /*
511          * ireason == 0: the drive wants to receive data from us
512          * ireason == 2: the drive is expecting to transfer data to us
513          */
514         if (ireason == (!rw << 1))
515                 return 0;
516         else if (ireason == (rw << 1)) {
517
518                 /* whoops... */
519                 printk(KERN_ERR PFX "%s: %s: wrong transfer direction!\n",
520                                 drive->name, __func__);
521
522                 ide_pad_transfer(drive, rw, len);
523         } else  if (rw == 0 && ireason == 1) {
524                 /*
525                  * Some drives (ASUS) seem to tell us that status info is
526                  * available.  Just get it and ignore.
527                  */
528                 (void)hwif->tp_ops->read_status(hwif);
529                 return 0;
530         } else {
531                 /* drive wants a command packet, or invalid ireason... */
532                 printk(KERN_ERR PFX "%s: %s: bad interrupt reason 0x%02x\n",
533                                 drive->name, __func__, ireason);
534         }
535
536         if (rq->cmd_type == REQ_TYPE_ATA_PC)
537                 rq->cmd_flags |= REQ_FAILED;
538
539         cdrom_end_request(drive, 0);
540         return -1;
541 }
542
543 /*
544  * Assume that the drive will always provide data in multiples of at least
545  * SECTOR_SIZE, as it gets hairy to keep track of the transfers otherwise.
546  */
547 static int ide_cd_check_transfer_size(ide_drive_t *drive, int len)
548 {
549         ide_debug_log(IDE_DBG_FUNC, "len: %d", len);
550
551         if ((len % SECTOR_SIZE) == 0)
552                 return 0;
553
554         printk(KERN_ERR PFX "%s: %s: Bad transfer size %d\n", drive->name,
555                         __func__, len);
556
557         if (drive->atapi_flags & IDE_AFLAG_LIMIT_NFRAMES)
558                 printk(KERN_ERR PFX "This drive is not supported by this "
559                                 "version of the driver\n");
560         else {
561                 printk(KERN_ERR PFX "Trying to limit transfer sizes\n");
562                 drive->atapi_flags |= IDE_AFLAG_LIMIT_NFRAMES;
563         }
564
565         return 1;
566 }
567
568 static ide_startstop_t ide_cd_prepare_rw_request(ide_drive_t *drive,
569                                                  struct request *rq)
570 {
571         ide_debug_log(IDE_DBG_RQ, "rq->cmd_flags: 0x%x", rq->cmd_flags);
572
573         if (rq_data_dir(rq) == READ) {
574                 unsigned short sectors_per_frame =
575                         queue_hardsect_size(drive->queue) >> SECTOR_BITS;
576                 int nskip = rq->sector & (sectors_per_frame - 1);
577
578                 /*
579                  * If the requested sector doesn't start on a frame boundary,
580                  * we must adjust the start of the transfer so that it does,
581                  * and remember to skip the first few sectors.
582                  *
583                  * If the rq->current_nr_sectors field is larger than the size
584                  * of the buffer, it will mean that we're to skip a number of
585                  * sectors equal to the amount by which rq->current_nr_sectors
586                  * is larger than the buffer size.
587                  */
588                 if (nskip > 0) {
589                         /* sanity check... */
590                         if (rq->current_nr_sectors !=
591                             bio_cur_sectors(rq->bio)) {
592                                 printk(KERN_ERR PFX "%s: %s: buffer botch (%u)\n",
593                                                 drive->name, __func__,
594                                                 rq->current_nr_sectors);
595                                 cdrom_end_request(drive, 0);
596                                 return ide_stopped;
597                         }
598                         rq->current_nr_sectors += nskip;
599                 }
600         }
601
602         /* set up the command */
603         rq->timeout = ATAPI_WAIT_PC;
604
605         return ide_started;
606 }
607
608 /*
609  * Fix up a possibly partially-processed request so that we can start it over
610  * entirely, or even put it back on the request queue.
611  */
612 static void ide_cd_restore_request(ide_drive_t *drive, struct request *rq)
613 {
614
615         ide_debug_log(IDE_DBG_FUNC, "enter");
616
617         if (rq->buffer != bio_data(rq->bio)) {
618                 sector_t n =
619                         (rq->buffer - (char *)bio_data(rq->bio)) / SECTOR_SIZE;
620
621                 rq->buffer = bio_data(rq->bio);
622                 rq->nr_sectors += n;
623                 rq->sector -= n;
624         }
625         rq->current_nr_sectors = bio_cur_sectors(rq->bio);
626         rq->hard_cur_sectors = rq->current_nr_sectors;
627         rq->hard_nr_sectors = rq->nr_sectors;
628         rq->hard_sector = rq->sector;
629         rq->q->prep_rq_fn(rq->q, rq);
630 }
631
632 static void ide_cd_request_sense_fixup(ide_drive_t *drive, struct request *rq)
633 {
634         ide_debug_log(IDE_DBG_FUNC, "rq->cmd[0]: 0x%x", rq->cmd[0]);
635
636         /*
637          * Some of the trailing request sense fields are optional,
638          * and some drives don't send them.  Sigh.
639          */
640         if (rq->cmd[0] == GPCMD_REQUEST_SENSE &&
641             rq->data_len > 0 && rq->data_len <= 5)
642                 while (rq->data_len > 0) {
643                         *(u8 *)rq->data++ = 0;
644                         --rq->data_len;
645                 }
646 }
647
648 int ide_cd_queue_pc(ide_drive_t *drive, const unsigned char *cmd,
649                     int write, void *buffer, unsigned *bufflen,
650                     struct request_sense *sense, int timeout,
651                     unsigned int cmd_flags)
652 {
653         struct cdrom_info *info = drive->driver_data;
654         struct request_sense local_sense;
655         int retries = 10;
656         unsigned int flags = 0;
657
658         if (!sense)
659                 sense = &local_sense;
660
661         ide_debug_log(IDE_DBG_PC, "cmd[0]: 0x%x, write: 0x%x, timeout: %d, "
662                                   "cmd_flags: 0x%x",
663                                   cmd[0], write, timeout, cmd_flags);
664
665         /* start of retry loop */
666         do {
667                 struct request *rq;
668                 int error;
669
670                 rq = blk_get_request(drive->queue, write, __GFP_WAIT);
671
672                 memcpy(rq->cmd, cmd, BLK_MAX_CDB);
673                 rq->cmd_type = REQ_TYPE_ATA_PC;
674                 rq->sense = sense;
675                 rq->cmd_flags |= cmd_flags;
676                 rq->timeout = timeout;
677                 if (buffer) {
678                         rq->data = buffer;
679                         rq->data_len = *bufflen;
680                 }
681
682                 error = blk_execute_rq(drive->queue, info->disk, rq, 0);
683
684                 if (buffer)
685                         *bufflen = rq->data_len;
686
687                 flags = rq->cmd_flags;
688                 blk_put_request(rq);
689
690                 /*
691                  * FIXME: we should probably abort/retry or something in case of
692                  * failure.
693                  */
694                 if (flags & REQ_FAILED) {
695                         /*
696                          * The request failed.  Retry if it was due to a unit
697                          * attention status (usually means media was changed).
698                          */
699                         struct request_sense *reqbuf = sense;
700
701                         if (reqbuf->sense_key == UNIT_ATTENTION)
702                                 cdrom_saw_media_change(drive);
703                         else if (reqbuf->sense_key == NOT_READY &&
704                                  reqbuf->asc == 4 && reqbuf->ascq != 4) {
705                                 /*
706                                  * The drive is in the process of loading
707                                  * a disk.  Retry, but wait a little to give
708                                  * the drive time to complete the load.
709                                  */
710                                 ssleep(2);
711                         } else {
712                                 /* otherwise, don't retry */
713                                 retries = 0;
714                         }
715                         --retries;
716                 }
717
718                 /* end of retry loop */
719         } while ((flags & REQ_FAILED) && retries >= 0);
720
721         /* return an error if the command failed */
722         return (flags & REQ_FAILED) ? -EIO : 0;
723 }
724
725 /*
726  * Called from blk_end_request_callback() after the data of the request is
727  * completed and before the request itself is completed. By returning value '1',
728  * blk_end_request_callback() returns immediately without completing it.
729  */
730 static int cdrom_newpc_intr_dummy_cb(struct request *rq)
731 {
732         return 1;
733 }
734
735 static ide_startstop_t cdrom_newpc_intr(ide_drive_t *drive)
736 {
737         ide_hwif_t *hwif = drive->hwif;
738         struct request *rq = hwif->rq;
739         xfer_func_t *xferfunc;
740         ide_expiry_t *expiry = NULL;
741         int dma_error = 0, dma, stat, thislen, uptodate = 0;
742         int write = (rq_data_dir(rq) == WRITE) ? 1 : 0;
743         unsigned int timeout;
744         u16 len;
745         u8 ireason;
746
747         ide_debug_log(IDE_DBG_PC, "cmd[0]: 0x%x, write: 0x%x",
748                                   rq->cmd[0], write);
749
750         /* check for errors */
751         dma = drive->dma;
752         if (dma) {
753                 drive->dma = 0;
754                 dma_error = hwif->dma_ops->dma_end(drive);
755                 if (dma_error) {
756                         printk(KERN_ERR PFX "%s: DMA %s error\n", drive->name,
757                                         write ? "write" : "read");
758                         ide_dma_off(drive);
759                 }
760         }
761
762         if (cdrom_decode_status(drive, 0, &stat))
763                 return ide_stopped;
764
765         /* using dma, transfer is complete now */
766         if (dma) {
767                 if (dma_error)
768                         return ide_error(drive, "dma error", stat);
769                 if (blk_fs_request(rq)) {
770                         ide_complete_rq(drive, 0, rq->nr_sectors
771                                 ? (rq->nr_sectors << 9) : ide_rq_bytes(rq));
772                         return ide_stopped;
773                 } else if (rq->cmd_type == REQ_TYPE_ATA_PC && !rq->bio) {
774                         ide_complete_rq(drive, 0, 512);
775                         return ide_stopped;
776                 }
777                 goto end_request;
778         }
779
780         ide_read_bcount_and_ireason(drive, &len, &ireason);
781
782         thislen = blk_fs_request(rq) ? len : rq->data_len;
783         if (thislen > len)
784                 thislen = len;
785
786         ide_debug_log(IDE_DBG_PC, "DRQ: stat: 0x%x, thislen: %d",
787                                   stat, thislen);
788
789         /* If DRQ is clear, the command has completed. */
790         if ((stat & ATA_DRQ) == 0) {
791                 if (blk_fs_request(rq)) {
792                         /*
793                          * If we're not done reading/writing, complain.
794                          * Otherwise, complete the command normally.
795                          */
796                         uptodate = 1;
797                         if (rq->current_nr_sectors > 0) {
798                                 printk(KERN_ERR PFX "%s: %s: data underrun "
799                                                 "(%d blocks)\n",
800                                                 drive->name, __func__,
801                                                 rq->current_nr_sectors);
802                                 if (!write)
803                                         rq->cmd_flags |= REQ_FAILED;
804                                 uptodate = 0;
805                         }
806                         cdrom_end_request(drive, uptodate);
807                         return ide_stopped;
808                 } else if (!blk_pc_request(rq)) {
809                         ide_cd_request_sense_fixup(drive, rq);
810                         /* complain if we still have data left to transfer */
811                         uptodate = rq->data_len ? 0 : 1;
812                 }
813                 goto end_request;
814         }
815
816         /* check which way to transfer data */
817         if (ide_cd_check_ireason(drive, rq, len, ireason, write))
818                 return ide_stopped;
819
820         if (blk_fs_request(rq)) {
821                 if (write == 0) {
822                         int nskip;
823
824                         if (ide_cd_check_transfer_size(drive, len)) {
825                                 cdrom_end_request(drive, 0);
826                                 return ide_stopped;
827                         }
828
829                         /*
830                          * First, figure out if we need to bit-bucket
831                          * any of the leading sectors.
832                          */
833                         nskip = min_t(int, rq->current_nr_sectors
834                                            - bio_cur_sectors(rq->bio),
835                                            thislen >> 9);
836                         if (nskip > 0) {
837                                 ide_pad_transfer(drive, write, nskip << 9);
838                                 rq->current_nr_sectors -= nskip;
839                                 thislen -= (nskip << 9);
840                         }
841                 }
842         }
843
844         if (ireason == 0) {
845                 write = 1;
846                 xferfunc = hwif->tp_ops->output_data;
847         } else {
848                 write = 0;
849                 xferfunc = hwif->tp_ops->input_data;
850         }
851
852         ide_debug_log(IDE_DBG_PC, "data transfer, rq->cmd_type: 0x%x, "
853                                   "ireason: 0x%x",
854                                   rq->cmd_type, ireason);
855
856         /* transfer data */
857         while (thislen > 0) {
858                 u8 *ptr = blk_fs_request(rq) ? NULL : rq->data;
859                 int blen = rq->data_len;
860
861                 /* bio backed? */
862                 if (rq->bio) {
863                         if (blk_fs_request(rq)) {
864                                 ptr = rq->buffer;
865                                 blen = rq->current_nr_sectors << 9;
866                         } else {
867                                 ptr = bio_data(rq->bio);
868                                 blen = bio_iovec(rq->bio)->bv_len;
869                         }
870                 }
871
872                 if (!ptr) {
873                         if (blk_fs_request(rq) && !write)
874                                 /*
875                                  * If the buffers are full, pipe the rest into
876                                  * oblivion.
877                                  */
878                                 ide_pad_transfer(drive, 0, thislen);
879                         else {
880                                 printk(KERN_ERR PFX "%s: confused, missing data\n",
881                                                 drive->name);
882                                 blk_dump_rq_flags(rq, rq_data_dir(rq)
883                                                   ? "cdrom_newpc_intr, write"
884                                                   : "cdrom_newpc_intr, read");
885                         }
886                         break;
887                 }
888
889                 if (blen > thislen)
890                         blen = thislen;
891
892                 xferfunc(drive, NULL, ptr, blen);
893
894                 thislen -= blen;
895                 len -= blen;
896
897                 if (blk_fs_request(rq)) {
898                         rq->buffer += blen;
899                         rq->nr_sectors -= (blen >> 9);
900                         rq->current_nr_sectors -= (blen >> 9);
901                         rq->sector += (blen >> 9);
902
903                         if (rq->current_nr_sectors == 0 && rq->nr_sectors)
904                                 cdrom_end_request(drive, 1);
905                 } else {
906                         rq->data_len -= blen;
907
908                         /*
909                          * The request can't be completed until DRQ is cleared.
910                          * So complete the data, but don't complete the request
911                          * using the dummy function for the callback feature
912                          * of blk_end_request_callback().
913                          */
914                         if (rq->bio)
915                                 blk_end_request_callback(rq, 0, blen,
916                                                  cdrom_newpc_intr_dummy_cb);
917                         else
918                                 rq->data += blen;
919                 }
920                 if (!write && blk_sense_request(rq))
921                         rq->sense_len += blen;
922         }
923
924         /* pad, if necessary */
925         if (!blk_fs_request(rq) && len > 0)
926                 ide_pad_transfer(drive, write, len);
927
928         if (blk_pc_request(rq)) {
929                 timeout = rq->timeout;
930         } else {
931                 timeout = ATAPI_WAIT_PC;
932                 if (!blk_fs_request(rq))
933                         expiry = ide_cd_expiry;
934         }
935
936         hwif->expiry = expiry;
937         ide_set_handler(drive, cdrom_newpc_intr, timeout);
938         return ide_started;
939
940 end_request:
941         if (blk_pc_request(rq)) {
942                 unsigned int dlen = rq->data_len;
943
944                 if (dma)
945                         rq->data_len = 0;
946
947                 if (blk_end_request(rq, 0, dlen))
948                         BUG();
949
950                 hwif->rq = NULL;
951         } else {
952                 if (!uptodate)
953                         rq->cmd_flags |= REQ_FAILED;
954                 cdrom_end_request(drive, uptodate);
955         }
956         return ide_stopped;
957 }
958
959 static ide_startstop_t cdrom_start_rw(ide_drive_t *drive, struct request *rq)
960 {
961         struct cdrom_info *cd = drive->driver_data;
962         int write = rq_data_dir(rq) == WRITE;
963         unsigned short sectors_per_frame =
964                 queue_hardsect_size(drive->queue) >> SECTOR_BITS;
965
966         ide_debug_log(IDE_DBG_RQ, "rq->cmd[0]: 0x%x, write: 0x%x, "
967                                   "secs_per_frame: %u",
968                                   rq->cmd[0], write, sectors_per_frame);
969
970         if (write) {
971                 /* disk has become write protected */
972                 if (get_disk_ro(cd->disk)) {
973                         cdrom_end_request(drive, 0);
974                         return ide_stopped;
975                 }
976         } else {
977                 /*
978                  * We may be retrying this request after an error.  Fix up any
979                  * weirdness which might be present in the request packet.
980                  */
981                 ide_cd_restore_request(drive, rq);
982         }
983
984         /* use DMA, if possible / writes *must* be hardware frame aligned */
985         if ((rq->nr_sectors & (sectors_per_frame - 1)) ||
986             (rq->sector & (sectors_per_frame - 1))) {
987                 if (write) {
988                         cdrom_end_request(drive, 0);
989                         return ide_stopped;
990                 }
991                 drive->dma = 0;
992         } else
993                 drive->dma = !!(drive->dev_flags & IDE_DFLAG_USING_DMA);
994
995         if (write)
996                 cd->devinfo.media_written = 1;
997
998         return ide_started;
999 }
1000
1001 static void cdrom_do_block_pc(ide_drive_t *drive, struct request *rq)
1002 {
1003
1004         ide_debug_log(IDE_DBG_PC, "rq->cmd[0]: 0x%x, rq->cmd_type: 0x%x",
1005                                   rq->cmd[0], rq->cmd_type);
1006
1007         if (blk_pc_request(rq))
1008                 rq->cmd_flags |= REQ_QUIET;
1009         else
1010                 rq->cmd_flags &= ~REQ_FAILED;
1011
1012         drive->dma = 0;
1013
1014         /* sg request */
1015         if (rq->bio || ((rq->cmd_type == REQ_TYPE_ATA_PC) && rq->data_len)) {
1016                 struct request_queue *q = drive->queue;
1017                 unsigned int alignment;
1018                 char *buf;
1019
1020                 if (rq->bio)
1021                         buf = bio_data(rq->bio);
1022                 else
1023                         buf = rq->data;
1024
1025                 drive->dma = !!(drive->dev_flags & IDE_DFLAG_USING_DMA);
1026
1027                 /*
1028                  * check if dma is safe
1029                  *
1030                  * NOTE! The "len" and "addr" checks should possibly have
1031                  * separate masks.
1032                  */
1033                 alignment = queue_dma_alignment(q) | q->dma_pad_mask;
1034                 if ((unsigned long)buf & alignment
1035                     || rq->data_len & q->dma_pad_mask
1036                     || object_is_on_stack(buf))
1037                         drive->dma = 0;
1038         }
1039 }
1040
1041 static ide_startstop_t ide_cd_do_request(ide_drive_t *drive, struct request *rq,
1042                                         sector_t block)
1043 {
1044         struct ide_cmd cmd;
1045
1046         ide_debug_log(IDE_DBG_RQ, "cmd: 0x%x, block: %llu",
1047                                   rq->cmd[0], (unsigned long long)block);
1048
1049         if (drive->debug_mask & IDE_DBG_RQ)
1050                 blk_dump_rq_flags(rq, "ide_cd_do_request");
1051
1052         if (blk_fs_request(rq)) {
1053                 if (cdrom_start_rw(drive, rq) == ide_stopped)
1054                         return ide_stopped;
1055
1056                 if (ide_cd_prepare_rw_request(drive, rq) == ide_stopped)
1057                         return ide_stopped;
1058         } else if (blk_sense_request(rq) || blk_pc_request(rq) ||
1059                    rq->cmd_type == REQ_TYPE_ATA_PC) {
1060                 if (!rq->timeout)
1061                         rq->timeout = ATAPI_WAIT_PC;
1062
1063                 cdrom_do_block_pc(drive, rq);
1064         } else if (blk_special_request(rq)) {
1065                 /* right now this can only be a reset... */
1066                 cdrom_end_request(drive, 1);
1067                 return ide_stopped;
1068         } else {
1069                 blk_dump_rq_flags(rq, DRV_NAME " bad flags");
1070                 cdrom_end_request(drive, 0);
1071                 return ide_stopped;
1072         }
1073
1074         memset(&cmd, 0, sizeof(cmd));
1075
1076         if (rq_data_dir(rq))
1077                 cmd.tf_flags |= IDE_TFLAG_WRITE;
1078
1079         cmd.rq = rq;
1080
1081         return ide_issue_pc(drive, &cmd);
1082 }
1083
1084 /*
1085  * Ioctl handling.
1086  *
1087  * Routines which queue packet commands take as a final argument a pointer to a
1088  * request_sense struct. If execution of the command results in an error with a
1089  * CHECK CONDITION status, this structure will be filled with the results of the
1090  * subsequent request sense command. The pointer can also be NULL, in which case
1091  * no sense information is returned.
1092  */
1093 static void msf_from_bcd(struct atapi_msf *msf)
1094 {
1095         msf->minute = bcd2bin(msf->minute);
1096         msf->second = bcd2bin(msf->second);
1097         msf->frame  = bcd2bin(msf->frame);
1098 }
1099
1100 int cdrom_check_status(ide_drive_t *drive, struct request_sense *sense)
1101 {
1102         struct cdrom_info *info = drive->driver_data;
1103         struct cdrom_device_info *cdi = &info->devinfo;
1104         unsigned char cmd[BLK_MAX_CDB];
1105
1106         ide_debug_log(IDE_DBG_FUNC, "enter");
1107
1108         memset(cmd, 0, BLK_MAX_CDB);
1109         cmd[0] = GPCMD_TEST_UNIT_READY;
1110
1111         /*
1112          * Sanyo 3 CD changer uses byte 7 of TEST_UNIT_READY to switch CDs
1113          * instead of supporting the LOAD_UNLOAD opcode.
1114          */
1115         cmd[7] = cdi->sanyo_slot % 3;
1116
1117         return ide_cd_queue_pc(drive, cmd, 0, NULL, NULL, sense, 0, REQ_QUIET);
1118 }
1119
1120 static int cdrom_read_capacity(ide_drive_t *drive, unsigned long *capacity,
1121                                unsigned long *sectors_per_frame,
1122                                struct request_sense *sense)
1123 {
1124         struct {
1125                 __be32 lba;
1126                 __be32 blocklen;
1127         } capbuf;
1128
1129         int stat;
1130         unsigned char cmd[BLK_MAX_CDB];
1131         unsigned len = sizeof(capbuf);
1132         u32 blocklen;
1133
1134         ide_debug_log(IDE_DBG_FUNC, "enter");
1135
1136         memset(cmd, 0, BLK_MAX_CDB);
1137         cmd[0] = GPCMD_READ_CDVD_CAPACITY;
1138
1139         stat = ide_cd_queue_pc(drive, cmd, 0, &capbuf, &len, sense, 0,
1140                                REQ_QUIET);
1141         if (stat)
1142                 return stat;
1143
1144         /*
1145          * Sanity check the given block size
1146          */
1147         blocklen = be32_to_cpu(capbuf.blocklen);
1148         switch (blocklen) {
1149         case 512:
1150         case 1024:
1151         case 2048:
1152         case 4096:
1153                 break;
1154         default:
1155                 printk(KERN_ERR PFX "%s: weird block size %u\n",
1156                                 drive->name, blocklen);
1157                 printk(KERN_ERR PFX "%s: default to 2kb block size\n",
1158                                 drive->name);
1159                 blocklen = 2048;
1160                 break;
1161         }
1162
1163         *capacity = 1 + be32_to_cpu(capbuf.lba);
1164         *sectors_per_frame = blocklen >> SECTOR_BITS;
1165
1166         ide_debug_log(IDE_DBG_PROBE, "cap: %lu, sectors_per_frame: %lu",
1167                                      *capacity, *sectors_per_frame);
1168
1169         return 0;
1170 }
1171
1172 static int cdrom_read_tocentry(ide_drive_t *drive, int trackno, int msf_flag,
1173                                 int format, char *buf, int buflen,
1174                                 struct request_sense *sense)
1175 {
1176         unsigned char cmd[BLK_MAX_CDB];
1177
1178         ide_debug_log(IDE_DBG_FUNC, "enter");
1179
1180         memset(cmd, 0, BLK_MAX_CDB);
1181
1182         cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
1183         cmd[6] = trackno;
1184         cmd[7] = (buflen >> 8);
1185         cmd[8] = (buflen & 0xff);
1186         cmd[9] = (format << 6);
1187
1188         if (msf_flag)
1189                 cmd[1] = 2;
1190
1191         return ide_cd_queue_pc(drive, cmd, 0, buf, &buflen, sense, 0, REQ_QUIET);
1192 }
1193
1194 /* Try to read the entire TOC for the disk into our internal buffer. */
1195 int ide_cd_read_toc(ide_drive_t *drive, struct request_sense *sense)
1196 {
1197         int stat, ntracks, i;
1198         struct cdrom_info *info = drive->driver_data;
1199         struct cdrom_device_info *cdi = &info->devinfo;
1200         struct atapi_toc *toc = info->toc;
1201         struct {
1202                 struct atapi_toc_header hdr;
1203                 struct atapi_toc_entry  ent;
1204         } ms_tmp;
1205         long last_written;
1206         unsigned long sectors_per_frame = SECTORS_PER_FRAME;
1207
1208         ide_debug_log(IDE_DBG_FUNC, "enter");
1209
1210         if (toc == NULL) {
1211                 /* try to allocate space */
1212                 toc = kmalloc(sizeof(struct atapi_toc), GFP_KERNEL);
1213                 if (toc == NULL) {
1214                         printk(KERN_ERR PFX "%s: No cdrom TOC buffer!\n",
1215                                         drive->name);
1216                         return -ENOMEM;
1217                 }
1218                 info->toc = toc;
1219         }
1220
1221         /*
1222          * Check to see if the existing data is still valid. If it is,
1223          * just return.
1224          */
1225         (void) cdrom_check_status(drive, sense);
1226
1227         if (drive->atapi_flags & IDE_AFLAG_TOC_VALID)
1228                 return 0;
1229
1230         /* try to get the total cdrom capacity and sector size */
1231         stat = cdrom_read_capacity(drive, &toc->capacity, &sectors_per_frame,
1232                                    sense);
1233         if (stat)
1234                 toc->capacity = 0x1fffff;
1235
1236         set_capacity(info->disk, toc->capacity * sectors_per_frame);
1237         /* save a private copy of the TOC capacity for error handling */
1238         drive->probed_capacity = toc->capacity * sectors_per_frame;
1239
1240         blk_queue_hardsect_size(drive->queue,
1241                                 sectors_per_frame << SECTOR_BITS);
1242
1243         /* first read just the header, so we know how long the TOC is */
1244         stat = cdrom_read_tocentry(drive, 0, 1, 0, (char *) &toc->hdr,
1245                                     sizeof(struct atapi_toc_header), sense);
1246         if (stat)
1247                 return stat;
1248
1249         if (drive->atapi_flags & IDE_AFLAG_TOCTRACKS_AS_BCD) {
1250                 toc->hdr.first_track = bcd2bin(toc->hdr.first_track);
1251                 toc->hdr.last_track  = bcd2bin(toc->hdr.last_track);
1252         }
1253
1254         ntracks = toc->hdr.last_track - toc->hdr.first_track + 1;
1255         if (ntracks <= 0)
1256                 return -EIO;
1257         if (ntracks > MAX_TRACKS)
1258                 ntracks = MAX_TRACKS;
1259
1260         /* now read the whole schmeer */
1261         stat = cdrom_read_tocentry(drive, toc->hdr.first_track, 1, 0,
1262                                   (char *)&toc->hdr,
1263                                    sizeof(struct atapi_toc_header) +
1264                                    (ntracks + 1) *
1265                                    sizeof(struct atapi_toc_entry), sense);
1266
1267         if (stat && toc->hdr.first_track > 1) {
1268                 /*
1269                  * Cds with CDI tracks only don't have any TOC entries, despite
1270                  * of this the returned values are
1271                  * first_track == last_track = number of CDI tracks + 1,
1272                  * so that this case is indistinguishable from the same layout
1273                  * plus an additional audio track. If we get an error for the
1274                  * regular case, we assume a CDI without additional audio
1275                  * tracks. In this case the readable TOC is empty (CDI tracks
1276                  * are not included) and only holds the Leadout entry.
1277                  *
1278                  * Heiko Eißfeldt.
1279                  */
1280                 ntracks = 0;
1281                 stat = cdrom_read_tocentry(drive, CDROM_LEADOUT, 1, 0,
1282                                            (char *)&toc->hdr,
1283                                            sizeof(struct atapi_toc_header) +
1284                                            (ntracks + 1) *
1285                                            sizeof(struct atapi_toc_entry),
1286                                            sense);
1287                 if (stat)
1288                         return stat;
1289
1290                 if (drive->atapi_flags & IDE_AFLAG_TOCTRACKS_AS_BCD) {
1291                         toc->hdr.first_track = (u8)bin2bcd(CDROM_LEADOUT);
1292                         toc->hdr.last_track = (u8)bin2bcd(CDROM_LEADOUT);
1293                 } else {
1294                         toc->hdr.first_track = CDROM_LEADOUT;
1295                         toc->hdr.last_track = CDROM_LEADOUT;
1296                 }
1297         }
1298
1299         if (stat)
1300                 return stat;
1301
1302         toc->hdr.toc_length = be16_to_cpu(toc->hdr.toc_length);
1303
1304         if (drive->atapi_flags & IDE_AFLAG_TOCTRACKS_AS_BCD) {
1305                 toc->hdr.first_track = bcd2bin(toc->hdr.first_track);
1306                 toc->hdr.last_track  = bcd2bin(toc->hdr.last_track);
1307         }
1308
1309         for (i = 0; i <= ntracks; i++) {
1310                 if (drive->atapi_flags & IDE_AFLAG_TOCADDR_AS_BCD) {
1311                         if (drive->atapi_flags & IDE_AFLAG_TOCTRACKS_AS_BCD)
1312                                 toc->ent[i].track = bcd2bin(toc->ent[i].track);
1313                         msf_from_bcd(&toc->ent[i].addr.msf);
1314                 }
1315                 toc->ent[i].addr.lba = msf_to_lba(toc->ent[i].addr.msf.minute,
1316                                                   toc->ent[i].addr.msf.second,
1317                                                   toc->ent[i].addr.msf.frame);
1318         }
1319
1320         if (toc->hdr.first_track != CDROM_LEADOUT) {
1321                 /* read the multisession information */
1322                 stat = cdrom_read_tocentry(drive, 0, 0, 1, (char *)&ms_tmp,
1323                                            sizeof(ms_tmp), sense);
1324                 if (stat)
1325                         return stat;
1326
1327                 toc->last_session_lba = be32_to_cpu(ms_tmp.ent.addr.lba);
1328         } else {
1329                 ms_tmp.hdr.last_track = CDROM_LEADOUT;
1330                 ms_tmp.hdr.first_track = ms_tmp.hdr.last_track;
1331                 toc->last_session_lba = msf_to_lba(0, 2, 0); /* 0m 2s 0f */
1332         }
1333
1334         if (drive->atapi_flags & IDE_AFLAG_TOCADDR_AS_BCD) {
1335                 /* re-read multisession information using MSF format */
1336                 stat = cdrom_read_tocentry(drive, 0, 1, 1, (char *)&ms_tmp,
1337                                            sizeof(ms_tmp), sense);
1338                 if (stat)
1339                         return stat;
1340
1341                 msf_from_bcd(&ms_tmp.ent.addr.msf);
1342                 toc->last_session_lba = msf_to_lba(ms_tmp.ent.addr.msf.minute,
1343                                                    ms_tmp.ent.addr.msf.second,
1344                                                    ms_tmp.ent.addr.msf.frame);
1345         }
1346
1347         toc->xa_flag = (ms_tmp.hdr.first_track != ms_tmp.hdr.last_track);
1348
1349         /* now try to get the total cdrom capacity */
1350         stat = cdrom_get_last_written(cdi, &last_written);
1351         if (!stat && (last_written > toc->capacity)) {
1352                 toc->capacity = last_written;
1353                 set_capacity(info->disk, toc->capacity * sectors_per_frame);
1354                 drive->probed_capacity = toc->capacity * sectors_per_frame;
1355         }
1356
1357         /* Remember that we've read this stuff. */
1358         drive->atapi_flags |= IDE_AFLAG_TOC_VALID;
1359
1360         return 0;
1361 }
1362
1363 int ide_cdrom_get_capabilities(ide_drive_t *drive, u8 *buf)
1364 {
1365         struct cdrom_info *info = drive->driver_data;
1366         struct cdrom_device_info *cdi = &info->devinfo;
1367         struct packet_command cgc;
1368         int stat, attempts = 3, size = ATAPI_CAPABILITIES_PAGE_SIZE;
1369
1370         ide_debug_log(IDE_DBG_FUNC, "enter");
1371
1372         if ((drive->atapi_flags & IDE_AFLAG_FULL_CAPS_PAGE) == 0)
1373                 size -= ATAPI_CAPABILITIES_PAGE_PAD_SIZE;
1374
1375         init_cdrom_command(&cgc, buf, size, CGC_DATA_UNKNOWN);
1376         do {
1377                 /* we seem to get stat=0x01,err=0x00 the first time (??) */
1378                 stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CAPABILITIES_PAGE, 0);
1379                 if (!stat)
1380                         break;
1381         } while (--attempts);
1382         return stat;
1383 }
1384
1385 void ide_cdrom_update_speed(ide_drive_t *drive, u8 *buf)
1386 {
1387         struct cdrom_info *cd = drive->driver_data;
1388         u16 curspeed, maxspeed;
1389
1390         ide_debug_log(IDE_DBG_FUNC, "enter");
1391
1392         if (drive->atapi_flags & IDE_AFLAG_LE_SPEED_FIELDS) {
1393                 curspeed = le16_to_cpup((__le16 *)&buf[8 + 14]);
1394                 maxspeed = le16_to_cpup((__le16 *)&buf[8 + 8]);
1395         } else {
1396                 curspeed = be16_to_cpup((__be16 *)&buf[8 + 14]);
1397                 maxspeed = be16_to_cpup((__be16 *)&buf[8 + 8]);
1398         }
1399
1400         ide_debug_log(IDE_DBG_PROBE, "curspeed: %u, maxspeed: %u",
1401                                      curspeed, maxspeed);
1402
1403         cd->current_speed = (curspeed + (176/2)) / 176;
1404         cd->max_speed = (maxspeed + (176/2)) / 176;
1405 }
1406
1407 #define IDE_CD_CAPABILITIES \
1408         (CDC_CLOSE_TRAY | CDC_OPEN_TRAY | CDC_LOCK | CDC_SELECT_SPEED | \
1409          CDC_SELECT_DISC | CDC_MULTI_SESSION | CDC_MCN | CDC_MEDIA_CHANGED | \
1410          CDC_PLAY_AUDIO | CDC_RESET | CDC_DRIVE_STATUS | CDC_CD_R | \
1411          CDC_CD_RW | CDC_DVD | CDC_DVD_R | CDC_DVD_RAM | CDC_GENERIC_PACKET | \
1412          CDC_MO_DRIVE | CDC_MRW | CDC_MRW_W | CDC_RAM)
1413
1414 static struct cdrom_device_ops ide_cdrom_dops = {
1415         .open                   = ide_cdrom_open_real,
1416         .release                = ide_cdrom_release_real,
1417         .drive_status           = ide_cdrom_drive_status,
1418         .media_changed          = ide_cdrom_check_media_change_real,
1419         .tray_move              = ide_cdrom_tray_move,
1420         .lock_door              = ide_cdrom_lock_door,
1421         .select_speed           = ide_cdrom_select_speed,
1422         .get_last_session       = ide_cdrom_get_last_session,
1423         .get_mcn                = ide_cdrom_get_mcn,
1424         .reset                  = ide_cdrom_reset,
1425         .audio_ioctl            = ide_cdrom_audio_ioctl,
1426         .capability             = IDE_CD_CAPABILITIES,
1427         .generic_packet         = ide_cdrom_packet,
1428 };
1429
1430 static int ide_cdrom_register(ide_drive_t *drive, int nslots)
1431 {
1432         struct cdrom_info *info = drive->driver_data;
1433         struct cdrom_device_info *devinfo = &info->devinfo;
1434
1435         ide_debug_log(IDE_DBG_PROBE, "nslots: %d", nslots);
1436
1437         devinfo->ops = &ide_cdrom_dops;
1438         devinfo->speed = info->current_speed;
1439         devinfo->capacity = nslots;
1440         devinfo->handle = drive;
1441         strcpy(devinfo->name, drive->name);
1442
1443         if (drive->atapi_flags & IDE_AFLAG_NO_SPEED_SELECT)
1444                 devinfo->mask |= CDC_SELECT_SPEED;
1445
1446         devinfo->disk = info->disk;
1447         return register_cdrom(devinfo);
1448 }
1449
1450 static int ide_cdrom_probe_capabilities(ide_drive_t *drive)
1451 {
1452         struct cdrom_info *cd = drive->driver_data;
1453         struct cdrom_device_info *cdi = &cd->devinfo;
1454         u8 buf[ATAPI_CAPABILITIES_PAGE_SIZE];
1455         mechtype_t mechtype;
1456         int nslots = 1;
1457
1458         ide_debug_log(IDE_DBG_PROBE, "media: 0x%x, atapi_flags: 0x%lx",
1459                                      drive->media, drive->atapi_flags);
1460
1461         cdi->mask = (CDC_CD_R | CDC_CD_RW | CDC_DVD | CDC_DVD_R |
1462                      CDC_DVD_RAM | CDC_SELECT_DISC | CDC_PLAY_AUDIO |
1463                      CDC_MO_DRIVE | CDC_RAM);
1464
1465         if (drive->media == ide_optical) {
1466                 cdi->mask &= ~(CDC_MO_DRIVE | CDC_RAM);
1467                 printk(KERN_ERR PFX "%s: ATAPI magneto-optical drive\n",
1468                                 drive->name);
1469                 return nslots;
1470         }
1471
1472         if (drive->atapi_flags & IDE_AFLAG_PRE_ATAPI12) {
1473                 drive->atapi_flags &= ~IDE_AFLAG_NO_EJECT;
1474                 cdi->mask &= ~CDC_PLAY_AUDIO;
1475                 return nslots;
1476         }
1477
1478         /*
1479          * We have to cheat a little here. the packet will eventually be queued
1480          * with ide_cdrom_packet(), which extracts the drive from cdi->handle.
1481          * Since this device hasn't been registered with the Uniform layer yet,
1482          * it can't do this. Same goes for cdi->ops.
1483          */
1484         cdi->handle = drive;
1485         cdi->ops = &ide_cdrom_dops;
1486
1487         if (ide_cdrom_get_capabilities(drive, buf))
1488                 return 0;
1489
1490         if ((buf[8 + 6] & 0x01) == 0)
1491                 drive->dev_flags &= ~IDE_DFLAG_DOORLOCKING;
1492         if (buf[8 + 6] & 0x08)
1493                 drive->atapi_flags &= ~IDE_AFLAG_NO_EJECT;
1494         if (buf[8 + 3] & 0x01)
1495                 cdi->mask &= ~CDC_CD_R;
1496         if (buf[8 + 3] & 0x02)
1497                 cdi->mask &= ~(CDC_CD_RW | CDC_RAM);
1498         if (buf[8 + 2] & 0x38)
1499                 cdi->mask &= ~CDC_DVD;
1500         if (buf[8 + 3] & 0x20)
1501                 cdi->mask &= ~(CDC_DVD_RAM | CDC_RAM);
1502         if (buf[8 + 3] & 0x10)
1503                 cdi->mask &= ~CDC_DVD_R;
1504         if ((buf[8 + 4] & 0x01) || (drive->atapi_flags & IDE_AFLAG_PLAY_AUDIO_OK))
1505                 cdi->mask &= ~CDC_PLAY_AUDIO;
1506
1507         mechtype = buf[8 + 6] >> 5;
1508         if (mechtype == mechtype_caddy ||
1509             mechtype == mechtype_popup ||
1510             (drive->atapi_flags & IDE_AFLAG_NO_AUTOCLOSE))
1511                 cdi->mask |= CDC_CLOSE_TRAY;
1512
1513         if (cdi->sanyo_slot > 0) {
1514                 cdi->mask &= ~CDC_SELECT_DISC;
1515                 nslots = 3;
1516         } else if (mechtype == mechtype_individual_changer ||
1517                    mechtype == mechtype_cartridge_changer) {
1518                 nslots = cdrom_number_of_slots(cdi);
1519                 if (nslots > 1)
1520                         cdi->mask &= ~CDC_SELECT_DISC;
1521         }
1522
1523         ide_cdrom_update_speed(drive, buf);
1524
1525         printk(KERN_INFO PFX "%s: ATAPI", drive->name);
1526
1527         /* don't print speed if the drive reported 0 */
1528         if (cd->max_speed)
1529                 printk(KERN_CONT " %dX", cd->max_speed);
1530
1531         printk(KERN_CONT " %s", (cdi->mask & CDC_DVD) ? "CD-ROM" : "DVD-ROM");
1532
1533         if ((cdi->mask & CDC_DVD_R) == 0 || (cdi->mask & CDC_DVD_RAM) == 0)
1534                 printk(KERN_CONT " DVD%s%s",
1535                                  (cdi->mask & CDC_DVD_R) ? "" : "-R",
1536                                  (cdi->mask & CDC_DVD_RAM) ? "" : "/RAM");
1537
1538         if ((cdi->mask & CDC_CD_R) == 0 || (cdi->mask & CDC_CD_RW) == 0)
1539                 printk(KERN_CONT " CD%s%s",
1540                                  (cdi->mask & CDC_CD_R) ? "" : "-R",
1541                                  (cdi->mask & CDC_CD_RW) ? "" : "/RW");
1542
1543         if ((cdi->mask & CDC_SELECT_DISC) == 0)
1544                 printk(KERN_CONT " changer w/%d slots", nslots);
1545         else
1546                 printk(KERN_CONT " drive");
1547
1548         printk(KERN_CONT ", %dkB Cache\n",
1549                          be16_to_cpup((__be16 *)&buf[8 + 12]));
1550
1551         return nslots;
1552 }
1553
1554 /* standard prep_rq_fn that builds 10 byte cmds */
1555 static int ide_cdrom_prep_fs(struct request_queue *q, struct request *rq)
1556 {
1557         int hard_sect = queue_hardsect_size(q);
1558         long block = (long)rq->hard_sector / (hard_sect >> 9);
1559         unsigned long blocks = rq->hard_nr_sectors / (hard_sect >> 9);
1560
1561         memset(rq->cmd, 0, BLK_MAX_CDB);
1562
1563         if (rq_data_dir(rq) == READ)
1564                 rq->cmd[0] = GPCMD_READ_10;
1565         else
1566                 rq->cmd[0] = GPCMD_WRITE_10;
1567
1568         /*
1569          * fill in lba
1570          */
1571         rq->cmd[2] = (block >> 24) & 0xff;
1572         rq->cmd[3] = (block >> 16) & 0xff;
1573         rq->cmd[4] = (block >>  8) & 0xff;
1574         rq->cmd[5] = block & 0xff;
1575
1576         /*
1577          * and transfer length
1578          */
1579         rq->cmd[7] = (blocks >> 8) & 0xff;
1580         rq->cmd[8] = blocks & 0xff;
1581         rq->cmd_len = 10;
1582         return BLKPREP_OK;
1583 }
1584
1585 /*
1586  * Most of the SCSI commands are supported directly by ATAPI devices.
1587  * This transform handles the few exceptions.
1588  */
1589 static int ide_cdrom_prep_pc(struct request *rq)
1590 {
1591         u8 *c = rq->cmd;
1592
1593         /* transform 6-byte read/write commands to the 10-byte version */
1594         if (c[0] == READ_6 || c[0] == WRITE_6) {
1595                 c[8] = c[4];
1596                 c[5] = c[3];
1597                 c[4] = c[2];
1598                 c[3] = c[1] & 0x1f;
1599                 c[2] = 0;
1600                 c[1] &= 0xe0;
1601                 c[0] += (READ_10 - READ_6);
1602                 rq->cmd_len = 10;
1603                 return BLKPREP_OK;
1604         }
1605
1606         /*
1607          * it's silly to pretend we understand 6-byte sense commands, just
1608          * reject with ILLEGAL_REQUEST and the caller should take the
1609          * appropriate action
1610          */
1611         if (c[0] == MODE_SENSE || c[0] == MODE_SELECT) {
1612                 rq->errors = ILLEGAL_REQUEST;
1613                 return BLKPREP_KILL;
1614         }
1615
1616         return BLKPREP_OK;
1617 }
1618
1619 static int ide_cdrom_prep_fn(struct request_queue *q, struct request *rq)
1620 {
1621         if (blk_fs_request(rq))
1622                 return ide_cdrom_prep_fs(q, rq);
1623         else if (blk_pc_request(rq))
1624                 return ide_cdrom_prep_pc(rq);
1625
1626         return 0;
1627 }
1628
1629 struct cd_list_entry {
1630         const char      *id_model;
1631         const char      *id_firmware;
1632         unsigned int    cd_flags;
1633 };
1634
1635 #ifdef CONFIG_IDE_PROC_FS
1636 static sector_t ide_cdrom_capacity(ide_drive_t *drive)
1637 {
1638         unsigned long capacity, sectors_per_frame;
1639
1640         if (cdrom_read_capacity(drive, &capacity, &sectors_per_frame, NULL))
1641                 return 0;
1642
1643         return capacity * sectors_per_frame;
1644 }
1645
1646 static int proc_idecd_read_capacity(char *page, char **start, off_t off,
1647                                         int count, int *eof, void *data)
1648 {
1649         ide_drive_t *drive = data;
1650         int len;
1651
1652         len = sprintf(page, "%llu\n", (long long)ide_cdrom_capacity(drive));
1653         PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
1654 }
1655
1656 static ide_proc_entry_t idecd_proc[] = {
1657         { "capacity", S_IFREG|S_IRUGO, proc_idecd_read_capacity, NULL },
1658         { NULL, 0, NULL, NULL }
1659 };
1660
1661 static ide_proc_entry_t *ide_cd_proc_entries(ide_drive_t *drive)
1662 {
1663         return idecd_proc;
1664 }
1665
1666 static const struct ide_proc_devset *ide_cd_proc_devsets(ide_drive_t *drive)
1667 {
1668         return NULL;
1669 }
1670 #endif
1671
1672 static const struct cd_list_entry ide_cd_quirks_list[] = {
1673         /* Limit transfer size per interrupt. */
1674         { "SAMSUNG CD-ROM SCR-2430", NULL,   IDE_AFLAG_LIMIT_NFRAMES         },
1675         { "SAMSUNG CD-ROM SCR-2432", NULL,   IDE_AFLAG_LIMIT_NFRAMES         },
1676         /* SCR-3231 doesn't support the SET_CD_SPEED command. */
1677         { "SAMSUNG CD-ROM SCR-3231", NULL,   IDE_AFLAG_NO_SPEED_SELECT       },
1678         /* Old NEC260 (not R) was released before ATAPI 1.2 spec. */
1679         { "NEC CD-ROM DRIVE:260",    "1.01", IDE_AFLAG_TOCADDR_AS_BCD |
1680                                              IDE_AFLAG_PRE_ATAPI12,          },
1681         /* Vertos 300, some versions of this drive like to talk BCD. */
1682         { "V003S0DS",                NULL,   IDE_AFLAG_VERTOS_300_SSD,       },
1683         /* Vertos 600 ESD. */
1684         { "V006E0DS",                NULL,   IDE_AFLAG_VERTOS_600_ESD,       },
1685         /*
1686          * Sanyo 3 CD changer uses a non-standard command for CD changing
1687          * (by default standard ATAPI support for CD changers is used).
1688          */
1689         { "CD-ROM CDR-C3 G",         NULL,   IDE_AFLAG_SANYO_3CD             },
1690         { "CD-ROM CDR-C3G",          NULL,   IDE_AFLAG_SANYO_3CD             },
1691         { "CD-ROM CDR_C36",          NULL,   IDE_AFLAG_SANYO_3CD             },
1692         /* Stingray 8X CD-ROM. */
1693         { "STINGRAY 8422 IDE 8X CD-ROM 7-27-95", NULL, IDE_AFLAG_PRE_ATAPI12 },
1694         /*
1695          * ACER 50X CD-ROM and WPI 32X CD-ROM require the full spec length
1696          * mode sense page capabilities size, but older drives break.
1697          */
1698         { "ATAPI CD ROM DRIVE 50X MAX", NULL,   IDE_AFLAG_FULL_CAPS_PAGE     },
1699         { "WPI CDS-32X",                NULL,   IDE_AFLAG_FULL_CAPS_PAGE     },
1700         /* ACER/AOpen 24X CD-ROM has the speed fields byte-swapped. */
1701         { "",                        "241N", IDE_AFLAG_LE_SPEED_FIELDS       },
1702         /*
1703          * Some drives used by Apple don't advertise audio play
1704          * but they do support reading TOC & audio datas.
1705          */
1706         { "MATSHITADVD-ROM SR-8187", NULL,   IDE_AFLAG_PLAY_AUDIO_OK         },
1707         { "MATSHITADVD-ROM SR-8186", NULL,   IDE_AFLAG_PLAY_AUDIO_OK         },
1708         { "MATSHITADVD-ROM SR-8176", NULL,   IDE_AFLAG_PLAY_AUDIO_OK         },
1709         { "MATSHITADVD-ROM SR-8174", NULL,   IDE_AFLAG_PLAY_AUDIO_OK         },
1710         { "Optiarc DVD RW AD-5200A", NULL,   IDE_AFLAG_PLAY_AUDIO_OK         },
1711         { "Optiarc DVD RW AD-7200A", NULL,   IDE_AFLAG_PLAY_AUDIO_OK         },
1712         { "Optiarc DVD RW AD-7543A", NULL,   IDE_AFLAG_NO_AUTOCLOSE          },
1713         { "TEAC CD-ROM CD-224E",     NULL,   IDE_AFLAG_NO_AUTOCLOSE          },
1714         { NULL, NULL, 0 }
1715 };
1716
1717 static unsigned int ide_cd_flags(u16 *id)
1718 {
1719         const struct cd_list_entry *cle = ide_cd_quirks_list;
1720
1721         while (cle->id_model) {
1722                 if (strcmp(cle->id_model, (char *)&id[ATA_ID_PROD]) == 0 &&
1723                     (cle->id_firmware == NULL ||
1724                      strstr((char *)&id[ATA_ID_FW_REV], cle->id_firmware)))
1725                         return cle->cd_flags;
1726                 cle++;
1727         }
1728
1729         return 0;
1730 }
1731
1732 static int ide_cdrom_setup(ide_drive_t *drive)
1733 {
1734         struct cdrom_info *cd = drive->driver_data;
1735         struct cdrom_device_info *cdi = &cd->devinfo;
1736         u16 *id = drive->id;
1737         char *fw_rev = (char *)&id[ATA_ID_FW_REV];
1738         int nslots;
1739
1740         ide_debug_log(IDE_DBG_PROBE, "enter");
1741
1742         blk_queue_prep_rq(drive->queue, ide_cdrom_prep_fn);
1743         blk_queue_dma_alignment(drive->queue, 31);
1744         blk_queue_update_dma_pad(drive->queue, 15);
1745         drive->queue->unplug_delay = (1 * HZ) / 1000;
1746         if (!drive->queue->unplug_delay)
1747                 drive->queue->unplug_delay = 1;
1748
1749         drive->dev_flags |= IDE_DFLAG_MEDIA_CHANGED;
1750         drive->atapi_flags = IDE_AFLAG_NO_EJECT | ide_cd_flags(id);
1751
1752         if ((drive->atapi_flags & IDE_AFLAG_VERTOS_300_SSD) &&
1753             fw_rev[4] == '1' && fw_rev[6] <= '2')
1754                 drive->atapi_flags |= (IDE_AFLAG_TOCTRACKS_AS_BCD |
1755                                      IDE_AFLAG_TOCADDR_AS_BCD);
1756         else if ((drive->atapi_flags & IDE_AFLAG_VERTOS_600_ESD) &&
1757                  fw_rev[4] == '1' && fw_rev[6] <= '2')
1758                 drive->atapi_flags |= IDE_AFLAG_TOCTRACKS_AS_BCD;
1759         else if (drive->atapi_flags & IDE_AFLAG_SANYO_3CD)
1760                 /* 3 => use CD in slot 0 */
1761                 cdi->sanyo_slot = 3;
1762
1763         nslots = ide_cdrom_probe_capabilities(drive);
1764
1765         /* set correct block size */
1766         blk_queue_hardsect_size(drive->queue, CD_FRAMESIZE);
1767
1768         if (ide_cdrom_register(drive, nslots)) {
1769                 printk(KERN_ERR PFX "%s: %s failed to register device with the"
1770                                 " cdrom driver.\n", drive->name, __func__);
1771                 cd->devinfo.handle = NULL;
1772                 return 1;
1773         }
1774
1775         ide_proc_register_driver(drive, cd->driver);
1776         return 0;
1777 }
1778
1779 static void ide_cd_remove(ide_drive_t *drive)
1780 {
1781         struct cdrom_info *info = drive->driver_data;
1782
1783         ide_debug_log(IDE_DBG_FUNC, "enter");
1784
1785         ide_proc_unregister_driver(drive, info->driver);
1786         device_del(&info->dev);
1787         del_gendisk(info->disk);
1788
1789         mutex_lock(&idecd_ref_mutex);
1790         put_device(&info->dev);
1791         mutex_unlock(&idecd_ref_mutex);
1792 }
1793
1794 static void ide_cd_release(struct device *dev)
1795 {
1796         struct cdrom_info *info = to_ide_drv(dev, cdrom_info);
1797         struct cdrom_device_info *devinfo = &info->devinfo;
1798         ide_drive_t *drive = info->drive;
1799         struct gendisk *g = info->disk;
1800
1801         ide_debug_log(IDE_DBG_FUNC, "enter");
1802
1803         kfree(info->toc);
1804         if (devinfo->handle == drive)
1805                 unregister_cdrom(devinfo);
1806         drive->driver_data = NULL;
1807         blk_queue_prep_rq(drive->queue, NULL);
1808         g->private_data = NULL;
1809         put_disk(g);
1810         kfree(info);
1811 }
1812
1813 static int ide_cd_probe(ide_drive_t *);
1814
1815 static struct ide_driver ide_cdrom_driver = {
1816         .gen_driver = {
1817                 .owner          = THIS_MODULE,
1818                 .name           = "ide-cdrom",
1819                 .bus            = &ide_bus_type,
1820         },
1821         .probe                  = ide_cd_probe,
1822         .remove                 = ide_cd_remove,
1823         .version                = IDECD_VERSION,
1824         .do_request             = ide_cd_do_request,
1825 #ifdef CONFIG_IDE_PROC_FS
1826         .proc_entries           = ide_cd_proc_entries,
1827         .proc_devsets           = ide_cd_proc_devsets,
1828 #endif
1829 };
1830
1831 static int idecd_open(struct block_device *bdev, fmode_t mode)
1832 {
1833         struct cdrom_info *info = ide_cd_get(bdev->bd_disk);
1834         int rc = -ENOMEM;
1835
1836         if (!info)
1837                 return -ENXIO;
1838
1839         rc = cdrom_open(&info->devinfo, bdev, mode);
1840
1841         if (rc < 0)
1842                 ide_cd_put(info);
1843
1844         return rc;
1845 }
1846
1847 static int idecd_release(struct gendisk *disk, fmode_t mode)
1848 {
1849         struct cdrom_info *info = ide_drv_g(disk, cdrom_info);
1850
1851         cdrom_release(&info->devinfo, mode);
1852
1853         ide_cd_put(info);
1854
1855         return 0;
1856 }
1857
1858 static int idecd_set_spindown(struct cdrom_device_info *cdi, unsigned long arg)
1859 {
1860         struct packet_command cgc;
1861         char buffer[16];
1862         int stat;
1863         char spindown;
1864
1865         if (copy_from_user(&spindown, (void __user *)arg, sizeof(char)))
1866                 return -EFAULT;
1867
1868         init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_UNKNOWN);
1869
1870         stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CDROM_PAGE, 0);
1871         if (stat)
1872                 return stat;
1873
1874         buffer[11] = (buffer[11] & 0xf0) | (spindown & 0x0f);
1875         return cdrom_mode_select(cdi, &cgc);
1876 }
1877
1878 static int idecd_get_spindown(struct cdrom_device_info *cdi, unsigned long arg)
1879 {
1880         struct packet_command cgc;
1881         char buffer[16];
1882         int stat;
1883         char spindown;
1884
1885         init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_UNKNOWN);
1886
1887         stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CDROM_PAGE, 0);
1888         if (stat)
1889                 return stat;
1890
1891         spindown = buffer[11] & 0x0f;
1892         if (copy_to_user((void __user *)arg, &spindown, sizeof(char)))
1893                 return -EFAULT;
1894         return 0;
1895 }
1896
1897 static int idecd_ioctl(struct block_device *bdev, fmode_t mode,
1898                         unsigned int cmd, unsigned long arg)
1899 {
1900         struct cdrom_info *info = ide_drv_g(bdev->bd_disk, cdrom_info);
1901         int err;
1902
1903         switch (cmd) {
1904         case CDROMSETSPINDOWN:
1905                 return idecd_set_spindown(&info->devinfo, arg);
1906         case CDROMGETSPINDOWN:
1907                 return idecd_get_spindown(&info->devinfo, arg);
1908         default:
1909                 break;
1910         }
1911
1912         err = generic_ide_ioctl(info->drive, bdev, cmd, arg);
1913         if (err == -EINVAL)
1914                 err = cdrom_ioctl(&info->devinfo, bdev, mode, cmd, arg);
1915
1916         return err;
1917 }
1918
1919 static int idecd_media_changed(struct gendisk *disk)
1920 {
1921         struct cdrom_info *info = ide_drv_g(disk, cdrom_info);
1922         return cdrom_media_changed(&info->devinfo);
1923 }
1924
1925 static int idecd_revalidate_disk(struct gendisk *disk)
1926 {
1927         struct cdrom_info *info = ide_drv_g(disk, cdrom_info);
1928         struct request_sense sense;
1929
1930         ide_cd_read_toc(info->drive, &sense);
1931
1932         return  0;
1933 }
1934
1935 static struct block_device_operations idecd_ops = {
1936         .owner                  = THIS_MODULE,
1937         .open                   = idecd_open,
1938         .release                = idecd_release,
1939         .locked_ioctl           = idecd_ioctl,
1940         .media_changed          = idecd_media_changed,
1941         .revalidate_disk        = idecd_revalidate_disk
1942 };
1943
1944 /* module options */
1945 static char *ignore;
1946 module_param(ignore, charp, 0400);
1947
1948 static unsigned long debug_mask;
1949 module_param(debug_mask, ulong, 0644);
1950
1951 MODULE_DESCRIPTION("ATAPI CD-ROM Driver");
1952
1953 static int ide_cd_probe(ide_drive_t *drive)
1954 {
1955         struct cdrom_info *info;
1956         struct gendisk *g;
1957         struct request_sense sense;
1958
1959         ide_debug_log(IDE_DBG_PROBE, "driver_req: %s, media: 0x%x",
1960                                      drive->driver_req, drive->media);
1961
1962         if (!strstr("ide-cdrom", drive->driver_req))
1963                 goto failed;
1964
1965         if (drive->media != ide_cdrom && drive->media != ide_optical)
1966                 goto failed;
1967
1968         /* skip drives that we were told to ignore */
1969         if (ignore != NULL) {
1970                 if (strstr(ignore, drive->name)) {
1971                         printk(KERN_INFO PFX "ignoring drive %s\n",
1972                                          drive->name);
1973                         goto failed;
1974                 }
1975         }
1976
1977         drive->debug_mask = debug_mask;
1978         drive->irq_handler = cdrom_newpc_intr;
1979
1980         info = kzalloc(sizeof(struct cdrom_info), GFP_KERNEL);
1981         if (info == NULL) {
1982                 printk(KERN_ERR PFX "%s: Can't allocate a cdrom structure\n",
1983                                 drive->name);
1984                 goto failed;
1985         }
1986
1987         g = alloc_disk(1 << PARTN_BITS);
1988         if (!g)
1989                 goto out_free_cd;
1990
1991         ide_init_disk(g, drive);
1992
1993         info->dev.parent = &drive->gendev;
1994         info->dev.release = ide_cd_release;
1995         dev_set_name(&info->dev, dev_name(&drive->gendev));
1996
1997         if (device_register(&info->dev))
1998                 goto out_free_disk;
1999
2000         info->drive = drive;
2001         info->driver = &ide_cdrom_driver;
2002         info->disk = g;
2003
2004         g->private_data = &info->driver;
2005
2006         drive->driver_data = info;
2007
2008         g->minors = 1;
2009         g->driverfs_dev = &drive->gendev;
2010         g->flags = GENHD_FL_CD | GENHD_FL_REMOVABLE;
2011         if (ide_cdrom_setup(drive)) {
2012                 put_device(&info->dev);
2013                 goto failed;
2014         }
2015
2016         ide_cd_read_toc(drive, &sense);
2017         g->fops = &idecd_ops;
2018         g->flags |= GENHD_FL_REMOVABLE;
2019         add_disk(g);
2020         return 0;
2021
2022 out_free_disk:
2023         put_disk(g);
2024 out_free_cd:
2025         kfree(info);
2026 failed:
2027         return -ENODEV;
2028 }
2029
2030 static void __exit ide_cdrom_exit(void)
2031 {
2032         driver_unregister(&ide_cdrom_driver.gen_driver);
2033 }
2034
2035 static int __init ide_cdrom_init(void)
2036 {
2037         printk(KERN_INFO DRV_NAME " driver " IDECD_VERSION "\n");
2038         return driver_register(&ide_cdrom_driver.gen_driver);
2039 }
2040
2041 MODULE_ALIAS("ide:*m-cdrom*");
2042 MODULE_ALIAS("ide-cd");
2043 module_init(ide_cdrom_init);
2044 module_exit(ide_cdrom_exit);
2045 MODULE_LICENSE("GPL");