initial commit with v2.6.9
[linux-2.6.9-moxart.git] / drivers / scsi / sr.c
blobb6c76b5680a2045b2b5798c80cf2dca61bcad4a9
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 } 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 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,
523 static int sr_open(struct cdrom_device_info *cdi, int purpose)
525 struct scsi_cd *cd = cdi->handle;
526 struct scsi_device *sdev = cd->device;
527 int retval;
530 * If the device is in error recovery, wait until it is done.
531 * If the device is offline, then disallow any access to it.
533 retval = -ENXIO;
534 if (!scsi_block_when_processing_errors(sdev))
535 goto error_out;
538 * If this device did not have media in the drive at boot time, then
539 * we would have been unable to get the sector size. Check to see if
540 * this is the case, and try again.
542 if (cd->needs_sector_size)
543 get_sectorsize(cd);
544 return 0;
546 error_out:
547 scsi_cd_put(cd);
548 return retval;
551 static void sr_release(struct cdrom_device_info *cdi)
553 struct scsi_cd *cd = cdi->handle;
555 if (cd->device->sector_size > 2048)
556 sr_set_blocklength(cd, 2048);
560 static int sr_probe(struct device *dev)
562 struct scsi_device *sdev = to_scsi_device(dev);
563 struct gendisk *disk;
564 struct scsi_cd *cd;
565 int minor, error;
567 error = -ENODEV;
568 if (sdev->type != TYPE_ROM && sdev->type != TYPE_WORM)
569 goto fail;
571 error = -ENOMEM;
572 cd = kmalloc(sizeof(*cd), GFP_KERNEL);
573 if (!cd)
574 goto fail;
575 memset(cd, 0, sizeof(*cd));
577 kref_init(&cd->kref);
579 disk = alloc_disk(1);
580 if (!disk)
581 goto fail_free;
583 spin_lock(&sr_index_lock);
584 minor = find_first_zero_bit(sr_index_bits, SR_DISKS);
585 if (minor == SR_DISKS) {
586 spin_unlock(&sr_index_lock);
587 error = -EBUSY;
588 goto fail_put;
590 __set_bit(minor, sr_index_bits);
591 spin_unlock(&sr_index_lock);
593 disk->major = SCSI_CDROM_MAJOR;
594 disk->first_minor = minor;
595 sprintf(disk->disk_name, "sr%d", minor);
596 disk->fops = &sr_bdops;
597 disk->flags = GENHD_FL_CD;
599 cd->device = sdev;
600 cd->disk = disk;
601 cd->driver = &sr_template;
602 cd->disk = disk;
603 cd->capacity = 0x1fffff;
604 cd->needs_sector_size = 1;
605 cd->device->changed = 1; /* force recheck CD type */
606 cd->use = 1;
607 cd->readcd_known = 0;
608 cd->readcd_cdda = 0;
610 cd->cdi.ops = &sr_dops;
611 cd->cdi.handle = cd;
612 cd->cdi.mask = 0;
613 cd->cdi.capacity = 1;
614 sprintf(cd->cdi.name, "sr%d", minor);
616 sdev->sector_size = 2048; /* A guess, just in case */
618 /* FIXME: need to handle a get_capabilities failure properly ?? */
619 get_capabilities(cd);
620 sr_vendor_init(cd);
622 snprintf(disk->devfs_name, sizeof(disk->devfs_name),
623 "%s/cd", sdev->devfs_name);
624 disk->driverfs_dev = &sdev->sdev_gendev;
625 set_capacity(disk, cd->capacity);
626 disk->private_data = &cd->driver;
627 disk->queue = sdev->request_queue;
628 cd->cdi.disk = disk;
630 if (register_cdrom(&cd->cdi))
631 goto fail_put;
633 dev_set_drvdata(dev, cd);
634 disk->flags |= GENHD_FL_REMOVABLE;
635 add_disk(disk);
637 printk(KERN_DEBUG
638 "Attached scsi CD-ROM %s at scsi%d, channel %d, id %d, lun %d\n",
639 cd->cdi.name, sdev->host->host_no, sdev->channel,
640 sdev->id, sdev->lun);
641 return 0;
643 fail_put:
644 put_disk(disk);
645 fail_free:
646 kfree(cd);
647 fail:
648 return error;
652 static void get_sectorsize(struct scsi_cd *cd)
654 unsigned char cmd[10];
655 unsigned char *buffer;
656 int the_result, retries = 3;
657 int sector_size;
658 struct scsi_request *SRpnt = NULL;
659 request_queue_t *queue;
661 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
662 if (!buffer)
663 goto Enomem;
664 SRpnt = scsi_allocate_request(cd->device, GFP_KERNEL);
665 if (!SRpnt)
666 goto Enomem;
668 do {
669 cmd[0] = READ_CAPACITY;
670 memset((void *) &cmd[1], 0, 9);
671 /* Mark as really busy */
672 SRpnt->sr_request->rq_status = RQ_SCSI_BUSY;
673 SRpnt->sr_cmd_len = 0;
675 memset(buffer, 0, 8);
677 /* Do the command and wait.. */
678 SRpnt->sr_data_direction = DMA_FROM_DEVICE;
679 scsi_wait_req(SRpnt, (void *) cmd, (void *) buffer,
680 8, SR_TIMEOUT, MAX_RETRIES);
682 the_result = SRpnt->sr_result;
683 retries--;
685 } while (the_result && retries);
688 scsi_release_request(SRpnt);
689 SRpnt = NULL;
691 if (the_result) {
692 cd->capacity = 0x1fffff;
693 sector_size = 2048; /* A guess, just in case */
694 cd->needs_sector_size = 1;
695 } else {
696 #if 0
697 if (cdrom_get_last_written(&cd->cdi,
698 &cd->capacity))
699 #endif
700 cd->capacity = 1 + ((buffer[0] << 24) |
701 (buffer[1] << 16) |
702 (buffer[2] << 8) |
703 buffer[3]);
704 sector_size = (buffer[4] << 24) |
705 (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
706 switch (sector_size) {
708 * HP 4020i CD-Recorder reports 2340 byte sectors
709 * Philips CD-Writers report 2352 byte sectors
711 * Use 2k sectors for them..
713 case 0:
714 case 2340:
715 case 2352:
716 sector_size = 2048;
717 /* fall through */
718 case 2048:
719 cd->capacity *= 4;
720 /* fall through */
721 case 512:
722 break;
723 default:
724 printk("%s: unsupported sector size %d.\n",
725 cd->cdi.name, sector_size);
726 cd->capacity = 0;
727 cd->needs_sector_size = 1;
730 cd->device->sector_size = sector_size;
733 * Add this so that we have the ability to correctly gauge
734 * what the device is capable of.
736 cd->needs_sector_size = 0;
737 set_capacity(cd->disk, cd->capacity);
740 queue = cd->device->request_queue;
741 blk_queue_hardsect_size(queue, sector_size);
742 out:
743 kfree(buffer);
744 return;
746 Enomem:
747 cd->capacity = 0x1fffff;
748 sector_size = 2048; /* A guess, just in case */
749 cd->needs_sector_size = 1;
750 if (SRpnt)
751 scsi_release_request(SRpnt);
752 goto out;
755 static void get_capabilities(struct scsi_cd *cd)
757 unsigned char *buffer;
758 struct scsi_mode_data data;
759 struct scsi_request *SRpnt;
760 unsigned char cmd[MAX_COMMAND_SIZE];
761 unsigned int the_result;
762 int retries, rc, n;
764 static char *loadmech[] =
766 "caddy",
767 "tray",
768 "pop-up",
770 "changer",
771 "cartridge changer",
776 /* allocate a request for the TEST_UNIT_READY */
777 SRpnt = scsi_allocate_request(cd->device, GFP_KERNEL);
778 if (!SRpnt) {
779 printk(KERN_WARNING "(get_capabilities:) Request allocation "
780 "failure.\n");
781 return;
784 /* allocate transfer buffer */
785 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
786 if (!buffer) {
787 printk(KERN_ERR "sr: out of memory.\n");
788 scsi_release_request(SRpnt);
789 return;
792 /* issue TEST_UNIT_READY until the initial startup UNIT_ATTENTION
793 * conditions are gone, or a timeout happens
795 retries = 0;
796 do {
797 memset((void *)cmd, 0, MAX_COMMAND_SIZE);
798 cmd[0] = TEST_UNIT_READY;
800 SRpnt->sr_cmd_len = 0;
801 SRpnt->sr_sense_buffer[0] = 0;
802 SRpnt->sr_sense_buffer[2] = 0;
803 SRpnt->sr_data_direction = DMA_NONE;
805 scsi_wait_req (SRpnt, (void *) cmd, buffer,
806 0, SR_TIMEOUT, MAX_RETRIES);
808 the_result = SRpnt->sr_result;
809 retries++;
810 } while (retries < 5 &&
811 (!scsi_status_is_good(the_result) ||
812 ((driver_byte(the_result) & DRIVER_SENSE) &&
813 SRpnt->sr_sense_buffer[2] == UNIT_ATTENTION)));
815 /* ask for mode page 0x2a */
816 rc = scsi_mode_sense(cd->device, 0, 0x2a, buffer, 128,
817 SR_TIMEOUT, 3, &data);
819 if (!scsi_status_is_good(rc)) {
820 /* failed, drive doesn't have capabilities mode page */
821 cd->cdi.speed = 1;
822 cd->cdi.mask |= (CDC_CD_R | CDC_CD_RW | CDC_DVD_R |
823 CDC_DVD | CDC_DVD_RAM |
824 CDC_SELECT_DISC | CDC_SELECT_SPEED);
825 scsi_release_request(SRpnt);
826 kfree(buffer);
827 printk("%s: scsi-1 drive\n", cd->cdi.name);
828 return;
831 n = data.header_length + data.block_descriptor_length;
832 cd->cdi.speed = ((buffer[n + 8] << 8) + buffer[n + 9]) / 176;
833 cd->readcd_known = 1;
834 cd->readcd_cdda = buffer[n + 5] & 0x01;
835 /* print some capability bits */
836 printk("%s: scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n", cd->cdi.name,
837 ((buffer[n + 14] << 8) + buffer[n + 15]) / 176,
838 cd->cdi.speed,
839 buffer[n + 3] & 0x01 ? "writer " : "", /* CD Writer */
840 buffer[n + 3] & 0x20 ? "dvd-ram " : "",
841 buffer[n + 2] & 0x02 ? "cd/rw " : "", /* can read rewriteable */
842 buffer[n + 4] & 0x20 ? "xa/form2 " : "", /* can read xa/from2 */
843 buffer[n + 5] & 0x01 ? "cdda " : "", /* can read audio data */
844 loadmech[buffer[n + 6] >> 5]);
845 if ((buffer[n + 6] >> 5) == 0)
846 /* caddy drives can't close tray... */
847 cd->cdi.mask |= CDC_CLOSE_TRAY;
848 if ((buffer[n + 2] & 0x8) == 0)
849 /* not a DVD drive */
850 cd->cdi.mask |= CDC_DVD;
851 if ((buffer[n + 3] & 0x20) == 0)
852 /* can't write DVD-RAM media */
853 cd->cdi.mask |= CDC_DVD_RAM;
854 if ((buffer[n + 3] & 0x10) == 0)
855 /* can't write DVD-R media */
856 cd->cdi.mask |= CDC_DVD_R;
857 if ((buffer[n + 3] & 0x2) == 0)
858 /* can't write CD-RW media */
859 cd->cdi.mask |= CDC_CD_RW;
860 if ((buffer[n + 3] & 0x1) == 0)
861 /* can't write CD-R media */
862 cd->cdi.mask |= CDC_CD_R;
863 if ((buffer[n + 6] & 0x8) == 0)
864 /* can't eject */
865 cd->cdi.mask |= CDC_OPEN_TRAY;
867 if ((buffer[n + 6] >> 5) == mechtype_individual_changer ||
868 (buffer[n + 6] >> 5) == mechtype_cartridge_changer)
869 cd->cdi.capacity =
870 cdrom_number_of_slots(&cd->cdi);
871 if (cd->cdi.capacity <= 1)
872 /* not a changer */
873 cd->cdi.mask |= CDC_SELECT_DISC;
874 /*else I don't think it can close its tray
875 cd->cdi.mask |= CDC_CLOSE_TRAY; */
878 * if DVD-RAM of MRW-W, we are randomly writeable
880 if ((cd->cdi.mask & (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM)) !=
881 (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM)) {
882 cd->device->writeable = 1;
885 scsi_release_request(SRpnt);
886 kfree(buffer);
890 * sr_packet() is the entry point for the generic commands generated
891 * by the Uniform CD-ROM layer.
893 static int sr_packet(struct cdrom_device_info *cdi,
894 struct packet_command *cgc)
896 if (cgc->timeout <= 0)
897 cgc->timeout = IOCTL_TIMEOUT;
899 sr_do_ioctl(cdi->handle, cgc);
901 return cgc->stat;
905 * sr_kref_release - Called to free the scsi_cd structure
906 * @kref: pointer to embedded kref
908 * sr_ref_sem must be held entering this routine. Because it is
909 * called on last put, you should always use the scsi_cd_get()
910 * scsi_cd_put() helpers which manipulate the semaphore directly
911 * and never do a direct kref_put().
913 static void sr_kref_release(struct kref *kref)
915 struct scsi_cd *cd = container_of(kref, struct scsi_cd, kref);
916 struct gendisk *disk = cd->disk;
918 spin_lock(&sr_index_lock);
919 clear_bit(disk->first_minor, sr_index_bits);
920 spin_unlock(&sr_index_lock);
922 unregister_cdrom(&cd->cdi);
924 disk->private_data = NULL;
926 put_disk(disk);
928 kfree(cd);
931 static int sr_remove(struct device *dev)
933 struct scsi_cd *cd = dev_get_drvdata(dev);
935 del_gendisk(cd->disk);
937 down(&sr_ref_sem);
938 kref_put(&cd->kref, sr_kref_release);
939 up(&sr_ref_sem);
941 return 0;
944 static int __init init_sr(void)
946 int rc;
948 rc = register_blkdev(SCSI_CDROM_MAJOR, "sr");
949 if (rc)
950 return rc;
951 return scsi_register_driver(&sr_template.gendrv);
954 static void __exit exit_sr(void)
956 scsi_unregister_driver(&sr_template.gendrv);
957 unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
960 module_init(init_sr);
961 module_exit(exit_sr);
962 MODULE_LICENSE("GPL");