[SCSI] SCSI core kmalloc2kzalloc
[linux-2.6/btrfs-unstable.git] / drivers / scsi / sr.c
blobd8d12a1718bf91cc9df203440b594dadb5c2ea76
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 = kzalloc(sizeof(*cd), GFP_KERNEL);
529 if (!cd)
530 goto fail;
532 kref_init(&cd->kref);
534 disk = alloc_disk(1);
535 if (!disk)
536 goto fail_free;
538 spin_lock(&sr_index_lock);
539 minor = find_first_zero_bit(sr_index_bits, SR_DISKS);
540 if (minor == SR_DISKS) {
541 spin_unlock(&sr_index_lock);
542 error = -EBUSY;
543 goto fail_put;
545 __set_bit(minor, sr_index_bits);
546 spin_unlock(&sr_index_lock);
548 disk->major = SCSI_CDROM_MAJOR;
549 disk->first_minor = minor;
550 sprintf(disk->disk_name, "sr%d", minor);
551 disk->fops = &sr_bdops;
552 disk->flags = GENHD_FL_CD;
554 cd->device = sdev;
555 cd->disk = disk;
556 cd->driver = &sr_template;
557 cd->disk = disk;
558 cd->capacity = 0x1fffff;
559 cd->device->changed = 1; /* force recheck CD type */
560 cd->use = 1;
561 cd->readcd_known = 0;
562 cd->readcd_cdda = 0;
564 cd->cdi.ops = &sr_dops;
565 cd->cdi.handle = cd;
566 cd->cdi.mask = 0;
567 cd->cdi.capacity = 1;
568 sprintf(cd->cdi.name, "sr%d", minor);
570 sdev->sector_size = 2048; /* A guess, just in case */
572 /* FIXME: need to handle a get_capabilities failure properly ?? */
573 get_capabilities(cd);
574 sr_vendor_init(cd);
576 snprintf(disk->devfs_name, sizeof(disk->devfs_name),
577 "%s/cd", sdev->devfs_name);
578 disk->driverfs_dev = &sdev->sdev_gendev;
579 set_capacity(disk, cd->capacity);
580 disk->private_data = &cd->driver;
581 disk->queue = sdev->request_queue;
582 cd->cdi.disk = disk;
584 if (register_cdrom(&cd->cdi))
585 goto fail_put;
587 dev_set_drvdata(dev, cd);
588 disk->flags |= GENHD_FL_REMOVABLE;
589 add_disk(disk);
591 sdev_printk(KERN_DEBUG, sdev,
592 "Attached scsi CD-ROM %s\n", cd->cdi.name);
593 return 0;
595 fail_put:
596 put_disk(disk);
597 fail_free:
598 kfree(cd);
599 fail:
600 return error;
604 static void get_sectorsize(struct scsi_cd *cd)
606 unsigned char cmd[10];
607 unsigned char *buffer;
608 int the_result, retries = 3;
609 int sector_size;
610 request_queue_t *queue;
612 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
613 if (!buffer)
614 goto Enomem;
616 do {
617 cmd[0] = READ_CAPACITY;
618 memset((void *) &cmd[1], 0, 9);
619 memset(buffer, 0, 8);
621 /* Do the command and wait.. */
622 the_result = scsi_execute_req(cd->device, cmd, DMA_FROM_DEVICE,
623 buffer, 8, NULL, SR_TIMEOUT,
624 MAX_RETRIES);
626 retries--;
628 } while (the_result && retries);
631 if (the_result) {
632 cd->capacity = 0x1fffff;
633 sector_size = 2048; /* A guess, just in case */
634 } else {
635 #if 0
636 if (cdrom_get_last_written(&cd->cdi,
637 &cd->capacity))
638 #endif
639 cd->capacity = 1 + ((buffer[0] << 24) |
640 (buffer[1] << 16) |
641 (buffer[2] << 8) |
642 buffer[3]);
643 sector_size = (buffer[4] << 24) |
644 (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
645 switch (sector_size) {
647 * HP 4020i CD-Recorder reports 2340 byte sectors
648 * Philips CD-Writers report 2352 byte sectors
650 * Use 2k sectors for them..
652 case 0:
653 case 2340:
654 case 2352:
655 sector_size = 2048;
656 /* fall through */
657 case 2048:
658 cd->capacity *= 4;
659 /* fall through */
660 case 512:
661 break;
662 default:
663 printk("%s: unsupported sector size %d.\n",
664 cd->cdi.name, sector_size);
665 cd->capacity = 0;
668 cd->device->sector_size = sector_size;
671 * Add this so that we have the ability to correctly gauge
672 * what the device is capable of.
674 set_capacity(cd->disk, cd->capacity);
677 queue = cd->device->request_queue;
678 blk_queue_hardsect_size(queue, sector_size);
679 out:
680 kfree(buffer);
681 return;
683 Enomem:
684 cd->capacity = 0x1fffff;
685 cd->device->sector_size = 2048; /* A guess, just in case */
686 goto out;
689 static void get_capabilities(struct scsi_cd *cd)
691 unsigned char *buffer;
692 struct scsi_mode_data data;
693 unsigned char cmd[MAX_COMMAND_SIZE];
694 struct scsi_sense_hdr sshdr;
695 unsigned int the_result;
696 int retries, rc, n;
698 static const char *loadmech[] =
700 "caddy",
701 "tray",
702 "pop-up",
704 "changer",
705 "cartridge changer",
711 /* allocate transfer buffer */
712 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
713 if (!buffer) {
714 printk(KERN_ERR "sr: out of memory.\n");
715 return;
718 /* issue TEST_UNIT_READY until the initial startup UNIT_ATTENTION
719 * conditions are gone, or a timeout happens
721 retries = 0;
722 do {
723 memset((void *)cmd, 0, MAX_COMMAND_SIZE);
724 cmd[0] = TEST_UNIT_READY;
726 the_result = scsi_execute_req (cd->device, cmd, DMA_NONE, NULL,
727 0, &sshdr, SR_TIMEOUT,
728 MAX_RETRIES);
730 retries++;
731 } while (retries < 5 &&
732 (!scsi_status_is_good(the_result) ||
733 (scsi_sense_valid(&sshdr) &&
734 sshdr.sense_key == UNIT_ATTENTION)));
736 /* ask for mode page 0x2a */
737 rc = scsi_mode_sense(cd->device, 0, 0x2a, buffer, 128,
738 SR_TIMEOUT, 3, &data, NULL);
740 if (!scsi_status_is_good(rc)) {
741 /* failed, drive doesn't have capabilities mode page */
742 cd->cdi.speed = 1;
743 cd->cdi.mask |= (CDC_CD_R | CDC_CD_RW | CDC_DVD_R |
744 CDC_DVD | CDC_DVD_RAM |
745 CDC_SELECT_DISC | CDC_SELECT_SPEED |
746 CDC_MRW | CDC_MRW_W | CDC_RAM);
747 kfree(buffer);
748 printk("%s: scsi-1 drive\n", cd->cdi.name);
749 return;
752 n = data.header_length + data.block_descriptor_length;
753 cd->cdi.speed = ((buffer[n + 8] << 8) + buffer[n + 9]) / 176;
754 cd->readcd_known = 1;
755 cd->readcd_cdda = buffer[n + 5] & 0x01;
756 /* print some capability bits */
757 printk("%s: scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n", cd->cdi.name,
758 ((buffer[n + 14] << 8) + buffer[n + 15]) / 176,
759 cd->cdi.speed,
760 buffer[n + 3] & 0x01 ? "writer " : "", /* CD Writer */
761 buffer[n + 3] & 0x20 ? "dvd-ram " : "",
762 buffer[n + 2] & 0x02 ? "cd/rw " : "", /* can read rewriteable */
763 buffer[n + 4] & 0x20 ? "xa/form2 " : "", /* can read xa/from2 */
764 buffer[n + 5] & 0x01 ? "cdda " : "", /* can read audio data */
765 loadmech[buffer[n + 6] >> 5]);
766 if ((buffer[n + 6] >> 5) == 0)
767 /* caddy drives can't close tray... */
768 cd->cdi.mask |= CDC_CLOSE_TRAY;
769 if ((buffer[n + 2] & 0x8) == 0)
770 /* not a DVD drive */
771 cd->cdi.mask |= CDC_DVD;
772 if ((buffer[n + 3] & 0x20) == 0)
773 /* can't write DVD-RAM media */
774 cd->cdi.mask |= CDC_DVD_RAM;
775 if ((buffer[n + 3] & 0x10) == 0)
776 /* can't write DVD-R media */
777 cd->cdi.mask |= CDC_DVD_R;
778 if ((buffer[n + 3] & 0x2) == 0)
779 /* can't write CD-RW media */
780 cd->cdi.mask |= CDC_CD_RW;
781 if ((buffer[n + 3] & 0x1) == 0)
782 /* can't write CD-R media */
783 cd->cdi.mask |= CDC_CD_R;
784 if ((buffer[n + 6] & 0x8) == 0)
785 /* can't eject */
786 cd->cdi.mask |= CDC_OPEN_TRAY;
788 if ((buffer[n + 6] >> 5) == mechtype_individual_changer ||
789 (buffer[n + 6] >> 5) == mechtype_cartridge_changer)
790 cd->cdi.capacity =
791 cdrom_number_of_slots(&cd->cdi);
792 if (cd->cdi.capacity <= 1)
793 /* not a changer */
794 cd->cdi.mask |= CDC_SELECT_DISC;
795 /*else I don't think it can close its tray
796 cd->cdi.mask |= CDC_CLOSE_TRAY; */
799 * if DVD-RAM, MRW-W or CD-RW, we are randomly writable
801 if ((cd->cdi.mask & (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) !=
802 (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) {
803 cd->device->writeable = 1;
806 kfree(buffer);
810 * sr_packet() is the entry point for the generic commands generated
811 * by the Uniform CD-ROM layer.
813 static int sr_packet(struct cdrom_device_info *cdi,
814 struct packet_command *cgc)
816 if (cgc->timeout <= 0)
817 cgc->timeout = IOCTL_TIMEOUT;
819 sr_do_ioctl(cdi->handle, cgc);
821 return cgc->stat;
825 * sr_kref_release - Called to free the scsi_cd structure
826 * @kref: pointer to embedded kref
828 * sr_ref_mutex must be held entering this routine. Because it is
829 * called on last put, you should always use the scsi_cd_get()
830 * scsi_cd_put() helpers which manipulate the semaphore directly
831 * and never do a direct kref_put().
833 static void sr_kref_release(struct kref *kref)
835 struct scsi_cd *cd = container_of(kref, struct scsi_cd, kref);
836 struct gendisk *disk = cd->disk;
838 spin_lock(&sr_index_lock);
839 clear_bit(disk->first_minor, sr_index_bits);
840 spin_unlock(&sr_index_lock);
842 unregister_cdrom(&cd->cdi);
844 disk->private_data = NULL;
846 put_disk(disk);
848 kfree(cd);
851 static int sr_remove(struct device *dev)
853 struct scsi_cd *cd = dev_get_drvdata(dev);
855 del_gendisk(cd->disk);
857 mutex_lock(&sr_ref_mutex);
858 kref_put(&cd->kref, sr_kref_release);
859 mutex_unlock(&sr_ref_mutex);
861 return 0;
864 static int __init init_sr(void)
866 int rc;
868 rc = register_blkdev(SCSI_CDROM_MAJOR, "sr");
869 if (rc)
870 return rc;
871 return scsi_register_driver(&sr_template.gendrv);
874 static void __exit exit_sr(void)
876 scsi_unregister_driver(&sr_template.gendrv);
877 unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
880 module_init(init_sr);
881 module_exit(exit_sr);
882 MODULE_LICENSE("GPL");