1 // SPDX-License-Identifier: GPL-2.0-only
3 * sr.c Copyright (C) 1992 David Giller
4 * Copyright (C) 1993, 1994, 1995, 1999 Eric Youngdale
7 * sd.c Copyright (C) 1992 Drew Eckhardt
8 * Linux scsi disk driver by
9 * Drew Eckhardt <drew@colorado.edu>
11 * Modified by Eric Youngdale ericy@andante.org to
12 * add scatter-gather, multiple outstanding request, and other
15 * Modified by Eric Youngdale eric@andante.org to support loadable
16 * low-level scsi drivers.
18 * Modified by Thomas Quinot thomas@melchior.cuivre.fdn.fr to
21 * Modified by Gerd Knorr <kraxel@cs.tu-berlin.de> to support the
22 * generic cdrom interface
24 * Modified by Jens Axboe <axboe@suse.de> - Uniform sr_packet()
25 * interface, capabilities probe additions, ioctl cleanups, etc.
27 * Modified by Richard Gooch <rgooch@atnf.csiro.au> to support devfs
29 * Modified by Jens Axboe <axboe@suse.de> - support DVD-RAM
30 * transparently and lose the GHOST hack
32 * Modified by Arnaldo Carvalho de Melo <acme@conectiva.com.br>
33 * check resource allocation in sr_init and some cleanups
36 #include <linux/module.h>
38 #include <linux/kernel.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>
54 #include <asm/unaligned.h>
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 */
65 #include "scsi_logging.h"
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);
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)
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);
90 static const struct dev_pm_ops sr_pm_ops = {
91 .runtime_suspend = sr_runtime_suspend,
94 static struct scsi_driver sr_template = {
102 .init_command = sr_init_command,
106 static unsigned long sr_index_bits[SR_DISKS / BITS_PER_LONG];
107 static DEFINE_SPINLOCK(sr_index_lock);
109 static struct lock_class_key sr_bio_compl_lkclass;
111 /* This semaphore is used to mediate the 0->1 reference get in the
112 * face of object destruction (i.e. we can't allow a get on an
113 * object after last put) */
114 static DEFINE_MUTEX(sr_ref_mutex);
116 static int sr_open(struct cdrom_device_info *, int);
117 static void sr_release(struct cdrom_device_info *);
119 static void get_sectorsize(struct scsi_cd *);
120 static void get_capabilities(struct scsi_cd *);
122 static unsigned int sr_check_events(struct cdrom_device_info *cdi,
123 unsigned int clearing, int slot);
124 static int sr_packet(struct cdrom_device_info *, struct packet_command *);
125 static int sr_read_cdda_bpc(struct cdrom_device_info *cdi, void __user *ubuf,
126 u32 lba, u32 nr, u8 *last_sense);
128 static const struct cdrom_device_ops sr_dops = {
130 .release = sr_release,
131 .drive_status = sr_drive_status,
132 .check_events = sr_check_events,
133 .tray_move = sr_tray_move,
134 .lock_door = sr_lock_door,
135 .select_speed = sr_select_speed,
136 .get_last_session = sr_get_last_session,
137 .get_mcn = sr_get_mcn,
139 .audio_ioctl = sr_audio_ioctl,
140 .generic_packet = sr_packet,
141 .read_cdda_bpc = sr_read_cdda_bpc,
142 .capability = SR_CAPABILITIES,
145 static void sr_kref_release(struct kref *kref);
147 static inline struct scsi_cd *scsi_cd(struct gendisk *disk)
149 return container_of(disk->private_data, struct scsi_cd, driver);
152 static int sr_runtime_suspend(struct device *dev)
154 struct scsi_cd *cd = dev_get_drvdata(dev);
156 if (!cd) /* E.g.: runtime suspend following sr_remove() */
159 if (cd->media_present)
166 * The get and put routines for the struct scsi_cd. Note this entity
167 * has a scsi_device pointer and owns a reference to this.
169 static inline struct scsi_cd *scsi_cd_get(struct gendisk *disk)
171 struct scsi_cd *cd = NULL;
173 mutex_lock(&sr_ref_mutex);
174 if (disk->private_data == NULL)
178 if (scsi_device_get(cd->device)) {
179 kref_put(&cd->kref, sr_kref_release);
183 mutex_unlock(&sr_ref_mutex);
187 static void scsi_cd_put(struct scsi_cd *cd)
189 struct scsi_device *sdev = cd->device;
191 mutex_lock(&sr_ref_mutex);
192 kref_put(&cd->kref, sr_kref_release);
193 scsi_device_put(sdev);
194 mutex_unlock(&sr_ref_mutex);
197 static unsigned int sr_get_events(struct scsi_device *sdev)
200 u8 cmd[] = { GET_EVENT_STATUS_NOTIFICATION,
203 1 << 4, /* notification class: media */
205 0, sizeof(buf), /* allocation length */
208 struct event_header *eh = (void *)buf;
209 struct media_event_desc *med = (void *)(buf + 4);
210 struct scsi_sense_hdr sshdr;
213 result = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buf, sizeof(buf),
214 &sshdr, SR_TIMEOUT, MAX_RETRIES, NULL);
215 if (scsi_sense_valid(&sshdr) && sshdr.sense_key == UNIT_ATTENTION)
216 return DISK_EVENT_MEDIA_CHANGE;
218 if (result || be16_to_cpu(eh->data_len) < sizeof(*med))
221 if (eh->nea || eh->notification_class != 0x4)
224 if (med->media_event_code == 1)
225 return DISK_EVENT_EJECT_REQUEST;
226 else if (med->media_event_code == 2)
227 return DISK_EVENT_MEDIA_CHANGE;
228 else if (med->media_event_code == 3)
229 return DISK_EVENT_MEDIA_CHANGE;
234 * This function checks to see if the media has been changed or eject
235 * button has been pressed. It is possible that we have already
236 * sensed a change, or the drive may have sensed one and not yet
237 * reported it. The past events are accumulated in sdev->changed and
238 * returned together with the current state.
240 static unsigned int sr_check_events(struct cdrom_device_info *cdi,
241 unsigned int clearing, int slot)
243 struct scsi_cd *cd = cdi->handle;
245 struct scsi_sense_hdr sshdr;
249 /* no changer support */
250 if (CDSL_CURRENT != slot)
253 events = sr_get_events(cd->device);
254 cd->get_event_changed |= events & DISK_EVENT_MEDIA_CHANGE;
257 * If earlier GET_EVENT_STATUS_NOTIFICATION and TUR did not agree
258 * for several times in a row. We rely on TUR only for this likely
259 * broken device, to prevent generating incorrect media changed
260 * events for every open().
262 if (cd->ignore_get_event) {
263 events &= ~DISK_EVENT_MEDIA_CHANGE;
268 * GET_EVENT_STATUS_NOTIFICATION is enough unless MEDIA_CHANGE
269 * is being cleared. Note that there are devices which hang
270 * if asked to execute TUR repeatedly.
272 if (cd->device->changed) {
273 events |= DISK_EVENT_MEDIA_CHANGE;
274 cd->device->changed = 0;
275 cd->tur_changed = true;
278 if (!(clearing & DISK_EVENT_MEDIA_CHANGE))
281 /* let's see whether the media is there with TUR */
282 last_present = cd->media_present;
283 ret = scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES, &sshdr);
286 * Media is considered to be present if TUR succeeds or fails with
287 * sense data indicating something other than media-not-present
290 cd->media_present = scsi_status_is_good(ret) ||
291 (scsi_sense_valid(&sshdr) && sshdr.asc != 0x3a);
293 if (last_present != cd->media_present)
294 cd->device->changed = 1;
296 if (cd->device->changed) {
297 events |= DISK_EVENT_MEDIA_CHANGE;
298 cd->device->changed = 0;
299 cd->tur_changed = true;
302 if (cd->ignore_get_event)
305 /* check whether GET_EVENT is reporting spurious MEDIA_CHANGE */
306 if (!cd->tur_changed) {
307 if (cd->get_event_changed) {
308 if (cd->tur_mismatch++ > 8) {
309 sr_printk(KERN_WARNING, cd,
310 "GET_EVENT and TUR disagree continuously, suppress GET_EVENT events\n");
311 cd->ignore_get_event = true;
314 cd->tur_mismatch = 0;
317 cd->tur_changed = false;
318 cd->get_event_changed = false;
324 * sr_done is the interrupt routine for the device driver.
326 * It will be notified on the end of a SCSI read / write, and will take one
327 * of several actions based on success or failure.
329 static int sr_done(struct scsi_cmnd *SCpnt)
331 int result = SCpnt->result;
332 int this_count = scsi_bufflen(SCpnt);
333 int good_bytes = (result == 0 ? this_count : 0);
334 int block_sectors = 0;
336 struct request *rq = scsi_cmd_to_rq(SCpnt);
337 struct scsi_cd *cd = scsi_cd(rq->rq_disk);
340 scmd_printk(KERN_INFO, SCpnt, "done: %x\n", result);
344 * Handle MEDIUM ERRORs or VOLUME OVERFLOWs that indicate partial
345 * success. Since this is a relatively rare error condition, no
346 * care is taken to avoid unnecessary additional work such as
347 * memcpy's that could be avoided.
349 if (scsi_status_is_check_condition(result) &&
350 (SCpnt->sense_buffer[0] & 0x7f) == 0x70) { /* Sense current */
351 switch (SCpnt->sense_buffer[2]) {
353 case VOLUME_OVERFLOW:
354 case ILLEGAL_REQUEST:
355 if (!(SCpnt->sense_buffer[0] & 0x90))
358 get_unaligned_be32(&SCpnt->sense_buffer[3]);
360 block_sectors = bio_sectors(rq->bio);
361 if (block_sectors < 4)
363 if (cd->device->sector_size == 2048)
365 error_sector &= ~(block_sectors - 1);
366 good_bytes = (error_sector - blk_rq_pos(rq)) << 9;
367 if (good_bytes < 0 || good_bytes >= this_count)
370 * The SCSI specification allows for the value
371 * returned by READ CAPACITY to be up to 75 2K
372 * sectors past the last readable block.
373 * Therefore, if we hit a medium error within the
374 * last 75 2K sectors, we decrease the saved size
377 if (error_sector < get_capacity(cd->disk) &&
378 cd->capacity - error_sector < 4 * 75)
379 set_capacity(cd->disk, error_sector);
382 case RECOVERED_ERROR:
383 good_bytes = this_count;
394 static blk_status_t sr_init_command(struct scsi_cmnd *SCpnt)
396 int block = 0, this_count, s_size;
398 struct request *rq = scsi_cmd_to_rq(SCpnt);
401 ret = scsi_alloc_sgtables(SCpnt);
402 if (ret != BLK_STS_OK)
404 cd = scsi_cd(rq->rq_disk);
406 SCSI_LOG_HLQUEUE(1, scmd_printk(KERN_INFO, SCpnt,
407 "Doing sr request, block = %d\n", block));
409 if (!cd->device || !scsi_device_online(cd->device)) {
410 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
411 "Finishing %u sectors\n", blk_rq_sectors(rq)));
412 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
413 "Retry with 0x%p\n", SCpnt));
417 if (cd->device->changed) {
419 * quietly refuse to do anything to a changed disc until the
420 * changed bit has been reset
425 s_size = cd->device->sector_size;
426 if (s_size != 512 && s_size != 1024 && s_size != 2048) {
427 scmd_printk(KERN_ERR, SCpnt, "bad sector size %d\n", s_size);
431 switch (req_op(rq)) {
435 SCpnt->cmnd[0] = WRITE_10;
436 cd->cdi.media_written = 1;
439 SCpnt->cmnd[0] = READ_10;
442 blk_dump_rq_flags(rq, "Unknown sr command");
447 struct scatterlist *sg;
448 int i, size = 0, sg_count = scsi_sg_count(SCpnt);
450 scsi_for_each_sg(SCpnt, sg, sg_count, i)
453 if (size != scsi_bufflen(SCpnt)) {
454 scmd_printk(KERN_ERR, SCpnt,
455 "mismatch count %d, bytes %d\n",
456 size, scsi_bufflen(SCpnt));
457 if (scsi_bufflen(SCpnt) > size)
458 SCpnt->sdb.length = size;
463 * request doesn't start on hw block boundary, add scatter pads
465 if (((unsigned int)blk_rq_pos(rq) % (s_size >> 9)) ||
466 (scsi_bufflen(SCpnt) % s_size)) {
467 scmd_printk(KERN_NOTICE, SCpnt, "unaligned transfer\n");
471 this_count = (scsi_bufflen(SCpnt) >> 9) / (s_size >> 9);
474 SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
475 "%s %d/%u 512 byte blocks.\n",
476 (rq_data_dir(rq) == WRITE) ?
477 "writing" : "reading",
478 this_count, blk_rq_sectors(rq)));
481 block = (unsigned int)blk_rq_pos(rq) / (s_size >> 9);
483 if (this_count > 0xffff) {
485 SCpnt->sdb.length = this_count * s_size;
488 put_unaligned_be32(block, &SCpnt->cmnd[2]);
489 SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
490 put_unaligned_be16(this_count, &SCpnt->cmnd[7]);
493 * We shouldn't disconnect in the middle of a sector, so with a dumb
494 * host adapter, it's safe to assume that we can at least transfer
495 * this many bytes between each connect / disconnect.
497 SCpnt->transfersize = cd->device->sector_size;
498 SCpnt->underflow = this_count << 9;
499 SCpnt->allowed = MAX_RETRIES;
503 * This indicates that the command is ready from our end to be queued.
507 scsi_free_sgtables(SCpnt);
508 return BLK_STS_IOERR;
511 static void sr_revalidate_disk(struct scsi_cd *cd)
513 struct scsi_sense_hdr sshdr;
515 /* if the unit is not ready, nothing more to do */
516 if (scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES, &sshdr))
518 sr_cd_check(&cd->cdi);
522 static int sr_block_open(struct block_device *bdev, fmode_t mode)
525 struct scsi_device *sdev;
528 cd = scsi_cd_get(bdev->bd_disk);
533 scsi_autopm_get_device(sdev);
534 if (bdev_check_media_change(bdev))
535 sr_revalidate_disk(cd);
537 mutex_lock(&cd->lock);
538 ret = cdrom_open(&cd->cdi, bdev, mode);
539 mutex_unlock(&cd->lock);
541 scsi_autopm_put_device(sdev);
549 static void sr_block_release(struct gendisk *disk, fmode_t mode)
551 struct scsi_cd *cd = scsi_cd(disk);
553 mutex_lock(&cd->lock);
554 cdrom_release(&cd->cdi, mode);
555 mutex_unlock(&cd->lock);
560 static int sr_block_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
563 struct gendisk *disk = bdev->bd_disk;
564 struct scsi_cd *cd = scsi_cd(disk);
565 struct scsi_device *sdev = cd->device;
566 void __user *argp = (void __user *)arg;
569 if (bdev_is_partition(bdev) && !capable(CAP_SYS_RAWIO))
572 mutex_lock(&cd->lock);
574 ret = scsi_ioctl_block_when_processing_errors(sdev, cmd,
575 (mode & FMODE_NDELAY) != 0);
579 scsi_autopm_get_device(sdev);
581 if (ret != CDROMCLOSETRAY && ret != CDROMEJECT) {
582 ret = cdrom_ioctl(&cd->cdi, bdev, mode, cmd, arg);
586 ret = scsi_ioctl(sdev, disk, mode, cmd, argp);
589 scsi_autopm_put_device(sdev);
591 mutex_unlock(&cd->lock);
595 static unsigned int sr_block_check_events(struct gendisk *disk,
596 unsigned int clearing)
598 unsigned int ret = 0;
601 cd = scsi_cd_get(disk);
605 if (!atomic_read(&cd->device->disk_events_disable_depth))
606 ret = cdrom_check_events(&cd->cdi, clearing);
612 static const struct block_device_operations sr_bdops =
614 .owner = THIS_MODULE,
615 .open = sr_block_open,
616 .release = sr_block_release,
617 .ioctl = sr_block_ioctl,
618 .compat_ioctl = blkdev_compat_ptr_ioctl,
619 .check_events = sr_block_check_events,
622 static int sr_open(struct cdrom_device_info *cdi, int purpose)
624 struct scsi_cd *cd = cdi->handle;
625 struct scsi_device *sdev = cd->device;
629 * If the device is in error recovery, wait until it is done.
630 * If the device is offline, then disallow any access to it.
633 if (!scsi_block_when_processing_errors(sdev))
642 static void sr_release(struct cdrom_device_info *cdi)
646 static int sr_probe(struct device *dev)
648 struct scsi_device *sdev = to_scsi_device(dev);
649 struct gendisk *disk;
653 scsi_autopm_get_device(sdev);
655 if (sdev->type != TYPE_ROM && sdev->type != TYPE_WORM)
659 cd = kzalloc(sizeof(*cd), GFP_KERNEL);
663 kref_init(&cd->kref);
665 disk = __alloc_disk_node(sdev->request_queue, NUMA_NO_NODE,
666 &sr_bio_compl_lkclass);
669 mutex_init(&cd->lock);
671 spin_lock(&sr_index_lock);
672 minor = find_first_zero_bit(sr_index_bits, SR_DISKS);
673 if (minor == SR_DISKS) {
674 spin_unlock(&sr_index_lock);
678 __set_bit(minor, sr_index_bits);
679 spin_unlock(&sr_index_lock);
681 disk->major = SCSI_CDROM_MAJOR;
682 disk->first_minor = minor;
684 sprintf(disk->disk_name, "sr%d", minor);
685 disk->fops = &sr_bdops;
686 disk->flags = GENHD_FL_CD | GENHD_FL_BLOCK_EVENTS_ON_EXCL_WRITE;
687 disk->events = DISK_EVENT_MEDIA_CHANGE | DISK_EVENT_EJECT_REQUEST;
688 disk->event_flags = DISK_EVENT_FLAG_POLL | DISK_EVENT_FLAG_UEVENT;
690 blk_queue_rq_timeout(sdev->request_queue, SR_TIMEOUT);
694 cd->driver = &sr_template;
696 cd->capacity = 0x1fffff;
697 cd->device->changed = 1; /* force recheck CD type */
698 cd->media_present = 1;
700 cd->readcd_known = 0;
703 cd->cdi.ops = &sr_dops;
706 cd->cdi.capacity = 1;
707 sprintf(cd->cdi.name, "sr%d", minor);
709 sdev->sector_size = 2048; /* A guess, just in case */
711 /* FIXME: need to handle a get_capabilities failure properly ?? */
712 get_capabilities(cd);
715 set_capacity(disk, cd->capacity);
716 disk->private_data = &cd->driver;
718 if (register_cdrom(disk, &cd->cdi))
722 * Initialize block layer runtime PM stuffs before the
723 * periodic event checking request gets started in add_disk.
725 blk_pm_runtime_init(sdev->request_queue, dev);
727 dev_set_drvdata(dev, cd);
728 disk->flags |= GENHD_FL_REMOVABLE;
729 sr_revalidate_disk(cd);
730 device_add_disk(&sdev->sdev_gendev, disk, NULL);
732 sdev_printk(KERN_DEBUG, sdev,
733 "Attached scsi CD-ROM %s\n", cd->cdi.name);
734 scsi_autopm_put_device(cd->device);
739 spin_lock(&sr_index_lock);
740 clear_bit(minor, sr_index_bits);
741 spin_unlock(&sr_index_lock);
744 mutex_destroy(&cd->lock);
748 scsi_autopm_put_device(sdev);
753 static void get_sectorsize(struct scsi_cd *cd)
755 unsigned char cmd[10];
756 unsigned char buffer[8];
757 int the_result, retries = 3;
759 struct request_queue *queue;
762 cmd[0] = READ_CAPACITY;
763 memset((void *) &cmd[1], 0, 9);
764 memset(buffer, 0, sizeof(buffer));
766 /* Do the command and wait.. */
767 the_result = scsi_execute_req(cd->device, cmd, DMA_FROM_DEVICE,
768 buffer, sizeof(buffer), NULL,
769 SR_TIMEOUT, MAX_RETRIES, NULL);
773 } while (the_result && retries);
777 cd->capacity = 0x1fffff;
778 sector_size = 2048; /* A guess, just in case */
782 cd->capacity = 1 + get_unaligned_be32(&buffer[0]);
784 * READ_CAPACITY doesn't return the correct size on
785 * certain UDF media. If last_written is larger, use
788 * http://bugzilla.kernel.org/show_bug.cgi?id=9668
790 if (!cdrom_get_last_written(&cd->cdi, &last_written))
791 cd->capacity = max_t(long, cd->capacity, last_written);
793 sector_size = get_unaligned_be32(&buffer[4]);
794 switch (sector_size) {
796 * HP 4020i CD-Recorder reports 2340 byte sectors
797 * Philips CD-Writers report 2352 byte sectors
799 * Use 2k sectors for them..
812 sr_printk(KERN_INFO, cd,
813 "unsupported sector size %d.", sector_size);
817 cd->device->sector_size = sector_size;
820 * Add this so that we have the ability to correctly gauge
821 * what the device is capable of.
823 set_capacity(cd->disk, cd->capacity);
826 queue = cd->device->request_queue;
827 blk_queue_logical_block_size(queue, sector_size);
832 static void get_capabilities(struct scsi_cd *cd)
834 unsigned char *buffer;
835 struct scsi_mode_data data;
836 struct scsi_sense_hdr sshdr;
837 unsigned int ms_len = 128;
840 static const char *loadmech[] =
853 /* allocate transfer buffer */
854 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
856 sr_printk(KERN_ERR, cd, "out of memory.\n");
860 /* eat unit attentions */
861 scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES, &sshdr);
863 /* ask for mode page 0x2a */
864 rc = scsi_mode_sense(cd->device, 0, 0x2a, buffer, ms_len,
865 SR_TIMEOUT, 3, &data, NULL);
867 if (rc < 0 || data.length > ms_len ||
868 data.header_length + data.block_descriptor_length > data.length) {
869 /* failed, drive doesn't have capabilities mode page */
871 cd->cdi.mask |= (CDC_CD_R | CDC_CD_RW | CDC_DVD_R |
872 CDC_DVD | CDC_DVD_RAM |
873 CDC_SELECT_DISC | CDC_SELECT_SPEED |
874 CDC_MRW | CDC_MRW_W | CDC_RAM);
876 sr_printk(KERN_INFO, cd, "scsi-1 drive");
880 n = data.header_length + data.block_descriptor_length;
881 cd->cdi.speed = get_unaligned_be16(&buffer[n + 8]) / 176;
882 cd->readcd_known = 1;
883 cd->readcd_cdda = buffer[n + 5] & 0x01;
884 /* print some capability bits */
885 sr_printk(KERN_INFO, cd,
886 "scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n",
887 get_unaligned_be16(&buffer[n + 14]) / 176,
889 buffer[n + 3] & 0x01 ? "writer " : "", /* CD Writer */
890 buffer[n + 3] & 0x20 ? "dvd-ram " : "",
891 buffer[n + 2] & 0x02 ? "cd/rw " : "", /* can read rewriteable */
892 buffer[n + 4] & 0x20 ? "xa/form2 " : "", /* can read xa/from2 */
893 buffer[n + 5] & 0x01 ? "cdda " : "", /* can read audio data */
894 loadmech[buffer[n + 6] >> 5]);
895 if ((buffer[n + 6] >> 5) == 0)
896 /* caddy drives can't close tray... */
897 cd->cdi.mask |= CDC_CLOSE_TRAY;
898 if ((buffer[n + 2] & 0x8) == 0)
899 /* not a DVD drive */
900 cd->cdi.mask |= CDC_DVD;
901 if ((buffer[n + 3] & 0x20) == 0)
902 /* can't write DVD-RAM media */
903 cd->cdi.mask |= CDC_DVD_RAM;
904 if ((buffer[n + 3] & 0x10) == 0)
905 /* can't write DVD-R media */
906 cd->cdi.mask |= CDC_DVD_R;
907 if ((buffer[n + 3] & 0x2) == 0)
908 /* can't write CD-RW media */
909 cd->cdi.mask |= CDC_CD_RW;
910 if ((buffer[n + 3] & 0x1) == 0)
911 /* can't write CD-R media */
912 cd->cdi.mask |= CDC_CD_R;
913 if ((buffer[n + 6] & 0x8) == 0)
915 cd->cdi.mask |= CDC_OPEN_TRAY;
917 if ((buffer[n + 6] >> 5) == mechtype_individual_changer ||
918 (buffer[n + 6] >> 5) == mechtype_cartridge_changer)
920 cdrom_number_of_slots(&cd->cdi);
921 if (cd->cdi.capacity <= 1)
923 cd->cdi.mask |= CDC_SELECT_DISC;
924 /*else I don't think it can close its tray
925 cd->cdi.mask |= CDC_CLOSE_TRAY; */
928 * if DVD-RAM, MRW-W or CD-RW, we are randomly writable
930 if ((cd->cdi.mask & (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) !=
931 (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) {
939 * sr_packet() is the entry point for the generic commands generated
940 * by the Uniform CD-ROM layer.
942 static int sr_packet(struct cdrom_device_info *cdi,
943 struct packet_command *cgc)
945 struct scsi_cd *cd = cdi->handle;
946 struct scsi_device *sdev = cd->device;
948 if (cgc->cmd[0] == GPCMD_READ_DISC_INFO && sdev->no_read_disc_info)
949 return -EDRIVE_CANT_DO_THIS;
951 if (cgc->timeout <= 0)
952 cgc->timeout = IOCTL_TIMEOUT;
954 sr_do_ioctl(cd, cgc);
959 static int sr_read_cdda_bpc(struct cdrom_device_info *cdi, void __user *ubuf,
960 u32 lba, u32 nr, u8 *last_sense)
962 struct gendisk *disk = cdi->disk;
963 u32 len = nr * CD_FRAMESIZE_RAW;
964 struct scsi_request *req;
969 rq = blk_get_request(disk->queue, REQ_OP_DRV_IN, 0);
974 ret = blk_rq_map_user(disk->queue, rq, NULL, ubuf, len, GFP_KERNEL);
976 goto out_put_request;
978 req->cmd[0] = GPCMD_READ_CD;
979 req->cmd[1] = 1 << 2;
980 req->cmd[2] = (lba >> 24) & 0xff;
981 req->cmd[3] = (lba >> 16) & 0xff;
982 req->cmd[4] = (lba >> 8) & 0xff;
983 req->cmd[5] = lba & 0xff;
984 req->cmd[6] = (nr >> 16) & 0xff;
985 req->cmd[7] = (nr >> 8) & 0xff;
986 req->cmd[8] = nr & 0xff;
989 rq->timeout = 60 * HZ;
992 blk_execute_rq(disk, rq, 0);
993 if (scsi_req(rq)->result) {
994 struct scsi_sense_hdr sshdr;
996 scsi_normalize_sense(req->sense, req->sense_len,
998 *last_sense = sshdr.sense_key;
1002 if (blk_rq_unmap_user(bio))
1005 blk_put_request(rq);
1011 * sr_kref_release - Called to free the scsi_cd structure
1012 * @kref: pointer to embedded kref
1014 * sr_ref_mutex must be held entering this routine. Because it is
1015 * called on last put, you should always use the scsi_cd_get()
1016 * scsi_cd_put() helpers which manipulate the semaphore directly
1017 * and never do a direct kref_put().
1019 static void sr_kref_release(struct kref *kref)
1021 struct scsi_cd *cd = container_of(kref, struct scsi_cd, kref);
1022 struct gendisk *disk = cd->disk;
1024 spin_lock(&sr_index_lock);
1025 clear_bit(MINOR(disk_devt(disk)), sr_index_bits);
1026 spin_unlock(&sr_index_lock);
1028 unregister_cdrom(&cd->cdi);
1030 disk->private_data = NULL;
1034 mutex_destroy(&cd->lock);
1039 static int sr_remove(struct device *dev)
1041 struct scsi_cd *cd = dev_get_drvdata(dev);
1043 scsi_autopm_get_device(cd->device);
1045 del_gendisk(cd->disk);
1046 dev_set_drvdata(dev, NULL);
1048 mutex_lock(&sr_ref_mutex);
1049 kref_put(&cd->kref, sr_kref_release);
1050 mutex_unlock(&sr_ref_mutex);
1055 static int __init init_sr(void)
1059 rc = register_blkdev(SCSI_CDROM_MAJOR, "sr");
1062 rc = scsi_register_driver(&sr_template.gendrv);
1064 unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
1069 static void __exit exit_sr(void)
1071 scsi_unregister_driver(&sr_template.gendrv);
1072 unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
1075 module_init(init_sr);
1076 module_exit(exit_sr);
1077 MODULE_LICENSE("GPL");