0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036 #include <linux/module.h>
0037 #include <linux/fs.h>
0038 #include <linux/kernel.h>
0039 #include <linux/mm.h>
0040 #include <linux/bio.h>
0041 #include <linux/compat.h>
0042 #include <linux/string.h>
0043 #include <linux/errno.h>
0044 #include <linux/cdrom.h>
0045 #include <linux/interrupt.h>
0046 #include <linux/init.h>
0047 #include <linux/major.h>
0048 #include <linux/blkdev.h>
0049 #include <linux/blk-pm.h>
0050 #include <linux/mutex.h>
0051 #include <linux/slab.h>
0052 #include <linux/pm_runtime.h>
0053 #include <linux/uaccess.h>
0054
0055 #include <asm/unaligned.h>
0056
0057 #include <scsi/scsi.h>
0058 #include <scsi/scsi_dbg.h>
0059 #include <scsi/scsi_device.h>
0060 #include <scsi/scsi_driver.h>
0061 #include <scsi/scsi_cmnd.h>
0062 #include <scsi/scsi_eh.h>
0063 #include <scsi/scsi_host.h>
0064 #include <scsi/scsi_ioctl.h> /* For the door lock/unlock commands */
0065
0066 #include "scsi_logging.h"
0067 #include "sr.h"
0068
0069
0070 MODULE_DESCRIPTION("SCSI cdrom (sr) driver");
0071 MODULE_LICENSE("GPL");
0072 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_CDROM_MAJOR);
0073 MODULE_ALIAS_SCSI_DEVICE(TYPE_ROM);
0074 MODULE_ALIAS_SCSI_DEVICE(TYPE_WORM);
0075
0076 #define SR_DISKS 256
0077
0078 #define SR_CAPABILITIES \
0079 (CDC_CLOSE_TRAY|CDC_OPEN_TRAY|CDC_LOCK|CDC_SELECT_SPEED| \
0080 CDC_SELECT_DISC|CDC_MULTI_SESSION|CDC_MCN|CDC_MEDIA_CHANGED| \
0081 CDC_PLAY_AUDIO|CDC_RESET|CDC_DRIVE_STATUS| \
0082 CDC_CD_R|CDC_CD_RW|CDC_DVD|CDC_DVD_R|CDC_DVD_RAM|CDC_GENERIC_PACKET| \
0083 CDC_MRW|CDC_MRW_W|CDC_RAM)
0084
0085 static int sr_probe(struct device *);
0086 static int sr_remove(struct device *);
0087 static blk_status_t sr_init_command(struct scsi_cmnd *SCpnt);
0088 static int sr_done(struct scsi_cmnd *);
0089 static int sr_runtime_suspend(struct device *dev);
0090
0091 static const struct dev_pm_ops sr_pm_ops = {
0092 .runtime_suspend = sr_runtime_suspend,
0093 };
0094
0095 static struct scsi_driver sr_template = {
0096 .gendrv = {
0097 .name = "sr",
0098 .owner = THIS_MODULE,
0099 .probe = sr_probe,
0100 .remove = sr_remove,
0101 .pm = &sr_pm_ops,
0102 },
0103 .init_command = sr_init_command,
0104 .done = sr_done,
0105 };
0106
0107 static unsigned long sr_index_bits[SR_DISKS / BITS_PER_LONG];
0108 static DEFINE_SPINLOCK(sr_index_lock);
0109
0110 static struct lock_class_key sr_bio_compl_lkclass;
0111
0112 static int sr_open(struct cdrom_device_info *, int);
0113 static void sr_release(struct cdrom_device_info *);
0114
0115 static void get_sectorsize(struct scsi_cd *);
0116 static int get_capabilities(struct scsi_cd *);
0117
0118 static unsigned int sr_check_events(struct cdrom_device_info *cdi,
0119 unsigned int clearing, int slot);
0120 static int sr_packet(struct cdrom_device_info *, struct packet_command *);
0121 static int sr_read_cdda_bpc(struct cdrom_device_info *cdi, void __user *ubuf,
0122 u32 lba, u32 nr, u8 *last_sense);
0123
0124 static const struct cdrom_device_ops sr_dops = {
0125 .open = sr_open,
0126 .release = sr_release,
0127 .drive_status = sr_drive_status,
0128 .check_events = sr_check_events,
0129 .tray_move = sr_tray_move,
0130 .lock_door = sr_lock_door,
0131 .select_speed = sr_select_speed,
0132 .get_last_session = sr_get_last_session,
0133 .get_mcn = sr_get_mcn,
0134 .reset = sr_reset,
0135 .audio_ioctl = sr_audio_ioctl,
0136 .generic_packet = sr_packet,
0137 .read_cdda_bpc = sr_read_cdda_bpc,
0138 .capability = SR_CAPABILITIES,
0139 };
0140
0141 static inline struct scsi_cd *scsi_cd(struct gendisk *disk)
0142 {
0143 return disk->private_data;
0144 }
0145
0146 static int sr_runtime_suspend(struct device *dev)
0147 {
0148 struct scsi_cd *cd = dev_get_drvdata(dev);
0149
0150 if (!cd)
0151 return 0;
0152
0153 if (cd->media_present)
0154 return -EBUSY;
0155 else
0156 return 0;
0157 }
0158
0159 static unsigned int sr_get_events(struct scsi_device *sdev)
0160 {
0161 u8 buf[8];
0162 u8 cmd[] = { GET_EVENT_STATUS_NOTIFICATION,
0163 1,
0164 0, 0,
0165 1 << 4,
0166 0, 0,
0167 0, sizeof(buf),
0168 0,
0169 };
0170 struct event_header *eh = (void *)buf;
0171 struct media_event_desc *med = (void *)(buf + 4);
0172 struct scsi_sense_hdr sshdr;
0173 int result;
0174
0175 result = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buf, sizeof(buf),
0176 &sshdr, SR_TIMEOUT, MAX_RETRIES, NULL);
0177 if (scsi_sense_valid(&sshdr) && sshdr.sense_key == UNIT_ATTENTION)
0178 return DISK_EVENT_MEDIA_CHANGE;
0179
0180 if (result || be16_to_cpu(eh->data_len) < sizeof(*med))
0181 return 0;
0182
0183 if (eh->nea || eh->notification_class != 0x4)
0184 return 0;
0185
0186 if (med->media_event_code == 1)
0187 return DISK_EVENT_EJECT_REQUEST;
0188 else if (med->media_event_code == 2)
0189 return DISK_EVENT_MEDIA_CHANGE;
0190 else if (med->media_event_code == 3)
0191 return DISK_EVENT_MEDIA_CHANGE;
0192 return 0;
0193 }
0194
0195
0196
0197
0198
0199
0200
0201
0202 static unsigned int sr_check_events(struct cdrom_device_info *cdi,
0203 unsigned int clearing, int slot)
0204 {
0205 struct scsi_cd *cd = cdi->handle;
0206 bool last_present;
0207 struct scsi_sense_hdr sshdr;
0208 unsigned int events;
0209 int ret;
0210
0211
0212 if (CDSL_CURRENT != slot)
0213 return 0;
0214
0215 events = sr_get_events(cd->device);
0216 cd->get_event_changed |= events & DISK_EVENT_MEDIA_CHANGE;
0217
0218
0219
0220
0221
0222
0223
0224 if (cd->ignore_get_event) {
0225 events &= ~DISK_EVENT_MEDIA_CHANGE;
0226 goto do_tur;
0227 }
0228
0229
0230
0231
0232
0233
0234 if (cd->device->changed) {
0235 events |= DISK_EVENT_MEDIA_CHANGE;
0236 cd->device->changed = 0;
0237 cd->tur_changed = true;
0238 }
0239
0240 if (!(clearing & DISK_EVENT_MEDIA_CHANGE))
0241 return events;
0242 do_tur:
0243
0244 last_present = cd->media_present;
0245 ret = scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES, &sshdr);
0246
0247
0248
0249
0250
0251
0252 cd->media_present = scsi_status_is_good(ret) ||
0253 (scsi_sense_valid(&sshdr) && sshdr.asc != 0x3a);
0254
0255 if (last_present != cd->media_present)
0256 cd->device->changed = 1;
0257
0258 if (cd->device->changed) {
0259 events |= DISK_EVENT_MEDIA_CHANGE;
0260 cd->device->changed = 0;
0261 cd->tur_changed = true;
0262 }
0263
0264 if (cd->ignore_get_event)
0265 return events;
0266
0267
0268 if (!cd->tur_changed) {
0269 if (cd->get_event_changed) {
0270 if (cd->tur_mismatch++ > 8) {
0271 sr_printk(KERN_WARNING, cd,
0272 "GET_EVENT and TUR disagree continuously, suppress GET_EVENT events\n");
0273 cd->ignore_get_event = true;
0274 }
0275 } else {
0276 cd->tur_mismatch = 0;
0277 }
0278 }
0279 cd->tur_changed = false;
0280 cd->get_event_changed = false;
0281
0282 return events;
0283 }
0284
0285
0286
0287
0288
0289
0290
0291 static int sr_done(struct scsi_cmnd *SCpnt)
0292 {
0293 int result = SCpnt->result;
0294 int this_count = scsi_bufflen(SCpnt);
0295 int good_bytes = (result == 0 ? this_count : 0);
0296 int block_sectors = 0;
0297 long error_sector;
0298 struct request *rq = scsi_cmd_to_rq(SCpnt);
0299 struct scsi_cd *cd = scsi_cd(rq->q->disk);
0300
0301 #ifdef DEBUG
0302 scmd_printk(KERN_INFO, SCpnt, "done: %x\n", result);
0303 #endif
0304
0305
0306
0307
0308
0309
0310
0311 if (scsi_status_is_check_condition(result) &&
0312 (SCpnt->sense_buffer[0] & 0x7f) == 0x70) {
0313 switch (SCpnt->sense_buffer[2]) {
0314 case MEDIUM_ERROR:
0315 case VOLUME_OVERFLOW:
0316 case ILLEGAL_REQUEST:
0317 if (!(SCpnt->sense_buffer[0] & 0x90))
0318 break;
0319 error_sector =
0320 get_unaligned_be32(&SCpnt->sense_buffer[3]);
0321 if (rq->bio != NULL)
0322 block_sectors = bio_sectors(rq->bio);
0323 if (block_sectors < 4)
0324 block_sectors = 4;
0325 if (cd->device->sector_size == 2048)
0326 error_sector <<= 2;
0327 error_sector &= ~(block_sectors - 1);
0328 good_bytes = (error_sector - blk_rq_pos(rq)) << 9;
0329 if (good_bytes < 0 || good_bytes >= this_count)
0330 good_bytes = 0;
0331
0332
0333
0334
0335
0336
0337
0338
0339 if (error_sector < get_capacity(cd->disk) &&
0340 cd->capacity - error_sector < 4 * 75)
0341 set_capacity(cd->disk, error_sector);
0342 break;
0343
0344 case RECOVERED_ERROR:
0345 good_bytes = this_count;
0346 break;
0347
0348 default:
0349 break;
0350 }
0351 }
0352
0353 return good_bytes;
0354 }
0355
0356 static blk_status_t sr_init_command(struct scsi_cmnd *SCpnt)
0357 {
0358 int block = 0, this_count, s_size;
0359 struct scsi_cd *cd;
0360 struct request *rq = scsi_cmd_to_rq(SCpnt);
0361 blk_status_t ret;
0362
0363 ret = scsi_alloc_sgtables(SCpnt);
0364 if (ret != BLK_STS_OK)
0365 return ret;
0366 cd = scsi_cd(rq->q->disk);
0367
0368 SCSI_LOG_HLQUEUE(1, scmd_printk(KERN_INFO, SCpnt,
0369 "Doing sr request, block = %d\n", block));
0370
0371 if (!cd->device || !scsi_device_online(cd->device)) {
0372 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
0373 "Finishing %u sectors\n", blk_rq_sectors(rq)));
0374 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
0375 "Retry with 0x%p\n", SCpnt));
0376 goto out;
0377 }
0378
0379 if (cd->device->changed) {
0380
0381
0382
0383
0384 goto out;
0385 }
0386
0387 s_size = cd->device->sector_size;
0388 if (s_size != 512 && s_size != 1024 && s_size != 2048) {
0389 scmd_printk(KERN_ERR, SCpnt, "bad sector size %d\n", s_size);
0390 goto out;
0391 }
0392
0393 switch (req_op(rq)) {
0394 case REQ_OP_WRITE:
0395 if (!cd->writeable)
0396 goto out;
0397 SCpnt->cmnd[0] = WRITE_10;
0398 cd->cdi.media_written = 1;
0399 break;
0400 case REQ_OP_READ:
0401 SCpnt->cmnd[0] = READ_10;
0402 break;
0403 default:
0404 blk_dump_rq_flags(rq, "Unknown sr command");
0405 goto out;
0406 }
0407
0408 {
0409 struct scatterlist *sg;
0410 int i, size = 0, sg_count = scsi_sg_count(SCpnt);
0411
0412 scsi_for_each_sg(SCpnt, sg, sg_count, i)
0413 size += sg->length;
0414
0415 if (size != scsi_bufflen(SCpnt)) {
0416 scmd_printk(KERN_ERR, SCpnt,
0417 "mismatch count %d, bytes %d\n",
0418 size, scsi_bufflen(SCpnt));
0419 if (scsi_bufflen(SCpnt) > size)
0420 SCpnt->sdb.length = size;
0421 }
0422 }
0423
0424
0425
0426
0427 if (((unsigned int)blk_rq_pos(rq) % (s_size >> 9)) ||
0428 (scsi_bufflen(SCpnt) % s_size)) {
0429 scmd_printk(KERN_NOTICE, SCpnt, "unaligned transfer\n");
0430 goto out;
0431 }
0432
0433 this_count = (scsi_bufflen(SCpnt) >> 9) / (s_size >> 9);
0434
0435
0436 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
0437 "%s %d/%u 512 byte blocks.\n",
0438 (rq_data_dir(rq) == WRITE) ?
0439 "writing" : "reading",
0440 this_count, blk_rq_sectors(rq)));
0441
0442 SCpnt->cmnd[1] = 0;
0443 block = (unsigned int)blk_rq_pos(rq) / (s_size >> 9);
0444
0445 if (this_count > 0xffff) {
0446 this_count = 0xffff;
0447 SCpnt->sdb.length = this_count * s_size;
0448 }
0449
0450 put_unaligned_be32(block, &SCpnt->cmnd[2]);
0451 SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
0452 put_unaligned_be16(this_count, &SCpnt->cmnd[7]);
0453
0454
0455
0456
0457
0458
0459 SCpnt->transfersize = cd->device->sector_size;
0460 SCpnt->underflow = this_count << 9;
0461 SCpnt->allowed = MAX_RETRIES;
0462 SCpnt->cmd_len = 10;
0463
0464
0465
0466
0467 return BLK_STS_OK;
0468 out:
0469 scsi_free_sgtables(SCpnt);
0470 return BLK_STS_IOERR;
0471 }
0472
0473 static void sr_revalidate_disk(struct scsi_cd *cd)
0474 {
0475 struct scsi_sense_hdr sshdr;
0476
0477
0478 if (scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES, &sshdr))
0479 return;
0480 sr_cd_check(&cd->cdi);
0481 get_sectorsize(cd);
0482 }
0483
0484 static int sr_block_open(struct block_device *bdev, fmode_t mode)
0485 {
0486 struct scsi_cd *cd = scsi_cd(bdev->bd_disk);
0487 struct scsi_device *sdev = cd->device;
0488 int ret;
0489
0490 if (scsi_device_get(cd->device))
0491 return -ENXIO;
0492
0493 scsi_autopm_get_device(sdev);
0494 if (bdev_check_media_change(bdev))
0495 sr_revalidate_disk(cd);
0496
0497 mutex_lock(&cd->lock);
0498 ret = cdrom_open(&cd->cdi, bdev, mode);
0499 mutex_unlock(&cd->lock);
0500
0501 scsi_autopm_put_device(sdev);
0502 if (ret)
0503 scsi_device_put(cd->device);
0504 return ret;
0505 }
0506
0507 static void sr_block_release(struct gendisk *disk, fmode_t mode)
0508 {
0509 struct scsi_cd *cd = scsi_cd(disk);
0510
0511 mutex_lock(&cd->lock);
0512 cdrom_release(&cd->cdi, mode);
0513 mutex_unlock(&cd->lock);
0514
0515 scsi_device_put(cd->device);
0516 }
0517
0518 static int sr_block_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
0519 unsigned long arg)
0520 {
0521 struct scsi_cd *cd = scsi_cd(bdev->bd_disk);
0522 struct scsi_device *sdev = cd->device;
0523 void __user *argp = (void __user *)arg;
0524 int ret;
0525
0526 if (bdev_is_partition(bdev) && !capable(CAP_SYS_RAWIO))
0527 return -ENOIOCTLCMD;
0528
0529 mutex_lock(&cd->lock);
0530
0531 ret = scsi_ioctl_block_when_processing_errors(sdev, cmd,
0532 (mode & FMODE_NDELAY) != 0);
0533 if (ret)
0534 goto out;
0535
0536 scsi_autopm_get_device(sdev);
0537
0538 if (cmd != CDROMCLOSETRAY && cmd != CDROMEJECT) {
0539 ret = cdrom_ioctl(&cd->cdi, bdev, mode, cmd, arg);
0540 if (ret != -ENOSYS)
0541 goto put;
0542 }
0543 ret = scsi_ioctl(sdev, mode, cmd, argp);
0544
0545 put:
0546 scsi_autopm_put_device(sdev);
0547 out:
0548 mutex_unlock(&cd->lock);
0549 return ret;
0550 }
0551
0552 static unsigned int sr_block_check_events(struct gendisk *disk,
0553 unsigned int clearing)
0554 {
0555 struct scsi_cd *cd = disk->private_data;
0556
0557 if (atomic_read(&cd->device->disk_events_disable_depth))
0558 return 0;
0559 return cdrom_check_events(&cd->cdi, clearing);
0560 }
0561
0562 static void sr_free_disk(struct gendisk *disk)
0563 {
0564 struct scsi_cd *cd = disk->private_data;
0565
0566 spin_lock(&sr_index_lock);
0567 clear_bit(MINOR(disk_devt(disk)), sr_index_bits);
0568 spin_unlock(&sr_index_lock);
0569
0570 unregister_cdrom(&cd->cdi);
0571 mutex_destroy(&cd->lock);
0572 kfree(cd);
0573 }
0574
0575 static const struct block_device_operations sr_bdops =
0576 {
0577 .owner = THIS_MODULE,
0578 .open = sr_block_open,
0579 .release = sr_block_release,
0580 .ioctl = sr_block_ioctl,
0581 .compat_ioctl = blkdev_compat_ptr_ioctl,
0582 .check_events = sr_block_check_events,
0583 .free_disk = sr_free_disk,
0584 };
0585
0586 static int sr_open(struct cdrom_device_info *cdi, int purpose)
0587 {
0588 struct scsi_cd *cd = cdi->handle;
0589 struct scsi_device *sdev = cd->device;
0590 int retval;
0591
0592
0593
0594
0595
0596 retval = -ENXIO;
0597 if (!scsi_block_when_processing_errors(sdev))
0598 goto error_out;
0599
0600 return 0;
0601
0602 error_out:
0603 return retval;
0604 }
0605
0606 static void sr_release(struct cdrom_device_info *cdi)
0607 {
0608 }
0609
0610 static int sr_probe(struct device *dev)
0611 {
0612 struct scsi_device *sdev = to_scsi_device(dev);
0613 struct gendisk *disk;
0614 struct scsi_cd *cd;
0615 int minor, error;
0616
0617 scsi_autopm_get_device(sdev);
0618 error = -ENODEV;
0619 if (sdev->type != TYPE_ROM && sdev->type != TYPE_WORM)
0620 goto fail;
0621
0622 error = -ENOMEM;
0623 cd = kzalloc(sizeof(*cd), GFP_KERNEL);
0624 if (!cd)
0625 goto fail;
0626
0627 disk = blk_mq_alloc_disk_for_queue(sdev->request_queue,
0628 &sr_bio_compl_lkclass);
0629 if (!disk)
0630 goto fail_free;
0631 mutex_init(&cd->lock);
0632
0633 spin_lock(&sr_index_lock);
0634 minor = find_first_zero_bit(sr_index_bits, SR_DISKS);
0635 if (minor == SR_DISKS) {
0636 spin_unlock(&sr_index_lock);
0637 error = -EBUSY;
0638 goto fail_put;
0639 }
0640 __set_bit(minor, sr_index_bits);
0641 spin_unlock(&sr_index_lock);
0642
0643 disk->major = SCSI_CDROM_MAJOR;
0644 disk->first_minor = minor;
0645 disk->minors = 1;
0646 sprintf(disk->disk_name, "sr%d", minor);
0647 disk->fops = &sr_bdops;
0648 disk->flags |= GENHD_FL_REMOVABLE | GENHD_FL_NO_PART;
0649 disk->events = DISK_EVENT_MEDIA_CHANGE | DISK_EVENT_EJECT_REQUEST;
0650 disk->event_flags = DISK_EVENT_FLAG_POLL | DISK_EVENT_FLAG_UEVENT |
0651 DISK_EVENT_FLAG_BLOCK_ON_EXCL_WRITE;
0652
0653 blk_queue_rq_timeout(sdev->request_queue, SR_TIMEOUT);
0654
0655 cd->device = sdev;
0656 cd->disk = disk;
0657 cd->capacity = 0x1fffff;
0658 cd->device->changed = 1;
0659 cd->media_present = 1;
0660 cd->use = 1;
0661 cd->readcd_known = 0;
0662 cd->readcd_cdda = 0;
0663
0664 cd->cdi.ops = &sr_dops;
0665 cd->cdi.handle = cd;
0666 cd->cdi.mask = 0;
0667 cd->cdi.capacity = 1;
0668 sprintf(cd->cdi.name, "sr%d", minor);
0669
0670 sdev->sector_size = 2048;
0671
0672 error = -ENOMEM;
0673 if (get_capabilities(cd))
0674 goto fail_minor;
0675 sr_vendor_init(cd);
0676
0677 set_capacity(disk, cd->capacity);
0678 disk->private_data = cd;
0679
0680 if (register_cdrom(disk, &cd->cdi))
0681 goto fail_minor;
0682
0683
0684
0685
0686
0687 blk_pm_runtime_init(sdev->request_queue, dev);
0688
0689 dev_set_drvdata(dev, cd);
0690 sr_revalidate_disk(cd);
0691
0692 error = device_add_disk(&sdev->sdev_gendev, disk, NULL);
0693 if (error)
0694 goto unregister_cdrom;
0695
0696 sdev_printk(KERN_DEBUG, sdev,
0697 "Attached scsi CD-ROM %s\n", cd->cdi.name);
0698 scsi_autopm_put_device(cd->device);
0699
0700 return 0;
0701
0702 unregister_cdrom:
0703 unregister_cdrom(&cd->cdi);
0704 fail_minor:
0705 spin_lock(&sr_index_lock);
0706 clear_bit(minor, sr_index_bits);
0707 spin_unlock(&sr_index_lock);
0708 fail_put:
0709 put_disk(disk);
0710 mutex_destroy(&cd->lock);
0711 fail_free:
0712 kfree(cd);
0713 fail:
0714 scsi_autopm_put_device(sdev);
0715 return error;
0716 }
0717
0718
0719 static void get_sectorsize(struct scsi_cd *cd)
0720 {
0721 unsigned char cmd[10];
0722 unsigned char buffer[8];
0723 int the_result, retries = 3;
0724 int sector_size;
0725 struct request_queue *queue;
0726
0727 do {
0728 cmd[0] = READ_CAPACITY;
0729 memset((void *) &cmd[1], 0, 9);
0730 memset(buffer, 0, sizeof(buffer));
0731
0732
0733 the_result = scsi_execute_req(cd->device, cmd, DMA_FROM_DEVICE,
0734 buffer, sizeof(buffer), NULL,
0735 SR_TIMEOUT, MAX_RETRIES, NULL);
0736
0737 retries--;
0738
0739 } while (the_result && retries);
0740
0741
0742 if (the_result) {
0743 cd->capacity = 0x1fffff;
0744 sector_size = 2048;
0745 } else {
0746 long last_written;
0747
0748 cd->capacity = 1 + get_unaligned_be32(&buffer[0]);
0749
0750
0751
0752
0753
0754
0755
0756 if (!cdrom_get_last_written(&cd->cdi, &last_written))
0757 cd->capacity = max_t(long, cd->capacity, last_written);
0758
0759 sector_size = get_unaligned_be32(&buffer[4]);
0760 switch (sector_size) {
0761
0762
0763
0764
0765
0766
0767 case 0:
0768 case 2340:
0769 case 2352:
0770 sector_size = 2048;
0771 fallthrough;
0772 case 2048:
0773 cd->capacity *= 4;
0774 fallthrough;
0775 case 512:
0776 break;
0777 default:
0778 sr_printk(KERN_INFO, cd,
0779 "unsupported sector size %d.", sector_size);
0780 cd->capacity = 0;
0781 }
0782
0783 cd->device->sector_size = sector_size;
0784
0785
0786
0787
0788
0789 set_capacity(cd->disk, cd->capacity);
0790 }
0791
0792 queue = cd->device->request_queue;
0793 blk_queue_logical_block_size(queue, sector_size);
0794
0795 return;
0796 }
0797
0798 static int get_capabilities(struct scsi_cd *cd)
0799 {
0800 unsigned char *buffer;
0801 struct scsi_mode_data data;
0802 struct scsi_sense_hdr sshdr;
0803 unsigned int ms_len = 128;
0804 int rc, n;
0805
0806 static const char *loadmech[] =
0807 {
0808 "caddy",
0809 "tray",
0810 "pop-up",
0811 "",
0812 "changer",
0813 "cartridge changer",
0814 "",
0815 ""
0816 };
0817
0818
0819
0820 buffer = kmalloc(512, GFP_KERNEL);
0821 if (!buffer) {
0822 sr_printk(KERN_ERR, cd, "out of memory.\n");
0823 return -ENOMEM;
0824 }
0825
0826
0827 scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES, &sshdr);
0828
0829
0830 rc = scsi_mode_sense(cd->device, 0, 0x2a, buffer, ms_len,
0831 SR_TIMEOUT, 3, &data, NULL);
0832
0833 if (rc < 0 || data.length > ms_len ||
0834 data.header_length + data.block_descriptor_length > data.length) {
0835
0836 cd->cdi.speed = 1;
0837 cd->cdi.mask |= (CDC_CD_R | CDC_CD_RW | CDC_DVD_R |
0838 CDC_DVD | CDC_DVD_RAM |
0839 CDC_SELECT_DISC | CDC_SELECT_SPEED |
0840 CDC_MRW | CDC_MRW_W | CDC_RAM);
0841 kfree(buffer);
0842 sr_printk(KERN_INFO, cd, "scsi-1 drive");
0843 return 0;
0844 }
0845
0846 n = data.header_length + data.block_descriptor_length;
0847 cd->cdi.speed = get_unaligned_be16(&buffer[n + 8]) / 176;
0848 cd->readcd_known = 1;
0849 cd->readcd_cdda = buffer[n + 5] & 0x01;
0850
0851 sr_printk(KERN_INFO, cd,
0852 "scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n",
0853 get_unaligned_be16(&buffer[n + 14]) / 176,
0854 cd->cdi.speed,
0855 buffer[n + 3] & 0x01 ? "writer " : "",
0856 buffer[n + 3] & 0x20 ? "dvd-ram " : "",
0857 buffer[n + 2] & 0x02 ? "cd/rw " : "",
0858 buffer[n + 4] & 0x20 ? "xa/form2 " : "",
0859 buffer[n + 5] & 0x01 ? "cdda " : "",
0860 loadmech[buffer[n + 6] >> 5]);
0861 if ((buffer[n + 6] >> 5) == 0)
0862
0863 cd->cdi.mask |= CDC_CLOSE_TRAY;
0864 if ((buffer[n + 2] & 0x8) == 0)
0865
0866 cd->cdi.mask |= CDC_DVD;
0867 if ((buffer[n + 3] & 0x20) == 0)
0868
0869 cd->cdi.mask |= CDC_DVD_RAM;
0870 if ((buffer[n + 3] & 0x10) == 0)
0871
0872 cd->cdi.mask |= CDC_DVD_R;
0873 if ((buffer[n + 3] & 0x2) == 0)
0874
0875 cd->cdi.mask |= CDC_CD_RW;
0876 if ((buffer[n + 3] & 0x1) == 0)
0877
0878 cd->cdi.mask |= CDC_CD_R;
0879 if ((buffer[n + 6] & 0x8) == 0)
0880
0881 cd->cdi.mask |= CDC_OPEN_TRAY;
0882
0883 if ((buffer[n + 6] >> 5) == mechtype_individual_changer ||
0884 (buffer[n + 6] >> 5) == mechtype_cartridge_changer)
0885 cd->cdi.capacity =
0886 cdrom_number_of_slots(&cd->cdi);
0887 if (cd->cdi.capacity <= 1)
0888
0889 cd->cdi.mask |= CDC_SELECT_DISC;
0890
0891
0892
0893
0894
0895
0896 if ((cd->cdi.mask & (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) !=
0897 (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) {
0898 cd->writeable = 1;
0899 }
0900
0901 kfree(buffer);
0902 return 0;
0903 }
0904
0905
0906
0907
0908
0909 static int sr_packet(struct cdrom_device_info *cdi,
0910 struct packet_command *cgc)
0911 {
0912 struct scsi_cd *cd = cdi->handle;
0913 struct scsi_device *sdev = cd->device;
0914
0915 if (cgc->cmd[0] == GPCMD_READ_DISC_INFO && sdev->no_read_disc_info)
0916 return -EDRIVE_CANT_DO_THIS;
0917
0918 if (cgc->timeout <= 0)
0919 cgc->timeout = IOCTL_TIMEOUT;
0920
0921 sr_do_ioctl(cd, cgc);
0922
0923 return cgc->stat;
0924 }
0925
0926 static int sr_read_cdda_bpc(struct cdrom_device_info *cdi, void __user *ubuf,
0927 u32 lba, u32 nr, u8 *last_sense)
0928 {
0929 struct gendisk *disk = cdi->disk;
0930 u32 len = nr * CD_FRAMESIZE_RAW;
0931 struct scsi_cmnd *scmd;
0932 struct request *rq;
0933 struct bio *bio;
0934 int ret;
0935
0936 rq = scsi_alloc_request(disk->queue, REQ_OP_DRV_IN, 0);
0937 if (IS_ERR(rq))
0938 return PTR_ERR(rq);
0939 scmd = blk_mq_rq_to_pdu(rq);
0940
0941 ret = blk_rq_map_user(disk->queue, rq, NULL, ubuf, len, GFP_KERNEL);
0942 if (ret)
0943 goto out_put_request;
0944
0945 scmd->cmnd[0] = GPCMD_READ_CD;
0946 scmd->cmnd[1] = 1 << 2;
0947 scmd->cmnd[2] = (lba >> 24) & 0xff;
0948 scmd->cmnd[3] = (lba >> 16) & 0xff;
0949 scmd->cmnd[4] = (lba >> 8) & 0xff;
0950 scmd->cmnd[5] = lba & 0xff;
0951 scmd->cmnd[6] = (nr >> 16) & 0xff;
0952 scmd->cmnd[7] = (nr >> 8) & 0xff;
0953 scmd->cmnd[8] = nr & 0xff;
0954 scmd->cmnd[9] = 0xf8;
0955 scmd->cmd_len = 12;
0956 rq->timeout = 60 * HZ;
0957 bio = rq->bio;
0958
0959 blk_execute_rq(rq, false);
0960 if (scmd->result) {
0961 struct scsi_sense_hdr sshdr;
0962
0963 scsi_normalize_sense(scmd->sense_buffer, scmd->sense_len,
0964 &sshdr);
0965 *last_sense = sshdr.sense_key;
0966 ret = -EIO;
0967 }
0968
0969 if (blk_rq_unmap_user(bio))
0970 ret = -EFAULT;
0971 out_put_request:
0972 blk_mq_free_request(rq);
0973 return ret;
0974 }
0975
0976 static int sr_remove(struct device *dev)
0977 {
0978 struct scsi_cd *cd = dev_get_drvdata(dev);
0979
0980 scsi_autopm_get_device(cd->device);
0981
0982 del_gendisk(cd->disk);
0983 put_disk(cd->disk);
0984
0985 return 0;
0986 }
0987
0988 static int __init init_sr(void)
0989 {
0990 int rc;
0991
0992 rc = register_blkdev(SCSI_CDROM_MAJOR, "sr");
0993 if (rc)
0994 return rc;
0995 rc = scsi_register_driver(&sr_template.gendrv);
0996 if (rc)
0997 unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
0998
0999 return rc;
1000 }
1001
1002 static void __exit exit_sr(void)
1003 {
1004 scsi_unregister_driver(&sr_template.gendrv);
1005 unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
1006 }
1007
1008 module_init(init_sr);
1009 module_exit(exit_sr);
1010 MODULE_LICENSE("GPL");