[PATCH] cleanup cdrom_ioctl
[GitHub/mt8127/android_kernel_alcatel_ttab.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
f018fa55
RH
63MODULE_DESCRIPTION("SCSI cdrom (sr) driver");
64MODULE_LICENSE("GPL");
65MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_CDROM_MAJOR);
66
1da177e4
LT
67#define SR_DISKS 256
68
69#define MAX_RETRIES 3
70#define SR_TIMEOUT (30 * HZ)
71#define SR_CAPABILITIES \
72 (CDC_CLOSE_TRAY|CDC_OPEN_TRAY|CDC_LOCK|CDC_SELECT_SPEED| \
73 CDC_SELECT_DISC|CDC_MULTI_SESSION|CDC_MCN|CDC_MEDIA_CHANGED| \
74 CDC_PLAY_AUDIO|CDC_RESET|CDC_IOCTLS|CDC_DRIVE_STATUS| \
75 CDC_CD_R|CDC_CD_RW|CDC_DVD|CDC_DVD_R|CDC_DVD_RAM|CDC_GENERIC_PACKET| \
76 CDC_MRW|CDC_MRW_W|CDC_RAM)
77
78static int sr_probe(struct device *);
79static int sr_remove(struct device *);
80static int sr_init_command(struct scsi_cmnd *);
81
82static struct scsi_driver sr_template = {
83 .owner = THIS_MODULE,
84 .gendrv = {
85 .name = "sr",
86 .probe = sr_probe,
87 .remove = sr_remove,
88 },
89 .init_command = sr_init_command,
90};
91
92static unsigned long sr_index_bits[SR_DISKS / BITS_PER_LONG];
93static DEFINE_SPINLOCK(sr_index_lock);
94
95/* This semaphore is used to mediate the 0->1 reference get in the
96 * face of object destruction (i.e. we can't allow a get on an
97 * object after last put) */
0b950672 98static DEFINE_MUTEX(sr_ref_mutex);
1da177e4
LT
99
100static int sr_open(struct cdrom_device_info *, int);
101static void sr_release(struct cdrom_device_info *);
102
103static void get_sectorsize(struct scsi_cd *);
104static void get_capabilities(struct scsi_cd *);
105
106static int sr_media_change(struct cdrom_device_info *, int);
107static int sr_packet(struct cdrom_device_info *, struct packet_command *);
108
109static struct cdrom_device_ops sr_dops = {
110 .open = sr_open,
111 .release = sr_release,
112 .drive_status = sr_drive_status,
113 .media_changed = sr_media_change,
114 .tray_move = sr_tray_move,
115 .lock_door = sr_lock_door,
116 .select_speed = sr_select_speed,
117 .get_last_session = sr_get_last_session,
118 .get_mcn = sr_get_mcn,
119 .reset = sr_reset,
120 .audio_ioctl = sr_audio_ioctl,
121 .dev_ioctl = sr_dev_ioctl,
122 .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
0b950672 141 mutex_lock(&sr_ref_mutex);
1da177e4
LT
142 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:
0b950672 154 mutex_unlock(&sr_ref_mutex);
1da177e4
LT
155 return cd;
156}
157
858119e1 158static void scsi_cd_put(struct scsi_cd *cd)
1da177e4
LT
159{
160 struct scsi_device *sdev = cd->device;
161
0b950672 162 mutex_lock(&sr_ref_mutex);
1da177e4
LT
163 kref_put(&cd->kref, sr_kref_release);
164 scsi_device_put(sdev);
0b950672 165 mutex_unlock(&sr_ref_mutex);
1da177e4
LT
166}
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
51490c89 207 get_sectorsize(cd);
1da177e4
LT
208 }
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;
221 int this_count = SCpnt->bufflen;
222 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;
1da177e4
LT
245 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 */
296 scsi_io_completion(SCpnt, good_bytes, block_sectors << 9);
297}
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
1da177e4
LT
322 /*
323 * 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) {
3bf743e7 335 scmd_printk(KERN_ERR, SCpnt, "bad sector size %d\n", s_size);
1da177e4
LT
336 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) {
3bf743e7
JG
360 scmd_printk(KERN_ERR, SCpnt,
361 "mismatch count %d, bytes %d\n",
362 size, SCpnt->request_bufflen);
1da177e4
LT
363 if (SCpnt->request_bufflen > size)
364 SCpnt->request_bufflen = SCpnt->bufflen = size;
365 }
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)) {
3bf743e7 373 scmd_printk(KERN_NOTICE, SCpnt, "unaligned transfer\n");
1da177e4
LT
374 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;
391 SCpnt->request_bufflen = SCpnt->bufflen =
392 this_count * s_size;
393 }
394
395 SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
396 SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
397 SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
398 SCpnt->cmnd[5] = (unsigned char) block & 0xff;
399 SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
400 SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
401 SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
402
403 /*
404 * We shouldn't disconnect in the middle of a sector, so with a dumb
405 * host adapter, it's safe to assume that we can at least transfer
406 * this many bytes between each connect / disconnect.
407 */
408 SCpnt->transfersize = cd->device->sector_size;
409 SCpnt->underflow = this_count << 9;
1da177e4
LT
410 SCpnt->allowed = MAX_RETRIES;
411 SCpnt->timeout_per_command = timeout;
412
413 /*
414 * This is the completion routine we use. This is matched in terms
415 * of capability to this function.
416 */
417 SCpnt->done = rw_intr;
418
419 /*
420 * This indicates that the command is ready from our end to be
421 * queued.
422 */
423 return 1;
424}
425
426static int sr_block_open(struct inode *inode, struct file *file)
427{
428 struct gendisk *disk = inode->i_bdev->bd_disk;
80d904c4 429 struct scsi_cd *cd;
1da177e4
LT
430 int ret = 0;
431
432 if(!(cd = scsi_cd_get(disk)))
433 return -ENXIO;
434
435 if((ret = cdrom_open(&cd->cdi, inode, file)) != 0)
436 scsi_cd_put(cd);
437
438 return ret;
439}
440
441static int sr_block_release(struct inode *inode, struct file *file)
442{
443 int ret;
444 struct scsi_cd *cd = scsi_cd(inode->i_bdev->bd_disk);
445 ret = cdrom_release(&cd->cdi, file);
446 if(ret)
447 return ret;
448
449 scsi_cd_put(cd);
450
451 return 0;
452}
453
454static int sr_block_ioctl(struct inode *inode, struct file *file, unsigned cmd,
455 unsigned long arg)
456{
457 struct scsi_cd *cd = scsi_cd(inode->i_bdev->bd_disk);
458 struct scsi_device *sdev = cd->device;
459
460 /*
461 * Send SCSI addressing ioctls directly to mid level, send other
462 * ioctls to cdrom/block level.
463 */
464 switch (cmd) {
465 case SCSI_IOCTL_GET_IDLUN:
466 case SCSI_IOCTL_GET_BUS_NUMBER:
467 return scsi_ioctl(sdev, cmd, (void __user *)arg);
468 }
469 return cdrom_ioctl(file, &cd->cdi, inode, cmd, arg);
470}
471
472static int sr_block_media_changed(struct gendisk *disk)
473{
474 struct scsi_cd *cd = scsi_cd(disk);
475 return cdrom_media_changed(&cd->cdi);
476}
477
478static struct block_device_operations sr_bdops =
479{
480 .owner = THIS_MODULE,
481 .open = sr_block_open,
482 .release = sr_block_release,
483 .ioctl = sr_block_ioctl,
484 .media_changed = sr_block_media_changed,
485 /*
486 * No compat_ioctl for now because sr_block_ioctl never
487 * seems to pass arbitary ioctls down to host drivers.
488 */
489};
490
491static int sr_open(struct cdrom_device_info *cdi, int purpose)
492{
493 struct scsi_cd *cd = cdi->handle;
494 struct scsi_device *sdev = cd->device;
495 int retval;
496
497 /*
498 * If the device is in error recovery, wait until it is done.
499 * If the device is offline, then disallow any access to it.
500 */
501 retval = -ENXIO;
502 if (!scsi_block_when_processing_errors(sdev))
503 goto error_out;
504
1da177e4
LT
505 return 0;
506
507error_out:
508 return retval;
509}
510
511static void sr_release(struct cdrom_device_info *cdi)
512{
513 struct scsi_cd *cd = cdi->handle;
514
515 if (cd->device->sector_size > 2048)
516 sr_set_blocklength(cd, 2048);
517
518}
519
520static int sr_probe(struct device *dev)
521{
522 struct scsi_device *sdev = to_scsi_device(dev);
523 struct gendisk *disk;
524 struct scsi_cd *cd;
525 int minor, error;
526
527 error = -ENODEV;
528 if (sdev->type != TYPE_ROM && sdev->type != TYPE_WORM)
529 goto fail;
530
531 error = -ENOMEM;
24669f75 532 cd = kzalloc(sizeof(*cd), GFP_KERNEL);
1da177e4
LT
533 if (!cd)
534 goto fail;
1da177e4
LT
535
536 kref_init(&cd->kref);
537
538 disk = alloc_disk(1);
539 if (!disk)
540 goto fail_free;
541
542 spin_lock(&sr_index_lock);
543 minor = find_first_zero_bit(sr_index_bits, SR_DISKS);
544 if (minor == SR_DISKS) {
545 spin_unlock(&sr_index_lock);
546 error = -EBUSY;
547 goto fail_put;
548 }
549 __set_bit(minor, sr_index_bits);
550 spin_unlock(&sr_index_lock);
551
552 disk->major = SCSI_CDROM_MAJOR;
553 disk->first_minor = minor;
554 sprintf(disk->disk_name, "sr%d", minor);
555 disk->fops = &sr_bdops;
556 disk->flags = GENHD_FL_CD;
557
558 cd->device = sdev;
559 cd->disk = disk;
560 cd->driver = &sr_template;
561 cd->disk = disk;
562 cd->capacity = 0x1fffff;
1da177e4
LT
563 cd->device->changed = 1; /* force recheck CD type */
564 cd->use = 1;
565 cd->readcd_known = 0;
566 cd->readcd_cdda = 0;
567
568 cd->cdi.ops = &sr_dops;
569 cd->cdi.handle = cd;
570 cd->cdi.mask = 0;
571 cd->cdi.capacity = 1;
572 sprintf(cd->cdi.name, "sr%d", minor);
573
574 sdev->sector_size = 2048; /* A guess, just in case */
575
576 /* FIXME: need to handle a get_capabilities failure properly ?? */
577 get_capabilities(cd);
578 sr_vendor_init(cd);
579
1da177e4
LT
580 disk->driverfs_dev = &sdev->sdev_gendev;
581 set_capacity(disk, cd->capacity);
582 disk->private_data = &cd->driver;
583 disk->queue = sdev->request_queue;
584 cd->cdi.disk = disk;
585
586 if (register_cdrom(&cd->cdi))
587 goto fail_put;
588
589 dev_set_drvdata(dev, cd);
590 disk->flags |= GENHD_FL_REMOVABLE;
591 add_disk(disk);
592
9ccfc756
JB
593 sdev_printk(KERN_DEBUG, sdev,
594 "Attached scsi CD-ROM %s\n", cd->cdi.name);
1da177e4
LT
595 return 0;
596
597fail_put:
598 put_disk(disk);
599fail_free:
600 kfree(cd);
601fail:
602 return error;
603}
604
605
606static void get_sectorsize(struct scsi_cd *cd)
607{
608 unsigned char cmd[10];
609 unsigned char *buffer;
610 int the_result, retries = 3;
611 int sector_size;
1da177e4
LT
612 request_queue_t *queue;
613
614 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
615 if (!buffer)
616 goto Enomem;
1da177e4
LT
617
618 do {
619 cmd[0] = READ_CAPACITY;
620 memset((void *) &cmd[1], 0, 9);
1da177e4
LT
621 memset(buffer, 0, 8);
622
623 /* Do the command and wait.. */
820732b5
JB
624 the_result = scsi_execute_req(cd->device, cmd, DMA_FROM_DEVICE,
625 buffer, 8, NULL, SR_TIMEOUT,
626 MAX_RETRIES);
1da177e4 627
1da177e4
LT
628 retries--;
629
630 } while (the_result && retries);
631
632
1da177e4
LT
633 if (the_result) {
634 cd->capacity = 0x1fffff;
635 sector_size = 2048; /* A guess, just in case */
1da177e4
LT
636 } else {
637#if 0
638 if (cdrom_get_last_written(&cd->cdi,
639 &cd->capacity))
640#endif
641 cd->capacity = 1 + ((buffer[0] << 24) |
642 (buffer[1] << 16) |
643 (buffer[2] << 8) |
644 buffer[3]);
645 sector_size = (buffer[4] << 24) |
646 (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
647 switch (sector_size) {
648 /*
649 * HP 4020i CD-Recorder reports 2340 byte sectors
650 * Philips CD-Writers report 2352 byte sectors
651 *
652 * Use 2k sectors for them..
653 */
654 case 0:
655 case 2340:
656 case 2352:
657 sector_size = 2048;
658 /* fall through */
659 case 2048:
660 cd->capacity *= 4;
661 /* fall through */
662 case 512:
663 break;
664 default:
665 printk("%s: unsupported sector size %d.\n",
666 cd->cdi.name, sector_size);
667 cd->capacity = 0;
1da177e4
LT
668 }
669
670 cd->device->sector_size = sector_size;
671
672 /*
673 * Add this so that we have the ability to correctly gauge
674 * what the device is capable of.
675 */
1da177e4
LT
676 set_capacity(cd->disk, cd->capacity);
677 }
678
679 queue = cd->device->request_queue;
680 blk_queue_hardsect_size(queue, sector_size);
681out:
682 kfree(buffer);
683 return;
684
685Enomem:
686 cd->capacity = 0x1fffff;
51490c89 687 cd->device->sector_size = 2048; /* A guess, just in case */
1da177e4
LT
688 goto out;
689}
690
691static void get_capabilities(struct scsi_cd *cd)
692{
693 unsigned char *buffer;
694 struct scsi_mode_data data;
1da177e4 695 unsigned char cmd[MAX_COMMAND_SIZE];
820732b5 696 struct scsi_sense_hdr sshdr;
1da177e4
LT
697 unsigned int the_result;
698 int retries, rc, n;
699
0ad78200 700 static const char *loadmech[] =
1da177e4
LT
701 {
702 "caddy",
703 "tray",
704 "pop-up",
705 "",
706 "changer",
707 "cartridge changer",
708 "",
709 ""
710 };
711
1da177e4
LT
712
713 /* allocate transfer buffer */
714 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
715 if (!buffer) {
716 printk(KERN_ERR "sr: out of memory.\n");
1da177e4
LT
717 return;
718 }
719
720 /* issue TEST_UNIT_READY until the initial startup UNIT_ATTENTION
721 * conditions are gone, or a timeout happens
722 */
723 retries = 0;
724 do {
725 memset((void *)cmd, 0, MAX_COMMAND_SIZE);
726 cmd[0] = TEST_UNIT_READY;
727
820732b5
JB
728 the_result = scsi_execute_req (cd->device, cmd, DMA_NONE, NULL,
729 0, &sshdr, SR_TIMEOUT,
730 MAX_RETRIES);
1da177e4 731
1da177e4
LT
732 retries++;
733 } while (retries < 5 &&
734 (!scsi_status_is_good(the_result) ||
820732b5
JB
735 (scsi_sense_valid(&sshdr) &&
736 sshdr.sense_key == UNIT_ATTENTION)));
1da177e4
LT
737
738 /* ask for mode page 0x2a */
739 rc = scsi_mode_sense(cd->device, 0, 0x2a, buffer, 128,
1cf72699 740 SR_TIMEOUT, 3, &data, NULL);
1da177e4
LT
741
742 if (!scsi_status_is_good(rc)) {
743 /* failed, drive doesn't have capabilities mode page */
744 cd->cdi.speed = 1;
745 cd->cdi.mask |= (CDC_CD_R | CDC_CD_RW | CDC_DVD_R |
bcc1e382
CE
746 CDC_DVD | CDC_DVD_RAM |
747 CDC_SELECT_DISC | CDC_SELECT_SPEED |
748 CDC_MRW | CDC_MRW_W | CDC_RAM);
1da177e4
LT
749 kfree(buffer);
750 printk("%s: scsi-1 drive\n", cd->cdi.name);
751 return;
752 }
753
754 n = data.header_length + data.block_descriptor_length;
755 cd->cdi.speed = ((buffer[n + 8] << 8) + buffer[n + 9]) / 176;
756 cd->readcd_known = 1;
757 cd->readcd_cdda = buffer[n + 5] & 0x01;
758 /* print some capability bits */
759 printk("%s: scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n", cd->cdi.name,
760 ((buffer[n + 14] << 8) + buffer[n + 15]) / 176,
761 cd->cdi.speed,
762 buffer[n + 3] & 0x01 ? "writer " : "", /* CD Writer */
763 buffer[n + 3] & 0x20 ? "dvd-ram " : "",
764 buffer[n + 2] & 0x02 ? "cd/rw " : "", /* can read rewriteable */
765 buffer[n + 4] & 0x20 ? "xa/form2 " : "", /* can read xa/from2 */
766 buffer[n + 5] & 0x01 ? "cdda " : "", /* can read audio data */
767 loadmech[buffer[n + 6] >> 5]);
768 if ((buffer[n + 6] >> 5) == 0)
769 /* caddy drives can't close tray... */
770 cd->cdi.mask |= CDC_CLOSE_TRAY;
771 if ((buffer[n + 2] & 0x8) == 0)
772 /* not a DVD drive */
773 cd->cdi.mask |= CDC_DVD;
774 if ((buffer[n + 3] & 0x20) == 0)
775 /* can't write DVD-RAM media */
776 cd->cdi.mask |= CDC_DVD_RAM;
777 if ((buffer[n + 3] & 0x10) == 0)
778 /* can't write DVD-R media */
779 cd->cdi.mask |= CDC_DVD_R;
780 if ((buffer[n + 3] & 0x2) == 0)
781 /* can't write CD-RW media */
782 cd->cdi.mask |= CDC_CD_RW;
783 if ((buffer[n + 3] & 0x1) == 0)
784 /* can't write CD-R media */
785 cd->cdi.mask |= CDC_CD_R;
786 if ((buffer[n + 6] & 0x8) == 0)
787 /* can't eject */
788 cd->cdi.mask |= CDC_OPEN_TRAY;
789
790 if ((buffer[n + 6] >> 5) == mechtype_individual_changer ||
791 (buffer[n + 6] >> 5) == mechtype_cartridge_changer)
792 cd->cdi.capacity =
793 cdrom_number_of_slots(&cd->cdi);
794 if (cd->cdi.capacity <= 1)
795 /* not a changer */
796 cd->cdi.mask |= CDC_SELECT_DISC;
797 /*else I don't think it can close its tray
798 cd->cdi.mask |= CDC_CLOSE_TRAY; */
799
800 /*
801 * if DVD-RAM, MRW-W or CD-RW, we are randomly writable
802 */
803 if ((cd->cdi.mask & (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) !=
804 (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) {
805 cd->device->writeable = 1;
806 }
807
1da177e4
LT
808 kfree(buffer);
809}
810
811/*
812 * sr_packet() is the entry point for the generic commands generated
813 * by the Uniform CD-ROM layer.
814 */
815static int sr_packet(struct cdrom_device_info *cdi,
816 struct packet_command *cgc)
817{
818 if (cgc->timeout <= 0)
819 cgc->timeout = IOCTL_TIMEOUT;
820
821 sr_do_ioctl(cdi->handle, cgc);
822
823 return cgc->stat;
824}
825
826/**
827 * sr_kref_release - Called to free the scsi_cd structure
828 * @kref: pointer to embedded kref
829 *
0b950672 830 * sr_ref_mutex must be held entering this routine. Because it is
1da177e4
LT
831 * called on last put, you should always use the scsi_cd_get()
832 * scsi_cd_put() helpers which manipulate the semaphore directly
833 * and never do a direct kref_put().
834 **/
835static void sr_kref_release(struct kref *kref)
836{
837 struct scsi_cd *cd = container_of(kref, struct scsi_cd, kref);
838 struct gendisk *disk = cd->disk;
839
840 spin_lock(&sr_index_lock);
841 clear_bit(disk->first_minor, sr_index_bits);
842 spin_unlock(&sr_index_lock);
843
844 unregister_cdrom(&cd->cdi);
845
846 disk->private_data = NULL;
847
848 put_disk(disk);
849
850 kfree(cd);
851}
852
853static int sr_remove(struct device *dev)
854{
855 struct scsi_cd *cd = dev_get_drvdata(dev);
856
857 del_gendisk(cd->disk);
858
0b950672 859 mutex_lock(&sr_ref_mutex);
1da177e4 860 kref_put(&cd->kref, sr_kref_release);
0b950672 861 mutex_unlock(&sr_ref_mutex);
1da177e4
LT
862
863 return 0;
864}
865
866static int __init init_sr(void)
867{
868 int rc;
869
870 rc = register_blkdev(SCSI_CDROM_MAJOR, "sr");
871 if (rc)
872 return rc;
873 return scsi_register_driver(&sr_template.gendrv);
874}
875
876static void __exit exit_sr(void)
877{
878 scsi_unregister_driver(&sr_template.gendrv);
879 unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
880}
881
882module_init(init_sr);
883module_exit(exit_sr);
884MODULE_LICENSE("GPL");