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 cdrom_generic_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))
147 static inline void scsi_cd_put(struct scsi_cd *cd)
155 * This function checks to see if the media has been changed in the
156 * CDROM drive. It is possible that we have already sensed a change,
157 * or the drive may have sensed one and not yet reported it. We must
158 * be ready for either case. This function always reports the current
159 * value of the changed bit. If flag is 0, then the changed bit is reset.
160 * This function could be done as an ioctl, but we would need to have
161 * an inode for that to work, and we do not always have one.
164 int sr_media_change(struct cdrom_device_info *cdi, int slot)
166 struct scsi_cd *cd = cdi->handle;
169 if (CDSL_CURRENT != slot) {
170 /* no changer support */
174 retval = scsi_ioctl(cd->device, SCSI_IOCTL_TEST_UNIT_READY, 0);
176 /* Unable to test, unit probably not ready. This usually
177 * means there is no disc in the drive. Mark as changed,
178 * and we will figure it out later once the drive is
179 * available again. */
180 cd->device->changed = 1;
181 return 1; /* This will force a flush, if called from
182 * check_disk_change */
185 retval = cd->device->changed;
186 cd->device->changed = 0;
187 /* If the disk changed, the capacity will now be different,
188 * so we force a re-read of this information */
190 /* check multisession offset etc */
194 * If the disk changed, the capacity will now be different,
195 * so we force a re-read of this information
196 * Force 2048 for the sector size so that filesystems won't
197 * be trying to use something that is too small if the disc
200 cd->needs_sector_size = 1;
201 cd->device->sector_size = 2048;
207 * rw_intr is the interrupt routine for the device driver.
209 * It will be notified on the end of a SCSI read / write, and will take on
210 * of several actions based on success or failure.
212 static void rw_intr(struct scsi_cmnd * SCpnt)
214 int result = SCpnt->result;
215 int this_count = SCpnt->bufflen;
216 int good_bytes = (result == 0 ? this_count : 0);
217 int block_sectors = 0;
219 struct scsi_cd *cd = scsi_cd(SCpnt->request->rq_disk);
222 printk("sr.c done: %x\n", result);
226 * Handle MEDIUM ERRORs or VOLUME OVERFLOWs that indicate partial
227 * success. Since this is a relatively rare error condition, no
228 * care is taken to avoid unnecessary additional work such as
229 * memcpy's that could be avoided.
231 if (driver_byte(result) != 0 && /* An error occurred */
232 (SCpnt->sense_buffer[0] & 0x7f) == 0x70) { /* Sense current */
233 switch (SCpnt->sense_buffer[2]) {
235 case VOLUME_OVERFLOW:
236 case ILLEGAL_REQUEST:
237 if (!(SCpnt->sense_buffer[0] & 0x90))
239 if (!blk_fs_request(SCpnt->request))
241 error_sector = (SCpnt->sense_buffer[3] << 24) |
242 (SCpnt->sense_buffer[4] << 16) |
243 (SCpnt->sense_buffer[5] << 8) |
244 SCpnt->sense_buffer[6];
245 if (SCpnt->request->bio != NULL)
247 bio_sectors(SCpnt->request->bio);
248 if (block_sectors < 4)
250 if (cd->device->sector_size == 2048)
252 error_sector &= ~(block_sectors - 1);
253 good_bytes = (error_sector - SCpnt->request->sector) << 9;
254 if (good_bytes < 0 || good_bytes >= this_count)
257 * The SCSI specification allows for the value
258 * returned by READ CAPACITY to be up to 75 2K
259 * sectors past the last readable block.
260 * Therefore, if we hit a medium error within the
261 * last 75 2K sectors, we decrease the saved size
264 if (error_sector < get_capacity(cd->disk) &&
265 cd->capacity - error_sector < 4 * 75)
266 set_capacity(cd->disk, error_sector);
269 case RECOVERED_ERROR:
272 * An error occured, but it recovered. Inform the
273 * user, but make sure that it's not treated as a
276 print_sense("sr", SCpnt);
278 SCpnt->sense_buffer[0] = 0x0;
279 good_bytes = this_count;
288 * This calls the generic completion function, now that we know
289 * how many actual sectors finished, and how many sectors we need
290 * to say have failed.
292 scsi_io_completion(SCpnt, good_bytes, block_sectors << 9);
295 static int sr_init_command(struct scsi_cmnd * SCpnt)
297 int block=0, this_count, s_size, timeout = SR_TIMEOUT;
298 struct scsi_cd *cd = scsi_cd(SCpnt->request->rq_disk);
300 SCSI_LOG_HLQUEUE(1, printk("Doing sr request, dev = %s, block = %d\n",
301 cd->disk->disk_name, block));
303 if (!cd->device || !scsi_device_online(cd->device)) {
304 SCSI_LOG_HLQUEUE(2, printk("Finishing %ld sectors\n",
305 SCpnt->request->nr_sectors));
306 SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt));
310 if (cd->device->changed) {
312 * quietly refuse to do anything to a changed disc until the
313 * changed bit has been reset
319 * these are already setup, just copy cdb basically
321 if (SCpnt->request->flags & REQ_BLOCK_PC) {
322 struct request *rq = SCpnt->request;
324 if (sizeof(rq->cmd) > sizeof(SCpnt->cmnd))
327 memcpy(SCpnt->cmnd, rq->cmd, sizeof(SCpnt->cmnd));
329 SCpnt->sc_data_direction = SCSI_DATA_NONE;
330 else if (rq_data_dir(rq) == WRITE)
331 SCpnt->sc_data_direction = SCSI_DATA_WRITE;
333 SCpnt->sc_data_direction = SCSI_DATA_READ;
335 this_count = rq->data_len;
337 timeout = rq->timeout;
339 SCpnt->transfersize = rq->data_len;
343 if (!(SCpnt->request->flags & REQ_CMD)) {
344 blk_dump_rq_flags(SCpnt->request, "sr unsup command");
349 * we do lazy blocksize switching (when reading XA sectors,
350 * see CDROMREADMODE2 ioctl)
352 s_size = cd->device->sector_size;
355 sr_set_blocklength(cd, 2048);
357 printk("sr: can't switch blocksize: in interrupt\n");
360 if (s_size != 512 && s_size != 1024 && s_size != 2048) {
361 printk("sr: bad sector size %d\n", s_size);
365 if (rq_data_dir(SCpnt->request) == WRITE) {
366 if (!cd->device->writeable)
368 SCpnt->cmnd[0] = WRITE_10;
369 SCpnt->sc_data_direction = SCSI_DATA_WRITE;
370 } else if (rq_data_dir(SCpnt->request) == READ) {
371 SCpnt->cmnd[0] = READ_10;
372 SCpnt->sc_data_direction = SCSI_DATA_READ;
374 blk_dump_rq_flags(SCpnt->request, "Unknown sr command");
379 struct scatterlist *sg = SCpnt->request_buffer;
381 for (i = 0; i < SCpnt->use_sg; i++)
382 size += sg[i].length;
384 if (size != SCpnt->request_bufflen && SCpnt->use_sg) {
385 printk(KERN_ERR "sr: mismatch count %d, bytes %d\n",
386 size, SCpnt->request_bufflen);
387 if (SCpnt->request_bufflen > size)
388 SCpnt->request_bufflen = SCpnt->bufflen = size;
393 * request doesn't start on hw block boundary, add scatter pads
395 if (((unsigned int)SCpnt->request->sector % (s_size >> 9)) ||
396 (SCpnt->request_bufflen % s_size)) {
397 printk("sr: unaligned transfer\n");
401 this_count = (SCpnt->request_bufflen >> 9) / (s_size >> 9);
404 SCSI_LOG_HLQUEUE(2, printk("%s : %s %d/%ld 512 byte blocks.\n",
406 (rq_data_dir(SCpnt->request) == WRITE) ?
407 "writing" : "reading",
408 this_count, SCpnt->request->nr_sectors));
411 block = (unsigned int)SCpnt->request->sector / (s_size >> 9);
413 if (this_count > 0xffff) {
415 SCpnt->request_bufflen = SCpnt->bufflen =
419 SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
420 SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
421 SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
422 SCpnt->cmnd[5] = (unsigned char) block & 0xff;
423 SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
424 SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
425 SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
428 * We shouldn't disconnect in the middle of a sector, so with a dumb
429 * host adapter, it's safe to assume that we can at least transfer
430 * this many bytes between each connect / disconnect.
432 SCpnt->transfersize = cd->device->sector_size;
433 SCpnt->underflow = this_count << 9;
436 SCpnt->allowed = MAX_RETRIES;
437 SCpnt->timeout_per_command = timeout;
440 * This is the completion routine we use. This is matched in terms
441 * of capability to this function.
443 SCpnt->done = rw_intr;
446 * This indicates that the command is ready from our end to be
452 static int sr_block_open(struct inode *inode, struct file *file)
454 struct gendisk *disk = inode->i_bdev->bd_disk;
455 struct scsi_cd *cd = scsi_cd(inode->i_bdev->bd_disk);
458 if(!(cd = scsi_cd_get(disk)))
461 if((ret = cdrom_open(&cd->cdi, inode, file)) != 0)
467 static int sr_block_release(struct inode *inode, struct file *file)
470 struct scsi_cd *cd = scsi_cd(inode->i_bdev->bd_disk);
471 ret = cdrom_release(&cd->cdi, file);
480 static int sr_block_ioctl(struct inode *inode, struct file *file, unsigned cmd,
483 struct scsi_cd *cd = scsi_cd(inode->i_bdev->bd_disk);
484 struct scsi_device *sdev = cd->device;
487 * Send SCSI addressing ioctls directly to mid level, send other
488 * ioctls to cdrom/block level.
491 case SCSI_IOCTL_GET_IDLUN:
492 case SCSI_IOCTL_GET_BUS_NUMBER:
493 return scsi_ioctl(sdev, cmd, (void *)arg);
495 return cdrom_ioctl(&cd->cdi, inode, cmd, arg);
498 static int sr_block_media_changed(struct gendisk *disk)
500 struct scsi_cd *cd = scsi_cd(disk);
501 return cdrom_media_changed(&cd->cdi);
504 struct block_device_operations sr_bdops =
506 .owner = THIS_MODULE,
507 .open = sr_block_open,
508 .release = sr_block_release,
509 .ioctl = sr_block_ioctl,
510 .media_changed = sr_block_media_changed,
513 static int sr_open(struct cdrom_device_info *cdi, int purpose)
515 struct scsi_cd *cd = cdi->handle;
516 struct scsi_device *sdev = cd->device;
520 * If the device is in error recovery, wait until it is done.
521 * If the device is offline, then disallow any access to it.
524 if (!scsi_block_when_processing_errors(sdev))
528 * If this device did not have media in the drive at boot time, then
529 * we would have been unable to get the sector size. Check to see if
530 * this is the case, and try again.
532 if (cd->needs_sector_size)
541 static void sr_release(struct cdrom_device_info *cdi)
543 struct scsi_cd *cd = cdi->handle;
545 if (cd->device->sector_size > 2048)
546 sr_set_blocklength(cd, 2048);
550 static int sr_probe(struct device *dev)
552 struct scsi_device *sdev = to_scsi_device(dev);
553 struct gendisk *disk;
558 if (sdev->type != TYPE_ROM && sdev->type != TYPE_WORM)
561 if ((error = scsi_device_get(sdev)) != 0)
565 cd = kmalloc(sizeof(*cd), GFP_KERNEL);
568 memset(cd, 0, sizeof(*cd));
570 kref_init(&cd->kref, sr_kref_release);
572 disk = alloc_disk(1);
576 spin_lock(&sr_index_lock);
577 minor = find_first_zero_bit(sr_index_bits, SR_DISKS);
578 if (minor == SR_DISKS) {
579 spin_unlock(&sr_index_lock);
583 __set_bit(minor, sr_index_bits);
584 spin_unlock(&sr_index_lock);
586 disk->major = SCSI_CDROM_MAJOR;
587 disk->first_minor = minor;
588 sprintf(disk->disk_name, "sr%d", minor);
589 disk->fops = &sr_bdops;
590 disk->flags = GENHD_FL_CD;
594 cd->driver = &sr_template;
596 cd->capacity = 0x1fffff;
597 cd->needs_sector_size = 1;
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);
615 snprintf(disk->devfs_name, sizeof(disk->devfs_name),
616 "%s/cd", sdev->devfs_name);
617 disk->driverfs_dev = &sdev->sdev_gendev;
618 set_capacity(disk, cd->capacity);
619 disk->private_data = &cd->driver;
620 disk->queue = sdev->request_queue;
623 if (register_cdrom(&cd->cdi))
626 dev_set_drvdata(dev, cd);
627 disk->flags |= GENHD_FL_REMOVABLE;
631 "Attached scsi CD-ROM %s at scsi%d, channel %d, id %d, lun %d\n",
632 cd->cdi.name, sdev->host->host_no, sdev->channel,
633 sdev->id, sdev->lun);
641 scsi_device_put(sdev);
647 static void get_sectorsize(struct scsi_cd *cd)
649 unsigned char cmd[10];
650 unsigned char *buffer;
651 int the_result, retries = 3;
653 struct scsi_request *SRpnt = NULL;
654 request_queue_t *queue;
656 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
659 SRpnt = scsi_allocate_request(cd->device, GFP_KERNEL);
664 cmd[0] = READ_CAPACITY;
665 memset((void *) &cmd[1], 0, 9);
666 /* Mark as really busy */
667 SRpnt->sr_request->rq_status = RQ_SCSI_BUSY;
668 SRpnt->sr_cmd_len = 0;
670 memset(buffer, 0, 8);
672 /* Do the command and wait.. */
673 SRpnt->sr_data_direction = SCSI_DATA_READ;
674 scsi_wait_req(SRpnt, (void *) cmd, (void *) buffer,
675 8, SR_TIMEOUT, MAX_RETRIES);
677 the_result = SRpnt->sr_result;
680 } while (the_result && retries);
683 scsi_release_request(SRpnt);
687 cd->capacity = 0x1fffff;
688 sector_size = 2048; /* A guess, just in case */
689 cd->needs_sector_size = 1;
692 if (cdrom_get_last_written(&cd->cdi,
695 cd->capacity = 1 + ((buffer[0] << 24) |
699 sector_size = (buffer[4] << 24) |
700 (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
701 switch (sector_size) {
703 * HP 4020i CD-Recorder reports 2340 byte sectors
704 * Philips CD-Writers report 2352 byte sectors
706 * Use 2k sectors for them..
719 printk("%s: unsupported sector size %d.\n",
720 cd->cdi.name, sector_size);
722 cd->needs_sector_size = 1;
725 cd->device->sector_size = sector_size;
728 * Add this so that we have the ability to correctly gauge
729 * what the device is capable of.
731 cd->needs_sector_size = 0;
732 set_capacity(cd->disk, cd->capacity);
735 queue = cd->device->request_queue;
736 blk_queue_hardsect_size(queue, sector_size);
742 cd->capacity = 0x1fffff;
743 sector_size = 2048; /* A guess, just in case */
744 cd->needs_sector_size = 1;
746 scsi_release_request(SRpnt);
750 static void get_capabilities(struct scsi_cd *cd)
752 unsigned char *buffer;
753 int rc, n, mrw_write = 0, mrw = 1,ram_write=0;
754 struct scsi_mode_data data;
755 struct scsi_request *SRpnt;
756 unsigned char cmd[MAX_COMMAND_SIZE];
757 unsigned int the_result;
760 static char *loadmech[] =
772 /* Set read only initially */
773 set_disk_ro(cd->disk, 1);
775 /* allocate a request for the TEST_UNIT_READY */
776 SRpnt = scsi_allocate_request(cd->device, GFP_KERNEL);
778 printk(KERN_WARNING "(get_capabilities:) Request allocation "
783 /* allocate transfer buffer */
784 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
786 printk(KERN_ERR "sr: out of memory.\n");
787 scsi_release_request(SRpnt);
791 /* issue TEST_UNIT_READY until the initial startup UNIT_ATTENTION
792 * conditions are gone, or a timeout happens
796 memset((void *)cmd, 0, MAX_COMMAND_SIZE);
797 cmd[0] = TEST_UNIT_READY;
799 SRpnt->sr_cmd_len = 0;
800 SRpnt->sr_sense_buffer[0] = 0;
801 SRpnt->sr_sense_buffer[2] = 0;
802 SRpnt->sr_data_direction = DMA_NONE;
804 scsi_wait_req (SRpnt, (void *) cmd, buffer,
805 0, SR_TIMEOUT, MAX_RETRIES);
807 the_result = SRpnt->sr_result;
809 } while (retries < 5 &&
810 (!scsi_status_is_good(the_result) ||
811 ((driver_byte(the_result) & DRIVER_SENSE) &&
812 SRpnt->sr_sense_buffer[2] == UNIT_ATTENTION)));
814 /* ask for mode page 0x2a */
815 rc = scsi_mode_sense(cd->device, 0, 0x2a, buffer, 128,
816 SR_TIMEOUT, 3, &data);
818 if (!scsi_status_is_good(rc)) {
819 /* failed, drive doesn't have capabilities mode page */
821 cd->cdi.mask |= (CDC_CD_R | CDC_CD_RW | CDC_DVD_R |
822 CDC_DVD | CDC_DVD_RAM |
823 CDC_SELECT_DISC | CDC_SELECT_SPEED);
824 scsi_release_request(SRpnt);
826 printk("%s: scsi-1 drive\n", cd->cdi.name);
830 if (cdrom_is_mrw(&cd->cdi, &mrw_write)) {
832 cd->cdi.mask |= CDC_MRW;
833 cd->cdi.mask |= CDC_MRW_W;
836 cd->cdi.mask |= CDC_MRW_W;
838 if (cdrom_is_random_writable(&cd->cdi, &ram_write))
839 cd->cdi.mask |= CDC_RAM;
841 cd->cdi.mask |= CDC_RAM;
843 n = data.header_length + data.block_descriptor_length;
844 cd->cdi.speed = ((buffer[n + 8] << 8) + buffer[n + 9]) / 176;
845 cd->readcd_known = 1;
846 cd->readcd_cdda = buffer[n + 5] & 0x01;
847 /* print some capability bits */
848 printk("%s: scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n", cd->cdi.name,
849 ((buffer[n + 14] << 8) + buffer[n + 15]) / 176,
851 buffer[n + 3] & 0x01 ? "writer " : "", /* CD Writer */
852 buffer[n + 3] & 0x20 ? "dvd-ram " : "",
853 buffer[n + 2] & 0x02 ? "cd/rw " : "", /* can read rewriteable */
854 buffer[n + 4] & 0x20 ? "xa/form2 " : "", /* can read xa/from2 */
855 buffer[n + 5] & 0x01 ? "cdda " : "", /* can read audio data */
856 loadmech[buffer[n + 6] >> 5]);
857 if ((buffer[n + 6] >> 5) == 0)
858 /* caddy drives can't close tray... */
859 cd->cdi.mask |= CDC_CLOSE_TRAY;
860 if ((buffer[n + 2] & 0x8) == 0)
861 /* not a DVD drive */
862 cd->cdi.mask |= CDC_DVD;
863 if ((buffer[n + 3] & 0x20) == 0)
864 /* can't write DVD-RAM media */
865 cd->cdi.mask |= CDC_DVD_RAM;
866 if ((buffer[n + 3] & 0x10) == 0)
867 /* can't write DVD-R media */
868 cd->cdi.mask |= CDC_DVD_R;
869 if ((buffer[n + 3] & 0x2) == 0)
870 /* can't write CD-RW media */
871 cd->cdi.mask |= CDC_CD_RW;
872 if ((buffer[n + 3] & 0x1) == 0)
873 /* can't write CD-R media */
874 cd->cdi.mask |= CDC_CD_R;
875 if ((buffer[n + 6] & 0x8) == 0)
877 cd->cdi.mask |= CDC_OPEN_TRAY;
879 if ((buffer[n + 6] >> 5) == mechtype_individual_changer ||
880 (buffer[n + 6] >> 5) == mechtype_cartridge_changer)
882 cdrom_number_of_slots(&cd->cdi);
883 if (cd->cdi.capacity <= 1)
885 cd->cdi.mask |= CDC_SELECT_DISC;
886 /*else I don't think it can close its tray
887 cd->cdi.mask |= CDC_CLOSE_TRAY; */
890 * if DVD-RAM of MRW-W, we are randomly writeable
892 if ((cd->cdi.mask & (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM)) !=
893 (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM)) {
894 cd->device->writeable = 1;
895 set_disk_ro(cd->disk, 0);
898 scsi_release_request(SRpnt);
903 * sr_packet() is the entry point for the generic commands generated
904 * by the Uniform CD-ROM layer.
906 static int sr_packet(struct cdrom_device_info *cdi,
907 struct cdrom_generic_command *cgc)
909 if (cgc->timeout <= 0)
910 cgc->timeout = IOCTL_TIMEOUT;
912 sr_do_ioctl(cdi->handle, cgc);
918 * sr_kref_release - Called to free the scsi_cd structure
919 * @kref: pointer to embedded kref
921 * sr_ref_sem must be held entering this routine. Because it is
922 * called on last put, you should always use the scsi_cd_get()
923 * scsi_cd_put() helpers which manipulate the semaphore directly
924 * and never do a direct kref_put().
926 static void sr_kref_release(struct kref *kref)
928 struct scsi_cd *cd = container_of(kref, struct scsi_cd, kref);
929 struct scsi_device *sdev = cd->device;
930 struct gendisk *disk = cd->disk;
932 spin_lock(&sr_index_lock);
933 clear_bit(disk->first_minor, sr_index_bits);
934 spin_unlock(&sr_index_lock);
936 unregister_cdrom(&cd->cdi);
938 disk->private_data = NULL;
944 scsi_device_put(sdev);
947 static int sr_remove(struct device *dev)
949 struct scsi_cd *cd = dev_get_drvdata(dev);
951 del_gendisk(cd->disk);
958 static int __init init_sr(void)
962 rc = register_blkdev(SCSI_CDROM_MAJOR, "sr");
965 return scsi_register_driver(&sr_template.gendrv);
968 static void __exit exit_sr(void)
970 scsi_unregister_driver(&sr_template.gendrv);
971 unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
974 module_init(init_sr);
975 module_exit(exit_sr);
976 MODULE_LICENSE("GPL");