[PATCH] ext3: remove d_splice_alias NULL check from ext3_lookup
[linux-2.6/btrfs-unstable.git] / drivers / scsi / sr.c
blob997f8e30509b89bf6e2f5af57a0bdffb689f9a87
1 /*
2 * sr.c Copyright (C) 1992 David Giller
3 * Copyright (C) 1993, 1994, 1995, 1999 Eric Youngdale
5 * adapted from:
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
12 * enhancements.
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
18 * provide auto-eject.
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>
36 #include <linux/fs.h>
37 #include <linux/kernel.h>
38 #include <linux/sched.h>
39 #include <linux/mm.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"
60 #include "sr.h"
63 #define SR_DISKS 256
65 #define MAX_RETRIES 3
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 = {
79 .owner = THIS_MODULE,
80 .gendrv = {
81 .name = "sr",
82 .probe = sr_probe,
83 .remove = sr_remove,
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 = {
106 .open = sr_open,
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,
115 .reset = sr_reset,
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)
139 goto out;
140 cd = scsi_cd(disk);
141 kref_get(&cd->kref);
142 if (scsi_device_get(cd->device))
143 goto out_put;
144 goto out;
146 out_put:
147 kref_put(&cd->kref, sr_kref_release);
148 cd = NULL;
149 out:
150 mutex_unlock(&sr_ref_mutex);
151 return cd;
154 static 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;
177 int retval;
179 if (CDSL_CURRENT != slot) {
180 /* no changer support */
181 return -EINVAL;
184 retval = scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES);
185 if (retval) {
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 */
199 if (retval) {
200 /* check multisession offset etc */
201 sr_cd_check(cdi);
203 get_sectorsize(cd);
205 return retval;
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;
220 long error_sector;
221 struct scsi_cd *cd = scsi_cd(SCpnt->request->rq_disk);
223 #ifdef DEBUG
224 printk("sr.c done: %x\n", result);
225 #endif
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]) {
236 case MEDIUM_ERROR:
237 case VOLUME_OVERFLOW:
238 case ILLEGAL_REQUEST:
239 if (!(SCpnt->sense_buffer[0] & 0x90))
240 break;
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)
246 block_sectors =
247 bio_sectors(SCpnt->request->bio);
248 if (block_sectors < 4)
249 block_sectors = 4;
250 if (cd->device->sector_size == 2048)
251 error_sector <<= 2;
252 error_sector &= ~(block_sectors - 1);
253 good_bytes = (error_sector - SCpnt->request->sector) << 9;
254 if (good_bytes < 0 || good_bytes >= this_count)
255 good_bytes = 0;
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
262 * value.
264 if (error_sector < get_capacity(cd->disk) &&
265 cd->capacity - error_sector < 4 * 75)
266 set_capacity(cd->disk, error_sector);
267 break;
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
274 * hard error.
276 scsi_print_sense("sr", SCpnt);
277 SCpnt->result = 0;
278 SCpnt->sense_buffer[0] = 0x0;
279 good_bytes = this_count;
280 break;
282 default:
283 break;
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));
307 return 0;
310 if (cd->device->changed) {
312 * quietly refuse to do anything to a changed disc until the
313 * changed bit has been reset
315 return 0;
319 * we do lazy blocksize switching (when reading XA sectors,
320 * see CDROMREADMODE2 ioctl)
322 s_size = cd->device->sector_size;
323 if (s_size > 2048) {
324 if (!in_interrupt())
325 sr_set_blocklength(cd, 2048);
326 else
327 printk("sr: can't switch blocksize: in interrupt\n");
330 if (s_size != 512 && s_size != 1024 && s_size != 2048) {
331 scmd_printk(KERN_ERR, SCpnt, "bad sector size %d\n", s_size);
332 return 0;
335 if (rq_data_dir(SCpnt->request) == WRITE) {
336 if (!cd->device->writeable)
337 return 0;
338 SCpnt->cmnd[0] = WRITE_10;
339 SCpnt->sc_data_direction = DMA_TO_DEVICE;
340 cd->cdi.media_written = 1;
341 } else if (rq_data_dir(SCpnt->request) == READ) {
342 SCpnt->cmnd[0] = READ_10;
343 SCpnt->sc_data_direction = DMA_FROM_DEVICE;
344 } else {
345 blk_dump_rq_flags(SCpnt->request, "Unknown sr command");
346 return 0;
350 struct scatterlist *sg = SCpnt->request_buffer;
351 int i, size = 0;
352 for (i = 0; i < SCpnt->use_sg; i++)
353 size += sg[i].length;
355 if (size != SCpnt->request_bufflen && SCpnt->use_sg) {
356 scmd_printk(KERN_ERR, SCpnt,
357 "mismatch count %d, bytes %d\n",
358 size, SCpnt->request_bufflen);
359 if (SCpnt->request_bufflen > size)
360 SCpnt->request_bufflen = SCpnt->bufflen = size;
365 * request doesn't start on hw block boundary, add scatter pads
367 if (((unsigned int)SCpnt->request->sector % (s_size >> 9)) ||
368 (SCpnt->request_bufflen % s_size)) {
369 scmd_printk(KERN_NOTICE, SCpnt, "unaligned transfer\n");
370 return 0;
373 this_count = (SCpnt->request_bufflen >> 9) / (s_size >> 9);
376 SCSI_LOG_HLQUEUE(2, printk("%s : %s %d/%ld 512 byte blocks.\n",
377 cd->cdi.name,
378 (rq_data_dir(SCpnt->request) == WRITE) ?
379 "writing" : "reading",
380 this_count, SCpnt->request->nr_sectors));
382 SCpnt->cmnd[1] = 0;
383 block = (unsigned int)SCpnt->request->sector / (s_size >> 9);
385 if (this_count > 0xffff) {
386 this_count = 0xffff;
387 SCpnt->request_bufflen = SCpnt->bufflen =
388 this_count * s_size;
391 SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
392 SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
393 SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
394 SCpnt->cmnd[5] = (unsigned char) block & 0xff;
395 SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
396 SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
397 SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
400 * We shouldn't disconnect in the middle of a sector, so with a dumb
401 * host adapter, it's safe to assume that we can at least transfer
402 * this many bytes between each connect / disconnect.
404 SCpnt->transfersize = cd->device->sector_size;
405 SCpnt->underflow = this_count << 9;
406 SCpnt->allowed = MAX_RETRIES;
407 SCpnt->timeout_per_command = timeout;
410 * This is the completion routine we use. This is matched in terms
411 * of capability to this function.
413 SCpnt->done = rw_intr;
416 * This indicates that the command is ready from our end to be
417 * queued.
419 return 1;
422 static int sr_block_open(struct inode *inode, struct file *file)
424 struct gendisk *disk = inode->i_bdev->bd_disk;
425 struct scsi_cd *cd;
426 int ret = 0;
428 if(!(cd = scsi_cd_get(disk)))
429 return -ENXIO;
431 if((ret = cdrom_open(&cd->cdi, inode, file)) != 0)
432 scsi_cd_put(cd);
434 return ret;
437 static int sr_block_release(struct inode *inode, struct file *file)
439 int ret;
440 struct scsi_cd *cd = scsi_cd(inode->i_bdev->bd_disk);
441 ret = cdrom_release(&cd->cdi, file);
442 if(ret)
443 return ret;
445 scsi_cd_put(cd);
447 return 0;
450 static int sr_block_ioctl(struct inode *inode, struct file *file, unsigned cmd,
451 unsigned long arg)
453 struct scsi_cd *cd = scsi_cd(inode->i_bdev->bd_disk);
454 struct scsi_device *sdev = cd->device;
457 * Send SCSI addressing ioctls directly to mid level, send other
458 * ioctls to cdrom/block level.
460 switch (cmd) {
461 case SCSI_IOCTL_GET_IDLUN:
462 case SCSI_IOCTL_GET_BUS_NUMBER:
463 return scsi_ioctl(sdev, cmd, (void __user *)arg);
465 return cdrom_ioctl(file, &cd->cdi, inode, cmd, arg);
468 static int sr_block_media_changed(struct gendisk *disk)
470 struct scsi_cd *cd = scsi_cd(disk);
471 return cdrom_media_changed(&cd->cdi);
474 static struct block_device_operations sr_bdops =
476 .owner = THIS_MODULE,
477 .open = sr_block_open,
478 .release = sr_block_release,
479 .ioctl = sr_block_ioctl,
480 .media_changed = sr_block_media_changed,
482 * No compat_ioctl for now because sr_block_ioctl never
483 * seems to pass arbitary ioctls down to host drivers.
487 static int sr_open(struct cdrom_device_info *cdi, int purpose)
489 struct scsi_cd *cd = cdi->handle;
490 struct scsi_device *sdev = cd->device;
491 int retval;
494 * If the device is in error recovery, wait until it is done.
495 * If the device is offline, then disallow any access to it.
497 retval = -ENXIO;
498 if (!scsi_block_when_processing_errors(sdev))
499 goto error_out;
501 return 0;
503 error_out:
504 return retval;
507 static void sr_release(struct cdrom_device_info *cdi)
509 struct scsi_cd *cd = cdi->handle;
511 if (cd->device->sector_size > 2048)
512 sr_set_blocklength(cd, 2048);
516 static int sr_probe(struct device *dev)
518 struct scsi_device *sdev = to_scsi_device(dev);
519 struct gendisk *disk;
520 struct scsi_cd *cd;
521 int minor, error;
523 error = -ENODEV;
524 if (sdev->type != TYPE_ROM && sdev->type != TYPE_WORM)
525 goto fail;
527 error = -ENOMEM;
528 cd = kmalloc(sizeof(*cd), GFP_KERNEL);
529 if (!cd)
530 goto fail;
531 memset(cd, 0, sizeof(*cd));
533 kref_init(&cd->kref);
535 disk = alloc_disk(1);
536 if (!disk)
537 goto fail_free;
539 spin_lock(&sr_index_lock);
540 minor = find_first_zero_bit(sr_index_bits, SR_DISKS);
541 if (minor == SR_DISKS) {
542 spin_unlock(&sr_index_lock);
543 error = -EBUSY;
544 goto fail_put;
546 __set_bit(minor, sr_index_bits);
547 spin_unlock(&sr_index_lock);
549 disk->major = SCSI_CDROM_MAJOR;
550 disk->first_minor = minor;
551 sprintf(disk->disk_name, "sr%d", minor);
552 disk->fops = &sr_bdops;
553 disk->flags = GENHD_FL_CD;
555 cd->device = sdev;
556 cd->disk = disk;
557 cd->driver = &sr_template;
558 cd->disk = disk;
559 cd->capacity = 0x1fffff;
560 cd->device->changed = 1; /* force recheck CD type */
561 cd->use = 1;
562 cd->readcd_known = 0;
563 cd->readcd_cdda = 0;
565 cd->cdi.ops = &sr_dops;
566 cd->cdi.handle = cd;
567 cd->cdi.mask = 0;
568 cd->cdi.capacity = 1;
569 sprintf(cd->cdi.name, "sr%d", minor);
571 sdev->sector_size = 2048; /* A guess, just in case */
573 /* FIXME: need to handle a get_capabilities failure properly ?? */
574 get_capabilities(cd);
575 sr_vendor_init(cd);
577 snprintf(disk->devfs_name, sizeof(disk->devfs_name),
578 "%s/cd", sdev->devfs_name);
579 disk->driverfs_dev = &sdev->sdev_gendev;
580 set_capacity(disk, cd->capacity);
581 disk->private_data = &cd->driver;
582 disk->queue = sdev->request_queue;
583 cd->cdi.disk = disk;
585 if (register_cdrom(&cd->cdi))
586 goto fail_put;
588 dev_set_drvdata(dev, cd);
589 disk->flags |= GENHD_FL_REMOVABLE;
590 add_disk(disk);
592 sdev_printk(KERN_DEBUG, sdev,
593 "Attached scsi CD-ROM %s\n", cd->cdi.name);
594 return 0;
596 fail_put:
597 put_disk(disk);
598 fail_free:
599 kfree(cd);
600 fail:
601 return error;
605 static void get_sectorsize(struct scsi_cd *cd)
607 unsigned char cmd[10];
608 unsigned char *buffer;
609 int the_result, retries = 3;
610 int sector_size;
611 request_queue_t *queue;
613 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
614 if (!buffer)
615 goto Enomem;
617 do {
618 cmd[0] = READ_CAPACITY;
619 memset((void *) &cmd[1], 0, 9);
620 memset(buffer, 0, 8);
622 /* Do the command and wait.. */
623 the_result = scsi_execute_req(cd->device, cmd, DMA_FROM_DEVICE,
624 buffer, 8, NULL, SR_TIMEOUT,
625 MAX_RETRIES);
627 retries--;
629 } while (the_result && retries);
632 if (the_result) {
633 cd->capacity = 0x1fffff;
634 sector_size = 2048; /* A guess, just in case */
635 } else {
636 #if 0
637 if (cdrom_get_last_written(&cd->cdi,
638 &cd->capacity))
639 #endif
640 cd->capacity = 1 + ((buffer[0] << 24) |
641 (buffer[1] << 16) |
642 (buffer[2] << 8) |
643 buffer[3]);
644 sector_size = (buffer[4] << 24) |
645 (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
646 switch (sector_size) {
648 * HP 4020i CD-Recorder reports 2340 byte sectors
649 * Philips CD-Writers report 2352 byte sectors
651 * Use 2k sectors for them..
653 case 0:
654 case 2340:
655 case 2352:
656 sector_size = 2048;
657 /* fall through */
658 case 2048:
659 cd->capacity *= 4;
660 /* fall through */
661 case 512:
662 break;
663 default:
664 printk("%s: unsupported sector size %d.\n",
665 cd->cdi.name, sector_size);
666 cd->capacity = 0;
669 cd->device->sector_size = sector_size;
672 * Add this so that we have the ability to correctly gauge
673 * what the device is capable of.
675 set_capacity(cd->disk, cd->capacity);
678 queue = cd->device->request_queue;
679 blk_queue_hardsect_size(queue, sector_size);
680 out:
681 kfree(buffer);
682 return;
684 Enomem:
685 cd->capacity = 0x1fffff;
686 cd->device->sector_size = 2048; /* A guess, just in case */
687 goto out;
690 static void get_capabilities(struct scsi_cd *cd)
692 unsigned char *buffer;
693 struct scsi_mode_data data;
694 unsigned char cmd[MAX_COMMAND_SIZE];
695 struct scsi_sense_hdr sshdr;
696 unsigned int the_result;
697 int retries, rc, n;
699 static const char *loadmech[] =
701 "caddy",
702 "tray",
703 "pop-up",
705 "changer",
706 "cartridge changer",
712 /* allocate transfer buffer */
713 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
714 if (!buffer) {
715 printk(KERN_ERR "sr: out of memory.\n");
716 return;
719 /* issue TEST_UNIT_READY until the initial startup UNIT_ATTENTION
720 * conditions are gone, or a timeout happens
722 retries = 0;
723 do {
724 memset((void *)cmd, 0, MAX_COMMAND_SIZE);
725 cmd[0] = TEST_UNIT_READY;
727 the_result = scsi_execute_req (cd->device, cmd, DMA_NONE, NULL,
728 0, &sshdr, SR_TIMEOUT,
729 MAX_RETRIES);
731 retries++;
732 } while (retries < 5 &&
733 (!scsi_status_is_good(the_result) ||
734 (scsi_sense_valid(&sshdr) &&
735 sshdr.sense_key == UNIT_ATTENTION)));
737 /* ask for mode page 0x2a */
738 rc = scsi_mode_sense(cd->device, 0, 0x2a, buffer, 128,
739 SR_TIMEOUT, 3, &data, NULL);
741 if (!scsi_status_is_good(rc)) {
742 /* failed, drive doesn't have capabilities mode page */
743 cd->cdi.speed = 1;
744 cd->cdi.mask |= (CDC_CD_R | CDC_CD_RW | CDC_DVD_R |
745 CDC_DVD | CDC_DVD_RAM |
746 CDC_SELECT_DISC | CDC_SELECT_SPEED |
747 CDC_MRW | CDC_MRW_W | CDC_RAM);
748 kfree(buffer);
749 printk("%s: scsi-1 drive\n", cd->cdi.name);
750 return;
753 n = data.header_length + data.block_descriptor_length;
754 cd->cdi.speed = ((buffer[n + 8] << 8) + buffer[n + 9]) / 176;
755 cd->readcd_known = 1;
756 cd->readcd_cdda = buffer[n + 5] & 0x01;
757 /* print some capability bits */
758 printk("%s: scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n", cd->cdi.name,
759 ((buffer[n + 14] << 8) + buffer[n + 15]) / 176,
760 cd->cdi.speed,
761 buffer[n + 3] & 0x01 ? "writer " : "", /* CD Writer */
762 buffer[n + 3] & 0x20 ? "dvd-ram " : "",
763 buffer[n + 2] & 0x02 ? "cd/rw " : "", /* can read rewriteable */
764 buffer[n + 4] & 0x20 ? "xa/form2 " : "", /* can read xa/from2 */
765 buffer[n + 5] & 0x01 ? "cdda " : "", /* can read audio data */
766 loadmech[buffer[n + 6] >> 5]);
767 if ((buffer[n + 6] >> 5) == 0)
768 /* caddy drives can't close tray... */
769 cd->cdi.mask |= CDC_CLOSE_TRAY;
770 if ((buffer[n + 2] & 0x8) == 0)
771 /* not a DVD drive */
772 cd->cdi.mask |= CDC_DVD;
773 if ((buffer[n + 3] & 0x20) == 0)
774 /* can't write DVD-RAM media */
775 cd->cdi.mask |= CDC_DVD_RAM;
776 if ((buffer[n + 3] & 0x10) == 0)
777 /* can't write DVD-R media */
778 cd->cdi.mask |= CDC_DVD_R;
779 if ((buffer[n + 3] & 0x2) == 0)
780 /* can't write CD-RW media */
781 cd->cdi.mask |= CDC_CD_RW;
782 if ((buffer[n + 3] & 0x1) == 0)
783 /* can't write CD-R media */
784 cd->cdi.mask |= CDC_CD_R;
785 if ((buffer[n + 6] & 0x8) == 0)
786 /* can't eject */
787 cd->cdi.mask |= CDC_OPEN_TRAY;
789 if ((buffer[n + 6] >> 5) == mechtype_individual_changer ||
790 (buffer[n + 6] >> 5) == mechtype_cartridge_changer)
791 cd->cdi.capacity =
792 cdrom_number_of_slots(&cd->cdi);
793 if (cd->cdi.capacity <= 1)
794 /* not a changer */
795 cd->cdi.mask |= CDC_SELECT_DISC;
796 /*else I don't think it can close its tray
797 cd->cdi.mask |= CDC_CLOSE_TRAY; */
800 * if DVD-RAM, MRW-W or CD-RW, we are randomly writable
802 if ((cd->cdi.mask & (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) !=
803 (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) {
804 cd->device->writeable = 1;
807 kfree(buffer);
811 * sr_packet() is the entry point for the generic commands generated
812 * by the Uniform CD-ROM layer.
814 static int sr_packet(struct cdrom_device_info *cdi,
815 struct packet_command *cgc)
817 if (cgc->timeout <= 0)
818 cgc->timeout = IOCTL_TIMEOUT;
820 sr_do_ioctl(cdi->handle, cgc);
822 return cgc->stat;
826 * sr_kref_release - Called to free the scsi_cd structure
827 * @kref: pointer to embedded kref
829 * sr_ref_mutex must be held entering this routine. Because it is
830 * called on last put, you should always use the scsi_cd_get()
831 * scsi_cd_put() helpers which manipulate the semaphore directly
832 * and never do a direct kref_put().
834 static void sr_kref_release(struct kref *kref)
836 struct scsi_cd *cd = container_of(kref, struct scsi_cd, kref);
837 struct gendisk *disk = cd->disk;
839 spin_lock(&sr_index_lock);
840 clear_bit(disk->first_minor, sr_index_bits);
841 spin_unlock(&sr_index_lock);
843 unregister_cdrom(&cd->cdi);
845 disk->private_data = NULL;
847 put_disk(disk);
849 kfree(cd);
852 static int sr_remove(struct device *dev)
854 struct scsi_cd *cd = dev_get_drvdata(dev);
856 del_gendisk(cd->disk);
858 mutex_lock(&sr_ref_mutex);
859 kref_put(&cd->kref, sr_kref_release);
860 mutex_unlock(&sr_ref_mutex);
862 return 0;
865 static int __init init_sr(void)
867 int rc;
869 rc = register_blkdev(SCSI_CDROM_MAJOR, "sr");
870 if (rc)
871 return rc;
872 return scsi_register_driver(&sr_template.gendrv);
875 static void __exit exit_sr(void)
877 scsi_unregister_driver(&sr_template.gendrv);
878 unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
881 module_init(init_sr);
882 module_exit(exit_sr);
883 MODULE_LICENSE("GPL");