blob: d8d12a1718bf91cc9df203440b594dadb5c2ea76 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Arjan van de Ven0b950672006-01-11 13:16:10 +010047#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#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>
James Bottomley820732b2005-06-12 22:21:29 -050054#include <scsi/scsi_cmnd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <scsi/scsi_eh.h>
56#include <scsi/scsi_host.h>
57#include <scsi/scsi_ioctl.h> /* For the door lock/unlock commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
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) */
Arjan van de Ven0b950672006-01-11 13:16:10 +010094static DEFINE_MUTEX(sr_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
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
Arjan van de Ven0b950672006-01-11 13:16:10 +0100137 mutex_lock(&sr_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 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:
Arjan van de Ven0b950672006-01-11 13:16:10 +0100150 mutex_unlock(&sr_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 return cd;
152}
153
Arjan van de Ven858119e2006-01-14 13:20:43 -0800154static void scsi_cd_put(struct scsi_cd *cd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
156 struct scsi_device *sdev = cd->device;
157
Arjan van de Ven0b950672006-01-11 13:16:10 +0100158 mutex_lock(&sr_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 kref_put(&cd->kref, sr_kref_release);
160 scsi_device_put(sdev);
Arjan van de Ven0b950672006-01-11 13:16:10 +0100161 mutex_unlock(&sr_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
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
Pete Zaitcev51490c82005-07-05 18:18:08 -0700203 get_sectorsize(cd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 }
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 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
318 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 * 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) {
Jeff Garzik3bf743e2005-10-24 18:04:06 -0400331 scmd_printk(KERN_ERR, SCpnt, "bad sector size %d\n", s_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 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) {
Jeff Garzik3bf743e2005-10-24 18:04:06 -0400356 scmd_printk(KERN_ERR, SCpnt,
357 "mismatch count %d, bytes %d\n",
358 size, SCpnt->request_bufflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 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)) {
Jeff Garzik3bf743e2005-10-24 18:04:06 -0400369 scmd_printk(KERN_NOTICE, SCpnt, "unaligned transfer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 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;
Jayachandran C80d904c2005-10-18 17:11:16 -0700425 struct scsi_cd *cd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 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;
Jes Sorensen24669f752006-01-16 10:31:18 -0500528 cd = kzalloc(sizeof(*cd), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 if (!cd)
530 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
532 kref_init(&cd->kref);
533
534 disk = alloc_disk(1);
535 if (!disk)
536 goto fail_free;
537
538 spin_lock(&sr_index_lock);
539 minor = find_first_zero_bit(sr_index_bits, SR_DISKS);
540 if (minor == SR_DISKS) {
541 spin_unlock(&sr_index_lock);
542 error = -EBUSY;
543 goto fail_put;
544 }
545 __set_bit(minor, sr_index_bits);
546 spin_unlock(&sr_index_lock);
547
548 disk->major = SCSI_CDROM_MAJOR;
549 disk->first_minor = minor;
550 sprintf(disk->disk_name, "sr%d", minor);
551 disk->fops = &sr_bdops;
552 disk->flags = GENHD_FL_CD;
553
554 cd->device = sdev;
555 cd->disk = disk;
556 cd->driver = &sr_template;
557 cd->disk = disk;
558 cd->capacity = 0x1fffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 cd->device->changed = 1; /* force recheck CD type */
560 cd->use = 1;
561 cd->readcd_known = 0;
562 cd->readcd_cdda = 0;
563
564 cd->cdi.ops = &sr_dops;
565 cd->cdi.handle = cd;
566 cd->cdi.mask = 0;
567 cd->cdi.capacity = 1;
568 sprintf(cd->cdi.name, "sr%d", minor);
569
570 sdev->sector_size = 2048; /* A guess, just in case */
571
572 /* FIXME: need to handle a get_capabilities failure properly ?? */
573 get_capabilities(cd);
574 sr_vendor_init(cd);
575
576 snprintf(disk->devfs_name, sizeof(disk->devfs_name),
577 "%s/cd", sdev->devfs_name);
578 disk->driverfs_dev = &sdev->sdev_gendev;
579 set_capacity(disk, cd->capacity);
580 disk->private_data = &cd->driver;
581 disk->queue = sdev->request_queue;
582 cd->cdi.disk = disk;
583
584 if (register_cdrom(&cd->cdi))
585 goto fail_put;
586
587 dev_set_drvdata(dev, cd);
588 disk->flags |= GENHD_FL_REMOVABLE;
589 add_disk(disk);
590
James Bottomley9ccfc752005-10-02 11:45:08 -0500591 sdev_printk(KERN_DEBUG, sdev,
592 "Attached scsi CD-ROM %s\n", cd->cdi.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 return 0;
594
595fail_put:
596 put_disk(disk);
597fail_free:
598 kfree(cd);
599fail:
600 return error;
601}
602
603
604static void get_sectorsize(struct scsi_cd *cd)
605{
606 unsigned char cmd[10];
607 unsigned char *buffer;
608 int the_result, retries = 3;
609 int sector_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 request_queue_t *queue;
611
612 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
613 if (!buffer)
614 goto Enomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 do {
617 cmd[0] = READ_CAPACITY;
618 memset((void *) &cmd[1], 0, 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 memset(buffer, 0, 8);
620
621 /* Do the command and wait.. */
James Bottomley820732b2005-06-12 22:21:29 -0500622 the_result = scsi_execute_req(cd->device, cmd, DMA_FROM_DEVICE,
623 buffer, 8, NULL, SR_TIMEOUT,
624 MAX_RETRIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 retries--;
627
628 } while (the_result && retries);
629
630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 if (the_result) {
632 cd->capacity = 0x1fffff;
633 sector_size = 2048; /* A guess, just in case */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 } else {
635#if 0
636 if (cdrom_get_last_written(&cd->cdi,
637 &cd->capacity))
638#endif
639 cd->capacity = 1 + ((buffer[0] << 24) |
640 (buffer[1] << 16) |
641 (buffer[2] << 8) |
642 buffer[3]);
643 sector_size = (buffer[4] << 24) |
644 (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
645 switch (sector_size) {
646 /*
647 * HP 4020i CD-Recorder reports 2340 byte sectors
648 * Philips CD-Writers report 2352 byte sectors
649 *
650 * Use 2k sectors for them..
651 */
652 case 0:
653 case 2340:
654 case 2352:
655 sector_size = 2048;
656 /* fall through */
657 case 2048:
658 cd->capacity *= 4;
659 /* fall through */
660 case 512:
661 break;
662 default:
663 printk("%s: unsupported sector size %d.\n",
664 cd->cdi.name, sector_size);
665 cd->capacity = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 }
667
668 cd->device->sector_size = sector_size;
669
670 /*
671 * Add this so that we have the ability to correctly gauge
672 * what the device is capable of.
673 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 set_capacity(cd->disk, cd->capacity);
675 }
676
677 queue = cd->device->request_queue;
678 blk_queue_hardsect_size(queue, sector_size);
679out:
680 kfree(buffer);
681 return;
682
683Enomem:
684 cd->capacity = 0x1fffff;
Pete Zaitcev51490c82005-07-05 18:18:08 -0700685 cd->device->sector_size = 2048; /* A guess, just in case */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 goto out;
687}
688
689static void get_capabilities(struct scsi_cd *cd)
690{
691 unsigned char *buffer;
692 struct scsi_mode_data data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 unsigned char cmd[MAX_COMMAND_SIZE];
James Bottomley820732b2005-06-12 22:21:29 -0500694 struct scsi_sense_hdr sshdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 unsigned int the_result;
696 int retries, rc, n;
697
Arjan van de Ven0ad78202005-11-28 16:22:25 +0100698 static const char *loadmech[] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 {
700 "caddy",
701 "tray",
702 "pop-up",
703 "",
704 "changer",
705 "cartridge changer",
706 "",
707 ""
708 };
709
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
711 /* allocate transfer buffer */
712 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
713 if (!buffer) {
714 printk(KERN_ERR "sr: out of memory.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 return;
716 }
717
718 /* issue TEST_UNIT_READY until the initial startup UNIT_ATTENTION
719 * conditions are gone, or a timeout happens
720 */
721 retries = 0;
722 do {
723 memset((void *)cmd, 0, MAX_COMMAND_SIZE);
724 cmd[0] = TEST_UNIT_READY;
725
James Bottomley820732b2005-06-12 22:21:29 -0500726 the_result = scsi_execute_req (cd->device, cmd, DMA_NONE, NULL,
727 0, &sshdr, SR_TIMEOUT,
728 MAX_RETRIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 retries++;
731 } while (retries < 5 &&
732 (!scsi_status_is_good(the_result) ||
James Bottomley820732b2005-06-12 22:21:29 -0500733 (scsi_sense_valid(&sshdr) &&
734 sshdr.sense_key == UNIT_ATTENTION)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
736 /* ask for mode page 0x2a */
737 rc = scsi_mode_sense(cd->device, 0, 0x2a, buffer, 128,
James Bottomley1cf72692005-08-28 11:27:01 -0500738 SR_TIMEOUT, 3, &data, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
740 if (!scsi_status_is_good(rc)) {
741 /* failed, drive doesn't have capabilities mode page */
742 cd->cdi.speed = 1;
743 cd->cdi.mask |= (CDC_CD_R | CDC_CD_RW | CDC_DVD_R |
Chuck Ebbertbcc1e382006-01-11 23:58:40 -0500744 CDC_DVD | CDC_DVD_RAM |
745 CDC_SELECT_DISC | CDC_SELECT_SPEED |
746 CDC_MRW | CDC_MRW_W | CDC_RAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 kfree(buffer);
748 printk("%s: scsi-1 drive\n", cd->cdi.name);
749 return;
750 }
751
752 n = data.header_length + data.block_descriptor_length;
753 cd->cdi.speed = ((buffer[n + 8] << 8) + buffer[n + 9]) / 176;
754 cd->readcd_known = 1;
755 cd->readcd_cdda = buffer[n + 5] & 0x01;
756 /* print some capability bits */
757 printk("%s: scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n", cd->cdi.name,
758 ((buffer[n + 14] << 8) + buffer[n + 15]) / 176,
759 cd->cdi.speed,
760 buffer[n + 3] & 0x01 ? "writer " : "", /* CD Writer */
761 buffer[n + 3] & 0x20 ? "dvd-ram " : "",
762 buffer[n + 2] & 0x02 ? "cd/rw " : "", /* can read rewriteable */
763 buffer[n + 4] & 0x20 ? "xa/form2 " : "", /* can read xa/from2 */
764 buffer[n + 5] & 0x01 ? "cdda " : "", /* can read audio data */
765 loadmech[buffer[n + 6] >> 5]);
766 if ((buffer[n + 6] >> 5) == 0)
767 /* caddy drives can't close tray... */
768 cd->cdi.mask |= CDC_CLOSE_TRAY;
769 if ((buffer[n + 2] & 0x8) == 0)
770 /* not a DVD drive */
771 cd->cdi.mask |= CDC_DVD;
772 if ((buffer[n + 3] & 0x20) == 0)
773 /* can't write DVD-RAM media */
774 cd->cdi.mask |= CDC_DVD_RAM;
775 if ((buffer[n + 3] & 0x10) == 0)
776 /* can't write DVD-R media */
777 cd->cdi.mask |= CDC_DVD_R;
778 if ((buffer[n + 3] & 0x2) == 0)
779 /* can't write CD-RW media */
780 cd->cdi.mask |= CDC_CD_RW;
781 if ((buffer[n + 3] & 0x1) == 0)
782 /* can't write CD-R media */
783 cd->cdi.mask |= CDC_CD_R;
784 if ((buffer[n + 6] & 0x8) == 0)
785 /* can't eject */
786 cd->cdi.mask |= CDC_OPEN_TRAY;
787
788 if ((buffer[n + 6] >> 5) == mechtype_individual_changer ||
789 (buffer[n + 6] >> 5) == mechtype_cartridge_changer)
790 cd->cdi.capacity =
791 cdrom_number_of_slots(&cd->cdi);
792 if (cd->cdi.capacity <= 1)
793 /* not a changer */
794 cd->cdi.mask |= CDC_SELECT_DISC;
795 /*else I don't think it can close its tray
796 cd->cdi.mask |= CDC_CLOSE_TRAY; */
797
798 /*
799 * if DVD-RAM, MRW-W or CD-RW, we are randomly writable
800 */
801 if ((cd->cdi.mask & (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) !=
802 (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) {
803 cd->device->writeable = 1;
804 }
805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 kfree(buffer);
807}
808
809/*
810 * sr_packet() is the entry point for the generic commands generated
811 * by the Uniform CD-ROM layer.
812 */
813static int sr_packet(struct cdrom_device_info *cdi,
814 struct packet_command *cgc)
815{
816 if (cgc->timeout <= 0)
817 cgc->timeout = IOCTL_TIMEOUT;
818
819 sr_do_ioctl(cdi->handle, cgc);
820
821 return cgc->stat;
822}
823
824/**
825 * sr_kref_release - Called to free the scsi_cd structure
826 * @kref: pointer to embedded kref
827 *
Arjan van de Ven0b950672006-01-11 13:16:10 +0100828 * sr_ref_mutex must be held entering this routine. Because it is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 * called on last put, you should always use the scsi_cd_get()
830 * scsi_cd_put() helpers which manipulate the semaphore directly
831 * and never do a direct kref_put().
832 **/
833static void sr_kref_release(struct kref *kref)
834{
835 struct scsi_cd *cd = container_of(kref, struct scsi_cd, kref);
836 struct gendisk *disk = cd->disk;
837
838 spin_lock(&sr_index_lock);
839 clear_bit(disk->first_minor, sr_index_bits);
840 spin_unlock(&sr_index_lock);
841
842 unregister_cdrom(&cd->cdi);
843
844 disk->private_data = NULL;
845
846 put_disk(disk);
847
848 kfree(cd);
849}
850
851static int sr_remove(struct device *dev)
852{
853 struct scsi_cd *cd = dev_get_drvdata(dev);
854
855 del_gendisk(cd->disk);
856
Arjan van de Ven0b950672006-01-11 13:16:10 +0100857 mutex_lock(&sr_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 kref_put(&cd->kref, sr_kref_release);
Arjan van de Ven0b950672006-01-11 13:16:10 +0100859 mutex_unlock(&sr_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
861 return 0;
862}
863
864static int __init init_sr(void)
865{
866 int rc;
867
868 rc = register_blkdev(SCSI_CDROM_MAJOR, "sr");
869 if (rc)
870 return rc;
871 return scsi_register_driver(&sr_template.gendrv);
872}
873
874static void __exit exit_sr(void)
875{
876 scsi_unregister_driver(&sr_template.gendrv);
877 unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
878}
879
880module_init(init_sr);
881module_exit(exit_sr);
882MODULE_LICENSE("GPL");