[TG3]: Set minimal hw interrupt mitigation.
[linux-2.6/verdex.git] / drivers / scsi / sr.c
blob2f259f2495229b54a1572492bfe3cc818d0dfbf7
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 #define SR_DISKS 256
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| \
71 CDC_MRW|CDC_MRW_W|CDC_RAM)
73 static int sr_probe(struct device *);
74 static int sr_remove(struct device *);
75 static int sr_init_command(struct scsi_cmnd *);
77 static struct scsi_driver sr_template = {
78 .owner = THIS_MODULE,
79 .gendrv = {
80 .name = "sr",
81 .probe = sr_probe,
82 .remove = sr_remove,
84 .init_command = sr_init_command,
87 static unsigned long sr_index_bits[SR_DISKS / BITS_PER_LONG];
88 static DEFINE_SPINLOCK(sr_index_lock);
90 /* This semaphore is used to mediate the 0->1 reference get in the
91 * face of object destruction (i.e. we can't allow a get on an
92 * object after last put) */
93 static DECLARE_MUTEX(sr_ref_sem);
95 static int sr_open(struct cdrom_device_info *, int);
96 static void sr_release(struct cdrom_device_info *);
98 static void get_sectorsize(struct scsi_cd *);
99 static void get_capabilities(struct scsi_cd *);
101 static int sr_media_change(struct cdrom_device_info *, int);
102 static int sr_packet(struct cdrom_device_info *, struct packet_command *);
104 static struct cdrom_device_ops sr_dops = {
105 .open = sr_open,
106 .release = sr_release,
107 .drive_status = sr_drive_status,
108 .media_changed = sr_media_change,
109 .tray_move = sr_tray_move,
110 .lock_door = sr_lock_door,
111 .select_speed = sr_select_speed,
112 .get_last_session = sr_get_last_session,
113 .get_mcn = sr_get_mcn,
114 .reset = sr_reset,
115 .audio_ioctl = sr_audio_ioctl,
116 .dev_ioctl = sr_dev_ioctl,
117 .capability = SR_CAPABILITIES,
118 .generic_packet = sr_packet,
121 static void sr_kref_release(struct kref *kref);
123 static inline struct scsi_cd *scsi_cd(struct gendisk *disk)
125 return container_of(disk->private_data, struct scsi_cd, driver);
129 * The get and put routines for the struct scsi_cd. Note this entity
130 * has a scsi_device pointer and owns a reference to this.
132 static inline struct scsi_cd *scsi_cd_get(struct gendisk *disk)
134 struct scsi_cd *cd = NULL;
136 down(&sr_ref_sem);
137 if (disk->private_data == NULL)
138 goto out;
139 cd = scsi_cd(disk);
140 kref_get(&cd->kref);
141 if (scsi_device_get(cd->device))
142 goto out_put;
143 goto out;
145 out_put:
146 kref_put(&cd->kref, sr_kref_release);
147 cd = NULL;
148 out:
149 up(&sr_ref_sem);
150 return cd;
153 static inline void scsi_cd_put(struct scsi_cd *cd)
155 struct scsi_device *sdev = cd->device;
157 down(&sr_ref_sem);
158 kref_put(&cd->kref, sr_kref_release);
159 scsi_device_put(sdev);
160 up(&sr_ref_sem);
164 * This function checks to see if the media has been changed in the
165 * CDROM drive. It is possible that we have already sensed a change,
166 * or the drive may have sensed one and not yet reported it. We must
167 * be ready for either case. This function always reports the current
168 * value of the changed bit. If flag is 0, then the changed bit is reset.
169 * This function could be done as an ioctl, but we would need to have
170 * an inode for that to work, and we do not always have one.
173 int sr_media_change(struct cdrom_device_info *cdi, int slot)
175 struct scsi_cd *cd = cdi->handle;
176 int retval;
178 if (CDSL_CURRENT != slot) {
179 /* no changer support */
180 return -EINVAL;
183 retval = scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES);
184 if (retval) {
185 /* Unable to test, unit probably not ready. This usually
186 * means there is no disc in the drive. Mark as changed,
187 * and we will figure it out later once the drive is
188 * available again. */
189 cd->device->changed = 1;
190 return 1; /* This will force a flush, if called from
191 * check_disk_change */
194 retval = cd->device->changed;
195 cd->device->changed = 0;
196 /* If the disk changed, the capacity will now be different,
197 * so we force a re-read of this information */
198 if (retval) {
199 /* check multisession offset etc */
200 sr_cd_check(cdi);
203 * If the disk changed, the capacity will now be different,
204 * so we force a re-read of this information
205 * Force 2048 for the sector size so that filesystems won't
206 * be trying to use something that is too small if the disc
207 * has changed.
209 cd->needs_sector_size = 1;
210 cd->device->sector_size = 2048;
212 return retval;
216 * rw_intr is the interrupt routine for the device driver.
218 * It will be notified on the end of a SCSI read / write, and will take on
219 * of several actions based on success or failure.
221 static void rw_intr(struct scsi_cmnd * SCpnt)
223 int result = SCpnt->result;
224 int this_count = SCpnt->bufflen;
225 int good_bytes = (result == 0 ? this_count : 0);
226 int block_sectors = 0;
227 long error_sector;
228 struct scsi_cd *cd = scsi_cd(SCpnt->request->rq_disk);
230 #ifdef DEBUG
231 printk("sr.c done: %x\n", result);
232 #endif
235 * Handle MEDIUM ERRORs or VOLUME OVERFLOWs that indicate partial
236 * success. Since this is a relatively rare error condition, no
237 * care is taken to avoid unnecessary additional work such as
238 * memcpy's that could be avoided.
240 if (driver_byte(result) != 0 && /* An error occurred */
241 (SCpnt->sense_buffer[0] & 0x7f) == 0x70) { /* Sense current */
242 switch (SCpnt->sense_buffer[2]) {
243 case MEDIUM_ERROR:
244 case VOLUME_OVERFLOW:
245 case ILLEGAL_REQUEST:
246 if (!(SCpnt->sense_buffer[0] & 0x90))
247 break;
248 if (!blk_fs_request(SCpnt->request))
249 break;
250 error_sector = (SCpnt->sense_buffer[3] << 24) |
251 (SCpnt->sense_buffer[4] << 16) |
252 (SCpnt->sense_buffer[5] << 8) |
253 SCpnt->sense_buffer[6];
254 if (SCpnt->request->bio != NULL)
255 block_sectors =
256 bio_sectors(SCpnt->request->bio);
257 if (block_sectors < 4)
258 block_sectors = 4;
259 if (cd->device->sector_size == 2048)
260 error_sector <<= 2;
261 error_sector &= ~(block_sectors - 1);
262 good_bytes = (error_sector - SCpnt->request->sector) << 9;
263 if (good_bytes < 0 || good_bytes >= this_count)
264 good_bytes = 0;
266 * The SCSI specification allows for the value
267 * returned by READ CAPACITY to be up to 75 2K
268 * sectors past the last readable block.
269 * Therefore, if we hit a medium error within the
270 * last 75 2K sectors, we decrease the saved size
271 * value.
273 if (error_sector < get_capacity(cd->disk) &&
274 cd->capacity - error_sector < 4 * 75)
275 set_capacity(cd->disk, error_sector);
276 break;
278 case RECOVERED_ERROR:
281 * An error occured, but it recovered. Inform the
282 * user, but make sure that it's not treated as a
283 * hard error.
285 scsi_print_sense("sr", SCpnt);
286 SCpnt->result = 0;
287 SCpnt->sense_buffer[0] = 0x0;
288 good_bytes = this_count;
289 break;
291 default:
292 break;
297 * This calls the generic completion function, now that we know
298 * how many actual sectors finished, and how many sectors we need
299 * to say have failed.
301 scsi_io_completion(SCpnt, good_bytes, block_sectors << 9);
304 static int sr_init_command(struct scsi_cmnd * SCpnt)
306 int block=0, this_count, s_size, timeout = SR_TIMEOUT;
307 struct scsi_cd *cd = scsi_cd(SCpnt->request->rq_disk);
309 SCSI_LOG_HLQUEUE(1, printk("Doing sr request, dev = %s, block = %d\n",
310 cd->disk->disk_name, block));
312 if (!cd->device || !scsi_device_online(cd->device)) {
313 SCSI_LOG_HLQUEUE(2, printk("Finishing %ld sectors\n",
314 SCpnt->request->nr_sectors));
315 SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt));
316 return 0;
319 if (cd->device->changed) {
321 * quietly refuse to do anything to a changed disc until the
322 * changed bit has been reset
324 return 0;
328 * these are already setup, just copy cdb basically
330 if (SCpnt->request->flags & REQ_BLOCK_PC) {
331 struct request *rq = SCpnt->request;
333 if (sizeof(rq->cmd) > sizeof(SCpnt->cmnd))
334 return 0;
336 memcpy(SCpnt->cmnd, rq->cmd, sizeof(SCpnt->cmnd));
337 if (!rq->data_len)
338 SCpnt->sc_data_direction = DMA_NONE;
339 else if (rq_data_dir(rq) == WRITE)
340 SCpnt->sc_data_direction = DMA_TO_DEVICE;
341 else
342 SCpnt->sc_data_direction = DMA_FROM_DEVICE;
344 this_count = rq->data_len;
345 if (rq->timeout)
346 timeout = rq->timeout;
348 SCpnt->transfersize = rq->data_len;
349 goto queue;
352 if (!(SCpnt->request->flags & REQ_CMD)) {
353 blk_dump_rq_flags(SCpnt->request, "sr unsup command");
354 return 0;
358 * we do lazy blocksize switching (when reading XA sectors,
359 * see CDROMREADMODE2 ioctl)
361 s_size = cd->device->sector_size;
362 if (s_size > 2048) {
363 if (!in_interrupt())
364 sr_set_blocklength(cd, 2048);
365 else
366 printk("sr: can't switch blocksize: in interrupt\n");
369 if (s_size != 512 && s_size != 1024 && s_size != 2048) {
370 printk("sr: bad sector size %d\n", s_size);
371 return 0;
374 if (rq_data_dir(SCpnt->request) == WRITE) {
375 if (!cd->device->writeable)
376 return 0;
377 SCpnt->cmnd[0] = WRITE_10;
378 SCpnt->sc_data_direction = DMA_TO_DEVICE;
379 cd->cdi.media_written = 1;
380 } else if (rq_data_dir(SCpnt->request) == READ) {
381 SCpnt->cmnd[0] = READ_10;
382 SCpnt->sc_data_direction = DMA_FROM_DEVICE;
383 } else {
384 blk_dump_rq_flags(SCpnt->request, "Unknown sr command");
385 return 0;
389 struct scatterlist *sg = SCpnt->request_buffer;
390 int i, size = 0;
391 for (i = 0; i < SCpnt->use_sg; i++)
392 size += sg[i].length;
394 if (size != SCpnt->request_bufflen && SCpnt->use_sg) {
395 printk(KERN_ERR "sr: mismatch count %d, bytes %d\n",
396 size, SCpnt->request_bufflen);
397 if (SCpnt->request_bufflen > size)
398 SCpnt->request_bufflen = SCpnt->bufflen = size;
403 * request doesn't start on hw block boundary, add scatter pads
405 if (((unsigned int)SCpnt->request->sector % (s_size >> 9)) ||
406 (SCpnt->request_bufflen % s_size)) {
407 printk("sr: unaligned transfer\n");
408 return 0;
411 this_count = (SCpnt->request_bufflen >> 9) / (s_size >> 9);
414 SCSI_LOG_HLQUEUE(2, printk("%s : %s %d/%ld 512 byte blocks.\n",
415 cd->cdi.name,
416 (rq_data_dir(SCpnt->request) == WRITE) ?
417 "writing" : "reading",
418 this_count, SCpnt->request->nr_sectors));
420 SCpnt->cmnd[1] = 0;
421 block = (unsigned int)SCpnt->request->sector / (s_size >> 9);
423 if (this_count > 0xffff) {
424 this_count = 0xffff;
425 SCpnt->request_bufflen = SCpnt->bufflen =
426 this_count * s_size;
429 SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
430 SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
431 SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
432 SCpnt->cmnd[5] = (unsigned char) block & 0xff;
433 SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
434 SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
435 SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
438 * We shouldn't disconnect in the middle of a sector, so with a dumb
439 * host adapter, it's safe to assume that we can at least transfer
440 * this many bytes between each connect / disconnect.
442 SCpnt->transfersize = cd->device->sector_size;
443 SCpnt->underflow = this_count << 9;
445 queue:
446 SCpnt->allowed = MAX_RETRIES;
447 SCpnt->timeout_per_command = timeout;
450 * This is the completion routine we use. This is matched in terms
451 * of capability to this function.
453 SCpnt->done = rw_intr;
456 * This indicates that the command is ready from our end to be
457 * queued.
459 return 1;
462 static int sr_block_open(struct inode *inode, struct file *file)
464 struct gendisk *disk = inode->i_bdev->bd_disk;
465 struct scsi_cd *cd = scsi_cd(inode->i_bdev->bd_disk);
466 int ret = 0;
468 if(!(cd = scsi_cd_get(disk)))
469 return -ENXIO;
471 if((ret = cdrom_open(&cd->cdi, inode, file)) != 0)
472 scsi_cd_put(cd);
474 return ret;
477 static int sr_block_release(struct inode *inode, struct file *file)
479 int ret;
480 struct scsi_cd *cd = scsi_cd(inode->i_bdev->bd_disk);
481 ret = cdrom_release(&cd->cdi, file);
482 if(ret)
483 return ret;
485 scsi_cd_put(cd);
487 return 0;
490 static int sr_block_ioctl(struct inode *inode, struct file *file, unsigned cmd,
491 unsigned long arg)
493 struct scsi_cd *cd = scsi_cd(inode->i_bdev->bd_disk);
494 struct scsi_device *sdev = cd->device;
497 * Send SCSI addressing ioctls directly to mid level, send other
498 * ioctls to cdrom/block level.
500 switch (cmd) {
501 case SCSI_IOCTL_GET_IDLUN:
502 case SCSI_IOCTL_GET_BUS_NUMBER:
503 return scsi_ioctl(sdev, cmd, (void __user *)arg);
505 return cdrom_ioctl(file, &cd->cdi, inode, cmd, arg);
508 static int sr_block_media_changed(struct gendisk *disk)
510 struct scsi_cd *cd = scsi_cd(disk);
511 return cdrom_media_changed(&cd->cdi);
514 static struct block_device_operations sr_bdops =
516 .owner = THIS_MODULE,
517 .open = sr_block_open,
518 .release = sr_block_release,
519 .ioctl = sr_block_ioctl,
520 .media_changed = sr_block_media_changed,
522 * No compat_ioctl for now because sr_block_ioctl never
523 * seems to pass arbitary ioctls down to host drivers.
527 static int sr_open(struct cdrom_device_info *cdi, int purpose)
529 struct scsi_cd *cd = cdi->handle;
530 struct scsi_device *sdev = cd->device;
531 int retval;
534 * If the device is in error recovery, wait until it is done.
535 * If the device is offline, then disallow any access to it.
537 retval = -ENXIO;
538 if (!scsi_block_when_processing_errors(sdev))
539 goto error_out;
542 * If this device did not have media in the drive at boot time, then
543 * we would have been unable to get the sector size. Check to see if
544 * this is the case, and try again.
546 if (cd->needs_sector_size)
547 get_sectorsize(cd);
548 return 0;
550 error_out:
551 return retval;
554 static void sr_release(struct cdrom_device_info *cdi)
556 struct scsi_cd *cd = cdi->handle;
558 if (cd->device->sector_size > 2048)
559 sr_set_blocklength(cd, 2048);
563 static int sr_probe(struct device *dev)
565 struct scsi_device *sdev = to_scsi_device(dev);
566 struct gendisk *disk;
567 struct scsi_cd *cd;
568 int minor, error;
570 error = -ENODEV;
571 if (sdev->type != TYPE_ROM && sdev->type != TYPE_WORM)
572 goto fail;
574 error = -ENOMEM;
575 cd = kmalloc(sizeof(*cd), GFP_KERNEL);
576 if (!cd)
577 goto fail;
578 memset(cd, 0, sizeof(*cd));
580 kref_init(&cd->kref);
582 disk = alloc_disk(1);
583 if (!disk)
584 goto fail_free;
586 spin_lock(&sr_index_lock);
587 minor = find_first_zero_bit(sr_index_bits, SR_DISKS);
588 if (minor == SR_DISKS) {
589 spin_unlock(&sr_index_lock);
590 error = -EBUSY;
591 goto fail_put;
593 __set_bit(minor, sr_index_bits);
594 spin_unlock(&sr_index_lock);
596 disk->major = SCSI_CDROM_MAJOR;
597 disk->first_minor = minor;
598 sprintf(disk->disk_name, "sr%d", minor);
599 disk->fops = &sr_bdops;
600 disk->flags = GENHD_FL_CD;
602 cd->device = sdev;
603 cd->disk = disk;
604 cd->driver = &sr_template;
605 cd->disk = disk;
606 cd->capacity = 0x1fffff;
607 cd->needs_sector_size = 1;
608 cd->device->changed = 1; /* force recheck CD type */
609 cd->use = 1;
610 cd->readcd_known = 0;
611 cd->readcd_cdda = 0;
613 cd->cdi.ops = &sr_dops;
614 cd->cdi.handle = cd;
615 cd->cdi.mask = 0;
616 cd->cdi.capacity = 1;
617 sprintf(cd->cdi.name, "sr%d", minor);
619 sdev->sector_size = 2048; /* A guess, just in case */
621 /* FIXME: need to handle a get_capabilities failure properly ?? */
622 get_capabilities(cd);
623 sr_vendor_init(cd);
625 snprintf(disk->devfs_name, sizeof(disk->devfs_name),
626 "%s/cd", sdev->devfs_name);
627 disk->driverfs_dev = &sdev->sdev_gendev;
628 set_capacity(disk, cd->capacity);
629 disk->private_data = &cd->driver;
630 disk->queue = sdev->request_queue;
631 cd->cdi.disk = disk;
633 if (register_cdrom(&cd->cdi))
634 goto fail_put;
636 dev_set_drvdata(dev, cd);
637 disk->flags |= GENHD_FL_REMOVABLE;
638 add_disk(disk);
640 printk(KERN_DEBUG
641 "Attached scsi CD-ROM %s at scsi%d, channel %d, id %d, lun %d\n",
642 cd->cdi.name, sdev->host->host_no, sdev->channel,
643 sdev->id, sdev->lun);
644 return 0;
646 fail_put:
647 put_disk(disk);
648 fail_free:
649 kfree(cd);
650 fail:
651 return error;
655 static void get_sectorsize(struct scsi_cd *cd)
657 unsigned char cmd[10];
658 unsigned char *buffer;
659 int the_result, retries = 3;
660 int sector_size;
661 struct scsi_request *SRpnt = NULL;
662 request_queue_t *queue;
664 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
665 if (!buffer)
666 goto Enomem;
667 SRpnt = scsi_allocate_request(cd->device, GFP_KERNEL);
668 if (!SRpnt)
669 goto Enomem;
671 do {
672 cmd[0] = READ_CAPACITY;
673 memset((void *) &cmd[1], 0, 9);
674 /* Mark as really busy */
675 SRpnt->sr_request->rq_status = RQ_SCSI_BUSY;
676 SRpnt->sr_cmd_len = 0;
678 memset(buffer, 0, 8);
680 /* Do the command and wait.. */
681 SRpnt->sr_data_direction = DMA_FROM_DEVICE;
682 scsi_wait_req(SRpnt, (void *) cmd, (void *) buffer,
683 8, SR_TIMEOUT, MAX_RETRIES);
685 the_result = SRpnt->sr_result;
686 retries--;
688 } while (the_result && retries);
691 scsi_release_request(SRpnt);
692 SRpnt = NULL;
694 if (the_result) {
695 cd->capacity = 0x1fffff;
696 sector_size = 2048; /* A guess, just in case */
697 cd->needs_sector_size = 1;
698 } else {
699 #if 0
700 if (cdrom_get_last_written(&cd->cdi,
701 &cd->capacity))
702 #endif
703 cd->capacity = 1 + ((buffer[0] << 24) |
704 (buffer[1] << 16) |
705 (buffer[2] << 8) |
706 buffer[3]);
707 sector_size = (buffer[4] << 24) |
708 (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
709 switch (sector_size) {
711 * HP 4020i CD-Recorder reports 2340 byte sectors
712 * Philips CD-Writers report 2352 byte sectors
714 * Use 2k sectors for them..
716 case 0:
717 case 2340:
718 case 2352:
719 sector_size = 2048;
720 /* fall through */
721 case 2048:
722 cd->capacity *= 4;
723 /* fall through */
724 case 512:
725 break;
726 default:
727 printk("%s: unsupported sector size %d.\n",
728 cd->cdi.name, sector_size);
729 cd->capacity = 0;
730 cd->needs_sector_size = 1;
733 cd->device->sector_size = sector_size;
736 * Add this so that we have the ability to correctly gauge
737 * what the device is capable of.
739 cd->needs_sector_size = 0;
740 set_capacity(cd->disk, cd->capacity);
743 queue = cd->device->request_queue;
744 blk_queue_hardsect_size(queue, sector_size);
745 out:
746 kfree(buffer);
747 return;
749 Enomem:
750 cd->capacity = 0x1fffff;
751 sector_size = 2048; /* A guess, just in case */
752 cd->needs_sector_size = 1;
753 if (SRpnt)
754 scsi_release_request(SRpnt);
755 goto out;
758 static void get_capabilities(struct scsi_cd *cd)
760 unsigned char *buffer;
761 struct scsi_mode_data data;
762 struct scsi_request *SRpnt;
763 unsigned char cmd[MAX_COMMAND_SIZE];
764 unsigned int the_result;
765 int retries, rc, n;
767 static char *loadmech[] =
769 "caddy",
770 "tray",
771 "pop-up",
773 "changer",
774 "cartridge changer",
779 /* allocate a request for the TEST_UNIT_READY */
780 SRpnt = scsi_allocate_request(cd->device, GFP_KERNEL);
781 if (!SRpnt) {
782 printk(KERN_WARNING "(get_capabilities:) Request allocation "
783 "failure.\n");
784 return;
787 /* allocate transfer buffer */
788 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
789 if (!buffer) {
790 printk(KERN_ERR "sr: out of memory.\n");
791 scsi_release_request(SRpnt);
792 return;
795 /* issue TEST_UNIT_READY until the initial startup UNIT_ATTENTION
796 * conditions are gone, or a timeout happens
798 retries = 0;
799 do {
800 memset((void *)cmd, 0, MAX_COMMAND_SIZE);
801 cmd[0] = TEST_UNIT_READY;
803 SRpnt->sr_cmd_len = 0;
804 SRpnt->sr_sense_buffer[0] = 0;
805 SRpnt->sr_sense_buffer[2] = 0;
806 SRpnt->sr_data_direction = DMA_NONE;
808 scsi_wait_req (SRpnt, (void *) cmd, buffer,
809 0, SR_TIMEOUT, MAX_RETRIES);
811 the_result = SRpnt->sr_result;
812 retries++;
813 } while (retries < 5 &&
814 (!scsi_status_is_good(the_result) ||
815 ((driver_byte(the_result) & DRIVER_SENSE) &&
816 SRpnt->sr_sense_buffer[2] == UNIT_ATTENTION)));
818 /* ask for mode page 0x2a */
819 rc = scsi_mode_sense(cd->device, 0, 0x2a, buffer, 128,
820 SR_TIMEOUT, 3, &data);
822 if (!scsi_status_is_good(rc)) {
823 /* failed, drive doesn't have capabilities mode page */
824 cd->cdi.speed = 1;
825 cd->cdi.mask |= (CDC_CD_R | CDC_CD_RW | CDC_DVD_R |
826 CDC_DVD | CDC_DVD_RAM |
827 CDC_SELECT_DISC | CDC_SELECT_SPEED);
828 scsi_release_request(SRpnt);
829 kfree(buffer);
830 printk("%s: scsi-1 drive\n", cd->cdi.name);
831 return;
834 n = data.header_length + data.block_descriptor_length;
835 cd->cdi.speed = ((buffer[n + 8] << 8) + buffer[n + 9]) / 176;
836 cd->readcd_known = 1;
837 cd->readcd_cdda = buffer[n + 5] & 0x01;
838 /* print some capability bits */
839 printk("%s: scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n", cd->cdi.name,
840 ((buffer[n + 14] << 8) + buffer[n + 15]) / 176,
841 cd->cdi.speed,
842 buffer[n + 3] & 0x01 ? "writer " : "", /* CD Writer */
843 buffer[n + 3] & 0x20 ? "dvd-ram " : "",
844 buffer[n + 2] & 0x02 ? "cd/rw " : "", /* can read rewriteable */
845 buffer[n + 4] & 0x20 ? "xa/form2 " : "", /* can read xa/from2 */
846 buffer[n + 5] & 0x01 ? "cdda " : "", /* can read audio data */
847 loadmech[buffer[n + 6] >> 5]);
848 if ((buffer[n + 6] >> 5) == 0)
849 /* caddy drives can't close tray... */
850 cd->cdi.mask |= CDC_CLOSE_TRAY;
851 if ((buffer[n + 2] & 0x8) == 0)
852 /* not a DVD drive */
853 cd->cdi.mask |= CDC_DVD;
854 if ((buffer[n + 3] & 0x20) == 0)
855 /* can't write DVD-RAM media */
856 cd->cdi.mask |= CDC_DVD_RAM;
857 if ((buffer[n + 3] & 0x10) == 0)
858 /* can't write DVD-R media */
859 cd->cdi.mask |= CDC_DVD_R;
860 if ((buffer[n + 3] & 0x2) == 0)
861 /* can't write CD-RW media */
862 cd->cdi.mask |= CDC_CD_RW;
863 if ((buffer[n + 3] & 0x1) == 0)
864 /* can't write CD-R media */
865 cd->cdi.mask |= CDC_CD_R;
866 if ((buffer[n + 6] & 0x8) == 0)
867 /* can't eject */
868 cd->cdi.mask |= CDC_OPEN_TRAY;
870 if ((buffer[n + 6] >> 5) == mechtype_individual_changer ||
871 (buffer[n + 6] >> 5) == mechtype_cartridge_changer)
872 cd->cdi.capacity =
873 cdrom_number_of_slots(&cd->cdi);
874 if (cd->cdi.capacity <= 1)
875 /* not a changer */
876 cd->cdi.mask |= CDC_SELECT_DISC;
877 /*else I don't think it can close its tray
878 cd->cdi.mask |= CDC_CLOSE_TRAY; */
881 * if DVD-RAM, MRW-W or CD-RW, we are randomly writable
883 if ((cd->cdi.mask & (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) !=
884 (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) {
885 cd->device->writeable = 1;
888 scsi_release_request(SRpnt);
889 kfree(buffer);
893 * sr_packet() is the entry point for the generic commands generated
894 * by the Uniform CD-ROM layer.
896 static int sr_packet(struct cdrom_device_info *cdi,
897 struct packet_command *cgc)
899 if (cgc->timeout <= 0)
900 cgc->timeout = IOCTL_TIMEOUT;
902 sr_do_ioctl(cdi->handle, cgc);
904 return cgc->stat;
908 * sr_kref_release - Called to free the scsi_cd structure
909 * @kref: pointer to embedded kref
911 * sr_ref_sem must be held entering this routine. Because it is
912 * called on last put, you should always use the scsi_cd_get()
913 * scsi_cd_put() helpers which manipulate the semaphore directly
914 * and never do a direct kref_put().
916 static void sr_kref_release(struct kref *kref)
918 struct scsi_cd *cd = container_of(kref, struct scsi_cd, kref);
919 struct gendisk *disk = cd->disk;
921 spin_lock(&sr_index_lock);
922 clear_bit(disk->first_minor, sr_index_bits);
923 spin_unlock(&sr_index_lock);
925 unregister_cdrom(&cd->cdi);
927 disk->private_data = NULL;
929 put_disk(disk);
931 kfree(cd);
934 static int sr_remove(struct device *dev)
936 struct scsi_cd *cd = dev_get_drvdata(dev);
938 del_gendisk(cd->disk);
940 down(&sr_ref_sem);
941 kref_put(&cd->kref, sr_kref_release);
942 up(&sr_ref_sem);
944 return 0;
947 static int __init init_sr(void)
949 int rc;
951 rc = register_blkdev(SCSI_CDROM_MAJOR, "sr");
952 if (rc)
953 return rc;
954 return scsi_register_driver(&sr_template.gendrv);
957 static void __exit exit_sr(void)
959 scsi_unregister_driver(&sr_template.gendrv);
960 unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
963 module_init(init_sr);
964 module_exit(exit_sr);
965 MODULE_LICENSE("GPL");