ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.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 lose 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 #include <linux/module.h>
36 #include <linux/fs.h>
37 #include <linux/kernel.h>
38 #include <linux/sched.h>
39 #include <linux/mm.h>
40 #include <linux/bio.h>
41 #include <linux/string.h>
42 #include <linux/errno.h>
43 #include <linux/cdrom.h>
44 #include <linux/interrupt.h>
45 #include <linux/init.h>
46 #include <linux/blkdev.h>
47 #include <asm/uaccess.h>
48
49 #include "scsi.h"
50 #include "hosts.h"
51
52 #include <scsi/scsi_driver.h>
53 #include <scsi/scsi_ioctl.h>    /* For the door lock/unlock commands */
54
55 #include "scsi_logging.h"
56 #include "sr.h"
57
58
59 MODULE_PARM(xa_test, "i");      /* see sr_ioctl.c */
60
61
62 #define SR_DISKS        256
63
64 #define MAX_RETRIES     3
65 #define SR_TIMEOUT      (30 * HZ)
66 #define SR_CAPABILITIES \
67         (CDC_CLOSE_TRAY|CDC_OPEN_TRAY|CDC_LOCK|CDC_SELECT_SPEED| \
68          CDC_SELECT_DISC|CDC_MULTI_SESSION|CDC_MCN|CDC_MEDIA_CHANGED| \
69          CDC_PLAY_AUDIO|CDC_RESET|CDC_IOCTLS|CDC_DRIVE_STATUS| \
70          CDC_CD_R|CDC_CD_RW|CDC_DVD|CDC_DVD_R|CDC_DVD_RAM|CDC_GENERIC_PACKET| \
71          CDC_MRW|CDC_MRW_W|CDC_RAM)
72
73 static int sr_probe(struct device *);
74 static int sr_remove(struct device *);
75 static int sr_init_command(struct scsi_cmnd *);
76
77 static struct scsi_driver sr_template = {
78         .owner                  = THIS_MODULE,
79         .gendrv = {
80                 .name           = "sr",
81                 .probe          = sr_probe,
82                 .remove         = sr_remove,
83         },
84         .init_command           = sr_init_command,
85 };
86
87 static unsigned long sr_index_bits[SR_DISKS / BITS_PER_LONG];
88 static spinlock_t sr_index_lock = SPIN_LOCK_UNLOCKED;
89
90 /* This semaphore is used to mediate the 0->1 reference get in the
91  * face of object destruction (i.e. we can't allow a get on an
92  * object after last put) */
93 static DECLARE_MUTEX(sr_ref_sem);
94
95 static int sr_open(struct cdrom_device_info *, int);
96 static void sr_release(struct cdrom_device_info *);
97
98 static void get_sectorsize(struct scsi_cd *);
99 static void get_capabilities(struct scsi_cd *);
100
101 static int sr_media_change(struct cdrom_device_info *, int);
102 static int sr_packet(struct cdrom_device_info *, struct cdrom_generic_command *);
103
104 static struct cdrom_device_ops sr_dops = {
105         .open                   = sr_open,
106         .release                = sr_release,
107         .drive_status           = sr_drive_status,
108         .media_changed          = sr_media_change,
109         .tray_move              = sr_tray_move,
110         .lock_door              = sr_lock_door,
111         .select_speed           = sr_select_speed,
112         .get_last_session       = sr_get_last_session,
113         .get_mcn                = sr_get_mcn,
114         .reset                  = sr_reset,
115         .audio_ioctl            = sr_audio_ioctl,
116         .dev_ioctl              = sr_dev_ioctl,
117         .capability             = SR_CAPABILITIES,
118         .generic_packet         = sr_packet,
119 };
120
121 static void sr_kref_release(struct kref *kref);
122
123 static inline struct scsi_cd *scsi_cd(struct gendisk *disk)
124 {
125         return container_of(disk->private_data, struct scsi_cd, driver);
126 }
127
128 /*
129  * The get and put routines for the struct scsi_cd.  Note this entity
130  * has a scsi_device pointer and owns a reference to this.
131  */
132 static inline struct scsi_cd *scsi_cd_get(struct gendisk *disk)
133 {
134         struct scsi_cd *cd = NULL;
135
136         down(&sr_ref_sem);
137         if (disk->private_data == NULL)
138                 goto out;
139         cd = scsi_cd(disk);
140         if (!kref_get(&cd->kref))
141                 cd = NULL;
142  out:
143         up(&sr_ref_sem);
144         return cd;
145 }
146
147 static inline void scsi_cd_put(struct scsi_cd *cd)
148 {
149         down(&sr_ref_sem);
150         kref_put(&cd->kref);
151         up(&sr_ref_sem);
152 }
153
154 /*
155  * This function checks to see if the media has been changed in the
156  * CDROM drive.  It is possible that we have already sensed a change,
157  * or the drive may have sensed one and not yet reported it.  We must
158  * be ready for either case. This function always reports the current
159  * value of the changed bit.  If flag is 0, then the changed bit is reset.
160  * This function could be done as an ioctl, but we would need to have
161  * an inode for that to work, and we do not always have one.
162  */
163
164 int sr_media_change(struct cdrom_device_info *cdi, int slot)
165 {
166         struct scsi_cd *cd = cdi->handle;
167         int retval;
168
169         if (CDSL_CURRENT != slot) {
170                 /* no changer support */
171                 return -EINVAL;
172         }
173
174         retval = scsi_ioctl(cd->device, SCSI_IOCTL_TEST_UNIT_READY, 0);
175         if (retval) {
176                 /* Unable to test, unit probably not ready.  This usually
177                  * means there is no disc in the drive.  Mark as changed,
178                  * and we will figure it out later once the drive is
179                  * available again.  */
180                 cd->device->changed = 1;
181                 return 1;       /* This will force a flush, if called from
182                                  * check_disk_change */
183         };
184
185         retval = cd->device->changed;
186         cd->device->changed = 0;
187         /* If the disk changed, the capacity will now be different,
188          * so we force a re-read of this information */
189         if (retval) {
190                 /* check multisession offset etc */
191                 sr_cd_check(cdi);
192
193                 /* 
194                  * If the disk changed, the capacity will now be different,
195                  * so we force a re-read of this information 
196                  * Force 2048 for the sector size so that filesystems won't
197                  * be trying to use something that is too small if the disc
198                  * has changed.
199                  */
200                 cd->needs_sector_size = 1;
201                 cd->device->sector_size = 2048;
202         }
203         return retval;
204 }
205  
206 /*
207  * rw_intr is the interrupt routine for the device driver.
208  *
209  * It will be notified on the end of a SCSI read / write, and will take on
210  * of several actions based on success or failure.
211  */
212 static void rw_intr(struct scsi_cmnd * SCpnt)
213 {
214         int result = SCpnt->result;
215         int this_count = SCpnt->bufflen;
216         int good_bytes = (result == 0 ? this_count : 0);
217         int block_sectors = 0;
218         long error_sector;
219         struct scsi_cd *cd = scsi_cd(SCpnt->request->rq_disk);
220
221 #ifdef DEBUG
222         printk("sr.c done: %x\n", result);
223 #endif
224
225         /*
226          * Handle MEDIUM ERRORs or VOLUME OVERFLOWs that indicate partial
227          * success.  Since this is a relatively rare error condition, no
228          * care is taken to avoid unnecessary additional work such as
229          * memcpy's that could be avoided.
230          */
231         if (driver_byte(result) != 0 &&         /* An error occurred */
232             (SCpnt->sense_buffer[0] & 0x7f) == 0x70) { /* Sense current */
233                 switch (SCpnt->sense_buffer[2]) {
234                 case MEDIUM_ERROR:
235                 case VOLUME_OVERFLOW:
236                 case ILLEGAL_REQUEST:
237                         if (!(SCpnt->sense_buffer[0] & 0x90))
238                                 break;
239                         if (!blk_fs_request(SCpnt->request))
240                                 break;
241                         error_sector = (SCpnt->sense_buffer[3] << 24) |
242                                 (SCpnt->sense_buffer[4] << 16) |
243                                 (SCpnt->sense_buffer[5] << 8) |
244                                 SCpnt->sense_buffer[6];
245                         if (SCpnt->request->bio != NULL)
246                                 block_sectors =
247                                         bio_sectors(SCpnt->request->bio);
248                         if (block_sectors < 4)
249                                 block_sectors = 4;
250                         if (cd->device->sector_size == 2048)
251                                 error_sector <<= 2;
252                         error_sector &= ~(block_sectors - 1);
253                         good_bytes = (error_sector - SCpnt->request->sector) << 9;
254                         if (good_bytes < 0 || good_bytes >= this_count)
255                                 good_bytes = 0;
256                         /*
257                          * The SCSI specification allows for the value
258                          * returned by READ CAPACITY to be up to 75 2K
259                          * sectors past the last readable block.
260                          * Therefore, if we hit a medium error within the
261                          * last 75 2K sectors, we decrease the saved size
262                          * value.
263                          */
264                         if (error_sector < get_capacity(cd->disk) &&
265                             cd->capacity - error_sector < 4 * 75)
266                                 set_capacity(cd->disk, error_sector);
267                         break;
268
269                 case RECOVERED_ERROR:
270
271                         /*
272                          * An error occured, but it recovered.  Inform the
273                          * user, but make sure that it's not treated as a
274                          * hard error.
275                          */
276                         print_sense("sr", SCpnt);
277                         SCpnt->result = 0;
278                         SCpnt->sense_buffer[0] = 0x0;
279                         good_bytes = this_count;
280                         break;
281
282                 default:
283                         break;
284                 }
285         }
286
287         /*
288          * This calls the generic completion function, now that we know
289          * how many actual sectors finished, and how many sectors we need
290          * to say have failed.
291          */
292         scsi_io_completion(SCpnt, good_bytes, block_sectors << 9);
293 }
294
295 static int sr_init_command(struct scsi_cmnd * SCpnt)
296 {
297         int block=0, this_count, s_size, timeout = SR_TIMEOUT;
298         struct scsi_cd *cd = scsi_cd(SCpnt->request->rq_disk);
299
300         SCSI_LOG_HLQUEUE(1, printk("Doing sr request, dev = %s, block = %d\n",
301                                 cd->disk->disk_name, block));
302
303         if (!cd->device || !scsi_device_online(cd->device)) {
304                 SCSI_LOG_HLQUEUE(2, printk("Finishing %ld sectors\n",
305                                         SCpnt->request->nr_sectors));
306                 SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt));
307                 return 0;
308         }
309
310         if (cd->device->changed) {
311                 /*
312                  * quietly refuse to do anything to a changed disc until the
313                  * changed bit has been reset
314                  */
315                 return 0;
316         }
317
318         /*
319          * these are already setup, just copy cdb basically
320          */
321         if (SCpnt->request->flags & REQ_BLOCK_PC) {
322                 struct request *rq = SCpnt->request;
323
324                 if (sizeof(rq->cmd) > sizeof(SCpnt->cmnd))
325                         return 0;
326
327                 memcpy(SCpnt->cmnd, rq->cmd, sizeof(SCpnt->cmnd));
328                 if (!rq->data_len)
329                         SCpnt->sc_data_direction = SCSI_DATA_NONE;
330                 else if (rq_data_dir(rq) == WRITE)
331                         SCpnt->sc_data_direction = SCSI_DATA_WRITE;
332                 else
333                         SCpnt->sc_data_direction = SCSI_DATA_READ;
334
335                 this_count = rq->data_len;
336                 if (rq->timeout)
337                         timeout = rq->timeout;
338
339                 SCpnt->transfersize = rq->data_len;
340                 goto queue;
341         }
342
343         if (!(SCpnt->request->flags & REQ_CMD)) {
344                 blk_dump_rq_flags(SCpnt->request, "sr unsup command");
345                 return 0;
346         }
347
348         /*
349          * we do lazy blocksize switching (when reading XA sectors,
350          * see CDROMREADMODE2 ioctl) 
351          */
352         s_size = cd->device->sector_size;
353         if (s_size > 2048) {
354                 if (!in_interrupt())
355                         sr_set_blocklength(cd, 2048);
356                 else
357                         printk("sr: can't switch blocksize: in interrupt\n");
358         }
359
360         if (s_size != 512 && s_size != 1024 && s_size != 2048) {
361                 printk("sr: bad sector size %d\n", s_size);
362                 return 0;
363         }
364
365         if (rq_data_dir(SCpnt->request) == WRITE) {
366                 if (!cd->device->writeable)
367                         return 0;
368                 SCpnt->cmnd[0] = WRITE_10;
369                 SCpnt->sc_data_direction = SCSI_DATA_WRITE;
370         } else if (rq_data_dir(SCpnt->request) == READ) {
371                 SCpnt->cmnd[0] = READ_10;
372                 SCpnt->sc_data_direction = SCSI_DATA_READ;
373         } else {
374                 blk_dump_rq_flags(SCpnt->request, "Unknown sr command");
375                 return 0;
376         }
377
378         {
379                 struct scatterlist *sg = SCpnt->request_buffer;
380                 int i, size = 0;
381                 for (i = 0; i < SCpnt->use_sg; i++)
382                         size += sg[i].length;
383
384                 if (size != SCpnt->request_bufflen && SCpnt->use_sg) {
385                         printk(KERN_ERR "sr: mismatch count %d, bytes %d\n",
386                                         size, SCpnt->request_bufflen);
387                         if (SCpnt->request_bufflen > size)
388                                 SCpnt->request_bufflen = SCpnt->bufflen = size;
389                 }
390         }
391
392         /*
393          * request doesn't start on hw block boundary, add scatter pads
394          */
395         if (((unsigned int)SCpnt->request->sector % (s_size >> 9)) ||
396             (SCpnt->request_bufflen % s_size)) {
397                 printk("sr: unaligned transfer\n");
398                 return 0;
399         }
400
401         this_count = (SCpnt->request_bufflen >> 9) / (s_size >> 9);
402
403
404         SCSI_LOG_HLQUEUE(2, printk("%s : %s %d/%ld 512 byte blocks.\n",
405                                 cd->cdi.name,
406                                 (rq_data_dir(SCpnt->request) == WRITE) ?
407                                         "writing" : "reading",
408                                 this_count, SCpnt->request->nr_sectors));
409
410         SCpnt->cmnd[1] = 0;
411         block = (unsigned int)SCpnt->request->sector / (s_size >> 9);
412
413         if (this_count > 0xffff) {
414                 this_count = 0xffff;
415                 SCpnt->request_bufflen = SCpnt->bufflen =
416                                 this_count * s_size;
417         }
418
419         SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
420         SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
421         SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
422         SCpnt->cmnd[5] = (unsigned char) block & 0xff;
423         SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
424         SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
425         SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
426
427         /*
428          * We shouldn't disconnect in the middle of a sector, so with a dumb
429          * host adapter, it's safe to assume that we can at least transfer
430          * this many bytes between each connect / disconnect.
431          */
432         SCpnt->transfersize = cd->device->sector_size;
433         SCpnt->underflow = this_count << 9;
434
435 queue:
436         SCpnt->allowed = MAX_RETRIES;
437         SCpnt->timeout_per_command = timeout;
438
439         /*
440          * This is the completion routine we use.  This is matched in terms
441          * of capability to this function.
442          */
443         SCpnt->done = rw_intr;
444
445         /*
446          * This indicates that the command is ready from our end to be
447          * queued.
448          */
449         return 1;
450 }
451
452 static int sr_block_open(struct inode *inode, struct file *file)
453 {
454         struct gendisk *disk = inode->i_bdev->bd_disk;
455         struct scsi_cd *cd = scsi_cd(inode->i_bdev->bd_disk);
456         int ret = 0;
457
458         if(!(cd = scsi_cd_get(disk)))
459                 return -ENXIO;
460
461         if((ret = cdrom_open(&cd->cdi, inode, file)) != 0)
462                 scsi_cd_put(cd);
463
464         return ret;
465 }
466
467 static int sr_block_release(struct inode *inode, struct file *file)
468 {
469         int ret;
470         struct scsi_cd *cd = scsi_cd(inode->i_bdev->bd_disk);
471         ret = cdrom_release(&cd->cdi, file);
472         if(ret)
473                 return ret;
474         
475         scsi_cd_put(cd);
476
477         return 0;
478 }
479
480 static int sr_block_ioctl(struct inode *inode, struct file *file, unsigned cmd,
481                           unsigned long arg)
482 {
483         struct scsi_cd *cd = scsi_cd(inode->i_bdev->bd_disk);
484         struct scsi_device *sdev = cd->device;
485
486         /*
487          * Send SCSI addressing ioctls directly to mid level, send other
488          * ioctls to cdrom/block level.
489          */
490         switch (cmd) {
491                 case SCSI_IOCTL_GET_IDLUN:
492                 case SCSI_IOCTL_GET_BUS_NUMBER:
493                         return scsi_ioctl(sdev, cmd, (void *)arg);
494         }
495         return cdrom_ioctl(&cd->cdi, inode, cmd, arg);
496 }
497
498 static int sr_block_media_changed(struct gendisk *disk)
499 {
500         struct scsi_cd *cd = scsi_cd(disk);
501         return cdrom_media_changed(&cd->cdi);
502 }
503
504 struct block_device_operations sr_bdops =
505 {
506         .owner          = THIS_MODULE,
507         .open           = sr_block_open,
508         .release        = sr_block_release,
509         .ioctl          = sr_block_ioctl,
510         .media_changed  = sr_block_media_changed,
511 };
512
513 static int sr_open(struct cdrom_device_info *cdi, int purpose)
514 {
515         struct scsi_cd *cd = cdi->handle;
516         struct scsi_device *sdev = cd->device;
517         int retval;
518
519         /*
520          * If the device is in error recovery, wait until it is done.
521          * If the device is offline, then disallow any access to it.
522          */
523         retval = -ENXIO;
524         if (!scsi_block_when_processing_errors(sdev))
525                 goto error_out;
526
527         /*
528          * If this device did not have media in the drive at boot time, then
529          * we would have been unable to get the sector size.  Check to see if
530          * this is the case, and try again.
531          */
532         if (cd->needs_sector_size)
533                 get_sectorsize(cd);
534         return 0;
535
536 error_out:
537         scsi_cd_put(cd);
538         return retval;  
539 }
540
541 static void sr_release(struct cdrom_device_info *cdi)
542 {
543         struct scsi_cd *cd = cdi->handle;
544
545         if (cd->device->sector_size > 2048)
546                 sr_set_blocklength(cd, 2048);
547
548 }
549
550 static int sr_probe(struct device *dev)
551 {
552         struct scsi_device *sdev = to_scsi_device(dev);
553         struct gendisk *disk;
554         struct scsi_cd *cd;
555         int minor, error;
556
557         error = -ENODEV;
558         if (sdev->type != TYPE_ROM && sdev->type != TYPE_WORM)
559                 goto fail;
560
561         if ((error = scsi_device_get(sdev)) != 0)
562                 goto fail;
563
564         error = -ENOMEM;
565         cd = kmalloc(sizeof(*cd), GFP_KERNEL);
566         if (!cd)
567                 goto fail_put_sdev;
568         memset(cd, 0, sizeof(*cd));
569
570         kref_init(&cd->kref, sr_kref_release);
571
572         disk = alloc_disk(1);
573         if (!disk)
574                 goto fail_free;
575
576         spin_lock(&sr_index_lock);
577         minor = find_first_zero_bit(sr_index_bits, SR_DISKS);
578         if (minor == SR_DISKS) {
579                 spin_unlock(&sr_index_lock);
580                 error = -EBUSY;
581                 goto fail_put;
582         }
583         __set_bit(minor, sr_index_bits);
584         spin_unlock(&sr_index_lock);
585
586         disk->major = SCSI_CDROM_MAJOR;
587         disk->first_minor = minor;
588         sprintf(disk->disk_name, "sr%d", minor);
589         disk->fops = &sr_bdops;
590         disk->flags = GENHD_FL_CD;
591
592         cd->device = sdev;
593         cd->disk = disk;
594         cd->driver = &sr_template;
595         cd->disk = disk;
596         cd->capacity = 0x1fffff;
597         cd->needs_sector_size = 1;
598         cd->device->changed = 1;        /* force recheck CD type */
599         cd->use = 1;
600         cd->readcd_known = 0;
601         cd->readcd_cdda = 0;
602
603         cd->cdi.ops = &sr_dops;
604         cd->cdi.handle = cd;
605         cd->cdi.mask = 0;
606         cd->cdi.capacity = 1;
607         sprintf(cd->cdi.name, "sr%d", minor);
608
609         sdev->sector_size = 2048;       /* A guess, just in case */
610
611         /* FIXME: need to handle a get_capabilities failure properly ?? */
612         get_capabilities(cd);
613         sr_vendor_init(cd);
614
615         snprintf(disk->devfs_name, sizeof(disk->devfs_name),
616                         "%s/cd", sdev->devfs_name);
617         disk->driverfs_dev = &sdev->sdev_gendev;
618         set_capacity(disk, cd->capacity);
619         disk->private_data = &cd->driver;
620         disk->queue = sdev->request_queue;
621         cd->cdi.disk = disk;
622
623         if (register_cdrom(&cd->cdi))
624                 goto fail_put;
625
626         dev_set_drvdata(dev, cd);
627         disk->flags |= GENHD_FL_REMOVABLE;
628         add_disk(disk);
629
630         printk(KERN_DEBUG
631             "Attached scsi CD-ROM %s at scsi%d, channel %d, id %d, lun %d\n",
632             cd->cdi.name, sdev->host->host_no, sdev->channel,
633             sdev->id, sdev->lun);
634         return 0;
635
636 fail_put:
637         put_disk(disk);
638 fail_free:
639         kfree(cd);
640 fail_put_sdev:
641         scsi_device_put(sdev);
642 fail:
643         return error;
644 }
645
646
647 static void get_sectorsize(struct scsi_cd *cd)
648 {
649         unsigned char cmd[10];
650         unsigned char *buffer;
651         int the_result, retries = 3;
652         int sector_size;
653         struct scsi_request *SRpnt = NULL;
654         request_queue_t *queue;
655
656         buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
657         if (!buffer)
658                 goto Enomem;
659         SRpnt = scsi_allocate_request(cd->device, GFP_KERNEL);
660         if (!SRpnt)
661                 goto Enomem;
662
663         do {
664                 cmd[0] = READ_CAPACITY;
665                 memset((void *) &cmd[1], 0, 9);
666                 /* Mark as really busy */
667                 SRpnt->sr_request->rq_status = RQ_SCSI_BUSY;
668                 SRpnt->sr_cmd_len = 0;
669
670                 memset(buffer, 0, 8);
671
672                 /* Do the command and wait.. */
673                 SRpnt->sr_data_direction = SCSI_DATA_READ;
674                 scsi_wait_req(SRpnt, (void *) cmd, (void *) buffer,
675                               8, SR_TIMEOUT, MAX_RETRIES);
676
677                 the_result = SRpnt->sr_result;
678                 retries--;
679
680         } while (the_result && retries);
681
682
683         scsi_release_request(SRpnt);
684         SRpnt = NULL;
685
686         if (the_result) {
687                 cd->capacity = 0x1fffff;
688                 sector_size = 2048;     /* A guess, just in case */
689                 cd->needs_sector_size = 1;
690         } else {
691 #if 0
692                 if (cdrom_get_last_written(&cd->cdi,
693                                            &cd->capacity))
694 #endif
695                         cd->capacity = 1 + ((buffer[0] << 24) |
696                                                     (buffer[1] << 16) |
697                                                     (buffer[2] << 8) |
698                                                     buffer[3]);
699                 sector_size = (buffer[4] << 24) |
700                     (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
701                 switch (sector_size) {
702                         /*
703                          * HP 4020i CD-Recorder reports 2340 byte sectors
704                          * Philips CD-Writers report 2352 byte sectors
705                          *
706                          * Use 2k sectors for them..
707                          */
708                 case 0:
709                 case 2340:
710                 case 2352:
711                         sector_size = 2048;
712                         /* fall through */
713                 case 2048:
714                         cd->capacity *= 4;
715                         /* fall through */
716                 case 512:
717                         break;
718                 default:
719                         printk("%s: unsupported sector size %d.\n",
720                                cd->cdi.name, sector_size);
721                         cd->capacity = 0;
722                         cd->needs_sector_size = 1;
723                 }
724
725                 cd->device->sector_size = sector_size;
726
727                 /*
728                  * Add this so that we have the ability to correctly gauge
729                  * what the device is capable of.
730                  */
731                 cd->needs_sector_size = 0;
732                 set_capacity(cd->disk, cd->capacity);
733         }
734
735         queue = cd->device->request_queue;
736         blk_queue_hardsect_size(queue, sector_size);
737 out:
738         kfree(buffer);
739         return;
740
741 Enomem:
742         cd->capacity = 0x1fffff;
743         sector_size = 2048;     /* A guess, just in case */
744         cd->needs_sector_size = 1;
745         if (SRpnt)
746                 scsi_release_request(SRpnt);
747         goto out;
748 }
749
750 static void get_capabilities(struct scsi_cd *cd)
751 {
752         unsigned char *buffer;
753         int rc, n, mrw_write = 0, mrw = 1,ram_write=0;
754         struct scsi_mode_data data;
755         struct scsi_request *SRpnt;
756         unsigned char cmd[MAX_COMMAND_SIZE];
757         unsigned int the_result;
758         int retries;
759
760         static char *loadmech[] =
761         {
762                 "caddy",
763                 "tray",
764                 "pop-up",
765                 "",
766                 "changer",
767                 "cartridge changer",
768                 "",
769                 ""
770         };
771
772         /* Set read only initially */
773         set_disk_ro(cd->disk, 1);
774
775         /* allocate a request for the TEST_UNIT_READY */
776         SRpnt = scsi_allocate_request(cd->device, GFP_KERNEL);
777         if (!SRpnt) {
778                 printk(KERN_WARNING "(get_capabilities:) Request allocation "
779                        "failure.\n");
780                 return;
781         }
782
783         /* allocate transfer buffer */
784         buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
785         if (!buffer) {
786                 printk(KERN_ERR "sr: out of memory.\n");
787                 scsi_release_request(SRpnt);
788                 return;
789         }
790
791         /* issue TEST_UNIT_READY until the initial startup UNIT_ATTENTION
792          * conditions are gone, or a timeout happens
793          */
794         retries = 0;
795         do {
796                 memset((void *)cmd, 0, MAX_COMMAND_SIZE);
797                 cmd[0] = TEST_UNIT_READY;
798
799                 SRpnt->sr_cmd_len = 0;
800                 SRpnt->sr_sense_buffer[0] = 0;
801                 SRpnt->sr_sense_buffer[2] = 0;
802                 SRpnt->sr_data_direction = DMA_NONE;
803
804                 scsi_wait_req (SRpnt, (void *) cmd, buffer,
805                                0, SR_TIMEOUT, MAX_RETRIES);
806
807                 the_result = SRpnt->sr_result;
808                 retries++;
809         } while (retries < 5 && 
810                  (!scsi_status_is_good(the_result) ||
811                   ((driver_byte(the_result) & DRIVER_SENSE) &&
812                    SRpnt->sr_sense_buffer[2] == UNIT_ATTENTION)));
813
814         /* ask for mode page 0x2a */
815         rc = scsi_mode_sense(cd->device, 0, 0x2a, buffer, 128,
816                              SR_TIMEOUT, 3, &data);
817
818         if (!scsi_status_is_good(rc)) {
819                 /* failed, drive doesn't have capabilities mode page */
820                 cd->cdi.speed = 1;
821                 cd->cdi.mask |= (CDC_CD_R | CDC_CD_RW | CDC_DVD_R |
822                                          CDC_DVD | CDC_DVD_RAM |
823                                          CDC_SELECT_DISC | CDC_SELECT_SPEED);
824                 scsi_release_request(SRpnt);
825                 kfree(buffer);
826                 printk("%s: scsi-1 drive\n", cd->cdi.name);
827                 return;
828         }
829
830         if (cdrom_is_mrw(&cd->cdi, &mrw_write)) {
831                 mrw = 0;
832                 cd->cdi.mask |= CDC_MRW;
833                 cd->cdi.mask |= CDC_MRW_W;
834         }
835         if (!mrw_write)
836                 cd->cdi.mask |= CDC_MRW_W;
837
838         if (cdrom_is_random_writable(&cd->cdi, &ram_write))
839                 cd->cdi.mask |= CDC_RAM;
840         if (!ram_write)
841                 cd->cdi.mask |= CDC_RAM;
842
843         n = data.header_length + data.block_descriptor_length;
844         cd->cdi.speed = ((buffer[n + 8] << 8) + buffer[n + 9]) / 176;
845         cd->readcd_known = 1;
846         cd->readcd_cdda = buffer[n + 5] & 0x01;
847         /* print some capability bits */
848         printk("%s: scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n", cd->cdi.name,
849                ((buffer[n + 14] << 8) + buffer[n + 15]) / 176,
850                cd->cdi.speed,
851                buffer[n + 3] & 0x01 ? "writer " : "", /* CD Writer */
852                buffer[n + 3] & 0x20 ? "dvd-ram " : "",
853                buffer[n + 2] & 0x02 ? "cd/rw " : "", /* can read rewriteable */
854                buffer[n + 4] & 0x20 ? "xa/form2 " : "", /* can read xa/from2 */
855                buffer[n + 5] & 0x01 ? "cdda " : "", /* can read audio data */
856                loadmech[buffer[n + 6] >> 5]);
857         if ((buffer[n + 6] >> 5) == 0)
858                 /* caddy drives can't close tray... */
859                 cd->cdi.mask |= CDC_CLOSE_TRAY;
860         if ((buffer[n + 2] & 0x8) == 0)
861                 /* not a DVD drive */
862                 cd->cdi.mask |= CDC_DVD;
863         if ((buffer[n + 3] & 0x20) == 0) 
864                 /* can't write DVD-RAM media */
865                 cd->cdi.mask |= CDC_DVD_RAM;
866         if ((buffer[n + 3] & 0x10) == 0)
867                 /* can't write DVD-R media */
868                 cd->cdi.mask |= CDC_DVD_R;
869         if ((buffer[n + 3] & 0x2) == 0)
870                 /* can't write CD-RW media */
871                 cd->cdi.mask |= CDC_CD_RW;
872         if ((buffer[n + 3] & 0x1) == 0)
873                 /* can't write CD-R media */
874                 cd->cdi.mask |= CDC_CD_R;
875         if ((buffer[n + 6] & 0x8) == 0)
876                 /* can't eject */
877                 cd->cdi.mask |= CDC_OPEN_TRAY;
878
879         if ((buffer[n + 6] >> 5) == mechtype_individual_changer ||
880             (buffer[n + 6] >> 5) == mechtype_cartridge_changer)
881                 cd->cdi.capacity =
882                     cdrom_number_of_slots(&cd->cdi);
883         if (cd->cdi.capacity <= 1)
884                 /* not a changer */
885                 cd->cdi.mask |= CDC_SELECT_DISC;
886         /*else    I don't think it can close its tray
887                 cd->cdi.mask |= CDC_CLOSE_TRAY; */
888
889         /*
890          * if DVD-RAM of MRW-W, we are randomly writeable
891          */
892         if ((cd->cdi.mask & (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM)) !=
893                         (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM)) {
894                 cd->device->writeable = 1;
895                 set_disk_ro(cd->disk, 0);
896         }
897
898         scsi_release_request(SRpnt);
899         kfree(buffer);
900 }
901
902 /*
903  * sr_packet() is the entry point for the generic commands generated
904  * by the Uniform CD-ROM layer. 
905  */
906 static int sr_packet(struct cdrom_device_info *cdi,
907                 struct cdrom_generic_command *cgc)
908 {
909         if (cgc->timeout <= 0)
910                 cgc->timeout = IOCTL_TIMEOUT;
911
912         sr_do_ioctl(cdi->handle, cgc);
913
914         return cgc->stat;
915 }
916
917 /**
918  *      sr_kref_release - Called to free the scsi_cd structure
919  *      @kref: pointer to embedded kref
920  *
921  *      sr_ref_sem must be held entering this routine.  Because it is
922  *      called on last put, you should always use the scsi_cd_get()
923  *      scsi_cd_put() helpers which manipulate the semaphore directly
924  *      and never do a direct kref_put().
925  **/
926 static void sr_kref_release(struct kref *kref)
927 {
928         struct scsi_cd *cd = container_of(kref, struct scsi_cd, kref);
929         struct scsi_device *sdev = cd->device;
930         struct gendisk *disk = cd->disk;
931
932         spin_lock(&sr_index_lock);
933         clear_bit(disk->first_minor, sr_index_bits);
934         spin_unlock(&sr_index_lock);
935
936         unregister_cdrom(&cd->cdi);
937
938         disk->private_data = NULL;
939
940         put_disk(disk);
941
942         kfree(cd);
943
944         scsi_device_put(sdev);
945 }
946
947 static int sr_remove(struct device *dev)
948 {
949         struct scsi_cd *cd = dev_get_drvdata(dev);
950
951         del_gendisk(cd->disk);
952
953         scsi_cd_put(cd);
954
955         return 0;
956 }
957
958 static int __init init_sr(void)
959 {
960         int rc;
961
962         rc = register_blkdev(SCSI_CDROM_MAJOR, "sr");
963         if (rc)
964                 return rc;
965         return scsi_register_driver(&sr_template.gendrv);
966 }
967
968 static void __exit exit_sr(void)
969 {
970         scsi_unregister_driver(&sr_template.gendrv);
971         unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
972 }
973
974 module_init(init_sr);
975 module_exit(exit_sr);
976 MODULE_LICENSE("GPL");