Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / drivers / scsi / sr.c
blob7ee86d4a761810f6c66ffbd5ce4af203a824edeb
1 /*
2 * sr.c Copyright (C) 1992 David Giller
3 * Copyright (C) 1993, 1994, 1995, 1999 Eric Youngdale
5 * adapted from:
6 * sd.c Copyright (C) 1992 Drew Eckhardt
7 * Linux scsi disk driver by
8 * Drew Eckhardt <drew@colorado.edu>
10 * Modified by Eric Youngdale ericy@andante.org to
11 * add scatter-gather, multiple outstanding request, and other
12 * enhancements.
14 * Modified by Eric Youngdale eric@andante.org to support loadable
15 * low-level scsi drivers.
17 * Modified by Thomas Quinot thomas@melchior.cuivre.fdn.fr to
18 * provide auto-eject.
20 * Modified by Gerd Knorr <kraxel@cs.tu-berlin.de> to support the
21 * generic cdrom interface
23 * Modified by Jens Axboe <axboe@suse.de> - Uniform sr_packet()
24 * interface, capabilities probe additions, ioctl cleanups, etc.
26 * Modified by Richard Gooch <rgooch@atnf.csiro.au> to support devfs
28 * Modified by Jens Axboe <axboe@suse.de> - support DVD-RAM
29 * transparently and lose the GHOST hack
31 * Modified by Arnaldo Carvalho de Melo <acme@conectiva.com.br>
32 * check resource allocation in sr_init and some cleanups
35 #include <linux/module.h>
36 #include <linux/fs.h>
37 #include <linux/kernel.h>
38 #include <linux/mm.h>
39 #include <linux/bio.h>
40 #include <linux/string.h>
41 #include <linux/errno.h>
42 #include <linux/cdrom.h>
43 #include <linux/interrupt.h>
44 #include <linux/init.h>
45 #include <linux/blkdev.h>
46 #include <linux/mutex.h>
47 #include <asm/uaccess.h>
49 #include <scsi/scsi.h>
50 #include <scsi/scsi_dbg.h>
51 #include <scsi/scsi_device.h>
52 #include <scsi/scsi_driver.h>
53 #include <scsi/scsi_cmnd.h>
54 #include <scsi/scsi_eh.h>
55 #include <scsi/scsi_host.h>
56 #include <scsi/scsi_ioctl.h> /* For the door lock/unlock commands */
58 #include "scsi_logging.h"
59 #include "sr.h"
62 MODULE_DESCRIPTION("SCSI cdrom (sr) driver");
63 MODULE_LICENSE("GPL");
64 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_CDROM_MAJOR);
65 MODULE_ALIAS_SCSI_DEVICE(TYPE_ROM);
66 MODULE_ALIAS_SCSI_DEVICE(TYPE_WORM);
68 #define SR_DISKS 256
70 #define SR_CAPABILITIES \
71 (CDC_CLOSE_TRAY|CDC_OPEN_TRAY|CDC_LOCK|CDC_SELECT_SPEED| \
72 CDC_SELECT_DISC|CDC_MULTI_SESSION|CDC_MCN|CDC_MEDIA_CHANGED| \
73 CDC_PLAY_AUDIO|CDC_RESET|CDC_DRIVE_STATUS| \
74 CDC_CD_R|CDC_CD_RW|CDC_DVD|CDC_DVD_R|CDC_DVD_RAM|CDC_GENERIC_PACKET| \
75 CDC_MRW|CDC_MRW_W|CDC_RAM)
77 static int sr_probe(struct device *);
78 static int sr_remove(struct device *);
79 static int sr_done(struct scsi_cmnd *);
81 static struct scsi_driver sr_template = {
82 .owner = THIS_MODULE,
83 .gendrv = {
84 .name = "sr",
85 .probe = sr_probe,
86 .remove = sr_remove,
88 .done = sr_done,
91 static unsigned long sr_index_bits[SR_DISKS / BITS_PER_LONG];
92 static DEFINE_SPINLOCK(sr_index_lock);
94 /* This semaphore is used to mediate the 0->1 reference get in the
95 * face of object destruction (i.e. we can't allow a get on an
96 * object after last put) */
97 static DEFINE_MUTEX(sr_ref_mutex);
99 static int sr_open(struct cdrom_device_info *, int);
100 static void sr_release(struct cdrom_device_info *);
102 static void get_sectorsize(struct scsi_cd *);
103 static void get_capabilities(struct scsi_cd *);
105 static int sr_media_change(struct cdrom_device_info *, int);
106 static int sr_packet(struct cdrom_device_info *, struct packet_command *);
108 static struct cdrom_device_ops sr_dops = {
109 .open = sr_open,
110 .release = sr_release,
111 .drive_status = sr_drive_status,
112 .media_changed = sr_media_change,
113 .tray_move = sr_tray_move,
114 .lock_door = sr_lock_door,
115 .select_speed = sr_select_speed,
116 .get_last_session = sr_get_last_session,
117 .get_mcn = sr_get_mcn,
118 .reset = sr_reset,
119 .audio_ioctl = sr_audio_ioctl,
120 .capability = SR_CAPABILITIES,
121 .generic_packet = sr_packet,
124 static void sr_kref_release(struct kref *kref);
126 static inline struct scsi_cd *scsi_cd(struct gendisk *disk)
128 return container_of(disk->private_data, struct scsi_cd, driver);
132 * The get and put routines for the struct scsi_cd. Note this entity
133 * has a scsi_device pointer and owns a reference to this.
135 static inline struct scsi_cd *scsi_cd_get(struct gendisk *disk)
137 struct scsi_cd *cd = NULL;
139 mutex_lock(&sr_ref_mutex);
140 if (disk->private_data == NULL)
141 goto out;
142 cd = scsi_cd(disk);
143 kref_get(&cd->kref);
144 if (scsi_device_get(cd->device))
145 goto out_put;
146 goto out;
148 out_put:
149 kref_put(&cd->kref, sr_kref_release);
150 cd = NULL;
151 out:
152 mutex_unlock(&sr_ref_mutex);
153 return cd;
156 static void scsi_cd_put(struct scsi_cd *cd)
158 struct scsi_device *sdev = cd->device;
160 mutex_lock(&sr_ref_mutex);
161 kref_put(&cd->kref, sr_kref_release);
162 scsi_device_put(sdev);
163 mutex_unlock(&sr_ref_mutex);
166 /* identical to scsi_test_unit_ready except that it doesn't
167 * eat the NOT_READY returns for removable media */
168 int sr_test_unit_ready(struct scsi_device *sdev, struct scsi_sense_hdr *sshdr)
170 int retries = MAX_RETRIES;
171 int the_result;
172 u8 cmd[] = {TEST_UNIT_READY, 0, 0, 0, 0, 0 };
174 /* issue TEST_UNIT_READY until the initial startup UNIT_ATTENTION
175 * conditions are gone, or a timeout happens
177 do {
178 the_result = scsi_execute_req(sdev, cmd, DMA_NONE, NULL,
179 0, sshdr, SR_TIMEOUT,
180 retries--);
182 } while (retries > 0 &&
183 (!scsi_status_is_good(the_result) ||
184 (scsi_sense_valid(sshdr) &&
185 sshdr->sense_key == UNIT_ATTENTION)));
186 return the_result;
190 * This function checks to see if the media has been changed in the
191 * CDROM drive. It is possible that we have already sensed a change,
192 * or the drive may have sensed one and not yet reported it. We must
193 * be ready for either case. This function always reports the current
194 * value of the changed bit. If flag is 0, then the changed bit is reset.
195 * This function could be done as an ioctl, but we would need to have
196 * an inode for that to work, and we do not always have one.
199 static int sr_media_change(struct cdrom_device_info *cdi, int slot)
201 struct scsi_cd *cd = cdi->handle;
202 int retval;
203 struct scsi_sense_hdr *sshdr;
205 if (CDSL_CURRENT != slot) {
206 /* no changer support */
207 return -EINVAL;
210 sshdr = kzalloc(sizeof(*sshdr), GFP_KERNEL);
211 retval = sr_test_unit_ready(cd->device, sshdr);
212 if (retval || (scsi_sense_valid(sshdr) &&
213 /* 0x3a is medium not present */
214 sshdr->asc == 0x3a)) {
215 /* Media not present or unable to test, unit probably not
216 * ready. This usually means there is no disc in the drive.
217 * Mark as changed, and we will figure it out later once
218 * the drive is available again.
220 cd->device->changed = 1;
221 /* This will force a flush, if called from check_disk_change */
222 retval = 1;
223 goto out;
226 retval = cd->device->changed;
227 cd->device->changed = 0;
228 /* If the disk changed, the capacity will now be different,
229 * so we force a re-read of this information */
230 if (retval) {
231 /* check multisession offset etc */
232 sr_cd_check(cdi);
233 get_sectorsize(cd);
236 out:
237 /* Notify userspace, that media has changed. */
238 if (retval != cd->previous_state)
239 sdev_evt_send_simple(cd->device, SDEV_EVT_MEDIA_CHANGE,
240 GFP_KERNEL);
241 cd->previous_state = retval;
242 kfree(sshdr);
244 return retval;
248 * sr_done is the interrupt routine for the device driver.
250 * It will be notified on the end of a SCSI read / write, and will take one
251 * of several actions based on success or failure.
253 static int sr_done(struct scsi_cmnd *SCpnt)
255 int result = SCpnt->result;
256 int this_count = scsi_bufflen(SCpnt);
257 int good_bytes = (result == 0 ? this_count : 0);
258 int block_sectors = 0;
259 long error_sector;
260 struct scsi_cd *cd = scsi_cd(SCpnt->request->rq_disk);
262 #ifdef DEBUG
263 printk("sr.c done: %x\n", result);
264 #endif
267 * Handle MEDIUM ERRORs or VOLUME OVERFLOWs that indicate partial
268 * success. Since this is a relatively rare error condition, no
269 * care is taken to avoid unnecessary additional work such as
270 * memcpy's that could be avoided.
272 if (driver_byte(result) != 0 && /* An error occurred */
273 (SCpnt->sense_buffer[0] & 0x7f) == 0x70) { /* Sense current */
274 switch (SCpnt->sense_buffer[2]) {
275 case MEDIUM_ERROR:
276 case VOLUME_OVERFLOW:
277 case ILLEGAL_REQUEST:
278 if (!(SCpnt->sense_buffer[0] & 0x90))
279 break;
280 error_sector = (SCpnt->sense_buffer[3] << 24) |
281 (SCpnt->sense_buffer[4] << 16) |
282 (SCpnt->sense_buffer[5] << 8) |
283 SCpnt->sense_buffer[6];
284 if (SCpnt->request->bio != NULL)
285 block_sectors =
286 bio_sectors(SCpnt->request->bio);
287 if (block_sectors < 4)
288 block_sectors = 4;
289 if (cd->device->sector_size == 2048)
290 error_sector <<= 2;
291 error_sector &= ~(block_sectors - 1);
292 good_bytes = (error_sector - SCpnt->request->sector) << 9;
293 if (good_bytes < 0 || good_bytes >= this_count)
294 good_bytes = 0;
296 * The SCSI specification allows for the value
297 * returned by READ CAPACITY to be up to 75 2K
298 * sectors past the last readable block.
299 * Therefore, if we hit a medium error within the
300 * last 75 2K sectors, we decrease the saved size
301 * value.
303 if (error_sector < get_capacity(cd->disk) &&
304 cd->capacity - error_sector < 4 * 75)
305 set_capacity(cd->disk, error_sector);
306 break;
308 case RECOVERED_ERROR:
311 * An error occured, but it recovered. Inform the
312 * user, but make sure that it's not treated as a
313 * hard error.
315 scsi_print_sense("sr", SCpnt);
316 SCpnt->result = 0;
317 SCpnt->sense_buffer[0] = 0x0;
318 good_bytes = this_count;
319 break;
321 default:
322 break;
326 return good_bytes;
329 static int sr_prep_fn(struct request_queue *q, struct request *rq)
331 int block=0, this_count, s_size, timeout = SR_TIMEOUT;
332 struct scsi_cd *cd;
333 struct scsi_cmnd *SCpnt;
334 struct scsi_device *sdp = q->queuedata;
335 int ret;
337 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
338 ret = scsi_setup_blk_pc_cmnd(sdp, rq);
339 goto out;
340 } else if (rq->cmd_type != REQ_TYPE_FS) {
341 ret = BLKPREP_KILL;
342 goto out;
344 ret = scsi_setup_fs_cmnd(sdp, rq);
345 if (ret != BLKPREP_OK)
346 goto out;
347 SCpnt = rq->special;
348 cd = scsi_cd(rq->rq_disk);
350 /* from here on until we're complete, any goto out
351 * is used for a killable error condition */
352 ret = BLKPREP_KILL;
354 SCSI_LOG_HLQUEUE(1, printk("Doing sr request, dev = %s, block = %d\n",
355 cd->disk->disk_name, block));
357 if (!cd->device || !scsi_device_online(cd->device)) {
358 SCSI_LOG_HLQUEUE(2, printk("Finishing %ld sectors\n",
359 rq->nr_sectors));
360 SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt));
361 goto out;
364 if (cd->device->changed) {
366 * quietly refuse to do anything to a changed disc until the
367 * changed bit has been reset
369 goto out;
373 * we do lazy blocksize switching (when reading XA sectors,
374 * see CDROMREADMODE2 ioctl)
376 s_size = cd->device->sector_size;
377 if (s_size > 2048) {
378 if (!in_interrupt())
379 sr_set_blocklength(cd, 2048);
380 else
381 printk("sr: can't switch blocksize: in interrupt\n");
384 if (s_size != 512 && s_size != 1024 && s_size != 2048) {
385 scmd_printk(KERN_ERR, SCpnt, "bad sector size %d\n", s_size);
386 goto out;
389 if (rq_data_dir(rq) == WRITE) {
390 if (!cd->device->writeable)
391 goto out;
392 SCpnt->cmnd[0] = WRITE_10;
393 SCpnt->sc_data_direction = DMA_TO_DEVICE;
394 cd->cdi.media_written = 1;
395 } else if (rq_data_dir(rq) == READ) {
396 SCpnt->cmnd[0] = READ_10;
397 SCpnt->sc_data_direction = DMA_FROM_DEVICE;
398 } else {
399 blk_dump_rq_flags(rq, "Unknown sr command");
400 goto out;
404 struct scatterlist *sg;
405 int i, size = 0, sg_count = scsi_sg_count(SCpnt);
407 scsi_for_each_sg(SCpnt, sg, sg_count, i)
408 size += sg->length;
410 if (size != scsi_bufflen(SCpnt)) {
411 scmd_printk(KERN_ERR, SCpnt,
412 "mismatch count %d, bytes %d\n",
413 size, scsi_bufflen(SCpnt));
414 if (scsi_bufflen(SCpnt) > size)
415 SCpnt->sdb.length = size;
420 * request doesn't start on hw block boundary, add scatter pads
422 if (((unsigned int)rq->sector % (s_size >> 9)) ||
423 (scsi_bufflen(SCpnt) % s_size)) {
424 scmd_printk(KERN_NOTICE, SCpnt, "unaligned transfer\n");
425 goto out;
428 this_count = (scsi_bufflen(SCpnt) >> 9) / (s_size >> 9);
431 SCSI_LOG_HLQUEUE(2, printk("%s : %s %d/%ld 512 byte blocks.\n",
432 cd->cdi.name,
433 (rq_data_dir(rq) == WRITE) ?
434 "writing" : "reading",
435 this_count, rq->nr_sectors));
437 SCpnt->cmnd[1] = 0;
438 block = (unsigned int)rq->sector / (s_size >> 9);
440 if (this_count > 0xffff) {
441 this_count = 0xffff;
442 SCpnt->sdb.length = this_count * s_size;
445 SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
446 SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
447 SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
448 SCpnt->cmnd[5] = (unsigned char) block & 0xff;
449 SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
450 SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
451 SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
454 * We shouldn't disconnect in the middle of a sector, so with a dumb
455 * host adapter, it's safe to assume that we can at least transfer
456 * this many bytes between each connect / disconnect.
458 SCpnt->transfersize = cd->device->sector_size;
459 SCpnt->underflow = this_count << 9;
460 SCpnt->allowed = MAX_RETRIES;
461 SCpnt->timeout_per_command = timeout;
464 * This indicates that the command is ready from our end to be
465 * queued.
467 ret = BLKPREP_OK;
468 out:
469 return scsi_prep_return(q, rq, ret);
472 static int sr_block_open(struct inode *inode, struct file *file)
474 struct gendisk *disk = inode->i_bdev->bd_disk;
475 struct scsi_cd *cd;
476 int ret = 0;
478 if(!(cd = scsi_cd_get(disk)))
479 return -ENXIO;
481 if((ret = cdrom_open(&cd->cdi, inode, file)) != 0)
482 scsi_cd_put(cd);
484 return ret;
487 static int sr_block_release(struct inode *inode, struct file *file)
489 int ret;
490 struct scsi_cd *cd = scsi_cd(inode->i_bdev->bd_disk);
491 ret = cdrom_release(&cd->cdi, file);
492 if(ret)
493 return ret;
495 scsi_cd_put(cd);
497 return 0;
500 static int sr_block_ioctl(struct inode *inode, struct file *file, unsigned cmd,
501 unsigned long arg)
503 struct scsi_cd *cd = scsi_cd(inode->i_bdev->bd_disk);
504 struct scsi_device *sdev = cd->device;
505 void __user *argp = (void __user *)arg;
506 int ret;
509 * Send SCSI addressing ioctls directly to mid level, send other
510 * ioctls to cdrom/block level.
512 switch (cmd) {
513 case SCSI_IOCTL_GET_IDLUN:
514 case SCSI_IOCTL_GET_BUS_NUMBER:
515 return scsi_ioctl(sdev, cmd, argp);
518 ret = cdrom_ioctl(file, &cd->cdi, inode, cmd, arg);
519 if (ret != -ENOSYS)
520 return ret;
523 * ENODEV means that we didn't recognise the ioctl, or that we
524 * cannot execute it in the current device state. In either
525 * case fall through to scsi_ioctl, which will return ENDOEV again
526 * if it doesn't recognise the ioctl
528 ret = scsi_nonblockable_ioctl(sdev, cmd, argp, NULL);
529 if (ret != -ENODEV)
530 return ret;
531 return scsi_ioctl(sdev, cmd, argp);
534 static int sr_block_media_changed(struct gendisk *disk)
536 struct scsi_cd *cd = scsi_cd(disk);
537 return cdrom_media_changed(&cd->cdi);
540 static struct block_device_operations sr_bdops =
542 .owner = THIS_MODULE,
543 .open = sr_block_open,
544 .release = sr_block_release,
545 .ioctl = sr_block_ioctl,
546 .media_changed = sr_block_media_changed,
548 * No compat_ioctl for now because sr_block_ioctl never
549 * seems to pass arbitary ioctls down to host drivers.
553 static int sr_open(struct cdrom_device_info *cdi, int purpose)
555 struct scsi_cd *cd = cdi->handle;
556 struct scsi_device *sdev = cd->device;
557 int retval;
560 * If the device is in error recovery, wait until it is done.
561 * If the device is offline, then disallow any access to it.
563 retval = -ENXIO;
564 if (!scsi_block_when_processing_errors(sdev))
565 goto error_out;
567 return 0;
569 error_out:
570 return retval;
573 static void sr_release(struct cdrom_device_info *cdi)
575 struct scsi_cd *cd = cdi->handle;
577 if (cd->device->sector_size > 2048)
578 sr_set_blocklength(cd, 2048);
582 static int sr_probe(struct device *dev)
584 struct scsi_device *sdev = to_scsi_device(dev);
585 struct gendisk *disk;
586 struct scsi_cd *cd;
587 int minor, error;
589 error = -ENODEV;
590 if (sdev->type != TYPE_ROM && sdev->type != TYPE_WORM)
591 goto fail;
593 error = -ENOMEM;
594 cd = kzalloc(sizeof(*cd), GFP_KERNEL);
595 if (!cd)
596 goto fail;
598 kref_init(&cd->kref);
600 disk = alloc_disk(1);
601 if (!disk)
602 goto fail_free;
604 spin_lock(&sr_index_lock);
605 minor = find_first_zero_bit(sr_index_bits, SR_DISKS);
606 if (minor == SR_DISKS) {
607 spin_unlock(&sr_index_lock);
608 error = -EBUSY;
609 goto fail_put;
611 __set_bit(minor, sr_index_bits);
612 spin_unlock(&sr_index_lock);
614 disk->major = SCSI_CDROM_MAJOR;
615 disk->first_minor = minor;
616 sprintf(disk->disk_name, "sr%d", minor);
617 disk->fops = &sr_bdops;
618 disk->flags = GENHD_FL_CD;
620 cd->device = sdev;
621 cd->disk = disk;
622 cd->driver = &sr_template;
623 cd->disk = disk;
624 cd->capacity = 0x1fffff;
625 cd->device->changed = 1; /* force recheck CD type */
626 cd->previous_state = 1;
627 cd->use = 1;
628 cd->readcd_known = 0;
629 cd->readcd_cdda = 0;
631 cd->cdi.ops = &sr_dops;
632 cd->cdi.handle = cd;
633 cd->cdi.mask = 0;
634 cd->cdi.capacity = 1;
635 sprintf(cd->cdi.name, "sr%d", minor);
637 sdev->sector_size = 2048; /* A guess, just in case */
639 /* FIXME: need to handle a get_capabilities failure properly ?? */
640 get_capabilities(cd);
641 blk_queue_prep_rq(sdev->request_queue, sr_prep_fn);
642 sr_vendor_init(cd);
644 disk->driverfs_dev = &sdev->sdev_gendev;
645 set_capacity(disk, cd->capacity);
646 disk->private_data = &cd->driver;
647 disk->queue = sdev->request_queue;
648 cd->cdi.disk = disk;
650 if (register_cdrom(&cd->cdi))
651 goto fail_put;
653 dev_set_drvdata(dev, cd);
654 disk->flags |= GENHD_FL_REMOVABLE;
655 add_disk(disk);
657 sdev_printk(KERN_DEBUG, sdev,
658 "Attached scsi CD-ROM %s\n", cd->cdi.name);
659 return 0;
661 fail_put:
662 put_disk(disk);
663 fail_free:
664 kfree(cd);
665 fail:
666 return error;
670 static void get_sectorsize(struct scsi_cd *cd)
672 unsigned char cmd[10];
673 unsigned char *buffer;
674 int the_result, retries = 3;
675 int sector_size;
676 struct request_queue *queue;
678 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
679 if (!buffer)
680 goto Enomem;
682 do {
683 cmd[0] = READ_CAPACITY;
684 memset((void *) &cmd[1], 0, 9);
685 memset(buffer, 0, 8);
687 /* Do the command and wait.. */
688 the_result = scsi_execute_req(cd->device, cmd, DMA_FROM_DEVICE,
689 buffer, 8, NULL, SR_TIMEOUT,
690 MAX_RETRIES);
692 retries--;
694 } while (the_result && retries);
697 if (the_result) {
698 cd->capacity = 0x1fffff;
699 sector_size = 2048; /* A guess, just in case */
700 } else {
701 #if 0
702 if (cdrom_get_last_written(&cd->cdi,
703 &cd->capacity))
704 #endif
705 cd->capacity = 1 + ((buffer[0] << 24) |
706 (buffer[1] << 16) |
707 (buffer[2] << 8) |
708 buffer[3]);
709 sector_size = (buffer[4] << 24) |
710 (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
711 switch (sector_size) {
713 * HP 4020i CD-Recorder reports 2340 byte sectors
714 * Philips CD-Writers report 2352 byte sectors
716 * Use 2k sectors for them..
718 case 0:
719 case 2340:
720 case 2352:
721 sector_size = 2048;
722 /* fall through */
723 case 2048:
724 cd->capacity *= 4;
725 /* fall through */
726 case 512:
727 break;
728 default:
729 printk("%s: unsupported sector size %d.\n",
730 cd->cdi.name, sector_size);
731 cd->capacity = 0;
734 cd->device->sector_size = sector_size;
737 * Add this so that we have the ability to correctly gauge
738 * what the device is capable of.
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 cd->device->sector_size = 2048; /* A guess, just in case */
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_sense_hdr sshdr;
760 int rc, n;
762 static const char *loadmech[] =
764 "caddy",
765 "tray",
766 "pop-up",
768 "changer",
769 "cartridge changer",
775 /* allocate transfer buffer */
776 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
777 if (!buffer) {
778 printk(KERN_ERR "sr: out of memory.\n");
779 return;
782 /* eat unit attentions */
783 sr_test_unit_ready(cd->device, &sshdr);
785 /* ask for mode page 0x2a */
786 rc = scsi_mode_sense(cd->device, 0, 0x2a, buffer, 128,
787 SR_TIMEOUT, 3, &data, NULL);
789 if (!scsi_status_is_good(rc)) {
790 /* failed, drive doesn't have capabilities mode page */
791 cd->cdi.speed = 1;
792 cd->cdi.mask |= (CDC_CD_R | CDC_CD_RW | CDC_DVD_R |
793 CDC_DVD | CDC_DVD_RAM |
794 CDC_SELECT_DISC | CDC_SELECT_SPEED |
795 CDC_MRW | CDC_MRW_W | CDC_RAM);
796 kfree(buffer);
797 printk("%s: scsi-1 drive\n", cd->cdi.name);
798 return;
801 n = data.header_length + data.block_descriptor_length;
802 cd->cdi.speed = ((buffer[n + 8] << 8) + buffer[n + 9]) / 176;
803 cd->readcd_known = 1;
804 cd->readcd_cdda = buffer[n + 5] & 0x01;
805 /* print some capability bits */
806 printk("%s: scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n", cd->cdi.name,
807 ((buffer[n + 14] << 8) + buffer[n + 15]) / 176,
808 cd->cdi.speed,
809 buffer[n + 3] & 0x01 ? "writer " : "", /* CD Writer */
810 buffer[n + 3] & 0x20 ? "dvd-ram " : "",
811 buffer[n + 2] & 0x02 ? "cd/rw " : "", /* can read rewriteable */
812 buffer[n + 4] & 0x20 ? "xa/form2 " : "", /* can read xa/from2 */
813 buffer[n + 5] & 0x01 ? "cdda " : "", /* can read audio data */
814 loadmech[buffer[n + 6] >> 5]);
815 if ((buffer[n + 6] >> 5) == 0)
816 /* caddy drives can't close tray... */
817 cd->cdi.mask |= CDC_CLOSE_TRAY;
818 if ((buffer[n + 2] & 0x8) == 0)
819 /* not a DVD drive */
820 cd->cdi.mask |= CDC_DVD;
821 if ((buffer[n + 3] & 0x20) == 0)
822 /* can't write DVD-RAM media */
823 cd->cdi.mask |= CDC_DVD_RAM;
824 if ((buffer[n + 3] & 0x10) == 0)
825 /* can't write DVD-R media */
826 cd->cdi.mask |= CDC_DVD_R;
827 if ((buffer[n + 3] & 0x2) == 0)
828 /* can't write CD-RW media */
829 cd->cdi.mask |= CDC_CD_RW;
830 if ((buffer[n + 3] & 0x1) == 0)
831 /* can't write CD-R media */
832 cd->cdi.mask |= CDC_CD_R;
833 if ((buffer[n + 6] & 0x8) == 0)
834 /* can't eject */
835 cd->cdi.mask |= CDC_OPEN_TRAY;
837 if ((buffer[n + 6] >> 5) == mechtype_individual_changer ||
838 (buffer[n + 6] >> 5) == mechtype_cartridge_changer)
839 cd->cdi.capacity =
840 cdrom_number_of_slots(&cd->cdi);
841 if (cd->cdi.capacity <= 1)
842 /* not a changer */
843 cd->cdi.mask |= CDC_SELECT_DISC;
844 /*else I don't think it can close its tray
845 cd->cdi.mask |= CDC_CLOSE_TRAY; */
848 * if DVD-RAM, MRW-W or CD-RW, we are randomly writable
850 if ((cd->cdi.mask & (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) !=
851 (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) {
852 cd->device->writeable = 1;
855 kfree(buffer);
859 * sr_packet() is the entry point for the generic commands generated
860 * by the Uniform CD-ROM layer.
862 static int sr_packet(struct cdrom_device_info *cdi,
863 struct packet_command *cgc)
865 if (cgc->timeout <= 0)
866 cgc->timeout = IOCTL_TIMEOUT;
868 sr_do_ioctl(cdi->handle, cgc);
870 return cgc->stat;
874 * sr_kref_release - Called to free the scsi_cd structure
875 * @kref: pointer to embedded kref
877 * sr_ref_mutex must be held entering this routine. Because it is
878 * called on last put, you should always use the scsi_cd_get()
879 * scsi_cd_put() helpers which manipulate the semaphore directly
880 * and never do a direct kref_put().
882 static void sr_kref_release(struct kref *kref)
884 struct scsi_cd *cd = container_of(kref, struct scsi_cd, kref);
885 struct gendisk *disk = cd->disk;
887 spin_lock(&sr_index_lock);
888 clear_bit(disk->first_minor, sr_index_bits);
889 spin_unlock(&sr_index_lock);
891 unregister_cdrom(&cd->cdi);
893 disk->private_data = NULL;
895 put_disk(disk);
897 kfree(cd);
900 static int sr_remove(struct device *dev)
902 struct scsi_cd *cd = dev_get_drvdata(dev);
904 del_gendisk(cd->disk);
906 mutex_lock(&sr_ref_mutex);
907 kref_put(&cd->kref, sr_kref_release);
908 mutex_unlock(&sr_ref_mutex);
910 return 0;
913 static int __init init_sr(void)
915 int rc;
917 rc = register_blkdev(SCSI_CDROM_MAJOR, "sr");
918 if (rc)
919 return rc;
920 rc = scsi_register_driver(&sr_template.gendrv);
921 if (rc)
922 unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
924 return rc;
927 static void __exit exit_sr(void)
929 scsi_unregister_driver(&sr_template.gendrv);
930 unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
933 module_init(init_sr);
934 module_exit(exit_sr);
935 MODULE_LICENSE("GPL");