Merge with Linux 2.5.74.
[linux-2.6/linux-mips.git] / drivers / scsi / sr.c
blob8fa5176834f2e086e5a3c837fb21eec72ab1897f
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/blk.h>
47 #include <asm/uaccess.h>
49 #include "scsi.h"
50 #include "hosts.h"
52 #include <scsi/scsi_driver.h>
53 #include <scsi/scsi_ioctl.h> /* For the door lock/unlock commands */
55 #include "scsi_logging.h"
56 #include "sr.h"
59 MODULE_PARM(xa_test, "i"); /* see sr_ioctl.c */
62 #define SR_DISKS (1 << KDEV_MINOR_BITS)
64 #define MAX_RETRIES 3
65 #define SR_TIMEOUT (30 * HZ)
66 #define SR_CAPABILITIES \
67 (CDC_CLOSE_TRAY|CDC_OPEN_TRAY|CDC_LOCK|CDC_SELECT_SPEED| \
68 CDC_SELECT_DISC|CDC_MULTI_SESSION|CDC_MCN|CDC_MEDIA_CHANGED| \
69 CDC_PLAY_AUDIO|CDC_RESET|CDC_IOCTLS|CDC_DRIVE_STATUS| \
70 CDC_CD_R|CDC_CD_RW|CDC_DVD|CDC_DVD_R|CDC_DVD_RAM|CDC_GENERIC_PACKET)
72 static int sr_probe(struct device *);
73 static int sr_remove(struct device *);
74 static int sr_init_command(struct scsi_cmnd *);
76 static struct scsi_driver sr_template = {
77 .owner = THIS_MODULE,
78 .gendrv = {
79 .name = "sr",
80 .probe = sr_probe,
81 .remove = sr_remove,
83 .init_command = sr_init_command,
86 static unsigned long sr_index_bits[SR_DISKS / BITS_PER_LONG];
87 static spinlock_t sr_index_lock = SPIN_LOCK_UNLOCKED;
89 static int sr_open(struct cdrom_device_info *, int);
90 static void sr_release(struct cdrom_device_info *);
92 static void get_sectorsize(struct scsi_cd *);
93 static void get_capabilities(struct scsi_cd *);
95 static int sr_media_change(struct cdrom_device_info *, int);
96 static int sr_packet(struct cdrom_device_info *, struct cdrom_generic_command *);
98 static struct cdrom_device_ops sr_dops = {
99 .open = sr_open,
100 .release = sr_release,
101 .drive_status = sr_drive_status,
102 .media_changed = sr_media_change,
103 .tray_move = sr_tray_move,
104 .lock_door = sr_lock_door,
105 .select_speed = sr_select_speed,
106 .get_last_session = sr_get_last_session,
107 .get_mcn = sr_get_mcn,
108 .reset = sr_reset,
109 .audio_ioctl = sr_audio_ioctl,
110 .dev_ioctl = sr_dev_ioctl,
111 .capability = SR_CAPABILITIES,
112 .generic_packet = sr_packet,
116 * This function checks to see if the media has been changed in the
117 * CDROM drive. It is possible that we have already sensed a change,
118 * or the drive may have sensed one and not yet reported it. We must
119 * be ready for either case. This function always reports the current
120 * value of the changed bit. If flag is 0, then the changed bit is reset.
121 * This function could be done as an ioctl, but we would need to have
122 * an inode for that to work, and we do not always have one.
125 int sr_media_change(struct cdrom_device_info *cdi, int slot)
127 struct scsi_cd *cd = cdi->handle;
128 int retval;
130 if (CDSL_CURRENT != slot) {
131 /* no changer support */
132 return -EINVAL;
135 retval = scsi_ioctl(cd->device, SCSI_IOCTL_TEST_UNIT_READY, 0);
136 if (retval) {
137 /* Unable to test, unit probably not ready. This usually
138 * means there is no disc in the drive. Mark as changed,
139 * and we will figure it out later once the drive is
140 * available again. */
141 cd->device->changed = 1;
142 return 1; /* This will force a flush, if called from
143 * check_disk_change */
146 retval = cd->device->changed;
147 cd->device->changed = 0;
148 /* If the disk changed, the capacity will now be different,
149 * so we force a re-read of this information */
150 if (retval) {
151 /* check multisession offset etc */
152 sr_cd_check(cdi);
155 * If the disk changed, the capacity will now be different,
156 * so we force a re-read of this information
157 * Force 2048 for the sector size so that filesystems won't
158 * be trying to use something that is too small if the disc
159 * has changed.
161 cd->needs_sector_size = 1;
162 cd->device->sector_size = 2048;
164 return retval;
167 static inline struct scsi_cd *scsi_cd(struct gendisk *disk)
169 return container_of(disk->private_data, struct scsi_cd, driver);
173 * rw_intr is the interrupt routine for the device driver.
175 * It will be notified on the end of a SCSI read / write, and will take on
176 * of several actions based on success or failure.
178 static void rw_intr(struct scsi_cmnd * SCpnt)
180 int result = SCpnt->result;
181 int this_count = SCpnt->bufflen >> 9;
182 int good_sectors = (result == 0 ? this_count : 0);
183 int block_sectors = 0;
184 struct scsi_cd *cd = scsi_cd(SCpnt->request->rq_disk);
186 #ifdef DEBUG
187 printk("sr.c done: %x %p\n", result, SCpnt->request->bh->b_data);
188 #endif
191 * Handle MEDIUM ERRORs or VOLUME OVERFLOWs that indicate partial
192 * success. Since this is a relatively rare error condition, no
193 * care is taken to avoid unnecessary additional work such as
194 * memcpy's that could be avoided.
196 if (driver_byte(result) != 0 && /* An error occurred */
197 SCpnt->sense_buffer[0] == 0xF0 && /* Sense data is valid */
198 (SCpnt->sense_buffer[2] == MEDIUM_ERROR ||
199 SCpnt->sense_buffer[2] == VOLUME_OVERFLOW ||
200 SCpnt->sense_buffer[2] == ILLEGAL_REQUEST)) {
201 long error_sector = (SCpnt->sense_buffer[3] << 24) |
202 (SCpnt->sense_buffer[4] << 16) |
203 (SCpnt->sense_buffer[5] << 8) |
204 SCpnt->sense_buffer[6];
205 if (SCpnt->request->bio != NULL)
206 block_sectors = bio_sectors(SCpnt->request->bio);
207 if (block_sectors < 4)
208 block_sectors = 4;
209 if (cd->device->sector_size == 2048)
210 error_sector <<= 2;
211 error_sector &= ~(block_sectors - 1);
212 good_sectors = error_sector - SCpnt->request->sector;
213 if (good_sectors < 0 || good_sectors >= this_count)
214 good_sectors = 0;
216 * The SCSI specification allows for the value returned by READ
217 * CAPACITY to be up to 75 2K sectors past the last readable
218 * block. Therefore, if we hit a medium error within the last
219 * 75 2K sectors, we decrease the saved size value.
221 if (error_sector < get_capacity(cd->disk) &&
222 cd->capacity - error_sector < 4 * 75)
223 set_capacity(cd->disk, error_sector);
227 * This calls the generic completion function, now that we know
228 * how many actual sectors finished, and how many sectors we need
229 * to say have failed.
231 scsi_io_completion(SCpnt, good_sectors, block_sectors);
234 static int sr_init_command(struct scsi_cmnd * SCpnt)
236 int block=0, this_count, s_size, timeout = SR_TIMEOUT;
237 struct scsi_cd *cd = scsi_cd(SCpnt->request->rq_disk);
239 SCSI_LOG_HLQUEUE(1, printk("Doing sr request, dev = %s, block = %d\n",
240 cd->disk->disk_name, block));
242 if (!cd->device || !cd->device->online) {
243 SCSI_LOG_HLQUEUE(2, printk("Finishing %ld sectors\n",
244 SCpnt->request->nr_sectors));
245 SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt));
246 return 0;
249 if (cd->device->changed) {
251 * quietly refuse to do anything to a changed disc until the
252 * changed bit has been reset
254 return 0;
258 * these are already setup, just copy cdb basically
260 if (SCpnt->request->flags & REQ_BLOCK_PC) {
261 struct request *rq = SCpnt->request;
263 if (sizeof(rq->cmd) > sizeof(SCpnt->cmnd))
264 return 0;
266 memcpy(SCpnt->cmnd, rq->cmd, sizeof(SCpnt->cmnd));
267 if (rq_data_dir(rq) == WRITE)
268 SCpnt->sc_data_direction = SCSI_DATA_WRITE;
269 else if (rq->data_len)
270 SCpnt->sc_data_direction = SCSI_DATA_READ;
271 else
272 SCpnt->sc_data_direction = SCSI_DATA_NONE;
274 this_count = rq->data_len;
275 if (rq->timeout)
276 timeout = rq->timeout;
278 SCpnt->transfersize = rq->data_len;
279 SCpnt->underflow = rq->data_len;
280 goto queue;
283 if (!(SCpnt->request->flags & REQ_CMD)) {
284 blk_dump_rq_flags(SCpnt->request, "sr unsup command");
285 return 0;
289 * we do lazy blocksize switching (when reading XA sectors,
290 * see CDROMREADMODE2 ioctl)
292 s_size = cd->device->sector_size;
293 if (s_size > 2048) {
294 if (!in_interrupt())
295 sr_set_blocklength(cd, 2048);
296 else
297 printk("sr: can't switch blocksize: in interrupt\n");
300 if (s_size != 512 && s_size != 1024 && s_size != 2048) {
301 printk("sr: bad sector size %d\n", s_size);
302 return 0;
305 if (rq_data_dir(SCpnt->request) == WRITE) {
306 if (!cd->device->writeable)
307 return 0;
308 SCpnt->cmnd[0] = WRITE_10;
309 SCpnt->sc_data_direction = SCSI_DATA_WRITE;
310 } else if (rq_data_dir(SCpnt->request) == READ) {
311 SCpnt->cmnd[0] = READ_10;
312 SCpnt->sc_data_direction = SCSI_DATA_READ;
313 } else {
314 blk_dump_rq_flags(SCpnt->request, "Unknown sr command");
315 return 0;
319 * request doesn't start on hw block boundary, add scatter pads
321 if (((unsigned int)SCpnt->request->sector % (s_size >> 9)) ||
322 (SCpnt->request_bufflen % s_size)) {
323 printk("sr: unaligned transfer\n");
324 return 0;
327 this_count = (SCpnt->request_bufflen >> 9) / (s_size >> 9);
330 SCSI_LOG_HLQUEUE(2, printk("%s : %s %d/%ld 512 byte blocks.\n",
331 cd->cdi.name,
332 (rq_data_dir(SCpnt->request) == WRITE) ?
333 "writing" : "reading",
334 this_count, SCpnt->request->nr_sectors));
336 SCpnt->cmnd[1] = 0;
337 block = (unsigned int)SCpnt->request->sector / (s_size >> 9);
339 if (this_count > 0xffff)
340 this_count = 0xffff;
342 SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
343 SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
344 SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
345 SCpnt->cmnd[5] = (unsigned char) block & 0xff;
346 SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
347 SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
348 SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
351 * We shouldn't disconnect in the middle of a sector, so with a dumb
352 * host adapter, it's safe to assume that we can at least transfer
353 * this many bytes between each connect / disconnect.
355 SCpnt->transfersize = cd->device->sector_size;
356 SCpnt->underflow = this_count << 9;
358 queue:
359 SCpnt->allowed = MAX_RETRIES;
360 SCpnt->timeout_per_command = timeout;
363 * This is the completion routine we use. This is matched in terms
364 * of capability to this function.
366 SCpnt->done = rw_intr;
369 struct scatterlist *sg = SCpnt->request_buffer;
370 int i, size = 0;
371 for (i = 0; i < SCpnt->use_sg; i++)
372 size += sg[i].length;
374 if (size != SCpnt->request_bufflen && SCpnt->use_sg) {
375 printk("sr: mismatch count %d, bytes %d\n", size, SCpnt->request_bufflen);
376 SCpnt->request_bufflen = size;
381 * This indicates that the command is ready from our end to be
382 * queued.
384 return 1;
387 static int sr_block_open(struct inode *inode, struct file *file)
389 struct scsi_cd *cd = scsi_cd(inode->i_bdev->bd_disk);
390 return cdrom_open(&cd->cdi, inode, file);
393 static int sr_block_release(struct inode *inode, struct file *file)
395 struct scsi_cd *cd = scsi_cd(inode->i_bdev->bd_disk);
396 return cdrom_release(&cd->cdi, file);
399 static int sr_block_ioctl(struct inode *inode, struct file *file, unsigned cmd,
400 unsigned long arg)
402 struct scsi_cd *cd = scsi_cd(inode->i_bdev->bd_disk);
403 struct scsi_device *sdev = cd->device;
406 * Send SCSI addressing ioctls directly to mid level, send other
407 * ioctls to cdrom/block level.
409 switch (cmd) {
410 case SCSI_IOCTL_GET_IDLUN:
411 case SCSI_IOCTL_GET_BUS_NUMBER:
412 return scsi_ioctl(sdev, cmd, (void *)arg);
414 return cdrom_ioctl(&cd->cdi, inode, cmd, arg);
417 static int sr_block_media_changed(struct gendisk *disk)
419 struct scsi_cd *cd = scsi_cd(disk);
420 return cdrom_media_changed(&cd->cdi);
423 struct block_device_operations sr_bdops =
425 .owner = THIS_MODULE,
426 .open = sr_block_open,
427 .release = sr_block_release,
428 .ioctl = sr_block_ioctl,
429 .media_changed = sr_block_media_changed,
432 static int sr_open(struct cdrom_device_info *cdi, int purpose)
434 struct scsi_cd *cd = cdi->handle;
435 struct scsi_device *sdev = cd->device;
436 int retval;
438 retval = scsi_device_get(sdev);
439 if (retval)
440 return retval;
443 * If the device is in error recovery, wait until it is done.
444 * If the device is offline, then disallow any access to it.
446 retval = -ENXIO;
447 if (!scsi_block_when_processing_errors(sdev))
448 goto error_out;
451 * If this device did not have media in the drive at boot time, then
452 * we would have been unable to get the sector size. Check to see if
453 * this is the case, and try again.
455 if (cd->needs_sector_size)
456 get_sectorsize(cd);
457 return 0;
459 error_out:
460 scsi_device_put(sdev);
461 return retval;
464 static void sr_release(struct cdrom_device_info *cdi)
466 struct scsi_cd *cd = cdi->handle;
468 if (cd->device->sector_size > 2048)
469 sr_set_blocklength(cd, 2048);
471 scsi_device_put(cd->device);
474 static int sr_probe(struct device *dev)
476 struct scsi_device *sdev = to_scsi_device(dev);
477 struct gendisk *disk;
478 struct scsi_cd *cd;
479 int minor, error;
481 error = -ENODEV;
482 if (sdev->type != TYPE_ROM && sdev->type != TYPE_WORM)
483 goto fail;
485 error = -ENOMEM;
486 cd = kmalloc(sizeof(*cd), GFP_KERNEL);
487 if (!cd)
488 goto fail;
489 memset(cd, 0, sizeof(*cd));
491 disk = alloc_disk(1);
492 if (!disk)
493 goto fail_free;
495 spin_lock(&sr_index_lock);
496 minor = find_first_zero_bit(sr_index_bits, SR_DISKS);
497 if (minor == SR_DISKS) {
498 spin_unlock(&sr_index_lock);
499 error = -EBUSY;
500 goto fail_put;
502 __set_bit(minor, sr_index_bits);
503 spin_unlock(&sr_index_lock);
505 disk->major = SCSI_CDROM_MAJOR;
506 disk->first_minor = minor;
507 sprintf(disk->disk_name, "sr%d", minor);
508 disk->fops = &sr_bdops;
509 disk->flags = GENHD_FL_CD;
511 cd->device = sdev;
512 cd->disk = disk;
513 cd->driver = &sr_template;
514 cd->disk = disk;
515 cd->capacity = 0x1fffff;
516 cd->needs_sector_size = 1;
517 cd->device->changed = 1; /* force recheck CD type */
518 cd->use = 1;
519 cd->readcd_known = 0;
520 cd->readcd_cdda = 0;
522 cd->cdi.ops = &sr_dops;
523 cd->cdi.handle = cd;
524 cd->cdi.mask = 0;
525 cd->cdi.capacity = 1;
526 sprintf(cd->cdi.name, "sr%d", minor);
528 sdev->sector_size = 2048; /* A guess, just in case */
530 /* FIXME: need to handle a get_capabilities failure properly ?? */
531 get_capabilities(cd);
532 sr_vendor_init(cd);
534 snprintf(disk->devfs_name, sizeof(disk->devfs_name),
535 "%s/cd", sdev->devfs_name);
536 disk->driverfs_dev = &sdev->sdev_driverfs_dev;
537 register_cdrom(&cd->cdi);
538 set_capacity(disk, cd->capacity);
539 disk->private_data = &cd->driver;
540 disk->queue = sdev->request_queue;
542 dev_set_drvdata(dev, cd);
543 add_disk(disk);
545 printk(KERN_DEBUG
546 "Attached scsi CD-ROM %s at scsi%d, channel %d, id %d, lun %d\n",
547 cd->cdi.name, sdev->host->host_no, sdev->channel,
548 sdev->id, sdev->lun);
549 return 0;
551 fail_put:
552 put_disk(disk);
553 fail_free:
554 kfree(cd);
555 fail:
556 return error;
560 static void get_sectorsize(struct scsi_cd *cd)
562 unsigned char cmd[10];
563 unsigned char *buffer;
564 int the_result, retries = 3;
565 int sector_size;
566 struct scsi_request *SRpnt = NULL;
567 request_queue_t *queue;
569 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
570 if (!buffer)
571 goto Enomem;
572 SRpnt = scsi_allocate_request(cd->device);
573 if (!SRpnt)
574 goto Enomem;
576 do {
577 cmd[0] = READ_CAPACITY;
578 memset((void *) &cmd[1], 0, 9);
579 /* Mark as really busy */
580 SRpnt->sr_request->rq_status = RQ_SCSI_BUSY;
581 SRpnt->sr_cmd_len = 0;
583 memset(buffer, 0, 8);
585 /* Do the command and wait.. */
586 SRpnt->sr_data_direction = SCSI_DATA_READ;
587 scsi_wait_req(SRpnt, (void *) cmd, (void *) buffer,
588 8, SR_TIMEOUT, MAX_RETRIES);
590 the_result = SRpnt->sr_result;
591 retries--;
593 } while (the_result && retries);
596 scsi_release_request(SRpnt);
597 SRpnt = NULL;
599 if (the_result) {
600 cd->capacity = 0x1fffff;
601 sector_size = 2048; /* A guess, just in case */
602 cd->needs_sector_size = 1;
603 } else {
604 #if 0
605 if (cdrom_get_last_written(&cd->cdi,
606 &cd->capacity))
607 #endif
608 cd->capacity = 1 + ((buffer[0] << 24) |
609 (buffer[1] << 16) |
610 (buffer[2] << 8) |
611 buffer[3]);
612 sector_size = (buffer[4] << 24) |
613 (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
614 switch (sector_size) {
616 * HP 4020i CD-Recorder reports 2340 byte sectors
617 * Philips CD-Writers report 2352 byte sectors
619 * Use 2k sectors for them..
621 case 0:
622 case 2340:
623 case 2352:
624 sector_size = 2048;
625 /* fall through */
626 case 2048:
627 cd->capacity *= 4;
628 /* fall through */
629 case 512:
630 break;
631 default:
632 printk("%s: unsupported sector size %d.\n",
633 cd->cdi.name, sector_size);
634 cd->capacity = 0;
635 cd->needs_sector_size = 1;
638 cd->device->sector_size = sector_size;
641 * Add this so that we have the ability to correctly gauge
642 * what the device is capable of.
644 cd->needs_sector_size = 0;
645 set_capacity(cd->disk, cd->capacity);
648 queue = cd->device->request_queue;
649 blk_queue_hardsect_size(queue, sector_size);
650 out:
651 kfree(buffer);
652 return;
654 Enomem:
655 cd->capacity = 0x1fffff;
656 sector_size = 2048; /* A guess, just in case */
657 cd->needs_sector_size = 1;
658 if (SRpnt)
659 scsi_release_request(SRpnt);
660 goto out;
663 static void get_capabilities(struct scsi_cd *cd)
665 unsigned char *buffer;
666 int rc, n;
667 struct scsi_mode_data data;
669 static char *loadmech[] =
671 "caddy",
672 "tray",
673 "pop-up",
675 "changer",
676 "cartridge changer",
681 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
682 if (!buffer) {
683 printk(KERN_ERR "sr: out of memory.\n");
684 return;
686 rc = scsi_mode_sense(cd->device, 0, 0x2a, buffer, 128,
687 SR_TIMEOUT, 3, &data);
689 if (!scsi_status_is_good(rc)) {
690 /* failed, drive doesn't have capabilities mode page */
691 cd->cdi.speed = 1;
692 cd->cdi.mask |= (CDC_CD_R | CDC_CD_RW | CDC_DVD_R |
693 CDC_DVD | CDC_DVD_RAM |
694 CDC_SELECT_DISC | CDC_SELECT_SPEED);
695 kfree(buffer);
696 printk("%s: scsi-1 drive\n", cd->cdi.name);
697 return;
699 n = data.header_length + data.block_descriptor_length;
700 cd->cdi.speed = ((buffer[n + 8] << 8) + buffer[n + 9]) / 176;
701 cd->readcd_known = 1;
702 cd->readcd_cdda = buffer[n + 5] & 0x01;
703 /* print some capability bits */
704 printk("%s: scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n", cd->cdi.name,
705 ((buffer[n + 14] << 8) + buffer[n + 15]) / 176,
706 cd->cdi.speed,
707 buffer[n + 3] & 0x01 ? "writer " : "", /* CD Writer */
708 buffer[n + 3] & 0x20 ? "dvd-ram " : "",
709 buffer[n + 2] & 0x02 ? "cd/rw " : "", /* can read rewriteable */
710 buffer[n + 4] & 0x20 ? "xa/form2 " : "", /* can read xa/from2 */
711 buffer[n + 5] & 0x01 ? "cdda " : "", /* can read audio data */
712 loadmech[buffer[n + 6] >> 5]);
713 if ((buffer[n + 6] >> 5) == 0)
714 /* caddy drives can't close tray... */
715 cd->cdi.mask |= CDC_CLOSE_TRAY;
716 if ((buffer[n + 2] & 0x8) == 0)
717 /* not a DVD drive */
718 cd->cdi.mask |= CDC_DVD;
719 if ((buffer[n + 3] & 0x20) == 0) {
720 /* can't write DVD-RAM media */
721 cd->cdi.mask |= CDC_DVD_RAM;
722 } else {
723 cd->device->writeable = 1;
725 if ((buffer[n + 3] & 0x10) == 0)
726 /* can't write DVD-R media */
727 cd->cdi.mask |= CDC_DVD_R;
728 if ((buffer[n + 3] & 0x2) == 0)
729 /* can't write CD-RW media */
730 cd->cdi.mask |= CDC_CD_RW;
731 if ((buffer[n + 3] & 0x1) == 0)
732 /* can't write CD-R media */
733 cd->cdi.mask |= CDC_CD_R;
734 if ((buffer[n + 6] & 0x8) == 0)
735 /* can't eject */
736 cd->cdi.mask |= CDC_OPEN_TRAY;
738 if ((buffer[n + 6] >> 5) == mechtype_individual_changer ||
739 (buffer[n + 6] >> 5) == mechtype_cartridge_changer)
740 cd->cdi.capacity =
741 cdrom_number_of_slots(&cd->cdi);
742 if (cd->cdi.capacity <= 1)
743 /* not a changer */
744 cd->cdi.mask |= CDC_SELECT_DISC;
745 /*else I don't think it can close its tray
746 cd->cdi.mask |= CDC_CLOSE_TRAY; */
748 kfree(buffer);
752 * sr_packet() is the entry point for the generic commands generated
753 * by the Uniform CD-ROM layer.
755 static int sr_packet(struct cdrom_device_info *cdi,
756 struct cdrom_generic_command *cgc)
758 if (cgc->timeout <= 0)
759 cgc->timeout = IOCTL_TIMEOUT;
761 sr_do_ioctl(cdi->handle, cgc);
763 return cgc->stat;
766 static int sr_remove(struct device *dev)
768 struct scsi_cd *cd = dev_get_drvdata(dev);
770 del_gendisk(cd->disk);
772 spin_lock(&sr_index_lock);
773 clear_bit(cd->disk->first_minor, sr_index_bits);
774 spin_unlock(&sr_index_lock);
776 put_disk(cd->disk);
777 unregister_cdrom(&cd->cdi);
778 kfree(cd);
780 return 0;
783 static int __init init_sr(void)
785 int rc;
787 rc = register_blkdev(SCSI_CDROM_MAJOR, "sr");
788 if (rc)
789 return rc;
790 return scsi_register_driver(&sr_template.gendrv);
793 static void __exit exit_sr(void)
795 scsi_unregister_driver(&sr_template.gendrv);
796 unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
799 module_init(init_sr);
800 module_exit(exit_sr);
801 MODULE_LICENSE("GPL");