File: /usr/src/linux/drivers/scsi/sr.c
1 /*
2 * sr.c Copyright (C) 1992 David Giller
3 * Copyright (C) 1993, 1994, 1995, 1999 Eric Youngdale
4 *
5 * adapted from:
6 * sd.c Copyright (C) 1992 Drew Eckhardt
7 * Linux scsi disk driver by
8 * Drew Eckhardt <drew@colorado.edu>
9 *
10 * Modified by Eric Youngdale ericy@andante.org to
11 * add scatter-gather, multiple outstanding request, and other
12 * enhancements.
13 *
14 * Modified by Eric Youngdale eric@andante.org to support loadable
15 * low-level scsi drivers.
16 *
17 * Modified by Thomas Quinot thomas@melchior.cuivre.fdn.fr to
18 * provide auto-eject.
19 *
20 * Modified by Gerd Knorr <kraxel@cs.tu-berlin.de> to support the
21 * generic cdrom interface
22 *
23 * Modified by Jens Axboe <axboe@suse.de> - Uniform sr_packet()
24 * interface, capabilities probe additions, ioctl cleanups, etc.
25 *
26 * Modified by Richard Gooch <rgooch@atnf.csiro.au> to support devfs
27 *
28 * Modified by Jens Axboe <axboe@suse.de> - support DVD-RAM
29 * transparently and loose the GHOST hack
30 *
31 * Modified by Arnaldo Carvalho de Melo <acme@conectiva.com.br>
32 * check resource allocation in sr_init and some cleanups
33 *
34 */
35
36 #include <linux/module.h>
37
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>
50
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"
58
59 MODULE_PARM(xa_test, "i"); /* see sr_ioctl.c */
60
61 #define MAX_RETRIES 3
62 #define SR_TIMEOUT (30 * HZ)
63
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 *);
69
70 static int sr_init_command(Scsi_Cmnd *);
71
72 static struct Scsi_Device_Template sr_template =
73 {
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
85 };
86
87 Scsi_CD *scsi_CDs;
88 static int *sr_sizes;
89
90 static int *sr_blocksizes;
91 static int *sr_hardsizes;
92
93 static int sr_open(struct cdrom_device_info *, int);
94 void get_sectorsize(int);
95 void get_capabilities(int);
96
97 static int sr_media_change(struct cdrom_device_info *, int);
98 static int sr_packet(struct cdrom_device_info *, struct cdrom_generic_command *);
99
100 static void sr_release(struct cdrom_device_info *cdi)
101 {
102 if (scsi_CDs[MINOR(cdi->dev)].device->sector_size > 2048)
103 sr_set_blocklength(MINOR(cdi->dev), 2048);
104 scsi_CDs[MINOR(cdi->dev)].device->access_count--;
105 if (scsi_CDs[MINOR(cdi->dev)].device->host->hostt->module)
106 __MOD_DEC_USE_COUNT(scsi_CDs[MINOR(cdi->dev)].device->host->hostt->module);
107 if (sr_template.module)
108 __MOD_DEC_USE_COUNT(sr_template.module);
109 }
110
111 static struct cdrom_device_ops sr_dops =
112 {
113 open: sr_open,
114 release: sr_release,
115 drive_status: sr_drive_status,
116 media_changed: sr_media_change,
117 tray_move: sr_tray_move,
118 lock_door: sr_lock_door,
119 select_speed: sr_select_speed,
120 get_last_session: sr_get_last_session,
121 get_mcn: sr_get_mcn,
122 reset: sr_reset,
123 audio_ioctl: sr_audio_ioctl,
124 dev_ioctl: sr_dev_ioctl,
125 capability: CDC_CLOSE_TRAY | CDC_OPEN_TRAY | CDC_LOCK |
126 CDC_SELECT_SPEED | CDC_SELECT_DISC |
127 CDC_MULTI_SESSION | CDC_MCN |
128 CDC_MEDIA_CHANGED | CDC_PLAY_AUDIO |
129 CDC_RESET | CDC_IOCTLS | CDC_DRIVE_STATUS |
130 CDC_CD_R | CDC_CD_RW | CDC_DVD | CDC_DVD_R |
131 CDC_DVD_RAM | CDC_GENERIC_PACKET,
132 generic_packet: sr_packet,
133 };
134
135 /*
136 * This function checks to see if the media has been changed in the
137 * CDROM drive. It is possible that we have already sensed a change,
138 * or the drive may have sensed one and not yet reported it. We must
139 * be ready for either case. This function always reports the current
140 * value of the changed bit. If flag is 0, then the changed bit is reset.
141 * This function could be done as an ioctl, but we would need to have
142 * an inode for that to work, and we do not always have one.
143 */
144
145 int sr_media_change(struct cdrom_device_info *cdi, int slot)
146 {
147 int retval;
148
149 if (CDSL_CURRENT != slot) {
150 /* no changer support */
151 return -EINVAL;
152 }
153 retval = scsi_ioctl(scsi_CDs[MINOR(cdi->dev)].device,
154 SCSI_IOCTL_TEST_UNIT_READY, 0);
155
156 if (retval) {
157 /* Unable to test, unit probably not ready. This usually
158 * means there is no disc in the drive. Mark as changed,
159 * and we will figure it out later once the drive is
160 * available again. */
161
162 scsi_CDs[MINOR(cdi->dev)].device->changed = 1;
163 return 1; /* This will force a flush, if called from
164 * check_disk_change */
165 };
166
167 retval = scsi_CDs[MINOR(cdi->dev)].device->changed;
168 scsi_CDs[MINOR(cdi->dev)].device->changed = 0;
169 /* If the disk changed, the capacity will now be different,
170 * so we force a re-read of this information */
171 if (retval) {
172 /* check multisession offset etc */
173 sr_cd_check(cdi);
174
175 /*
176 * If the disk changed, the capacity will now be different,
177 * so we force a re-read of this information
178 * Force 2048 for the sector size so that filesystems won't
179 * be trying to use something that is too small if the disc
180 * has changed.
181 */
182 scsi_CDs[MINOR(cdi->dev)].needs_sector_size = 1;
183
184 scsi_CDs[MINOR(cdi->dev)].device->sector_size = 2048;
185 }
186 return retval;
187 }
188
189 /*
190 * rw_intr is the interrupt routine for the device driver. It will be notified on the
191 * end of a SCSI read / write, and will take on of several actions based on success or failure.
192 */
193
194 static void rw_intr(Scsi_Cmnd * SCpnt)
195 {
196 int result = SCpnt->result;
197 int this_count = SCpnt->bufflen >> 9;
198 int good_sectors = (result == 0 ? this_count : 0);
199 int block_sectors = 0;
200 int device_nr = DEVICE_NR(SCpnt->request.rq_dev);
201
202 #ifdef DEBUG
203 printk("sr.c done: %x %p\n", result, SCpnt->request.bh->b_data);
204 #endif
205 /*
206 Handle MEDIUM ERRORs or VOLUME OVERFLOWs that indicate partial success.
207 Since this is a relatively rare error condition, no care is taken to
208 avoid unnecessary additional work such as memcpy's that could be avoided.
209 */
210
211
212 if (driver_byte(result) != 0 && /* An error occurred */
213 SCpnt->sense_buffer[0] == 0xF0 && /* Sense data is valid */
214 (SCpnt->sense_buffer[2] == MEDIUM_ERROR ||
215 SCpnt->sense_buffer[2] == VOLUME_OVERFLOW ||
216 SCpnt->sense_buffer[2] == ILLEGAL_REQUEST)) {
217 long error_sector = (SCpnt->sense_buffer[3] << 24) |
218 (SCpnt->sense_buffer[4] << 16) |
219 (SCpnt->sense_buffer[5] << 8) |
220 SCpnt->sense_buffer[6];
221 if (SCpnt->request.bh != NULL)
222 block_sectors = SCpnt->request.bh->b_size >> 9;
223 if (block_sectors < 4)
224 block_sectors = 4;
225 if (scsi_CDs[device_nr].device->sector_size == 2048)
226 error_sector <<= 2;
227 error_sector &= ~(block_sectors - 1);
228 good_sectors = error_sector - SCpnt->request.sector;
229 if (good_sectors < 0 || good_sectors >= this_count)
230 good_sectors = 0;
231 /*
232 * The SCSI specification allows for the value returned by READ
233 * CAPACITY to be up to 75 2K sectors past the last readable
234 * block. Therefore, if we hit a medium error within the last
235 * 75 2K sectors, we decrease the saved size value.
236 */
237 if ((error_sector >> 1) < sr_sizes[device_nr] &&
238 scsi_CDs[device_nr].capacity - error_sector < 4 * 75)
239 sr_sizes[device_nr] = error_sector >> 1;
240 }
241
242 /*
243 * This calls the generic completion function, now that we know
244 * how many actual sectors finished, and how many sectors we need
245 * to say have failed.
246 */
247 scsi_io_completion(SCpnt, good_sectors, block_sectors);
248 }
249
250
251 static request_queue_t *sr_find_queue(kdev_t dev)
252 {
253 /*
254 * No such device
255 */
256 if (MINOR(dev) >= sr_template.dev_max || !scsi_CDs[MINOR(dev)].device)
257 return NULL;
258
259 return &scsi_CDs[MINOR(dev)].device->request_queue;
260 }
261
262 static int sr_scatter_pad(Scsi_Cmnd *SCpnt, int s_size)
263 {
264 struct scatterlist *sg, *old_sg = NULL;
265 int i, fsize, bsize, sg_ent, sg_count;
266 char *front, *back;
267
268 back = front = NULL;
269 sg_ent = SCpnt->use_sg;
270 bsize = 0; /* gcc... */
271
272 /*
273 * need front pad
274 */
275 if ((fsize = SCpnt->request.sector % (s_size >> 9))) {
276 fsize <<= 9;
277 sg_ent++;
278 if ((front = scsi_malloc(fsize)) == NULL)
279 goto no_mem;
280 }
281 /*
282 * need a back pad too
283 */
284 if ((bsize = s_size - ((SCpnt->request_bufflen + fsize) % s_size))) {
285 sg_ent++;
286 if ((back = scsi_malloc(bsize)) == NULL)
287 goto no_mem;
288 }
289
290 /*
291 * extend or allocate new scatter-gather table
292 */
293 sg_count = SCpnt->use_sg;
294 if (sg_count)
295 old_sg = (struct scatterlist *) SCpnt->request_buffer;
296 else {
297 sg_count = 1;
298 sg_ent++;
299 }
300
301 i = ((sg_ent * sizeof(struct scatterlist)) + 511) & ~511;
302 if ((sg = scsi_malloc(i)) == NULL)
303 goto no_mem;
304
305 /*
306 * no more failing memory allocs possible, we can safely assign
307 * SCpnt values now
308 */
309 SCpnt->sglist_len = i;
310 SCpnt->use_sg = sg_count;
311 memset(sg, 0, SCpnt->sglist_len);
312
313 i = 0;
314 if (fsize) {
315 sg[0].address = sg[0].alt_address = front;
316 sg[0].length = fsize;
317 i++;
318 }
319 if (old_sg) {
320 memcpy(sg + i, old_sg, SCpnt->use_sg * sizeof(struct scatterlist));
321 scsi_free(old_sg, ((SCpnt->use_sg * sizeof(struct scatterlist)) + 511) & ~511);
322 } else {
323 sg[i].address = SCpnt->request_buffer;
324 sg[i].length = SCpnt->request_bufflen;
325 }
326
327 SCpnt->request_bufflen += (fsize + bsize);
328 SCpnt->request_buffer = sg;
329 SCpnt->use_sg += i;
330
331 if (bsize) {
332 sg[SCpnt->use_sg].address = back;
333 sg[SCpnt->use_sg].alt_address = back;
334 sg[SCpnt->use_sg].length = bsize;
335 SCpnt->use_sg++;
336 }
337
338 return 0;
339
340 no_mem:
341 printk("sr: ran out of mem for scatter pad\n");
342 if (front)
343 scsi_free(front, fsize);
344 if (back)
345 scsi_free(back, bsize);
346
347 return 1;
348 }
349
350
351 static int sr_init_command(Scsi_Cmnd * SCpnt)
352 {
353 int dev, devm, block=0, this_count, s_size;
354
355 devm = MINOR(SCpnt->request.rq_dev);
356 dev = DEVICE_NR(SCpnt->request.rq_dev);
357
358 SCSI_LOG_HLQUEUE(1, printk("Doing sr request, dev = %d, block = %d\n", devm, block));
359
360 if (dev >= sr_template.nr_dev ||
361 !scsi_CDs[dev].device ||
362 !scsi_CDs[dev].device->online) {
363 SCSI_LOG_HLQUEUE(2, printk("Finishing %ld sectors\n", SCpnt->request.nr_sectors));
364 SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt));
365 return 0;
366 }
367 if (scsi_CDs[dev].device->changed) {
368 /*
369 * quietly refuse to do anything to a changed disc until the
370 * changed bit has been reset
371 */
372 return 0;
373 }
374
375 if ((SCpnt->request.cmd == WRITE) && !scsi_CDs[dev].device->writeable)
376 return 0;
377
378 /*
379 * we do lazy blocksize switching (when reading XA sectors,
380 * see CDROMREADMODE2 ioctl)
381 */
382 s_size = scsi_CDs[dev].device->sector_size;
383 if (s_size > 2048) {
384 if (!in_interrupt())
385 sr_set_blocklength(DEVICE_NR(CURRENT->rq_dev), 2048);
386 else
387 printk("sr: can't switch blocksize: in interrupt\n");
388 }
389
390 if (s_size != 512 && s_size != 1024 && s_size != 2048) {
391 printk("sr: bad sector size %d\n", s_size);
392 return 0;
393 }
394
395 block = SCpnt->request.sector / (s_size >> 9);
396
397 /*
398 * request doesn't start on hw block boundary, add scatter pads
399 */
400 if ((SCpnt->request.sector % (s_size >> 9)) || (SCpnt->request_bufflen % s_size))
401 if (sr_scatter_pad(SCpnt, s_size))
402 return 0;
403
404 this_count = (SCpnt->request_bufflen >> 9) / (s_size >> 9);
405
406 switch (SCpnt->request.cmd) {
407 case WRITE:
408 SCpnt->cmnd[0] = WRITE_10;
409 SCpnt->sc_data_direction = SCSI_DATA_WRITE;
410 break;
411 case READ:
412 SCpnt->cmnd[0] = READ_10;
413 SCpnt->sc_data_direction = SCSI_DATA_READ;
414 break;
415 default:
416 printk("Unknown sr command %d\n", SCpnt->request.cmd);
417 return 0;
418 }
419
420 SCSI_LOG_HLQUEUE(2, printk("sr%d : %s %d/%ld 512 byte blocks.\n",
421 devm,
422 (SCpnt->request.cmd == WRITE) ? "writing" : "reading",
423 this_count, SCpnt->request.nr_sectors));
424
425 SCpnt->cmnd[1] = (SCpnt->device->scsi_level <= SCSI_2) ?
426 ((SCpnt->lun << 5) & 0xe0) : 0;
427
428 if (this_count > 0xffff)
429 this_count = 0xffff;
430
431 SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
432 SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
433 SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
434 SCpnt->cmnd[5] = (unsigned char) block & 0xff;
435 SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
436 SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
437 SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
438
439 /*
440 * We shouldn't disconnect in the middle of a sector, so with a dumb
441 * host adapter, it's safe to assume that we can at least transfer
442 * this many bytes between each connect / disconnect.
443 */
444 SCpnt->transfersize = scsi_CDs[dev].device->sector_size;
445 SCpnt->underflow = this_count << 9;
446
447 SCpnt->allowed = MAX_RETRIES;
448 SCpnt->timeout_per_command = SR_TIMEOUT;
449
450 /*
451 * This is the completion routine we use. This is matched in terms
452 * of capability to this function.
453 */
454 SCpnt->done = rw_intr;
455
456 {
457 struct scatterlist *sg = SCpnt->request_buffer;
458 int i, size = 0;
459 for (i = 0; i < SCpnt->use_sg; i++)
460 size += sg[i].length;
461
462 if (size != SCpnt->request_bufflen && SCpnt->use_sg) {
463 printk("sr: mismatch count %d, bytes %d\n", size, SCpnt->request_bufflen);
464 SCpnt->request_bufflen = size;
465 }
466 }
467
468 /*
469 * This indicates that the command is ready from our end to be
470 * queued.
471 */
472 return 1;
473 }
474
475 static int sr_open(struct cdrom_device_info *cdi, int purpose)
476 {
477 check_disk_change(cdi->dev);
478
479 if (MINOR(cdi->dev) >= sr_template.dev_max
480 || !scsi_CDs[MINOR(cdi->dev)].device) {
481 return -ENXIO; /* No such device */
482 }
483 /*
484 * If the device is in error recovery, wait until it is done.
485 * If the device is offline, then disallow any access to it.
486 */
487 if (!scsi_block_when_processing_errors(scsi_CDs[MINOR(cdi->dev)].device)) {
488 return -ENXIO;
489 }
490 scsi_CDs[MINOR(cdi->dev)].device->access_count++;
491 if (scsi_CDs[MINOR(cdi->dev)].device->host->hostt->module)
492 __MOD_INC_USE_COUNT(scsi_CDs[MINOR(cdi->dev)].device->host->hostt->module);
493 if (sr_template.module)
494 __MOD_INC_USE_COUNT(sr_template.module);
495
496 /* If this device did not have media in the drive at boot time, then
497 * we would have been unable to get the sector size. Check to see if
498 * this is the case, and try again.
499 */
500
501 if (scsi_CDs[MINOR(cdi->dev)].needs_sector_size)
502 get_sectorsize(MINOR(cdi->dev));
503
504 return 0;
505 }
506
507 static int sr_detect(Scsi_Device * SDp)
508 {
509
510 if (SDp->type != TYPE_ROM && SDp->type != TYPE_WORM)
511 return 0;
512 sr_template.dev_noticed++;
513 return 1;
514 }
515
516 static int sr_attach(Scsi_Device * SDp)
517 {
518 Scsi_CD *cpnt;
519 int i;
520
521 if (SDp->type != TYPE_ROM && SDp->type != TYPE_WORM)
522 return 1;
523
524 if (sr_template.nr_dev >= sr_template.dev_max) {
525 SDp->attached--;
526 return 1;
527 }
528 for (cpnt = scsi_CDs, i = 0; i < sr_template.dev_max; i++, cpnt++)
529 if (!cpnt->device)
530 break;
531
532 if (i >= sr_template.dev_max)
533 panic("scsi_devices corrupt (sr)");
534
535
536 scsi_CDs[i].device = SDp;
537
538 sr_template.nr_dev++;
539 if (sr_template.nr_dev > sr_template.dev_max)
540 panic("scsi_devices corrupt (sr)");
541
542 printk("Attached scsi CD-ROM sr%d at scsi%d, channel %d, id %d, lun %d\n",
543 i, SDp->host->host_no, SDp->channel, SDp->id, SDp->lun);
544 return 0;
545 }
546
547
548 void get_sectorsize(int i)
549 {
550 unsigned char cmd[10];
551 unsigned char *buffer;
552 int the_result, retries;
553 int sector_size;
554 Scsi_Request *SRpnt;
555
556 buffer = (unsigned char *) scsi_malloc(512);
557 SRpnt = scsi_allocate_request(scsi_CDs[i].device);
558
559 if(buffer == NULL || SRpnt == NULL)
560 {
561 scsi_CDs[i].capacity = 0x1fffff;
562 sector_size = 2048; /* A guess, just in case */
563 scsi_CDs[i].needs_sector_size = 1;
564 if(buffer)
565 scsi_free(buffer, 512);
566 if(SRpnt)
567 scsi_release_request(SRpnt);
568 return;
569 }
570
571 retries = 3;
572 do {
573 cmd[0] = READ_CAPACITY;
574 cmd[1] = (scsi_CDs[i].device->scsi_level <= SCSI_2) ?
575 ((scsi_CDs[i].device->lun << 5) & 0xe0) : 0;
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;
579
580 memset(buffer, 0, 8);
581
582 /* Do the command and wait.. */
583
584 SRpnt->sr_data_direction = SCSI_DATA_READ;
585 scsi_wait_req(SRpnt, (void *) cmd, (void *) buffer,
586 8, SR_TIMEOUT, MAX_RETRIES);
587
588 the_result = SRpnt->sr_result;
589 retries--;
590
591 } while (the_result && retries);
592
593
594 scsi_release_request(SRpnt);
595 SRpnt = NULL;
596
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 &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) {
613 /*
614 * HP 4020i CD-Recorder reports 2340 byte sectors
615 * Philips CD-Writers report 2352 byte sectors
616 *
617 * Use 2k sectors for them..
618 */
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;
634 }
635
636 scsi_CDs[i].device->sector_size = sector_size;
637
638 /*
639 * Add this so that we have the ability to correctly gauge
640 * what the device is capable of.
641 */
642 scsi_CDs[i].needs_sector_size = 0;
643 sr_sizes[i] = scsi_CDs[i].capacity >> (BLOCK_SIZE_BITS - 9);
644 };
645 scsi_free(buffer, 512);
646 }
647
648 void get_capabilities(int i)
649 {
650 unsigned char cmd[6];
651 unsigned char *buffer;
652 int rc, n;
653
654 static char *loadmech[] =
655 {
656 "caddy",
657 "tray",
658 "pop-up",
659 "",
660 "changer",
661 "cartridge changer",
662 "",
663 ""
664 };
665
666 buffer = (unsigned char *) scsi_malloc(512);
667 if (!buffer)
668 {
669 printk(KERN_ERR "sr: out of memory.\n");
670 return;
671 }
672 cmd[0] = MODE_SENSE;
673 cmd[1] = (scsi_CDs[i].device->scsi_level <= SCSI_2) ?
674 ((scsi_CDs[i].device->lun << 5) & 0xe0) : 0;
675 cmd[2] = 0x2a;
676 cmd[4] = 128;
677 cmd[3] = cmd[5] = 0;
678 rc = sr_do_ioctl(i, cmd, buffer, 128, 1, SCSI_DATA_READ, NULL);
679
680 if (rc) {
681 /* failed, drive doesn't have capabilities mode page */
682 scsi_CDs[i].cdi.speed = 1;
683 scsi_CDs[i].cdi.mask |= (CDC_CD_R | CDC_CD_RW | CDC_DVD_R |
684 CDC_DVD | CDC_DVD_RAM |
685 CDC_SELECT_DISC | CDC_SELECT_SPEED);
686 scsi_free(buffer, 512);
687 printk("sr%i: scsi-1 drive\n", i);
688 return;
689 }
690 n = buffer[3] + 4;
691 scsi_CDs[i].cdi.speed = ((buffer[n + 8] << 8) + buffer[n + 9]) / 176;
692 scsi_CDs[i].readcd_known = 1;
693 scsi_CDs[i].readcd_cdda = buffer[n + 5] & 0x01;
694 /* print some capability bits */
695 printk("sr%i: scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n", i,
696 ((buffer[n + 14] << 8) + buffer[n + 15]) / 176,
697 scsi_CDs[i].cdi.speed,
698 buffer[n + 3] & 0x01 ? "writer " : "", /* CD Writer */
699 buffer[n + 3] & 0x20 ? "dvd-ram " : "",
700 buffer[n + 2] & 0x02 ? "cd/rw " : "", /* can read rewriteable */
701 buffer[n + 4] & 0x20 ? "xa/form2 " : "", /* can read xa/from2 */
702 buffer[n + 5] & 0x01 ? "cdda " : "", /* can read audio data */
703 loadmech[buffer[n + 6] >> 5]);
704 if ((buffer[n + 6] >> 5) == 0)
705 /* caddy drives can't close tray... */
706 scsi_CDs[i].cdi.mask |= CDC_CLOSE_TRAY;
707 if ((buffer[n + 2] & 0x8) == 0)
708 /* not a DVD drive */
709 scsi_CDs[i].cdi.mask |= CDC_DVD;
710 if ((buffer[n + 3] & 0x20) == 0) {
711 /* can't write DVD-RAM media */
712 scsi_CDs[i].cdi.mask |= CDC_DVD_RAM;
713 } else {
714 scsi_CDs[i].device->writeable = 1;
715 }
716 if ((buffer[n + 3] & 0x10) == 0)
717 /* can't write DVD-R media */
718 scsi_CDs[i].cdi.mask |= CDC_DVD_R;
719 if ((buffer[n + 3] & 0x2) == 0)
720 /* can't write CD-RW media */
721 scsi_CDs[i].cdi.mask |= CDC_CD_RW;
722 if ((buffer[n + 3] & 0x1) == 0)
723 /* can't write CD-R media */
724 scsi_CDs[i].cdi.mask |= CDC_CD_R;
725 if ((buffer[n + 6] & 0x8) == 0)
726 /* can't eject */
727 scsi_CDs[i].cdi.mask |= CDC_OPEN_TRAY;
728
729 if ((buffer[n + 6] >> 5) == mechtype_individual_changer ||
730 (buffer[n + 6] >> 5) == mechtype_cartridge_changer)
731 scsi_CDs[i].cdi.capacity =
732 cdrom_number_of_slots(&(scsi_CDs[i].cdi));
733 if (scsi_CDs[i].cdi.capacity <= 1)
734 /* not a changer */
735 scsi_CDs[i].cdi.mask |= CDC_SELECT_DISC;
736 /*else I don't think it can close its tray
737 scsi_CDs[i].cdi.mask |= CDC_CLOSE_TRAY; */
738
739 scsi_free(buffer, 512);
740 }
741
742 /*
743 * sr_packet() is the entry point for the generic commands generated
744 * by the Uniform CD-ROM layer.
745 */
746 static int sr_packet(struct cdrom_device_info *cdi, struct cdrom_generic_command *cgc)
747 {
748 Scsi_Device *device = scsi_CDs[MINOR(cdi->dev)].device;
749
750 /* set the LUN */
751 if (device->scsi_level <= SCSI_2)
752 cgc->cmd[1] |= device->lun << 5;
753
754 cgc->stat = sr_do_ioctl(MINOR(cdi->dev), cgc->cmd, cgc->buffer, cgc->buflen, cgc->quiet, cgc->data_direction, cgc->sense);
755
756 return cgc->stat;
757 }
758
759 static int sr_registered;
760
761 static int sr_init()
762 {
763 int i;
764
765 if (sr_template.dev_noticed == 0)
766 return 0;
767
768 if (!sr_registered) {
769 if (devfs_register_blkdev(MAJOR_NR, "sr", &cdrom_fops)) {
770 printk("Unable to get major %d for SCSI-CD\n", MAJOR_NR);
771 return 1;
772 }
773 sr_registered++;
774 }
775 if (scsi_CDs)
776 return 0;
777
778 sr_template.dev_max = sr_template.dev_noticed + SR_EXTRA_DEVS;
779 scsi_CDs = kmalloc(sr_template.dev_max * sizeof(Scsi_CD), GFP_ATOMIC);
780 if (!scsi_CDs)
781 goto cleanup_devfs;
782 memset(scsi_CDs, 0, sr_template.dev_max * sizeof(Scsi_CD));
783
784 sr_sizes = kmalloc(sr_template.dev_max * sizeof(int), GFP_ATOMIC);
785 if (!sr_sizes)
786 goto cleanup_cds;
787 memset(sr_sizes, 0, sr_template.dev_max * sizeof(int));
788
789 sr_blocksizes = kmalloc(sr_template.dev_max * sizeof(int), GFP_ATOMIC);
790 if (!sr_blocksizes)
791 goto cleanup_sizes;
792
793 sr_hardsizes = kmalloc(sr_template.dev_max * sizeof(int), GFP_ATOMIC);
794 if (!sr_hardsizes)
795 goto cleanup_blocksizes;
796 /*
797 * These are good guesses for the time being.
798 */
799 for (i = 0; i < sr_template.dev_max; i++) {
800 sr_blocksizes[i] = 2048;
801 sr_hardsizes[i] = 2048;
802 }
803 blksize_size[MAJOR_NR] = sr_blocksizes;
804 hardsect_size[MAJOR_NR] = sr_hardsizes;
805 return 0;
806 cleanup_blocksizes:
807 kfree(sr_blocksizes);
808 cleanup_sizes:
809 kfree(sr_sizes);
810 cleanup_cds:
811 kfree(scsi_CDs);
812 cleanup_devfs:
813 devfs_unregister_blkdev(MAJOR_NR, "sr");
814 sr_registered--;
815 return 1;
816 }
817
818 void sr_finish()
819 {
820 int i;
821 char name[6];
822
823 blk_dev[MAJOR_NR].queue = sr_find_queue;
824 blk_size[MAJOR_NR] = sr_sizes;
825
826 for (i = 0; i < sr_template.nr_dev; ++i) {
827 /* If we have already seen this, then skip it. Comes up
828 * with loadable modules. */
829 if (scsi_CDs[i].capacity)
830 continue;
831 scsi_CDs[i].capacity = 0x1fffff;
832 scsi_CDs[i].device->sector_size = 2048; /* A guess, just in case */
833 scsi_CDs[i].needs_sector_size = 1;
834 scsi_CDs[i].device->changed = 1; /* force recheck CD type */
835 #if 0
836 /* seems better to leave this for later */
837 get_sectorsize(i);
838 printk("Scd sectorsize = %d bytes.\n", scsi_CDs[i].sector_size);
839 #endif
840 scsi_CDs[i].use = 1;
841
842 scsi_CDs[i].device->ten = 1;
843 scsi_CDs[i].device->remap = 1;
844 scsi_CDs[i].readcd_known = 0;
845 scsi_CDs[i].readcd_cdda = 0;
846 sr_sizes[i] = scsi_CDs[i].capacity >> (BLOCK_SIZE_BITS - 9);
847
848 scsi_CDs[i].cdi.ops = &sr_dops;
849 scsi_CDs[i].cdi.handle = &scsi_CDs[i];
850 scsi_CDs[i].cdi.dev = MKDEV(MAJOR_NR, i);
851 scsi_CDs[i].cdi.mask = 0;
852 scsi_CDs[i].cdi.capacity = 1;
853 /*
854 * FIXME: someone needs to handle a get_capabilities
855 * failure properly ??
856 */
857 get_capabilities(i);
858 sr_vendor_init(i);
859
860 sprintf(name, "sr%d", i);
861 strcpy(scsi_CDs[i].cdi.name, name);
862 scsi_CDs[i].cdi.de =
863 devfs_register (scsi_CDs[i].device->de, "cd",
864 DEVFS_FL_DEFAULT, MAJOR_NR, i,
865 S_IFBLK | S_IRUGO | S_IWUGO,
866 &cdrom_fops, NULL);
867 register_cdrom(&scsi_CDs[i].cdi);
868 }
869
870
871 /* If our host adapter is capable of scatter-gather, then we increase
872 * the read-ahead to 16 blocks (32 sectors). If not, we use
873 * a two block (4 sector) read ahead. */
874 if (scsi_CDs[0].device && scsi_CDs[0].device->host->sg_tablesize)
875 read_ahead[MAJOR_NR] = 32; /* 32 sector read-ahead. Always removable. */
876 else
877 read_ahead[MAJOR_NR] = 4; /* 4 sector read-ahead */
878
879 return;
880 }
881
882 static void sr_detach(Scsi_Device * SDp)
883 {
884 Scsi_CD *cpnt;
885 int i;
886
887 for (cpnt = scsi_CDs, i = 0; i < sr_template.dev_max; i++, cpnt++)
888 if (cpnt->device == SDp) {
889 /*
890 * Since the cdrom is read-only, no need to sync the device.
891 * We should be kind to our buffer cache, however.
892 */
893 invalidate_device(MKDEV(MAJOR_NR, i), 0);
894
895 /*
896 * Reset things back to a sane state so that one can re-load a new
897 * driver (perhaps the same one).
898 */
899 unregister_cdrom(&(cpnt->cdi));
900 cpnt->device = NULL;
901 cpnt->capacity = 0;
902 SDp->attached--;
903 sr_template.nr_dev--;
904 sr_template.dev_noticed--;
905 sr_sizes[i] = 0;
906 return;
907 }
908 return;
909 }
910
911 static int __init init_sr(void)
912 {
913 sr_template.module = THIS_MODULE;
914 return scsi_register_module(MODULE_SCSI_DEV, &sr_template);
915 }
916
917 static void __exit exit_sr(void)
918 {
919 scsi_unregister_module(MODULE_SCSI_DEV, &sr_template);
920 devfs_unregister_blkdev(MAJOR_NR, "sr");
921 sr_registered--;
922 if (scsi_CDs != NULL) {
923 kfree(scsi_CDs);
924
925 kfree(sr_sizes);
926 sr_sizes = NULL;
927
928 kfree(sr_blocksizes);
929 sr_blocksizes = NULL;
930 kfree(sr_hardsizes);
931 sr_hardsizes = NULL;
932 }
933 blksize_size[MAJOR_NR] = NULL;
934 hardsect_size[MAJOR_NR] = NULL;
935 blk_size[MAJOR_NR] = NULL;
936 read_ahead[MAJOR_NR] = 0;
937
938 sr_template.dev_max = 0;
939 }
940
941 module_init(init_sr);
942 module_exit(exit_sr);
943