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);
104 scsi_CDs[MINOR(cdi->dev)].device->access_count--;
105 if (scsi_CDs[MINOR(cdi->dev)].device->host->hostt->module)
106 __MOD_DEC_USE_COUNT(scsi_CDs[MINOR(cdi->dev)].device->host->hostt->module);
107 if (sr_template.module)
108 __MOD_DEC_USE_COUNT(sr_template.module);
111 static struct cdrom_device_ops sr_dops =
115 drive_status: sr_drive_status,
116 media_changed: sr_media_change,
117 tray_move: sr_tray_move,
118 lock_door: sr_lock_door,
119 select_speed: sr_select_speed,
120 get_last_session: sr_get_last_session,
123 audio_ioctl: sr_audio_ioctl,
124 dev_ioctl: sr_dev_ioctl,
125 capability: CDC_CLOSE_TRAY | CDC_OPEN_TRAY | CDC_LOCK |
126 CDC_SELECT_SPEED | CDC_SELECT_DISC |
127 CDC_MULTI_SESSION | CDC_MCN |
128 CDC_MEDIA_CHANGED | CDC_PLAY_AUDIO |
129 CDC_RESET | CDC_IOCTLS | CDC_DRIVE_STATUS |
130 CDC_CD_R | CDC_CD_RW | CDC_DVD | CDC_DVD_R |
131 CDC_DVD_RAM | CDC_GENERIC_PACKET,
132 generic_packet: sr_packet,
136 * This function checks to see if the media has been changed in the
137 * CDROM drive. It is possible that we have already sensed a change,
138 * or the drive may have sensed one and not yet reported it. We must
139 * be ready for either case. This function always reports the current
140 * value of the changed bit. If flag is 0, then the changed bit is reset.
141 * This function could be done as an ioctl, but we would need to have
142 * an inode for that to work, and we do not always have one.
145 int sr_media_change(struct cdrom_device_info *cdi, int slot)
149 if (CDSL_CURRENT != slot) {
150 /* no changer support */
153 retval = scsi_ioctl(scsi_CDs[MINOR(cdi->dev)].device,
154 SCSI_IOCTL_TEST_UNIT_READY, 0);
157 /* Unable to test, unit probably not ready. This usually
158 * means there is no disc in the drive. Mark as changed,
159 * and we will figure it out later once the drive is
160 * available again. */
162 scsi_CDs[MINOR(cdi->dev)].device->changed = 1;
163 return 1; /* This will force a flush, if called from
164 * check_disk_change */
167 retval = scsi_CDs[MINOR(cdi->dev)].device->changed;
168 scsi_CDs[MINOR(cdi->dev)].device->changed = 0;
169 /* If the disk changed, the capacity will now be different,
170 * so we force a re-read of this information */
172 /* check multisession offset etc */
176 * If the disk changed, the capacity will now be different,
177 * so we force a re-read of this information
178 * Force 2048 for the sector size so that filesystems won't
179 * be trying to use something that is too small if the disc
182 scsi_CDs[MINOR(cdi->dev)].needs_sector_size = 1;
184 scsi_CDs[MINOR(cdi->dev)].device->sector_size = 2048;
190 * rw_intr is the interrupt routine for the device driver. It will be notified on the
191 * end of a SCSI read / write, and will take on of several actions based on success or failure.
194 static void rw_intr(Scsi_Cmnd * SCpnt)
196 int result = SCpnt->result;
197 int this_count = SCpnt->bufflen >> 9;
198 int good_sectors = (result == 0 ? this_count : 0);
199 int block_sectors = 0;
200 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] & 0x7f) == 0x70) { /* Sense data is valid */
215 switch (SCpnt->sense_buffer[2]) {
217 case VOLUME_OVERFLOW:
218 case ILLEGAL_REQUEST:
219 if (!(SCpnt->sense_buffer[0] & 0x80))
221 error_sector = (SCpnt->sense_buffer[3] << 24) |
222 (SCpnt->sense_buffer[4] << 16) |
223 (SCpnt->sense_buffer[5] << 8) |
224 SCpnt->sense_buffer[6];
225 if (SCpnt->request.bh != NULL)
226 block_sectors = SCpnt->request.bh->b_size >> 9;
227 if (block_sectors < 4)
229 if (scsi_CDs[device_nr].device->sector_size == 2048)
231 error_sector &= ~(block_sectors - 1);
232 good_sectors = error_sector - SCpnt->request.sector;
233 if (good_sectors < 0 || good_sectors >= this_count)
236 * The SCSI specification allows for the value returned
237 * by READ CAPACITY to be up to 75 2K sectors past the
238 * last readable block. Therefore, if we hit a medium
239 * error within the last 75 2K sectors, we decrease the
242 if ((error_sector >> 1) < sr_sizes[device_nr] &&
243 scsi_CDs[device_nr].capacity - error_sector < 4 *75)
244 sr_sizes[device_nr] = error_sector >> 1;
247 case RECOVERED_ERROR:
249 * An error occured, but it recovered. Inform the
250 * user, but make sure that it's not treated as a
253 print_sense("sr", SCpnt);
255 SCpnt->sense_buffer[0] = 0x0;
256 good_sectors = this_count;
265 * This calls the generic completion function, now that we know
266 * how many actual sectors finished, and how many sectors we need
267 * to say have failed.
269 scsi_io_completion(SCpnt, good_sectors, block_sectors);
273 static request_queue_t *sr_find_queue(kdev_t dev)
278 if (MINOR(dev) >= sr_template.dev_max || !scsi_CDs[MINOR(dev)].device)
281 return &scsi_CDs[MINOR(dev)].device->request_queue;
284 static int sr_scatter_pad(Scsi_Cmnd *SCpnt, int s_size)
286 struct scatterlist *sg, *old_sg = NULL;
287 int i, fsize, bsize, sg_ent, sg_count;
289 void **bbpnt, **old_bbpnt = NULL;
292 sg_ent = SCpnt->use_sg;
293 bsize = 0; /* gcc... */
298 if ((fsize = SCpnt->request.sector % (s_size >> 9))) {
301 if ((front = scsi_malloc(fsize)) == NULL)
305 * need a back pad too
307 if ((bsize = s_size - ((SCpnt->request_bufflen + fsize) % s_size))) {
309 if ((back = scsi_malloc(bsize)) == NULL)
314 * extend or allocate new scatter-gather table
316 sg_count = SCpnt->use_sg;
318 old_sg = (struct scatterlist *) SCpnt->request_buffer;
319 old_bbpnt = SCpnt->bounce_buffers;
325 /* Get space for scatterlist and bounce buffer array. */
326 i = sg_ent * sizeof(struct scatterlist);
327 i += sg_ent * sizeof(void *);
328 i = (i + 511) & ~511;
330 if ((sg = scsi_malloc(i)) == NULL)
334 ((char *)sg + (sg_ent * sizeof(struct scatterlist)));
337 * no more failing memory allocs possible, we can safely assign
340 SCpnt->sglist_len = i;
341 SCpnt->use_sg = sg_count;
342 memset(sg, 0, SCpnt->sglist_len);
346 sg[0].address = bbpnt[0] = front;
347 sg[0].length = fsize;
351 memcpy(sg + i, old_sg, SCpnt->use_sg * sizeof(struct scatterlist));
353 memcpy(bbpnt + i, old_bbpnt, SCpnt->use_sg * sizeof(void *));
354 scsi_free(old_sg, (((SCpnt->use_sg * sizeof(struct scatterlist)) +
355 (SCpnt->use_sg * sizeof(void *))) + 511) & ~511);
357 sg[i].address = SCpnt->request_buffer;
358 sg[i].length = SCpnt->request_bufflen;
361 SCpnt->request_bufflen += (fsize + bsize);
362 SCpnt->request_buffer = sg;
363 SCpnt->bounce_buffers = bbpnt;
367 sg[SCpnt->use_sg].address = back;
368 bbpnt[SCpnt->use_sg] = back;
369 sg[SCpnt->use_sg].length = bsize;
376 printk("sr: ran out of mem for scatter pad\n");
378 scsi_free(front, fsize);
380 scsi_free(back, bsize);
386 static int sr_init_command(Scsi_Cmnd * SCpnt)
388 int dev, devm, block=0, this_count, s_size;
390 devm = MINOR(SCpnt->request.rq_dev);
391 dev = DEVICE_NR(SCpnt->request.rq_dev);
393 SCSI_LOG_HLQUEUE(1, printk("Doing sr request, dev = %d, block = %d\n", devm, block));
395 if (dev >= sr_template.nr_dev ||
396 !scsi_CDs[dev].device ||
397 !scsi_CDs[dev].device->online) {
398 SCSI_LOG_HLQUEUE(2, printk("Finishing %ld sectors\n", SCpnt->request.nr_sectors));
399 SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt));
402 if (scsi_CDs[dev].device->changed) {
404 * quietly refuse to do anything to a changed disc until the
405 * changed bit has been reset
410 if ((SCpnt->request.cmd == WRITE) && !scsi_CDs[dev].device->writeable)
414 * we do lazy blocksize switching (when reading XA sectors,
415 * see CDROMREADMODE2 ioctl)
417 s_size = scsi_CDs[dev].device->sector_size;
420 sr_set_blocklength(DEVICE_NR(CURRENT->rq_dev), 2048);
422 printk("sr: can't switch blocksize: in interrupt\n");
425 if (s_size != 512 && s_size != 1024 && s_size != 2048) {
426 printk("sr: bad sector size %d\n", s_size);
430 block = SCpnt->request.sector / (s_size >> 9);
433 * request doesn't start on hw block boundary, add scatter pads
435 if ((SCpnt->request.sector % (s_size >> 9)) || (SCpnt->request_bufflen % s_size))
436 if (sr_scatter_pad(SCpnt, s_size))
439 this_count = (SCpnt->request_bufflen >> 9) / (s_size >> 9);
441 switch (SCpnt->request.cmd) {
443 SCpnt->cmnd[0] = WRITE_10;
444 SCpnt->sc_data_direction = SCSI_DATA_WRITE;
447 SCpnt->cmnd[0] = READ_10;
448 SCpnt->sc_data_direction = SCSI_DATA_READ;
451 printk("Unknown sr command %d\n", SCpnt->request.cmd);
455 SCSI_LOG_HLQUEUE(2, printk("sr%d : %s %d/%ld 512 byte blocks.\n",
457 (SCpnt->request.cmd == WRITE) ? "writing" : "reading",
458 this_count, SCpnt->request.nr_sectors));
460 SCpnt->cmnd[1] = (SCpnt->device->scsi_level <= SCSI_2) ?
461 ((SCpnt->lun << 5) & 0xe0) : 0;
463 if (this_count > 0xffff)
466 SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
467 SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
468 SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
469 SCpnt->cmnd[5] = (unsigned char) block & 0xff;
470 SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
471 SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
472 SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
475 * We shouldn't disconnect in the middle of a sector, so with a dumb
476 * host adapter, it's safe to assume that we can at least transfer
477 * this many bytes between each connect / disconnect.
479 SCpnt->transfersize = scsi_CDs[dev].device->sector_size;
480 SCpnt->underflow = this_count << 9;
482 SCpnt->allowed = MAX_RETRIES;
483 SCpnt->timeout_per_command = SR_TIMEOUT;
486 * This is the completion routine we use. This is matched in terms
487 * of capability to this function.
489 SCpnt->done = rw_intr;
492 struct scatterlist *sg = SCpnt->request_buffer;
494 for (i = 0; i < SCpnt->use_sg; i++)
495 size += sg[i].length;
497 if (size != SCpnt->request_bufflen && SCpnt->use_sg) {
498 printk("sr: mismatch count %d, bytes %d\n", size, SCpnt->request_bufflen);
499 SCpnt->request_bufflen = size;
504 * This indicates that the command is ready from our end to be
510 struct block_device_operations sr_bdops =
514 release: cdrom_release,
516 check_media_change: cdrom_media_changed,
519 static int sr_open(struct cdrom_device_info *cdi, int purpose)
521 check_disk_change(cdi->dev);
523 if (MINOR(cdi->dev) >= sr_template.dev_max
524 || !scsi_CDs[MINOR(cdi->dev)].device) {
525 return -ENXIO; /* No such device */
528 * If the device is in error recovery, wait until it is done.
529 * If the device is offline, then disallow any access to it.
531 if (!scsi_block_when_processing_errors(scsi_CDs[MINOR(cdi->dev)].device)) {
534 scsi_CDs[MINOR(cdi->dev)].device->access_count++;
535 if (scsi_CDs[MINOR(cdi->dev)].device->host->hostt->module)
536 __MOD_INC_USE_COUNT(scsi_CDs[MINOR(cdi->dev)].device->host->hostt->module);
537 if (sr_template.module)
538 __MOD_INC_USE_COUNT(sr_template.module);
540 /* If this device did not have media in the drive at boot time, then
541 * we would have been unable to get the sector size. Check to see if
542 * this is the case, and try again.
545 if (scsi_CDs[MINOR(cdi->dev)].needs_sector_size)
546 get_sectorsize(MINOR(cdi->dev));
551 static int sr_detect(Scsi_Device * SDp)
554 if (SDp->type != TYPE_ROM && SDp->type != TYPE_WORM)
556 sr_template.dev_noticed++;
560 static int sr_attach(Scsi_Device * SDp)
565 if (SDp->type != TYPE_ROM && SDp->type != TYPE_WORM)
568 if (sr_template.nr_dev >= sr_template.dev_max) {
572 for (cpnt = scsi_CDs, i = 0; i < sr_template.dev_max; i++, cpnt++)
576 if (i >= sr_template.dev_max)
577 panic("scsi_devices corrupt (sr)");
580 scsi_CDs[i].device = SDp;
582 sr_template.nr_dev++;
583 if (sr_template.nr_dev > sr_template.dev_max)
584 panic("scsi_devices corrupt (sr)");
586 printk("Attached scsi CD-ROM sr%d at scsi%d, channel %d, id %d, lun %d\n",
587 i, SDp->host->host_no, SDp->channel, SDp->id, SDp->lun);
592 void get_sectorsize(int i)
594 unsigned char cmd[10];
595 unsigned char *buffer;
596 int the_result, retries;
600 buffer = (unsigned char *) scsi_malloc(512);
601 SRpnt = scsi_allocate_request(scsi_CDs[i].device);
603 if(buffer == NULL || SRpnt == NULL)
605 scsi_CDs[i].capacity = 0x1fffff;
606 sector_size = 2048; /* A guess, just in case */
607 scsi_CDs[i].needs_sector_size = 1;
609 scsi_free(buffer, 512);
611 scsi_release_request(SRpnt);
617 cmd[0] = READ_CAPACITY;
618 cmd[1] = (scsi_CDs[i].device->scsi_level <= SCSI_2) ?
619 ((scsi_CDs[i].device->lun << 5) & 0xe0) : 0;
620 memset((void *) &cmd[2], 0, 8);
621 SRpnt->sr_request.rq_status = RQ_SCSI_BUSY; /* Mark as really busy */
622 SRpnt->sr_cmd_len = 0;
624 memset(buffer, 0, 8);
626 /* Do the command and wait.. */
628 SRpnt->sr_data_direction = SCSI_DATA_READ;
629 scsi_wait_req(SRpnt, (void *) cmd, (void *) buffer,
630 8, SR_TIMEOUT, MAX_RETRIES);
632 the_result = SRpnt->sr_result;
635 } while (the_result && retries);
638 scsi_release_request(SRpnt);
642 scsi_CDs[i].capacity = 0x1fffff;
643 sector_size = 2048; /* A guess, just in case */
644 scsi_CDs[i].needs_sector_size = 1;
647 if (cdrom_get_last_written(MKDEV(MAJOR_NR, i),
648 &scsi_CDs[i].capacity))
650 scsi_CDs[i].capacity = 1 + ((buffer[0] << 24) |
654 sector_size = (buffer[4] << 24) |
655 (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
656 switch (sector_size) {
658 * HP 4020i CD-Recorder reports 2340 byte sectors
659 * Philips CD-Writers report 2352 byte sectors
661 * Use 2k sectors for them..
669 scsi_CDs[i].capacity *= 4;
674 printk("sr%d: unsupported sector size %d.\n",
676 scsi_CDs[i].capacity = 0;
677 scsi_CDs[i].needs_sector_size = 1;
680 scsi_CDs[i].device->sector_size = sector_size;
683 * Add this so that we have the ability to correctly gauge
684 * what the device is capable of.
686 scsi_CDs[i].needs_sector_size = 0;
687 sr_sizes[i] = scsi_CDs[i].capacity >> (BLOCK_SIZE_BITS - 9);
689 scsi_free(buffer, 512);
692 void get_capabilities(int i)
694 unsigned char cmd[6];
695 unsigned char *buffer;
698 static char *loadmech[] =
710 buffer = (unsigned char *) scsi_malloc(512);
713 printk(KERN_ERR "sr: out of memory.\n");
717 cmd[1] = (scsi_CDs[i].device->scsi_level <= SCSI_2) ?
718 ((scsi_CDs[i].device->lun << 5) & 0xe0) : 0;
722 rc = sr_do_ioctl(i, cmd, buffer, 128, 1, SCSI_DATA_READ, NULL);
725 /* failed, drive doesn't have capabilities mode page */
726 scsi_CDs[i].cdi.speed = 1;
727 scsi_CDs[i].cdi.mask |= (CDC_CD_R | CDC_CD_RW | CDC_DVD_R |
728 CDC_DVD | CDC_DVD_RAM |
729 CDC_SELECT_DISC | CDC_SELECT_SPEED);
730 scsi_free(buffer, 512);
731 printk("sr%i: scsi-1 drive\n", i);
735 scsi_CDs[i].cdi.speed = ((buffer[n + 8] << 8) + buffer[n + 9]) / 176;
736 scsi_CDs[i].readcd_known = 1;
737 scsi_CDs[i].readcd_cdda = buffer[n + 5] & 0x01;
738 /* print some capability bits */
739 printk("sr%i: scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n", i,
740 ((buffer[n + 14] << 8) + buffer[n + 15]) / 176,
741 scsi_CDs[i].cdi.speed,
742 buffer[n + 3] & 0x01 ? "writer " : "", /* CD Writer */
743 buffer[n + 3] & 0x20 ? "dvd-ram " : "",
744 buffer[n + 2] & 0x02 ? "cd/rw " : "", /* can read rewriteable */
745 buffer[n + 4] & 0x20 ? "xa/form2 " : "", /* can read xa/from2 */
746 buffer[n + 5] & 0x01 ? "cdda " : "", /* can read audio data */
747 loadmech[buffer[n + 6] >> 5]);
748 if ((buffer[n + 6] >> 5) == 0)
749 /* caddy drives can't close tray... */
750 scsi_CDs[i].cdi.mask |= CDC_CLOSE_TRAY;
751 if ((buffer[n + 2] & 0x8) == 0)
752 /* not a DVD drive */
753 scsi_CDs[i].cdi.mask |= CDC_DVD;
754 if ((buffer[n + 3] & 0x20) == 0) {
755 /* can't write DVD-RAM media */
756 scsi_CDs[i].cdi.mask |= CDC_DVD_RAM;
758 scsi_CDs[i].device->writeable = 1;
760 if ((buffer[n + 3] & 0x10) == 0)
761 /* can't write DVD-R media */
762 scsi_CDs[i].cdi.mask |= CDC_DVD_R;
763 if ((buffer[n + 3] & 0x2) == 0)
764 /* can't write CD-RW media */
765 scsi_CDs[i].cdi.mask |= CDC_CD_RW;
766 if ((buffer[n + 3] & 0x1) == 0)
767 /* can't write CD-R media */
768 scsi_CDs[i].cdi.mask |= CDC_CD_R;
769 if ((buffer[n + 6] & 0x8) == 0)
771 scsi_CDs[i].cdi.mask |= CDC_OPEN_TRAY;
773 if ((buffer[n + 6] >> 5) == mechtype_individual_changer ||
774 (buffer[n + 6] >> 5) == mechtype_cartridge_changer)
775 scsi_CDs[i].cdi.capacity =
776 cdrom_number_of_slots(&(scsi_CDs[i].cdi));
777 if (scsi_CDs[i].cdi.capacity <= 1)
779 scsi_CDs[i].cdi.mask |= CDC_SELECT_DISC;
780 /*else I don't think it can close its tray
781 scsi_CDs[i].cdi.mask |= CDC_CLOSE_TRAY; */
783 scsi_free(buffer, 512);
787 * sr_packet() is the entry point for the generic commands generated
788 * by the Uniform CD-ROM layer.
790 static int sr_packet(struct cdrom_device_info *cdi, struct cdrom_generic_command *cgc)
792 Scsi_Device *device = scsi_CDs[MINOR(cdi->dev)].device;
795 if (device->scsi_level <= SCSI_2)
796 cgc->cmd[1] |= device->lun << 5;
798 cgc->stat = sr_do_ioctl(MINOR(cdi->dev), cgc->cmd, cgc->buffer, cgc->buflen, cgc->quiet, cgc->data_direction, cgc->sense);
803 static int sr_registered;
809 if (sr_template.dev_noticed == 0)
812 if (!sr_registered) {
813 if (devfs_register_blkdev(MAJOR_NR, "sr", &sr_bdops)) {
814 printk("Unable to get major %d for SCSI-CD\n", MAJOR_NR);
815 sr_template.dev_noticed = 0;
823 sr_template.dev_max = sr_template.dev_noticed + SR_EXTRA_DEVS;
824 scsi_CDs = kmalloc(sr_template.dev_max * sizeof(Scsi_CD), GFP_ATOMIC);
827 memset(scsi_CDs, 0, sr_template.dev_max * sizeof(Scsi_CD));
829 sr_sizes = kmalloc(sr_template.dev_max * sizeof(int), GFP_ATOMIC);
832 memset(sr_sizes, 0, sr_template.dev_max * sizeof(int));
834 sr_blocksizes = kmalloc(sr_template.dev_max * sizeof(int), GFP_ATOMIC);
838 sr_hardsizes = kmalloc(sr_template.dev_max * sizeof(int), GFP_ATOMIC);
840 goto cleanup_blocksizes;
842 * These are good guesses for the time being.
844 for (i = 0; i < sr_template.dev_max; i++) {
845 sr_blocksizes[i] = 2048;
846 sr_hardsizes[i] = 2048;
848 blksize_size[MAJOR_NR] = sr_blocksizes;
849 hardsect_size[MAJOR_NR] = sr_hardsizes;
852 kfree(sr_blocksizes);
859 devfs_unregister_blkdev(MAJOR_NR, "sr");
860 sr_template.dev_noticed = 0;
870 blk_dev[MAJOR_NR].queue = sr_find_queue;
871 blk_size[MAJOR_NR] = sr_sizes;
873 for (i = 0; i < sr_template.nr_dev; ++i) {
874 /* If we have already seen this, then skip it. Comes up
875 * with loadable modules. */
876 if (scsi_CDs[i].capacity)
878 scsi_CDs[i].capacity = 0x1fffff;
879 scsi_CDs[i].device->sector_size = 2048; /* A guess, just in case */
880 scsi_CDs[i].needs_sector_size = 1;
881 scsi_CDs[i].device->changed = 1; /* force recheck CD type */
883 /* seems better to leave this for later */
885 printk("Scd sectorsize = %d bytes.\n", scsi_CDs[i].sector_size);
889 scsi_CDs[i].device->ten = 1;
890 scsi_CDs[i].device->remap = 1;
891 scsi_CDs[i].readcd_known = 0;
892 scsi_CDs[i].readcd_cdda = 0;
893 sr_sizes[i] = scsi_CDs[i].capacity >> (BLOCK_SIZE_BITS - 9);
895 scsi_CDs[i].cdi.ops = &sr_dops;
896 scsi_CDs[i].cdi.handle = &scsi_CDs[i];
897 scsi_CDs[i].cdi.dev = MKDEV(MAJOR_NR, i);
898 scsi_CDs[i].cdi.mask = 0;
899 scsi_CDs[i].cdi.capacity = 1;
901 * FIXME: someone needs to handle a get_capabilities
902 * failure properly ??
907 sprintf(name, "sr%d", i);
908 strcpy(scsi_CDs[i].cdi.name, name);
910 devfs_register (scsi_CDs[i].device->de, "cd",
911 DEVFS_FL_DEFAULT, MAJOR_NR, i,
912 S_IFBLK | S_IRUGO | S_IWUGO,
914 register_cdrom(&scsi_CDs[i].cdi);
918 /* If our host adapter is capable of scatter-gather, then we increase
919 * the read-ahead to 16 blocks (32 sectors). If not, we use
920 * a two block (4 sector) read ahead. */
921 if (scsi_CDs[0].device && scsi_CDs[0].device->host->sg_tablesize)
922 read_ahead[MAJOR_NR] = 32; /* 32 sector read-ahead. Always removable. */
924 read_ahead[MAJOR_NR] = 4; /* 4 sector read-ahead */
929 static void sr_detach(Scsi_Device * SDp)
934 if (scsi_CDs == NULL)
936 for (cpnt = scsi_CDs, i = 0; i < sr_template.dev_max; i++, cpnt++)
937 if (cpnt->device == SDp) {
939 * Since the cdrom is read-only, no need to sync the device.
940 * We should be kind to our buffer cache, however.
942 invalidate_device(MKDEV(MAJOR_NR, i), 0);
945 * Reset things back to a sane state so that one can re-load a new
946 * driver (perhaps the same one).
948 unregister_cdrom(&(cpnt->cdi));
952 sr_template.nr_dev--;
953 sr_template.dev_noticed--;
960 static int __init init_sr(void)
962 sr_template.module = THIS_MODULE;
963 return scsi_register_module(MODULE_SCSI_DEV, &sr_template);
966 static void __exit exit_sr(void)
968 scsi_unregister_module(MODULE_SCSI_DEV, &sr_template);
969 devfs_unregister_blkdev(MAJOR_NR, "sr");
971 if (scsi_CDs != NULL) {
977 kfree(sr_blocksizes);
978 sr_blocksizes = NULL;
982 blksize_size[MAJOR_NR] = NULL;
983 hardsect_size[MAJOR_NR] = NULL;
984 blk_size[MAJOR_NR] = NULL;
985 read_ahead[MAJOR_NR] = 0;
987 sr_template.dev_max = 0;
990 module_init(init_sr);
991 module_exit(exit_sr);
992 MODULE_LICENSE("GPL");