- Kai Germaschewski: ISDN update (including Makefiles)
[davej-history.git] / drivers / scsi / sr.c
blob71ec104f40e97e7c940e62a58e6a9d303e49a42c
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@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>
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 MODULE_PARM(xa_test, "i"); /* see sr_ioctl.c */
61 #define MAX_RETRIES 3
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 =
74 name:"cdrom",
75 tag:"sr",
76 scsi_type:TYPE_ROM,
77 major:SCSI_CDROM_MAJOR,
78 blk:1,
79 detect:sr_detect,
80 init:sr_init,
81 finish:sr_finish,
82 attach:sr_attach,
83 detach:sr_detach,
84 init_command:sr_init_command
87 Scsi_CD *scsi_CDs;
88 static int *sr_sizes;
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 sync_dev(cdi->dev);
105 scsi_CDs[MINOR(cdi->dev)].device->access_count--;
106 if (scsi_CDs[MINOR(cdi->dev)].device->host->hostt->module)
107 __MOD_DEC_USE_COUNT(scsi_CDs[MINOR(cdi->dev)].device->host->hostt->module);
108 if (sr_template.module)
109 __MOD_DEC_USE_COUNT(sr_template.module);
112 static struct cdrom_device_ops sr_dops =
114 open: sr_open,
115 release: sr_release,
116 drive_status: sr_drive_status,
117 media_changed: sr_media_change,
118 tray_move: sr_tray_move,
119 lock_door: sr_lock_door,
120 select_speed: sr_select_speed,
121 get_last_session: sr_get_last_session,
122 get_mcn: sr_get_mcn,
123 reset: sr_reset,
124 audio_ioctl: sr_audio_ioctl,
125 dev_ioctl: sr_dev_ioctl,
126 capability: CDC_CLOSE_TRAY | CDC_OPEN_TRAY | CDC_LOCK |
127 CDC_SELECT_SPEED | CDC_SELECT_DISC |
128 CDC_MULTI_SESSION | CDC_MCN |
129 CDC_MEDIA_CHANGED | CDC_PLAY_AUDIO |
130 CDC_RESET | CDC_IOCTLS | CDC_DRIVE_STATUS |
131 CDC_CD_R | CDC_CD_RW | CDC_DVD | CDC_DVD_R |
132 CDC_DVD_RAM | CDC_GENERIC_PACKET,
133 generic_packet: sr_packet,
137 * This function checks to see if the media has been changed in the
138 * CDROM drive. It is possible that we have already sensed a change,
139 * or the drive may have sensed one and not yet reported it. We must
140 * be ready for either case. This function always reports the current
141 * value of the changed bit. If flag is 0, then the changed bit is reset.
142 * This function could be done as an ioctl, but we would need to have
143 * an inode for that to work, and we do not always have one.
146 int sr_media_change(struct cdrom_device_info *cdi, int slot)
148 int retval;
150 if (CDSL_CURRENT != slot) {
151 /* no changer support */
152 return -EINVAL;
154 retval = scsi_ioctl(scsi_CDs[MINOR(cdi->dev)].device,
155 SCSI_IOCTL_TEST_UNIT_READY, 0);
157 if (retval) {
158 /* Unable to test, unit probably not ready. This usually
159 * means there is no disc in the drive. Mark as changed,
160 * and we will figure it out later once the drive is
161 * available again. */
163 scsi_CDs[MINOR(cdi->dev)].device->changed = 1;
164 return 1; /* This will force a flush, if called from
165 * check_disk_change */
168 retval = scsi_CDs[MINOR(cdi->dev)].device->changed;
169 scsi_CDs[MINOR(cdi->dev)].device->changed = 0;
170 /* If the disk changed, the capacity will now be different,
171 * so we force a re-read of this information */
172 if (retval) {
173 /* check multisession offset etc */
174 sr_cd_check(cdi);
177 * If the disk changed, the capacity will now be different,
178 * so we force a re-read of this information
179 * Force 2048 for the sector size so that filesystems won't
180 * be trying to use something that is too small if the disc
181 * has changed.
183 scsi_CDs[MINOR(cdi->dev)].needs_sector_size = 1;
185 scsi_CDs[MINOR(cdi->dev)].device->sector_size = 2048;
187 return retval;
191 * rw_intr is the interrupt routine for the device driver. It will be notified on the
192 * end of a SCSI read / write, and will take on of several actions based on success or failure.
195 static void rw_intr(Scsi_Cmnd * SCpnt)
197 int result = SCpnt->result;
198 int this_count = SCpnt->bufflen >> 9;
199 int good_sectors = (result == 0 ? this_count : 0);
200 int block_sectors = 0;
201 int device_nr = DEVICE_NR(SCpnt->request.rq_dev);
203 #ifdef DEBUG
204 printk("sr.c done: %x %p\n", result, SCpnt->request.bh->b_data);
205 #endif
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[2] == MEDIUM_ERROR ||
216 SCpnt->sense_buffer[2] == VOLUME_OVERFLOW ||
217 SCpnt->sense_buffer[2] == ILLEGAL_REQUEST)) {
218 long error_sector = (SCpnt->sense_buffer[3] << 24) |
219 (SCpnt->sense_buffer[4] << 16) |
220 (SCpnt->sense_buffer[5] << 8) |
221 SCpnt->sense_buffer[6];
222 if (SCpnt->request.bh != NULL)
223 block_sectors = SCpnt->request.bh->b_size >> 9;
224 if (block_sectors < 4)
225 block_sectors = 4;
226 if (scsi_CDs[device_nr].device->sector_size == 2048)
227 error_sector <<= 2;
228 error_sector &= ~(block_sectors - 1);
229 good_sectors = error_sector - SCpnt->request.sector;
230 if (good_sectors < 0 || good_sectors >= this_count)
231 good_sectors = 0;
233 * The SCSI specification allows for the value returned by READ
234 * CAPACITY to be up to 75 2K sectors past the last readable
235 * block. Therefore, if we hit a medium error within the last
236 * 75 2K sectors, we decrease the saved size value.
238 if ((error_sector >> 1) < sr_sizes[device_nr] &&
239 scsi_CDs[device_nr].capacity - error_sector < 4 * 75)
240 sr_sizes[device_nr] = error_sector >> 1;
244 * This calls the generic completion function, now that we know
245 * how many actual sectors finished, and how many sectors we need
246 * to say have failed.
248 scsi_io_completion(SCpnt, good_sectors, block_sectors);
252 static request_queue_t *sr_find_queue(kdev_t dev)
255 * No such device
257 if (MINOR(dev) >= sr_template.dev_max || !scsi_CDs[MINOR(dev)].device)
258 return NULL;
260 return &scsi_CDs[MINOR(dev)].device->request_queue;
263 static int sr_scatter_pad(Scsi_Cmnd *SCpnt, int s_size)
265 struct scatterlist *sg, *old_sg = NULL;
266 int i, fsize, bsize, sg_ent;
267 char *front, *back;
269 back = front = NULL;
270 sg_ent = SCpnt->use_sg;
271 bsize = 0; /* gcc... */
274 * need front pad
276 if ((fsize = SCpnt->request.sector % (s_size >> 9))) {
277 fsize <<= 9;
278 sg_ent++;
279 if ((front = scsi_malloc(fsize)) == NULL)
280 goto no_mem;
283 * need a back pad too
285 if ((bsize = s_size - ((SCpnt->request_bufflen + fsize) % s_size))) {
286 sg_ent++;
287 if ((back = scsi_malloc(bsize)) == NULL)
288 goto no_mem;
292 * extend or allocate new scatter-gather table
294 if (SCpnt->use_sg)
295 old_sg = (struct scatterlist *) SCpnt->request_buffer;
296 else {
297 SCpnt->use_sg = 1;
298 sg_ent++;
301 SCpnt->sglist_len = ((sg_ent * sizeof(struct scatterlist)) + 511) & ~511;
302 if ((sg = scsi_malloc(SCpnt->sglist_len)) == NULL)
303 goto no_mem;
305 memset(sg, 0, SCpnt->sglist_len);
307 i = 0;
308 if (fsize) {
309 sg[0].address = sg[0].alt_address = front;
310 sg[0].length = fsize;
311 i++;
313 if (old_sg) {
314 memcpy(sg + i, old_sg, SCpnt->use_sg * sizeof(struct scatterlist));
315 scsi_free(old_sg, ((SCpnt->use_sg * sizeof(struct scatterlist)) + 511) & ~511);
316 } else {
317 sg[i].address = SCpnt->request_buffer;
318 sg[i].length = SCpnt->request_bufflen;
321 SCpnt->request_bufflen += (fsize + bsize);
322 SCpnt->request_buffer = sg;
323 SCpnt->use_sg += i;
325 if (bsize) {
326 sg[SCpnt->use_sg].address = back;
327 sg[SCpnt->use_sg].alt_address = back;
328 sg[SCpnt->use_sg].length = bsize;
329 SCpnt->use_sg++;
332 return 0;
334 no_mem:
335 printk("sr: ran out of mem for scatter pad\n");
336 if (front)
337 scsi_free(front, fsize);
338 if (back)
339 scsi_free(back, bsize);
341 return 1;
345 static int sr_init_command(Scsi_Cmnd * SCpnt)
347 int dev, devm, block, this_count, s_size;
349 devm = MINOR(SCpnt->request.rq_dev);
350 dev = DEVICE_NR(SCpnt->request.rq_dev);
352 SCSI_LOG_HLQUEUE(1, printk("Doing sr request, dev = %d, block = %d\n", devm, block));
354 if (dev >= sr_template.nr_dev ||
355 !scsi_CDs[dev].device ||
356 !scsi_CDs[dev].device->online) {
357 SCSI_LOG_HLQUEUE(2, printk("Finishing %ld sectors\n", SCpnt->request.nr_sectors));
358 SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt));
359 return 0;
361 if (scsi_CDs[dev].device->changed) {
363 * quietly refuse to do anything to a changed disc until the
364 * changed bit has been reset
366 return 0;
369 if ((SCpnt->request.cmd == WRITE) && !scsi_CDs[dev].device->writeable)
370 return 0;
373 * we do lazy blocksize switching (when reading XA sectors,
374 * see CDROMREADMODE2 ioctl)
376 s_size = scsi_CDs[dev].device->sector_size;
377 if (s_size > 2048) {
378 if (!in_interrupt())
379 sr_set_blocklength(DEVICE_NR(CURRENT->rq_dev), 2048);
380 else
381 printk("sr: can't switch blocksize: in interrupt\n");
384 if (s_size != 512 && s_size != 1024 && s_size != 2048) {
385 printk("sr: bad sector size %d\n", s_size);
386 return 0;
389 block = SCpnt->request.sector / (s_size >> 9);
392 * request doesn't start on hw block boundary, add scatter pads
394 if ((SCpnt->request.sector % (s_size >> 9)) || (SCpnt->request_bufflen % s_size))
395 if (sr_scatter_pad(SCpnt, s_size))
396 return 0;
398 this_count = (SCpnt->request_bufflen >> 9) / (s_size >> 9);
400 switch (SCpnt->request.cmd) {
401 case WRITE:
402 SCpnt->cmnd[0] = WRITE_10;
403 SCpnt->sc_data_direction = SCSI_DATA_WRITE;
404 break;
405 case READ:
406 SCpnt->cmnd[0] = READ_10;
407 SCpnt->sc_data_direction = SCSI_DATA_READ;
408 break;
409 default:
410 printk("Unknown sr command %d\n", SCpnt->request.cmd);
411 return 0;
414 SCSI_LOG_HLQUEUE(2, printk("sr%d : %s %d/%ld 512 byte blocks.\n",
415 devm,
416 (SCpnt->request.cmd == WRITE) ? "writing" : "reading",
417 this_count, SCpnt->request.nr_sectors));
419 SCpnt->cmnd[1] = (SCpnt->lun << 5) & 0xe0;
421 if (this_count > 0xffff)
422 this_count = 0xffff;
424 SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
425 SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
426 SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
427 SCpnt->cmnd[5] = (unsigned char) block & 0xff;
428 SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
429 SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
430 SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
433 * We shouldn't disconnect in the middle of a sector, so with a dumb
434 * host adapter, it's safe to assume that we can at least transfer
435 * this many bytes between each connect / disconnect.
437 SCpnt->transfersize = scsi_CDs[dev].device->sector_size;
438 SCpnt->underflow = this_count << 9;
440 SCpnt->allowed = MAX_RETRIES;
441 SCpnt->timeout_per_command = SR_TIMEOUT;
444 * This is the completion routine we use. This is matched in terms
445 * of capability to this function.
447 SCpnt->done = rw_intr;
450 struct scatterlist *sg = SCpnt->request_buffer;
451 int i, size = 0;
452 for (i = 0; i < SCpnt->use_sg; i++)
453 size += sg[i].length;
455 if (size != SCpnt->request_bufflen && SCpnt->use_sg) {
456 printk("sr: mismatch count %d, bytes %d\n", size, SCpnt->request_bufflen);
457 SCpnt->request_bufflen = size;
462 * This indicates that the command is ready from our end to be
463 * queued.
465 return 1;
468 static int sr_open(struct cdrom_device_info *cdi, int purpose)
470 check_disk_change(cdi->dev);
472 if (MINOR(cdi->dev) >= sr_template.dev_max
473 || !scsi_CDs[MINOR(cdi->dev)].device) {
474 return -ENXIO; /* No such device */
477 * If the device is in error recovery, wait until it is done.
478 * If the device is offline, then disallow any access to it.
480 if (!scsi_block_when_processing_errors(scsi_CDs[MINOR(cdi->dev)].device)) {
481 return -ENXIO;
483 scsi_CDs[MINOR(cdi->dev)].device->access_count++;
484 if (scsi_CDs[MINOR(cdi->dev)].device->host->hostt->module)
485 __MOD_INC_USE_COUNT(scsi_CDs[MINOR(cdi->dev)].device->host->hostt->module);
486 if (sr_template.module)
487 __MOD_INC_USE_COUNT(sr_template.module);
489 /* If this device did not have media in the drive at boot time, then
490 * we would have been unable to get the sector size. Check to see if
491 * this is the case, and try again.
494 if (scsi_CDs[MINOR(cdi->dev)].needs_sector_size)
495 get_sectorsize(MINOR(cdi->dev));
497 return 0;
501 * do_sr_request() is the request handler function for the sr driver.
502 * Its function in life is to take block device requests, and
503 * translate them to SCSI commands.
507 static int sr_detect(Scsi_Device * SDp)
510 if (SDp->type != TYPE_ROM && SDp->type != TYPE_WORM)
511 return 0;
513 printk("Detected scsi CD-ROM sr%d at scsi%d, channel %d, id %d, lun %d\n",
514 sr_template.dev_noticed++,
515 SDp->host->host_no, SDp->channel, SDp->id, SDp->lun);
517 return 1;
520 static int sr_attach(Scsi_Device * SDp)
522 Scsi_CD *cpnt;
523 int i;
525 if (SDp->type != TYPE_ROM && SDp->type != TYPE_WORM)
526 return 1;
528 if (sr_template.nr_dev >= sr_template.dev_max) {
529 SDp->attached--;
530 return 1;
532 for (cpnt = scsi_CDs, i = 0; i < sr_template.dev_max; i++, cpnt++)
533 if (!cpnt->device)
534 break;
536 if (i >= sr_template.dev_max)
537 panic("scsi_devices corrupt (sr)");
540 scsi_CDs[i].device = SDp;
542 sr_template.nr_dev++;
543 if (sr_template.nr_dev > sr_template.dev_max)
544 panic("scsi_devices corrupt (sr)");
545 return 0;
549 void get_sectorsize(int i)
551 unsigned char cmd[10];
552 unsigned char *buffer;
553 int the_result, retries;
554 int sector_size;
555 Scsi_Request *SRpnt;
557 buffer = (unsigned char *) scsi_malloc(512);
558 SRpnt = scsi_allocate_request(scsi_CDs[i].device);
560 if(buffer == NULL || SRpnt == NULL)
562 scsi_CDs[i].capacity = 0x1fffff;
563 sector_size = 2048; /* A guess, just in case */
564 scsi_CDs[i].needs_sector_size = 1;
565 if(buffer)
566 scsi_free(buffer, 512);
567 if(SRpnt)
568 scsi_release_request(SRpnt);
569 return;
572 retries = 3;
573 do {
574 cmd[0] = READ_CAPACITY;
575 cmd[1] = (scsi_CDs[i].device->lun << 5) & 0xe0;
576 memset((void *) &cmd[2], 0, 8);
577 SRpnt->sr_request.rq_status = RQ_SCSI_BUSY; /* Mark as really busy */
578 SRpnt->sr_cmd_len = 0;
580 memset(buffer, 0, 8);
582 /* Do the command and wait.. */
584 SRpnt->sr_data_direction = SCSI_DATA_READ;
585 scsi_wait_req(SRpnt, (void *) cmd, (void *) buffer,
586 8, SR_TIMEOUT, MAX_RETRIES);
588 the_result = SRpnt->sr_result;
589 retries--;
591 } while (the_result && retries);
594 scsi_release_request(SRpnt);
595 SRpnt = NULL;
597 if (the_result) {
598 scsi_CDs[i].capacity = 0x1fffff;
599 sector_size = 2048; /* A guess, just in case */
600 scsi_CDs[i].needs_sector_size = 1;
601 } else {
602 #if 0
603 if (cdrom_get_last_written(MKDEV(MAJOR_NR, i),
604 (long *) &scsi_CDs[i].capacity))
605 #endif
606 scsi_CDs[i].capacity = 1 + ((buffer[0] << 24) |
607 (buffer[1] << 16) |
608 (buffer[2] << 8) |
609 buffer[3]);
610 sector_size = (buffer[4] << 24) |
611 (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
612 switch (sector_size) {
614 * HP 4020i CD-Recorder reports 2340 byte sectors
615 * Philips CD-Writers report 2352 byte sectors
617 * Use 2k sectors for them..
619 case 0:
620 case 2340:
621 case 2352:
622 sector_size = 2048;
623 /* fall through */
624 case 2048:
625 scsi_CDs[i].capacity *= 4;
626 /* fall through */
627 case 512:
628 break;
629 default:
630 printk("sr%d: unsupported sector size %d.\n",
631 i, sector_size);
632 scsi_CDs[i].capacity = 0;
633 scsi_CDs[i].needs_sector_size = 1;
636 scsi_CDs[i].device->sector_size = sector_size;
639 * Add this so that we have the ability to correctly gauge
640 * what the device is capable of.
642 scsi_CDs[i].needs_sector_size = 0;
643 sr_sizes[i] = scsi_CDs[i].capacity >> (BLOCK_SIZE_BITS - 9);
645 scsi_free(buffer, 512);
648 void get_capabilities(int i)
650 unsigned char cmd[6];
651 unsigned char *buffer;
652 int rc, n;
654 static char *loadmech[] =
656 "caddy",
657 "tray",
658 "pop-up",
660 "changer",
661 "cartridge changer",
666 buffer = (unsigned char *) scsi_malloc(512);
667 cmd[0] = MODE_SENSE;
668 cmd[1] = (scsi_CDs[i].device->lun << 5) & 0xe0;
669 cmd[2] = 0x2a;
670 cmd[4] = 128;
671 cmd[3] = cmd[5] = 0;
672 rc = sr_do_ioctl(i, cmd, buffer, 128, 1, SCSI_DATA_READ, NULL);
674 if (-EINVAL == rc) {
675 /* failed, drive has'nt this mode page */
676 scsi_CDs[i].cdi.speed = 1;
677 /* disable speed select, drive probably can't do this either */
678 scsi_CDs[i].cdi.mask |= CDC_SELECT_SPEED;
679 scsi_free(buffer, 512);
680 return;
682 n = buffer[3] + 4;
683 scsi_CDs[i].cdi.speed = ((buffer[n + 8] << 8) + buffer[n + 9]) / 176;
684 scsi_CDs[i].readcd_known = 1;
685 scsi_CDs[i].readcd_cdda = buffer[n + 5] & 0x01;
686 /* print some capability bits */
687 printk("sr%i: scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n", i,
688 ((buffer[n + 14] << 8) + buffer[n + 15]) / 176,
689 scsi_CDs[i].cdi.speed,
690 buffer[n + 3] & 0x01 ? "writer " : "", /* CD Writer */
691 buffer[n + 3] & 0x20 ? "dvd-ram " : "",
692 buffer[n + 2] & 0x02 ? "cd/rw " : "", /* can read rewriteable */
693 buffer[n + 4] & 0x20 ? "xa/form2 " : "", /* can read xa/from2 */
694 buffer[n + 5] & 0x01 ? "cdda " : "", /* can read audio data */
695 loadmech[buffer[n + 6] >> 5]);
696 if ((buffer[n + 6] >> 5) == 0)
697 /* caddy drives can't close tray... */
698 scsi_CDs[i].cdi.mask |= CDC_CLOSE_TRAY;
699 if ((buffer[n + 2] & 0x8) == 0)
700 /* not a DVD drive */
701 scsi_CDs[i].cdi.mask |= CDC_DVD;
702 if ((buffer[n + 3] & 0x20) == 0) {
703 /* can't write DVD-RAM media */
704 scsi_CDs[i].cdi.mask |= CDC_DVD_RAM;
705 } else {
706 scsi_CDs[i].device->writeable = 1;
708 if ((buffer[n + 3] & 0x10) == 0)
709 /* can't write DVD-R media */
710 scsi_CDs[i].cdi.mask |= CDC_DVD_R;
711 if ((buffer[n + 3] & 0x2) == 0)
712 /* can't write CD-RW media */
713 scsi_CDs[i].cdi.mask |= CDC_CD_RW;
714 if ((buffer[n + 3] & 0x1) == 0)
715 /* can't write CD-R media */
716 scsi_CDs[i].cdi.mask |= CDC_CD_R;
717 if ((buffer[n + 6] & 0x8) == 0)
718 /* can't eject */
719 scsi_CDs[i].cdi.mask |= CDC_OPEN_TRAY;
721 if ((buffer[n + 6] >> 5) == mechtype_individual_changer ||
722 (buffer[n + 6] >> 5) == mechtype_cartridge_changer)
723 scsi_CDs[i].cdi.capacity =
724 cdrom_number_of_slots(&(scsi_CDs[i].cdi));
725 if (scsi_CDs[i].cdi.capacity <= 1)
726 /* not a changer */
727 scsi_CDs[i].cdi.mask |= CDC_SELECT_DISC;
728 /*else I don't think it can close its tray
729 scsi_CDs[i].cdi.mask |= CDC_CLOSE_TRAY; */
731 scsi_free(buffer, 512);
735 * sr_packet() is the entry point for the generic commands generated
736 * by the Uniform CD-ROM layer.
738 static int sr_packet(struct cdrom_device_info *cdi, struct cdrom_generic_command *cgc)
740 Scsi_Device *device = scsi_CDs[MINOR(cdi->dev)].device;
742 /* set the LUN */
743 cgc->cmd[1] |= device->lun << 5;
745 cgc->stat = sr_do_ioctl(MINOR(cdi->dev), cgc->cmd, cgc->buffer, cgc->buflen, cgc->quiet, cgc->data_direction, cgc->sense);
747 return cgc->stat;
750 static int sr_registered;
752 static int sr_init()
754 int i;
756 if (sr_template.dev_noticed == 0)
757 return 0;
759 if (!sr_registered) {
760 if (devfs_register_blkdev(MAJOR_NR, "sr", &cdrom_fops)) {
761 printk("Unable to get major %d for SCSI-CD\n", MAJOR_NR);
762 return 1;
764 sr_registered++;
766 if (scsi_CDs)
767 return 0;
769 sr_template.dev_max = sr_template.dev_noticed + SR_EXTRA_DEVS;
770 scsi_CDs = kmalloc(sr_template.dev_max * sizeof(Scsi_CD), GFP_ATOMIC);
771 if (!scsi_CDs)
772 goto cleanup_devfs;
773 memset(scsi_CDs, 0, sr_template.dev_max * sizeof(Scsi_CD));
775 sr_sizes = kmalloc(sr_template.dev_max * sizeof(int), GFP_ATOMIC);
776 if (!sr_sizes)
777 goto cleanup_cds;
778 memset(sr_sizes, 0, sr_template.dev_max * sizeof(int));
780 sr_blocksizes = kmalloc(sr_template.dev_max * sizeof(int), GFP_ATOMIC);
781 if (!sr_blocksizes)
782 goto cleanup_sizes;
784 sr_hardsizes = kmalloc(sr_template.dev_max * sizeof(int), GFP_ATOMIC);
785 if (!sr_hardsizes)
786 goto cleanup_blocksizes;
788 * These are good guesses for the time being.
790 for (i = 0; i < sr_template.dev_max; i++) {
791 sr_blocksizes[i] = 2048;
792 sr_hardsizes[i] = 2048;
794 blksize_size[MAJOR_NR] = sr_blocksizes;
795 hardsect_size[MAJOR_NR] = sr_hardsizes;
796 return 0;
797 cleanup_blocksizes:
798 kfree(sr_blocksizes);
799 cleanup_sizes:
800 kfree(sr_sizes);
801 cleanup_cds:
802 kfree(scsi_CDs);
803 cleanup_devfs:
804 devfs_unregister_blkdev(MAJOR_NR, "sr");
805 sr_registered--;
806 return 1;
809 void sr_finish()
811 int i;
812 char name[6];
814 blk_dev[MAJOR_NR].queue = sr_find_queue;
815 blk_size[MAJOR_NR] = sr_sizes;
817 for (i = 0; i < sr_template.nr_dev; ++i) {
818 /* If we have already seen this, then skip it. Comes up
819 * with loadable modules. */
820 if (scsi_CDs[i].capacity)
821 continue;
822 scsi_CDs[i].capacity = 0x1fffff;
823 scsi_CDs[i].device->sector_size = 2048; /* A guess, just in case */
824 scsi_CDs[i].needs_sector_size = 1;
825 scsi_CDs[i].device->changed = 1; /* force recheck CD type */
826 #if 0
827 /* seems better to leave this for later */
828 get_sectorsize(i);
829 printk("Scd sectorsize = %d bytes.\n", scsi_CDs[i].sector_size);
830 #endif
831 scsi_CDs[i].use = 1;
833 scsi_CDs[i].device->ten = 1;
834 scsi_CDs[i].device->remap = 1;
835 scsi_CDs[i].readcd_known = 0;
836 scsi_CDs[i].readcd_cdda = 0;
837 sr_sizes[i] = scsi_CDs[i].capacity >> (BLOCK_SIZE_BITS - 9);
839 scsi_CDs[i].cdi.ops = &sr_dops;
840 scsi_CDs[i].cdi.handle = &scsi_CDs[i];
841 scsi_CDs[i].cdi.dev = MKDEV(MAJOR_NR, i);
842 scsi_CDs[i].cdi.mask = 0;
843 scsi_CDs[i].cdi.capacity = 1;
844 get_capabilities(i);
845 sr_vendor_init(i);
847 sprintf(name, "sr%d", i);
848 strcpy(scsi_CDs[i].cdi.name, name);
849 scsi_CDs[i].cdi.de =
850 devfs_register (scsi_CDs[i].device->de, "cd",
851 DEVFS_FL_DEFAULT, MAJOR_NR, i,
852 S_IFBLK | S_IRUGO | S_IWUGO,
853 &cdrom_fops, NULL);
854 register_cdrom(&scsi_CDs[i].cdi);
858 /* If our host adapter is capable of scatter-gather, then we increase
859 * the read-ahead to 16 blocks (32 sectors). If not, we use
860 * a two block (4 sector) read ahead. */
861 if (scsi_CDs[0].device && scsi_CDs[0].device->host->sg_tablesize)
862 read_ahead[MAJOR_NR] = 32; /* 32 sector read-ahead. Always removable. */
863 else
864 read_ahead[MAJOR_NR] = 4; /* 4 sector read-ahead */
866 return;
869 static void sr_detach(Scsi_Device * SDp)
871 Scsi_CD *cpnt;
872 int i;
874 for (cpnt = scsi_CDs, i = 0; i < sr_template.dev_max; i++, cpnt++)
875 if (cpnt->device == SDp) {
876 kdev_t devi = MKDEV(MAJOR_NR, i);
877 struct super_block *sb = get_super(devi);
880 * Since the cdrom is read-only, no need to sync the device.
881 * We should be kind to our buffer cache, however.
883 if (sb)
884 invalidate_inodes(sb);
885 invalidate_buffers(devi);
888 * Reset things back to a sane state so that one can re-load a new
889 * driver (perhaps the same one).
891 unregister_cdrom(&(cpnt->cdi));
892 cpnt->device = NULL;
893 cpnt->capacity = 0;
894 SDp->attached--;
895 sr_template.nr_dev--;
896 sr_template.dev_noticed--;
897 sr_sizes[i] = 0;
898 return;
900 return;
903 static int __init init_sr(void)
905 sr_template.module = THIS_MODULE;
906 return scsi_register_module(MODULE_SCSI_DEV, &sr_template);
909 static void __exit exit_sr(void)
911 scsi_unregister_module(MODULE_SCSI_DEV, &sr_template);
912 devfs_unregister_blkdev(MAJOR_NR, "sr");
913 sr_registered--;
914 if (scsi_CDs != NULL) {
915 kfree(scsi_CDs);
917 kfree(sr_sizes);
918 sr_sizes = NULL;
920 kfree(sr_blocksizes);
921 sr_blocksizes = NULL;
922 kfree(sr_hardsizes);
923 sr_hardsizes = NULL;
925 blksize_size[MAJOR_NR] = NULL;
926 hardsect_size[MAJOR_NR] = NULL;
927 blk_size[MAJOR_NR] = NULL;
928 read_ahead[MAJOR_NR] = 0;
930 sr_template.dev_max = 0;
933 module_init(init_sr);
934 module_exit(exit_sr);