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