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 <linux/mutex.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"
66 #define SR_TIMEOUT (30 * HZ)
67 #define SR_CAPABILITIES \
68 (CDC_CLOSE_TRAY|CDC_OPEN_TRAY|CDC_LOCK|CDC_SELECT_SPEED| \
69 CDC_SELECT_DISC|CDC_MULTI_SESSION|CDC_MCN|CDC_MEDIA_CHANGED| \
70 CDC_PLAY_AUDIO|CDC_RESET|CDC_IOCTLS|CDC_DRIVE_STATUS| \
71 CDC_CD_R|CDC_CD_RW|CDC_DVD|CDC_DVD_R|CDC_DVD_RAM|CDC_GENERIC_PACKET| \
72 CDC_MRW|CDC_MRW_W|CDC_RAM)
74 static int sr_probe(struct device *);
75 static int sr_remove(struct device *);
76 static int sr_init_command(struct scsi_cmnd *);
78 static struct scsi_driver sr_template = {
85 .init_command = sr_init_command,
88 static unsigned long sr_index_bits[SR_DISKS / BITS_PER_LONG];
89 static DEFINE_SPINLOCK(sr_index_lock);
91 /* This semaphore is used to mediate the 0->1 reference get in the
92 * face of object destruction (i.e. we can't allow a get on an
93 * object after last put) */
94 static DEFINE_MUTEX(sr_ref_mutex);
96 static int sr_open(struct cdrom_device_info *, int);
97 static void sr_release(struct cdrom_device_info *);
99 static void get_sectorsize(struct scsi_cd *);
100 static void get_capabilities(struct scsi_cd *);
102 static int sr_media_change(struct cdrom_device_info *, int);
103 static int sr_packet(struct cdrom_device_info *, struct packet_command *);
105 static struct cdrom_device_ops sr_dops = {
107 .release = sr_release,
108 .drive_status = sr_drive_status,
109 .media_changed = sr_media_change,
110 .tray_move = sr_tray_move,
111 .lock_door = sr_lock_door,
112 .select_speed = sr_select_speed,
113 .get_last_session = sr_get_last_session,
114 .get_mcn = sr_get_mcn,
116 .audio_ioctl = sr_audio_ioctl,
117 .dev_ioctl = sr_dev_ioctl,
118 .capability = SR_CAPABILITIES,
119 .generic_packet = sr_packet,
122 static void sr_kref_release(struct kref *kref);
124 static inline struct scsi_cd *scsi_cd(struct gendisk *disk)
126 return container_of(disk->private_data, struct scsi_cd, driver);
130 * The get and put routines for the struct scsi_cd. Note this entity
131 * has a scsi_device pointer and owns a reference to this.
133 static inline struct scsi_cd *scsi_cd_get(struct gendisk *disk)
135 struct scsi_cd *cd = NULL;
137 mutex_lock(&sr_ref_mutex);
138 if (disk->private_data == NULL)
142 if (scsi_device_get(cd->device))
147 kref_put(&cd->kref, sr_kref_release);
150 mutex_unlock(&sr_ref_mutex);
154 static inline void scsi_cd_put(struct scsi_cd *cd)
156 struct scsi_device *sdev = cd->device;
158 mutex_lock(&sr_ref_mutex);
159 kref_put(&cd->kref, sr_kref_release);
160 scsi_device_put(sdev);
161 mutex_unlock(&sr_ref_mutex);
165 * This function checks to see if the media has been changed in the
166 * CDROM drive. It is possible that we have already sensed a change,
167 * or the drive may have sensed one and not yet reported it. We must
168 * be ready for either case. This function always reports the current
169 * value of the changed bit. If flag is 0, then the changed bit is reset.
170 * This function could be done as an ioctl, but we would need to have
171 * an inode for that to work, and we do not always have one.
174 int sr_media_change(struct cdrom_device_info *cdi, int slot)
176 struct scsi_cd *cd = cdi->handle;
179 if (CDSL_CURRENT != slot) {
180 /* no changer support */
184 retval = scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES);
186 /* Unable to test, unit probably not ready. This usually
187 * means there is no disc in the drive. Mark as changed,
188 * and we will figure it out later once the drive is
189 * available again. */
190 cd->device->changed = 1;
191 return 1; /* This will force a flush, if called from
192 * check_disk_change */
195 retval = cd->device->changed;
196 cd->device->changed = 0;
197 /* If the disk changed, the capacity will now be different,
198 * so we force a re-read of this information */
200 /* check multisession offset etc */
209 * rw_intr is the interrupt routine for the device driver.
211 * It will be notified on the end of a SCSI read / write, and will take on
212 * of several actions based on success or failure.
214 static void rw_intr(struct scsi_cmnd * SCpnt)
216 int result = SCpnt->result;
217 int this_count = SCpnt->bufflen;
218 int good_bytes = (result == 0 ? this_count : 0);
219 int block_sectors = 0;
221 struct scsi_cd *cd = scsi_cd(SCpnt->request->rq_disk);
224 printk("sr.c done: %x\n", result);
228 * Handle MEDIUM ERRORs or VOLUME OVERFLOWs that indicate partial
229 * success. Since this is a relatively rare error condition, no
230 * care is taken to avoid unnecessary additional work such as
231 * memcpy's that could be avoided.
233 if (driver_byte(result) != 0 && /* An error occurred */
234 (SCpnt->sense_buffer[0] & 0x7f) == 0x70) { /* Sense current */
235 switch (SCpnt->sense_buffer[2]) {
237 case VOLUME_OVERFLOW:
238 case ILLEGAL_REQUEST:
239 if (!(SCpnt->sense_buffer[0] & 0x90))
241 if (!blk_fs_request(SCpnt->request))
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, block_sectors << 9);
297 static int sr_init_command(struct scsi_cmnd * SCpnt)
299 int block=0, this_count, s_size, timeout = SR_TIMEOUT;
300 struct scsi_cd *cd = scsi_cd(SCpnt->request->rq_disk);
302 SCSI_LOG_HLQUEUE(1, printk("Doing sr request, dev = %s, block = %d\n",
303 cd->disk->disk_name, block));
305 if (!cd->device || !scsi_device_online(cd->device)) {
306 SCSI_LOG_HLQUEUE(2, printk("Finishing %ld sectors\n",
307 SCpnt->request->nr_sectors));
308 SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt));
312 if (cd->device->changed) {
314 * quietly refuse to do anything to a changed disc until the
315 * changed bit has been reset
321 * these are already setup, just copy cdb basically
323 if (SCpnt->request->flags & REQ_BLOCK_PC) {
324 scsi_setup_blk_pc_cmnd(SCpnt);
326 if (SCpnt->timeout_per_command)
327 timeout = SCpnt->timeout_per_command;
332 if (!(SCpnt->request->flags & REQ_CMD)) {
333 blk_dump_rq_flags(SCpnt->request, "sr unsup command");
338 * we do lazy blocksize switching (when reading XA sectors,
339 * see CDROMREADMODE2 ioctl)
341 s_size = cd->device->sector_size;
344 sr_set_blocklength(cd, 2048);
346 printk("sr: can't switch blocksize: in interrupt\n");
349 if (s_size != 512 && s_size != 1024 && s_size != 2048) {
350 scmd_printk(KERN_ERR, SCpnt, "bad sector size %d\n", s_size);
354 if (rq_data_dir(SCpnt->request) == WRITE) {
355 if (!cd->device->writeable)
357 SCpnt->cmnd[0] = WRITE_10;
358 SCpnt->sc_data_direction = DMA_TO_DEVICE;
359 cd->cdi.media_written = 1;
360 } else if (rq_data_dir(SCpnt->request) == READ) {
361 SCpnt->cmnd[0] = READ_10;
362 SCpnt->sc_data_direction = DMA_FROM_DEVICE;
364 blk_dump_rq_flags(SCpnt->request, "Unknown sr command");
369 struct scatterlist *sg = SCpnt->request_buffer;
371 for (i = 0; i < SCpnt->use_sg; i++)
372 size += sg[i].length;
374 if (size != SCpnt->request_bufflen && SCpnt->use_sg) {
375 scmd_printk(KERN_ERR, SCpnt,
376 "mismatch count %d, bytes %d\n",
377 size, SCpnt->request_bufflen);
378 if (SCpnt->request_bufflen > size)
379 SCpnt->request_bufflen = SCpnt->bufflen = size;
384 * request doesn't start on hw block boundary, add scatter pads
386 if (((unsigned int)SCpnt->request->sector % (s_size >> 9)) ||
387 (SCpnt->request_bufflen % s_size)) {
388 scmd_printk(KERN_NOTICE, SCpnt, "unaligned transfer\n");
392 this_count = (SCpnt->request_bufflen >> 9) / (s_size >> 9);
395 SCSI_LOG_HLQUEUE(2, printk("%s : %s %d/%ld 512 byte blocks.\n",
397 (rq_data_dir(SCpnt->request) == WRITE) ?
398 "writing" : "reading",
399 this_count, SCpnt->request->nr_sectors));
402 block = (unsigned int)SCpnt->request->sector / (s_size >> 9);
404 if (this_count > 0xffff) {
406 SCpnt->request_bufflen = SCpnt->bufflen =
410 SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
411 SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
412 SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
413 SCpnt->cmnd[5] = (unsigned char) block & 0xff;
414 SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
415 SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
416 SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
419 * We shouldn't disconnect in the middle of a sector, so with a dumb
420 * host adapter, it's safe to assume that we can at least transfer
421 * this many bytes between each connect / disconnect.
423 SCpnt->transfersize = cd->device->sector_size;
424 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
443 static int sr_block_open(struct inode *inode, struct file *file)
445 struct gendisk *disk = inode->i_bdev->bd_disk;
449 if(!(cd = scsi_cd_get(disk)))
452 if((ret = cdrom_open(&cd->cdi, inode, file)) != 0)
458 static int sr_block_release(struct inode *inode, struct file *file)
461 struct scsi_cd *cd = scsi_cd(inode->i_bdev->bd_disk);
462 ret = cdrom_release(&cd->cdi, file);
471 static int sr_block_ioctl(struct inode *inode, struct file *file, unsigned cmd,
474 struct scsi_cd *cd = scsi_cd(inode->i_bdev->bd_disk);
475 struct scsi_device *sdev = cd->device;
478 * Send SCSI addressing ioctls directly to mid level, send other
479 * ioctls to cdrom/block level.
482 case SCSI_IOCTL_GET_IDLUN:
483 case SCSI_IOCTL_GET_BUS_NUMBER:
484 return scsi_ioctl(sdev, cmd, (void __user *)arg);
486 return cdrom_ioctl(file, &cd->cdi, inode, cmd, arg);
489 static int sr_block_media_changed(struct gendisk *disk)
491 struct scsi_cd *cd = scsi_cd(disk);
492 return cdrom_media_changed(&cd->cdi);
495 static struct block_device_operations sr_bdops =
497 .owner = THIS_MODULE,
498 .open = sr_block_open,
499 .release = sr_block_release,
500 .ioctl = sr_block_ioctl,
501 .media_changed = sr_block_media_changed,
503 * No compat_ioctl for now because sr_block_ioctl never
504 * seems to pass arbitary ioctls down to host drivers.
508 static int sr_open(struct cdrom_device_info *cdi, int purpose)
510 struct scsi_cd *cd = cdi->handle;
511 struct scsi_device *sdev = cd->device;
515 * If the device is in error recovery, wait until it is done.
516 * If the device is offline, then disallow any access to it.
519 if (!scsi_block_when_processing_errors(sdev))
528 static void sr_release(struct cdrom_device_info *cdi)
530 struct scsi_cd *cd = cdi->handle;
532 if (cd->device->sector_size > 2048)
533 sr_set_blocklength(cd, 2048);
537 static int sr_probe(struct device *dev)
539 struct scsi_device *sdev = to_scsi_device(dev);
540 struct gendisk *disk;
545 if (sdev->type != TYPE_ROM && sdev->type != TYPE_WORM)
549 cd = kmalloc(sizeof(*cd), GFP_KERNEL);
552 memset(cd, 0, sizeof(*cd));
554 kref_init(&cd->kref);
556 disk = alloc_disk(1);
560 spin_lock(&sr_index_lock);
561 minor = find_first_zero_bit(sr_index_bits, SR_DISKS);
562 if (minor == SR_DISKS) {
563 spin_unlock(&sr_index_lock);
567 __set_bit(minor, sr_index_bits);
568 spin_unlock(&sr_index_lock);
570 disk->major = SCSI_CDROM_MAJOR;
571 disk->first_minor = minor;
572 sprintf(disk->disk_name, "sr%d", minor);
573 disk->fops = &sr_bdops;
574 disk->flags = GENHD_FL_CD;
578 cd->driver = &sr_template;
580 cd->capacity = 0x1fffff;
581 cd->device->changed = 1; /* force recheck CD type */
583 cd->readcd_known = 0;
586 cd->cdi.ops = &sr_dops;
589 cd->cdi.capacity = 1;
590 sprintf(cd->cdi.name, "sr%d", minor);
592 sdev->sector_size = 2048; /* A guess, just in case */
594 /* FIXME: need to handle a get_capabilities failure properly ?? */
595 get_capabilities(cd);
598 snprintf(disk->devfs_name, sizeof(disk->devfs_name),
599 "%s/cd", sdev->devfs_name);
600 disk->driverfs_dev = &sdev->sdev_gendev;
601 set_capacity(disk, cd->capacity);
602 disk->private_data = &cd->driver;
603 disk->queue = sdev->request_queue;
606 if (register_cdrom(&cd->cdi))
609 dev_set_drvdata(dev, cd);
610 disk->flags |= GENHD_FL_REMOVABLE;
613 sdev_printk(KERN_DEBUG, sdev,
614 "Attached scsi CD-ROM %s\n", cd->cdi.name);
626 static void get_sectorsize(struct scsi_cd *cd)
628 unsigned char cmd[10];
629 unsigned char *buffer;
630 int the_result, retries = 3;
632 request_queue_t *queue;
634 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
639 cmd[0] = READ_CAPACITY;
640 memset((void *) &cmd[1], 0, 9);
641 memset(buffer, 0, 8);
643 /* Do the command and wait.. */
644 the_result = scsi_execute_req(cd->device, cmd, DMA_FROM_DEVICE,
645 buffer, 8, NULL, SR_TIMEOUT,
650 } while (the_result && retries);
654 cd->capacity = 0x1fffff;
655 sector_size = 2048; /* A guess, just in case */
658 if (cdrom_get_last_written(&cd->cdi,
661 cd->capacity = 1 + ((buffer[0] << 24) |
665 sector_size = (buffer[4] << 24) |
666 (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
667 switch (sector_size) {
669 * HP 4020i CD-Recorder reports 2340 byte sectors
670 * Philips CD-Writers report 2352 byte sectors
672 * Use 2k sectors for them..
685 printk("%s: unsupported sector size %d.\n",
686 cd->cdi.name, sector_size);
690 cd->device->sector_size = sector_size;
693 * Add this so that we have the ability to correctly gauge
694 * what the device is capable of.
696 set_capacity(cd->disk, cd->capacity);
699 queue = cd->device->request_queue;
700 blk_queue_hardsect_size(queue, sector_size);
706 cd->capacity = 0x1fffff;
707 cd->device->sector_size = 2048; /* A guess, just in case */
711 static void get_capabilities(struct scsi_cd *cd)
713 unsigned char *buffer;
714 struct scsi_mode_data data;
715 unsigned char cmd[MAX_COMMAND_SIZE];
716 struct scsi_sense_hdr sshdr;
717 unsigned int the_result;
720 static const char *loadmech[] =
733 /* allocate transfer buffer */
734 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
736 printk(KERN_ERR "sr: out of memory.\n");
740 /* issue TEST_UNIT_READY until the initial startup UNIT_ATTENTION
741 * conditions are gone, or a timeout happens
745 memset((void *)cmd, 0, MAX_COMMAND_SIZE);
746 cmd[0] = TEST_UNIT_READY;
748 the_result = scsi_execute_req (cd->device, cmd, DMA_NONE, NULL,
749 0, &sshdr, SR_TIMEOUT,
753 } while (retries < 5 &&
754 (!scsi_status_is_good(the_result) ||
755 (scsi_sense_valid(&sshdr) &&
756 sshdr.sense_key == UNIT_ATTENTION)));
758 /* ask for mode page 0x2a */
759 rc = scsi_mode_sense(cd->device, 0, 0x2a, buffer, 128,
760 SR_TIMEOUT, 3, &data, NULL);
762 if (!scsi_status_is_good(rc)) {
763 /* failed, drive doesn't have capabilities mode page */
765 cd->cdi.mask |= (CDC_CD_R | CDC_CD_RW | CDC_DVD_R |
766 CDC_DVD | CDC_DVD_RAM |
767 CDC_SELECT_DISC | CDC_SELECT_SPEED |
768 CDC_MRW | CDC_MRW_W | CDC_RAM);
770 printk("%s: scsi-1 drive\n", cd->cdi.name);
774 n = data.header_length + data.block_descriptor_length;
775 cd->cdi.speed = ((buffer[n + 8] << 8) + buffer[n + 9]) / 176;
776 cd->readcd_known = 1;
777 cd->readcd_cdda = buffer[n + 5] & 0x01;
778 /* print some capability bits */
779 printk("%s: scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n", cd->cdi.name,
780 ((buffer[n + 14] << 8) + buffer[n + 15]) / 176,
782 buffer[n + 3] & 0x01 ? "writer " : "", /* CD Writer */
783 buffer[n + 3] & 0x20 ? "dvd-ram " : "",
784 buffer[n + 2] & 0x02 ? "cd/rw " : "", /* can read rewriteable */
785 buffer[n + 4] & 0x20 ? "xa/form2 " : "", /* can read xa/from2 */
786 buffer[n + 5] & 0x01 ? "cdda " : "", /* can read audio data */
787 loadmech[buffer[n + 6] >> 5]);
788 if ((buffer[n + 6] >> 5) == 0)
789 /* caddy drives can't close tray... */
790 cd->cdi.mask |= CDC_CLOSE_TRAY;
791 if ((buffer[n + 2] & 0x8) == 0)
792 /* not a DVD drive */
793 cd->cdi.mask |= CDC_DVD;
794 if ((buffer[n + 3] & 0x20) == 0)
795 /* can't write DVD-RAM media */
796 cd->cdi.mask |= CDC_DVD_RAM;
797 if ((buffer[n + 3] & 0x10) == 0)
798 /* can't write DVD-R media */
799 cd->cdi.mask |= CDC_DVD_R;
800 if ((buffer[n + 3] & 0x2) == 0)
801 /* can't write CD-RW media */
802 cd->cdi.mask |= CDC_CD_RW;
803 if ((buffer[n + 3] & 0x1) == 0)
804 /* can't write CD-R media */
805 cd->cdi.mask |= CDC_CD_R;
806 if ((buffer[n + 6] & 0x8) == 0)
808 cd->cdi.mask |= CDC_OPEN_TRAY;
810 if ((buffer[n + 6] >> 5) == mechtype_individual_changer ||
811 (buffer[n + 6] >> 5) == mechtype_cartridge_changer)
813 cdrom_number_of_slots(&cd->cdi);
814 if (cd->cdi.capacity <= 1)
816 cd->cdi.mask |= CDC_SELECT_DISC;
817 /*else I don't think it can close its tray
818 cd->cdi.mask |= CDC_CLOSE_TRAY; */
821 * if DVD-RAM, MRW-W or CD-RW, we are randomly writable
823 if ((cd->cdi.mask & (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) !=
824 (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) {
825 cd->device->writeable = 1;
832 * sr_packet() is the entry point for the generic commands generated
833 * by the Uniform CD-ROM layer.
835 static int sr_packet(struct cdrom_device_info *cdi,
836 struct packet_command *cgc)
838 if (cgc->timeout <= 0)
839 cgc->timeout = IOCTL_TIMEOUT;
841 sr_do_ioctl(cdi->handle, cgc);
847 * sr_kref_release - Called to free the scsi_cd structure
848 * @kref: pointer to embedded kref
850 * sr_ref_mutex must be held entering this routine. Because it is
851 * called on last put, you should always use the scsi_cd_get()
852 * scsi_cd_put() helpers which manipulate the semaphore directly
853 * and never do a direct kref_put().
855 static void sr_kref_release(struct kref *kref)
857 struct scsi_cd *cd = container_of(kref, struct scsi_cd, kref);
858 struct gendisk *disk = cd->disk;
860 spin_lock(&sr_index_lock);
861 clear_bit(disk->first_minor, sr_index_bits);
862 spin_unlock(&sr_index_lock);
864 unregister_cdrom(&cd->cdi);
866 disk->private_data = NULL;
873 static int sr_remove(struct device *dev)
875 struct scsi_cd *cd = dev_get_drvdata(dev);
877 del_gendisk(cd->disk);
879 mutex_lock(&sr_ref_mutex);
880 kref_put(&cd->kref, sr_kref_release);
881 mutex_unlock(&sr_ref_mutex);
886 static int __init init_sr(void)
890 rc = register_blkdev(SCSI_CDROM_MAJOR, "sr");
893 return scsi_register_driver(&sr_template.gendrv);
896 static void __exit exit_sr(void)
898 scsi_unregister_driver(&sr_template.gendrv);
899 unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
902 module_init(init_sr);
903 module_exit(exit_sr);
904 MODULE_LICENSE("GPL");