]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/ide/ide-cd.c
qd65xx: fix setup of QD6580 Control register
[mirror_ubuntu-artful-kernel.git] / drivers / ide / ide-cd.c
CommitLineData
1da177e4 1/*
3bb4663b 2 * ATAPI CD-ROM driver.
1da177e4 3 *
59bca8cc
BZ
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
1da177e4
LT
8 *
9 * May be copied or modified under the terms of the GNU General Public
10 * License. See linux/COPYING for more information.
11 *
1da177e4
LT
12 * See Documentation/cdrom/ide-cd for usage information.
13 *
14 * Suggestions are welcome. Patches that work are more welcome though. ;-)
15 * For those wishing to work on this driver, please be sure you download
16 * and comply with the latest Mt. Fuji (SFF8090 version 4) and ATAPI
17 * (SFF-8020i rev 2.6) standards. These documents can be obtained by
18 * anonymous ftp from:
19 * ftp://fission.dt.wdc.com/pub/standards/SFF_atapi/spec/SFF8020-r2.6/PS/8020r26.ps
20 * ftp://ftp.avc-pioneer.com/Mtfuji4/Spec/Fuji4r10.pdf
21 *
03553353
BZ
22 * For historical changelog please see:
23 * Documentation/ide/ChangeLog.ide-cd.1994-2004
24 */
25
3bb4663b 26#define IDECD_VERSION "5.00"
1da177e4 27
1da177e4
LT
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>
cf8b8975 39#include <linux/mutex.h>
9a6dc668 40#include <linux/bcd.h>
1da177e4
LT
41
42#include <scsi/scsi.h> /* For SCSI -> ATAPI command conversion */
43
44#include <asm/irq.h>
45#include <asm/io.h>
46#include <asm/byteorder.h>
47#include <asm/uaccess.h>
48#include <asm/unaligned.h>
49
50#include "ide-cd.h"
51
cf8b8975 52static DEFINE_MUTEX(idecd_ref_mutex);
1da177e4
LT
53
54#define to_ide_cd(obj) container_of(obj, struct cdrom_info, kref)
55
56#define ide_cd_g(disk) \
57 container_of((disk)->private_data, struct cdrom_info, driver)
58
59static struct cdrom_info *ide_cd_get(struct gendisk *disk)
60{
61 struct cdrom_info *cd = NULL;
62
cf8b8975 63 mutex_lock(&idecd_ref_mutex);
1da177e4
LT
64 cd = ide_cd_g(disk);
65 if (cd)
66 kref_get(&cd->kref);
cf8b8975 67 mutex_unlock(&idecd_ref_mutex);
1da177e4
LT
68 return cd;
69}
70
71static void ide_cd_release(struct kref *);
72
73static void ide_cd_put(struct cdrom_info *cd)
74{
cf8b8975 75 mutex_lock(&idecd_ref_mutex);
1da177e4 76 kref_put(&cd->kref, ide_cd_release);
cf8b8975 77 mutex_unlock(&idecd_ref_mutex);
1da177e4
LT
78}
79
80/****************************************************************************
81 * Generic packet command support and error handling routines.
82 */
83
84/* Mark that we've seen a media change, and invalidate our internal
85 buffers. */
86static void cdrom_saw_media_change (ide_drive_t *drive)
87{
0ba11211
BZ
88 struct cdrom_info *cd = drive->driver_data;
89
2bc4cf2d
BZ
90 cd->cd_flags |= IDE_CD_FLAG_MEDIA_CHANGED;
91 cd->cd_flags &= ~IDE_CD_FLAG_TOC_VALID;
0ba11211 92 cd->nsectors_buffered = 0;
1da177e4
LT
93}
94
95static int cdrom_log_sense(ide_drive_t *drive, struct request *rq,
96 struct request_sense *sense)
97{
98 int log = 0;
99
4aff5e23 100 if (!sense || !rq || (rq->cmd_flags & REQ_QUIET))
1da177e4
LT
101 return 0;
102
103 switch (sense->sense_key) {
104 case NO_SENSE: case RECOVERED_ERROR:
105 break;
106 case NOT_READY:
107 /*
108 * don't care about tray state messages for
109 * e.g. capacity commands or in-progress or
110 * becoming ready
111 */
112 if (sense->asc == 0x3a || sense->asc == 0x04)
113 break;
114 log = 1;
115 break;
116 case ILLEGAL_REQUEST:
117 /*
118 * don't log START_STOP unit with LoEj set, since
119 * we cannot reliably check if drive can auto-close
120 */
121 if (rq->cmd[0] == GPCMD_START_STOP_UNIT && sense->asc == 0x24)
dbe217af
AC
122 break;
123 log = 1;
1da177e4
LT
124 break;
125 case UNIT_ATTENTION:
126 /*
127 * Make good and sure we've seen this potential media
128 * change. Some drives (i.e. Creative) fail to present
129 * the correct sense key in the error register.
130 */
131 cdrom_saw_media_change(drive);
132 break;
133 default:
134 log = 1;
135 break;
136 }
137 return log;
138}
139
140static
141void cdrom_analyze_sense_data(ide_drive_t *drive,
142 struct request *failed_command,
143 struct request_sense *sense)
144{
dbe217af
AC
145 unsigned long sector;
146 unsigned long bio_sectors;
147 unsigned long valid;
148 struct cdrom_info *info = drive->driver_data;
149
1da177e4
LT
150 if (!cdrom_log_sense(drive, failed_command, sense))
151 return;
152
153 /*
154 * If a read toc is executed for a CD-R or CD-RW medium where
155 * the first toc has not been recorded yet, it will fail with
156 * 05/24/00 (which is a confusing error)
157 */
158 if (failed_command && failed_command->cmd[0] == GPCMD_READ_TOC_PMA_ATIP)
159 if (sense->sense_key == 0x05 && sense->asc == 0x24)
160 return;
161
dbe217af
AC
162 if (sense->error_code == 0x70) { /* Current Error */
163 switch(sense->sense_key) {
164 case MEDIUM_ERROR:
165 case VOLUME_OVERFLOW:
166 case ILLEGAL_REQUEST:
167 if (!sense->valid)
168 break;
169 if (failed_command == NULL ||
170 !blk_fs_request(failed_command))
171 break;
172 sector = (sense->information[0] << 24) |
173 (sense->information[1] << 16) |
174 (sense->information[2] << 8) |
175 (sense->information[3]);
176
177 bio_sectors = bio_sectors(failed_command->bio);
178 if (bio_sectors < 4)
179 bio_sectors = 4;
180 if (drive->queue->hardsect_size == 2048)
181 sector <<= 2; /* Device sector size is 2K */
182 sector &= ~(bio_sectors -1);
183 valid = (sector - failed_command->sector) << 9;
184
185 if (valid < 0)
186 valid = 0;
187 if (sector < get_capacity(info->disk) &&
188 drive->probed_capacity - sector < 4 * 75) {
189 set_capacity(info->disk, sector);
190 }
191 }
192 }
1da177e4 193
972560fb 194 ide_cd_log_error(drive->name, failed_command, sense);
1da177e4
LT
195}
196
197/*
198 * Initialize a ide-cd packet command request
199 */
17802998 200void ide_cd_init_rq(ide_drive_t *drive, struct request *rq)
1da177e4
LT
201{
202 struct cdrom_info *cd = drive->driver_data;
203
204 ide_init_drive_cmd(rq);
cea2885a 205 rq->cmd_type = REQ_TYPE_ATA_PC;
1da177e4
LT
206 rq->rq_disk = cd->disk;
207}
208
209static 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 = &info->request_sense_request;
214
215 if (sense == NULL)
216 sense = &info->sense_data;
217
218 /* stuff the sense request in front of our current request */
139c829d 219 ide_cd_init_rq(drive, rq);
1da177e4
LT
220
221 rq->data = sense;
222 rq->cmd[0] = GPCMD_REQUEST_SENSE;
223 rq->cmd[4] = rq->data_len = 18;
224
4aff5e23 225 rq->cmd_type = REQ_TYPE_SENSE;
1da177e4
LT
226
227 /* NOTE! Save the failed command in "rq->buffer" */
228 rq->buffer = (void *) failed_command;
229
230 (void) ide_do_drive_cmd(drive, rq, ide_preempt);
231}
232
233static void cdrom_end_request (ide_drive_t *drive, int uptodate)
234{
235 struct request *rq = HWGROUP(drive)->rq;
236 int nsectors = rq->hard_cur_sectors;
237
4aff5e23 238 if (blk_sense_request(rq) && uptodate) {
1da177e4 239 /*
4aff5e23
JA
240 * For REQ_TYPE_SENSE, "rq->buffer" points to the original
241 * failed request
1da177e4
LT
242 */
243 struct request *failed = (struct request *) rq->buffer;
244 struct cdrom_info *info = drive->driver_data;
245 void *sense = &info->sense_data;
246 unsigned long flags;
247
248 if (failed) {
249 if (failed->sense) {
250 sense = failed->sense;
251 failed->sense_len = rq->sense_len;
252 }
dbe217af 253 cdrom_analyze_sense_data(drive, failed, sense);
1da177e4
LT
254 /*
255 * now end failed request
256 */
dbe217af
AC
257 if (blk_fs_request(failed)) {
258 if (ide_end_dequeued_request(drive, failed, 0,
259 failed->hard_nr_sectors))
260 BUG();
261 } else {
262 spin_lock_irqsave(&ide_lock, flags);
5e36bb6e
KU
263 if (__blk_end_request(failed, -EIO,
264 failed->data_len))
265 BUG();
dbe217af
AC
266 spin_unlock_irqrestore(&ide_lock, flags);
267 }
268 } else
269 cdrom_analyze_sense_data(drive, NULL, sense);
1da177e4
LT
270 }
271
272 if (!rq->current_nr_sectors && blk_fs_request(rq))
273 uptodate = 1;
274 /* make sure it's fully ended */
275 if (blk_pc_request(rq))
276 nsectors = (rq->data_len + 511) >> 9;
277 if (!nsectors)
278 nsectors = 1;
279
280 ide_end_request(drive, uptodate, nsectors);
281}
282
dbe217af
AC
283static void ide_dump_status_no_sense(ide_drive_t *drive, const char *msg, u8 stat)
284{
285 if (stat & 0x80)
286 return;
287 ide_dump_status(drive, msg, stat);
288}
289
1da177e4
LT
290/* Returns 0 if the request should be continued.
291 Returns 1 if the request was ended. */
292static int cdrom_decode_status(ide_drive_t *drive, int good_stat, int *stat_ret)
293{
294 struct request *rq = HWGROUP(drive)->rq;
295 int stat, err, sense_key;
296
297 /* Check for errors. */
c47137a9
BZ
298 stat = ide_read_status(drive);
299
1da177e4
LT
300 if (stat_ret)
301 *stat_ret = stat;
302
303 if (OK_STAT(stat, good_stat, BAD_R_STAT))
304 return 0;
305
306 /* Get the IDE error register. */
64a57fe4 307 err = ide_read_error(drive);
1da177e4
LT
308 sense_key = err >> 4;
309
310 if (rq == NULL) {
311 printk("%s: missing rq in cdrom_decode_status\n", drive->name);
312 return 1;
313 }
314
4aff5e23 315 if (blk_sense_request(rq)) {
1da177e4
LT
316 /* We got an error trying to get sense info
317 from the drive (probably while trying
318 to recover from a former error). Just give up. */
319
4aff5e23 320 rq->cmd_flags |= REQ_FAILED;
1da177e4
LT
321 cdrom_end_request(drive, 0);
322 ide_error(drive, "request sense failure", stat);
323 return 1;
324
8770c018 325 } else if (blk_pc_request(rq) || rq->cmd_type == REQ_TYPE_ATA_PC) {
1da177e4 326 /* All other functions, except for READ. */
1da177e4
LT
327
328 /*
329 * if we have an error, pass back CHECK_CONDITION as the
330 * scsi status byte
331 */
b7156731 332 if (blk_pc_request(rq) && !rq->errors)
1da177e4
LT
333 rq->errors = SAM_STAT_CHECK_CONDITION;
334
335 /* Check for tray open. */
336 if (sense_key == NOT_READY) {
337 cdrom_saw_media_change (drive);
338 } else if (sense_key == UNIT_ATTENTION) {
339 /* Check for media change. */
340 cdrom_saw_media_change (drive);
341 /*printk("%s: media changed\n",drive->name);*/
342 return 0;
76ca1af1
SH
343 } else if ((sense_key == ILLEGAL_REQUEST) &&
344 (rq->cmd[0] == GPCMD_START_STOP_UNIT)) {
345 /*
346 * Don't print error message for this condition--
347 * SFF8090i indicates that 5/24/00 is the correct
348 * response to a request to close the tray if the
349 * drive doesn't have that capability.
350 * cdrom_log_sense() knows this!
351 */
4aff5e23 352 } else if (!(rq->cmd_flags & REQ_QUIET)) {
1da177e4
LT
353 /* Otherwise, print an error. */
354 ide_dump_status(drive, "packet command error", stat);
355 }
356
4aff5e23 357 rq->cmd_flags |= REQ_FAILED;
1da177e4
LT
358
359 /*
360 * instead of playing games with moving completions around,
361 * remove failed request completely and end it when the
362 * request sense has completed
363 */
bbb89e3d 364 goto end_request;
1da177e4
LT
365
366 } else if (blk_fs_request(rq)) {
367 int do_end_request = 0;
368
369 /* Handle errors from READ and WRITE requests. */
370
371 if (blk_noretry_request(rq))
372 do_end_request = 1;
373
374 if (sense_key == NOT_READY) {
375 /* Tray open. */
376 if (rq_data_dir(rq) == READ) {
377 cdrom_saw_media_change (drive);
378
379 /* Fail the request. */
380 printk ("%s: tray open\n", drive->name);
381 do_end_request = 1;
382 } else {
383 struct cdrom_info *info = drive->driver_data;
384
385 /* allow the drive 5 seconds to recover, some
386 * devices will return this error while flushing
387 * data from cache */
388 if (!rq->errors)
389 info->write_timeout = jiffies + ATAPI_WAIT_WRITE_BUSY;
390 rq->errors = 1;
391 if (time_after(jiffies, info->write_timeout))
392 do_end_request = 1;
393 else {
394 unsigned long flags;
395
396 /*
397 * take a breather relying on the
398 * unplug timer to kick us again
399 */
400 spin_lock_irqsave(&ide_lock, flags);
401 blk_plug_device(drive->queue);
402 spin_unlock_irqrestore(&ide_lock,flags);
403 return 1;
404 }
405 }
406 } else if (sense_key == UNIT_ATTENTION) {
407 /* Media change. */
408 cdrom_saw_media_change (drive);
409
410 /* Arrange to retry the request.
411 But be sure to give up if we've retried
412 too many times. */
413 if (++rq->errors > ERROR_MAX)
414 do_end_request = 1;
415 } else if (sense_key == ILLEGAL_REQUEST ||
416 sense_key == DATA_PROTECT) {
417 /* No point in retrying after an illegal
418 request or data protect error.*/
dbe217af 419 ide_dump_status_no_sense (drive, "command error", stat);
1da177e4
LT
420 do_end_request = 1;
421 } else if (sense_key == MEDIUM_ERROR) {
422 /* No point in re-trying a zillion times on a bad
423 * sector... If we got here the error is not correctable */
dbe217af 424 ide_dump_status_no_sense (drive, "media error (bad sector)", stat);
1da177e4
LT
425 do_end_request = 1;
426 } else if (sense_key == BLANK_CHECK) {
427 /* Disk appears blank ?? */
dbe217af 428 ide_dump_status_no_sense (drive, "media error (blank)", stat);
1da177e4
LT
429 do_end_request = 1;
430 } else if ((err & ~ABRT_ERR) != 0) {
431 /* Go to the default handler
432 for other errors. */
433 ide_error(drive, "cdrom_decode_status", stat);
434 return 1;
435 } else if ((++rq->errors > ERROR_MAX)) {
436 /* We've racked up too many retries. Abort. */
437 do_end_request = 1;
438 }
439
dbe217af
AC
440 /* End a request through request sense analysis when we have
441 sense data. We need this in order to perform end of media
442 processing */
443
bbb89e3d
BZ
444 if (do_end_request)
445 goto end_request;
dbe217af 446
bbb89e3d
BZ
447 /*
448 * If we got a CHECK_CONDITION status,
449 * queue a request sense command.
450 */
451 if (stat & ERR_STAT)
452 cdrom_queue_request_sense(drive, NULL, NULL);
1da177e4
LT
453 } else {
454 blk_dump_rq_flags(rq, "ide-cd: bad rq");
455 cdrom_end_request(drive, 0);
456 }
457
458 /* Retry, or handle the next request. */
459 return 1;
bbb89e3d
BZ
460
461end_request:
462 if (stat & ERR_STAT) {
463 unsigned long flags;
464
465 spin_lock_irqsave(&ide_lock, flags);
466 blkdev_dequeue_request(rq);
467 HWGROUP(drive)->rq = NULL;
468 spin_unlock_irqrestore(&ide_lock, flags);
469
470 cdrom_queue_request_sense(drive, rq->sense, rq);
471 } else
472 cdrom_end_request(drive, 0);
473
474 return 1;
1da177e4
LT
475}
476
477static int cdrom_timer_expiry(ide_drive_t *drive)
478{
479 struct request *rq = HWGROUP(drive)->rq;
480 unsigned long wait = 0;
481
482 /*
483 * Some commands are *slow* and normally take a long time to
484 * complete. Usually we can use the ATAPI "disconnect" to bypass
485 * this, but not all commands/drives support that. Let
486 * ide_timer_expiry keep polling us for these.
487 */
488 switch (rq->cmd[0]) {
489 case GPCMD_BLANK:
490 case GPCMD_FORMAT_UNIT:
491 case GPCMD_RESERVE_RZONE_TRACK:
492 case GPCMD_CLOSE_TRACK:
493 case GPCMD_FLUSH_CACHE:
494 wait = ATAPI_WAIT_PC;
495 break;
496 default:
4aff5e23 497 if (!(rq->cmd_flags & REQ_QUIET))
1da177e4
LT
498 printk(KERN_INFO "ide-cd: cmd 0x%x timed out\n", rq->cmd[0]);
499 wait = 0;
500 break;
501 }
502 return wait;
503}
504
505/* Set up the device registers for transferring a packet command on DEV,
506 expecting to later transfer XFERLEN bytes. HANDLER is the routine
507 which actually transfers the command to the drive. If this is a
508 drq_interrupt device, this routine will arrange for HANDLER to be
509 called when the interrupt from the drive arrives. Otherwise, HANDLER
510 will be called immediately after the drive is prepared for the transfer. */
511
512static ide_startstop_t cdrom_start_packet_command(ide_drive_t *drive,
513 int xferlen,
514 ide_handler_t *handler)
515{
516 ide_startstop_t startstop;
517 struct cdrom_info *info = drive->driver_data;
518 ide_hwif_t *hwif = drive->hwif;
519
520 /* Wait for the controller to be idle. */
521 if (ide_wait_stat(&startstop, drive, 0, BUSY_STAT, WAIT_READY))
522 return startstop;
523
3a6a3549 524 /* FIXME: for Virtual DMA we must check harder */
1da177e4
LT
525 if (info->dma)
526 info->dma = !hwif->dma_setup(drive);
527
528 /* Set up the controller registers. */
2fc57388
BZ
529 ide_pktcmd_tf_load(drive, IDE_TFLAG_OUT_NSECT | IDE_TFLAG_OUT_LBAL |
530 IDE_TFLAG_NO_SELECT_MASK, xferlen, info->dma);
4fe67178 531
2bc4cf2d 532 if (info->cd_flags & IDE_CD_FLAG_DRQ_INTERRUPT) {
f0dd8712
AL
533 /* waiting for CDB interrupt, not DMA yet. */
534 if (info->dma)
535 drive->waiting_for_dma = 0;
536
1da177e4
LT
537 /* packet command */
538 ide_execute_command(drive, WIN_PACKETCMD, handler, ATAPI_WAIT_PC, cdrom_timer_expiry);
539 return ide_started;
540 } else {
541 unsigned long flags;
542
543 /* packet command */
544 spin_lock_irqsave(&ide_lock, flags);
545 hwif->OUTBSYNC(drive, WIN_PACKETCMD, IDE_COMMAND_REG);
546 ndelay(400);
547 spin_unlock_irqrestore(&ide_lock, flags);
548
549 return (*handler) (drive);
550 }
551}
552
553/* Send a packet command to DRIVE described by CMD_BUF and CMD_LEN.
554 The device registers must have already been prepared
555 by cdrom_start_packet_command.
556 HANDLER is the interrupt handler to call when the command completes
557 or there's data ready. */
1da177e4
LT
558#define ATAPI_MIN_CDB_BYTES 12
559static ide_startstop_t cdrom_transfer_packet_command (ide_drive_t *drive,
560 struct request *rq,
561 ide_handler_t *handler)
562{
563 ide_hwif_t *hwif = drive->hwif;
564 int cmd_len;
565 struct cdrom_info *info = drive->driver_data;
566 ide_startstop_t startstop;
567
2bc4cf2d 568 if (info->cd_flags & IDE_CD_FLAG_DRQ_INTERRUPT) {
1da177e4
LT
569 /* Here we should have been called after receiving an interrupt
570 from the device. DRQ should how be set. */
571
572 /* Check for errors. */
573 if (cdrom_decode_status(drive, DRQ_STAT, NULL))
574 return ide_stopped;
f0dd8712
AL
575
576 /* Ok, next interrupt will be DMA interrupt. */
577 if (info->dma)
578 drive->waiting_for_dma = 1;
1da177e4
LT
579 } else {
580 /* Otherwise, we must wait for DRQ to get set. */
581 if (ide_wait_stat(&startstop, drive, DRQ_STAT,
582 BUSY_STAT, WAIT_READY))
583 return startstop;
584 }
585
586 /* Arm the interrupt handler. */
587 ide_set_handler(drive, handler, rq->timeout, cdrom_timer_expiry);
588
589 /* ATAPI commands get padded out to 12 bytes minimum */
590 cmd_len = COMMAND_SIZE(rq->cmd[0]);
591 if (cmd_len < ATAPI_MIN_CDB_BYTES)
592 cmd_len = ATAPI_MIN_CDB_BYTES;
593
594 /* Send the command to the device. */
595 HWIF(drive)->atapi_output_bytes(drive, rq->cmd, cmd_len);
596
597 /* Start the DMA if need be */
598 if (info->dma)
599 hwif->dma_start(drive);
600
601 return ide_started;
602}
603
604/****************************************************************************
605 * Block read functions.
606 */
607
5a5222d9
BZ
608static void ide_cd_pad_transfer(ide_drive_t *drive, xfer_func_t *xf, int len)
609{
610 while (len > 0) {
611 int dum = 0;
612 xf(drive, &dum, sizeof(dum));
613 len -= sizeof(dum);
614 }
615}
616
b4ab726c
BZ
617static void ide_cd_drain_data(ide_drive_t *drive, int nsects)
618{
619 while (nsects > 0) {
620 static char dum[SECTOR_SIZE];
621
622 drive->hwif->atapi_input_bytes(drive, dum, sizeof(dum));
623 nsects--;
624 }
625}
626
1da177e4
LT
627/*
628 * Buffer up to SECTORS_TO_TRANSFER sectors from the drive in our sector
629 * buffer. Once the first sector is added, any subsequent sectors are
630 * assumed to be continuous (until the buffer is cleared). For the first
631 * sector added, SECTOR is its sector number. (SECTOR is then ignored until
632 * the buffer is cleared.)
633 */
634static void cdrom_buffer_sectors (ide_drive_t *drive, unsigned long sector,
635 int sectors_to_transfer)
636{
637 struct cdrom_info *info = drive->driver_data;
638
639 /* Number of sectors to read into the buffer. */
640 int sectors_to_buffer = min_t(int, sectors_to_transfer,
641 (SECTOR_BUFFER_SIZE >> SECTOR_BITS) -
642 info->nsectors_buffered);
643
644 char *dest;
645
646 /* If we couldn't get a buffer, don't try to buffer anything... */
647 if (info->buffer == NULL)
648 sectors_to_buffer = 0;
649
650 /* If this is the first sector in the buffer, remember its number. */
651 if (info->nsectors_buffered == 0)
652 info->sector_buffered = sector;
653
654 /* Read the data into the buffer. */
655 dest = info->buffer + info->nsectors_buffered * SECTOR_SIZE;
656 while (sectors_to_buffer > 0) {
657 HWIF(drive)->atapi_input_bytes(drive, dest, SECTOR_SIZE);
658 --sectors_to_buffer;
659 --sectors_to_transfer;
660 ++info->nsectors_buffered;
661 dest += SECTOR_SIZE;
662 }
663
664 /* Throw away any remaining data. */
b4ab726c 665 ide_cd_drain_data(drive, sectors_to_transfer);
1da177e4
LT
666}
667
668/*
669 * Check the contents of the interrupt reason register from the cdrom
670 * and attempt to recover if there are problems. Returns 0 if everything's
671 * ok; nonzero if the request has been terminated.
672 */
858119e1 673static
0d6f7e3a 674int ide_cd_check_ireason(ide_drive_t *drive, int len, int ireason, int rw)
1da177e4 675{
0d6f7e3a
BZ
676 /*
677 * ireason == 0: the drive wants to receive data from us
678 * ireason == 2: the drive is expecting to transfer data to us
679 */
680 if (ireason == (!rw << 1))
1da177e4 681 return 0;
0d6f7e3a 682 else if (ireason == (rw << 1)) {
5a5222d9 683 ide_hwif_t *hwif = drive->hwif;
0d6f7e3a 684 xfer_func_t *xf;
5a5222d9 685
0d6f7e3a 686 /* Whoops... */
35379c07
BZ
687 printk(KERN_ERR "%s: %s: wrong transfer direction!\n",
688 drive->name, __FUNCTION__);
1da177e4 689
0d6f7e3a
BZ
690 xf = rw ? hwif->atapi_output_bytes : hwif->atapi_input_bytes;
691 ide_cd_pad_transfer(drive, xf, len);
692 } else if (rw == 0 && ireason == 1) {
1da177e4
LT
693 /* Some drives (ASUS) seem to tell us that status
694 * info is available. just get it and ignore.
695 */
c47137a9 696 (void)ide_read_status(drive);
1da177e4
LT
697 return 0;
698 } else {
699 /* Drive wants a command packet, or invalid ireason... */
35379c07
BZ
700 printk(KERN_ERR "%s: %s: bad interrupt reason 0x%02x\n",
701 drive->name, __FUNCTION__, ireason);
1da177e4
LT
702 }
703
704 cdrom_end_request(drive, 0);
705 return -1;
706}
707
64814f23
BZ
708/*
709 * Assume that the drive will always provide data in multiples of at least
710 * SECTOR_SIZE, as it gets hairy to keep track of the transfers otherwise.
711 */
712static int ide_cd_check_transfer_size(ide_drive_t *drive, int len)
713{
714 struct cdrom_info *cd = drive->driver_data;
715
716 if ((len % SECTOR_SIZE) == 0)
717 return 0;
718
719 printk(KERN_ERR "%s: %s: Bad transfer size %d\n",
720 drive->name, __FUNCTION__, len);
721
722 if (cd->cd_flags & IDE_CD_FLAG_LIMIT_NFRAMES)
723 printk(KERN_ERR " This drive is not supported by "
724 "this version of the driver\n");
725 else {
726 printk(KERN_ERR " Trying to limit transfer sizes\n");
727 cd->cd_flags |= IDE_CD_FLAG_LIMIT_NFRAMES;
728 }
729
730 return 1;
731}
732
1da177e4
LT
733/*
734 * Try to satisfy some of the current read request from our cached data.
735 * Returns nonzero if the request has been completed, zero otherwise.
736 */
737static int cdrom_read_from_buffer (ide_drive_t *drive)
738{
739 struct cdrom_info *info = drive->driver_data;
740 struct request *rq = HWGROUP(drive)->rq;
741 unsigned short sectors_per_frame;
742
743 sectors_per_frame = queue_hardsect_size(drive->queue) >> SECTOR_BITS;
744
745 /* Can't do anything if there's no buffer. */
746 if (info->buffer == NULL) return 0;
747
748 /* Loop while this request needs data and the next block is present
749 in our cache. */
750 while (rq->nr_sectors > 0 &&
751 rq->sector >= info->sector_buffered &&
752 rq->sector < info->sector_buffered + info->nsectors_buffered) {
753 if (rq->current_nr_sectors == 0)
754 cdrom_end_request(drive, 1);
755
756 memcpy (rq->buffer,
757 info->buffer +
758 (rq->sector - info->sector_buffered) * SECTOR_SIZE,
759 SECTOR_SIZE);
760 rq->buffer += SECTOR_SIZE;
761 --rq->current_nr_sectors;
762 --rq->nr_sectors;
763 ++rq->sector;
764 }
765
766 /* If we've satisfied the current request,
767 terminate it successfully. */
768 if (rq->nr_sectors == 0) {
769 cdrom_end_request(drive, 1);
770 return -1;
771 }
772
773 /* Move on to the next buffer if needed. */
774 if (rq->current_nr_sectors == 0)
775 cdrom_end_request(drive, 1);
776
777 /* If this condition does not hold, then the kluge i use to
778 represent the number of sectors to skip at the start of a transfer
779 will fail. I think that this will never happen, but let's be
780 paranoid and check. */
781 if (rq->current_nr_sectors < bio_cur_sectors(rq->bio) &&
782 (rq->sector & (sectors_per_frame - 1))) {
783 printk(KERN_ERR "%s: cdrom_read_from_buffer: buffer botch (%ld)\n",
784 drive->name, (long)rq->sector);
785 cdrom_end_request(drive, 0);
786 return -1;
787 }
788
789 return 0;
790}
791
0041e7c6 792static ide_startstop_t cdrom_newpc_intr(ide_drive_t *);
94f5a86d 793
1da177e4 794/*
29f3aaca
BZ
795 * Routine to send a read/write packet command to the drive.
796 * This is usually called directly from cdrom_start_{read,write}().
1da177e4
LT
797 * However, for drq_interrupt devices, it is called from an interrupt
798 * when the drive is ready to accept the command.
799 */
29f3aaca 800static ide_startstop_t cdrom_start_rw_cont(ide_drive_t *drive)
1da177e4
LT
801{
802 struct request *rq = HWGROUP(drive)->rq;
1da177e4 803
29f3aaca
BZ
804 if (rq_data_dir(rq) == READ) {
805 unsigned short sectors_per_frame =
806 queue_hardsect_size(drive->queue) >> SECTOR_BITS;
807 int nskip = rq->sector & (sectors_per_frame - 1);
1da177e4 808
29f3aaca
BZ
809 /*
810 * If the requested sector doesn't start on a frame boundary,
811 * we must adjust the start of the transfer so that it does,
812 * and remember to skip the first few sectors.
813 *
814 * If the rq->current_nr_sectors field is larger than the size
815 * of the buffer, it will mean that we're to skip a number of
816 * sectors equal to the amount by which rq->current_nr_sectors
817 * is larger than the buffer size.
818 */
819 if (nskip > 0) {
820 /* Sanity check... */
821 if (rq->current_nr_sectors !=
822 bio_cur_sectors(rq->bio)) {
823 printk(KERN_ERR "%s: %s: buffer botch (%u)\n",
824 drive->name, __FUNCTION__,
825 rq->current_nr_sectors);
826 cdrom_end_request(drive, 0);
827 return ide_stopped;
828 }
829 rq->current_nr_sectors += nskip;
1da177e4 830 }
1da177e4 831 }
29f3aaca
BZ
832#if 0
833 else
834 /* the immediate bit */
835 rq->cmd[1] = 1 << 3;
836#endif
1da177e4
LT
837 /* Set up the command */
838 rq->timeout = ATAPI_WAIT_PC;
839
840 /* Send the command to the drive and return. */
0041e7c6 841 return cdrom_transfer_packet_command(drive, rq, cdrom_newpc_intr);
1da177e4
LT
842}
843
1da177e4
LT
844#define IDECD_SEEK_THRESHOLD (1000) /* 1000 blocks */
845#define IDECD_SEEK_TIMER (5 * WAIT_MIN_SLEEP) /* 100 ms */
846#define IDECD_SEEK_TIMEOUT (2 * WAIT_CMD) /* 20 sec */
847
848static ide_startstop_t cdrom_seek_intr (ide_drive_t *drive)
849{
850 struct cdrom_info *info = drive->driver_data;
851 int stat;
852 static int retry = 10;
853
854 if (cdrom_decode_status(drive, 0, &stat))
855 return ide_stopped;
4fe67178 856
2bc4cf2d 857 info->cd_flags |= IDE_CD_FLAG_SEEKING;
1da177e4
LT
858
859 if (retry && time_after(jiffies, info->start_seek + IDECD_SEEK_TIMER)) {
860 if (--retry == 0) {
861 /*
862 * this condition is far too common, to bother
863 * users about it
864 */
865 /* printk("%s: disabled DSC seek overlap\n", drive->name);*/
866 drive->dsc_overlap = 0;
867 }
868 }
869 return ide_stopped;
870}
871
872static ide_startstop_t cdrom_start_seek_continuation (ide_drive_t *drive)
873{
874 struct request *rq = HWGROUP(drive)->rq;
875 sector_t frame = rq->sector;
876
877 sector_div(frame, queue_hardsect_size(drive->queue) >> SECTOR_BITS);
878
879 memset(rq->cmd, 0, sizeof(rq->cmd));
880 rq->cmd[0] = GPCMD_SEEK;
881 put_unaligned(cpu_to_be32(frame), (unsigned int *) &rq->cmd[2]);
882
883 rq->timeout = ATAPI_WAIT_PC;
884 return cdrom_transfer_packet_command(drive, rq, &cdrom_seek_intr);
885}
886
887static ide_startstop_t cdrom_start_seek (ide_drive_t *drive, unsigned int block)
888{
889 struct cdrom_info *info = drive->driver_data;
890
891 info->dma = 0;
1da177e4
LT
892 info->start_seek = jiffies;
893 return cdrom_start_packet_command(drive, 0, cdrom_start_seek_continuation);
894}
895
896/* Fix up a possibly partially-processed request so that we can
897 start it over entirely, or even put it back on the request queue. */
898static void restore_request (struct request *rq)
899{
900 if (rq->buffer != bio_data(rq->bio)) {
901 sector_t n = (rq->buffer - (char *) bio_data(rq->bio)) / SECTOR_SIZE;
902
903 rq->buffer = bio_data(rq->bio);
904 rq->nr_sectors += n;
905 rq->sector -= n;
906 }
907 rq->hard_cur_sectors = rq->current_nr_sectors = bio_cur_sectors(rq->bio);
908 rq->hard_nr_sectors = rq->nr_sectors;
909 rq->hard_sector = rq->sector;
910 rq->q->prep_rq_fn(rq->q, rq);
911}
912
1da177e4
LT
913/****************************************************************************
914 * Execute all other packet commands.
915 */
916
8ee69f5a
BZ
917static void ide_cd_request_sense_fixup(struct request *rq)
918{
919 /*
920 * Some of the trailing request sense fields are optional,
921 * and some drives don't send them. Sigh.
922 */
923 if (rq->cmd[0] == GPCMD_REQUEST_SENSE &&
924 rq->data_len > 0 && rq->data_len <= 5)
925 while (rq->data_len > 0) {
926 *(u8 *)rq->data++ = 0;
927 --rq->data_len;
928 }
929}
930
17802998 931int ide_cd_queue_pc(ide_drive_t *drive, struct request *rq)
1da177e4
LT
932{
933 struct request_sense sense;
934 int retries = 10;
4aff5e23 935 unsigned int flags = rq->cmd_flags;
1da177e4
LT
936
937 if (rq->sense == NULL)
938 rq->sense = &sense;
939
940 /* Start of retry loop. */
941 do {
942 int error;
943 unsigned long time = jiffies;
4aff5e23 944 rq->cmd_flags = flags;
1da177e4
LT
945
946 error = ide_do_drive_cmd(drive, rq, ide_wait);
947 time = jiffies - time;
948
949 /* FIXME: we should probably abort/retry or something
950 * in case of failure */
4aff5e23 951 if (rq->cmd_flags & REQ_FAILED) {
1da177e4
LT
952 /* The request failed. Retry if it was due to a unit
953 attention status
954 (usually means media was changed). */
955 struct request_sense *reqbuf = rq->sense;
956
957 if (reqbuf->sense_key == UNIT_ATTENTION)
958 cdrom_saw_media_change(drive);
959 else if (reqbuf->sense_key == NOT_READY &&
960 reqbuf->asc == 4 && reqbuf->ascq != 4) {
961 /* The drive is in the process of loading
962 a disk. Retry, but wait a little to give
963 the drive time to complete the load. */
964 ssleep(2);
965 } else {
966 /* Otherwise, don't retry. */
967 retries = 0;
968 }
969 --retries;
970 }
971
972 /* End of retry loop. */
4aff5e23 973 } while ((rq->cmd_flags & REQ_FAILED) && retries >= 0);
1da177e4
LT
974
975 /* Return an error if the command failed. */
4aff5e23 976 return (rq->cmd_flags & REQ_FAILED) ? -EIO : 0;
1da177e4
LT
977}
978
aaa04c28
KU
979/*
980 * Called from blk_end_request_callback() after the data of the request
981 * is completed and before the request is completed.
982 * By returning value '1', blk_end_request_callback() returns immediately
983 * without completing the request.
984 */
985static int cdrom_newpc_intr_dummy_cb(struct request *rq)
986{
987 return 1;
988}
989
1da177e4
LT
990static ide_startstop_t cdrom_newpc_intr(ide_drive_t *drive)
991{
992 struct cdrom_info *info = drive->driver_data;
993 struct request *rq = HWGROUP(drive)->rq;
1da177e4 994 xfer_func_t *xferfunc;
0041e7c6 995 ide_expiry_t *expiry = NULL;
ff1bfbc1
BZ
996 int dma_error = 0, dma, stat, ireason, len, thislen, uptodate = 0;
997 int write = (rq_data_dir(rq) == WRITE) ? 1 : 0;
998 unsigned int timeout;
999 u8 lowcyl, highcyl;
1da177e4
LT
1000
1001 /* Check for errors. */
1da177e4
LT
1002 dma = info->dma;
1003 if (dma) {
1004 info->dma = 0;
1005 dma_error = HWIF(drive)->ide_dma_end(drive);
eba15fba
BZ
1006 if (dma_error) {
1007 printk(KERN_ERR "%s: DMA %s error\n", drive->name,
ff1bfbc1 1008 write ? "write" : "read");
eba15fba
BZ
1009 ide_dma_off(drive);
1010 }
1da177e4
LT
1011 }
1012
1013 if (cdrom_decode_status(drive, 0, &stat))
1014 return ide_stopped;
1015
1016 /*
1017 * using dma, transfer is complete now
1018 */
1019 if (dma) {
eba15fba 1020 if (dma_error)
1da177e4 1021 return ide_error(drive, "dma error", stat);
0041e7c6
BZ
1022 if (blk_fs_request(rq)) {
1023 ide_end_request(drive, 1, rq->nr_sectors);
1024 return ide_stopped;
1025 }
ff1bfbc1 1026 goto end_request;
1da177e4
LT
1027 }
1028
1029 /*
1030 * ok we fall to pio :/
1031 */
1032 ireason = HWIF(drive)->INB(IDE_IREASON_REG) & 0x3;
1033 lowcyl = HWIF(drive)->INB(IDE_BCOUNTL_REG);
1034 highcyl = HWIF(drive)->INB(IDE_BCOUNTH_REG);
1035
1036 len = lowcyl + (256 * highcyl);
0041e7c6
BZ
1037
1038 thislen = blk_fs_request(rq) ? len : rq->data_len;
1da177e4
LT
1039 if (thislen > len)
1040 thislen = len;
1041
1042 /*
1043 * If DRQ is clear, the command has completed.
1044 */
aaa04c28 1045 if ((stat & DRQ_STAT) == 0) {
0041e7c6
BZ
1046 if (blk_fs_request(rq)) {
1047 /*
1048 * If we're not done reading/writing, complain.
1049 * Otherwise, complete the command normally.
1050 */
1051 uptodate = 1;
1052 if (rq->current_nr_sectors > 0) {
1053 printk(KERN_ERR "%s: %s: data underrun "
1054 "(%d blocks)\n",
1055 drive->name, __FUNCTION__,
1056 rq->current_nr_sectors);
1057 if (!write)
1058 rq->cmd_flags |= REQ_FAILED;
1059 uptodate = 0;
1060 }
1061 cdrom_end_request(drive, uptodate);
1062 return ide_stopped;
1063 } else if (!blk_pc_request(rq)) {
ff1bfbc1
BZ
1064 ide_cd_request_sense_fixup(rq);
1065 /* Complain if we still have data left to transfer. */
1066 uptodate = rq->data_len ? 0 : 1;
1067 }
1068 goto end_request;
aaa04c28 1069 }
1da177e4
LT
1070
1071 /*
1072 * check which way to transfer data
1073 */
0d6f7e3a
BZ
1074 if (blk_fs_request(rq) || blk_pc_request(rq)) {
1075 if (ide_cd_check_ireason(drive, len, ireason, write))
1da177e4 1076 return ide_stopped;
0041e7c6 1077
0d6f7e3a 1078 if (blk_fs_request(rq) && write == 0) {
0041e7c6
BZ
1079 int nskip;
1080
1081 if (ide_cd_check_transfer_size(drive, len)) {
1082 cdrom_end_request(drive, 0);
1083 return ide_stopped;
1084 }
1085
1086 /*
1087 * First, figure out if we need to bit-bucket
1088 * any of the leading sectors.
1089 */
1090 nskip = min_t(int, rq->current_nr_sectors
1091 - bio_cur_sectors(rq->bio),
1092 thislen >> 9);
1093 if (nskip > 0) {
1094 ide_cd_drain_data(drive, nskip);
1095 rq->current_nr_sectors -= nskip;
1096 thislen -= (nskip << 9);
1097 }
1098 }
ff1bfbc1 1099 }
1da177e4 1100
ff1bfbc1
BZ
1101 if (ireason == 0) {
1102 write = 1;
1103 xferfunc = HWIF(drive)->atapi_output_bytes;
0041e7c6
BZ
1104 } else if (ireason == 2 || (ireason == 1 &&
1105 (blk_fs_request(rq) || blk_pc_request(rq)))) {
ff1bfbc1 1106 write = 0;
1da177e4 1107 xferfunc = HWIF(drive)->atapi_input_bytes;
ff1bfbc1
BZ
1108 } else {
1109 printk(KERN_ERR "%s: %s: The drive "
1110 "appears confused (ireason = 0x%02x). "
1111 "Trying to recover by ending request.\n",
1112 drive->name, __FUNCTION__, ireason);
1113 goto end_request;
1da177e4
LT
1114 }
1115
1116 /*
1117 * transfer data
1118 */
1119 while (thislen > 0) {
0041e7c6 1120 u8 *ptr = blk_fs_request(rq) ? NULL : rq->data;
a11e77db 1121 int blen = rq->data_len;
1da177e4
LT
1122
1123 /*
1124 * bio backed?
1125 */
1126 if (rq->bio) {
0041e7c6
BZ
1127 if (blk_fs_request(rq)) {
1128 ptr = rq->buffer;
1129 blen = rq->current_nr_sectors << 9;
1130 } else {
1131 ptr = bio_data(rq->bio);
1132 blen = bio_iovec(rq->bio)->bv_len;
1133 }
1da177e4
LT
1134 }
1135
1136 if (!ptr) {
0041e7c6
BZ
1137 if (blk_fs_request(rq) && !write)
1138 /*
1139 * If the buffers are full, cache the rest
1140 * of the data in our internal buffer.
1141 */
1142 cdrom_buffer_sectors(drive, rq->sector,
1143 thislen >> 9);
1144 else {
1145 printk(KERN_ERR "%s: confused, missing data\n",
1146 drive->name);
1147 blk_dump_rq_flags(rq, rq_data_dir(rq)
1148 ? "cdrom_newpc_intr, write"
1149 : "cdrom_newpc_intr, read");
1150 }
1da177e4
LT
1151 break;
1152 }
1153
1154 if (blen > thislen)
1155 blen = thislen;
1156
1157 xferfunc(drive, ptr, blen);
1158
1159 thislen -= blen;
1160 len -= blen;
1da177e4 1161
0041e7c6
BZ
1162 if (blk_fs_request(rq)) {
1163 rq->buffer += blen;
1164 rq->nr_sectors -= (blen >> 9);
1165 rq->current_nr_sectors -= (blen >> 9);
1166 rq->sector += (blen >> 9);
1167
1168 if (rq->current_nr_sectors == 0 && rq->nr_sectors)
1169 cdrom_end_request(drive, 1);
1170 } else {
1171 rq->data_len -= blen;
1172
aaa04c28
KU
1173 /*
1174 * The request can't be completed until DRQ is cleared.
1175 * So complete the data, but don't complete the request
1176 * using the dummy function for the callback feature
1177 * of blk_end_request_callback().
1178 */
0041e7c6
BZ
1179 if (rq->bio)
1180 blk_end_request_callback(rq, 0, blen,
aaa04c28 1181 cdrom_newpc_intr_dummy_cb);
0041e7c6
BZ
1182 else
1183 rq->data += blen;
1184 }
1da177e4
LT
1185 }
1186
ff1bfbc1
BZ
1187 if (write && blk_sense_request(rq))
1188 rq->sense_len += thislen;
1189
1da177e4
LT
1190 /*
1191 * pad, if necessary
1192 */
0041e7c6 1193 if (!blk_fs_request(rq) && len > 0)
5a5222d9 1194 ide_cd_pad_transfer(drive, xferfunc, len);
1da177e4 1195
ff1bfbc1
BZ
1196 if (blk_pc_request(rq)) {
1197 timeout = rq->timeout;
ff1bfbc1
BZ
1198 } else {
1199 timeout = ATAPI_WAIT_PC;
0041e7c6
BZ
1200 if (!blk_fs_request(rq))
1201 expiry = cdrom_timer_expiry;
ff1bfbc1
BZ
1202 }
1203
1204 ide_set_handler(drive, cdrom_newpc_intr, timeout, expiry);
1da177e4 1205 return ide_started;
ff1bfbc1
BZ
1206
1207end_request:
1208 if (blk_pc_request(rq)) {
1209 unsigned long flags;
14e04c3f
KU
1210 unsigned int dlen = rq->data_len;
1211
1212 if (dma)
1213 rq->data_len = 0;
ff1bfbc1
BZ
1214
1215 spin_lock_irqsave(&ide_lock, flags);
14e04c3f 1216 if (__blk_end_request(rq, 0, dlen))
ff1bfbc1
BZ
1217 BUG();
1218 HWGROUP(drive)->rq = NULL;
1219 spin_unlock_irqrestore(&ide_lock, flags);
1220 } else {
1221 if (!uptodate)
1222 rq->cmd_flags |= REQ_FAILED;
1223 cdrom_end_request(drive, uptodate);
1224 }
1225 return ide_stopped;
1da177e4
LT
1226}
1227
21ea1f0f 1228static ide_startstop_t cdrom_start_rw(ide_drive_t *drive, struct request *rq)
1da177e4 1229{
21ea1f0f
BZ
1230 struct cdrom_info *cd = drive->driver_data;
1231 int write = rq_data_dir(rq) == WRITE;
1232 unsigned short sectors_per_frame =
1233 queue_hardsect_size(drive->queue) >> SECTOR_BITS;
1da177e4 1234
21ea1f0f
BZ
1235 if (write) {
1236 /*
1237 * disk has become write protected
1238 */
1239 if (cd->disk->policy) {
1240 cdrom_end_request(drive, 0);
1241 return ide_stopped;
1242 }
1243 } else {
1244 /*
1245 * We may be retrying this request after an error. Fix up any
1246 * weirdness which might be present in the request packet.
1247 */
1248 restore_request(rq);
1249
1250 /* Satisfy whatever we can of this request from our cache. */
1251 if (cdrom_read_from_buffer(drive))
1252 return ide_stopped;
1da177e4
LT
1253 }
1254
1255 /*
21ea1f0f 1256 * use DMA, if possible / writes *must* be hardware frame aligned
1da177e4 1257 */
21ea1f0f
BZ
1258 if ((rq->nr_sectors & (sectors_per_frame - 1)) ||
1259 (rq->sector & (sectors_per_frame - 1))) {
1260 if (write) {
1261 cdrom_end_request(drive, 0);
1262 return ide_stopped;
1263 }
1264 cd->dma = 0;
1265 } else
1266 cd->dma = drive->using_dma;
1da177e4 1267
21ea1f0f
BZ
1268 /* Clear the local sector buffer. */
1269 cd->nsectors_buffered = 0;
1da177e4 1270
21ea1f0f
BZ
1271 if (write)
1272 cd->devinfo.media_written = 1;
1da177e4 1273
21ea1f0f 1274 /* Start sending the read/write request to the drive. */
29f3aaca 1275 return cdrom_start_packet_command(drive, 32768, cdrom_start_rw_cont);
1da177e4
LT
1276}
1277
1278static ide_startstop_t cdrom_do_newpc_cont(ide_drive_t *drive)
1279{
1280 struct request *rq = HWGROUP(drive)->rq;
1281
1282 if (!rq->timeout)
1283 rq->timeout = ATAPI_WAIT_PC;
1284
1285 return cdrom_transfer_packet_command(drive, rq, cdrom_newpc_intr);
1286}
1287
1288static ide_startstop_t cdrom_do_block_pc(ide_drive_t *drive, struct request *rq)
1289{
1290 struct cdrom_info *info = drive->driver_data;
1291
c9f56a80
BZ
1292 if (blk_pc_request(rq))
1293 rq->cmd_flags |= REQ_QUIET;
1294 else
1295 rq->cmd_flags &= ~REQ_FAILED;
1da177e4
LT
1296
1297 info->dma = 0;
1da177e4
LT
1298
1299 /*
1300 * sg request
1301 */
1302 if (rq->bio) {
1303 int mask = drive->queue->dma_alignment;
1304 unsigned long addr = (unsigned long) page_address(bio_page(rq->bio));
1305
1da177e4
LT
1306 info->dma = drive->using_dma;
1307
1308 /*
1309 * check if dma is safe
5d9e4ea5
LT
1310 *
1311 * NOTE! The "len" and "addr" checks should possibly have
1312 * separate masks.
1da177e4 1313 */
4e7c6816 1314 if ((rq->data_len & 15) || (addr & mask))
1da177e4
LT
1315 info->dma = 0;
1316 }
1317
1318 /* Start sending the command to the drive. */
1319 return cdrom_start_packet_command(drive, rq->data_len, cdrom_do_newpc_cont);
1320}
1321
1322/****************************************************************************
1323 * cdrom driver request routine.
1324 */
1325static ide_startstop_t
1326ide_do_rw_cdrom (ide_drive_t *drive, struct request *rq, sector_t block)
1327{
1328 ide_startstop_t action;
1329 struct cdrom_info *info = drive->driver_data;
1330
1331 if (blk_fs_request(rq)) {
2bc4cf2d 1332 if (info->cd_flags & IDE_CD_FLAG_SEEKING) {
1da177e4 1333 unsigned long elapsed = jiffies - info->start_seek;
c47137a9 1334 int stat = ide_read_status(drive);
1da177e4
LT
1335
1336 if ((stat & SEEK_STAT) != SEEK_STAT) {
1337 if (elapsed < IDECD_SEEK_TIMEOUT) {
1338 ide_stall_queue(drive, IDECD_SEEK_TIMER);
1339 return ide_stopped;
1340 }
1341 printk (KERN_ERR "%s: DSC timeout\n", drive->name);
1342 }
2bc4cf2d 1343 info->cd_flags &= ~IDE_CD_FLAG_SEEKING;
1da177e4
LT
1344 }
1345 if ((rq_data_dir(rq) == READ) && IDE_LARGE_SEEK(info->last_block, block, IDECD_SEEK_THRESHOLD) && drive->dsc_overlap) {
1346 action = cdrom_start_seek(drive, block);
21ea1f0f
BZ
1347 } else
1348 action = cdrom_start_rw(drive, rq);
1da177e4
LT
1349 info->last_block = block;
1350 return action;
c9f56a80 1351 } else if (blk_sense_request(rq) || blk_pc_request(rq) ||
cea2885a 1352 rq->cmd_type == REQ_TYPE_ATA_PC) {
1da177e4 1353 return cdrom_do_block_pc(drive, rq);
4aff5e23 1354 } else if (blk_special_request(rq)) {
1da177e4
LT
1355 /*
1356 * right now this can only be a reset...
1357 */
1358 cdrom_end_request(drive, 1);
1359 return ide_stopped;
1360 }
1361
1362 blk_dump_rq_flags(rq, "ide-cd bad flags");
1363 cdrom_end_request(drive, 0);
1364 return ide_stopped;
1365}
1366
1367
1368
1369/****************************************************************************
1370 * Ioctl handling.
1371 *
1372 * Routines which queue packet commands take as a final argument a pointer
1373 * to a request_sense struct. If execution of the command results
1374 * in an error with a CHECK CONDITION status, this structure will be filled
1375 * with the results of the subsequent request sense command. The pointer
1376 * can also be NULL, in which case no sense information is returned.
1377 */
1378
1da177e4
LT
1379static
1380void msf_from_bcd (struct atapi_msf *msf)
1381{
9a6dc668
BZ
1382 msf->minute = BCD2BIN(msf->minute);
1383 msf->second = BCD2BIN(msf->second);
1384 msf->frame = BCD2BIN(msf->frame);
1da177e4
LT
1385}
1386
f9afd18b 1387int cdrom_check_status(ide_drive_t *drive, struct request_sense *sense)
1da177e4
LT
1388{
1389 struct request req;
1390 struct cdrom_info *info = drive->driver_data;
1391 struct cdrom_device_info *cdi = &info->devinfo;
1392
139c829d 1393 ide_cd_init_rq(drive, &req);
1da177e4
LT
1394
1395 req.sense = sense;
1396 req.cmd[0] = GPCMD_TEST_UNIT_READY;
4aff5e23 1397 req.cmd_flags |= REQ_QUIET;
1da177e4 1398
cdf6000d
BZ
1399 /*
1400 * Sanyo 3 CD changer uses byte 7 of TEST_UNIT_READY to
1401 * switch CDs instead of supporting the LOAD_UNLOAD opcode.
1402 */
1da177e4 1403 req.cmd[7] = cdi->sanyo_slot % 3;
1da177e4 1404
139c829d 1405 return ide_cd_queue_pc(drive, &req);
1da177e4
LT
1406}
1407
1da177e4
LT
1408static int cdrom_read_capacity(ide_drive_t *drive, unsigned long *capacity,
1409 unsigned long *sectors_per_frame,
1410 struct request_sense *sense)
1411{
1412 struct {
1413 __u32 lba;
1414 __u32 blocklen;
1415 } capbuf;
1416
1417 int stat;
1418 struct request req;
1419
139c829d 1420 ide_cd_init_rq(drive, &req);
1da177e4
LT
1421
1422 req.sense = sense;
1423 req.cmd[0] = GPCMD_READ_CDVD_CAPACITY;
1424 req.data = (char *)&capbuf;
1425 req.data_len = sizeof(capbuf);
4aff5e23 1426 req.cmd_flags |= REQ_QUIET;
1da177e4 1427
139c829d 1428 stat = ide_cd_queue_pc(drive, &req);
1da177e4
LT
1429 if (stat == 0) {
1430 *capacity = 1 + be32_to_cpu(capbuf.lba);
1431 *sectors_per_frame =
1432 be32_to_cpu(capbuf.blocklen) >> SECTOR_BITS;
1433 }
1434
1435 return stat;
1436}
1437
1438static int cdrom_read_tocentry(ide_drive_t *drive, int trackno, int msf_flag,
1439 int format, char *buf, int buflen,
1440 struct request_sense *sense)
1441{
1442 struct request req;
1443
139c829d 1444 ide_cd_init_rq(drive, &req);
1da177e4
LT
1445
1446 req.sense = sense;
1447 req.data = buf;
1448 req.data_len = buflen;
4aff5e23 1449 req.cmd_flags |= REQ_QUIET;
1da177e4
LT
1450 req.cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
1451 req.cmd[6] = trackno;
1452 req.cmd[7] = (buflen >> 8);
1453 req.cmd[8] = (buflen & 0xff);
1454 req.cmd[9] = (format << 6);
1455
1456 if (msf_flag)
1457 req.cmd[1] = 2;
1458
139c829d 1459 return ide_cd_queue_pc(drive, &req);
1da177e4
LT
1460}
1461
1da177e4 1462/* Try to read the entire TOC for the disk into our internal buffer. */
17802998 1463int ide_cd_read_toc(ide_drive_t *drive, struct request_sense *sense)
1da177e4
LT
1464{
1465 int stat, ntracks, i;
1466 struct cdrom_info *info = drive->driver_data;
1467 struct cdrom_device_info *cdi = &info->devinfo;
1468 struct atapi_toc *toc = info->toc;
1469 struct {
1470 struct atapi_toc_header hdr;
1471 struct atapi_toc_entry ent;
1472 } ms_tmp;
1473 long last_written;
1474 unsigned long sectors_per_frame = SECTORS_PER_FRAME;
1475
1476 if (toc == NULL) {
1477 /* Try to allocate space. */
2a91f3e5 1478 toc = kmalloc(sizeof(struct atapi_toc), GFP_KERNEL);
1da177e4
LT
1479 if (toc == NULL) {
1480 printk (KERN_ERR "%s: No cdrom TOC buffer!\n", drive->name);
1481 return -ENOMEM;
1482 }
2a91f3e5 1483 info->toc = toc;
1da177e4
LT
1484 }
1485
1486 /* Check to see if the existing data is still valid.
1487 If it is, just return. */
1488 (void) cdrom_check_status(drive, sense);
1489
2bc4cf2d 1490 if (info->cd_flags & IDE_CD_FLAG_TOC_VALID)
1da177e4
LT
1491 return 0;
1492
1493 /* Try to get the total cdrom capacity and sector size. */
1494 stat = cdrom_read_capacity(drive, &toc->capacity, &sectors_per_frame,
1495 sense);
1496 if (stat)
1497 toc->capacity = 0x1fffff;
1498
1499 set_capacity(info->disk, toc->capacity * sectors_per_frame);
dbe217af
AC
1500 /* Save a private copy of te TOC capacity for error handling */
1501 drive->probed_capacity = toc->capacity * sectors_per_frame;
1502
1da177e4
LT
1503 blk_queue_hardsect_size(drive->queue,
1504 sectors_per_frame << SECTOR_BITS);
1505
1506 /* First read just the header, so we know how long the TOC is. */
1507 stat = cdrom_read_tocentry(drive, 0, 1, 0, (char *) &toc->hdr,
1508 sizeof(struct atapi_toc_header), sense);
2a91f3e5
JJ
1509 if (stat)
1510 return stat;
1da177e4 1511
2bc4cf2d 1512 if (info->cd_flags & IDE_CD_FLAG_TOCTRACKS_AS_BCD) {
9a6dc668
BZ
1513 toc->hdr.first_track = BCD2BIN(toc->hdr.first_track);
1514 toc->hdr.last_track = BCD2BIN(toc->hdr.last_track);
1da177e4 1515 }
1da177e4
LT
1516
1517 ntracks = toc->hdr.last_track - toc->hdr.first_track + 1;
1518 if (ntracks <= 0)
1519 return -EIO;
1520 if (ntracks > MAX_TRACKS)
1521 ntracks = MAX_TRACKS;
1522
1523 /* Now read the whole schmeer. */
1524 stat = cdrom_read_tocentry(drive, toc->hdr.first_track, 1, 0,
1525 (char *)&toc->hdr,
1526 sizeof(struct atapi_toc_header) +
1527 (ntracks + 1) *
1528 sizeof(struct atapi_toc_entry), sense);
1529
1530 if (stat && toc->hdr.first_track > 1) {
1531 /* Cds with CDI tracks only don't have any TOC entries,
1532 despite of this the returned values are
1533 first_track == last_track = number of CDI tracks + 1,
1534 so that this case is indistinguishable from the same
1535 layout plus an additional audio track.
1536 If we get an error for the regular case, we assume
1537 a CDI without additional audio tracks. In this case
1538 the readable TOC is empty (CDI tracks are not included)
96de0e25 1539 and only holds the Leadout entry. Heiko Eißfeldt */
1da177e4
LT
1540 ntracks = 0;
1541 stat = cdrom_read_tocentry(drive, CDROM_LEADOUT, 1, 0,
1542 (char *)&toc->hdr,
1543 sizeof(struct atapi_toc_header) +
1544 (ntracks + 1) *
1545 sizeof(struct atapi_toc_entry),
1546 sense);
cdf6000d 1547 if (stat)
1da177e4 1548 return stat;
cdf6000d 1549
2bc4cf2d 1550 if (info->cd_flags & IDE_CD_FLAG_TOCTRACKS_AS_BCD) {
9a6dc668
BZ
1551 toc->hdr.first_track = (u8)BIN2BCD(CDROM_LEADOUT);
1552 toc->hdr.last_track = (u8)BIN2BCD(CDROM_LEADOUT);
cdf6000d 1553 } else {
1da177e4
LT
1554 toc->hdr.first_track = CDROM_LEADOUT;
1555 toc->hdr.last_track = CDROM_LEADOUT;
1556 }
1557 }
1558
1559 if (stat)
1560 return stat;
1561
7eb43fd2 1562 toc->hdr.toc_length = be16_to_cpu(toc->hdr.toc_length);
1da177e4 1563
2bc4cf2d 1564 if (info->cd_flags & IDE_CD_FLAG_TOCTRACKS_AS_BCD) {
9a6dc668
BZ
1565 toc->hdr.first_track = BCD2BIN(toc->hdr.first_track);
1566 toc->hdr.last_track = BCD2BIN(toc->hdr.last_track);
1da177e4 1567 }
1da177e4 1568
cdf6000d 1569 for (i = 0; i <= ntracks; i++) {
2bc4cf2d
BZ
1570 if (info->cd_flags & IDE_CD_FLAG_TOCADDR_AS_BCD) {
1571 if (info->cd_flags & IDE_CD_FLAG_TOCTRACKS_AS_BCD)
9a6dc668 1572 toc->ent[i].track = BCD2BIN(toc->ent[i].track);
1da177e4
LT
1573 msf_from_bcd(&toc->ent[i].addr.msf);
1574 }
1da177e4
LT
1575 toc->ent[i].addr.lba = msf_to_lba (toc->ent[i].addr.msf.minute,
1576 toc->ent[i].addr.msf.second,
1577 toc->ent[i].addr.msf.frame);
1578 }
1579
1580 /* Read the multisession information. */
1581 if (toc->hdr.first_track != CDROM_LEADOUT) {
1582 /* Read the multisession information. */
1583 stat = cdrom_read_tocentry(drive, 0, 0, 1, (char *)&ms_tmp,
1584 sizeof(ms_tmp), sense);
2a91f3e5
JJ
1585 if (stat)
1586 return stat;
1da177e4
LT
1587
1588 toc->last_session_lba = be32_to_cpu(ms_tmp.ent.addr.lba);
1589 } else {
1590 ms_tmp.hdr.first_track = ms_tmp.hdr.last_track = CDROM_LEADOUT;
1591 toc->last_session_lba = msf_to_lba(0, 2, 0); /* 0m 2s 0f */
1592 }
1593
2bc4cf2d 1594 if (info->cd_flags & IDE_CD_FLAG_TOCADDR_AS_BCD) {
1da177e4
LT
1595 /* Re-read multisession information using MSF format */
1596 stat = cdrom_read_tocentry(drive, 0, 1, 1, (char *)&ms_tmp,
1597 sizeof(ms_tmp), sense);
1598 if (stat)
1599 return stat;
1600
1601 msf_from_bcd (&ms_tmp.ent.addr.msf);
1602 toc->last_session_lba = msf_to_lba(ms_tmp.ent.addr.msf.minute,
1603 ms_tmp.ent.addr.msf.second,
1604 ms_tmp.ent.addr.msf.frame);
1605 }
1da177e4
LT
1606
1607 toc->xa_flag = (ms_tmp.hdr.first_track != ms_tmp.hdr.last_track);
1608
1609 /* Now try to get the total cdrom capacity. */
1610 stat = cdrom_get_last_written(cdi, &last_written);
1611 if (!stat && (last_written > toc->capacity)) {
1612 toc->capacity = last_written;
1613 set_capacity(info->disk, toc->capacity * sectors_per_frame);
dbe217af 1614 drive->probed_capacity = toc->capacity * sectors_per_frame;
1da177e4
LT
1615 }
1616
1617 /* Remember that we've read this stuff. */
2bc4cf2d 1618 info->cd_flags |= IDE_CD_FLAG_TOC_VALID;
1da177e4
LT
1619
1620 return 0;
1621}
1622
17802998 1623int ide_cdrom_get_capabilities(ide_drive_t *drive, u8 *buf)
9235e68b
EP
1624{
1625 struct cdrom_info *info = drive->driver_data;
1626 struct cdrom_device_info *cdi = &info->devinfo;
1627 struct packet_command cgc;
455d80a9 1628 int stat, attempts = 3, size = ATAPI_CAPABILITIES_PAGE_SIZE;
9235e68b 1629
e59724c7 1630 if ((info->cd_flags & IDE_CD_FLAG_FULL_CAPS_PAGE) == 0)
455d80a9 1631 size -= ATAPI_CAPABILITIES_PAGE_PAD_SIZE;
9235e68b 1632
455d80a9 1633 init_cdrom_command(&cgc, buf, size, CGC_DATA_UNKNOWN);
9235e68b
EP
1634 do { /* we seem to get stat=0x01,err=0x00 the first time (??) */
1635 stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CAPABILITIES_PAGE, 0);
1636 if (!stat)
1637 break;
1638 } while (--attempts);
1639 return stat;
1640}
1641
17802998 1642void ide_cdrom_update_speed(ide_drive_t *drive, u8 *buf)
9235e68b 1643{
4fe67178 1644 struct cdrom_info *cd = drive->driver_data;
481c8c64
BZ
1645 u16 curspeed, maxspeed;
1646
455d80a9
BZ
1647 curspeed = *(u16 *)&buf[8 + 14];
1648 maxspeed = *(u16 *)&buf[8 + 8];
1649
e59724c7 1650 if (cd->cd_flags & IDE_CD_FLAG_LE_SPEED_FIELDS) {
455d80a9
BZ
1651 curspeed = le16_to_cpu(curspeed);
1652 maxspeed = le16_to_cpu(maxspeed);
9235e68b 1653 } else {
455d80a9
BZ
1654 curspeed = be16_to_cpu(curspeed);
1655 maxspeed = be16_to_cpu(maxspeed);
9235e68b 1656 }
481c8c64 1657
2bc4cf2d
BZ
1658 cd->current_speed = (curspeed + (176/2)) / 176;
1659 cd->max_speed = (maxspeed + (176/2)) / 176;
9235e68b
EP
1660}
1661
20e7f7ef
BZ
1662#define IDE_CD_CAPABILITIES \
1663 (CDC_CLOSE_TRAY | CDC_OPEN_TRAY | CDC_LOCK | CDC_SELECT_SPEED | \
1664 CDC_SELECT_DISC | CDC_MULTI_SESSION | CDC_MCN | CDC_MEDIA_CHANGED | \
1665 CDC_PLAY_AUDIO | CDC_RESET | CDC_DRIVE_STATUS | CDC_CD_R | \
1666 CDC_CD_RW | CDC_DVD | CDC_DVD_R | CDC_DVD_RAM | CDC_GENERIC_PACKET | \
1667 CDC_MO_DRIVE | CDC_MRW | CDC_MRW_W | CDC_RAM)
1da177e4 1668
1da177e4
LT
1669static struct cdrom_device_ops ide_cdrom_dops = {
1670 .open = ide_cdrom_open_real,
1671 .release = ide_cdrom_release_real,
1672 .drive_status = ide_cdrom_drive_status,
1673 .media_changed = ide_cdrom_check_media_change_real,
1674 .tray_move = ide_cdrom_tray_move,
1675 .lock_door = ide_cdrom_lock_door,
1676 .select_speed = ide_cdrom_select_speed,
1677 .get_last_session = ide_cdrom_get_last_session,
1678 .get_mcn = ide_cdrom_get_mcn,
1679 .reset = ide_cdrom_reset,
1680 .audio_ioctl = ide_cdrom_audio_ioctl,
20e7f7ef 1681 .capability = IDE_CD_CAPABILITIES,
1da177e4
LT
1682 .generic_packet = ide_cdrom_packet,
1683};
1684
1685static int ide_cdrom_register (ide_drive_t *drive, int nslots)
1686{
1687 struct cdrom_info *info = drive->driver_data;
1688 struct cdrom_device_info *devinfo = &info->devinfo;
1689
1690 devinfo->ops = &ide_cdrom_dops;
2bc4cf2d 1691 devinfo->speed = info->current_speed;
1da177e4 1692 devinfo->capacity = nslots;
2a91f3e5 1693 devinfo->handle = drive;
1da177e4 1694 strcpy(devinfo->name, drive->name);
1da177e4 1695
2bc4cf2d 1696 if (info->cd_flags & IDE_CD_FLAG_NO_SPEED_SELECT)
3cbd814e
BZ
1697 devinfo->mask |= CDC_SELECT_SPEED;
1698
1da177e4
LT
1699 devinfo->disk = info->disk;
1700 return register_cdrom(devinfo);
1701}
1702
1da177e4
LT
1703static
1704int ide_cdrom_probe_capabilities (ide_drive_t *drive)
1705{
4fe67178
BZ
1706 struct cdrom_info *cd = drive->driver_data;
1707 struct cdrom_device_info *cdi = &cd->devinfo;
455d80a9
BZ
1708 u8 buf[ATAPI_CAPABILITIES_PAGE_SIZE];
1709 mechtype_t mechtype;
1da177e4
LT
1710 int nslots = 1;
1711
3f1b86d8
BZ
1712 cdi->mask = (CDC_CD_R | CDC_CD_RW | CDC_DVD | CDC_DVD_R |
1713 CDC_DVD_RAM | CDC_SELECT_DISC | CDC_PLAY_AUDIO |
1714 CDC_MO_DRIVE | CDC_RAM);
1715
1da177e4 1716 if (drive->media == ide_optical) {
3f1b86d8 1717 cdi->mask &= ~(CDC_MO_DRIVE | CDC_RAM);
1da177e4
LT
1718 printk(KERN_ERR "%s: ATAPI magneto-optical drive\n", drive->name);
1719 return nslots;
1720 }
1721
e59724c7 1722 if (cd->cd_flags & IDE_CD_FLAG_PRE_ATAPI12) {
2bc4cf2d 1723 cd->cd_flags &= ~IDE_CD_FLAG_NO_EJECT;
3f1b86d8 1724 cdi->mask &= ~CDC_PLAY_AUDIO;
1da177e4
LT
1725 return nslots;
1726 }
1727
1728 /*
1729 * we have to cheat a little here. the packet will eventually
1730 * be queued with ide_cdrom_packet(), which extracts the
1731 * drive from cdi->handle. Since this device hasn't been
1732 * registered with the Uniform layer yet, it can't do this.
1733 * Same goes for cdi->ops.
1734 */
2a91f3e5 1735 cdi->handle = drive;
1da177e4
LT
1736 cdi->ops = &ide_cdrom_dops;
1737
455d80a9 1738 if (ide_cdrom_get_capabilities(drive, buf))
1da177e4
LT
1739 return 0;
1740
455d80a9 1741 if ((buf[8 + 6] & 0x01) == 0)
2bc4cf2d 1742 cd->cd_flags |= IDE_CD_FLAG_NO_DOORLOCK;
455d80a9 1743 if (buf[8 + 6] & 0x08)
2bc4cf2d 1744 cd->cd_flags &= ~IDE_CD_FLAG_NO_EJECT;
455d80a9 1745 if (buf[8 + 3] & 0x01)
3f1b86d8 1746 cdi->mask &= ~CDC_CD_R;
455d80a9 1747 if (buf[8 + 3] & 0x02)
3f1b86d8 1748 cdi->mask &= ~(CDC_CD_RW | CDC_RAM);
455d80a9 1749 if (buf[8 + 2] & 0x38)
3f1b86d8 1750 cdi->mask &= ~CDC_DVD;
455d80a9 1751 if (buf[8 + 3] & 0x20)
3f1b86d8 1752 cdi->mask &= ~(CDC_DVD_RAM | CDC_RAM);
455d80a9 1753 if (buf[8 + 3] & 0x10)
3f1b86d8 1754 cdi->mask &= ~CDC_DVD_R;
e59724c7 1755 if ((buf[8 + 4] & 0x01) || (cd->cd_flags & IDE_CD_FLAG_PLAY_AUDIO_OK))
3f1b86d8 1756 cdi->mask &= ~CDC_PLAY_AUDIO;
455d80a9
BZ
1757
1758 mechtype = buf[8 + 6] >> 5;
1759 if (mechtype == mechtype_caddy || mechtype == mechtype_popup)
3f1b86d8 1760 cdi->mask |= CDC_CLOSE_TRAY;
1da177e4 1761
1da177e4 1762 if (cdi->sanyo_slot > 0) {
3f1b86d8 1763 cdi->mask &= ~CDC_SELECT_DISC;
1da177e4 1764 nslots = 3;
cdf6000d
BZ
1765 } else if (mechtype == mechtype_individual_changer ||
1766 mechtype == mechtype_cartridge_changer) {
2609d06d
BZ
1767 nslots = cdrom_number_of_slots(cdi);
1768 if (nslots > 1)
3f1b86d8 1769 cdi->mask &= ~CDC_SELECT_DISC;
1da177e4
LT
1770 }
1771
455d80a9 1772 ide_cdrom_update_speed(drive, buf);
4fe67178 1773
1da177e4 1774 printk(KERN_INFO "%s: ATAPI", drive->name);
4fe67178
BZ
1775
1776 /* don't print speed if the drive reported 0 */
2bc4cf2d
BZ
1777 if (cd->max_speed)
1778 printk(KERN_CONT " %dX", cd->max_speed);
4fe67178 1779
3f1b86d8 1780 printk(KERN_CONT " %s", (cdi->mask & CDC_DVD) ? "CD-ROM" : "DVD-ROM");
1da177e4 1781
3f1b86d8
BZ
1782 if ((cdi->mask & CDC_DVD_R) == 0 || (cdi->mask & CDC_DVD_RAM) == 0)
1783 printk(KERN_CONT " DVD%s%s",
1784 (cdi->mask & CDC_DVD_R) ? "" : "-R",
1785 (cdi->mask & CDC_DVD_RAM) ? "" : "-RAM");
1da177e4 1786
3f1b86d8
BZ
1787 if ((cdi->mask & CDC_CD_R) == 0 || (cdi->mask & CDC_CD_RW) == 0)
1788 printk(KERN_CONT " CD%s%s",
1789 (cdi->mask & CDC_CD_R) ? "" : "-R",
1790 (cdi->mask & CDC_CD_RW) ? "" : "/RW");
1da177e4 1791
3f1b86d8
BZ
1792 if ((cdi->mask & CDC_SELECT_DISC) == 0)
1793 printk(KERN_CONT " changer w/%d slots", nslots);
1794 else
1795 printk(KERN_CONT " drive");
1da177e4 1796
455d80a9 1797 printk(KERN_CONT ", %dkB Cache\n", be16_to_cpu(*(u16 *)&buf[8 + 12]));
1da177e4
LT
1798
1799 return nslots;
1800}
1801
7662d046 1802#ifdef CONFIG_IDE_PROC_FS
1da177e4
LT
1803static void ide_cdrom_add_settings(ide_drive_t *drive)
1804{
1497943e 1805 ide_add_setting(drive, "dsc_overlap", SETTING_RW, TYPE_BYTE, 0, 1, 1, 1, &drive->dsc_overlap, NULL);
1da177e4 1806}
7662d046
BZ
1807#else
1808static inline void ide_cdrom_add_settings(ide_drive_t *drive) { ; }
1809#endif
1da177e4
LT
1810
1811/*
1812 * standard prep_rq_fn that builds 10 byte cmds
1813 */
165125e1 1814static int ide_cdrom_prep_fs(struct request_queue *q, struct request *rq)
1da177e4
LT
1815{
1816 int hard_sect = queue_hardsect_size(q);
1817 long block = (long)rq->hard_sector / (hard_sect >> 9);
1818 unsigned long blocks = rq->hard_nr_sectors / (hard_sect >> 9);
1819
1820 memset(rq->cmd, 0, sizeof(rq->cmd));
1821
1822 if (rq_data_dir(rq) == READ)
1823 rq->cmd[0] = GPCMD_READ_10;
1824 else
1825 rq->cmd[0] = GPCMD_WRITE_10;
1826
1827 /*
1828 * fill in lba
1829 */
1830 rq->cmd[2] = (block >> 24) & 0xff;
1831 rq->cmd[3] = (block >> 16) & 0xff;
1832 rq->cmd[4] = (block >> 8) & 0xff;
1833 rq->cmd[5] = block & 0xff;
1834
1835 /*
1836 * and transfer length
1837 */
1838 rq->cmd[7] = (blocks >> 8) & 0xff;
1839 rq->cmd[8] = blocks & 0xff;
1840 rq->cmd_len = 10;
1841 return BLKPREP_OK;
1842}
1843
1844/*
1845 * Most of the SCSI commands are supported directly by ATAPI devices.
1846 * This transform handles the few exceptions.
1847 */
1848static int ide_cdrom_prep_pc(struct request *rq)
1849{
1850 u8 *c = rq->cmd;
1851
1852 /*
1853 * Transform 6-byte read/write commands to the 10-byte version
1854 */
1855 if (c[0] == READ_6 || c[0] == WRITE_6) {
1856 c[8] = c[4];
1857 c[5] = c[3];
1858 c[4] = c[2];
1859 c[3] = c[1] & 0x1f;
1860 c[2] = 0;
1861 c[1] &= 0xe0;
1862 c[0] += (READ_10 - READ_6);
1863 rq->cmd_len = 10;
1864 return BLKPREP_OK;
1865 }
1866
1867 /*
1868 * it's silly to pretend we understand 6-byte sense commands, just
1869 * reject with ILLEGAL_REQUEST and the caller should take the
1870 * appropriate action
1871 */
1872 if (c[0] == MODE_SENSE || c[0] == MODE_SELECT) {
1873 rq->errors = ILLEGAL_REQUEST;
1874 return BLKPREP_KILL;
1875 }
1876
1877 return BLKPREP_OK;
1878}
1879
165125e1 1880static int ide_cdrom_prep_fn(struct request_queue *q, struct request *rq)
1da177e4 1881{
4aff5e23 1882 if (blk_fs_request(rq))
1da177e4 1883 return ide_cdrom_prep_fs(q, rq);
4aff5e23 1884 else if (blk_pc_request(rq))
1da177e4
LT
1885 return ide_cdrom_prep_pc(rq);
1886
1887 return 0;
1888}
1889
e59724c7
BZ
1890struct cd_list_entry {
1891 const char *id_model;
1892 const char *id_firmware;
1893 unsigned int cd_flags;
1894};
1895
1896static const struct cd_list_entry ide_cd_quirks_list[] = {
1897 /* Limit transfer size per interrupt. */
1898 { "SAMSUNG CD-ROM SCR-2430", NULL, IDE_CD_FLAG_LIMIT_NFRAMES },
1899 { "SAMSUNG CD-ROM SCR-2432", NULL, IDE_CD_FLAG_LIMIT_NFRAMES },
1900 /* SCR-3231 doesn't support the SET_CD_SPEED command. */
1901 { "SAMSUNG CD-ROM SCR-3231", NULL, IDE_CD_FLAG_NO_SPEED_SELECT },
1902 /* Old NEC260 (not R) was released before ATAPI 1.2 spec. */
1903 { "NEC CD-ROM DRIVE:260", "1.01", IDE_CD_FLAG_TOCADDR_AS_BCD |
1904 IDE_CD_FLAG_PRE_ATAPI12, },
1905 /* Vertos 300, some versions of this drive like to talk BCD. */
1906 { "V003S0DS", NULL, IDE_CD_FLAG_VERTOS_300_SSD, },
1907 /* Vertos 600 ESD. */
1908 { "V006E0DS", NULL, IDE_CD_FLAG_VERTOS_600_ESD, },
1909 /*
1910 * Sanyo 3 CD changer uses a non-standard command for CD changing
1911 * (by default standard ATAPI support for CD changers is used).
1912 */
1913 { "CD-ROM CDR-C3 G", NULL, IDE_CD_FLAG_SANYO_3CD },
1914 { "CD-ROM CDR-C3G", NULL, IDE_CD_FLAG_SANYO_3CD },
1915 { "CD-ROM CDR_C36", NULL, IDE_CD_FLAG_SANYO_3CD },
1916 /* Stingray 8X CD-ROM. */
1917 { "STINGRAY 8422 IDE 8X CD-ROM 7-27-95", NULL, IDE_CD_FLAG_PRE_ATAPI12},
1918 /*
1919 * ACER 50X CD-ROM and WPI 32X CD-ROM require the full spec length
1920 * mode sense page capabilities size, but older drives break.
1921 */
1922 { "ATAPI CD ROM DRIVE 50X MAX", NULL, IDE_CD_FLAG_FULL_CAPS_PAGE },
1923 { "WPI CDS-32X", NULL, IDE_CD_FLAG_FULL_CAPS_PAGE },
1924 /* ACER/AOpen 24X CD-ROM has the speed fields byte-swapped. */
1925 { "", "241N", IDE_CD_FLAG_LE_SPEED_FIELDS },
1926 /*
1927 * Some drives used by Apple don't advertise audio play
1928 * but they do support reading TOC & audio datas.
1929 */
1930 { "MATSHITADVD-ROM SR-8187", NULL, IDE_CD_FLAG_PLAY_AUDIO_OK },
1931 { "MATSHITADVD-ROM SR-8186", NULL, IDE_CD_FLAG_PLAY_AUDIO_OK },
1932 { "MATSHITADVD-ROM SR-8176", NULL, IDE_CD_FLAG_PLAY_AUDIO_OK },
1933 { "MATSHITADVD-ROM SR-8174", NULL, IDE_CD_FLAG_PLAY_AUDIO_OK },
acbe44e6 1934 { "Optiarc DVD RW AD-5200A", NULL, IDE_CD_FLAG_PLAY_AUDIO_OK },
e59724c7
BZ
1935 { NULL, NULL, 0 }
1936};
1937
1938static unsigned int ide_cd_flags(struct hd_driveid *id)
1939{
1940 const struct cd_list_entry *cle = ide_cd_quirks_list;
1941
1942 while (cle->id_model) {
1943 if (strcmp(cle->id_model, id->model) == 0 &&
1944 (cle->id_firmware == NULL ||
1945 strstr(id->fw_rev, cle->id_firmware)))
1946 return cle->cd_flags;
1947 cle++;
1948 }
1949
1950 return 0;
1951}
1952
1da177e4
LT
1953static
1954int ide_cdrom_setup (ide_drive_t *drive)
1955{
4fe67178
BZ
1956 struct cdrom_info *cd = drive->driver_data;
1957 struct cdrom_device_info *cdi = &cd->devinfo;
e59724c7 1958 struct hd_driveid *id = drive->id;
1da177e4
LT
1959 int nslots;
1960
1961 blk_queue_prep_rq(drive->queue, ide_cdrom_prep_fn);
1962 blk_queue_dma_alignment(drive->queue, 31);
1963 drive->queue->unplug_delay = (1 * HZ) / 1000;
1964 if (!drive->queue->unplug_delay)
1965 drive->queue->unplug_delay = 1;
1966
1967 drive->special.all = 0;
1968
e59724c7
BZ
1969 cd->cd_flags = IDE_CD_FLAG_MEDIA_CHANGED | IDE_CD_FLAG_NO_EJECT |
1970 ide_cd_flags(id);
1da177e4 1971
e59724c7 1972 if ((id->config & 0x0060) == 0x20)
2bc4cf2d 1973 cd->cd_flags |= IDE_CD_FLAG_DRQ_INTERRUPT;
e59724c7
BZ
1974
1975 if ((cd->cd_flags & IDE_CD_FLAG_VERTOS_300_SSD) &&
1976 id->fw_rev[4] == '1' && id->fw_rev[6] <= '2')
2bc4cf2d
BZ
1977 cd->cd_flags |= (IDE_CD_FLAG_TOCTRACKS_AS_BCD |
1978 IDE_CD_FLAG_TOCADDR_AS_BCD);
e59724c7
BZ
1979 else if ((cd->cd_flags & IDE_CD_FLAG_VERTOS_600_ESD) &&
1980 id->fw_rev[4] == '1' && id->fw_rev[6] <= '2')
2bc4cf2d 1981 cd->cd_flags |= IDE_CD_FLAG_TOCTRACKS_AS_BCD;
e59724c7
BZ
1982 else if (cd->cd_flags & IDE_CD_FLAG_SANYO_3CD)
1983 cdi->sanyo_slot = 3; /* 3 => use CD in slot 0 */
1da177e4 1984
1da177e4
LT
1985 nslots = ide_cdrom_probe_capabilities (drive);
1986
1987 /*
1988 * set correct block size
1989 */
1990 blk_queue_hardsect_size(drive->queue, CD_FRAMESIZE);
1991
1992 if (drive->autotune == IDE_TUNE_DEFAULT ||
1993 drive->autotune == IDE_TUNE_AUTO)
1994 drive->dsc_overlap = (drive->next != drive);
1da177e4
LT
1995
1996 if (ide_cdrom_register(drive, nslots)) {
1997 printk (KERN_ERR "%s: ide_cdrom_setup failed to register device with the cdrom driver.\n", drive->name);
4fe67178 1998 cd->devinfo.handle = NULL;
1da177e4
LT
1999 return 1;
2000 }
2001 ide_cdrom_add_settings(drive);
2002 return 0;
2003}
2004
ecfd80e4 2005#ifdef CONFIG_IDE_PROC_FS
1da177e4
LT
2006static
2007sector_t ide_cdrom_capacity (ide_drive_t *drive)
2008{
2009 unsigned long capacity, sectors_per_frame;
2010
2011 if (cdrom_read_capacity(drive, &capacity, &sectors_per_frame, NULL))
2012 return 0;
2013
2014 return capacity * sectors_per_frame;
2015}
d97b3214 2016#endif
1da177e4 2017
4031bbe4 2018static void ide_cd_remove(ide_drive_t *drive)
1da177e4
LT
2019{
2020 struct cdrom_info *info = drive->driver_data;
2021
7662d046 2022 ide_proc_unregister_driver(drive, info->driver);
1da177e4
LT
2023
2024 del_gendisk(info->disk);
2025
2026 ide_cd_put(info);
1da177e4
LT
2027}
2028
2029static void ide_cd_release(struct kref *kref)
2030{
2031 struct cdrom_info *info = to_ide_cd(kref);
2032 struct cdrom_device_info *devinfo = &info->devinfo;
2033 ide_drive_t *drive = info->drive;
2034 struct gendisk *g = info->disk;
2035
6044ec88
JJ
2036 kfree(info->buffer);
2037 kfree(info->toc);
1da177e4
LT
2038 if (devinfo->handle == drive && unregister_cdrom(devinfo))
2039 printk(KERN_ERR "%s: %s failed to unregister device from the cdrom "
2040 "driver.\n", __FUNCTION__, drive->name);
2041 drive->dsc_overlap = 0;
2042 drive->driver_data = NULL;
2043 blk_queue_prep_rq(drive->queue, NULL);
2044 g->private_data = NULL;
2045 put_disk(g);
2046 kfree(info);
2047}
2048
4031bbe4 2049static int ide_cd_probe(ide_drive_t *);
1da177e4 2050
ecfd80e4 2051#ifdef CONFIG_IDE_PROC_FS
1da177e4
LT
2052static int proc_idecd_read_capacity
2053 (char *page, char **start, off_t off, int count, int *eof, void *data)
2054{
2a91f3e5 2055 ide_drive_t *drive = data;
1da177e4
LT
2056 int len;
2057
2058 len = sprintf(page,"%llu\n", (long long)ide_cdrom_capacity(drive));
2059 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
2060}
2061
2062static ide_proc_entry_t idecd_proc[] = {
2063 { "capacity", S_IFREG|S_IRUGO, proc_idecd_read_capacity, NULL },
2064 { NULL, 0, NULL, NULL }
2065};
1da177e4
LT
2066#endif
2067
2068static ide_driver_t ide_cdrom_driver = {
8604affd 2069 .gen_driver = {
4ef3b8f4 2070 .owner = THIS_MODULE,
8604affd
BZ
2071 .name = "ide-cdrom",
2072 .bus = &ide_bus_type,
8604affd 2073 },
4031bbe4
RK
2074 .probe = ide_cd_probe,
2075 .remove = ide_cd_remove,
1da177e4
LT
2076 .version = IDECD_VERSION,
2077 .media = ide_cdrom,
1da177e4 2078 .supports_dsc_overlap = 1,
1da177e4
LT
2079 .do_request = ide_do_rw_cdrom,
2080 .end_request = ide_end_request,
2081 .error = __ide_error,
2082 .abort = __ide_abort,
7662d046 2083#ifdef CONFIG_IDE_PROC_FS
1da177e4 2084 .proc = idecd_proc,
7662d046 2085#endif
1da177e4
LT
2086};
2087
2088static int idecd_open(struct inode * inode, struct file * file)
2089{
2090 struct gendisk *disk = inode->i_bdev->bd_disk;
2091 struct cdrom_info *info;
1da177e4
LT
2092 int rc = -ENOMEM;
2093
2094 if (!(info = ide_cd_get(disk)))
2095 return -ENXIO;
2096
1da177e4 2097 if (!info->buffer)
c94964a4
BZ
2098 info->buffer = kmalloc(SECTOR_BUFFER_SIZE, GFP_KERNEL|__GFP_REPEAT);
2099
2100 if (info->buffer)
2101 rc = cdrom_open(&info->devinfo, inode, file);
1da177e4
LT
2102
2103 if (rc < 0)
2104 ide_cd_put(info);
2105
2106 return rc;
2107}
2108
2109static int idecd_release(struct inode * inode, struct file * file)
2110{
2111 struct gendisk *disk = inode->i_bdev->bd_disk;
2112 struct cdrom_info *info = ide_cd_g(disk);
1da177e4
LT
2113
2114 cdrom_release (&info->devinfo, file);
1da177e4
LT
2115
2116 ide_cd_put(info);
2117
2118 return 0;
2119}
2120
6a2900b6
CH
2121static int idecd_set_spindown(struct cdrom_device_info *cdi, unsigned long arg)
2122{
2123 struct packet_command cgc;
2124 char buffer[16];
2125 int stat;
2126 char spindown;
2127
2128 if (copy_from_user(&spindown, (void __user *)arg, sizeof(char)))
2129 return -EFAULT;
2130
2131 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_UNKNOWN);
2132
2133 stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CDROM_PAGE, 0);
2134 if (stat)
2135 return stat;
2136
2137 buffer[11] = (buffer[11] & 0xf0) | (spindown & 0x0f);
2138 return cdrom_mode_select(cdi, &cgc);
2139}
2140
2141static int idecd_get_spindown(struct cdrom_device_info *cdi, unsigned long arg)
2142{
2143 struct packet_command cgc;
2144 char buffer[16];
2145 int stat;
2146 char spindown;
2147
2148 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_UNKNOWN);
2149
2150 stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CDROM_PAGE, 0);
2151 if (stat)
2152 return stat;
2153
2154 spindown = buffer[11] & 0x0f;
2155 if (copy_to_user((void __user *)arg, &spindown, sizeof (char)))
2156 return -EFAULT;
2157 return 0;
2158}
2159
1da177e4
LT
2160static int idecd_ioctl (struct inode *inode, struct file *file,
2161 unsigned int cmd, unsigned long arg)
2162{
2163 struct block_device *bdev = inode->i_bdev;
2164 struct cdrom_info *info = ide_cd_g(bdev->bd_disk);
2165 int err;
2166
6a2900b6
CH
2167 switch (cmd) {
2168 case CDROMSETSPINDOWN:
2169 return idecd_set_spindown(&info->devinfo, arg);
2170 case CDROMGETSPINDOWN:
2171 return idecd_get_spindown(&info->devinfo, arg);
2172 default:
2173 break;
2174 }
2175
2176 err = generic_ide_ioctl(info->drive, file, bdev, cmd, arg);
1da177e4
LT
2177 if (err == -EINVAL)
2178 err = cdrom_ioctl(file, &info->devinfo, inode, cmd, arg);
2179
2180 return err;
2181}
2182
2183static int idecd_media_changed(struct gendisk *disk)
2184{
2185 struct cdrom_info *info = ide_cd_g(disk);
2186 return cdrom_media_changed(&info->devinfo);
2187}
2188
2189static int idecd_revalidate_disk(struct gendisk *disk)
2190{
2191 struct cdrom_info *info = ide_cd_g(disk);
2192 struct request_sense sense;
139c829d
BZ
2193
2194 ide_cd_read_toc(info->drive, &sense);
2195
1da177e4
LT
2196 return 0;
2197}
2198
2199static struct block_device_operations idecd_ops = {
2200 .owner = THIS_MODULE,
2201 .open = idecd_open,
2202 .release = idecd_release,
2203 .ioctl = idecd_ioctl,
2204 .media_changed = idecd_media_changed,
2205 .revalidate_disk= idecd_revalidate_disk
2206};
2207
2208/* options */
2209static char *ignore = NULL;
2210
2211module_param(ignore, charp, 0400);
2212MODULE_DESCRIPTION("ATAPI CD-ROM Driver");
2213
4031bbe4 2214static int ide_cd_probe(ide_drive_t *drive)
1da177e4
LT
2215{
2216 struct cdrom_info *info;
2217 struct gendisk *g;
2218 struct request_sense sense;
2219
2220 if (!strstr("ide-cdrom", drive->driver_req))
2221 goto failed;
2222 if (!drive->present)
2223 goto failed;
2224 if (drive->media != ide_cdrom && drive->media != ide_optical)
2225 goto failed;
2226 /* skip drives that we were told to ignore */
2227 if (ignore != NULL) {
2228 if (strstr(ignore, drive->name)) {
2229 printk(KERN_INFO "ide-cd: ignoring drive %s\n", drive->name);
2230 goto failed;
2231 }
2232 }
2233 if (drive->scsi) {
2234 printk(KERN_INFO "ide-cd: passing drive %s to ide-scsi emulation.\n", drive->name);
2235 goto failed;
2236 }
f5e3c2fa 2237 info = kzalloc(sizeof(struct cdrom_info), GFP_KERNEL);
1da177e4
LT
2238 if (info == NULL) {
2239 printk(KERN_ERR "%s: Can't allocate a cdrom structure\n", drive->name);
2240 goto failed;
2241 }
2242
2243 g = alloc_disk(1 << PARTN_BITS);
2244 if (!g)
2245 goto out_free_cd;
2246
2247 ide_init_disk(g, drive);
2248
7662d046 2249 ide_proc_register_driver(drive, &ide_cdrom_driver);
8604affd 2250
1da177e4
LT
2251 kref_init(&info->kref);
2252
2253 info->drive = drive;
2254 info->driver = &ide_cdrom_driver;
2255 info->disk = g;
2256
2257 g->private_data = &info->driver;
2258
2259 drive->driver_data = info;
2260
1da177e4 2261 g->minors = 1;
1da177e4
LT
2262 g->driverfs_dev = &drive->gendev;
2263 g->flags = GENHD_FL_CD | GENHD_FL_REMOVABLE;
2264 if (ide_cdrom_setup(drive)) {
7662d046 2265 ide_proc_unregister_driver(drive, &ide_cdrom_driver);
05017db3 2266 ide_cd_release(&info->kref);
1da177e4
LT
2267 goto failed;
2268 }
1da177e4 2269
139c829d 2270 ide_cd_read_toc(drive, &sense);
1da177e4
LT
2271 g->fops = &idecd_ops;
2272 g->flags |= GENHD_FL_REMOVABLE;
2273 add_disk(g);
2274 return 0;
2275
1da177e4
LT
2276out_free_cd:
2277 kfree(info);
2278failed:
8604affd 2279 return -ENODEV;
1da177e4
LT
2280}
2281
2282static void __exit ide_cdrom_exit(void)
2283{
8604affd 2284 driver_unregister(&ide_cdrom_driver.gen_driver);
1da177e4 2285}
17514e8a
BZ
2286
2287static int __init ide_cdrom_init(void)
1da177e4 2288{
8604affd 2289 return driver_register(&ide_cdrom_driver.gen_driver);
1da177e4
LT
2290}
2291
263756ec 2292MODULE_ALIAS("ide:*m-cdrom*");
972560fb 2293MODULE_ALIAS("ide-cd");
1da177e4
LT
2294module_init(ide_cdrom_init);
2295module_exit(ide_cdrom_exit);
2296MODULE_LICENSE("GPL");