x86/amd-iommu: Add per IOMMU reference counting
[linux/fpc-iii.git] / drivers / scsi / sr.c
blobd6f340f48a3bcecebbf57aef61d5b0e8cd67d3b5
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--, NULL);
181 if (scsi_sense_valid(sshdr) &&
182 sshdr->sense_key == UNIT_ATTENTION)
183 sdev->changed = 1;
185 } while (retries > 0 &&
186 (!scsi_status_is_good(the_result) ||
187 (scsi_sense_valid(sshdr) &&
188 sshdr->sense_key == UNIT_ATTENTION)));
189 return the_result;
193 * This function checks to see if the media has been changed in the
194 * CDROM drive. It is possible that we have already sensed a change,
195 * or the drive may have sensed one and not yet reported it. We must
196 * be ready for either case. This function always reports the current
197 * value of the changed bit. If flag is 0, then the changed bit is reset.
198 * This function could be done as an ioctl, but we would need to have
199 * an inode for that to work, and we do not always have one.
202 static int sr_media_change(struct cdrom_device_info *cdi, int slot)
204 struct scsi_cd *cd = cdi->handle;
205 int retval;
206 struct scsi_sense_hdr *sshdr;
208 if (CDSL_CURRENT != slot) {
209 /* no changer support */
210 return -EINVAL;
213 sshdr = kzalloc(sizeof(*sshdr), GFP_KERNEL);
214 retval = sr_test_unit_ready(cd->device, sshdr);
215 if (retval || (scsi_sense_valid(sshdr) &&
216 /* 0x3a is medium not present */
217 sshdr->asc == 0x3a)) {
218 /* Media not present or unable to test, unit probably not
219 * ready. This usually means there is no disc in the drive.
220 * Mark as changed, and we will figure it out later once
221 * the drive is available again.
223 cd->device->changed = 1;
224 /* This will force a flush, if called from check_disk_change */
225 retval = 1;
226 goto out;
229 retval = cd->device->changed;
230 cd->device->changed = 0;
231 /* If the disk changed, the capacity will now be different,
232 * so we force a re-read of this information */
233 if (retval) {
234 /* check multisession offset etc */
235 sr_cd_check(cdi);
236 get_sectorsize(cd);
239 out:
240 /* Notify userspace, that media has changed. */
241 if (retval != cd->previous_state)
242 sdev_evt_send_simple(cd->device, SDEV_EVT_MEDIA_CHANGE,
243 GFP_KERNEL);
244 cd->previous_state = retval;
245 kfree(sshdr);
247 return retval;
251 * sr_done is the interrupt routine for the device driver.
253 * It will be notified on the end of a SCSI read / write, and will take one
254 * of several actions based on success or failure.
256 static int sr_done(struct scsi_cmnd *SCpnt)
258 int result = SCpnt->result;
259 int this_count = scsi_bufflen(SCpnt);
260 int good_bytes = (result == 0 ? this_count : 0);
261 int block_sectors = 0;
262 long error_sector;
263 struct scsi_cd *cd = scsi_cd(SCpnt->request->rq_disk);
265 #ifdef DEBUG
266 printk("sr.c done: %x\n", result);
267 #endif
270 * Handle MEDIUM ERRORs or VOLUME OVERFLOWs that indicate partial
271 * success. Since this is a relatively rare error condition, no
272 * care is taken to avoid unnecessary additional work such as
273 * memcpy's that could be avoided.
275 if (driver_byte(result) != 0 && /* An error occurred */
276 (SCpnt->sense_buffer[0] & 0x7f) == 0x70) { /* Sense current */
277 switch (SCpnt->sense_buffer[2]) {
278 case MEDIUM_ERROR:
279 case VOLUME_OVERFLOW:
280 case ILLEGAL_REQUEST:
281 if (!(SCpnt->sense_buffer[0] & 0x90))
282 break;
283 error_sector = (SCpnt->sense_buffer[3] << 24) |
284 (SCpnt->sense_buffer[4] << 16) |
285 (SCpnt->sense_buffer[5] << 8) |
286 SCpnt->sense_buffer[6];
287 if (SCpnt->request->bio != NULL)
288 block_sectors =
289 bio_sectors(SCpnt->request->bio);
290 if (block_sectors < 4)
291 block_sectors = 4;
292 if (cd->device->sector_size == 2048)
293 error_sector <<= 2;
294 error_sector &= ~(block_sectors - 1);
295 good_bytes = (error_sector -
296 blk_rq_pos(SCpnt->request)) << 9;
297 if (good_bytes < 0 || good_bytes >= this_count)
298 good_bytes = 0;
300 * The SCSI specification allows for the value
301 * returned by READ CAPACITY to be up to 75 2K
302 * sectors past the last readable block.
303 * Therefore, if we hit a medium error within the
304 * last 75 2K sectors, we decrease the saved size
305 * value.
307 if (error_sector < get_capacity(cd->disk) &&
308 cd->capacity - error_sector < 4 * 75)
309 set_capacity(cd->disk, error_sector);
310 break;
312 case RECOVERED_ERROR:
313 good_bytes = this_count;
314 break;
316 default:
317 break;
321 return good_bytes;
324 static int sr_prep_fn(struct request_queue *q, struct request *rq)
326 int block = 0, this_count, s_size;
327 struct scsi_cd *cd;
328 struct scsi_cmnd *SCpnt;
329 struct scsi_device *sdp = q->queuedata;
330 int ret;
332 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
333 ret = scsi_setup_blk_pc_cmnd(sdp, rq);
334 goto out;
335 } else if (rq->cmd_type != REQ_TYPE_FS) {
336 ret = BLKPREP_KILL;
337 goto out;
339 ret = scsi_setup_fs_cmnd(sdp, rq);
340 if (ret != BLKPREP_OK)
341 goto out;
342 SCpnt = rq->special;
343 cd = scsi_cd(rq->rq_disk);
345 /* from here on until we're complete, any goto out
346 * is used for a killable error condition */
347 ret = BLKPREP_KILL;
349 SCSI_LOG_HLQUEUE(1, printk("Doing sr request, dev = %s, block = %d\n",
350 cd->disk->disk_name, block));
352 if (!cd->device || !scsi_device_online(cd->device)) {
353 SCSI_LOG_HLQUEUE(2, printk("Finishing %u sectors\n",
354 blk_rq_sectors(rq)));
355 SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt));
356 goto out;
359 if (cd->device->changed) {
361 * quietly refuse to do anything to a changed disc until the
362 * changed bit has been reset
364 goto out;
368 * we do lazy blocksize switching (when reading XA sectors,
369 * see CDROMREADMODE2 ioctl)
371 s_size = cd->device->sector_size;
372 if (s_size > 2048) {
373 if (!in_interrupt())
374 sr_set_blocklength(cd, 2048);
375 else
376 printk("sr: can't switch blocksize: in interrupt\n");
379 if (s_size != 512 && s_size != 1024 && s_size != 2048) {
380 scmd_printk(KERN_ERR, SCpnt, "bad sector size %d\n", s_size);
381 goto out;
384 if (rq_data_dir(rq) == WRITE) {
385 if (!cd->device->writeable)
386 goto out;
387 SCpnt->cmnd[0] = WRITE_10;
388 SCpnt->sc_data_direction = DMA_TO_DEVICE;
389 cd->cdi.media_written = 1;
390 } else if (rq_data_dir(rq) == READ) {
391 SCpnt->cmnd[0] = READ_10;
392 SCpnt->sc_data_direction = DMA_FROM_DEVICE;
393 } else {
394 blk_dump_rq_flags(rq, "Unknown sr command");
395 goto out;
399 struct scatterlist *sg;
400 int i, size = 0, sg_count = scsi_sg_count(SCpnt);
402 scsi_for_each_sg(SCpnt, sg, sg_count, i)
403 size += sg->length;
405 if (size != scsi_bufflen(SCpnt)) {
406 scmd_printk(KERN_ERR, SCpnt,
407 "mismatch count %d, bytes %d\n",
408 size, scsi_bufflen(SCpnt));
409 if (scsi_bufflen(SCpnt) > size)
410 SCpnt->sdb.length = size;
415 * request doesn't start on hw block boundary, add scatter pads
417 if (((unsigned int)blk_rq_pos(rq) % (s_size >> 9)) ||
418 (scsi_bufflen(SCpnt) % s_size)) {
419 scmd_printk(KERN_NOTICE, SCpnt, "unaligned transfer\n");
420 goto out;
423 this_count = (scsi_bufflen(SCpnt) >> 9) / (s_size >> 9);
426 SCSI_LOG_HLQUEUE(2, printk("%s : %s %d/%u 512 byte blocks.\n",
427 cd->cdi.name,
428 (rq_data_dir(rq) == WRITE) ?
429 "writing" : "reading",
430 this_count, blk_rq_sectors(rq)));
432 SCpnt->cmnd[1] = 0;
433 block = (unsigned int)blk_rq_pos(rq) / (s_size >> 9);
435 if (this_count > 0xffff) {
436 this_count = 0xffff;
437 SCpnt->sdb.length = this_count * s_size;
440 SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
441 SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
442 SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
443 SCpnt->cmnd[5] = (unsigned char) block & 0xff;
444 SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
445 SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
446 SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
449 * We shouldn't disconnect in the middle of a sector, so with a dumb
450 * host adapter, it's safe to assume that we can at least transfer
451 * this many bytes between each connect / disconnect.
453 SCpnt->transfersize = cd->device->sector_size;
454 SCpnt->underflow = this_count << 9;
455 SCpnt->allowed = MAX_RETRIES;
458 * This indicates that the command is ready from our end to be
459 * queued.
461 ret = BLKPREP_OK;
462 out:
463 return scsi_prep_return(q, rq, ret);
466 static int sr_block_open(struct block_device *bdev, fmode_t mode)
468 struct scsi_cd *cd = scsi_cd_get(bdev->bd_disk);
469 int ret = -ENXIO;
471 if (cd) {
472 ret = cdrom_open(&cd->cdi, bdev, mode);
473 if (ret)
474 scsi_cd_put(cd);
476 return ret;
479 static int sr_block_release(struct gendisk *disk, fmode_t mode)
481 struct scsi_cd *cd = scsi_cd(disk);
482 cdrom_release(&cd->cdi, mode);
483 scsi_cd_put(cd);
484 return 0;
487 static int sr_block_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
488 unsigned long arg)
490 struct scsi_cd *cd = scsi_cd(bdev->bd_disk);
491 struct scsi_device *sdev = cd->device;
492 void __user *argp = (void __user *)arg;
493 int ret;
496 * Send SCSI addressing ioctls directly to mid level, send other
497 * ioctls to cdrom/block level.
499 switch (cmd) {
500 case SCSI_IOCTL_GET_IDLUN:
501 case SCSI_IOCTL_GET_BUS_NUMBER:
502 return scsi_ioctl(sdev, cmd, argp);
505 ret = cdrom_ioctl(&cd->cdi, bdev, mode, cmd, arg);
506 if (ret != -ENOSYS)
507 return ret;
510 * ENODEV means that we didn't recognise the ioctl, or that we
511 * cannot execute it in the current device state. In either
512 * case fall through to scsi_ioctl, which will return ENDOEV again
513 * if it doesn't recognise the ioctl
515 ret = scsi_nonblockable_ioctl(sdev, cmd, argp,
516 (mode & FMODE_NDELAY) != 0);
517 if (ret != -ENODEV)
518 return ret;
519 return scsi_ioctl(sdev, cmd, argp);
522 static int sr_block_media_changed(struct gendisk *disk)
524 struct scsi_cd *cd = scsi_cd(disk);
525 return cdrom_media_changed(&cd->cdi);
528 static const struct block_device_operations sr_bdops =
530 .owner = THIS_MODULE,
531 .open = sr_block_open,
532 .release = sr_block_release,
533 .locked_ioctl = sr_block_ioctl,
534 .media_changed = sr_block_media_changed,
536 * No compat_ioctl for now because sr_block_ioctl never
537 * seems to pass arbitary ioctls down to host drivers.
541 static int sr_open(struct cdrom_device_info *cdi, int purpose)
543 struct scsi_cd *cd = cdi->handle;
544 struct scsi_device *sdev = cd->device;
545 int retval;
548 * If the device is in error recovery, wait until it is done.
549 * If the device is offline, then disallow any access to it.
551 retval = -ENXIO;
552 if (!scsi_block_when_processing_errors(sdev))
553 goto error_out;
555 return 0;
557 error_out:
558 return retval;
561 static void sr_release(struct cdrom_device_info *cdi)
563 struct scsi_cd *cd = cdi->handle;
565 if (cd->device->sector_size > 2048)
566 sr_set_blocklength(cd, 2048);
570 static int sr_probe(struct device *dev)
572 struct scsi_device *sdev = to_scsi_device(dev);
573 struct gendisk *disk;
574 struct scsi_cd *cd;
575 int minor, error;
577 error = -ENODEV;
578 if (sdev->type != TYPE_ROM && sdev->type != TYPE_WORM)
579 goto fail;
581 error = -ENOMEM;
582 cd = kzalloc(sizeof(*cd), GFP_KERNEL);
583 if (!cd)
584 goto fail;
586 kref_init(&cd->kref);
588 disk = alloc_disk(1);
589 if (!disk)
590 goto fail_free;
592 spin_lock(&sr_index_lock);
593 minor = find_first_zero_bit(sr_index_bits, SR_DISKS);
594 if (minor == SR_DISKS) {
595 spin_unlock(&sr_index_lock);
596 error = -EBUSY;
597 goto fail_put;
599 __set_bit(minor, sr_index_bits);
600 spin_unlock(&sr_index_lock);
602 disk->major = SCSI_CDROM_MAJOR;
603 disk->first_minor = minor;
604 sprintf(disk->disk_name, "sr%d", minor);
605 disk->fops = &sr_bdops;
606 disk->flags = GENHD_FL_CD;
608 blk_queue_rq_timeout(sdev->request_queue, SR_TIMEOUT);
610 cd->device = sdev;
611 cd->disk = disk;
612 cd->driver = &sr_template;
613 cd->disk = disk;
614 cd->capacity = 0x1fffff;
615 cd->device->changed = 1; /* force recheck CD type */
616 cd->previous_state = 1;
617 cd->use = 1;
618 cd->readcd_known = 0;
619 cd->readcd_cdda = 0;
621 cd->cdi.ops = &sr_dops;
622 cd->cdi.handle = cd;
623 cd->cdi.mask = 0;
624 cd->cdi.capacity = 1;
625 sprintf(cd->cdi.name, "sr%d", minor);
627 sdev->sector_size = 2048; /* A guess, just in case */
629 /* FIXME: need to handle a get_capabilities failure properly ?? */
630 get_capabilities(cd);
631 blk_queue_prep_rq(sdev->request_queue, sr_prep_fn);
632 sr_vendor_init(cd);
634 disk->driverfs_dev = &sdev->sdev_gendev;
635 set_capacity(disk, cd->capacity);
636 disk->private_data = &cd->driver;
637 disk->queue = sdev->request_queue;
638 cd->cdi.disk = disk;
640 if (register_cdrom(&cd->cdi))
641 goto fail_put;
643 dev_set_drvdata(dev, cd);
644 disk->flags |= GENHD_FL_REMOVABLE;
645 add_disk(disk);
647 sdev_printk(KERN_DEBUG, sdev,
648 "Attached scsi CD-ROM %s\n", cd->cdi.name);
649 return 0;
651 fail_put:
652 put_disk(disk);
653 fail_free:
654 kfree(cd);
655 fail:
656 return error;
660 static void get_sectorsize(struct scsi_cd *cd)
662 unsigned char cmd[10];
663 unsigned char buffer[8];
664 int the_result, retries = 3;
665 int sector_size;
666 struct request_queue *queue;
668 do {
669 cmd[0] = READ_CAPACITY;
670 memset((void *) &cmd[1], 0, 9);
671 memset(buffer, 0, sizeof(buffer));
673 /* Do the command and wait.. */
674 the_result = scsi_execute_req(cd->device, cmd, DMA_FROM_DEVICE,
675 buffer, sizeof(buffer), NULL,
676 SR_TIMEOUT, MAX_RETRIES, NULL);
678 retries--;
680 } while (the_result && retries);
683 if (the_result) {
684 cd->capacity = 0x1fffff;
685 sector_size = 2048; /* A guess, just in case */
686 } else {
687 long last_written;
689 cd->capacity = 1 + ((buffer[0] << 24) | (buffer[1] << 16) |
690 (buffer[2] << 8) | buffer[3]);
692 * READ_CAPACITY doesn't return the correct size on
693 * certain UDF media. If last_written is larger, use
694 * it instead.
696 * http://bugzilla.kernel.org/show_bug.cgi?id=9668
698 if (!cdrom_get_last_written(&cd->cdi, &last_written))
699 cd->capacity = max_t(long, cd->capacity, last_written);
701 sector_size = (buffer[4] << 24) |
702 (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
703 switch (sector_size) {
705 * HP 4020i CD-Recorder reports 2340 byte sectors
706 * Philips CD-Writers report 2352 byte sectors
708 * Use 2k sectors for them..
710 case 0:
711 case 2340:
712 case 2352:
713 sector_size = 2048;
714 /* fall through */
715 case 2048:
716 cd->capacity *= 4;
717 /* fall through */
718 case 512:
719 break;
720 default:
721 printk("%s: unsupported sector size %d.\n",
722 cd->cdi.name, sector_size);
723 cd->capacity = 0;
726 cd->device->sector_size = sector_size;
729 * Add this so that we have the ability to correctly gauge
730 * what the device is capable of.
732 set_capacity(cd->disk, cd->capacity);
735 queue = cd->device->request_queue;
736 blk_queue_logical_block_size(queue, sector_size);
738 return;
741 static void get_capabilities(struct scsi_cd *cd)
743 unsigned char *buffer;
744 struct scsi_mode_data data;
745 struct scsi_sense_hdr sshdr;
746 int rc, n;
748 static const char *loadmech[] =
750 "caddy",
751 "tray",
752 "pop-up",
754 "changer",
755 "cartridge changer",
761 /* allocate transfer buffer */
762 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
763 if (!buffer) {
764 printk(KERN_ERR "sr: out of memory.\n");
765 return;
768 /* eat unit attentions */
769 sr_test_unit_ready(cd->device, &sshdr);
771 /* ask for mode page 0x2a */
772 rc = scsi_mode_sense(cd->device, 0, 0x2a, buffer, 128,
773 SR_TIMEOUT, 3, &data, NULL);
775 if (!scsi_status_is_good(rc)) {
776 /* failed, drive doesn't have capabilities mode page */
777 cd->cdi.speed = 1;
778 cd->cdi.mask |= (CDC_CD_R | CDC_CD_RW | CDC_DVD_R |
779 CDC_DVD | CDC_DVD_RAM |
780 CDC_SELECT_DISC | CDC_SELECT_SPEED |
781 CDC_MRW | CDC_MRW_W | CDC_RAM);
782 kfree(buffer);
783 printk("%s: scsi-1 drive\n", cd->cdi.name);
784 return;
787 n = data.header_length + data.block_descriptor_length;
788 cd->cdi.speed = ((buffer[n + 8] << 8) + buffer[n + 9]) / 176;
789 cd->readcd_known = 1;
790 cd->readcd_cdda = buffer[n + 5] & 0x01;
791 /* print some capability bits */
792 printk("%s: scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n", cd->cdi.name,
793 ((buffer[n + 14] << 8) + buffer[n + 15]) / 176,
794 cd->cdi.speed,
795 buffer[n + 3] & 0x01 ? "writer " : "", /* CD Writer */
796 buffer[n + 3] & 0x20 ? "dvd-ram " : "",
797 buffer[n + 2] & 0x02 ? "cd/rw " : "", /* can read rewriteable */
798 buffer[n + 4] & 0x20 ? "xa/form2 " : "", /* can read xa/from2 */
799 buffer[n + 5] & 0x01 ? "cdda " : "", /* can read audio data */
800 loadmech[buffer[n + 6] >> 5]);
801 if ((buffer[n + 6] >> 5) == 0)
802 /* caddy drives can't close tray... */
803 cd->cdi.mask |= CDC_CLOSE_TRAY;
804 if ((buffer[n + 2] & 0x8) == 0)
805 /* not a DVD drive */
806 cd->cdi.mask |= CDC_DVD;
807 if ((buffer[n + 3] & 0x20) == 0)
808 /* can't write DVD-RAM media */
809 cd->cdi.mask |= CDC_DVD_RAM;
810 if ((buffer[n + 3] & 0x10) == 0)
811 /* can't write DVD-R media */
812 cd->cdi.mask |= CDC_DVD_R;
813 if ((buffer[n + 3] & 0x2) == 0)
814 /* can't write CD-RW media */
815 cd->cdi.mask |= CDC_CD_RW;
816 if ((buffer[n + 3] & 0x1) == 0)
817 /* can't write CD-R media */
818 cd->cdi.mask |= CDC_CD_R;
819 if ((buffer[n + 6] & 0x8) == 0)
820 /* can't eject */
821 cd->cdi.mask |= CDC_OPEN_TRAY;
823 if ((buffer[n + 6] >> 5) == mechtype_individual_changer ||
824 (buffer[n + 6] >> 5) == mechtype_cartridge_changer)
825 cd->cdi.capacity =
826 cdrom_number_of_slots(&cd->cdi);
827 if (cd->cdi.capacity <= 1)
828 /* not a changer */
829 cd->cdi.mask |= CDC_SELECT_DISC;
830 /*else I don't think it can close its tray
831 cd->cdi.mask |= CDC_CLOSE_TRAY; */
834 * if DVD-RAM, MRW-W or CD-RW, we are randomly writable
836 if ((cd->cdi.mask & (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) !=
837 (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) {
838 cd->device->writeable = 1;
841 kfree(buffer);
845 * sr_packet() is the entry point for the generic commands generated
846 * by the Uniform CD-ROM layer.
848 static int sr_packet(struct cdrom_device_info *cdi,
849 struct packet_command *cgc)
851 if (cgc->timeout <= 0)
852 cgc->timeout = IOCTL_TIMEOUT;
854 sr_do_ioctl(cdi->handle, cgc);
856 return cgc->stat;
860 * sr_kref_release - Called to free the scsi_cd structure
861 * @kref: pointer to embedded kref
863 * sr_ref_mutex must be held entering this routine. Because it is
864 * called on last put, you should always use the scsi_cd_get()
865 * scsi_cd_put() helpers which manipulate the semaphore directly
866 * and never do a direct kref_put().
868 static void sr_kref_release(struct kref *kref)
870 struct scsi_cd *cd = container_of(kref, struct scsi_cd, kref);
871 struct gendisk *disk = cd->disk;
873 spin_lock(&sr_index_lock);
874 clear_bit(MINOR(disk_devt(disk)), sr_index_bits);
875 spin_unlock(&sr_index_lock);
877 unregister_cdrom(&cd->cdi);
879 disk->private_data = NULL;
881 put_disk(disk);
883 kfree(cd);
886 static int sr_remove(struct device *dev)
888 struct scsi_cd *cd = dev_get_drvdata(dev);
890 blk_queue_prep_rq(cd->device->request_queue, scsi_prep_fn);
891 del_gendisk(cd->disk);
893 mutex_lock(&sr_ref_mutex);
894 kref_put(&cd->kref, sr_kref_release);
895 mutex_unlock(&sr_ref_mutex);
897 return 0;
900 static int __init init_sr(void)
902 int rc;
904 rc = register_blkdev(SCSI_CDROM_MAJOR, "sr");
905 if (rc)
906 return rc;
907 rc = scsi_register_driver(&sr_template.gendrv);
908 if (rc)
909 unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
911 return rc;
914 static void __exit exit_sr(void)
916 scsi_unregister_driver(&sr_template.gendrv);
917 unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
920 module_init(init_sr);
921 module_exit(exit_sr);
922 MODULE_LICENSE("GPL");