2 * sr.c Copyright (C) 1992 David Giller
3 * Copyright (C) 1993, 1994, 1995, 1999 Eric Youngdale
6 * sd.c Copyright (C) 1992 Drew Eckhardt
7 * Linux scsi disk driver by
8 * Drew Eckhardt <drew@colorado.edu>
10 * Modified by Eric Youngdale ericy@andante.org to
11 * add scatter-gather, multiple outstanding request, and other
14 * Modified by Eric Youngdale eric@andante.org to support loadable
15 * low-level scsi drivers.
17 * Modified by Thomas Quinot thomas@melchior.cuivre.fdn.fr to
20 * Modified by Gerd Knorr <kraxel@cs.tu-berlin.de> to support the
21 * generic cdrom interface
23 * Modified by Jens Axboe <axboe@suse.de> - Uniform sr_packet()
24 * interface, capabilities probe additions, ioctl cleanups, etc.
26 * Modified by Richard Gooch <rgooch@atnf.csiro.au> to support devfs
28 * Modified by Jens Axboe <axboe@suse.de> - support DVD-RAM
29 * transparently and lose the GHOST hack
31 * Modified by Arnaldo Carvalho de Melo <acme@conectiva.com.br>
32 * check resource allocation in sr_init and some cleanups
35 #include <linux/module.h>
37 #include <linux/kernel.h>
39 #include <linux/bio.h>
40 #include <linux/string.h>
41 #include <linux/errno.h>
42 #include <linux/cdrom.h>
43 #include <linux/interrupt.h>
44 #include <linux/init.h>
45 #include <linux/blkdev.h>
46 #include <linux/mutex.h>
47 #include <linux/slab.h>
48 #include <asm/uaccess.h>
50 #include <scsi/scsi.h>
51 #include <scsi/scsi_dbg.h>
52 #include <scsi/scsi_device.h>
53 #include <scsi/scsi_driver.h>
54 #include <scsi/scsi_cmnd.h>
55 #include <scsi/scsi_eh.h>
56 #include <scsi/scsi_host.h>
57 #include <scsi/scsi_ioctl.h> /* For the door lock/unlock commands */
59 #include "scsi_logging.h"
63 MODULE_DESCRIPTION("SCSI cdrom (sr) driver");
64 MODULE_LICENSE("GPL");
65 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_CDROM_MAJOR);
66 MODULE_ALIAS_SCSI_DEVICE(TYPE_ROM);
67 MODULE_ALIAS_SCSI_DEVICE(TYPE_WORM);
71 #define SR_CAPABILITIES \
72 (CDC_CLOSE_TRAY|CDC_OPEN_TRAY|CDC_LOCK|CDC_SELECT_SPEED| \
73 CDC_SELECT_DISC|CDC_MULTI_SESSION|CDC_MCN|CDC_MEDIA_CHANGED| \
74 CDC_PLAY_AUDIO|CDC_RESET|CDC_DRIVE_STATUS| \
75 CDC_CD_R|CDC_CD_RW|CDC_DVD|CDC_DVD_R|CDC_DVD_RAM|CDC_GENERIC_PACKET| \
76 CDC_MRW|CDC_MRW_W|CDC_RAM)
78 static DEFINE_MUTEX(sr_mutex);
79 static int sr_probe(struct device *);
80 static int sr_remove(struct device *);
81 static int sr_done(struct scsi_cmnd *);
83 static struct scsi_driver sr_template = {
93 static unsigned long sr_index_bits[SR_DISKS / BITS_PER_LONG];
94 static DEFINE_SPINLOCK(sr_index_lock);
96 /* This semaphore is used to mediate the 0->1 reference get in the
97 * face of object destruction (i.e. we can't allow a get on an
98 * object after last put) */
99 static DEFINE_MUTEX(sr_ref_mutex);
101 static int sr_open(struct cdrom_device_info *, int);
102 static void sr_release(struct cdrom_device_info *);
104 static void get_sectorsize(struct scsi_cd *);
105 static void get_capabilities(struct scsi_cd *);
107 static unsigned int sr_check_events(struct cdrom_device_info *cdi,
108 unsigned int clearing, int slot);
109 static int sr_packet(struct cdrom_device_info *, struct packet_command *);
111 static struct cdrom_device_ops sr_dops = {
113 .release = sr_release,
114 .drive_status = sr_drive_status,
115 .check_events = sr_check_events,
116 .tray_move = sr_tray_move,
117 .lock_door = sr_lock_door,
118 .select_speed = sr_select_speed,
119 .get_last_session = sr_get_last_session,
120 .get_mcn = sr_get_mcn,
122 .audio_ioctl = sr_audio_ioctl,
123 .capability = SR_CAPABILITIES,
124 .generic_packet = sr_packet,
127 static void sr_kref_release(struct kref *kref);
129 static inline struct scsi_cd *scsi_cd(struct gendisk *disk)
131 return container_of(disk->private_data, struct scsi_cd, driver);
135 * The get and put routines for the struct scsi_cd. Note this entity
136 * has a scsi_device pointer and owns a reference to this.
138 static inline struct scsi_cd *scsi_cd_get(struct gendisk *disk)
140 struct scsi_cd *cd = NULL;
142 mutex_lock(&sr_ref_mutex);
143 if (disk->private_data == NULL)
147 if (scsi_device_get(cd->device))
152 kref_put(&cd->kref, sr_kref_release);
155 mutex_unlock(&sr_ref_mutex);
159 static void scsi_cd_put(struct scsi_cd *cd)
161 struct scsi_device *sdev = cd->device;
163 mutex_lock(&sr_ref_mutex);
164 kref_put(&cd->kref, sr_kref_release);
165 scsi_device_put(sdev);
166 mutex_unlock(&sr_ref_mutex);
169 static unsigned int sr_get_events(struct scsi_device *sdev)
172 u8 cmd[] = { GET_EVENT_STATUS_NOTIFICATION,
175 1 << 4, /* notification class: media */
177 0, sizeof(buf), /* allocation length */
180 struct event_header *eh = (void *)buf;
181 struct media_event_desc *med = (void *)(buf + 4);
182 struct scsi_sense_hdr sshdr;
185 result = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buf, sizeof(buf),
186 &sshdr, SR_TIMEOUT, MAX_RETRIES, NULL);
187 if (scsi_sense_valid(&sshdr) && sshdr.sense_key == UNIT_ATTENTION)
188 return DISK_EVENT_MEDIA_CHANGE;
190 if (result || be16_to_cpu(eh->data_len) < sizeof(*med))
193 if (eh->nea || eh->notification_class != 0x4)
196 if (med->media_event_code == 1)
197 return DISK_EVENT_EJECT_REQUEST;
198 else if (med->media_event_code == 2)
199 return DISK_EVENT_MEDIA_CHANGE;
204 * This function checks to see if the media has been changed or eject
205 * button has been pressed. It is possible that we have already
206 * sensed a change, or the drive may have sensed one and not yet
207 * reported it. The past events are accumulated in sdev->changed and
208 * returned together with the current state.
210 static unsigned int sr_check_events(struct cdrom_device_info *cdi,
211 unsigned int clearing, int slot)
213 struct scsi_cd *cd = cdi->handle;
215 struct scsi_sense_hdr sshdr;
219 /* no changer support */
220 if (CDSL_CURRENT != slot)
223 events = sr_get_events(cd->device);
225 * GET_EVENT_STATUS_NOTIFICATION is enough unless MEDIA_CHANGE
226 * is being cleared. Note that there are devices which hang
227 * if asked to execute TUR repeatedly.
229 if (!(clearing & DISK_EVENT_MEDIA_CHANGE))
232 /* let's see whether the media is there with TUR */
233 last_present = cd->media_present;
234 ret = scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES, &sshdr);
237 * Media is considered to be present if TUR succeeds or fails with
238 * sense data indicating something other than media-not-present
241 cd->media_present = scsi_status_is_good(ret) ||
242 (scsi_sense_valid(&sshdr) && sshdr.asc != 0x3a);
244 if (last_present != cd->media_present)
245 events |= DISK_EVENT_MEDIA_CHANGE;
247 if (cd->device->changed) {
248 events |= DISK_EVENT_MEDIA_CHANGE;
249 cd->device->changed = 0;
252 /* for backward compatibility */
253 if (events & DISK_EVENT_MEDIA_CHANGE)
254 sdev_evt_send_simple(cd->device, SDEV_EVT_MEDIA_CHANGE,
260 * sr_done is the interrupt routine for the device driver.
262 * It will be notified on the end of a SCSI read / write, and will take one
263 * of several actions based on success or failure.
265 static int sr_done(struct scsi_cmnd *SCpnt)
267 int result = SCpnt->result;
268 int this_count = scsi_bufflen(SCpnt);
269 int good_bytes = (result == 0 ? this_count : 0);
270 int block_sectors = 0;
272 struct scsi_cd *cd = scsi_cd(SCpnt->request->rq_disk);
275 printk("sr.c done: %x\n", result);
279 * Handle MEDIUM ERRORs or VOLUME OVERFLOWs that indicate partial
280 * success. Since this is a relatively rare error condition, no
281 * care is taken to avoid unnecessary additional work such as
282 * memcpy's that could be avoided.
284 if (driver_byte(result) != 0 && /* An error occurred */
285 (SCpnt->sense_buffer[0] & 0x7f) == 0x70) { /* Sense current */
286 switch (SCpnt->sense_buffer[2]) {
288 case VOLUME_OVERFLOW:
289 case ILLEGAL_REQUEST:
290 if (!(SCpnt->sense_buffer[0] & 0x90))
292 error_sector = (SCpnt->sense_buffer[3] << 24) |
293 (SCpnt->sense_buffer[4] << 16) |
294 (SCpnt->sense_buffer[5] << 8) |
295 SCpnt->sense_buffer[6];
296 if (SCpnt->request->bio != NULL)
298 bio_sectors(SCpnt->request->bio);
299 if (block_sectors < 4)
301 if (cd->device->sector_size == 2048)
303 error_sector &= ~(block_sectors - 1);
304 good_bytes = (error_sector -
305 blk_rq_pos(SCpnt->request)) << 9;
306 if (good_bytes < 0 || good_bytes >= this_count)
309 * The SCSI specification allows for the value
310 * returned by READ CAPACITY to be up to 75 2K
311 * sectors past the last readable block.
312 * Therefore, if we hit a medium error within the
313 * last 75 2K sectors, we decrease the saved size
316 if (error_sector < get_capacity(cd->disk) &&
317 cd->capacity - error_sector < 4 * 75)
318 set_capacity(cd->disk, error_sector);
321 case RECOVERED_ERROR:
322 good_bytes = this_count;
333 static int sr_prep_fn(struct request_queue *q, struct request *rq)
335 int block = 0, this_count, s_size;
337 struct scsi_cmnd *SCpnt;
338 struct scsi_device *sdp = q->queuedata;
341 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
342 ret = scsi_setup_blk_pc_cmnd(sdp, rq);
344 } else if (rq->cmd_type != REQ_TYPE_FS) {
348 ret = scsi_setup_fs_cmnd(sdp, rq);
349 if (ret != BLKPREP_OK)
352 cd = scsi_cd(rq->rq_disk);
354 /* from here on until we're complete, any goto out
355 * is used for a killable error condition */
358 SCSI_LOG_HLQUEUE(1, printk("Doing sr request, dev = %s, block = %d\n",
359 cd->disk->disk_name, block));
361 if (!cd->device || !scsi_device_online(cd->device)) {
362 SCSI_LOG_HLQUEUE(2, printk("Finishing %u sectors\n",
363 blk_rq_sectors(rq)));
364 SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt));
368 if (cd->device->changed) {
370 * quietly refuse to do anything to a changed disc until the
371 * changed bit has been reset
377 * we do lazy blocksize switching (when reading XA sectors,
378 * see CDROMREADMODE2 ioctl)
380 s_size = cd->device->sector_size;
383 sr_set_blocklength(cd, 2048);
385 printk("sr: can't switch blocksize: in interrupt\n");
388 if (s_size != 512 && s_size != 1024 && s_size != 2048) {
389 scmd_printk(KERN_ERR, SCpnt, "bad sector size %d\n", s_size);
393 if (rq_data_dir(rq) == WRITE) {
394 if (!cd->device->writeable)
396 SCpnt->cmnd[0] = WRITE_10;
397 SCpnt->sc_data_direction = DMA_TO_DEVICE;
398 cd->cdi.media_written = 1;
399 } else if (rq_data_dir(rq) == READ) {
400 SCpnt->cmnd[0] = READ_10;
401 SCpnt->sc_data_direction = DMA_FROM_DEVICE;
403 blk_dump_rq_flags(rq, "Unknown sr command");
408 struct scatterlist *sg;
409 int i, size = 0, sg_count = scsi_sg_count(SCpnt);
411 scsi_for_each_sg(SCpnt, sg, sg_count, i)
414 if (size != scsi_bufflen(SCpnt)) {
415 scmd_printk(KERN_ERR, SCpnt,
416 "mismatch count %d, bytes %d\n",
417 size, scsi_bufflen(SCpnt));
418 if (scsi_bufflen(SCpnt) > size)
419 SCpnt->sdb.length = size;
424 * request doesn't start on hw block boundary, add scatter pads
426 if (((unsigned int)blk_rq_pos(rq) % (s_size >> 9)) ||
427 (scsi_bufflen(SCpnt) % s_size)) {
428 scmd_printk(KERN_NOTICE, SCpnt, "unaligned transfer\n");
432 this_count = (scsi_bufflen(SCpnt) >> 9) / (s_size >> 9);
435 SCSI_LOG_HLQUEUE(2, printk("%s : %s %d/%u 512 byte blocks.\n",
437 (rq_data_dir(rq) == WRITE) ?
438 "writing" : "reading",
439 this_count, blk_rq_sectors(rq)));
442 block = (unsigned int)blk_rq_pos(rq) / (s_size >> 9);
444 if (this_count > 0xffff) {
446 SCpnt->sdb.length = this_count * s_size;
449 SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
450 SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
451 SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
452 SCpnt->cmnd[5] = (unsigned char) block & 0xff;
453 SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
454 SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
455 SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
458 * We shouldn't disconnect in the middle of a sector, so with a dumb
459 * host adapter, it's safe to assume that we can at least transfer
460 * this many bytes between each connect / disconnect.
462 SCpnt->transfersize = cd->device->sector_size;
463 SCpnt->underflow = this_count << 9;
464 SCpnt->allowed = MAX_RETRIES;
467 * This indicates that the command is ready from our end to be
472 return scsi_prep_return(q, rq, ret);
475 static int sr_block_open(struct block_device *bdev, fmode_t mode)
480 mutex_lock(&sr_mutex);
481 cd = scsi_cd_get(bdev->bd_disk);
483 ret = cdrom_open(&cd->cdi, bdev, mode);
487 mutex_unlock(&sr_mutex);
491 static int sr_block_release(struct gendisk *disk, fmode_t mode)
493 struct scsi_cd *cd = scsi_cd(disk);
494 mutex_lock(&sr_mutex);
495 cdrom_release(&cd->cdi, mode);
497 mutex_unlock(&sr_mutex);
501 static int sr_block_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
504 struct scsi_cd *cd = scsi_cd(bdev->bd_disk);
505 struct scsi_device *sdev = cd->device;
506 void __user *argp = (void __user *)arg;
509 mutex_lock(&sr_mutex);
512 * Send SCSI addressing ioctls directly to mid level, send other
513 * ioctls to cdrom/block level.
516 case SCSI_IOCTL_GET_IDLUN:
517 case SCSI_IOCTL_GET_BUS_NUMBER:
518 ret = scsi_ioctl(sdev, cmd, argp);
522 ret = cdrom_ioctl(&cd->cdi, bdev, mode, cmd, arg);
527 * ENODEV means that we didn't recognise the ioctl, or that we
528 * cannot execute it in the current device state. In either
529 * case fall through to scsi_ioctl, which will return ENDOEV again
530 * if it doesn't recognise the ioctl
532 ret = scsi_nonblockable_ioctl(sdev, cmd, argp,
533 (mode & FMODE_NDELAY) != 0);
536 ret = scsi_ioctl(sdev, cmd, argp);
539 mutex_unlock(&sr_mutex);
543 static unsigned int sr_block_check_events(struct gendisk *disk,
544 unsigned int clearing)
546 struct scsi_cd *cd = scsi_cd(disk);
547 return cdrom_check_events(&cd->cdi, clearing);
550 static int sr_block_revalidate_disk(struct gendisk *disk)
552 struct scsi_cd *cd = scsi_cd(disk);
553 struct scsi_sense_hdr sshdr;
555 /* if the unit is not ready, nothing more to do */
556 if (scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES, &sshdr))
559 sr_cd_check(&cd->cdi);
564 static const struct block_device_operations sr_bdops =
566 .owner = THIS_MODULE,
567 .open = sr_block_open,
568 .release = sr_block_release,
569 .ioctl = sr_block_ioctl,
570 .check_events = sr_block_check_events,
571 .revalidate_disk = sr_block_revalidate_disk,
573 * No compat_ioctl for now because sr_block_ioctl never
574 * seems to pass arbitary ioctls down to host drivers.
578 static int sr_open(struct cdrom_device_info *cdi, int purpose)
580 struct scsi_cd *cd = cdi->handle;
581 struct scsi_device *sdev = cd->device;
585 * If the device is in error recovery, wait until it is done.
586 * If the device is offline, then disallow any access to it.
589 if (!scsi_block_when_processing_errors(sdev))
598 static void sr_release(struct cdrom_device_info *cdi)
600 struct scsi_cd *cd = cdi->handle;
602 if (cd->device->sector_size > 2048)
603 sr_set_blocklength(cd, 2048);
607 static int sr_probe(struct device *dev)
609 struct scsi_device *sdev = to_scsi_device(dev);
610 struct gendisk *disk;
615 if (sdev->type != TYPE_ROM && sdev->type != TYPE_WORM)
619 cd = kzalloc(sizeof(*cd), GFP_KERNEL);
623 kref_init(&cd->kref);
625 disk = alloc_disk(1);
629 spin_lock(&sr_index_lock);
630 minor = find_first_zero_bit(sr_index_bits, SR_DISKS);
631 if (minor == SR_DISKS) {
632 spin_unlock(&sr_index_lock);
636 __set_bit(minor, sr_index_bits);
637 spin_unlock(&sr_index_lock);
639 disk->major = SCSI_CDROM_MAJOR;
640 disk->first_minor = minor;
641 sprintf(disk->disk_name, "sr%d", minor);
642 disk->fops = &sr_bdops;
643 disk->flags = GENHD_FL_CD;
644 disk->events = DISK_EVENT_MEDIA_CHANGE | DISK_EVENT_EJECT_REQUEST;
646 blk_queue_rq_timeout(sdev->request_queue, SR_TIMEOUT);
650 cd->driver = &sr_template;
652 cd->capacity = 0x1fffff;
653 cd->device->changed = 1; /* force recheck CD type */
654 cd->media_present = 1;
656 cd->readcd_known = 0;
659 cd->cdi.ops = &sr_dops;
662 cd->cdi.capacity = 1;
663 sprintf(cd->cdi.name, "sr%d", minor);
665 sdev->sector_size = 2048; /* A guess, just in case */
667 /* FIXME: need to handle a get_capabilities failure properly ?? */
668 get_capabilities(cd);
669 blk_queue_prep_rq(sdev->request_queue, sr_prep_fn);
672 disk->driverfs_dev = &sdev->sdev_gendev;
673 set_capacity(disk, cd->capacity);
674 disk->private_data = &cd->driver;
675 disk->queue = sdev->request_queue;
678 if (register_cdrom(&cd->cdi))
681 dev_set_drvdata(dev, cd);
682 disk->flags |= GENHD_FL_REMOVABLE;
685 sdev_printk(KERN_DEBUG, sdev,
686 "Attached scsi CD-ROM %s\n", cd->cdi.name);
698 static void get_sectorsize(struct scsi_cd *cd)
700 unsigned char cmd[10];
701 unsigned char buffer[8];
702 int the_result, retries = 3;
704 struct request_queue *queue;
707 cmd[0] = READ_CAPACITY;
708 memset((void *) &cmd[1], 0, 9);
709 memset(buffer, 0, sizeof(buffer));
711 /* Do the command and wait.. */
712 the_result = scsi_execute_req(cd->device, cmd, DMA_FROM_DEVICE,
713 buffer, sizeof(buffer), NULL,
714 SR_TIMEOUT, MAX_RETRIES, NULL);
718 } while (the_result && retries);
722 cd->capacity = 0x1fffff;
723 sector_size = 2048; /* A guess, just in case */
727 cd->capacity = 1 + ((buffer[0] << 24) | (buffer[1] << 16) |
728 (buffer[2] << 8) | buffer[3]);
730 * READ_CAPACITY doesn't return the correct size on
731 * certain UDF media. If last_written is larger, use
734 * http://bugzilla.kernel.org/show_bug.cgi?id=9668
736 if (!cdrom_get_last_written(&cd->cdi, &last_written))
737 cd->capacity = max_t(long, cd->capacity, last_written);
739 sector_size = (buffer[4] << 24) |
740 (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
741 switch (sector_size) {
743 * HP 4020i CD-Recorder reports 2340 byte sectors
744 * Philips CD-Writers report 2352 byte sectors
746 * Use 2k sectors for them..
759 printk("%s: unsupported sector size %d.\n",
760 cd->cdi.name, sector_size);
764 cd->device->sector_size = sector_size;
767 * Add this so that we have the ability to correctly gauge
768 * what the device is capable of.
770 set_capacity(cd->disk, cd->capacity);
773 queue = cd->device->request_queue;
774 blk_queue_logical_block_size(queue, sector_size);
779 static void get_capabilities(struct scsi_cd *cd)
781 unsigned char *buffer;
782 struct scsi_mode_data data;
783 struct scsi_sense_hdr sshdr;
786 static const char *loadmech[] =
799 /* allocate transfer buffer */
800 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
802 printk(KERN_ERR "sr: out of memory.\n");
806 /* eat unit attentions */
807 scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES, &sshdr);
809 /* ask for mode page 0x2a */
810 rc = scsi_mode_sense(cd->device, 0, 0x2a, buffer, 128,
811 SR_TIMEOUT, 3, &data, NULL);
813 if (!scsi_status_is_good(rc)) {
814 /* failed, drive doesn't have capabilities mode page */
816 cd->cdi.mask |= (CDC_CD_R | CDC_CD_RW | CDC_DVD_R |
817 CDC_DVD | CDC_DVD_RAM |
818 CDC_SELECT_DISC | CDC_SELECT_SPEED |
819 CDC_MRW | CDC_MRW_W | CDC_RAM);
821 printk("%s: scsi-1 drive\n", cd->cdi.name);
825 n = data.header_length + data.block_descriptor_length;
826 cd->cdi.speed = ((buffer[n + 8] << 8) + buffer[n + 9]) / 176;
827 cd->readcd_known = 1;
828 cd->readcd_cdda = buffer[n + 5] & 0x01;
829 /* print some capability bits */
830 printk("%s: scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n", cd->cdi.name,
831 ((buffer[n + 14] << 8) + buffer[n + 15]) / 176,
833 buffer[n + 3] & 0x01 ? "writer " : "", /* CD Writer */
834 buffer[n + 3] & 0x20 ? "dvd-ram " : "",
835 buffer[n + 2] & 0x02 ? "cd/rw " : "", /* can read rewriteable */
836 buffer[n + 4] & 0x20 ? "xa/form2 " : "", /* can read xa/from2 */
837 buffer[n + 5] & 0x01 ? "cdda " : "", /* can read audio data */
838 loadmech[buffer[n + 6] >> 5]);
839 if ((buffer[n + 6] >> 5) == 0)
840 /* caddy drives can't close tray... */
841 cd->cdi.mask |= CDC_CLOSE_TRAY;
842 if ((buffer[n + 2] & 0x8) == 0)
843 /* not a DVD drive */
844 cd->cdi.mask |= CDC_DVD;
845 if ((buffer[n + 3] & 0x20) == 0)
846 /* can't write DVD-RAM media */
847 cd->cdi.mask |= CDC_DVD_RAM;
848 if ((buffer[n + 3] & 0x10) == 0)
849 /* can't write DVD-R media */
850 cd->cdi.mask |= CDC_DVD_R;
851 if ((buffer[n + 3] & 0x2) == 0)
852 /* can't write CD-RW media */
853 cd->cdi.mask |= CDC_CD_RW;
854 if ((buffer[n + 3] & 0x1) == 0)
855 /* can't write CD-R media */
856 cd->cdi.mask |= CDC_CD_R;
857 if ((buffer[n + 6] & 0x8) == 0)
859 cd->cdi.mask |= CDC_OPEN_TRAY;
861 if ((buffer[n + 6] >> 5) == mechtype_individual_changer ||
862 (buffer[n + 6] >> 5) == mechtype_cartridge_changer)
864 cdrom_number_of_slots(&cd->cdi);
865 if (cd->cdi.capacity <= 1)
867 cd->cdi.mask |= CDC_SELECT_DISC;
868 /*else I don't think it can close its tray
869 cd->cdi.mask |= CDC_CLOSE_TRAY; */
872 * if DVD-RAM, MRW-W or CD-RW, we are randomly writable
874 if ((cd->cdi.mask & (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) !=
875 (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) {
876 cd->device->writeable = 1;
883 * sr_packet() is the entry point for the generic commands generated
884 * by the Uniform CD-ROM layer.
886 static int sr_packet(struct cdrom_device_info *cdi,
887 struct packet_command *cgc)
889 struct scsi_cd *cd = cdi->handle;
890 struct scsi_device *sdev = cd->device;
892 if (cgc->cmd[0] == GPCMD_READ_DISC_INFO && sdev->no_read_disc_info)
893 return -EDRIVE_CANT_DO_THIS;
895 if (cgc->timeout <= 0)
896 cgc->timeout = IOCTL_TIMEOUT;
898 sr_do_ioctl(cd, cgc);
904 * sr_kref_release - Called to free the scsi_cd structure
905 * @kref: pointer to embedded kref
907 * sr_ref_mutex must be held entering this routine. Because it is
908 * called on last put, you should always use the scsi_cd_get()
909 * scsi_cd_put() helpers which manipulate the semaphore directly
910 * and never do a direct kref_put().
912 static void sr_kref_release(struct kref *kref)
914 struct scsi_cd *cd = container_of(kref, struct scsi_cd, kref);
915 struct gendisk *disk = cd->disk;
917 spin_lock(&sr_index_lock);
918 clear_bit(MINOR(disk_devt(disk)), sr_index_bits);
919 spin_unlock(&sr_index_lock);
921 unregister_cdrom(&cd->cdi);
923 disk->private_data = NULL;
930 static int sr_remove(struct device *dev)
932 struct scsi_cd *cd = dev_get_drvdata(dev);
934 blk_queue_prep_rq(cd->device->request_queue, scsi_prep_fn);
935 del_gendisk(cd->disk);
937 mutex_lock(&sr_ref_mutex);
938 kref_put(&cd->kref, sr_kref_release);
939 mutex_unlock(&sr_ref_mutex);
944 static int __init init_sr(void)
948 rc = register_blkdev(SCSI_CDROM_MAJOR, "sr");
951 rc = scsi_register_driver(&sr_template.gendrv);
953 unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
958 static void __exit exit_sr(void)
960 scsi_unregister_driver(&sr_template.gendrv);
961 unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
964 module_init(init_sr);
965 module_exit(exit_sr);
966 MODULE_LICENSE("GPL");