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