make oldconfig will rebuild these...
[linux-2.4.21-pre4.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         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         long error_sector;
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[0] & 0x7f) == 0x70) {  /* Sense data is valid */ //+Wilson04012004,from 2.4.24
216                 switch (SCpnt->sense_buffer[2]) {
217                 case MEDIUM_ERROR:
218                 case VOLUME_OVERFLOW:
219                 case ILLEGAL_REQUEST:
220                         //+Wilson04012004, from 2.4.24
221                         if (!(SCpnt->sense_buffer[0] & 0x80))
222                                 break;
223                         //Wilson04012004+
224                         error_sector = (SCpnt->sense_buffer[3] << 24) |
225                         (SCpnt->sense_buffer[4] << 16) |
226                         (SCpnt->sense_buffer[5] << 8) |
227                         SCpnt->sense_buffer[6];
228                         if (SCpnt->request.bh != NULL)
229                                 block_sectors = SCpnt->request.bh->b_size >> 9;
230                         if (block_sectors < 4)
231                                 block_sectors = 4;
232                         if (scsi_CDs[device_nr].device->sector_size == 2048)
233                                 error_sector <<= 2;
234                         error_sector &= ~(block_sectors - 1);
235                         good_sectors = error_sector - SCpnt->request.sector;
236                         if (good_sectors < 0 || good_sectors >= this_count)
237                                 good_sectors = 0;
238                         /*
239                          * The SCSI specification allows for the value returned
240                          * by READ CAPACITY to be up to 75 2K sectors past the
241                          * last readable block.  Therefore, if we hit a medium
242                          * error within the last 75 2K sectors, we decrease the
243                          * saved size value.
244                          */
245                         if ((error_sector >> 1) < sr_sizes[device_nr] &&
246                             scsi_CDs[device_nr].capacity - error_sector < 4 *75)
247                                 sr_sizes[device_nr] = error_sector >> 1;
248                         break;
249
250                 case RECOVERED_ERROR:
251                         /*
252                          * An error occured, but it recovered.  Inform the
253                          * user, but make sure that it's not treated as a
254                          * hard error.
255                          */
256                         print_sense("sr", SCpnt);
257                         result = 0;
258                         SCpnt->sense_buffer[0] = 0x0;
259                         good_sectors = this_count;
260                         break;
261
262                 default:
263                         break;
264                 }
265         }
266
267         /*
268          * This calls the generic completion function, now that we know
269          * how many actual sectors finished, and how many sectors we need
270          * to say have failed.
271          */
272         scsi_io_completion(SCpnt, good_sectors, block_sectors);
273 }
274
275
276 static request_queue_t *sr_find_queue(kdev_t dev)
277 {
278         /*
279          * No such device
280          */
281         if (MINOR(dev) >= sr_template.dev_max || !scsi_CDs[MINOR(dev)].device)
282                 return NULL;
283
284         return &scsi_CDs[MINOR(dev)].device->request_queue;
285 }
286
287 static int sr_scatter_pad(Scsi_Cmnd *SCpnt, int s_size)
288 {
289         struct scatterlist *sg, *old_sg = NULL;
290         int i, fsize, bsize, sg_ent, sg_count;
291         char *front, *back;
292         void **bbpnt, **old_bbpnt = NULL;
293
294         back = front = NULL;
295         sg_ent = SCpnt->use_sg;
296         bsize = 0; /* gcc... */
297
298         /*
299          * need front pad
300          */
301         if ((fsize = SCpnt->request.sector % (s_size >> 9))) {
302                 fsize <<= 9;
303                 sg_ent++;
304                 if ((front = scsi_malloc(fsize)) == NULL)
305                         goto no_mem;
306         }
307         /*
308          * need a back pad too
309          */
310         if ((bsize = s_size - ((SCpnt->request_bufflen + fsize) % s_size))) {
311                 sg_ent++;
312                 if ((back = scsi_malloc(bsize)) == NULL)
313                         goto no_mem;
314         }
315
316         /*
317          * extend or allocate new scatter-gather table
318          */
319         sg_count = SCpnt->use_sg;
320         if (sg_count) {
321                 old_sg = (struct scatterlist *) SCpnt->request_buffer;
322                 old_bbpnt = SCpnt->bounce_buffers;
323         } else {
324                 sg_count = 1;
325                 sg_ent++;
326         }
327
328         /* Get space for scatterlist and bounce buffer array. */
329         i  = sg_ent * sizeof(struct scatterlist);
330         i += sg_ent * sizeof(void *);
331         i  = (i + 511) & ~511;
332
333         if ((sg = scsi_malloc(i)) == NULL)
334                 goto no_mem;
335
336         bbpnt = (void **)
337                 ((char *)sg + (sg_ent * sizeof(struct scatterlist)));
338
339         /*
340          * no more failing memory allocs possible, we can safely assign
341          * SCpnt values now
342          */
343         SCpnt->sglist_len = i;
344         SCpnt->use_sg = sg_count;
345         memset(sg, 0, SCpnt->sglist_len);
346
347         i = 0;
348         if (fsize) {
349                 sg[0].address = bbpnt[0] = front;
350                 sg[0].length = fsize;
351                 i++;
352         }
353         if (old_sg) {
354                 memcpy(sg + i, old_sg, SCpnt->use_sg * sizeof(struct scatterlist));
355                 if (old_bbpnt)
356                         memcpy(bbpnt + i, old_bbpnt, SCpnt->use_sg * sizeof(void *));
357                 scsi_free(old_sg, (((SCpnt->use_sg * sizeof(struct scatterlist)) +
358                                     (SCpnt->use_sg * sizeof(void *))) + 511) & ~511);
359         } else {
360                 sg[i].address = SCpnt->request_buffer;
361                 sg[i].length = SCpnt->request_bufflen;
362         }
363
364         SCpnt->request_bufflen += (fsize + bsize);
365         SCpnt->request_buffer = sg;
366         SCpnt->bounce_buffers = bbpnt;
367         SCpnt->use_sg += i;
368
369         if (bsize) {
370                 sg[SCpnt->use_sg].address = back;
371                 bbpnt[SCpnt->use_sg] = back;
372                 sg[SCpnt->use_sg].length = bsize;
373                 SCpnt->use_sg++;
374         }
375
376         return 0;
377
378 no_mem:
379         printk("sr: ran out of mem for scatter pad\n");
380         if (front)
381                 scsi_free(front, fsize);
382         if (back)
383                 scsi_free(back, bsize);
384
385         return 1;
386 }
387
388
389 static int sr_init_command(Scsi_Cmnd * SCpnt)
390 {
391         int dev, devm, block=0, this_count, s_size;
392
393         devm = MINOR(SCpnt->request.rq_dev);
394         dev = DEVICE_NR(SCpnt->request.rq_dev);
395
396         SCSI_LOG_HLQUEUE(1, printk("Doing sr request, dev = %d, block = %d\n", devm, block));
397
398         if (dev >= sr_template.nr_dev ||
399             !scsi_CDs[dev].device ||
400             !scsi_CDs[dev].device->online) {
401                 SCSI_LOG_HLQUEUE(2, printk("Finishing %ld sectors\n", SCpnt->request.nr_sectors));
402                 SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt));
403                 return 0;
404         }
405         if (scsi_CDs[dev].device->changed) {
406                 /*
407                  * quietly refuse to do anything to a changed disc until the
408                  * changed bit has been reset
409                  */
410                 return 0;
411         }
412
413         if ((SCpnt->request.cmd == WRITE) && !scsi_CDs[dev].device->writeable)
414                 return 0;
415
416         /*
417          * we do lazy blocksize switching (when reading XA sectors,
418          * see CDROMREADMODE2 ioctl) 
419          */
420         s_size = scsi_CDs[dev].device->sector_size;
421         if (s_size > 2048) {
422                 if (!in_interrupt())
423                         sr_set_blocklength(DEVICE_NR(CURRENT->rq_dev), 2048);
424                 else
425                         printk("sr: can't switch blocksize: in interrupt\n");
426         }
427
428         if (s_size != 512 && s_size != 1024 && s_size != 2048) {
429                 printk("sr: bad sector size %d\n", s_size);
430                 return 0;
431         }
432
433         block = SCpnt->request.sector / (s_size >> 9);
434
435         /*
436          * request doesn't start on hw block boundary, add scatter pads
437          */
438         if ((SCpnt->request.sector % (s_size >> 9)) || (SCpnt->request_bufflen % s_size))
439                 if (sr_scatter_pad(SCpnt, s_size))
440                         return 0;
441
442         this_count = (SCpnt->request_bufflen >> 9) / (s_size >> 9);
443
444         switch (SCpnt->request.cmd) {
445         case WRITE:
446                 SCpnt->cmnd[0] = WRITE_10;
447                 SCpnt->sc_data_direction = SCSI_DATA_WRITE;
448                 break;
449         case READ:
450                 SCpnt->cmnd[0] = READ_10;
451                 SCpnt->sc_data_direction = SCSI_DATA_READ;
452                 break;
453         default:
454                 printk("Unknown sr command %d\n", SCpnt->request.cmd);
455                 return 0;
456         }
457
458         SCSI_LOG_HLQUEUE(2, printk("sr%d : %s %d/%ld 512 byte blocks.\n",
459                                    devm,
460                    (SCpnt->request.cmd == WRITE) ? "writing" : "reading",
461                                  this_count, SCpnt->request.nr_sectors));
462
463         SCpnt->cmnd[1] = (SCpnt->device->scsi_level <= SCSI_2) ?
464                          ((SCpnt->lun << 5) & 0xe0) : 0;
465
466         if (this_count > 0xffff)
467                 this_count = 0xffff;
468
469         SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
470         SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
471         SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
472         SCpnt->cmnd[5] = (unsigned char) block & 0xff;
473         SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
474         SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
475         SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
476
477         /*
478          * We shouldn't disconnect in the middle of a sector, so with a dumb
479          * host adapter, it's safe to assume that we can at least transfer
480          * this many bytes between each connect / disconnect.
481          */
482         SCpnt->transfersize = scsi_CDs[dev].device->sector_size;
483         SCpnt->underflow = this_count << 9;
484
485         SCpnt->allowed = MAX_RETRIES;
486         SCpnt->timeout_per_command = SR_TIMEOUT;
487
488         /*
489          * This is the completion routine we use.  This is matched in terms
490          * of capability to this function.
491          */
492         SCpnt->done = rw_intr;
493
494         {
495                 struct scatterlist *sg = SCpnt->request_buffer;
496                 int i, size = 0;
497                 for (i = 0; i < SCpnt->use_sg; i++)
498                         size += sg[i].length;
499
500                 if (size != SCpnt->request_bufflen && SCpnt->use_sg) {
501                         printk("sr: mismatch count %d, bytes %d\n", size, SCpnt->request_bufflen);
502                         SCpnt->request_bufflen = size;
503                 }
504         }
505
506         /*
507          * This indicates that the command is ready from our end to be
508          * queued.
509          */
510         return 1;
511 }
512
513 struct block_device_operations sr_bdops =
514 {
515         owner:                  THIS_MODULE,
516         open:                   cdrom_open,
517         release:                cdrom_release,
518         ioctl:                  cdrom_ioctl,
519         check_media_change:     cdrom_media_changed,
520 };
521
522 static int sr_open(struct cdrom_device_info *cdi, int purpose)
523 {
524         check_disk_change(cdi->dev);
525
526         if (MINOR(cdi->dev) >= sr_template.dev_max
527             || !scsi_CDs[MINOR(cdi->dev)].device) {
528                 return -ENXIO;  /* No such device */
529         }
530         /*
531          * If the device is in error recovery, wait until it is done.
532          * If the device is offline, then disallow any access to it.
533          */
534         if (!scsi_block_when_processing_errors(scsi_CDs[MINOR(cdi->dev)].device)) {
535                 return -ENXIO;
536         }
537         scsi_CDs[MINOR(cdi->dev)].device->access_count++;
538         if (scsi_CDs[MINOR(cdi->dev)].device->host->hostt->module)
539                 __MOD_INC_USE_COUNT(scsi_CDs[MINOR(cdi->dev)].device->host->hostt->module);
540         if (sr_template.module)
541                 __MOD_INC_USE_COUNT(sr_template.module);
542
543         /* If this device did not have media in the drive at boot time, then
544          * we would have been unable to get the sector size.  Check to see if
545          * this is the case, and try again.
546          */
547
548         if (scsi_CDs[MINOR(cdi->dev)].needs_sector_size)
549                 get_sectorsize(MINOR(cdi->dev));
550
551         return 0;
552 }
553
554 static int sr_detect(Scsi_Device * SDp)
555 {
556
557         if (SDp->type != TYPE_ROM && SDp->type != TYPE_WORM)
558                 return 0;
559         sr_template.dev_noticed++;
560         return 1;
561 }
562
563 static int sr_attach(Scsi_Device * SDp)
564 {
565         Scsi_CD *cpnt;
566         int i;
567
568         if (SDp->type != TYPE_ROM && SDp->type != TYPE_WORM)
569                 return 1;
570
571         if (sr_template.nr_dev >= sr_template.dev_max) {
572                 SDp->attached--;
573                 return 1;
574         }
575         for (cpnt = scsi_CDs, i = 0; i < sr_template.dev_max; i++, cpnt++)
576                 if (!cpnt->device)
577                         break;
578
579         if (i >= sr_template.dev_max)
580                 panic("scsi_devices corrupt (sr)");
581
582
583         scsi_CDs[i].device = SDp;
584
585         sr_template.nr_dev++;
586         if (sr_template.nr_dev > sr_template.dev_max)
587                 panic("scsi_devices corrupt (sr)");
588
589         printk("Attached scsi CD-ROM sr%d at scsi%d, channel %d, id %d, lun %d\n",
590                i, SDp->host->host_no, SDp->channel, SDp->id, SDp->lun);
591         return 0;
592 }
593
594
595 void get_sectorsize(int i)
596 {
597         unsigned char cmd[10];
598         unsigned char *buffer;
599         int the_result, retries;
600         int sector_size;
601         Scsi_Request *SRpnt;
602
603         buffer = (unsigned char *) scsi_malloc(512);
604         SRpnt = scsi_allocate_request(scsi_CDs[i].device);
605         
606         if(buffer == NULL || SRpnt == NULL)
607         {
608                 scsi_CDs[i].capacity = 0x1fffff;
609                 sector_size = 2048;     /* A guess, just in case */
610                 scsi_CDs[i].needs_sector_size = 1;
611                 if(buffer)
612                         scsi_free(buffer, 512);
613                 if(SRpnt)
614                         scsi_release_request(SRpnt);
615                 return;
616         }       
617
618         retries = 3;
619         do {
620                 cmd[0] = READ_CAPACITY;
621                 cmd[1] = (scsi_CDs[i].device->scsi_level <= SCSI_2) ?
622                          ((scsi_CDs[i].device->lun << 5) & 0xe0) : 0;
623                 memset((void *) &cmd[2], 0, 8);
624                 SRpnt->sr_request.rq_status = RQ_SCSI_BUSY;     /* Mark as really busy */
625                 SRpnt->sr_cmd_len = 0;
626
627                 memset(buffer, 0, 8);
628
629                 /* Do the command and wait.. */
630
631                 SRpnt->sr_data_direction = SCSI_DATA_READ;
632                 scsi_wait_req(SRpnt, (void *) cmd, (void *) buffer,
633                               8, SR_TIMEOUT, MAX_RETRIES);
634
635                 the_result = SRpnt->sr_result;
636                 retries--;
637
638         } while (the_result && retries);
639
640
641         scsi_release_request(SRpnt);
642         SRpnt = NULL;
643
644         if (the_result) {
645                 scsi_CDs[i].capacity = 0x1fffff;
646                 sector_size = 2048;     /* A guess, just in case */
647                 scsi_CDs[i].needs_sector_size = 1;
648         } else {
649 #if 0
650                 if (cdrom_get_last_written(MKDEV(MAJOR_NR, i),
651                                            &scsi_CDs[i].capacity))
652 #endif
653                         scsi_CDs[i].capacity = 1 + ((buffer[0] << 24) |
654                                                     (buffer[1] << 16) |
655                                                     (buffer[2] << 8) |
656                                                     buffer[3]);
657                 sector_size = (buffer[4] << 24) |
658                     (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
659                 switch (sector_size) {
660                         /*
661                          * HP 4020i CD-Recorder reports 2340 byte sectors
662                          * Philips CD-Writers report 2352 byte sectors
663                          *
664                          * Use 2k sectors for them..
665                          */
666                 case 0:
667                 case 2340:
668                 case 2352:
669                         sector_size = 2048;
670                         /* fall through */
671                 case 2048:
672                         scsi_CDs[i].capacity *= 4;
673                         /* fall through */
674                 case 512:
675                         break;
676                 default:
677                         printk("sr%d: unsupported sector size %d.\n",
678                                i, sector_size);
679                         scsi_CDs[i].capacity = 0;
680                         scsi_CDs[i].needs_sector_size = 1;
681                 }
682
683                 scsi_CDs[i].device->sector_size = sector_size;
684
685                 /*
686                  * Add this so that we have the ability to correctly gauge
687                  * what the device is capable of.
688                  */
689                 scsi_CDs[i].needs_sector_size = 0;
690                 sr_sizes[i] = scsi_CDs[i].capacity >> (BLOCK_SIZE_BITS - 9);
691         };
692         scsi_free(buffer, 512);
693 }
694
695 void get_capabilities(int i)
696 {
697         unsigned char cmd[6];
698         unsigned char *buffer;
699         int rc, n;
700
701         static char *loadmech[] =
702         {
703                 "caddy",
704                 "tray",
705                 "pop-up",
706                 "",
707                 "changer",
708                 "cartridge changer",
709                 "",
710                 ""
711         };
712
713         buffer = (unsigned char *) scsi_malloc(512);
714         if (!buffer)
715         {
716                 printk(KERN_ERR "sr: out of memory.\n");
717                 return;
718         }
719         cmd[0] = MODE_SENSE;
720         cmd[1] = (scsi_CDs[i].device->scsi_level <= SCSI_2) ?
721                  ((scsi_CDs[i].device->lun << 5) & 0xe0) : 0;
722         cmd[2] = 0x2a;
723         cmd[4] = 128;
724         cmd[3] = cmd[5] = 0;
725         rc = sr_do_ioctl(i, cmd, buffer, 128, 1, SCSI_DATA_READ, NULL);
726
727         if (rc) {
728                 /* failed, drive doesn't have capabilities mode page */
729                 scsi_CDs[i].cdi.speed = 1;
730                 scsi_CDs[i].cdi.mask |= (CDC_CD_R | CDC_CD_RW | CDC_DVD_R |
731                                          CDC_DVD | CDC_DVD_RAM |
732                                          CDC_SELECT_DISC | CDC_SELECT_SPEED);
733                 scsi_free(buffer, 512);
734                 printk("sr%i: scsi-1 drive\n", i);
735                 return;
736         }
737         n = buffer[3] + 4;
738         scsi_CDs[i].cdi.speed = ((buffer[n + 8] << 8) + buffer[n + 9]) / 176;
739         scsi_CDs[i].readcd_known = 1;
740         scsi_CDs[i].readcd_cdda = buffer[n + 5] & 0x01;
741         /* print some capability bits */
742         printk("sr%i: scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n", i,
743                ((buffer[n + 14] << 8) + buffer[n + 15]) / 176,
744                scsi_CDs[i].cdi.speed,
745                buffer[n + 3] & 0x01 ? "writer " : "",   /* CD Writer */
746                buffer[n + 3] & 0x20 ? "dvd-ram " : "",
747                buffer[n + 2] & 0x02 ? "cd/rw " : "",    /* can read rewriteable */
748                buffer[n + 4] & 0x20 ? "xa/form2 " : "",         /* can read xa/from2 */
749                buffer[n + 5] & 0x01 ? "cdda " : "",     /* can read audio data */
750                loadmech[buffer[n + 6] >> 5]);
751         if ((buffer[n + 6] >> 5) == 0)
752                 /* caddy drives can't close tray... */
753                 scsi_CDs[i].cdi.mask |= CDC_CLOSE_TRAY;
754         if ((buffer[n + 2] & 0x8) == 0)
755                 /* not a DVD drive */
756                 scsi_CDs[i].cdi.mask |= CDC_DVD;
757         if ((buffer[n + 3] & 0x20) == 0) {
758                 /* can't write DVD-RAM media */
759                 scsi_CDs[i].cdi.mask |= CDC_DVD_RAM;
760         } else {
761                 scsi_CDs[i].device->writeable = 1;
762         }
763         if ((buffer[n + 3] & 0x10) == 0)
764                 /* can't write DVD-R media */
765                 scsi_CDs[i].cdi.mask |= CDC_DVD_R;
766         if ((buffer[n + 3] & 0x2) == 0)
767                 /* can't write CD-RW media */
768                 scsi_CDs[i].cdi.mask |= CDC_CD_RW;
769         if ((buffer[n + 3] & 0x1) == 0)
770                 /* can't write CD-R media */
771                 scsi_CDs[i].cdi.mask |= CDC_CD_R;
772         if ((buffer[n + 6] & 0x8) == 0)
773                 /* can't eject */
774                 scsi_CDs[i].cdi.mask |= CDC_OPEN_TRAY;
775
776         if ((buffer[n + 6] >> 5) == mechtype_individual_changer ||
777             (buffer[n + 6] >> 5) == mechtype_cartridge_changer)
778                 scsi_CDs[i].cdi.capacity =
779                     cdrom_number_of_slots(&(scsi_CDs[i].cdi));
780         if (scsi_CDs[i].cdi.capacity <= 1)
781                 /* not a changer */
782                 scsi_CDs[i].cdi.mask |= CDC_SELECT_DISC;
783         /*else    I don't think it can close its tray
784            scsi_CDs[i].cdi.mask |= CDC_CLOSE_TRAY; */
785
786         scsi_free(buffer, 512);
787 }
788
789 /*
790  * sr_packet() is the entry point for the generic commands generated
791  * by the Uniform CD-ROM layer. 
792  */
793 static int sr_packet(struct cdrom_device_info *cdi, struct cdrom_generic_command *cgc)
794 {
795         Scsi_Device *device = scsi_CDs[MINOR(cdi->dev)].device;
796
797         /* set the LUN */
798         if (device->scsi_level <= SCSI_2)
799                 cgc->cmd[1] |= device->lun << 5;
800
801         cgc->stat = sr_do_ioctl(MINOR(cdi->dev), cgc->cmd, cgc->buffer, cgc->buflen, cgc->quiet, cgc->data_direction, cgc->sense);
802
803         return cgc->stat;
804 }
805
806 static int sr_registered;
807
808 static int sr_init()
809 {
810         int i;
811
812         if (sr_template.dev_noticed == 0)
813                 return 0;
814
815         if (!sr_registered) {
816                 if (devfs_register_blkdev(MAJOR_NR, "sr", &sr_bdops)) {
817                         printk("Unable to get major %d for SCSI-CD\n", MAJOR_NR);
818                         sr_template.dev_noticed = 0;
819                         return 1;
820                 }
821                 sr_registered++;
822         }
823         if (scsi_CDs)
824                 return 0;
825
826         sr_template.dev_max = sr_template.dev_noticed + SR_EXTRA_DEVS;
827         scsi_CDs = kmalloc(sr_template.dev_max * sizeof(Scsi_CD), GFP_ATOMIC);
828         if (!scsi_CDs)
829                 goto cleanup_devfs;
830         memset(scsi_CDs, 0, sr_template.dev_max * sizeof(Scsi_CD));
831
832         sr_sizes = kmalloc(sr_template.dev_max * sizeof(int), GFP_ATOMIC);
833         if (!sr_sizes)
834                 goto cleanup_cds;
835         memset(sr_sizes, 0, sr_template.dev_max * sizeof(int));
836
837         sr_blocksizes = kmalloc(sr_template.dev_max * sizeof(int), GFP_ATOMIC);
838         if (!sr_blocksizes)
839                 goto cleanup_sizes;
840
841         sr_hardsizes = kmalloc(sr_template.dev_max * sizeof(int), GFP_ATOMIC);
842         if (!sr_hardsizes)
843                 goto cleanup_blocksizes;
844         /*
845          * These are good guesses for the time being.
846          */
847         for (i = 0; i < sr_template.dev_max; i++) {
848                 sr_blocksizes[i] = 2048;
849                 sr_hardsizes[i] = 2048;
850         }
851         blksize_size[MAJOR_NR] = sr_blocksizes;
852         hardsect_size[MAJOR_NR] = sr_hardsizes;
853         return 0;
854 cleanup_blocksizes:
855         kfree(sr_blocksizes);
856 cleanup_sizes:
857         kfree(sr_sizes);
858 cleanup_cds:
859         kfree(scsi_CDs);
860         scsi_CDs = NULL;
861 cleanup_devfs:
862         devfs_unregister_blkdev(MAJOR_NR, "sr");
863         sr_template.dev_noticed = 0;
864         sr_registered--;
865         return 1;
866 }
867
868 void sr_finish()
869 {
870         int i;
871         char name[6];
872
873         blk_dev[MAJOR_NR].queue = sr_find_queue;
874         blk_size[MAJOR_NR] = sr_sizes;
875
876         for (i = 0; i < sr_template.nr_dev; ++i) {
877                 /* If we have already seen this, then skip it.  Comes up
878                  * with loadable modules. */
879                 if (scsi_CDs[i].capacity)
880                         continue;
881                 scsi_CDs[i].capacity = 0x1fffff;
882                 scsi_CDs[i].device->sector_size = 2048;         /* A guess, just in case */
883                 scsi_CDs[i].needs_sector_size = 1;
884                 scsi_CDs[i].device->changed = 1;        /* force recheck CD type */
885 #if 0
886                 /* seems better to leave this for later */
887                 get_sectorsize(i);
888                 printk("Scd sectorsize = %d bytes.\n", scsi_CDs[i].sector_size);
889 #endif
890                 scsi_CDs[i].use = 1;
891
892                 scsi_CDs[i].device->ten = 1;
893                 scsi_CDs[i].device->remap = 1;
894                 scsi_CDs[i].readcd_known = 0;
895                 scsi_CDs[i].readcd_cdda = 0;
896                 sr_sizes[i] = scsi_CDs[i].capacity >> (BLOCK_SIZE_BITS - 9);
897
898                 scsi_CDs[i].cdi.ops = &sr_dops;
899                 scsi_CDs[i].cdi.handle = &scsi_CDs[i];
900                 scsi_CDs[i].cdi.dev = MKDEV(MAJOR_NR, i);
901                 scsi_CDs[i].cdi.mask = 0;
902                 scsi_CDs[i].cdi.capacity = 1;
903                 /*
904                  *      FIXME: someone needs to handle a get_capabilities
905                  *      failure properly ??
906                  */
907                 get_capabilities(i);
908                 sr_vendor_init(i);
909
910                 sprintf(name, "sr%d", i);
911                 strcpy(scsi_CDs[i].cdi.name, name);
912                 scsi_CDs[i].cdi.de =
913                     devfs_register (scsi_CDs[i].device->de, "cd",
914                                     DEVFS_FL_DEFAULT, MAJOR_NR, i,
915                                     S_IFBLK | S_IRUGO | S_IWUGO,
916                                     &sr_bdops, NULL);
917                 register_cdrom(&scsi_CDs[i].cdi);
918         }
919
920
921         /* If our host adapter is capable of scatter-gather, then we increase
922          * the read-ahead to 16 blocks (32 sectors).  If not, we use
923          * a two block (4 sector) read ahead. */
924         if (scsi_CDs[0].device && scsi_CDs[0].device->host->sg_tablesize)
925                 read_ahead[MAJOR_NR] = 32;      /* 32 sector read-ahead.  Always removable. */
926         else
927                 read_ahead[MAJOR_NR] = 4;       /* 4 sector read-ahead */
928
929         return;
930 }
931
932 static void sr_detach(Scsi_Device * SDp)
933 {
934         Scsi_CD *cpnt;
935         int i;
936
937         if (scsi_CDs == NULL)
938                 return;
939         for (cpnt = scsi_CDs, i = 0; i < sr_template.dev_max; i++, cpnt++)
940                 if (cpnt->device == SDp) {
941                         /*
942                          * Since the cdrom is read-only, no need to sync the device.
943                          * We should be kind to our buffer cache, however.
944                          */
945                         invalidate_device(MKDEV(MAJOR_NR, i), 0);
946
947                         /*
948                          * Reset things back to a sane state so that one can re-load a new
949                          * driver (perhaps the same one).
950                          */
951                         unregister_cdrom(&(cpnt->cdi));
952                         cpnt->device = NULL;
953                         cpnt->capacity = 0;
954                         SDp->attached--;
955                         sr_template.nr_dev--;
956                         sr_template.dev_noticed--;
957                         sr_sizes[i] = 0;
958                         return;
959                 }
960         return;
961 }
962
963 static int __init init_sr(void)
964 {
965         sr_template.module = THIS_MODULE;
966         return scsi_register_module(MODULE_SCSI_DEV, &sr_template);
967 }
968
969 static void __exit exit_sr(void)
970 {
971         scsi_unregister_module(MODULE_SCSI_DEV, &sr_template);
972         devfs_unregister_blkdev(MAJOR_NR, "sr");
973         sr_registered--;
974         if (scsi_CDs != NULL) {
975                 kfree(scsi_CDs);
976
977                 kfree(sr_sizes);
978                 sr_sizes = NULL;
979
980                 kfree(sr_blocksizes);
981                 sr_blocksizes = NULL;
982                 kfree(sr_hardsizes);
983                 sr_hardsizes = NULL;
984         }
985         blksize_size[MAJOR_NR] = NULL;
986         hardsect_size[MAJOR_NR] = NULL;
987         blk_size[MAJOR_NR] = NULL;
988         read_ahead[MAJOR_NR] = 0;
989
990         sr_template.dev_max = 0;
991 }
992
993 module_init(init_sr);
994 module_exit(exit_sr);
995 MODULE_LICENSE("GPL");