Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * sr.c Copyright (C) 1992 David Giller |
| 3 | * Copyright (C) 1993, 1994, 1995, 1999 Eric Youngdale |
| 4 | * |
| 5 | * adapted from: |
| 6 | * sd.c Copyright (C) 1992 Drew Eckhardt |
| 7 | * Linux scsi disk driver by |
| 8 | * Drew Eckhardt <drew@colorado.edu> |
| 9 | * |
| 10 | * Modified by Eric Youngdale ericy@andante.org to |
| 11 | * add scatter-gather, multiple outstanding request, and other |
| 12 | * enhancements. |
| 13 | * |
| 14 | * Modified by Eric Youngdale eric@andante.org to support loadable |
| 15 | * low-level scsi drivers. |
| 16 | * |
| 17 | * Modified by Thomas Quinot thomas@melchior.cuivre.fdn.fr to |
| 18 | * provide auto-eject. |
| 19 | * |
| 20 | * Modified by Gerd Knorr <kraxel@cs.tu-berlin.de> to support the |
| 21 | * generic cdrom interface |
| 22 | * |
| 23 | * Modified by Jens Axboe <axboe@suse.de> - Uniform sr_packet() |
| 24 | * interface, capabilities probe additions, ioctl cleanups, etc. |
| 25 | * |
| 26 | * Modified by Richard Gooch <rgooch@atnf.csiro.au> to support devfs |
| 27 | * |
| 28 | * Modified by Jens Axboe <axboe@suse.de> - support DVD-RAM |
| 29 | * transparently and lose the GHOST hack |
| 30 | * |
| 31 | * Modified by Arnaldo Carvalho de Melo <acme@conectiva.com.br> |
| 32 | * check resource allocation in sr_init and some cleanups |
| 33 | */ |
| 34 | |
| 35 | #include <linux/module.h> |
| 36 | #include <linux/fs.h> |
| 37 | #include <linux/kernel.h> |
| 38 | #include <linux/sched.h> |
| 39 | #include <linux/mm.h> |
| 40 | #include <linux/bio.h> |
| 41 | #include <linux/string.h> |
| 42 | #include <linux/errno.h> |
| 43 | #include <linux/cdrom.h> |
| 44 | #include <linux/interrupt.h> |
| 45 | #include <linux/init.h> |
| 46 | #include <linux/blkdev.h> |
| 47 | #include <asm/uaccess.h> |
| 48 | |
| 49 | #include <scsi/scsi.h> |
| 50 | #include <scsi/scsi_dbg.h> |
| 51 | #include <scsi/scsi_device.h> |
| 52 | #include <scsi/scsi_driver.h> |
James Bottomley | 820732b | 2005-06-12 22:21:29 -0500 | [diff] [blame^] | 53 | #include <scsi/scsi_cmnd.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | #include <scsi/scsi_eh.h> |
| 55 | #include <scsi/scsi_host.h> |
| 56 | #include <scsi/scsi_ioctl.h> /* For the door lock/unlock commands */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
| 58 | #include "scsi_logging.h" |
| 59 | #include "sr.h" |
| 60 | |
| 61 | |
| 62 | #define SR_DISKS 256 |
| 63 | |
| 64 | #define MAX_RETRIES 3 |
| 65 | #define SR_TIMEOUT (30 * HZ) |
| 66 | #define SR_CAPABILITIES \ |
| 67 | (CDC_CLOSE_TRAY|CDC_OPEN_TRAY|CDC_LOCK|CDC_SELECT_SPEED| \ |
| 68 | CDC_SELECT_DISC|CDC_MULTI_SESSION|CDC_MCN|CDC_MEDIA_CHANGED| \ |
| 69 | CDC_PLAY_AUDIO|CDC_RESET|CDC_IOCTLS|CDC_DRIVE_STATUS| \ |
| 70 | CDC_CD_R|CDC_CD_RW|CDC_DVD|CDC_DVD_R|CDC_DVD_RAM|CDC_GENERIC_PACKET| \ |
| 71 | CDC_MRW|CDC_MRW_W|CDC_RAM) |
| 72 | |
| 73 | static int sr_probe(struct device *); |
| 74 | static int sr_remove(struct device *); |
| 75 | static int sr_init_command(struct scsi_cmnd *); |
| 76 | |
| 77 | static struct scsi_driver sr_template = { |
| 78 | .owner = THIS_MODULE, |
| 79 | .gendrv = { |
| 80 | .name = "sr", |
| 81 | .probe = sr_probe, |
| 82 | .remove = sr_remove, |
| 83 | }, |
| 84 | .init_command = sr_init_command, |
| 85 | }; |
| 86 | |
| 87 | static unsigned long sr_index_bits[SR_DISKS / BITS_PER_LONG]; |
| 88 | static DEFINE_SPINLOCK(sr_index_lock); |
| 89 | |
| 90 | /* This semaphore is used to mediate the 0->1 reference get in the |
| 91 | * face of object destruction (i.e. we can't allow a get on an |
| 92 | * object after last put) */ |
| 93 | static DECLARE_MUTEX(sr_ref_sem); |
| 94 | |
| 95 | static int sr_open(struct cdrom_device_info *, int); |
| 96 | static void sr_release(struct cdrom_device_info *); |
| 97 | |
| 98 | static void get_sectorsize(struct scsi_cd *); |
| 99 | static void get_capabilities(struct scsi_cd *); |
| 100 | |
| 101 | static int sr_media_change(struct cdrom_device_info *, int); |
| 102 | static int sr_packet(struct cdrom_device_info *, struct packet_command *); |
| 103 | |
| 104 | static struct cdrom_device_ops sr_dops = { |
| 105 | .open = sr_open, |
| 106 | .release = sr_release, |
| 107 | .drive_status = sr_drive_status, |
| 108 | .media_changed = sr_media_change, |
| 109 | .tray_move = sr_tray_move, |
| 110 | .lock_door = sr_lock_door, |
| 111 | .select_speed = sr_select_speed, |
| 112 | .get_last_session = sr_get_last_session, |
| 113 | .get_mcn = sr_get_mcn, |
| 114 | .reset = sr_reset, |
| 115 | .audio_ioctl = sr_audio_ioctl, |
| 116 | .dev_ioctl = sr_dev_ioctl, |
| 117 | .capability = SR_CAPABILITIES, |
| 118 | .generic_packet = sr_packet, |
| 119 | }; |
| 120 | |
| 121 | static void sr_kref_release(struct kref *kref); |
| 122 | |
| 123 | static inline struct scsi_cd *scsi_cd(struct gendisk *disk) |
| 124 | { |
| 125 | return container_of(disk->private_data, struct scsi_cd, driver); |
| 126 | } |
| 127 | |
| 128 | /* |
| 129 | * The get and put routines for the struct scsi_cd. Note this entity |
| 130 | * has a scsi_device pointer and owns a reference to this. |
| 131 | */ |
| 132 | static inline struct scsi_cd *scsi_cd_get(struct gendisk *disk) |
| 133 | { |
| 134 | struct scsi_cd *cd = NULL; |
| 135 | |
| 136 | down(&sr_ref_sem); |
| 137 | if (disk->private_data == NULL) |
| 138 | goto out; |
| 139 | cd = scsi_cd(disk); |
| 140 | kref_get(&cd->kref); |
| 141 | if (scsi_device_get(cd->device)) |
| 142 | goto out_put; |
| 143 | goto out; |
| 144 | |
| 145 | out_put: |
| 146 | kref_put(&cd->kref, sr_kref_release); |
| 147 | cd = NULL; |
| 148 | out: |
| 149 | up(&sr_ref_sem); |
| 150 | return cd; |
| 151 | } |
| 152 | |
| 153 | static inline void scsi_cd_put(struct scsi_cd *cd) |
| 154 | { |
| 155 | struct scsi_device *sdev = cd->device; |
| 156 | |
| 157 | down(&sr_ref_sem); |
| 158 | kref_put(&cd->kref, sr_kref_release); |
| 159 | scsi_device_put(sdev); |
| 160 | up(&sr_ref_sem); |
| 161 | } |
| 162 | |
| 163 | /* |
| 164 | * This function checks to see if the media has been changed in the |
| 165 | * CDROM drive. It is possible that we have already sensed a change, |
| 166 | * or the drive may have sensed one and not yet reported it. We must |
| 167 | * be ready for either case. This function always reports the current |
| 168 | * value of the changed bit. If flag is 0, then the changed bit is reset. |
| 169 | * This function could be done as an ioctl, but we would need to have |
| 170 | * an inode for that to work, and we do not always have one. |
| 171 | */ |
| 172 | |
| 173 | int sr_media_change(struct cdrom_device_info *cdi, int slot) |
| 174 | { |
| 175 | struct scsi_cd *cd = cdi->handle; |
| 176 | int retval; |
| 177 | |
| 178 | if (CDSL_CURRENT != slot) { |
| 179 | /* no changer support */ |
| 180 | return -EINVAL; |
| 181 | } |
| 182 | |
| 183 | retval = scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES); |
| 184 | if (retval) { |
| 185 | /* Unable to test, unit probably not ready. This usually |
| 186 | * means there is no disc in the drive. Mark as changed, |
| 187 | * and we will figure it out later once the drive is |
| 188 | * available again. */ |
| 189 | cd->device->changed = 1; |
| 190 | return 1; /* This will force a flush, if called from |
| 191 | * check_disk_change */ |
| 192 | }; |
| 193 | |
| 194 | retval = cd->device->changed; |
| 195 | cd->device->changed = 0; |
| 196 | /* If the disk changed, the capacity will now be different, |
| 197 | * so we force a re-read of this information */ |
| 198 | if (retval) { |
| 199 | /* check multisession offset etc */ |
| 200 | sr_cd_check(cdi); |
| 201 | |
| 202 | /* |
| 203 | * If the disk changed, the capacity will now be different, |
| 204 | * so we force a re-read of this information |
| 205 | * Force 2048 for the sector size so that filesystems won't |
| 206 | * be trying to use something that is too small if the disc |
| 207 | * has changed. |
| 208 | */ |
| 209 | cd->needs_sector_size = 1; |
| 210 | cd->device->sector_size = 2048; |
| 211 | } |
| 212 | return retval; |
| 213 | } |
| 214 | |
| 215 | /* |
| 216 | * rw_intr is the interrupt routine for the device driver. |
| 217 | * |
| 218 | * It will be notified on the end of a SCSI read / write, and will take on |
| 219 | * of several actions based on success or failure. |
| 220 | */ |
| 221 | static void rw_intr(struct scsi_cmnd * SCpnt) |
| 222 | { |
| 223 | int result = SCpnt->result; |
| 224 | int this_count = SCpnt->bufflen; |
| 225 | int good_bytes = (result == 0 ? this_count : 0); |
| 226 | int block_sectors = 0; |
| 227 | long error_sector; |
| 228 | struct scsi_cd *cd = scsi_cd(SCpnt->request->rq_disk); |
| 229 | |
| 230 | #ifdef DEBUG |
| 231 | printk("sr.c done: %x\n", result); |
| 232 | #endif |
| 233 | |
| 234 | /* |
| 235 | * Handle MEDIUM ERRORs or VOLUME OVERFLOWs that indicate partial |
| 236 | * success. Since this is a relatively rare error condition, no |
| 237 | * care is taken to avoid unnecessary additional work such as |
| 238 | * memcpy's that could be avoided. |
| 239 | */ |
| 240 | if (driver_byte(result) != 0 && /* An error occurred */ |
| 241 | (SCpnt->sense_buffer[0] & 0x7f) == 0x70) { /* Sense current */ |
| 242 | switch (SCpnt->sense_buffer[2]) { |
| 243 | case MEDIUM_ERROR: |
| 244 | case VOLUME_OVERFLOW: |
| 245 | case ILLEGAL_REQUEST: |
| 246 | if (!(SCpnt->sense_buffer[0] & 0x90)) |
| 247 | break; |
| 248 | if (!blk_fs_request(SCpnt->request)) |
| 249 | break; |
| 250 | error_sector = (SCpnt->sense_buffer[3] << 24) | |
| 251 | (SCpnt->sense_buffer[4] << 16) | |
| 252 | (SCpnt->sense_buffer[5] << 8) | |
| 253 | SCpnt->sense_buffer[6]; |
| 254 | if (SCpnt->request->bio != NULL) |
| 255 | block_sectors = |
| 256 | bio_sectors(SCpnt->request->bio); |
| 257 | if (block_sectors < 4) |
| 258 | block_sectors = 4; |
| 259 | if (cd->device->sector_size == 2048) |
| 260 | error_sector <<= 2; |
| 261 | error_sector &= ~(block_sectors - 1); |
| 262 | good_bytes = (error_sector - SCpnt->request->sector) << 9; |
| 263 | if (good_bytes < 0 || good_bytes >= this_count) |
| 264 | good_bytes = 0; |
| 265 | /* |
| 266 | * The SCSI specification allows for the value |
| 267 | * returned by READ CAPACITY to be up to 75 2K |
| 268 | * sectors past the last readable block. |
| 269 | * Therefore, if we hit a medium error within the |
| 270 | * last 75 2K sectors, we decrease the saved size |
| 271 | * value. |
| 272 | */ |
| 273 | if (error_sector < get_capacity(cd->disk) && |
| 274 | cd->capacity - error_sector < 4 * 75) |
| 275 | set_capacity(cd->disk, error_sector); |
| 276 | break; |
| 277 | |
| 278 | case RECOVERED_ERROR: |
| 279 | |
| 280 | /* |
| 281 | * An error occured, but it recovered. Inform the |
| 282 | * user, but make sure that it's not treated as a |
| 283 | * hard error. |
| 284 | */ |
| 285 | scsi_print_sense("sr", SCpnt); |
| 286 | SCpnt->result = 0; |
| 287 | SCpnt->sense_buffer[0] = 0x0; |
| 288 | good_bytes = this_count; |
| 289 | break; |
| 290 | |
| 291 | default: |
| 292 | break; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | /* |
| 297 | * This calls the generic completion function, now that we know |
| 298 | * how many actual sectors finished, and how many sectors we need |
| 299 | * to say have failed. |
| 300 | */ |
| 301 | scsi_io_completion(SCpnt, good_bytes, block_sectors << 9); |
| 302 | } |
| 303 | |
| 304 | static int sr_init_command(struct scsi_cmnd * SCpnt) |
| 305 | { |
| 306 | int block=0, this_count, s_size, timeout = SR_TIMEOUT; |
| 307 | struct scsi_cd *cd = scsi_cd(SCpnt->request->rq_disk); |
| 308 | |
| 309 | SCSI_LOG_HLQUEUE(1, printk("Doing sr request, dev = %s, block = %d\n", |
| 310 | cd->disk->disk_name, block)); |
| 311 | |
| 312 | if (!cd->device || !scsi_device_online(cd->device)) { |
| 313 | SCSI_LOG_HLQUEUE(2, printk("Finishing %ld sectors\n", |
| 314 | SCpnt->request->nr_sectors)); |
| 315 | SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt)); |
| 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | if (cd->device->changed) { |
| 320 | /* |
| 321 | * quietly refuse to do anything to a changed disc until the |
| 322 | * changed bit has been reset |
| 323 | */ |
| 324 | return 0; |
| 325 | } |
| 326 | |
| 327 | /* |
| 328 | * these are already setup, just copy cdb basically |
| 329 | */ |
| 330 | if (SCpnt->request->flags & REQ_BLOCK_PC) { |
| 331 | struct request *rq = SCpnt->request; |
| 332 | |
| 333 | if (sizeof(rq->cmd) > sizeof(SCpnt->cmnd)) |
| 334 | return 0; |
| 335 | |
| 336 | memcpy(SCpnt->cmnd, rq->cmd, sizeof(SCpnt->cmnd)); |
| 337 | if (!rq->data_len) |
| 338 | SCpnt->sc_data_direction = DMA_NONE; |
| 339 | else if (rq_data_dir(rq) == WRITE) |
| 340 | SCpnt->sc_data_direction = DMA_TO_DEVICE; |
| 341 | else |
| 342 | SCpnt->sc_data_direction = DMA_FROM_DEVICE; |
| 343 | |
| 344 | this_count = rq->data_len; |
| 345 | if (rq->timeout) |
| 346 | timeout = rq->timeout; |
| 347 | |
| 348 | SCpnt->transfersize = rq->data_len; |
| 349 | goto queue; |
| 350 | } |
| 351 | |
| 352 | if (!(SCpnt->request->flags & REQ_CMD)) { |
| 353 | blk_dump_rq_flags(SCpnt->request, "sr unsup command"); |
| 354 | return 0; |
| 355 | } |
| 356 | |
| 357 | /* |
| 358 | * we do lazy blocksize switching (when reading XA sectors, |
| 359 | * see CDROMREADMODE2 ioctl) |
| 360 | */ |
| 361 | s_size = cd->device->sector_size; |
| 362 | if (s_size > 2048) { |
| 363 | if (!in_interrupt()) |
| 364 | sr_set_blocklength(cd, 2048); |
| 365 | else |
| 366 | printk("sr: can't switch blocksize: in interrupt\n"); |
| 367 | } |
| 368 | |
| 369 | if (s_size != 512 && s_size != 1024 && s_size != 2048) { |
| 370 | printk("sr: bad sector size %d\n", s_size); |
| 371 | return 0; |
| 372 | } |
| 373 | |
| 374 | if (rq_data_dir(SCpnt->request) == WRITE) { |
| 375 | if (!cd->device->writeable) |
| 376 | return 0; |
| 377 | SCpnt->cmnd[0] = WRITE_10; |
| 378 | SCpnt->sc_data_direction = DMA_TO_DEVICE; |
| 379 | cd->cdi.media_written = 1; |
| 380 | } else if (rq_data_dir(SCpnt->request) == READ) { |
| 381 | SCpnt->cmnd[0] = READ_10; |
| 382 | SCpnt->sc_data_direction = DMA_FROM_DEVICE; |
| 383 | } else { |
| 384 | blk_dump_rq_flags(SCpnt->request, "Unknown sr command"); |
| 385 | return 0; |
| 386 | } |
| 387 | |
| 388 | { |
| 389 | struct scatterlist *sg = SCpnt->request_buffer; |
| 390 | int i, size = 0; |
| 391 | for (i = 0; i < SCpnt->use_sg; i++) |
| 392 | size += sg[i].length; |
| 393 | |
| 394 | if (size != SCpnt->request_bufflen && SCpnt->use_sg) { |
| 395 | printk(KERN_ERR "sr: mismatch count %d, bytes %d\n", |
| 396 | size, SCpnt->request_bufflen); |
| 397 | if (SCpnt->request_bufflen > size) |
| 398 | SCpnt->request_bufflen = SCpnt->bufflen = size; |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | /* |
| 403 | * request doesn't start on hw block boundary, add scatter pads |
| 404 | */ |
| 405 | if (((unsigned int)SCpnt->request->sector % (s_size >> 9)) || |
| 406 | (SCpnt->request_bufflen % s_size)) { |
| 407 | printk("sr: unaligned transfer\n"); |
| 408 | return 0; |
| 409 | } |
| 410 | |
| 411 | this_count = (SCpnt->request_bufflen >> 9) / (s_size >> 9); |
| 412 | |
| 413 | |
| 414 | SCSI_LOG_HLQUEUE(2, printk("%s : %s %d/%ld 512 byte blocks.\n", |
| 415 | cd->cdi.name, |
| 416 | (rq_data_dir(SCpnt->request) == WRITE) ? |
| 417 | "writing" : "reading", |
| 418 | this_count, SCpnt->request->nr_sectors)); |
| 419 | |
| 420 | SCpnt->cmnd[1] = 0; |
| 421 | block = (unsigned int)SCpnt->request->sector / (s_size >> 9); |
| 422 | |
| 423 | if (this_count > 0xffff) { |
| 424 | this_count = 0xffff; |
| 425 | SCpnt->request_bufflen = SCpnt->bufflen = |
| 426 | this_count * s_size; |
| 427 | } |
| 428 | |
| 429 | SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff; |
| 430 | SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff; |
| 431 | SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff; |
| 432 | SCpnt->cmnd[5] = (unsigned char) block & 0xff; |
| 433 | SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0; |
| 434 | SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff; |
| 435 | SCpnt->cmnd[8] = (unsigned char) this_count & 0xff; |
| 436 | |
| 437 | /* |
| 438 | * We shouldn't disconnect in the middle of a sector, so with a dumb |
| 439 | * host adapter, it's safe to assume that we can at least transfer |
| 440 | * this many bytes between each connect / disconnect. |
| 441 | */ |
| 442 | SCpnt->transfersize = cd->device->sector_size; |
| 443 | SCpnt->underflow = this_count << 9; |
| 444 | |
| 445 | queue: |
| 446 | SCpnt->allowed = MAX_RETRIES; |
| 447 | SCpnt->timeout_per_command = timeout; |
| 448 | |
| 449 | /* |
| 450 | * This is the completion routine we use. This is matched in terms |
| 451 | * of capability to this function. |
| 452 | */ |
| 453 | SCpnt->done = rw_intr; |
| 454 | |
| 455 | /* |
| 456 | * This indicates that the command is ready from our end to be |
| 457 | * queued. |
| 458 | */ |
| 459 | return 1; |
| 460 | } |
| 461 | |
| 462 | static int sr_block_open(struct inode *inode, struct file *file) |
| 463 | { |
| 464 | struct gendisk *disk = inode->i_bdev->bd_disk; |
| 465 | struct scsi_cd *cd = scsi_cd(inode->i_bdev->bd_disk); |
| 466 | int ret = 0; |
| 467 | |
| 468 | if(!(cd = scsi_cd_get(disk))) |
| 469 | return -ENXIO; |
| 470 | |
| 471 | if((ret = cdrom_open(&cd->cdi, inode, file)) != 0) |
| 472 | scsi_cd_put(cd); |
| 473 | |
| 474 | return ret; |
| 475 | } |
| 476 | |
| 477 | static int sr_block_release(struct inode *inode, struct file *file) |
| 478 | { |
| 479 | int ret; |
| 480 | struct scsi_cd *cd = scsi_cd(inode->i_bdev->bd_disk); |
| 481 | ret = cdrom_release(&cd->cdi, file); |
| 482 | if(ret) |
| 483 | return ret; |
| 484 | |
| 485 | scsi_cd_put(cd); |
| 486 | |
| 487 | return 0; |
| 488 | } |
| 489 | |
| 490 | static int sr_block_ioctl(struct inode *inode, struct file *file, unsigned cmd, |
| 491 | unsigned long arg) |
| 492 | { |
| 493 | struct scsi_cd *cd = scsi_cd(inode->i_bdev->bd_disk); |
| 494 | struct scsi_device *sdev = cd->device; |
| 495 | |
| 496 | /* |
| 497 | * Send SCSI addressing ioctls directly to mid level, send other |
| 498 | * ioctls to cdrom/block level. |
| 499 | */ |
| 500 | switch (cmd) { |
| 501 | case SCSI_IOCTL_GET_IDLUN: |
| 502 | case SCSI_IOCTL_GET_BUS_NUMBER: |
| 503 | return scsi_ioctl(sdev, cmd, (void __user *)arg); |
| 504 | } |
| 505 | return cdrom_ioctl(file, &cd->cdi, inode, cmd, arg); |
| 506 | } |
| 507 | |
| 508 | static int sr_block_media_changed(struct gendisk *disk) |
| 509 | { |
| 510 | struct scsi_cd *cd = scsi_cd(disk); |
| 511 | return cdrom_media_changed(&cd->cdi); |
| 512 | } |
| 513 | |
| 514 | static struct block_device_operations sr_bdops = |
| 515 | { |
| 516 | .owner = THIS_MODULE, |
| 517 | .open = sr_block_open, |
| 518 | .release = sr_block_release, |
| 519 | .ioctl = sr_block_ioctl, |
| 520 | .media_changed = sr_block_media_changed, |
| 521 | /* |
| 522 | * No compat_ioctl for now because sr_block_ioctl never |
| 523 | * seems to pass arbitary ioctls down to host drivers. |
| 524 | */ |
| 525 | }; |
| 526 | |
| 527 | static int sr_open(struct cdrom_device_info *cdi, int purpose) |
| 528 | { |
| 529 | struct scsi_cd *cd = cdi->handle; |
| 530 | struct scsi_device *sdev = cd->device; |
| 531 | int retval; |
| 532 | |
| 533 | /* |
| 534 | * If the device is in error recovery, wait until it is done. |
| 535 | * If the device is offline, then disallow any access to it. |
| 536 | */ |
| 537 | retval = -ENXIO; |
| 538 | if (!scsi_block_when_processing_errors(sdev)) |
| 539 | goto error_out; |
| 540 | |
| 541 | /* |
| 542 | * If this device did not have media in the drive at boot time, then |
| 543 | * we would have been unable to get the sector size. Check to see if |
| 544 | * this is the case, and try again. |
| 545 | */ |
| 546 | if (cd->needs_sector_size) |
| 547 | get_sectorsize(cd); |
| 548 | return 0; |
| 549 | |
| 550 | error_out: |
| 551 | return retval; |
| 552 | } |
| 553 | |
| 554 | static void sr_release(struct cdrom_device_info *cdi) |
| 555 | { |
| 556 | struct scsi_cd *cd = cdi->handle; |
| 557 | |
| 558 | if (cd->device->sector_size > 2048) |
| 559 | sr_set_blocklength(cd, 2048); |
| 560 | |
| 561 | } |
| 562 | |
| 563 | static int sr_probe(struct device *dev) |
| 564 | { |
| 565 | struct scsi_device *sdev = to_scsi_device(dev); |
| 566 | struct gendisk *disk; |
| 567 | struct scsi_cd *cd; |
| 568 | int minor, error; |
| 569 | |
| 570 | error = -ENODEV; |
| 571 | if (sdev->type != TYPE_ROM && sdev->type != TYPE_WORM) |
| 572 | goto fail; |
| 573 | |
| 574 | error = -ENOMEM; |
| 575 | cd = kmalloc(sizeof(*cd), GFP_KERNEL); |
| 576 | if (!cd) |
| 577 | goto fail; |
| 578 | memset(cd, 0, sizeof(*cd)); |
| 579 | |
| 580 | kref_init(&cd->kref); |
| 581 | |
| 582 | disk = alloc_disk(1); |
| 583 | if (!disk) |
| 584 | goto fail_free; |
| 585 | |
| 586 | spin_lock(&sr_index_lock); |
| 587 | minor = find_first_zero_bit(sr_index_bits, SR_DISKS); |
| 588 | if (minor == SR_DISKS) { |
| 589 | spin_unlock(&sr_index_lock); |
| 590 | error = -EBUSY; |
| 591 | goto fail_put; |
| 592 | } |
| 593 | __set_bit(minor, sr_index_bits); |
| 594 | spin_unlock(&sr_index_lock); |
| 595 | |
| 596 | disk->major = SCSI_CDROM_MAJOR; |
| 597 | disk->first_minor = minor; |
| 598 | sprintf(disk->disk_name, "sr%d", minor); |
| 599 | disk->fops = &sr_bdops; |
| 600 | disk->flags = GENHD_FL_CD; |
| 601 | |
| 602 | cd->device = sdev; |
| 603 | cd->disk = disk; |
| 604 | cd->driver = &sr_template; |
| 605 | cd->disk = disk; |
| 606 | cd->capacity = 0x1fffff; |
| 607 | cd->needs_sector_size = 1; |
| 608 | cd->device->changed = 1; /* force recheck CD type */ |
| 609 | cd->use = 1; |
| 610 | cd->readcd_known = 0; |
| 611 | cd->readcd_cdda = 0; |
| 612 | |
| 613 | cd->cdi.ops = &sr_dops; |
| 614 | cd->cdi.handle = cd; |
| 615 | cd->cdi.mask = 0; |
| 616 | cd->cdi.capacity = 1; |
| 617 | sprintf(cd->cdi.name, "sr%d", minor); |
| 618 | |
| 619 | sdev->sector_size = 2048; /* A guess, just in case */ |
| 620 | |
| 621 | /* FIXME: need to handle a get_capabilities failure properly ?? */ |
| 622 | get_capabilities(cd); |
| 623 | sr_vendor_init(cd); |
| 624 | |
| 625 | snprintf(disk->devfs_name, sizeof(disk->devfs_name), |
| 626 | "%s/cd", sdev->devfs_name); |
| 627 | disk->driverfs_dev = &sdev->sdev_gendev; |
| 628 | set_capacity(disk, cd->capacity); |
| 629 | disk->private_data = &cd->driver; |
| 630 | disk->queue = sdev->request_queue; |
| 631 | cd->cdi.disk = disk; |
| 632 | |
| 633 | if (register_cdrom(&cd->cdi)) |
| 634 | goto fail_put; |
| 635 | |
| 636 | dev_set_drvdata(dev, cd); |
| 637 | disk->flags |= GENHD_FL_REMOVABLE; |
| 638 | add_disk(disk); |
| 639 | |
| 640 | printk(KERN_DEBUG |
| 641 | "Attached scsi CD-ROM %s at scsi%d, channel %d, id %d, lun %d\n", |
| 642 | cd->cdi.name, sdev->host->host_no, sdev->channel, |
| 643 | sdev->id, sdev->lun); |
| 644 | return 0; |
| 645 | |
| 646 | fail_put: |
| 647 | put_disk(disk); |
| 648 | fail_free: |
| 649 | kfree(cd); |
| 650 | fail: |
| 651 | return error; |
| 652 | } |
| 653 | |
| 654 | |
| 655 | static void get_sectorsize(struct scsi_cd *cd) |
| 656 | { |
| 657 | unsigned char cmd[10]; |
| 658 | unsigned char *buffer; |
| 659 | int the_result, retries = 3; |
| 660 | int sector_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | request_queue_t *queue; |
| 662 | |
| 663 | buffer = kmalloc(512, GFP_KERNEL | GFP_DMA); |
| 664 | if (!buffer) |
| 665 | goto Enomem; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | |
| 667 | do { |
| 668 | cmd[0] = READ_CAPACITY; |
| 669 | memset((void *) &cmd[1], 0, 9); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | memset(buffer, 0, 8); |
| 671 | |
| 672 | /* Do the command and wait.. */ |
James Bottomley | 820732b | 2005-06-12 22:21:29 -0500 | [diff] [blame^] | 673 | the_result = scsi_execute_req(cd->device, cmd, DMA_FROM_DEVICE, |
| 674 | buffer, 8, NULL, SR_TIMEOUT, |
| 675 | MAX_RETRIES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | retries--; |
| 678 | |
| 679 | } while (the_result && retries); |
| 680 | |
| 681 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | if (the_result) { |
| 683 | cd->capacity = 0x1fffff; |
| 684 | sector_size = 2048; /* A guess, just in case */ |
| 685 | cd->needs_sector_size = 1; |
| 686 | } else { |
| 687 | #if 0 |
| 688 | if (cdrom_get_last_written(&cd->cdi, |
| 689 | &cd->capacity)) |
| 690 | #endif |
| 691 | cd->capacity = 1 + ((buffer[0] << 24) | |
| 692 | (buffer[1] << 16) | |
| 693 | (buffer[2] << 8) | |
| 694 | buffer[3]); |
| 695 | sector_size = (buffer[4] << 24) | |
| 696 | (buffer[5] << 16) | (buffer[6] << 8) | buffer[7]; |
| 697 | switch (sector_size) { |
| 698 | /* |
| 699 | * HP 4020i CD-Recorder reports 2340 byte sectors |
| 700 | * Philips CD-Writers report 2352 byte sectors |
| 701 | * |
| 702 | * Use 2k sectors for them.. |
| 703 | */ |
| 704 | case 0: |
| 705 | case 2340: |
| 706 | case 2352: |
| 707 | sector_size = 2048; |
| 708 | /* fall through */ |
| 709 | case 2048: |
| 710 | cd->capacity *= 4; |
| 711 | /* fall through */ |
| 712 | case 512: |
| 713 | break; |
| 714 | default: |
| 715 | printk("%s: unsupported sector size %d.\n", |
| 716 | cd->cdi.name, sector_size); |
| 717 | cd->capacity = 0; |
| 718 | cd->needs_sector_size = 1; |
| 719 | } |
| 720 | |
| 721 | cd->device->sector_size = sector_size; |
| 722 | |
| 723 | /* |
| 724 | * Add this so that we have the ability to correctly gauge |
| 725 | * what the device is capable of. |
| 726 | */ |
| 727 | cd->needs_sector_size = 0; |
| 728 | set_capacity(cd->disk, cd->capacity); |
| 729 | } |
| 730 | |
| 731 | queue = cd->device->request_queue; |
| 732 | blk_queue_hardsect_size(queue, sector_size); |
| 733 | out: |
| 734 | kfree(buffer); |
| 735 | return; |
| 736 | |
| 737 | Enomem: |
| 738 | cd->capacity = 0x1fffff; |
| 739 | sector_size = 2048; /* A guess, just in case */ |
| 740 | cd->needs_sector_size = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | goto out; |
| 742 | } |
| 743 | |
| 744 | static void get_capabilities(struct scsi_cd *cd) |
| 745 | { |
| 746 | unsigned char *buffer; |
| 747 | struct scsi_mode_data data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | unsigned char cmd[MAX_COMMAND_SIZE]; |
James Bottomley | 820732b | 2005-06-12 22:21:29 -0500 | [diff] [blame^] | 749 | struct scsi_sense_hdr sshdr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | unsigned int the_result; |
| 751 | int retries, rc, n; |
| 752 | |
| 753 | static char *loadmech[] = |
| 754 | { |
| 755 | "caddy", |
| 756 | "tray", |
| 757 | "pop-up", |
| 758 | "", |
| 759 | "changer", |
| 760 | "cartridge changer", |
| 761 | "", |
| 762 | "" |
| 763 | }; |
| 764 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | |
| 766 | /* allocate transfer buffer */ |
| 767 | buffer = kmalloc(512, GFP_KERNEL | GFP_DMA); |
| 768 | if (!buffer) { |
| 769 | printk(KERN_ERR "sr: out of memory.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | return; |
| 771 | } |
| 772 | |
| 773 | /* issue TEST_UNIT_READY until the initial startup UNIT_ATTENTION |
| 774 | * conditions are gone, or a timeout happens |
| 775 | */ |
| 776 | retries = 0; |
| 777 | do { |
| 778 | memset((void *)cmd, 0, MAX_COMMAND_SIZE); |
| 779 | cmd[0] = TEST_UNIT_READY; |
| 780 | |
James Bottomley | 820732b | 2005-06-12 22:21:29 -0500 | [diff] [blame^] | 781 | the_result = scsi_execute_req (cd->device, cmd, DMA_NONE, NULL, |
| 782 | 0, &sshdr, SR_TIMEOUT, |
| 783 | MAX_RETRIES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | retries++; |
| 786 | } while (retries < 5 && |
| 787 | (!scsi_status_is_good(the_result) || |
James Bottomley | 820732b | 2005-06-12 22:21:29 -0500 | [diff] [blame^] | 788 | (scsi_sense_valid(&sshdr) && |
| 789 | sshdr.sense_key == UNIT_ATTENTION))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | |
| 791 | /* ask for mode page 0x2a */ |
| 792 | rc = scsi_mode_sense(cd->device, 0, 0x2a, buffer, 128, |
James Bottomley | 1cf7269 | 2005-08-28 11:27:01 -0500 | [diff] [blame] | 793 | SR_TIMEOUT, 3, &data, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | |
| 795 | if (!scsi_status_is_good(rc)) { |
| 796 | /* failed, drive doesn't have capabilities mode page */ |
| 797 | cd->cdi.speed = 1; |
| 798 | cd->cdi.mask |= (CDC_CD_R | CDC_CD_RW | CDC_DVD_R | |
| 799 | CDC_DVD | CDC_DVD_RAM | |
| 800 | CDC_SELECT_DISC | CDC_SELECT_SPEED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | kfree(buffer); |
| 802 | printk("%s: scsi-1 drive\n", cd->cdi.name); |
| 803 | return; |
| 804 | } |
| 805 | |
| 806 | n = data.header_length + data.block_descriptor_length; |
| 807 | cd->cdi.speed = ((buffer[n + 8] << 8) + buffer[n + 9]) / 176; |
| 808 | cd->readcd_known = 1; |
| 809 | cd->readcd_cdda = buffer[n + 5] & 0x01; |
| 810 | /* print some capability bits */ |
| 811 | printk("%s: scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n", cd->cdi.name, |
| 812 | ((buffer[n + 14] << 8) + buffer[n + 15]) / 176, |
| 813 | cd->cdi.speed, |
| 814 | buffer[n + 3] & 0x01 ? "writer " : "", /* CD Writer */ |
| 815 | buffer[n + 3] & 0x20 ? "dvd-ram " : "", |
| 816 | buffer[n + 2] & 0x02 ? "cd/rw " : "", /* can read rewriteable */ |
| 817 | buffer[n + 4] & 0x20 ? "xa/form2 " : "", /* can read xa/from2 */ |
| 818 | buffer[n + 5] & 0x01 ? "cdda " : "", /* can read audio data */ |
| 819 | loadmech[buffer[n + 6] >> 5]); |
| 820 | if ((buffer[n + 6] >> 5) == 0) |
| 821 | /* caddy drives can't close tray... */ |
| 822 | cd->cdi.mask |= CDC_CLOSE_TRAY; |
| 823 | if ((buffer[n + 2] & 0x8) == 0) |
| 824 | /* not a DVD drive */ |
| 825 | cd->cdi.mask |= CDC_DVD; |
| 826 | if ((buffer[n + 3] & 0x20) == 0) |
| 827 | /* can't write DVD-RAM media */ |
| 828 | cd->cdi.mask |= CDC_DVD_RAM; |
| 829 | if ((buffer[n + 3] & 0x10) == 0) |
| 830 | /* can't write DVD-R media */ |
| 831 | cd->cdi.mask |= CDC_DVD_R; |
| 832 | if ((buffer[n + 3] & 0x2) == 0) |
| 833 | /* can't write CD-RW media */ |
| 834 | cd->cdi.mask |= CDC_CD_RW; |
| 835 | if ((buffer[n + 3] & 0x1) == 0) |
| 836 | /* can't write CD-R media */ |
| 837 | cd->cdi.mask |= CDC_CD_R; |
| 838 | if ((buffer[n + 6] & 0x8) == 0) |
| 839 | /* can't eject */ |
| 840 | cd->cdi.mask |= CDC_OPEN_TRAY; |
| 841 | |
| 842 | if ((buffer[n + 6] >> 5) == mechtype_individual_changer || |
| 843 | (buffer[n + 6] >> 5) == mechtype_cartridge_changer) |
| 844 | cd->cdi.capacity = |
| 845 | cdrom_number_of_slots(&cd->cdi); |
| 846 | if (cd->cdi.capacity <= 1) |
| 847 | /* not a changer */ |
| 848 | cd->cdi.mask |= CDC_SELECT_DISC; |
| 849 | /*else I don't think it can close its tray |
| 850 | cd->cdi.mask |= CDC_CLOSE_TRAY; */ |
| 851 | |
| 852 | /* |
| 853 | * if DVD-RAM, MRW-W or CD-RW, we are randomly writable |
| 854 | */ |
| 855 | if ((cd->cdi.mask & (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) != |
| 856 | (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) { |
| 857 | cd->device->writeable = 1; |
| 858 | } |
| 859 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | kfree(buffer); |
| 861 | } |
| 862 | |
| 863 | /* |
| 864 | * sr_packet() is the entry point for the generic commands generated |
| 865 | * by the Uniform CD-ROM layer. |
| 866 | */ |
| 867 | static int sr_packet(struct cdrom_device_info *cdi, |
| 868 | struct packet_command *cgc) |
| 869 | { |
| 870 | if (cgc->timeout <= 0) |
| 871 | cgc->timeout = IOCTL_TIMEOUT; |
| 872 | |
| 873 | sr_do_ioctl(cdi->handle, cgc); |
| 874 | |
| 875 | return cgc->stat; |
| 876 | } |
| 877 | |
| 878 | /** |
| 879 | * sr_kref_release - Called to free the scsi_cd structure |
| 880 | * @kref: pointer to embedded kref |
| 881 | * |
| 882 | * sr_ref_sem must be held entering this routine. Because it is |
| 883 | * called on last put, you should always use the scsi_cd_get() |
| 884 | * scsi_cd_put() helpers which manipulate the semaphore directly |
| 885 | * and never do a direct kref_put(). |
| 886 | **/ |
| 887 | static void sr_kref_release(struct kref *kref) |
| 888 | { |
| 889 | struct scsi_cd *cd = container_of(kref, struct scsi_cd, kref); |
| 890 | struct gendisk *disk = cd->disk; |
| 891 | |
| 892 | spin_lock(&sr_index_lock); |
| 893 | clear_bit(disk->first_minor, sr_index_bits); |
| 894 | spin_unlock(&sr_index_lock); |
| 895 | |
| 896 | unregister_cdrom(&cd->cdi); |
| 897 | |
| 898 | disk->private_data = NULL; |
| 899 | |
| 900 | put_disk(disk); |
| 901 | |
| 902 | kfree(cd); |
| 903 | } |
| 904 | |
| 905 | static int sr_remove(struct device *dev) |
| 906 | { |
| 907 | struct scsi_cd *cd = dev_get_drvdata(dev); |
| 908 | |
| 909 | del_gendisk(cd->disk); |
| 910 | |
| 911 | down(&sr_ref_sem); |
| 912 | kref_put(&cd->kref, sr_kref_release); |
| 913 | up(&sr_ref_sem); |
| 914 | |
| 915 | return 0; |
| 916 | } |
| 917 | |
| 918 | static int __init init_sr(void) |
| 919 | { |
| 920 | int rc; |
| 921 | |
| 922 | rc = register_blkdev(SCSI_CDROM_MAJOR, "sr"); |
| 923 | if (rc) |
| 924 | return rc; |
| 925 | return scsi_register_driver(&sr_template.gendrv); |
| 926 | } |
| 927 | |
| 928 | static void __exit exit_sr(void) |
| 929 | { |
| 930 | scsi_unregister_driver(&sr_template.gendrv); |
| 931 | unregister_blkdev(SCSI_CDROM_MAJOR, "sr"); |
| 932 | } |
| 933 | |
| 934 | module_init(init_sr); |
| 935 | module_exit(exit_sr); |
| 936 | MODULE_LICENSE("GPL"); |