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] == 0xF0) { /* Sense data is valid */
215 (SCpnt->sense_buffer[0] & 0x7f) == 0x70) { /* Sense data is valid */ //+Wilson04012004,from 2.4.24
216 switch (SCpnt->sense_buffer[2]) {
218 case VOLUME_OVERFLOW:
219 case ILLEGAL_REQUEST:
220 //+Wilson04012004, from 2.4.24
221 if (!(SCpnt->sense_buffer[0] & 0x80))
224 error_sector = (SCpnt->sense_buffer[3] << 24) |
225 (SCpnt->sense_buffer[4] << 16) |
226 (SCpnt->sense_buffer[5] << 8) |
227 SCpnt->sense_buffer[6];
228 if (SCpnt->request.bh != NULL)
229 block_sectors = SCpnt->request.bh->b_size >> 9;
230 if (block_sectors < 4)
232 if (scsi_CDs[device_nr].device->sector_size == 2048)
234 error_sector &= ~(block_sectors - 1);
235 good_sectors = error_sector - SCpnt->request.sector;
236 if (good_sectors < 0 || good_sectors >= this_count)
239 * The SCSI specification allows for the value returned
240 * by READ CAPACITY to be up to 75 2K sectors past the
241 * last readable block. Therefore, if we hit a medium
242 * error within the last 75 2K sectors, we decrease the
245 if ((error_sector >> 1) < sr_sizes[device_nr] &&
246 scsi_CDs[device_nr].capacity - error_sector < 4 *75)
247 sr_sizes[device_nr] = error_sector >> 1;
250 case RECOVERED_ERROR:
252 * An error occured, but it recovered. Inform the
253 * user, but make sure that it's not treated as a
256 print_sense("sr", SCpnt);
258 SCpnt->sense_buffer[0] = 0x0;
259 good_sectors = this_count;
268 * This calls the generic completion function, now that we know
269 * how many actual sectors finished, and how many sectors we need
270 * to say have failed.
272 scsi_io_completion(SCpnt, good_sectors, block_sectors);
276 static request_queue_t *sr_find_queue(kdev_t dev)
281 if (MINOR(dev) >= sr_template.dev_max || !scsi_CDs[MINOR(dev)].device)
284 return &scsi_CDs[MINOR(dev)].device->request_queue;
287 static int sr_scatter_pad(Scsi_Cmnd *SCpnt, int s_size)
289 struct scatterlist *sg, *old_sg = NULL;
290 int i, fsize, bsize, sg_ent, sg_count;
292 void **bbpnt, **old_bbpnt = NULL;
295 sg_ent = SCpnt->use_sg;
296 bsize = 0; /* gcc... */
301 if ((fsize = SCpnt->request.sector % (s_size >> 9))) {
304 if ((front = scsi_malloc(fsize)) == NULL)
308 * need a back pad too
310 if ((bsize = s_size - ((SCpnt->request_bufflen + fsize) % s_size))) {
312 if ((back = scsi_malloc(bsize)) == NULL)
317 * extend or allocate new scatter-gather table
319 sg_count = SCpnt->use_sg;
321 old_sg = (struct scatterlist *) SCpnt->request_buffer;
322 old_bbpnt = SCpnt->bounce_buffers;
328 /* Get space for scatterlist and bounce buffer array. */
329 i = sg_ent * sizeof(struct scatterlist);
330 i += sg_ent * sizeof(void *);
331 i = (i + 511) & ~511;
333 if ((sg = scsi_malloc(i)) == NULL)
337 ((char *)sg + (sg_ent * sizeof(struct scatterlist)));
340 * no more failing memory allocs possible, we can safely assign
343 SCpnt->sglist_len = i;
344 SCpnt->use_sg = sg_count;
345 memset(sg, 0, SCpnt->sglist_len);
349 sg[0].address = bbpnt[0] = front;
350 sg[0].length = fsize;
354 memcpy(sg + i, old_sg, SCpnt->use_sg * sizeof(struct scatterlist));
356 memcpy(bbpnt + i, old_bbpnt, SCpnt->use_sg * sizeof(void *));
357 scsi_free(old_sg, (((SCpnt->use_sg * sizeof(struct scatterlist)) +
358 (SCpnt->use_sg * sizeof(void *))) + 511) & ~511);
360 sg[i].address = SCpnt->request_buffer;
361 sg[i].length = SCpnt->request_bufflen;
364 SCpnt->request_bufflen += (fsize + bsize);
365 SCpnt->request_buffer = sg;
366 SCpnt->bounce_buffers = bbpnt;
370 sg[SCpnt->use_sg].address = back;
371 bbpnt[SCpnt->use_sg] = back;
372 sg[SCpnt->use_sg].length = bsize;
379 printk("sr: ran out of mem for scatter pad\n");
381 scsi_free(front, fsize);
383 scsi_free(back, bsize);
389 static int sr_init_command(Scsi_Cmnd * SCpnt)
391 int dev, devm, block=0, this_count, s_size;
393 devm = MINOR(SCpnt->request.rq_dev);
394 dev = DEVICE_NR(SCpnt->request.rq_dev);
396 SCSI_LOG_HLQUEUE(1, printk("Doing sr request, dev = %d, block = %d\n", devm, block));
398 if (dev >= sr_template.nr_dev ||
399 !scsi_CDs[dev].device ||
400 !scsi_CDs[dev].device->online) {
401 SCSI_LOG_HLQUEUE(2, printk("Finishing %ld sectors\n", SCpnt->request.nr_sectors));
402 SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt));
405 if (scsi_CDs[dev].device->changed) {
407 * quietly refuse to do anything to a changed disc until the
408 * changed bit has been reset
413 if ((SCpnt->request.cmd == WRITE) && !scsi_CDs[dev].device->writeable)
417 * we do lazy blocksize switching (when reading XA sectors,
418 * see CDROMREADMODE2 ioctl)
420 s_size = scsi_CDs[dev].device->sector_size;
423 sr_set_blocklength(DEVICE_NR(CURRENT->rq_dev), 2048);
425 printk("sr: can't switch blocksize: in interrupt\n");
428 if (s_size != 512 && s_size != 1024 && s_size != 2048) {
429 printk("sr: bad sector size %d\n", s_size);
433 block = SCpnt->request.sector / (s_size >> 9);
436 * request doesn't start on hw block boundary, add scatter pads
438 if ((SCpnt->request.sector % (s_size >> 9)) || (SCpnt->request_bufflen % s_size))
439 if (sr_scatter_pad(SCpnt, s_size))
442 this_count = (SCpnt->request_bufflen >> 9) / (s_size >> 9);
444 switch (SCpnt->request.cmd) {
446 SCpnt->cmnd[0] = WRITE_10;
447 SCpnt->sc_data_direction = SCSI_DATA_WRITE;
450 SCpnt->cmnd[0] = READ_10;
451 SCpnt->sc_data_direction = SCSI_DATA_READ;
454 printk("Unknown sr command %d\n", SCpnt->request.cmd);
458 SCSI_LOG_HLQUEUE(2, printk("sr%d : %s %d/%ld 512 byte blocks.\n",
460 (SCpnt->request.cmd == WRITE) ? "writing" : "reading",
461 this_count, SCpnt->request.nr_sectors));
463 SCpnt->cmnd[1] = (SCpnt->device->scsi_level <= SCSI_2) ?
464 ((SCpnt->lun << 5) & 0xe0) : 0;
466 if (this_count > 0xffff)
469 SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
470 SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
471 SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
472 SCpnt->cmnd[5] = (unsigned char) block & 0xff;
473 SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
474 SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
475 SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
478 * We shouldn't disconnect in the middle of a sector, so with a dumb
479 * host adapter, it's safe to assume that we can at least transfer
480 * this many bytes between each connect / disconnect.
482 SCpnt->transfersize = scsi_CDs[dev].device->sector_size;
483 SCpnt->underflow = this_count << 9;
485 SCpnt->allowed = MAX_RETRIES;
486 SCpnt->timeout_per_command = SR_TIMEOUT;
489 * This is the completion routine we use. This is matched in terms
490 * of capability to this function.
492 SCpnt->done = rw_intr;
495 struct scatterlist *sg = SCpnt->request_buffer;
497 for (i = 0; i < SCpnt->use_sg; i++)
498 size += sg[i].length;
500 if (size != SCpnt->request_bufflen && SCpnt->use_sg) {
501 printk("sr: mismatch count %d, bytes %d\n", size, SCpnt->request_bufflen);
502 SCpnt->request_bufflen = size;
507 * This indicates that the command is ready from our end to be
513 struct block_device_operations sr_bdops =
517 release: cdrom_release,
519 check_media_change: cdrom_media_changed,
522 static int sr_open(struct cdrom_device_info *cdi, int purpose)
524 check_disk_change(cdi->dev);
526 if (MINOR(cdi->dev) >= sr_template.dev_max
527 || !scsi_CDs[MINOR(cdi->dev)].device) {
528 return -ENXIO; /* No such device */
531 * If the device is in error recovery, wait until it is done.
532 * If the device is offline, then disallow any access to it.
534 if (!scsi_block_when_processing_errors(scsi_CDs[MINOR(cdi->dev)].device)) {
537 scsi_CDs[MINOR(cdi->dev)].device->access_count++;
538 if (scsi_CDs[MINOR(cdi->dev)].device->host->hostt->module)
539 __MOD_INC_USE_COUNT(scsi_CDs[MINOR(cdi->dev)].device->host->hostt->module);
540 if (sr_template.module)
541 __MOD_INC_USE_COUNT(sr_template.module);
543 /* If this device did not have media in the drive at boot time, then
544 * we would have been unable to get the sector size. Check to see if
545 * this is the case, and try again.
548 if (scsi_CDs[MINOR(cdi->dev)].needs_sector_size)
549 get_sectorsize(MINOR(cdi->dev));
554 static int sr_detect(Scsi_Device * SDp)
557 if (SDp->type != TYPE_ROM && SDp->type != TYPE_WORM)
559 sr_template.dev_noticed++;
563 static int sr_attach(Scsi_Device * SDp)
568 if (SDp->type != TYPE_ROM && SDp->type != TYPE_WORM)
571 if (sr_template.nr_dev >= sr_template.dev_max) {
575 for (cpnt = scsi_CDs, i = 0; i < sr_template.dev_max; i++, cpnt++)
579 if (i >= sr_template.dev_max)
580 panic("scsi_devices corrupt (sr)");
583 scsi_CDs[i].device = SDp;
585 sr_template.nr_dev++;
586 if (sr_template.nr_dev > sr_template.dev_max)
587 panic("scsi_devices corrupt (sr)");
589 printk("Attached scsi CD-ROM sr%d at scsi%d, channel %d, id %d, lun %d\n",
590 i, SDp->host->host_no, SDp->channel, SDp->id, SDp->lun);
595 void get_sectorsize(int i)
597 unsigned char cmd[10];
598 unsigned char *buffer;
599 int the_result, retries;
603 buffer = (unsigned char *) scsi_malloc(512);
604 SRpnt = scsi_allocate_request(scsi_CDs[i].device);
606 if(buffer == NULL || SRpnt == NULL)
608 scsi_CDs[i].capacity = 0x1fffff;
609 sector_size = 2048; /* A guess, just in case */
610 scsi_CDs[i].needs_sector_size = 1;
612 scsi_free(buffer, 512);
614 scsi_release_request(SRpnt);
620 cmd[0] = READ_CAPACITY;
621 cmd[1] = (scsi_CDs[i].device->scsi_level <= SCSI_2) ?
622 ((scsi_CDs[i].device->lun << 5) & 0xe0) : 0;
623 memset((void *) &cmd[2], 0, 8);
624 SRpnt->sr_request.rq_status = RQ_SCSI_BUSY; /* Mark as really busy */
625 SRpnt->sr_cmd_len = 0;
627 memset(buffer, 0, 8);
629 /* Do the command and wait.. */
631 SRpnt->sr_data_direction = SCSI_DATA_READ;
632 scsi_wait_req(SRpnt, (void *) cmd, (void *) buffer,
633 8, SR_TIMEOUT, MAX_RETRIES);
635 the_result = SRpnt->sr_result;
638 } while (the_result && retries);
641 scsi_release_request(SRpnt);
645 scsi_CDs[i].capacity = 0x1fffff;
646 sector_size = 2048; /* A guess, just in case */
647 scsi_CDs[i].needs_sector_size = 1;
650 if (cdrom_get_last_written(MKDEV(MAJOR_NR, i),
651 &scsi_CDs[i].capacity))
653 scsi_CDs[i].capacity = 1 + ((buffer[0] << 24) |
657 sector_size = (buffer[4] << 24) |
658 (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
659 switch (sector_size) {
661 * HP 4020i CD-Recorder reports 2340 byte sectors
662 * Philips CD-Writers report 2352 byte sectors
664 * Use 2k sectors for them..
672 scsi_CDs[i].capacity *= 4;
677 printk("sr%d: unsupported sector size %d.\n",
679 scsi_CDs[i].capacity = 0;
680 scsi_CDs[i].needs_sector_size = 1;
683 scsi_CDs[i].device->sector_size = sector_size;
686 * Add this so that we have the ability to correctly gauge
687 * what the device is capable of.
689 scsi_CDs[i].needs_sector_size = 0;
690 sr_sizes[i] = scsi_CDs[i].capacity >> (BLOCK_SIZE_BITS - 9);
692 scsi_free(buffer, 512);
695 void get_capabilities(int i)
697 unsigned char cmd[6];
698 unsigned char *buffer;
701 static char *loadmech[] =
713 buffer = (unsigned char *) scsi_malloc(512);
716 printk(KERN_ERR "sr: out of memory.\n");
720 cmd[1] = (scsi_CDs[i].device->scsi_level <= SCSI_2) ?
721 ((scsi_CDs[i].device->lun << 5) & 0xe0) : 0;
725 rc = sr_do_ioctl(i, cmd, buffer, 128, 1, SCSI_DATA_READ, NULL);
728 /* failed, drive doesn't have capabilities mode page */
729 scsi_CDs[i].cdi.speed = 1;
730 scsi_CDs[i].cdi.mask |= (CDC_CD_R | CDC_CD_RW | CDC_DVD_R |
731 CDC_DVD | CDC_DVD_RAM |
732 CDC_SELECT_DISC | CDC_SELECT_SPEED);
733 scsi_free(buffer, 512);
734 printk("sr%i: scsi-1 drive\n", i);
738 scsi_CDs[i].cdi.speed = ((buffer[n + 8] << 8) + buffer[n + 9]) / 176;
739 scsi_CDs[i].readcd_known = 1;
740 scsi_CDs[i].readcd_cdda = buffer[n + 5] & 0x01;
741 /* print some capability bits */
742 printk("sr%i: scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n", i,
743 ((buffer[n + 14] << 8) + buffer[n + 15]) / 176,
744 scsi_CDs[i].cdi.speed,
745 buffer[n + 3] & 0x01 ? "writer " : "", /* CD Writer */
746 buffer[n + 3] & 0x20 ? "dvd-ram " : "",
747 buffer[n + 2] & 0x02 ? "cd/rw " : "", /* can read rewriteable */
748 buffer[n + 4] & 0x20 ? "xa/form2 " : "", /* can read xa/from2 */
749 buffer[n + 5] & 0x01 ? "cdda " : "", /* can read audio data */
750 loadmech[buffer[n + 6] >> 5]);
751 if ((buffer[n + 6] >> 5) == 0)
752 /* caddy drives can't close tray... */
753 scsi_CDs[i].cdi.mask |= CDC_CLOSE_TRAY;
754 if ((buffer[n + 2] & 0x8) == 0)
755 /* not a DVD drive */
756 scsi_CDs[i].cdi.mask |= CDC_DVD;
757 if ((buffer[n + 3] & 0x20) == 0) {
758 /* can't write DVD-RAM media */
759 scsi_CDs[i].cdi.mask |= CDC_DVD_RAM;
761 scsi_CDs[i].device->writeable = 1;
763 if ((buffer[n + 3] & 0x10) == 0)
764 /* can't write DVD-R media */
765 scsi_CDs[i].cdi.mask |= CDC_DVD_R;
766 if ((buffer[n + 3] & 0x2) == 0)
767 /* can't write CD-RW media */
768 scsi_CDs[i].cdi.mask |= CDC_CD_RW;
769 if ((buffer[n + 3] & 0x1) == 0)
770 /* can't write CD-R media */
771 scsi_CDs[i].cdi.mask |= CDC_CD_R;
772 if ((buffer[n + 6] & 0x8) == 0)
774 scsi_CDs[i].cdi.mask |= CDC_OPEN_TRAY;
776 if ((buffer[n + 6] >> 5) == mechtype_individual_changer ||
777 (buffer[n + 6] >> 5) == mechtype_cartridge_changer)
778 scsi_CDs[i].cdi.capacity =
779 cdrom_number_of_slots(&(scsi_CDs[i].cdi));
780 if (scsi_CDs[i].cdi.capacity <= 1)
782 scsi_CDs[i].cdi.mask |= CDC_SELECT_DISC;
783 /*else I don't think it can close its tray
784 scsi_CDs[i].cdi.mask |= CDC_CLOSE_TRAY; */
786 scsi_free(buffer, 512);
790 * sr_packet() is the entry point for the generic commands generated
791 * by the Uniform CD-ROM layer.
793 static int sr_packet(struct cdrom_device_info *cdi, struct cdrom_generic_command *cgc)
795 Scsi_Device *device = scsi_CDs[MINOR(cdi->dev)].device;
798 if (device->scsi_level <= SCSI_2)
799 cgc->cmd[1] |= device->lun << 5;
801 cgc->stat = sr_do_ioctl(MINOR(cdi->dev), cgc->cmd, cgc->buffer, cgc->buflen, cgc->quiet, cgc->data_direction, cgc->sense);
806 static int sr_registered;
812 if (sr_template.dev_noticed == 0)
815 if (!sr_registered) {
816 if (devfs_register_blkdev(MAJOR_NR, "sr", &sr_bdops)) {
817 printk("Unable to get major %d for SCSI-CD\n", MAJOR_NR);
818 sr_template.dev_noticed = 0;
826 sr_template.dev_max = sr_template.dev_noticed + SR_EXTRA_DEVS;
827 scsi_CDs = kmalloc(sr_template.dev_max * sizeof(Scsi_CD), GFP_ATOMIC);
830 memset(scsi_CDs, 0, sr_template.dev_max * sizeof(Scsi_CD));
832 sr_sizes = kmalloc(sr_template.dev_max * sizeof(int), GFP_ATOMIC);
835 memset(sr_sizes, 0, sr_template.dev_max * sizeof(int));
837 sr_blocksizes = kmalloc(sr_template.dev_max * sizeof(int), GFP_ATOMIC);
841 sr_hardsizes = kmalloc(sr_template.dev_max * sizeof(int), GFP_ATOMIC);
843 goto cleanup_blocksizes;
845 * These are good guesses for the time being.
847 for (i = 0; i < sr_template.dev_max; i++) {
848 sr_blocksizes[i] = 2048;
849 sr_hardsizes[i] = 2048;
851 blksize_size[MAJOR_NR] = sr_blocksizes;
852 hardsect_size[MAJOR_NR] = sr_hardsizes;
855 kfree(sr_blocksizes);
862 devfs_unregister_blkdev(MAJOR_NR, "sr");
863 sr_template.dev_noticed = 0;
873 blk_dev[MAJOR_NR].queue = sr_find_queue;
874 blk_size[MAJOR_NR] = sr_sizes;
876 for (i = 0; i < sr_template.nr_dev; ++i) {
877 /* If we have already seen this, then skip it. Comes up
878 * with loadable modules. */
879 if (scsi_CDs[i].capacity)
881 scsi_CDs[i].capacity = 0x1fffff;
882 scsi_CDs[i].device->sector_size = 2048; /* A guess, just in case */
883 scsi_CDs[i].needs_sector_size = 1;
884 scsi_CDs[i].device->changed = 1; /* force recheck CD type */
886 /* seems better to leave this for later */
888 printk("Scd sectorsize = %d bytes.\n", scsi_CDs[i].sector_size);
892 scsi_CDs[i].device->ten = 1;
893 scsi_CDs[i].device->remap = 1;
894 scsi_CDs[i].readcd_known = 0;
895 scsi_CDs[i].readcd_cdda = 0;
896 sr_sizes[i] = scsi_CDs[i].capacity >> (BLOCK_SIZE_BITS - 9);
898 scsi_CDs[i].cdi.ops = &sr_dops;
899 scsi_CDs[i].cdi.handle = &scsi_CDs[i];
900 scsi_CDs[i].cdi.dev = MKDEV(MAJOR_NR, i);
901 scsi_CDs[i].cdi.mask = 0;
902 scsi_CDs[i].cdi.capacity = 1;
904 * FIXME: someone needs to handle a get_capabilities
905 * failure properly ??
910 sprintf(name, "sr%d", i);
911 strcpy(scsi_CDs[i].cdi.name, name);
913 devfs_register (scsi_CDs[i].device->de, "cd",
914 DEVFS_FL_DEFAULT, MAJOR_NR, i,
915 S_IFBLK | S_IRUGO | S_IWUGO,
917 register_cdrom(&scsi_CDs[i].cdi);
921 /* If our host adapter is capable of scatter-gather, then we increase
922 * the read-ahead to 16 blocks (32 sectors). If not, we use
923 * a two block (4 sector) read ahead. */
924 if (scsi_CDs[0].device && scsi_CDs[0].device->host->sg_tablesize)
925 read_ahead[MAJOR_NR] = 32; /* 32 sector read-ahead. Always removable. */
927 read_ahead[MAJOR_NR] = 4; /* 4 sector read-ahead */
932 static void sr_detach(Scsi_Device * SDp)
937 if (scsi_CDs == NULL)
939 for (cpnt = scsi_CDs, i = 0; i < sr_template.dev_max; i++, cpnt++)
940 if (cpnt->device == SDp) {
942 * Since the cdrom is read-only, no need to sync the device.
943 * We should be kind to our buffer cache, however.
945 invalidate_device(MKDEV(MAJOR_NR, i), 0);
948 * Reset things back to a sane state so that one can re-load a new
949 * driver (perhaps the same one).
951 unregister_cdrom(&(cpnt->cdi));
955 sr_template.nr_dev--;
956 sr_template.dev_noticed--;
963 static int __init init_sr(void)
965 sr_template.module = THIS_MODULE;
966 return scsi_register_module(MODULE_SCSI_DEV, &sr_template);
969 static void __exit exit_sr(void)
971 scsi_unregister_module(MODULE_SCSI_DEV, &sr_template);
972 devfs_unregister_blkdev(MAJOR_NR, "sr");
974 if (scsi_CDs != NULL) {
980 kfree(sr_blocksizes);
981 sr_blocksizes = NULL;
985 blksize_size[MAJOR_NR] = NULL;
986 hardsect_size[MAJOR_NR] = NULL;
987 blk_size[MAJOR_NR] = NULL;
988 read_ahead[MAJOR_NR] = 0;
990 sr_template.dev_max = 0;
993 module_init(init_sr);
994 module_exit(exit_sr);
995 MODULE_LICENSE("GPL");