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>
52 #include <scsi/scsi_driver.h>
53 #include <scsi/scsi_ioctl.h> /* For the door lock/unlock commands */
55 #include "scsi_logging.h"
59 MODULE_PARM(xa_test, "i"); /* see sr_ioctl.c */
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 spinlock_t sr_index_lock = SPIN_LOCK_UNLOCKED;
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)
140 if (!kref_get(&cd->kref))
142 if (scsi_device_get(cd->device))
155 static inline void scsi_cd_put(struct scsi_cd *cd)
158 scsi_device_put(cd->device);
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_ioctl(cd->device, SCSI_IOCTL_TEST_UNIT_READY, 0);
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 */
203 * If the disk changed, the capacity will now be different,
204 * so we force a re-read of this information
205 * Force 2048 for the sector size so that filesystems won't
206 * be trying to use something that is too small if the disc
209 cd->needs_sector_size = 1;
210 cd->device->sector_size = 2048;
216 * rw_intr is the interrupt routine for the device driver.
218 * It will be notified on the end of a SCSI read / write, and will take on
219 * of several actions based on success or failure.
221 static void rw_intr(struct scsi_cmnd * SCpnt)
223 int result = SCpnt->result;
224 int this_count = SCpnt->bufflen;
225 int good_bytes = (result == 0 ? this_count : 0);
226 int block_sectors = 0;
228 struct scsi_cd *cd = scsi_cd(SCpnt->request->rq_disk);
231 printk("sr.c done: %x\n", result);
235 * Handle MEDIUM ERRORs or VOLUME OVERFLOWs that indicate partial
236 * success. Since this is a relatively rare error condition, no
237 * care is taken to avoid unnecessary additional work such as
238 * memcpy's that could be avoided.
240 if (driver_byte(result) != 0 && /* An error occurred */
241 (SCpnt->sense_buffer[0] & 0x7f) == 0x70) { /* Sense current */
242 switch (SCpnt->sense_buffer[2]) {
244 case VOLUME_OVERFLOW:
245 case ILLEGAL_REQUEST:
246 if (!(SCpnt->sense_buffer[0] & 0x90))
248 if (!blk_fs_request(SCpnt->request))
250 error_sector = (SCpnt->sense_buffer[3] << 24) |
251 (SCpnt->sense_buffer[4] << 16) |
252 (SCpnt->sense_buffer[5] << 8) |
253 SCpnt->sense_buffer[6];
254 if (SCpnt->request->bio != NULL)
256 bio_sectors(SCpnt->request->bio);
257 if (block_sectors < 4)
259 if (cd->device->sector_size == 2048)
261 error_sector &= ~(block_sectors - 1);
262 good_bytes = (error_sector - SCpnt->request->sector) << 9;
263 if (good_bytes < 0 || good_bytes >= this_count)
266 * The SCSI specification allows for the value
267 * returned by READ CAPACITY to be up to 75 2K
268 * sectors past the last readable block.
269 * Therefore, if we hit a medium error within the
270 * last 75 2K sectors, we decrease the saved size
273 if (error_sector < get_capacity(cd->disk) &&
274 cd->capacity - error_sector < 4 * 75)
275 set_capacity(cd->disk, error_sector);
278 case RECOVERED_ERROR:
281 * An error occured, but it recovered. Inform the
282 * user, but make sure that it's not treated as a
285 print_sense("sr", SCpnt);
287 SCpnt->sense_buffer[0] = 0x0;
288 good_bytes = this_count;
297 * This calls the generic completion function, now that we know
298 * how many actual sectors finished, and how many sectors we need
299 * to say have failed.
301 scsi_io_completion(SCpnt, good_bytes, block_sectors << 9);
304 static int sr_init_command(struct scsi_cmnd * SCpnt)
306 int block=0, this_count, s_size, timeout = SR_TIMEOUT;
307 struct scsi_cd *cd = scsi_cd(SCpnt->request->rq_disk);
309 SCSI_LOG_HLQUEUE(1, printk("Doing sr request, dev = %s, block = %d\n",
310 cd->disk->disk_name, block));
312 if (!cd->device || !scsi_device_online(cd->device)) {
313 SCSI_LOG_HLQUEUE(2, printk("Finishing %ld sectors\n",
314 SCpnt->request->nr_sectors));
315 SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt));
319 if (cd->device->changed) {
321 * quietly refuse to do anything to a changed disc until the
322 * changed bit has been reset
328 * these are already setup, just copy cdb basically
330 if (SCpnt->request->flags & REQ_BLOCK_PC) {
331 struct request *rq = SCpnt->request;
333 if (sizeof(rq->cmd) > sizeof(SCpnt->cmnd))
336 memcpy(SCpnt->cmnd, rq->cmd, sizeof(SCpnt->cmnd));
338 SCpnt->sc_data_direction = SCSI_DATA_NONE;
339 else if (rq_data_dir(rq) == WRITE)
340 SCpnt->sc_data_direction = SCSI_DATA_WRITE;
342 SCpnt->sc_data_direction = SCSI_DATA_READ;
344 this_count = rq->data_len;
346 timeout = rq->timeout;
348 SCpnt->transfersize = rq->data_len;
352 if (!(SCpnt->request->flags & REQ_CMD)) {
353 blk_dump_rq_flags(SCpnt->request, "sr unsup command");
358 * we do lazy blocksize switching (when reading XA sectors,
359 * see CDROMREADMODE2 ioctl)
361 s_size = cd->device->sector_size;
364 sr_set_blocklength(cd, 2048);
366 printk("sr: can't switch blocksize: in interrupt\n");
369 if (s_size != 512 && s_size != 1024 && s_size != 2048) {
370 printk("sr: bad sector size %d\n", s_size);
374 if (rq_data_dir(SCpnt->request) == WRITE) {
375 if (!cd->device->writeable)
377 SCpnt->cmnd[0] = WRITE_10;
378 SCpnt->sc_data_direction = SCSI_DATA_WRITE;
379 } else if (rq_data_dir(SCpnt->request) == READ) {
380 SCpnt->cmnd[0] = READ_10;
381 SCpnt->sc_data_direction = SCSI_DATA_READ;
383 blk_dump_rq_flags(SCpnt->request, "Unknown sr command");
388 struct scatterlist *sg = SCpnt->request_buffer;
390 for (i = 0; i < SCpnt->use_sg; i++)
391 size += sg[i].length;
393 if (size != SCpnt->request_bufflen && SCpnt->use_sg) {
394 printk(KERN_ERR "sr: mismatch count %d, bytes %d\n",
395 size, SCpnt->request_bufflen);
396 if (SCpnt->request_bufflen > size)
397 SCpnt->request_bufflen = SCpnt->bufflen = size;
402 * request doesn't start on hw block boundary, add scatter pads
404 if (((unsigned int)SCpnt->request->sector % (s_size >> 9)) ||
405 (SCpnt->request_bufflen % s_size)) {
406 printk("sr: unaligned transfer\n");
410 this_count = (SCpnt->request_bufflen >> 9) / (s_size >> 9);
413 SCSI_LOG_HLQUEUE(2, printk("%s : %s %d/%ld 512 byte blocks.\n",
415 (rq_data_dir(SCpnt->request) == WRITE) ?
416 "writing" : "reading",
417 this_count, SCpnt->request->nr_sectors));
420 block = (unsigned int)SCpnt->request->sector / (s_size >> 9);
422 if (this_count > 0xffff) {
424 SCpnt->request_bufflen = SCpnt->bufflen =
428 SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
429 SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
430 SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
431 SCpnt->cmnd[5] = (unsigned char) block & 0xff;
432 SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
433 SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
434 SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
437 * We shouldn't disconnect in the middle of a sector, so with a dumb
438 * host adapter, it's safe to assume that we can at least transfer
439 * this many bytes between each connect / disconnect.
441 SCpnt->transfersize = cd->device->sector_size;
442 SCpnt->underflow = this_count << 9;
445 SCpnt->allowed = MAX_RETRIES;
446 SCpnt->timeout_per_command = timeout;
449 * This is the completion routine we use. This is matched in terms
450 * of capability to this function.
452 SCpnt->done = rw_intr;
455 * This indicates that the command is ready from our end to be
461 static int sr_block_open(struct inode *inode, struct file *file)
463 struct gendisk *disk = inode->i_bdev->bd_disk;
464 struct scsi_cd *cd = scsi_cd(inode->i_bdev->bd_disk);
467 if(!(cd = scsi_cd_get(disk)))
470 if((ret = cdrom_open(&cd->cdi, inode, file)) != 0)
476 static int sr_block_release(struct inode *inode, struct file *file)
479 struct scsi_cd *cd = scsi_cd(inode->i_bdev->bd_disk);
480 ret = cdrom_release(&cd->cdi, file);
489 static int sr_block_ioctl(struct inode *inode, struct file *file, unsigned cmd,
492 struct scsi_cd *cd = scsi_cd(inode->i_bdev->bd_disk);
493 struct scsi_device *sdev = cd->device;
496 * Send SCSI addressing ioctls directly to mid level, send other
497 * ioctls to cdrom/block level.
500 case SCSI_IOCTL_GET_IDLUN:
501 case SCSI_IOCTL_GET_BUS_NUMBER:
502 return scsi_ioctl(sdev, cmd, (void __user *)arg);
504 return cdrom_ioctl(&cd->cdi, inode, cmd, arg);
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 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,
522 static int sr_open(struct cdrom_device_info *cdi, int purpose)
524 struct scsi_cd *cd = cdi->handle;
525 struct scsi_device *sdev = cd->device;
529 * If the device is in error recovery, wait until it is done.
530 * If the device is offline, then disallow any access to it.
533 if (!scsi_block_when_processing_errors(sdev))
537 * If this device did not have media in the drive at boot time, then
538 * we would have been unable to get the sector size. Check to see if
539 * this is the case, and try again.
541 if (cd->needs_sector_size)
550 static void sr_release(struct cdrom_device_info *cdi)
552 struct scsi_cd *cd = cdi->handle;
554 if (cd->device->sector_size > 2048)
555 sr_set_blocklength(cd, 2048);
559 static int sr_probe(struct device *dev)
561 struct scsi_device *sdev = to_scsi_device(dev);
562 struct gendisk *disk;
567 if (sdev->type != TYPE_ROM && sdev->type != TYPE_WORM)
571 cd = kmalloc(sizeof(*cd), GFP_KERNEL);
574 memset(cd, 0, sizeof(*cd));
576 kref_init(&cd->kref, sr_kref_release);
578 disk = alloc_disk(1);
582 spin_lock(&sr_index_lock);
583 minor = find_first_zero_bit(sr_index_bits, SR_DISKS);
584 if (minor == SR_DISKS) {
585 spin_unlock(&sr_index_lock);
589 __set_bit(minor, sr_index_bits);
590 spin_unlock(&sr_index_lock);
592 disk->major = SCSI_CDROM_MAJOR;
593 disk->first_minor = minor;
594 sprintf(disk->disk_name, "sr%d", minor);
595 disk->fops = &sr_bdops;
596 disk->flags = GENHD_FL_CD;
600 cd->driver = &sr_template;
602 cd->capacity = 0x1fffff;
603 cd->needs_sector_size = 1;
604 cd->device->changed = 1; /* force recheck CD type */
606 cd->readcd_known = 0;
609 cd->cdi.ops = &sr_dops;
612 cd->cdi.capacity = 1;
613 sprintf(cd->cdi.name, "sr%d", minor);
615 sdev->sector_size = 2048; /* A guess, just in case */
617 /* FIXME: need to handle a get_capabilities failure properly ?? */
618 get_capabilities(cd);
621 snprintf(disk->devfs_name, sizeof(disk->devfs_name),
622 "%s/cd", sdev->devfs_name);
623 disk->driverfs_dev = &sdev->sdev_gendev;
624 set_capacity(disk, cd->capacity);
625 disk->private_data = &cd->driver;
626 disk->queue = sdev->request_queue;
629 if (register_cdrom(&cd->cdi))
632 dev_set_drvdata(dev, cd);
633 disk->flags |= GENHD_FL_REMOVABLE;
637 "Attached scsi CD-ROM %s at scsi%d, channel %d, id %d, lun %d\n",
638 cd->cdi.name, sdev->host->host_no, sdev->channel,
639 sdev->id, sdev->lun);
651 static void get_sectorsize(struct scsi_cd *cd)
653 unsigned char cmd[10];
654 unsigned char *buffer;
655 int the_result, retries = 3;
657 struct scsi_request *SRpnt = NULL;
658 request_queue_t *queue;
660 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
663 SRpnt = scsi_allocate_request(cd->device, GFP_KERNEL);
668 cmd[0] = READ_CAPACITY;
669 memset((void *) &cmd[1], 0, 9);
670 /* Mark as really busy */
671 SRpnt->sr_request->rq_status = RQ_SCSI_BUSY;
672 SRpnt->sr_cmd_len = 0;
674 memset(buffer, 0, 8);
676 /* Do the command and wait.. */
677 SRpnt->sr_data_direction = SCSI_DATA_READ;
678 scsi_wait_req(SRpnt, (void *) cmd, (void *) buffer,
679 8, SR_TIMEOUT, MAX_RETRIES);
681 the_result = SRpnt->sr_result;
684 } while (the_result && retries);
687 scsi_release_request(SRpnt);
691 cd->capacity = 0x1fffff;
692 sector_size = 2048; /* A guess, just in case */
693 cd->needs_sector_size = 1;
696 if (cdrom_get_last_written(&cd->cdi,
699 cd->capacity = 1 + ((buffer[0] << 24) |
703 sector_size = (buffer[4] << 24) |
704 (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
705 switch (sector_size) {
707 * HP 4020i CD-Recorder reports 2340 byte sectors
708 * Philips CD-Writers report 2352 byte sectors
710 * Use 2k sectors for them..
723 printk("%s: unsupported sector size %d.\n",
724 cd->cdi.name, sector_size);
726 cd->needs_sector_size = 1;
729 cd->device->sector_size = sector_size;
732 * Add this so that we have the ability to correctly gauge
733 * what the device is capable of.
735 cd->needs_sector_size = 0;
736 set_capacity(cd->disk, cd->capacity);
739 queue = cd->device->request_queue;
740 blk_queue_hardsect_size(queue, sector_size);
746 cd->capacity = 0x1fffff;
747 sector_size = 2048; /* A guess, just in case */
748 cd->needs_sector_size = 1;
750 scsi_release_request(SRpnt);
754 static void get_capabilities(struct scsi_cd *cd)
756 unsigned char *buffer;
757 int rc, n, mrw_write = 0, mrw = 1,ram_write=0;
758 struct scsi_mode_data data;
759 struct scsi_request *SRpnt;
760 unsigned char cmd[MAX_COMMAND_SIZE];
761 unsigned int the_result;
764 static char *loadmech[] =
776 /* Set read only initially */
777 set_disk_ro(cd->disk, 1);
779 /* allocate a request for the TEST_UNIT_READY */
780 SRpnt = scsi_allocate_request(cd->device, GFP_KERNEL);
782 printk(KERN_WARNING "(get_capabilities:) Request allocation "
787 /* allocate transfer buffer */
788 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
790 printk(KERN_ERR "sr: out of memory.\n");
791 scsi_release_request(SRpnt);
795 /* issue TEST_UNIT_READY until the initial startup UNIT_ATTENTION
796 * conditions are gone, or a timeout happens
800 memset((void *)cmd, 0, MAX_COMMAND_SIZE);
801 cmd[0] = TEST_UNIT_READY;
803 SRpnt->sr_cmd_len = 0;
804 SRpnt->sr_sense_buffer[0] = 0;
805 SRpnt->sr_sense_buffer[2] = 0;
806 SRpnt->sr_data_direction = DMA_NONE;
808 scsi_wait_req (SRpnt, (void *) cmd, buffer,
809 0, SR_TIMEOUT, MAX_RETRIES);
811 the_result = SRpnt->sr_result;
813 } while (retries < 5 &&
814 (!scsi_status_is_good(the_result) ||
815 ((driver_byte(the_result) & DRIVER_SENSE) &&
816 SRpnt->sr_sense_buffer[2] == UNIT_ATTENTION)));
818 /* ask for mode page 0x2a */
819 rc = scsi_mode_sense(cd->device, 0, 0x2a, buffer, 128,
820 SR_TIMEOUT, 3, &data);
822 if (!scsi_status_is_good(rc)) {
823 /* failed, drive doesn't have capabilities mode page */
825 cd->cdi.mask |= (CDC_CD_R | CDC_CD_RW | CDC_DVD_R |
826 CDC_DVD | CDC_DVD_RAM |
827 CDC_SELECT_DISC | CDC_SELECT_SPEED);
828 scsi_release_request(SRpnt);
830 printk("%s: scsi-1 drive\n", cd->cdi.name);
834 if (cdrom_is_mrw(&cd->cdi, &mrw_write)) {
836 cd->cdi.mask |= CDC_MRW;
837 cd->cdi.mask |= CDC_MRW_W;
840 cd->cdi.mask |= CDC_MRW_W;
842 if (cdrom_is_random_writable(&cd->cdi, &ram_write))
843 cd->cdi.mask |= CDC_RAM;
845 cd->cdi.mask |= CDC_RAM;
847 n = data.header_length + data.block_descriptor_length;
848 cd->cdi.speed = ((buffer[n + 8] << 8) + buffer[n + 9]) / 176;
849 cd->readcd_known = 1;
850 cd->readcd_cdda = buffer[n + 5] & 0x01;
851 /* print some capability bits */
852 printk("%s: scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n", cd->cdi.name,
853 ((buffer[n + 14] << 8) + buffer[n + 15]) / 176,
855 buffer[n + 3] & 0x01 ? "writer " : "", /* CD Writer */
856 buffer[n + 3] & 0x20 ? "dvd-ram " : "",
857 buffer[n + 2] & 0x02 ? "cd/rw " : "", /* can read rewriteable */
858 buffer[n + 4] & 0x20 ? "xa/form2 " : "", /* can read xa/from2 */
859 buffer[n + 5] & 0x01 ? "cdda " : "", /* can read audio data */
860 loadmech[buffer[n + 6] >> 5]);
861 if ((buffer[n + 6] >> 5) == 0)
862 /* caddy drives can't close tray... */
863 cd->cdi.mask |= CDC_CLOSE_TRAY;
864 if ((buffer[n + 2] & 0x8) == 0)
865 /* not a DVD drive */
866 cd->cdi.mask |= CDC_DVD;
867 if ((buffer[n + 3] & 0x20) == 0)
868 /* can't write DVD-RAM media */
869 cd->cdi.mask |= CDC_DVD_RAM;
870 if ((buffer[n + 3] & 0x10) == 0)
871 /* can't write DVD-R media */
872 cd->cdi.mask |= CDC_DVD_R;
873 if ((buffer[n + 3] & 0x2) == 0)
874 /* can't write CD-RW media */
875 cd->cdi.mask |= CDC_CD_RW;
876 if ((buffer[n + 3] & 0x1) == 0)
877 /* can't write CD-R media */
878 cd->cdi.mask |= CDC_CD_R;
879 if ((buffer[n + 6] & 0x8) == 0)
881 cd->cdi.mask |= CDC_OPEN_TRAY;
883 if ((buffer[n + 6] >> 5) == mechtype_individual_changer ||
884 (buffer[n + 6] >> 5) == mechtype_cartridge_changer)
886 cdrom_number_of_slots(&cd->cdi);
887 if (cd->cdi.capacity <= 1)
889 cd->cdi.mask |= CDC_SELECT_DISC;
890 /*else I don't think it can close its tray
891 cd->cdi.mask |= CDC_CLOSE_TRAY; */
894 * if DVD-RAM of MRW-W, we are randomly writeable
896 if ((cd->cdi.mask & (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM)) !=
897 (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM)) {
898 cd->device->writeable = 1;
899 set_disk_ro(cd->disk, 0);
902 scsi_release_request(SRpnt);
907 * sr_packet() is the entry point for the generic commands generated
908 * by the Uniform CD-ROM layer.
910 static int sr_packet(struct cdrom_device_info *cdi,
911 struct packet_command *cgc)
913 if (cgc->timeout <= 0)
914 cgc->timeout = IOCTL_TIMEOUT;
916 sr_do_ioctl(cdi->handle, cgc);
922 * sr_kref_release - Called to free the scsi_cd structure
923 * @kref: pointer to embedded kref
925 * sr_ref_sem must be held entering this routine. Because it is
926 * called on last put, you should always use the scsi_cd_get()
927 * scsi_cd_put() helpers which manipulate the semaphore directly
928 * and never do a direct kref_put().
930 static void sr_kref_release(struct kref *kref)
932 struct scsi_cd *cd = container_of(kref, struct scsi_cd, kref);
933 struct gendisk *disk = cd->disk;
935 spin_lock(&sr_index_lock);
936 clear_bit(disk->first_minor, sr_index_bits);
937 spin_unlock(&sr_index_lock);
939 unregister_cdrom(&cd->cdi);
941 disk->private_data = NULL;
948 static int sr_remove(struct device *dev)
950 struct scsi_cd *cd = dev_get_drvdata(dev);
952 del_gendisk(cd->disk);
961 static int __init init_sr(void)
965 rc = register_blkdev(SCSI_CDROM_MAJOR, "sr");
968 return scsi_register_driver(&sr_template.gendrv);
971 static void __exit exit_sr(void)
973 scsi_unregister_driver(&sr_template.gendrv);
974 unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
977 module_init(init_sr);
978 module_exit(exit_sr);
979 MODULE_LICENSE("GPL");