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