Ok, test10-final is out there now. This has no _known_ bugs that I
[davej-history.git] / drivers / scsi / sr.c
blobeb2c84f925a97818d2652cd60dfa334de5d211fb
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
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>
38 #include <linux/fs.h>
39 #include <linux/kernel.h>
40 #include <linux/sched.h>
41 #include <linux/mm.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>
48 #include <asm/io.h>
49 #include <asm/uaccess.h>
51 #define MAJOR_NR SCSI_CDROM_MAJOR
52 #include <linux/blk.h>
53 #include "scsi.h"
54 #include "hosts.h"
55 #include "sr.h"
56 #include <scsi/scsi_ioctl.h> /* For the door lock/unlock commands */
57 #include "constants.h"
59 #ifdef MODULE
60 MODULE_PARM(xa_test, "i"); /* see sr_ioctl.c */
61 #endif
63 #define MAX_RETRIES 3
64 #define SR_TIMEOUT (30 * HZ)
66 static int sr_init(void);
67 static void sr_finish(void);
68 static int sr_attach(Scsi_Device *);
69 static int sr_detect(Scsi_Device *);
70 static void sr_detach(Scsi_Device *);
72 static int sr_init_command(Scsi_Cmnd *);
74 static struct Scsi_Device_Template sr_template =
76 name:"cdrom",
77 tag:"sr",
78 scsi_type:TYPE_ROM,
79 major:SCSI_CDROM_MAJOR,
80 blk:1,
81 detect:sr_detect,
82 init:sr_init,
83 finish:sr_finish,
84 attach:sr_attach,
85 detach:sr_detach,
86 init_command:sr_init_command
89 Scsi_CD *scsi_CDs;
90 static int *sr_sizes;
92 static int *sr_blocksizes;
93 static int *sr_hardsizes;
95 static int sr_open(struct cdrom_device_info *, int);
96 void get_sectorsize(int);
97 void get_capabilities(int);
99 static int sr_media_change(struct cdrom_device_info *, int);
100 static int sr_packet(struct cdrom_device_info *, struct cdrom_generic_command *);
102 static void sr_release(struct cdrom_device_info *cdi)
104 if (scsi_CDs[MINOR(cdi->dev)].device->sector_size > 2048)
105 sr_set_blocklength(MINOR(cdi->dev), 2048);
106 sync_dev(cdi->dev);
107 scsi_CDs[MINOR(cdi->dev)].device->access_count--;
108 if (scsi_CDs[MINOR(cdi->dev)].device->host->hostt->module)
109 __MOD_DEC_USE_COUNT(scsi_CDs[MINOR(cdi->dev)].device->host->hostt->module);
110 if (sr_template.module)
111 __MOD_DEC_USE_COUNT(sr_template.module);
114 static struct cdrom_device_ops sr_dops =
116 open: sr_open,
117 release: sr_release,
118 drive_status: sr_drive_status,
119 media_changed: sr_media_change,
120 tray_move: sr_tray_move,
121 lock_door: sr_lock_door,
122 select_speed: sr_select_speed,
123 get_last_session: sr_get_last_session,
124 get_mcn: sr_get_mcn,
125 reset: sr_reset,
126 audio_ioctl: sr_audio_ioctl,
127 dev_ioctl: sr_dev_ioctl,
128 capability: CDC_CLOSE_TRAY | CDC_OPEN_TRAY | CDC_LOCK |
129 CDC_SELECT_SPEED | CDC_SELECT_DISC |
130 CDC_MULTI_SESSION | CDC_MCN |
131 CDC_MEDIA_CHANGED | CDC_PLAY_AUDIO |
132 CDC_RESET | CDC_IOCTLS | CDC_DRIVE_STATUS |
133 CDC_CD_R | CDC_CD_RW | CDC_DVD | CDC_DVD_R |
134 CDC_DVD_RAM | CDC_GENERIC_PACKET,
135 generic_packet: sr_packet,
139 * This function checks to see if the media has been changed in the
140 * CDROM drive. It is possible that we have already sensed a change,
141 * or the drive may have sensed one and not yet reported it. We must
142 * be ready for either case. This function always reports the current
143 * value of the changed bit. If flag is 0, then the changed bit is reset.
144 * This function could be done as an ioctl, but we would need to have
145 * an inode for that to work, and we do not always have one.
148 int sr_media_change(struct cdrom_device_info *cdi, int slot)
150 int retval;
152 if (CDSL_CURRENT != slot) {
153 /* no changer support */
154 return -EINVAL;
156 retval = scsi_ioctl(scsi_CDs[MINOR(cdi->dev)].device,
157 SCSI_IOCTL_TEST_UNIT_READY, 0);
159 if (retval) {
160 /* Unable to test, unit probably not ready. This usually
161 * means there is no disc in the drive. Mark as changed,
162 * and we will figure it out later once the drive is
163 * available again. */
165 scsi_CDs[MINOR(cdi->dev)].device->changed = 1;
166 return 1; /* This will force a flush, if called from
167 * check_disk_change */
170 retval = scsi_CDs[MINOR(cdi->dev)].device->changed;
171 scsi_CDs[MINOR(cdi->dev)].device->changed = 0;
172 /* If the disk changed, the capacity will now be different,
173 * so we force a re-read of this information */
174 if (retval) {
175 /* check multisession offset etc */
176 sr_cd_check(cdi);
179 * If the disk changed, the capacity will now be different,
180 * so we force a re-read of this information
181 * Force 2048 for the sector size so that filesystems won't
182 * be trying to use something that is too small if the disc
183 * has changed.
185 scsi_CDs[MINOR(cdi->dev)].needs_sector_size = 1;
187 scsi_CDs[MINOR(cdi->dev)].device->sector_size = 2048;
189 return retval;
193 * rw_intr is the interrupt routine for the device driver. It will be notified on the
194 * end of a SCSI read / write, and will take on of several actions based on success or failure.
197 static void rw_intr(Scsi_Cmnd * SCpnt)
199 int result = SCpnt->result;
200 int this_count = SCpnt->bufflen >> 9;
201 int good_sectors = (result == 0 ? this_count : 0);
202 int block_sectors = 0;
204 #ifdef DEBUG
205 printk("sr.c done: %x %x\n", result, SCpnt->request.bh->b_data);
206 #endif
208 Handle MEDIUM ERRORs or VOLUME OVERFLOWs that indicate partial success.
209 Since this is a relatively rare error condition, no care is taken to
210 avoid unnecessary additional work such as memcpy's that could be avoided.
214 if (driver_byte(result) != 0 && /* An error occurred */
215 SCpnt->sense_buffer[0] == 0xF0 && /* Sense data is valid */
216 (SCpnt->sense_buffer[2] == MEDIUM_ERROR ||
217 SCpnt->sense_buffer[2] == VOLUME_OVERFLOW ||
218 SCpnt->sense_buffer[2] == ILLEGAL_REQUEST)) {
219 long error_sector = (SCpnt->sense_buffer[3] << 24) |
220 (SCpnt->sense_buffer[4] << 16) |
221 (SCpnt->sense_buffer[5] << 8) |
222 SCpnt->sense_buffer[6];
223 int device_nr = DEVICE_NR(SCpnt->request.rq_dev);
224 if (SCpnt->request.bh != NULL)
225 block_sectors = SCpnt->request.bh->b_size >> 9;
226 if (block_sectors < 4)
227 block_sectors = 4;
228 if (scsi_CDs[device_nr].device->sector_size == 2048)
229 error_sector <<= 2;
230 error_sector &= ~(block_sectors - 1);
231 good_sectors = error_sector - SCpnt->request.sector;
232 if (good_sectors < 0 || good_sectors >= this_count)
233 good_sectors = 0;
235 * The SCSI specification allows for the value returned by READ
236 * CAPACITY to be up to 75 2K sectors past the last readable
237 * block. Therefore, if we hit a medium error within the last
238 * 75 2K sectors, we decrease the saved size value.
240 if ((error_sector >> 1) < sr_sizes[device_nr] &&
241 scsi_CDs[device_nr].capacity - error_sector < 4 * 75)
242 sr_sizes[device_nr] = error_sector >> 1;
245 * This calls the generic completion function, now that we know
246 * how many actual sectors finished, and how many sectors we need
247 * to say have failed.
249 scsi_io_completion(SCpnt, good_sectors, block_sectors);
253 static request_queue_t *sr_find_queue(kdev_t dev)
256 * No such device
258 if (MINOR(dev) >= sr_template.dev_max || !scsi_CDs[MINOR(dev)].device)
259 return NULL;
261 return &scsi_CDs[MINOR(dev)].device->request_queue;
264 static int sr_init_command(Scsi_Cmnd * SCpnt)
266 int dev, devm, block, this_count;
268 devm = MINOR(SCpnt->request.rq_dev);
269 dev = DEVICE_NR(SCpnt->request.rq_dev);
271 block = SCpnt->request.sector;
272 this_count = SCpnt->request_bufflen >> 9;
274 if (!SCpnt->request.bh) {
276 * Umm, yeah, right. Swapping to a cdrom. Nice try.
278 return 0;
280 SCSI_LOG_HLQUEUE(1, printk("Doing sr request, dev = %d, block = %d\n", devm, block));
282 if (dev >= sr_template.nr_dev ||
283 !scsi_CDs[dev].device ||
284 !scsi_CDs[dev].device->online) {
285 SCSI_LOG_HLQUEUE(2, printk("Finishing %ld sectors\n", SCpnt->request.nr_sectors));
286 SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt));
287 return 0;
289 if (scsi_CDs[dev].device->changed) {
291 * quietly refuse to do anything to a changed disc until the changed
292 * bit has been reset
294 /* printk("SCSI disk has been changed. Prohibiting further I/O.\n"); */
295 return 0;
298 * we do lazy blocksize switching (when reading XA sectors,
299 * see CDROMREADMODE2 ioctl)
301 if (scsi_CDs[dev].device->sector_size > 2048) {
302 if (!in_interrupt())
303 sr_set_blocklength(DEVICE_NR(CURRENT->rq_dev), 2048);
304 else
305 printk("sr: can't switch blocksize: in interrupt\n");
308 if ((SCpnt->request.cmd == WRITE) && !scsi_CDs[dev].device->writeable)
309 return 0;
311 if (scsi_CDs[dev].device->sector_size == 1024) {
312 if ((block & 1) || (SCpnt->request.nr_sectors & 1)) {
313 printk("sr.c:Bad 1K block number requested (%d %ld)",
314 block, SCpnt->request.nr_sectors);
315 return 0;
316 } else {
317 block = block >> 1;
318 this_count = this_count >> 1;
321 if (scsi_CDs[dev].device->sector_size == 2048) {
322 if ((block & 3) || (SCpnt->request.nr_sectors & 3)) {
323 printk("sr.c:Bad 2K block number requested (%d %ld)",
324 block, SCpnt->request.nr_sectors);
325 return 0;
326 } else {
327 block = block >> 2;
328 this_count = this_count >> 2;
331 switch (SCpnt->request.cmd) {
332 case WRITE:
333 SCpnt->cmnd[0] = WRITE_10;
334 SCpnt->sc_data_direction = SCSI_DATA_WRITE;
335 break;
336 case READ:
337 SCpnt->cmnd[0] = READ_10;
338 SCpnt->sc_data_direction = SCSI_DATA_READ;
339 break;
340 default:
341 panic("Unknown sr command %d\n", SCpnt->request.cmd);
344 SCSI_LOG_HLQUEUE(2, printk("sr%d : %s %d/%ld 512 byte blocks.\n",
345 devm,
346 (SCpnt->request.cmd == WRITE) ? "writing" : "reading",
347 this_count, SCpnt->request.nr_sectors));
349 SCpnt->cmnd[1] = (SCpnt->lun << 5) & 0xe0;
351 if (this_count > 0xffff)
352 this_count = 0xffff;
354 SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
355 SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
356 SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
357 SCpnt->cmnd[5] = (unsigned char) block & 0xff;
358 SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
359 SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
360 SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
363 * We shouldn't disconnect in the middle of a sector, so with a dumb
364 * host adapter, it's safe to assume that we can at least transfer
365 * this many bytes between each connect / disconnect.
367 SCpnt->transfersize = scsi_CDs[dev].device->sector_size;
368 SCpnt->underflow = this_count << 9;
370 SCpnt->allowed = MAX_RETRIES;
371 SCpnt->timeout_per_command = SR_TIMEOUT;
374 * This is the completion routine we use. This is matched in terms
375 * of capability to this function.
377 SCpnt->done = rw_intr;
380 * This indicates that the command is ready from our end to be
381 * queued.
383 return 1;
386 static int sr_open(struct cdrom_device_info *cdi, int purpose)
388 check_disk_change(cdi->dev);
390 if (MINOR(cdi->dev) >= sr_template.dev_max
391 || !scsi_CDs[MINOR(cdi->dev)].device) {
392 return -ENXIO; /* No such device */
395 * If the device is in error recovery, wait until it is done.
396 * If the device is offline, then disallow any access to it.
398 if (!scsi_block_when_processing_errors(scsi_CDs[MINOR(cdi->dev)].device)) {
399 return -ENXIO;
401 scsi_CDs[MINOR(cdi->dev)].device->access_count++;
402 if (scsi_CDs[MINOR(cdi->dev)].device->host->hostt->module)
403 __MOD_INC_USE_COUNT(scsi_CDs[MINOR(cdi->dev)].device->host->hostt->module);
404 if (sr_template.module)
405 __MOD_INC_USE_COUNT(sr_template.module);
407 /* If this device did not have media in the drive at boot time, then
408 * we would have been unable to get the sector size. Check to see if
409 * this is the case, and try again.
412 if (scsi_CDs[MINOR(cdi->dev)].needs_sector_size)
413 get_sectorsize(MINOR(cdi->dev));
415 return 0;
419 * do_sr_request() is the request handler function for the sr driver.
420 * Its function in life is to take block device requests, and
421 * translate them to SCSI commands.
425 static int sr_detect(Scsi_Device * SDp)
428 if (SDp->type != TYPE_ROM && SDp->type != TYPE_WORM)
429 return 0;
431 printk("Detected scsi CD-ROM sr%d at scsi%d, channel %d, id %d, lun %d\n",
432 sr_template.dev_noticed++,
433 SDp->host->host_no, SDp->channel, SDp->id, SDp->lun);
435 return 1;
438 static int sr_attach(Scsi_Device * SDp)
440 Scsi_CD *cpnt;
441 int i;
443 if (SDp->type != TYPE_ROM && SDp->type != TYPE_WORM)
444 return 1;
446 if (sr_template.nr_dev >= sr_template.dev_max) {
447 SDp->attached--;
448 return 1;
450 for (cpnt = scsi_CDs, i = 0; i < sr_template.dev_max; i++, cpnt++)
451 if (!cpnt->device)
452 break;
454 if (i >= sr_template.dev_max)
455 panic("scsi_devices corrupt (sr)");
458 scsi_CDs[i].device = SDp;
460 sr_template.nr_dev++;
461 if (sr_template.nr_dev > sr_template.dev_max)
462 panic("scsi_devices corrupt (sr)");
463 return 0;
467 void get_sectorsize(int i)
469 unsigned char cmd[10];
470 unsigned char *buffer;
471 int the_result, retries;
472 int sector_size;
473 Scsi_Request *SRpnt;
475 buffer = (unsigned char *) scsi_malloc(512);
476 SRpnt = scsi_allocate_request(scsi_CDs[i].device);
478 if(buffer == NULL || SRpnt == NULL)
480 scsi_CDs[i].capacity = 0x1fffff;
481 sector_size = 2048; /* A guess, just in case */
482 scsi_CDs[i].needs_sector_size = 1;
483 if(buffer)
484 scsi_free(buffer, 512);
485 if(SRpnt)
486 scsi_release_request(SRpnt);
487 return;
490 retries = 3;
491 do {
492 cmd[0] = READ_CAPACITY;
493 cmd[1] = (scsi_CDs[i].device->lun << 5) & 0xe0;
494 memset((void *) &cmd[2], 0, 8);
495 SRpnt->sr_request.rq_status = RQ_SCSI_BUSY; /* Mark as really busy */
496 SRpnt->sr_cmd_len = 0;
498 memset(buffer, 0, 8);
500 /* Do the command and wait.. */
502 SRpnt->sr_data_direction = SCSI_DATA_READ;
503 scsi_wait_req(SRpnt, (void *) cmd, (void *) buffer,
504 8, SR_TIMEOUT, MAX_RETRIES);
506 the_result = SRpnt->sr_result;
507 retries--;
509 } while (the_result && retries);
512 scsi_release_request(SRpnt);
513 SRpnt = NULL;
515 if (the_result) {
516 scsi_CDs[i].capacity = 0x1fffff;
517 sector_size = 2048; /* A guess, just in case */
518 scsi_CDs[i].needs_sector_size = 1;
519 } else {
520 #if 0
521 if (cdrom_get_last_written(MKDEV(MAJOR_NR, i),
522 (long *) &scsi_CDs[i].capacity))
523 #endif
524 scsi_CDs[i].capacity = 1 + ((buffer[0] << 24) |
525 (buffer[1] << 16) |
526 (buffer[2] << 8) |
527 buffer[3]);
528 sector_size = (buffer[4] << 24) |
529 (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
530 switch (sector_size) {
532 * HP 4020i CD-Recorder reports 2340 byte sectors
533 * Philips CD-Writers report 2352 byte sectors
535 * Use 2k sectors for them..
537 case 0:
538 case 2340:
539 case 2352:
540 sector_size = 2048;
541 /* fall through */
542 case 2048:
543 scsi_CDs[i].capacity *= 4;
544 /* fall through */
545 case 512:
546 break;
547 default:
548 printk("sr%d: unsupported sector size %d.\n",
549 i, sector_size);
550 scsi_CDs[i].capacity = 0;
551 scsi_CDs[i].needs_sector_size = 1;
554 scsi_CDs[i].device->sector_size = sector_size;
557 * Add this so that we have the ability to correctly gauge
558 * what the device is capable of.
560 scsi_CDs[i].needs_sector_size = 0;
561 sr_sizes[i] = scsi_CDs[i].capacity >> (BLOCK_SIZE_BITS - 9);
563 scsi_free(buffer, 512);
566 void get_capabilities(int i)
568 unsigned char cmd[6];
569 unsigned char *buffer;
570 int rc, n;
572 static char *loadmech[] =
574 "caddy",
575 "tray",
576 "pop-up",
578 "changer",
579 "cartridge changer",
584 buffer = (unsigned char *) scsi_malloc(512);
585 cmd[0] = MODE_SENSE;
586 cmd[1] = (scsi_CDs[i].device->lun << 5) & 0xe0;
587 cmd[2] = 0x2a;
588 cmd[4] = 128;
589 cmd[3] = cmd[5] = 0;
590 rc = sr_do_ioctl(i, cmd, buffer, 128, 1, SCSI_DATA_READ);
592 if (-EINVAL == rc) {
593 /* failed, drive has'nt this mode page */
594 scsi_CDs[i].cdi.speed = 1;
595 /* disable speed select, drive probably can't do this either */
596 scsi_CDs[i].cdi.mask |= CDC_SELECT_SPEED;
597 scsi_free(buffer, 512);
598 return;
600 n = buffer[3] + 4;
601 scsi_CDs[i].cdi.speed = ((buffer[n + 8] << 8) + buffer[n + 9]) / 176;
602 scsi_CDs[i].readcd_known = 1;
603 scsi_CDs[i].readcd_cdda = buffer[n + 5] & 0x01;
604 /* print some capability bits */
605 printk("sr%i: scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n", i,
606 ((buffer[n + 14] << 8) + buffer[n + 15]) / 176,
607 scsi_CDs[i].cdi.speed,
608 buffer[n + 3] & 0x01 ? "writer " : "", /* CD Writer */
609 buffer[n + 3] & 0x20 ? "dvd-ram " : "",
610 buffer[n + 2] & 0x02 ? "cd/rw " : "", /* can read rewriteable */
611 buffer[n + 4] & 0x20 ? "xa/form2 " : "", /* can read xa/from2 */
612 buffer[n + 5] & 0x01 ? "cdda " : "", /* can read audio data */
613 loadmech[buffer[n + 6] >> 5]);
614 if ((buffer[n + 6] >> 5) == 0)
615 /* caddy drives can't close tray... */
616 scsi_CDs[i].cdi.mask |= CDC_CLOSE_TRAY;
617 if ((buffer[n + 2] & 0x8) == 0)
618 /* not a DVD drive */
619 scsi_CDs[i].cdi.mask |= CDC_DVD;
620 if ((buffer[n + 3] & 0x20) == 0) {
621 /* can't write DVD-RAM media */
622 scsi_CDs[i].cdi.mask |= CDC_DVD_RAM;
623 } else {
624 scsi_CDs[i].device->writeable = 1;
626 if ((buffer[n + 3] & 0x10) == 0)
627 /* can't write DVD-R media */
628 scsi_CDs[i].cdi.mask |= CDC_DVD_R;
629 if ((buffer[n + 3] & 0x2) == 0)
630 /* can't write CD-RW media */
631 scsi_CDs[i].cdi.mask |= CDC_CD_RW;
632 if ((buffer[n + 3] & 0x1) == 0)
633 /* can't write CD-R media */
634 scsi_CDs[i].cdi.mask |= CDC_CD_R;
635 if ((buffer[n + 6] & 0x8) == 0)
636 /* can't eject */
637 scsi_CDs[i].cdi.mask |= CDC_OPEN_TRAY;
639 if ((buffer[n + 6] >> 5) == mechtype_individual_changer ||
640 (buffer[n + 6] >> 5) == mechtype_cartridge_changer)
641 scsi_CDs[i].cdi.capacity =
642 cdrom_number_of_slots(&(scsi_CDs[i].cdi));
643 if (scsi_CDs[i].cdi.capacity <= 1)
644 /* not a changer */
645 scsi_CDs[i].cdi.mask |= CDC_SELECT_DISC;
646 /*else I don't think it can close its tray
647 scsi_CDs[i].cdi.mask |= CDC_CLOSE_TRAY; */
649 scsi_free(buffer, 512);
653 * sr_packet() is the entry point for the generic commands generated
654 * by the Uniform CD-ROM layer.
656 static int sr_packet(struct cdrom_device_info *cdi, struct cdrom_generic_command *cgc)
658 Scsi_Request *SRpnt;
659 Scsi_Device *device = scsi_CDs[MINOR(cdi->dev)].device;
660 unsigned char *buffer = cgc->buffer;
661 int buflen;
663 /* get the device */
664 SRpnt = scsi_allocate_request(device);
665 if (SRpnt == NULL)
666 return -ENODEV; /* this just doesn't seem right /axboe */
668 /* use buffer for ISA DMA */
669 buflen = (cgc->buflen + 511) & ~511;
670 if (cgc->buffer && SRpnt->sr_host->unchecked_isa_dma &&
671 (virt_to_phys(cgc->buffer) + cgc->buflen - 1 > ISA_DMA_THRESHOLD)) {
672 buffer = scsi_malloc(buflen);
673 if (buffer == NULL) {
674 printk("sr: SCSI DMA pool exhausted.");
675 return -ENOMEM;
677 memcpy(buffer, cgc->buffer, cgc->buflen);
679 /* set the LUN */
680 cgc->cmd[1] |= device->lun << 5;
682 /* do the locking and issue the command */
683 SRpnt->sr_request.rq_dev = cdi->dev;
684 /* scsi_wait_req sets the command length */
685 SRpnt->sr_cmd_len = 0;
687 SRpnt->sr_data_direction = cgc->data_direction;
688 scsi_wait_req(SRpnt, (void *) cgc->cmd, (void *) buffer, cgc->buflen,
689 SR_TIMEOUT, MAX_RETRIES);
691 if ((cgc->stat = SRpnt->sr_result))
692 cgc->sense = (struct request_sense *) SRpnt->sr_sense_buffer;
694 /* release */
695 SRpnt->sr_request.rq_dev = MKDEV(0, 0);
696 scsi_release_request(SRpnt);
697 SRpnt = NULL;
699 /* write DMA buffer back if used */
700 if (buffer && (buffer != cgc->buffer)) {
701 memcpy(cgc->buffer, buffer, cgc->buflen);
702 scsi_free(buffer, buflen);
706 return cgc->stat;
709 static int sr_registered;
711 static int sr_init()
713 int i;
715 if (sr_template.dev_noticed == 0)
716 return 0;
718 if (!sr_registered) {
719 if (devfs_register_blkdev(MAJOR_NR, "sr", &cdrom_fops)) {
720 printk("Unable to get major %d for SCSI-CD\n", MAJOR_NR);
721 return 1;
723 sr_registered++;
725 if (scsi_CDs)
726 return 0;
728 sr_template.dev_max = sr_template.dev_noticed + SR_EXTRA_DEVS;
729 scsi_CDs = kmalloc(sr_template.dev_max * sizeof(Scsi_CD), GFP_ATOMIC);
730 if (!scsi_CDs)
731 goto cleanup_devfs;
732 memset(scsi_CDs, 0, sr_template.dev_max * sizeof(Scsi_CD));
734 sr_sizes = kmalloc(sr_template.dev_max * sizeof(int), GFP_ATOMIC);
735 if (!sr_sizes)
736 goto cleanup_cds;
737 memset(sr_sizes, 0, sr_template.dev_max * sizeof(int));
739 sr_blocksizes = kmalloc(sr_template.dev_max * sizeof(int), GFP_ATOMIC);
740 if (!sr_blocksizes)
741 goto cleanup_sizes;
743 sr_hardsizes = kmalloc(sr_template.dev_max * sizeof(int), GFP_ATOMIC);
744 if (!sr_hardsizes)
745 goto cleanup_blocksizes;
747 * These are good guesses for the time being.
749 for (i = 0; i < sr_template.dev_max; i++) {
750 sr_blocksizes[i] = 2048;
751 sr_hardsizes[i] = 2048;
753 blksize_size[MAJOR_NR] = sr_blocksizes;
754 hardsect_size[MAJOR_NR] = sr_hardsizes;
755 return 0;
756 cleanup_blocksizes:
757 kfree(sr_blocksizes);
758 cleanup_sizes:
759 kfree(sr_sizes);
760 cleanup_cds:
761 kfree(scsi_CDs);
762 cleanup_devfs:
763 devfs_unregister_blkdev(MAJOR_NR, "sr");
764 sr_registered--;
765 return 1;
768 void sr_finish()
770 int i;
771 char name[6];
773 blk_dev[MAJOR_NR].queue = sr_find_queue;
774 blk_size[MAJOR_NR] = sr_sizes;
776 for (i = 0; i < sr_template.nr_dev; ++i) {
777 /* If we have already seen this, then skip it. Comes up
778 * with loadable modules. */
779 if (scsi_CDs[i].capacity)
780 continue;
781 scsi_CDs[i].capacity = 0x1fffff;
782 scsi_CDs[i].device->sector_size = 2048; /* A guess, just in case */
783 scsi_CDs[i].needs_sector_size = 1;
784 scsi_CDs[i].device->changed = 1; /* force recheck CD type */
785 #if 0
786 /* seems better to leave this for later */
787 get_sectorsize(i);
788 printk("Scd sectorsize = %d bytes.\n", scsi_CDs[i].sector_size);
789 #endif
790 scsi_CDs[i].use = 1;
792 scsi_CDs[i].device->ten = 1;
793 scsi_CDs[i].device->remap = 1;
794 scsi_CDs[i].readcd_known = 0;
795 scsi_CDs[i].readcd_cdda = 0;
796 sr_sizes[i] = scsi_CDs[i].capacity >> (BLOCK_SIZE_BITS - 9);
798 scsi_CDs[i].cdi.ops = &sr_dops;
799 scsi_CDs[i].cdi.handle = &scsi_CDs[i];
800 scsi_CDs[i].cdi.dev = MKDEV(MAJOR_NR, i);
801 scsi_CDs[i].cdi.mask = 0;
802 scsi_CDs[i].cdi.capacity = 1;
803 get_capabilities(i);
804 sr_vendor_init(i);
806 sprintf(name, "sr%d", i);
807 strcpy(scsi_CDs[i].cdi.name, name);
808 scsi_CDs[i].cdi.de =
809 devfs_register (scsi_CDs[i].device->de, "cd",
810 DEVFS_FL_DEFAULT, MAJOR_NR, i,
811 S_IFBLK | S_IRUGO | S_IWUGO,
812 &cdrom_fops, NULL);
813 register_cdrom(&scsi_CDs[i].cdi);
817 /* If our host adapter is capable of scatter-gather, then we increase
818 * the read-ahead to 16 blocks (32 sectors). If not, we use
819 * a two block (4 sector) read ahead. */
820 if (scsi_CDs[0].device && scsi_CDs[0].device->host->sg_tablesize)
821 read_ahead[MAJOR_NR] = 32; /* 32 sector read-ahead. Always removable. */
822 else
823 read_ahead[MAJOR_NR] = 4; /* 4 sector read-ahead */
825 return;
828 static void sr_detach(Scsi_Device * SDp)
830 Scsi_CD *cpnt;
831 int i;
833 for (cpnt = scsi_CDs, i = 0; i < sr_template.dev_max; i++, cpnt++)
834 if (cpnt->device == SDp) {
835 kdev_t devi = MKDEV(MAJOR_NR, i);
836 struct super_block *sb = get_super(devi);
839 * Since the cdrom is read-only, no need to sync the device.
840 * We should be kind to our buffer cache, however.
842 if (sb)
843 invalidate_inodes(sb);
844 invalidate_buffers(devi);
847 * Reset things back to a sane state so that one can re-load a new
848 * driver (perhaps the same one).
850 unregister_cdrom(&(cpnt->cdi));
851 cpnt->device = NULL;
852 cpnt->capacity = 0;
853 SDp->attached--;
854 sr_template.nr_dev--;
855 sr_template.dev_noticed--;
856 sr_sizes[i] = 0;
857 return;
859 return;
862 static int __init init_sr(void)
864 sr_template.module = THIS_MODULE;
865 return scsi_register_module(MODULE_SCSI_DEV, &sr_template);
868 static void __exit exit_sr(void)
870 scsi_unregister_module(MODULE_SCSI_DEV, &sr_template);
871 devfs_unregister_blkdev(MAJOR_NR, "sr");
872 sr_registered--;
873 if (scsi_CDs != NULL) {
874 kfree(scsi_CDs);
876 kfree(sr_sizes);
877 sr_sizes = NULL;
879 kfree(sr_blocksizes);
880 sr_blocksizes = NULL;
881 kfree(sr_hardsizes);
882 sr_hardsizes = NULL;
884 blksize_size[MAJOR_NR] = NULL;
885 hardsect_size[MAJOR_NR] = NULL;
886 blk_size[MAJOR_NR] = NULL;
887 read_ahead[MAJOR_NR] = 0;
889 sr_template.dev_max = 0;
892 module_init(init_sr);
893 module_exit(exit_sr);