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 <asm/uaccess.h>
49 #include <scsi/scsi.h>
50 #include <scsi/scsi_dbg.h>
51 #include <scsi/scsi_device.h>
52 #include <scsi/scsi_driver.h>
53 #include <scsi/scsi_cmnd.h>
54 #include <scsi/scsi_eh.h>
55 #include <scsi/scsi_host.h>
56 #include <scsi/scsi_ioctl.h> /* For the door lock/unlock commands */
58 #include "scsi_logging.h"
62 MODULE_DESCRIPTION("SCSI cdrom (sr) driver");
63 MODULE_LICENSE("GPL");
64 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_CDROM_MAJOR);
65 MODULE_ALIAS_SCSI_DEVICE(TYPE_ROM);
66 MODULE_ALIAS_SCSI_DEVICE(TYPE_WORM);
71 #define SR_TIMEOUT (30 * HZ)
72 #define SR_CAPABILITIES \
73 (CDC_CLOSE_TRAY|CDC_OPEN_TRAY|CDC_LOCK|CDC_SELECT_SPEED| \
74 CDC_SELECT_DISC|CDC_MULTI_SESSION|CDC_MCN|CDC_MEDIA_CHANGED| \
75 CDC_PLAY_AUDIO|CDC_RESET|CDC_DRIVE_STATUS| \
76 CDC_CD_R|CDC_CD_RW|CDC_DVD|CDC_DVD_R|CDC_DVD_RAM|CDC_GENERIC_PACKET| \
77 CDC_MRW|CDC_MRW_W|CDC_RAM)
79 static int sr_probe(struct device *);
80 static int sr_remove(struct device *);
82 static struct scsi_driver sr_template = {
91 static unsigned long sr_index_bits[SR_DISKS / BITS_PER_LONG];
92 static DEFINE_SPINLOCK(sr_index_lock);
94 /* This semaphore is used to mediate the 0->1 reference get in the
95 * face of object destruction (i.e. we can't allow a get on an
96 * object after last put) */
97 static DEFINE_MUTEX(sr_ref_mutex);
99 static int sr_open(struct cdrom_device_info *, int);
100 static void sr_release(struct cdrom_device_info *);
102 static void get_sectorsize(struct scsi_cd *);
103 static void get_capabilities(struct scsi_cd *);
105 static int sr_media_change(struct cdrom_device_info *, int);
106 static int sr_packet(struct cdrom_device_info *, struct packet_command *);
108 static struct cdrom_device_ops sr_dops = {
110 .release = sr_release,
111 .drive_status = sr_drive_status,
112 .media_changed = sr_media_change,
113 .tray_move = sr_tray_move,
114 .lock_door = sr_lock_door,
115 .select_speed = sr_select_speed,
116 .get_last_session = sr_get_last_session,
117 .get_mcn = sr_get_mcn,
119 .audio_ioctl = sr_audio_ioctl,
120 .capability = SR_CAPABILITIES,
121 .generic_packet = sr_packet,
124 static void sr_kref_release(struct kref *kref);
126 static inline struct scsi_cd *scsi_cd(struct gendisk *disk)
128 return container_of(disk->private_data, struct scsi_cd, driver);
132 * The get and put routines for the struct scsi_cd. Note this entity
133 * has a scsi_device pointer and owns a reference to this.
135 static inline struct scsi_cd *scsi_cd_get(struct gendisk *disk)
137 struct scsi_cd *cd = NULL;
139 mutex_lock(&sr_ref_mutex);
140 if (disk->private_data == NULL)
144 if (scsi_device_get(cd->device))
149 kref_put(&cd->kref, sr_kref_release);
152 mutex_unlock(&sr_ref_mutex);
156 static void scsi_cd_put(struct scsi_cd *cd)
158 struct scsi_device *sdev = cd->device;
160 mutex_lock(&sr_ref_mutex);
161 kref_put(&cd->kref, sr_kref_release);
162 scsi_device_put(sdev);
163 mutex_unlock(&sr_ref_mutex);
167 * This function checks to see if the media has been changed in the
168 * CDROM drive. It is possible that we have already sensed a change,
169 * or the drive may have sensed one and not yet reported it. We must
170 * be ready for either case. This function always reports the current
171 * value of the changed bit. If flag is 0, then the changed bit is reset.
172 * This function could be done as an ioctl, but we would need to have
173 * an inode for that to work, and we do not always have one.
176 static int sr_media_change(struct cdrom_device_info *cdi, int slot)
178 struct scsi_cd *cd = cdi->handle;
181 if (CDSL_CURRENT != slot) {
182 /* no changer support */
186 retval = scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES);
188 /* Unable to test, unit probably not ready. This usually
189 * means there is no disc in the drive. Mark as changed,
190 * and we will figure it out later once the drive is
191 * available again. */
192 cd->device->changed = 1;
193 return 1; /* This will force a flush, if called from
194 * check_disk_change */
197 retval = cd->device->changed;
198 cd->device->changed = 0;
199 /* If the disk changed, the capacity will now be different,
200 * so we force a re-read of this information */
202 /* check multisession offset etc */
211 * rw_intr is the interrupt routine for the device driver.
213 * It will be notified on the end of a SCSI read / write, and will take on
214 * of several actions based on success or failure.
216 static void rw_intr(struct scsi_cmnd * SCpnt)
218 int result = SCpnt->result;
219 int this_count = SCpnt->request_bufflen;
220 int good_bytes = (result == 0 ? this_count : 0);
221 int block_sectors = 0;
223 struct scsi_cd *cd = scsi_cd(SCpnt->request->rq_disk);
226 printk("sr.c done: %x\n", result);
230 * Handle MEDIUM ERRORs or VOLUME OVERFLOWs that indicate partial
231 * success. Since this is a relatively rare error condition, no
232 * care is taken to avoid unnecessary additional work such as
233 * memcpy's that could be avoided.
235 if (driver_byte(result) != 0 && /* An error occurred */
236 (SCpnt->sense_buffer[0] & 0x7f) == 0x70) { /* Sense current */
237 switch (SCpnt->sense_buffer[2]) {
239 case VOLUME_OVERFLOW:
240 case ILLEGAL_REQUEST:
241 if (!(SCpnt->sense_buffer[0] & 0x90))
243 error_sector = (SCpnt->sense_buffer[3] << 24) |
244 (SCpnt->sense_buffer[4] << 16) |
245 (SCpnt->sense_buffer[5] << 8) |
246 SCpnt->sense_buffer[6];
247 if (SCpnt->request->bio != NULL)
249 bio_sectors(SCpnt->request->bio);
250 if (block_sectors < 4)
252 if (cd->device->sector_size == 2048)
254 error_sector &= ~(block_sectors - 1);
255 good_bytes = (error_sector - SCpnt->request->sector) << 9;
256 if (good_bytes < 0 || good_bytes >= this_count)
259 * The SCSI specification allows for the value
260 * returned by READ CAPACITY to be up to 75 2K
261 * sectors past the last readable block.
262 * Therefore, if we hit a medium error within the
263 * last 75 2K sectors, we decrease the saved size
266 if (error_sector < get_capacity(cd->disk) &&
267 cd->capacity - error_sector < 4 * 75)
268 set_capacity(cd->disk, error_sector);
271 case RECOVERED_ERROR:
274 * An error occured, but it recovered. Inform the
275 * user, but make sure that it's not treated as a
278 scsi_print_sense("sr", SCpnt);
280 SCpnt->sense_buffer[0] = 0x0;
281 good_bytes = this_count;
290 * This calls the generic completion function, now that we know
291 * how many actual sectors finished, and how many sectors we need
292 * to say have failed.
294 scsi_io_completion(SCpnt, good_bytes);
297 static int sr_prep_fn(struct request_queue *q, struct request *rq)
299 int block=0, this_count, s_size, timeout = SR_TIMEOUT;
301 struct scsi_cmnd *SCpnt;
302 struct scsi_device *sdp = q->queuedata;
305 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
306 ret = scsi_setup_blk_pc_cmnd(sdp, rq);
308 } else if (rq->cmd_type != REQ_TYPE_FS) {
312 ret = scsi_setup_fs_cmnd(sdp, rq);
313 if (ret != BLKPREP_OK)
316 cd = scsi_cd(rq->rq_disk);
318 /* from here on until we're complete, any goto out
319 * is used for a killable error condition */
322 SCSI_LOG_HLQUEUE(1, printk("Doing sr request, dev = %s, block = %d\n",
323 cd->disk->disk_name, block));
325 if (!cd->device || !scsi_device_online(cd->device)) {
326 SCSI_LOG_HLQUEUE(2, printk("Finishing %ld sectors\n",
328 SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt));
332 if (cd->device->changed) {
334 * quietly refuse to do anything to a changed disc until the
335 * changed bit has been reset
341 * we do lazy blocksize switching (when reading XA sectors,
342 * see CDROMREADMODE2 ioctl)
344 s_size = cd->device->sector_size;
347 sr_set_blocklength(cd, 2048);
349 printk("sr: can't switch blocksize: in interrupt\n");
352 if (s_size != 512 && s_size != 1024 && s_size != 2048) {
353 scmd_printk(KERN_ERR, SCpnt, "bad sector size %d\n", s_size);
357 if (rq_data_dir(rq) == WRITE) {
358 if (!cd->device->writeable)
360 SCpnt->cmnd[0] = WRITE_10;
361 SCpnt->sc_data_direction = DMA_TO_DEVICE;
362 cd->cdi.media_written = 1;
363 } else if (rq_data_dir(rq) == READ) {
364 SCpnt->cmnd[0] = READ_10;
365 SCpnt->sc_data_direction = DMA_FROM_DEVICE;
367 blk_dump_rq_flags(rq, "Unknown sr command");
372 struct scatterlist *sg = SCpnt->request_buffer;
374 for (i = 0; i < SCpnt->use_sg; i++)
375 size += sg[i].length;
377 if (size != SCpnt->request_bufflen && SCpnt->use_sg) {
378 scmd_printk(KERN_ERR, SCpnt,
379 "mismatch count %d, bytes %d\n",
380 size, SCpnt->request_bufflen);
381 if (SCpnt->request_bufflen > size)
382 SCpnt->request_bufflen = size;
387 * request doesn't start on hw block boundary, add scatter pads
389 if (((unsigned int)rq->sector % (s_size >> 9)) ||
390 (SCpnt->request_bufflen % s_size)) {
391 scmd_printk(KERN_NOTICE, SCpnt, "unaligned transfer\n");
395 this_count = (SCpnt->request_bufflen >> 9) / (s_size >> 9);
398 SCSI_LOG_HLQUEUE(2, printk("%s : %s %d/%ld 512 byte blocks.\n",
400 (rq_data_dir(rq) == WRITE) ?
401 "writing" : "reading",
402 this_count, rq->nr_sectors));
405 block = (unsigned int)rq->sector / (s_size >> 9);
407 if (this_count > 0xffff) {
409 SCpnt->request_bufflen = this_count * s_size;
412 SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
413 SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
414 SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
415 SCpnt->cmnd[5] = (unsigned char) block & 0xff;
416 SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
417 SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
418 SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
421 * We shouldn't disconnect in the middle of a sector, so with a dumb
422 * host adapter, it's safe to assume that we can at least transfer
423 * this many bytes between each connect / disconnect.
425 SCpnt->transfersize = cd->device->sector_size;
426 SCpnt->underflow = this_count << 9;
427 SCpnt->allowed = MAX_RETRIES;
428 SCpnt->timeout_per_command = timeout;
431 * This is the completion routine we use. This is matched in terms
432 * of capability to this function.
434 SCpnt->done = rw_intr;
437 * This indicates that the command is ready from our end to be
442 return scsi_prep_return(q, rq, ret);
445 static int sr_block_open(struct inode *inode, struct file *file)
447 struct gendisk *disk = inode->i_bdev->bd_disk;
451 if(!(cd = scsi_cd_get(disk)))
454 if((ret = cdrom_open(&cd->cdi, inode, file)) != 0)
460 static int sr_block_release(struct inode *inode, struct file *file)
463 struct scsi_cd *cd = scsi_cd(inode->i_bdev->bd_disk);
464 ret = cdrom_release(&cd->cdi, file);
473 static int sr_block_ioctl(struct inode *inode, struct file *file, unsigned cmd,
476 struct scsi_cd *cd = scsi_cd(inode->i_bdev->bd_disk);
477 struct scsi_device *sdev = cd->device;
478 void __user *argp = (void __user *)arg;
482 * Send SCSI addressing ioctls directly to mid level, send other
483 * ioctls to cdrom/block level.
486 case SCSI_IOCTL_GET_IDLUN:
487 case SCSI_IOCTL_GET_BUS_NUMBER:
488 return scsi_ioctl(sdev, cmd, argp);
491 ret = cdrom_ioctl(file, &cd->cdi, inode, cmd, arg);
496 * ENODEV means that we didn't recognise the ioctl, or that we
497 * cannot execute it in the current device state. In either
498 * case fall through to scsi_ioctl, which will return ENDOEV again
499 * if it doesn't recognise the ioctl
501 ret = scsi_nonblockable_ioctl(sdev, cmd, argp, NULL);
504 return scsi_ioctl(sdev, cmd, argp);
507 static int sr_block_media_changed(struct gendisk *disk)
509 struct scsi_cd *cd = scsi_cd(disk);
510 return cdrom_media_changed(&cd->cdi);
513 static struct block_device_operations sr_bdops =
515 .owner = THIS_MODULE,
516 .open = sr_block_open,
517 .release = sr_block_release,
518 .ioctl = sr_block_ioctl,
519 .media_changed = sr_block_media_changed,
521 * No compat_ioctl for now because sr_block_ioctl never
522 * seems to pass arbitary ioctls down to host drivers.
526 static int sr_open(struct cdrom_device_info *cdi, int purpose)
528 struct scsi_cd *cd = cdi->handle;
529 struct scsi_device *sdev = cd->device;
533 * If the device is in error recovery, wait until it is done.
534 * If the device is offline, then disallow any access to it.
537 if (!scsi_block_when_processing_errors(sdev))
546 static void sr_release(struct cdrom_device_info *cdi)
548 struct scsi_cd *cd = cdi->handle;
550 if (cd->device->sector_size > 2048)
551 sr_set_blocklength(cd, 2048);
555 static int sr_probe(struct device *dev)
557 struct scsi_device *sdev = to_scsi_device(dev);
558 struct gendisk *disk;
563 if (sdev->type != TYPE_ROM && sdev->type != TYPE_WORM)
567 cd = kzalloc(sizeof(*cd), GFP_KERNEL);
571 kref_init(&cd->kref);
573 disk = alloc_disk(1);
577 spin_lock(&sr_index_lock);
578 minor = find_first_zero_bit(sr_index_bits, SR_DISKS);
579 if (minor == SR_DISKS) {
580 spin_unlock(&sr_index_lock);
584 __set_bit(minor, sr_index_bits);
585 spin_unlock(&sr_index_lock);
587 disk->major = SCSI_CDROM_MAJOR;
588 disk->first_minor = minor;
589 sprintf(disk->disk_name, "sr%d", minor);
590 disk->fops = &sr_bdops;
591 disk->flags = GENHD_FL_CD;
595 cd->driver = &sr_template;
597 cd->capacity = 0x1fffff;
598 cd->device->changed = 1; /* force recheck CD type */
600 cd->readcd_known = 0;
603 cd->cdi.ops = &sr_dops;
606 cd->cdi.capacity = 1;
607 sprintf(cd->cdi.name, "sr%d", minor);
609 sdev->sector_size = 2048; /* A guess, just in case */
611 /* FIXME: need to handle a get_capabilities failure properly ?? */
612 get_capabilities(cd);
613 blk_queue_prep_rq(sdev->request_queue, sr_prep_fn);
616 disk->driverfs_dev = &sdev->sdev_gendev;
617 set_capacity(disk, cd->capacity);
618 disk->private_data = &cd->driver;
619 disk->queue = sdev->request_queue;
622 if (register_cdrom(&cd->cdi))
625 dev_set_drvdata(dev, cd);
626 disk->flags |= GENHD_FL_REMOVABLE;
629 sdev_printk(KERN_DEBUG, sdev,
630 "Attached scsi CD-ROM %s\n", cd->cdi.name);
642 static void get_sectorsize(struct scsi_cd *cd)
644 unsigned char cmd[10];
645 unsigned char *buffer;
646 int the_result, retries = 3;
648 struct request_queue *queue;
650 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
655 cmd[0] = READ_CAPACITY;
656 memset((void *) &cmd[1], 0, 9);
657 memset(buffer, 0, 8);
659 /* Do the command and wait.. */
660 the_result = scsi_execute_req(cd->device, cmd, DMA_FROM_DEVICE,
661 buffer, 8, NULL, SR_TIMEOUT,
666 } while (the_result && retries);
670 cd->capacity = 0x1fffff;
671 sector_size = 2048; /* A guess, just in case */
674 if (cdrom_get_last_written(&cd->cdi,
677 cd->capacity = 1 + ((buffer[0] << 24) |
681 sector_size = (buffer[4] << 24) |
682 (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
683 switch (sector_size) {
685 * HP 4020i CD-Recorder reports 2340 byte sectors
686 * Philips CD-Writers report 2352 byte sectors
688 * Use 2k sectors for them..
701 printk("%s: unsupported sector size %d.\n",
702 cd->cdi.name, sector_size);
706 cd->device->sector_size = sector_size;
709 * Add this so that we have the ability to correctly gauge
710 * what the device is capable of.
712 set_capacity(cd->disk, cd->capacity);
715 queue = cd->device->request_queue;
716 blk_queue_hardsect_size(queue, sector_size);
722 cd->capacity = 0x1fffff;
723 cd->device->sector_size = 2048; /* A guess, just in case */
727 static void get_capabilities(struct scsi_cd *cd)
729 unsigned char *buffer;
730 struct scsi_mode_data data;
731 unsigned char cmd[MAX_COMMAND_SIZE];
732 struct scsi_sense_hdr sshdr;
733 unsigned int the_result;
736 static const char *loadmech[] =
749 /* allocate transfer buffer */
750 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
752 printk(KERN_ERR "sr: out of memory.\n");
756 /* issue TEST_UNIT_READY until the initial startup UNIT_ATTENTION
757 * conditions are gone, or a timeout happens
761 memset((void *)cmd, 0, MAX_COMMAND_SIZE);
762 cmd[0] = TEST_UNIT_READY;
764 the_result = scsi_execute_req (cd->device, cmd, DMA_NONE, NULL,
765 0, &sshdr, SR_TIMEOUT,
769 } while (retries < 5 &&
770 (!scsi_status_is_good(the_result) ||
771 (scsi_sense_valid(&sshdr) &&
772 sshdr.sense_key == UNIT_ATTENTION)));
774 /* ask for mode page 0x2a */
775 rc = scsi_mode_sense(cd->device, 0, 0x2a, buffer, 128,
776 SR_TIMEOUT, 3, &data, NULL);
778 if (!scsi_status_is_good(rc)) {
779 /* failed, drive doesn't have capabilities mode page */
781 cd->cdi.mask |= (CDC_CD_R | CDC_CD_RW | CDC_DVD_R |
782 CDC_DVD | CDC_DVD_RAM |
783 CDC_SELECT_DISC | CDC_SELECT_SPEED |
784 CDC_MRW | CDC_MRW_W | CDC_RAM);
786 printk("%s: scsi-1 drive\n", cd->cdi.name);
790 n = data.header_length + data.block_descriptor_length;
791 cd->cdi.speed = ((buffer[n + 8] << 8) + buffer[n + 9]) / 176;
792 cd->readcd_known = 1;
793 cd->readcd_cdda = buffer[n + 5] & 0x01;
794 /* print some capability bits */
795 printk("%s: scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n", cd->cdi.name,
796 ((buffer[n + 14] << 8) + buffer[n + 15]) / 176,
798 buffer[n + 3] & 0x01 ? "writer " : "", /* CD Writer */
799 buffer[n + 3] & 0x20 ? "dvd-ram " : "",
800 buffer[n + 2] & 0x02 ? "cd/rw " : "", /* can read rewriteable */
801 buffer[n + 4] & 0x20 ? "xa/form2 " : "", /* can read xa/from2 */
802 buffer[n + 5] & 0x01 ? "cdda " : "", /* can read audio data */
803 loadmech[buffer[n + 6] >> 5]);
804 if ((buffer[n + 6] >> 5) == 0)
805 /* caddy drives can't close tray... */
806 cd->cdi.mask |= CDC_CLOSE_TRAY;
807 if ((buffer[n + 2] & 0x8) == 0)
808 /* not a DVD drive */
809 cd->cdi.mask |= CDC_DVD;
810 if ((buffer[n + 3] & 0x20) == 0)
811 /* can't write DVD-RAM media */
812 cd->cdi.mask |= CDC_DVD_RAM;
813 if ((buffer[n + 3] & 0x10) == 0)
814 /* can't write DVD-R media */
815 cd->cdi.mask |= CDC_DVD_R;
816 if ((buffer[n + 3] & 0x2) == 0)
817 /* can't write CD-RW media */
818 cd->cdi.mask |= CDC_CD_RW;
819 if ((buffer[n + 3] & 0x1) == 0)
820 /* can't write CD-R media */
821 cd->cdi.mask |= CDC_CD_R;
822 if ((buffer[n + 6] & 0x8) == 0)
824 cd->cdi.mask |= CDC_OPEN_TRAY;
826 if ((buffer[n + 6] >> 5) == mechtype_individual_changer ||
827 (buffer[n + 6] >> 5) == mechtype_cartridge_changer)
829 cdrom_number_of_slots(&cd->cdi);
830 if (cd->cdi.capacity <= 1)
832 cd->cdi.mask |= CDC_SELECT_DISC;
833 /*else I don't think it can close its tray
834 cd->cdi.mask |= CDC_CLOSE_TRAY; */
837 * if DVD-RAM, MRW-W or CD-RW, we are randomly writable
839 if ((cd->cdi.mask & (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) !=
840 (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) {
841 cd->device->writeable = 1;
848 * sr_packet() is the entry point for the generic commands generated
849 * by the Uniform CD-ROM layer.
851 static int sr_packet(struct cdrom_device_info *cdi,
852 struct packet_command *cgc)
854 if (cgc->timeout <= 0)
855 cgc->timeout = IOCTL_TIMEOUT;
857 sr_do_ioctl(cdi->handle, cgc);
863 * sr_kref_release - Called to free the scsi_cd structure
864 * @kref: pointer to embedded kref
866 * sr_ref_mutex must be held entering this routine. Because it is
867 * called on last put, you should always use the scsi_cd_get()
868 * scsi_cd_put() helpers which manipulate the semaphore directly
869 * and never do a direct kref_put().
871 static void sr_kref_release(struct kref *kref)
873 struct scsi_cd *cd = container_of(kref, struct scsi_cd, kref);
874 struct gendisk *disk = cd->disk;
876 spin_lock(&sr_index_lock);
877 clear_bit(disk->first_minor, sr_index_bits);
878 spin_unlock(&sr_index_lock);
880 unregister_cdrom(&cd->cdi);
882 disk->private_data = NULL;
889 static int sr_remove(struct device *dev)
891 struct scsi_cd *cd = dev_get_drvdata(dev);
893 del_gendisk(cd->disk);
895 mutex_lock(&sr_ref_mutex);
896 kref_put(&cd->kref, sr_kref_release);
897 mutex_unlock(&sr_ref_mutex);
902 static int __init init_sr(void)
906 rc = register_blkdev(SCSI_CDROM_MAJOR, "sr");
909 rc = scsi_register_driver(&sr_template.gendrv);
911 unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
916 static void __exit exit_sr(void)
918 scsi_unregister_driver(&sr_template.gendrv);
919 unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
922 module_init(init_sr);
923 module_exit(exit_sr);
924 MODULE_LICENSE("GPL");