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