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