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@image.dk> - Uniform sr_packet()
24 * interface, capabilities probe additions, ioctl cleanups, etc.
26 * Modified by Richard Gooch <rgooch@atnf.csiro.au> to support devfs
30 #include <linux/module.h>
33 #include <linux/kernel.h>
34 #include <linux/sched.h>
36 #include <linux/string.h>
37 #include <linux/errno.h>
38 #include <linux/cdrom.h>
39 #include <linux/interrupt.h>
40 #include <asm/system.h>
42 #include <asm/uaccess.h>
44 #define MAJOR_NR SCSI_CDROM_MAJOR
45 #include <linux/blk.h>
49 #include <scsi/scsi_ioctl.h> /* For the door lock/unlock commands */
50 #include "constants.h"
53 MODULE_PARM(xa_test
, "i"); /* see sr_ioctl.c */
57 #define SR_TIMEOUT (30 * HZ)
59 static int sr_init(void);
60 static void sr_finish(void);
61 static int sr_attach(Scsi_Device
*);
62 static int sr_detect(Scsi_Device
*);
63 static void sr_detach(Scsi_Device
*);
65 static int sr_init_command(Scsi_Cmnd
*);
67 struct Scsi_Device_Template sr_template
=
72 major
:SCSI_CDROM_MAJOR
,
79 init_command
:sr_init_command
82 Scsi_CD
*scsi_CDs
= NULL
;
83 static int *sr_sizes
= NULL
;
85 static int *sr_blocksizes
= NULL
;
86 static int *sr_hardsizes
= NULL
;
88 static int sr_open(struct cdrom_device_info
*, int);
89 void get_sectorsize(int);
90 void get_capabilities(int);
92 static int sr_media_change(struct cdrom_device_info
*, int);
93 static int sr_packet(struct cdrom_device_info
*, struct cdrom_generic_command
*);
95 static void sr_release(struct cdrom_device_info
*cdi
)
97 if (scsi_CDs
[MINOR(cdi
->dev
)].device
->sector_size
> 2048)
98 sr_set_blocklength(MINOR(cdi
->dev
), 2048);
100 scsi_CDs
[MINOR(cdi
->dev
)].device
->access_count
--;
101 if (scsi_CDs
[MINOR(cdi
->dev
)].device
->host
->hostt
->module
)
102 __MOD_DEC_USE_COUNT(scsi_CDs
[MINOR(cdi
->dev
)].device
->host
->hostt
->module
);
103 if (sr_template
.module
)
104 __MOD_DEC_USE_COUNT(sr_template
.module
);
107 static struct cdrom_device_ops sr_dops
=
110 sr_release
, /* release */
111 sr_drive_status
, /* drive status */
112 sr_media_change
, /* media changed */
113 sr_tray_move
, /* tray move */
114 sr_lock_door
, /* lock door */
115 sr_select_speed
, /* select speed */
116 NULL
, /* select disc */
117 sr_get_last_session
, /* get last session */
118 sr_get_mcn
, /* get universal product code */
119 sr_reset
, /* hard reset */
120 sr_audio_ioctl
, /* audio ioctl */
121 sr_dev_ioctl
, /* device-specific ioctl */
122 CDC_CLOSE_TRAY
| CDC_OPEN_TRAY
| CDC_LOCK
| CDC_SELECT_SPEED
|
123 CDC_SELECT_DISC
| CDC_MULTI_SESSION
| CDC_MCN
| CDC_MEDIA_CHANGED
|
124 CDC_PLAY_AUDIO
| CDC_RESET
| CDC_IOCTLS
| CDC_DRIVE_STATUS
|
125 CDC_CD_R
| CDC_CD_RW
| CDC_DVD
| CDC_DVD_R
| CDC_DVD_RAM
|
132 * This function checks to see if the media has been changed in the
133 * CDROM drive. It is possible that we have already sensed a change,
134 * or the drive may have sensed one and not yet reported it. We must
135 * be ready for either case. This function always reports the current
136 * value of the changed bit. If flag is 0, then the changed bit is reset.
137 * This function could be done as an ioctl, but we would need to have
138 * an inode for that to work, and we do not always have one.
141 int sr_media_change(struct cdrom_device_info
*cdi
, int slot
)
145 if (CDSL_CURRENT
!= slot
) {
146 /* no changer support */
149 retval
= scsi_ioctl(scsi_CDs
[MINOR(cdi
->dev
)].device
,
150 SCSI_IOCTL_TEST_UNIT_READY
, 0);
153 /* Unable to test, unit probably not ready. This usually
154 * means there is no disc in the drive. Mark as changed,
155 * and we will figure it out later once the drive is
156 * available again. */
158 scsi_CDs
[MINOR(cdi
->dev
)].device
->changed
= 1;
159 return 1; /* This will force a flush, if called from
160 * check_disk_change */
163 retval
= scsi_CDs
[MINOR(cdi
->dev
)].device
->changed
;
164 scsi_CDs
[MINOR(cdi
->dev
)].device
->changed
= 0;
165 /* If the disk changed, the capacity will now be different,
166 * so we force a re-read of this information */
168 /* check multisession offset etc */
172 * If the disk changed, the capacity will now be different,
173 * so we force a re-read of this information
174 * Force 2048 for the sector size so that filesystems won't
175 * be trying to use something that is too small if the disc
178 scsi_CDs
[MINOR(cdi
->dev
)].needs_sector_size
= 1;
180 scsi_CDs
[MINOR(cdi
->dev
)].device
->sector_size
= 2048;
186 * rw_intr is the interrupt routine for the device driver. It will be notified on the
187 * end of a SCSI read / write, and will take on of several actions based on success or failure.
190 static void rw_intr(Scsi_Cmnd
* SCpnt
)
192 int result
= SCpnt
->result
;
193 int this_count
= SCpnt
->bufflen
>> 9;
194 int good_sectors
= (result
== 0 ? this_count
: 0);
195 int block_sectors
= 0;
198 printk("sr.c done: %x %x\n", result
, SCpnt
->request
.bh
->b_data
);
201 Handle MEDIUM ERRORs or VOLUME OVERFLOWs that indicate partial success.
202 Since this is a relatively rare error condition, no care is taken to
203 avoid unnecessary additional work such as memcpy's that could be avoided.
207 if (driver_byte(result
) != 0 && /* An error occurred */
208 SCpnt
->sense_buffer
[0] == 0xF0 && /* Sense data is valid */
209 (SCpnt
->sense_buffer
[2] == MEDIUM_ERROR
||
210 SCpnt
->sense_buffer
[2] == VOLUME_OVERFLOW
||
211 SCpnt
->sense_buffer
[2] == ILLEGAL_REQUEST
)) {
212 long error_sector
= (SCpnt
->sense_buffer
[3] << 24) |
213 (SCpnt
->sense_buffer
[4] << 16) |
214 (SCpnt
->sense_buffer
[5] << 8) |
215 SCpnt
->sense_buffer
[6];
216 int device_nr
= DEVICE_NR(SCpnt
->request
.rq_dev
);
217 if (SCpnt
->request
.bh
!= NULL
)
218 block_sectors
= SCpnt
->request
.bh
->b_size
>> 9;
219 if (block_sectors
< 4)
221 if (scsi_CDs
[device_nr
].device
->sector_size
== 2048)
223 error_sector
&= ~(block_sectors
- 1);
224 good_sectors
= error_sector
- SCpnt
->request
.sector
;
225 if (good_sectors
< 0 || good_sectors
>= this_count
)
228 * The SCSI specification allows for the value returned by READ
229 * CAPACITY to be up to 75 2K sectors past the last readable
230 * block. Therefore, if we hit a medium error within the last
231 * 75 2K sectors, we decrease the saved size value.
233 if ((error_sector
>> 1) < sr_sizes
[device_nr
] &&
234 scsi_CDs
[device_nr
].capacity
- error_sector
< 4 * 75)
235 sr_sizes
[device_nr
] = error_sector
>> 1;
238 * This calls the generic completion function, now that we know
239 * how many actual sectors finished, and how many sectors we need
240 * to say have failed.
242 scsi_io_completion(SCpnt
, good_sectors
, block_sectors
);
246 static request_queue_t
*sr_find_queue(kdev_t dev
)
251 if (MINOR(dev
) >= sr_template
.dev_max
|| !scsi_CDs
[MINOR(dev
)].device
)
254 return &scsi_CDs
[MINOR(dev
)].device
->request_queue
;
257 static int sr_init_command(Scsi_Cmnd
* SCpnt
)
259 int dev
, devm
, block
, this_count
;
261 devm
= MINOR(SCpnt
->request
.rq_dev
);
262 dev
= DEVICE_NR(SCpnt
->request
.rq_dev
);
264 block
= SCpnt
->request
.sector
;
265 this_count
= SCpnt
->request_bufflen
>> 9;
267 if (!SCpnt
->request
.bh
) {
269 * Umm, yeah, right. Swapping to a cdrom. Nice try.
273 SCSI_LOG_HLQUEUE(1, printk("Doing sr request, dev = %d, block = %d\n", devm
, block
));
275 if (dev
>= sr_template
.nr_dev
||
276 !scsi_CDs
[dev
].device
||
277 !scsi_CDs
[dev
].device
->online
) {
278 SCSI_LOG_HLQUEUE(2, printk("Finishing %ld sectors\n", SCpnt
->request
.nr_sectors
));
279 SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt
));
282 if (scsi_CDs
[dev
].device
->changed
) {
284 * quietly refuse to do anything to a changed disc until the changed
287 /* printk("SCSI disk has been changed. Prohibiting further I/O.\n"); */
291 * we do lazy blocksize switching (when reading XA sectors,
292 * see CDROMREADMODE2 ioctl)
294 if (scsi_CDs
[dev
].device
->sector_size
> 2048) {
296 sr_set_blocklength(DEVICE_NR(CURRENT
->rq_dev
), 2048);
298 printk("sr: can't switch blocksize: in interrupt\n");
300 if (SCpnt
->request
.cmd
== WRITE
) {
303 if (scsi_CDs
[dev
].device
->sector_size
== 1024) {
304 if ((block
& 1) || (SCpnt
->request
.nr_sectors
& 1)) {
305 printk("sr.c:Bad 1K block number requested (%d %ld)",
306 block
, SCpnt
->request
.nr_sectors
);
310 this_count
= this_count
>> 1;
313 if (scsi_CDs
[dev
].device
->sector_size
== 2048) {
314 if ((block
& 3) || (SCpnt
->request
.nr_sectors
& 3)) {
315 printk("sr.c:Bad 2K block number requested (%d %ld)",
316 block
, SCpnt
->request
.nr_sectors
);
320 this_count
= this_count
>> 2;
323 switch (SCpnt
->request
.cmd
) {
325 if (!scsi_CDs
[dev
].device
->writeable
) {
328 SCpnt
->cmnd
[0] = WRITE_10
;
329 SCpnt
->sc_data_direction
= SCSI_DATA_WRITE
;
332 SCpnt
->cmnd
[0] = READ_10
;
333 SCpnt
->sc_data_direction
= SCSI_DATA_READ
;
336 panic("Unknown sr command %d\n", SCpnt
->request
.cmd
);
339 SCSI_LOG_HLQUEUE(2, printk("sr%d : %s %d/%ld 512 byte blocks.\n",
341 (SCpnt
->request
.cmd
== WRITE
) ? "writing" : "reading",
342 this_count
, SCpnt
->request
.nr_sectors
));
344 SCpnt
->cmnd
[1] = (SCpnt
->lun
<< 5) & 0xe0;
346 if (this_count
> 0xffff)
349 SCpnt
->cmnd
[2] = (unsigned char) (block
>> 24) & 0xff;
350 SCpnt
->cmnd
[3] = (unsigned char) (block
>> 16) & 0xff;
351 SCpnt
->cmnd
[4] = (unsigned char) (block
>> 8) & 0xff;
352 SCpnt
->cmnd
[5] = (unsigned char) block
& 0xff;
353 SCpnt
->cmnd
[6] = SCpnt
->cmnd
[9] = 0;
354 SCpnt
->cmnd
[7] = (unsigned char) (this_count
>> 8) & 0xff;
355 SCpnt
->cmnd
[8] = (unsigned char) this_count
& 0xff;
358 * We shouldn't disconnect in the middle of a sector, so with a dumb
359 * host adapter, it's safe to assume that we can at least transfer
360 * this many bytes between each connect / disconnect.
362 SCpnt
->transfersize
= scsi_CDs
[dev
].device
->sector_size
;
363 SCpnt
->underflow
= this_count
<< 9;
365 SCpnt
->allowed
= MAX_RETRIES
;
366 SCpnt
->timeout_per_command
= SR_TIMEOUT
;
369 * This is the completion routine we use. This is matched in terms
370 * of capability to this function.
372 SCpnt
->done
= rw_intr
;
375 * This indicates that the command is ready from our end to be
381 static int sr_open(struct cdrom_device_info
*cdi
, int purpose
)
383 check_disk_change(cdi
->dev
);
385 if (MINOR(cdi
->dev
) >= sr_template
.dev_max
386 || !scsi_CDs
[MINOR(cdi
->dev
)].device
) {
387 return -ENXIO
; /* No such device */
390 * If the device is in error recovery, wait until it is done.
391 * If the device is offline, then disallow any access to it.
393 if (!scsi_block_when_processing_errors(scsi_CDs
[MINOR(cdi
->dev
)].device
)) {
396 scsi_CDs
[MINOR(cdi
->dev
)].device
->access_count
++;
397 if (scsi_CDs
[MINOR(cdi
->dev
)].device
->host
->hostt
->module
)
398 __MOD_INC_USE_COUNT(scsi_CDs
[MINOR(cdi
->dev
)].device
->host
->hostt
->module
);
399 if (sr_template
.module
)
400 __MOD_INC_USE_COUNT(sr_template
.module
);
402 /* If this device did not have media in the drive at boot time, then
403 * we would have been unable to get the sector size. Check to see if
404 * this is the case, and try again.
407 if (scsi_CDs
[MINOR(cdi
->dev
)].needs_sector_size
)
408 get_sectorsize(MINOR(cdi
->dev
));
414 * do_sr_request() is the request handler function for the sr driver.
415 * Its function in life is to take block device requests, and
416 * translate them to SCSI commands.
420 static int sr_detect(Scsi_Device
* SDp
)
423 if (SDp
->type
!= TYPE_ROM
&& SDp
->type
!= TYPE_WORM
)
426 printk("Detected scsi CD-ROM sr%d at scsi%d, channel %d, id %d, lun %d\n",
427 sr_template
.dev_noticed
++,
428 SDp
->host
->host_no
, SDp
->channel
, SDp
->id
, SDp
->lun
);
433 static int sr_attach(Scsi_Device
* SDp
)
438 if (SDp
->type
!= TYPE_ROM
&& SDp
->type
!= TYPE_WORM
)
441 if (sr_template
.nr_dev
>= sr_template
.dev_max
) {
445 for (cpnt
= scsi_CDs
, i
= 0; i
< sr_template
.dev_max
; i
++, cpnt
++)
449 if (i
>= sr_template
.dev_max
)
450 panic("scsi_devices corrupt (sr)");
453 scsi_CDs
[i
].device
= SDp
;
455 sr_template
.nr_dev
++;
456 if (sr_template
.nr_dev
> sr_template
.dev_max
)
457 panic("scsi_devices corrupt (sr)");
462 void get_sectorsize(int i
)
464 unsigned char cmd
[10];
465 unsigned char *buffer
;
466 int the_result
, retries
;
470 buffer
= (unsigned char *) scsi_malloc(512);
473 SRpnt
= scsi_allocate_request(scsi_CDs
[i
].device
);
477 cmd
[0] = READ_CAPACITY
;
478 cmd
[1] = (scsi_CDs
[i
].device
->lun
<< 5) & 0xe0;
479 memset((void *) &cmd
[2], 0, 8);
480 SRpnt
->sr_request
.rq_status
= RQ_SCSI_BUSY
; /* Mark as really busy */
481 SRpnt
->sr_cmd_len
= 0;
483 memset(buffer
, 0, 8);
485 /* Do the command and wait.. */
487 SRpnt
->sr_data_direction
= SCSI_DATA_READ
;
488 scsi_wait_req(SRpnt
, (void *) cmd
, (void *) buffer
,
489 512, SR_TIMEOUT
, MAX_RETRIES
);
491 the_result
= SRpnt
->sr_result
;
494 } while (the_result
&& retries
);
497 scsi_release_request(SRpnt
);
501 scsi_CDs
[i
].capacity
= 0x1fffff;
502 sector_size
= 2048; /* A guess, just in case */
503 scsi_CDs
[i
].needs_sector_size
= 1;
506 if (cdrom_get_last_written(MKDEV(MAJOR_NR
, i
),
507 (long *) &scsi_CDs
[i
].capacity
))
509 scsi_CDs
[i
].capacity
= 1 + ((buffer
[0] << 24) |
513 sector_size
= (buffer
[4] << 24) |
514 (buffer
[5] << 16) | (buffer
[6] << 8) | buffer
[7];
515 switch (sector_size
) {
517 * HP 4020i CD-Recorder reports 2340 byte sectors
518 * Philips CD-Writers report 2352 byte sectors
520 * Use 2k sectors for them..
528 scsi_CDs
[i
].capacity
*= 4;
533 printk("sr%d: unsupported sector size %d.\n",
535 scsi_CDs
[i
].capacity
= 0;
536 scsi_CDs
[i
].needs_sector_size
= 1;
539 scsi_CDs
[i
].device
->sector_size
= sector_size
;
542 * Add this so that we have the ability to correctly gauge
543 * what the device is capable of.
545 scsi_CDs
[i
].needs_sector_size
= 0;
546 sr_sizes
[i
] = scsi_CDs
[i
].capacity
>> (BLOCK_SIZE_BITS
- 9);
548 scsi_free(buffer
, 512);
551 void get_capabilities(int i
)
553 unsigned char cmd
[6];
554 unsigned char *buffer
;
557 static char *loadmech
[] =
569 buffer
= (unsigned char *) scsi_malloc(512);
571 cmd
[1] = (scsi_CDs
[i
].device
->lun
<< 5) & 0xe0;
575 rc
= sr_do_ioctl(i
, cmd
, buffer
, 128, 1, SCSI_DATA_READ
);
578 /* failed, drive has'nt this mode page */
579 scsi_CDs
[i
].cdi
.speed
= 1;
580 /* disable speed select, drive probably can't do this either */
581 scsi_CDs
[i
].cdi
.mask
|= CDC_SELECT_SPEED
;
582 scsi_free(buffer
, 512);
586 scsi_CDs
[i
].cdi
.speed
= ((buffer
[n
+ 8] << 8) + buffer
[n
+ 9]) / 176;
587 scsi_CDs
[i
].readcd_known
= 1;
588 scsi_CDs
[i
].readcd_cdda
= buffer
[n
+ 5] & 0x01;
589 /* print some capability bits */
590 printk("sr%i: scsi3-mmc drive: %dx/%dx %s%s%s%s%s\n", i
,
591 ((buffer
[n
+ 14] << 8) + buffer
[n
+ 15]) / 176,
592 scsi_CDs
[i
].cdi
.speed
,
593 buffer
[n
+ 3] & 0x01 ? "writer " : "", /* CD Writer */
594 buffer
[n
+ 2] & 0x02 ? "cd/rw " : "", /* can read rewriteable */
595 buffer
[n
+ 4] & 0x20 ? "xa/form2 " : "", /* can read xa/from2 */
596 buffer
[n
+ 5] & 0x01 ? "cdda " : "", /* can read audio data */
597 loadmech
[buffer
[n
+ 6] >> 5]);
598 if ((buffer
[n
+ 6] >> 5) == 0)
599 /* caddy drives can't close tray... */
600 scsi_CDs
[i
].cdi
.mask
|= CDC_CLOSE_TRAY
;
601 if ((buffer
[n
+ 2] & 0x8) == 0)
602 /* not a DVD drive */
603 scsi_CDs
[i
].cdi
.mask
|= CDC_DVD
;
604 if ((buffer
[n
+ 3] & 0x20) == 0)
605 /* can't write DVD-RAM media */
606 scsi_CDs
[i
].cdi
.mask
|= CDC_DVD_RAM
;
607 if ((buffer
[n
+ 3] & 0x10) == 0)
608 /* can't write DVD-R media */
609 scsi_CDs
[i
].cdi
.mask
|= CDC_DVD_R
;
610 if ((buffer
[n
+ 3] & 0x2) == 0)
611 /* can't write CD-RW media */
612 scsi_CDs
[i
].cdi
.mask
|= CDC_CD_RW
;
613 if ((buffer
[n
+ 3] & 0x1) == 0)
614 /* can't write CD-R media */
615 scsi_CDs
[i
].cdi
.mask
|= CDC_CD_R
;
616 if ((buffer
[n
+ 6] & 0x8) == 0)
618 scsi_CDs
[i
].cdi
.mask
|= CDC_OPEN_TRAY
;
620 if ((buffer
[n
+ 6] >> 5) == mechtype_individual_changer
||
621 (buffer
[n
+ 6] >> 5) == mechtype_cartridge_changer
)
622 scsi_CDs
[i
].cdi
.capacity
=
623 cdrom_number_of_slots(&(scsi_CDs
[i
].cdi
));
624 if (scsi_CDs
[i
].cdi
.capacity
<= 1)
626 scsi_CDs
[i
].cdi
.mask
|= CDC_SELECT_DISC
;
627 /*else I don't think it can close its tray
628 scsi_CDs[i].cdi.mask |= CDC_CLOSE_TRAY; */
631 scsi_free(buffer
, 512);
635 * sr_packet() is the entry point for the generic commands generated
636 * by the Uniform CD-ROM layer.
638 static int sr_packet(struct cdrom_device_info
*cdi
, struct cdrom_generic_command
*cgc
)
641 Scsi_Device
*device
= scsi_CDs
[MINOR(cdi
->dev
)].device
;
642 unsigned char *buffer
= cgc
->buffer
;
646 SRpnt
= scsi_allocate_request(device
);
648 return -ENODEV
; /* this just doesn't seem right /axboe */
650 /* use buffer for ISA DMA */
651 buflen
= (cgc
->buflen
+ 511) & ~511;
652 if (cgc
->buffer
&& SRpnt
->sr_host
->unchecked_isa_dma
&&
653 (virt_to_phys(cgc
->buffer
) + cgc
->buflen
- 1 > ISA_DMA_THRESHOLD
)) {
654 buffer
= scsi_malloc(buflen
);
655 if (buffer
== NULL
) {
656 printk("sr: SCSI DMA pool exhausted.");
659 memcpy(buffer
, cgc
->buffer
, cgc
->buflen
);
662 cgc
->cmd
[1] |= device
->lun
<< 5;
664 /* do the locking and issue the command */
665 SRpnt
->sr_request
.rq_dev
= cdi
->dev
;
666 /* scsi_wait_cmd sets the command length */
667 SRpnt
->sr_cmd_len
= 0;
669 SRpnt
->sr_data_direction
= cgc
->data_direction
;
670 scsi_wait_req(SRpnt
, (void *) cgc
->cmd
, (void *) buffer
, cgc
->buflen
,
671 SR_TIMEOUT
, MAX_RETRIES
);
673 if ((cgc
->stat
= SRpnt
->sr_result
))
674 cgc
->sense
= (struct request_sense
*) SRpnt
->sr_sense_buffer
;
677 SRpnt
->sr_request
.rq_dev
= MKDEV(0, 0);
678 scsi_release_request(SRpnt
);
681 /* write DMA buffer back if used */
682 if (buffer
&& (buffer
!= cgc
->buffer
)) {
683 memcpy(cgc
->buffer
, buffer
, cgc
->buflen
);
684 scsi_free(buffer
, buflen
);
691 static int sr_registered
= 0;
697 if (sr_template
.dev_noticed
== 0)
700 if (!sr_registered
) {
701 if (devfs_register_blkdev(MAJOR_NR
, "sr", &cdrom_fops
)) {
702 printk("Unable to get major %d for SCSI-CD\n", MAJOR_NR
);
709 sr_template
.dev_max
=
710 sr_template
.dev_noticed
+ SR_EXTRA_DEVS
;
711 scsi_CDs
= (Scsi_CD
*) kmalloc(sr_template
.dev_max
* sizeof(Scsi_CD
), GFP_ATOMIC
);
712 memset(scsi_CDs
, 0, sr_template
.dev_max
* sizeof(Scsi_CD
));
714 sr_sizes
= (int *) kmalloc(sr_template
.dev_max
* sizeof(int), GFP_ATOMIC
);
715 memset(sr_sizes
, 0, sr_template
.dev_max
* sizeof(int));
717 sr_blocksizes
= (int *) kmalloc(sr_template
.dev_max
*
718 sizeof(int), GFP_ATOMIC
);
720 sr_hardsizes
= (int *) kmalloc(sr_template
.dev_max
*
721 sizeof(int), GFP_ATOMIC
);
723 * These are good guesses for the time being.
725 for (i
= 0; i
< sr_template
.dev_max
; i
++)
727 sr_blocksizes
[i
] = 2048;
728 sr_hardsizes
[i
] = 2048;
730 blksize_size
[MAJOR_NR
] = sr_blocksizes
;
731 hardsect_size
[MAJOR_NR
] = sr_hardsizes
;
740 blk_dev
[MAJOR_NR
].queue
= sr_find_queue
;
741 blk_size
[MAJOR_NR
] = sr_sizes
;
743 for (i
= 0; i
< sr_template
.nr_dev
; ++i
) {
744 /* If we have already seen this, then skip it. Comes up
745 * with loadable modules. */
746 if (scsi_CDs
[i
].capacity
)
748 scsi_CDs
[i
].capacity
= 0x1fffff;
749 scsi_CDs
[i
].device
->sector_size
= 2048; /* A guess, just in case */
750 scsi_CDs
[i
].needs_sector_size
= 1;
751 scsi_CDs
[i
].device
->changed
= 1; /* force recheck CD type */
753 /* seems better to leave this for later */
755 printk("Scd sectorsize = %d bytes.\n", scsi_CDs
[i
].sector_size
);
759 scsi_CDs
[i
].device
->ten
= 1;
760 scsi_CDs
[i
].device
->remap
= 1;
761 scsi_CDs
[i
].readcd_known
= 0;
762 scsi_CDs
[i
].readcd_cdda
= 0;
763 sr_sizes
[i
] = scsi_CDs
[i
].capacity
>> (BLOCK_SIZE_BITS
- 9);
765 scsi_CDs
[i
].cdi
.ops
= &sr_dops
;
766 scsi_CDs
[i
].cdi
.handle
= &scsi_CDs
[i
];
767 scsi_CDs
[i
].cdi
.dev
= MKDEV(MAJOR_NR
, i
);
768 scsi_CDs
[i
].cdi
.mask
= 0;
769 scsi_CDs
[i
].cdi
.capacity
= 1;
773 sprintf(name
, "sr%d", i
);
774 strcpy(scsi_CDs
[i
].cdi
.name
, name
);
776 devfs_register (scsi_CDs
[i
].device
->de
, "cd", 2,
777 DEVFS_FL_DEFAULT
, MAJOR_NR
, i
,
778 S_IFBLK
| S_IRUGO
| S_IWUGO
, 0, 0,
780 register_cdrom(&scsi_CDs
[i
].cdi
);
784 /* If our host adapter is capable of scatter-gather, then we increase
785 * the read-ahead to 16 blocks (32 sectors). If not, we use
786 * a two block (4 sector) read ahead. */
787 if (scsi_CDs
[0].device
&& scsi_CDs
[0].device
->host
->sg_tablesize
)
788 read_ahead
[MAJOR_NR
] = 32; /* 32 sector read-ahead. Always removable. */
790 read_ahead
[MAJOR_NR
] = 4; /* 4 sector read-ahead */
795 static void sr_detach(Scsi_Device
* SDp
)
800 for (cpnt
= scsi_CDs
, i
= 0; i
< sr_template
.dev_max
; i
++, cpnt
++)
801 if (cpnt
->device
== SDp
) {
802 kdev_t devi
= MKDEV(MAJOR_NR
, i
);
803 struct super_block
*sb
= get_super(devi
);
806 * Since the cdrom is read-only, no need to sync the device.
807 * We should be kind to our buffer cache, however.
810 invalidate_inodes(sb
);
811 invalidate_buffers(devi
);
814 * Reset things back to a sane state so that one can re-load a new
815 * driver (perhaps the same one).
817 unregister_cdrom(&(cpnt
->cdi
));
821 sr_template
.nr_dev
--;
822 sr_template
.dev_noticed
--;
832 int init_module(void)
834 sr_template
.module
= &__this_module
;
835 return scsi_register_module(MODULE_SCSI_DEV
, &sr_template
);
838 void cleanup_module(void)
840 scsi_unregister_module(MODULE_SCSI_DEV
, &sr_template
);
841 devfs_unregister_blkdev(MAJOR_NR
, "sr");
843 if (scsi_CDs
!= NULL
) {
844 kfree((char *) scsi_CDs
);
846 kfree((char *) sr_sizes
);
849 kfree((char *) sr_blocksizes
);
850 sr_blocksizes
= NULL
;
851 kfree((char *) sr_hardsizes
);
854 blksize_size
[MAJOR_NR
] = NULL
;
855 hardsect_size
[MAJOR_NR
] = NULL
;
856 blk_cleanup_queue(BLK_DEFAULT_QUEUE(MAJOR_NR
));
857 blk_size
[MAJOR_NR
] = NULL
;
858 read_ahead
[MAJOR_NR
] = 0;
860 sr_template
.dev_max
= 0;
866 * Overrides for Emacs so that we follow Linus's tabbing style.
867 * Emacs will notice this stuff at the end of the file and automatically
868 * adjust the settings for this buffer only. This must remain at the end
870 * ---------------------------------------------------------------------------
873 * c-brace-imaginary-offset: 0
875 * c-argdecl-indent: 4
877 * c-continued-statement-offset: 4
878 * c-continued-brace-offset: 0
879 * indent-tabs-mode: nil