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