Import changeset
[linux-flexiantxendom0-3.2.10.git] / 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         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);
110 }
111
112 static struct cdrom_device_ops sr_dops =
113 {
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,
134 };
135
136 /*
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.
144  */
145
146 int sr_media_change(struct cdrom_device_info *cdi, int slot)
147 {
148         int retval;
149
150         if (CDSL_CURRENT != slot) {
151                 /* no changer support */
152                 return -EINVAL;
153         }
154         retval = scsi_ioctl(scsi_CDs[MINOR(cdi->dev)].device,
155                             SCSI_IOCTL_TEST_UNIT_READY, 0);
156
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.  */
162
163                 scsi_CDs[MINOR(cdi->dev)].device->changed = 1;
164                 return 1;       /* This will force a flush, if called from
165                                  * check_disk_change */
166         };
167
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);
175
176                 /* 
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.
182                  */
183                 scsi_CDs[MINOR(cdi->dev)].needs_sector_size = 1;
184
185                 scsi_CDs[MINOR(cdi->dev)].device->sector_size = 2048;
186         }
187         return retval;
188 }
189
190 /*
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.
193  */
194
195 static void rw_intr(Scsi_Cmnd * SCpnt)
196 {
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);
202
203 #ifdef DEBUG
204         printk("sr.c done: %x %p\n", result, SCpnt->request.bh->b_data);
205 #endif
206         /*
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.
210          */
211
212
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;
232                 /*
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.
237                  */
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;
241         }
242
243         /*
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.
247          */
248         scsi_io_completion(SCpnt, good_sectors, block_sectors);
249 }
250
251
252 static request_queue_t *sr_find_queue(kdev_t dev)
253 {
254         /*
255          * No such device
256          */
257         if (MINOR(dev) >= sr_template.dev_max || !scsi_CDs[MINOR(dev)].device)
258                 return NULL;
259
260         return &scsi_CDs[MINOR(dev)].device->request_queue;
261 }
262
263 static int sr_scatter_pad(Scsi_Cmnd *SCpnt, int s_size)
264 {
265         struct scatterlist *sg, *old_sg = NULL;
266         int i, fsize, bsize, sg_ent;
267         char *front, *back;
268
269         back = front = NULL;
270         sg_ent = SCpnt->use_sg;
271         bsize = 0; /* gcc... */
272
273         /*
274          * need front pad
275          */
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;
281         }
282         /*
283          * need a back pad too
284          */
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;
289         }
290
291         /*
292          * extend or allocate new scatter-gather table
293          */
294         if (SCpnt->use_sg)
295                 old_sg = (struct scatterlist *) SCpnt->request_buffer;
296         else {
297                 SCpnt->use_sg = 1;
298                 sg_ent++;
299         }
300
301         SCpnt->sglist_len = ((sg_ent * sizeof(struct scatterlist)) + 511) & ~511;
302         if ((sg = scsi_malloc(SCpnt->sglist_len)) == NULL)
303                 goto no_mem;
304
305         memset(sg, 0, SCpnt->sglist_len);
306
307         i = 0;
308         if (fsize) {
309                 sg[0].address = sg[0].alt_address = front;
310                 sg[0].length = fsize;
311                 i++;
312         }
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;
319         }
320
321         SCpnt->request_bufflen += (fsize + bsize);
322         SCpnt->request_buffer = sg;
323         SCpnt->use_sg += i;
324
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++;
330         }
331
332         return 0;
333
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);
340
341         return 1;
342 }
343
344
345 static int sr_init_command(Scsi_Cmnd * SCpnt)
346 {
347         int dev, devm, block, this_count, s_size;
348
349         devm = MINOR(SCpnt->request.rq_dev);
350         dev = DEVICE_NR(SCpnt->request.rq_dev);
351
352         SCSI_LOG_HLQUEUE(1, printk("Doing sr request, dev = %d, block = %d\n", devm, block));
353
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;
360         }
361         if (scsi_CDs[dev].device->changed) {
362                 /*
363                  * quietly refuse to do anything to a changed disc until the
364                  * changed bit has been reset
365                  */
366                 return 0;
367         }
368
369         if ((SCpnt->request.cmd == WRITE) && !scsi_CDs[dev].device->writeable)
370                 return 0;
371
372         /*
373          * we do lazy blocksize switching (when reading XA sectors,
374          * see CDROMREADMODE2 ioctl) 
375          */
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");
382         }
383
384         if (s_size != 512 && s_size != 1024 && s_size != 2048) {
385                 printk("sr: bad sector size %d\n", s_size);
386                 return 0;
387         }
388
389         block = SCpnt->request.sector / (s_size >> 9);
390
391         /*
392          * request doesn't start on hw block boundary, add scatter pads
393          */
394         if ((SCpnt->request.sector % (s_size >> 9)) || (SCpnt->request_bufflen % s_size))
395                 if (sr_scatter_pad(SCpnt, s_size))
396                         return 0;
397
398         this_count = (SCpnt->request_bufflen >> 9) / (s_size >> 9);
399
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;
412         }
413
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));
418
419         SCpnt->cmnd[1] = (SCpnt->lun << 5) & 0xe0;
420
421         if (this_count > 0xffff)
422                 this_count = 0xffff;
423
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;
431
432         /*
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.
436          */
437         SCpnt->transfersize = scsi_CDs[dev].device->sector_size;
438         SCpnt->underflow = this_count << 9;
439
440         SCpnt->allowed = MAX_RETRIES;
441         SCpnt->timeout_per_command = SR_TIMEOUT;
442
443         /*
444          * This is the completion routine we use.  This is matched in terms
445          * of capability to this function.
446          */
447         SCpnt->done = rw_intr;
448
449         {
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;
454
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;
458                 }
459         }
460
461         /*
462          * This indicates that the command is ready from our end to be
463          * queued.
464          */
465         return 1;
466 }
467
468 static int sr_open(struct cdrom_device_info *cdi, int purpose)
469 {
470         check_disk_change(cdi->dev);
471
472         if (MINOR(cdi->dev) >= sr_template.dev_max
473             || !scsi_CDs[MINOR(cdi->dev)].device) {
474                 return -ENXIO;  /* No such device */
475         }
476         /*
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.
479          */
480         if (!scsi_block_when_processing_errors(scsi_CDs[MINOR(cdi->dev)].device)) {
481                 return -ENXIO;
482         }
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);
488
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.
492          */
493
494         if (scsi_CDs[MINOR(cdi->dev)].needs_sector_size)
495                 get_sectorsize(MINOR(cdi->dev));
496
497         return 0;
498 }
499
500 /*
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.
504  */
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
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);
516
517         return 1;
518 }
519
520 static int sr_attach(Scsi_Device * SDp)
521 {
522         Scsi_CD *cpnt;
523         int i;
524
525         if (SDp->type != TYPE_ROM && SDp->type != TYPE_WORM)
526                 return 1;
527
528         if (sr_template.nr_dev >= sr_template.dev_max) {
529                 SDp->attached--;
530                 return 1;
531         }
532         for (cpnt = scsi_CDs, i = 0; i < sr_template.dev_max; i++, cpnt++)
533                 if (!cpnt->device)
534                         break;
535
536         if (i >= sr_template.dev_max)
537                 panic("scsi_devices corrupt (sr)");
538
539
540         scsi_CDs[i].device = SDp;
541
542         sr_template.nr_dev++;
543         if (sr_template.nr_dev > sr_template.dev_max)
544                 panic("scsi_devices corrupt (sr)");
545         return 0;
546 }
547
548
549 void get_sectorsize(int i)
550 {
551         unsigned char cmd[10];
552         unsigned char *buffer;
553         int the_result, retries;
554         int sector_size;
555         Scsi_Request *SRpnt;
556
557         buffer = (unsigned char *) scsi_malloc(512);
558         SRpnt = scsi_allocate_request(scsi_CDs[i].device);
559         
560         if(buffer == NULL || SRpnt == NULL)
561         {
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;
570         }       
571
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;
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                                          (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) {
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         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);
673
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;
681         }
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;
707         }
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;
720
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; */
730
731         scsi_free(buffer, 512);
732 }
733
734 /*
735  * sr_packet() is the entry point for the generic commands generated
736  * by the Uniform CD-ROM layer. 
737  */
738 static int sr_packet(struct cdrom_device_info *cdi, struct cdrom_generic_command *cgc)
739 {
740         Scsi_Device *device = scsi_CDs[MINOR(cdi->dev)].device;
741
742         /* set the LUN */
743         cgc->cmd[1] |= device->lun << 5;
744
745         cgc->stat = sr_do_ioctl(MINOR(cdi->dev), cgc->cmd, cgc->buffer, cgc->buflen, cgc->quiet, cgc->data_direction, cgc->sense);
746
747         return cgc->stat;
748 }
749
750 static int sr_registered;
751
752 static int sr_init()
753 {
754         int i;
755
756         if (sr_template.dev_noticed == 0)
757                 return 0;
758
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;
763                 }
764                 sr_registered++;
765         }
766         if (scsi_CDs)
767                 return 0;
768
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));
774
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));
779
780         sr_blocksizes = kmalloc(sr_template.dev_max * sizeof(int), GFP_ATOMIC);
781         if (!sr_blocksizes)
782                 goto cleanup_sizes;
783
784         sr_hardsizes = kmalloc(sr_template.dev_max * sizeof(int), GFP_ATOMIC);
785         if (!sr_hardsizes)
786                 goto cleanup_blocksizes;
787         /*
788          * These are good guesses for the time being.
789          */
790         for (i = 0; i < sr_template.dev_max; i++) {
791                 sr_blocksizes[i] = 2048;
792                 sr_hardsizes[i] = 2048;
793         }
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;
807 }
808
809 void sr_finish()
810 {
811         int i;
812         char name[6];
813
814         blk_dev[MAJOR_NR].queue = sr_find_queue;
815         blk_size[MAJOR_NR] = sr_sizes;
816
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;
832
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);
838
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);
846
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);
855         }
856
857
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 */
865
866         return;
867 }
868
869 static void sr_detach(Scsi_Device * SDp)
870 {
871         Scsi_CD *cpnt;
872         int i;
873
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);
878
879                         /*
880                          * Since the cdrom is read-only, no need to sync the device.
881                          * We should be kind to our buffer cache, however.
882                          */
883                         if (sb)
884                                 invalidate_inodes(sb);
885                         invalidate_buffers(devi);
886
887                         /*
888                          * Reset things back to a sane state so that one can re-load a new
889                          * driver (perhaps the same one).
890                          */
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;
899                 }
900         return;
901 }
902
903 static int __init init_sr(void)
904 {
905         sr_template.module = THIS_MODULE;
906         return scsi_register_module(MODULE_SCSI_DEV, &sr_template);
907 }
908
909 static void __exit exit_sr(void)
910 {
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);
916
917                 kfree(sr_sizes);
918                 sr_sizes = NULL;
919
920                 kfree(sr_blocksizes);
921                 sr_blocksizes = NULL;
922                 kfree(sr_hardsizes);
923                 sr_hardsizes = NULL;
924         }
925         blksize_size[MAJOR_NR] = NULL;
926         hardsect_size[MAJOR_NR] = NULL;
927         blk_size[MAJOR_NR] = NULL;
928         read_ahead[MAJOR_NR] = 0;
929
930         sr_template.dev_max = 0;
931 }
932
933 module_init(init_sr);
934 module_exit(exit_sr);