blob: 42ab6d8715f26c6018201e32cace684d15d7435b [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
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +020016 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 * 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
Borislav Petkovfc8323f2008-10-13 21:39:48 +020026#define DRV_NAME "ide-cd"
27#define PFX DRV_NAME ": "
28
Bartlomiej Zolnierkiewicz3bb46632008-02-01 23:09:28 +010029#define IDECD_VERSION "5.00"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/module.h>
32#include <linux/types.h>
33#include <linux/kernel.h>
34#include <linux/delay.h>
35#include <linux/timer.h>
36#include <linux/slab.h>
37#include <linux/interrupt.h>
38#include <linux/errno.h>
39#include <linux/cdrom.h>
40#include <linux/ide.h>
41#include <linux/completion.h>
Arjan van de Vencf8b8972006-03-23 03:00:45 -080042#include <linux/mutex.h>
Bartlomiej Zolnierkiewicz9a6dc662008-02-01 23:09:22 +010043#include <linux/bcd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +020045/* For SCSI -> ATAPI command conversion */
46#include <scsi/scsi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Borislav Petkov9aba4682008-04-26 22:25:15 +020048#include <linux/irq.h>
49#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <asm/byteorder.h>
Borislav Petkov9aba4682008-04-26 22:25:15 +020051#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <asm/unaligned.h>
53
54#include "ide-cd.h"
55
Borislav Petkovfc8323f2008-10-13 21:39:48 +020056#define IDECD_DEBUG_LOG 1
57
58#if IDECD_DEBUG_LOG
59#define ide_debug_log(lvl, fmt, args...) __ide_debug_log(lvl, fmt, args)
60#else
61#define ide_debug_log(lvl, fmt, args...) do {} while (0)
62#endif
63
Arjan van de Vencf8b8972006-03-23 03:00:45 -080064static DEFINE_MUTEX(idecd_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +020066static void ide_cd_release(struct kref *);
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068static struct cdrom_info *ide_cd_get(struct gendisk *disk)
69{
70 struct cdrom_info *cd = NULL;
71
Arjan van de Vencf8b8972006-03-23 03:00:45 -080072 mutex_lock(&idecd_ref_mutex);
Borislav Petkov5aeddf92008-10-13 21:39:34 +020073 cd = ide_drv_g(disk, cdrom_info);
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +020074 if (cd) {
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +020075 if (ide_device_get(cd->drive))
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +020076 cd = NULL;
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +020077 else
78 kref_get(&cd->kref);
79
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +020080 }
Arjan van de Vencf8b8972006-03-23 03:00:45 -080081 mutex_unlock(&idecd_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 return cd;
83}
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085static void ide_cd_put(struct cdrom_info *cd)
86{
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +020087 ide_drive_t *drive = cd->drive;
88
Arjan van de Vencf8b8972006-03-23 03:00:45 -080089 mutex_lock(&idecd_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 kref_put(&cd->kref, ide_cd_release);
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +020091 ide_device_put(drive);
Arjan van de Vencf8b8972006-03-23 03:00:45 -080092 mutex_unlock(&idecd_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093}
94
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +020095/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 * Generic packet command support and error handling routines.
97 */
98
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +020099/* Mark that we've seen a media change and invalidate our internal buffers. */
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +0200100static void cdrom_saw_media_change(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
Bartlomiej Zolnierkiewiczfe11edf2008-10-17 18:09:11 +0200102 drive->dev_flags |= IDE_DFLAG_MEDIA_CHANGED;
Borislav Petkov570f89e2008-07-23 19:56:02 +0200103 drive->atapi_flags &= ~IDE_AFLAG_TOC_VALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
106static int cdrom_log_sense(ide_drive_t *drive, struct request *rq,
107 struct request_sense *sense)
108{
109 int log = 0;
110
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200111 ide_debug_log(IDE_DBG_SENSE, "Call %s, sense_key: 0x%x\n", __func__,
112 sense->sense_key);
113
Jens Axboe4aff5e22006-08-10 08:44:47 +0200114 if (!sense || !rq || (rq->cmd_flags & REQ_QUIET))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 return 0;
116
117 switch (sense->sense_key) {
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +0200118 case NO_SENSE:
119 case RECOVERED_ERROR:
120 break;
121 case NOT_READY:
122 /*
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200123 * don't care about tray state messages for e.g. capacity
124 * commands or in-progress or becoming ready
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +0200125 */
126 if (sense->asc == 0x3a || sense->asc == 0x04)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 break;
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +0200128 log = 1;
129 break;
130 case ILLEGAL_REQUEST:
131 /*
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200132 * don't log START_STOP unit with LoEj set, since we cannot
133 * reliably check if drive can auto-close
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +0200134 */
135 if (rq->cmd[0] == GPCMD_START_STOP_UNIT && sense->asc == 0x24)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 break;
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +0200137 log = 1;
138 break;
139 case UNIT_ATTENTION:
140 /*
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200141 * Make good and sure we've seen this potential media change.
142 * Some drives (i.e. Creative) fail to present the correct sense
143 * key in the error register.
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +0200144 */
145 cdrom_saw_media_change(drive);
146 break;
147 default:
148 log = 1;
149 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 }
151 return log;
152}
153
Borislav Petkove5e076a2008-04-26 22:25:15 +0200154static void cdrom_analyze_sense_data(ide_drive_t *drive,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 struct request *failed_command,
156 struct request_sense *sense)
157{
Alan Coxdbe217a2006-06-25 05:47:44 -0700158 unsigned long sector;
159 unsigned long bio_sectors;
Alan Coxdbe217a2006-06-25 05:47:44 -0700160 struct cdrom_info *info = drive->driver_data;
161
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200162 ide_debug_log(IDE_DBG_SENSE, "Call %s, error_code: 0x%x, "
163 "sense_key: 0x%x\n", __func__, sense->error_code,
164 sense->sense_key);
165
166 if (failed_command)
167 ide_debug_log(IDE_DBG_SENSE, "%s: failed cmd: 0x%x\n",
168 __func__, failed_command->cmd[0]);
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 if (!cdrom_log_sense(drive, failed_command, sense))
171 return;
172
173 /*
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200174 * If a read toc is executed for a CD-R or CD-RW medium where the first
175 * toc has not been recorded yet, it will fail with 05/24/00 (which is a
176 * confusing error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 */
178 if (failed_command && failed_command->cmd[0] == GPCMD_READ_TOC_PMA_ATIP)
179 if (sense->sense_key == 0x05 && sense->asc == 0x24)
180 return;
181
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200182 /* current error */
183 if (sense->error_code == 0x70) {
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +0200184 switch (sense->sense_key) {
Alan Coxdbe217a2006-06-25 05:47:44 -0700185 case MEDIUM_ERROR:
186 case VOLUME_OVERFLOW:
187 case ILLEGAL_REQUEST:
188 if (!sense->valid)
189 break;
190 if (failed_command == NULL ||
191 !blk_fs_request(failed_command))
192 break;
193 sector = (sense->information[0] << 24) |
194 (sense->information[1] << 16) |
195 (sense->information[2] << 8) |
196 (sense->information[3]);
197
Alan Coxdbe217a2006-06-25 05:47:44 -0700198 if (drive->queue->hardsect_size == 2048)
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200199 /* device sector size is 2K */
200 sector <<= 2;
Roel Kluineee49292008-04-28 23:44:43 +0200201
202 bio_sectors = max(bio_sectors(failed_command->bio), 4U);
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +0200203 sector &= ~(bio_sectors - 1);
Alan Coxdbe217a2006-06-25 05:47:44 -0700204
Alan Coxdbe217a2006-06-25 05:47:44 -0700205 if (sector < get_capacity(info->disk) &&
Borislav Petkove5e076a2008-04-26 22:25:15 +0200206 drive->probed_capacity - sector < 4 * 75)
Alan Coxdbe217a2006-06-25 05:47:44 -0700207 set_capacity(info->disk, sector);
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +0200208 }
209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Bartlomiej Zolnierkiewicz972560f2008-02-01 23:09:23 +0100211 ide_cd_log_error(drive->name, failed_command, sense);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214static void cdrom_queue_request_sense(ide_drive_t *drive, void *sense,
215 struct request *failed_command)
216{
217 struct cdrom_info *info = drive->driver_data;
218 struct request *rq = &info->request_sense_request;
219
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200220 ide_debug_log(IDE_DBG_SENSE, "Call %s\n", __func__);
221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 if (sense == NULL)
223 sense = &info->sense_data;
224
225 /* stuff the sense request in front of our current request */
FUJITA Tomonoried820f12008-07-15 21:21:45 +0200226 blk_rq_init(NULL, rq);
227 rq->cmd_type = REQ_TYPE_ATA_PC;
228 rq->rq_disk = info->disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 rq->data = sense;
231 rq->cmd[0] = GPCMD_REQUEST_SENSE;
Borislav Petkove5e076a2008-04-26 22:25:15 +0200232 rq->cmd[4] = 18;
233 rq->data_len = 18;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Jens Axboe4aff5e22006-08-10 08:44:47 +0200235 rq->cmd_type = REQ_TYPE_SENSE;
Bartlomiej Zolnierkiewicze8a96aa2008-07-15 21:21:41 +0200236 rq->cmd_flags |= REQ_PREEMPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238 /* NOTE! Save the failed command in "rq->buffer" */
239 rq->buffer = (void *) failed_command;
240
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200241 if (failed_command)
242 ide_debug_log(IDE_DBG_SENSE, "failed_cmd: 0x%x\n",
243 failed_command->cmd[0]);
244
FUJITA Tomonori63f5abb2008-07-15 21:21:51 +0200245 ide_do_drive_cmd(drive, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246}
247
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +0200248static void cdrom_end_request(ide_drive_t *drive, int uptodate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
250 struct request *rq = HWGROUP(drive)->rq;
251 int nsectors = rq->hard_cur_sectors;
252
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200253 ide_debug_log(IDE_DBG_FUNC, "Call %s, cmd: 0x%x, uptodate: 0x%x, "
254 "nsectors: %d\n", __func__, rq->cmd[0], uptodate,
255 nsectors);
256
Jens Axboe4aff5e22006-08-10 08:44:47 +0200257 if (blk_sense_request(rq) && uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 /*
Jens Axboe4aff5e22006-08-10 08:44:47 +0200259 * For REQ_TYPE_SENSE, "rq->buffer" points to the original
260 * failed request
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 */
262 struct request *failed = (struct request *) rq->buffer;
263 struct cdrom_info *info = drive->driver_data;
264 void *sense = &info->sense_data;
265 unsigned long flags;
266
267 if (failed) {
268 if (failed->sense) {
269 sense = failed->sense;
270 failed->sense_len = rq->sense_len;
271 }
Alan Coxdbe217a2006-06-25 05:47:44 -0700272 cdrom_analyze_sense_data(drive, failed, sense);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 /*
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200274 * now end the failed request
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 */
Alan Coxdbe217a2006-06-25 05:47:44 -0700276 if (blk_fs_request(failed)) {
277 if (ide_end_dequeued_request(drive, failed, 0,
278 failed->hard_nr_sectors))
279 BUG();
280 } else {
281 spin_lock_irqsave(&ide_lock, flags);
Kiyoshi Ueda5e36bb62008-01-28 10:34:20 +0100282 if (__blk_end_request(failed, -EIO,
283 failed->data_len))
284 BUG();
Alan Coxdbe217a2006-06-25 05:47:44 -0700285 spin_unlock_irqrestore(&ide_lock, flags);
286 }
287 } else
288 cdrom_analyze_sense_data(drive, NULL, sense);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 }
290
291 if (!rq->current_nr_sectors && blk_fs_request(rq))
292 uptodate = 1;
293 /* make sure it's fully ended */
294 if (blk_pc_request(rq))
295 nsectors = (rq->data_len + 511) >> 9;
296 if (!nsectors)
297 nsectors = 1;
298
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200299 ide_debug_log(IDE_DBG_FUNC, "Exit %s, uptodate: 0x%x, nsectors: %d\n",
300 __func__, uptodate, nsectors);
301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 ide_end_request(drive, uptodate, nsectors);
303}
304
Borislav Petkov83c85652008-04-26 22:25:15 +0200305static void ide_dump_status_no_sense(ide_drive_t *drive, const char *msg, u8 st)
Alan Coxdbe217a2006-06-25 05:47:44 -0700306{
Borislav Petkov83c85652008-04-26 22:25:15 +0200307 if (st & 0x80)
Alan Coxdbe217a2006-06-25 05:47:44 -0700308 return;
Borislav Petkov83c85652008-04-26 22:25:15 +0200309 ide_dump_status(drive, msg, st);
Alan Coxdbe217a2006-06-25 05:47:44 -0700310}
311
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200312/*
313 * Returns:
314 * 0: if the request should be continued.
315 * 1: if the request was ended.
316 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317static int cdrom_decode_status(ide_drive_t *drive, int good_stat, int *stat_ret)
318{
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200319 ide_hwif_t *hwif = drive->hwif;
320 struct request *rq = hwif->hwgroup->rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 int stat, err, sense_key;
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +0200322
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200323 /* check for errors */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200324 stat = hwif->tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 if (stat_ret)
327 *stat_ret = stat;
328
329 if (OK_STAT(stat, good_stat, BAD_R_STAT))
330 return 0;
331
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200332 /* get the IDE error register */
Bartlomiej Zolnierkiewicz64a57fe2008-02-06 02:57:51 +0100333 err = ide_read_error(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 sense_key = err >> 4;
335
336 if (rq == NULL) {
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200337 printk(KERN_ERR PFX "%s: missing rq in %s\n",
Borislav Petkove5e076a2008-04-26 22:25:15 +0200338 drive->name, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 return 1;
340 }
341
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200342 ide_debug_log(IDE_DBG_RQ, "%s: stat: 0x%x, good_stat: 0x%x, "
Borislav Petkov71b429ca2008-10-17 18:09:14 +0200343 "rq->cmd[0]: 0x%x, rq->cmd_type: 0x%x, err: 0x%x\n",
344 __func__, stat, good_stat, rq->cmd[0], rq->cmd_type, err);
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200345
Jens Axboe4aff5e22006-08-10 08:44:47 +0200346 if (blk_sense_request(rq)) {
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200347 /*
348 * We got an error trying to get sense info from the drive
349 * (probably while trying to recover from a former error).
350 * Just give up.
351 */
Jens Axboe4aff5e22006-08-10 08:44:47 +0200352 rq->cmd_flags |= REQ_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 cdrom_end_request(drive, 0);
354 ide_error(drive, "request sense failure", stat);
355 return 1;
356
Jens Axboe8770c012006-10-12 17:24:52 +0200357 } else if (blk_pc_request(rq) || rq->cmd_type == REQ_TYPE_ATA_PC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 /* All other functions, except for READ. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 /*
361 * if we have an error, pass back CHECK_CONDITION as the
362 * scsi status byte
363 */
Jens Axboeb7156732006-11-13 18:05:02 +0100364 if (blk_pc_request(rq) && !rq->errors)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 rq->errors = SAM_STAT_CHECK_CONDITION;
366
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200367 /* check for tray open */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 if (sense_key == NOT_READY) {
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +0200369 cdrom_saw_media_change(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 } else if (sense_key == UNIT_ATTENTION) {
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200371 /* check for media change */
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +0200372 cdrom_saw_media_change(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return 0;
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +0200374 } else if (sense_key == ILLEGAL_REQUEST &&
375 rq->cmd[0] == GPCMD_START_STOP_UNIT) {
376 /*
377 * Don't print error message for this condition--
378 * SFF8090i indicates that 5/24/00 is the correct
379 * response to a request to close the tray if the
380 * drive doesn't have that capability.
381 * cdrom_log_sense() knows this!
382 */
Jens Axboe4aff5e22006-08-10 08:44:47 +0200383 } else if (!(rq->cmd_flags & REQ_QUIET)) {
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200384 /* otherwise, print an error */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 ide_dump_status(drive, "packet command error", stat);
386 }
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +0200387
Jens Axboe4aff5e22006-08-10 08:44:47 +0200388 rq->cmd_flags |= REQ_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390 /*
391 * instead of playing games with moving completions around,
392 * remove failed request completely and end it when the
393 * request sense has completed
394 */
Bartlomiej Zolnierkiewiczbbb89e32008-02-01 23:09:28 +0100395 goto end_request;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397 } else if (blk_fs_request(rq)) {
398 int do_end_request = 0;
399
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200400 /* handle errors from READ and WRITE requests */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402 if (blk_noretry_request(rq))
403 do_end_request = 1;
404
405 if (sense_key == NOT_READY) {
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200406 /* tray open */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if (rq_data_dir(rq) == READ) {
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +0200408 cdrom_saw_media_change(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200410 /* fail the request */
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200411 printk(KERN_ERR PFX "%s: tray open\n",
412 drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 do_end_request = 1;
414 } else {
415 struct cdrom_info *info = drive->driver_data;
416
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200417 /*
418 * Allow the drive 5 seconds to recover, some
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 * devices will return this error while flushing
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200420 * data from cache.
421 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 if (!rq->errors)
Borislav Petkov83c85652008-04-26 22:25:15 +0200423 info->write_timeout = jiffies +
424 ATAPI_WAIT_WRITE_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 rq->errors = 1;
426 if (time_after(jiffies, info->write_timeout))
427 do_end_request = 1;
428 else {
429 unsigned long flags;
430
431 /*
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200432 * take a breather relying on the unplug
433 * timer to kick us again
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 */
435 spin_lock_irqsave(&ide_lock, flags);
436 blk_plug_device(drive->queue);
Borislav Petkov83c85652008-04-26 22:25:15 +0200437 spin_unlock_irqrestore(&ide_lock,
438 flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 return 1;
440 }
441 }
442 } else if (sense_key == UNIT_ATTENTION) {
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200443 /* media change */
Borislav Petkove5e076a2008-04-26 22:25:15 +0200444 cdrom_saw_media_change(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +0200446 /*
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200447 * Arrange to retry the request but be sure to give up
448 * if we've retried too many times.
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +0200449 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 if (++rq->errors > ERROR_MAX)
451 do_end_request = 1;
452 } else if (sense_key == ILLEGAL_REQUEST ||
453 sense_key == DATA_PROTECT) {
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +0200454 /*
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200455 * No point in retrying after an illegal request or data
456 * protect error.
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +0200457 */
458 ide_dump_status_no_sense(drive, "command error", stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 do_end_request = 1;
460 } else if (sense_key == MEDIUM_ERROR) {
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +0200461 /*
462 * No point in re-trying a zillion times on a bad
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200463 * sector. If we got here the error is not correctable.
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +0200464 */
Borislav Petkov83c85652008-04-26 22:25:15 +0200465 ide_dump_status_no_sense(drive,
466 "media error (bad sector)",
467 stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 do_end_request = 1;
469 } else if (sense_key == BLANK_CHECK) {
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200470 /* disk appears blank ?? */
Borislav Petkov83c85652008-04-26 22:25:15 +0200471 ide_dump_status_no_sense(drive, "media error (blank)",
472 stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 do_end_request = 1;
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200474 } else if ((err & ~ATA_ABORTED) != 0) {
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200475 /* go to the default handler for other errors */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 ide_error(drive, "cdrom_decode_status", stat);
477 return 1;
478 } else if ((++rq->errors > ERROR_MAX)) {
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200479 /* we've racked up too many retries, abort */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 do_end_request = 1;
481 }
482
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200483 /*
484 * End a request through request sense analysis when we have
485 * sense data. We need this in order to perform end of media
486 * processing.
487 */
Bartlomiej Zolnierkiewiczbbb89e32008-02-01 23:09:28 +0100488 if (do_end_request)
489 goto end_request;
Alan Coxdbe217a2006-06-25 05:47:44 -0700490
Bartlomiej Zolnierkiewiczbbb89e32008-02-01 23:09:28 +0100491 /*
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200492 * If we got a CHECK_CONDITION status, queue
493 * a request sense command.
Bartlomiej Zolnierkiewiczbbb89e32008-02-01 23:09:28 +0100494 */
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200495 if (stat & ATA_ERR)
Bartlomiej Zolnierkiewiczbbb89e32008-02-01 23:09:28 +0100496 cdrom_queue_request_sense(drive, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 } else {
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200498 blk_dump_rq_flags(rq, PFX "bad rq");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 cdrom_end_request(drive, 0);
500 }
501
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200502 /* retry, or handle the next request */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 return 1;
Bartlomiej Zolnierkiewiczbbb89e32008-02-01 23:09:28 +0100504
505end_request:
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200506 if (stat & ATA_ERR) {
Bartlomiej Zolnierkiewiczbbb89e32008-02-01 23:09:28 +0100507 unsigned long flags;
508
509 spin_lock_irqsave(&ide_lock, flags);
510 blkdev_dequeue_request(rq);
511 HWGROUP(drive)->rq = NULL;
512 spin_unlock_irqrestore(&ide_lock, flags);
513
514 cdrom_queue_request_sense(drive, rq->sense, rq);
515 } else
516 cdrom_end_request(drive, 0);
517
518 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519}
520
521static int cdrom_timer_expiry(ide_drive_t *drive)
522{
523 struct request *rq = HWGROUP(drive)->rq;
524 unsigned long wait = 0;
525
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200526 ide_debug_log(IDE_DBG_RQ, "Call %s: rq->cmd[0]: 0x%x\n", __func__,
527 rq->cmd[0]);
528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 /*
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200530 * Some commands are *slow* and normally take a long time to complete.
531 * Usually we can use the ATAPI "disconnect" to bypass this, but not all
532 * commands/drives support that. Let ide_timer_expiry keep polling us
533 * for these.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 */
535 switch (rq->cmd[0]) {
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +0200536 case GPCMD_BLANK:
537 case GPCMD_FORMAT_UNIT:
538 case GPCMD_RESERVE_RZONE_TRACK:
539 case GPCMD_CLOSE_TRACK:
540 case GPCMD_FLUSH_CACHE:
541 wait = ATAPI_WAIT_PC;
542 break;
543 default:
544 if (!(rq->cmd_flags & REQ_QUIET))
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200545 printk(KERN_INFO PFX "cmd 0x%x timed out\n",
Borislav Petkov83c85652008-04-26 22:25:15 +0200546 rq->cmd[0]);
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +0200547 wait = 0;
548 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 }
550 return wait;
551}
552
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200553/*
554 * Set up the device registers for transferring a packet command on DEV,
555 * expecting to later transfer XFERLEN bytes. HANDLER is the routine
556 * which actually transfers the command to the drive. If this is a
557 * drq_interrupt device, this routine will arrange for HANDLER to be
558 * called when the interrupt from the drive arrives. Otherwise, HANDLER
559 * will be called immediately after the drive is prepared for the transfer.
560 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561static ide_startstop_t cdrom_start_packet_command(ide_drive_t *drive,
562 int xferlen,
563 ide_handler_t *handler)
564{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 ide_hwif_t *hwif = drive->hwif;
566
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200567 ide_debug_log(IDE_DBG_PC, "Call %s, xferlen: %d\n", __func__, xferlen);
568
Bartlomiej Zolnierkiewicz3a6a3542008-01-25 22:17:13 +0100569 /* FIXME: for Virtual DMA we must check harder */
Borislav Petkov12469ac2008-10-13 21:39:49 +0200570 if (drive->dma)
571 drive->dma = !hwif->dma_ops->dma_setup(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200573 /* set up the controller registers */
Bartlomiej Zolnierkiewicz9a410e72008-07-15 21:21:48 +0200574 ide_pktcmd_tf_load(drive, IDE_TFLAG_OUT_NSECT | IDE_TFLAG_OUT_LBAL,
Borislav Petkov12469ac2008-10-13 21:39:49 +0200575 xferlen, drive->dma);
Bartlomiej Zolnierkiewicz4fe67172008-02-01 23:09:21 +0100576
Borislav Petkov570f89e2008-07-23 19:56:02 +0200577 if (drive->atapi_flags & IDE_AFLAG_DRQ_INTERRUPT) {
Albert Leef0dd8712007-02-17 02:40:21 +0100578 /* waiting for CDB interrupt, not DMA yet. */
Borislav Petkov12469ac2008-10-13 21:39:49 +0200579 if (drive->dma)
Albert Leef0dd8712007-02-17 02:40:21 +0100580 drive->waiting_for_dma = 0;
581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 /* packet command */
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200583 ide_execute_command(drive, ATA_CMD_PACKET, handler,
Borislav Petkov83c85652008-04-26 22:25:15 +0200584 ATAPI_WAIT_PC, cdrom_timer_expiry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 return ide_started;
586 } else {
Bartlomiej Zolnierkiewicz1fc14252008-04-28 23:44:39 +0200587 ide_execute_pkt_cmd(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589 return (*handler) (drive);
590 }
591}
592
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200593/*
594 * Send a packet command to DRIVE described by CMD_BUF and CMD_LEN. The device
595 * registers must have already been prepared by cdrom_start_packet_command.
596 * HANDLER is the interrupt handler to call when the command completes or
597 * there's data ready.
598 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599#define ATAPI_MIN_CDB_BYTES 12
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +0200600static ide_startstop_t cdrom_transfer_packet_command(ide_drive_t *drive,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 struct request *rq,
602 ide_handler_t *handler)
603{
604 ide_hwif_t *hwif = drive->hwif;
605 int cmd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 ide_startstop_t startstop;
607
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200608 ide_debug_log(IDE_DBG_PC, "Call %s\n", __func__);
609
Borislav Petkov570f89e2008-07-23 19:56:02 +0200610 if (drive->atapi_flags & IDE_AFLAG_DRQ_INTERRUPT) {
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200611 /*
612 * Here we should have been called after receiving an interrupt
613 * from the device. DRQ should how be set.
614 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200616 /* check for errors */
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200617 if (cdrom_decode_status(drive, ATA_DRQ, NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 return ide_stopped;
Albert Leef0dd8712007-02-17 02:40:21 +0100619
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200620 /* ok, next interrupt will be DMA interrupt */
Borislav Petkov12469ac2008-10-13 21:39:49 +0200621 if (drive->dma)
Albert Leef0dd8712007-02-17 02:40:21 +0100622 drive->waiting_for_dma = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 } else {
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200624 /* otherwise, we must wait for DRQ to get set */
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200625 if (ide_wait_stat(&startstop, drive, ATA_DRQ,
626 ATA_BUSY, WAIT_READY))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 return startstop;
628 }
629
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200630 /* arm the interrupt handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 ide_set_handler(drive, handler, rq->timeout, cdrom_timer_expiry);
632
633 /* ATAPI commands get padded out to 12 bytes minimum */
634 cmd_len = COMMAND_SIZE(rq->cmd[0]);
635 if (cmd_len < ATAPI_MIN_CDB_BYTES)
636 cmd_len = ATAPI_MIN_CDB_BYTES;
637
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200638 /* send the command to the device */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200639 hwif->tp_ops->output_data(drive, NULL, rq->cmd, cmd_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200641 /* start the DMA if need be */
Borislav Petkov12469ac2008-10-13 21:39:49 +0200642 if (drive->dma)
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +0200643 hwif->dma_ops->dma_start(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
645 return ide_started;
646}
647
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200648/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 * Check the contents of the interrupt reason register from the cdrom
650 * and attempt to recover if there are problems. Returns 0 if everything's
651 * ok; nonzero if the request has been terminated.
652 */
Bartlomiej Zolnierkiewicz9f10d9e2008-02-26 21:50:35 +0100653static int ide_cd_check_ireason(ide_drive_t *drive, struct request *rq,
654 int len, int ireason, int rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655{
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200656 ide_hwif_t *hwif = drive->hwif;
657
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200658 ide_debug_log(IDE_DBG_FUNC, "Call %s, ireason: 0x%x, rw: 0x%x\n",
659 __func__, ireason, rw);
660
Bartlomiej Zolnierkiewicz0d6f7e32008-02-01 23:09:28 +0100661 /*
662 * ireason == 0: the drive wants to receive data from us
663 * ireason == 2: the drive is expecting to transfer data to us
664 */
665 if (ireason == (!rw << 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 return 0;
Bartlomiej Zolnierkiewicz0d6f7e32008-02-01 23:09:28 +0100667 else if (ireason == (rw << 1)) {
Bartlomiej Zolnierkiewicz5a5222d2008-02-01 23:09:17 +0100668
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200669 /* whoops... */
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200670 printk(KERN_ERR PFX "%s: %s: wrong transfer direction!\n",
Paolo Ciarrocchi177773e2008-04-26 17:36:42 +0200671 drive->name, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Borislav Petkovae8789f2008-07-16 20:33:45 +0200673 ide_pad_transfer(drive, rw, len);
Bartlomiej Zolnierkiewicz0d6f7e32008-02-01 23:09:28 +0100674 } else if (rw == 0 && ireason == 1) {
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200675 /*
676 * Some drives (ASUS) seem to tell us that status info is
677 * available. Just get it and ignore.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200679 (void)hwif->tp_ops->read_status(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 return 0;
681 } else {
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200682 /* drive wants a command packet, or invalid ireason... */
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200683 printk(KERN_ERR PFX "%s: %s: bad interrupt reason 0x%02x\n",
Paolo Ciarrocchi177773e2008-04-26 17:36:42 +0200684 drive->name, __func__, ireason);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 }
686
Bartlomiej Zolnierkiewicz9f10d9e2008-02-26 21:50:35 +0100687 if (rq->cmd_type == REQ_TYPE_ATA_PC)
688 rq->cmd_flags |= REQ_FAILED;
689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 cdrom_end_request(drive, 0);
691 return -1;
692}
693
694/*
Bartlomiej Zolnierkiewicz64814f22008-02-01 23:09:26 +0100695 * Assume that the drive will always provide data in multiples of at least
696 * SECTOR_SIZE, as it gets hairy to keep track of the transfers otherwise.
697 */
698static int ide_cd_check_transfer_size(ide_drive_t *drive, int len)
699{
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200700 ide_debug_log(IDE_DBG_FUNC, "Call %s, len: %d\n", __func__, len);
701
Bartlomiej Zolnierkiewicz64814f22008-02-01 23:09:26 +0100702 if ((len % SECTOR_SIZE) == 0)
703 return 0;
704
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200705 printk(KERN_ERR PFX "%s: %s: Bad transfer size %d\n", drive->name,
706 __func__, len);
Bartlomiej Zolnierkiewicz64814f22008-02-01 23:09:26 +0100707
Borislav Petkov570f89e2008-07-23 19:56:02 +0200708 if (drive->atapi_flags & IDE_AFLAG_LIMIT_NFRAMES)
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200709 printk(KERN_ERR PFX "This drive is not supported by this "
710 "version of the driver\n");
Bartlomiej Zolnierkiewicz64814f22008-02-01 23:09:26 +0100711 else {
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200712 printk(KERN_ERR PFX "Trying to limit transfer sizes\n");
Borislav Petkov570f89e2008-07-23 19:56:02 +0200713 drive->atapi_flags |= IDE_AFLAG_LIMIT_NFRAMES;
Bartlomiej Zolnierkiewicz64814f22008-02-01 23:09:26 +0100714 }
715
716 return 1;
717}
718
Bartlomiej Zolnierkiewicz0041e7c2008-02-01 23:09:28 +0100719static ide_startstop_t cdrom_newpc_intr(ide_drive_t *);
Bartlomiej Zolnierkiewicz94f5a862008-02-01 23:09:26 +0100720
Borislav Petkov90eb8082008-07-16 20:33:46 +0200721static ide_startstop_t ide_cd_prepare_rw_request(ide_drive_t *drive,
722 struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200724 ide_debug_log(IDE_DBG_RQ, "Call %s: rq->cmd_flags: 0x%x\n", __func__,
725 rq->cmd_flags);
726
Bartlomiej Zolnierkiewicz29f3aac2008-02-01 23:09:27 +0100727 if (rq_data_dir(rq) == READ) {
728 unsigned short sectors_per_frame =
729 queue_hardsect_size(drive->queue) >> SECTOR_BITS;
730 int nskip = rq->sector & (sectors_per_frame - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Bartlomiej Zolnierkiewicz29f3aac2008-02-01 23:09:27 +0100732 /*
733 * If the requested sector doesn't start on a frame boundary,
734 * we must adjust the start of the transfer so that it does,
735 * and remember to skip the first few sectors.
736 *
737 * If the rq->current_nr_sectors field is larger than the size
738 * of the buffer, it will mean that we're to skip a number of
739 * sectors equal to the amount by which rq->current_nr_sectors
740 * is larger than the buffer size.
741 */
742 if (nskip > 0) {
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200743 /* sanity check... */
Bartlomiej Zolnierkiewicz29f3aac2008-02-01 23:09:27 +0100744 if (rq->current_nr_sectors !=
745 bio_cur_sectors(rq->bio)) {
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200746 printk(KERN_ERR PFX "%s: %s: buffer botch (%u)\n",
Paolo Ciarrocchi177773e2008-04-26 17:36:42 +0200747 drive->name, __func__,
Bartlomiej Zolnierkiewicz29f3aac2008-02-01 23:09:27 +0100748 rq->current_nr_sectors);
749 cdrom_end_request(drive, 0);
750 return ide_stopped;
751 }
752 rq->current_nr_sectors += nskip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 }
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200755
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200756 /* set up the command */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 rq->timeout = ATAPI_WAIT_PC;
758
Borislav Petkov90eb8082008-07-16 20:33:46 +0200759 return ide_started;
760}
761
762/*
763 * Routine to send a read/write packet command to the drive. This is usually
764 * called directly from cdrom_start_{read,write}(). However, for drq_interrupt
765 * devices, it is called from an interrupt when the drive is ready to accept
766 * the command.
767 */
768static ide_startstop_t cdrom_start_rw_cont(ide_drive_t *drive)
769{
770 struct request *rq = drive->hwif->hwgroup->rq;
771
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200772 /* send the command to the drive and return */
Bartlomiej Zolnierkiewicz0041e7c2008-02-01 23:09:28 +0100773 return cdrom_transfer_packet_command(drive, rq, cdrom_newpc_intr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774}
775
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776#define IDECD_SEEK_THRESHOLD (1000) /* 1000 blocks */
777#define IDECD_SEEK_TIMER (5 * WAIT_MIN_SLEEP) /* 100 ms */
778#define IDECD_SEEK_TIMEOUT (2 * WAIT_CMD) /* 20 sec */
779
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +0200780static ide_startstop_t cdrom_seek_intr(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
782 struct cdrom_info *info = drive->driver_data;
783 int stat;
784 static int retry = 10;
785
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200786 ide_debug_log(IDE_DBG_FUNC, "Call %s\n", __func__);
787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 if (cdrom_decode_status(drive, 0, &stat))
789 return ide_stopped;
Bartlomiej Zolnierkiewicz4fe67172008-02-01 23:09:21 +0100790
Borislav Petkov570f89e2008-07-23 19:56:02 +0200791 drive->atapi_flags |= IDE_AFLAG_SEEKING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
793 if (retry && time_after(jiffies, info->start_seek + IDECD_SEEK_TIMER)) {
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200794 if (--retry == 0)
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200795 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 }
797 return ide_stopped;
798}
799
Borislav Petkove529c602008-07-16 20:33:46 +0200800static void ide_cd_prepare_seek_request(ide_drive_t *drive, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 sector_t frame = rq->sector;
803
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200804 ide_debug_log(IDE_DBG_FUNC, "Call %s\n", __func__);
805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 sector_div(frame, queue_hardsect_size(drive->queue) >> SECTOR_BITS);
807
FUJITA Tomonorid34c87e2008-04-29 14:37:52 +0200808 memset(rq->cmd, 0, BLK_MAX_CDB);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 rq->cmd[0] = GPCMD_SEEK;
810 put_unaligned(cpu_to_be32(frame), (unsigned int *) &rq->cmd[2]);
811
812 rq->timeout = ATAPI_WAIT_PC;
Borislav Petkove529c602008-07-16 20:33:46 +0200813}
814
815static ide_startstop_t cdrom_start_seek_continuation(ide_drive_t *drive)
816{
817 struct request *rq = drive->hwif->hwgroup->rq;
818
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 return cdrom_transfer_packet_command(drive, rq, &cdrom_seek_intr);
820}
821
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +0200822/*
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200823 * Fix up a possibly partially-processed request so that we can start it over
824 * entirely, or even put it back on the request queue.
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +0200825 */
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200826static void ide_cd_restore_request(ide_drive_t *drive, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200828
829 ide_debug_log(IDE_DBG_FUNC, "Call %s\n", __func__);
830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 if (rq->buffer != bio_data(rq->bio)) {
Borislav Petkov83c85652008-04-26 22:25:15 +0200832 sector_t n =
833 (rq->buffer - (char *)bio_data(rq->bio)) / SECTOR_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
835 rq->buffer = bio_data(rq->bio);
836 rq->nr_sectors += n;
837 rq->sector -= n;
838 }
Borislav Petkov83c85652008-04-26 22:25:15 +0200839 rq->current_nr_sectors = bio_cur_sectors(rq->bio);
840 rq->hard_cur_sectors = rq->current_nr_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 rq->hard_nr_sectors = rq->nr_sectors;
842 rq->hard_sector = rq->sector;
843 rq->q->prep_rq_fn(rq->q, rq);
844}
845
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200846static void ide_cd_request_sense_fixup(ide_drive_t *drive, struct request *rq)
Bartlomiej Zolnierkiewicz8ee69f52008-02-01 23:09:25 +0100847{
Borislav Petkov71b429ca2008-10-17 18:09:14 +0200848 ide_debug_log(IDE_DBG_FUNC, "Call %s, rq->cmd[0]: 0x%x\n",
849 __func__, rq->cmd[0]);
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200850
Bartlomiej Zolnierkiewicz8ee69f52008-02-01 23:09:25 +0100851 /*
852 * Some of the trailing request sense fields are optional,
853 * and some drives don't send them. Sigh.
854 */
855 if (rq->cmd[0] == GPCMD_REQUEST_SENSE &&
856 rq->data_len > 0 && rq->data_len <= 5)
857 while (rq->data_len > 0) {
858 *(u8 *)rq->data++ = 0;
859 --rq->data_len;
860 }
861}
862
FUJITA Tomonori5f828542008-07-15 21:21:42 +0200863int ide_cd_queue_pc(ide_drive_t *drive, const unsigned char *cmd,
864 int write, void *buffer, unsigned *bufflen,
865 struct request_sense *sense, int timeout,
866 unsigned int cmd_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867{
FUJITA Tomonori5f828542008-07-15 21:21:42 +0200868 struct cdrom_info *info = drive->driver_data;
869 struct request_sense local_sense;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 int retries = 10;
FUJITA Tomonori5f828542008-07-15 21:21:42 +0200871 unsigned int flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
FUJITA Tomonori5f828542008-07-15 21:21:42 +0200873 if (!sense)
874 sense = &local_sense;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Borislav Petkov71b429ca2008-10-17 18:09:14 +0200876 ide_debug_log(IDE_DBG_PC, "Call %s, cmd[0]: 0x%x, write: 0x%x, "
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200877 "timeout: %d, cmd_flags: 0x%x\n", __func__, cmd[0], write,
878 timeout, cmd_flags);
879
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200880 /* start of retry loop */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 do {
FUJITA Tomonori5f828542008-07-15 21:21:42 +0200882 struct request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
FUJITA Tomonori5f828542008-07-15 21:21:42 +0200885 rq = blk_get_request(drive->queue, write, __GFP_WAIT);
886
887 memcpy(rq->cmd, cmd, BLK_MAX_CDB);
888 rq->cmd_type = REQ_TYPE_ATA_PC;
889 rq->sense = sense;
890 rq->cmd_flags |= cmd_flags;
891 rq->timeout = timeout;
892 if (buffer) {
893 rq->data = buffer;
894 rq->data_len = *bufflen;
895 }
896
897 error = blk_execute_rq(drive->queue, info->disk, rq, 0);
898
899 if (buffer)
900 *bufflen = rq->data_len;
901
902 flags = rq->cmd_flags;
903 blk_put_request(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200905 /*
906 * FIXME: we should probably abort/retry or something in case of
907 * failure.
908 */
FUJITA Tomonori5f828542008-07-15 21:21:42 +0200909 if (flags & REQ_FAILED) {
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200910 /*
911 * The request failed. Retry if it was due to a unit
912 * attention status (usually means media was changed).
913 */
FUJITA Tomonori5f828542008-07-15 21:21:42 +0200914 struct request_sense *reqbuf = sense;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
916 if (reqbuf->sense_key == UNIT_ATTENTION)
917 cdrom_saw_media_change(drive);
918 else if (reqbuf->sense_key == NOT_READY &&
919 reqbuf->asc == 4 && reqbuf->ascq != 4) {
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200920 /*
921 * The drive is in the process of loading
922 * a disk. Retry, but wait a little to give
923 * the drive time to complete the load.
924 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 ssleep(2);
926 } else {
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200927 /* otherwise, don't retry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 retries = 0;
929 }
930 --retries;
931 }
932
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200933 /* end of retry loop */
FUJITA Tomonori5f828542008-07-15 21:21:42 +0200934 } while ((flags & REQ_FAILED) && retries >= 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200936 /* return an error if the command failed */
FUJITA Tomonori5f828542008-07-15 21:21:42 +0200937 return (flags & REQ_FAILED) ? -EIO : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938}
939
940/*
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200941 * Called from blk_end_request_callback() after the data of the request is
942 * completed and before the request itself is completed. By returning value '1',
943 * blk_end_request_callback() returns immediately without completing it.
Kiyoshi Uedaaaa04c22007-12-11 17:51:23 -0500944 */
945static int cdrom_newpc_intr_dummy_cb(struct request *rq)
946{
947 return 1;
948}
949
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950static ide_startstop_t cdrom_newpc_intr(ide_drive_t *drive)
951{
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +0200952 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 struct request *rq = HWGROUP(drive)->rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 xfer_func_t *xferfunc;
Bartlomiej Zolnierkiewicz0041e7c2008-02-01 23:09:28 +0100955 ide_expiry_t *expiry = NULL;
Bartlomiej Zolnierkiewicz18236492008-07-23 19:55:54 +0200956 int dma_error = 0, dma, stat, thislen, uptodate = 0;
Bartlomiej Zolnierkiewiczff1bfbc12008-02-01 23:09:25 +0100957 int write = (rq_data_dir(rq) == WRITE) ? 1 : 0;
958 unsigned int timeout;
Bartlomiej Zolnierkiewicz18236492008-07-23 19:55:54 +0200959 u16 len;
960 u8 ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200962 ide_debug_log(IDE_DBG_PC, "Call %s, rq->cmd[0]: 0x%x, write: 0x%x\n",
963 __func__, rq->cmd[0], write);
964
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200965 /* check for errors */
Borislav Petkov12469ac2008-10-13 21:39:49 +0200966 dma = drive->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 if (dma) {
Borislav Petkov12469ac2008-10-13 21:39:49 +0200968 drive->dma = 0;
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +0200969 dma_error = hwif->dma_ops->dma_end(drive);
Bartlomiej Zolnierkiewiczeba15fb2008-02-01 23:09:17 +0100970 if (dma_error) {
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200971 printk(KERN_ERR PFX "%s: DMA %s error\n", drive->name,
Bartlomiej Zolnierkiewiczff1bfbc12008-02-01 23:09:25 +0100972 write ? "write" : "read");
Bartlomiej Zolnierkiewiczeba15fb2008-02-01 23:09:17 +0100973 ide_dma_off(drive);
974 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 }
976
977 if (cdrom_decode_status(drive, 0, &stat))
978 return ide_stopped;
979
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200980 /* using dma, transfer is complete now */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 if (dma) {
Bartlomiej Zolnierkiewiczeba15fb2008-02-01 23:09:17 +0100982 if (dma_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 return ide_error(drive, "dma error", stat);
Bartlomiej Zolnierkiewicz0041e7c2008-02-01 23:09:28 +0100984 if (blk_fs_request(rq)) {
985 ide_end_request(drive, 1, rq->nr_sectors);
986 return ide_stopped;
987 }
Bartlomiej Zolnierkiewiczff1bfbc12008-02-01 23:09:25 +0100988 goto end_request;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 }
990
Bartlomiej Zolnierkiewicz18236492008-07-23 19:55:54 +0200991 ide_read_bcount_and_ireason(drive, &len, &ireason);
Bartlomiej Zolnierkiewicz0041e7c2008-02-01 23:09:28 +0100992
993 thislen = blk_fs_request(rq) ? len : rq->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 if (thislen > len)
995 thislen = len;
996
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200997 ide_debug_log(IDE_DBG_PC, "%s: DRQ: stat: 0x%x, thislen: %d\n",
998 __func__, stat, thislen);
999
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02001000 /* If DRQ is clear, the command has completed. */
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +02001001 if ((stat & ATA_DRQ) == 0) {
Bartlomiej Zolnierkiewicz0041e7c2008-02-01 23:09:28 +01001002 if (blk_fs_request(rq)) {
1003 /*
1004 * If we're not done reading/writing, complain.
1005 * Otherwise, complete the command normally.
1006 */
1007 uptodate = 1;
1008 if (rq->current_nr_sectors > 0) {
Borislav Petkovfc8323f2008-10-13 21:39:48 +02001009 printk(KERN_ERR PFX "%s: %s: data underrun "
Bartlomiej Zolnierkiewicz0041e7c2008-02-01 23:09:28 +01001010 "(%d blocks)\n",
Paolo Ciarrocchi177773e2008-04-26 17:36:42 +02001011 drive->name, __func__,
Bartlomiej Zolnierkiewicz0041e7c2008-02-01 23:09:28 +01001012 rq->current_nr_sectors);
1013 if (!write)
1014 rq->cmd_flags |= REQ_FAILED;
1015 uptodate = 0;
1016 }
1017 cdrom_end_request(drive, uptodate);
1018 return ide_stopped;
1019 } else if (!blk_pc_request(rq)) {
Borislav Petkovfc8323f2008-10-13 21:39:48 +02001020 ide_cd_request_sense_fixup(drive, rq);
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02001021 /* complain if we still have data left to transfer */
Bartlomiej Zolnierkiewiczff1bfbc12008-02-01 23:09:25 +01001022 uptodate = rq->data_len ? 0 : 1;
1023 }
1024 goto end_request;
Kiyoshi Uedaaaa04c22007-12-11 17:51:23 -05001025 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02001027 /* check which way to transfer data */
Bartlomiej Zolnierkiewicz9f10d9e2008-02-26 21:50:35 +01001028 if (ide_cd_check_ireason(drive, rq, len, ireason, write))
1029 return ide_stopped;
Bartlomiej Zolnierkiewicz0041e7c2008-02-01 23:09:28 +01001030
Bartlomiej Zolnierkiewicz9f10d9e2008-02-26 21:50:35 +01001031 if (blk_fs_request(rq)) {
1032 if (write == 0) {
Bartlomiej Zolnierkiewicz0041e7c2008-02-01 23:09:28 +01001033 int nskip;
1034
1035 if (ide_cd_check_transfer_size(drive, len)) {
1036 cdrom_end_request(drive, 0);
1037 return ide_stopped;
1038 }
1039
1040 /*
1041 * First, figure out if we need to bit-bucket
1042 * any of the leading sectors.
1043 */
1044 nskip = min_t(int, rq->current_nr_sectors
1045 - bio_cur_sectors(rq->bio),
1046 thislen >> 9);
1047 if (nskip > 0) {
Borislav Petkovae8789f2008-07-16 20:33:45 +02001048 ide_pad_transfer(drive, write, nskip << 9);
Bartlomiej Zolnierkiewicz0041e7c2008-02-01 23:09:28 +01001049 rq->current_nr_sectors -= nskip;
1050 thislen -= (nskip << 9);
1051 }
1052 }
Bartlomiej Zolnierkiewiczff1bfbc12008-02-01 23:09:25 +01001053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
Bartlomiej Zolnierkiewiczff1bfbc12008-02-01 23:09:25 +01001055 if (ireason == 0) {
1056 write = 1;
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +02001057 xferfunc = hwif->tp_ops->output_data;
Bartlomiej Zolnierkiewicz9f10d9e2008-02-26 21:50:35 +01001058 } else {
Bartlomiej Zolnierkiewiczff1bfbc12008-02-01 23:09:25 +01001059 write = 0;
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +02001060 xferfunc = hwif->tp_ops->input_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 }
1062
Borislav Petkovfc8323f2008-10-13 21:39:48 +02001063 ide_debug_log(IDE_DBG_PC, "%s: data transfer, rq->cmd_type: 0x%x, "
1064 "ireason: 0x%x\n", __func__, rq->cmd_type, ireason);
1065
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02001066 /* transfer data */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 while (thislen > 0) {
Bartlomiej Zolnierkiewicz0041e7c2008-02-01 23:09:28 +01001068 u8 *ptr = blk_fs_request(rq) ? NULL : rq->data;
Bartlomiej Zolnierkiewicza11e77d2008-02-01 23:09:27 +01001069 int blen = rq->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02001071 /* bio backed? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 if (rq->bio) {
Bartlomiej Zolnierkiewicz0041e7c2008-02-01 23:09:28 +01001073 if (blk_fs_request(rq)) {
1074 ptr = rq->buffer;
1075 blen = rq->current_nr_sectors << 9;
1076 } else {
1077 ptr = bio_data(rq->bio);
1078 blen = bio_iovec(rq->bio)->bv_len;
1079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 }
1081
1082 if (!ptr) {
Bartlomiej Zolnierkiewicz0041e7c2008-02-01 23:09:28 +01001083 if (blk_fs_request(rq) && !write)
1084 /*
Borislav Petkov968c4962008-04-26 17:36:37 +02001085 * If the buffers are full, pipe the rest into
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02001086 * oblivion.
1087 */
Borislav Petkovae8789f2008-07-16 20:33:45 +02001088 ide_pad_transfer(drive, 0, thislen);
Bartlomiej Zolnierkiewicz0041e7c2008-02-01 23:09:28 +01001089 else {
Borislav Petkovfc8323f2008-10-13 21:39:48 +02001090 printk(KERN_ERR PFX "%s: confused, missing data\n",
Bartlomiej Zolnierkiewicz0041e7c2008-02-01 23:09:28 +01001091 drive->name);
1092 blk_dump_rq_flags(rq, rq_data_dir(rq)
1093 ? "cdrom_newpc_intr, write"
1094 : "cdrom_newpc_intr, read");
1095 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 break;
1097 }
1098
1099 if (blen > thislen)
1100 blen = thislen;
1101
Bartlomiej Zolnierkiewicz9567b342008-04-28 23:44:36 +02001102 xferfunc(drive, NULL, ptr, blen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
1104 thislen -= blen;
1105 len -= blen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Bartlomiej Zolnierkiewicz0041e7c2008-02-01 23:09:28 +01001107 if (blk_fs_request(rq)) {
1108 rq->buffer += blen;
1109 rq->nr_sectors -= (blen >> 9);
1110 rq->current_nr_sectors -= (blen >> 9);
1111 rq->sector += (blen >> 9);
1112
1113 if (rq->current_nr_sectors == 0 && rq->nr_sectors)
1114 cdrom_end_request(drive, 1);
1115 } else {
1116 rq->data_len -= blen;
1117
Kiyoshi Uedaaaa04c22007-12-11 17:51:23 -05001118 /*
1119 * The request can't be completed until DRQ is cleared.
1120 * So complete the data, but don't complete the request
1121 * using the dummy function for the callback feature
1122 * of blk_end_request_callback().
1123 */
Bartlomiej Zolnierkiewicz0041e7c2008-02-01 23:09:28 +01001124 if (rq->bio)
1125 blk_end_request_callback(rq, 0, blen,
Kiyoshi Uedaaaa04c22007-12-11 17:51:23 -05001126 cdrom_newpc_intr_dummy_cb);
Bartlomiej Zolnierkiewicz0041e7c2008-02-01 23:09:28 +01001127 else
1128 rq->data += blen;
1129 }
Andreas Schwabbcd88ac2008-02-26 21:50:35 +01001130 if (!write && blk_sense_request(rq))
1131 rq->sense_len += blen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 }
1133
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02001134 /* pad, if necessary */
Bartlomiej Zolnierkiewicz0041e7c2008-02-01 23:09:28 +01001135 if (!blk_fs_request(rq) && len > 0)
Borislav Petkovae8789f2008-07-16 20:33:45 +02001136 ide_pad_transfer(drive, write, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Bartlomiej Zolnierkiewiczff1bfbc12008-02-01 23:09:25 +01001138 if (blk_pc_request(rq)) {
1139 timeout = rq->timeout;
Bartlomiej Zolnierkiewiczff1bfbc12008-02-01 23:09:25 +01001140 } else {
1141 timeout = ATAPI_WAIT_PC;
Bartlomiej Zolnierkiewicz0041e7c2008-02-01 23:09:28 +01001142 if (!blk_fs_request(rq))
1143 expiry = cdrom_timer_expiry;
Bartlomiej Zolnierkiewiczff1bfbc12008-02-01 23:09:25 +01001144 }
1145
1146 ide_set_handler(drive, cdrom_newpc_intr, timeout, expiry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 return ide_started;
Bartlomiej Zolnierkiewiczff1bfbc12008-02-01 23:09:25 +01001148
1149end_request:
1150 if (blk_pc_request(rq)) {
1151 unsigned long flags;
Kiyoshi Ueda14e04c32008-02-19 01:41:26 +01001152 unsigned int dlen = rq->data_len;
1153
1154 if (dma)
1155 rq->data_len = 0;
Bartlomiej Zolnierkiewiczff1bfbc12008-02-01 23:09:25 +01001156
1157 spin_lock_irqsave(&ide_lock, flags);
Kiyoshi Ueda14e04c32008-02-19 01:41:26 +01001158 if (__blk_end_request(rq, 0, dlen))
Bartlomiej Zolnierkiewiczff1bfbc12008-02-01 23:09:25 +01001159 BUG();
1160 HWGROUP(drive)->rq = NULL;
1161 spin_unlock_irqrestore(&ide_lock, flags);
1162 } else {
1163 if (!uptodate)
1164 rq->cmd_flags |= REQ_FAILED;
1165 cdrom_end_request(drive, uptodate);
1166 }
1167 return ide_stopped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168}
1169
Bartlomiej Zolnierkiewicz21ea1f02008-02-01 23:09:27 +01001170static ide_startstop_t cdrom_start_rw(ide_drive_t *drive, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171{
Bartlomiej Zolnierkiewicz21ea1f02008-02-01 23:09:27 +01001172 struct cdrom_info *cd = drive->driver_data;
1173 int write = rq_data_dir(rq) == WRITE;
1174 unsigned short sectors_per_frame =
1175 queue_hardsect_size(drive->queue) >> SECTOR_BITS;
1176
Borislav Petkov71b429ca2008-10-17 18:09:14 +02001177 ide_debug_log(IDE_DBG_RQ, "Call %s, rq->cmd[0]: 0x%x, write: 0x%x, "
1178 "secs_per_frame: %u\n",
1179 __func__, rq->cmd[0], write, sectors_per_frame);
Borislav Petkovfc8323f2008-10-13 21:39:48 +02001180
Bartlomiej Zolnierkiewicz21ea1f02008-02-01 23:09:27 +01001181 if (write) {
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02001182 /* disk has become write protected */
Tejun Heob7db9952008-08-25 19:56:10 +09001183 if (get_disk_ro(cd->disk)) {
Bartlomiej Zolnierkiewicz21ea1f02008-02-01 23:09:27 +01001184 cdrom_end_request(drive, 0);
1185 return ide_stopped;
1186 }
1187 } else {
1188 /*
1189 * We may be retrying this request after an error. Fix up any
1190 * weirdness which might be present in the request packet.
1191 */
Borislav Petkovfc8323f2008-10-13 21:39:48 +02001192 ide_cd_restore_request(drive, rq);
Bartlomiej Zolnierkiewicz21ea1f02008-02-01 23:09:27 +01001193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02001195 /* use DMA, if possible / writes *must* be hardware frame aligned */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 if ((rq->nr_sectors & (sectors_per_frame - 1)) ||
1197 (rq->sector & (sectors_per_frame - 1))) {
Bartlomiej Zolnierkiewicz21ea1f02008-02-01 23:09:27 +01001198 if (write) {
1199 cdrom_end_request(drive, 0);
1200 return ide_stopped;
1201 }
Borislav Petkov12469ac2008-10-13 21:39:49 +02001202 drive->dma = 0;
Bartlomiej Zolnierkiewicz21ea1f02008-02-01 23:09:27 +01001203 } else
Borislav Petkov12469ac2008-10-13 21:39:49 +02001204 drive->dma = !!(drive->dev_flags & IDE_DFLAG_USING_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Bartlomiej Zolnierkiewicz21ea1f02008-02-01 23:09:27 +01001206 if (write)
1207 cd->devinfo.media_written = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Borislav Petkovb6ca4402008-07-16 20:33:46 +02001209 return ide_started;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210}
1211
1212static ide_startstop_t cdrom_do_newpc_cont(ide_drive_t *drive)
1213{
1214 struct request *rq = HWGROUP(drive)->rq;
1215
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 return cdrom_transfer_packet_command(drive, rq, cdrom_newpc_intr);
1217}
1218
Borislav Petkovb6ca4402008-07-16 20:33:46 +02001219static void cdrom_do_block_pc(ide_drive_t *drive, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Borislav Petkov71b429ca2008-10-17 18:09:14 +02001222 ide_debug_log(IDE_DBG_PC, "Call %s, rq->cmd[0]: 0x%x, "
1223 "rq->cmd_type: 0x%x\n", __func__, rq->cmd[0],
Borislav Petkovfc8323f2008-10-13 21:39:48 +02001224 rq->cmd_type);
1225
Bartlomiej Zolnierkiewiczc9f56a82008-02-01 23:09:26 +01001226 if (blk_pc_request(rq))
1227 rq->cmd_flags |= REQ_QUIET;
1228 else
1229 rq->cmd_flags &= ~REQ_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
Borislav Petkov12469ac2008-10-13 21:39:49 +02001231 drive->dma = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02001233 /* sg request */
FUJITA Tomonorie5318b52008-07-16 20:33:35 +02001234 if (rq->bio || ((rq->cmd_type == REQ_TYPE_ATA_PC) && rq->data_len)) {
1235 struct request_queue *q = drive->queue;
1236 unsigned int alignment;
FUJITA Tomonoriefa402d2008-10-10 22:39:22 +02001237 char *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
FUJITA Tomonorie5318b52008-07-16 20:33:35 +02001239 if (rq->bio)
FUJITA Tomonoriefa402d2008-10-10 22:39:22 +02001240 buf = bio_data(rq->bio);
FUJITA Tomonorie5318b52008-07-16 20:33:35 +02001241 else
FUJITA Tomonoriefa402d2008-10-10 22:39:22 +02001242 buf = rq->data;
FUJITA Tomonorie5318b52008-07-16 20:33:35 +02001243
Borislav Petkov12469ac2008-10-13 21:39:49 +02001244 drive->dma = !!(drive->dev_flags & IDE_DFLAG_USING_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
1246 /*
1247 * check if dma is safe
Linus Torvalds5d9e4ea2005-05-27 07:36:17 -07001248 *
1249 * NOTE! The "len" and "addr" checks should possibly have
1250 * separate masks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 */
FUJITA Tomonorie5318b52008-07-16 20:33:35 +02001252 alignment = queue_dma_alignment(q) | q->dma_pad_mask;
Borislav Petkov9bd27cb2008-11-02 21:40:07 +01001253 if ((unsigned long)buf & alignment
1254 || rq->data_len & q->dma_pad_mask
FUJITA Tomonoriefa402d2008-10-10 22:39:22 +02001255 || object_is_on_stack(buf))
Borislav Petkov12469ac2008-10-13 21:39:49 +02001256 drive->dma = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258}
1259
Borislav Petkov99384ae2008-07-16 20:33:45 +02001260static ide_startstop_t ide_cd_do_request(ide_drive_t *drive, struct request *rq,
Borislav Petkove5e076a2008-04-26 22:25:15 +02001261 sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 struct cdrom_info *info = drive->driver_data;
Borislav Petkovb6ca4402008-07-16 20:33:46 +02001264 ide_handler_t *fn;
1265 int xferlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
Borislav Petkov71b429ca2008-10-17 18:09:14 +02001267 ide_debug_log(IDE_DBG_RQ, "Call %s, rq->cmd[0]: 0x%x, "
1268 "rq->cmd_type: 0x%x, block: %llu\n",
1269 __func__, rq->cmd[0], rq->cmd_type,
1270 (unsigned long long)block);
Borislav Petkovfc8323f2008-10-13 21:39:48 +02001271
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 if (blk_fs_request(rq)) {
Borislav Petkov570f89e2008-07-23 19:56:02 +02001273 if (drive->atapi_flags & IDE_AFLAG_SEEKING) {
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +02001274 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 unsigned long elapsed = jiffies - info->start_seek;
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +02001276 int stat = hwif->tp_ops->read_status(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +02001278 if ((stat & ATA_DSC) != ATA_DSC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 if (elapsed < IDECD_SEEK_TIMEOUT) {
Borislav Petkov83c85652008-04-26 22:25:15 +02001280 ide_stall_queue(drive,
1281 IDECD_SEEK_TIMER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 return ide_stopped;
1283 }
Borislav Petkovfc8323f2008-10-13 21:39:48 +02001284 printk(KERN_ERR PFX "%s: DSC timeout\n",
Borislav Petkov83c85652008-04-26 22:25:15 +02001285 drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 }
Borislav Petkov570f89e2008-07-23 19:56:02 +02001287 drive->atapi_flags &= ~IDE_AFLAG_SEEKING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 }
Borislav Petkov83c85652008-04-26 22:25:15 +02001289 if (rq_data_dir(rq) == READ &&
1290 IDE_LARGE_SEEK(info->last_block, block,
Borislav Petkovb6ca4402008-07-16 20:33:46 +02001291 IDECD_SEEK_THRESHOLD) &&
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001292 (drive->dev_flags & IDE_DFLAG_DSC_OVERLAP)) {
Borislav Petkovb6ca4402008-07-16 20:33:46 +02001293 xferlen = 0;
1294 fn = cdrom_start_seek_continuation;
Borislav Petkove529c602008-07-16 20:33:46 +02001295
Borislav Petkov12469ac2008-10-13 21:39:49 +02001296 drive->dma = 0;
Borislav Petkov4b01fcb2008-07-16 20:33:46 +02001297 info->start_seek = jiffies;
Borislav Petkove529c602008-07-16 20:33:46 +02001298
1299 ide_cd_prepare_seek_request(drive, rq);
Borislav Petkovb6ca4402008-07-16 20:33:46 +02001300 } else {
1301 xferlen = 32768;
1302 fn = cdrom_start_rw_cont;
Borislav Petkov90eb8082008-07-16 20:33:46 +02001303
Borislav Petkovb6ca4402008-07-16 20:33:46 +02001304 if (cdrom_start_rw(drive, rq) == ide_stopped)
1305 return ide_stopped;
Borislav Petkov90eb8082008-07-16 20:33:46 +02001306
1307 if (ide_cd_prepare_rw_request(drive, rq) == ide_stopped)
1308 return ide_stopped;
Borislav Petkovb6ca4402008-07-16 20:33:46 +02001309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 info->last_block = block;
Bartlomiej Zolnierkiewiczc9f56a82008-02-01 23:09:26 +01001311 } else if (blk_sense_request(rq) || blk_pc_request(rq) ||
Jens Axboecea28852006-10-12 15:08:45 +02001312 rq->cmd_type == REQ_TYPE_ATA_PC) {
Borislav Petkovb6ca4402008-07-16 20:33:46 +02001313 xferlen = rq->data_len;
1314 fn = cdrom_do_newpc_cont;
Borislav Petkov7fcebda2008-07-16 20:33:46 +02001315
1316 if (!rq->timeout)
1317 rq->timeout = ATAPI_WAIT_PC;
1318
Borislav Petkovb6ca4402008-07-16 20:33:46 +02001319 cdrom_do_block_pc(drive, rq);
Jens Axboe4aff5e22006-08-10 08:44:47 +02001320 } else if (blk_special_request(rq)) {
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02001321 /* right now this can only be a reset... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 cdrom_end_request(drive, 1);
1323 return ide_stopped;
Borislav Petkovab9d6e32008-07-16 20:33:45 +02001324 } else {
Borislav Petkovfc8323f2008-10-13 21:39:48 +02001325 blk_dump_rq_flags(rq, DRV_NAME " bad flags");
Borislav Petkovab9d6e32008-07-16 20:33:45 +02001326 cdrom_end_request(drive, 0);
1327 return ide_stopped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 }
Borislav Petkovb6ca4402008-07-16 20:33:46 +02001329
1330 return cdrom_start_packet_command(drive, xferlen, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331}
1332
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02001333/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 * Ioctl handling.
1335 *
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02001336 * Routines which queue packet commands take as a final argument a pointer to a
1337 * request_sense struct. If execution of the command results in an error with a
1338 * CHECK CONDITION status, this structure will be filled with the results of the
1339 * subsequent request sense command. The pointer can also be NULL, in which case
1340 * no sense information is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 */
Borislav Petkove5e076a2008-04-26 22:25:15 +02001342static void msf_from_bcd(struct atapi_msf *msf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343{
Adrian Bunkfc99856a2008-08-18 21:40:04 +02001344 msf->minute = bcd2bin(msf->minute);
1345 msf->second = bcd2bin(msf->second);
1346 msf->frame = bcd2bin(msf->frame);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347}
1348
Borislav Petkovf9afd182008-02-01 23:09:29 +01001349int cdrom_check_status(ide_drive_t *drive, struct request_sense *sense)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 struct cdrom_info *info = drive->driver_data;
1352 struct cdrom_device_info *cdi = &info->devinfo;
FUJITA Tomonori5f828542008-07-15 21:21:42 +02001353 unsigned char cmd[BLK_MAX_CDB];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
Borislav Petkovfc8323f2008-10-13 21:39:48 +02001355 ide_debug_log(IDE_DBG_FUNC, "Call %s\n", __func__);
1356
FUJITA Tomonori5f828542008-07-15 21:21:42 +02001357 memset(cmd, 0, BLK_MAX_CDB);
1358 cmd[0] = GPCMD_TEST_UNIT_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
Bartlomiej Zolnierkiewiczcdf60002008-02-01 23:09:22 +01001360 /*
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02001361 * Sanyo 3 CD changer uses byte 7 of TEST_UNIT_READY to switch CDs
1362 * instead of supporting the LOAD_UNLOAD opcode.
Bartlomiej Zolnierkiewiczcdf60002008-02-01 23:09:22 +01001363 */
FUJITA Tomonori5f828542008-07-15 21:21:42 +02001364 cmd[7] = cdi->sanyo_slot % 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Harvey Harrison141d3b22008-07-23 19:56:02 +02001366 return ide_cd_queue_pc(drive, cmd, 0, NULL, NULL, sense, 0, REQ_QUIET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367}
1368
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369static int cdrom_read_capacity(ide_drive_t *drive, unsigned long *capacity,
1370 unsigned long *sectors_per_frame,
1371 struct request_sense *sense)
1372{
1373 struct {
Harvey Harrison141d3b22008-07-23 19:56:02 +02001374 __be32 lba;
1375 __be32 blocklen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 } capbuf;
1377
1378 int stat;
FUJITA Tomonori5f828542008-07-15 21:21:42 +02001379 unsigned char cmd[BLK_MAX_CDB];
1380 unsigned len = sizeof(capbuf);
Petr Tesarik938bb032008-08-05 18:17:02 +02001381 u32 blocklen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Borislav Petkovfc8323f2008-10-13 21:39:48 +02001383 ide_debug_log(IDE_DBG_FUNC, "Call %s\n", __func__);
1384
FUJITA Tomonori5f828542008-07-15 21:21:42 +02001385 memset(cmd, 0, BLK_MAX_CDB);
1386 cmd[0] = GPCMD_READ_CDVD_CAPACITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
FUJITA Tomonori5f828542008-07-15 21:21:42 +02001388 stat = ide_cd_queue_pc(drive, cmd, 0, &capbuf, &len, sense, 0,
1389 REQ_QUIET);
Jens Axboee8e7b9e2008-07-24 22:53:35 +02001390 if (stat)
1391 return stat;
1392
1393 /*
1394 * Sanity check the given block size
1395 */
Petr Tesarik938bb032008-08-05 18:17:02 +02001396 blocklen = be32_to_cpu(capbuf.blocklen);
1397 switch (blocklen) {
1398 case 512:
1399 case 1024:
1400 case 2048:
1401 case 4096:
Jens Axboee8e7b9e2008-07-24 22:53:35 +02001402 break;
1403 default:
Borislav Petkovfc8323f2008-10-13 21:39:48 +02001404 printk(KERN_ERR PFX "%s: weird block size %u\n",
1405 drive->name, blocklen);
1406 printk(KERN_ERR PFX "%s: default to 2kb block size\n",
1407 drive->name);
Petr Tesarik938bb032008-08-05 18:17:02 +02001408 blocklen = 2048;
Jens Axboee8e7b9e2008-07-24 22:53:35 +02001409 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 }
1411
Jens Axboee8e7b9e2008-07-24 22:53:35 +02001412 *capacity = 1 + be32_to_cpu(capbuf.lba);
Petr Tesarik938bb032008-08-05 18:17:02 +02001413 *sectors_per_frame = blocklen >> SECTOR_BITS;
Borislav Petkov71b429ca2008-10-17 18:09:14 +02001414
1415 ide_debug_log(IDE_DBG_PROBE, "%s: cap: %lu, sectors_per_frame: %lu\n",
1416 __func__, *capacity, *sectors_per_frame);
1417
Jens Axboee8e7b9e2008-07-24 22:53:35 +02001418 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419}
1420
1421static int cdrom_read_tocentry(ide_drive_t *drive, int trackno, int msf_flag,
1422 int format, char *buf, int buflen,
1423 struct request_sense *sense)
1424{
FUJITA Tomonori5f828542008-07-15 21:21:42 +02001425 unsigned char cmd[BLK_MAX_CDB];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
Borislav Petkovfc8323f2008-10-13 21:39:48 +02001427 ide_debug_log(IDE_DBG_FUNC, "Call %s\n", __func__);
1428
FUJITA Tomonori5f828542008-07-15 21:21:42 +02001429 memset(cmd, 0, BLK_MAX_CDB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
FUJITA Tomonori5f828542008-07-15 21:21:42 +02001431 cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
1432 cmd[6] = trackno;
1433 cmd[7] = (buflen >> 8);
1434 cmd[8] = (buflen & 0xff);
1435 cmd[9] = (format << 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
1437 if (msf_flag)
FUJITA Tomonori5f828542008-07-15 21:21:42 +02001438 cmd[1] = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
FUJITA Tomonori5f828542008-07-15 21:21:42 +02001440 return ide_cd_queue_pc(drive, cmd, 0, buf, &buflen, sense, 0, REQ_QUIET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441}
1442
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443/* Try to read the entire TOC for the disk into our internal buffer. */
Bartlomiej Zolnierkiewicz17802992008-02-01 23:09:25 +01001444int ide_cd_read_toc(ide_drive_t *drive, struct request_sense *sense)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445{
1446 int stat, ntracks, i;
1447 struct cdrom_info *info = drive->driver_data;
1448 struct cdrom_device_info *cdi = &info->devinfo;
1449 struct atapi_toc *toc = info->toc;
1450 struct {
1451 struct atapi_toc_header hdr;
1452 struct atapi_toc_entry ent;
1453 } ms_tmp;
1454 long last_written;
1455 unsigned long sectors_per_frame = SECTORS_PER_FRAME;
1456
Borislav Petkovfc8323f2008-10-13 21:39:48 +02001457 ide_debug_log(IDE_DBG_FUNC, "Call %s\n", __func__);
1458
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 if (toc == NULL) {
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02001460 /* try to allocate space */
Jesper Juhl2a91f3e2005-10-30 15:02:45 -08001461 toc = kmalloc(sizeof(struct atapi_toc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 if (toc == NULL) {
Borislav Petkovfc8323f2008-10-13 21:39:48 +02001463 printk(KERN_ERR PFX "%s: No cdrom TOC buffer!\n",
Borislav Petkov83c85652008-04-26 22:25:15 +02001464 drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 return -ENOMEM;
1466 }
Jesper Juhl2a91f3e2005-10-30 15:02:45 -08001467 info->toc = toc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 }
1469
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02001470 /*
1471 * Check to see if the existing data is still valid. If it is,
1472 * just return.
1473 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 (void) cdrom_check_status(drive, sense);
1475
Borislav Petkov570f89e2008-07-23 19:56:02 +02001476 if (drive->atapi_flags & IDE_AFLAG_TOC_VALID)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 return 0;
1478
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02001479 /* try to get the total cdrom capacity and sector size */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 stat = cdrom_read_capacity(drive, &toc->capacity, &sectors_per_frame,
1481 sense);
1482 if (stat)
1483 toc->capacity = 0x1fffff;
1484
1485 set_capacity(info->disk, toc->capacity * sectors_per_frame);
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02001486 /* save a private copy of the TOC capacity for error handling */
Alan Coxdbe217a2006-06-25 05:47:44 -07001487 drive->probed_capacity = toc->capacity * sectors_per_frame;
1488
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 blk_queue_hardsect_size(drive->queue,
1490 sectors_per_frame << SECTOR_BITS);
1491
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02001492 /* first read just the header, so we know how long the TOC is */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 stat = cdrom_read_tocentry(drive, 0, 1, 0, (char *) &toc->hdr,
1494 sizeof(struct atapi_toc_header), sense);
Jesper Juhl2a91f3e2005-10-30 15:02:45 -08001495 if (stat)
1496 return stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
Borislav Petkov570f89e2008-07-23 19:56:02 +02001498 if (drive->atapi_flags & IDE_AFLAG_TOCTRACKS_AS_BCD) {
Adrian Bunkfc99856a2008-08-18 21:40:04 +02001499 toc->hdr.first_track = bcd2bin(toc->hdr.first_track);
1500 toc->hdr.last_track = bcd2bin(toc->hdr.last_track);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
1503 ntracks = toc->hdr.last_track - toc->hdr.first_track + 1;
1504 if (ntracks <= 0)
1505 return -EIO;
1506 if (ntracks > MAX_TRACKS)
1507 ntracks = MAX_TRACKS;
1508
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02001509 /* now read the whole schmeer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 stat = cdrom_read_tocentry(drive, toc->hdr.first_track, 1, 0,
1511 (char *)&toc->hdr,
1512 sizeof(struct atapi_toc_header) +
1513 (ntracks + 1) *
1514 sizeof(struct atapi_toc_entry), sense);
1515
1516 if (stat && toc->hdr.first_track > 1) {
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02001517 /*
1518 * Cds with CDI tracks only don't have any TOC entries, despite
1519 * of this the returned values are
1520 * first_track == last_track = number of CDI tracks + 1,
1521 * so that this case is indistinguishable from the same layout
1522 * plus an additional audio track. If we get an error for the
1523 * regular case, we assume a CDI without additional audio
1524 * tracks. In this case the readable TOC is empty (CDI tracks
1525 * are not included) and only holds the Leadout entry.
1526 *
1527 * Heiko Eißfeldt.
1528 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 ntracks = 0;
1530 stat = cdrom_read_tocentry(drive, CDROM_LEADOUT, 1, 0,
1531 (char *)&toc->hdr,
1532 sizeof(struct atapi_toc_header) +
1533 (ntracks + 1) *
1534 sizeof(struct atapi_toc_entry),
1535 sense);
Bartlomiej Zolnierkiewiczcdf60002008-02-01 23:09:22 +01001536 if (stat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 return stat;
Bartlomiej Zolnierkiewiczcdf60002008-02-01 23:09:22 +01001538
Borislav Petkov570f89e2008-07-23 19:56:02 +02001539 if (drive->atapi_flags & IDE_AFLAG_TOCTRACKS_AS_BCD) {
Adrian Bunkfc99856a2008-08-18 21:40:04 +02001540 toc->hdr.first_track = (u8)bin2bcd(CDROM_LEADOUT);
1541 toc->hdr.last_track = (u8)bin2bcd(CDROM_LEADOUT);
Bartlomiej Zolnierkiewiczcdf60002008-02-01 23:09:22 +01001542 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 toc->hdr.first_track = CDROM_LEADOUT;
1544 toc->hdr.last_track = CDROM_LEADOUT;
1545 }
1546 }
1547
1548 if (stat)
1549 return stat;
1550
Borislav Petkov7eb43fd2008-02-11 00:32:13 +01001551 toc->hdr.toc_length = be16_to_cpu(toc->hdr.toc_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
Borislav Petkov570f89e2008-07-23 19:56:02 +02001553 if (drive->atapi_flags & IDE_AFLAG_TOCTRACKS_AS_BCD) {
Adrian Bunkfc99856a2008-08-18 21:40:04 +02001554 toc->hdr.first_track = bcd2bin(toc->hdr.first_track);
1555 toc->hdr.last_track = bcd2bin(toc->hdr.last_track);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
Bartlomiej Zolnierkiewiczcdf60002008-02-01 23:09:22 +01001558 for (i = 0; i <= ntracks; i++) {
Borislav Petkov570f89e2008-07-23 19:56:02 +02001559 if (drive->atapi_flags & IDE_AFLAG_TOCADDR_AS_BCD) {
1560 if (drive->atapi_flags & IDE_AFLAG_TOCTRACKS_AS_BCD)
Adrian Bunkfc99856a2008-08-18 21:40:04 +02001561 toc->ent[i].track = bcd2bin(toc->ent[i].track);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 msf_from_bcd(&toc->ent[i].addr.msf);
1563 }
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +02001564 toc->ent[i].addr.lba = msf_to_lba(toc->ent[i].addr.msf.minute,
1565 toc->ent[i].addr.msf.second,
1566 toc->ent[i].addr.msf.frame);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 }
1568
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 if (toc->hdr.first_track != CDROM_LEADOUT) {
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02001570 /* read the multisession information */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 stat = cdrom_read_tocentry(drive, 0, 0, 1, (char *)&ms_tmp,
1572 sizeof(ms_tmp), sense);
Jesper Juhl2a91f3e2005-10-30 15:02:45 -08001573 if (stat)
1574 return stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
1576 toc->last_session_lba = be32_to_cpu(ms_tmp.ent.addr.lba);
1577 } else {
Borislav Petkove5e076a2008-04-26 22:25:15 +02001578 ms_tmp.hdr.last_track = CDROM_LEADOUT;
1579 ms_tmp.hdr.first_track = ms_tmp.hdr.last_track;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 toc->last_session_lba = msf_to_lba(0, 2, 0); /* 0m 2s 0f */
1581 }
1582
Borislav Petkov570f89e2008-07-23 19:56:02 +02001583 if (drive->atapi_flags & IDE_AFLAG_TOCADDR_AS_BCD) {
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02001584 /* re-read multisession information using MSF format */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 stat = cdrom_read_tocentry(drive, 0, 1, 1, (char *)&ms_tmp,
1586 sizeof(ms_tmp), sense);
1587 if (stat)
1588 return stat;
1589
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +02001590 msf_from_bcd(&ms_tmp.ent.addr.msf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 toc->last_session_lba = msf_to_lba(ms_tmp.ent.addr.msf.minute,
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +02001592 ms_tmp.ent.addr.msf.second,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 ms_tmp.ent.addr.msf.frame);
1594 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
1596 toc->xa_flag = (ms_tmp.hdr.first_track != ms_tmp.hdr.last_track);
1597
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02001598 /* now try to get the total cdrom capacity */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 stat = cdrom_get_last_written(cdi, &last_written);
1600 if (!stat && (last_written > toc->capacity)) {
1601 toc->capacity = last_written;
1602 set_capacity(info->disk, toc->capacity * sectors_per_frame);
Alan Coxdbe217a2006-06-25 05:47:44 -07001603 drive->probed_capacity = toc->capacity * sectors_per_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 }
1605
1606 /* Remember that we've read this stuff. */
Borislav Petkov570f89e2008-07-23 19:56:02 +02001607 drive->atapi_flags |= IDE_AFLAG_TOC_VALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
1609 return 0;
1610}
1611
Bartlomiej Zolnierkiewicz17802992008-02-01 23:09:25 +01001612int ide_cdrom_get_capabilities(ide_drive_t *drive, u8 *buf)
Eric Piel9235e682005-06-23 00:10:29 -07001613{
1614 struct cdrom_info *info = drive->driver_data;
1615 struct cdrom_device_info *cdi = &info->devinfo;
1616 struct packet_command cgc;
Bartlomiej Zolnierkiewicz455d80a2008-02-01 23:09:21 +01001617 int stat, attempts = 3, size = ATAPI_CAPABILITIES_PAGE_SIZE;
Eric Piel9235e682005-06-23 00:10:29 -07001618
Borislav Petkovfc8323f2008-10-13 21:39:48 +02001619 ide_debug_log(IDE_DBG_FUNC, "Call %s\n", __func__);
1620
Borislav Petkov570f89e2008-07-23 19:56:02 +02001621 if ((drive->atapi_flags & IDE_AFLAG_FULL_CAPS_PAGE) == 0)
Bartlomiej Zolnierkiewicz455d80a2008-02-01 23:09:21 +01001622 size -= ATAPI_CAPABILITIES_PAGE_PAD_SIZE;
Eric Piel9235e682005-06-23 00:10:29 -07001623
Bartlomiej Zolnierkiewicz455d80a2008-02-01 23:09:21 +01001624 init_cdrom_command(&cgc, buf, size, CGC_DATA_UNKNOWN);
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02001625 do {
1626 /* we seem to get stat=0x01,err=0x00 the first time (??) */
Eric Piel9235e682005-06-23 00:10:29 -07001627 stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CAPABILITIES_PAGE, 0);
1628 if (!stat)
1629 break;
1630 } while (--attempts);
1631 return stat;
1632}
1633
Bartlomiej Zolnierkiewicz17802992008-02-01 23:09:25 +01001634void ide_cdrom_update_speed(ide_drive_t *drive, u8 *buf)
Eric Piel9235e682005-06-23 00:10:29 -07001635{
Bartlomiej Zolnierkiewicz4fe67172008-02-01 23:09:21 +01001636 struct cdrom_info *cd = drive->driver_data;
Bartlomiej Zolnierkiewicz481c8c62008-02-01 23:09:20 +01001637 u16 curspeed, maxspeed;
1638
Borislav Petkovfc8323f2008-10-13 21:39:48 +02001639 ide_debug_log(IDE_DBG_FUNC, "Call %s\n", __func__);
1640
Borislav Petkov570f89e2008-07-23 19:56:02 +02001641 if (drive->atapi_flags & IDE_AFLAG_LE_SPEED_FIELDS) {
Harvey Harrison141d3b22008-07-23 19:56:02 +02001642 curspeed = le16_to_cpup((__le16 *)&buf[8 + 14]);
1643 maxspeed = le16_to_cpup((__le16 *)&buf[8 + 8]);
Eric Piel9235e682005-06-23 00:10:29 -07001644 } else {
Harvey Harrison141d3b22008-07-23 19:56:02 +02001645 curspeed = be16_to_cpup((__be16 *)&buf[8 + 14]);
1646 maxspeed = be16_to_cpup((__be16 *)&buf[8 + 8]);
Eric Piel9235e682005-06-23 00:10:29 -07001647 }
Bartlomiej Zolnierkiewicz481c8c62008-02-01 23:09:20 +01001648
Borislav Petkov71b429ca2008-10-17 18:09:14 +02001649 ide_debug_log(IDE_DBG_PROBE, "%s: curspeed: %u, maxspeed: %u\n",
1650 __func__, curspeed, maxspeed);
1651
Bartlomiej Zolnierkiewicz2bc4cf22008-02-01 23:09:22 +01001652 cd->current_speed = (curspeed + (176/2)) / 176;
1653 cd->max_speed = (maxspeed + (176/2)) / 176;
Eric Piel9235e682005-06-23 00:10:29 -07001654}
1655
Bartlomiej Zolnierkiewicz20e7f7e2008-02-01 23:09:20 +01001656#define IDE_CD_CAPABILITIES \
1657 (CDC_CLOSE_TRAY | CDC_OPEN_TRAY | CDC_LOCK | CDC_SELECT_SPEED | \
1658 CDC_SELECT_DISC | CDC_MULTI_SESSION | CDC_MCN | CDC_MEDIA_CHANGED | \
1659 CDC_PLAY_AUDIO | CDC_RESET | CDC_DRIVE_STATUS | CDC_CD_R | \
1660 CDC_CD_RW | CDC_DVD | CDC_DVD_R | CDC_DVD_RAM | CDC_GENERIC_PACKET | \
1661 CDC_MO_DRIVE | CDC_MRW | CDC_MRW_W | CDC_RAM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663static struct cdrom_device_ops ide_cdrom_dops = {
1664 .open = ide_cdrom_open_real,
1665 .release = ide_cdrom_release_real,
1666 .drive_status = ide_cdrom_drive_status,
1667 .media_changed = ide_cdrom_check_media_change_real,
1668 .tray_move = ide_cdrom_tray_move,
1669 .lock_door = ide_cdrom_lock_door,
1670 .select_speed = ide_cdrom_select_speed,
1671 .get_last_session = ide_cdrom_get_last_session,
1672 .get_mcn = ide_cdrom_get_mcn,
1673 .reset = ide_cdrom_reset,
1674 .audio_ioctl = ide_cdrom_audio_ioctl,
Bartlomiej Zolnierkiewicz20e7f7e2008-02-01 23:09:20 +01001675 .capability = IDE_CD_CAPABILITIES,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 .generic_packet = ide_cdrom_packet,
1677};
1678
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +02001679static int ide_cdrom_register(ide_drive_t *drive, int nslots)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680{
1681 struct cdrom_info *info = drive->driver_data;
1682 struct cdrom_device_info *devinfo = &info->devinfo;
1683
Borislav Petkovfc8323f2008-10-13 21:39:48 +02001684 ide_debug_log(IDE_DBG_PROBE, "Call %s, nslots: %d\n", __func__, nslots);
1685
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 devinfo->ops = &ide_cdrom_dops;
Bartlomiej Zolnierkiewicz2bc4cf22008-02-01 23:09:22 +01001687 devinfo->speed = info->current_speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 devinfo->capacity = nslots;
Jesper Juhl2a91f3e2005-10-30 15:02:45 -08001689 devinfo->handle = drive;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 strcpy(devinfo->name, drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
Borislav Petkov570f89e2008-07-23 19:56:02 +02001692 if (drive->atapi_flags & IDE_AFLAG_NO_SPEED_SELECT)
Bartlomiej Zolnierkiewicz3cbd8142007-12-24 15:23:43 +01001693 devinfo->mask |= CDC_SELECT_SPEED;
1694
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 devinfo->disk = info->disk;
1696 return register_cdrom(devinfo);
1697}
1698
Borislav Petkove5e076a2008-04-26 22:25:15 +02001699static int ide_cdrom_probe_capabilities(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700{
Bartlomiej Zolnierkiewicz4fe67172008-02-01 23:09:21 +01001701 struct cdrom_info *cd = drive->driver_data;
1702 struct cdrom_device_info *cdi = &cd->devinfo;
Bartlomiej Zolnierkiewicz455d80a2008-02-01 23:09:21 +01001703 u8 buf[ATAPI_CAPABILITIES_PAGE_SIZE];
1704 mechtype_t mechtype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 int nslots = 1;
1706
Borislav Petkovfc8323f2008-10-13 21:39:48 +02001707 ide_debug_log(IDE_DBG_PROBE, "Call %s, drive->media: 0x%x, "
1708 "drive->atapi_flags: 0x%lx\n", __func__, drive->media,
1709 drive->atapi_flags);
1710
Bartlomiej Zolnierkiewicz3f1b86d2008-02-01 23:09:20 +01001711 cdi->mask = (CDC_CD_R | CDC_CD_RW | CDC_DVD | CDC_DVD_R |
1712 CDC_DVD_RAM | CDC_SELECT_DISC | CDC_PLAY_AUDIO |
1713 CDC_MO_DRIVE | CDC_RAM);
1714
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 if (drive->media == ide_optical) {
Bartlomiej Zolnierkiewicz3f1b86d2008-02-01 23:09:20 +01001716 cdi->mask &= ~(CDC_MO_DRIVE | CDC_RAM);
Borislav Petkovfc8323f2008-10-13 21:39:48 +02001717 printk(KERN_ERR PFX "%s: ATAPI magneto-optical drive\n",
Borislav Petkov83c85652008-04-26 22:25:15 +02001718 drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 return nslots;
1720 }
1721
Borislav Petkov570f89e2008-07-23 19:56:02 +02001722 if (drive->atapi_flags & IDE_AFLAG_PRE_ATAPI12) {
1723 drive->atapi_flags &= ~IDE_AFLAG_NO_EJECT;
Bartlomiej Zolnierkiewicz3f1b86d2008-02-01 23:09:20 +01001724 cdi->mask &= ~CDC_PLAY_AUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 return nslots;
1726 }
1727
1728 /*
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02001729 * We have to cheat a little here. the packet will eventually be queued
1730 * with ide_cdrom_packet(), which extracts the drive from cdi->handle.
1731 * Since this device hasn't been registered with the Uniform layer yet,
1732 * it can't do this. Same goes for cdi->ops.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 */
Jesper Juhl2a91f3e2005-10-30 15:02:45 -08001734 cdi->handle = drive;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 cdi->ops = &ide_cdrom_dops;
1736
Bartlomiej Zolnierkiewicz455d80a2008-02-01 23:09:21 +01001737 if (ide_cdrom_get_capabilities(drive, buf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 return 0;
1739
Bartlomiej Zolnierkiewicz455d80a2008-02-01 23:09:21 +01001740 if ((buf[8 + 6] & 0x01) == 0)
Bartlomiej Zolnierkiewicz42619d32008-10-17 18:09:11 +02001741 drive->dev_flags &= ~IDE_DFLAG_DOORLOCKING;
Bartlomiej Zolnierkiewicz455d80a2008-02-01 23:09:21 +01001742 if (buf[8 + 6] & 0x08)
Borislav Petkov570f89e2008-07-23 19:56:02 +02001743 drive->atapi_flags &= ~IDE_AFLAG_NO_EJECT;
Bartlomiej Zolnierkiewicz455d80a2008-02-01 23:09:21 +01001744 if (buf[8 + 3] & 0x01)
Bartlomiej Zolnierkiewicz3f1b86d2008-02-01 23:09:20 +01001745 cdi->mask &= ~CDC_CD_R;
Bartlomiej Zolnierkiewicz455d80a2008-02-01 23:09:21 +01001746 if (buf[8 + 3] & 0x02)
Bartlomiej Zolnierkiewicz3f1b86d2008-02-01 23:09:20 +01001747 cdi->mask &= ~(CDC_CD_RW | CDC_RAM);
Bartlomiej Zolnierkiewicz455d80a2008-02-01 23:09:21 +01001748 if (buf[8 + 2] & 0x38)
Bartlomiej Zolnierkiewicz3f1b86d2008-02-01 23:09:20 +01001749 cdi->mask &= ~CDC_DVD;
Bartlomiej Zolnierkiewicz455d80a2008-02-01 23:09:21 +01001750 if (buf[8 + 3] & 0x20)
Bartlomiej Zolnierkiewicz3f1b86d2008-02-01 23:09:20 +01001751 cdi->mask &= ~(CDC_DVD_RAM | CDC_RAM);
Bartlomiej Zolnierkiewicz455d80a2008-02-01 23:09:21 +01001752 if (buf[8 + 3] & 0x10)
Bartlomiej Zolnierkiewicz3f1b86d2008-02-01 23:09:20 +01001753 cdi->mask &= ~CDC_DVD_R;
Borislav Petkov570f89e2008-07-23 19:56:02 +02001754 if ((buf[8 + 4] & 0x01) || (drive->atapi_flags & IDE_AFLAG_PLAY_AUDIO_OK))
Bartlomiej Zolnierkiewicz3f1b86d2008-02-01 23:09:20 +01001755 cdi->mask &= ~CDC_PLAY_AUDIO;
Bartlomiej Zolnierkiewicz455d80a2008-02-01 23:09:21 +01001756
1757 mechtype = buf[8 + 6] >> 5;
Borislav Petkovf20f2582008-10-05 18:23:27 +02001758 if (mechtype == mechtype_caddy ||
1759 mechtype == mechtype_popup ||
1760 (drive->atapi_flags & IDE_AFLAG_NO_AUTOCLOSE))
Bartlomiej Zolnierkiewicz3f1b86d2008-02-01 23:09:20 +01001761 cdi->mask |= CDC_CLOSE_TRAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 if (cdi->sanyo_slot > 0) {
Bartlomiej Zolnierkiewicz3f1b86d2008-02-01 23:09:20 +01001764 cdi->mask &= ~CDC_SELECT_DISC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 nslots = 3;
Bartlomiej Zolnierkiewiczcdf60002008-02-01 23:09:22 +01001766 } else if (mechtype == mechtype_individual_changer ||
1767 mechtype == mechtype_cartridge_changer) {
Bartlomiej Zolnierkiewicz2609d062008-02-01 23:09:19 +01001768 nslots = cdrom_number_of_slots(cdi);
1769 if (nslots > 1)
Bartlomiej Zolnierkiewicz3f1b86d2008-02-01 23:09:20 +01001770 cdi->mask &= ~CDC_SELECT_DISC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 }
1772
Bartlomiej Zolnierkiewicz455d80a2008-02-01 23:09:21 +01001773 ide_cdrom_update_speed(drive, buf);
Bartlomiej Zolnierkiewicz4fe67172008-02-01 23:09:21 +01001774
Borislav Petkovfc8323f2008-10-13 21:39:48 +02001775 printk(KERN_INFO PFX "%s: ATAPI", drive->name);
Bartlomiej Zolnierkiewicz4fe67172008-02-01 23:09:21 +01001776
1777 /* don't print speed if the drive reported 0 */
Bartlomiej Zolnierkiewicz2bc4cf22008-02-01 23:09:22 +01001778 if (cd->max_speed)
1779 printk(KERN_CONT " %dX", cd->max_speed);
Bartlomiej Zolnierkiewicz4fe67172008-02-01 23:09:21 +01001780
Bartlomiej Zolnierkiewicz3f1b86d2008-02-01 23:09:20 +01001781 printk(KERN_CONT " %s", (cdi->mask & CDC_DVD) ? "CD-ROM" : "DVD-ROM");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
Bartlomiej Zolnierkiewicz3f1b86d2008-02-01 23:09:20 +01001783 if ((cdi->mask & CDC_DVD_R) == 0 || (cdi->mask & CDC_DVD_RAM) == 0)
1784 printk(KERN_CONT " DVD%s%s",
1785 (cdi->mask & CDC_DVD_R) ? "" : "-R",
Borislav Petkov419a5b62008-10-17 18:09:14 +02001786 (cdi->mask & CDC_DVD_RAM) ? "" : "/RAM");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
Bartlomiej Zolnierkiewicz3f1b86d2008-02-01 23:09:20 +01001788 if ((cdi->mask & CDC_CD_R) == 0 || (cdi->mask & CDC_CD_RW) == 0)
1789 printk(KERN_CONT " CD%s%s",
1790 (cdi->mask & CDC_CD_R) ? "" : "-R",
1791 (cdi->mask & CDC_CD_RW) ? "" : "/RW");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
Bartlomiej Zolnierkiewicz3f1b86d2008-02-01 23:09:20 +01001793 if ((cdi->mask & CDC_SELECT_DISC) == 0)
1794 printk(KERN_CONT " changer w/%d slots", nslots);
1795 else
1796 printk(KERN_CONT " drive");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
Borislav Petkovfc8323f2008-10-13 21:39:48 +02001798 printk(KERN_CONT ", %dkB Cache\n",
1799 be16_to_cpup((__be16 *)&buf[8 + 12]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
1801 return nslots;
1802}
1803
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02001804/* standard prep_rq_fn that builds 10 byte cmds */
Jens Axboe165125e2007-07-24 09:28:11 +02001805static int ide_cdrom_prep_fs(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806{
1807 int hard_sect = queue_hardsect_size(q);
1808 long block = (long)rq->hard_sector / (hard_sect >> 9);
1809 unsigned long blocks = rq->hard_nr_sectors / (hard_sect >> 9);
1810
FUJITA Tomonorid34c87e2008-04-29 14:37:52 +02001811 memset(rq->cmd, 0, BLK_MAX_CDB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
1813 if (rq_data_dir(rq) == READ)
1814 rq->cmd[0] = GPCMD_READ_10;
1815 else
1816 rq->cmd[0] = GPCMD_WRITE_10;
1817
1818 /*
1819 * fill in lba
1820 */
1821 rq->cmd[2] = (block >> 24) & 0xff;
1822 rq->cmd[3] = (block >> 16) & 0xff;
1823 rq->cmd[4] = (block >> 8) & 0xff;
1824 rq->cmd[5] = block & 0xff;
1825
1826 /*
1827 * and transfer length
1828 */
1829 rq->cmd[7] = (blocks >> 8) & 0xff;
1830 rq->cmd[8] = blocks & 0xff;
1831 rq->cmd_len = 10;
1832 return BLKPREP_OK;
1833}
1834
1835/*
1836 * Most of the SCSI commands are supported directly by ATAPI devices.
1837 * This transform handles the few exceptions.
1838 */
1839static int ide_cdrom_prep_pc(struct request *rq)
1840{
1841 u8 *c = rq->cmd;
1842
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02001843 /* transform 6-byte read/write commands to the 10-byte version */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 if (c[0] == READ_6 || c[0] == WRITE_6) {
1845 c[8] = c[4];
1846 c[5] = c[3];
1847 c[4] = c[2];
1848 c[3] = c[1] & 0x1f;
1849 c[2] = 0;
1850 c[1] &= 0xe0;
1851 c[0] += (READ_10 - READ_6);
1852 rq->cmd_len = 10;
1853 return BLKPREP_OK;
1854 }
1855
1856 /*
1857 * it's silly to pretend we understand 6-byte sense commands, just
1858 * reject with ILLEGAL_REQUEST and the caller should take the
1859 * appropriate action
1860 */
1861 if (c[0] == MODE_SENSE || c[0] == MODE_SELECT) {
1862 rq->errors = ILLEGAL_REQUEST;
1863 return BLKPREP_KILL;
1864 }
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +02001865
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 return BLKPREP_OK;
1867}
1868
Jens Axboe165125e2007-07-24 09:28:11 +02001869static int ide_cdrom_prep_fn(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870{
Jens Axboe4aff5e22006-08-10 08:44:47 +02001871 if (blk_fs_request(rq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 return ide_cdrom_prep_fs(q, rq);
Jens Axboe4aff5e22006-08-10 08:44:47 +02001873 else if (blk_pc_request(rq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 return ide_cdrom_prep_pc(rq);
1875
1876 return 0;
1877}
1878
Bartlomiej Zolnierkiewicze59724c2008-02-01 23:09:22 +01001879struct cd_list_entry {
1880 const char *id_model;
1881 const char *id_firmware;
1882 unsigned int cd_flags;
1883};
1884
Borislav Petkov5e657a92008-04-26 22:25:15 +02001885#ifdef CONFIG_IDE_PROC_FS
1886static sector_t ide_cdrom_capacity(ide_drive_t *drive)
1887{
1888 unsigned long capacity, sectors_per_frame;
1889
1890 if (cdrom_read_capacity(drive, &capacity, &sectors_per_frame, NULL))
1891 return 0;
1892
1893 return capacity * sectors_per_frame;
1894}
1895
Borislav Petkove5e076a2008-04-26 22:25:15 +02001896static int proc_idecd_read_capacity(char *page, char **start, off_t off,
1897 int count, int *eof, void *data)
Borislav Petkov5e657a92008-04-26 22:25:15 +02001898{
1899 ide_drive_t *drive = data;
1900 int len;
1901
1902 len = sprintf(page, "%llu\n", (long long)ide_cdrom_capacity(drive));
1903 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
1904}
1905
1906static ide_proc_entry_t idecd_proc[] = {
1907 { "capacity", S_IFREG|S_IRUGO, proc_idecd_read_capacity, NULL },
1908 { NULL, 0, NULL, NULL }
1909};
1910
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001911ide_devset_rw_flag(dsc_overlap, IDE_DFLAG_DSC_OVERLAP);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001912
Elias Oltmanns92f1f8f2008-10-10 22:39:40 +02001913static const struct ide_proc_devset idecd_settings[] = {
1914 IDE_PROC_DEVSET(dsc_overlap, 0, 1),
1915 { 0 },
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +02001916};
Bartlomiej Zolnierkiewicz79cb3802008-10-17 18:09:13 +02001917
1918static ide_proc_entry_t *ide_cd_proc_entries(ide_drive_t *drive)
1919{
1920 return idecd_proc;
1921}
1922
1923static const struct ide_proc_devset *ide_cd_proc_devsets(ide_drive_t *drive)
1924{
1925 return idecd_settings;
1926}
Borislav Petkov5e657a92008-04-26 22:25:15 +02001927#endif
1928
Bartlomiej Zolnierkiewicze59724c2008-02-01 23:09:22 +01001929static const struct cd_list_entry ide_cd_quirks_list[] = {
1930 /* Limit transfer size per interrupt. */
Borislav Petkov570f89e2008-07-23 19:56:02 +02001931 { "SAMSUNG CD-ROM SCR-2430", NULL, IDE_AFLAG_LIMIT_NFRAMES },
1932 { "SAMSUNG CD-ROM SCR-2432", NULL, IDE_AFLAG_LIMIT_NFRAMES },
Bartlomiej Zolnierkiewicze59724c2008-02-01 23:09:22 +01001933 /* SCR-3231 doesn't support the SET_CD_SPEED command. */
Borislav Petkov570f89e2008-07-23 19:56:02 +02001934 { "SAMSUNG CD-ROM SCR-3231", NULL, IDE_AFLAG_NO_SPEED_SELECT },
Bartlomiej Zolnierkiewicze59724c2008-02-01 23:09:22 +01001935 /* Old NEC260 (not R) was released before ATAPI 1.2 spec. */
Borislav Petkov570f89e2008-07-23 19:56:02 +02001936 { "NEC CD-ROM DRIVE:260", "1.01", IDE_AFLAG_TOCADDR_AS_BCD |
1937 IDE_AFLAG_PRE_ATAPI12, },
Bartlomiej Zolnierkiewicze59724c2008-02-01 23:09:22 +01001938 /* Vertos 300, some versions of this drive like to talk BCD. */
Borislav Petkov570f89e2008-07-23 19:56:02 +02001939 { "V003S0DS", NULL, IDE_AFLAG_VERTOS_300_SSD, },
Bartlomiej Zolnierkiewicze59724c2008-02-01 23:09:22 +01001940 /* Vertos 600 ESD. */
Borislav Petkov570f89e2008-07-23 19:56:02 +02001941 { "V006E0DS", NULL, IDE_AFLAG_VERTOS_600_ESD, },
Bartlomiej Zolnierkiewicze59724c2008-02-01 23:09:22 +01001942 /*
1943 * Sanyo 3 CD changer uses a non-standard command for CD changing
1944 * (by default standard ATAPI support for CD changers is used).
1945 */
Borislav Petkov570f89e2008-07-23 19:56:02 +02001946 { "CD-ROM CDR-C3 G", NULL, IDE_AFLAG_SANYO_3CD },
1947 { "CD-ROM CDR-C3G", NULL, IDE_AFLAG_SANYO_3CD },
1948 { "CD-ROM CDR_C36", NULL, IDE_AFLAG_SANYO_3CD },
Bartlomiej Zolnierkiewicze59724c2008-02-01 23:09:22 +01001949 /* Stingray 8X CD-ROM. */
Borislav Petkov570f89e2008-07-23 19:56:02 +02001950 { "STINGRAY 8422 IDE 8X CD-ROM 7-27-95", NULL, IDE_AFLAG_PRE_ATAPI12 },
Bartlomiej Zolnierkiewicze59724c2008-02-01 23:09:22 +01001951 /*
1952 * ACER 50X CD-ROM and WPI 32X CD-ROM require the full spec length
1953 * mode sense page capabilities size, but older drives break.
1954 */
Borislav Petkov570f89e2008-07-23 19:56:02 +02001955 { "ATAPI CD ROM DRIVE 50X MAX", NULL, IDE_AFLAG_FULL_CAPS_PAGE },
1956 { "WPI CDS-32X", NULL, IDE_AFLAG_FULL_CAPS_PAGE },
Bartlomiej Zolnierkiewicze59724c2008-02-01 23:09:22 +01001957 /* ACER/AOpen 24X CD-ROM has the speed fields byte-swapped. */
Borislav Petkov570f89e2008-07-23 19:56:02 +02001958 { "", "241N", IDE_AFLAG_LE_SPEED_FIELDS },
Bartlomiej Zolnierkiewicze59724c2008-02-01 23:09:22 +01001959 /*
1960 * Some drives used by Apple don't advertise audio play
1961 * but they do support reading TOC & audio datas.
1962 */
Borislav Petkov570f89e2008-07-23 19:56:02 +02001963 { "MATSHITADVD-ROM SR-8187", NULL, IDE_AFLAG_PLAY_AUDIO_OK },
1964 { "MATSHITADVD-ROM SR-8186", NULL, IDE_AFLAG_PLAY_AUDIO_OK },
1965 { "MATSHITADVD-ROM SR-8176", NULL, IDE_AFLAG_PLAY_AUDIO_OK },
1966 { "MATSHITADVD-ROM SR-8174", NULL, IDE_AFLAG_PLAY_AUDIO_OK },
1967 { "Optiarc DVD RW AD-5200A", NULL, IDE_AFLAG_PLAY_AUDIO_OK },
Bodo Eggertf3e85ee2008-10-05 18:23:28 +02001968 { "Optiarc DVD RW AD-7200A", NULL, IDE_AFLAG_PLAY_AUDIO_OK },
Borislav Petkovf20f2582008-10-05 18:23:27 +02001969 { "Optiarc DVD RW AD-7543A", NULL, IDE_AFLAG_NO_AUTOCLOSE },
Márton Németh64c2eae2008-10-23 23:22:09 +02001970 { "TEAC CD-ROM CD-224E", NULL, IDE_AFLAG_NO_AUTOCLOSE },
Bartlomiej Zolnierkiewicze59724c2008-02-01 23:09:22 +01001971 { NULL, NULL, 0 }
1972};
1973
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +02001974static unsigned int ide_cd_flags(u16 *id)
Bartlomiej Zolnierkiewicze59724c2008-02-01 23:09:22 +01001975{
1976 const struct cd_list_entry *cle = ide_cd_quirks_list;
1977
1978 while (cle->id_model) {
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +02001979 if (strcmp(cle->id_model, (char *)&id[ATA_ID_PROD]) == 0 &&
Bartlomiej Zolnierkiewicze59724c2008-02-01 23:09:22 +01001980 (cle->id_firmware == NULL ||
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +02001981 strstr((char *)&id[ATA_ID_FW_REV], cle->id_firmware)))
Bartlomiej Zolnierkiewicze59724c2008-02-01 23:09:22 +01001982 return cle->cd_flags;
1983 cle++;
1984 }
1985
1986 return 0;
1987}
1988
Borislav Petkove5e076a2008-04-26 22:25:15 +02001989static int ide_cdrom_setup(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990{
Bartlomiej Zolnierkiewicz4fe67172008-02-01 23:09:21 +01001991 struct cdrom_info *cd = drive->driver_data;
1992 struct cdrom_device_info *cdi = &cd->devinfo;
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +02001993 u16 *id = drive->id;
1994 char *fw_rev = (char *)&id[ATA_ID_FW_REV];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 int nslots;
1996
Borislav Petkovfc8323f2008-10-13 21:39:48 +02001997 ide_debug_log(IDE_DBG_PROBE, "Call %s\n", __func__);
1998
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 blk_queue_prep_rq(drive->queue, ide_cdrom_prep_fn);
2000 blk_queue_dma_alignment(drive->queue, 31);
FUJITA Tomonorie5318b52008-07-16 20:33:35 +02002001 blk_queue_update_dma_pad(drive->queue, 15);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 drive->queue->unplug_delay = (1 * HZ) / 1000;
2003 if (!drive->queue->unplug_delay)
2004 drive->queue->unplug_delay = 1;
2005
Bartlomiej Zolnierkiewiczfe11edf2008-10-17 18:09:11 +02002006 drive->dev_flags |= IDE_DFLAG_MEDIA_CHANGED;
2007 drive->atapi_flags = IDE_AFLAG_NO_EJECT | ide_cd_flags(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008
Borislav Petkov570f89e2008-07-23 19:56:02 +02002009 if ((drive->atapi_flags & IDE_AFLAG_VERTOS_300_SSD) &&
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +02002010 fw_rev[4] == '1' && fw_rev[6] <= '2')
Borislav Petkov570f89e2008-07-23 19:56:02 +02002011 drive->atapi_flags |= (IDE_AFLAG_TOCTRACKS_AS_BCD |
2012 IDE_AFLAG_TOCADDR_AS_BCD);
2013 else if ((drive->atapi_flags & IDE_AFLAG_VERTOS_600_ESD) &&
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +02002014 fw_rev[4] == '1' && fw_rev[6] <= '2')
Borislav Petkov570f89e2008-07-23 19:56:02 +02002015 drive->atapi_flags |= IDE_AFLAG_TOCTRACKS_AS_BCD;
2016 else if (drive->atapi_flags & IDE_AFLAG_SANYO_3CD)
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02002017 /* 3 => use CD in slot 0 */
2018 cdi->sanyo_slot = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +02002020 nslots = ide_cdrom_probe_capabilities(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02002022 /* set correct block size */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 blk_queue_hardsect_size(drive->queue, CD_FRAMESIZE);
2024
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02002025 if (drive->next != drive)
2026 drive->dev_flags |= IDE_DFLAG_DSC_OVERLAP;
2027 else
2028 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
2030 if (ide_cdrom_register(drive, nslots)) {
Borislav Petkovfc8323f2008-10-13 21:39:48 +02002031 printk(KERN_ERR PFX "%s: %s failed to register device with the"
Borislav Petkov83c85652008-04-26 22:25:15 +02002032 " cdrom driver.\n", drive->name, __func__);
Bartlomiej Zolnierkiewicz4fe67172008-02-01 23:09:21 +01002033 cd->devinfo.handle = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 return 1;
2035 }
Bartlomiej Zolnierkiewicz1e874f42008-10-10 22:39:27 +02002036
2037 ide_proc_register_driver(drive, cd->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 return 0;
2039}
2040
Russell King4031bbe2006-01-06 11:41:00 +00002041static void ide_cd_remove(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042{
2043 struct cdrom_info *info = drive->driver_data;
2044
Borislav Petkovfc8323f2008-10-13 21:39:48 +02002045 ide_debug_log(IDE_DBG_FUNC, "Call %s\n", __func__);
2046
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002047 ide_proc_unregister_driver(drive, info->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048
2049 del_gendisk(info->disk);
2050
2051 ide_cd_put(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052}
2053
2054static void ide_cd_release(struct kref *kref)
2055{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02002056 struct cdrom_info *info = to_ide_drv(kref, cdrom_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 struct cdrom_device_info *devinfo = &info->devinfo;
2058 ide_drive_t *drive = info->drive;
2059 struct gendisk *g = info->disk;
2060
Borislav Petkovfc8323f2008-10-13 21:39:48 +02002061 ide_debug_log(IDE_DBG_FUNC, "Call %s\n", __func__);
2062
Jesper Juhl6044ec82005-11-07 01:01:32 -08002063 kfree(info->toc);
Akinobu Mita0a0c4112008-03-26 12:09:02 +01002064 if (devinfo->handle == drive)
2065 unregister_cdrom(devinfo);
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02002066 drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 drive->driver_data = NULL;
2068 blk_queue_prep_rq(drive->queue, NULL);
2069 g->private_data = NULL;
2070 put_disk(g);
2071 kfree(info);
2072}
2073
Russell King4031bbe2006-01-06 11:41:00 +00002074static int ide_cd_probe(ide_drive_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076static ide_driver_t ide_cdrom_driver = {
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002077 .gen_driver = {
Laurent Riffard4ef3b8f2005-11-18 22:15:40 +01002078 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002079 .name = "ide-cdrom",
2080 .bus = &ide_bus_type,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002081 },
Russell King4031bbe2006-01-06 11:41:00 +00002082 .probe = ide_cd_probe,
2083 .remove = ide_cd_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 .version = IDECD_VERSION,
Borislav Petkov99384ae2008-07-16 20:33:45 +02002085 .do_request = ide_cd_do_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 .end_request = ide_end_request,
2087 .error = __ide_error,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002088#ifdef CONFIG_IDE_PROC_FS
Bartlomiej Zolnierkiewicz79cb3802008-10-17 18:09:13 +02002089 .proc_entries = ide_cd_proc_entries,
2090 .proc_devsets = ide_cd_proc_devsets,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02002091#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092};
2093
Al Viro488ca602008-03-02 10:26:23 -05002094static int idecd_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095{
Al Viro488ca602008-03-02 10:26:23 -05002096 struct cdrom_info *info = ide_cd_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 int rc = -ENOMEM;
2098
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +02002099 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 return -ENXIO;
2101
Al Viro488ca602008-03-02 10:26:23 -05002102 rc = cdrom_open(&info->devinfo, bdev, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
2104 if (rc < 0)
2105 ide_cd_put(info);
2106
2107 return rc;
2108}
2109
Al Viro488ca602008-03-02 10:26:23 -05002110static int idecd_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02002112 struct cdrom_info *info = ide_drv_g(disk, cdrom_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113
Al Viro488ca602008-03-02 10:26:23 -05002114 cdrom_release(&info->devinfo, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115
2116 ide_cd_put(info);
2117
2118 return 0;
2119}
2120
Christoph Hellwig6a2900b2006-03-23 03:00:15 -08002121static int idecd_set_spindown(struct cdrom_device_info *cdi, unsigned long arg)
2122{
2123 struct packet_command cgc;
2124 char buffer[16];
2125 int stat;
2126 char spindown;
2127
2128 if (copy_from_user(&spindown, (void __user *)arg, sizeof(char)))
2129 return -EFAULT;
2130
2131 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_UNKNOWN);
2132
2133 stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CDROM_PAGE, 0);
2134 if (stat)
2135 return stat;
2136
2137 buffer[11] = (buffer[11] & 0xf0) | (spindown & 0x0f);
2138 return cdrom_mode_select(cdi, &cgc);
2139}
2140
2141static int idecd_get_spindown(struct cdrom_device_info *cdi, unsigned long arg)
2142{
2143 struct packet_command cgc;
2144 char buffer[16];
2145 int stat;
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +02002146 char spindown;
Christoph Hellwig6a2900b2006-03-23 03:00:15 -08002147
2148 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_UNKNOWN);
2149
2150 stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CDROM_PAGE, 0);
2151 if (stat)
2152 return stat;
2153
2154 spindown = buffer[11] & 0x0f;
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +02002155 if (copy_to_user((void __user *)arg, &spindown, sizeof(char)))
Christoph Hellwig6a2900b2006-03-23 03:00:15 -08002156 return -EFAULT;
2157 return 0;
2158}
2159
Al Viro488ca602008-03-02 10:26:23 -05002160static int idecd_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 unsigned int cmd, unsigned long arg)
2162{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02002163 struct cdrom_info *info = ide_drv_g(bdev->bd_disk, cdrom_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 int err;
2165
Christoph Hellwig6a2900b2006-03-23 03:00:15 -08002166 switch (cmd) {
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +02002167 case CDROMSETSPINDOWN:
Christoph Hellwig6a2900b2006-03-23 03:00:15 -08002168 return idecd_set_spindown(&info->devinfo, arg);
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +02002169 case CDROMGETSPINDOWN:
Christoph Hellwig6a2900b2006-03-23 03:00:15 -08002170 return idecd_get_spindown(&info->devinfo, arg);
2171 default:
2172 break;
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +02002173 }
Christoph Hellwig6a2900b2006-03-23 03:00:15 -08002174
Al Viro1bddd9e2008-09-02 17:19:43 -04002175 err = generic_ide_ioctl(info->drive, bdev, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 if (err == -EINVAL)
Al Viro488ca602008-03-02 10:26:23 -05002177 err = cdrom_ioctl(&info->devinfo, bdev, mode, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
2179 return err;
2180}
2181
2182static int idecd_media_changed(struct gendisk *disk)
2183{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02002184 struct cdrom_info *info = ide_drv_g(disk, cdrom_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 return cdrom_media_changed(&info->devinfo);
2186}
2187
2188static int idecd_revalidate_disk(struct gendisk *disk)
2189{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02002190 struct cdrom_info *info = ide_drv_g(disk, cdrom_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 struct request_sense sense;
Bartlomiej Zolnierkiewicz139c8292008-02-01 23:09:24 +01002192
2193 ide_cd_read_toc(info->drive, &sense);
2194
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 return 0;
2196}
2197
2198static struct block_device_operations idecd_ops = {
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +02002199 .owner = THIS_MODULE,
Al Viro488ca602008-03-02 10:26:23 -05002200 .open = idecd_open,
2201 .release = idecd_release,
2202 .locked_ioctl = idecd_ioctl,
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +02002203 .media_changed = idecd_media_changed,
2204 .revalidate_disk = idecd_revalidate_disk
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205};
2206
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02002207/* module options */
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +02002208static char *ignore;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209module_param(ignore, charp, 0400);
Borislav Petkov35d9b172008-10-13 21:39:49 +02002210
2211static unsigned long debug_mask;
2212module_param(debug_mask, ulong, 0644);
2213
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214MODULE_DESCRIPTION("ATAPI CD-ROM Driver");
2215
Russell King4031bbe2006-01-06 11:41:00 +00002216static int ide_cd_probe(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217{
2218 struct cdrom_info *info;
2219 struct gendisk *g;
2220 struct request_sense sense;
2221
Borislav Petkovfc8323f2008-10-13 21:39:48 +02002222 ide_debug_log(IDE_DBG_PROBE, "Call %s, drive->driver_req: %s, "
2223 "drive->media: 0x%x\n", __func__, drive->driver_req,
2224 drive->media);
2225
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 if (!strstr("ide-cdrom", drive->driver_req))
2227 goto failed;
Bartlomiej Zolnierkiewicz2a924662008-10-10 22:39:24 +02002228
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 if (drive->media != ide_cdrom && drive->media != ide_optical)
2230 goto failed;
Bartlomiej Zolnierkiewicz2a924662008-10-10 22:39:24 +02002231
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 /* skip drives that we were told to ignore */
2233 if (ignore != NULL) {
2234 if (strstr(ignore, drive->name)) {
Borislav Petkovfc8323f2008-10-13 21:39:48 +02002235 printk(KERN_INFO PFX "ignoring drive %s\n",
Borislav Petkov83c85652008-04-26 22:25:15 +02002236 drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 goto failed;
2238 }
2239 }
Borislav Petkov35d9b172008-10-13 21:39:49 +02002240
2241 drive->debug_mask = debug_mask;
2242
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -08002243 info = kzalloc(sizeof(struct cdrom_info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 if (info == NULL) {
Borislav Petkovfc8323f2008-10-13 21:39:48 +02002245 printk(KERN_ERR PFX "%s: Can't allocate a cdrom structure\n",
Borislav Petkov83c85652008-04-26 22:25:15 +02002246 drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 goto failed;
2248 }
2249
2250 g = alloc_disk(1 << PARTN_BITS);
2251 if (!g)
2252 goto out_free_cd;
2253
2254 ide_init_disk(g, drive);
2255
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 kref_init(&info->kref);
2257
2258 info->drive = drive;
2259 info->driver = &ide_cdrom_driver;
2260 info->disk = g;
2261
2262 g->private_data = &info->driver;
2263
2264 drive->driver_data = info;
2265
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 g->minors = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 g->driverfs_dev = &drive->gendev;
2268 g->flags = GENHD_FL_CD | GENHD_FL_REMOVABLE;
2269 if (ide_cdrom_setup(drive)) {
Bartlomiej Zolnierkiewicz05017db2007-12-24 15:23:43 +01002270 ide_cd_release(&info->kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 goto failed;
2272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273
Bartlomiej Zolnierkiewicz139c8292008-02-01 23:09:24 +01002274 ide_cd_read_toc(drive, &sense);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 g->fops = &idecd_ops;
2276 g->flags |= GENHD_FL_REMOVABLE;
2277 add_disk(g);
2278 return 0;
2279
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280out_free_cd:
2281 kfree(info);
2282failed:
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002283 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284}
2285
2286static void __exit ide_cdrom_exit(void)
2287{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002288 driver_unregister(&ide_cdrom_driver.gen_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289}
Bartlomiej Zolnierkiewicz17514e82005-11-19 22:24:35 +01002290
2291static int __init ide_cdrom_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292{
Borislav Petkovfc8323f2008-10-13 21:39:48 +02002293 printk(KERN_INFO DRV_NAME " driver " IDECD_VERSION "\n");
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002294 return driver_register(&ide_cdrom_driver.gen_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295}
2296
Kay Sievers263756e2005-12-12 18:03:44 +01002297MODULE_ALIAS("ide:*m-cdrom*");
Bartlomiej Zolnierkiewicz972560f2008-02-01 23:09:23 +01002298MODULE_ALIAS("ide-cd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299module_init(ide_cdrom_init);
2300module_exit(ide_cdrom_exit);
2301MODULE_LICENSE("GPL");