blob: fe5aefbf83392fae89553050dd484dcf108fd428 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Bartlomiej Zolnierkiewicz3bb46632008-02-01 23:09:28 +01002 * ATAPI CD-ROM driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Bartlomiej Zolnierkiewicz59bca8c2008-02-01 23:09:33 +01004 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * May be copied or modified under the terms of the GNU General Public
10 * License. See linux/COPYING for more information.
11 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * 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 *
Bartlomiej Zolnierkiewicz03553352008-02-01 23:09:18 +010022 * For historical changelog please see:
23 * Documentation/ide/ChangeLog.ide-cd.1994-2004
24 */
25
Bartlomiej Zolnierkiewicz3bb46632008-02-01 23:09:28 +010026#define IDECD_VERSION "5.00"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#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>
Arjan van de Vencf8b8972006-03-23 03:00:45 -080039#include <linux/mutex.h>
Bartlomiej Zolnierkiewicz9a6dc662008-02-01 23:09:22 +010040#include <linux/bcd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
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
Arjan van de Vencf8b8972006-03-23 03:00:45 -080052static DEFINE_MUTEX(idecd_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
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
Arjan van de Vencf8b8972006-03-23 03:00:45 -080063 mutex_lock(&idecd_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 cd = ide_cd_g(disk);
65 if (cd)
66 kref_get(&cd->kref);
Arjan van de Vencf8b8972006-03-23 03:00:45 -080067 mutex_unlock(&idecd_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 return cd;
69}
70
71static void ide_cd_release(struct kref *);
72
73static void ide_cd_put(struct cdrom_info *cd)
74{
Arjan van de Vencf8b8972006-03-23 03:00:45 -080075 mutex_lock(&idecd_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 kref_put(&cd->kref, ide_cd_release);
Arjan van de Vencf8b8972006-03-23 03:00:45 -080077 mutex_unlock(&idecd_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
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{
Bartlomiej Zolnierkiewicz0ba11212008-02-01 23:09:21 +010088 struct cdrom_info *cd = drive->driver_data;
89
Bartlomiej Zolnierkiewicz2bc4cf22008-02-01 23:09:22 +010090 cd->cd_flags |= IDE_CD_FLAG_MEDIA_CHANGED;
91 cd->cd_flags &= ~IDE_CD_FLAG_TOC_VALID;
Bartlomiej Zolnierkiewicz0ba11212008-02-01 23:09:21 +010092 cd->nsectors_buffered = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093}
94
95static int cdrom_log_sense(ide_drive_t *drive, struct request *rq,
96 struct request_sense *sense)
97{
98 int log = 0;
99
Jens Axboe4aff5e22006-08-10 08:44:47 +0200100 if (!sense || !rq || (rq->cmd_flags & REQ_QUIET))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 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)
Alan Coxdbe217a2006-06-25 05:47:44 -0700122 break;
123 log = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 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{
Alan Coxdbe217a2006-06-25 05:47:44 -0700145 unsigned long sector;
146 unsigned long bio_sectors;
147 unsigned long valid;
148 struct cdrom_info *info = drive->driver_data;
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 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
Alan Coxdbe217a2006-06-25 05:47:44 -0700162 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Bartlomiej Zolnierkiewicz972560f2008-02-01 23:09:23 +0100194 ide_cd_log_error(drive->name, failed_command, sense);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
196
197/*
198 * Initialize a ide-cd packet command request
199 */
Bartlomiej Zolnierkiewicz17802992008-02-01 23:09:25 +0100200void ide_cd_init_rq(ide_drive_t *drive, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
202 struct cdrom_info *cd = drive->driver_data;
203
204 ide_init_drive_cmd(rq);
Jens Axboecea28852006-10-12 15:08:45 +0200205 rq->cmd_type = REQ_TYPE_ATA_PC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 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 */
Bartlomiej Zolnierkiewicz139c8292008-02-01 23:09:24 +0100219 ide_cd_init_rq(drive, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221 rq->data = sense;
222 rq->cmd[0] = GPCMD_REQUEST_SENSE;
223 rq->cmd[4] = rq->data_len = 18;
224
Jens Axboe4aff5e22006-08-10 08:44:47 +0200225 rq->cmd_type = REQ_TYPE_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
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
Jens Axboe4aff5e22006-08-10 08:44:47 +0200238 if (blk_sense_request(rq) && uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 /*
Jens Axboe4aff5e22006-08-10 08:44:47 +0200240 * For REQ_TYPE_SENSE, "rq->buffer" points to the original
241 * failed request
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 */
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 }
Alan Coxdbe217a2006-06-25 05:47:44 -0700253 cdrom_analyze_sense_data(drive, failed, sense);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 /*
255 * now end failed request
256 */
Alan Coxdbe217a2006-06-25 05:47:44 -0700257 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);
Kiyoshi Ueda5e36bb62008-01-28 10:34:20 +0100263 if (__blk_end_request(failed, -EIO,
264 failed->data_len))
265 BUG();
Alan Coxdbe217a2006-06-25 05:47:44 -0700266 spin_unlock_irqrestore(&ide_lock, flags);
267 }
268 } else
269 cdrom_analyze_sense_data(drive, NULL, sense);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 }
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
Alan Coxdbe217a2006-06-25 05:47:44 -0700283static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290/* 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. */
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100298 stat = ide_read_status(drive);
299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 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. */
Bartlomiej Zolnierkiewicz64a57fe2008-02-06 02:57:51 +0100307 err = ide_read_error(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 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
Jens Axboe4aff5e22006-08-10 08:44:47 +0200315 if (blk_sense_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 /* 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
Jens Axboe4aff5e22006-08-10 08:44:47 +0200320 rq->cmd_flags |= REQ_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 cdrom_end_request(drive, 0);
322 ide_error(drive, "request sense failure", stat);
323 return 1;
324
Jens Axboe8770c012006-10-12 17:24:52 +0200325 } else if (blk_pc_request(rq) || rq->cmd_type == REQ_TYPE_ATA_PC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 /* All other functions, except for READ. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 /*
329 * if we have an error, pass back CHECK_CONDITION as the
330 * scsi status byte
331 */
Jens Axboeb7156732006-11-13 18:05:02 +0100332 if (blk_pc_request(rq) && !rq->errors)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 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;
Stuart Hayes76ca1af2007-04-10 22:38:43 +0200343 } 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 */
Jens Axboe4aff5e22006-08-10 08:44:47 +0200352 } else if (!(rq->cmd_flags & REQ_QUIET)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 /* Otherwise, print an error. */
354 ide_dump_status(drive, "packet command error", stat);
355 }
356
Jens Axboe4aff5e22006-08-10 08:44:47 +0200357 rq->cmd_flags |= REQ_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
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 */
Bartlomiej Zolnierkiewiczbbb89e32008-02-01 23:09:28 +0100364 goto end_request;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
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.*/
Alan Coxdbe217a2006-06-25 05:47:44 -0700419 ide_dump_status_no_sense (drive, "command error", stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 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 */
Alan Coxdbe217a2006-06-25 05:47:44 -0700424 ide_dump_status_no_sense (drive, "media error (bad sector)", stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 do_end_request = 1;
426 } else if (sense_key == BLANK_CHECK) {
427 /* Disk appears blank ?? */
Alan Coxdbe217a2006-06-25 05:47:44 -0700428 ide_dump_status_no_sense (drive, "media error (blank)", stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 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
Alan Coxdbe217a2006-06-25 05:47:44 -0700440 /* 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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Bartlomiej Zolnierkiewiczbbb89e32008-02-01 23:09:28 +0100444 if (do_end_request)
445 goto end_request;
Alan Coxdbe217a2006-06-25 05:47:44 -0700446
Bartlomiej Zolnierkiewiczbbb89e32008-02-01 23:09:28 +0100447 /*
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 } 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;
Bartlomiej Zolnierkiewiczbbb89e32008-02-01 23:09:28 +0100460
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475}
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:
Jens Axboe4aff5e22006-08-10 08:44:47 +0200497 if (!(rq->cmd_flags & REQ_QUIET))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 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
Bartlomiej Zolnierkiewicz3a6a3542008-01-25 22:17:13 +0100524 /* FIXME: for Virtual DMA we must check harder */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 if (info->dma)
526 info->dma = !hwif->dma_setup(drive);
527
528 /* Set up the controller registers. */
Bartlomiej Zolnierkiewicz2fc57382008-01-25 22:17:13 +0100529 ide_pktcmd_tf_load(drive, IDE_TFLAG_OUT_NSECT | IDE_TFLAG_OUT_LBAL |
530 IDE_TFLAG_NO_SELECT_MASK, xferlen, info->dma);
Bartlomiej Zolnierkiewicz4fe67172008-02-01 23:09:21 +0100531
Bartlomiej Zolnierkiewicz2bc4cf22008-02-01 23:09:22 +0100532 if (info->cd_flags & IDE_CD_FLAG_DRQ_INTERRUPT) {
Albert Leef0dd8712007-02-17 02:40:21 +0100533 /* waiting for CDB interrupt, not DMA yet. */
534 if (info->dma)
535 drive->waiting_for_dma = 0;
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 /* 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);
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +0200545 hwif->OUTBSYNC(drive, WIN_PACKETCMD,
546 hwif->io_ports[IDE_COMMAND_OFFSET]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 ndelay(400);
548 spin_unlock_irqrestore(&ide_lock, flags);
549
550 return (*handler) (drive);
551 }
552}
553
554/* Send a packet command to DRIVE described by CMD_BUF and CMD_LEN.
555 The device registers must have already been prepared
556 by cdrom_start_packet_command.
557 HANDLER is the interrupt handler to call when the command completes
558 or there's data ready. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559#define ATAPI_MIN_CDB_BYTES 12
560static ide_startstop_t cdrom_transfer_packet_command (ide_drive_t *drive,
561 struct request *rq,
562 ide_handler_t *handler)
563{
564 ide_hwif_t *hwif = drive->hwif;
565 int cmd_len;
566 struct cdrom_info *info = drive->driver_data;
567 ide_startstop_t startstop;
568
Bartlomiej Zolnierkiewicz2bc4cf22008-02-01 23:09:22 +0100569 if (info->cd_flags & IDE_CD_FLAG_DRQ_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 /* Here we should have been called after receiving an interrupt
571 from the device. DRQ should how be set. */
572
573 /* Check for errors. */
574 if (cdrom_decode_status(drive, DRQ_STAT, NULL))
575 return ide_stopped;
Albert Leef0dd8712007-02-17 02:40:21 +0100576
577 /* Ok, next interrupt will be DMA interrupt. */
578 if (info->dma)
579 drive->waiting_for_dma = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 } else {
581 /* Otherwise, we must wait for DRQ to get set. */
582 if (ide_wait_stat(&startstop, drive, DRQ_STAT,
583 BUSY_STAT, WAIT_READY))
584 return startstop;
585 }
586
587 /* Arm the interrupt handler. */
588 ide_set_handler(drive, handler, rq->timeout, cdrom_timer_expiry);
589
590 /* ATAPI commands get padded out to 12 bytes minimum */
591 cmd_len = COMMAND_SIZE(rq->cmd[0]);
592 if (cmd_len < ATAPI_MIN_CDB_BYTES)
593 cmd_len = ATAPI_MIN_CDB_BYTES;
594
595 /* Send the command to the device. */
596 HWIF(drive)->atapi_output_bytes(drive, rq->cmd, cmd_len);
597
598 /* Start the DMA if need be */
599 if (info->dma)
600 hwif->dma_start(drive);
601
602 return ide_started;
603}
604
605/****************************************************************************
606 * Block read functions.
607 */
608
Bartlomiej Zolnierkiewicz5a5222d2008-02-01 23:09:17 +0100609static void ide_cd_pad_transfer(ide_drive_t *drive, xfer_func_t *xf, int len)
610{
611 while (len > 0) {
612 int dum = 0;
613 xf(drive, &dum, sizeof(dum));
614 len -= sizeof(dum);
615 }
616}
617
Bartlomiej Zolnierkiewiczb4ab7262008-02-01 23:09:26 +0100618static void ide_cd_drain_data(ide_drive_t *drive, int nsects)
619{
620 while (nsects > 0) {
621 static char dum[SECTOR_SIZE];
622
623 drive->hwif->atapi_input_bytes(drive, dum, sizeof(dum));
624 nsects--;
625 }
626}
627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628/*
629 * Buffer up to SECTORS_TO_TRANSFER sectors from the drive in our sector
630 * buffer. Once the first sector is added, any subsequent sectors are
631 * assumed to be continuous (until the buffer is cleared). For the first
632 * sector added, SECTOR is its sector number. (SECTOR is then ignored until
633 * the buffer is cleared.)
634 */
635static void cdrom_buffer_sectors (ide_drive_t *drive, unsigned long sector,
636 int sectors_to_transfer)
637{
638 struct cdrom_info *info = drive->driver_data;
639
640 /* Number of sectors to read into the buffer. */
641 int sectors_to_buffer = min_t(int, sectors_to_transfer,
642 (SECTOR_BUFFER_SIZE >> SECTOR_BITS) -
643 info->nsectors_buffered);
644
645 char *dest;
646
647 /* If we couldn't get a buffer, don't try to buffer anything... */
648 if (info->buffer == NULL)
649 sectors_to_buffer = 0;
650
651 /* If this is the first sector in the buffer, remember its number. */
652 if (info->nsectors_buffered == 0)
653 info->sector_buffered = sector;
654
655 /* Read the data into the buffer. */
656 dest = info->buffer + info->nsectors_buffered * SECTOR_SIZE;
657 while (sectors_to_buffer > 0) {
658 HWIF(drive)->atapi_input_bytes(drive, dest, SECTOR_SIZE);
659 --sectors_to_buffer;
660 --sectors_to_transfer;
661 ++info->nsectors_buffered;
662 dest += SECTOR_SIZE;
663 }
664
665 /* Throw away any remaining data. */
Bartlomiej Zolnierkiewiczb4ab7262008-02-01 23:09:26 +0100666 ide_cd_drain_data(drive, sectors_to_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667}
668
669/*
670 * Check the contents of the interrupt reason register from the cdrom
671 * and attempt to recover if there are problems. Returns 0 if everything's
672 * ok; nonzero if the request has been terminated.
673 */
Bartlomiej Zolnierkiewicz9f10d9e2008-02-26 21:50:35 +0100674static int ide_cd_check_ireason(ide_drive_t *drive, struct request *rq,
675 int len, int ireason, int rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
Bartlomiej Zolnierkiewicz0d6f7e32008-02-01 23:09:28 +0100677 /*
678 * ireason == 0: the drive wants to receive data from us
679 * ireason == 2: the drive is expecting to transfer data to us
680 */
681 if (ireason == (!rw << 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 return 0;
Bartlomiej Zolnierkiewicz0d6f7e32008-02-01 23:09:28 +0100683 else if (ireason == (rw << 1)) {
Bartlomiej Zolnierkiewicz5a5222d2008-02-01 23:09:17 +0100684 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewicz0d6f7e32008-02-01 23:09:28 +0100685 xfer_func_t *xf;
Bartlomiej Zolnierkiewicz5a5222d2008-02-01 23:09:17 +0100686
Bartlomiej Zolnierkiewicz0d6f7e32008-02-01 23:09:28 +0100687 /* Whoops... */
Bartlomiej Zolnierkiewicz35379c02007-12-24 15:23:43 +0100688 printk(KERN_ERR "%s: %s: wrong transfer direction!\n",
689 drive->name, __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Bartlomiej Zolnierkiewicz0d6f7e32008-02-01 23:09:28 +0100691 xf = rw ? hwif->atapi_output_bytes : hwif->atapi_input_bytes;
692 ide_cd_pad_transfer(drive, xf, len);
693 } else if (rw == 0 && ireason == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 /* Some drives (ASUS) seem to tell us that status
695 * info is available. just get it and ignore.
696 */
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100697 (void)ide_read_status(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 return 0;
699 } else {
700 /* Drive wants a command packet, or invalid ireason... */
Bartlomiej Zolnierkiewicz35379c02007-12-24 15:23:43 +0100701 printk(KERN_ERR "%s: %s: bad interrupt reason 0x%02x\n",
702 drive->name, __FUNCTION__, ireason);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 }
704
Bartlomiej Zolnierkiewicz9f10d9e2008-02-26 21:50:35 +0100705 if (rq->cmd_type == REQ_TYPE_ATA_PC)
706 rq->cmd_flags |= REQ_FAILED;
707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 cdrom_end_request(drive, 0);
709 return -1;
710}
711
712/*
Bartlomiej Zolnierkiewicz64814f22008-02-01 23:09:26 +0100713 * Assume that the drive will always provide data in multiples of at least
714 * SECTOR_SIZE, as it gets hairy to keep track of the transfers otherwise.
715 */
716static int ide_cd_check_transfer_size(ide_drive_t *drive, int len)
717{
718 struct cdrom_info *cd = drive->driver_data;
719
720 if ((len % SECTOR_SIZE) == 0)
721 return 0;
722
723 printk(KERN_ERR "%s: %s: Bad transfer size %d\n",
724 drive->name, __FUNCTION__, len);
725
726 if (cd->cd_flags & IDE_CD_FLAG_LIMIT_NFRAMES)
727 printk(KERN_ERR " This drive is not supported by "
728 "this version of the driver\n");
729 else {
730 printk(KERN_ERR " Trying to limit transfer sizes\n");
731 cd->cd_flags |= IDE_CD_FLAG_LIMIT_NFRAMES;
732 }
733
734 return 1;
735}
736
737/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 * Try to satisfy some of the current read request from our cached data.
739 * Returns nonzero if the request has been completed, zero otherwise.
740 */
741static int cdrom_read_from_buffer (ide_drive_t *drive)
742{
743 struct cdrom_info *info = drive->driver_data;
744 struct request *rq = HWGROUP(drive)->rq;
745 unsigned short sectors_per_frame;
746
747 sectors_per_frame = queue_hardsect_size(drive->queue) >> SECTOR_BITS;
748
749 /* Can't do anything if there's no buffer. */
750 if (info->buffer == NULL) return 0;
751
752 /* Loop while this request needs data and the next block is present
753 in our cache. */
754 while (rq->nr_sectors > 0 &&
755 rq->sector >= info->sector_buffered &&
756 rq->sector < info->sector_buffered + info->nsectors_buffered) {
757 if (rq->current_nr_sectors == 0)
758 cdrom_end_request(drive, 1);
759
760 memcpy (rq->buffer,
761 info->buffer +
762 (rq->sector - info->sector_buffered) * SECTOR_SIZE,
763 SECTOR_SIZE);
764 rq->buffer += SECTOR_SIZE;
765 --rq->current_nr_sectors;
766 --rq->nr_sectors;
767 ++rq->sector;
768 }
769
770 /* If we've satisfied the current request,
771 terminate it successfully. */
772 if (rq->nr_sectors == 0) {
773 cdrom_end_request(drive, 1);
774 return -1;
775 }
776
777 /* Move on to the next buffer if needed. */
778 if (rq->current_nr_sectors == 0)
779 cdrom_end_request(drive, 1);
780
781 /* If this condition does not hold, then the kluge i use to
782 represent the number of sectors to skip at the start of a transfer
783 will fail. I think that this will never happen, but let's be
784 paranoid and check. */
785 if (rq->current_nr_sectors < bio_cur_sectors(rq->bio) &&
786 (rq->sector & (sectors_per_frame - 1))) {
787 printk(KERN_ERR "%s: cdrom_read_from_buffer: buffer botch (%ld)\n",
788 drive->name, (long)rq->sector);
789 cdrom_end_request(drive, 0);
790 return -1;
791 }
792
793 return 0;
794}
795
Bartlomiej Zolnierkiewicz0041e7c2008-02-01 23:09:28 +0100796static ide_startstop_t cdrom_newpc_intr(ide_drive_t *);
Bartlomiej Zolnierkiewicz94f5a862008-02-01 23:09:26 +0100797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798/*
Bartlomiej Zolnierkiewicz29f3aac2008-02-01 23:09:27 +0100799 * Routine to send a read/write packet command to the drive.
800 * This is usually called directly from cdrom_start_{read,write}().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 * However, for drq_interrupt devices, it is called from an interrupt
802 * when the drive is ready to accept the command.
803 */
Bartlomiej Zolnierkiewicz29f3aac2008-02-01 23:09:27 +0100804static ide_startstop_t cdrom_start_rw_cont(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
806 struct request *rq = HWGROUP(drive)->rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Bartlomiej Zolnierkiewicz29f3aac2008-02-01 23:09:27 +0100808 if (rq_data_dir(rq) == READ) {
809 unsigned short sectors_per_frame =
810 queue_hardsect_size(drive->queue) >> SECTOR_BITS;
811 int nskip = rq->sector & (sectors_per_frame - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Bartlomiej Zolnierkiewicz29f3aac2008-02-01 23:09:27 +0100813 /*
814 * If the requested sector doesn't start on a frame boundary,
815 * we must adjust the start of the transfer so that it does,
816 * and remember to skip the first few sectors.
817 *
818 * If the rq->current_nr_sectors field is larger than the size
819 * of the buffer, it will mean that we're to skip a number of
820 * sectors equal to the amount by which rq->current_nr_sectors
821 * is larger than the buffer size.
822 */
823 if (nskip > 0) {
824 /* Sanity check... */
825 if (rq->current_nr_sectors !=
826 bio_cur_sectors(rq->bio)) {
827 printk(KERN_ERR "%s: %s: buffer botch (%u)\n",
828 drive->name, __FUNCTION__,
829 rq->current_nr_sectors);
830 cdrom_end_request(drive, 0);
831 return ide_stopped;
832 }
833 rq->current_nr_sectors += nskip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 }
Bartlomiej Zolnierkiewicz29f3aac2008-02-01 23:09:27 +0100836#if 0
837 else
838 /* the immediate bit */
839 rq->cmd[1] = 1 << 3;
840#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 /* Set up the command */
842 rq->timeout = ATAPI_WAIT_PC;
843
844 /* Send the command to the drive and return. */
Bartlomiej Zolnierkiewicz0041e7c2008-02-01 23:09:28 +0100845 return cdrom_transfer_packet_command(drive, rq, cdrom_newpc_intr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846}
847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848#define IDECD_SEEK_THRESHOLD (1000) /* 1000 blocks */
849#define IDECD_SEEK_TIMER (5 * WAIT_MIN_SLEEP) /* 100 ms */
850#define IDECD_SEEK_TIMEOUT (2 * WAIT_CMD) /* 20 sec */
851
852static ide_startstop_t cdrom_seek_intr (ide_drive_t *drive)
853{
854 struct cdrom_info *info = drive->driver_data;
855 int stat;
856 static int retry = 10;
857
858 if (cdrom_decode_status(drive, 0, &stat))
859 return ide_stopped;
Bartlomiej Zolnierkiewicz4fe67172008-02-01 23:09:21 +0100860
Bartlomiej Zolnierkiewicz2bc4cf22008-02-01 23:09:22 +0100861 info->cd_flags |= IDE_CD_FLAG_SEEKING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
863 if (retry && time_after(jiffies, info->start_seek + IDECD_SEEK_TIMER)) {
864 if (--retry == 0) {
865 /*
866 * this condition is far too common, to bother
867 * users about it
868 */
869 /* printk("%s: disabled DSC seek overlap\n", drive->name);*/
870 drive->dsc_overlap = 0;
871 }
872 }
873 return ide_stopped;
874}
875
876static ide_startstop_t cdrom_start_seek_continuation (ide_drive_t *drive)
877{
878 struct request *rq = HWGROUP(drive)->rq;
879 sector_t frame = rq->sector;
880
881 sector_div(frame, queue_hardsect_size(drive->queue) >> SECTOR_BITS);
882
883 memset(rq->cmd, 0, sizeof(rq->cmd));
884 rq->cmd[0] = GPCMD_SEEK;
885 put_unaligned(cpu_to_be32(frame), (unsigned int *) &rq->cmd[2]);
886
887 rq->timeout = ATAPI_WAIT_PC;
888 return cdrom_transfer_packet_command(drive, rq, &cdrom_seek_intr);
889}
890
891static ide_startstop_t cdrom_start_seek (ide_drive_t *drive, unsigned int block)
892{
893 struct cdrom_info *info = drive->driver_data;
894
895 info->dma = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 info->start_seek = jiffies;
897 return cdrom_start_packet_command(drive, 0, cdrom_start_seek_continuation);
898}
899
900/* Fix up a possibly partially-processed request so that we can
901 start it over entirely, or even put it back on the request queue. */
902static void restore_request (struct request *rq)
903{
904 if (rq->buffer != bio_data(rq->bio)) {
905 sector_t n = (rq->buffer - (char *) bio_data(rq->bio)) / SECTOR_SIZE;
906
907 rq->buffer = bio_data(rq->bio);
908 rq->nr_sectors += n;
909 rq->sector -= n;
910 }
911 rq->hard_cur_sectors = rq->current_nr_sectors = bio_cur_sectors(rq->bio);
912 rq->hard_nr_sectors = rq->nr_sectors;
913 rq->hard_sector = rq->sector;
914 rq->q->prep_rq_fn(rq->q, rq);
915}
916
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917/****************************************************************************
918 * Execute all other packet commands.
919 */
920
Bartlomiej Zolnierkiewicz8ee69f52008-02-01 23:09:25 +0100921static void ide_cd_request_sense_fixup(struct request *rq)
922{
923 /*
924 * Some of the trailing request sense fields are optional,
925 * and some drives don't send them. Sigh.
926 */
927 if (rq->cmd[0] == GPCMD_REQUEST_SENSE &&
928 rq->data_len > 0 && rq->data_len <= 5)
929 while (rq->data_len > 0) {
930 *(u8 *)rq->data++ = 0;
931 --rq->data_len;
932 }
933}
934
Bartlomiej Zolnierkiewicz17802992008-02-01 23:09:25 +0100935int ide_cd_queue_pc(ide_drive_t *drive, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936{
937 struct request_sense sense;
938 int retries = 10;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200939 unsigned int flags = rq->cmd_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
941 if (rq->sense == NULL)
942 rq->sense = &sense;
943
944 /* Start of retry loop. */
945 do {
946 int error;
947 unsigned long time = jiffies;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200948 rq->cmd_flags = flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
950 error = ide_do_drive_cmd(drive, rq, ide_wait);
951 time = jiffies - time;
952
953 /* FIXME: we should probably abort/retry or something
954 * in case of failure */
Jens Axboe4aff5e22006-08-10 08:44:47 +0200955 if (rq->cmd_flags & REQ_FAILED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 /* The request failed. Retry if it was due to a unit
957 attention status
958 (usually means media was changed). */
959 struct request_sense *reqbuf = rq->sense;
960
961 if (reqbuf->sense_key == UNIT_ATTENTION)
962 cdrom_saw_media_change(drive);
963 else if (reqbuf->sense_key == NOT_READY &&
964 reqbuf->asc == 4 && reqbuf->ascq != 4) {
965 /* The drive is in the process of loading
966 a disk. Retry, but wait a little to give
967 the drive time to complete the load. */
968 ssleep(2);
969 } else {
970 /* Otherwise, don't retry. */
971 retries = 0;
972 }
973 --retries;
974 }
975
976 /* End of retry loop. */
Jens Axboe4aff5e22006-08-10 08:44:47 +0200977 } while ((rq->cmd_flags & REQ_FAILED) && retries >= 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
979 /* Return an error if the command failed. */
Jens Axboe4aff5e22006-08-10 08:44:47 +0200980 return (rq->cmd_flags & REQ_FAILED) ? -EIO : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981}
982
983/*
Kiyoshi Uedaaaa04c22007-12-11 17:51:23 -0500984 * Called from blk_end_request_callback() after the data of the request
985 * is completed and before the request is completed.
986 * By returning value '1', blk_end_request_callback() returns immediately
987 * without completing the request.
988 */
989static int cdrom_newpc_intr_dummy_cb(struct request *rq)
990{
991 return 1;
992}
993
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994static ide_startstop_t cdrom_newpc_intr(ide_drive_t *drive)
995{
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +0200996 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 struct cdrom_info *info = drive->driver_data;
998 struct request *rq = HWGROUP(drive)->rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 xfer_func_t *xferfunc;
Bartlomiej Zolnierkiewicz0041e7c2008-02-01 23:09:28 +01001000 ide_expiry_t *expiry = NULL;
Bartlomiej Zolnierkiewiczff1bfbc2008-02-01 23:09:25 +01001001 int dma_error = 0, dma, stat, ireason, len, thislen, uptodate = 0;
1002 int write = (rq_data_dir(rq) == WRITE) ? 1 : 0;
1003 unsigned int timeout;
1004 u8 lowcyl, highcyl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
1006 /* Check for errors. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 dma = info->dma;
1008 if (dma) {
1009 info->dma = 0;
1010 dma_error = HWIF(drive)->ide_dma_end(drive);
Bartlomiej Zolnierkiewiczeba15fb2008-02-01 23:09:17 +01001011 if (dma_error) {
1012 printk(KERN_ERR "%s: DMA %s error\n", drive->name,
Bartlomiej Zolnierkiewiczff1bfbc2008-02-01 23:09:25 +01001013 write ? "write" : "read");
Bartlomiej Zolnierkiewiczeba15fb2008-02-01 23:09:17 +01001014 ide_dma_off(drive);
1015 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 }
1017
1018 if (cdrom_decode_status(drive, 0, &stat))
1019 return ide_stopped;
1020
1021 /*
1022 * using dma, transfer is complete now
1023 */
1024 if (dma) {
Bartlomiej Zolnierkiewiczeba15fb2008-02-01 23:09:17 +01001025 if (dma_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 return ide_error(drive, "dma error", stat);
Bartlomiej Zolnierkiewicz0041e7c2008-02-01 23:09:28 +01001027 if (blk_fs_request(rq)) {
1028 ide_end_request(drive, 1, rq->nr_sectors);
1029 return ide_stopped;
1030 }
Bartlomiej Zolnierkiewiczff1bfbc2008-02-01 23:09:25 +01001031 goto end_request;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 }
1033
1034 /*
1035 * ok we fall to pio :/
1036 */
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +02001037 ireason = hwif->INB(hwif->io_ports[IDE_IREASON_OFFSET]) & 0x3;
1038 lowcyl = hwif->INB(hwif->io_ports[IDE_BCOUNTL_OFFSET]);
1039 highcyl = hwif->INB(hwif->io_ports[IDE_BCOUNTH_OFFSET]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
1041 len = lowcyl + (256 * highcyl);
Bartlomiej Zolnierkiewicz0041e7c2008-02-01 23:09:28 +01001042
1043 thislen = blk_fs_request(rq) ? len : rq->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 if (thislen > len)
1045 thislen = len;
1046
1047 /*
1048 * If DRQ is clear, the command has completed.
1049 */
Kiyoshi Uedaaaa04c22007-12-11 17:51:23 -05001050 if ((stat & DRQ_STAT) == 0) {
Bartlomiej Zolnierkiewicz0041e7c2008-02-01 23:09:28 +01001051 if (blk_fs_request(rq)) {
1052 /*
1053 * If we're not done reading/writing, complain.
1054 * Otherwise, complete the command normally.
1055 */
1056 uptodate = 1;
1057 if (rq->current_nr_sectors > 0) {
1058 printk(KERN_ERR "%s: %s: data underrun "
1059 "(%d blocks)\n",
1060 drive->name, __FUNCTION__,
1061 rq->current_nr_sectors);
1062 if (!write)
1063 rq->cmd_flags |= REQ_FAILED;
1064 uptodate = 0;
1065 }
1066 cdrom_end_request(drive, uptodate);
1067 return ide_stopped;
1068 } else if (!blk_pc_request(rq)) {
Bartlomiej Zolnierkiewiczff1bfbc2008-02-01 23:09:25 +01001069 ide_cd_request_sense_fixup(rq);
1070 /* Complain if we still have data left to transfer. */
1071 uptodate = rq->data_len ? 0 : 1;
1072 }
1073 goto end_request;
Kiyoshi Uedaaaa04c22007-12-11 17:51:23 -05001074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
1076 /*
1077 * check which way to transfer data
1078 */
Bartlomiej Zolnierkiewicz9f10d9e2008-02-26 21:50:35 +01001079 if (ide_cd_check_ireason(drive, rq, len, ireason, write))
1080 return ide_stopped;
Bartlomiej Zolnierkiewicz0041e7c2008-02-01 23:09:28 +01001081
Bartlomiej Zolnierkiewicz9f10d9e2008-02-26 21:50:35 +01001082 if (blk_fs_request(rq)) {
1083 if (write == 0) {
Bartlomiej Zolnierkiewicz0041e7c2008-02-01 23:09:28 +01001084 int nskip;
1085
1086 if (ide_cd_check_transfer_size(drive, len)) {
1087 cdrom_end_request(drive, 0);
1088 return ide_stopped;
1089 }
1090
1091 /*
1092 * First, figure out if we need to bit-bucket
1093 * any of the leading sectors.
1094 */
1095 nskip = min_t(int, rq->current_nr_sectors
1096 - bio_cur_sectors(rq->bio),
1097 thislen >> 9);
1098 if (nskip > 0) {
1099 ide_cd_drain_data(drive, nskip);
1100 rq->current_nr_sectors -= nskip;
1101 thislen -= (nskip << 9);
1102 }
1103 }
Bartlomiej Zolnierkiewiczff1bfbc2008-02-01 23:09:25 +01001104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
Bartlomiej Zolnierkiewiczff1bfbc2008-02-01 23:09:25 +01001106 if (ireason == 0) {
1107 write = 1;
1108 xferfunc = HWIF(drive)->atapi_output_bytes;
Bartlomiej Zolnierkiewicz9f10d9e2008-02-26 21:50:35 +01001109 } else {
Bartlomiej Zolnierkiewiczff1bfbc2008-02-01 23:09:25 +01001110 write = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 xferfunc = HWIF(drive)->atapi_input_bytes;
1112 }
1113
1114 /*
1115 * transfer data
1116 */
1117 while (thislen > 0) {
Bartlomiej Zolnierkiewicz0041e7c2008-02-01 23:09:28 +01001118 u8 *ptr = blk_fs_request(rq) ? NULL : rq->data;
Bartlomiej Zolnierkiewicza11e77d2008-02-01 23:09:27 +01001119 int blen = rq->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
1121 /*
1122 * bio backed?
1123 */
1124 if (rq->bio) {
Bartlomiej Zolnierkiewicz0041e7c2008-02-01 23:09:28 +01001125 if (blk_fs_request(rq)) {
1126 ptr = rq->buffer;
1127 blen = rq->current_nr_sectors << 9;
1128 } else {
1129 ptr = bio_data(rq->bio);
1130 blen = bio_iovec(rq->bio)->bv_len;
1131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 }
1133
1134 if (!ptr) {
Bartlomiej Zolnierkiewicz0041e7c2008-02-01 23:09:28 +01001135 if (blk_fs_request(rq) && !write)
1136 /*
1137 * If the buffers are full, cache the rest
1138 * of the data in our internal buffer.
1139 */
1140 cdrom_buffer_sectors(drive, rq->sector,
1141 thislen >> 9);
1142 else {
1143 printk(KERN_ERR "%s: confused, missing data\n",
1144 drive->name);
1145 blk_dump_rq_flags(rq, rq_data_dir(rq)
1146 ? "cdrom_newpc_intr, write"
1147 : "cdrom_newpc_intr, read");
1148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 break;
1150 }
1151
1152 if (blen > thislen)
1153 blen = thislen;
1154
1155 xferfunc(drive, ptr, blen);
1156
1157 thislen -= blen;
1158 len -= blen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Bartlomiej Zolnierkiewicz0041e7c2008-02-01 23:09:28 +01001160 if (blk_fs_request(rq)) {
1161 rq->buffer += blen;
1162 rq->nr_sectors -= (blen >> 9);
1163 rq->current_nr_sectors -= (blen >> 9);
1164 rq->sector += (blen >> 9);
1165
1166 if (rq->current_nr_sectors == 0 && rq->nr_sectors)
1167 cdrom_end_request(drive, 1);
1168 } else {
1169 rq->data_len -= blen;
1170
Kiyoshi Uedaaaa04c22007-12-11 17:51:23 -05001171 /*
1172 * The request can't be completed until DRQ is cleared.
1173 * So complete the data, but don't complete the request
1174 * using the dummy function for the callback feature
1175 * of blk_end_request_callback().
1176 */
Bartlomiej Zolnierkiewicz0041e7c2008-02-01 23:09:28 +01001177 if (rq->bio)
1178 blk_end_request_callback(rq, 0, blen,
Kiyoshi Uedaaaa04c22007-12-11 17:51:23 -05001179 cdrom_newpc_intr_dummy_cb);
Bartlomiej Zolnierkiewicz0041e7c2008-02-01 23:09:28 +01001180 else
1181 rq->data += blen;
1182 }
Andreas Schwabbcd88ac2008-02-26 21:50:35 +01001183 if (!write && blk_sense_request(rq))
1184 rq->sense_len += blen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 }
1186
1187 /*
1188 * pad, if necessary
1189 */
Bartlomiej Zolnierkiewicz0041e7c2008-02-01 23:09:28 +01001190 if (!blk_fs_request(rq) && len > 0)
Bartlomiej Zolnierkiewicz5a5222d2008-02-01 23:09:17 +01001191 ide_cd_pad_transfer(drive, xferfunc, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
Bartlomiej Zolnierkiewiczff1bfbc2008-02-01 23:09:25 +01001193 if (blk_pc_request(rq)) {
1194 timeout = rq->timeout;
Bartlomiej Zolnierkiewiczff1bfbc2008-02-01 23:09:25 +01001195 } else {
1196 timeout = ATAPI_WAIT_PC;
Bartlomiej Zolnierkiewicz0041e7c2008-02-01 23:09:28 +01001197 if (!blk_fs_request(rq))
1198 expiry = cdrom_timer_expiry;
Bartlomiej Zolnierkiewiczff1bfbc2008-02-01 23:09:25 +01001199 }
1200
1201 ide_set_handler(drive, cdrom_newpc_intr, timeout, expiry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 return ide_started;
Bartlomiej Zolnierkiewiczff1bfbc2008-02-01 23:09:25 +01001203
1204end_request:
1205 if (blk_pc_request(rq)) {
1206 unsigned long flags;
Kiyoshi Ueda14e04c32008-02-19 01:41:26 +01001207 unsigned int dlen = rq->data_len;
1208
1209 if (dma)
1210 rq->data_len = 0;
Bartlomiej Zolnierkiewiczff1bfbc2008-02-01 23:09:25 +01001211
1212 spin_lock_irqsave(&ide_lock, flags);
Kiyoshi Ueda14e04c32008-02-19 01:41:26 +01001213 if (__blk_end_request(rq, 0, dlen))
Bartlomiej Zolnierkiewiczff1bfbc2008-02-01 23:09:25 +01001214 BUG();
1215 HWGROUP(drive)->rq = NULL;
1216 spin_unlock_irqrestore(&ide_lock, flags);
1217 } else {
1218 if (!uptodate)
1219 rq->cmd_flags |= REQ_FAILED;
1220 cdrom_end_request(drive, uptodate);
1221 }
1222 return ide_stopped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223}
1224
Bartlomiej Zolnierkiewicz21ea1f02008-02-01 23:09:27 +01001225static ide_startstop_t cdrom_start_rw(ide_drive_t *drive, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226{
Bartlomiej Zolnierkiewicz21ea1f02008-02-01 23:09:27 +01001227 struct cdrom_info *cd = drive->driver_data;
1228 int write = rq_data_dir(rq) == WRITE;
1229 unsigned short sectors_per_frame =
1230 queue_hardsect_size(drive->queue) >> SECTOR_BITS;
1231
1232 if (write) {
1233 /*
1234 * disk has become write protected
1235 */
1236 if (cd->disk->policy) {
1237 cdrom_end_request(drive, 0);
1238 return ide_stopped;
1239 }
1240 } else {
1241 /*
1242 * We may be retrying this request after an error. Fix up any
1243 * weirdness which might be present in the request packet.
1244 */
1245 restore_request(rq);
1246
1247 /* Satisfy whatever we can of this request from our cache. */
1248 if (cdrom_read_from_buffer(drive))
1249 return ide_stopped;
1250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
1252 /*
Bartlomiej Zolnierkiewicz21ea1f02008-02-01 23:09:27 +01001253 * use DMA, if possible / writes *must* be hardware frame aligned
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 */
1255 if ((rq->nr_sectors & (sectors_per_frame - 1)) ||
1256 (rq->sector & (sectors_per_frame - 1))) {
Bartlomiej Zolnierkiewicz21ea1f02008-02-01 23:09:27 +01001257 if (write) {
1258 cdrom_end_request(drive, 0);
1259 return ide_stopped;
1260 }
1261 cd->dma = 0;
1262 } else
1263 cd->dma = drive->using_dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
Bartlomiej Zolnierkiewicz21ea1f02008-02-01 23:09:27 +01001265 /* Clear the local sector buffer. */
1266 cd->nsectors_buffered = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
Bartlomiej Zolnierkiewicz21ea1f02008-02-01 23:09:27 +01001268 if (write)
1269 cd->devinfo.media_written = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
Bartlomiej Zolnierkiewicz21ea1f02008-02-01 23:09:27 +01001271 /* Start sending the read/write request to the drive. */
Bartlomiej Zolnierkiewicz29f3aac2008-02-01 23:09:27 +01001272 return cdrom_start_packet_command(drive, 32768, cdrom_start_rw_cont);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273}
1274
1275static ide_startstop_t cdrom_do_newpc_cont(ide_drive_t *drive)
1276{
1277 struct request *rq = HWGROUP(drive)->rq;
1278
1279 if (!rq->timeout)
1280 rq->timeout = ATAPI_WAIT_PC;
1281
1282 return cdrom_transfer_packet_command(drive, rq, cdrom_newpc_intr);
1283}
1284
1285static ide_startstop_t cdrom_do_block_pc(ide_drive_t *drive, struct request *rq)
1286{
1287 struct cdrom_info *info = drive->driver_data;
1288
Bartlomiej Zolnierkiewiczc9f56a82008-02-01 23:09:26 +01001289 if (blk_pc_request(rq))
1290 rq->cmd_flags |= REQ_QUIET;
1291 else
1292 rq->cmd_flags &= ~REQ_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
1294 info->dma = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
1296 /*
1297 * sg request
1298 */
1299 if (rq->bio) {
1300 int mask = drive->queue->dma_alignment;
1301 unsigned long addr = (unsigned long) page_address(bio_page(rq->bio));
1302
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 info->dma = drive->using_dma;
1304
1305 /*
1306 * check if dma is safe
Linus Torvalds5d9e4ea2005-05-27 07:36:17 -07001307 *
1308 * NOTE! The "len" and "addr" checks should possibly have
1309 * separate masks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 */
Jens Axboe4e7c6812005-05-31 17:47:36 +02001311 if ((rq->data_len & 15) || (addr & mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 info->dma = 0;
1313 }
1314
1315 /* Start sending the command to the drive. */
1316 return cdrom_start_packet_command(drive, rq->data_len, cdrom_do_newpc_cont);
1317}
1318
1319/****************************************************************************
1320 * cdrom driver request routine.
1321 */
1322static ide_startstop_t
1323ide_do_rw_cdrom (ide_drive_t *drive, struct request *rq, sector_t block)
1324{
1325 ide_startstop_t action;
1326 struct cdrom_info *info = drive->driver_data;
1327
1328 if (blk_fs_request(rq)) {
Bartlomiej Zolnierkiewicz2bc4cf22008-02-01 23:09:22 +01001329 if (info->cd_flags & IDE_CD_FLAG_SEEKING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 unsigned long elapsed = jiffies - info->start_seek;
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +01001331 int stat = ide_read_status(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
1333 if ((stat & SEEK_STAT) != SEEK_STAT) {
1334 if (elapsed < IDECD_SEEK_TIMEOUT) {
1335 ide_stall_queue(drive, IDECD_SEEK_TIMER);
1336 return ide_stopped;
1337 }
1338 printk (KERN_ERR "%s: DSC timeout\n", drive->name);
1339 }
Bartlomiej Zolnierkiewicz2bc4cf22008-02-01 23:09:22 +01001340 info->cd_flags &= ~IDE_CD_FLAG_SEEKING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 }
1342 if ((rq_data_dir(rq) == READ) && IDE_LARGE_SEEK(info->last_block, block, IDECD_SEEK_THRESHOLD) && drive->dsc_overlap) {
1343 action = cdrom_start_seek(drive, block);
Bartlomiej Zolnierkiewicz21ea1f02008-02-01 23:09:27 +01001344 } else
1345 action = cdrom_start_rw(drive, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 info->last_block = block;
1347 return action;
Bartlomiej Zolnierkiewiczc9f56a82008-02-01 23:09:26 +01001348 } else if (blk_sense_request(rq) || blk_pc_request(rq) ||
Jens Axboecea28852006-10-12 15:08:45 +02001349 rq->cmd_type == REQ_TYPE_ATA_PC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 return cdrom_do_block_pc(drive, rq);
Jens Axboe4aff5e22006-08-10 08:44:47 +02001351 } else if (blk_special_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 /*
1353 * right now this can only be a reset...
1354 */
1355 cdrom_end_request(drive, 1);
1356 return ide_stopped;
1357 }
1358
1359 blk_dump_rq_flags(rq, "ide-cd bad flags");
1360 cdrom_end_request(drive, 0);
1361 return ide_stopped;
1362}
1363
1364
1365
1366/****************************************************************************
1367 * Ioctl handling.
1368 *
1369 * Routines which queue packet commands take as a final argument a pointer
1370 * to a request_sense struct. If execution of the command results
1371 * in an error with a CHECK CONDITION status, this structure will be filled
1372 * with the results of the subsequent request sense command. The pointer
1373 * can also be NULL, in which case no sense information is returned.
1374 */
1375
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376static
1377void msf_from_bcd (struct atapi_msf *msf)
1378{
Bartlomiej Zolnierkiewicz9a6dc662008-02-01 23:09:22 +01001379 msf->minute = BCD2BIN(msf->minute);
1380 msf->second = BCD2BIN(msf->second);
1381 msf->frame = BCD2BIN(msf->frame);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382}
1383
Borislav Petkovf9afd182008-02-01 23:09:29 +01001384int cdrom_check_status(ide_drive_t *drive, struct request_sense *sense)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385{
1386 struct request req;
1387 struct cdrom_info *info = drive->driver_data;
1388 struct cdrom_device_info *cdi = &info->devinfo;
1389
Bartlomiej Zolnierkiewicz139c8292008-02-01 23:09:24 +01001390 ide_cd_init_rq(drive, &req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
1392 req.sense = sense;
1393 req.cmd[0] = GPCMD_TEST_UNIT_READY;
Jens Axboe4aff5e22006-08-10 08:44:47 +02001394 req.cmd_flags |= REQ_QUIET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
Bartlomiej Zolnierkiewiczcdf60002008-02-01 23:09:22 +01001396 /*
1397 * Sanyo 3 CD changer uses byte 7 of TEST_UNIT_READY to
1398 * switch CDs instead of supporting the LOAD_UNLOAD opcode.
1399 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 req.cmd[7] = cdi->sanyo_slot % 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
Bartlomiej Zolnierkiewicz139c8292008-02-01 23:09:24 +01001402 return ide_cd_queue_pc(drive, &req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403}
1404
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405static int cdrom_read_capacity(ide_drive_t *drive, unsigned long *capacity,
1406 unsigned long *sectors_per_frame,
1407 struct request_sense *sense)
1408{
1409 struct {
1410 __u32 lba;
1411 __u32 blocklen;
1412 } capbuf;
1413
1414 int stat;
1415 struct request req;
1416
Bartlomiej Zolnierkiewicz139c8292008-02-01 23:09:24 +01001417 ide_cd_init_rq(drive, &req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
1419 req.sense = sense;
1420 req.cmd[0] = GPCMD_READ_CDVD_CAPACITY;
1421 req.data = (char *)&capbuf;
1422 req.data_len = sizeof(capbuf);
Jens Axboe4aff5e22006-08-10 08:44:47 +02001423 req.cmd_flags |= REQ_QUIET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
Bartlomiej Zolnierkiewicz139c8292008-02-01 23:09:24 +01001425 stat = ide_cd_queue_pc(drive, &req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 if (stat == 0) {
1427 *capacity = 1 + be32_to_cpu(capbuf.lba);
1428 *sectors_per_frame =
1429 be32_to_cpu(capbuf.blocklen) >> SECTOR_BITS;
1430 }
1431
1432 return stat;
1433}
1434
1435static int cdrom_read_tocentry(ide_drive_t *drive, int trackno, int msf_flag,
1436 int format, char *buf, int buflen,
1437 struct request_sense *sense)
1438{
1439 struct request req;
1440
Bartlomiej Zolnierkiewicz139c8292008-02-01 23:09:24 +01001441 ide_cd_init_rq(drive, &req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
1443 req.sense = sense;
1444 req.data = buf;
1445 req.data_len = buflen;
Jens Axboe4aff5e22006-08-10 08:44:47 +02001446 req.cmd_flags |= REQ_QUIET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 req.cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
1448 req.cmd[6] = trackno;
1449 req.cmd[7] = (buflen >> 8);
1450 req.cmd[8] = (buflen & 0xff);
1451 req.cmd[9] = (format << 6);
1452
1453 if (msf_flag)
1454 req.cmd[1] = 2;
1455
Bartlomiej Zolnierkiewicz139c8292008-02-01 23:09:24 +01001456 return ide_cd_queue_pc(drive, &req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457}
1458
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459/* Try to read the entire TOC for the disk into our internal buffer. */
Bartlomiej Zolnierkiewicz17802992008-02-01 23:09:25 +01001460int ide_cd_read_toc(ide_drive_t *drive, struct request_sense *sense)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461{
1462 int stat, ntracks, i;
1463 struct cdrom_info *info = drive->driver_data;
1464 struct cdrom_device_info *cdi = &info->devinfo;
1465 struct atapi_toc *toc = info->toc;
1466 struct {
1467 struct atapi_toc_header hdr;
1468 struct atapi_toc_entry ent;
1469 } ms_tmp;
1470 long last_written;
1471 unsigned long sectors_per_frame = SECTORS_PER_FRAME;
1472
1473 if (toc == NULL) {
1474 /* Try to allocate space. */
Jesper Juhl2a91f3e2005-10-30 15:02:45 -08001475 toc = kmalloc(sizeof(struct atapi_toc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 if (toc == NULL) {
1477 printk (KERN_ERR "%s: No cdrom TOC buffer!\n", drive->name);
1478 return -ENOMEM;
1479 }
Jesper Juhl2a91f3e2005-10-30 15:02:45 -08001480 info->toc = toc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 }
1482
1483 /* Check to see if the existing data is still valid.
1484 If it is, just return. */
1485 (void) cdrom_check_status(drive, sense);
1486
Bartlomiej Zolnierkiewicz2bc4cf22008-02-01 23:09:22 +01001487 if (info->cd_flags & IDE_CD_FLAG_TOC_VALID)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 return 0;
1489
1490 /* Try to get the total cdrom capacity and sector size. */
1491 stat = cdrom_read_capacity(drive, &toc->capacity, &sectors_per_frame,
1492 sense);
1493 if (stat)
1494 toc->capacity = 0x1fffff;
1495
1496 set_capacity(info->disk, toc->capacity * sectors_per_frame);
Alan Coxdbe217a2006-06-25 05:47:44 -07001497 /* Save a private copy of te TOC capacity for error handling */
1498 drive->probed_capacity = toc->capacity * sectors_per_frame;
1499
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 blk_queue_hardsect_size(drive->queue,
1501 sectors_per_frame << SECTOR_BITS);
1502
1503 /* First read just the header, so we know how long the TOC is. */
1504 stat = cdrom_read_tocentry(drive, 0, 1, 0, (char *) &toc->hdr,
1505 sizeof(struct atapi_toc_header), sense);
Jesper Juhl2a91f3e2005-10-30 15:02:45 -08001506 if (stat)
1507 return stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
Bartlomiej Zolnierkiewicz2bc4cf22008-02-01 23:09:22 +01001509 if (info->cd_flags & IDE_CD_FLAG_TOCTRACKS_AS_BCD) {
Bartlomiej Zolnierkiewicz9a6dc662008-02-01 23:09:22 +01001510 toc->hdr.first_track = BCD2BIN(toc->hdr.first_track);
1511 toc->hdr.last_track = BCD2BIN(toc->hdr.last_track);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
1514 ntracks = toc->hdr.last_track - toc->hdr.first_track + 1;
1515 if (ntracks <= 0)
1516 return -EIO;
1517 if (ntracks > MAX_TRACKS)
1518 ntracks = MAX_TRACKS;
1519
1520 /* Now read the whole schmeer. */
1521 stat = cdrom_read_tocentry(drive, toc->hdr.first_track, 1, 0,
1522 (char *)&toc->hdr,
1523 sizeof(struct atapi_toc_header) +
1524 (ntracks + 1) *
1525 sizeof(struct atapi_toc_entry), sense);
1526
1527 if (stat && toc->hdr.first_track > 1) {
1528 /* Cds with CDI tracks only don't have any TOC entries,
1529 despite of this the returned values are
1530 first_track == last_track = number of CDI tracks + 1,
1531 so that this case is indistinguishable from the same
1532 layout plus an additional audio track.
1533 If we get an error for the regular case, we assume
1534 a CDI without additional audio tracks. In this case
1535 the readable TOC is empty (CDI tracks are not included)
Jan Engelhardt96de0e22007-10-19 23:21:04 +02001536 and only holds the Leadout entry. Heiko Eißfeldt */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 ntracks = 0;
1538 stat = cdrom_read_tocentry(drive, CDROM_LEADOUT, 1, 0,
1539 (char *)&toc->hdr,
1540 sizeof(struct atapi_toc_header) +
1541 (ntracks + 1) *
1542 sizeof(struct atapi_toc_entry),
1543 sense);
Bartlomiej Zolnierkiewiczcdf60002008-02-01 23:09:22 +01001544 if (stat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 return stat;
Bartlomiej Zolnierkiewiczcdf60002008-02-01 23:09:22 +01001546
Bartlomiej Zolnierkiewicz2bc4cf22008-02-01 23:09:22 +01001547 if (info->cd_flags & IDE_CD_FLAG_TOCTRACKS_AS_BCD) {
Bartlomiej Zolnierkiewicz9a6dc662008-02-01 23:09:22 +01001548 toc->hdr.first_track = (u8)BIN2BCD(CDROM_LEADOUT);
1549 toc->hdr.last_track = (u8)BIN2BCD(CDROM_LEADOUT);
Bartlomiej Zolnierkiewiczcdf60002008-02-01 23:09:22 +01001550 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 toc->hdr.first_track = CDROM_LEADOUT;
1552 toc->hdr.last_track = CDROM_LEADOUT;
1553 }
1554 }
1555
1556 if (stat)
1557 return stat;
1558
Borislav Petkov7eb43fd2008-02-11 00:32:13 +01001559 toc->hdr.toc_length = be16_to_cpu(toc->hdr.toc_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
Bartlomiej Zolnierkiewicz2bc4cf22008-02-01 23:09:22 +01001561 if (info->cd_flags & IDE_CD_FLAG_TOCTRACKS_AS_BCD) {
Bartlomiej Zolnierkiewicz9a6dc662008-02-01 23:09:22 +01001562 toc->hdr.first_track = BCD2BIN(toc->hdr.first_track);
1563 toc->hdr.last_track = BCD2BIN(toc->hdr.last_track);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
Bartlomiej Zolnierkiewiczcdf60002008-02-01 23:09:22 +01001566 for (i = 0; i <= ntracks; i++) {
Bartlomiej Zolnierkiewicz2bc4cf22008-02-01 23:09:22 +01001567 if (info->cd_flags & IDE_CD_FLAG_TOCADDR_AS_BCD) {
1568 if (info->cd_flags & IDE_CD_FLAG_TOCTRACKS_AS_BCD)
Bartlomiej Zolnierkiewicz9a6dc662008-02-01 23:09:22 +01001569 toc->ent[i].track = BCD2BIN(toc->ent[i].track);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 msf_from_bcd(&toc->ent[i].addr.msf);
1571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 toc->ent[i].addr.lba = msf_to_lba (toc->ent[i].addr.msf.minute,
1573 toc->ent[i].addr.msf.second,
1574 toc->ent[i].addr.msf.frame);
1575 }
1576
1577 /* Read the multisession information. */
1578 if (toc->hdr.first_track != CDROM_LEADOUT) {
1579 /* Read the multisession information. */
1580 stat = cdrom_read_tocentry(drive, 0, 0, 1, (char *)&ms_tmp,
1581 sizeof(ms_tmp), sense);
Jesper Juhl2a91f3e2005-10-30 15:02:45 -08001582 if (stat)
1583 return stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
1585 toc->last_session_lba = be32_to_cpu(ms_tmp.ent.addr.lba);
1586 } else {
1587 ms_tmp.hdr.first_track = ms_tmp.hdr.last_track = CDROM_LEADOUT;
1588 toc->last_session_lba = msf_to_lba(0, 2, 0); /* 0m 2s 0f */
1589 }
1590
Bartlomiej Zolnierkiewicz2bc4cf22008-02-01 23:09:22 +01001591 if (info->cd_flags & IDE_CD_FLAG_TOCADDR_AS_BCD) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 /* Re-read multisession information using MSF format */
1593 stat = cdrom_read_tocentry(drive, 0, 1, 1, (char *)&ms_tmp,
1594 sizeof(ms_tmp), sense);
1595 if (stat)
1596 return stat;
1597
1598 msf_from_bcd (&ms_tmp.ent.addr.msf);
1599 toc->last_session_lba = msf_to_lba(ms_tmp.ent.addr.msf.minute,
1600 ms_tmp.ent.addr.msf.second,
1601 ms_tmp.ent.addr.msf.frame);
1602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
1604 toc->xa_flag = (ms_tmp.hdr.first_track != ms_tmp.hdr.last_track);
1605
1606 /* Now try to get the total cdrom capacity. */
1607 stat = cdrom_get_last_written(cdi, &last_written);
1608 if (!stat && (last_written > toc->capacity)) {
1609 toc->capacity = last_written;
1610 set_capacity(info->disk, toc->capacity * sectors_per_frame);
Alan Coxdbe217a2006-06-25 05:47:44 -07001611 drive->probed_capacity = toc->capacity * sectors_per_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 }
1613
1614 /* Remember that we've read this stuff. */
Bartlomiej Zolnierkiewicz2bc4cf22008-02-01 23:09:22 +01001615 info->cd_flags |= IDE_CD_FLAG_TOC_VALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
1617 return 0;
1618}
1619
Bartlomiej Zolnierkiewicz17802992008-02-01 23:09:25 +01001620int ide_cdrom_get_capabilities(ide_drive_t *drive, u8 *buf)
Eric Piel9235e682005-06-23 00:10:29 -07001621{
1622 struct cdrom_info *info = drive->driver_data;
1623 struct cdrom_device_info *cdi = &info->devinfo;
1624 struct packet_command cgc;
Bartlomiej Zolnierkiewicz455d80a2008-02-01 23:09:21 +01001625 int stat, attempts = 3, size = ATAPI_CAPABILITIES_PAGE_SIZE;
Eric Piel9235e682005-06-23 00:10:29 -07001626
Bartlomiej Zolnierkiewicze59724c2008-02-01 23:09:22 +01001627 if ((info->cd_flags & IDE_CD_FLAG_FULL_CAPS_PAGE) == 0)
Bartlomiej Zolnierkiewicz455d80a2008-02-01 23:09:21 +01001628 size -= ATAPI_CAPABILITIES_PAGE_PAD_SIZE;
Eric Piel9235e682005-06-23 00:10:29 -07001629
Bartlomiej Zolnierkiewicz455d80a2008-02-01 23:09:21 +01001630 init_cdrom_command(&cgc, buf, size, CGC_DATA_UNKNOWN);
Eric Piel9235e682005-06-23 00:10:29 -07001631 do { /* we seem to get stat=0x01,err=0x00 the first time (??) */
1632 stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CAPABILITIES_PAGE, 0);
1633 if (!stat)
1634 break;
1635 } while (--attempts);
1636 return stat;
1637}
1638
Bartlomiej Zolnierkiewicz17802992008-02-01 23:09:25 +01001639void ide_cdrom_update_speed(ide_drive_t *drive, u8 *buf)
Eric Piel9235e682005-06-23 00:10:29 -07001640{
Bartlomiej Zolnierkiewicz4fe67172008-02-01 23:09:21 +01001641 struct cdrom_info *cd = drive->driver_data;
Bartlomiej Zolnierkiewicz481c8c62008-02-01 23:09:20 +01001642 u16 curspeed, maxspeed;
1643
Bartlomiej Zolnierkiewicz455d80a2008-02-01 23:09:21 +01001644 curspeed = *(u16 *)&buf[8 + 14];
1645 maxspeed = *(u16 *)&buf[8 + 8];
1646
Bartlomiej Zolnierkiewicze59724c2008-02-01 23:09:22 +01001647 if (cd->cd_flags & IDE_CD_FLAG_LE_SPEED_FIELDS) {
Bartlomiej Zolnierkiewicz455d80a2008-02-01 23:09:21 +01001648 curspeed = le16_to_cpu(curspeed);
1649 maxspeed = le16_to_cpu(maxspeed);
Eric Piel9235e682005-06-23 00:10:29 -07001650 } else {
Bartlomiej Zolnierkiewicz455d80a2008-02-01 23:09:21 +01001651 curspeed = be16_to_cpu(curspeed);
1652 maxspeed = be16_to_cpu(maxspeed);
Eric Piel9235e682005-06-23 00:10:29 -07001653 }
Bartlomiej Zolnierkiewicz481c8c62008-02-01 23:09:20 +01001654
Bartlomiej Zolnierkiewicz2bc4cf22008-02-01 23:09:22 +01001655 cd->current_speed = (curspeed + (176/2)) / 176;
1656 cd->max_speed = (maxspeed + (176/2)) / 176;
Eric Piel9235e682005-06-23 00:10:29 -07001657}
1658
Bartlomiej Zolnierkiewicz20e7f7e2008-02-01 23:09:20 +01001659#define IDE_CD_CAPABILITIES \
1660 (CDC_CLOSE_TRAY | CDC_OPEN_TRAY | CDC_LOCK | CDC_SELECT_SPEED | \
1661 CDC_SELECT_DISC | CDC_MULTI_SESSION | CDC_MCN | CDC_MEDIA_CHANGED | \
1662 CDC_PLAY_AUDIO | CDC_RESET | CDC_DRIVE_STATUS | CDC_CD_R | \
1663 CDC_CD_RW | CDC_DVD | CDC_DVD_R | CDC_DVD_RAM | CDC_GENERIC_PACKET | \
1664 CDC_MO_DRIVE | CDC_MRW | CDC_MRW_W | CDC_RAM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666static struct cdrom_device_ops ide_cdrom_dops = {
1667 .open = ide_cdrom_open_real,
1668 .release = ide_cdrom_release_real,
1669 .drive_status = ide_cdrom_drive_status,
1670 .media_changed = ide_cdrom_check_media_change_real,
1671 .tray_move = ide_cdrom_tray_move,
1672 .lock_door = ide_cdrom_lock_door,
1673 .select_speed = ide_cdrom_select_speed,
1674 .get_last_session = ide_cdrom_get_last_session,
1675 .get_mcn = ide_cdrom_get_mcn,
1676 .reset = ide_cdrom_reset,
1677 .audio_ioctl = ide_cdrom_audio_ioctl,
Bartlomiej Zolnierkiewicz20e7f7e2008-02-01 23:09:20 +01001678 .capability = IDE_CD_CAPABILITIES,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 .generic_packet = ide_cdrom_packet,
1680};
1681
1682static int ide_cdrom_register (ide_drive_t *drive, int nslots)
1683{
1684 struct cdrom_info *info = drive->driver_data;
1685 struct cdrom_device_info *devinfo = &info->devinfo;
1686
1687 devinfo->ops = &ide_cdrom_dops;
Bartlomiej Zolnierkiewicz2bc4cf22008-02-01 23:09:22 +01001688 devinfo->speed = info->current_speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 devinfo->capacity = nslots;
Jesper Juhl2a91f3e2005-10-30 15:02:45 -08001690 devinfo->handle = drive;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 strcpy(devinfo->name, drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
Bartlomiej Zolnierkiewicz2bc4cf22008-02-01 23:09:22 +01001693 if (info->cd_flags & IDE_CD_FLAG_NO_SPEED_SELECT)
Bartlomiej Zolnierkiewicz3cbd8142007-12-24 15:23:43 +01001694 devinfo->mask |= CDC_SELECT_SPEED;
1695
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 devinfo->disk = info->disk;
1697 return register_cdrom(devinfo);
1698}
1699
1700static
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701int ide_cdrom_probe_capabilities (ide_drive_t *drive)
1702{
Bartlomiej Zolnierkiewicz4fe67172008-02-01 23:09:21 +01001703 struct cdrom_info *cd = drive->driver_data;
1704 struct cdrom_device_info *cdi = &cd->devinfo;
Bartlomiej Zolnierkiewicz455d80a2008-02-01 23:09:21 +01001705 u8 buf[ATAPI_CAPABILITIES_PAGE_SIZE];
1706 mechtype_t mechtype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 int nslots = 1;
1708
Bartlomiej Zolnierkiewicz3f1b86d2008-02-01 23:09:20 +01001709 cdi->mask = (CDC_CD_R | CDC_CD_RW | CDC_DVD | CDC_DVD_R |
1710 CDC_DVD_RAM | CDC_SELECT_DISC | CDC_PLAY_AUDIO |
1711 CDC_MO_DRIVE | CDC_RAM);
1712
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 if (drive->media == ide_optical) {
Bartlomiej Zolnierkiewicz3f1b86d2008-02-01 23:09:20 +01001714 cdi->mask &= ~(CDC_MO_DRIVE | CDC_RAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 printk(KERN_ERR "%s: ATAPI magneto-optical drive\n", drive->name);
1716 return nslots;
1717 }
1718
Bartlomiej Zolnierkiewicze59724c2008-02-01 23:09:22 +01001719 if (cd->cd_flags & IDE_CD_FLAG_PRE_ATAPI12) {
Bartlomiej Zolnierkiewicz2bc4cf22008-02-01 23:09:22 +01001720 cd->cd_flags &= ~IDE_CD_FLAG_NO_EJECT;
Bartlomiej Zolnierkiewicz3f1b86d2008-02-01 23:09:20 +01001721 cdi->mask &= ~CDC_PLAY_AUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 return nslots;
1723 }
1724
1725 /*
1726 * we have to cheat a little here. the packet will eventually
1727 * be queued with ide_cdrom_packet(), which extracts the
1728 * drive from cdi->handle. Since this device hasn't been
1729 * registered with the Uniform layer yet, it can't do this.
1730 * Same goes for cdi->ops.
1731 */
Jesper Juhl2a91f3e2005-10-30 15:02:45 -08001732 cdi->handle = drive;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 cdi->ops = &ide_cdrom_dops;
1734
Bartlomiej Zolnierkiewicz455d80a2008-02-01 23:09:21 +01001735 if (ide_cdrom_get_capabilities(drive, buf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 return 0;
1737
Bartlomiej Zolnierkiewicz455d80a2008-02-01 23:09:21 +01001738 if ((buf[8 + 6] & 0x01) == 0)
Bartlomiej Zolnierkiewicz2bc4cf22008-02-01 23:09:22 +01001739 cd->cd_flags |= IDE_CD_FLAG_NO_DOORLOCK;
Bartlomiej Zolnierkiewicz455d80a2008-02-01 23:09:21 +01001740 if (buf[8 + 6] & 0x08)
Bartlomiej Zolnierkiewicz2bc4cf22008-02-01 23:09:22 +01001741 cd->cd_flags &= ~IDE_CD_FLAG_NO_EJECT;
Bartlomiej Zolnierkiewicz455d80a2008-02-01 23:09:21 +01001742 if (buf[8 + 3] & 0x01)
Bartlomiej Zolnierkiewicz3f1b86d2008-02-01 23:09:20 +01001743 cdi->mask &= ~CDC_CD_R;
Bartlomiej Zolnierkiewicz455d80a2008-02-01 23:09:21 +01001744 if (buf[8 + 3] & 0x02)
Bartlomiej Zolnierkiewicz3f1b86d2008-02-01 23:09:20 +01001745 cdi->mask &= ~(CDC_CD_RW | CDC_RAM);
Bartlomiej Zolnierkiewicz455d80a2008-02-01 23:09:21 +01001746 if (buf[8 + 2] & 0x38)
Bartlomiej Zolnierkiewicz3f1b86d2008-02-01 23:09:20 +01001747 cdi->mask &= ~CDC_DVD;
Bartlomiej Zolnierkiewicz455d80a2008-02-01 23:09:21 +01001748 if (buf[8 + 3] & 0x20)
Bartlomiej Zolnierkiewicz3f1b86d2008-02-01 23:09:20 +01001749 cdi->mask &= ~(CDC_DVD_RAM | CDC_RAM);
Bartlomiej Zolnierkiewicz455d80a2008-02-01 23:09:21 +01001750 if (buf[8 + 3] & 0x10)
Bartlomiej Zolnierkiewicz3f1b86d2008-02-01 23:09:20 +01001751 cdi->mask &= ~CDC_DVD_R;
Bartlomiej Zolnierkiewicze59724c2008-02-01 23:09:22 +01001752 if ((buf[8 + 4] & 0x01) || (cd->cd_flags & IDE_CD_FLAG_PLAY_AUDIO_OK))
Bartlomiej Zolnierkiewicz3f1b86d2008-02-01 23:09:20 +01001753 cdi->mask &= ~CDC_PLAY_AUDIO;
Bartlomiej Zolnierkiewicz455d80a2008-02-01 23:09:21 +01001754
1755 mechtype = buf[8 + 6] >> 5;
1756 if (mechtype == mechtype_caddy || mechtype == mechtype_popup)
Bartlomiej Zolnierkiewicz3f1b86d2008-02-01 23:09:20 +01001757 cdi->mask |= CDC_CLOSE_TRAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 if (cdi->sanyo_slot > 0) {
Bartlomiej Zolnierkiewicz3f1b86d2008-02-01 23:09:20 +01001760 cdi->mask &= ~CDC_SELECT_DISC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 nslots = 3;
Bartlomiej Zolnierkiewiczcdf60002008-02-01 23:09:22 +01001762 } else if (mechtype == mechtype_individual_changer ||
1763 mechtype == mechtype_cartridge_changer) {
Bartlomiej Zolnierkiewicz2609d062008-02-01 23:09:19 +01001764 nslots = cdrom_number_of_slots(cdi);
1765 if (nslots > 1)
Bartlomiej Zolnierkiewicz3f1b86d2008-02-01 23:09:20 +01001766 cdi->mask &= ~CDC_SELECT_DISC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 }
1768
Bartlomiej Zolnierkiewicz455d80a2008-02-01 23:09:21 +01001769 ide_cdrom_update_speed(drive, buf);
Bartlomiej Zolnierkiewicz4fe67172008-02-01 23:09:21 +01001770
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 printk(KERN_INFO "%s: ATAPI", drive->name);
Bartlomiej Zolnierkiewicz4fe67172008-02-01 23:09:21 +01001772
1773 /* don't print speed if the drive reported 0 */
Bartlomiej Zolnierkiewicz2bc4cf22008-02-01 23:09:22 +01001774 if (cd->max_speed)
1775 printk(KERN_CONT " %dX", cd->max_speed);
Bartlomiej Zolnierkiewicz4fe67172008-02-01 23:09:21 +01001776
Bartlomiej Zolnierkiewicz3f1b86d2008-02-01 23:09:20 +01001777 printk(KERN_CONT " %s", (cdi->mask & CDC_DVD) ? "CD-ROM" : "DVD-ROM");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778
Bartlomiej Zolnierkiewicz3f1b86d2008-02-01 23:09:20 +01001779 if ((cdi->mask & CDC_DVD_R) == 0 || (cdi->mask & CDC_DVD_RAM) == 0)
1780 printk(KERN_CONT " DVD%s%s",
1781 (cdi->mask & CDC_DVD_R) ? "" : "-R",
1782 (cdi->mask & CDC_DVD_RAM) ? "" : "-RAM");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
Bartlomiej Zolnierkiewicz3f1b86d2008-02-01 23:09:20 +01001784 if ((cdi->mask & CDC_CD_R) == 0 || (cdi->mask & CDC_CD_RW) == 0)
1785 printk(KERN_CONT " CD%s%s",
1786 (cdi->mask & CDC_CD_R) ? "" : "-R",
1787 (cdi->mask & CDC_CD_RW) ? "" : "/RW");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
Bartlomiej Zolnierkiewicz3f1b86d2008-02-01 23:09:20 +01001789 if ((cdi->mask & CDC_SELECT_DISC) == 0)
1790 printk(KERN_CONT " changer w/%d slots", nslots);
1791 else
1792 printk(KERN_CONT " drive");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
Bartlomiej Zolnierkiewicz455d80a2008-02-01 23:09:21 +01001794 printk(KERN_CONT ", %dkB Cache\n", be16_to_cpu(*(u16 *)&buf[8 + 12]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
1796 return nslots;
1797}
1798
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001799#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800static void ide_cdrom_add_settings(ide_drive_t *drive)
1801{
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +02001802 ide_add_setting(drive, "dsc_overlap", SETTING_RW, TYPE_BYTE, 0, 1, 1, 1, &drive->dsc_overlap, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803}
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001804#else
1805static inline void ide_cdrom_add_settings(ide_drive_t *drive) { ; }
1806#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
1808/*
1809 * standard prep_rq_fn that builds 10 byte cmds
1810 */
Jens Axboe165125e2007-07-24 09:28:11 +02001811static int ide_cdrom_prep_fs(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812{
1813 int hard_sect = queue_hardsect_size(q);
1814 long block = (long)rq->hard_sector / (hard_sect >> 9);
1815 unsigned long blocks = rq->hard_nr_sectors / (hard_sect >> 9);
1816
1817 memset(rq->cmd, 0, sizeof(rq->cmd));
1818
1819 if (rq_data_dir(rq) == READ)
1820 rq->cmd[0] = GPCMD_READ_10;
1821 else
1822 rq->cmd[0] = GPCMD_WRITE_10;
1823
1824 /*
1825 * fill in lba
1826 */
1827 rq->cmd[2] = (block >> 24) & 0xff;
1828 rq->cmd[3] = (block >> 16) & 0xff;
1829 rq->cmd[4] = (block >> 8) & 0xff;
1830 rq->cmd[5] = block & 0xff;
1831
1832 /*
1833 * and transfer length
1834 */
1835 rq->cmd[7] = (blocks >> 8) & 0xff;
1836 rq->cmd[8] = blocks & 0xff;
1837 rq->cmd_len = 10;
1838 return BLKPREP_OK;
1839}
1840
1841/*
1842 * Most of the SCSI commands are supported directly by ATAPI devices.
1843 * This transform handles the few exceptions.
1844 */
1845static int ide_cdrom_prep_pc(struct request *rq)
1846{
1847 u8 *c = rq->cmd;
1848
1849 /*
1850 * Transform 6-byte read/write commands to the 10-byte version
1851 */
1852 if (c[0] == READ_6 || c[0] == WRITE_6) {
1853 c[8] = c[4];
1854 c[5] = c[3];
1855 c[4] = c[2];
1856 c[3] = c[1] & 0x1f;
1857 c[2] = 0;
1858 c[1] &= 0xe0;
1859 c[0] += (READ_10 - READ_6);
1860 rq->cmd_len = 10;
1861 return BLKPREP_OK;
1862 }
1863
1864 /*
1865 * it's silly to pretend we understand 6-byte sense commands, just
1866 * reject with ILLEGAL_REQUEST and the caller should take the
1867 * appropriate action
1868 */
1869 if (c[0] == MODE_SENSE || c[0] == MODE_SELECT) {
1870 rq->errors = ILLEGAL_REQUEST;
1871 return BLKPREP_KILL;
1872 }
1873
1874 return BLKPREP_OK;
1875}
1876
Jens Axboe165125e2007-07-24 09:28:11 +02001877static int ide_cdrom_prep_fn(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878{
Jens Axboe4aff5e22006-08-10 08:44:47 +02001879 if (blk_fs_request(rq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 return ide_cdrom_prep_fs(q, rq);
Jens Axboe4aff5e22006-08-10 08:44:47 +02001881 else if (blk_pc_request(rq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 return ide_cdrom_prep_pc(rq);
1883
1884 return 0;
1885}
1886
Bartlomiej Zolnierkiewicze59724c2008-02-01 23:09:22 +01001887struct cd_list_entry {
1888 const char *id_model;
1889 const char *id_firmware;
1890 unsigned int cd_flags;
1891};
1892
1893static const struct cd_list_entry ide_cd_quirks_list[] = {
1894 /* Limit transfer size per interrupt. */
1895 { "SAMSUNG CD-ROM SCR-2430", NULL, IDE_CD_FLAG_LIMIT_NFRAMES },
1896 { "SAMSUNG CD-ROM SCR-2432", NULL, IDE_CD_FLAG_LIMIT_NFRAMES },
1897 /* SCR-3231 doesn't support the SET_CD_SPEED command. */
1898 { "SAMSUNG CD-ROM SCR-3231", NULL, IDE_CD_FLAG_NO_SPEED_SELECT },
1899 /* Old NEC260 (not R) was released before ATAPI 1.2 spec. */
1900 { "NEC CD-ROM DRIVE:260", "1.01", IDE_CD_FLAG_TOCADDR_AS_BCD |
1901 IDE_CD_FLAG_PRE_ATAPI12, },
1902 /* Vertos 300, some versions of this drive like to talk BCD. */
1903 { "V003S0DS", NULL, IDE_CD_FLAG_VERTOS_300_SSD, },
1904 /* Vertos 600 ESD. */
1905 { "V006E0DS", NULL, IDE_CD_FLAG_VERTOS_600_ESD, },
1906 /*
1907 * Sanyo 3 CD changer uses a non-standard command for CD changing
1908 * (by default standard ATAPI support for CD changers is used).
1909 */
1910 { "CD-ROM CDR-C3 G", NULL, IDE_CD_FLAG_SANYO_3CD },
1911 { "CD-ROM CDR-C3G", NULL, IDE_CD_FLAG_SANYO_3CD },
1912 { "CD-ROM CDR_C36", NULL, IDE_CD_FLAG_SANYO_3CD },
1913 /* Stingray 8X CD-ROM. */
1914 { "STINGRAY 8422 IDE 8X CD-ROM 7-27-95", NULL, IDE_CD_FLAG_PRE_ATAPI12},
1915 /*
1916 * ACER 50X CD-ROM and WPI 32X CD-ROM require the full spec length
1917 * mode sense page capabilities size, but older drives break.
1918 */
1919 { "ATAPI CD ROM DRIVE 50X MAX", NULL, IDE_CD_FLAG_FULL_CAPS_PAGE },
1920 { "WPI CDS-32X", NULL, IDE_CD_FLAG_FULL_CAPS_PAGE },
1921 /* ACER/AOpen 24X CD-ROM has the speed fields byte-swapped. */
1922 { "", "241N", IDE_CD_FLAG_LE_SPEED_FIELDS },
1923 /*
1924 * Some drives used by Apple don't advertise audio play
1925 * but they do support reading TOC & audio datas.
1926 */
1927 { "MATSHITADVD-ROM SR-8187", NULL, IDE_CD_FLAG_PLAY_AUDIO_OK },
1928 { "MATSHITADVD-ROM SR-8186", NULL, IDE_CD_FLAG_PLAY_AUDIO_OK },
1929 { "MATSHITADVD-ROM SR-8176", NULL, IDE_CD_FLAG_PLAY_AUDIO_OK },
1930 { "MATSHITADVD-ROM SR-8174", NULL, IDE_CD_FLAG_PLAY_AUDIO_OK },
Borislav Petkovacbe44e2008-02-26 21:50:32 +01001931 { "Optiarc DVD RW AD-5200A", NULL, IDE_CD_FLAG_PLAY_AUDIO_OK },
Bartlomiej Zolnierkiewicze59724c2008-02-01 23:09:22 +01001932 { NULL, NULL, 0 }
1933};
1934
1935static unsigned int ide_cd_flags(struct hd_driveid *id)
1936{
1937 const struct cd_list_entry *cle = ide_cd_quirks_list;
1938
1939 while (cle->id_model) {
1940 if (strcmp(cle->id_model, id->model) == 0 &&
1941 (cle->id_firmware == NULL ||
1942 strstr(id->fw_rev, cle->id_firmware)))
1943 return cle->cd_flags;
1944 cle++;
1945 }
1946
1947 return 0;
1948}
1949
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950static
1951int ide_cdrom_setup (ide_drive_t *drive)
1952{
Bartlomiej Zolnierkiewicz4fe67172008-02-01 23:09:21 +01001953 struct cdrom_info *cd = drive->driver_data;
1954 struct cdrom_device_info *cdi = &cd->devinfo;
Bartlomiej Zolnierkiewicze59724c2008-02-01 23:09:22 +01001955 struct hd_driveid *id = drive->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 int nslots;
1957
1958 blk_queue_prep_rq(drive->queue, ide_cdrom_prep_fn);
1959 blk_queue_dma_alignment(drive->queue, 31);
1960 drive->queue->unplug_delay = (1 * HZ) / 1000;
1961 if (!drive->queue->unplug_delay)
1962 drive->queue->unplug_delay = 1;
1963
1964 drive->special.all = 0;
1965
Bartlomiej Zolnierkiewicze59724c2008-02-01 23:09:22 +01001966 cd->cd_flags = IDE_CD_FLAG_MEDIA_CHANGED | IDE_CD_FLAG_NO_EJECT |
1967 ide_cd_flags(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
Bartlomiej Zolnierkiewicze59724c2008-02-01 23:09:22 +01001969 if ((id->config & 0x0060) == 0x20)
Bartlomiej Zolnierkiewicz2bc4cf22008-02-01 23:09:22 +01001970 cd->cd_flags |= IDE_CD_FLAG_DRQ_INTERRUPT;
Bartlomiej Zolnierkiewiczb8d25de2008-02-01 23:09:19 +01001971
Bartlomiej Zolnierkiewicze59724c2008-02-01 23:09:22 +01001972 if ((cd->cd_flags & IDE_CD_FLAG_VERTOS_300_SSD) &&
1973 id->fw_rev[4] == '1' && id->fw_rev[6] <= '2')
Bartlomiej Zolnierkiewicz2bc4cf22008-02-01 23:09:22 +01001974 cd->cd_flags |= (IDE_CD_FLAG_TOCTRACKS_AS_BCD |
1975 IDE_CD_FLAG_TOCADDR_AS_BCD);
Bartlomiej Zolnierkiewicze59724c2008-02-01 23:09:22 +01001976 else if ((cd->cd_flags & IDE_CD_FLAG_VERTOS_600_ESD) &&
1977 id->fw_rev[4] == '1' && id->fw_rev[6] <= '2')
Bartlomiej Zolnierkiewicz2bc4cf22008-02-01 23:09:22 +01001978 cd->cd_flags |= IDE_CD_FLAG_TOCTRACKS_AS_BCD;
Bartlomiej Zolnierkiewicze59724c2008-02-01 23:09:22 +01001979 else if (cd->cd_flags & IDE_CD_FLAG_SANYO_3CD)
1980 cdi->sanyo_slot = 3; /* 3 => use CD in slot 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 nslots = ide_cdrom_probe_capabilities (drive);
1983
1984 /*
1985 * set correct block size
1986 */
1987 blk_queue_hardsect_size(drive->queue, CD_FRAMESIZE);
1988
1989 if (drive->autotune == IDE_TUNE_DEFAULT ||
1990 drive->autotune == IDE_TUNE_AUTO)
1991 drive->dsc_overlap = (drive->next != drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
1993 if (ide_cdrom_register(drive, nslots)) {
1994 printk (KERN_ERR "%s: ide_cdrom_setup failed to register device with the cdrom driver.\n", drive->name);
Bartlomiej Zolnierkiewicz4fe67172008-02-01 23:09:21 +01001995 cd->devinfo.handle = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 return 1;
1997 }
1998 ide_cdrom_add_settings(drive);
1999 return 0;
2000}
2001
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +02002002#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003static
2004sector_t ide_cdrom_capacity (ide_drive_t *drive)
2005{
2006 unsigned long capacity, sectors_per_frame;
2007
2008 if (cdrom_read_capacity(drive, &capacity, &sectors_per_frame, NULL))
2009 return 0;
2010
2011 return capacity * sectors_per_frame;
2012}
Amos Waterlandd97b32142005-10-30 15:02:10 -08002013#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
Russell King4031bbe2006-01-06 11:41:00 +00002015static void ide_cd_remove(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016{
2017 struct cdrom_info *info = drive->driver_data;
2018
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002019 ide_proc_unregister_driver(drive, info->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
2021 del_gendisk(info->disk);
2022
2023 ide_cd_put(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024}
2025
2026static void ide_cd_release(struct kref *kref)
2027{
2028 struct cdrom_info *info = to_ide_cd(kref);
2029 struct cdrom_device_info *devinfo = &info->devinfo;
2030 ide_drive_t *drive = info->drive;
2031 struct gendisk *g = info->disk;
2032
Jesper Juhl6044ec82005-11-07 01:01:32 -08002033 kfree(info->buffer);
2034 kfree(info->toc);
Akinobu Mita0a0c4112008-03-26 12:09:02 +01002035 if (devinfo->handle == drive)
2036 unregister_cdrom(devinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 drive->dsc_overlap = 0;
2038 drive->driver_data = NULL;
2039 blk_queue_prep_rq(drive->queue, NULL);
2040 g->private_data = NULL;
2041 put_disk(g);
2042 kfree(info);
2043}
2044
Russell King4031bbe2006-01-06 11:41:00 +00002045static int ide_cd_probe(ide_drive_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
Bartlomiej Zolnierkiewiczecfd80e2007-05-10 00:01:09 +02002047#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048static int proc_idecd_read_capacity
2049 (char *page, char **start, off_t off, int count, int *eof, void *data)
2050{
Jesper Juhl2a91f3e2005-10-30 15:02:45 -08002051 ide_drive_t *drive = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 int len;
2053
2054 len = sprintf(page,"%llu\n", (long long)ide_cdrom_capacity(drive));
2055 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
2056}
2057
2058static ide_proc_entry_t idecd_proc[] = {
2059 { "capacity", S_IFREG|S_IRUGO, proc_idecd_read_capacity, NULL },
2060 { NULL, 0, NULL, NULL }
2061};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062#endif
2063
2064static ide_driver_t ide_cdrom_driver = {
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002065 .gen_driver = {
Laurent Riffard4ef3b8f2005-11-18 22:15:40 +01002066 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002067 .name = "ide-cdrom",
2068 .bus = &ide_bus_type,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002069 },
Russell King4031bbe2006-01-06 11:41:00 +00002070 .probe = ide_cd_probe,
2071 .remove = ide_cd_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 .version = IDECD_VERSION,
2073 .media = ide_cdrom,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 .supports_dsc_overlap = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 .do_request = ide_do_rw_cdrom,
2076 .end_request = ide_end_request,
2077 .error = __ide_error,
2078 .abort = __ide_abort,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002079#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 .proc = idecd_proc,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002081#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082};
2083
2084static int idecd_open(struct inode * inode, struct file * file)
2085{
2086 struct gendisk *disk = inode->i_bdev->bd_disk;
2087 struct cdrom_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 int rc = -ENOMEM;
2089
2090 if (!(info = ide_cd_get(disk)))
2091 return -ENXIO;
2092
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 if (!info->buffer)
Bartlomiej Zolnierkiewiczc94964a2007-02-17 02:40:24 +01002094 info->buffer = kmalloc(SECTOR_BUFFER_SIZE, GFP_KERNEL|__GFP_REPEAT);
2095
2096 if (info->buffer)
2097 rc = cdrom_open(&info->devinfo, inode, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
2099 if (rc < 0)
2100 ide_cd_put(info);
2101
2102 return rc;
2103}
2104
2105static int idecd_release(struct inode * inode, struct file * file)
2106{
2107 struct gendisk *disk = inode->i_bdev->bd_disk;
2108 struct cdrom_info *info = ide_cd_g(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109
2110 cdrom_release (&info->devinfo, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111
2112 ide_cd_put(info);
2113
2114 return 0;
2115}
2116
Christoph Hellwig6a2900b2006-03-23 03:00:15 -08002117static int idecd_set_spindown(struct cdrom_device_info *cdi, unsigned long arg)
2118{
2119 struct packet_command cgc;
2120 char buffer[16];
2121 int stat;
2122 char spindown;
2123
2124 if (copy_from_user(&spindown, (void __user *)arg, sizeof(char)))
2125 return -EFAULT;
2126
2127 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_UNKNOWN);
2128
2129 stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CDROM_PAGE, 0);
2130 if (stat)
2131 return stat;
2132
2133 buffer[11] = (buffer[11] & 0xf0) | (spindown & 0x0f);
2134 return cdrom_mode_select(cdi, &cgc);
2135}
2136
2137static int idecd_get_spindown(struct cdrom_device_info *cdi, unsigned long arg)
2138{
2139 struct packet_command cgc;
2140 char buffer[16];
2141 int stat;
2142 char spindown;
2143
2144 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_UNKNOWN);
2145
2146 stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CDROM_PAGE, 0);
2147 if (stat)
2148 return stat;
2149
2150 spindown = buffer[11] & 0x0f;
2151 if (copy_to_user((void __user *)arg, &spindown, sizeof (char)))
2152 return -EFAULT;
2153 return 0;
2154}
2155
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156static int idecd_ioctl (struct inode *inode, struct file *file,
2157 unsigned int cmd, unsigned long arg)
2158{
2159 struct block_device *bdev = inode->i_bdev;
2160 struct cdrom_info *info = ide_cd_g(bdev->bd_disk);
2161 int err;
2162
Christoph Hellwig6a2900b2006-03-23 03:00:15 -08002163 switch (cmd) {
2164 case CDROMSETSPINDOWN:
2165 return idecd_set_spindown(&info->devinfo, arg);
2166 case CDROMGETSPINDOWN:
2167 return idecd_get_spindown(&info->devinfo, arg);
2168 default:
2169 break;
2170 }
2171
2172 err = generic_ide_ioctl(info->drive, file, bdev, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 if (err == -EINVAL)
2174 err = cdrom_ioctl(file, &info->devinfo, inode, cmd, arg);
2175
2176 return err;
2177}
2178
2179static int idecd_media_changed(struct gendisk *disk)
2180{
2181 struct cdrom_info *info = ide_cd_g(disk);
2182 return cdrom_media_changed(&info->devinfo);
2183}
2184
2185static int idecd_revalidate_disk(struct gendisk *disk)
2186{
2187 struct cdrom_info *info = ide_cd_g(disk);
2188 struct request_sense sense;
Bartlomiej Zolnierkiewicz139c8292008-02-01 23:09:24 +01002189
2190 ide_cd_read_toc(info->drive, &sense);
2191
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 return 0;
2193}
2194
2195static struct block_device_operations idecd_ops = {
2196 .owner = THIS_MODULE,
2197 .open = idecd_open,
2198 .release = idecd_release,
2199 .ioctl = idecd_ioctl,
2200 .media_changed = idecd_media_changed,
2201 .revalidate_disk= idecd_revalidate_disk
2202};
2203
2204/* options */
2205static char *ignore = NULL;
2206
2207module_param(ignore, charp, 0400);
2208MODULE_DESCRIPTION("ATAPI CD-ROM Driver");
2209
Russell King4031bbe2006-01-06 11:41:00 +00002210static int ide_cd_probe(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211{
2212 struct cdrom_info *info;
2213 struct gendisk *g;
2214 struct request_sense sense;
2215
2216 if (!strstr("ide-cdrom", drive->driver_req))
2217 goto failed;
2218 if (!drive->present)
2219 goto failed;
2220 if (drive->media != ide_cdrom && drive->media != ide_optical)
2221 goto failed;
2222 /* skip drives that we were told to ignore */
2223 if (ignore != NULL) {
2224 if (strstr(ignore, drive->name)) {
2225 printk(KERN_INFO "ide-cd: ignoring drive %s\n", drive->name);
2226 goto failed;
2227 }
2228 }
2229 if (drive->scsi) {
2230 printk(KERN_INFO "ide-cd: passing drive %s to ide-scsi emulation.\n", drive->name);
2231 goto failed;
2232 }
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -08002233 info = kzalloc(sizeof(struct cdrom_info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 if (info == NULL) {
2235 printk(KERN_ERR "%s: Can't allocate a cdrom structure\n", drive->name);
2236 goto failed;
2237 }
2238
2239 g = alloc_disk(1 << PARTN_BITS);
2240 if (!g)
2241 goto out_free_cd;
2242
2243 ide_init_disk(g, drive);
2244
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002245 ide_proc_register_driver(drive, &ide_cdrom_driver);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002246
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 kref_init(&info->kref);
2248
2249 info->drive = drive;
2250 info->driver = &ide_cdrom_driver;
2251 info->disk = g;
2252
2253 g->private_data = &info->driver;
2254
2255 drive->driver_data = info;
2256
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 g->minors = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 g->driverfs_dev = &drive->gendev;
2259 g->flags = GENHD_FL_CD | GENHD_FL_REMOVABLE;
2260 if (ide_cdrom_setup(drive)) {
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002261 ide_proc_unregister_driver(drive, &ide_cdrom_driver);
Bartlomiej Zolnierkiewicz05017db2007-12-24 15:23:43 +01002262 ide_cd_release(&info->kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 goto failed;
2264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265
Bartlomiej Zolnierkiewicz139c8292008-02-01 23:09:24 +01002266 ide_cd_read_toc(drive, &sense);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 g->fops = &idecd_ops;
2268 g->flags |= GENHD_FL_REMOVABLE;
2269 add_disk(g);
2270 return 0;
2271
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272out_free_cd:
2273 kfree(info);
2274failed:
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002275 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276}
2277
2278static void __exit ide_cdrom_exit(void)
2279{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002280 driver_unregister(&ide_cdrom_driver.gen_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281}
Bartlomiej Zolnierkiewicz17514e82005-11-19 22:24:35 +01002282
2283static int __init ide_cdrom_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002285 return driver_register(&ide_cdrom_driver.gen_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286}
2287
Kay Sievers263756e2005-12-12 18:03:44 +01002288MODULE_ALIAS("ide:*m-cdrom*");
Bartlomiej Zolnierkiewicz972560f2008-02-01 23:09:23 +01002289MODULE_ALIAS("ide-cd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290module_init(ide_cdrom_init);
2291module_exit(ide_cdrom_exit);
2292MODULE_LICENSE("GPL");