[PATCH] v4l: keep tvaudio driver away from saa7146
[linux-2.6/history.git] / drivers / scsi / sr.c
blobda06adf488344c5c10d4061371b038da66eda8bf
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 <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_eh.h>
54 #include <scsi/scsi_host.h>
55 #include <scsi/scsi_ioctl.h> /* For the door lock/unlock commands */
56 #include <scsi/scsi_request.h>
58 #include "scsi_logging.h"
59 #include "sr.h"
62 MODULE_PARM(xa_test, "i"); /* see sr_ioctl.c */
65 #define SR_DISKS 256
67 #define MAX_RETRIES 3
68 #define SR_TIMEOUT (30 * HZ)
69 #define SR_CAPABILITIES \
70 (CDC_CLOSE_TRAY|CDC_OPEN_TRAY|CDC_LOCK|CDC_SELECT_SPEED| \
71 CDC_SELECT_DISC|CDC_MULTI_SESSION|CDC_MCN|CDC_MEDIA_CHANGED| \
72 CDC_PLAY_AUDIO|CDC_RESET|CDC_IOCTLS|CDC_DRIVE_STATUS| \
73 CDC_CD_R|CDC_CD_RW|CDC_DVD|CDC_DVD_R|CDC_DVD_RAM|CDC_GENERIC_PACKET| \
74 CDC_MRW|CDC_MRW_W|CDC_RAM)
76 static int sr_probe(struct device *);
77 static int sr_remove(struct device *);
78 static int sr_init_command(struct scsi_cmnd *);
80 static struct scsi_driver sr_template = {
81 .owner = THIS_MODULE,
82 .gendrv = {
83 .name = "sr",
84 .probe = sr_probe,
85 .remove = sr_remove,
87 .init_command = sr_init_command,
90 static unsigned long sr_index_bits[SR_DISKS / BITS_PER_LONG];
91 static spinlock_t sr_index_lock = SPIN_LOCK_UNLOCKED;
93 /* This semaphore is used to mediate the 0->1 reference get in the
94 * face of object destruction (i.e. we can't allow a get on an
95 * object after last put) */
96 static DECLARE_MUTEX(sr_ref_sem);
98 static int sr_open(struct cdrom_device_info *, int);
99 static void sr_release(struct cdrom_device_info *);
101 static void get_sectorsize(struct scsi_cd *);
102 static void get_capabilities(struct scsi_cd *);
104 static int sr_media_change(struct cdrom_device_info *, int);
105 static int sr_packet(struct cdrom_device_info *, struct packet_command *);
107 static struct cdrom_device_ops sr_dops = {
108 .open = sr_open,
109 .release = sr_release,
110 .drive_status = sr_drive_status,
111 .media_changed = sr_media_change,
112 .tray_move = sr_tray_move,
113 .lock_door = sr_lock_door,
114 .select_speed = sr_select_speed,
115 .get_last_session = sr_get_last_session,
116 .get_mcn = sr_get_mcn,
117 .reset = sr_reset,
118 .audio_ioctl = sr_audio_ioctl,
119 .dev_ioctl = sr_dev_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 down(&sr_ref_sem);
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 up(&sr_ref_sem);
153 return cd;
156 static inline void scsi_cd_put(struct scsi_cd *cd)
158 down(&sr_ref_sem);
159 scsi_device_put(cd->device);
160 kref_put(&cd->kref, sr_kref_release);
161 up(&sr_ref_sem);
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);
204 * If the disk changed, the capacity will now be different,
205 * so we force a re-read of this information
206 * Force 2048 for the sector size so that filesystems won't
207 * be trying to use something that is too small if the disc
208 * has changed.
210 cd->needs_sector_size = 1;
211 cd->device->sector_size = 2048;
213 return retval;
217 * rw_intr is the interrupt routine for the device driver.
219 * It will be notified on the end of a SCSI read / write, and will take on
220 * of several actions based on success or failure.
222 static void rw_intr(struct scsi_cmnd * SCpnt)
224 int result = SCpnt->result;
225 int this_count = SCpnt->bufflen;
226 int good_bytes = (result == 0 ? this_count : 0);
227 int block_sectors = 0;
228 long error_sector;
229 struct scsi_cd *cd = scsi_cd(SCpnt->request->rq_disk);
231 #ifdef DEBUG
232 printk("sr.c done: %x\n", result);
233 #endif
236 * Handle MEDIUM ERRORs or VOLUME OVERFLOWs that indicate partial
237 * success. Since this is a relatively rare error condition, no
238 * care is taken to avoid unnecessary additional work such as
239 * memcpy's that could be avoided.
241 if (driver_byte(result) != 0 && /* An error occurred */
242 (SCpnt->sense_buffer[0] & 0x7f) == 0x70) { /* Sense current */
243 switch (SCpnt->sense_buffer[2]) {
244 case MEDIUM_ERROR:
245 case VOLUME_OVERFLOW:
246 case ILLEGAL_REQUEST:
247 if (!(SCpnt->sense_buffer[0] & 0x90))
248 break;
249 if (!blk_fs_request(SCpnt->request))
250 break;
251 error_sector = (SCpnt->sense_buffer[3] << 24) |
252 (SCpnt->sense_buffer[4] << 16) |
253 (SCpnt->sense_buffer[5] << 8) |
254 SCpnt->sense_buffer[6];
255 if (SCpnt->request->bio != NULL)
256 block_sectors =
257 bio_sectors(SCpnt->request->bio);
258 if (block_sectors < 4)
259 block_sectors = 4;
260 if (cd->device->sector_size == 2048)
261 error_sector <<= 2;
262 error_sector &= ~(block_sectors - 1);
263 good_bytes = (error_sector - SCpnt->request->sector) << 9;
264 if (good_bytes < 0 || good_bytes >= this_count)
265 good_bytes = 0;
267 * The SCSI specification allows for the value
268 * returned by READ CAPACITY to be up to 75 2K
269 * sectors past the last readable block.
270 * Therefore, if we hit a medium error within the
271 * last 75 2K sectors, we decrease the saved size
272 * value.
274 if (error_sector < get_capacity(cd->disk) &&
275 cd->capacity - error_sector < 4 * 75)
276 set_capacity(cd->disk, error_sector);
277 break;
279 case RECOVERED_ERROR:
282 * An error occured, but it recovered. Inform the
283 * user, but make sure that it's not treated as a
284 * hard error.
286 scsi_print_sense("sr", SCpnt);
287 SCpnt->result = 0;
288 SCpnt->sense_buffer[0] = 0x0;
289 good_bytes = this_count;
290 break;
292 default:
293 break;
298 * This calls the generic completion function, now that we know
299 * how many actual sectors finished, and how many sectors we need
300 * to say have failed.
302 scsi_io_completion(SCpnt, good_bytes, block_sectors << 9);
305 static int sr_init_command(struct scsi_cmnd * SCpnt)
307 int block=0, this_count, s_size, timeout = SR_TIMEOUT;
308 struct scsi_cd *cd = scsi_cd(SCpnt->request->rq_disk);
310 SCSI_LOG_HLQUEUE(1, printk("Doing sr request, dev = %s, block = %d\n",
311 cd->disk->disk_name, block));
313 if (!cd->device || !scsi_device_online(cd->device)) {
314 SCSI_LOG_HLQUEUE(2, printk("Finishing %ld sectors\n",
315 SCpnt->request->nr_sectors));
316 SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt));
317 return 0;
320 if (cd->device->changed) {
322 * quietly refuse to do anything to a changed disc until the
323 * changed bit has been reset
325 return 0;
329 * these are already setup, just copy cdb basically
331 if (SCpnt->request->flags & REQ_BLOCK_PC) {
332 struct request *rq = SCpnt->request;
334 if (sizeof(rq->cmd) > sizeof(SCpnt->cmnd))
335 return 0;
337 memcpy(SCpnt->cmnd, rq->cmd, sizeof(SCpnt->cmnd));
338 if (!rq->data_len)
339 SCpnt->sc_data_direction = DMA_NONE;
340 else if (rq_data_dir(rq) == WRITE)
341 SCpnt->sc_data_direction = DMA_TO_DEVICE;
342 else
343 SCpnt->sc_data_direction = DMA_FROM_DEVICE;
345 this_count = rq->data_len;
346 if (rq->timeout)
347 timeout = rq->timeout;
349 SCpnt->transfersize = rq->data_len;
350 goto queue;
353 if (!(SCpnt->request->flags & REQ_CMD)) {
354 blk_dump_rq_flags(SCpnt->request, "sr unsup command");
355 return 0;
359 * we do lazy blocksize switching (when reading XA sectors,
360 * see CDROMREADMODE2 ioctl)
362 s_size = cd->device->sector_size;
363 if (s_size > 2048) {
364 if (!in_interrupt())
365 sr_set_blocklength(cd, 2048);
366 else
367 printk("sr: can't switch blocksize: in interrupt\n");
370 if (s_size != 512 && s_size != 1024 && s_size != 2048) {
371 printk("sr: bad sector size %d\n", s_size);
372 return 0;
375 if (rq_data_dir(SCpnt->request) == WRITE) {
376 if (!cd->device->writeable)
377 return 0;
378 SCpnt->cmnd[0] = WRITE_10;
379 SCpnt->sc_data_direction = DMA_TO_DEVICE;
380 cd->cdi.media_written = 1;
381 } else if (rq_data_dir(SCpnt->request) == READ) {
382 SCpnt->cmnd[0] = READ_10;
383 SCpnt->sc_data_direction = DMA_FROM_DEVICE;
384 } else {
385 blk_dump_rq_flags(SCpnt->request, "Unknown sr command");
386 return 0;
390 struct scatterlist *sg = SCpnt->request_buffer;
391 int i, size = 0;
392 for (i = 0; i < SCpnt->use_sg; i++)
393 size += sg[i].length;
395 if (size != SCpnt->request_bufflen && SCpnt->use_sg) {
396 printk(KERN_ERR "sr: mismatch count %d, bytes %d\n",
397 size, SCpnt->request_bufflen);
398 if (SCpnt->request_bufflen > size)
399 SCpnt->request_bufflen = SCpnt->bufflen = size;
404 * request doesn't start on hw block boundary, add scatter pads
406 if (((unsigned int)SCpnt->request->sector % (s_size >> 9)) ||
407 (SCpnt->request_bufflen % s_size)) {
408 printk("sr: unaligned transfer\n");
409 return 0;
412 this_count = (SCpnt->request_bufflen >> 9) / (s_size >> 9);
415 SCSI_LOG_HLQUEUE(2, printk("%s : %s %d/%ld 512 byte blocks.\n",
416 cd->cdi.name,
417 (rq_data_dir(SCpnt->request) == WRITE) ?
418 "writing" : "reading",
419 this_count, SCpnt->request->nr_sectors));
421 SCpnt->cmnd[1] = 0;
422 block = (unsigned int)SCpnt->request->sector / (s_size >> 9);
424 if (this_count > 0xffff) {
425 this_count = 0xffff;
426 SCpnt->request_bufflen = SCpnt->bufflen =
427 this_count * s_size;
430 SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
431 SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
432 SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
433 SCpnt->cmnd[5] = (unsigned char) block & 0xff;
434 SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
435 SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
436 SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
439 * We shouldn't disconnect in the middle of a sector, so with a dumb
440 * host adapter, it's safe to assume that we can at least transfer
441 * this many bytes between each connect / disconnect.
443 SCpnt->transfersize = cd->device->sector_size;
444 SCpnt->underflow = this_count << 9;
446 queue:
447 SCpnt->allowed = MAX_RETRIES;
448 SCpnt->timeout_per_command = timeout;
451 * This is the completion routine we use. This is matched in terms
452 * of capability to this function.
454 SCpnt->done = rw_intr;
457 * This indicates that the command is ready from our end to be
458 * queued.
460 return 1;
463 static int sr_block_open(struct inode *inode, struct file *file)
465 struct gendisk *disk = inode->i_bdev->bd_disk;
466 struct scsi_cd *cd = scsi_cd(inode->i_bdev->bd_disk);
467 int ret = 0;
469 if(!(cd = scsi_cd_get(disk)))
470 return -ENXIO;
472 if((ret = cdrom_open(&cd->cdi, inode, file)) != 0)
473 scsi_cd_put(cd);
475 return ret;
478 static int sr_block_release(struct inode *inode, struct file *file)
480 int ret;
481 struct scsi_cd *cd = scsi_cd(inode->i_bdev->bd_disk);
482 ret = cdrom_release(&cd->cdi, file);
483 if(ret)
484 return ret;
486 scsi_cd_put(cd);
488 return 0;
491 static int sr_block_ioctl(struct inode *inode, struct file *file, unsigned cmd,
492 unsigned long arg)
494 struct scsi_cd *cd = scsi_cd(inode->i_bdev->bd_disk);
495 struct scsi_device *sdev = cd->device;
498 * Send SCSI addressing ioctls directly to mid level, send other
499 * ioctls to cdrom/block level.
501 switch (cmd) {
502 case SCSI_IOCTL_GET_IDLUN:
503 case SCSI_IOCTL_GET_BUS_NUMBER:
504 return scsi_ioctl(sdev, cmd, (void __user *)arg);
506 return cdrom_ioctl(file, &cd->cdi, inode, cmd, arg);
509 static int sr_block_media_changed(struct gendisk *disk)
511 struct scsi_cd *cd = scsi_cd(disk);
512 return cdrom_media_changed(&cd->cdi);
515 struct block_device_operations sr_bdops =
517 .owner = THIS_MODULE,
518 .open = sr_block_open,
519 .release = sr_block_release,
520 .ioctl = sr_block_ioctl,
521 .media_changed = sr_block_media_changed,
524 static int sr_open(struct cdrom_device_info *cdi, int purpose)
526 struct scsi_cd *cd = cdi->handle;
527 struct scsi_device *sdev = cd->device;
528 int retval;
531 * If the device is in error recovery, wait until it is done.
532 * If the device is offline, then disallow any access to it.
534 retval = -ENXIO;
535 if (!scsi_block_when_processing_errors(sdev))
536 goto error_out;
539 * If this device did not have media in the drive at boot time, then
540 * we would have been unable to get the sector size. Check to see if
541 * this is the case, and try again.
543 if (cd->needs_sector_size)
544 get_sectorsize(cd);
545 return 0;
547 error_out:
548 scsi_cd_put(cd);
549 return retval;
552 static void sr_release(struct cdrom_device_info *cdi)
554 struct scsi_cd *cd = cdi->handle;
556 if (cd->device->sector_size > 2048)
557 sr_set_blocklength(cd, 2048);
561 static int sr_probe(struct device *dev)
563 struct scsi_device *sdev = to_scsi_device(dev);
564 struct gendisk *disk;
565 struct scsi_cd *cd;
566 int minor, error;
568 error = -ENODEV;
569 if (sdev->type != TYPE_ROM && sdev->type != TYPE_WORM)
570 goto fail;
572 error = -ENOMEM;
573 cd = kmalloc(sizeof(*cd), GFP_KERNEL);
574 if (!cd)
575 goto fail;
576 memset(cd, 0, sizeof(*cd));
578 kref_init(&cd->kref);
580 disk = alloc_disk(1);
581 if (!disk)
582 goto fail_free;
584 spin_lock(&sr_index_lock);
585 minor = find_first_zero_bit(sr_index_bits, SR_DISKS);
586 if (minor == SR_DISKS) {
587 spin_unlock(&sr_index_lock);
588 error = -EBUSY;
589 goto fail_put;
591 __set_bit(minor, sr_index_bits);
592 spin_unlock(&sr_index_lock);
594 disk->major = SCSI_CDROM_MAJOR;
595 disk->first_minor = minor;
596 sprintf(disk->disk_name, "sr%d", minor);
597 disk->fops = &sr_bdops;
598 disk->flags = GENHD_FL_CD;
600 cd->device = sdev;
601 cd->disk = disk;
602 cd->driver = &sr_template;
603 cd->disk = disk;
604 cd->capacity = 0x1fffff;
605 cd->needs_sector_size = 1;
606 cd->device->changed = 1; /* force recheck CD type */
607 cd->use = 1;
608 cd->readcd_known = 0;
609 cd->readcd_cdda = 0;
611 cd->cdi.ops = &sr_dops;
612 cd->cdi.handle = cd;
613 cd->cdi.mask = 0;
614 cd->cdi.capacity = 1;
615 sprintf(cd->cdi.name, "sr%d", minor);
617 sdev->sector_size = 2048; /* A guess, just in case */
619 /* FIXME: need to handle a get_capabilities failure properly ?? */
620 get_capabilities(cd);
621 sr_vendor_init(cd);
623 snprintf(disk->devfs_name, sizeof(disk->devfs_name),
624 "%s/cd", sdev->devfs_name);
625 disk->driverfs_dev = &sdev->sdev_gendev;
626 set_capacity(disk, cd->capacity);
627 disk->private_data = &cd->driver;
628 disk->queue = sdev->request_queue;
629 cd->cdi.disk = disk;
631 if (register_cdrom(&cd->cdi))
632 goto fail_put;
634 dev_set_drvdata(dev, cd);
635 disk->flags |= GENHD_FL_REMOVABLE;
636 add_disk(disk);
638 printk(KERN_DEBUG
639 "Attached scsi CD-ROM %s at scsi%d, channel %d, id %d, lun %d\n",
640 cd->cdi.name, sdev->host->host_no, sdev->channel,
641 sdev->id, sdev->lun);
642 return 0;
644 fail_put:
645 put_disk(disk);
646 fail_free:
647 kfree(cd);
648 fail:
649 return error;
653 static void get_sectorsize(struct scsi_cd *cd)
655 unsigned char cmd[10];
656 unsigned char *buffer;
657 int the_result, retries = 3;
658 int sector_size;
659 struct scsi_request *SRpnt = NULL;
660 request_queue_t *queue;
662 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
663 if (!buffer)
664 goto Enomem;
665 SRpnt = scsi_allocate_request(cd->device, GFP_KERNEL);
666 if (!SRpnt)
667 goto Enomem;
669 do {
670 cmd[0] = READ_CAPACITY;
671 memset((void *) &cmd[1], 0, 9);
672 /* Mark as really busy */
673 SRpnt->sr_request->rq_status = RQ_SCSI_BUSY;
674 SRpnt->sr_cmd_len = 0;
676 memset(buffer, 0, 8);
678 /* Do the command and wait.. */
679 SRpnt->sr_data_direction = DMA_FROM_DEVICE;
680 scsi_wait_req(SRpnt, (void *) cmd, (void *) buffer,
681 8, SR_TIMEOUT, MAX_RETRIES);
683 the_result = SRpnt->sr_result;
684 retries--;
686 } while (the_result && retries);
689 scsi_release_request(SRpnt);
690 SRpnt = NULL;
692 if (the_result) {
693 cd->capacity = 0x1fffff;
694 sector_size = 2048; /* A guess, just in case */
695 cd->needs_sector_size = 1;
696 } else {
697 #if 0
698 if (cdrom_get_last_written(&cd->cdi,
699 &cd->capacity))
700 #endif
701 cd->capacity = 1 + ((buffer[0] << 24) |
702 (buffer[1] << 16) |
703 (buffer[2] << 8) |
704 buffer[3]);
705 sector_size = (buffer[4] << 24) |
706 (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
707 switch (sector_size) {
709 * HP 4020i CD-Recorder reports 2340 byte sectors
710 * Philips CD-Writers report 2352 byte sectors
712 * Use 2k sectors for them..
714 case 0:
715 case 2340:
716 case 2352:
717 sector_size = 2048;
718 /* fall through */
719 case 2048:
720 cd->capacity *= 4;
721 /* fall through */
722 case 512:
723 break;
724 default:
725 printk("%s: unsupported sector size %d.\n",
726 cd->cdi.name, sector_size);
727 cd->capacity = 0;
728 cd->needs_sector_size = 1;
731 cd->device->sector_size = sector_size;
734 * Add this so that we have the ability to correctly gauge
735 * what the device is capable of.
737 cd->needs_sector_size = 0;
738 set_capacity(cd->disk, cd->capacity);
741 queue = cd->device->request_queue;
742 blk_queue_hardsect_size(queue, sector_size);
743 out:
744 kfree(buffer);
745 return;
747 Enomem:
748 cd->capacity = 0x1fffff;
749 sector_size = 2048; /* A guess, just in case */
750 cd->needs_sector_size = 1;
751 if (SRpnt)
752 scsi_release_request(SRpnt);
753 goto out;
756 static void get_capabilities(struct scsi_cd *cd)
758 unsigned char *buffer;
759 struct scsi_mode_data data;
760 struct scsi_request *SRpnt;
761 unsigned char cmd[MAX_COMMAND_SIZE];
762 unsigned int the_result;
763 int retries, rc, n;
765 static char *loadmech[] =
767 "caddy",
768 "tray",
769 "pop-up",
771 "changer",
772 "cartridge changer",
777 /* allocate a request for the TEST_UNIT_READY */
778 SRpnt = scsi_allocate_request(cd->device, GFP_KERNEL);
779 if (!SRpnt) {
780 printk(KERN_WARNING "(get_capabilities:) Request allocation "
781 "failure.\n");
782 return;
785 /* allocate transfer buffer */
786 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
787 if (!buffer) {
788 printk(KERN_ERR "sr: out of memory.\n");
789 scsi_release_request(SRpnt);
790 return;
793 /* issue TEST_UNIT_READY until the initial startup UNIT_ATTENTION
794 * conditions are gone, or a timeout happens
796 retries = 0;
797 do {
798 memset((void *)cmd, 0, MAX_COMMAND_SIZE);
799 cmd[0] = TEST_UNIT_READY;
801 SRpnt->sr_cmd_len = 0;
802 SRpnt->sr_sense_buffer[0] = 0;
803 SRpnt->sr_sense_buffer[2] = 0;
804 SRpnt->sr_data_direction = DMA_NONE;
806 scsi_wait_req (SRpnt, (void *) cmd, buffer,
807 0, SR_TIMEOUT, MAX_RETRIES);
809 the_result = SRpnt->sr_result;
810 retries++;
811 } while (retries < 5 &&
812 (!scsi_status_is_good(the_result) ||
813 ((driver_byte(the_result) & DRIVER_SENSE) &&
814 SRpnt->sr_sense_buffer[2] == UNIT_ATTENTION)));
816 /* ask for mode page 0x2a */
817 rc = scsi_mode_sense(cd->device, 0, 0x2a, buffer, 128,
818 SR_TIMEOUT, 3, &data);
820 if (!scsi_status_is_good(rc)) {
821 /* failed, drive doesn't have capabilities mode page */
822 cd->cdi.speed = 1;
823 cd->cdi.mask |= (CDC_CD_R | CDC_CD_RW | CDC_DVD_R |
824 CDC_DVD | CDC_DVD_RAM |
825 CDC_SELECT_DISC | CDC_SELECT_SPEED);
826 scsi_release_request(SRpnt);
827 kfree(buffer);
828 printk("%s: scsi-1 drive\n", cd->cdi.name);
829 return;
832 n = data.header_length + data.block_descriptor_length;
833 cd->cdi.speed = ((buffer[n + 8] << 8) + buffer[n + 9]) / 176;
834 cd->readcd_known = 1;
835 cd->readcd_cdda = buffer[n + 5] & 0x01;
836 /* print some capability bits */
837 printk("%s: scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n", cd->cdi.name,
838 ((buffer[n + 14] << 8) + buffer[n + 15]) / 176,
839 cd->cdi.speed,
840 buffer[n + 3] & 0x01 ? "writer " : "", /* CD Writer */
841 buffer[n + 3] & 0x20 ? "dvd-ram " : "",
842 buffer[n + 2] & 0x02 ? "cd/rw " : "", /* can read rewriteable */
843 buffer[n + 4] & 0x20 ? "xa/form2 " : "", /* can read xa/from2 */
844 buffer[n + 5] & 0x01 ? "cdda " : "", /* can read audio data */
845 loadmech[buffer[n + 6] >> 5]);
846 if ((buffer[n + 6] >> 5) == 0)
847 /* caddy drives can't close tray... */
848 cd->cdi.mask |= CDC_CLOSE_TRAY;
849 if ((buffer[n + 2] & 0x8) == 0)
850 /* not a DVD drive */
851 cd->cdi.mask |= CDC_DVD;
852 if ((buffer[n + 3] & 0x20) == 0)
853 /* can't write DVD-RAM media */
854 cd->cdi.mask |= CDC_DVD_RAM;
855 if ((buffer[n + 3] & 0x10) == 0)
856 /* can't write DVD-R media */
857 cd->cdi.mask |= CDC_DVD_R;
858 if ((buffer[n + 3] & 0x2) == 0)
859 /* can't write CD-RW media */
860 cd->cdi.mask |= CDC_CD_RW;
861 if ((buffer[n + 3] & 0x1) == 0)
862 /* can't write CD-R media */
863 cd->cdi.mask |= CDC_CD_R;
864 if ((buffer[n + 6] & 0x8) == 0)
865 /* can't eject */
866 cd->cdi.mask |= CDC_OPEN_TRAY;
868 if ((buffer[n + 6] >> 5) == mechtype_individual_changer ||
869 (buffer[n + 6] >> 5) == mechtype_cartridge_changer)
870 cd->cdi.capacity =
871 cdrom_number_of_slots(&cd->cdi);
872 if (cd->cdi.capacity <= 1)
873 /* not a changer */
874 cd->cdi.mask |= CDC_SELECT_DISC;
875 /*else I don't think it can close its tray
876 cd->cdi.mask |= CDC_CLOSE_TRAY; */
879 * if DVD-RAM, MRW-W or CD-RW, we are randomly writable
881 if ((cd->cdi.mask & (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) !=
882 (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) {
883 cd->device->writeable = 1;
886 scsi_release_request(SRpnt);
887 kfree(buffer);
891 * sr_packet() is the entry point for the generic commands generated
892 * by the Uniform CD-ROM layer.
894 static int sr_packet(struct cdrom_device_info *cdi,
895 struct packet_command *cgc)
897 if (cgc->timeout <= 0)
898 cgc->timeout = IOCTL_TIMEOUT;
900 sr_do_ioctl(cdi->handle, cgc);
902 return cgc->stat;
906 * sr_kref_release - Called to free the scsi_cd structure
907 * @kref: pointer to embedded kref
909 * sr_ref_sem must be held entering this routine. Because it is
910 * called on last put, you should always use the scsi_cd_get()
911 * scsi_cd_put() helpers which manipulate the semaphore directly
912 * and never do a direct kref_put().
914 static void sr_kref_release(struct kref *kref)
916 struct scsi_cd *cd = container_of(kref, struct scsi_cd, kref);
917 struct gendisk *disk = cd->disk;
919 spin_lock(&sr_index_lock);
920 clear_bit(disk->first_minor, sr_index_bits);
921 spin_unlock(&sr_index_lock);
923 unregister_cdrom(&cd->cdi);
925 disk->private_data = NULL;
927 put_disk(disk);
929 kfree(cd);
932 static int sr_remove(struct device *dev)
934 struct scsi_cd *cd = dev_get_drvdata(dev);
936 del_gendisk(cd->disk);
938 down(&sr_ref_sem);
939 kref_put(&cd->kref, sr_kref_release);
940 up(&sr_ref_sem);
942 return 0;
945 static int __init init_sr(void)
947 int rc;
949 rc = register_blkdev(SCSI_CDROM_MAJOR, "sr");
950 if (rc)
951 return rc;
952 return scsi_register_driver(&sr_template.gendrv);
955 static void __exit exit_sr(void)
957 scsi_unregister_driver(&sr_template.gendrv);
958 unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
961 module_init(init_sr);
962 module_exit(exit_sr);
963 MODULE_LICENSE("GPL");