2 * sr.c Copyright (C) 1992 David Giller
3 * Copyright (C) 1993, 1994, 1995, 1999 Eric Youngdale
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
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
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>
37 #include <linux/kernel.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 <linux/smp_lock.h>
48 #include <linux/slab.h>
49 #include <asm/uaccess.h>
51 #include <scsi/scsi.h>
52 #include <scsi/scsi_dbg.h>
53 #include <scsi/scsi_device.h>
54 #include <scsi/scsi_driver.h>
55 #include <scsi/scsi_cmnd.h>
56 #include <scsi/scsi_eh.h>
57 #include <scsi/scsi_host.h>
58 #include <scsi/scsi_ioctl.h> /* For the door lock/unlock commands */
60 #include "scsi_logging.h"
64 MODULE_DESCRIPTION("SCSI cdrom (sr) driver");
65 MODULE_LICENSE("GPL");
66 MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_CDROM_MAJOR
);
67 MODULE_ALIAS_SCSI_DEVICE(TYPE_ROM
);
68 MODULE_ALIAS_SCSI_DEVICE(TYPE_WORM
);
72 #define SR_CAPABILITIES \
73 (CDC_CLOSE_TRAY|CDC_OPEN_TRAY|CDC_LOCK|CDC_SELECT_SPEED| \
74 CDC_SELECT_DISC|CDC_MULTI_SESSION|CDC_MCN|CDC_MEDIA_CHANGED| \
75 CDC_PLAY_AUDIO|CDC_RESET|CDC_DRIVE_STATUS| \
76 CDC_CD_R|CDC_CD_RW|CDC_DVD|CDC_DVD_R|CDC_DVD_RAM|CDC_GENERIC_PACKET| \
77 CDC_MRW|CDC_MRW_W|CDC_RAM)
79 static int sr_probe(struct device
*);
80 static int sr_remove(struct device
*);
81 static int sr_done(struct scsi_cmnd
*);
83 static struct scsi_driver sr_template
= {
93 static unsigned long sr_index_bits
[SR_DISKS
/ BITS_PER_LONG
];
94 static DEFINE_SPINLOCK(sr_index_lock
);
96 /* This semaphore is used to mediate the 0->1 reference get in the
97 * face of object destruction (i.e. we can't allow a get on an
98 * object after last put) */
99 static DEFINE_MUTEX(sr_ref_mutex
);
101 static int sr_open(struct cdrom_device_info
*, int);
102 static void sr_release(struct cdrom_device_info
*);
104 static void get_sectorsize(struct scsi_cd
*);
105 static void get_capabilities(struct scsi_cd
*);
107 static int sr_media_change(struct cdrom_device_info
*, int);
108 static int sr_packet(struct cdrom_device_info
*, struct packet_command
*);
110 static struct cdrom_device_ops sr_dops
= {
112 .release
= sr_release
,
113 .drive_status
= sr_drive_status
,
114 .media_changed
= sr_media_change
,
115 .tray_move
= sr_tray_move
,
116 .lock_door
= sr_lock_door
,
117 .select_speed
= sr_select_speed
,
118 .get_last_session
= sr_get_last_session
,
119 .get_mcn
= sr_get_mcn
,
121 .audio_ioctl
= sr_audio_ioctl
,
122 .capability
= SR_CAPABILITIES
,
123 .generic_packet
= sr_packet
,
126 static void sr_kref_release(struct kref
*kref
);
128 static inline struct scsi_cd
*scsi_cd(struct gendisk
*disk
)
130 return container_of(disk
->private_data
, struct scsi_cd
, driver
);
134 * The get and put routines for the struct scsi_cd. Note this entity
135 * has a scsi_device pointer and owns a reference to this.
137 static inline struct scsi_cd
*scsi_cd_get(struct gendisk
*disk
)
139 struct scsi_cd
*cd
= NULL
;
141 mutex_lock(&sr_ref_mutex
);
142 if (disk
->private_data
== NULL
)
146 if (scsi_device_get(cd
->device
))
151 kref_put(&cd
->kref
, sr_kref_release
);
154 mutex_unlock(&sr_ref_mutex
);
158 static void scsi_cd_put(struct scsi_cd
*cd
)
160 struct scsi_device
*sdev
= cd
->device
;
162 mutex_lock(&sr_ref_mutex
);
163 kref_put(&cd
->kref
, sr_kref_release
);
164 scsi_device_put(sdev
);
165 mutex_unlock(&sr_ref_mutex
);
168 /* identical to scsi_test_unit_ready except that it doesn't
169 * eat the NOT_READY returns for removable media */
170 int sr_test_unit_ready(struct scsi_device
*sdev
, struct scsi_sense_hdr
*sshdr
)
172 int retries
= MAX_RETRIES
;
174 u8 cmd
[] = {TEST_UNIT_READY
, 0, 0, 0, 0, 0 };
176 /* issue TEST_UNIT_READY until the initial startup UNIT_ATTENTION
177 * conditions are gone, or a timeout happens
180 the_result
= scsi_execute_req(sdev
, cmd
, DMA_NONE
, NULL
,
181 0, sshdr
, SR_TIMEOUT
,
183 if (scsi_sense_valid(sshdr
) &&
184 sshdr
->sense_key
== UNIT_ATTENTION
)
187 } while (retries
> 0 &&
188 (!scsi_status_is_good(the_result
) ||
189 (scsi_sense_valid(sshdr
) &&
190 sshdr
->sense_key
== UNIT_ATTENTION
)));
195 * This function checks to see if the media has been changed in the
196 * CDROM drive. It is possible that we have already sensed a change,
197 * or the drive may have sensed one and not yet reported it. We must
198 * be ready for either case. This function always reports the current
199 * value of the changed bit. If flag is 0, then the changed bit is reset.
200 * This function could be done as an ioctl, but we would need to have
201 * an inode for that to work, and we do not always have one.
204 static int sr_media_change(struct cdrom_device_info
*cdi
, int slot
)
206 struct scsi_cd
*cd
= cdi
->handle
;
208 struct scsi_sense_hdr
*sshdr
;
210 if (CDSL_CURRENT
!= slot
) {
211 /* no changer support */
215 sshdr
= kzalloc(sizeof(*sshdr
), GFP_KERNEL
);
216 retval
= sr_test_unit_ready(cd
->device
, sshdr
);
217 if (retval
|| (scsi_sense_valid(sshdr
) &&
218 /* 0x3a is medium not present */
219 sshdr
->asc
== 0x3a)) {
220 /* Media not present or unable to test, unit probably not
221 * ready. This usually means there is no disc in the drive.
222 * Mark as changed, and we will figure it out later once
223 * the drive is available again.
225 cd
->device
->changed
= 1;
226 /* This will force a flush, if called from check_disk_change */
231 retval
= cd
->device
->changed
;
232 cd
->device
->changed
= 0;
233 /* If the disk changed, the capacity will now be different,
234 * so we force a re-read of this information */
236 /* check multisession offset etc */
242 /* Notify userspace, that media has changed. */
243 if (retval
!= cd
->previous_state
)
244 sdev_evt_send_simple(cd
->device
, SDEV_EVT_MEDIA_CHANGE
,
246 cd
->previous_state
= retval
;
253 * sr_done is the interrupt routine for the device driver.
255 * It will be notified on the end of a SCSI read / write, and will take one
256 * of several actions based on success or failure.
258 static int sr_done(struct scsi_cmnd
*SCpnt
)
260 int result
= SCpnt
->result
;
261 int this_count
= scsi_bufflen(SCpnt
);
262 int good_bytes
= (result
== 0 ? this_count
: 0);
263 int block_sectors
= 0;
265 struct scsi_cd
*cd
= scsi_cd(SCpnt
->request
->rq_disk
);
268 printk("sr.c done: %x\n", result
);
272 * Handle MEDIUM ERRORs or VOLUME OVERFLOWs that indicate partial
273 * success. Since this is a relatively rare error condition, no
274 * care is taken to avoid unnecessary additional work such as
275 * memcpy's that could be avoided.
277 if (driver_byte(result
) != 0 && /* An error occurred */
278 (SCpnt
->sense_buffer
[0] & 0x7f) == 0x70) { /* Sense current */
279 switch (SCpnt
->sense_buffer
[2]) {
281 case VOLUME_OVERFLOW
:
282 case ILLEGAL_REQUEST
:
283 if (!(SCpnt
->sense_buffer
[0] & 0x90))
285 error_sector
= (SCpnt
->sense_buffer
[3] << 24) |
286 (SCpnt
->sense_buffer
[4] << 16) |
287 (SCpnt
->sense_buffer
[5] << 8) |
288 SCpnt
->sense_buffer
[6];
289 if (SCpnt
->request
->bio
!= NULL
)
291 bio_sectors(SCpnt
->request
->bio
);
292 if (block_sectors
< 4)
294 if (cd
->device
->sector_size
== 2048)
296 error_sector
&= ~(block_sectors
- 1);
297 good_bytes
= (error_sector
-
298 blk_rq_pos(SCpnt
->request
)) << 9;
299 if (good_bytes
< 0 || good_bytes
>= this_count
)
302 * The SCSI specification allows for the value
303 * returned by READ CAPACITY to be up to 75 2K
304 * sectors past the last readable block.
305 * Therefore, if we hit a medium error within the
306 * last 75 2K sectors, we decrease the saved size
309 if (error_sector
< get_capacity(cd
->disk
) &&
310 cd
->capacity
- error_sector
< 4 * 75)
311 set_capacity(cd
->disk
, error_sector
);
314 case RECOVERED_ERROR
:
315 good_bytes
= this_count
;
326 static int sr_prep_fn(struct request_queue
*q
, struct request
*rq
)
328 int block
= 0, this_count
, s_size
;
330 struct scsi_cmnd
*SCpnt
;
331 struct scsi_device
*sdp
= q
->queuedata
;
334 if (rq
->cmd_type
== REQ_TYPE_BLOCK_PC
) {
335 ret
= scsi_setup_blk_pc_cmnd(sdp
, rq
);
337 } else if (rq
->cmd_type
!= REQ_TYPE_FS
) {
341 ret
= scsi_setup_fs_cmnd(sdp
, rq
);
342 if (ret
!= BLKPREP_OK
)
345 cd
= scsi_cd(rq
->rq_disk
);
347 /* from here on until we're complete, any goto out
348 * is used for a killable error condition */
351 SCSI_LOG_HLQUEUE(1, printk("Doing sr request, dev = %s, block = %d\n",
352 cd
->disk
->disk_name
, block
));
354 if (!cd
->device
|| !scsi_device_online(cd
->device
)) {
355 SCSI_LOG_HLQUEUE(2, printk("Finishing %u sectors\n",
356 blk_rq_sectors(rq
)));
357 SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt
));
361 if (cd
->device
->changed
) {
363 * quietly refuse to do anything to a changed disc until the
364 * changed bit has been reset
370 * we do lazy blocksize switching (when reading XA sectors,
371 * see CDROMREADMODE2 ioctl)
373 s_size
= cd
->device
->sector_size
;
376 sr_set_blocklength(cd
, 2048);
378 printk("sr: can't switch blocksize: in interrupt\n");
381 if (s_size
!= 512 && s_size
!= 1024 && s_size
!= 2048) {
382 scmd_printk(KERN_ERR
, SCpnt
, "bad sector size %d\n", s_size
);
386 if (rq_data_dir(rq
) == WRITE
) {
387 if (!cd
->device
->writeable
)
389 SCpnt
->cmnd
[0] = WRITE_10
;
390 SCpnt
->sc_data_direction
= DMA_TO_DEVICE
;
391 cd
->cdi
.media_written
= 1;
392 } else if (rq_data_dir(rq
) == READ
) {
393 SCpnt
->cmnd
[0] = READ_10
;
394 SCpnt
->sc_data_direction
= DMA_FROM_DEVICE
;
396 blk_dump_rq_flags(rq
, "Unknown sr command");
401 struct scatterlist
*sg
;
402 int i
, size
= 0, sg_count
= scsi_sg_count(SCpnt
);
404 scsi_for_each_sg(SCpnt
, sg
, sg_count
, i
)
407 if (size
!= scsi_bufflen(SCpnt
)) {
408 scmd_printk(KERN_ERR
, SCpnt
,
409 "mismatch count %d, bytes %d\n",
410 size
, scsi_bufflen(SCpnt
));
411 if (scsi_bufflen(SCpnt
) > size
)
412 SCpnt
->sdb
.length
= size
;
417 * request doesn't start on hw block boundary, add scatter pads
419 if (((unsigned int)blk_rq_pos(rq
) % (s_size
>> 9)) ||
420 (scsi_bufflen(SCpnt
) % s_size
)) {
421 scmd_printk(KERN_NOTICE
, SCpnt
, "unaligned transfer\n");
425 this_count
= (scsi_bufflen(SCpnt
) >> 9) / (s_size
>> 9);
428 SCSI_LOG_HLQUEUE(2, printk("%s : %s %d/%u 512 byte blocks.\n",
430 (rq_data_dir(rq
) == WRITE
) ?
431 "writing" : "reading",
432 this_count
, blk_rq_sectors(rq
)));
435 block
= (unsigned int)blk_rq_pos(rq
) / (s_size
>> 9);
437 if (this_count
> 0xffff) {
439 SCpnt
->sdb
.length
= this_count
* s_size
;
442 SCpnt
->cmnd
[2] = (unsigned char) (block
>> 24) & 0xff;
443 SCpnt
->cmnd
[3] = (unsigned char) (block
>> 16) & 0xff;
444 SCpnt
->cmnd
[4] = (unsigned char) (block
>> 8) & 0xff;
445 SCpnt
->cmnd
[5] = (unsigned char) block
& 0xff;
446 SCpnt
->cmnd
[6] = SCpnt
->cmnd
[9] = 0;
447 SCpnt
->cmnd
[7] = (unsigned char) (this_count
>> 8) & 0xff;
448 SCpnt
->cmnd
[8] = (unsigned char) this_count
& 0xff;
451 * We shouldn't disconnect in the middle of a sector, so with a dumb
452 * host adapter, it's safe to assume that we can at least transfer
453 * this many bytes between each connect / disconnect.
455 SCpnt
->transfersize
= cd
->device
->sector_size
;
456 SCpnt
->underflow
= this_count
<< 9;
457 SCpnt
->allowed
= MAX_RETRIES
;
460 * This indicates that the command is ready from our end to be
465 return scsi_prep_return(q
, rq
, ret
);
468 static int sr_block_open(struct block_device
*bdev
, fmode_t mode
)
470 struct scsi_cd
*cd
= scsi_cd_get(bdev
->bd_disk
);
474 ret
= cdrom_open(&cd
->cdi
, bdev
, mode
);
481 static int sr_block_release(struct gendisk
*disk
, fmode_t mode
)
483 struct scsi_cd
*cd
= scsi_cd(disk
);
484 cdrom_release(&cd
->cdi
, mode
);
489 static int sr_block_ioctl(struct block_device
*bdev
, fmode_t mode
, unsigned cmd
,
492 struct scsi_cd
*cd
= scsi_cd(bdev
->bd_disk
);
493 struct scsi_device
*sdev
= cd
->device
;
494 void __user
*argp
= (void __user
*)arg
;
500 * Send SCSI addressing ioctls directly to mid level, send other
501 * ioctls to cdrom/block level.
504 case SCSI_IOCTL_GET_IDLUN
:
505 case SCSI_IOCTL_GET_BUS_NUMBER
:
506 ret
= scsi_ioctl(sdev
, cmd
, argp
);
510 ret
= cdrom_ioctl(&cd
->cdi
, bdev
, mode
, cmd
, arg
);
515 * ENODEV means that we didn't recognise the ioctl, or that we
516 * cannot execute it in the current device state. In either
517 * case fall through to scsi_ioctl, which will return ENDOEV again
518 * if it doesn't recognise the ioctl
520 ret
= scsi_nonblockable_ioctl(sdev
, cmd
, argp
,
521 (mode
& FMODE_NDELAY
) != 0);
524 ret
= scsi_ioctl(sdev
, cmd
, argp
);
531 static int sr_block_media_changed(struct gendisk
*disk
)
533 struct scsi_cd
*cd
= scsi_cd(disk
);
534 return cdrom_media_changed(&cd
->cdi
);
537 static const struct block_device_operations sr_bdops
=
539 .owner
= THIS_MODULE
,
540 .open
= sr_block_open
,
541 .release
= sr_block_release
,
542 .ioctl
= sr_block_ioctl
,
543 .media_changed
= sr_block_media_changed
,
545 * No compat_ioctl for now because sr_block_ioctl never
546 * seems to pass arbitary ioctls down to host drivers.
550 static int sr_open(struct cdrom_device_info
*cdi
, int purpose
)
552 struct scsi_cd
*cd
= cdi
->handle
;
553 struct scsi_device
*sdev
= cd
->device
;
557 * If the device is in error recovery, wait until it is done.
558 * If the device is offline, then disallow any access to it.
561 if (!scsi_block_when_processing_errors(sdev
))
570 static void sr_release(struct cdrom_device_info
*cdi
)
572 struct scsi_cd
*cd
= cdi
->handle
;
574 if (cd
->device
->sector_size
> 2048)
575 sr_set_blocklength(cd
, 2048);
579 static int sr_probe(struct device
*dev
)
581 struct scsi_device
*sdev
= to_scsi_device(dev
);
582 struct gendisk
*disk
;
587 if (sdev
->type
!= TYPE_ROM
&& sdev
->type
!= TYPE_WORM
)
591 cd
= kzalloc(sizeof(*cd
), GFP_KERNEL
);
595 kref_init(&cd
->kref
);
597 disk
= alloc_disk(1);
601 spin_lock(&sr_index_lock
);
602 minor
= find_first_zero_bit(sr_index_bits
, SR_DISKS
);
603 if (minor
== SR_DISKS
) {
604 spin_unlock(&sr_index_lock
);
608 __set_bit(minor
, sr_index_bits
);
609 spin_unlock(&sr_index_lock
);
611 disk
->major
= SCSI_CDROM_MAJOR
;
612 disk
->first_minor
= minor
;
613 sprintf(disk
->disk_name
, "sr%d", minor
);
614 disk
->fops
= &sr_bdops
;
615 disk
->flags
= GENHD_FL_CD
;
617 blk_queue_rq_timeout(sdev
->request_queue
, SR_TIMEOUT
);
621 cd
->driver
= &sr_template
;
623 cd
->capacity
= 0x1fffff;
624 cd
->device
->changed
= 1; /* force recheck CD type */
625 cd
->previous_state
= 1;
627 cd
->readcd_known
= 0;
630 cd
->cdi
.ops
= &sr_dops
;
633 cd
->cdi
.capacity
= 1;
634 sprintf(cd
->cdi
.name
, "sr%d", minor
);
636 sdev
->sector_size
= 2048; /* A guess, just in case */
638 /* FIXME: need to handle a get_capabilities failure properly ?? */
639 get_capabilities(cd
);
640 blk_queue_prep_rq(sdev
->request_queue
, sr_prep_fn
);
643 disk
->driverfs_dev
= &sdev
->sdev_gendev
;
644 set_capacity(disk
, cd
->capacity
);
645 disk
->private_data
= &cd
->driver
;
646 disk
->queue
= sdev
->request_queue
;
649 if (register_cdrom(&cd
->cdi
))
652 dev_set_drvdata(dev
, cd
);
653 disk
->flags
|= GENHD_FL_REMOVABLE
;
656 sdev_printk(KERN_DEBUG
, sdev
,
657 "Attached scsi CD-ROM %s\n", cd
->cdi
.name
);
669 static void get_sectorsize(struct scsi_cd
*cd
)
671 unsigned char cmd
[10];
672 unsigned char buffer
[8];
673 int the_result
, retries
= 3;
675 struct request_queue
*queue
;
678 cmd
[0] = READ_CAPACITY
;
679 memset((void *) &cmd
[1], 0, 9);
680 memset(buffer
, 0, sizeof(buffer
));
682 /* Do the command and wait.. */
683 the_result
= scsi_execute_req(cd
->device
, cmd
, DMA_FROM_DEVICE
,
684 buffer
, sizeof(buffer
), NULL
,
685 SR_TIMEOUT
, MAX_RETRIES
, NULL
);
689 } while (the_result
&& retries
);
693 cd
->capacity
= 0x1fffff;
694 sector_size
= 2048; /* A guess, just in case */
698 cd
->capacity
= 1 + ((buffer
[0] << 24) | (buffer
[1] << 16) |
699 (buffer
[2] << 8) | buffer
[3]);
701 * READ_CAPACITY doesn't return the correct size on
702 * certain UDF media. If last_written is larger, use
705 * http://bugzilla.kernel.org/show_bug.cgi?id=9668
707 if (!cdrom_get_last_written(&cd
->cdi
, &last_written
))
708 cd
->capacity
= max_t(long, cd
->capacity
, last_written
);
710 sector_size
= (buffer
[4] << 24) |
711 (buffer
[5] << 16) | (buffer
[6] << 8) | buffer
[7];
712 switch (sector_size
) {
714 * HP 4020i CD-Recorder reports 2340 byte sectors
715 * Philips CD-Writers report 2352 byte sectors
717 * Use 2k sectors for them..
730 printk("%s: unsupported sector size %d.\n",
731 cd
->cdi
.name
, sector_size
);
735 cd
->device
->sector_size
= sector_size
;
738 * Add this so that we have the ability to correctly gauge
739 * what the device is capable of.
741 set_capacity(cd
->disk
, cd
->capacity
);
744 queue
= cd
->device
->request_queue
;
745 blk_queue_logical_block_size(queue
, sector_size
);
750 static void get_capabilities(struct scsi_cd
*cd
)
752 unsigned char *buffer
;
753 struct scsi_mode_data data
;
754 struct scsi_sense_hdr sshdr
;
757 static const char *loadmech
[] =
770 /* allocate transfer buffer */
771 buffer
= kmalloc(512, GFP_KERNEL
| GFP_DMA
);
773 printk(KERN_ERR
"sr: out of memory.\n");
777 /* eat unit attentions */
778 sr_test_unit_ready(cd
->device
, &sshdr
);
780 /* ask for mode page 0x2a */
781 rc
= scsi_mode_sense(cd
->device
, 0, 0x2a, buffer
, 128,
782 SR_TIMEOUT
, 3, &data
, NULL
);
784 if (!scsi_status_is_good(rc
)) {
785 /* failed, drive doesn't have capabilities mode page */
787 cd
->cdi
.mask
|= (CDC_CD_R
| CDC_CD_RW
| CDC_DVD_R
|
788 CDC_DVD
| CDC_DVD_RAM
|
789 CDC_SELECT_DISC
| CDC_SELECT_SPEED
|
790 CDC_MRW
| CDC_MRW_W
| CDC_RAM
);
792 printk("%s: scsi-1 drive\n", cd
->cdi
.name
);
796 n
= data
.header_length
+ data
.block_descriptor_length
;
797 cd
->cdi
.speed
= ((buffer
[n
+ 8] << 8) + buffer
[n
+ 9]) / 176;
798 cd
->readcd_known
= 1;
799 cd
->readcd_cdda
= buffer
[n
+ 5] & 0x01;
800 /* print some capability bits */
801 printk("%s: scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n", cd
->cdi
.name
,
802 ((buffer
[n
+ 14] << 8) + buffer
[n
+ 15]) / 176,
804 buffer
[n
+ 3] & 0x01 ? "writer " : "", /* CD Writer */
805 buffer
[n
+ 3] & 0x20 ? "dvd-ram " : "",
806 buffer
[n
+ 2] & 0x02 ? "cd/rw " : "", /* can read rewriteable */
807 buffer
[n
+ 4] & 0x20 ? "xa/form2 " : "", /* can read xa/from2 */
808 buffer
[n
+ 5] & 0x01 ? "cdda " : "", /* can read audio data */
809 loadmech
[buffer
[n
+ 6] >> 5]);
810 if ((buffer
[n
+ 6] >> 5) == 0)
811 /* caddy drives can't close tray... */
812 cd
->cdi
.mask
|= CDC_CLOSE_TRAY
;
813 if ((buffer
[n
+ 2] & 0x8) == 0)
814 /* not a DVD drive */
815 cd
->cdi
.mask
|= CDC_DVD
;
816 if ((buffer
[n
+ 3] & 0x20) == 0)
817 /* can't write DVD-RAM media */
818 cd
->cdi
.mask
|= CDC_DVD_RAM
;
819 if ((buffer
[n
+ 3] & 0x10) == 0)
820 /* can't write DVD-R media */
821 cd
->cdi
.mask
|= CDC_DVD_R
;
822 if ((buffer
[n
+ 3] & 0x2) == 0)
823 /* can't write CD-RW media */
824 cd
->cdi
.mask
|= CDC_CD_RW
;
825 if ((buffer
[n
+ 3] & 0x1) == 0)
826 /* can't write CD-R media */
827 cd
->cdi
.mask
|= CDC_CD_R
;
828 if ((buffer
[n
+ 6] & 0x8) == 0)
830 cd
->cdi
.mask
|= CDC_OPEN_TRAY
;
832 if ((buffer
[n
+ 6] >> 5) == mechtype_individual_changer
||
833 (buffer
[n
+ 6] >> 5) == mechtype_cartridge_changer
)
835 cdrom_number_of_slots(&cd
->cdi
);
836 if (cd
->cdi
.capacity
<= 1)
838 cd
->cdi
.mask
|= CDC_SELECT_DISC
;
839 /*else I don't think it can close its tray
840 cd->cdi.mask |= CDC_CLOSE_TRAY; */
843 * if DVD-RAM, MRW-W or CD-RW, we are randomly writable
845 if ((cd
->cdi
.mask
& (CDC_DVD_RAM
| CDC_MRW_W
| CDC_RAM
| CDC_CD_RW
)) !=
846 (CDC_DVD_RAM
| CDC_MRW_W
| CDC_RAM
| CDC_CD_RW
)) {
847 cd
->device
->writeable
= 1;
854 * sr_packet() is the entry point for the generic commands generated
855 * by the Uniform CD-ROM layer.
857 static int sr_packet(struct cdrom_device_info
*cdi
,
858 struct packet_command
*cgc
)
860 if (cgc
->timeout
<= 0)
861 cgc
->timeout
= IOCTL_TIMEOUT
;
863 sr_do_ioctl(cdi
->handle
, cgc
);
869 * sr_kref_release - Called to free the scsi_cd structure
870 * @kref: pointer to embedded kref
872 * sr_ref_mutex must be held entering this routine. Because it is
873 * called on last put, you should always use the scsi_cd_get()
874 * scsi_cd_put() helpers which manipulate the semaphore directly
875 * and never do a direct kref_put().
877 static void sr_kref_release(struct kref
*kref
)
879 struct scsi_cd
*cd
= container_of(kref
, struct scsi_cd
, kref
);
880 struct gendisk
*disk
= cd
->disk
;
882 spin_lock(&sr_index_lock
);
883 clear_bit(MINOR(disk_devt(disk
)), sr_index_bits
);
884 spin_unlock(&sr_index_lock
);
886 unregister_cdrom(&cd
->cdi
);
888 disk
->private_data
= NULL
;
895 static int sr_remove(struct device
*dev
)
897 struct scsi_cd
*cd
= dev_get_drvdata(dev
);
899 blk_queue_prep_rq(cd
->device
->request_queue
, scsi_prep_fn
);
900 del_gendisk(cd
->disk
);
902 mutex_lock(&sr_ref_mutex
);
903 kref_put(&cd
->kref
, sr_kref_release
);
904 mutex_unlock(&sr_ref_mutex
);
909 static int __init
init_sr(void)
913 rc
= register_blkdev(SCSI_CDROM_MAJOR
, "sr");
916 rc
= scsi_register_driver(&sr_template
.gendrv
);
918 unregister_blkdev(SCSI_CDROM_MAJOR
, "sr");
923 static void __exit
exit_sr(void)
925 scsi_unregister_driver(&sr_template
.gendrv
);
926 unregister_blkdev(SCSI_CDROM_MAJOR
, "sr");
929 module_init(init_sr
);
930 module_exit(exit_sr
);
931 MODULE_LICENSE("GPL");