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