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 loose the GHOST hack
31 * Modified by Arnaldo Carvalho de Melo <acme@conectiva.com.br>
32 * check resource allocation in sr_init and some cleanups
36 #include <linux/module.h>
39 #include <linux/kernel.h>
40 #include <linux/sched.h>
42 #include <linux/string.h>
43 #include <linux/errno.h>
44 #include <linux/cdrom.h>
45 #include <linux/interrupt.h>
46 #include <linux/init.h>
47 #include <asm/system.h>
49 #include <asm/uaccess.h>
51 #define MAJOR_NR SCSI_CDROM_MAJOR
52 #include <linux/blk.h>
56 #include <scsi/scsi_ioctl.h> /* For the door lock/unlock commands */
57 #include "constants.h"
59 MODULE_PARM(xa_test
, "i"); /* see sr_ioctl.c */
62 #define SR_TIMEOUT (30 * HZ)
64 static int sr_init(void);
65 static void sr_finish(void);
66 static int sr_attach(Scsi_Device
*);
67 static int sr_detect(Scsi_Device
*);
68 static void sr_detach(Scsi_Device
*);
70 static int sr_init_command(Scsi_Cmnd
*);
72 static struct Scsi_Device_Template sr_template
=
77 major
:SCSI_CDROM_MAJOR
,
84 init_command
:sr_init_command
90 static int *sr_blocksizes
;
91 static int *sr_hardsizes
;
93 static int sr_open(struct cdrom_device_info
*, int);
94 void get_sectorsize(int);
95 void get_capabilities(int);
97 static int sr_media_change(struct cdrom_device_info
*, int);
98 static int sr_packet(struct cdrom_device_info
*, struct cdrom_generic_command
*);
100 static void sr_release(struct cdrom_device_info
*cdi
)
102 if (scsi_CDs
[MINOR(cdi
->dev
)].device
->sector_size
> 2048)
103 sr_set_blocklength(MINOR(cdi
->dev
), 2048);
105 scsi_CDs
[MINOR(cdi
->dev
)].device
->access_count
--;
106 if (scsi_CDs
[MINOR(cdi
->dev
)].device
->host
->hostt
->module
)
107 __MOD_DEC_USE_COUNT(scsi_CDs
[MINOR(cdi
->dev
)].device
->host
->hostt
->module
);
108 if (sr_template
.module
)
109 __MOD_DEC_USE_COUNT(sr_template
.module
);
112 static struct cdrom_device_ops sr_dops
=
116 drive_status
: sr_drive_status
,
117 media_changed
: sr_media_change
,
118 tray_move
: sr_tray_move
,
119 lock_door
: sr_lock_door
,
120 select_speed
: sr_select_speed
,
121 get_last_session
: sr_get_last_session
,
124 audio_ioctl
: sr_audio_ioctl
,
125 dev_ioctl
: sr_dev_ioctl
,
126 capability
: CDC_CLOSE_TRAY
| CDC_OPEN_TRAY
| CDC_LOCK
|
127 CDC_SELECT_SPEED
| CDC_SELECT_DISC
|
128 CDC_MULTI_SESSION
| CDC_MCN
|
129 CDC_MEDIA_CHANGED
| CDC_PLAY_AUDIO
|
130 CDC_RESET
| CDC_IOCTLS
| CDC_DRIVE_STATUS
|
131 CDC_CD_R
| CDC_CD_RW
| CDC_DVD
| CDC_DVD_R
|
132 CDC_DVD_RAM
| CDC_GENERIC_PACKET
,
133 generic_packet
: sr_packet
,
137 * This function checks to see if the media has been changed in the
138 * CDROM drive. It is possible that we have already sensed a change,
139 * or the drive may have sensed one and not yet reported it. We must
140 * be ready for either case. This function always reports the current
141 * value of the changed bit. If flag is 0, then the changed bit is reset.
142 * This function could be done as an ioctl, but we would need to have
143 * an inode for that to work, and we do not always have one.
146 int sr_media_change(struct cdrom_device_info
*cdi
, int slot
)
150 if (CDSL_CURRENT
!= slot
) {
151 /* no changer support */
154 retval
= scsi_ioctl(scsi_CDs
[MINOR(cdi
->dev
)].device
,
155 SCSI_IOCTL_TEST_UNIT_READY
, 0);
158 /* Unable to test, unit probably not ready. This usually
159 * means there is no disc in the drive. Mark as changed,
160 * and we will figure it out later once the drive is
161 * available again. */
163 scsi_CDs
[MINOR(cdi
->dev
)].device
->changed
= 1;
164 return 1; /* This will force a flush, if called from
165 * check_disk_change */
168 retval
= scsi_CDs
[MINOR(cdi
->dev
)].device
->changed
;
169 scsi_CDs
[MINOR(cdi
->dev
)].device
->changed
= 0;
170 /* If the disk changed, the capacity will now be different,
171 * so we force a re-read of this information */
173 /* check multisession offset etc */
177 * If the disk changed, the capacity will now be different,
178 * so we force a re-read of this information
179 * Force 2048 for the sector size so that filesystems won't
180 * be trying to use something that is too small if the disc
183 scsi_CDs
[MINOR(cdi
->dev
)].needs_sector_size
= 1;
185 scsi_CDs
[MINOR(cdi
->dev
)].device
->sector_size
= 2048;
191 * rw_intr is the interrupt routine for the device driver. It will be notified on the
192 * end of a SCSI read / write, and will take on of several actions based on success or failure.
195 static void rw_intr(Scsi_Cmnd
* SCpnt
)
197 int result
= SCpnt
->result
;
198 int this_count
= SCpnt
->bufflen
>> 9;
199 int good_sectors
= (result
== 0 ? this_count
: 0);
200 int block_sectors
= 0;
201 int device_nr
= DEVICE_NR(SCpnt
->request
.rq_dev
);
204 printk("sr.c done: %x %p\n", result
, SCpnt
->request
.bh
->b_data
);
207 Handle MEDIUM ERRORs or VOLUME OVERFLOWs that indicate partial success.
208 Since this is a relatively rare error condition, no care is taken to
209 avoid unnecessary additional work such as memcpy's that could be avoided.
213 if (driver_byte(result
) != 0 && /* An error occurred */
214 SCpnt
->sense_buffer
[0] == 0xF0 && /* Sense data is valid */
215 (SCpnt
->sense_buffer
[2] == MEDIUM_ERROR
||
216 SCpnt
->sense_buffer
[2] == VOLUME_OVERFLOW
||
217 SCpnt
->sense_buffer
[2] == ILLEGAL_REQUEST
)) {
218 long error_sector
= (SCpnt
->sense_buffer
[3] << 24) |
219 (SCpnt
->sense_buffer
[4] << 16) |
220 (SCpnt
->sense_buffer
[5] << 8) |
221 SCpnt
->sense_buffer
[6];
222 if (SCpnt
->request
.bh
!= NULL
)
223 block_sectors
= SCpnt
->request
.bh
->b_size
>> 9;
224 if (block_sectors
< 4)
226 if (scsi_CDs
[device_nr
].device
->sector_size
== 2048)
228 error_sector
&= ~(block_sectors
- 1);
229 good_sectors
= error_sector
- SCpnt
->request
.sector
;
230 if (good_sectors
< 0 || good_sectors
>= this_count
)
233 * The SCSI specification allows for the value returned by READ
234 * CAPACITY to be up to 75 2K sectors past the last readable
235 * block. Therefore, if we hit a medium error within the last
236 * 75 2K sectors, we decrease the saved size value.
238 if ((error_sector
>> 1) < sr_sizes
[device_nr
] &&
239 scsi_CDs
[device_nr
].capacity
- error_sector
< 4 * 75)
240 sr_sizes
[device_nr
] = error_sector
>> 1;
244 * This calls the generic completion function, now that we know
245 * how many actual sectors finished, and how many sectors we need
246 * to say have failed.
248 scsi_io_completion(SCpnt
, good_sectors
, block_sectors
);
252 static request_queue_t
*sr_find_queue(kdev_t dev
)
257 if (MINOR(dev
) >= sr_template
.dev_max
|| !scsi_CDs
[MINOR(dev
)].device
)
260 return &scsi_CDs
[MINOR(dev
)].device
->request_queue
;
263 static int sr_scatter_pad(Scsi_Cmnd
*SCpnt
, int s_size
)
265 struct scatterlist
*sg
, *old_sg
= NULL
;
266 int i
, fsize
, bsize
, sg_ent
;
270 sg_ent
= SCpnt
->use_sg
;
271 bsize
= 0; /* gcc... */
276 if ((fsize
= SCpnt
->request
.sector
% (s_size
>> 9))) {
279 if ((front
= scsi_malloc(fsize
)) == NULL
)
283 * need a back pad too
285 if ((bsize
= s_size
- ((SCpnt
->request_bufflen
+ fsize
) % s_size
))) {
287 if ((back
= scsi_malloc(bsize
)) == NULL
)
292 * extend or allocate new scatter-gather table
295 old_sg
= (struct scatterlist
*) SCpnt
->request_buffer
;
301 SCpnt
->sglist_len
= ((sg_ent
* sizeof(struct scatterlist
)) + 511) & ~511;
302 if ((sg
= scsi_malloc(SCpnt
->sglist_len
)) == NULL
)
305 memset(sg
, 0, SCpnt
->sglist_len
);
309 sg
[0].address
= sg
[0].alt_address
= front
;
310 sg
[0].length
= fsize
;
314 memcpy(sg
+ i
, old_sg
, SCpnt
->use_sg
* sizeof(struct scatterlist
));
315 scsi_free(old_sg
, ((SCpnt
->use_sg
* sizeof(struct scatterlist
)) + 511) & ~511);
317 sg
[i
].address
= SCpnt
->request_buffer
;
318 sg
[i
].length
= SCpnt
->request_bufflen
;
321 SCpnt
->request_bufflen
+= (fsize
+ bsize
);
322 SCpnt
->request_buffer
= sg
;
326 sg
[SCpnt
->use_sg
].address
= back
;
327 sg
[SCpnt
->use_sg
].alt_address
= back
;
328 sg
[SCpnt
->use_sg
].length
= bsize
;
335 printk("sr: ran out of mem for scatter pad\n");
337 scsi_free(front
, fsize
);
339 scsi_free(back
, bsize
);
345 static int sr_init_command(Scsi_Cmnd
* SCpnt
)
347 int dev
, devm
, block
, this_count
, s_size
;
349 devm
= MINOR(SCpnt
->request
.rq_dev
);
350 dev
= DEVICE_NR(SCpnt
->request
.rq_dev
);
352 SCSI_LOG_HLQUEUE(1, printk("Doing sr request, dev = %d, block = %d\n", devm
, block
));
354 if (dev
>= sr_template
.nr_dev
||
355 !scsi_CDs
[dev
].device
||
356 !scsi_CDs
[dev
].device
->online
) {
357 SCSI_LOG_HLQUEUE(2, printk("Finishing %ld sectors\n", SCpnt
->request
.nr_sectors
));
358 SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt
));
361 if (scsi_CDs
[dev
].device
->changed
) {
363 * quietly refuse to do anything to a changed disc until the
364 * changed bit has been reset
369 if ((SCpnt
->request
.cmd
== WRITE
) && !scsi_CDs
[dev
].device
->writeable
)
373 * we do lazy blocksize switching (when reading XA sectors,
374 * see CDROMREADMODE2 ioctl)
376 s_size
= scsi_CDs
[dev
].device
->sector_size
;
379 sr_set_blocklength(DEVICE_NR(CURRENT
->rq_dev
), 2048);
381 printk("sr: can't switch blocksize: in interrupt\n");
384 if (s_size
!= 512 && s_size
!= 1024 && s_size
!= 2048) {
385 printk("sr: bad sector size %d\n", s_size
);
389 block
= SCpnt
->request
.sector
/ (s_size
>> 9);
392 * request doesn't start on hw block boundary, add scatter pads
394 if ((SCpnt
->request
.sector
% (s_size
>> 9)) || (SCpnt
->request_bufflen
% s_size
))
395 if (sr_scatter_pad(SCpnt
, s_size
))
398 this_count
= (SCpnt
->request_bufflen
>> 9) / (s_size
>> 9);
400 switch (SCpnt
->request
.cmd
) {
402 SCpnt
->cmnd
[0] = WRITE_10
;
403 SCpnt
->sc_data_direction
= SCSI_DATA_WRITE
;
406 SCpnt
->cmnd
[0] = READ_10
;
407 SCpnt
->sc_data_direction
= SCSI_DATA_READ
;
410 printk("Unknown sr command %d\n", SCpnt
->request
.cmd
);
414 SCSI_LOG_HLQUEUE(2, printk("sr%d : %s %d/%ld 512 byte blocks.\n",
416 (SCpnt
->request
.cmd
== WRITE
) ? "writing" : "reading",
417 this_count
, SCpnt
->request
.nr_sectors
));
419 SCpnt
->cmnd
[1] = (SCpnt
->lun
<< 5) & 0xe0;
421 if (this_count
> 0xffff)
424 SCpnt
->cmnd
[2] = (unsigned char) (block
>> 24) & 0xff;
425 SCpnt
->cmnd
[3] = (unsigned char) (block
>> 16) & 0xff;
426 SCpnt
->cmnd
[4] = (unsigned char) (block
>> 8) & 0xff;
427 SCpnt
->cmnd
[5] = (unsigned char) block
& 0xff;
428 SCpnt
->cmnd
[6] = SCpnt
->cmnd
[9] = 0;
429 SCpnt
->cmnd
[7] = (unsigned char) (this_count
>> 8) & 0xff;
430 SCpnt
->cmnd
[8] = (unsigned char) this_count
& 0xff;
433 * We shouldn't disconnect in the middle of a sector, so with a dumb
434 * host adapter, it's safe to assume that we can at least transfer
435 * this many bytes between each connect / disconnect.
437 SCpnt
->transfersize
= scsi_CDs
[dev
].device
->sector_size
;
438 SCpnt
->underflow
= this_count
<< 9;
440 SCpnt
->allowed
= MAX_RETRIES
;
441 SCpnt
->timeout_per_command
= SR_TIMEOUT
;
444 * This is the completion routine we use. This is matched in terms
445 * of capability to this function.
447 SCpnt
->done
= rw_intr
;
450 struct scatterlist
*sg
= SCpnt
->request_buffer
;
452 for (i
= 0; i
< SCpnt
->use_sg
; i
++)
453 size
+= sg
[i
].length
;
455 if (size
!= SCpnt
->request_bufflen
&& SCpnt
->use_sg
) {
456 printk("sr: mismatch count %d, bytes %d\n", size
, SCpnt
->request_bufflen
);
457 SCpnt
->request_bufflen
= size
;
462 * This indicates that the command is ready from our end to be
468 static int sr_open(struct cdrom_device_info
*cdi
, int purpose
)
470 check_disk_change(cdi
->dev
);
472 if (MINOR(cdi
->dev
) >= sr_template
.dev_max
473 || !scsi_CDs
[MINOR(cdi
->dev
)].device
) {
474 return -ENXIO
; /* No such device */
477 * If the device is in error recovery, wait until it is done.
478 * If the device is offline, then disallow any access to it.
480 if (!scsi_block_when_processing_errors(scsi_CDs
[MINOR(cdi
->dev
)].device
)) {
483 scsi_CDs
[MINOR(cdi
->dev
)].device
->access_count
++;
484 if (scsi_CDs
[MINOR(cdi
->dev
)].device
->host
->hostt
->module
)
485 __MOD_INC_USE_COUNT(scsi_CDs
[MINOR(cdi
->dev
)].device
->host
->hostt
->module
);
486 if (sr_template
.module
)
487 __MOD_INC_USE_COUNT(sr_template
.module
);
489 /* If this device did not have media in the drive at boot time, then
490 * we would have been unable to get the sector size. Check to see if
491 * this is the case, and try again.
494 if (scsi_CDs
[MINOR(cdi
->dev
)].needs_sector_size
)
495 get_sectorsize(MINOR(cdi
->dev
));
501 * do_sr_request() is the request handler function for the sr driver.
502 * Its function in life is to take block device requests, and
503 * translate them to SCSI commands.
507 static int sr_detect(Scsi_Device
* SDp
)
510 if (SDp
->type
!= TYPE_ROM
&& SDp
->type
!= TYPE_WORM
)
513 printk("Detected scsi CD-ROM sr%d at scsi%d, channel %d, id %d, lun %d\n",
514 sr_template
.dev_noticed
++,
515 SDp
->host
->host_no
, SDp
->channel
, SDp
->id
, SDp
->lun
);
520 static int sr_attach(Scsi_Device
* SDp
)
525 if (SDp
->type
!= TYPE_ROM
&& SDp
->type
!= TYPE_WORM
)
528 if (sr_template
.nr_dev
>= sr_template
.dev_max
) {
532 for (cpnt
= scsi_CDs
, i
= 0; i
< sr_template
.dev_max
; i
++, cpnt
++)
536 if (i
>= sr_template
.dev_max
)
537 panic("scsi_devices corrupt (sr)");
540 scsi_CDs
[i
].device
= SDp
;
542 sr_template
.nr_dev
++;
543 if (sr_template
.nr_dev
> sr_template
.dev_max
)
544 panic("scsi_devices corrupt (sr)");
549 void get_sectorsize(int i
)
551 unsigned char cmd
[10];
552 unsigned char *buffer
;
553 int the_result
, retries
;
557 buffer
= (unsigned char *) scsi_malloc(512);
558 SRpnt
= scsi_allocate_request(scsi_CDs
[i
].device
);
560 if(buffer
== NULL
|| SRpnt
== NULL
)
562 scsi_CDs
[i
].capacity
= 0x1fffff;
563 sector_size
= 2048; /* A guess, just in case */
564 scsi_CDs
[i
].needs_sector_size
= 1;
566 scsi_free(buffer
, 512);
568 scsi_release_request(SRpnt
);
574 cmd
[0] = READ_CAPACITY
;
575 cmd
[1] = (scsi_CDs
[i
].device
->lun
<< 5) & 0xe0;
576 memset((void *) &cmd
[2], 0, 8);
577 SRpnt
->sr_request
.rq_status
= RQ_SCSI_BUSY
; /* Mark as really busy */
578 SRpnt
->sr_cmd_len
= 0;
580 memset(buffer
, 0, 8);
582 /* Do the command and wait.. */
584 SRpnt
->sr_data_direction
= SCSI_DATA_READ
;
585 scsi_wait_req(SRpnt
, (void *) cmd
, (void *) buffer
,
586 8, SR_TIMEOUT
, MAX_RETRIES
);
588 the_result
= SRpnt
->sr_result
;
591 } while (the_result
&& retries
);
594 scsi_release_request(SRpnt
);
598 scsi_CDs
[i
].capacity
= 0x1fffff;
599 sector_size
= 2048; /* A guess, just in case */
600 scsi_CDs
[i
].needs_sector_size
= 1;
603 if (cdrom_get_last_written(MKDEV(MAJOR_NR
, i
),
604 (long *) &scsi_CDs
[i
].capacity
))
606 scsi_CDs
[i
].capacity
= 1 + ((buffer
[0] << 24) |
610 sector_size
= (buffer
[4] << 24) |
611 (buffer
[5] << 16) | (buffer
[6] << 8) | buffer
[7];
612 switch (sector_size
) {
614 * HP 4020i CD-Recorder reports 2340 byte sectors
615 * Philips CD-Writers report 2352 byte sectors
617 * Use 2k sectors for them..
625 scsi_CDs
[i
].capacity
*= 4;
630 printk("sr%d: unsupported sector size %d.\n",
632 scsi_CDs
[i
].capacity
= 0;
633 scsi_CDs
[i
].needs_sector_size
= 1;
636 scsi_CDs
[i
].device
->sector_size
= sector_size
;
639 * Add this so that we have the ability to correctly gauge
640 * what the device is capable of.
642 scsi_CDs
[i
].needs_sector_size
= 0;
643 sr_sizes
[i
] = scsi_CDs
[i
].capacity
>> (BLOCK_SIZE_BITS
- 9);
645 scsi_free(buffer
, 512);
648 void get_capabilities(int i
)
650 unsigned char cmd
[6];
651 unsigned char *buffer
;
654 static char *loadmech
[] =
666 buffer
= (unsigned char *) scsi_malloc(512);
668 cmd
[1] = (scsi_CDs
[i
].device
->lun
<< 5) & 0xe0;
672 rc
= sr_do_ioctl(i
, cmd
, buffer
, 128, 1, SCSI_DATA_READ
, NULL
);
675 /* failed, drive has'nt this mode page */
676 scsi_CDs
[i
].cdi
.speed
= 1;
677 /* disable speed select, drive probably can't do this either */
678 scsi_CDs
[i
].cdi
.mask
|= CDC_SELECT_SPEED
;
679 scsi_free(buffer
, 512);
683 scsi_CDs
[i
].cdi
.speed
= ((buffer
[n
+ 8] << 8) + buffer
[n
+ 9]) / 176;
684 scsi_CDs
[i
].readcd_known
= 1;
685 scsi_CDs
[i
].readcd_cdda
= buffer
[n
+ 5] & 0x01;
686 /* print some capability bits */
687 printk("sr%i: scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n", i
,
688 ((buffer
[n
+ 14] << 8) + buffer
[n
+ 15]) / 176,
689 scsi_CDs
[i
].cdi
.speed
,
690 buffer
[n
+ 3] & 0x01 ? "writer " : "", /* CD Writer */
691 buffer
[n
+ 3] & 0x20 ? "dvd-ram " : "",
692 buffer
[n
+ 2] & 0x02 ? "cd/rw " : "", /* can read rewriteable */
693 buffer
[n
+ 4] & 0x20 ? "xa/form2 " : "", /* can read xa/from2 */
694 buffer
[n
+ 5] & 0x01 ? "cdda " : "", /* can read audio data */
695 loadmech
[buffer
[n
+ 6] >> 5]);
696 if ((buffer
[n
+ 6] >> 5) == 0)
697 /* caddy drives can't close tray... */
698 scsi_CDs
[i
].cdi
.mask
|= CDC_CLOSE_TRAY
;
699 if ((buffer
[n
+ 2] & 0x8) == 0)
700 /* not a DVD drive */
701 scsi_CDs
[i
].cdi
.mask
|= CDC_DVD
;
702 if ((buffer
[n
+ 3] & 0x20) == 0) {
703 /* can't write DVD-RAM media */
704 scsi_CDs
[i
].cdi
.mask
|= CDC_DVD_RAM
;
706 scsi_CDs
[i
].device
->writeable
= 1;
708 if ((buffer
[n
+ 3] & 0x10) == 0)
709 /* can't write DVD-R media */
710 scsi_CDs
[i
].cdi
.mask
|= CDC_DVD_R
;
711 if ((buffer
[n
+ 3] & 0x2) == 0)
712 /* can't write CD-RW media */
713 scsi_CDs
[i
].cdi
.mask
|= CDC_CD_RW
;
714 if ((buffer
[n
+ 3] & 0x1) == 0)
715 /* can't write CD-R media */
716 scsi_CDs
[i
].cdi
.mask
|= CDC_CD_R
;
717 if ((buffer
[n
+ 6] & 0x8) == 0)
719 scsi_CDs
[i
].cdi
.mask
|= CDC_OPEN_TRAY
;
721 if ((buffer
[n
+ 6] >> 5) == mechtype_individual_changer
||
722 (buffer
[n
+ 6] >> 5) == mechtype_cartridge_changer
)
723 scsi_CDs
[i
].cdi
.capacity
=
724 cdrom_number_of_slots(&(scsi_CDs
[i
].cdi
));
725 if (scsi_CDs
[i
].cdi
.capacity
<= 1)
727 scsi_CDs
[i
].cdi
.mask
|= CDC_SELECT_DISC
;
728 /*else I don't think it can close its tray
729 scsi_CDs[i].cdi.mask |= CDC_CLOSE_TRAY; */
731 scsi_free(buffer
, 512);
735 * sr_packet() is the entry point for the generic commands generated
736 * by the Uniform CD-ROM layer.
738 static int sr_packet(struct cdrom_device_info
*cdi
, struct cdrom_generic_command
*cgc
)
740 Scsi_Device
*device
= scsi_CDs
[MINOR(cdi
->dev
)].device
;
743 cgc
->cmd
[1] |= device
->lun
<< 5;
745 cgc
->stat
= sr_do_ioctl(MINOR(cdi
->dev
), cgc
->cmd
, cgc
->buffer
, cgc
->buflen
, cgc
->quiet
, cgc
->data_direction
, cgc
->sense
);
750 static int sr_registered
;
756 if (sr_template
.dev_noticed
== 0)
759 if (!sr_registered
) {
760 if (devfs_register_blkdev(MAJOR_NR
, "sr", &cdrom_fops
)) {
761 printk("Unable to get major %d for SCSI-CD\n", MAJOR_NR
);
769 sr_template
.dev_max
= sr_template
.dev_noticed
+ SR_EXTRA_DEVS
;
770 scsi_CDs
= kmalloc(sr_template
.dev_max
* sizeof(Scsi_CD
), GFP_ATOMIC
);
773 memset(scsi_CDs
, 0, sr_template
.dev_max
* sizeof(Scsi_CD
));
775 sr_sizes
= kmalloc(sr_template
.dev_max
* sizeof(int), GFP_ATOMIC
);
778 memset(sr_sizes
, 0, sr_template
.dev_max
* sizeof(int));
780 sr_blocksizes
= kmalloc(sr_template
.dev_max
* sizeof(int), GFP_ATOMIC
);
784 sr_hardsizes
= kmalloc(sr_template
.dev_max
* sizeof(int), GFP_ATOMIC
);
786 goto cleanup_blocksizes
;
788 * These are good guesses for the time being.
790 for (i
= 0; i
< sr_template
.dev_max
; i
++) {
791 sr_blocksizes
[i
] = 2048;
792 sr_hardsizes
[i
] = 2048;
794 blksize_size
[MAJOR_NR
] = sr_blocksizes
;
795 hardsect_size
[MAJOR_NR
] = sr_hardsizes
;
798 kfree(sr_blocksizes
);
804 devfs_unregister_blkdev(MAJOR_NR
, "sr");
814 blk_dev
[MAJOR_NR
].queue
= sr_find_queue
;
815 blk_size
[MAJOR_NR
] = sr_sizes
;
817 for (i
= 0; i
< sr_template
.nr_dev
; ++i
) {
818 /* If we have already seen this, then skip it. Comes up
819 * with loadable modules. */
820 if (scsi_CDs
[i
].capacity
)
822 scsi_CDs
[i
].capacity
= 0x1fffff;
823 scsi_CDs
[i
].device
->sector_size
= 2048; /* A guess, just in case */
824 scsi_CDs
[i
].needs_sector_size
= 1;
825 scsi_CDs
[i
].device
->changed
= 1; /* force recheck CD type */
827 /* seems better to leave this for later */
829 printk("Scd sectorsize = %d bytes.\n", scsi_CDs
[i
].sector_size
);
833 scsi_CDs
[i
].device
->ten
= 1;
834 scsi_CDs
[i
].device
->remap
= 1;
835 scsi_CDs
[i
].readcd_known
= 0;
836 scsi_CDs
[i
].readcd_cdda
= 0;
837 sr_sizes
[i
] = scsi_CDs
[i
].capacity
>> (BLOCK_SIZE_BITS
- 9);
839 scsi_CDs
[i
].cdi
.ops
= &sr_dops
;
840 scsi_CDs
[i
].cdi
.handle
= &scsi_CDs
[i
];
841 scsi_CDs
[i
].cdi
.dev
= MKDEV(MAJOR_NR
, i
);
842 scsi_CDs
[i
].cdi
.mask
= 0;
843 scsi_CDs
[i
].cdi
.capacity
= 1;
847 sprintf(name
, "sr%d", i
);
848 strcpy(scsi_CDs
[i
].cdi
.name
, name
);
850 devfs_register (scsi_CDs
[i
].device
->de
, "cd",
851 DEVFS_FL_DEFAULT
, MAJOR_NR
, i
,
852 S_IFBLK
| S_IRUGO
| S_IWUGO
,
854 register_cdrom(&scsi_CDs
[i
].cdi
);
858 /* If our host adapter is capable of scatter-gather, then we increase
859 * the read-ahead to 16 blocks (32 sectors). If not, we use
860 * a two block (4 sector) read ahead. */
861 if (scsi_CDs
[0].device
&& scsi_CDs
[0].device
->host
->sg_tablesize
)
862 read_ahead
[MAJOR_NR
] = 32; /* 32 sector read-ahead. Always removable. */
864 read_ahead
[MAJOR_NR
] = 4; /* 4 sector read-ahead */
869 static void sr_detach(Scsi_Device
* SDp
)
874 for (cpnt
= scsi_CDs
, i
= 0; i
< sr_template
.dev_max
; i
++, cpnt
++)
875 if (cpnt
->device
== SDp
) {
876 kdev_t devi
= MKDEV(MAJOR_NR
, i
);
877 struct super_block
*sb
= get_super(devi
);
880 * Since the cdrom is read-only, no need to sync the device.
881 * We should be kind to our buffer cache, however.
884 invalidate_inodes(sb
);
885 invalidate_buffers(devi
);
888 * Reset things back to a sane state so that one can re-load a new
889 * driver (perhaps the same one).
891 unregister_cdrom(&(cpnt
->cdi
));
895 sr_template
.nr_dev
--;
896 sr_template
.dev_noticed
--;
903 static int __init
init_sr(void)
905 sr_template
.module
= THIS_MODULE
;
906 return scsi_register_module(MODULE_SCSI_DEV
, &sr_template
);
909 static void __exit
exit_sr(void)
911 scsi_unregister_module(MODULE_SCSI_DEV
, &sr_template
);
912 devfs_unregister_blkdev(MAJOR_NR
, "sr");
914 if (scsi_CDs
!= NULL
) {
920 kfree(sr_blocksizes
);
921 sr_blocksizes
= NULL
;
925 blksize_size
[MAJOR_NR
] = NULL
;
926 hardsect_size
[MAJOR_NR
] = NULL
;
927 blk_size
[MAJOR_NR
] = NULL
;
928 read_ahead
[MAJOR_NR
] = 0;
930 sr_template
.dev_max
= 0;
933 module_init(init_sr
);
934 module_exit(exit_sr
);