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