1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * sr.c Copyright (C) 1992 David Giller 4 * Copyright (C) 1993, 1994, 1995, 1999 Eric Youngdale 5 * 6 * adapted from: 7 * sd.c Copyright (C) 1992 Drew Eckhardt 8 * Linux scsi disk driver by 9 * Drew Eckhardt <drew@colorado.edu> 10 * 11 * Modified by Eric Youngdale ericy@andante.org to 12 * add scatter-gather, multiple outstanding request, and other 13 * enhancements. 14 * 15 * Modified by Eric Youngdale eric@andante.org to support loadable 16 * low-level scsi drivers. 17 * 18 * Modified by Thomas Quinot thomas@melchior.cuivre.fdn.fr to 19 * provide auto-eject. 20 * 21 * Modified by Gerd Knorr <kraxel@cs.tu-berlin.de> to support the 22 * generic cdrom interface 23 * 24 * Modified by Jens Axboe <axboe@suse.de> - Uniform sr_packet() 25 * interface, capabilities probe additions, ioctl cleanups, etc. 26 * 27 * Modified by Richard Gooch <rgooch@atnf.csiro.au> to support devfs 28 * 29 * Modified by Jens Axboe <axboe@suse.de> - support DVD-RAM 30 * transparently and lose the GHOST hack 31 * 32 * Modified by Arnaldo Carvalho de Melo <acme@conectiva.com.br> 33 * check resource allocation in sr_init and some cleanups 34 */ 35 36 #include <linux/module.h> 37 #include <linux/fs.h> 38 #include <linux/kernel.h> 39 #include <linux/mm.h> 40 #include <linux/bio.h> 41 #include <linux/compat.h> 42 #include <linux/string.h> 43 #include <linux/errno.h> 44 #include <linux/cdrom.h> 45 #include <linux/interrupt.h> 46 #include <linux/init.h> 47 #include <linux/blkdev.h> 48 #include <linux/blk-pm.h> 49 #include <linux/mutex.h> 50 #include <linux/slab.h> 51 #include <linux/pm_runtime.h> 52 #include <linux/uaccess.h> 53 54 #include <asm/unaligned.h> 55 56 #include <scsi/scsi.h> 57 #include <scsi/scsi_dbg.h> 58 #include <scsi/scsi_device.h> 59 #include <scsi/scsi_driver.h> 60 #include <scsi/scsi_cmnd.h> 61 #include <scsi/scsi_eh.h> 62 #include <scsi/scsi_host.h> 63 #include <scsi/scsi_ioctl.h> /* For the door lock/unlock commands */ 64 65 #include "scsi_logging.h" 66 #include "sr.h" 67 68 69 MODULE_DESCRIPTION("SCSI cdrom (sr) driver"); 70 MODULE_LICENSE("GPL"); 71 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_CDROM_MAJOR); 72 MODULE_ALIAS_SCSI_DEVICE(TYPE_ROM); 73 MODULE_ALIAS_SCSI_DEVICE(TYPE_WORM); 74 75 #define SR_DISKS 256 76 77 #define SR_CAPABILITIES \ 78 (CDC_CLOSE_TRAY|CDC_OPEN_TRAY|CDC_LOCK|CDC_SELECT_SPEED| \ 79 CDC_SELECT_DISC|CDC_MULTI_SESSION|CDC_MCN|CDC_MEDIA_CHANGED| \ 80 CDC_PLAY_AUDIO|CDC_RESET|CDC_DRIVE_STATUS| \ 81 CDC_CD_R|CDC_CD_RW|CDC_DVD|CDC_DVD_R|CDC_DVD_RAM|CDC_GENERIC_PACKET| \ 82 CDC_MRW|CDC_MRW_W|CDC_RAM) 83 84 static int sr_probe(struct device *); 85 static int sr_remove(struct device *); 86 static blk_status_t sr_init_command(struct scsi_cmnd *SCpnt); 87 static int sr_done(struct scsi_cmnd *); 88 static int sr_runtime_suspend(struct device *dev); 89 90 static const struct dev_pm_ops sr_pm_ops = { 91 .runtime_suspend = sr_runtime_suspend, 92 }; 93 94 static struct scsi_driver sr_template = { 95 .gendrv = { 96 .name = "sr", 97 .owner = THIS_MODULE, 98 .probe = sr_probe, 99 .remove = sr_remove, 100 .pm = &sr_pm_ops, 101 }, 102 .init_command = sr_init_command, 103 .done = sr_done, 104 }; 105 106 static unsigned long sr_index_bits[SR_DISKS / BITS_PER_LONG]; 107 static DEFINE_SPINLOCK(sr_index_lock); 108 109 /* This semaphore is used to mediate the 0->1 reference get in the 110 * face of object destruction (i.e. we can't allow a get on an 111 * object after last put) */ 112 static DEFINE_MUTEX(sr_ref_mutex); 113 114 static int sr_open(struct cdrom_device_info *, int); 115 static void sr_release(struct cdrom_device_info *); 116 117 static void get_sectorsize(struct scsi_cd *); 118 static void get_capabilities(struct scsi_cd *); 119 120 static unsigned int sr_check_events(struct cdrom_device_info *cdi, 121 unsigned int clearing, int slot); 122 static int sr_packet(struct cdrom_device_info *, struct packet_command *); 123 static int sr_read_cdda_bpc(struct cdrom_device_info *cdi, void __user *ubuf, 124 u32 lba, u32 nr, u8 *last_sense); 125 126 static const struct cdrom_device_ops sr_dops = { 127 .open = sr_open, 128 .release = sr_release, 129 .drive_status = sr_drive_status, 130 .check_events = sr_check_events, 131 .tray_move = sr_tray_move, 132 .lock_door = sr_lock_door, 133 .select_speed = sr_select_speed, 134 .get_last_session = sr_get_last_session, 135 .get_mcn = sr_get_mcn, 136 .reset = sr_reset, 137 .audio_ioctl = sr_audio_ioctl, 138 .generic_packet = sr_packet, 139 .read_cdda_bpc = sr_read_cdda_bpc, 140 .capability = SR_CAPABILITIES, 141 }; 142 143 static void sr_kref_release(struct kref *kref); 144 145 static inline struct scsi_cd *scsi_cd(struct gendisk *disk) 146 { 147 return container_of(disk->private_data, struct scsi_cd, driver); 148 } 149 150 static int sr_runtime_suspend(struct device *dev) 151 { 152 struct scsi_cd *cd = dev_get_drvdata(dev); 153 154 if (!cd) /* E.g.: runtime suspend following sr_remove() */ 155 return 0; 156 157 if (cd->media_present) 158 return -EBUSY; 159 else 160 return 0; 161 } 162 163 /* 164 * The get and put routines for the struct scsi_cd. Note this entity 165 * has a scsi_device pointer and owns a reference to this. 166 */ 167 static inline struct scsi_cd *scsi_cd_get(struct gendisk *disk) 168 { 169 struct scsi_cd *cd = NULL; 170 171 mutex_lock(&sr_ref_mutex); 172 if (disk->private_data == NULL) 173 goto out; 174 cd = scsi_cd(disk); 175 kref_get(&cd->kref); 176 if (scsi_device_get(cd->device)) { 177 kref_put(&cd->kref, sr_kref_release); 178 cd = NULL; 179 } 180 out: 181 mutex_unlock(&sr_ref_mutex); 182 return cd; 183 } 184 185 static void scsi_cd_put(struct scsi_cd *cd) 186 { 187 struct scsi_device *sdev = cd->device; 188 189 mutex_lock(&sr_ref_mutex); 190 kref_put(&cd->kref, sr_kref_release); 191 scsi_device_put(sdev); 192 mutex_unlock(&sr_ref_mutex); 193 } 194 195 static unsigned int sr_get_events(struct scsi_device *sdev) 196 { 197 u8 buf[8]; 198 u8 cmd[] = { GET_EVENT_STATUS_NOTIFICATION, 199 1, /* polled */ 200 0, 0, /* reserved */ 201 1 << 4, /* notification class: media */ 202 0, 0, /* reserved */ 203 0, sizeof(buf), /* allocation length */ 204 0, /* control */ 205 }; 206 struct event_header *eh = (void *)buf; 207 struct media_event_desc *med = (void *)(buf + 4); 208 struct scsi_sense_hdr sshdr; 209 int result; 210 211 result = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buf, sizeof(buf), 212 &sshdr, SR_TIMEOUT, MAX_RETRIES, NULL); 213 if (scsi_sense_valid(&sshdr) && sshdr.sense_key == UNIT_ATTENTION) 214 return DISK_EVENT_MEDIA_CHANGE; 215 216 if (result || be16_to_cpu(eh->data_len) < sizeof(*med)) 217 return 0; 218 219 if (eh->nea || eh->notification_class != 0x4) 220 return 0; 221 222 if (med->media_event_code == 1) 223 return DISK_EVENT_EJECT_REQUEST; 224 else if (med->media_event_code == 2) 225 return DISK_EVENT_MEDIA_CHANGE; 226 else if (med->media_event_code == 3) 227 return DISK_EVENT_EJECT_REQUEST; 228 return 0; 229 } 230 231 /* 232 * This function checks to see if the media has been changed or eject 233 * button has been pressed. It is possible that we have already 234 * sensed a change, or the drive may have sensed one and not yet 235 * reported it. The past events are accumulated in sdev->changed and 236 * returned together with the current state. 237 */ 238 static unsigned int sr_check_events(struct cdrom_device_info *cdi, 239 unsigned int clearing, int slot) 240 { 241 struct scsi_cd *cd = cdi->handle; 242 bool last_present; 243 struct scsi_sense_hdr sshdr; 244 unsigned int events; 245 int ret; 246 247 /* no changer support */ 248 if (CDSL_CURRENT != slot) 249 return 0; 250 251 events = sr_get_events(cd->device); 252 cd->get_event_changed |= events & DISK_EVENT_MEDIA_CHANGE; 253 254 /* 255 * If earlier GET_EVENT_STATUS_NOTIFICATION and TUR did not agree 256 * for several times in a row. We rely on TUR only for this likely 257 * broken device, to prevent generating incorrect media changed 258 * events for every open(). 259 */ 260 if (cd->ignore_get_event) { 261 events &= ~DISK_EVENT_MEDIA_CHANGE; 262 goto do_tur; 263 } 264 265 /* 266 * GET_EVENT_STATUS_NOTIFICATION is enough unless MEDIA_CHANGE 267 * is being cleared. Note that there are devices which hang 268 * if asked to execute TUR repeatedly. 269 */ 270 if (cd->device->changed) { 271 events |= DISK_EVENT_MEDIA_CHANGE; 272 cd->device->changed = 0; 273 cd->tur_changed = true; 274 } 275 276 if (!(clearing & DISK_EVENT_MEDIA_CHANGE)) 277 return events; 278 do_tur: 279 /* let's see whether the media is there with TUR */ 280 last_present = cd->media_present; 281 ret = scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES, &sshdr); 282 283 /* 284 * Media is considered to be present if TUR succeeds or fails with 285 * sense data indicating something other than media-not-present 286 * (ASC 0x3a). 287 */ 288 cd->media_present = scsi_status_is_good(ret) || 289 (scsi_sense_valid(&sshdr) && sshdr.asc != 0x3a); 290 291 if (last_present != cd->media_present) 292 cd->device->changed = 1; 293 294 if (cd->device->changed) { 295 events |= DISK_EVENT_MEDIA_CHANGE; 296 cd->device->changed = 0; 297 cd->tur_changed = true; 298 } 299 300 if (cd->ignore_get_event) 301 return events; 302 303 /* check whether GET_EVENT is reporting spurious MEDIA_CHANGE */ 304 if (!cd->tur_changed) { 305 if (cd->get_event_changed) { 306 if (cd->tur_mismatch++ > 8) { 307 sr_printk(KERN_WARNING, cd, 308 "GET_EVENT and TUR disagree continuously, suppress GET_EVENT events\n"); 309 cd->ignore_get_event = true; 310 } 311 } else { 312 cd->tur_mismatch = 0; 313 } 314 } 315 cd->tur_changed = false; 316 cd->get_event_changed = false; 317 318 return events; 319 } 320 321 /* 322 * sr_done is the interrupt routine for the device driver. 323 * 324 * It will be notified on the end of a SCSI read / write, and will take one 325 * of several actions based on success or failure. 326 */ 327 static int sr_done(struct scsi_cmnd *SCpnt) 328 { 329 int result = SCpnt->result; 330 int this_count = scsi_bufflen(SCpnt); 331 int good_bytes = (result == 0 ? this_count : 0); 332 int block_sectors = 0; 333 long error_sector; 334 struct request *rq = scsi_cmd_to_rq(SCpnt); 335 struct scsi_cd *cd = scsi_cd(rq->rq_disk); 336 337 #ifdef DEBUG 338 scmd_printk(KERN_INFO, SCpnt, "done: %x\n", result); 339 #endif 340 341 /* 342 * Handle MEDIUM ERRORs or VOLUME OVERFLOWs that indicate partial 343 * success. Since this is a relatively rare error condition, no 344 * care is taken to avoid unnecessary additional work such as 345 * memcpy's that could be avoided. 346 */ 347 if (scsi_status_is_check_condition(result) && 348 (SCpnt->sense_buffer[0] & 0x7f) == 0x70) { /* Sense current */ 349 switch (SCpnt->sense_buffer[2]) { 350 case MEDIUM_ERROR: 351 case VOLUME_OVERFLOW: 352 case ILLEGAL_REQUEST: 353 if (!(SCpnt->sense_buffer[0] & 0x90)) 354 break; 355 error_sector = 356 get_unaligned_be32(&SCpnt->sense_buffer[3]); 357 if (rq->bio != NULL) 358 block_sectors = bio_sectors(rq->bio); 359 if (block_sectors < 4) 360 block_sectors = 4; 361 if (cd->device->sector_size == 2048) 362 error_sector <<= 2; 363 error_sector &= ~(block_sectors - 1); 364 good_bytes = (error_sector - blk_rq_pos(rq)) << 9; 365 if (good_bytes < 0 || good_bytes >= this_count) 366 good_bytes = 0; 367 /* 368 * The SCSI specification allows for the value 369 * returned by READ CAPACITY to be up to 75 2K 370 * sectors past the last readable block. 371 * Therefore, if we hit a medium error within the 372 * last 75 2K sectors, we decrease the saved size 373 * value. 374 */ 375 if (error_sector < get_capacity(cd->disk) && 376 cd->capacity - error_sector < 4 * 75) 377 set_capacity(cd->disk, error_sector); 378 break; 379 380 case RECOVERED_ERROR: 381 good_bytes = this_count; 382 break; 383 384 default: 385 break; 386 } 387 } 388 389 return good_bytes; 390 } 391 392 static blk_status_t sr_init_command(struct scsi_cmnd *SCpnt) 393 { 394 int block = 0, this_count, s_size; 395 struct scsi_cd *cd; 396 struct request *rq = scsi_cmd_to_rq(SCpnt); 397 blk_status_t ret; 398 399 ret = scsi_alloc_sgtables(SCpnt); 400 if (ret != BLK_STS_OK) 401 return ret; 402 cd = scsi_cd(rq->rq_disk); 403 404 SCSI_LOG_HLQUEUE(1, scmd_printk(KERN_INFO, SCpnt, 405 "Doing sr request, block = %d\n", block)); 406 407 if (!cd->device || !scsi_device_online(cd->device)) { 408 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt, 409 "Finishing %u sectors\n", blk_rq_sectors(rq))); 410 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt, 411 "Retry with 0x%p\n", SCpnt)); 412 goto out; 413 } 414 415 if (cd->device->changed) { 416 /* 417 * quietly refuse to do anything to a changed disc until the 418 * changed bit has been reset 419 */ 420 goto out; 421 } 422 423 s_size = cd->device->sector_size; 424 if (s_size != 512 && s_size != 1024 && s_size != 2048) { 425 scmd_printk(KERN_ERR, SCpnt, "bad sector size %d\n", s_size); 426 goto out; 427 } 428 429 switch (req_op(rq)) { 430 case REQ_OP_WRITE: 431 if (!cd->writeable) 432 goto out; 433 SCpnt->cmnd[0] = WRITE_10; 434 cd->cdi.media_written = 1; 435 break; 436 case REQ_OP_READ: 437 SCpnt->cmnd[0] = READ_10; 438 break; 439 default: 440 blk_dump_rq_flags(rq, "Unknown sr command"); 441 goto out; 442 } 443 444 { 445 struct scatterlist *sg; 446 int i, size = 0, sg_count = scsi_sg_count(SCpnt); 447 448 scsi_for_each_sg(SCpnt, sg, sg_count, i) 449 size += sg->length; 450 451 if (size != scsi_bufflen(SCpnt)) { 452 scmd_printk(KERN_ERR, SCpnt, 453 "mismatch count %d, bytes %d\n", 454 size, scsi_bufflen(SCpnt)); 455 if (scsi_bufflen(SCpnt) > size) 456 SCpnt->sdb.length = size; 457 } 458 } 459 460 /* 461 * request doesn't start on hw block boundary, add scatter pads 462 */ 463 if (((unsigned int)blk_rq_pos(rq) % (s_size >> 9)) || 464 (scsi_bufflen(SCpnt) % s_size)) { 465 scmd_printk(KERN_NOTICE, SCpnt, "unaligned transfer\n"); 466 goto out; 467 } 468 469 this_count = (scsi_bufflen(SCpnt) >> 9) / (s_size >> 9); 470 471 472 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt, 473 "%s %d/%u 512 byte blocks.\n", 474 (rq_data_dir(rq) == WRITE) ? 475 "writing" : "reading", 476 this_count, blk_rq_sectors(rq))); 477 478 SCpnt->cmnd[1] = 0; 479 block = (unsigned int)blk_rq_pos(rq) / (s_size >> 9); 480 481 if (this_count > 0xffff) { 482 this_count = 0xffff; 483 SCpnt->sdb.length = this_count * s_size; 484 } 485 486 put_unaligned_be32(block, &SCpnt->cmnd[2]); 487 SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0; 488 put_unaligned_be16(this_count, &SCpnt->cmnd[7]); 489 490 /* 491 * We shouldn't disconnect in the middle of a sector, so with a dumb 492 * host adapter, it's safe to assume that we can at least transfer 493 * this many bytes between each connect / disconnect. 494 */ 495 SCpnt->transfersize = cd->device->sector_size; 496 SCpnt->underflow = this_count << 9; 497 SCpnt->allowed = MAX_RETRIES; 498 SCpnt->cmd_len = 10; 499 500 /* 501 * This indicates that the command is ready from our end to be queued. 502 */ 503 return BLK_STS_OK; 504 out: 505 scsi_free_sgtables(SCpnt); 506 return BLK_STS_IOERR; 507 } 508 509 static void sr_revalidate_disk(struct scsi_cd *cd) 510 { 511 struct scsi_sense_hdr sshdr; 512 513 /* if the unit is not ready, nothing more to do */ 514 if (scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES, &sshdr)) 515 return; 516 sr_cd_check(&cd->cdi); 517 get_sectorsize(cd); 518 } 519 520 static int sr_block_open(struct block_device *bdev, fmode_t mode) 521 { 522 struct scsi_cd *cd; 523 struct scsi_device *sdev; 524 int ret = -ENXIO; 525 526 cd = scsi_cd_get(bdev->bd_disk); 527 if (!cd) 528 goto out; 529 530 sdev = cd->device; 531 scsi_autopm_get_device(sdev); 532 if (bdev_check_media_change(bdev)) 533 sr_revalidate_disk(cd); 534 535 mutex_lock(&cd->lock); 536 ret = cdrom_open(&cd->cdi, bdev, mode); 537 mutex_unlock(&cd->lock); 538 539 scsi_autopm_put_device(sdev); 540 if (ret) 541 scsi_cd_put(cd); 542 543 out: 544 return ret; 545 } 546 547 static void sr_block_release(struct gendisk *disk, fmode_t mode) 548 { 549 struct scsi_cd *cd = scsi_cd(disk); 550 551 mutex_lock(&cd->lock); 552 cdrom_release(&cd->cdi, mode); 553 mutex_unlock(&cd->lock); 554 555 scsi_cd_put(cd); 556 } 557 558 static int sr_block_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd, 559 unsigned long arg) 560 { 561 struct gendisk *disk = bdev->bd_disk; 562 struct scsi_cd *cd = scsi_cd(disk); 563 struct scsi_device *sdev = cd->device; 564 void __user *argp = (void __user *)arg; 565 int ret; 566 567 if (bdev_is_partition(bdev) && !capable(CAP_SYS_RAWIO)) 568 return -ENOIOCTLCMD; 569 570 mutex_lock(&cd->lock); 571 572 ret = scsi_ioctl_block_when_processing_errors(sdev, cmd, 573 (mode & FMODE_NDELAY) != 0); 574 if (ret) 575 goto out; 576 577 scsi_autopm_get_device(sdev); 578 579 if (ret != CDROMCLOSETRAY && ret != CDROMEJECT) { 580 ret = cdrom_ioctl(&cd->cdi, bdev, mode, cmd, arg); 581 if (ret != -ENOSYS) 582 goto put; 583 } 584 ret = scsi_ioctl(sdev, disk, mode, cmd, argp); 585 586 put: 587 scsi_autopm_put_device(sdev); 588 out: 589 mutex_unlock(&cd->lock); 590 return ret; 591 } 592 593 static unsigned int sr_block_check_events(struct gendisk *disk, 594 unsigned int clearing) 595 { 596 unsigned int ret = 0; 597 struct scsi_cd *cd; 598 599 cd = scsi_cd_get(disk); 600 if (!cd) 601 return 0; 602 603 if (!atomic_read(&cd->device->disk_events_disable_depth)) 604 ret = cdrom_check_events(&cd->cdi, clearing); 605 606 scsi_cd_put(cd); 607 return ret; 608 } 609 610 static const struct block_device_operations sr_bdops = 611 { 612 .owner = THIS_MODULE, 613 .open = sr_block_open, 614 .release = sr_block_release, 615 .ioctl = sr_block_ioctl, 616 .compat_ioctl = blkdev_compat_ptr_ioctl, 617 .check_events = sr_block_check_events, 618 }; 619 620 static int sr_open(struct cdrom_device_info *cdi, int purpose) 621 { 622 struct scsi_cd *cd = cdi->handle; 623 struct scsi_device *sdev = cd->device; 624 int retval; 625 626 /* 627 * If the device is in error recovery, wait until it is done. 628 * If the device is offline, then disallow any access to it. 629 */ 630 retval = -ENXIO; 631 if (!scsi_block_when_processing_errors(sdev)) 632 goto error_out; 633 634 return 0; 635 636 error_out: 637 return retval; 638 } 639 640 static void sr_release(struct cdrom_device_info *cdi) 641 { 642 } 643 644 static int sr_probe(struct device *dev) 645 { 646 struct scsi_device *sdev = to_scsi_device(dev); 647 struct gendisk *disk; 648 struct scsi_cd *cd; 649 int minor, error; 650 651 scsi_autopm_get_device(sdev); 652 error = -ENODEV; 653 if (sdev->type != TYPE_ROM && sdev->type != TYPE_WORM) 654 goto fail; 655 656 error = -ENOMEM; 657 cd = kzalloc(sizeof(*cd), GFP_KERNEL); 658 if (!cd) 659 goto fail; 660 661 kref_init(&cd->kref); 662 663 disk = alloc_disk(1); 664 if (!disk) 665 goto fail_free; 666 mutex_init(&cd->lock); 667 668 spin_lock(&sr_index_lock); 669 minor = find_first_zero_bit(sr_index_bits, SR_DISKS); 670 if (minor == SR_DISKS) { 671 spin_unlock(&sr_index_lock); 672 error = -EBUSY; 673 goto fail_put; 674 } 675 __set_bit(minor, sr_index_bits); 676 spin_unlock(&sr_index_lock); 677 678 disk->major = SCSI_CDROM_MAJOR; 679 disk->first_minor = minor; 680 sprintf(disk->disk_name, "sr%d", minor); 681 disk->fops = &sr_bdops; 682 disk->flags = GENHD_FL_CD | GENHD_FL_BLOCK_EVENTS_ON_EXCL_WRITE; 683 disk->events = DISK_EVENT_MEDIA_CHANGE | DISK_EVENT_EJECT_REQUEST; 684 disk->event_flags = DISK_EVENT_FLAG_POLL | DISK_EVENT_FLAG_UEVENT; 685 686 blk_queue_rq_timeout(sdev->request_queue, SR_TIMEOUT); 687 688 cd->device = sdev; 689 cd->disk = disk; 690 cd->driver = &sr_template; 691 cd->disk = disk; 692 cd->capacity = 0x1fffff; 693 cd->device->changed = 1; /* force recheck CD type */ 694 cd->media_present = 1; 695 cd->use = 1; 696 cd->readcd_known = 0; 697 cd->readcd_cdda = 0; 698 699 cd->cdi.ops = &sr_dops; 700 cd->cdi.handle = cd; 701 cd->cdi.mask = 0; 702 cd->cdi.capacity = 1; 703 sprintf(cd->cdi.name, "sr%d", minor); 704 705 sdev->sector_size = 2048; /* A guess, just in case */ 706 707 /* FIXME: need to handle a get_capabilities failure properly ?? */ 708 get_capabilities(cd); 709 sr_vendor_init(cd); 710 711 set_capacity(disk, cd->capacity); 712 disk->private_data = &cd->driver; 713 disk->queue = sdev->request_queue; 714 715 if (register_cdrom(disk, &cd->cdi)) 716 goto fail_minor; 717 718 /* 719 * Initialize block layer runtime PM stuffs before the 720 * periodic event checking request gets started in add_disk. 721 */ 722 blk_pm_runtime_init(sdev->request_queue, dev); 723 724 dev_set_drvdata(dev, cd); 725 disk->flags |= GENHD_FL_REMOVABLE; 726 sr_revalidate_disk(cd); 727 device_add_disk(&sdev->sdev_gendev, disk, NULL); 728 729 sdev_printk(KERN_DEBUG, sdev, 730 "Attached scsi CD-ROM %s\n", cd->cdi.name); 731 scsi_autopm_put_device(cd->device); 732 733 return 0; 734 735 fail_minor: 736 spin_lock(&sr_index_lock); 737 clear_bit(minor, sr_index_bits); 738 spin_unlock(&sr_index_lock); 739 fail_put: 740 put_disk(disk); 741 mutex_destroy(&cd->lock); 742 fail_free: 743 kfree(cd); 744 fail: 745 scsi_autopm_put_device(sdev); 746 return error; 747 } 748 749 750 static void get_sectorsize(struct scsi_cd *cd) 751 { 752 unsigned char cmd[10]; 753 unsigned char buffer[8]; 754 int the_result, retries = 3; 755 int sector_size; 756 struct request_queue *queue; 757 758 do { 759 cmd[0] = READ_CAPACITY; 760 memset((void *) &cmd[1], 0, 9); 761 memset(buffer, 0, sizeof(buffer)); 762 763 /* Do the command and wait.. */ 764 the_result = scsi_execute_req(cd->device, cmd, DMA_FROM_DEVICE, 765 buffer, sizeof(buffer), NULL, 766 SR_TIMEOUT, MAX_RETRIES, NULL); 767 768 retries--; 769 770 } while (the_result && retries); 771 772 773 if (the_result) { 774 cd->capacity = 0x1fffff; 775 sector_size = 2048; /* A guess, just in case */ 776 } else { 777 long last_written; 778 779 cd->capacity = 1 + get_unaligned_be32(&buffer[0]); 780 /* 781 * READ_CAPACITY doesn't return the correct size on 782 * certain UDF media. If last_written is larger, use 783 * it instead. 784 * 785 * http://bugzilla.kernel.org/show_bug.cgi?id=9668 786 */ 787 if (!cdrom_get_last_written(&cd->cdi, &last_written)) 788 cd->capacity = max_t(long, cd->capacity, last_written); 789 790 sector_size = get_unaligned_be32(&buffer[4]); 791 switch (sector_size) { 792 /* 793 * HP 4020i CD-Recorder reports 2340 byte sectors 794 * Philips CD-Writers report 2352 byte sectors 795 * 796 * Use 2k sectors for them.. 797 */ 798 case 0: 799 case 2340: 800 case 2352: 801 sector_size = 2048; 802 fallthrough; 803 case 2048: 804 cd->capacity *= 4; 805 fallthrough; 806 case 512: 807 break; 808 default: 809 sr_printk(KERN_INFO, cd, 810 "unsupported sector size %d.", sector_size); 811 cd->capacity = 0; 812 } 813 814 cd->device->sector_size = sector_size; 815 816 /* 817 * Add this so that we have the ability to correctly gauge 818 * what the device is capable of. 819 */ 820 set_capacity(cd->disk, cd->capacity); 821 } 822 823 queue = cd->device->request_queue; 824 blk_queue_logical_block_size(queue, sector_size); 825 826 return; 827 } 828 829 static void get_capabilities(struct scsi_cd *cd) 830 { 831 unsigned char *buffer; 832 struct scsi_mode_data data; 833 struct scsi_sense_hdr sshdr; 834 unsigned int ms_len = 128; 835 int rc, n; 836 837 static const char *loadmech[] = 838 { 839 "caddy", 840 "tray", 841 "pop-up", 842 "", 843 "changer", 844 "cartridge changer", 845 "", 846 "" 847 }; 848 849 850 /* allocate transfer buffer */ 851 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA); 852 if (!buffer) { 853 sr_printk(KERN_ERR, cd, "out of memory.\n"); 854 return; 855 } 856 857 /* eat unit attentions */ 858 scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES, &sshdr); 859 860 /* ask for mode page 0x2a */ 861 rc = scsi_mode_sense(cd->device, 0, 0x2a, buffer, ms_len, 862 SR_TIMEOUT, 3, &data, NULL); 863 864 if (rc < 0 || data.length > ms_len || 865 data.header_length + data.block_descriptor_length > data.length) { 866 /* failed, drive doesn't have capabilities mode page */ 867 cd->cdi.speed = 1; 868 cd->cdi.mask |= (CDC_CD_R | CDC_CD_RW | CDC_DVD_R | 869 CDC_DVD | CDC_DVD_RAM | 870 CDC_SELECT_DISC | CDC_SELECT_SPEED | 871 CDC_MRW | CDC_MRW_W | CDC_RAM); 872 kfree(buffer); 873 sr_printk(KERN_INFO, cd, "scsi-1 drive"); 874 return; 875 } 876 877 n = data.header_length + data.block_descriptor_length; 878 cd->cdi.speed = get_unaligned_be16(&buffer[n + 8]) / 176; 879 cd->readcd_known = 1; 880 cd->readcd_cdda = buffer[n + 5] & 0x01; 881 /* print some capability bits */ 882 sr_printk(KERN_INFO, cd, 883 "scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n", 884 get_unaligned_be16(&buffer[n + 14]) / 176, 885 cd->cdi.speed, 886 buffer[n + 3] & 0x01 ? "writer " : "", /* CD Writer */ 887 buffer[n + 3] & 0x20 ? "dvd-ram " : "", 888 buffer[n + 2] & 0x02 ? "cd/rw " : "", /* can read rewriteable */ 889 buffer[n + 4] & 0x20 ? "xa/form2 " : "", /* can read xa/from2 */ 890 buffer[n + 5] & 0x01 ? "cdda " : "", /* can read audio data */ 891 loadmech[buffer[n + 6] >> 5]); 892 if ((buffer[n + 6] >> 5) == 0) 893 /* caddy drives can't close tray... */ 894 cd->cdi.mask |= CDC_CLOSE_TRAY; 895 if ((buffer[n + 2] & 0x8) == 0) 896 /* not a DVD drive */ 897 cd->cdi.mask |= CDC_DVD; 898 if ((buffer[n + 3] & 0x20) == 0) 899 /* can't write DVD-RAM media */ 900 cd->cdi.mask |= CDC_DVD_RAM; 901 if ((buffer[n + 3] & 0x10) == 0) 902 /* can't write DVD-R media */ 903 cd->cdi.mask |= CDC_DVD_R; 904 if ((buffer[n + 3] & 0x2) == 0) 905 /* can't write CD-RW media */ 906 cd->cdi.mask |= CDC_CD_RW; 907 if ((buffer[n + 3] & 0x1) == 0) 908 /* can't write CD-R media */ 909 cd->cdi.mask |= CDC_CD_R; 910 if ((buffer[n + 6] & 0x8) == 0) 911 /* can't eject */ 912 cd->cdi.mask |= CDC_OPEN_TRAY; 913 914 if ((buffer[n + 6] >> 5) == mechtype_individual_changer || 915 (buffer[n + 6] >> 5) == mechtype_cartridge_changer) 916 cd->cdi.capacity = 917 cdrom_number_of_slots(&cd->cdi); 918 if (cd->cdi.capacity <= 1) 919 /* not a changer */ 920 cd->cdi.mask |= CDC_SELECT_DISC; 921 /*else I don't think it can close its tray 922 cd->cdi.mask |= CDC_CLOSE_TRAY; */ 923 924 /* 925 * if DVD-RAM, MRW-W or CD-RW, we are randomly writable 926 */ 927 if ((cd->cdi.mask & (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) != 928 (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) { 929 cd->writeable = 1; 930 } 931 932 kfree(buffer); 933 } 934 935 /* 936 * sr_packet() is the entry point for the generic commands generated 937 * by the Uniform CD-ROM layer. 938 */ 939 static int sr_packet(struct cdrom_device_info *cdi, 940 struct packet_command *cgc) 941 { 942 struct scsi_cd *cd = cdi->handle; 943 struct scsi_device *sdev = cd->device; 944 945 if (cgc->cmd[0] == GPCMD_READ_DISC_INFO && sdev->no_read_disc_info) 946 return -EDRIVE_CANT_DO_THIS; 947 948 if (cgc->timeout <= 0) 949 cgc->timeout = IOCTL_TIMEOUT; 950 951 sr_do_ioctl(cd, cgc); 952 953 return cgc->stat; 954 } 955 956 static int sr_read_cdda_bpc(struct cdrom_device_info *cdi, void __user *ubuf, 957 u32 lba, u32 nr, u8 *last_sense) 958 { 959 struct gendisk *disk = cdi->disk; 960 u32 len = nr * CD_FRAMESIZE_RAW; 961 struct scsi_request *req; 962 struct request *rq; 963 struct bio *bio; 964 int ret; 965 966 rq = blk_get_request(disk->queue, REQ_OP_DRV_IN, 0); 967 if (IS_ERR(rq)) 968 return PTR_ERR(rq); 969 req = scsi_req(rq); 970 971 ret = blk_rq_map_user(disk->queue, rq, NULL, ubuf, len, GFP_KERNEL); 972 if (ret) 973 goto out_put_request; 974 975 req->cmd[0] = GPCMD_READ_CD; 976 req->cmd[1] = 1 << 2; 977 req->cmd[2] = (lba >> 24) & 0xff; 978 req->cmd[3] = (lba >> 16) & 0xff; 979 req->cmd[4] = (lba >> 8) & 0xff; 980 req->cmd[5] = lba & 0xff; 981 req->cmd[6] = (nr >> 16) & 0xff; 982 req->cmd[7] = (nr >> 8) & 0xff; 983 req->cmd[8] = nr & 0xff; 984 req->cmd[9] = 0xf8; 985 req->cmd_len = 12; 986 rq->timeout = 60 * HZ; 987 bio = rq->bio; 988 989 blk_execute_rq(disk, rq, 0); 990 if (scsi_req(rq)->result) { 991 struct scsi_sense_hdr sshdr; 992 993 scsi_normalize_sense(req->sense, req->sense_len, 994 &sshdr); 995 *last_sense = sshdr.sense_key; 996 ret = -EIO; 997 } 998 999 if (blk_rq_unmap_user(bio)) 1000 ret = -EFAULT; 1001 out_put_request: 1002 blk_put_request(rq); 1003 return ret; 1004 } 1005 1006 1007 /** 1008 * sr_kref_release - Called to free the scsi_cd structure 1009 * @kref: pointer to embedded kref 1010 * 1011 * sr_ref_mutex must be held entering this routine. Because it is 1012 * called on last put, you should always use the scsi_cd_get() 1013 * scsi_cd_put() helpers which manipulate the semaphore directly 1014 * and never do a direct kref_put(). 1015 **/ 1016 static void sr_kref_release(struct kref *kref) 1017 { 1018 struct scsi_cd *cd = container_of(kref, struct scsi_cd, kref); 1019 struct gendisk *disk = cd->disk; 1020 1021 spin_lock(&sr_index_lock); 1022 clear_bit(MINOR(disk_devt(disk)), sr_index_bits); 1023 spin_unlock(&sr_index_lock); 1024 1025 unregister_cdrom(&cd->cdi); 1026 1027 disk->private_data = NULL; 1028 1029 put_disk(disk); 1030 1031 mutex_destroy(&cd->lock); 1032 1033 kfree(cd); 1034 } 1035 1036 static int sr_remove(struct device *dev) 1037 { 1038 struct scsi_cd *cd = dev_get_drvdata(dev); 1039 1040 scsi_autopm_get_device(cd->device); 1041 1042 del_gendisk(cd->disk); 1043 dev_set_drvdata(dev, NULL); 1044 1045 mutex_lock(&sr_ref_mutex); 1046 kref_put(&cd->kref, sr_kref_release); 1047 mutex_unlock(&sr_ref_mutex); 1048 1049 return 0; 1050 } 1051 1052 static int __init init_sr(void) 1053 { 1054 int rc; 1055 1056 rc = register_blkdev(SCSI_CDROM_MAJOR, "sr"); 1057 if (rc) 1058 return rc; 1059 rc = scsi_register_driver(&sr_template.gendrv); 1060 if (rc) 1061 unregister_blkdev(SCSI_CDROM_MAJOR, "sr"); 1062 1063 return rc; 1064 } 1065 1066 static void __exit exit_sr(void) 1067 { 1068 scsi_unregister_driver(&sr_template.gendrv); 1069 unregister_blkdev(SCSI_CDROM_MAJOR, "sr"); 1070 } 1071 1072 module_init(init_sr); 1073 module_exit(exit_sr); 1074 MODULE_LICENSE("GPL"); 1075