Linux 2.4.0-test7pre1
[davej-history.git] / drivers / scsi / sr.c
blobc5680bca31cf8e46913a6b6833311f7bd6bffc44
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
33 #include <linux/module.h>
35 #include <linux/fs.h>
36 #include <linux/kernel.h>
37 #include <linux/sched.h>
38 #include <linux/mm.h>
39 #include <linux/string.h>
40 #include <linux/errno.h>
41 #include <linux/cdrom.h>
42 #include <linux/interrupt.h>
43 #include <asm/system.h>
44 #include <asm/io.h>
45 #include <asm/uaccess.h>
47 #define MAJOR_NR SCSI_CDROM_MAJOR
48 #include <linux/blk.h>
49 #include "scsi.h"
50 #include "hosts.h"
51 #include "sr.h"
52 #include <scsi/scsi_ioctl.h> /* For the door lock/unlock commands */
53 #include "constants.h"
55 #ifdef MODULE
56 MODULE_PARM(xa_test, "i"); /* see sr_ioctl.c */
57 #endif
59 #define MAX_RETRIES 3
60 #define SR_TIMEOUT (30 * HZ)
62 static int sr_init(void);
63 static void sr_finish(void);
64 static int sr_attach(Scsi_Device *);
65 static int sr_detect(Scsi_Device *);
66 static void sr_detach(Scsi_Device *);
68 static int sr_init_command(Scsi_Cmnd *);
70 struct Scsi_Device_Template sr_template =
72 name:"cdrom",
73 tag:"sr",
74 scsi_type:TYPE_ROM,
75 major:SCSI_CDROM_MAJOR,
76 blk:1,
77 detect:sr_detect,
78 init:sr_init,
79 finish:sr_finish,
80 attach:sr_attach,
81 detach:sr_detach,
82 init_command:sr_init_command
85 Scsi_CD *scsi_CDs = NULL;
86 static int *sr_sizes = NULL;
88 static int *sr_blocksizes = NULL;
89 static int *sr_hardsizes = NULL;
91 static int sr_open(struct cdrom_device_info *, int);
92 void get_sectorsize(int);
93 void get_capabilities(int);
95 static int sr_media_change(struct cdrom_device_info *, int);
96 static int sr_packet(struct cdrom_device_info *, struct cdrom_generic_command *);
98 static void sr_release(struct cdrom_device_info *cdi)
100 if (scsi_CDs[MINOR(cdi->dev)].device->sector_size > 2048)
101 sr_set_blocklength(MINOR(cdi->dev), 2048);
102 sync_dev(cdi->dev);
103 scsi_CDs[MINOR(cdi->dev)].device->access_count--;
104 if (scsi_CDs[MINOR(cdi->dev)].device->host->hostt->module)
105 __MOD_DEC_USE_COUNT(scsi_CDs[MINOR(cdi->dev)].device->host->hostt->module);
106 if (sr_template.module)
107 __MOD_DEC_USE_COUNT(sr_template.module);
110 static struct cdrom_device_ops sr_dops =
112 open: sr_open,
113 release: sr_release,
114 drive_status: sr_drive_status,
115 media_changed: sr_media_change,
116 tray_move: sr_tray_move,
117 lock_door: sr_lock_door,
118 select_speed: sr_select_speed,
119 get_last_session: sr_get_last_session,
120 get_mcn: sr_get_mcn,
121 reset: sr_reset,
122 audio_ioctl: sr_audio_ioctl,
123 dev_ioctl: sr_dev_ioctl,
124 capability: CDC_CLOSE_TRAY | CDC_OPEN_TRAY | CDC_LOCK |
125 CDC_SELECT_SPEED | CDC_SELECT_DISC |
126 CDC_MULTI_SESSION | CDC_MCN |
127 CDC_MEDIA_CHANGED | CDC_PLAY_AUDIO |
128 CDC_RESET | CDC_IOCTLS | CDC_DRIVE_STATUS |
129 CDC_CD_R | CDC_CD_RW | CDC_DVD | CDC_DVD_R |
130 CDC_DVD_RAM | CDC_GENERIC_PACKET,
131 generic_packet: sr_packet,
135 * This function checks to see if the media has been changed in the
136 * CDROM drive. It is possible that we have already sensed a change,
137 * or the drive may have sensed one and not yet reported it. We must
138 * be ready for either case. This function always reports the current
139 * value of the changed bit. If flag is 0, then the changed bit is reset.
140 * This function could be done as an ioctl, but we would need to have
141 * an inode for that to work, and we do not always have one.
144 int sr_media_change(struct cdrom_device_info *cdi, int slot)
146 int retval;
148 if (CDSL_CURRENT != slot) {
149 /* no changer support */
150 return -EINVAL;
152 retval = scsi_ioctl(scsi_CDs[MINOR(cdi->dev)].device,
153 SCSI_IOCTL_TEST_UNIT_READY, 0);
155 if (retval) {
156 /* Unable to test, unit probably not ready. This usually
157 * means there is no disc in the drive. Mark as changed,
158 * and we will figure it out later once the drive is
159 * available again. */
161 scsi_CDs[MINOR(cdi->dev)].device->changed = 1;
162 return 1; /* This will force a flush, if called from
163 * check_disk_change */
166 retval = scsi_CDs[MINOR(cdi->dev)].device->changed;
167 scsi_CDs[MINOR(cdi->dev)].device->changed = 0;
168 /* If the disk changed, the capacity will now be different,
169 * so we force a re-read of this information */
170 if (retval) {
171 /* check multisession offset etc */
172 sr_cd_check(cdi);
175 * If the disk changed, the capacity will now be different,
176 * so we force a re-read of this information
177 * Force 2048 for the sector size so that filesystems won't
178 * be trying to use something that is too small if the disc
179 * has changed.
181 scsi_CDs[MINOR(cdi->dev)].needs_sector_size = 1;
183 scsi_CDs[MINOR(cdi->dev)].device->sector_size = 2048;
185 return retval;
189 * rw_intr is the interrupt routine for the device driver. It will be notified on the
190 * end of a SCSI read / write, and will take on of several actions based on success or failure.
193 static void rw_intr(Scsi_Cmnd * SCpnt)
195 int result = SCpnt->result;
196 int this_count = SCpnt->bufflen >> 9;
197 int good_sectors = (result == 0 ? this_count : 0);
198 int block_sectors = 0;
200 #ifdef DEBUG
201 printk("sr.c done: %x %x\n", result, SCpnt->request.bh->b_data);
202 #endif
204 Handle MEDIUM ERRORs or VOLUME OVERFLOWs that indicate partial success.
205 Since this is a relatively rare error condition, no care is taken to
206 avoid unnecessary additional work such as memcpy's that could be avoided.
210 if (driver_byte(result) != 0 && /* An error occurred */
211 SCpnt->sense_buffer[0] == 0xF0 && /* Sense data is valid */
212 (SCpnt->sense_buffer[2] == MEDIUM_ERROR ||
213 SCpnt->sense_buffer[2] == VOLUME_OVERFLOW ||
214 SCpnt->sense_buffer[2] == ILLEGAL_REQUEST)) {
215 long error_sector = (SCpnt->sense_buffer[3] << 24) |
216 (SCpnt->sense_buffer[4] << 16) |
217 (SCpnt->sense_buffer[5] << 8) |
218 SCpnt->sense_buffer[6];
219 int device_nr = DEVICE_NR(SCpnt->request.rq_dev);
220 if (SCpnt->request.bh != NULL)
221 block_sectors = SCpnt->request.bh->b_size >> 9;
222 if (block_sectors < 4)
223 block_sectors = 4;
224 if (scsi_CDs[device_nr].device->sector_size == 2048)
225 error_sector <<= 2;
226 error_sector &= ~(block_sectors - 1);
227 good_sectors = error_sector - SCpnt->request.sector;
228 if (good_sectors < 0 || good_sectors >= this_count)
229 good_sectors = 0;
231 * The SCSI specification allows for the value returned by READ
232 * CAPACITY to be up to 75 2K sectors past the last readable
233 * block. Therefore, if we hit a medium error within the last
234 * 75 2K sectors, we decrease the saved size value.
236 if ((error_sector >> 1) < sr_sizes[device_nr] &&
237 scsi_CDs[device_nr].capacity - error_sector < 4 * 75)
238 sr_sizes[device_nr] = error_sector >> 1;
241 * This calls the generic completion function, now that we know
242 * how many actual sectors finished, and how many sectors we need
243 * to say have failed.
245 scsi_io_completion(SCpnt, good_sectors, block_sectors);
249 static request_queue_t *sr_find_queue(kdev_t dev)
252 * No such device
254 if (MINOR(dev) >= sr_template.dev_max || !scsi_CDs[MINOR(dev)].device)
255 return NULL;
257 return &scsi_CDs[MINOR(dev)].device->request_queue;
260 static int sr_init_command(Scsi_Cmnd * SCpnt)
262 int dev, devm, block, this_count;
264 devm = MINOR(SCpnt->request.rq_dev);
265 dev = DEVICE_NR(SCpnt->request.rq_dev);
267 block = SCpnt->request.sector;
268 this_count = SCpnt->request_bufflen >> 9;
270 if (!SCpnt->request.bh) {
272 * Umm, yeah, right. Swapping to a cdrom. Nice try.
274 return 0;
276 SCSI_LOG_HLQUEUE(1, printk("Doing sr request, dev = %d, block = %d\n", devm, block));
278 if (dev >= sr_template.nr_dev ||
279 !scsi_CDs[dev].device ||
280 !scsi_CDs[dev].device->online) {
281 SCSI_LOG_HLQUEUE(2, printk("Finishing %ld sectors\n", SCpnt->request.nr_sectors));
282 SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt));
283 return 0;
285 if (scsi_CDs[dev].device->changed) {
287 * quietly refuse to do anything to a changed disc until the changed
288 * bit has been reset
290 /* printk("SCSI disk has been changed. Prohibiting further I/O.\n"); */
291 return 0;
294 * we do lazy blocksize switching (when reading XA sectors,
295 * see CDROMREADMODE2 ioctl)
297 if (scsi_CDs[dev].device->sector_size > 2048) {
298 if (!in_interrupt())
299 sr_set_blocklength(DEVICE_NR(CURRENT->rq_dev), 2048);
300 else
301 printk("sr: can't switch blocksize: in interrupt\n");
304 if ((SCpnt->request.cmd == WRITE) && !scsi_CDs[dev].device->writeable)
305 return 0;
307 if (scsi_CDs[dev].device->sector_size == 1024) {
308 if ((block & 1) || (SCpnt->request.nr_sectors & 1)) {
309 printk("sr.c:Bad 1K block number requested (%d %ld)",
310 block, SCpnt->request.nr_sectors);
311 return 0;
312 } else {
313 block = block >> 1;
314 this_count = this_count >> 1;
317 if (scsi_CDs[dev].device->sector_size == 2048) {
318 if ((block & 3) || (SCpnt->request.nr_sectors & 3)) {
319 printk("sr.c:Bad 2K block number requested (%d %ld)",
320 block, SCpnt->request.nr_sectors);
321 return 0;
322 } else {
323 block = block >> 2;
324 this_count = this_count >> 2;
327 switch (SCpnt->request.cmd) {
328 case WRITE:
329 SCpnt->cmnd[0] = WRITE_10;
330 SCpnt->sc_data_direction = SCSI_DATA_WRITE;
331 break;
332 case READ:
333 SCpnt->cmnd[0] = READ_10;
334 SCpnt->sc_data_direction = SCSI_DATA_READ;
335 break;
336 default:
337 panic("Unknown sr command %d\n", SCpnt->request.cmd);
340 SCSI_LOG_HLQUEUE(2, printk("sr%d : %s %d/%ld 512 byte blocks.\n",
341 devm,
342 (SCpnt->request.cmd == WRITE) ? "writing" : "reading",
343 this_count, SCpnt->request.nr_sectors));
345 SCpnt->cmnd[1] = (SCpnt->lun << 5) & 0xe0;
347 if (this_count > 0xffff)
348 this_count = 0xffff;
350 SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
351 SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
352 SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
353 SCpnt->cmnd[5] = (unsigned char) block & 0xff;
354 SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
355 SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
356 SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
359 * We shouldn't disconnect in the middle of a sector, so with a dumb
360 * host adapter, it's safe to assume that we can at least transfer
361 * this many bytes between each connect / disconnect.
363 SCpnt->transfersize = scsi_CDs[dev].device->sector_size;
364 SCpnt->underflow = this_count << 9;
366 SCpnt->allowed = MAX_RETRIES;
367 SCpnt->timeout_per_command = SR_TIMEOUT;
370 * This is the completion routine we use. This is matched in terms
371 * of capability to this function.
373 SCpnt->done = rw_intr;
376 * This indicates that the command is ready from our end to be
377 * queued.
379 return 1;
382 static int sr_open(struct cdrom_device_info *cdi, int purpose)
384 check_disk_change(cdi->dev);
386 if (MINOR(cdi->dev) >= sr_template.dev_max
387 || !scsi_CDs[MINOR(cdi->dev)].device) {
388 return -ENXIO; /* No such device */
391 * If the device is in error recovery, wait until it is done.
392 * If the device is offline, then disallow any access to it.
394 if (!scsi_block_when_processing_errors(scsi_CDs[MINOR(cdi->dev)].device)) {
395 return -ENXIO;
397 scsi_CDs[MINOR(cdi->dev)].device->access_count++;
398 if (scsi_CDs[MINOR(cdi->dev)].device->host->hostt->module)
399 __MOD_INC_USE_COUNT(scsi_CDs[MINOR(cdi->dev)].device->host->hostt->module);
400 if (sr_template.module)
401 __MOD_INC_USE_COUNT(sr_template.module);
403 /* If this device did not have media in the drive at boot time, then
404 * we would have been unable to get the sector size. Check to see if
405 * this is the case, and try again.
408 if (scsi_CDs[MINOR(cdi->dev)].needs_sector_size)
409 get_sectorsize(MINOR(cdi->dev));
411 return 0;
415 * do_sr_request() is the request handler function for the sr driver.
416 * Its function in life is to take block device requests, and
417 * translate them to SCSI commands.
421 static int sr_detect(Scsi_Device * SDp)
424 if (SDp->type != TYPE_ROM && SDp->type != TYPE_WORM)
425 return 0;
427 printk("Detected scsi CD-ROM sr%d at scsi%d, channel %d, id %d, lun %d\n",
428 sr_template.dev_noticed++,
429 SDp->host->host_no, SDp->channel, SDp->id, SDp->lun);
431 return 1;
434 static int sr_attach(Scsi_Device * SDp)
436 Scsi_CD *cpnt;
437 int i;
439 if (SDp->type != TYPE_ROM && SDp->type != TYPE_WORM)
440 return 1;
442 if (sr_template.nr_dev >= sr_template.dev_max) {
443 SDp->attached--;
444 return 1;
446 for (cpnt = scsi_CDs, i = 0; i < sr_template.dev_max; i++, cpnt++)
447 if (!cpnt->device)
448 break;
450 if (i >= sr_template.dev_max)
451 panic("scsi_devices corrupt (sr)");
454 scsi_CDs[i].device = SDp;
456 sr_template.nr_dev++;
457 if (sr_template.nr_dev > sr_template.dev_max)
458 panic("scsi_devices corrupt (sr)");
459 return 0;
463 void get_sectorsize(int i)
465 unsigned char cmd[10];
466 unsigned char *buffer;
467 int the_result, retries;
468 int sector_size;
469 Scsi_Request *SRpnt;
471 buffer = (unsigned char *) scsi_malloc(512);
474 SRpnt = scsi_allocate_request(scsi_CDs[i].device);
476 retries = 3;
477 do {
478 cmd[0] = READ_CAPACITY;
479 cmd[1] = (scsi_CDs[i].device->lun << 5) & 0xe0;
480 memset((void *) &cmd[2], 0, 8);
481 SRpnt->sr_request.rq_status = RQ_SCSI_BUSY; /* Mark as really busy */
482 SRpnt->sr_cmd_len = 0;
484 memset(buffer, 0, 8);
486 /* Do the command and wait.. */
488 SRpnt->sr_data_direction = SCSI_DATA_READ;
489 scsi_wait_req(SRpnt, (void *) cmd, (void *) buffer,
490 8, SR_TIMEOUT, MAX_RETRIES);
492 the_result = SRpnt->sr_result;
493 retries--;
495 } while (the_result && retries);
498 scsi_release_request(SRpnt);
499 SRpnt = NULL;
501 if (the_result) {
502 scsi_CDs[i].capacity = 0x1fffff;
503 sector_size = 2048; /* A guess, just in case */
504 scsi_CDs[i].needs_sector_size = 1;
505 } else {
506 #if 0
507 if (cdrom_get_last_written(MKDEV(MAJOR_NR, i),
508 (long *) &scsi_CDs[i].capacity))
509 #endif
510 scsi_CDs[i].capacity = 1 + ((buffer[0] << 24) |
511 (buffer[1] << 16) |
512 (buffer[2] << 8) |
513 buffer[3]);
514 sector_size = (buffer[4] << 24) |
515 (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
516 switch (sector_size) {
518 * HP 4020i CD-Recorder reports 2340 byte sectors
519 * Philips CD-Writers report 2352 byte sectors
521 * Use 2k sectors for them..
523 case 0:
524 case 2340:
525 case 2352:
526 sector_size = 2048;
527 /* fall through */
528 case 2048:
529 scsi_CDs[i].capacity *= 4;
530 /* fall through */
531 case 512:
532 break;
533 default:
534 printk("sr%d: unsupported sector size %d.\n",
535 i, sector_size);
536 scsi_CDs[i].capacity = 0;
537 scsi_CDs[i].needs_sector_size = 1;
540 scsi_CDs[i].device->sector_size = sector_size;
543 * Add this so that we have the ability to correctly gauge
544 * what the device is capable of.
546 scsi_CDs[i].needs_sector_size = 0;
547 sr_sizes[i] = scsi_CDs[i].capacity >> (BLOCK_SIZE_BITS - 9);
549 scsi_free(buffer, 512);
552 void get_capabilities(int i)
554 unsigned char cmd[6];
555 unsigned char *buffer;
556 int rc, n;
558 static char *loadmech[] =
560 "caddy",
561 "tray",
562 "pop-up",
564 "changer",
565 "cartridge changer",
570 buffer = (unsigned char *) scsi_malloc(512);
571 cmd[0] = MODE_SENSE;
572 cmd[1] = (scsi_CDs[i].device->lun << 5) & 0xe0;
573 cmd[2] = 0x2a;
574 cmd[4] = 128;
575 cmd[3] = cmd[5] = 0;
576 rc = sr_do_ioctl(i, cmd, buffer, 128, 1, SCSI_DATA_READ);
578 if (-EINVAL == rc) {
579 /* failed, drive has'nt this mode page */
580 scsi_CDs[i].cdi.speed = 1;
581 /* disable speed select, drive probably can't do this either */
582 scsi_CDs[i].cdi.mask |= CDC_SELECT_SPEED;
583 scsi_free(buffer, 512);
584 return;
586 n = buffer[3] + 4;
587 scsi_CDs[i].cdi.speed = ((buffer[n + 8] << 8) + buffer[n + 9]) / 176;
588 scsi_CDs[i].readcd_known = 1;
589 scsi_CDs[i].readcd_cdda = buffer[n + 5] & 0x01;
590 /* print some capability bits */
591 printk("sr%i: scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n", i,
592 ((buffer[n + 14] << 8) + buffer[n + 15]) / 176,
593 scsi_CDs[i].cdi.speed,
594 buffer[n + 3] & 0x01 ? "writer " : "", /* CD Writer */
595 buffer[n + 3] & 0x20 ? "dvd-ram " : "",
596 buffer[n + 2] & 0x02 ? "cd/rw " : "", /* can read rewriteable */
597 buffer[n + 4] & 0x20 ? "xa/form2 " : "", /* can read xa/from2 */
598 buffer[n + 5] & 0x01 ? "cdda " : "", /* can read audio data */
599 loadmech[buffer[n + 6] >> 5]);
600 if ((buffer[n + 6] >> 5) == 0)
601 /* caddy drives can't close tray... */
602 scsi_CDs[i].cdi.mask |= CDC_CLOSE_TRAY;
603 if ((buffer[n + 2] & 0x8) == 0)
604 /* not a DVD drive */
605 scsi_CDs[i].cdi.mask |= CDC_DVD;
606 if ((buffer[n + 3] & 0x20) == 0) {
607 /* can't write DVD-RAM media */
608 scsi_CDs[i].cdi.mask |= CDC_DVD_RAM;
609 } else {
610 scsi_CDs[i].device->writeable = 1;
612 if ((buffer[n + 3] & 0x10) == 0)
613 /* can't write DVD-R media */
614 scsi_CDs[i].cdi.mask |= CDC_DVD_R;
615 if ((buffer[n + 3] & 0x2) == 0)
616 /* can't write CD-RW media */
617 scsi_CDs[i].cdi.mask |= CDC_CD_RW;
618 if ((buffer[n + 3] & 0x1) == 0)
619 /* can't write CD-R media */
620 scsi_CDs[i].cdi.mask |= CDC_CD_R;
621 if ((buffer[n + 6] & 0x8) == 0)
622 /* can't eject */
623 scsi_CDs[i].cdi.mask |= CDC_OPEN_TRAY;
625 if ((buffer[n + 6] >> 5) == mechtype_individual_changer ||
626 (buffer[n + 6] >> 5) == mechtype_cartridge_changer)
627 scsi_CDs[i].cdi.capacity =
628 cdrom_number_of_slots(&(scsi_CDs[i].cdi));
629 if (scsi_CDs[i].cdi.capacity <= 1)
630 /* not a changer */
631 scsi_CDs[i].cdi.mask |= CDC_SELECT_DISC;
632 /*else I don't think it can close its tray
633 scsi_CDs[i].cdi.mask |= CDC_CLOSE_TRAY; */
635 scsi_free(buffer, 512);
639 * sr_packet() is the entry point for the generic commands generated
640 * by the Uniform CD-ROM layer.
642 static int sr_packet(struct cdrom_device_info *cdi, struct cdrom_generic_command *cgc)
644 Scsi_Request *SRpnt;
645 Scsi_Device *device = scsi_CDs[MINOR(cdi->dev)].device;
646 unsigned char *buffer = cgc->buffer;
647 int buflen;
649 /* get the device */
650 SRpnt = scsi_allocate_request(device);
651 if (SRpnt == NULL)
652 return -ENODEV; /* this just doesn't seem right /axboe */
654 /* use buffer for ISA DMA */
655 buflen = (cgc->buflen + 511) & ~511;
656 if (cgc->buffer && SRpnt->sr_host->unchecked_isa_dma &&
657 (virt_to_phys(cgc->buffer) + cgc->buflen - 1 > ISA_DMA_THRESHOLD)) {
658 buffer = scsi_malloc(buflen);
659 if (buffer == NULL) {
660 printk("sr: SCSI DMA pool exhausted.");
661 return -ENOMEM;
663 memcpy(buffer, cgc->buffer, cgc->buflen);
665 /* set the LUN */
666 cgc->cmd[1] |= device->lun << 5;
668 /* do the locking and issue the command */
669 SRpnt->sr_request.rq_dev = cdi->dev;
670 /* scsi_wait_req sets the command length */
671 SRpnt->sr_cmd_len = 0;
673 SRpnt->sr_data_direction = cgc->data_direction;
674 scsi_wait_req(SRpnt, (void *) cgc->cmd, (void *) buffer, cgc->buflen,
675 SR_TIMEOUT, MAX_RETRIES);
677 if ((cgc->stat = SRpnt->sr_result))
678 cgc->sense = (struct request_sense *) SRpnt->sr_sense_buffer;
680 /* release */
681 SRpnt->sr_request.rq_dev = MKDEV(0, 0);
682 scsi_release_request(SRpnt);
683 SRpnt = NULL;
685 /* write DMA buffer back if used */
686 if (buffer && (buffer != cgc->buffer)) {
687 memcpy(cgc->buffer, buffer, cgc->buflen);
688 scsi_free(buffer, buflen);
692 return cgc->stat;
695 static int sr_registered = 0;
697 static int sr_init()
699 int i;
701 if (sr_template.dev_noticed == 0)
702 return 0;
704 if (!sr_registered) {
705 if (devfs_register_blkdev(MAJOR_NR, "sr", &cdrom_fops)) {
706 printk("Unable to get major %d for SCSI-CD\n", MAJOR_NR);
707 return 1;
709 sr_registered++;
711 if (scsi_CDs)
712 return 0;
713 sr_template.dev_max =
714 sr_template.dev_noticed + SR_EXTRA_DEVS;
715 scsi_CDs = (Scsi_CD *) kmalloc(sr_template.dev_max * sizeof(Scsi_CD), GFP_ATOMIC);
716 memset(scsi_CDs, 0, sr_template.dev_max * sizeof(Scsi_CD));
718 sr_sizes = (int *) kmalloc(sr_template.dev_max * sizeof(int), GFP_ATOMIC);
719 memset(sr_sizes, 0, sr_template.dev_max * sizeof(int));
721 sr_blocksizes = (int *) kmalloc(sr_template.dev_max *
722 sizeof(int), GFP_ATOMIC);
724 sr_hardsizes = (int *) kmalloc(sr_template.dev_max *
725 sizeof(int), GFP_ATOMIC);
727 * These are good guesses for the time being.
729 for (i = 0; i < sr_template.dev_max; i++)
731 sr_blocksizes[i] = 2048;
732 sr_hardsizes[i] = 2048;
734 blksize_size[MAJOR_NR] = sr_blocksizes;
735 hardsect_size[MAJOR_NR] = sr_hardsizes;
736 return 0;
739 void sr_finish()
741 int i;
742 char name[6];
744 blk_dev[MAJOR_NR].queue = sr_find_queue;
745 blk_size[MAJOR_NR] = sr_sizes;
747 for (i = 0; i < sr_template.nr_dev; ++i) {
748 /* If we have already seen this, then skip it. Comes up
749 * with loadable modules. */
750 if (scsi_CDs[i].capacity)
751 continue;
752 scsi_CDs[i].capacity = 0x1fffff;
753 scsi_CDs[i].device->sector_size = 2048; /* A guess, just in case */
754 scsi_CDs[i].needs_sector_size = 1;
755 scsi_CDs[i].device->changed = 1; /* force recheck CD type */
756 #if 0
757 /* seems better to leave this for later */
758 get_sectorsize(i);
759 printk("Scd sectorsize = %d bytes.\n", scsi_CDs[i].sector_size);
760 #endif
761 scsi_CDs[i].use = 1;
763 scsi_CDs[i].device->ten = 1;
764 scsi_CDs[i].device->remap = 1;
765 scsi_CDs[i].readcd_known = 0;
766 scsi_CDs[i].readcd_cdda = 0;
767 sr_sizes[i] = scsi_CDs[i].capacity >> (BLOCK_SIZE_BITS - 9);
769 scsi_CDs[i].cdi.ops = &sr_dops;
770 scsi_CDs[i].cdi.handle = &scsi_CDs[i];
771 scsi_CDs[i].cdi.dev = MKDEV(MAJOR_NR, i);
772 scsi_CDs[i].cdi.mask = 0;
773 scsi_CDs[i].cdi.capacity = 1;
774 get_capabilities(i);
775 sr_vendor_init(i);
777 sprintf(name, "sr%d", i);
778 strcpy(scsi_CDs[i].cdi.name, name);
779 scsi_CDs[i].cdi.de =
780 devfs_register (scsi_CDs[i].device->de, "cd",
781 DEVFS_FL_DEFAULT, MAJOR_NR, i,
782 S_IFBLK | S_IRUGO | S_IWUGO,
783 &cdrom_fops, NULL);
784 register_cdrom(&scsi_CDs[i].cdi);
788 /* If our host adapter is capable of scatter-gather, then we increase
789 * the read-ahead to 16 blocks (32 sectors). If not, we use
790 * a two block (4 sector) read ahead. */
791 if (scsi_CDs[0].device && scsi_CDs[0].device->host->sg_tablesize)
792 read_ahead[MAJOR_NR] = 32; /* 32 sector read-ahead. Always removable. */
793 else
794 read_ahead[MAJOR_NR] = 4; /* 4 sector read-ahead */
796 return;
799 static void sr_detach(Scsi_Device * SDp)
801 Scsi_CD *cpnt;
802 int i;
804 for (cpnt = scsi_CDs, i = 0; i < sr_template.dev_max; i++, cpnt++)
805 if (cpnt->device == SDp) {
806 kdev_t devi = MKDEV(MAJOR_NR, i);
807 struct super_block *sb = get_super(devi);
810 * Since the cdrom is read-only, no need to sync the device.
811 * We should be kind to our buffer cache, however.
813 if (sb)
814 invalidate_inodes(sb);
815 invalidate_buffers(devi);
818 * Reset things back to a sane state so that one can re-load a new
819 * driver (perhaps the same one).
821 unregister_cdrom(&(cpnt->cdi));
822 cpnt->device = NULL;
823 cpnt->capacity = 0;
824 SDp->attached--;
825 sr_template.nr_dev--;
826 sr_template.dev_noticed--;
827 sr_sizes[i] = 0;
828 return;
830 return;
834 #ifdef MODULE
836 int init_module(void)
838 sr_template.module = &__this_module;
839 return scsi_register_module(MODULE_SCSI_DEV, &sr_template);
842 void cleanup_module(void)
844 scsi_unregister_module(MODULE_SCSI_DEV, &sr_template);
845 devfs_unregister_blkdev(MAJOR_NR, "sr");
846 sr_registered--;
847 if (scsi_CDs != NULL) {
848 kfree((char *) scsi_CDs);
850 kfree((char *) sr_sizes);
851 sr_sizes = NULL;
853 kfree((char *) sr_blocksizes);
854 sr_blocksizes = NULL;
855 kfree((char *) sr_hardsizes);
856 sr_hardsizes = NULL;
858 blksize_size[MAJOR_NR] = NULL;
859 hardsect_size[MAJOR_NR] = NULL;
860 blk_size[MAJOR_NR] = NULL;
861 read_ahead[MAJOR_NR] = 0;
863 sr_template.dev_max = 0;
866 #endif /* MODULE */
869 * Overrides for Emacs so that we follow Linus's tabbing style.
870 * Emacs will notice this stuff at the end of the file and automatically
871 * adjust the settings for this buffer only. This must remain at the end
872 * of the file.
873 * ---------------------------------------------------------------------------
874 * Local variables:
875 * c-indent-level: 4
876 * c-brace-imaginary-offset: 0
877 * c-brace-offset: -4
878 * c-argdecl-indent: 4
879 * c-label-offset: -4
880 * c-continued-statement-offset: 4
881 * c-continued-brace-offset: 0
882 * indent-tabs-mode: nil
883 * tab-width: 8
884 * End: