RT-AC66 3.0.0.4.374.130 core
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / drivers / scsi / sr.c
blobc8679565175624c0a47f770b65ffb6a18e73cf36
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/mm.h>
39 #include <linux/bio.h>
40 #include <linux/string.h>
41 #include <linux/errno.h>
42 #include <linux/cdrom.h>
43 #include <linux/interrupt.h>
44 #include <linux/init.h>
45 #include <linux/blkdev.h>
46 #include <linux/mutex.h>
47 #include <asm/uaccess.h>
49 #include <scsi/scsi.h>
50 #include <scsi/scsi_dbg.h>
51 #include <scsi/scsi_device.h>
52 #include <scsi/scsi_driver.h>
53 #include <scsi/scsi_cmnd.h>
54 #include <scsi/scsi_eh.h>
55 #include <scsi/scsi_host.h>
56 #include <scsi/scsi_ioctl.h> /* For the door lock/unlock commands */
58 #include "scsi_logging.h"
59 #include "sr.h"
62 MODULE_DESCRIPTION("SCSI cdrom (sr) driver");
63 MODULE_LICENSE("GPL");
64 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_CDROM_MAJOR);
65 MODULE_ALIAS_SCSI_DEVICE(TYPE_ROM);
66 MODULE_ALIAS_SCSI_DEVICE(TYPE_WORM);
68 #define SR_DISKS 256
70 #define MAX_RETRIES 3
71 #define SR_TIMEOUT (30 * HZ)
72 #define SR_CAPABILITIES \
73 (CDC_CLOSE_TRAY|CDC_OPEN_TRAY|CDC_LOCK|CDC_SELECT_SPEED| \
74 CDC_SELECT_DISC|CDC_MULTI_SESSION|CDC_MCN|CDC_MEDIA_CHANGED| \
75 CDC_PLAY_AUDIO|CDC_RESET|CDC_DRIVE_STATUS| \
76 CDC_CD_R|CDC_CD_RW|CDC_DVD|CDC_DVD_R|CDC_DVD_RAM|CDC_GENERIC_PACKET| \
77 CDC_MRW|CDC_MRW_W|CDC_RAM)
79 static int sr_probe(struct device *);
80 static int sr_remove(struct device *);
81 static int sr_init_command(struct scsi_cmnd *);
83 static struct scsi_driver sr_template = {
84 .owner = THIS_MODULE,
85 .gendrv = {
86 .name = "sr",
87 .probe = sr_probe,
88 .remove = sr_remove,
90 .init_command = sr_init_command,
93 static unsigned long sr_index_bits[SR_DISKS / BITS_PER_LONG];
94 static DEFINE_SPINLOCK(sr_index_lock);
96 /* This semaphore is used to mediate the 0->1 reference get in the
97 * face of object destruction (i.e. we can't allow a get on an
98 * object after last put) */
99 static DEFINE_MUTEX(sr_ref_mutex);
101 static int sr_open(struct cdrom_device_info *, int);
102 static void sr_release(struct cdrom_device_info *);
104 static void get_sectorsize(struct scsi_cd *);
105 static void get_capabilities(struct scsi_cd *);
107 static int sr_media_change(struct cdrom_device_info *, int);
108 static int sr_packet(struct cdrom_device_info *, struct packet_command *);
110 static struct cdrom_device_ops sr_dops = {
111 .open = sr_open,
112 .release = sr_release,
113 .drive_status = sr_drive_status,
114 .media_changed = sr_media_change,
115 .tray_move = sr_tray_move,
116 .lock_door = sr_lock_door,
117 .select_speed = sr_select_speed,
118 .get_last_session = sr_get_last_session,
119 .get_mcn = sr_get_mcn,
120 .reset = sr_reset,
121 .audio_ioctl = sr_audio_ioctl,
122 .capability = SR_CAPABILITIES,
123 .generic_packet = sr_packet,
126 static void sr_kref_release(struct kref *kref);
128 static inline struct scsi_cd *scsi_cd(struct gendisk *disk)
130 return container_of(disk->private_data, struct scsi_cd, driver);
134 * The get and put routines for the struct scsi_cd. Note this entity
135 * has a scsi_device pointer and owns a reference to this.
137 static inline struct scsi_cd *scsi_cd_get(struct gendisk *disk)
139 struct scsi_cd *cd = NULL;
141 mutex_lock(&sr_ref_mutex);
142 if (disk->private_data == NULL)
143 goto out;
144 cd = scsi_cd(disk);
145 kref_get(&cd->kref);
146 if (scsi_device_get(cd->device))
147 goto out_put;
148 goto out;
150 out_put:
151 kref_put(&cd->kref, sr_kref_release);
152 cd = NULL;
153 out:
154 mutex_unlock(&sr_ref_mutex);
155 return cd;
158 static void scsi_cd_put(struct scsi_cd *cd)
160 struct scsi_device *sdev = cd->device;
162 mutex_lock(&sr_ref_mutex);
163 kref_put(&cd->kref, sr_kref_release);
164 scsi_device_put(sdev);
165 mutex_unlock(&sr_ref_mutex);
169 * This function checks to see if the media has been changed in the
170 * CDROM drive. It is possible that we have already sensed a change,
171 * or the drive may have sensed one and not yet reported it. We must
172 * be ready for either case. This function always reports the current
173 * value of the changed bit. If flag is 0, then the changed bit is reset.
174 * This function could be done as an ioctl, but we would need to have
175 * an inode for that to work, and we do not always have one.
178 int sr_media_change(struct cdrom_device_info *cdi, int slot)
180 struct scsi_cd *cd = cdi->handle;
181 int retval;
183 if (CDSL_CURRENT != slot) {
184 /* no changer support */
185 return -EINVAL;
188 retval = scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES);
189 if (retval) {
190 /* Unable to test, unit probably not ready. This usually
191 * means there is no disc in the drive. Mark as changed,
192 * and we will figure it out later once the drive is
193 * available again. */
194 cd->device->changed = 1;
195 return 1; /* This will force a flush, if called from
196 * check_disk_change */
199 retval = cd->device->changed;
200 cd->device->changed = 0;
201 /* If the disk changed, the capacity will now be different,
202 * so we force a re-read of this information */
203 if (retval) {
204 /* check multisession offset etc */
205 sr_cd_check(cdi);
207 get_sectorsize(cd);
209 return retval;
213 * rw_intr is the interrupt routine for the device driver.
215 * It will be notified on the end of a SCSI read / write, and will take on
216 * of several actions based on success or failure.
218 static void rw_intr(struct scsi_cmnd * SCpnt)
220 int result = SCpnt->result;
221 int this_count = SCpnt->request_bufflen;
222 int good_bytes = (result == 0 ? this_count : 0);
223 int block_sectors = 0;
224 long error_sector;
225 struct scsi_cd *cd = scsi_cd(SCpnt->request->rq_disk);
227 #ifdef DEBUG
228 printk("sr.c done: %x\n", result);
229 #endif
232 * Handle MEDIUM ERRORs or VOLUME OVERFLOWs that indicate partial
233 * success. Since this is a relatively rare error condition, no
234 * care is taken to avoid unnecessary additional work such as
235 * memcpy's that could be avoided.
237 if (driver_byte(result) != 0 && /* An error occurred */
238 (SCpnt->sense_buffer[0] & 0x7f) == 0x70) { /* Sense current */
239 switch (SCpnt->sense_buffer[2]) {
240 case MEDIUM_ERROR:
241 case VOLUME_OVERFLOW:
242 case ILLEGAL_REQUEST:
243 if (!(SCpnt->sense_buffer[0] & 0x90))
244 break;
245 error_sector = (SCpnt->sense_buffer[3] << 24) |
246 (SCpnt->sense_buffer[4] << 16) |
247 (SCpnt->sense_buffer[5] << 8) |
248 SCpnt->sense_buffer[6];
249 if (SCpnt->request->bio != NULL)
250 block_sectors =
251 bio_sectors(SCpnt->request->bio);
252 if (block_sectors < 4)
253 block_sectors = 4;
254 if (cd->device->sector_size == 2048)
255 error_sector <<= 2;
256 error_sector &= ~(block_sectors - 1);
257 good_bytes = (error_sector - SCpnt->request->sector) << 9;
258 if (good_bytes < 0 || good_bytes >= this_count)
259 good_bytes = 0;
261 * The SCSI specification allows for the value
262 * returned by READ CAPACITY to be up to 75 2K
263 * sectors past the last readable block.
264 * Therefore, if we hit a medium error within the
265 * last 75 2K sectors, we decrease the saved size
266 * value.
268 if (error_sector < get_capacity(cd->disk) &&
269 cd->capacity - error_sector < 4 * 75)
270 set_capacity(cd->disk, error_sector);
271 break;
273 case RECOVERED_ERROR:
274 good_bytes = this_count;
275 break;
277 default:
278 break;
283 * This calls the generic completion function, now that we know
284 * how many actual sectors finished, and how many sectors we need
285 * to say have failed.
287 scsi_io_completion(SCpnt, good_bytes);
290 static int sr_init_command(struct scsi_cmnd * SCpnt)
292 int block=0, this_count, s_size, timeout = SR_TIMEOUT;
293 struct scsi_cd *cd = scsi_cd(SCpnt->request->rq_disk);
295 SCSI_LOG_HLQUEUE(1, printk("Doing sr request, dev = %s, block = %d\n",
296 cd->disk->disk_name, block));
298 if (!cd->device || !scsi_device_online(cd->device)) {
299 SCSI_LOG_HLQUEUE(2, printk("Finishing %ld sectors\n",
300 SCpnt->request->nr_sectors));
301 SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt));
302 return 0;
305 if (cd->device->changed) {
307 * quietly refuse to do anything to a changed disc until the
308 * changed bit has been reset
310 return 0;
314 * we do lazy blocksize switching (when reading XA sectors,
315 * see CDROMREADMODE2 ioctl)
317 s_size = cd->device->sector_size;
318 if (s_size > 2048) {
319 if (!in_interrupt())
320 sr_set_blocklength(cd, 2048);
321 else
322 printk("sr: can't switch blocksize: in interrupt\n");
325 if (s_size != 512 && s_size != 1024 && s_size != 2048) {
326 scmd_printk(KERN_ERR, SCpnt, "bad sector size %d\n", s_size);
327 return 0;
330 if (rq_data_dir(SCpnt->request) == WRITE) {
331 if (!cd->device->writeable)
332 return 0;
333 SCpnt->cmnd[0] = WRITE_10;
334 SCpnt->sc_data_direction = DMA_TO_DEVICE;
335 cd->cdi.media_written = 1;
336 } else if (rq_data_dir(SCpnt->request) == READ) {
337 SCpnt->cmnd[0] = READ_10;
338 SCpnt->sc_data_direction = DMA_FROM_DEVICE;
339 } else {
340 blk_dump_rq_flags(SCpnt->request, "Unknown sr command");
341 return 0;
345 struct scatterlist *sg = SCpnt->request_buffer;
346 int i, size = 0;
347 for (i = 0; i < SCpnt->use_sg; i++)
348 size += sg[i].length;
350 if (size != SCpnt->request_bufflen && SCpnt->use_sg) {
351 scmd_printk(KERN_ERR, SCpnt,
352 "mismatch count %d, bytes %d\n",
353 size, SCpnt->request_bufflen);
354 if (SCpnt->request_bufflen > size)
355 SCpnt->request_bufflen = size;
360 * request doesn't start on hw block boundary, add scatter pads
362 if (((unsigned int)SCpnt->request->sector % (s_size >> 9)) ||
363 (SCpnt->request_bufflen % s_size)) {
364 scmd_printk(KERN_NOTICE, SCpnt, "unaligned transfer\n");
365 return 0;
368 this_count = (SCpnt->request_bufflen >> 9) / (s_size >> 9);
371 SCSI_LOG_HLQUEUE(2, printk("%s : %s %d/%ld 512 byte blocks.\n",
372 cd->cdi.name,
373 (rq_data_dir(SCpnt->request) == WRITE) ?
374 "writing" : "reading",
375 this_count, SCpnt->request->nr_sectors));
377 SCpnt->cmnd[1] = 0;
378 block = (unsigned int)SCpnt->request->sector / (s_size >> 9);
380 if (this_count > 0xffff) {
381 this_count = 0xffff;
382 SCpnt->request_bufflen = this_count * s_size;
385 SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
386 SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
387 SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
388 SCpnt->cmnd[5] = (unsigned char) block & 0xff;
389 SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
390 SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
391 SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
394 * We shouldn't disconnect in the middle of a sector, so with a dumb
395 * host adapter, it's safe to assume that we can at least transfer
396 * this many bytes between each connect / disconnect.
398 SCpnt->transfersize = cd->device->sector_size;
399 SCpnt->underflow = this_count << 9;
400 SCpnt->allowed = MAX_RETRIES;
401 SCpnt->timeout_per_command = timeout;
404 * This is the completion routine we use. This is matched in terms
405 * of capability to this function.
407 SCpnt->done = rw_intr;
410 * This indicates that the command is ready from our end to be
411 * queued.
413 return 1;
416 static int sr_block_open(struct inode *inode, struct file *file)
418 struct gendisk *disk = inode->i_bdev->bd_disk;
419 struct scsi_cd *cd;
420 int ret = 0;
422 if(!(cd = scsi_cd_get(disk)))
423 return -ENXIO;
425 if((ret = cdrom_open(&cd->cdi, inode, file)) != 0)
426 scsi_cd_put(cd);
428 return ret;
431 static int sr_block_release(struct inode *inode, struct file *file)
433 int ret;
434 struct scsi_cd *cd = scsi_cd(inode->i_bdev->bd_disk);
435 ret = cdrom_release(&cd->cdi, file);
436 if(ret)
437 return ret;
439 scsi_cd_put(cd);
441 return 0;
444 static int sr_block_ioctl(struct inode *inode, struct file *file, unsigned cmd,
445 unsigned long arg)
447 struct scsi_cd *cd = scsi_cd(inode->i_bdev->bd_disk);
448 struct scsi_device *sdev = cd->device;
449 void __user *argp = (void __user *)arg;
450 int ret;
453 * Send SCSI addressing ioctls directly to mid level, send other
454 * ioctls to cdrom/block level.
456 switch (cmd) {
457 case SCSI_IOCTL_GET_IDLUN:
458 case SCSI_IOCTL_GET_BUS_NUMBER:
459 return scsi_ioctl(sdev, cmd, argp);
462 ret = cdrom_ioctl(file, &cd->cdi, inode, cmd, arg);
463 if (ret != -ENOSYS)
464 return ret;
467 * ENODEV means that we didn't recognise the ioctl, or that we
468 * cannot execute it in the current device state. In either
469 * case fall through to scsi_ioctl, which will return ENDOEV again
470 * if it doesn't recognise the ioctl
472 ret = scsi_nonblockable_ioctl(sdev, cmd, argp, NULL);
473 if (ret != -ENODEV)
474 return ret;
475 return scsi_ioctl(sdev, cmd, argp);
478 static int sr_block_media_changed(struct gendisk *disk)
480 struct scsi_cd *cd = scsi_cd(disk);
481 return cdrom_media_changed(&cd->cdi);
484 static struct block_device_operations sr_bdops =
486 .owner = THIS_MODULE,
487 .open = sr_block_open,
488 .release = sr_block_release,
489 .ioctl = sr_block_ioctl,
490 .media_changed = sr_block_media_changed,
492 * No compat_ioctl for now because sr_block_ioctl never
493 * seems to pass arbitary ioctls down to host drivers.
497 static int sr_open(struct cdrom_device_info *cdi, int purpose)
499 struct scsi_cd *cd = cdi->handle;
500 struct scsi_device *sdev = cd->device;
501 int retval;
504 * If the device is in error recovery, wait until it is done.
505 * If the device is offline, then disallow any access to it.
507 retval = -ENXIO;
508 if (!scsi_block_when_processing_errors(sdev))
509 goto error_out;
511 return 0;
513 error_out:
514 return retval;
517 static void sr_release(struct cdrom_device_info *cdi)
519 struct scsi_cd *cd = cdi->handle;
521 if (cd->device->sector_size > 2048)
522 sr_set_blocklength(cd, 2048);
526 static int sr_probe(struct device *dev)
528 struct scsi_device *sdev = to_scsi_device(dev);
529 struct gendisk *disk;
530 struct scsi_cd *cd;
531 int minor, error;
533 error = -ENODEV;
534 if (sdev->type != TYPE_ROM && sdev->type != TYPE_WORM)
535 goto fail;
537 error = -ENOMEM;
538 cd = kzalloc(sizeof(*cd), GFP_KERNEL);
539 if (!cd)
540 goto fail;
542 kref_init(&cd->kref);
544 disk = alloc_disk(1);
545 if (!disk)
546 goto fail_free;
548 spin_lock(&sr_index_lock);
549 minor = find_first_zero_bit(sr_index_bits, SR_DISKS);
550 if (minor == SR_DISKS) {
551 spin_unlock(&sr_index_lock);
552 error = -EBUSY;
553 goto fail_put;
555 __set_bit(minor, sr_index_bits);
556 spin_unlock(&sr_index_lock);
558 disk->major = SCSI_CDROM_MAJOR;
559 disk->first_minor = minor;
560 sprintf(disk->disk_name, "sr%d", minor);
561 disk->fops = &sr_bdops;
562 disk->flags = GENHD_FL_CD;
564 cd->device = sdev;
565 cd->disk = disk;
566 cd->driver = &sr_template;
567 cd->disk = disk;
568 cd->capacity = 0x1fffff;
569 cd->device->changed = 1; /* force recheck CD type */
570 cd->use = 1;
571 cd->readcd_known = 0;
572 cd->readcd_cdda = 0;
574 cd->cdi.ops = &sr_dops;
575 cd->cdi.handle = cd;
576 cd->cdi.mask = 0;
577 cd->cdi.capacity = 1;
578 sprintf(cd->cdi.name, "sr%d", minor);
580 sdev->sector_size = 2048; /* A guess, just in case */
582 /* FIXME: need to handle a get_capabilities failure properly ?? */
583 get_capabilities(cd);
584 sr_vendor_init(cd);
586 disk->driverfs_dev = &sdev->sdev_gendev;
587 set_capacity(disk, cd->capacity);
588 disk->private_data = &cd->driver;
589 disk->queue = sdev->request_queue;
590 cd->cdi.disk = disk;
592 if (register_cdrom(&cd->cdi))
593 goto fail_put;
595 dev_set_drvdata(dev, cd);
596 disk->flags |= GENHD_FL_REMOVABLE;
597 add_disk(disk);
599 sdev_printk(KERN_DEBUG, sdev,
600 "Attached scsi CD-ROM %s\n", cd->cdi.name);
601 return 0;
603 fail_put:
604 put_disk(disk);
605 fail_free:
606 kfree(cd);
607 fail:
608 return error;
612 static void get_sectorsize(struct scsi_cd *cd)
614 unsigned char cmd[10];
615 unsigned char *buffer;
616 int the_result, retries = 3;
617 int sector_size;
618 request_queue_t *queue;
620 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
621 if (!buffer)
622 goto Enomem;
624 do {
625 cmd[0] = READ_CAPACITY;
626 memset((void *) &cmd[1], 0, 9);
627 memset(buffer, 0, 8);
629 /* Do the command and wait.. */
630 the_result = scsi_execute_req(cd->device, cmd, DMA_FROM_DEVICE,
631 buffer, 8, NULL, SR_TIMEOUT,
632 MAX_RETRIES);
634 retries--;
636 } while (the_result && retries);
639 if (the_result) {
640 cd->capacity = 0x1fffff;
641 sector_size = 2048; /* A guess, just in case */
642 } else {
643 #if 0
644 if (cdrom_get_last_written(&cd->cdi,
645 &cd->capacity))
646 #endif
647 cd->capacity = 1 + ((buffer[0] << 24) |
648 (buffer[1] << 16) |
649 (buffer[2] << 8) |
650 buffer[3]);
651 sector_size = (buffer[4] << 24) |
652 (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
653 switch (sector_size) {
655 * HP 4020i CD-Recorder reports 2340 byte sectors
656 * Philips CD-Writers report 2352 byte sectors
658 * Use 2k sectors for them..
660 case 0:
661 case 2340:
662 case 2352:
663 sector_size = 2048;
664 /* fall through */
665 case 2048:
666 cd->capacity *= 4;
667 /* fall through */
668 case 512:
669 break;
670 default:
671 printk("%s: unsupported sector size %d.\n",
672 cd->cdi.name, sector_size);
673 cd->capacity = 0;
676 cd->device->sector_size = sector_size;
679 * Add this so that we have the ability to correctly gauge
680 * what the device is capable of.
682 set_capacity(cd->disk, cd->capacity);
685 queue = cd->device->request_queue;
686 blk_queue_hardsect_size(queue, sector_size);
687 out:
688 kfree(buffer);
689 return;
691 Enomem:
692 cd->capacity = 0x1fffff;
693 cd->device->sector_size = 2048; /* A guess, just in case */
694 goto out;
697 static void get_capabilities(struct scsi_cd *cd)
699 unsigned char *buffer;
700 struct scsi_mode_data data;
701 unsigned char cmd[MAX_COMMAND_SIZE];
702 struct scsi_sense_hdr sshdr;
703 unsigned int the_result;
704 int retries, rc, n;
706 static const char *loadmech[] =
708 "caddy",
709 "tray",
710 "pop-up",
712 "changer",
713 "cartridge changer",
719 /* allocate transfer buffer */
720 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
721 if (!buffer) {
722 printk(KERN_ERR "sr: out of memory.\n");
723 return;
726 /* issue TEST_UNIT_READY until the initial startup UNIT_ATTENTION
727 * conditions are gone, or a timeout happens
729 retries = 0;
730 do {
731 memset((void *)cmd, 0, MAX_COMMAND_SIZE);
732 cmd[0] = TEST_UNIT_READY;
734 the_result = scsi_execute_req (cd->device, cmd, DMA_NONE, NULL,
735 0, &sshdr, SR_TIMEOUT,
736 MAX_RETRIES);
738 retries++;
739 } while (retries < 5 &&
740 (!scsi_status_is_good(the_result) ||
741 (scsi_sense_valid(&sshdr) &&
742 sshdr.sense_key == UNIT_ATTENTION)));
744 /* ask for mode page 0x2a */
745 rc = scsi_mode_sense(cd->device, 0, 0x2a, buffer, 128,
746 SR_TIMEOUT, 3, &data, NULL);
748 if (!scsi_status_is_good(rc)) {
749 /* failed, drive doesn't have capabilities mode page */
750 cd->cdi.speed = 1;
751 cd->cdi.mask |= (CDC_CD_R | CDC_CD_RW | CDC_DVD_R |
752 CDC_DVD | CDC_DVD_RAM |
753 CDC_SELECT_DISC | CDC_SELECT_SPEED |
754 CDC_MRW | CDC_MRW_W | CDC_RAM);
755 kfree(buffer);
756 printk("%s: scsi-1 drive\n", cd->cdi.name);
757 return;
760 n = data.header_length + data.block_descriptor_length;
761 cd->cdi.speed = ((buffer[n + 8] << 8) + buffer[n + 9]) / 176;
762 cd->readcd_known = 1;
763 cd->readcd_cdda = buffer[n + 5] & 0x01;
764 /* print some capability bits */
765 printk("%s: scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n", cd->cdi.name,
766 ((buffer[n + 14] << 8) + buffer[n + 15]) / 176,
767 cd->cdi.speed,
768 buffer[n + 3] & 0x01 ? "writer " : "", /* CD Writer */
769 buffer[n + 3] & 0x20 ? "dvd-ram " : "",
770 buffer[n + 2] & 0x02 ? "cd/rw " : "", /* can read rewriteable */
771 buffer[n + 4] & 0x20 ? "xa/form2 " : "", /* can read xa/from2 */
772 buffer[n + 5] & 0x01 ? "cdda " : "", /* can read audio data */
773 loadmech[buffer[n + 6] >> 5]);
774 if ((buffer[n + 6] >> 5) == 0)
775 /* caddy drives can't close tray... */
776 cd->cdi.mask |= CDC_CLOSE_TRAY;
777 if ((buffer[n + 2] & 0x8) == 0)
778 /* not a DVD drive */
779 cd->cdi.mask |= CDC_DVD;
780 if ((buffer[n + 3] & 0x20) == 0)
781 /* can't write DVD-RAM media */
782 cd->cdi.mask |= CDC_DVD_RAM;
783 if ((buffer[n + 3] & 0x10) == 0)
784 /* can't write DVD-R media */
785 cd->cdi.mask |= CDC_DVD_R;
786 if ((buffer[n + 3] & 0x2) == 0)
787 /* can't write CD-RW media */
788 cd->cdi.mask |= CDC_CD_RW;
789 if ((buffer[n + 3] & 0x1) == 0)
790 /* can't write CD-R media */
791 cd->cdi.mask |= CDC_CD_R;
792 if ((buffer[n + 6] & 0x8) == 0)
793 /* can't eject */
794 cd->cdi.mask |= CDC_OPEN_TRAY;
796 if ((buffer[n + 6] >> 5) == mechtype_individual_changer ||
797 (buffer[n + 6] >> 5) == mechtype_cartridge_changer)
798 cd->cdi.capacity =
799 cdrom_number_of_slots(&cd->cdi);
800 if (cd->cdi.capacity <= 1)
801 /* not a changer */
802 cd->cdi.mask |= CDC_SELECT_DISC;
803 /*else I don't think it can close its tray
804 cd->cdi.mask |= CDC_CLOSE_TRAY; */
807 * if DVD-RAM, MRW-W or CD-RW, we are randomly writable
809 if ((cd->cdi.mask & (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) !=
810 (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) {
811 cd->device->writeable = 1;
814 kfree(buffer);
818 * sr_packet() is the entry point for the generic commands generated
819 * by the Uniform CD-ROM layer.
821 static int sr_packet(struct cdrom_device_info *cdi,
822 struct packet_command *cgc)
824 if (cgc->timeout <= 0)
825 cgc->timeout = IOCTL_TIMEOUT;
827 sr_do_ioctl(cdi->handle, cgc);
829 return cgc->stat;
833 * sr_kref_release - Called to free the scsi_cd structure
834 * @kref: pointer to embedded kref
836 * sr_ref_mutex must be held entering this routine. Because it is
837 * called on last put, you should always use the scsi_cd_get()
838 * scsi_cd_put() helpers which manipulate the semaphore directly
839 * and never do a direct kref_put().
841 static void sr_kref_release(struct kref *kref)
843 struct scsi_cd *cd = container_of(kref, struct scsi_cd, kref);
844 struct gendisk *disk = cd->disk;
846 spin_lock(&sr_index_lock);
847 clear_bit(disk->first_minor, sr_index_bits);
848 spin_unlock(&sr_index_lock);
850 unregister_cdrom(&cd->cdi);
852 disk->private_data = NULL;
854 put_disk(disk);
856 kfree(cd);
859 static int sr_remove(struct device *dev)
861 struct scsi_cd *cd = dev_get_drvdata(dev);
863 del_gendisk(cd->disk);
865 mutex_lock(&sr_ref_mutex);
866 kref_put(&cd->kref, sr_kref_release);
867 mutex_unlock(&sr_ref_mutex);
869 return 0;
872 static int __init init_sr(void)
874 int rc;
876 rc = register_blkdev(SCSI_CDROM_MAJOR, "sr");
877 if (rc)
878 return rc;
879 return scsi_register_driver(&sr_template.gendrv);
882 static void __exit exit_sr(void)
884 scsi_unregister_driver(&sr_template.gendrv);
885 unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
888 module_init(init_sr);
889 module_exit(exit_sr);
890 MODULE_LICENSE("GPL");