blob: 1a7410f882494907b3568d0263c917a0af73d2fa [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
Arjan van de Vencf8b8972006-03-23 03:00:45 -080056static DEFINE_MUTEX(idecd_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +020058static void ide_cd_release(struct kref *);
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060static struct cdrom_info *ide_cd_get(struct gendisk *disk)
61{
62 struct cdrom_info *cd = NULL;
63
Arjan van de Vencf8b8972006-03-23 03:00:45 -080064 mutex_lock(&idecd_ref_mutex);
Borislav Petkov5aeddf92008-10-13 21:39:34 +020065 cd = ide_drv_g(disk, cdrom_info);
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +020066 if (cd) {
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +020067 if (ide_device_get(cd->drive))
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +020068 cd = NULL;
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +020069 else
70 kref_get(&cd->kref);
71
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +020072 }
Arjan van de Vencf8b8972006-03-23 03:00:45 -080073 mutex_unlock(&idecd_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 return cd;
75}
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static void ide_cd_put(struct cdrom_info *cd)
78{
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +020079 ide_drive_t *drive = cd->drive;
80
Arjan van de Vencf8b8972006-03-23 03:00:45 -080081 mutex_lock(&idecd_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 kref_put(&cd->kref, ide_cd_release);
Bartlomiej Zolnierkiewiczd3e33ff2008-08-05 18:16:59 +020083 ide_device_put(drive);
Arjan van de Vencf8b8972006-03-23 03:00:45 -080084 mutex_unlock(&idecd_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +020087/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 * Generic packet command support and error handling routines.
89 */
90
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +020091/* Mark that we've seen a media change and invalidate our internal buffers. */
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +020092static void cdrom_saw_media_change(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
Bartlomiej Zolnierkiewiczfe11edf2008-10-17 18:09:11 +020094 drive->dev_flags |= IDE_DFLAG_MEDIA_CHANGED;
Borislav Petkov570f89e2008-07-23 19:56:02 +020095 drive->atapi_flags &= ~IDE_AFLAG_TOC_VALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
97
98static int cdrom_log_sense(ide_drive_t *drive, struct request *rq,
99 struct request_sense *sense)
100{
101 int log = 0;
102
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200103 ide_debug_log(IDE_DBG_SENSE, "Call %s, sense_key: 0x%x\n", __func__,
104 sense->sense_key);
105
Jens Axboe4aff5e22006-08-10 08:44:47 +0200106 if (!sense || !rq || (rq->cmd_flags & REQ_QUIET))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 return 0;
108
109 switch (sense->sense_key) {
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +0200110 case NO_SENSE:
111 case RECOVERED_ERROR:
112 break;
113 case NOT_READY:
114 /*
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200115 * don't care about tray state messages for e.g. capacity
116 * commands or in-progress or becoming ready
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +0200117 */
118 if (sense->asc == 0x3a || sense->asc == 0x04)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 break;
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +0200120 log = 1;
121 break;
122 case ILLEGAL_REQUEST:
123 /*
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200124 * don't log START_STOP unit with LoEj set, since we cannot
125 * reliably check if drive can auto-close
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +0200126 */
127 if (rq->cmd[0] == GPCMD_START_STOP_UNIT && sense->asc == 0x24)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 break;
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +0200129 log = 1;
130 break;
131 case UNIT_ATTENTION:
132 /*
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200133 * Make good and sure we've seen this potential media change.
134 * Some drives (i.e. Creative) fail to present the correct sense
135 * key in the error register.
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +0200136 */
137 cdrom_saw_media_change(drive);
138 break;
139 default:
140 log = 1;
141 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 }
143 return log;
144}
145
Borislav Petkove5e076a2008-04-26 22:25:15 +0200146static void cdrom_analyze_sense_data(ide_drive_t *drive,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 struct request *failed_command,
148 struct request_sense *sense)
149{
Alan Coxdbe217a2006-06-25 05:47:44 -0700150 unsigned long sector;
151 unsigned long bio_sectors;
Alan Coxdbe217a2006-06-25 05:47:44 -0700152 struct cdrom_info *info = drive->driver_data;
153
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200154 ide_debug_log(IDE_DBG_SENSE, "Call %s, error_code: 0x%x, "
155 "sense_key: 0x%x\n", __func__, sense->error_code,
156 sense->sense_key);
157
158 if (failed_command)
159 ide_debug_log(IDE_DBG_SENSE, "%s: failed cmd: 0x%x\n",
160 __func__, failed_command->cmd[0]);
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 if (!cdrom_log_sense(drive, failed_command, sense))
163 return;
164
165 /*
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200166 * If a read toc is executed for a CD-R or CD-RW medium where the first
167 * toc has not been recorded yet, it will fail with 05/24/00 (which is a
168 * confusing error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 */
170 if (failed_command && failed_command->cmd[0] == GPCMD_READ_TOC_PMA_ATIP)
171 if (sense->sense_key == 0x05 && sense->asc == 0x24)
172 return;
173
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200174 /* current error */
175 if (sense->error_code == 0x70) {
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +0200176 switch (sense->sense_key) {
Alan Coxdbe217a2006-06-25 05:47:44 -0700177 case MEDIUM_ERROR:
178 case VOLUME_OVERFLOW:
179 case ILLEGAL_REQUEST:
180 if (!sense->valid)
181 break;
182 if (failed_command == NULL ||
183 !blk_fs_request(failed_command))
184 break;
185 sector = (sense->information[0] << 24) |
186 (sense->information[1] << 16) |
187 (sense->information[2] << 8) |
188 (sense->information[3]);
189
Alan Coxdbe217a2006-06-25 05:47:44 -0700190 if (drive->queue->hardsect_size == 2048)
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200191 /* device sector size is 2K */
192 sector <<= 2;
Roel Kluineee49292008-04-28 23:44:43 +0200193
194 bio_sectors = max(bio_sectors(failed_command->bio), 4U);
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +0200195 sector &= ~(bio_sectors - 1);
Alan Coxdbe217a2006-06-25 05:47:44 -0700196
Alan Coxdbe217a2006-06-25 05:47:44 -0700197 if (sector < get_capacity(info->disk) &&
Borislav Petkove5e076a2008-04-26 22:25:15 +0200198 drive->probed_capacity - sector < 4 * 75)
Alan Coxdbe217a2006-06-25 05:47:44 -0700199 set_capacity(info->disk, sector);
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +0200200 }
201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Bartlomiej Zolnierkiewicz972560f2008-02-01 23:09:23 +0100203 ide_cd_log_error(drive->name, failed_command, sense);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206static void cdrom_queue_request_sense(ide_drive_t *drive, void *sense,
207 struct request *failed_command)
208{
209 struct cdrom_info *info = drive->driver_data;
210 struct request *rq = &info->request_sense_request;
211
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200212 ide_debug_log(IDE_DBG_SENSE, "Call %s\n", __func__);
213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 if (sense == NULL)
215 sense = &info->sense_data;
216
217 /* stuff the sense request in front of our current request */
FUJITA Tomonoried820f12008-07-15 21:21:45 +0200218 blk_rq_init(NULL, rq);
219 rq->cmd_type = REQ_TYPE_ATA_PC;
220 rq->rq_disk = info->disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 rq->data = sense;
223 rq->cmd[0] = GPCMD_REQUEST_SENSE;
Borislav Petkove5e076a2008-04-26 22:25:15 +0200224 rq->cmd[4] = 18;
225 rq->data_len = 18;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Jens Axboe4aff5e22006-08-10 08:44:47 +0200227 rq->cmd_type = REQ_TYPE_SENSE;
Bartlomiej Zolnierkiewicze8a96aa2008-07-15 21:21:41 +0200228 rq->cmd_flags |= REQ_PREEMPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 /* NOTE! Save the failed command in "rq->buffer" */
231 rq->buffer = (void *) failed_command;
232
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200233 if (failed_command)
234 ide_debug_log(IDE_DBG_SENSE, "failed_cmd: 0x%x\n",
235 failed_command->cmd[0]);
236
FUJITA Tomonori63f5abb2008-07-15 21:21:51 +0200237 ide_do_drive_cmd(drive, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
239
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +0200240static void cdrom_end_request(ide_drive_t *drive, int uptodate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
242 struct request *rq = HWGROUP(drive)->rq;
243 int nsectors = rq->hard_cur_sectors;
244
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200245 ide_debug_log(IDE_DBG_FUNC, "Call %s, cmd: 0x%x, uptodate: 0x%x, "
246 "nsectors: %d\n", __func__, rq->cmd[0], uptodate,
247 nsectors);
248
Jens Axboe4aff5e22006-08-10 08:44:47 +0200249 if (blk_sense_request(rq) && uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 /*
Jens Axboe4aff5e22006-08-10 08:44:47 +0200251 * For REQ_TYPE_SENSE, "rq->buffer" points to the original
252 * failed request
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 */
254 struct request *failed = (struct request *) rq->buffer;
255 struct cdrom_info *info = drive->driver_data;
256 void *sense = &info->sense_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 if (failed) {
259 if (failed->sense) {
260 sense = failed->sense;
261 failed->sense_len = rq->sense_len;
262 }
Alan Coxdbe217a2006-06-25 05:47:44 -0700263 cdrom_analyze_sense_data(drive, failed, sense);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 /*
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200265 * now end the failed request
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 */
Alan Coxdbe217a2006-06-25 05:47:44 -0700267 if (blk_fs_request(failed)) {
268 if (ide_end_dequeued_request(drive, failed, 0,
269 failed->hard_nr_sectors))
270 BUG();
271 } else {
Bartlomiej Zolnierkiewicz3c8a2cc2008-12-29 20:27:31 +0100272 if (blk_end_request(failed, -EIO,
273 failed->data_len))
Kiyoshi Ueda5e36bb62008-01-28 10:34:20 +0100274 BUG();
Alan Coxdbe217a2006-06-25 05:47:44 -0700275 }
276 } else
277 cdrom_analyze_sense_data(drive, NULL, sense);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 }
279
280 if (!rq->current_nr_sectors && blk_fs_request(rq))
281 uptodate = 1;
282 /* make sure it's fully ended */
283 if (blk_pc_request(rq))
284 nsectors = (rq->data_len + 511) >> 9;
285 if (!nsectors)
286 nsectors = 1;
287
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200288 ide_debug_log(IDE_DBG_FUNC, "Exit %s, uptodate: 0x%x, nsectors: %d\n",
289 __func__, uptodate, nsectors);
290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 ide_end_request(drive, uptodate, nsectors);
292}
293
Borislav Petkov83c85652008-04-26 22:25:15 +0200294static void ide_dump_status_no_sense(ide_drive_t *drive, const char *msg, u8 st)
Alan Coxdbe217a2006-06-25 05:47:44 -0700295{
Borislav Petkov83c85652008-04-26 22:25:15 +0200296 if (st & 0x80)
Alan Coxdbe217a2006-06-25 05:47:44 -0700297 return;
Borislav Petkov83c85652008-04-26 22:25:15 +0200298 ide_dump_status(drive, msg, st);
Alan Coxdbe217a2006-06-25 05:47:44 -0700299}
300
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200301/*
302 * Returns:
303 * 0: if the request should be continued.
304 * 1: if the request was ended.
305 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306static int cdrom_decode_status(ide_drive_t *drive, int good_stat, int *stat_ret)
307{
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200308 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewicz1d0bf582008-12-29 20:27:30 +0100309 ide_hwgroup_t *hwgroup = hwif->hwgroup;
310 struct request *rq = hwgroup->rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 int stat, err, sense_key;
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +0200312
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200313 /* check for errors */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200314 stat = hwif->tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 if (stat_ret)
317 *stat_ret = stat;
318
319 if (OK_STAT(stat, good_stat, BAD_R_STAT))
320 return 0;
321
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200322 /* get the IDE error register */
Bartlomiej Zolnierkiewicz64a57fe2008-02-06 02:57:51 +0100323 err = ide_read_error(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 sense_key = err >> 4;
325
326 if (rq == NULL) {
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200327 printk(KERN_ERR PFX "%s: missing rq in %s\n",
Borislav Petkove5e076a2008-04-26 22:25:15 +0200328 drive->name, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 return 1;
330 }
331
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200332 ide_debug_log(IDE_DBG_RQ, "%s: stat: 0x%x, good_stat: 0x%x, "
Borislav Petkov71b429ca2008-10-17 18:09:14 +0200333 "rq->cmd[0]: 0x%x, rq->cmd_type: 0x%x, err: 0x%x\n",
334 __func__, stat, good_stat, rq->cmd[0], rq->cmd_type, err);
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200335
Jens Axboe4aff5e22006-08-10 08:44:47 +0200336 if (blk_sense_request(rq)) {
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200337 /*
338 * We got an error trying to get sense info from the drive
339 * (probably while trying to recover from a former error).
340 * Just give up.
341 */
Jens Axboe4aff5e22006-08-10 08:44:47 +0200342 rq->cmd_flags |= REQ_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 cdrom_end_request(drive, 0);
344 ide_error(drive, "request sense failure", stat);
345 return 1;
346
Jens Axboe8770c012006-10-12 17:24:52 +0200347 } else if (blk_pc_request(rq) || rq->cmd_type == REQ_TYPE_ATA_PC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 /* All other functions, except for READ. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350 /*
351 * if we have an error, pass back CHECK_CONDITION as the
352 * scsi status byte
353 */
Jens Axboeb7156732006-11-13 18:05:02 +0100354 if (blk_pc_request(rq) && !rq->errors)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 rq->errors = SAM_STAT_CHECK_CONDITION;
356
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200357 /* check for tray open */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 if (sense_key == NOT_READY) {
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +0200359 cdrom_saw_media_change(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 } else if (sense_key == UNIT_ATTENTION) {
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200361 /* check for media change */
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +0200362 cdrom_saw_media_change(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 return 0;
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +0200364 } else if (sense_key == ILLEGAL_REQUEST &&
365 rq->cmd[0] == GPCMD_START_STOP_UNIT) {
366 /*
367 * Don't print error message for this condition--
368 * SFF8090i indicates that 5/24/00 is the correct
369 * response to a request to close the tray if the
370 * drive doesn't have that capability.
371 * cdrom_log_sense() knows this!
372 */
Jens Axboe4aff5e22006-08-10 08:44:47 +0200373 } else if (!(rq->cmd_flags & REQ_QUIET)) {
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200374 /* otherwise, print an error */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 ide_dump_status(drive, "packet command error", stat);
376 }
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +0200377
Jens Axboe4aff5e22006-08-10 08:44:47 +0200378 rq->cmd_flags |= REQ_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380 /*
381 * instead of playing games with moving completions around,
382 * remove failed request completely and end it when the
383 * request sense has completed
384 */
Bartlomiej Zolnierkiewiczbbb89e32008-02-01 23:09:28 +0100385 goto end_request;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 } else if (blk_fs_request(rq)) {
388 int do_end_request = 0;
389
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200390 /* handle errors from READ and WRITE requests */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392 if (blk_noretry_request(rq))
393 do_end_request = 1;
394
395 if (sense_key == NOT_READY) {
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200396 /* tray open */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 if (rq_data_dir(rq) == READ) {
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +0200398 cdrom_saw_media_change(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200400 /* fail the request */
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200401 printk(KERN_ERR PFX "%s: tray open\n",
402 drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 do_end_request = 1;
404 } else {
405 struct cdrom_info *info = drive->driver_data;
406
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200407 /*
408 * Allow the drive 5 seconds to recover, some
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 * devices will return this error while flushing
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200410 * data from cache.
411 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 if (!rq->errors)
Borislav Petkov83c85652008-04-26 22:25:15 +0200413 info->write_timeout = jiffies +
414 ATAPI_WAIT_WRITE_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 rq->errors = 1;
416 if (time_after(jiffies, info->write_timeout))
417 do_end_request = 1;
418 else {
Bartlomiej Zolnierkiewicz6ea52222008-12-29 20:27:31 +0100419 struct request_queue *q = drive->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 unsigned long flags;
421
422 /*
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200423 * take a breather relying on the unplug
424 * timer to kick us again
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 */
Bartlomiej Zolnierkiewicz6ea52222008-12-29 20:27:31 +0100426 spin_lock_irqsave(q->queue_lock, flags);
427 blk_plug_device(q);
428 spin_unlock_irqrestore(q->queue_lock, flags);
429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 return 1;
431 }
432 }
433 } else if (sense_key == UNIT_ATTENTION) {
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200434 /* media change */
Borislav Petkove5e076a2008-04-26 22:25:15 +0200435 cdrom_saw_media_change(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +0200437 /*
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200438 * Arrange to retry the request but be sure to give up
439 * if we've retried too many times.
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +0200440 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 if (++rq->errors > ERROR_MAX)
442 do_end_request = 1;
443 } else if (sense_key == ILLEGAL_REQUEST ||
444 sense_key == DATA_PROTECT) {
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +0200445 /*
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200446 * No point in retrying after an illegal request or data
447 * protect error.
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +0200448 */
449 ide_dump_status_no_sense(drive, "command error", stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 do_end_request = 1;
451 } else if (sense_key == MEDIUM_ERROR) {
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +0200452 /*
453 * No point in re-trying a zillion times on a bad
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200454 * sector. If we got here the error is not correctable.
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +0200455 */
Borislav Petkov83c85652008-04-26 22:25:15 +0200456 ide_dump_status_no_sense(drive,
457 "media error (bad sector)",
458 stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 do_end_request = 1;
460 } else if (sense_key == BLANK_CHECK) {
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200461 /* disk appears blank ?? */
Borislav Petkov83c85652008-04-26 22:25:15 +0200462 ide_dump_status_no_sense(drive, "media error (blank)",
463 stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 do_end_request = 1;
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200465 } else if ((err & ~ATA_ABORTED) != 0) {
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200466 /* go to the default handler for other errors */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 ide_error(drive, "cdrom_decode_status", stat);
468 return 1;
469 } else if ((++rq->errors > ERROR_MAX)) {
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200470 /* we've racked up too many retries, abort */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 do_end_request = 1;
472 }
473
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200474 /*
475 * End a request through request sense analysis when we have
476 * sense data. We need this in order to perform end of media
477 * processing.
478 */
Bartlomiej Zolnierkiewiczbbb89e32008-02-01 23:09:28 +0100479 if (do_end_request)
480 goto end_request;
Alan Coxdbe217a2006-06-25 05:47:44 -0700481
Bartlomiej Zolnierkiewiczbbb89e32008-02-01 23:09:28 +0100482 /*
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200483 * If we got a CHECK_CONDITION status, queue
484 * a request sense command.
Bartlomiej Zolnierkiewiczbbb89e32008-02-01 23:09:28 +0100485 */
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200486 if (stat & ATA_ERR)
Bartlomiej Zolnierkiewiczbbb89e32008-02-01 23:09:28 +0100487 cdrom_queue_request_sense(drive, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 } else {
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200489 blk_dump_rq_flags(rq, PFX "bad rq");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 cdrom_end_request(drive, 0);
491 }
492
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200493 /* retry, or handle the next request */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 return 1;
Bartlomiej Zolnierkiewiczbbb89e32008-02-01 23:09:28 +0100495
496end_request:
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200497 if (stat & ATA_ERR) {
Bartlomiej Zolnierkiewicz6ea52222008-12-29 20:27:31 +0100498 struct request_queue *q = drive->queue;
Bartlomiej Zolnierkiewiczbbb89e32008-02-01 23:09:28 +0100499 unsigned long flags;
500
Bartlomiej Zolnierkiewicz6ea52222008-12-29 20:27:31 +0100501 spin_lock_irqsave(q->queue_lock, flags);
Bartlomiej Zolnierkiewiczbbb89e32008-02-01 23:09:28 +0100502 blkdev_dequeue_request(rq);
Bartlomiej Zolnierkiewicz6ea52222008-12-29 20:27:31 +0100503 spin_unlock_irqrestore(q->queue_lock, flags);
Bartlomiej Zolnierkiewiczbbb89e32008-02-01 23:09:28 +0100504
Bartlomiej Zolnierkiewicz1d0bf582008-12-29 20:27:30 +0100505 hwgroup->rq = NULL;
506
Bartlomiej Zolnierkiewiczbbb89e32008-02-01 23:09:28 +0100507 cdrom_queue_request_sense(drive, rq->sense, rq);
508 } else
509 cdrom_end_request(drive, 0);
510
511 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512}
513
Borislav Petkov65a33092009-01-02 16:12:55 +0100514static ide_startstop_t cdrom_transfer_packet_command(ide_drive_t *);
515static ide_startstop_t cdrom_newpc_intr(ide_drive_t *);
516
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200517/*
518 * Set up the device registers for transferring a packet command on DEV,
519 * expecting to later transfer XFERLEN bytes. HANDLER is the routine
520 * which actually transfers the command to the drive. If this is a
521 * drq_interrupt device, this routine will arrange for HANDLER to be
522 * called when the interrupt from the drive arrives. Otherwise, HANDLER
523 * will be called immediately after the drive is prepared for the transfer.
524 */
Borislav Petkov65a33092009-01-02 16:12:55 +0100525static ide_startstop_t cdrom_start_packet_command(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 ide_hwif_t *hwif = drive->hwif;
Borislav Petkov563d9932009-01-02 16:12:55 +0100528 struct request *rq = hwif->hwgroup->rq;
529 int xferlen;
530
531 xferlen = ide_cd_get_xferlen(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200533 ide_debug_log(IDE_DBG_PC, "Call %s, xferlen: %d\n", __func__, xferlen);
534
Bartlomiej Zolnierkiewicz3a6a3542008-01-25 22:17:13 +0100535 /* FIXME: for Virtual DMA we must check harder */
Borislav Petkov12469ac2008-10-13 21:39:49 +0200536 if (drive->dma)
537 drive->dma = !hwif->dma_ops->dma_setup(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200539 /* set up the controller registers */
Bartlomiej Zolnierkiewicz9a410e72008-07-15 21:21:48 +0200540 ide_pktcmd_tf_load(drive, IDE_TFLAG_OUT_NSECT | IDE_TFLAG_OUT_LBAL,
Borislav Petkov12469ac2008-10-13 21:39:49 +0200541 xferlen, drive->dma);
Bartlomiej Zolnierkiewicz4fe67172008-02-01 23:09:21 +0100542
Borislav Petkov570f89e2008-07-23 19:56:02 +0200543 if (drive->atapi_flags & IDE_AFLAG_DRQ_INTERRUPT) {
Albert Leef0dd8712007-02-17 02:40:21 +0100544 /* waiting for CDB interrupt, not DMA yet. */
Borislav Petkov12469ac2008-10-13 21:39:49 +0200545 if (drive->dma)
Albert Leef0dd8712007-02-17 02:40:21 +0100546 drive->waiting_for_dma = 0;
547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 /* packet command */
Borislav Petkov65a33092009-01-02 16:12:55 +0100549 ide_execute_command(drive, ATA_CMD_PACKET,
550 cdrom_transfer_packet_command,
Borislav Petkov4cad0852009-01-02 16:12:53 +0100551 ATAPI_WAIT_PC, ide_cd_expiry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 return ide_started;
553 } else {
Bartlomiej Zolnierkiewicz1fc14252008-04-28 23:44:39 +0200554 ide_execute_pkt_cmd(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Borislav Petkov65a33092009-01-02 16:12:55 +0100556 return cdrom_transfer_packet_command(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
558}
559
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200560/*
561 * Send a packet command to DRIVE described by CMD_BUF and CMD_LEN. The device
562 * registers must have already been prepared by cdrom_start_packet_command.
563 * HANDLER is the interrupt handler to call when the command completes or
564 * there's data ready.
565 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566#define ATAPI_MIN_CDB_BYTES 12
Borislav Petkov65a33092009-01-02 16:12:55 +0100567static ide_startstop_t cdrom_transfer_packet_command(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
569 ide_hwif_t *hwif = drive->hwif;
Borislav Petkov65a33092009-01-02 16:12:55 +0100570 struct request *rq = hwif->hwgroup->rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 int cmd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 ide_startstop_t startstop;
573
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200574 ide_debug_log(IDE_DBG_PC, "Call %s\n", __func__);
575
Borislav Petkov570f89e2008-07-23 19:56:02 +0200576 if (drive->atapi_flags & IDE_AFLAG_DRQ_INTERRUPT) {
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200577 /*
578 * Here we should have been called after receiving an interrupt
579 * from the device. DRQ should how be set.
580 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200582 /* check for errors */
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200583 if (cdrom_decode_status(drive, ATA_DRQ, NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 return ide_stopped;
Albert Leef0dd8712007-02-17 02:40:21 +0100585
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200586 /* ok, next interrupt will be DMA interrupt */
Borislav Petkov12469ac2008-10-13 21:39:49 +0200587 if (drive->dma)
Albert Leef0dd8712007-02-17 02:40:21 +0100588 drive->waiting_for_dma = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 } else {
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200590 /* otherwise, we must wait for DRQ to get set */
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200591 if (ide_wait_stat(&startstop, drive, ATA_DRQ,
592 ATA_BUSY, WAIT_READY))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 return startstop;
594 }
595
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200596 /* arm the interrupt handler */
Borislav Petkov65a33092009-01-02 16:12:55 +0100597 ide_set_handler(drive, cdrom_newpc_intr, rq->timeout, ide_cd_expiry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
599 /* ATAPI commands get padded out to 12 bytes minimum */
600 cmd_len = COMMAND_SIZE(rq->cmd[0]);
601 if (cmd_len < ATAPI_MIN_CDB_BYTES)
602 cmd_len = ATAPI_MIN_CDB_BYTES;
603
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200604 /* send the command to the device */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200605 hwif->tp_ops->output_data(drive, NULL, rq->cmd, cmd_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200607 /* start the DMA if need be */
Borislav Petkov12469ac2008-10-13 21:39:49 +0200608 if (drive->dma)
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +0200609 hwif->dma_ops->dma_start(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611 return ide_started;
612}
613
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200614/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 * Check the contents of the interrupt reason register from the cdrom
616 * and attempt to recover if there are problems. Returns 0 if everything's
617 * ok; nonzero if the request has been terminated.
618 */
Bartlomiej Zolnierkiewicz9f10d9e2008-02-26 21:50:35 +0100619static int ide_cd_check_ireason(ide_drive_t *drive, struct request *rq,
620 int len, int ireason, int rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
Bartlomiej Zolnierkiewiczb73c7ee2008-07-23 19:55:52 +0200622 ide_hwif_t *hwif = drive->hwif;
623
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200624 ide_debug_log(IDE_DBG_FUNC, "Call %s, ireason: 0x%x, rw: 0x%x\n",
625 __func__, ireason, rw);
626
Bartlomiej Zolnierkiewicz0d6f7e32008-02-01 23:09:28 +0100627 /*
628 * ireason == 0: the drive wants to receive data from us
629 * ireason == 2: the drive is expecting to transfer data to us
630 */
631 if (ireason == (!rw << 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 return 0;
Bartlomiej Zolnierkiewicz0d6f7e32008-02-01 23:09:28 +0100633 else if (ireason == (rw << 1)) {
Bartlomiej Zolnierkiewicz5a5222d2008-02-01 23:09:17 +0100634
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200635 /* whoops... */
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200636 printk(KERN_ERR PFX "%s: %s: wrong transfer direction!\n",
Paolo Ciarrocchi177773e2008-04-26 17:36:42 +0200637 drive->name, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Borislav Petkovae8789f2008-07-16 20:33:45 +0200639 ide_pad_transfer(drive, rw, len);
Bartlomiej Zolnierkiewicz0d6f7e32008-02-01 23:09:28 +0100640 } else if (rw == 0 && ireason == 1) {
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200641 /*
642 * Some drives (ASUS) seem to tell us that status info is
643 * available. Just get it and ignore.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200645 (void)hwif->tp_ops->read_status(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 return 0;
647 } else {
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200648 /* drive wants a command packet, or invalid ireason... */
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200649 printk(KERN_ERR PFX "%s: %s: bad interrupt reason 0x%02x\n",
Paolo Ciarrocchi177773e2008-04-26 17:36:42 +0200650 drive->name, __func__, ireason);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 }
652
Bartlomiej Zolnierkiewicz9f10d9e2008-02-26 21:50:35 +0100653 if (rq->cmd_type == REQ_TYPE_ATA_PC)
654 rq->cmd_flags |= REQ_FAILED;
655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 cdrom_end_request(drive, 0);
657 return -1;
658}
659
660/*
Bartlomiej Zolnierkiewicz64814f22008-02-01 23:09:26 +0100661 * Assume that the drive will always provide data in multiples of at least
662 * SECTOR_SIZE, as it gets hairy to keep track of the transfers otherwise.
663 */
664static int ide_cd_check_transfer_size(ide_drive_t *drive, int len)
665{
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200666 ide_debug_log(IDE_DBG_FUNC, "Call %s, len: %d\n", __func__, len);
667
Bartlomiej Zolnierkiewicz64814f22008-02-01 23:09:26 +0100668 if ((len % SECTOR_SIZE) == 0)
669 return 0;
670
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200671 printk(KERN_ERR PFX "%s: %s: Bad transfer size %d\n", drive->name,
672 __func__, len);
Bartlomiej Zolnierkiewicz64814f22008-02-01 23:09:26 +0100673
Borislav Petkov570f89e2008-07-23 19:56:02 +0200674 if (drive->atapi_flags & IDE_AFLAG_LIMIT_NFRAMES)
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200675 printk(KERN_ERR PFX "This drive is not supported by this "
676 "version of the driver\n");
Bartlomiej Zolnierkiewicz64814f22008-02-01 23:09:26 +0100677 else {
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200678 printk(KERN_ERR PFX "Trying to limit transfer sizes\n");
Borislav Petkov570f89e2008-07-23 19:56:02 +0200679 drive->atapi_flags |= IDE_AFLAG_LIMIT_NFRAMES;
Bartlomiej Zolnierkiewicz64814f22008-02-01 23:09:26 +0100680 }
681
682 return 1;
683}
684
Borislav Petkov90eb8082008-07-16 20:33:46 +0200685static ide_startstop_t ide_cd_prepare_rw_request(ide_drive_t *drive,
686 struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200688 ide_debug_log(IDE_DBG_RQ, "Call %s: rq->cmd_flags: 0x%x\n", __func__,
689 rq->cmd_flags);
690
Bartlomiej Zolnierkiewicz29f3aac2008-02-01 23:09:27 +0100691 if (rq_data_dir(rq) == READ) {
692 unsigned short sectors_per_frame =
693 queue_hardsect_size(drive->queue) >> SECTOR_BITS;
694 int nskip = rq->sector & (sectors_per_frame - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Bartlomiej Zolnierkiewicz29f3aac2008-02-01 23:09:27 +0100696 /*
697 * If the requested sector doesn't start on a frame boundary,
698 * we must adjust the start of the transfer so that it does,
699 * and remember to skip the first few sectors.
700 *
701 * If the rq->current_nr_sectors field is larger than the size
702 * of the buffer, it will mean that we're to skip a number of
703 * sectors equal to the amount by which rq->current_nr_sectors
704 * is larger than the buffer size.
705 */
706 if (nskip > 0) {
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200707 /* sanity check... */
Bartlomiej Zolnierkiewicz29f3aac2008-02-01 23:09:27 +0100708 if (rq->current_nr_sectors !=
709 bio_cur_sectors(rq->bio)) {
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200710 printk(KERN_ERR PFX "%s: %s: buffer botch (%u)\n",
Paolo Ciarrocchi177773e2008-04-26 17:36:42 +0200711 drive->name, __func__,
Bartlomiej Zolnierkiewicz29f3aac2008-02-01 23:09:27 +0100712 rq->current_nr_sectors);
713 cdrom_end_request(drive, 0);
714 return ide_stopped;
715 }
716 rq->current_nr_sectors += nskip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 }
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200719
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200720 /* set up the command */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 rq->timeout = ATAPI_WAIT_PC;
722
Borislav Petkov90eb8082008-07-16 20:33:46 +0200723 return ide_started;
724}
725
726/*
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200727 * Fix up a possibly partially-processed request so that we can start it over
728 * entirely, or even put it back on the request queue.
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +0200729 */
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200730static void ide_cd_restore_request(ide_drive_t *drive, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200732
733 ide_debug_log(IDE_DBG_FUNC, "Call %s\n", __func__);
734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 if (rq->buffer != bio_data(rq->bio)) {
Borislav Petkov83c85652008-04-26 22:25:15 +0200736 sector_t n =
737 (rq->buffer - (char *)bio_data(rq->bio)) / SECTOR_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
739 rq->buffer = bio_data(rq->bio);
740 rq->nr_sectors += n;
741 rq->sector -= n;
742 }
Borislav Petkov83c85652008-04-26 22:25:15 +0200743 rq->current_nr_sectors = bio_cur_sectors(rq->bio);
744 rq->hard_cur_sectors = rq->current_nr_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 rq->hard_nr_sectors = rq->nr_sectors;
746 rq->hard_sector = rq->sector;
747 rq->q->prep_rq_fn(rq->q, rq);
748}
749
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200750static void ide_cd_request_sense_fixup(ide_drive_t *drive, struct request *rq)
Bartlomiej Zolnierkiewicz8ee69f52008-02-01 23:09:25 +0100751{
Borislav Petkov71b429ca2008-10-17 18:09:14 +0200752 ide_debug_log(IDE_DBG_FUNC, "Call %s, rq->cmd[0]: 0x%x\n",
753 __func__, rq->cmd[0]);
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200754
Bartlomiej Zolnierkiewicz8ee69f52008-02-01 23:09:25 +0100755 /*
756 * Some of the trailing request sense fields are optional,
757 * and some drives don't send them. Sigh.
758 */
759 if (rq->cmd[0] == GPCMD_REQUEST_SENSE &&
760 rq->data_len > 0 && rq->data_len <= 5)
761 while (rq->data_len > 0) {
762 *(u8 *)rq->data++ = 0;
763 --rq->data_len;
764 }
765}
766
FUJITA Tomonori5f828542008-07-15 21:21:42 +0200767int ide_cd_queue_pc(ide_drive_t *drive, const unsigned char *cmd,
768 int write, void *buffer, unsigned *bufflen,
769 struct request_sense *sense, int timeout,
770 unsigned int cmd_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
FUJITA Tomonori5f828542008-07-15 21:21:42 +0200772 struct cdrom_info *info = drive->driver_data;
773 struct request_sense local_sense;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 int retries = 10;
FUJITA Tomonori5f828542008-07-15 21:21:42 +0200775 unsigned int flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
FUJITA Tomonori5f828542008-07-15 21:21:42 +0200777 if (!sense)
778 sense = &local_sense;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Borislav Petkov71b429ca2008-10-17 18:09:14 +0200780 ide_debug_log(IDE_DBG_PC, "Call %s, cmd[0]: 0x%x, write: 0x%x, "
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200781 "timeout: %d, cmd_flags: 0x%x\n", __func__, cmd[0], write,
782 timeout, cmd_flags);
783
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200784 /* start of retry loop */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 do {
FUJITA Tomonori5f828542008-07-15 21:21:42 +0200786 struct request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
FUJITA Tomonori5f828542008-07-15 21:21:42 +0200789 rq = blk_get_request(drive->queue, write, __GFP_WAIT);
790
791 memcpy(rq->cmd, cmd, BLK_MAX_CDB);
792 rq->cmd_type = REQ_TYPE_ATA_PC;
793 rq->sense = sense;
794 rq->cmd_flags |= cmd_flags;
795 rq->timeout = timeout;
796 if (buffer) {
797 rq->data = buffer;
798 rq->data_len = *bufflen;
799 }
800
801 error = blk_execute_rq(drive->queue, info->disk, rq, 0);
802
803 if (buffer)
804 *bufflen = rq->data_len;
805
806 flags = rq->cmd_flags;
807 blk_put_request(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200809 /*
810 * FIXME: we should probably abort/retry or something in case of
811 * failure.
812 */
FUJITA Tomonori5f828542008-07-15 21:21:42 +0200813 if (flags & REQ_FAILED) {
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200814 /*
815 * The request failed. Retry if it was due to a unit
816 * attention status (usually means media was changed).
817 */
FUJITA Tomonori5f828542008-07-15 21:21:42 +0200818 struct request_sense *reqbuf = sense;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820 if (reqbuf->sense_key == UNIT_ATTENTION)
821 cdrom_saw_media_change(drive);
822 else if (reqbuf->sense_key == NOT_READY &&
823 reqbuf->asc == 4 && reqbuf->ascq != 4) {
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200824 /*
825 * The drive is in the process of loading
826 * a disk. Retry, but wait a little to give
827 * the drive time to complete the load.
828 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 ssleep(2);
830 } else {
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200831 /* otherwise, don't retry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 retries = 0;
833 }
834 --retries;
835 }
836
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200837 /* end of retry loop */
FUJITA Tomonori5f828542008-07-15 21:21:42 +0200838 } while ((flags & REQ_FAILED) && retries >= 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200840 /* return an error if the command failed */
FUJITA Tomonori5f828542008-07-15 21:21:42 +0200841 return (flags & REQ_FAILED) ? -EIO : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842}
843
844/*
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200845 * Called from blk_end_request_callback() after the data of the request is
846 * completed and before the request itself is completed. By returning value '1',
847 * blk_end_request_callback() returns immediately without completing it.
Kiyoshi Uedaaaa04c22007-12-11 17:51:23 -0500848 */
849static int cdrom_newpc_intr_dummy_cb(struct request *rq)
850{
851 return 1;
852}
853
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854static ide_startstop_t cdrom_newpc_intr(ide_drive_t *drive)
855{
Bartlomiej Zolnierkiewicz23579a22008-04-18 00:46:26 +0200856 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewicz1d0bf582008-12-29 20:27:30 +0100857 ide_hwgroup_t *hwgroup = hwif->hwgroup;
858 struct request *rq = hwgroup->rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 xfer_func_t *xferfunc;
Bartlomiej Zolnierkiewicz0041e7c2008-02-01 23:09:28 +0100860 ide_expiry_t *expiry = NULL;
Bartlomiej Zolnierkiewicz18236492008-07-23 19:55:54 +0200861 int dma_error = 0, dma, stat, thislen, uptodate = 0;
Bartlomiej Zolnierkiewiczff1bfbc12008-02-01 23:09:25 +0100862 int write = (rq_data_dir(rq) == WRITE) ? 1 : 0;
863 unsigned int timeout;
Bartlomiej Zolnierkiewicz18236492008-07-23 19:55:54 +0200864 u16 len;
865 u8 ireason;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200867 ide_debug_log(IDE_DBG_PC, "Call %s, rq->cmd[0]: 0x%x, write: 0x%x\n",
868 __func__, rq->cmd[0], write);
869
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200870 /* check for errors */
Borislav Petkov12469ac2008-10-13 21:39:49 +0200871 dma = drive->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 if (dma) {
Borislav Petkov12469ac2008-10-13 21:39:49 +0200873 drive->dma = 0;
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +0200874 dma_error = hwif->dma_ops->dma_end(drive);
Bartlomiej Zolnierkiewiczeba15fb2008-02-01 23:09:17 +0100875 if (dma_error) {
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200876 printk(KERN_ERR PFX "%s: DMA %s error\n", drive->name,
Bartlomiej Zolnierkiewiczff1bfbc12008-02-01 23:09:25 +0100877 write ? "write" : "read");
Bartlomiej Zolnierkiewiczeba15fb2008-02-01 23:09:17 +0100878 ide_dma_off(drive);
879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 }
881
882 if (cdrom_decode_status(drive, 0, &stat))
883 return ide_stopped;
884
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200885 /* using dma, transfer is complete now */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 if (dma) {
Bartlomiej Zolnierkiewiczeba15fb2008-02-01 23:09:17 +0100887 if (dma_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 return ide_error(drive, "dma error", stat);
Bartlomiej Zolnierkiewicz0041e7c2008-02-01 23:09:28 +0100889 if (blk_fs_request(rq)) {
890 ide_end_request(drive, 1, rq->nr_sectors);
891 return ide_stopped;
892 }
Bartlomiej Zolnierkiewiczff1bfbc12008-02-01 23:09:25 +0100893 goto end_request;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 }
895
Bartlomiej Zolnierkiewicz18236492008-07-23 19:55:54 +0200896 ide_read_bcount_and_ireason(drive, &len, &ireason);
Bartlomiej Zolnierkiewicz0041e7c2008-02-01 23:09:28 +0100897
898 thislen = blk_fs_request(rq) ? len : rq->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 if (thislen > len)
900 thislen = len;
901
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200902 ide_debug_log(IDE_DBG_PC, "%s: DRQ: stat: 0x%x, thislen: %d\n",
903 __func__, stat, thislen);
904
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200905 /* If DRQ is clear, the command has completed. */
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200906 if ((stat & ATA_DRQ) == 0) {
Bartlomiej Zolnierkiewicz0041e7c2008-02-01 23:09:28 +0100907 if (blk_fs_request(rq)) {
908 /*
909 * If we're not done reading/writing, complain.
910 * Otherwise, complete the command normally.
911 */
912 uptodate = 1;
913 if (rq->current_nr_sectors > 0) {
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200914 printk(KERN_ERR PFX "%s: %s: data underrun "
Bartlomiej Zolnierkiewicz0041e7c2008-02-01 23:09:28 +0100915 "(%d blocks)\n",
Paolo Ciarrocchi177773e2008-04-26 17:36:42 +0200916 drive->name, __func__,
Bartlomiej Zolnierkiewicz0041e7c2008-02-01 23:09:28 +0100917 rq->current_nr_sectors);
918 if (!write)
919 rq->cmd_flags |= REQ_FAILED;
920 uptodate = 0;
921 }
922 cdrom_end_request(drive, uptodate);
923 return ide_stopped;
924 } else if (!blk_pc_request(rq)) {
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200925 ide_cd_request_sense_fixup(drive, rq);
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200926 /* complain if we still have data left to transfer */
Bartlomiej Zolnierkiewiczff1bfbc12008-02-01 23:09:25 +0100927 uptodate = rq->data_len ? 0 : 1;
928 }
929 goto end_request;
Kiyoshi Uedaaaa04c22007-12-11 17:51:23 -0500930 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200932 /* check which way to transfer data */
Bartlomiej Zolnierkiewicz9f10d9e2008-02-26 21:50:35 +0100933 if (ide_cd_check_ireason(drive, rq, len, ireason, write))
934 return ide_stopped;
Bartlomiej Zolnierkiewicz0041e7c2008-02-01 23:09:28 +0100935
Bartlomiej Zolnierkiewicz9f10d9e2008-02-26 21:50:35 +0100936 if (blk_fs_request(rq)) {
937 if (write == 0) {
Bartlomiej Zolnierkiewicz0041e7c2008-02-01 23:09:28 +0100938 int nskip;
939
940 if (ide_cd_check_transfer_size(drive, len)) {
941 cdrom_end_request(drive, 0);
942 return ide_stopped;
943 }
944
945 /*
946 * First, figure out if we need to bit-bucket
947 * any of the leading sectors.
948 */
949 nskip = min_t(int, rq->current_nr_sectors
950 - bio_cur_sectors(rq->bio),
951 thislen >> 9);
952 if (nskip > 0) {
Borislav Petkovae8789f2008-07-16 20:33:45 +0200953 ide_pad_transfer(drive, write, nskip << 9);
Bartlomiej Zolnierkiewicz0041e7c2008-02-01 23:09:28 +0100954 rq->current_nr_sectors -= nskip;
955 thislen -= (nskip << 9);
956 }
957 }
Bartlomiej Zolnierkiewiczff1bfbc12008-02-01 23:09:25 +0100958 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
Bartlomiej Zolnierkiewiczff1bfbc12008-02-01 23:09:25 +0100960 if (ireason == 0) {
961 write = 1;
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200962 xferfunc = hwif->tp_ops->output_data;
Bartlomiej Zolnierkiewicz9f10d9e2008-02-26 21:50:35 +0100963 } else {
Bartlomiej Zolnierkiewiczff1bfbc12008-02-01 23:09:25 +0100964 write = 0;
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200965 xferfunc = hwif->tp_ops->input_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 }
967
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200968 ide_debug_log(IDE_DBG_PC, "%s: data transfer, rq->cmd_type: 0x%x, "
969 "ireason: 0x%x\n", __func__, rq->cmd_type, ireason);
970
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200971 /* transfer data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 while (thislen > 0) {
Bartlomiej Zolnierkiewicz0041e7c2008-02-01 23:09:28 +0100973 u8 *ptr = blk_fs_request(rq) ? NULL : rq->data;
Bartlomiej Zolnierkiewicza11e77d2008-02-01 23:09:27 +0100974 int blen = rq->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200976 /* bio backed? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 if (rq->bio) {
Bartlomiej Zolnierkiewicz0041e7c2008-02-01 23:09:28 +0100978 if (blk_fs_request(rq)) {
979 ptr = rq->buffer;
980 blen = rq->current_nr_sectors << 9;
981 } else {
982 ptr = bio_data(rq->bio);
983 blen = bio_iovec(rq->bio)->bv_len;
984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 }
986
987 if (!ptr) {
Bartlomiej Zolnierkiewicz0041e7c2008-02-01 23:09:28 +0100988 if (blk_fs_request(rq) && !write)
989 /*
Borislav Petkov968c4962008-04-26 17:36:37 +0200990 * If the buffers are full, pipe the rest into
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +0200991 * oblivion.
992 */
Borislav Petkovae8789f2008-07-16 20:33:45 +0200993 ide_pad_transfer(drive, 0, thislen);
Bartlomiej Zolnierkiewicz0041e7c2008-02-01 23:09:28 +0100994 else {
Borislav Petkovfc8323f2008-10-13 21:39:48 +0200995 printk(KERN_ERR PFX "%s: confused, missing data\n",
Bartlomiej Zolnierkiewicz0041e7c2008-02-01 23:09:28 +0100996 drive->name);
997 blk_dump_rq_flags(rq, rq_data_dir(rq)
998 ? "cdrom_newpc_intr, write"
999 : "cdrom_newpc_intr, read");
1000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 break;
1002 }
1003
1004 if (blen > thislen)
1005 blen = thislen;
1006
Bartlomiej Zolnierkiewicz9567b342008-04-28 23:44:36 +02001007 xferfunc(drive, NULL, ptr, blen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
1009 thislen -= blen;
1010 len -= blen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Bartlomiej Zolnierkiewicz0041e7c2008-02-01 23:09:28 +01001012 if (blk_fs_request(rq)) {
1013 rq->buffer += blen;
1014 rq->nr_sectors -= (blen >> 9);
1015 rq->current_nr_sectors -= (blen >> 9);
1016 rq->sector += (blen >> 9);
1017
1018 if (rq->current_nr_sectors == 0 && rq->nr_sectors)
1019 cdrom_end_request(drive, 1);
1020 } else {
1021 rq->data_len -= blen;
1022
Kiyoshi Uedaaaa04c22007-12-11 17:51:23 -05001023 /*
1024 * The request can't be completed until DRQ is cleared.
1025 * So complete the data, but don't complete the request
1026 * using the dummy function for the callback feature
1027 * of blk_end_request_callback().
1028 */
Bartlomiej Zolnierkiewicz0041e7c2008-02-01 23:09:28 +01001029 if (rq->bio)
1030 blk_end_request_callback(rq, 0, blen,
Kiyoshi Uedaaaa04c22007-12-11 17:51:23 -05001031 cdrom_newpc_intr_dummy_cb);
Bartlomiej Zolnierkiewicz0041e7c2008-02-01 23:09:28 +01001032 else
1033 rq->data += blen;
1034 }
Andreas Schwabbcd88ac2008-02-26 21:50:35 +01001035 if (!write && blk_sense_request(rq))
1036 rq->sense_len += blen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 }
1038
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02001039 /* pad, if necessary */
Bartlomiej Zolnierkiewicz0041e7c2008-02-01 23:09:28 +01001040 if (!blk_fs_request(rq) && len > 0)
Borislav Petkovae8789f2008-07-16 20:33:45 +02001041 ide_pad_transfer(drive, write, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Bartlomiej Zolnierkiewiczff1bfbc12008-02-01 23:09:25 +01001043 if (blk_pc_request(rq)) {
1044 timeout = rq->timeout;
Bartlomiej Zolnierkiewiczff1bfbc12008-02-01 23:09:25 +01001045 } else {
1046 timeout = ATAPI_WAIT_PC;
Bartlomiej Zolnierkiewicz0041e7c2008-02-01 23:09:28 +01001047 if (!blk_fs_request(rq))
Borislav Petkov4cad0852009-01-02 16:12:53 +01001048 expiry = ide_cd_expiry;
Bartlomiej Zolnierkiewiczff1bfbc12008-02-01 23:09:25 +01001049 }
1050
1051 ide_set_handler(drive, cdrom_newpc_intr, timeout, expiry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 return ide_started;
Bartlomiej Zolnierkiewiczff1bfbc12008-02-01 23:09:25 +01001053
1054end_request:
1055 if (blk_pc_request(rq)) {
Kiyoshi Ueda14e04c32008-02-19 01:41:26 +01001056 unsigned int dlen = rq->data_len;
1057
1058 if (dma)
1059 rq->data_len = 0;
Bartlomiej Zolnierkiewiczff1bfbc12008-02-01 23:09:25 +01001060
Bartlomiej Zolnierkiewicz3c8a2cc2008-12-29 20:27:31 +01001061 if (blk_end_request(rq, 0, dlen))
Bartlomiej Zolnierkiewiczff1bfbc12008-02-01 23:09:25 +01001062 BUG();
Bartlomiej Zolnierkiewicz1d0bf582008-12-29 20:27:30 +01001063
1064 hwgroup->rq = NULL;
Bartlomiej Zolnierkiewiczff1bfbc12008-02-01 23:09:25 +01001065 } else {
1066 if (!uptodate)
1067 rq->cmd_flags |= REQ_FAILED;
1068 cdrom_end_request(drive, uptodate);
1069 }
1070 return ide_stopped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071}
1072
Bartlomiej Zolnierkiewicz21ea1f02008-02-01 23:09:27 +01001073static ide_startstop_t cdrom_start_rw(ide_drive_t *drive, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074{
Bartlomiej Zolnierkiewicz21ea1f02008-02-01 23:09:27 +01001075 struct cdrom_info *cd = drive->driver_data;
1076 int write = rq_data_dir(rq) == WRITE;
1077 unsigned short sectors_per_frame =
1078 queue_hardsect_size(drive->queue) >> SECTOR_BITS;
1079
Borislav Petkov71b429ca2008-10-17 18:09:14 +02001080 ide_debug_log(IDE_DBG_RQ, "Call %s, rq->cmd[0]: 0x%x, write: 0x%x, "
1081 "secs_per_frame: %u\n",
1082 __func__, rq->cmd[0], write, sectors_per_frame);
Borislav Petkovfc8323f2008-10-13 21:39:48 +02001083
Bartlomiej Zolnierkiewicz21ea1f02008-02-01 23:09:27 +01001084 if (write) {
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02001085 /* disk has become write protected */
Tejun Heob7db9952008-08-25 19:56:10 +09001086 if (get_disk_ro(cd->disk)) {
Bartlomiej Zolnierkiewicz21ea1f02008-02-01 23:09:27 +01001087 cdrom_end_request(drive, 0);
1088 return ide_stopped;
1089 }
1090 } else {
1091 /*
1092 * We may be retrying this request after an error. Fix up any
1093 * weirdness which might be present in the request packet.
1094 */
Borislav Petkovfc8323f2008-10-13 21:39:48 +02001095 ide_cd_restore_request(drive, rq);
Bartlomiej Zolnierkiewicz21ea1f02008-02-01 23:09:27 +01001096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02001098 /* use DMA, if possible / writes *must* be hardware frame aligned */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 if ((rq->nr_sectors & (sectors_per_frame - 1)) ||
1100 (rq->sector & (sectors_per_frame - 1))) {
Bartlomiej Zolnierkiewicz21ea1f02008-02-01 23:09:27 +01001101 if (write) {
1102 cdrom_end_request(drive, 0);
1103 return ide_stopped;
1104 }
Borislav Petkov12469ac2008-10-13 21:39:49 +02001105 drive->dma = 0;
Bartlomiej Zolnierkiewicz21ea1f02008-02-01 23:09:27 +01001106 } else
Borislav Petkov12469ac2008-10-13 21:39:49 +02001107 drive->dma = !!(drive->dev_flags & IDE_DFLAG_USING_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Bartlomiej Zolnierkiewicz21ea1f02008-02-01 23:09:27 +01001109 if (write)
1110 cd->devinfo.media_written = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
Borislav Petkovb6ca4402008-07-16 20:33:46 +02001112 return ide_started;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113}
1114
Borislav Petkovb6ca4402008-07-16 20:33:46 +02001115static void cdrom_do_block_pc(ide_drive_t *drive, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
Borislav Petkov71b429ca2008-10-17 18:09:14 +02001118 ide_debug_log(IDE_DBG_PC, "Call %s, rq->cmd[0]: 0x%x, "
1119 "rq->cmd_type: 0x%x\n", __func__, rq->cmd[0],
Borislav Petkovfc8323f2008-10-13 21:39:48 +02001120 rq->cmd_type);
1121
Bartlomiej Zolnierkiewiczc9f56a82008-02-01 23:09:26 +01001122 if (blk_pc_request(rq))
1123 rq->cmd_flags |= REQ_QUIET;
1124 else
1125 rq->cmd_flags &= ~REQ_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Borislav Petkov12469ac2008-10-13 21:39:49 +02001127 drive->dma = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02001129 /* sg request */
FUJITA Tomonorie5318b52008-07-16 20:33:35 +02001130 if (rq->bio || ((rq->cmd_type == REQ_TYPE_ATA_PC) && rq->data_len)) {
1131 struct request_queue *q = drive->queue;
1132 unsigned int alignment;
FUJITA Tomonoriefa402d2008-10-10 22:39:22 +02001133 char *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
FUJITA Tomonorie5318b52008-07-16 20:33:35 +02001135 if (rq->bio)
FUJITA Tomonoriefa402d2008-10-10 22:39:22 +02001136 buf = bio_data(rq->bio);
FUJITA Tomonorie5318b52008-07-16 20:33:35 +02001137 else
FUJITA Tomonoriefa402d2008-10-10 22:39:22 +02001138 buf = rq->data;
FUJITA Tomonorie5318b52008-07-16 20:33:35 +02001139
Borislav Petkov12469ac2008-10-13 21:39:49 +02001140 drive->dma = !!(drive->dev_flags & IDE_DFLAG_USING_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
1142 /*
1143 * check if dma is safe
Linus Torvalds5d9e4ea2005-05-27 07:36:17 -07001144 *
1145 * NOTE! The "len" and "addr" checks should possibly have
1146 * separate masks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 */
FUJITA Tomonorie5318b52008-07-16 20:33:35 +02001148 alignment = queue_dma_alignment(q) | q->dma_pad_mask;
Borislav Petkov9bd27cb2008-11-02 21:40:07 +01001149 if ((unsigned long)buf & alignment
1150 || rq->data_len & q->dma_pad_mask
FUJITA Tomonoriefa402d2008-10-10 22:39:22 +02001151 || object_is_on_stack(buf))
Borislav Petkov12469ac2008-10-13 21:39:49 +02001152 drive->dma = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154}
1155
Borislav Petkov99384ae2008-07-16 20:33:45 +02001156static ide_startstop_t ide_cd_do_request(ide_drive_t *drive, struct request *rq,
Borislav Petkove5e076a2008-04-26 22:25:15 +02001157 sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158{
Borislav Petkov71b429ca2008-10-17 18:09:14 +02001159 ide_debug_log(IDE_DBG_RQ, "Call %s, rq->cmd[0]: 0x%x, "
1160 "rq->cmd_type: 0x%x, block: %llu\n",
1161 __func__, rq->cmd[0], rq->cmd_type,
1162 (unsigned long long)block);
Borislav Petkovfc8323f2008-10-13 21:39:48 +02001163
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 if (blk_fs_request(rq)) {
Bartlomiej Zolnierkiewicz27c01c22008-12-29 20:27:32 +01001165 if (cdrom_start_rw(drive, rq) == ide_stopped)
1166 return ide_stopped;
Borislav Petkove529c602008-07-16 20:33:46 +02001167
Bartlomiej Zolnierkiewicz27c01c22008-12-29 20:27:32 +01001168 if (ide_cd_prepare_rw_request(drive, rq) == ide_stopped)
1169 return ide_stopped;
Bartlomiej Zolnierkiewiczc9f56a82008-02-01 23:09:26 +01001170 } else if (blk_sense_request(rq) || blk_pc_request(rq) ||
Jens Axboecea28852006-10-12 15:08:45 +02001171 rq->cmd_type == REQ_TYPE_ATA_PC) {
Borislav Petkov7fcebda2008-07-16 20:33:46 +02001172 if (!rq->timeout)
1173 rq->timeout = ATAPI_WAIT_PC;
1174
Borislav Petkovb6ca4402008-07-16 20:33:46 +02001175 cdrom_do_block_pc(drive, rq);
Jens Axboe4aff5e22006-08-10 08:44:47 +02001176 } else if (blk_special_request(rq)) {
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02001177 /* right now this can only be a reset... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 cdrom_end_request(drive, 1);
1179 return ide_stopped;
Borislav Petkovab9d6e32008-07-16 20:33:45 +02001180 } else {
Borislav Petkovfc8323f2008-10-13 21:39:48 +02001181 blk_dump_rq_flags(rq, DRV_NAME " bad flags");
Borislav Petkovab9d6e32008-07-16 20:33:45 +02001182 cdrom_end_request(drive, 0);
1183 return ide_stopped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 }
Borislav Petkovb6ca4402008-07-16 20:33:46 +02001185
Borislav Petkov65a33092009-01-02 16:12:55 +01001186 return cdrom_start_packet_command(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187}
1188
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02001189/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 * Ioctl handling.
1191 *
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02001192 * Routines which queue packet commands take as a final argument a pointer to a
1193 * request_sense struct. If execution of the command results in an error with a
1194 * CHECK CONDITION status, this structure will be filled with the results of the
1195 * subsequent request sense command. The pointer can also be NULL, in which case
1196 * no sense information is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 */
Borislav Petkove5e076a2008-04-26 22:25:15 +02001198static void msf_from_bcd(struct atapi_msf *msf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199{
Adrian Bunkfc99856a2008-08-18 21:40:04 +02001200 msf->minute = bcd2bin(msf->minute);
1201 msf->second = bcd2bin(msf->second);
1202 msf->frame = bcd2bin(msf->frame);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203}
1204
Borislav Petkovf9afd182008-02-01 23:09:29 +01001205int cdrom_check_status(ide_drive_t *drive, struct request_sense *sense)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 struct cdrom_info *info = drive->driver_data;
1208 struct cdrom_device_info *cdi = &info->devinfo;
FUJITA Tomonori5f828542008-07-15 21:21:42 +02001209 unsigned char cmd[BLK_MAX_CDB];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
Borislav Petkovfc8323f2008-10-13 21:39:48 +02001211 ide_debug_log(IDE_DBG_FUNC, "Call %s\n", __func__);
1212
FUJITA Tomonori5f828542008-07-15 21:21:42 +02001213 memset(cmd, 0, BLK_MAX_CDB);
1214 cmd[0] = GPCMD_TEST_UNIT_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
Bartlomiej Zolnierkiewiczcdf60002008-02-01 23:09:22 +01001216 /*
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02001217 * Sanyo 3 CD changer uses byte 7 of TEST_UNIT_READY to switch CDs
1218 * instead of supporting the LOAD_UNLOAD opcode.
Bartlomiej Zolnierkiewiczcdf60002008-02-01 23:09:22 +01001219 */
FUJITA Tomonori5f828542008-07-15 21:21:42 +02001220 cmd[7] = cdi->sanyo_slot % 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Harvey Harrison141d3b22008-07-23 19:56:02 +02001222 return ide_cd_queue_pc(drive, cmd, 0, NULL, NULL, sense, 0, REQ_QUIET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223}
1224
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225static int cdrom_read_capacity(ide_drive_t *drive, unsigned long *capacity,
1226 unsigned long *sectors_per_frame,
1227 struct request_sense *sense)
1228{
1229 struct {
Harvey Harrison141d3b22008-07-23 19:56:02 +02001230 __be32 lba;
1231 __be32 blocklen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 } capbuf;
1233
1234 int stat;
FUJITA Tomonori5f828542008-07-15 21:21:42 +02001235 unsigned char cmd[BLK_MAX_CDB];
1236 unsigned len = sizeof(capbuf);
Petr Tesarik938bb032008-08-05 18:17:02 +02001237 u32 blocklen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Borislav Petkovfc8323f2008-10-13 21:39:48 +02001239 ide_debug_log(IDE_DBG_FUNC, "Call %s\n", __func__);
1240
FUJITA Tomonori5f828542008-07-15 21:21:42 +02001241 memset(cmd, 0, BLK_MAX_CDB);
1242 cmd[0] = GPCMD_READ_CDVD_CAPACITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
FUJITA Tomonori5f828542008-07-15 21:21:42 +02001244 stat = ide_cd_queue_pc(drive, cmd, 0, &capbuf, &len, sense, 0,
1245 REQ_QUIET);
Jens Axboee8e7b9e2008-07-24 22:53:35 +02001246 if (stat)
1247 return stat;
1248
1249 /*
1250 * Sanity check the given block size
1251 */
Petr Tesarik938bb032008-08-05 18:17:02 +02001252 blocklen = be32_to_cpu(capbuf.blocklen);
1253 switch (blocklen) {
1254 case 512:
1255 case 1024:
1256 case 2048:
1257 case 4096:
Jens Axboee8e7b9e2008-07-24 22:53:35 +02001258 break;
1259 default:
Borislav Petkovfc8323f2008-10-13 21:39:48 +02001260 printk(KERN_ERR PFX "%s: weird block size %u\n",
1261 drive->name, blocklen);
1262 printk(KERN_ERR PFX "%s: default to 2kb block size\n",
1263 drive->name);
Petr Tesarik938bb032008-08-05 18:17:02 +02001264 blocklen = 2048;
Jens Axboee8e7b9e2008-07-24 22:53:35 +02001265 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 }
1267
Jens Axboee8e7b9e2008-07-24 22:53:35 +02001268 *capacity = 1 + be32_to_cpu(capbuf.lba);
Petr Tesarik938bb032008-08-05 18:17:02 +02001269 *sectors_per_frame = blocklen >> SECTOR_BITS;
Borislav Petkov71b429ca2008-10-17 18:09:14 +02001270
1271 ide_debug_log(IDE_DBG_PROBE, "%s: cap: %lu, sectors_per_frame: %lu\n",
1272 __func__, *capacity, *sectors_per_frame);
1273
Jens Axboee8e7b9e2008-07-24 22:53:35 +02001274 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275}
1276
1277static int cdrom_read_tocentry(ide_drive_t *drive, int trackno, int msf_flag,
1278 int format, char *buf, int buflen,
1279 struct request_sense *sense)
1280{
FUJITA Tomonori5f828542008-07-15 21:21:42 +02001281 unsigned char cmd[BLK_MAX_CDB];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
Borislav Petkovfc8323f2008-10-13 21:39:48 +02001283 ide_debug_log(IDE_DBG_FUNC, "Call %s\n", __func__);
1284
FUJITA Tomonori5f828542008-07-15 21:21:42 +02001285 memset(cmd, 0, BLK_MAX_CDB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
FUJITA Tomonori5f828542008-07-15 21:21:42 +02001287 cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
1288 cmd[6] = trackno;
1289 cmd[7] = (buflen >> 8);
1290 cmd[8] = (buflen & 0xff);
1291 cmd[9] = (format << 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
1293 if (msf_flag)
FUJITA Tomonori5f828542008-07-15 21:21:42 +02001294 cmd[1] = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
FUJITA Tomonori5f828542008-07-15 21:21:42 +02001296 return ide_cd_queue_pc(drive, cmd, 0, buf, &buflen, sense, 0, REQ_QUIET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297}
1298
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299/* Try to read the entire TOC for the disk into our internal buffer. */
Bartlomiej Zolnierkiewicz17802992008-02-01 23:09:25 +01001300int ide_cd_read_toc(ide_drive_t *drive, struct request_sense *sense)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301{
1302 int stat, ntracks, i;
1303 struct cdrom_info *info = drive->driver_data;
1304 struct cdrom_device_info *cdi = &info->devinfo;
1305 struct atapi_toc *toc = info->toc;
1306 struct {
1307 struct atapi_toc_header hdr;
1308 struct atapi_toc_entry ent;
1309 } ms_tmp;
1310 long last_written;
1311 unsigned long sectors_per_frame = SECTORS_PER_FRAME;
1312
Borislav Petkovfc8323f2008-10-13 21:39:48 +02001313 ide_debug_log(IDE_DBG_FUNC, "Call %s\n", __func__);
1314
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 if (toc == NULL) {
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02001316 /* try to allocate space */
Jesper Juhl2a91f3e2005-10-30 15:02:45 -08001317 toc = kmalloc(sizeof(struct atapi_toc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 if (toc == NULL) {
Borislav Petkovfc8323f2008-10-13 21:39:48 +02001319 printk(KERN_ERR PFX "%s: No cdrom TOC buffer!\n",
Borislav Petkov83c85652008-04-26 22:25:15 +02001320 drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 return -ENOMEM;
1322 }
Jesper Juhl2a91f3e2005-10-30 15:02:45 -08001323 info->toc = toc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 }
1325
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02001326 /*
1327 * Check to see if the existing data is still valid. If it is,
1328 * just return.
1329 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 (void) cdrom_check_status(drive, sense);
1331
Borislav Petkov570f89e2008-07-23 19:56:02 +02001332 if (drive->atapi_flags & IDE_AFLAG_TOC_VALID)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 return 0;
1334
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02001335 /* try to get the total cdrom capacity and sector size */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 stat = cdrom_read_capacity(drive, &toc->capacity, &sectors_per_frame,
1337 sense);
1338 if (stat)
1339 toc->capacity = 0x1fffff;
1340
1341 set_capacity(info->disk, toc->capacity * sectors_per_frame);
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02001342 /* save a private copy of the TOC capacity for error handling */
Alan Coxdbe217a2006-06-25 05:47:44 -07001343 drive->probed_capacity = toc->capacity * sectors_per_frame;
1344
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 blk_queue_hardsect_size(drive->queue,
1346 sectors_per_frame << SECTOR_BITS);
1347
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02001348 /* first read just the header, so we know how long the TOC is */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 stat = cdrom_read_tocentry(drive, 0, 1, 0, (char *) &toc->hdr,
1350 sizeof(struct atapi_toc_header), sense);
Jesper Juhl2a91f3e2005-10-30 15:02:45 -08001351 if (stat)
1352 return stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
Borislav Petkov570f89e2008-07-23 19:56:02 +02001354 if (drive->atapi_flags & IDE_AFLAG_TOCTRACKS_AS_BCD) {
Adrian Bunkfc99856a2008-08-18 21:40:04 +02001355 toc->hdr.first_track = bcd2bin(toc->hdr.first_track);
1356 toc->hdr.last_track = bcd2bin(toc->hdr.last_track);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
1359 ntracks = toc->hdr.last_track - toc->hdr.first_track + 1;
1360 if (ntracks <= 0)
1361 return -EIO;
1362 if (ntracks > MAX_TRACKS)
1363 ntracks = MAX_TRACKS;
1364
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02001365 /* now read the whole schmeer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 stat = cdrom_read_tocentry(drive, toc->hdr.first_track, 1, 0,
1367 (char *)&toc->hdr,
1368 sizeof(struct atapi_toc_header) +
1369 (ntracks + 1) *
1370 sizeof(struct atapi_toc_entry), sense);
1371
1372 if (stat && toc->hdr.first_track > 1) {
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02001373 /*
1374 * Cds with CDI tracks only don't have any TOC entries, despite
1375 * of this the returned values are
1376 * first_track == last_track = number of CDI tracks + 1,
1377 * so that this case is indistinguishable from the same layout
1378 * plus an additional audio track. If we get an error for the
1379 * regular case, we assume a CDI without additional audio
1380 * tracks. In this case the readable TOC is empty (CDI tracks
1381 * are not included) and only holds the Leadout entry.
1382 *
1383 * Heiko Eißfeldt.
1384 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 ntracks = 0;
1386 stat = cdrom_read_tocentry(drive, CDROM_LEADOUT, 1, 0,
1387 (char *)&toc->hdr,
1388 sizeof(struct atapi_toc_header) +
1389 (ntracks + 1) *
1390 sizeof(struct atapi_toc_entry),
1391 sense);
Bartlomiej Zolnierkiewiczcdf60002008-02-01 23:09:22 +01001392 if (stat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 return stat;
Bartlomiej Zolnierkiewiczcdf60002008-02-01 23:09:22 +01001394
Borislav Petkov570f89e2008-07-23 19:56:02 +02001395 if (drive->atapi_flags & IDE_AFLAG_TOCTRACKS_AS_BCD) {
Adrian Bunkfc99856a2008-08-18 21:40:04 +02001396 toc->hdr.first_track = (u8)bin2bcd(CDROM_LEADOUT);
1397 toc->hdr.last_track = (u8)bin2bcd(CDROM_LEADOUT);
Bartlomiej Zolnierkiewiczcdf60002008-02-01 23:09:22 +01001398 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 toc->hdr.first_track = CDROM_LEADOUT;
1400 toc->hdr.last_track = CDROM_LEADOUT;
1401 }
1402 }
1403
1404 if (stat)
1405 return stat;
1406
Borislav Petkov7eb43fd2008-02-11 00:32:13 +01001407 toc->hdr.toc_length = be16_to_cpu(toc->hdr.toc_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
Borislav Petkov570f89e2008-07-23 19:56:02 +02001409 if (drive->atapi_flags & IDE_AFLAG_TOCTRACKS_AS_BCD) {
Adrian Bunkfc99856a2008-08-18 21:40:04 +02001410 toc->hdr.first_track = bcd2bin(toc->hdr.first_track);
1411 toc->hdr.last_track = bcd2bin(toc->hdr.last_track);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
Bartlomiej Zolnierkiewiczcdf60002008-02-01 23:09:22 +01001414 for (i = 0; i <= ntracks; i++) {
Borislav Petkov570f89e2008-07-23 19:56:02 +02001415 if (drive->atapi_flags & IDE_AFLAG_TOCADDR_AS_BCD) {
1416 if (drive->atapi_flags & IDE_AFLAG_TOCTRACKS_AS_BCD)
Adrian Bunkfc99856a2008-08-18 21:40:04 +02001417 toc->ent[i].track = bcd2bin(toc->ent[i].track);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 msf_from_bcd(&toc->ent[i].addr.msf);
1419 }
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +02001420 toc->ent[i].addr.lba = msf_to_lba(toc->ent[i].addr.msf.minute,
1421 toc->ent[i].addr.msf.second,
1422 toc->ent[i].addr.msf.frame);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 }
1424
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 if (toc->hdr.first_track != CDROM_LEADOUT) {
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02001426 /* read the multisession information */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 stat = cdrom_read_tocentry(drive, 0, 0, 1, (char *)&ms_tmp,
1428 sizeof(ms_tmp), sense);
Jesper Juhl2a91f3e2005-10-30 15:02:45 -08001429 if (stat)
1430 return stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
1432 toc->last_session_lba = be32_to_cpu(ms_tmp.ent.addr.lba);
1433 } else {
Borislav Petkove5e076a2008-04-26 22:25:15 +02001434 ms_tmp.hdr.last_track = CDROM_LEADOUT;
1435 ms_tmp.hdr.first_track = ms_tmp.hdr.last_track;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 toc->last_session_lba = msf_to_lba(0, 2, 0); /* 0m 2s 0f */
1437 }
1438
Borislav Petkov570f89e2008-07-23 19:56:02 +02001439 if (drive->atapi_flags & IDE_AFLAG_TOCADDR_AS_BCD) {
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02001440 /* re-read multisession information using MSF format */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 stat = cdrom_read_tocentry(drive, 0, 1, 1, (char *)&ms_tmp,
1442 sizeof(ms_tmp), sense);
1443 if (stat)
1444 return stat;
1445
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +02001446 msf_from_bcd(&ms_tmp.ent.addr.msf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 toc->last_session_lba = msf_to_lba(ms_tmp.ent.addr.msf.minute,
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +02001448 ms_tmp.ent.addr.msf.second,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 ms_tmp.ent.addr.msf.frame);
1450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
1452 toc->xa_flag = (ms_tmp.hdr.first_track != ms_tmp.hdr.last_track);
1453
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02001454 /* now try to get the total cdrom capacity */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 stat = cdrom_get_last_written(cdi, &last_written);
1456 if (!stat && (last_written > toc->capacity)) {
1457 toc->capacity = last_written;
1458 set_capacity(info->disk, toc->capacity * sectors_per_frame);
Alan Coxdbe217a2006-06-25 05:47:44 -07001459 drive->probed_capacity = toc->capacity * sectors_per_frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 }
1461
1462 /* Remember that we've read this stuff. */
Borislav Petkov570f89e2008-07-23 19:56:02 +02001463 drive->atapi_flags |= IDE_AFLAG_TOC_VALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
1465 return 0;
1466}
1467
Bartlomiej Zolnierkiewicz17802992008-02-01 23:09:25 +01001468int ide_cdrom_get_capabilities(ide_drive_t *drive, u8 *buf)
Eric Piel9235e682005-06-23 00:10:29 -07001469{
1470 struct cdrom_info *info = drive->driver_data;
1471 struct cdrom_device_info *cdi = &info->devinfo;
1472 struct packet_command cgc;
Bartlomiej Zolnierkiewicz455d80a2008-02-01 23:09:21 +01001473 int stat, attempts = 3, size = ATAPI_CAPABILITIES_PAGE_SIZE;
Eric Piel9235e682005-06-23 00:10:29 -07001474
Borislav Petkovfc8323f2008-10-13 21:39:48 +02001475 ide_debug_log(IDE_DBG_FUNC, "Call %s\n", __func__);
1476
Borislav Petkov570f89e2008-07-23 19:56:02 +02001477 if ((drive->atapi_flags & IDE_AFLAG_FULL_CAPS_PAGE) == 0)
Bartlomiej Zolnierkiewicz455d80a2008-02-01 23:09:21 +01001478 size -= ATAPI_CAPABILITIES_PAGE_PAD_SIZE;
Eric Piel9235e682005-06-23 00:10:29 -07001479
Bartlomiej Zolnierkiewicz455d80a2008-02-01 23:09:21 +01001480 init_cdrom_command(&cgc, buf, size, CGC_DATA_UNKNOWN);
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02001481 do {
1482 /* we seem to get stat=0x01,err=0x00 the first time (??) */
Eric Piel9235e682005-06-23 00:10:29 -07001483 stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CAPABILITIES_PAGE, 0);
1484 if (!stat)
1485 break;
1486 } while (--attempts);
1487 return stat;
1488}
1489
Bartlomiej Zolnierkiewicz17802992008-02-01 23:09:25 +01001490void ide_cdrom_update_speed(ide_drive_t *drive, u8 *buf)
Eric Piel9235e682005-06-23 00:10:29 -07001491{
Bartlomiej Zolnierkiewicz4fe67172008-02-01 23:09:21 +01001492 struct cdrom_info *cd = drive->driver_data;
Bartlomiej Zolnierkiewicz481c8c62008-02-01 23:09:20 +01001493 u16 curspeed, maxspeed;
1494
Borislav Petkovfc8323f2008-10-13 21:39:48 +02001495 ide_debug_log(IDE_DBG_FUNC, "Call %s\n", __func__);
1496
Borislav Petkov570f89e2008-07-23 19:56:02 +02001497 if (drive->atapi_flags & IDE_AFLAG_LE_SPEED_FIELDS) {
Harvey Harrison141d3b22008-07-23 19:56:02 +02001498 curspeed = le16_to_cpup((__le16 *)&buf[8 + 14]);
1499 maxspeed = le16_to_cpup((__le16 *)&buf[8 + 8]);
Eric Piel9235e682005-06-23 00:10:29 -07001500 } else {
Harvey Harrison141d3b22008-07-23 19:56:02 +02001501 curspeed = be16_to_cpup((__be16 *)&buf[8 + 14]);
1502 maxspeed = be16_to_cpup((__be16 *)&buf[8 + 8]);
Eric Piel9235e682005-06-23 00:10:29 -07001503 }
Bartlomiej Zolnierkiewicz481c8c62008-02-01 23:09:20 +01001504
Borislav Petkov71b429ca2008-10-17 18:09:14 +02001505 ide_debug_log(IDE_DBG_PROBE, "%s: curspeed: %u, maxspeed: %u\n",
1506 __func__, curspeed, maxspeed);
1507
Bartlomiej Zolnierkiewicz2bc4cf22008-02-01 23:09:22 +01001508 cd->current_speed = (curspeed + (176/2)) / 176;
1509 cd->max_speed = (maxspeed + (176/2)) / 176;
Eric Piel9235e682005-06-23 00:10:29 -07001510}
1511
Bartlomiej Zolnierkiewicz20e7f7e2008-02-01 23:09:20 +01001512#define IDE_CD_CAPABILITIES \
1513 (CDC_CLOSE_TRAY | CDC_OPEN_TRAY | CDC_LOCK | CDC_SELECT_SPEED | \
1514 CDC_SELECT_DISC | CDC_MULTI_SESSION | CDC_MCN | CDC_MEDIA_CHANGED | \
1515 CDC_PLAY_AUDIO | CDC_RESET | CDC_DRIVE_STATUS | CDC_CD_R | \
1516 CDC_CD_RW | CDC_DVD | CDC_DVD_R | CDC_DVD_RAM | CDC_GENERIC_PACKET | \
1517 CDC_MO_DRIVE | CDC_MRW | CDC_MRW_W | CDC_RAM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519static struct cdrom_device_ops ide_cdrom_dops = {
1520 .open = ide_cdrom_open_real,
1521 .release = ide_cdrom_release_real,
1522 .drive_status = ide_cdrom_drive_status,
1523 .media_changed = ide_cdrom_check_media_change_real,
1524 .tray_move = ide_cdrom_tray_move,
1525 .lock_door = ide_cdrom_lock_door,
1526 .select_speed = ide_cdrom_select_speed,
1527 .get_last_session = ide_cdrom_get_last_session,
1528 .get_mcn = ide_cdrom_get_mcn,
1529 .reset = ide_cdrom_reset,
1530 .audio_ioctl = ide_cdrom_audio_ioctl,
Bartlomiej Zolnierkiewicz20e7f7e2008-02-01 23:09:20 +01001531 .capability = IDE_CD_CAPABILITIES,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 .generic_packet = ide_cdrom_packet,
1533};
1534
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +02001535static int ide_cdrom_register(ide_drive_t *drive, int nslots)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536{
1537 struct cdrom_info *info = drive->driver_data;
1538 struct cdrom_device_info *devinfo = &info->devinfo;
1539
Borislav Petkovfc8323f2008-10-13 21:39:48 +02001540 ide_debug_log(IDE_DBG_PROBE, "Call %s, nslots: %d\n", __func__, nslots);
1541
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 devinfo->ops = &ide_cdrom_dops;
Bartlomiej Zolnierkiewicz2bc4cf22008-02-01 23:09:22 +01001543 devinfo->speed = info->current_speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 devinfo->capacity = nslots;
Jesper Juhl2a91f3e2005-10-30 15:02:45 -08001545 devinfo->handle = drive;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 strcpy(devinfo->name, drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
Borislav Petkov570f89e2008-07-23 19:56:02 +02001548 if (drive->atapi_flags & IDE_AFLAG_NO_SPEED_SELECT)
Bartlomiej Zolnierkiewicz3cbd8142007-12-24 15:23:43 +01001549 devinfo->mask |= CDC_SELECT_SPEED;
1550
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 devinfo->disk = info->disk;
1552 return register_cdrom(devinfo);
1553}
1554
Borislav Petkove5e076a2008-04-26 22:25:15 +02001555static int ide_cdrom_probe_capabilities(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556{
Bartlomiej Zolnierkiewicz4fe67172008-02-01 23:09:21 +01001557 struct cdrom_info *cd = drive->driver_data;
1558 struct cdrom_device_info *cdi = &cd->devinfo;
Bartlomiej Zolnierkiewicz455d80a2008-02-01 23:09:21 +01001559 u8 buf[ATAPI_CAPABILITIES_PAGE_SIZE];
1560 mechtype_t mechtype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 int nslots = 1;
1562
Borislav Petkovfc8323f2008-10-13 21:39:48 +02001563 ide_debug_log(IDE_DBG_PROBE, "Call %s, drive->media: 0x%x, "
1564 "drive->atapi_flags: 0x%lx\n", __func__, drive->media,
1565 drive->atapi_flags);
1566
Bartlomiej Zolnierkiewicz3f1b86d2008-02-01 23:09:20 +01001567 cdi->mask = (CDC_CD_R | CDC_CD_RW | CDC_DVD | CDC_DVD_R |
1568 CDC_DVD_RAM | CDC_SELECT_DISC | CDC_PLAY_AUDIO |
1569 CDC_MO_DRIVE | CDC_RAM);
1570
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 if (drive->media == ide_optical) {
Bartlomiej Zolnierkiewicz3f1b86d2008-02-01 23:09:20 +01001572 cdi->mask &= ~(CDC_MO_DRIVE | CDC_RAM);
Borislav Petkovfc8323f2008-10-13 21:39:48 +02001573 printk(KERN_ERR PFX "%s: ATAPI magneto-optical drive\n",
Borislav Petkov83c85652008-04-26 22:25:15 +02001574 drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 return nslots;
1576 }
1577
Borislav Petkov570f89e2008-07-23 19:56:02 +02001578 if (drive->atapi_flags & IDE_AFLAG_PRE_ATAPI12) {
1579 drive->atapi_flags &= ~IDE_AFLAG_NO_EJECT;
Bartlomiej Zolnierkiewicz3f1b86d2008-02-01 23:09:20 +01001580 cdi->mask &= ~CDC_PLAY_AUDIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 return nslots;
1582 }
1583
1584 /*
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02001585 * We have to cheat a little here. the packet will eventually be queued
1586 * with ide_cdrom_packet(), which extracts the drive from cdi->handle.
1587 * Since this device hasn't been registered with the Uniform layer yet,
1588 * it can't do this. Same goes for cdi->ops.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 */
Jesper Juhl2a91f3e2005-10-30 15:02:45 -08001590 cdi->handle = drive;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 cdi->ops = &ide_cdrom_dops;
1592
Bartlomiej Zolnierkiewicz455d80a2008-02-01 23:09:21 +01001593 if (ide_cdrom_get_capabilities(drive, buf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 return 0;
1595
Bartlomiej Zolnierkiewicz455d80a2008-02-01 23:09:21 +01001596 if ((buf[8 + 6] & 0x01) == 0)
Bartlomiej Zolnierkiewicz42619d32008-10-17 18:09:11 +02001597 drive->dev_flags &= ~IDE_DFLAG_DOORLOCKING;
Bartlomiej Zolnierkiewicz455d80a2008-02-01 23:09:21 +01001598 if (buf[8 + 6] & 0x08)
Borislav Petkov570f89e2008-07-23 19:56:02 +02001599 drive->atapi_flags &= ~IDE_AFLAG_NO_EJECT;
Bartlomiej Zolnierkiewicz455d80a2008-02-01 23:09:21 +01001600 if (buf[8 + 3] & 0x01)
Bartlomiej Zolnierkiewicz3f1b86d2008-02-01 23:09:20 +01001601 cdi->mask &= ~CDC_CD_R;
Bartlomiej Zolnierkiewicz455d80a2008-02-01 23:09:21 +01001602 if (buf[8 + 3] & 0x02)
Bartlomiej Zolnierkiewicz3f1b86d2008-02-01 23:09:20 +01001603 cdi->mask &= ~(CDC_CD_RW | CDC_RAM);
Bartlomiej Zolnierkiewicz455d80a2008-02-01 23:09:21 +01001604 if (buf[8 + 2] & 0x38)
Bartlomiej Zolnierkiewicz3f1b86d2008-02-01 23:09:20 +01001605 cdi->mask &= ~CDC_DVD;
Bartlomiej Zolnierkiewicz455d80a2008-02-01 23:09:21 +01001606 if (buf[8 + 3] & 0x20)
Bartlomiej Zolnierkiewicz3f1b86d2008-02-01 23:09:20 +01001607 cdi->mask &= ~(CDC_DVD_RAM | CDC_RAM);
Bartlomiej Zolnierkiewicz455d80a2008-02-01 23:09:21 +01001608 if (buf[8 + 3] & 0x10)
Bartlomiej Zolnierkiewicz3f1b86d2008-02-01 23:09:20 +01001609 cdi->mask &= ~CDC_DVD_R;
Borislav Petkov570f89e2008-07-23 19:56:02 +02001610 if ((buf[8 + 4] & 0x01) || (drive->atapi_flags & IDE_AFLAG_PLAY_AUDIO_OK))
Bartlomiej Zolnierkiewicz3f1b86d2008-02-01 23:09:20 +01001611 cdi->mask &= ~CDC_PLAY_AUDIO;
Bartlomiej Zolnierkiewicz455d80a2008-02-01 23:09:21 +01001612
1613 mechtype = buf[8 + 6] >> 5;
Borislav Petkovf20f2582008-10-05 18:23:27 +02001614 if (mechtype == mechtype_caddy ||
1615 mechtype == mechtype_popup ||
1616 (drive->atapi_flags & IDE_AFLAG_NO_AUTOCLOSE))
Bartlomiej Zolnierkiewicz3f1b86d2008-02-01 23:09:20 +01001617 cdi->mask |= CDC_CLOSE_TRAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 if (cdi->sanyo_slot > 0) {
Bartlomiej Zolnierkiewicz3f1b86d2008-02-01 23:09:20 +01001620 cdi->mask &= ~CDC_SELECT_DISC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 nslots = 3;
Bartlomiej Zolnierkiewiczcdf60002008-02-01 23:09:22 +01001622 } else if (mechtype == mechtype_individual_changer ||
1623 mechtype == mechtype_cartridge_changer) {
Bartlomiej Zolnierkiewicz2609d062008-02-01 23:09:19 +01001624 nslots = cdrom_number_of_slots(cdi);
1625 if (nslots > 1)
Bartlomiej Zolnierkiewicz3f1b86d2008-02-01 23:09:20 +01001626 cdi->mask &= ~CDC_SELECT_DISC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 }
1628
Bartlomiej Zolnierkiewicz455d80a2008-02-01 23:09:21 +01001629 ide_cdrom_update_speed(drive, buf);
Bartlomiej Zolnierkiewicz4fe67172008-02-01 23:09:21 +01001630
Borislav Petkovfc8323f2008-10-13 21:39:48 +02001631 printk(KERN_INFO PFX "%s: ATAPI", drive->name);
Bartlomiej Zolnierkiewicz4fe67172008-02-01 23:09:21 +01001632
1633 /* don't print speed if the drive reported 0 */
Bartlomiej Zolnierkiewicz2bc4cf22008-02-01 23:09:22 +01001634 if (cd->max_speed)
1635 printk(KERN_CONT " %dX", cd->max_speed);
Bartlomiej Zolnierkiewicz4fe67172008-02-01 23:09:21 +01001636
Bartlomiej Zolnierkiewicz3f1b86d2008-02-01 23:09:20 +01001637 printk(KERN_CONT " %s", (cdi->mask & CDC_DVD) ? "CD-ROM" : "DVD-ROM");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
Bartlomiej Zolnierkiewicz3f1b86d2008-02-01 23:09:20 +01001639 if ((cdi->mask & CDC_DVD_R) == 0 || (cdi->mask & CDC_DVD_RAM) == 0)
1640 printk(KERN_CONT " DVD%s%s",
1641 (cdi->mask & CDC_DVD_R) ? "" : "-R",
Borislav Petkov419a5b62008-10-17 18:09:14 +02001642 (cdi->mask & CDC_DVD_RAM) ? "" : "/RAM");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
Bartlomiej Zolnierkiewicz3f1b86d2008-02-01 23:09:20 +01001644 if ((cdi->mask & CDC_CD_R) == 0 || (cdi->mask & CDC_CD_RW) == 0)
1645 printk(KERN_CONT " CD%s%s",
1646 (cdi->mask & CDC_CD_R) ? "" : "-R",
1647 (cdi->mask & CDC_CD_RW) ? "" : "/RW");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
Bartlomiej Zolnierkiewicz3f1b86d2008-02-01 23:09:20 +01001649 if ((cdi->mask & CDC_SELECT_DISC) == 0)
1650 printk(KERN_CONT " changer w/%d slots", nslots);
1651 else
1652 printk(KERN_CONT " drive");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
Borislav Petkovfc8323f2008-10-13 21:39:48 +02001654 printk(KERN_CONT ", %dkB Cache\n",
1655 be16_to_cpup((__be16 *)&buf[8 + 12]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
1657 return nslots;
1658}
1659
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02001660/* standard prep_rq_fn that builds 10 byte cmds */
Jens Axboe165125e2007-07-24 09:28:11 +02001661static int ide_cdrom_prep_fs(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662{
1663 int hard_sect = queue_hardsect_size(q);
1664 long block = (long)rq->hard_sector / (hard_sect >> 9);
1665 unsigned long blocks = rq->hard_nr_sectors / (hard_sect >> 9);
1666
FUJITA Tomonorid34c87e2008-04-29 14:37:52 +02001667 memset(rq->cmd, 0, BLK_MAX_CDB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
1669 if (rq_data_dir(rq) == READ)
1670 rq->cmd[0] = GPCMD_READ_10;
1671 else
1672 rq->cmd[0] = GPCMD_WRITE_10;
1673
1674 /*
1675 * fill in lba
1676 */
1677 rq->cmd[2] = (block >> 24) & 0xff;
1678 rq->cmd[3] = (block >> 16) & 0xff;
1679 rq->cmd[4] = (block >> 8) & 0xff;
1680 rq->cmd[5] = block & 0xff;
1681
1682 /*
1683 * and transfer length
1684 */
1685 rq->cmd[7] = (blocks >> 8) & 0xff;
1686 rq->cmd[8] = blocks & 0xff;
1687 rq->cmd_len = 10;
1688 return BLKPREP_OK;
1689}
1690
1691/*
1692 * Most of the SCSI commands are supported directly by ATAPI devices.
1693 * This transform handles the few exceptions.
1694 */
1695static int ide_cdrom_prep_pc(struct request *rq)
1696{
1697 u8 *c = rq->cmd;
1698
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02001699 /* transform 6-byte read/write commands to the 10-byte version */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 if (c[0] == READ_6 || c[0] == WRITE_6) {
1701 c[8] = c[4];
1702 c[5] = c[3];
1703 c[4] = c[2];
1704 c[3] = c[1] & 0x1f;
1705 c[2] = 0;
1706 c[1] &= 0xe0;
1707 c[0] += (READ_10 - READ_6);
1708 rq->cmd_len = 10;
1709 return BLKPREP_OK;
1710 }
1711
1712 /*
1713 * it's silly to pretend we understand 6-byte sense commands, just
1714 * reject with ILLEGAL_REQUEST and the caller should take the
1715 * appropriate action
1716 */
1717 if (c[0] == MODE_SENSE || c[0] == MODE_SELECT) {
1718 rq->errors = ILLEGAL_REQUEST;
1719 return BLKPREP_KILL;
1720 }
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +02001721
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 return BLKPREP_OK;
1723}
1724
Jens Axboe165125e2007-07-24 09:28:11 +02001725static int ide_cdrom_prep_fn(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726{
Jens Axboe4aff5e22006-08-10 08:44:47 +02001727 if (blk_fs_request(rq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 return ide_cdrom_prep_fs(q, rq);
Jens Axboe4aff5e22006-08-10 08:44:47 +02001729 else if (blk_pc_request(rq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 return ide_cdrom_prep_pc(rq);
1731
1732 return 0;
1733}
1734
Bartlomiej Zolnierkiewicze59724c2008-02-01 23:09:22 +01001735struct cd_list_entry {
1736 const char *id_model;
1737 const char *id_firmware;
1738 unsigned int cd_flags;
1739};
1740
Borislav Petkov5e657a92008-04-26 22:25:15 +02001741#ifdef CONFIG_IDE_PROC_FS
1742static sector_t ide_cdrom_capacity(ide_drive_t *drive)
1743{
1744 unsigned long capacity, sectors_per_frame;
1745
1746 if (cdrom_read_capacity(drive, &capacity, &sectors_per_frame, NULL))
1747 return 0;
1748
1749 return capacity * sectors_per_frame;
1750}
1751
Borislav Petkove5e076a2008-04-26 22:25:15 +02001752static int proc_idecd_read_capacity(char *page, char **start, off_t off,
1753 int count, int *eof, void *data)
Borislav Petkov5e657a92008-04-26 22:25:15 +02001754{
1755 ide_drive_t *drive = data;
1756 int len;
1757
1758 len = sprintf(page, "%llu\n", (long long)ide_cdrom_capacity(drive));
1759 PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
1760}
1761
1762static ide_proc_entry_t idecd_proc[] = {
1763 { "capacity", S_IFREG|S_IRUGO, proc_idecd_read_capacity, NULL },
1764 { NULL, 0, NULL, NULL }
1765};
1766
Bartlomiej Zolnierkiewicz79cb3802008-10-17 18:09:13 +02001767static ide_proc_entry_t *ide_cd_proc_entries(ide_drive_t *drive)
1768{
1769 return idecd_proc;
1770}
1771
1772static const struct ide_proc_devset *ide_cd_proc_devsets(ide_drive_t *drive)
1773{
Bartlomiej Zolnierkiewicz519d6802008-12-29 20:27:38 +01001774 return NULL;
Bartlomiej Zolnierkiewicz79cb3802008-10-17 18:09:13 +02001775}
Borislav Petkov5e657a92008-04-26 22:25:15 +02001776#endif
1777
Bartlomiej Zolnierkiewicze59724c2008-02-01 23:09:22 +01001778static const struct cd_list_entry ide_cd_quirks_list[] = {
1779 /* Limit transfer size per interrupt. */
Borislav Petkov570f89e2008-07-23 19:56:02 +02001780 { "SAMSUNG CD-ROM SCR-2430", NULL, IDE_AFLAG_LIMIT_NFRAMES },
1781 { "SAMSUNG CD-ROM SCR-2432", NULL, IDE_AFLAG_LIMIT_NFRAMES },
Bartlomiej Zolnierkiewicze59724c2008-02-01 23:09:22 +01001782 /* SCR-3231 doesn't support the SET_CD_SPEED command. */
Borislav Petkov570f89e2008-07-23 19:56:02 +02001783 { "SAMSUNG CD-ROM SCR-3231", NULL, IDE_AFLAG_NO_SPEED_SELECT },
Bartlomiej Zolnierkiewicze59724c2008-02-01 23:09:22 +01001784 /* Old NEC260 (not R) was released before ATAPI 1.2 spec. */
Borislav Petkov570f89e2008-07-23 19:56:02 +02001785 { "NEC CD-ROM DRIVE:260", "1.01", IDE_AFLAG_TOCADDR_AS_BCD |
1786 IDE_AFLAG_PRE_ATAPI12, },
Bartlomiej Zolnierkiewicze59724c2008-02-01 23:09:22 +01001787 /* Vertos 300, some versions of this drive like to talk BCD. */
Borislav Petkov570f89e2008-07-23 19:56:02 +02001788 { "V003S0DS", NULL, IDE_AFLAG_VERTOS_300_SSD, },
Bartlomiej Zolnierkiewicze59724c2008-02-01 23:09:22 +01001789 /* Vertos 600 ESD. */
Borislav Petkov570f89e2008-07-23 19:56:02 +02001790 { "V006E0DS", NULL, IDE_AFLAG_VERTOS_600_ESD, },
Bartlomiej Zolnierkiewicze59724c2008-02-01 23:09:22 +01001791 /*
1792 * Sanyo 3 CD changer uses a non-standard command for CD changing
1793 * (by default standard ATAPI support for CD changers is used).
1794 */
Borislav Petkov570f89e2008-07-23 19:56:02 +02001795 { "CD-ROM CDR-C3 G", NULL, IDE_AFLAG_SANYO_3CD },
1796 { "CD-ROM CDR-C3G", NULL, IDE_AFLAG_SANYO_3CD },
1797 { "CD-ROM CDR_C36", NULL, IDE_AFLAG_SANYO_3CD },
Bartlomiej Zolnierkiewicze59724c2008-02-01 23:09:22 +01001798 /* Stingray 8X CD-ROM. */
Borislav Petkov570f89e2008-07-23 19:56:02 +02001799 { "STINGRAY 8422 IDE 8X CD-ROM 7-27-95", NULL, IDE_AFLAG_PRE_ATAPI12 },
Bartlomiej Zolnierkiewicze59724c2008-02-01 23:09:22 +01001800 /*
1801 * ACER 50X CD-ROM and WPI 32X CD-ROM require the full spec length
1802 * mode sense page capabilities size, but older drives break.
1803 */
Borislav Petkov570f89e2008-07-23 19:56:02 +02001804 { "ATAPI CD ROM DRIVE 50X MAX", NULL, IDE_AFLAG_FULL_CAPS_PAGE },
1805 { "WPI CDS-32X", NULL, IDE_AFLAG_FULL_CAPS_PAGE },
Bartlomiej Zolnierkiewicze59724c2008-02-01 23:09:22 +01001806 /* ACER/AOpen 24X CD-ROM has the speed fields byte-swapped. */
Borislav Petkov570f89e2008-07-23 19:56:02 +02001807 { "", "241N", IDE_AFLAG_LE_SPEED_FIELDS },
Bartlomiej Zolnierkiewicze59724c2008-02-01 23:09:22 +01001808 /*
1809 * Some drives used by Apple don't advertise audio play
1810 * but they do support reading TOC & audio datas.
1811 */
Borislav Petkov570f89e2008-07-23 19:56:02 +02001812 { "MATSHITADVD-ROM SR-8187", NULL, IDE_AFLAG_PLAY_AUDIO_OK },
1813 { "MATSHITADVD-ROM SR-8186", NULL, IDE_AFLAG_PLAY_AUDIO_OK },
1814 { "MATSHITADVD-ROM SR-8176", NULL, IDE_AFLAG_PLAY_AUDIO_OK },
1815 { "MATSHITADVD-ROM SR-8174", NULL, IDE_AFLAG_PLAY_AUDIO_OK },
1816 { "Optiarc DVD RW AD-5200A", NULL, IDE_AFLAG_PLAY_AUDIO_OK },
Bodo Eggertf3e85ee2008-10-05 18:23:28 +02001817 { "Optiarc DVD RW AD-7200A", NULL, IDE_AFLAG_PLAY_AUDIO_OK },
Borislav Petkovf20f2582008-10-05 18:23:27 +02001818 { "Optiarc DVD RW AD-7543A", NULL, IDE_AFLAG_NO_AUTOCLOSE },
Márton Németh64c2eae2008-10-23 23:22:09 +02001819 { "TEAC CD-ROM CD-224E", NULL, IDE_AFLAG_NO_AUTOCLOSE },
Bartlomiej Zolnierkiewicze59724c2008-02-01 23:09:22 +01001820 { NULL, NULL, 0 }
1821};
1822
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +02001823static unsigned int ide_cd_flags(u16 *id)
Bartlomiej Zolnierkiewicze59724c2008-02-01 23:09:22 +01001824{
1825 const struct cd_list_entry *cle = ide_cd_quirks_list;
1826
1827 while (cle->id_model) {
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +02001828 if (strcmp(cle->id_model, (char *)&id[ATA_ID_PROD]) == 0 &&
Bartlomiej Zolnierkiewicze59724c2008-02-01 23:09:22 +01001829 (cle->id_firmware == NULL ||
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +02001830 strstr((char *)&id[ATA_ID_FW_REV], cle->id_firmware)))
Bartlomiej Zolnierkiewicze59724c2008-02-01 23:09:22 +01001831 return cle->cd_flags;
1832 cle++;
1833 }
1834
1835 return 0;
1836}
1837
Borislav Petkove5e076a2008-04-26 22:25:15 +02001838static int ide_cdrom_setup(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839{
Bartlomiej Zolnierkiewicz4fe67172008-02-01 23:09:21 +01001840 struct cdrom_info *cd = drive->driver_data;
1841 struct cdrom_device_info *cdi = &cd->devinfo;
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +02001842 u16 *id = drive->id;
1843 char *fw_rev = (char *)&id[ATA_ID_FW_REV];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 int nslots;
1845
Borislav Petkovfc8323f2008-10-13 21:39:48 +02001846 ide_debug_log(IDE_DBG_PROBE, "Call %s\n", __func__);
1847
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 blk_queue_prep_rq(drive->queue, ide_cdrom_prep_fn);
1849 blk_queue_dma_alignment(drive->queue, 31);
FUJITA Tomonorie5318b52008-07-16 20:33:35 +02001850 blk_queue_update_dma_pad(drive->queue, 15);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 drive->queue->unplug_delay = (1 * HZ) / 1000;
1852 if (!drive->queue->unplug_delay)
1853 drive->queue->unplug_delay = 1;
1854
Bartlomiej Zolnierkiewiczfe11edf2008-10-17 18:09:11 +02001855 drive->dev_flags |= IDE_DFLAG_MEDIA_CHANGED;
1856 drive->atapi_flags = IDE_AFLAG_NO_EJECT | ide_cd_flags(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
Borislav Petkov570f89e2008-07-23 19:56:02 +02001858 if ((drive->atapi_flags & IDE_AFLAG_VERTOS_300_SSD) &&
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +02001859 fw_rev[4] == '1' && fw_rev[6] <= '2')
Borislav Petkov570f89e2008-07-23 19:56:02 +02001860 drive->atapi_flags |= (IDE_AFLAG_TOCTRACKS_AS_BCD |
1861 IDE_AFLAG_TOCADDR_AS_BCD);
1862 else if ((drive->atapi_flags & IDE_AFLAG_VERTOS_600_ESD) &&
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +02001863 fw_rev[4] == '1' && fw_rev[6] <= '2')
Borislav Petkov570f89e2008-07-23 19:56:02 +02001864 drive->atapi_flags |= IDE_AFLAG_TOCTRACKS_AS_BCD;
1865 else if (drive->atapi_flags & IDE_AFLAG_SANYO_3CD)
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02001866 /* 3 => use CD in slot 0 */
1867 cdi->sanyo_slot = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +02001869 nslots = ide_cdrom_probe_capabilities(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02001871 /* set correct block size */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 blk_queue_hardsect_size(drive->queue, CD_FRAMESIZE);
1873
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 if (ide_cdrom_register(drive, nslots)) {
Borislav Petkovfc8323f2008-10-13 21:39:48 +02001875 printk(KERN_ERR PFX "%s: %s failed to register device with the"
Borislav Petkov83c85652008-04-26 22:25:15 +02001876 " cdrom driver.\n", drive->name, __func__);
Bartlomiej Zolnierkiewicz4fe67172008-02-01 23:09:21 +01001877 cd->devinfo.handle = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 return 1;
1879 }
Bartlomiej Zolnierkiewicz1e874f42008-10-10 22:39:27 +02001880
1881 ide_proc_register_driver(drive, cd->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 return 0;
1883}
1884
Russell King4031bbe2006-01-06 11:41:00 +00001885static void ide_cd_remove(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886{
1887 struct cdrom_info *info = drive->driver_data;
1888
Borislav Petkovfc8323f2008-10-13 21:39:48 +02001889 ide_debug_log(IDE_DBG_FUNC, "Call %s\n", __func__);
1890
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001891 ide_proc_unregister_driver(drive, info->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
1893 del_gendisk(info->disk);
1894
1895 ide_cd_put(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896}
1897
1898static void ide_cd_release(struct kref *kref)
1899{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001900 struct cdrom_info *info = to_ide_drv(kref, cdrom_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 struct cdrom_device_info *devinfo = &info->devinfo;
1902 ide_drive_t *drive = info->drive;
1903 struct gendisk *g = info->disk;
1904
Borislav Petkovfc8323f2008-10-13 21:39:48 +02001905 ide_debug_log(IDE_DBG_FUNC, "Call %s\n", __func__);
1906
Jesper Juhl6044ec82005-11-07 01:01:32 -08001907 kfree(info->toc);
Akinobu Mita0a0c4112008-03-26 12:09:02 +01001908 if (devinfo->handle == drive)
1909 unregister_cdrom(devinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 drive->driver_data = NULL;
1911 blk_queue_prep_rq(drive->queue, NULL);
1912 g->private_data = NULL;
1913 put_disk(g);
1914 kfree(info);
1915}
1916
Russell King4031bbe2006-01-06 11:41:00 +00001917static int ide_cd_probe(ide_drive_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919static ide_driver_t ide_cdrom_driver = {
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001920 .gen_driver = {
Laurent Riffard4ef3b8f2005-11-18 22:15:40 +01001921 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001922 .name = "ide-cdrom",
1923 .bus = &ide_bus_type,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001924 },
Russell King4031bbe2006-01-06 11:41:00 +00001925 .probe = ide_cd_probe,
1926 .remove = ide_cd_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 .version = IDECD_VERSION,
Borislav Petkov99384ae2008-07-16 20:33:45 +02001928 .do_request = ide_cd_do_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 .end_request = ide_end_request,
1930 .error = __ide_error,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001931#ifdef CONFIG_IDE_PROC_FS
Bartlomiej Zolnierkiewicz79cb3802008-10-17 18:09:13 +02001932 .proc_entries = ide_cd_proc_entries,
1933 .proc_devsets = ide_cd_proc_devsets,
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +02001934#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935};
1936
Al Viro488ca602008-03-02 10:26:23 -05001937static int idecd_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938{
Al Viro488ca602008-03-02 10:26:23 -05001939 struct cdrom_info *info = ide_cd_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 int rc = -ENOMEM;
1941
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +02001942 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 return -ENXIO;
1944
Al Viro488ca602008-03-02 10:26:23 -05001945 rc = cdrom_open(&info->devinfo, bdev, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
1947 if (rc < 0)
1948 ide_cd_put(info);
1949
1950 return rc;
1951}
1952
Al Viro488ca602008-03-02 10:26:23 -05001953static int idecd_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02001955 struct cdrom_info *info = ide_drv_g(disk, cdrom_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
Al Viro488ca602008-03-02 10:26:23 -05001957 cdrom_release(&info->devinfo, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
1959 ide_cd_put(info);
1960
1961 return 0;
1962}
1963
Christoph Hellwig6a2900b2006-03-23 03:00:15 -08001964static int idecd_set_spindown(struct cdrom_device_info *cdi, unsigned long arg)
1965{
1966 struct packet_command cgc;
1967 char buffer[16];
1968 int stat;
1969 char spindown;
1970
1971 if (copy_from_user(&spindown, (void __user *)arg, sizeof(char)))
1972 return -EFAULT;
1973
1974 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_UNKNOWN);
1975
1976 stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CDROM_PAGE, 0);
1977 if (stat)
1978 return stat;
1979
1980 buffer[11] = (buffer[11] & 0xf0) | (spindown & 0x0f);
1981 return cdrom_mode_select(cdi, &cgc);
1982}
1983
1984static int idecd_get_spindown(struct cdrom_device_info *cdi, unsigned long arg)
1985{
1986 struct packet_command cgc;
1987 char buffer[16];
1988 int stat;
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +02001989 char spindown;
Christoph Hellwig6a2900b2006-03-23 03:00:15 -08001990
1991 init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_UNKNOWN);
1992
1993 stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CDROM_PAGE, 0);
1994 if (stat)
1995 return stat;
1996
1997 spindown = buffer[11] & 0x0f;
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +02001998 if (copy_to_user((void __user *)arg, &spindown, sizeof(char)))
Christoph Hellwig6a2900b2006-03-23 03:00:15 -08001999 return -EFAULT;
2000 return 0;
2001}
2002
Al Viro488ca602008-03-02 10:26:23 -05002003static int idecd_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 unsigned int cmd, unsigned long arg)
2005{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02002006 struct cdrom_info *info = ide_drv_g(bdev->bd_disk, cdrom_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 int err;
2008
Christoph Hellwig6a2900b2006-03-23 03:00:15 -08002009 switch (cmd) {
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +02002010 case CDROMSETSPINDOWN:
Christoph Hellwig6a2900b2006-03-23 03:00:15 -08002011 return idecd_set_spindown(&info->devinfo, arg);
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +02002012 case CDROMGETSPINDOWN:
Christoph Hellwig6a2900b2006-03-23 03:00:15 -08002013 return idecd_get_spindown(&info->devinfo, arg);
2014 default:
2015 break;
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +02002016 }
Christoph Hellwig6a2900b2006-03-23 03:00:15 -08002017
Al Viro1bddd9e2008-09-02 17:19:43 -04002018 err = generic_ide_ioctl(info->drive, bdev, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 if (err == -EINVAL)
Al Viro488ca602008-03-02 10:26:23 -05002020 err = cdrom_ioctl(&info->devinfo, bdev, mode, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
2022 return err;
2023}
2024
2025static int idecd_media_changed(struct gendisk *disk)
2026{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02002027 struct cdrom_info *info = ide_drv_g(disk, cdrom_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 return cdrom_media_changed(&info->devinfo);
2029}
2030
2031static int idecd_revalidate_disk(struct gendisk *disk)
2032{
Borislav Petkov5aeddf92008-10-13 21:39:34 +02002033 struct cdrom_info *info = ide_drv_g(disk, cdrom_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 struct request_sense sense;
Bartlomiej Zolnierkiewicz139c8292008-02-01 23:09:24 +01002035
2036 ide_cd_read_toc(info->drive, &sense);
2037
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 return 0;
2039}
2040
2041static struct block_device_operations idecd_ops = {
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +02002042 .owner = THIS_MODULE,
Al Viro488ca602008-03-02 10:26:23 -05002043 .open = idecd_open,
2044 .release = idecd_release,
2045 .locked_ioctl = idecd_ioctl,
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +02002046 .media_changed = idecd_media_changed,
2047 .revalidate_disk = idecd_revalidate_disk
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048};
2049
Borislav Petkov5a3ea3b2008-04-26 22:25:15 +02002050/* module options */
Paolo Ciarrocchi9ce70fb2008-04-26 17:36:42 +02002051static char *ignore;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052module_param(ignore, charp, 0400);
Borislav Petkov35d9b172008-10-13 21:39:49 +02002053
2054static unsigned long debug_mask;
2055module_param(debug_mask, ulong, 0644);
2056
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057MODULE_DESCRIPTION("ATAPI CD-ROM Driver");
2058
Russell King4031bbe2006-01-06 11:41:00 +00002059static int ide_cd_probe(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060{
2061 struct cdrom_info *info;
2062 struct gendisk *g;
2063 struct request_sense sense;
2064
Borislav Petkovfc8323f2008-10-13 21:39:48 +02002065 ide_debug_log(IDE_DBG_PROBE, "Call %s, drive->driver_req: %s, "
2066 "drive->media: 0x%x\n", __func__, drive->driver_req,
2067 drive->media);
2068
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 if (!strstr("ide-cdrom", drive->driver_req))
2070 goto failed;
Bartlomiej Zolnierkiewicz2a924662008-10-10 22:39:24 +02002071
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 if (drive->media != ide_cdrom && drive->media != ide_optical)
2073 goto failed;
Bartlomiej Zolnierkiewicz2a924662008-10-10 22:39:24 +02002074
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 /* skip drives that we were told to ignore */
2076 if (ignore != NULL) {
2077 if (strstr(ignore, drive->name)) {
Borislav Petkovfc8323f2008-10-13 21:39:48 +02002078 printk(KERN_INFO PFX "ignoring drive %s\n",
Borislav Petkov83c85652008-04-26 22:25:15 +02002079 drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 goto failed;
2081 }
2082 }
Borislav Petkov35d9b172008-10-13 21:39:49 +02002083
2084 drive->debug_mask = debug_mask;
2085
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -08002086 info = kzalloc(sizeof(struct cdrom_info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 if (info == NULL) {
Borislav Petkovfc8323f2008-10-13 21:39:48 +02002088 printk(KERN_ERR PFX "%s: Can't allocate a cdrom structure\n",
Borislav Petkov83c85652008-04-26 22:25:15 +02002089 drive->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 goto failed;
2091 }
2092
2093 g = alloc_disk(1 << PARTN_BITS);
2094 if (!g)
2095 goto out_free_cd;
2096
2097 ide_init_disk(g, drive);
2098
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 kref_init(&info->kref);
2100
2101 info->drive = drive;
2102 info->driver = &ide_cdrom_driver;
2103 info->disk = g;
2104
2105 g->private_data = &info->driver;
2106
2107 drive->driver_data = info;
2108
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 g->minors = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 g->driverfs_dev = &drive->gendev;
2111 g->flags = GENHD_FL_CD | GENHD_FL_REMOVABLE;
2112 if (ide_cdrom_setup(drive)) {
Bartlomiej Zolnierkiewicz05017db2007-12-24 15:23:43 +01002113 ide_cd_release(&info->kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 goto failed;
2115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
Bartlomiej Zolnierkiewicz139c8292008-02-01 23:09:24 +01002117 ide_cd_read_toc(drive, &sense);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 g->fops = &idecd_ops;
2119 g->flags |= GENHD_FL_REMOVABLE;
2120 add_disk(g);
2121 return 0;
2122
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123out_free_cd:
2124 kfree(info);
2125failed:
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002126 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127}
2128
2129static void __exit ide_cdrom_exit(void)
2130{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002131 driver_unregister(&ide_cdrom_driver.gen_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132}
Bartlomiej Zolnierkiewicz17514e82005-11-19 22:24:35 +01002133
2134static int __init ide_cdrom_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135{
Borislav Petkovfc8323f2008-10-13 21:39:48 +02002136 printk(KERN_INFO DRV_NAME " driver " IDECD_VERSION "\n");
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02002137 return driver_register(&ide_cdrom_driver.gen_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138}
2139
Kay Sievers263756e2005-12-12 18:03:44 +01002140MODULE_ALIAS("ide:*m-cdrom*");
Bartlomiej Zolnierkiewicz972560f2008-02-01 23:09:23 +01002141MODULE_ALIAS("ide-cd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142module_init(ide_cdrom_init);
2143module_exit(ide_cdrom_exit);
2144MODULE_LICENSE("GPL");