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