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