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