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>
38 #include <linux/sched.h>
40 #include <linux/bio.h>
41 #include <linux/string.h>
42 #include <linux/errno.h>
43 #include <linux/cdrom.h>
44 #include <linux/interrupt.h>
45 #include <linux/init.h>
46 #include <linux/blkdev.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"
65 #define SR_TIMEOUT (30 * HZ)
66 #define SR_CAPABILITIES \
67 (CDC_CLOSE_TRAY|CDC_OPEN_TRAY|CDC_LOCK|CDC_SELECT_SPEED| \
68 CDC_SELECT_DISC|CDC_MULTI_SESSION|CDC_MCN|CDC_MEDIA_CHANGED| \
69 CDC_PLAY_AUDIO|CDC_RESET|CDC_IOCTLS|CDC_DRIVE_STATUS| \
70 CDC_CD_R|CDC_CD_RW|CDC_DVD|CDC_DVD_R|CDC_DVD_RAM|CDC_GENERIC_PACKET| \
71 CDC_MRW|CDC_MRW_W|CDC_RAM)
73 static int sr_probe(struct device *);
74 static int sr_remove(struct device *);
75 static int sr_init_command(struct scsi_cmnd *);
77 static struct scsi_driver sr_template = {
84 .init_command = sr_init_command,
87 static unsigned long sr_index_bits[SR_DISKS / BITS_PER_LONG];
88 static DEFINE_SPINLOCK(sr_index_lock);
90 /* This semaphore is used to mediate the 0->1 reference get in the
91 * face of object destruction (i.e. we can't allow a get on an
92 * object after last put) */
93 static DECLARE_MUTEX(sr_ref_sem);
95 static int sr_open(struct cdrom_device_info *, int);
96 static void sr_release(struct cdrom_device_info *);
98 static void get_sectorsize(struct scsi_cd *);
99 static void get_capabilities(struct scsi_cd *);
101 static int sr_media_change(struct cdrom_device_info *, int);
102 static int sr_packet(struct cdrom_device_info *, struct packet_command *);
104 static struct cdrom_device_ops sr_dops = {
106 .release = sr_release,
107 .drive_status = sr_drive_status,
108 .media_changed = sr_media_change,
109 .tray_move = sr_tray_move,
110 .lock_door = sr_lock_door,
111 .select_speed = sr_select_speed,
112 .get_last_session = sr_get_last_session,
113 .get_mcn = sr_get_mcn,
115 .audio_ioctl = sr_audio_ioctl,
116 .dev_ioctl = sr_dev_ioctl,
117 .capability = SR_CAPABILITIES,
118 .generic_packet = sr_packet,
121 static void sr_kref_release(struct kref *kref);
123 static inline struct scsi_cd *scsi_cd(struct gendisk *disk)
125 return container_of(disk->private_data, struct scsi_cd, driver);
129 * The get and put routines for the struct scsi_cd. Note this entity
130 * has a scsi_device pointer and owns a reference to this.
132 static inline struct scsi_cd *scsi_cd_get(struct gendisk *disk)
134 struct scsi_cd *cd = NULL;
137 if (disk->private_data == NULL)
141 if (scsi_device_get(cd->device))
146 kref_put(&cd->kref, sr_kref_release);
153 static inline void scsi_cd_put(struct scsi_cd *cd)
155 struct scsi_device *sdev = cd->device;
158 kref_put(&cd->kref, sr_kref_release);
159 scsi_device_put(sdev);
164 * This function checks to see if the media has been changed in the
165 * CDROM drive. It is possible that we have already sensed a change,
166 * or the drive may have sensed one and not yet reported it. We must
167 * be ready for either case. This function always reports the current
168 * value of the changed bit. If flag is 0, then the changed bit is reset.
169 * This function could be done as an ioctl, but we would need to have
170 * an inode for that to work, and we do not always have one.
173 int sr_media_change(struct cdrom_device_info *cdi, int slot)
175 struct scsi_cd *cd = cdi->handle;
178 if (CDSL_CURRENT != slot) {
179 /* no changer support */
183 retval = scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES);
185 /* Unable to test, unit probably not ready. This usually
186 * means there is no disc in the drive. Mark as changed,
187 * and we will figure it out later once the drive is
188 * available again. */
189 cd->device->changed = 1;
190 return 1; /* This will force a flush, if called from
191 * check_disk_change */
194 retval = cd->device->changed;
195 cd->device->changed = 0;
196 /* If the disk changed, the capacity will now be different,
197 * so we force a re-read of this information */
199 /* check multisession offset etc */
208 * rw_intr is the interrupt routine for the device driver.
210 * It will be notified on the end of a SCSI read / write, and will take on
211 * of several actions based on success or failure.
213 static void rw_intr(struct scsi_cmnd * SCpnt)
215 int result = SCpnt->result;
216 int this_count = SCpnt->bufflen;
217 int good_bytes = (result == 0 ? this_count : 0);
218 int block_sectors = 0;
220 struct scsi_cd *cd = scsi_cd(SCpnt->request->rq_disk);
223 printk("sr.c done: %x\n", result);
227 * Handle MEDIUM ERRORs or VOLUME OVERFLOWs that indicate partial
228 * success. Since this is a relatively rare error condition, no
229 * care is taken to avoid unnecessary additional work such as
230 * memcpy's that could be avoided.
232 if (driver_byte(result) != 0 && /* An error occurred */
233 (SCpnt->sense_buffer[0] & 0x7f) == 0x70) { /* Sense current */
234 switch (SCpnt->sense_buffer[2]) {
236 case VOLUME_OVERFLOW:
237 case ILLEGAL_REQUEST:
238 if (!(SCpnt->sense_buffer[0] & 0x90))
240 if (!blk_fs_request(SCpnt->request))
242 error_sector = (SCpnt->sense_buffer[3] << 24) |
243 (SCpnt->sense_buffer[4] << 16) |
244 (SCpnt->sense_buffer[5] << 8) |
245 SCpnt->sense_buffer[6];
246 if (SCpnt->request->bio != NULL)
248 bio_sectors(SCpnt->request->bio);
249 if (block_sectors < 4)
251 if (cd->device->sector_size == 2048)
253 error_sector &= ~(block_sectors - 1);
254 good_bytes = (error_sector - SCpnt->request->sector) << 9;
255 if (good_bytes < 0 || good_bytes >= this_count)
258 * The SCSI specification allows for the value
259 * returned by READ CAPACITY to be up to 75 2K
260 * sectors past the last readable block.
261 * Therefore, if we hit a medium error within the
262 * last 75 2K sectors, we decrease the saved size
265 if (error_sector < get_capacity(cd->disk) &&
266 cd->capacity - error_sector < 4 * 75)
267 set_capacity(cd->disk, error_sector);
270 case RECOVERED_ERROR:
273 * An error occured, but it recovered. Inform the
274 * user, but make sure that it's not treated as a
277 scsi_print_sense("sr", SCpnt);
279 SCpnt->sense_buffer[0] = 0x0;
280 good_bytes = this_count;
289 * This calls the generic completion function, now that we know
290 * how many actual sectors finished, and how many sectors we need
291 * to say have failed.
293 scsi_io_completion(SCpnt, good_bytes, block_sectors << 9);
296 static int sr_init_command(struct scsi_cmnd * SCpnt)
298 int block=0, this_count, s_size, timeout = SR_TIMEOUT;
299 struct scsi_cd *cd = scsi_cd(SCpnt->request->rq_disk);
301 SCSI_LOG_HLQUEUE(1, printk("Doing sr request, dev = %s, block = %d\n",
302 cd->disk->disk_name, block));
304 if (!cd->device || !scsi_device_online(cd->device)) {
305 SCSI_LOG_HLQUEUE(2, printk("Finishing %ld sectors\n",
306 SCpnt->request->nr_sectors));
307 SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt));
311 if (cd->device->changed) {
313 * quietly refuse to do anything to a changed disc until the
314 * changed bit has been reset
320 * these are already setup, just copy cdb basically
322 if (SCpnt->request->flags & REQ_BLOCK_PC) {
323 struct request *rq = SCpnt->request;
325 if (sizeof(rq->cmd) > sizeof(SCpnt->cmnd))
328 memcpy(SCpnt->cmnd, rq->cmd, sizeof(SCpnt->cmnd));
330 SCpnt->sc_data_direction = DMA_NONE;
331 else if (rq_data_dir(rq) == WRITE)
332 SCpnt->sc_data_direction = DMA_TO_DEVICE;
334 SCpnt->sc_data_direction = DMA_FROM_DEVICE;
336 this_count = rq->data_len;
338 timeout = rq->timeout;
340 SCpnt->transfersize = rq->data_len;
344 if (!(SCpnt->request->flags & REQ_CMD)) {
345 blk_dump_rq_flags(SCpnt->request, "sr unsup command");
350 * we do lazy blocksize switching (when reading XA sectors,
351 * see CDROMREADMODE2 ioctl)
353 s_size = cd->device->sector_size;
356 sr_set_blocklength(cd, 2048);
358 printk("sr: can't switch blocksize: in interrupt\n");
361 if (s_size != 512 && s_size != 1024 && s_size != 2048) {
362 printk("sr: bad sector size %d\n", s_size);
366 if (rq_data_dir(SCpnt->request) == WRITE) {
367 if (!cd->device->writeable)
369 SCpnt->cmnd[0] = WRITE_10;
370 SCpnt->sc_data_direction = DMA_TO_DEVICE;
371 cd->cdi.media_written = 1;
372 } else if (rq_data_dir(SCpnt->request) == READ) {
373 SCpnt->cmnd[0] = READ_10;
374 SCpnt->sc_data_direction = DMA_FROM_DEVICE;
376 blk_dump_rq_flags(SCpnt->request, "Unknown sr command");
381 struct scatterlist *sg = SCpnt->request_buffer;
383 for (i = 0; i < SCpnt->use_sg; i++)
384 size += sg[i].length;
386 if (size != SCpnt->request_bufflen && SCpnt->use_sg) {
387 printk(KERN_ERR "sr: mismatch count %d, bytes %d\n",
388 size, SCpnt->request_bufflen);
389 if (SCpnt->request_bufflen > size)
390 SCpnt->request_bufflen = SCpnt->bufflen = size;
395 * request doesn't start on hw block boundary, add scatter pads
397 if (((unsigned int)SCpnt->request->sector % (s_size >> 9)) ||
398 (SCpnt->request_bufflen % s_size)) {
399 printk("sr: unaligned transfer\n");
403 this_count = (SCpnt->request_bufflen >> 9) / (s_size >> 9);
406 SCSI_LOG_HLQUEUE(2, printk("%s : %s %d/%ld 512 byte blocks.\n",
408 (rq_data_dir(SCpnt->request) == WRITE) ?
409 "writing" : "reading",
410 this_count, SCpnt->request->nr_sectors));
413 block = (unsigned int)SCpnt->request->sector / (s_size >> 9);
415 if (this_count > 0xffff) {
417 SCpnt->request_bufflen = SCpnt->bufflen =
421 SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
422 SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
423 SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
424 SCpnt->cmnd[5] = (unsigned char) block & 0xff;
425 SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
426 SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
427 SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
430 * We shouldn't disconnect in the middle of a sector, so with a dumb
431 * host adapter, it's safe to assume that we can at least transfer
432 * this many bytes between each connect / disconnect.
434 SCpnt->transfersize = cd->device->sector_size;
435 SCpnt->underflow = this_count << 9;
438 SCpnt->allowed = MAX_RETRIES;
439 SCpnt->timeout_per_command = timeout;
442 * This is the completion routine we use. This is matched in terms
443 * of capability to this function.
445 SCpnt->done = rw_intr;
448 * This indicates that the command is ready from our end to be
454 static int sr_block_open(struct inode *inode, struct file *file)
456 struct gendisk *disk = inode->i_bdev->bd_disk;
457 struct scsi_cd *cd = scsi_cd(inode->i_bdev->bd_disk);
460 if(!(cd = scsi_cd_get(disk)))
463 if((ret = cdrom_open(&cd->cdi, inode, file)) != 0)
469 static int sr_block_release(struct inode *inode, struct file *file)
472 struct scsi_cd *cd = scsi_cd(inode->i_bdev->bd_disk);
473 ret = cdrom_release(&cd->cdi, file);
482 static int sr_block_ioctl(struct inode *inode, struct file *file, unsigned cmd,
485 struct scsi_cd *cd = scsi_cd(inode->i_bdev->bd_disk);
486 struct scsi_device *sdev = cd->device;
489 * Send SCSI addressing ioctls directly to mid level, send other
490 * ioctls to cdrom/block level.
493 case SCSI_IOCTL_GET_IDLUN:
494 case SCSI_IOCTL_GET_BUS_NUMBER:
495 return scsi_ioctl(sdev, cmd, (void __user *)arg);
497 return cdrom_ioctl(file, &cd->cdi, inode, cmd, arg);
500 static int sr_block_media_changed(struct gendisk *disk)
502 struct scsi_cd *cd = scsi_cd(disk);
503 return cdrom_media_changed(&cd->cdi);
506 static struct block_device_operations sr_bdops =
508 .owner = THIS_MODULE,
509 .open = sr_block_open,
510 .release = sr_block_release,
511 .ioctl = sr_block_ioctl,
512 .media_changed = sr_block_media_changed,
514 * No compat_ioctl for now because sr_block_ioctl never
515 * seems to pass arbitary ioctls down to host drivers.
519 static int sr_open(struct cdrom_device_info *cdi, int purpose)
521 struct scsi_cd *cd = cdi->handle;
522 struct scsi_device *sdev = cd->device;
526 * If the device is in error recovery, wait until it is done.
527 * If the device is offline, then disallow any access to it.
530 if (!scsi_block_when_processing_errors(sdev))
539 static void sr_release(struct cdrom_device_info *cdi)
541 struct scsi_cd *cd = cdi->handle;
543 if (cd->device->sector_size > 2048)
544 sr_set_blocklength(cd, 2048);
548 static int sr_probe(struct device *dev)
550 struct scsi_device *sdev = to_scsi_device(dev);
551 struct gendisk *disk;
556 if (sdev->type != TYPE_ROM && sdev->type != TYPE_WORM)
560 cd = kmalloc(sizeof(*cd), GFP_KERNEL);
563 memset(cd, 0, sizeof(*cd));
565 kref_init(&cd->kref);
567 disk = alloc_disk(1);
571 spin_lock(&sr_index_lock);
572 minor = find_first_zero_bit(sr_index_bits, SR_DISKS);
573 if (minor == SR_DISKS) {
574 spin_unlock(&sr_index_lock);
578 __set_bit(minor, sr_index_bits);
579 spin_unlock(&sr_index_lock);
581 disk->major = SCSI_CDROM_MAJOR;
582 disk->first_minor = minor;
583 sprintf(disk->disk_name, "sr%d", minor);
584 disk->fops = &sr_bdops;
585 disk->flags = GENHD_FL_CD;
589 cd->driver = &sr_template;
591 cd->capacity = 0x1fffff;
592 cd->device->changed = 1; /* force recheck CD type */
594 cd->readcd_known = 0;
597 cd->cdi.ops = &sr_dops;
600 cd->cdi.capacity = 1;
601 sprintf(cd->cdi.name, "sr%d", minor);
603 sdev->sector_size = 2048; /* A guess, just in case */
605 /* FIXME: need to handle a get_capabilities failure properly ?? */
606 get_capabilities(cd);
609 snprintf(disk->devfs_name, sizeof(disk->devfs_name),
610 "%s/cd", sdev->devfs_name);
611 disk->driverfs_dev = &sdev->sdev_gendev;
612 set_capacity(disk, cd->capacity);
613 disk->private_data = &cd->driver;
614 disk->queue = sdev->request_queue;
617 if (register_cdrom(&cd->cdi))
620 dev_set_drvdata(dev, cd);
621 disk->flags |= GENHD_FL_REMOVABLE;
625 "Attached scsi CD-ROM %s at scsi%d, channel %d, id %d, lun %d\n",
626 cd->cdi.name, sdev->host->host_no, sdev->channel,
627 sdev->id, sdev->lun);
639 static void get_sectorsize(struct scsi_cd *cd)
641 unsigned char cmd[10];
642 unsigned char *buffer;
643 int the_result, retries = 3;
645 request_queue_t *queue;
647 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
652 cmd[0] = READ_CAPACITY;
653 memset((void *) &cmd[1], 0, 9);
654 memset(buffer, 0, 8);
656 /* Do the command and wait.. */
657 the_result = scsi_execute_req(cd->device, cmd, DMA_FROM_DEVICE,
658 buffer, 8, NULL, SR_TIMEOUT,
663 } while (the_result && retries);
667 cd->capacity = 0x1fffff;
668 sector_size = 2048; /* A guess, just in case */
671 if (cdrom_get_last_written(&cd->cdi,
674 cd->capacity = 1 + ((buffer[0] << 24) |
678 sector_size = (buffer[4] << 24) |
679 (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
680 switch (sector_size) {
682 * HP 4020i CD-Recorder reports 2340 byte sectors
683 * Philips CD-Writers report 2352 byte sectors
685 * Use 2k sectors for them..
698 printk("%s: unsupported sector size %d.\n",
699 cd->cdi.name, sector_size);
703 cd->device->sector_size = sector_size;
706 * Add this so that we have the ability to correctly gauge
707 * what the device is capable of.
709 set_capacity(cd->disk, cd->capacity);
712 queue = cd->device->request_queue;
713 blk_queue_hardsect_size(queue, sector_size);
719 cd->capacity = 0x1fffff;
720 cd->device->sector_size = 2048; /* A guess, just in case */
724 static void get_capabilities(struct scsi_cd *cd)
726 unsigned char *buffer;
727 struct scsi_mode_data data;
728 unsigned char cmd[MAX_COMMAND_SIZE];
729 struct scsi_sense_hdr sshdr;
730 unsigned int the_result;
733 static char *loadmech[] =
746 /* allocate transfer buffer */
747 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
749 printk(KERN_ERR "sr: out of memory.\n");
753 /* issue TEST_UNIT_READY until the initial startup UNIT_ATTENTION
754 * conditions are gone, or a timeout happens
758 memset((void *)cmd, 0, MAX_COMMAND_SIZE);
759 cmd[0] = TEST_UNIT_READY;
761 the_result = scsi_execute_req (cd->device, cmd, DMA_NONE, NULL,
762 0, &sshdr, SR_TIMEOUT,
766 } while (retries < 5 &&
767 (!scsi_status_is_good(the_result) ||
768 (scsi_sense_valid(&sshdr) &&
769 sshdr.sense_key == UNIT_ATTENTION)));
771 /* ask for mode page 0x2a */
772 rc = scsi_mode_sense(cd->device, 0, 0x2a, buffer, 128,
773 SR_TIMEOUT, 3, &data, NULL);
775 if (!scsi_status_is_good(rc)) {
776 /* failed, drive doesn't have capabilities mode page */
778 cd->cdi.mask |= (CDC_CD_R | CDC_CD_RW | CDC_DVD_R |
779 CDC_DVD | CDC_DVD_RAM |
780 CDC_SELECT_DISC | CDC_SELECT_SPEED);
782 printk("%s: scsi-1 drive\n", cd->cdi.name);
786 n = data.header_length + data.block_descriptor_length;
787 cd->cdi.speed = ((buffer[n + 8] << 8) + buffer[n + 9]) / 176;
788 cd->readcd_known = 1;
789 cd->readcd_cdda = buffer[n + 5] & 0x01;
790 /* print some capability bits */
791 printk("%s: scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n", cd->cdi.name,
792 ((buffer[n + 14] << 8) + buffer[n + 15]) / 176,
794 buffer[n + 3] & 0x01 ? "writer " : "", /* CD Writer */
795 buffer[n + 3] & 0x20 ? "dvd-ram " : "",
796 buffer[n + 2] & 0x02 ? "cd/rw " : "", /* can read rewriteable */
797 buffer[n + 4] & 0x20 ? "xa/form2 " : "", /* can read xa/from2 */
798 buffer[n + 5] & 0x01 ? "cdda " : "", /* can read audio data */
799 loadmech[buffer[n + 6] >> 5]);
800 if ((buffer[n + 6] >> 5) == 0)
801 /* caddy drives can't close tray... */
802 cd->cdi.mask |= CDC_CLOSE_TRAY;
803 if ((buffer[n + 2] & 0x8) == 0)
804 /* not a DVD drive */
805 cd->cdi.mask |= CDC_DVD;
806 if ((buffer[n + 3] & 0x20) == 0)
807 /* can't write DVD-RAM media */
808 cd->cdi.mask |= CDC_DVD_RAM;
809 if ((buffer[n + 3] & 0x10) == 0)
810 /* can't write DVD-R media */
811 cd->cdi.mask |= CDC_DVD_R;
812 if ((buffer[n + 3] & 0x2) == 0)
813 /* can't write CD-RW media */
814 cd->cdi.mask |= CDC_CD_RW;
815 if ((buffer[n + 3] & 0x1) == 0)
816 /* can't write CD-R media */
817 cd->cdi.mask |= CDC_CD_R;
818 if ((buffer[n + 6] & 0x8) == 0)
820 cd->cdi.mask |= CDC_OPEN_TRAY;
822 if ((buffer[n + 6] >> 5) == mechtype_individual_changer ||
823 (buffer[n + 6] >> 5) == mechtype_cartridge_changer)
825 cdrom_number_of_slots(&cd->cdi);
826 if (cd->cdi.capacity <= 1)
828 cd->cdi.mask |= CDC_SELECT_DISC;
829 /*else I don't think it can close its tray
830 cd->cdi.mask |= CDC_CLOSE_TRAY; */
833 * if DVD-RAM, MRW-W or CD-RW, we are randomly writable
835 if ((cd->cdi.mask & (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) !=
836 (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) {
837 cd->device->writeable = 1;
844 * sr_packet() is the entry point for the generic commands generated
845 * by the Uniform CD-ROM layer.
847 static int sr_packet(struct cdrom_device_info *cdi,
848 struct packet_command *cgc)
850 if (cgc->timeout <= 0)
851 cgc->timeout = IOCTL_TIMEOUT;
853 sr_do_ioctl(cdi->handle, cgc);
859 * sr_kref_release - Called to free the scsi_cd structure
860 * @kref: pointer to embedded kref
862 * sr_ref_sem must be held entering this routine. Because it is
863 * called on last put, you should always use the scsi_cd_get()
864 * scsi_cd_put() helpers which manipulate the semaphore directly
865 * and never do a direct kref_put().
867 static void sr_kref_release(struct kref *kref)
869 struct scsi_cd *cd = container_of(kref, struct scsi_cd, kref);
870 struct gendisk *disk = cd->disk;
872 spin_lock(&sr_index_lock);
873 clear_bit(disk->first_minor, sr_index_bits);
874 spin_unlock(&sr_index_lock);
876 unregister_cdrom(&cd->cdi);
878 disk->private_data = NULL;
885 static int sr_remove(struct device *dev)
887 struct scsi_cd *cd = dev_get_drvdata(dev);
889 del_gendisk(cd->disk);
892 kref_put(&cd->kref, sr_kref_release);
898 static int __init init_sr(void)
902 rc = register_blkdev(SCSI_CDROM_MAJOR, "sr");
905 return scsi_register_driver(&sr_template.gendrv);
908 static void __exit exit_sr(void)
910 scsi_unregister_driver(&sr_template.gendrv);
911 unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
914 module_init(init_sr);
915 module_exit(exit_sr);
916 MODULE_LICENSE("GPL");