blob: 328837b7ccb97a48776c64355b5950234f78a6c0 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 disk->driverfs_dev = &sdev->sdev_gendev;
577 set_capacity(disk, cd->capacity);
578 disk->private_data = &cd->driver;
579 disk->queue = sdev->request_queue;
580 cd->cdi.disk = disk;
581
582 if (register_cdrom(&cd->cdi))
583 goto fail_put;
584
585 dev_set_drvdata(dev, cd);
586 disk->flags |= GENHD_FL_REMOVABLE;
587 add_disk(disk);
588
James Bottomley9ccfc752005-10-02 11:45:08 -0500589 sdev_printk(KERN_DEBUG, sdev,
590 "Attached scsi CD-ROM %s\n", cd->cdi.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 return 0;
592
593fail_put:
594 put_disk(disk);
595fail_free:
596 kfree(cd);
597fail:
598 return error;
599}
600
601
602static void get_sectorsize(struct scsi_cd *cd)
603{
604 unsigned char cmd[10];
605 unsigned char *buffer;
606 int the_result, retries = 3;
607 int sector_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 request_queue_t *queue;
609
610 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
611 if (!buffer)
612 goto Enomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 do {
615 cmd[0] = READ_CAPACITY;
616 memset((void *) &cmd[1], 0, 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 memset(buffer, 0, 8);
618
619 /* Do the command and wait.. */
James Bottomley820732b2005-06-12 22:21:29 -0500620 the_result = scsi_execute_req(cd->device, cmd, DMA_FROM_DEVICE,
621 buffer, 8, NULL, SR_TIMEOUT,
622 MAX_RETRIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 retries--;
625
626 } while (the_result && retries);
627
628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 if (the_result) {
630 cd->capacity = 0x1fffff;
631 sector_size = 2048; /* A guess, just in case */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 } else {
633#if 0
634 if (cdrom_get_last_written(&cd->cdi,
635 &cd->capacity))
636#endif
637 cd->capacity = 1 + ((buffer[0] << 24) |
638 (buffer[1] << 16) |
639 (buffer[2] << 8) |
640 buffer[3]);
641 sector_size = (buffer[4] << 24) |
642 (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
643 switch (sector_size) {
644 /*
645 * HP 4020i CD-Recorder reports 2340 byte sectors
646 * Philips CD-Writers report 2352 byte sectors
647 *
648 * Use 2k sectors for them..
649 */
650 case 0:
651 case 2340:
652 case 2352:
653 sector_size = 2048;
654 /* fall through */
655 case 2048:
656 cd->capacity *= 4;
657 /* fall through */
658 case 512:
659 break;
660 default:
661 printk("%s: unsupported sector size %d.\n",
662 cd->cdi.name, sector_size);
663 cd->capacity = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 }
665
666 cd->device->sector_size = sector_size;
667
668 /*
669 * Add this so that we have the ability to correctly gauge
670 * what the device is capable of.
671 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 set_capacity(cd->disk, cd->capacity);
673 }
674
675 queue = cd->device->request_queue;
676 blk_queue_hardsect_size(queue, sector_size);
677out:
678 kfree(buffer);
679 return;
680
681Enomem:
682 cd->capacity = 0x1fffff;
Pete Zaitcev51490c82005-07-05 18:18:08 -0700683 cd->device->sector_size = 2048; /* A guess, just in case */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 goto out;
685}
686
687static void get_capabilities(struct scsi_cd *cd)
688{
689 unsigned char *buffer;
690 struct scsi_mode_data data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 unsigned char cmd[MAX_COMMAND_SIZE];
James Bottomley820732b2005-06-12 22:21:29 -0500692 struct scsi_sense_hdr sshdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 unsigned int the_result;
694 int retries, rc, n;
695
Arjan van de Ven0ad78202005-11-28 16:22:25 +0100696 static const char *loadmech[] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 {
698 "caddy",
699 "tray",
700 "pop-up",
701 "",
702 "changer",
703 "cartridge changer",
704 "",
705 ""
706 };
707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 /* allocate transfer buffer */
710 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
711 if (!buffer) {
712 printk(KERN_ERR "sr: out of memory.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 return;
714 }
715
716 /* issue TEST_UNIT_READY until the initial startup UNIT_ATTENTION
717 * conditions are gone, or a timeout happens
718 */
719 retries = 0;
720 do {
721 memset((void *)cmd, 0, MAX_COMMAND_SIZE);
722 cmd[0] = TEST_UNIT_READY;
723
James Bottomley820732b2005-06-12 22:21:29 -0500724 the_result = scsi_execute_req (cd->device, cmd, DMA_NONE, NULL,
725 0, &sshdr, SR_TIMEOUT,
726 MAX_RETRIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 retries++;
729 } while (retries < 5 &&
730 (!scsi_status_is_good(the_result) ||
James Bottomley820732b2005-06-12 22:21:29 -0500731 (scsi_sense_valid(&sshdr) &&
732 sshdr.sense_key == UNIT_ATTENTION)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
734 /* ask for mode page 0x2a */
735 rc = scsi_mode_sense(cd->device, 0, 0x2a, buffer, 128,
James Bottomley1cf72692005-08-28 11:27:01 -0500736 SR_TIMEOUT, 3, &data, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
738 if (!scsi_status_is_good(rc)) {
739 /* failed, drive doesn't have capabilities mode page */
740 cd->cdi.speed = 1;
741 cd->cdi.mask |= (CDC_CD_R | CDC_CD_RW | CDC_DVD_R |
Chuck Ebbertbcc1e382006-01-11 23:58:40 -0500742 CDC_DVD | CDC_DVD_RAM |
743 CDC_SELECT_DISC | CDC_SELECT_SPEED |
744 CDC_MRW | CDC_MRW_W | CDC_RAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 kfree(buffer);
746 printk("%s: scsi-1 drive\n", cd->cdi.name);
747 return;
748 }
749
750 n = data.header_length + data.block_descriptor_length;
751 cd->cdi.speed = ((buffer[n + 8] << 8) + buffer[n + 9]) / 176;
752 cd->readcd_known = 1;
753 cd->readcd_cdda = buffer[n + 5] & 0x01;
754 /* print some capability bits */
755 printk("%s: scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n", cd->cdi.name,
756 ((buffer[n + 14] << 8) + buffer[n + 15]) / 176,
757 cd->cdi.speed,
758 buffer[n + 3] & 0x01 ? "writer " : "", /* CD Writer */
759 buffer[n + 3] & 0x20 ? "dvd-ram " : "",
760 buffer[n + 2] & 0x02 ? "cd/rw " : "", /* can read rewriteable */
761 buffer[n + 4] & 0x20 ? "xa/form2 " : "", /* can read xa/from2 */
762 buffer[n + 5] & 0x01 ? "cdda " : "", /* can read audio data */
763 loadmech[buffer[n + 6] >> 5]);
764 if ((buffer[n + 6] >> 5) == 0)
765 /* caddy drives can't close tray... */
766 cd->cdi.mask |= CDC_CLOSE_TRAY;
767 if ((buffer[n + 2] & 0x8) == 0)
768 /* not a DVD drive */
769 cd->cdi.mask |= CDC_DVD;
770 if ((buffer[n + 3] & 0x20) == 0)
771 /* can't write DVD-RAM media */
772 cd->cdi.mask |= CDC_DVD_RAM;
773 if ((buffer[n + 3] & 0x10) == 0)
774 /* can't write DVD-R media */
775 cd->cdi.mask |= CDC_DVD_R;
776 if ((buffer[n + 3] & 0x2) == 0)
777 /* can't write CD-RW media */
778 cd->cdi.mask |= CDC_CD_RW;
779 if ((buffer[n + 3] & 0x1) == 0)
780 /* can't write CD-R media */
781 cd->cdi.mask |= CDC_CD_R;
782 if ((buffer[n + 6] & 0x8) == 0)
783 /* can't eject */
784 cd->cdi.mask |= CDC_OPEN_TRAY;
785
786 if ((buffer[n + 6] >> 5) == mechtype_individual_changer ||
787 (buffer[n + 6] >> 5) == mechtype_cartridge_changer)
788 cd->cdi.capacity =
789 cdrom_number_of_slots(&cd->cdi);
790 if (cd->cdi.capacity <= 1)
791 /* not a changer */
792 cd->cdi.mask |= CDC_SELECT_DISC;
793 /*else I don't think it can close its tray
794 cd->cdi.mask |= CDC_CLOSE_TRAY; */
795
796 /*
797 * if DVD-RAM, MRW-W or CD-RW, we are randomly writable
798 */
799 if ((cd->cdi.mask & (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) !=
800 (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) {
801 cd->device->writeable = 1;
802 }
803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 kfree(buffer);
805}
806
807/*
808 * sr_packet() is the entry point for the generic commands generated
809 * by the Uniform CD-ROM layer.
810 */
811static int sr_packet(struct cdrom_device_info *cdi,
812 struct packet_command *cgc)
813{
814 if (cgc->timeout <= 0)
815 cgc->timeout = IOCTL_TIMEOUT;
816
817 sr_do_ioctl(cdi->handle, cgc);
818
819 return cgc->stat;
820}
821
822/**
823 * sr_kref_release - Called to free the scsi_cd structure
824 * @kref: pointer to embedded kref
825 *
Arjan van de Ven0b950672006-01-11 13:16:10 +0100826 * sr_ref_mutex must be held entering this routine. Because it is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 * called on last put, you should always use the scsi_cd_get()
828 * scsi_cd_put() helpers which manipulate the semaphore directly
829 * and never do a direct kref_put().
830 **/
831static void sr_kref_release(struct kref *kref)
832{
833 struct scsi_cd *cd = container_of(kref, struct scsi_cd, kref);
834 struct gendisk *disk = cd->disk;
835
836 spin_lock(&sr_index_lock);
837 clear_bit(disk->first_minor, sr_index_bits);
838 spin_unlock(&sr_index_lock);
839
840 unregister_cdrom(&cd->cdi);
841
842 disk->private_data = NULL;
843
844 put_disk(disk);
845
846 kfree(cd);
847}
848
849static int sr_remove(struct device *dev)
850{
851 struct scsi_cd *cd = dev_get_drvdata(dev);
852
853 del_gendisk(cd->disk);
854
Arjan van de Ven0b950672006-01-11 13:16:10 +0100855 mutex_lock(&sr_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 kref_put(&cd->kref, sr_kref_release);
Arjan van de Ven0b950672006-01-11 13:16:10 +0100857 mutex_unlock(&sr_ref_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
859 return 0;
860}
861
862static int __init init_sr(void)
863{
864 int rc;
865
866 rc = register_blkdev(SCSI_CDROM_MAJOR, "sr");
867 if (rc)
868 return rc;
869 return scsi_register_driver(&sr_template.gendrv);
870}
871
872static void __exit exit_sr(void)
873{
874 scsi_unregister_driver(&sr_template.gendrv);
875 unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
876}
877
878module_init(init_sr);
879module_exit(exit_sr);
880MODULE_LICENSE("GPL");