Import 2.3.50pre1
[davej-history.git] / drivers / scsi / sr.c
blob781cde85af84b4c3c778fe723964f475bac11862
1 /*
2 * sr.c Copyright (C) 1992 David Giller
3 * Copyright (C) 1993, 1994, 1995, 1999 Eric Youngdale
5 * adapted from:
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
12 * enhancements.
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
18 * provide auto-eject.
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>
32 #include <linux/fs.h>
33 #include <linux/kernel.h>
34 #include <linux/sched.h>
35 #include <linux/mm.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>
41 #include <asm/io.h>
42 #include <asm/uaccess.h>
44 #define MAJOR_NR SCSI_CDROM_MAJOR
45 #include <linux/blk.h>
46 #include "scsi.h"
47 #include "hosts.h"
48 #include "sr.h"
49 #include <scsi/scsi_ioctl.h> /* For the door lock/unlock commands */
50 #include "constants.h"
52 #ifdef MODULE
53 MODULE_PARM(xa_test, "i"); /* see sr_ioctl.c */
54 #endif
56 #define MAX_RETRIES 3
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 =
69 name:"cdrom",
70 tag:"sr",
71 scsi_type:TYPE_ROM,
72 major:SCSI_CDROM_MAJOR,
73 blk:1,
74 detect:sr_detect,
75 init:sr_init,
76 finish:sr_finish,
77 attach:sr_attach,
78 detach:sr_detach,
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);
99 sync_dev(cdi->dev);
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 =
109 sr_open, /* open */
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 |
126 CDC_GENERIC_PACKET,
128 sr_packet
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)
143 int retval;
145 if (CDSL_CURRENT != slot) {
146 /* no changer support */
147 return -EINVAL;
149 retval = scsi_ioctl(scsi_CDs[MINOR(cdi->dev)].device,
150 SCSI_IOCTL_TEST_UNIT_READY, 0);
152 if (retval) {
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 */
167 if (retval) {
168 /* check multisession offset etc */
169 sr_cd_check(cdi);
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
176 * has changed.
178 scsi_CDs[MINOR(cdi->dev)].needs_sector_size = 1;
180 scsi_CDs[MINOR(cdi->dev)].device->sector_size = 2048;
182 return retval;
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;
197 #ifdef DEBUG
198 printk("sr.c done: %x %x\n", result, SCpnt->request.bh->b_data);
199 #endif
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)
220 block_sectors = 4;
221 if (scsi_CDs[device_nr].device->sector_size == 2048)
222 error_sector <<= 2;
223 error_sector &= ~(block_sectors - 1);
224 good_sectors = error_sector - SCpnt->request.sector;
225 if (good_sectors < 0 || good_sectors >= this_count)
226 good_sectors = 0;
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)
249 * No such device
251 if (MINOR(dev) >= sr_template.dev_max || !scsi_CDs[MINOR(dev)].device)
252 return NULL;
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.
271 return 0;
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));
280 return 0;
282 if (scsi_CDs[dev].device->changed) {
284 * quietly refuse to do anything to a changed disc until the changed
285 * bit has been reset
287 /* printk("SCSI disk has been changed. Prohibiting further I/O.\n"); */
288 return 0;
291 * we do lazy blocksize switching (when reading XA sectors,
292 * see CDROMREADMODE2 ioctl)
294 if (scsi_CDs[dev].device->sector_size > 2048) {
295 if (!in_interrupt())
296 sr_set_blocklength(DEVICE_NR(CURRENT->rq_dev), 2048);
297 else
298 printk("sr: can't switch blocksize: in interrupt\n");
300 if (SCpnt->request.cmd == WRITE) {
301 return 0;
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);
307 return 0;
308 } else {
309 block = block >> 1;
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);
317 return 0;
318 } else {
319 block = block >> 2;
320 this_count = this_count >> 2;
323 switch (SCpnt->request.cmd) {
324 case WRITE:
325 if (!scsi_CDs[dev].device->writeable) {
326 return 0;
328 SCpnt->cmnd[0] = WRITE_10;
329 SCpnt->sc_data_direction = SCSI_DATA_WRITE;
330 break;
331 case READ:
332 SCpnt->cmnd[0] = READ_10;
333 SCpnt->sc_data_direction = SCSI_DATA_READ;
334 break;
335 default:
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",
340 devm,
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)
347 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
376 * queued.
378 return 1;
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)) {
394 return -ENXIO;
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));
410 return 0;
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)
424 return 0;
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);
430 return 1;
433 static int sr_attach(Scsi_Device * SDp)
435 Scsi_CD *cpnt;
436 int i;
438 if (SDp->type != TYPE_ROM && SDp->type != TYPE_WORM)
439 return 1;
441 if (sr_template.nr_dev >= sr_template.dev_max) {
442 SDp->attached--;
443 return 1;
445 for (cpnt = scsi_CDs, i = 0; i < sr_template.dev_max; i++, cpnt++)
446 if (!cpnt->device)
447 break;
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)");
458 return 0;
462 void get_sectorsize(int i)
464 unsigned char cmd[10];
465 unsigned char *buffer;
466 int the_result, retries;
467 int sector_size;
468 Scsi_Request *SRpnt;
470 buffer = (unsigned char *) scsi_malloc(512);
473 SRpnt = scsi_allocate_request(scsi_CDs[i].device);
475 retries = 3;
476 do {
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;
492 retries--;
494 } while (the_result && retries);
497 scsi_release_request(SRpnt);
498 SRpnt = NULL;
500 if (the_result) {
501 scsi_CDs[i].capacity = 0x1fffff;
502 sector_size = 2048; /* A guess, just in case */
503 scsi_CDs[i].needs_sector_size = 1;
504 } else {
505 #if 0
506 if (cdrom_get_last_written(MKDEV(MAJOR_NR, i),
507 (long *) &scsi_CDs[i].capacity))
508 #endif
509 scsi_CDs[i].capacity = 1 + ((buffer[0] << 24) |
510 (buffer[1] << 16) |
511 (buffer[2] << 8) |
512 buffer[3]);
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..
522 case 0:
523 case 2340:
524 case 2352:
525 sector_size = 2048;
526 /* fall through */
527 case 2048:
528 scsi_CDs[i].capacity *= 4;
529 /* fall through */
530 case 512:
531 break;
532 default:
533 printk("sr%d: unsupported sector size %d.\n",
534 i, sector_size);
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;
555 int rc, n;
557 static char *loadmech[] =
559 "caddy",
560 "tray",
561 "pop-up",
563 "changer",
564 "cartridge changer",
569 buffer = (unsigned char *) scsi_malloc(512);
570 cmd[0] = MODE_SENSE;
571 cmd[1] = (scsi_CDs[i].device->lun << 5) & 0xe0;
572 cmd[2] = 0x2a;
573 cmd[4] = 128;
574 cmd[3] = cmd[5] = 0;
575 rc = sr_do_ioctl(i, cmd, buffer, 128, 1, SCSI_DATA_READ);
577 if (-EINVAL == rc) {
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);
583 return;
585 n = buffer[3] + 4;
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)
617 /* can't eject */
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)
625 /* not a changer */
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)
640 Scsi_Request *SRpnt;
641 Scsi_Device *device = scsi_CDs[MINOR(cdi->dev)].device;
642 unsigned char *buffer = cgc->buffer;
643 int buflen;
645 /* get the device */
646 SRpnt = scsi_allocate_request(device);
647 if (SRpnt == NULL)
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.");
657 return -ENOMEM;
659 memcpy(buffer, cgc->buffer, cgc->buflen);
661 /* set the LUN */
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;
676 /* release */
677 SRpnt->sr_request.rq_dev = MKDEV(0, 0);
678 scsi_release_request(SRpnt);
679 SRpnt = NULL;
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);
688 return cgc->stat;
691 static int sr_registered = 0;
693 static int sr_init()
695 int i;
697 if (sr_template.dev_noticed == 0)
698 return 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);
703 return 1;
705 sr_registered++;
707 if (scsi_CDs)
708 return 0;
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;
732 return 0;
735 void sr_finish()
737 int i;
738 char name[6];
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)
747 continue;
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 */
752 #if 0
753 /* seems better to leave this for later */
754 get_sectorsize(i);
755 printk("Scd sectorsize = %d bytes.\n", scsi_CDs[i].sector_size);
756 #endif
757 scsi_CDs[i].use = 1;
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;
770 get_capabilities(i);
771 sr_vendor_init(i);
773 sprintf(name, "sr%d", i);
774 strcpy(scsi_CDs[i].cdi.name, name);
775 scsi_CDs[i].cdi.de =
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,
779 &cdrom_fops, NULL);
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. */
789 else
790 read_ahead[MAJOR_NR] = 4; /* 4 sector read-ahead */
792 return;
795 static void sr_detach(Scsi_Device * SDp)
797 Scsi_CD *cpnt;
798 int i;
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.
809 if (sb)
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));
818 cpnt->device = NULL;
819 cpnt->capacity = 0;
820 SDp->attached--;
821 sr_template.nr_dev--;
822 sr_template.dev_noticed--;
823 sr_sizes[i] = 0;
824 return;
826 return;
830 #ifdef MODULE
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");
842 sr_registered--;
843 if (scsi_CDs != NULL) {
844 kfree((char *) scsi_CDs);
846 kfree((char *) sr_sizes);
847 sr_sizes = NULL;
849 kfree((char *) sr_blocksizes);
850 sr_blocksizes = NULL;
851 kfree((char *) sr_hardsizes);
852 sr_hardsizes = NULL;
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;
863 #endif /* MODULE */
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
869 * of the file.
870 * ---------------------------------------------------------------------------
871 * Local variables:
872 * c-indent-level: 4
873 * c-brace-imaginary-offset: 0
874 * c-brace-offset: -4
875 * c-argdecl-indent: 4
876 * c-label-offset: -4
877 * c-continued-statement-offset: 4
878 * c-continued-brace-offset: 0
879 * indent-tabs-mode: nil
880 * tab-width: 8
881 * End: