]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/scsi/sr.c
[SCSI] aic7xxx: fix timer handling bug
[net-next-2.6.git] / drivers / scsi / sr.c
CommitLineData
1da177e4
LT
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>
820732b5 53#include <scsi/scsi_cmnd.h>
1da177e4
LT
54#include <scsi/scsi_eh.h>
55#include <scsi/scsi_host.h>
56#include <scsi/scsi_ioctl.h> /* For the door lock/unlock commands */
1da177e4
LT
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
73static int sr_probe(struct device *);
74static int sr_remove(struct device *);
75static int sr_init_command(struct scsi_cmnd *);
76
77static 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
87static unsigned long sr_index_bits[SR_DISKS / BITS_PER_LONG];
88static 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) */
93static DECLARE_MUTEX(sr_ref_sem);
94
95static int sr_open(struct cdrom_device_info *, int);
96static void sr_release(struct cdrom_device_info *);
97
98static void get_sectorsize(struct scsi_cd *);
99static void get_capabilities(struct scsi_cd *);
100
101static int sr_media_change(struct cdrom_device_info *, int);
102static int sr_packet(struct cdrom_device_info *, struct packet_command *);
103
104static 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
121static void sr_kref_release(struct kref *kref);
122
123static 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 */
132static 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
153static 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
173int 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
51490c89 202 get_sectorsize(cd);
1da177e4
LT
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 */
213static 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
296static 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) {
7b16318d 323 scsi_setup_blk_pc_cmnd(SCpnt);
1da177e4 324
c9526497
JB
325 if (SCpnt->timeout_per_command)
326 timeout = SCpnt->timeout_per_command;
1da177e4 327
1da177e4
LT
328 goto queue;
329 }
330
331 if (!(SCpnt->request->flags & REQ_CMD)) {
332 blk_dump_rq_flags(SCpnt->request, "sr unsup command");
333 return 0;
334 }
335
336 /*
337 * we do lazy blocksize switching (when reading XA sectors,
338 * see CDROMREADMODE2 ioctl)
339 */
340 s_size = cd->device->sector_size;
341 if (s_size > 2048) {
342 if (!in_interrupt())
343 sr_set_blocklength(cd, 2048);
344 else
345 printk("sr: can't switch blocksize: in interrupt\n");
346 }
347
348 if (s_size != 512 && s_size != 1024 && s_size != 2048) {
3bf743e7 349 scmd_printk(KERN_ERR, SCpnt, "bad sector size %d\n", s_size);
1da177e4
LT
350 return 0;
351 }
352
353 if (rq_data_dir(SCpnt->request) == WRITE) {
354 if (!cd->device->writeable)
355 return 0;
356 SCpnt->cmnd[0] = WRITE_10;
357 SCpnt->sc_data_direction = DMA_TO_DEVICE;
358 cd->cdi.media_written = 1;
359 } else if (rq_data_dir(SCpnt->request) == READ) {
360 SCpnt->cmnd[0] = READ_10;
361 SCpnt->sc_data_direction = DMA_FROM_DEVICE;
362 } else {
363 blk_dump_rq_flags(SCpnt->request, "Unknown sr command");
364 return 0;
365 }
366
367 {
368 struct scatterlist *sg = SCpnt->request_buffer;
369 int i, size = 0;
370 for (i = 0; i < SCpnt->use_sg; i++)
371 size += sg[i].length;
372
373 if (size != SCpnt->request_bufflen && SCpnt->use_sg) {
3bf743e7
JG
374 scmd_printk(KERN_ERR, SCpnt,
375 "mismatch count %d, bytes %d\n",
376 size, SCpnt->request_bufflen);
1da177e4
LT
377 if (SCpnt->request_bufflen > size)
378 SCpnt->request_bufflen = SCpnt->bufflen = size;
379 }
380 }
381
382 /*
383 * request doesn't start on hw block boundary, add scatter pads
384 */
385 if (((unsigned int)SCpnt->request->sector % (s_size >> 9)) ||
386 (SCpnt->request_bufflen % s_size)) {
3bf743e7 387 scmd_printk(KERN_NOTICE, SCpnt, "unaligned transfer\n");
1da177e4
LT
388 return 0;
389 }
390
391 this_count = (SCpnt->request_bufflen >> 9) / (s_size >> 9);
392
393
394 SCSI_LOG_HLQUEUE(2, printk("%s : %s %d/%ld 512 byte blocks.\n",
395 cd->cdi.name,
396 (rq_data_dir(SCpnt->request) == WRITE) ?
397 "writing" : "reading",
398 this_count, SCpnt->request->nr_sectors));
399
400 SCpnt->cmnd[1] = 0;
401 block = (unsigned int)SCpnt->request->sector / (s_size >> 9);
402
403 if (this_count > 0xffff) {
404 this_count = 0xffff;
405 SCpnt->request_bufflen = SCpnt->bufflen =
406 this_count * s_size;
407 }
408
409 SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
410 SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
411 SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
412 SCpnt->cmnd[5] = (unsigned char) block & 0xff;
413 SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
414 SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
415 SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
416
417 /*
418 * We shouldn't disconnect in the middle of a sector, so with a dumb
419 * host adapter, it's safe to assume that we can at least transfer
420 * this many bytes between each connect / disconnect.
421 */
422 SCpnt->transfersize = cd->device->sector_size;
423 SCpnt->underflow = this_count << 9;
424
425queue:
426 SCpnt->allowed = MAX_RETRIES;
427 SCpnt->timeout_per_command = timeout;
428
429 /*
430 * This is the completion routine we use. This is matched in terms
431 * of capability to this function.
432 */
433 SCpnt->done = rw_intr;
434
435 /*
436 * This indicates that the command is ready from our end to be
437 * queued.
438 */
439 return 1;
440}
441
442static int sr_block_open(struct inode *inode, struct file *file)
443{
444 struct gendisk *disk = inode->i_bdev->bd_disk;
80d904c4 445 struct scsi_cd *cd;
1da177e4
LT
446 int ret = 0;
447
448 if(!(cd = scsi_cd_get(disk)))
449 return -ENXIO;
450
451 if((ret = cdrom_open(&cd->cdi, inode, file)) != 0)
452 scsi_cd_put(cd);
453
454 return ret;
455}
456
457static int sr_block_release(struct inode *inode, struct file *file)
458{
459 int ret;
460 struct scsi_cd *cd = scsi_cd(inode->i_bdev->bd_disk);
461 ret = cdrom_release(&cd->cdi, file);
462 if(ret)
463 return ret;
464
465 scsi_cd_put(cd);
466
467 return 0;
468}
469
470static int sr_block_ioctl(struct inode *inode, struct file *file, unsigned cmd,
471 unsigned long arg)
472{
473 struct scsi_cd *cd = scsi_cd(inode->i_bdev->bd_disk);
474 struct scsi_device *sdev = cd->device;
475
476 /*
477 * Send SCSI addressing ioctls directly to mid level, send other
478 * ioctls to cdrom/block level.
479 */
480 switch (cmd) {
481 case SCSI_IOCTL_GET_IDLUN:
482 case SCSI_IOCTL_GET_BUS_NUMBER:
483 return scsi_ioctl(sdev, cmd, (void __user *)arg);
484 }
485 return cdrom_ioctl(file, &cd->cdi, inode, cmd, arg);
486}
487
488static int sr_block_media_changed(struct gendisk *disk)
489{
490 struct scsi_cd *cd = scsi_cd(disk);
491 return cdrom_media_changed(&cd->cdi);
492}
493
494static struct block_device_operations sr_bdops =
495{
496 .owner = THIS_MODULE,
497 .open = sr_block_open,
498 .release = sr_block_release,
499 .ioctl = sr_block_ioctl,
500 .media_changed = sr_block_media_changed,
501 /*
502 * No compat_ioctl for now because sr_block_ioctl never
503 * seems to pass arbitary ioctls down to host drivers.
504 */
505};
506
507static int sr_open(struct cdrom_device_info *cdi, int purpose)
508{
509 struct scsi_cd *cd = cdi->handle;
510 struct scsi_device *sdev = cd->device;
511 int retval;
512
513 /*
514 * If the device is in error recovery, wait until it is done.
515 * If the device is offline, then disallow any access to it.
516 */
517 retval = -ENXIO;
518 if (!scsi_block_when_processing_errors(sdev))
519 goto error_out;
520
1da177e4
LT
521 return 0;
522
523error_out:
524 return retval;
525}
526
527static void sr_release(struct cdrom_device_info *cdi)
528{
529 struct scsi_cd *cd = cdi->handle;
530
531 if (cd->device->sector_size > 2048)
532 sr_set_blocklength(cd, 2048);
533
534}
535
536static int sr_probe(struct device *dev)
537{
538 struct scsi_device *sdev = to_scsi_device(dev);
539 struct gendisk *disk;
540 struct scsi_cd *cd;
541 int minor, error;
542
543 error = -ENODEV;
544 if (sdev->type != TYPE_ROM && sdev->type != TYPE_WORM)
545 goto fail;
546
547 error = -ENOMEM;
548 cd = kmalloc(sizeof(*cd), GFP_KERNEL);
549 if (!cd)
550 goto fail;
551 memset(cd, 0, sizeof(*cd));
552
553 kref_init(&cd->kref);
554
555 disk = alloc_disk(1);
556 if (!disk)
557 goto fail_free;
558
559 spin_lock(&sr_index_lock);
560 minor = find_first_zero_bit(sr_index_bits, SR_DISKS);
561 if (minor == SR_DISKS) {
562 spin_unlock(&sr_index_lock);
563 error = -EBUSY;
564 goto fail_put;
565 }
566 __set_bit(minor, sr_index_bits);
567 spin_unlock(&sr_index_lock);
568
569 disk->major = SCSI_CDROM_MAJOR;
570 disk->first_minor = minor;
571 sprintf(disk->disk_name, "sr%d", minor);
572 disk->fops = &sr_bdops;
573 disk->flags = GENHD_FL_CD;
574
575 cd->device = sdev;
576 cd->disk = disk;
577 cd->driver = &sr_template;
578 cd->disk = disk;
579 cd->capacity = 0x1fffff;
1da177e4
LT
580 cd->device->changed = 1; /* force recheck CD type */
581 cd->use = 1;
582 cd->readcd_known = 0;
583 cd->readcd_cdda = 0;
584
585 cd->cdi.ops = &sr_dops;
586 cd->cdi.handle = cd;
587 cd->cdi.mask = 0;
588 cd->cdi.capacity = 1;
589 sprintf(cd->cdi.name, "sr%d", minor);
590
591 sdev->sector_size = 2048; /* A guess, just in case */
592
593 /* FIXME: need to handle a get_capabilities failure properly ?? */
594 get_capabilities(cd);
595 sr_vendor_init(cd);
596
597 snprintf(disk->devfs_name, sizeof(disk->devfs_name),
598 "%s/cd", sdev->devfs_name);
599 disk->driverfs_dev = &sdev->sdev_gendev;
600 set_capacity(disk, cd->capacity);
601 disk->private_data = &cd->driver;
602 disk->queue = sdev->request_queue;
603 cd->cdi.disk = disk;
604
605 if (register_cdrom(&cd->cdi))
606 goto fail_put;
607
608 dev_set_drvdata(dev, cd);
609 disk->flags |= GENHD_FL_REMOVABLE;
610 add_disk(disk);
611
9ccfc756
JB
612 sdev_printk(KERN_DEBUG, sdev,
613 "Attached scsi CD-ROM %s\n", cd->cdi.name);
1da177e4
LT
614 return 0;
615
616fail_put:
617 put_disk(disk);
618fail_free:
619 kfree(cd);
620fail:
621 return error;
622}
623
624
625static void get_sectorsize(struct scsi_cd *cd)
626{
627 unsigned char cmd[10];
628 unsigned char *buffer;
629 int the_result, retries = 3;
630 int sector_size;
1da177e4
LT
631 request_queue_t *queue;
632
633 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
634 if (!buffer)
635 goto Enomem;
1da177e4
LT
636
637 do {
638 cmd[0] = READ_CAPACITY;
639 memset((void *) &cmd[1], 0, 9);
1da177e4
LT
640 memset(buffer, 0, 8);
641
642 /* Do the command and wait.. */
820732b5
JB
643 the_result = scsi_execute_req(cd->device, cmd, DMA_FROM_DEVICE,
644 buffer, 8, NULL, SR_TIMEOUT,
645 MAX_RETRIES);
1da177e4 646
1da177e4
LT
647 retries--;
648
649 } while (the_result && retries);
650
651
1da177e4
LT
652 if (the_result) {
653 cd->capacity = 0x1fffff;
654 sector_size = 2048; /* A guess, just in case */
1da177e4
LT
655 } else {
656#if 0
657 if (cdrom_get_last_written(&cd->cdi,
658 &cd->capacity))
659#endif
660 cd->capacity = 1 + ((buffer[0] << 24) |
661 (buffer[1] << 16) |
662 (buffer[2] << 8) |
663 buffer[3]);
664 sector_size = (buffer[4] << 24) |
665 (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
666 switch (sector_size) {
667 /*
668 * HP 4020i CD-Recorder reports 2340 byte sectors
669 * Philips CD-Writers report 2352 byte sectors
670 *
671 * Use 2k sectors for them..
672 */
673 case 0:
674 case 2340:
675 case 2352:
676 sector_size = 2048;
677 /* fall through */
678 case 2048:
679 cd->capacity *= 4;
680 /* fall through */
681 case 512:
682 break;
683 default:
684 printk("%s: unsupported sector size %d.\n",
685 cd->cdi.name, sector_size);
686 cd->capacity = 0;
1da177e4
LT
687 }
688
689 cd->device->sector_size = sector_size;
690
691 /*
692 * Add this so that we have the ability to correctly gauge
693 * what the device is capable of.
694 */
1da177e4
LT
695 set_capacity(cd->disk, cd->capacity);
696 }
697
698 queue = cd->device->request_queue;
699 blk_queue_hardsect_size(queue, sector_size);
700out:
701 kfree(buffer);
702 return;
703
704Enomem:
705 cd->capacity = 0x1fffff;
51490c89 706 cd->device->sector_size = 2048; /* A guess, just in case */
1da177e4
LT
707 goto out;
708}
709
710static void get_capabilities(struct scsi_cd *cd)
711{
712 unsigned char *buffer;
713 struct scsi_mode_data data;
1da177e4 714 unsigned char cmd[MAX_COMMAND_SIZE];
820732b5 715 struct scsi_sense_hdr sshdr;
1da177e4
LT
716 unsigned int the_result;
717 int retries, rc, n;
718
0ad78200 719 static const char *loadmech[] =
1da177e4
LT
720 {
721 "caddy",
722 "tray",
723 "pop-up",
724 "",
725 "changer",
726 "cartridge changer",
727 "",
728 ""
729 };
730
1da177e4
LT
731
732 /* allocate transfer buffer */
733 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
734 if (!buffer) {
735 printk(KERN_ERR "sr: out of memory.\n");
1da177e4
LT
736 return;
737 }
738
739 /* issue TEST_UNIT_READY until the initial startup UNIT_ATTENTION
740 * conditions are gone, or a timeout happens
741 */
742 retries = 0;
743 do {
744 memset((void *)cmd, 0, MAX_COMMAND_SIZE);
745 cmd[0] = TEST_UNIT_READY;
746
820732b5
JB
747 the_result = scsi_execute_req (cd->device, cmd, DMA_NONE, NULL,
748 0, &sshdr, SR_TIMEOUT,
749 MAX_RETRIES);
1da177e4 750
1da177e4
LT
751 retries++;
752 } while (retries < 5 &&
753 (!scsi_status_is_good(the_result) ||
820732b5
JB
754 (scsi_sense_valid(&sshdr) &&
755 sshdr.sense_key == UNIT_ATTENTION)));
1da177e4
LT
756
757 /* ask for mode page 0x2a */
758 rc = scsi_mode_sense(cd->device, 0, 0x2a, buffer, 128,
1cf72699 759 SR_TIMEOUT, 3, &data, NULL);
1da177e4
LT
760
761 if (!scsi_status_is_good(rc)) {
762 /* failed, drive doesn't have capabilities mode page */
763 cd->cdi.speed = 1;
764 cd->cdi.mask |= (CDC_CD_R | CDC_CD_RW | CDC_DVD_R |
765 CDC_DVD | CDC_DVD_RAM |
766 CDC_SELECT_DISC | CDC_SELECT_SPEED);
1da177e4
LT
767 kfree(buffer);
768 printk("%s: scsi-1 drive\n", cd->cdi.name);
769 return;
770 }
771
772 n = data.header_length + data.block_descriptor_length;
773 cd->cdi.speed = ((buffer[n + 8] << 8) + buffer[n + 9]) / 176;
774 cd->readcd_known = 1;
775 cd->readcd_cdda = buffer[n + 5] & 0x01;
776 /* print some capability bits */
777 printk("%s: scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n", cd->cdi.name,
778 ((buffer[n + 14] << 8) + buffer[n + 15]) / 176,
779 cd->cdi.speed,
780 buffer[n + 3] & 0x01 ? "writer " : "", /* CD Writer */
781 buffer[n + 3] & 0x20 ? "dvd-ram " : "",
782 buffer[n + 2] & 0x02 ? "cd/rw " : "", /* can read rewriteable */
783 buffer[n + 4] & 0x20 ? "xa/form2 " : "", /* can read xa/from2 */
784 buffer[n + 5] & 0x01 ? "cdda " : "", /* can read audio data */
785 loadmech[buffer[n + 6] >> 5]);
786 if ((buffer[n + 6] >> 5) == 0)
787 /* caddy drives can't close tray... */
788 cd->cdi.mask |= CDC_CLOSE_TRAY;
789 if ((buffer[n + 2] & 0x8) == 0)
790 /* not a DVD drive */
791 cd->cdi.mask |= CDC_DVD;
792 if ((buffer[n + 3] & 0x20) == 0)
793 /* can't write DVD-RAM media */
794 cd->cdi.mask |= CDC_DVD_RAM;
795 if ((buffer[n + 3] & 0x10) == 0)
796 /* can't write DVD-R media */
797 cd->cdi.mask |= CDC_DVD_R;
798 if ((buffer[n + 3] & 0x2) == 0)
799 /* can't write CD-RW media */
800 cd->cdi.mask |= CDC_CD_RW;
801 if ((buffer[n + 3] & 0x1) == 0)
802 /* can't write CD-R media */
803 cd->cdi.mask |= CDC_CD_R;
804 if ((buffer[n + 6] & 0x8) == 0)
805 /* can't eject */
806 cd->cdi.mask |= CDC_OPEN_TRAY;
807
808 if ((buffer[n + 6] >> 5) == mechtype_individual_changer ||
809 (buffer[n + 6] >> 5) == mechtype_cartridge_changer)
810 cd->cdi.capacity =
811 cdrom_number_of_slots(&cd->cdi);
812 if (cd->cdi.capacity <= 1)
813 /* not a changer */
814 cd->cdi.mask |= CDC_SELECT_DISC;
815 /*else I don't think it can close its tray
816 cd->cdi.mask |= CDC_CLOSE_TRAY; */
817
818 /*
819 * if DVD-RAM, MRW-W or CD-RW, we are randomly writable
820 */
821 if ((cd->cdi.mask & (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) !=
822 (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) {
823 cd->device->writeable = 1;
824 }
825
1da177e4
LT
826 kfree(buffer);
827}
828
829/*
830 * sr_packet() is the entry point for the generic commands generated
831 * by the Uniform CD-ROM layer.
832 */
833static int sr_packet(struct cdrom_device_info *cdi,
834 struct packet_command *cgc)
835{
836 if (cgc->timeout <= 0)
837 cgc->timeout = IOCTL_TIMEOUT;
838
839 sr_do_ioctl(cdi->handle, cgc);
840
841 return cgc->stat;
842}
843
844/**
845 * sr_kref_release - Called to free the scsi_cd structure
846 * @kref: pointer to embedded kref
847 *
848 * sr_ref_sem must be held entering this routine. Because it is
849 * called on last put, you should always use the scsi_cd_get()
850 * scsi_cd_put() helpers which manipulate the semaphore directly
851 * and never do a direct kref_put().
852 **/
853static void sr_kref_release(struct kref *kref)
854{
855 struct scsi_cd *cd = container_of(kref, struct scsi_cd, kref);
856 struct gendisk *disk = cd->disk;
857
858 spin_lock(&sr_index_lock);
859 clear_bit(disk->first_minor, sr_index_bits);
860 spin_unlock(&sr_index_lock);
861
862 unregister_cdrom(&cd->cdi);
863
864 disk->private_data = NULL;
865
866 put_disk(disk);
867
868 kfree(cd);
869}
870
871static int sr_remove(struct device *dev)
872{
873 struct scsi_cd *cd = dev_get_drvdata(dev);
874
875 del_gendisk(cd->disk);
876
877 down(&sr_ref_sem);
878 kref_put(&cd->kref, sr_kref_release);
879 up(&sr_ref_sem);
880
881 return 0;
882}
883
884static int __init init_sr(void)
885{
886 int rc;
887
888 rc = register_blkdev(SCSI_CDROM_MAJOR, "sr");
889 if (rc)
890 return rc;
891 return scsi_register_driver(&sr_template.gendrv);
892}
893
894static void __exit exit_sr(void)
895{
896 scsi_unregister_driver(&sr_template.gendrv);
897 unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
898}
899
900module_init(init_sr);
901module_exit(exit_sr);
902MODULE_LICENSE("GPL");