Fix misspellings of "agressive".
[wrt350n-kernel.git] / drivers / scsi / sr.c
blob1857d68e7195e5b9c19a20afa3fd1ed6af08e790
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);
66 #define SR_DISKS 256
68 #define MAX_RETRIES 3
69 #define SR_TIMEOUT (30 * HZ)
70 #define SR_CAPABILITIES \
71 (CDC_CLOSE_TRAY|CDC_OPEN_TRAY|CDC_LOCK|CDC_SELECT_SPEED| \
72 CDC_SELECT_DISC|CDC_MULTI_SESSION|CDC_MCN|CDC_MEDIA_CHANGED| \
73 CDC_PLAY_AUDIO|CDC_RESET|CDC_DRIVE_STATUS| \
74 CDC_CD_R|CDC_CD_RW|CDC_DVD|CDC_DVD_R|CDC_DVD_RAM|CDC_GENERIC_PACKET| \
75 CDC_MRW|CDC_MRW_W|CDC_RAM)
77 static int sr_probe(struct device *);
78 static int sr_remove(struct device *);
79 static int sr_init_command(struct scsi_cmnd *);
81 static struct scsi_driver sr_template = {
82 .owner = THIS_MODULE,
83 .gendrv = {
84 .name = "sr",
85 .probe = sr_probe,
86 .remove = sr_remove,
88 .init_command = sr_init_command,
91 static unsigned long sr_index_bits[SR_DISKS / BITS_PER_LONG];
92 static DEFINE_SPINLOCK(sr_index_lock);
94 /* This semaphore is used to mediate the 0->1 reference get in the
95 * face of object destruction (i.e. we can't allow a get on an
96 * object after last put) */
97 static DEFINE_MUTEX(sr_ref_mutex);
99 static int sr_open(struct cdrom_device_info *, int);
100 static void sr_release(struct cdrom_device_info *);
102 static void get_sectorsize(struct scsi_cd *);
103 static void get_capabilities(struct scsi_cd *);
105 static int sr_media_change(struct cdrom_device_info *, int);
106 static int sr_packet(struct cdrom_device_info *, struct packet_command *);
108 static struct cdrom_device_ops sr_dops = {
109 .open = sr_open,
110 .release = sr_release,
111 .drive_status = sr_drive_status,
112 .media_changed = sr_media_change,
113 .tray_move = sr_tray_move,
114 .lock_door = sr_lock_door,
115 .select_speed = sr_select_speed,
116 .get_last_session = sr_get_last_session,
117 .get_mcn = sr_get_mcn,
118 .reset = sr_reset,
119 .audio_ioctl = sr_audio_ioctl,
120 .capability = SR_CAPABILITIES,
121 .generic_packet = sr_packet,
124 static void sr_kref_release(struct kref *kref);
126 static inline struct scsi_cd *scsi_cd(struct gendisk *disk)
128 return container_of(disk->private_data, struct scsi_cd, driver);
132 * The get and put routines for the struct scsi_cd. Note this entity
133 * has a scsi_device pointer and owns a reference to this.
135 static inline struct scsi_cd *scsi_cd_get(struct gendisk *disk)
137 struct scsi_cd *cd = NULL;
139 mutex_lock(&sr_ref_mutex);
140 if (disk->private_data == NULL)
141 goto out;
142 cd = scsi_cd(disk);
143 kref_get(&cd->kref);
144 if (scsi_device_get(cd->device))
145 goto out_put;
146 goto out;
148 out_put:
149 kref_put(&cd->kref, sr_kref_release);
150 cd = NULL;
151 out:
152 mutex_unlock(&sr_ref_mutex);
153 return cd;
156 static void scsi_cd_put(struct scsi_cd *cd)
158 struct scsi_device *sdev = cd->device;
160 mutex_lock(&sr_ref_mutex);
161 kref_put(&cd->kref, sr_kref_release);
162 scsi_device_put(sdev);
163 mutex_unlock(&sr_ref_mutex);
167 * This function checks to see if the media has been changed in the
168 * CDROM drive. It is possible that we have already sensed a change,
169 * or the drive may have sensed one and not yet reported it. We must
170 * be ready for either case. This function always reports the current
171 * value of the changed bit. If flag is 0, then the changed bit is reset.
172 * This function could be done as an ioctl, but we would need to have
173 * an inode for that to work, and we do not always have one.
176 int sr_media_change(struct cdrom_device_info *cdi, int slot)
178 struct scsi_cd *cd = cdi->handle;
179 int retval;
181 if (CDSL_CURRENT != slot) {
182 /* no changer support */
183 return -EINVAL;
186 retval = scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES);
187 if (retval) {
188 /* Unable to test, unit probably not ready. This usually
189 * means there is no disc in the drive. Mark as changed,
190 * and we will figure it out later once the drive is
191 * available again. */
192 cd->device->changed = 1;
193 return 1; /* This will force a flush, if called from
194 * check_disk_change */
197 retval = cd->device->changed;
198 cd->device->changed = 0;
199 /* If the disk changed, the capacity will now be different,
200 * so we force a re-read of this information */
201 if (retval) {
202 /* check multisession offset etc */
203 sr_cd_check(cdi);
205 get_sectorsize(cd);
207 return retval;
211 * rw_intr is the interrupt routine for the device driver.
213 * It will be notified on the end of a SCSI read / write, and will take on
214 * of several actions based on success or failure.
216 static void rw_intr(struct scsi_cmnd * SCpnt)
218 int result = SCpnt->result;
219 int this_count = SCpnt->request_bufflen;
220 int good_bytes = (result == 0 ? this_count : 0);
221 int block_sectors = 0;
222 long error_sector;
223 struct scsi_cd *cd = scsi_cd(SCpnt->request->rq_disk);
225 #ifdef DEBUG
226 printk("sr.c done: %x\n", result);
227 #endif
230 * Handle MEDIUM ERRORs or VOLUME OVERFLOWs that indicate partial
231 * success. Since this is a relatively rare error condition, no
232 * care is taken to avoid unnecessary additional work such as
233 * memcpy's that could be avoided.
235 if (driver_byte(result) != 0 && /* An error occurred */
236 (SCpnt->sense_buffer[0] & 0x7f) == 0x70) { /* Sense current */
237 switch (SCpnt->sense_buffer[2]) {
238 case MEDIUM_ERROR:
239 case VOLUME_OVERFLOW:
240 case ILLEGAL_REQUEST:
241 if (!(SCpnt->sense_buffer[0] & 0x90))
242 break;
243 error_sector = (SCpnt->sense_buffer[3] << 24) |
244 (SCpnt->sense_buffer[4] << 16) |
245 (SCpnt->sense_buffer[5] << 8) |
246 SCpnt->sense_buffer[6];
247 if (SCpnt->request->bio != NULL)
248 block_sectors =
249 bio_sectors(SCpnt->request->bio);
250 if (block_sectors < 4)
251 block_sectors = 4;
252 if (cd->device->sector_size == 2048)
253 error_sector <<= 2;
254 error_sector &= ~(block_sectors - 1);
255 good_bytes = (error_sector - SCpnt->request->sector) << 9;
256 if (good_bytes < 0 || good_bytes >= this_count)
257 good_bytes = 0;
259 * The SCSI specification allows for the value
260 * returned by READ CAPACITY to be up to 75 2K
261 * sectors past the last readable block.
262 * Therefore, if we hit a medium error within the
263 * last 75 2K sectors, we decrease the saved size
264 * value.
266 if (error_sector < get_capacity(cd->disk) &&
267 cd->capacity - error_sector < 4 * 75)
268 set_capacity(cd->disk, error_sector);
269 break;
271 case RECOVERED_ERROR:
274 * An error occured, but it recovered. Inform the
275 * user, but make sure that it's not treated as a
276 * hard error.
278 scsi_print_sense("sr", SCpnt);
279 SCpnt->result = 0;
280 SCpnt->sense_buffer[0] = 0x0;
281 good_bytes = this_count;
282 break;
284 default:
285 break;
290 * This calls the generic completion function, now that we know
291 * how many actual sectors finished, and how many sectors we need
292 * to say have failed.
294 scsi_io_completion(SCpnt, good_bytes);
297 static int sr_init_command(struct scsi_cmnd * SCpnt)
299 int block=0, this_count, s_size, timeout = SR_TIMEOUT;
300 struct scsi_cd *cd = scsi_cd(SCpnt->request->rq_disk);
302 SCSI_LOG_HLQUEUE(1, printk("Doing sr request, dev = %s, block = %d\n",
303 cd->disk->disk_name, block));
305 if (!cd->device || !scsi_device_online(cd->device)) {
306 SCSI_LOG_HLQUEUE(2, printk("Finishing %ld sectors\n",
307 SCpnt->request->nr_sectors));
308 SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt));
309 return 0;
312 if (cd->device->changed) {
314 * quietly refuse to do anything to a changed disc until the
315 * changed bit has been reset
317 return 0;
321 * we do lazy blocksize switching (when reading XA sectors,
322 * see CDROMREADMODE2 ioctl)
324 s_size = cd->device->sector_size;
325 if (s_size > 2048) {
326 if (!in_interrupt())
327 sr_set_blocklength(cd, 2048);
328 else
329 printk("sr: can't switch blocksize: in interrupt\n");
332 if (s_size != 512 && s_size != 1024 && s_size != 2048) {
333 scmd_printk(KERN_ERR, SCpnt, "bad sector size %d\n", s_size);
334 return 0;
337 if (rq_data_dir(SCpnt->request) == WRITE) {
338 if (!cd->device->writeable)
339 return 0;
340 SCpnt->cmnd[0] = WRITE_10;
341 SCpnt->sc_data_direction = DMA_TO_DEVICE;
342 cd->cdi.media_written = 1;
343 } else if (rq_data_dir(SCpnt->request) == READ) {
344 SCpnt->cmnd[0] = READ_10;
345 SCpnt->sc_data_direction = DMA_FROM_DEVICE;
346 } else {
347 blk_dump_rq_flags(SCpnt->request, "Unknown sr command");
348 return 0;
352 struct scatterlist *sg = SCpnt->request_buffer;
353 int i, size = 0;
354 for (i = 0; i < SCpnt->use_sg; i++)
355 size += sg[i].length;
357 if (size != SCpnt->request_bufflen && SCpnt->use_sg) {
358 scmd_printk(KERN_ERR, SCpnt,
359 "mismatch count %d, bytes %d\n",
360 size, SCpnt->request_bufflen);
361 if (SCpnt->request_bufflen > size)
362 SCpnt->request_bufflen = size;
367 * request doesn't start on hw block boundary, add scatter pads
369 if (((unsigned int)SCpnt->request->sector % (s_size >> 9)) ||
370 (SCpnt->request_bufflen % s_size)) {
371 scmd_printk(KERN_NOTICE, SCpnt, "unaligned transfer\n");
372 return 0;
375 this_count = (SCpnt->request_bufflen >> 9) / (s_size >> 9);
378 SCSI_LOG_HLQUEUE(2, printk("%s : %s %d/%ld 512 byte blocks.\n",
379 cd->cdi.name,
380 (rq_data_dir(SCpnt->request) == WRITE) ?
381 "writing" : "reading",
382 this_count, SCpnt->request->nr_sectors));
384 SCpnt->cmnd[1] = 0;
385 block = (unsigned int)SCpnt->request->sector / (s_size >> 9);
387 if (this_count > 0xffff) {
388 this_count = 0xffff;
389 SCpnt->request_bufflen = this_count * s_size;
392 SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
393 SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
394 SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
395 SCpnt->cmnd[5] = (unsigned char) block & 0xff;
396 SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
397 SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
398 SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
401 * We shouldn't disconnect in the middle of a sector, so with a dumb
402 * host adapter, it's safe to assume that we can at least transfer
403 * this many bytes between each connect / disconnect.
405 SCpnt->transfersize = cd->device->sector_size;
406 SCpnt->underflow = this_count << 9;
407 SCpnt->allowed = MAX_RETRIES;
408 SCpnt->timeout_per_command = timeout;
411 * This is the completion routine we use. This is matched in terms
412 * of capability to this function.
414 SCpnt->done = rw_intr;
417 * This indicates that the command is ready from our end to be
418 * queued.
420 return 1;
423 static int sr_block_open(struct inode *inode, struct file *file)
425 struct gendisk *disk = inode->i_bdev->bd_disk;
426 struct scsi_cd *cd;
427 int ret = 0;
429 if(!(cd = scsi_cd_get(disk)))
430 return -ENXIO;
432 if((ret = cdrom_open(&cd->cdi, inode, file)) != 0)
433 scsi_cd_put(cd);
435 return ret;
438 static int sr_block_release(struct inode *inode, struct file *file)
440 int ret;
441 struct scsi_cd *cd = scsi_cd(inode->i_bdev->bd_disk);
442 ret = cdrom_release(&cd->cdi, file);
443 if(ret)
444 return ret;
446 scsi_cd_put(cd);
448 return 0;
451 static int sr_block_ioctl(struct inode *inode, struct file *file, unsigned cmd,
452 unsigned long arg)
454 struct scsi_cd *cd = scsi_cd(inode->i_bdev->bd_disk);
455 struct scsi_device *sdev = cd->device;
456 void __user *argp = (void __user *)arg;
457 int ret;
460 * Send SCSI addressing ioctls directly to mid level, send other
461 * ioctls to cdrom/block level.
463 switch (cmd) {
464 case SCSI_IOCTL_GET_IDLUN:
465 case SCSI_IOCTL_GET_BUS_NUMBER:
466 return scsi_ioctl(sdev, cmd, argp);
469 ret = cdrom_ioctl(file, &cd->cdi, inode, cmd, arg);
470 if (ret != -ENOSYS)
471 return ret;
474 * ENODEV means that we didn't recognise the ioctl, or that we
475 * cannot execute it in the current device state. In either
476 * case fall through to scsi_ioctl, which will return ENDOEV again
477 * if it doesn't recognise the ioctl
479 ret = scsi_nonblockable_ioctl(sdev, cmd, argp, NULL);
480 if (ret != -ENODEV)
481 return ret;
482 return scsi_ioctl(sdev, cmd, argp);
485 static int sr_block_media_changed(struct gendisk *disk)
487 struct scsi_cd *cd = scsi_cd(disk);
488 return cdrom_media_changed(&cd->cdi);
491 static struct block_device_operations sr_bdops =
493 .owner = THIS_MODULE,
494 .open = sr_block_open,
495 .release = sr_block_release,
496 .ioctl = sr_block_ioctl,
497 .media_changed = sr_block_media_changed,
499 * No compat_ioctl for now because sr_block_ioctl never
500 * seems to pass arbitary ioctls down to host drivers.
504 static int sr_open(struct cdrom_device_info *cdi, int purpose)
506 struct scsi_cd *cd = cdi->handle;
507 struct scsi_device *sdev = cd->device;
508 int retval;
511 * If the device is in error recovery, wait until it is done.
512 * If the device is offline, then disallow any access to it.
514 retval = -ENXIO;
515 if (!scsi_block_when_processing_errors(sdev))
516 goto error_out;
518 return 0;
520 error_out:
521 return retval;
524 static void sr_release(struct cdrom_device_info *cdi)
526 struct scsi_cd *cd = cdi->handle;
528 if (cd->device->sector_size > 2048)
529 sr_set_blocklength(cd, 2048);
533 static int sr_probe(struct device *dev)
535 struct scsi_device *sdev = to_scsi_device(dev);
536 struct gendisk *disk;
537 struct scsi_cd *cd;
538 int minor, error;
540 error = -ENODEV;
541 if (sdev->type != TYPE_ROM && sdev->type != TYPE_WORM)
542 goto fail;
544 error = -ENOMEM;
545 cd = kzalloc(sizeof(*cd), GFP_KERNEL);
546 if (!cd)
547 goto fail;
549 kref_init(&cd->kref);
551 disk = alloc_disk(1);
552 if (!disk)
553 goto fail_free;
555 spin_lock(&sr_index_lock);
556 minor = find_first_zero_bit(sr_index_bits, SR_DISKS);
557 if (minor == SR_DISKS) {
558 spin_unlock(&sr_index_lock);
559 error = -EBUSY;
560 goto fail_put;
562 __set_bit(minor, sr_index_bits);
563 spin_unlock(&sr_index_lock);
565 disk->major = SCSI_CDROM_MAJOR;
566 disk->first_minor = minor;
567 sprintf(disk->disk_name, "sr%d", minor);
568 disk->fops = &sr_bdops;
569 disk->flags = GENHD_FL_CD;
571 cd->device = sdev;
572 cd->disk = disk;
573 cd->driver = &sr_template;
574 cd->disk = disk;
575 cd->capacity = 0x1fffff;
576 cd->device->changed = 1; /* force recheck CD type */
577 cd->use = 1;
578 cd->readcd_known = 0;
579 cd->readcd_cdda = 0;
581 cd->cdi.ops = &sr_dops;
582 cd->cdi.handle = cd;
583 cd->cdi.mask = 0;
584 cd->cdi.capacity = 1;
585 sprintf(cd->cdi.name, "sr%d", minor);
587 sdev->sector_size = 2048; /* A guess, just in case */
589 /* FIXME: need to handle a get_capabilities failure properly ?? */
590 get_capabilities(cd);
591 sr_vendor_init(cd);
593 disk->driverfs_dev = &sdev->sdev_gendev;
594 set_capacity(disk, cd->capacity);
595 disk->private_data = &cd->driver;
596 disk->queue = sdev->request_queue;
597 cd->cdi.disk = disk;
599 if (register_cdrom(&cd->cdi))
600 goto fail_put;
602 dev_set_drvdata(dev, cd);
603 disk->flags |= GENHD_FL_REMOVABLE;
604 add_disk(disk);
606 sdev_printk(KERN_DEBUG, sdev,
607 "Attached scsi CD-ROM %s\n", cd->cdi.name);
608 return 0;
610 fail_put:
611 put_disk(disk);
612 fail_free:
613 kfree(cd);
614 fail:
615 return error;
619 static void get_sectorsize(struct scsi_cd *cd)
621 unsigned char cmd[10];
622 unsigned char *buffer;
623 int the_result, retries = 3;
624 int sector_size;
625 request_queue_t *queue;
627 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
628 if (!buffer)
629 goto Enomem;
631 do {
632 cmd[0] = READ_CAPACITY;
633 memset((void *) &cmd[1], 0, 9);
634 memset(buffer, 0, 8);
636 /* Do the command and wait.. */
637 the_result = scsi_execute_req(cd->device, cmd, DMA_FROM_DEVICE,
638 buffer, 8, NULL, SR_TIMEOUT,
639 MAX_RETRIES);
641 retries--;
643 } while (the_result && retries);
646 if (the_result) {
647 cd->capacity = 0x1fffff;
648 sector_size = 2048; /* A guess, just in case */
649 } else {
650 #if 0
651 if (cdrom_get_last_written(&cd->cdi,
652 &cd->capacity))
653 #endif
654 cd->capacity = 1 + ((buffer[0] << 24) |
655 (buffer[1] << 16) |
656 (buffer[2] << 8) |
657 buffer[3]);
658 sector_size = (buffer[4] << 24) |
659 (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
660 switch (sector_size) {
662 * HP 4020i CD-Recorder reports 2340 byte sectors
663 * Philips CD-Writers report 2352 byte sectors
665 * Use 2k sectors for them..
667 case 0:
668 case 2340:
669 case 2352:
670 sector_size = 2048;
671 /* fall through */
672 case 2048:
673 cd->capacity *= 4;
674 /* fall through */
675 case 512:
676 break;
677 default:
678 printk("%s: unsupported sector size %d.\n",
679 cd->cdi.name, sector_size);
680 cd->capacity = 0;
683 cd->device->sector_size = sector_size;
686 * Add this so that we have the ability to correctly gauge
687 * what the device is capable of.
689 set_capacity(cd->disk, cd->capacity);
692 queue = cd->device->request_queue;
693 blk_queue_hardsect_size(queue, sector_size);
694 out:
695 kfree(buffer);
696 return;
698 Enomem:
699 cd->capacity = 0x1fffff;
700 cd->device->sector_size = 2048; /* A guess, just in case */
701 goto out;
704 static void get_capabilities(struct scsi_cd *cd)
706 unsigned char *buffer;
707 struct scsi_mode_data data;
708 unsigned char cmd[MAX_COMMAND_SIZE];
709 struct scsi_sense_hdr sshdr;
710 unsigned int the_result;
711 int retries, rc, n;
713 static const char *loadmech[] =
715 "caddy",
716 "tray",
717 "pop-up",
719 "changer",
720 "cartridge changer",
726 /* allocate transfer buffer */
727 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
728 if (!buffer) {
729 printk(KERN_ERR "sr: out of memory.\n");
730 return;
733 /* issue TEST_UNIT_READY until the initial startup UNIT_ATTENTION
734 * conditions are gone, or a timeout happens
736 retries = 0;
737 do {
738 memset((void *)cmd, 0, MAX_COMMAND_SIZE);
739 cmd[0] = TEST_UNIT_READY;
741 the_result = scsi_execute_req (cd->device, cmd, DMA_NONE, NULL,
742 0, &sshdr, SR_TIMEOUT,
743 MAX_RETRIES);
745 retries++;
746 } while (retries < 5 &&
747 (!scsi_status_is_good(the_result) ||
748 (scsi_sense_valid(&sshdr) &&
749 sshdr.sense_key == UNIT_ATTENTION)));
751 /* ask for mode page 0x2a */
752 rc = scsi_mode_sense(cd->device, 0, 0x2a, buffer, 128,
753 SR_TIMEOUT, 3, &data, NULL);
755 if (!scsi_status_is_good(rc)) {
756 /* failed, drive doesn't have capabilities mode page */
757 cd->cdi.speed = 1;
758 cd->cdi.mask |= (CDC_CD_R | CDC_CD_RW | CDC_DVD_R |
759 CDC_DVD | CDC_DVD_RAM |
760 CDC_SELECT_DISC | CDC_SELECT_SPEED |
761 CDC_MRW | CDC_MRW_W | CDC_RAM);
762 kfree(buffer);
763 printk("%s: scsi-1 drive\n", cd->cdi.name);
764 return;
767 n = data.header_length + data.block_descriptor_length;
768 cd->cdi.speed = ((buffer[n + 8] << 8) + buffer[n + 9]) / 176;
769 cd->readcd_known = 1;
770 cd->readcd_cdda = buffer[n + 5] & 0x01;
771 /* print some capability bits */
772 printk("%s: scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n", cd->cdi.name,
773 ((buffer[n + 14] << 8) + buffer[n + 15]) / 176,
774 cd->cdi.speed,
775 buffer[n + 3] & 0x01 ? "writer " : "", /* CD Writer */
776 buffer[n + 3] & 0x20 ? "dvd-ram " : "",
777 buffer[n + 2] & 0x02 ? "cd/rw " : "", /* can read rewriteable */
778 buffer[n + 4] & 0x20 ? "xa/form2 " : "", /* can read xa/from2 */
779 buffer[n + 5] & 0x01 ? "cdda " : "", /* can read audio data */
780 loadmech[buffer[n + 6] >> 5]);
781 if ((buffer[n + 6] >> 5) == 0)
782 /* caddy drives can't close tray... */
783 cd->cdi.mask |= CDC_CLOSE_TRAY;
784 if ((buffer[n + 2] & 0x8) == 0)
785 /* not a DVD drive */
786 cd->cdi.mask |= CDC_DVD;
787 if ((buffer[n + 3] & 0x20) == 0)
788 /* can't write DVD-RAM media */
789 cd->cdi.mask |= CDC_DVD_RAM;
790 if ((buffer[n + 3] & 0x10) == 0)
791 /* can't write DVD-R media */
792 cd->cdi.mask |= CDC_DVD_R;
793 if ((buffer[n + 3] & 0x2) == 0)
794 /* can't write CD-RW media */
795 cd->cdi.mask |= CDC_CD_RW;
796 if ((buffer[n + 3] & 0x1) == 0)
797 /* can't write CD-R media */
798 cd->cdi.mask |= CDC_CD_R;
799 if ((buffer[n + 6] & 0x8) == 0)
800 /* can't eject */
801 cd->cdi.mask |= CDC_OPEN_TRAY;
803 if ((buffer[n + 6] >> 5) == mechtype_individual_changer ||
804 (buffer[n + 6] >> 5) == mechtype_cartridge_changer)
805 cd->cdi.capacity =
806 cdrom_number_of_slots(&cd->cdi);
807 if (cd->cdi.capacity <= 1)
808 /* not a changer */
809 cd->cdi.mask |= CDC_SELECT_DISC;
810 /*else I don't think it can close its tray
811 cd->cdi.mask |= CDC_CLOSE_TRAY; */
814 * if DVD-RAM, MRW-W or CD-RW, we are randomly writable
816 if ((cd->cdi.mask & (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) !=
817 (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) {
818 cd->device->writeable = 1;
821 kfree(buffer);
825 * sr_packet() is the entry point for the generic commands generated
826 * by the Uniform CD-ROM layer.
828 static int sr_packet(struct cdrom_device_info *cdi,
829 struct packet_command *cgc)
831 if (cgc->timeout <= 0)
832 cgc->timeout = IOCTL_TIMEOUT;
834 sr_do_ioctl(cdi->handle, cgc);
836 return cgc->stat;
840 * sr_kref_release - Called to free the scsi_cd structure
841 * @kref: pointer to embedded kref
843 * sr_ref_mutex must be held entering this routine. Because it is
844 * called on last put, you should always use the scsi_cd_get()
845 * scsi_cd_put() helpers which manipulate the semaphore directly
846 * and never do a direct kref_put().
848 static void sr_kref_release(struct kref *kref)
850 struct scsi_cd *cd = container_of(kref, struct scsi_cd, kref);
851 struct gendisk *disk = cd->disk;
853 spin_lock(&sr_index_lock);
854 clear_bit(disk->first_minor, sr_index_bits);
855 spin_unlock(&sr_index_lock);
857 unregister_cdrom(&cd->cdi);
859 disk->private_data = NULL;
861 put_disk(disk);
863 kfree(cd);
866 static int sr_remove(struct device *dev)
868 struct scsi_cd *cd = dev_get_drvdata(dev);
870 del_gendisk(cd->disk);
872 mutex_lock(&sr_ref_mutex);
873 kref_put(&cd->kref, sr_kref_release);
874 mutex_unlock(&sr_ref_mutex);
876 return 0;
879 static int __init init_sr(void)
881 int rc;
883 rc = register_blkdev(SCSI_CDROM_MAJOR, "sr");
884 if (rc)
885 return rc;
886 return scsi_register_driver(&sr_template.gendrv);
889 static void __exit exit_sr(void)
891 scsi_unregister_driver(&sr_template.gendrv);
892 unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
895 module_init(init_sr);
896 module_exit(exit_sr);
897 MODULE_LICENSE("GPL");