]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/scsi/sr.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/mchehab/v4l-dvb
[net-next-2.6.git] / drivers / scsi / sr.c
CommitLineData
1da177e4
LT
1/*
2 * sr.c Copyright (C) 1992 David Giller
3 * Copyright (C) 1993, 1994, 1995, 1999 Eric Youngdale
4 *
5 * adapted from:
6 * sd.c Copyright (C) 1992 Drew Eckhardt
7 * Linux scsi disk driver by
8 * Drew Eckhardt <drew@colorado.edu>
9 *
10 * Modified by Eric Youngdale ericy@andante.org to
11 * add scatter-gather, multiple outstanding request, and other
12 * enhancements.
13 *
14 * Modified by Eric Youngdale eric@andante.org to support loadable
15 * low-level scsi drivers.
16 *
17 * Modified by Thomas Quinot thomas@melchior.cuivre.fdn.fr to
18 * provide auto-eject.
19 *
20 * Modified by Gerd Knorr <kraxel@cs.tu-berlin.de> to support the
21 * generic cdrom interface
22 *
23 * Modified by Jens Axboe <axboe@suse.de> - Uniform sr_packet()
24 * interface, capabilities probe additions, ioctl cleanups, etc.
25 *
26 * Modified by Richard Gooch <rgooch@atnf.csiro.au> to support devfs
27 *
28 * Modified by Jens Axboe <axboe@suse.de> - support DVD-RAM
29 * transparently and lose the GHOST hack
30 *
31 * Modified by Arnaldo Carvalho de Melo <acme@conectiva.com.br>
32 * check resource allocation in sr_init and some cleanups
33 */
34
35#include <linux/module.h>
36#include <linux/fs.h>
37#include <linux/kernel.h>
1da177e4
LT
38#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>
0b950672 46#include <linux/mutex.h>
1da177e4
LT
47#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>
820732b5 53#include <scsi/scsi_cmnd.h>
1da177e4
LT
54#include <scsi/scsi_eh.h>
55#include <scsi/scsi_host.h>
56#include <scsi/scsi_ioctl.h> /* For the door lock/unlock commands */
1da177e4
LT
57
58#include "scsi_logging.h"
59#include "sr.h"
60
61
f018fa55
RH
62MODULE_DESCRIPTION("SCSI cdrom (sr) driver");
63MODULE_LICENSE("GPL");
64MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_CDROM_MAJOR);
d7b8bcb0
MT
65MODULE_ALIAS_SCSI_DEVICE(TYPE_ROM);
66MODULE_ALIAS_SCSI_DEVICE(TYPE_WORM);
f018fa55 67
1da177e4
LT
68#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| \
6a2900b6 75 CDC_PLAY_AUDIO|CDC_RESET|CDC_DRIVE_STATUS| \
1da177e4
LT
76 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 *);
7b3d9545 81static int sr_done(struct scsi_cmnd *);
1da177e4
LT
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 },
7b3d9545 90 .done = sr_done,
1da177e4
LT
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) */
0b950672 99static DEFINE_MUTEX(sr_ref_mutex);
1da177e4
LT
100
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,
1da177e4
LT
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
44818efb 178static int sr_media_change(struct cdrom_device_info *cdi, int slot)
1da177e4
LT
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/*
7b3d9545 213 * sr_done is the interrupt routine for the device driver.
1da177e4 214 *
7b3d9545 215 * It will be notified on the end of a SCSI read / write, and will take one
1da177e4
LT
216 * of several actions based on success or failure.
217 */
7b3d9545 218static int sr_done(struct scsi_cmnd *SCpnt)
1da177e4
LT
219{
220 int result = SCpnt->result;
5d5ff44f 221 int this_count = SCpnt->request_bufflen;
1da177e4
LT
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
7b3d9545 291 return good_bytes;
1da177e4
LT
292}
293
7f9a6bc4 294static int sr_prep_fn(struct request_queue *q, struct request *rq)
1da177e4
LT
295{
296 int block=0, this_count, s_size, timeout = SR_TIMEOUT;
7f9a6bc4
JB
297 struct scsi_cd *cd;
298 struct scsi_cmnd *SCpnt;
299 struct scsi_device *sdp = q->queuedata;
300 int ret;
301
302 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
303 ret = scsi_setup_blk_pc_cmnd(sdp, rq);
304 goto out;
305 } else if (rq->cmd_type != REQ_TYPE_FS) {
306 ret = BLKPREP_KILL;
307 goto out;
308 }
309 ret = scsi_setup_fs_cmnd(sdp, rq);
310 if (ret != BLKPREP_OK)
311 goto out;
312 SCpnt = rq->special;
313 cd = scsi_cd(rq->rq_disk);
314
315 /* from here on until we're complete, any goto out
316 * is used for a killable error condition */
317 ret = BLKPREP_KILL;
1da177e4
LT
318
319 SCSI_LOG_HLQUEUE(1, printk("Doing sr request, dev = %s, block = %d\n",
320 cd->disk->disk_name, block));
321
322 if (!cd->device || !scsi_device_online(cd->device)) {
323 SCSI_LOG_HLQUEUE(2, printk("Finishing %ld sectors\n",
7f9a6bc4 324 rq->nr_sectors));
1da177e4 325 SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt));
7f9a6bc4 326 goto out;
1da177e4
LT
327 }
328
329 if (cd->device->changed) {
330 /*
331 * quietly refuse to do anything to a changed disc until the
332 * changed bit has been reset
333 */
7f9a6bc4 334 goto out;
1da177e4
LT
335 }
336
1da177e4
LT
337 /*
338 * we do lazy blocksize switching (when reading XA sectors,
339 * see CDROMREADMODE2 ioctl)
340 */
341 s_size = cd->device->sector_size;
342 if (s_size > 2048) {
343 if (!in_interrupt())
344 sr_set_blocklength(cd, 2048);
345 else
346 printk("sr: can't switch blocksize: in interrupt\n");
347 }
348
349 if (s_size != 512 && s_size != 1024 && s_size != 2048) {
3bf743e7 350 scmd_printk(KERN_ERR, SCpnt, "bad sector size %d\n", s_size);
7f9a6bc4 351 goto out;
1da177e4
LT
352 }
353
7f9a6bc4 354 if (rq_data_dir(rq) == WRITE) {
1da177e4 355 if (!cd->device->writeable)
7f9a6bc4 356 goto out;
1da177e4
LT
357 SCpnt->cmnd[0] = WRITE_10;
358 SCpnt->sc_data_direction = DMA_TO_DEVICE;
359 cd->cdi.media_written = 1;
7f9a6bc4 360 } else if (rq_data_dir(rq) == READ) {
1da177e4
LT
361 SCpnt->cmnd[0] = READ_10;
362 SCpnt->sc_data_direction = DMA_FROM_DEVICE;
363 } else {
7f9a6bc4
JB
364 blk_dump_rq_flags(rq, "Unknown sr command");
365 goto out;
1da177e4
LT
366 }
367
368 {
369 struct scatterlist *sg = SCpnt->request_buffer;
370 int i, size = 0;
371 for (i = 0; i < SCpnt->use_sg; i++)
372 size += sg[i].length;
373
374 if (size != SCpnt->request_bufflen && SCpnt->use_sg) {
3bf743e7
JG
375 scmd_printk(KERN_ERR, SCpnt,
376 "mismatch count %d, bytes %d\n",
377 size, SCpnt->request_bufflen);
1da177e4 378 if (SCpnt->request_bufflen > size)
631c228c 379 SCpnt->request_bufflen = size;
1da177e4
LT
380 }
381 }
382
383 /*
384 * request doesn't start on hw block boundary, add scatter pads
385 */
7f9a6bc4 386 if (((unsigned int)rq->sector % (s_size >> 9)) ||
1da177e4 387 (SCpnt->request_bufflen % s_size)) {
3bf743e7 388 scmd_printk(KERN_NOTICE, SCpnt, "unaligned transfer\n");
7f9a6bc4 389 goto out;
1da177e4
LT
390 }
391
392 this_count = (SCpnt->request_bufflen >> 9) / (s_size >> 9);
393
394
395 SCSI_LOG_HLQUEUE(2, printk("%s : %s %d/%ld 512 byte blocks.\n",
396 cd->cdi.name,
7f9a6bc4 397 (rq_data_dir(rq) == WRITE) ?
1da177e4 398 "writing" : "reading",
7f9a6bc4 399 this_count, rq->nr_sectors));
1da177e4
LT
400
401 SCpnt->cmnd[1] = 0;
7f9a6bc4 402 block = (unsigned int)rq->sector / (s_size >> 9);
1da177e4
LT
403
404 if (this_count > 0xffff) {
405 this_count = 0xffff;
631c228c 406 SCpnt->request_bufflen = this_count * s_size;
1da177e4
LT
407 }
408
409 SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
410 SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
411 SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
412 SCpnt->cmnd[5] = (unsigned char) block & 0xff;
413 SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
414 SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
415 SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
416
417 /*
418 * We shouldn't disconnect in the middle of a sector, so with a dumb
419 * host adapter, it's safe to assume that we can at least transfer
420 * this many bytes between each connect / disconnect.
421 */
422 SCpnt->transfersize = cd->device->sector_size;
423 SCpnt->underflow = this_count << 9;
1da177e4
LT
424 SCpnt->allowed = MAX_RETRIES;
425 SCpnt->timeout_per_command = timeout;
426
1da177e4
LT
427 /*
428 * This indicates that the command is ready from our end to be
429 * queued.
430 */
7f9a6bc4
JB
431 ret = BLKPREP_OK;
432 out:
433 return scsi_prep_return(q, rq, ret);
1da177e4
LT
434}
435
436static int sr_block_open(struct inode *inode, struct file *file)
437{
438 struct gendisk *disk = inode->i_bdev->bd_disk;
80d904c4 439 struct scsi_cd *cd;
1da177e4
LT
440 int ret = 0;
441
442 if(!(cd = scsi_cd_get(disk)))
443 return -ENXIO;
444
445 if((ret = cdrom_open(&cd->cdi, inode, file)) != 0)
446 scsi_cd_put(cd);
447
448 return ret;
449}
450
451static int sr_block_release(struct inode *inode, struct file *file)
452{
453 int ret;
454 struct scsi_cd *cd = scsi_cd(inode->i_bdev->bd_disk);
455 ret = cdrom_release(&cd->cdi, file);
456 if(ret)
457 return ret;
458
459 scsi_cd_put(cd);
460
461 return 0;
462}
463
464static int sr_block_ioctl(struct inode *inode, struct file *file, unsigned cmd,
465 unsigned long arg)
466{
467 struct scsi_cd *cd = scsi_cd(inode->i_bdev->bd_disk);
468 struct scsi_device *sdev = cd->device;
6a2900b6
CH
469 void __user *argp = (void __user *)arg;
470 int ret;
1da177e4 471
6a2900b6
CH
472 /*
473 * Send SCSI addressing ioctls directly to mid level, send other
474 * ioctls to cdrom/block level.
475 */
476 switch (cmd) {
477 case SCSI_IOCTL_GET_IDLUN:
478 case SCSI_IOCTL_GET_BUS_NUMBER:
479 return scsi_ioctl(sdev, cmd, argp);
1da177e4 480 }
6a2900b6
CH
481
482 ret = cdrom_ioctl(file, &cd->cdi, inode, cmd, arg);
6397256b 483 if (ret != -ENOSYS)
6a2900b6
CH
484 return ret;
485
486 /*
487 * ENODEV means that we didn't recognise the ioctl, or that we
488 * cannot execute it in the current device state. In either
489 * case fall through to scsi_ioctl, which will return ENDOEV again
490 * if it doesn't recognise the ioctl
491 */
492 ret = scsi_nonblockable_ioctl(sdev, cmd, argp, NULL);
493 if (ret != -ENODEV)
494 return ret;
495 return scsi_ioctl(sdev, cmd, argp);
1da177e4
LT
496}
497
498static int sr_block_media_changed(struct gendisk *disk)
499{
500 struct scsi_cd *cd = scsi_cd(disk);
501 return cdrom_media_changed(&cd->cdi);
502}
503
504static struct block_device_operations sr_bdops =
505{
506 .owner = THIS_MODULE,
507 .open = sr_block_open,
508 .release = sr_block_release,
509 .ioctl = sr_block_ioctl,
510 .media_changed = sr_block_media_changed,
511 /*
512 * No compat_ioctl for now because sr_block_ioctl never
513 * seems to pass arbitary ioctls down to host drivers.
514 */
515};
516
517static int sr_open(struct cdrom_device_info *cdi, int purpose)
518{
519 struct scsi_cd *cd = cdi->handle;
520 struct scsi_device *sdev = cd->device;
521 int retval;
522
523 /*
524 * If the device is in error recovery, wait until it is done.
525 * If the device is offline, then disallow any access to it.
526 */
527 retval = -ENXIO;
528 if (!scsi_block_when_processing_errors(sdev))
529 goto error_out;
530
1da177e4
LT
531 return 0;
532
533error_out:
534 return retval;
535}
536
537static void sr_release(struct cdrom_device_info *cdi)
538{
539 struct scsi_cd *cd = cdi->handle;
540
541 if (cd->device->sector_size > 2048)
542 sr_set_blocklength(cd, 2048);
543
544}
545
546static int sr_probe(struct device *dev)
547{
548 struct scsi_device *sdev = to_scsi_device(dev);
549 struct gendisk *disk;
550 struct scsi_cd *cd;
551 int minor, error;
552
553 error = -ENODEV;
554 if (sdev->type != TYPE_ROM && sdev->type != TYPE_WORM)
555 goto fail;
556
557 error = -ENOMEM;
24669f75 558 cd = kzalloc(sizeof(*cd), GFP_KERNEL);
1da177e4
LT
559 if (!cd)
560 goto fail;
1da177e4
LT
561
562 kref_init(&cd->kref);
563
564 disk = alloc_disk(1);
565 if (!disk)
566 goto fail_free;
567
568 spin_lock(&sr_index_lock);
569 minor = find_first_zero_bit(sr_index_bits, SR_DISKS);
570 if (minor == SR_DISKS) {
571 spin_unlock(&sr_index_lock);
572 error = -EBUSY;
573 goto fail_put;
574 }
575 __set_bit(minor, sr_index_bits);
576 spin_unlock(&sr_index_lock);
577
578 disk->major = SCSI_CDROM_MAJOR;
579 disk->first_minor = minor;
580 sprintf(disk->disk_name, "sr%d", minor);
581 disk->fops = &sr_bdops;
582 disk->flags = GENHD_FL_CD;
583
584 cd->device = sdev;
585 cd->disk = disk;
586 cd->driver = &sr_template;
587 cd->disk = disk;
588 cd->capacity = 0x1fffff;
1da177e4
LT
589 cd->device->changed = 1; /* force recheck CD type */
590 cd->use = 1;
591 cd->readcd_known = 0;
592 cd->readcd_cdda = 0;
593
594 cd->cdi.ops = &sr_dops;
595 cd->cdi.handle = cd;
596 cd->cdi.mask = 0;
597 cd->cdi.capacity = 1;
598 sprintf(cd->cdi.name, "sr%d", minor);
599
600 sdev->sector_size = 2048; /* A guess, just in case */
601
602 /* FIXME: need to handle a get_capabilities failure properly ?? */
603 get_capabilities(cd);
7f9a6bc4 604 blk_queue_prep_rq(sdev->request_queue, sr_prep_fn);
1da177e4
LT
605 sr_vendor_init(cd);
606
1da177e4
LT
607 disk->driverfs_dev = &sdev->sdev_gendev;
608 set_capacity(disk, cd->capacity);
609 disk->private_data = &cd->driver;
610 disk->queue = sdev->request_queue;
611 cd->cdi.disk = disk;
612
613 if (register_cdrom(&cd->cdi))
614 goto fail_put;
615
616 dev_set_drvdata(dev, cd);
617 disk->flags |= GENHD_FL_REMOVABLE;
618 add_disk(disk);
619
9ccfc756
JB
620 sdev_printk(KERN_DEBUG, sdev,
621 "Attached scsi CD-ROM %s\n", cd->cdi.name);
1da177e4
LT
622 return 0;
623
624fail_put:
625 put_disk(disk);
626fail_free:
627 kfree(cd);
628fail:
629 return error;
630}
631
632
633static void get_sectorsize(struct scsi_cd *cd)
634{
635 unsigned char cmd[10];
636 unsigned char *buffer;
637 int the_result, retries = 3;
638 int sector_size;
165125e1 639 struct request_queue *queue;
1da177e4
LT
640
641 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
642 if (!buffer)
643 goto Enomem;
1da177e4
LT
644
645 do {
646 cmd[0] = READ_CAPACITY;
647 memset((void *) &cmd[1], 0, 9);
1da177e4
LT
648 memset(buffer, 0, 8);
649
650 /* Do the command and wait.. */
820732b5
JB
651 the_result = scsi_execute_req(cd->device, cmd, DMA_FROM_DEVICE,
652 buffer, 8, NULL, SR_TIMEOUT,
653 MAX_RETRIES);
1da177e4 654
1da177e4
LT
655 retries--;
656
657 } while (the_result && retries);
658
659
1da177e4
LT
660 if (the_result) {
661 cd->capacity = 0x1fffff;
662 sector_size = 2048; /* A guess, just in case */
1da177e4
LT
663 } else {
664#if 0
665 if (cdrom_get_last_written(&cd->cdi,
666 &cd->capacity))
667#endif
668 cd->capacity = 1 + ((buffer[0] << 24) |
669 (buffer[1] << 16) |
670 (buffer[2] << 8) |
671 buffer[3]);
672 sector_size = (buffer[4] << 24) |
673 (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
674 switch (sector_size) {
675 /*
676 * HP 4020i CD-Recorder reports 2340 byte sectors
677 * Philips CD-Writers report 2352 byte sectors
678 *
679 * Use 2k sectors for them..
680 */
681 case 0:
682 case 2340:
683 case 2352:
684 sector_size = 2048;
685 /* fall through */
686 case 2048:
687 cd->capacity *= 4;
688 /* fall through */
689 case 512:
690 break;
691 default:
692 printk("%s: unsupported sector size %d.\n",
693 cd->cdi.name, sector_size);
694 cd->capacity = 0;
1da177e4
LT
695 }
696
697 cd->device->sector_size = sector_size;
698
699 /*
700 * Add this so that we have the ability to correctly gauge
701 * what the device is capable of.
702 */
1da177e4
LT
703 set_capacity(cd->disk, cd->capacity);
704 }
705
706 queue = cd->device->request_queue;
707 blk_queue_hardsect_size(queue, sector_size);
708out:
709 kfree(buffer);
710 return;
711
712Enomem:
713 cd->capacity = 0x1fffff;
51490c89 714 cd->device->sector_size = 2048; /* A guess, just in case */
1da177e4
LT
715 goto out;
716}
717
718static void get_capabilities(struct scsi_cd *cd)
719{
720 unsigned char *buffer;
721 struct scsi_mode_data data;
1da177e4 722 unsigned char cmd[MAX_COMMAND_SIZE];
820732b5 723 struct scsi_sense_hdr sshdr;
1da177e4
LT
724 unsigned int the_result;
725 int retries, rc, n;
726
0ad78200 727 static const char *loadmech[] =
1da177e4
LT
728 {
729 "caddy",
730 "tray",
731 "pop-up",
732 "",
733 "changer",
734 "cartridge changer",
735 "",
736 ""
737 };
738
1da177e4
LT
739
740 /* allocate transfer buffer */
741 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
742 if (!buffer) {
743 printk(KERN_ERR "sr: out of memory.\n");
1da177e4
LT
744 return;
745 }
746
747 /* issue TEST_UNIT_READY until the initial startup UNIT_ATTENTION
748 * conditions are gone, or a timeout happens
749 */
750 retries = 0;
751 do {
752 memset((void *)cmd, 0, MAX_COMMAND_SIZE);
753 cmd[0] = TEST_UNIT_READY;
754
820732b5
JB
755 the_result = scsi_execute_req (cd->device, cmd, DMA_NONE, NULL,
756 0, &sshdr, SR_TIMEOUT,
757 MAX_RETRIES);
1da177e4 758
1da177e4
LT
759 retries++;
760 } while (retries < 5 &&
761 (!scsi_status_is_good(the_result) ||
820732b5
JB
762 (scsi_sense_valid(&sshdr) &&
763 sshdr.sense_key == UNIT_ATTENTION)));
1da177e4
LT
764
765 /* ask for mode page 0x2a */
766 rc = scsi_mode_sense(cd->device, 0, 0x2a, buffer, 128,
1cf72699 767 SR_TIMEOUT, 3, &data, NULL);
1da177e4
LT
768
769 if (!scsi_status_is_good(rc)) {
770 /* failed, drive doesn't have capabilities mode page */
771 cd->cdi.speed = 1;
772 cd->cdi.mask |= (CDC_CD_R | CDC_CD_RW | CDC_DVD_R |
bcc1e382
CE
773 CDC_DVD | CDC_DVD_RAM |
774 CDC_SELECT_DISC | CDC_SELECT_SPEED |
775 CDC_MRW | CDC_MRW_W | CDC_RAM);
1da177e4
LT
776 kfree(buffer);
777 printk("%s: scsi-1 drive\n", cd->cdi.name);
778 return;
779 }
780
781 n = data.header_length + data.block_descriptor_length;
782 cd->cdi.speed = ((buffer[n + 8] << 8) + buffer[n + 9]) / 176;
783 cd->readcd_known = 1;
784 cd->readcd_cdda = buffer[n + 5] & 0x01;
785 /* print some capability bits */
786 printk("%s: scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n", cd->cdi.name,
787 ((buffer[n + 14] << 8) + buffer[n + 15]) / 176,
788 cd->cdi.speed,
789 buffer[n + 3] & 0x01 ? "writer " : "", /* CD Writer */
790 buffer[n + 3] & 0x20 ? "dvd-ram " : "",
791 buffer[n + 2] & 0x02 ? "cd/rw " : "", /* can read rewriteable */
792 buffer[n + 4] & 0x20 ? "xa/form2 " : "", /* can read xa/from2 */
793 buffer[n + 5] & 0x01 ? "cdda " : "", /* can read audio data */
794 loadmech[buffer[n + 6] >> 5]);
795 if ((buffer[n + 6] >> 5) == 0)
796 /* caddy drives can't close tray... */
797 cd->cdi.mask |= CDC_CLOSE_TRAY;
798 if ((buffer[n + 2] & 0x8) == 0)
799 /* not a DVD drive */
800 cd->cdi.mask |= CDC_DVD;
801 if ((buffer[n + 3] & 0x20) == 0)
802 /* can't write DVD-RAM media */
803 cd->cdi.mask |= CDC_DVD_RAM;
804 if ((buffer[n + 3] & 0x10) == 0)
805 /* can't write DVD-R media */
806 cd->cdi.mask |= CDC_DVD_R;
807 if ((buffer[n + 3] & 0x2) == 0)
808 /* can't write CD-RW media */
809 cd->cdi.mask |= CDC_CD_RW;
810 if ((buffer[n + 3] & 0x1) == 0)
811 /* can't write CD-R media */
812 cd->cdi.mask |= CDC_CD_R;
813 if ((buffer[n + 6] & 0x8) == 0)
814 /* can't eject */
815 cd->cdi.mask |= CDC_OPEN_TRAY;
816
817 if ((buffer[n + 6] >> 5) == mechtype_individual_changer ||
818 (buffer[n + 6] >> 5) == mechtype_cartridge_changer)
819 cd->cdi.capacity =
820 cdrom_number_of_slots(&cd->cdi);
821 if (cd->cdi.capacity <= 1)
822 /* not a changer */
823 cd->cdi.mask |= CDC_SELECT_DISC;
824 /*else I don't think it can close its tray
825 cd->cdi.mask |= CDC_CLOSE_TRAY; */
826
827 /*
828 * if DVD-RAM, MRW-W or CD-RW, we are randomly writable
829 */
830 if ((cd->cdi.mask & (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) !=
831 (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) {
832 cd->device->writeable = 1;
833 }
834
1da177e4
LT
835 kfree(buffer);
836}
837
838/*
839 * sr_packet() is the entry point for the generic commands generated
840 * by the Uniform CD-ROM layer.
841 */
842static int sr_packet(struct cdrom_device_info *cdi,
843 struct packet_command *cgc)
844{
845 if (cgc->timeout <= 0)
846 cgc->timeout = IOCTL_TIMEOUT;
847
848 sr_do_ioctl(cdi->handle, cgc);
849
850 return cgc->stat;
851}
852
853/**
854 * sr_kref_release - Called to free the scsi_cd structure
855 * @kref: pointer to embedded kref
856 *
0b950672 857 * sr_ref_mutex must be held entering this routine. Because it is
1da177e4
LT
858 * called on last put, you should always use the scsi_cd_get()
859 * scsi_cd_put() helpers which manipulate the semaphore directly
860 * and never do a direct kref_put().
861 **/
862static void sr_kref_release(struct kref *kref)
863{
864 struct scsi_cd *cd = container_of(kref, struct scsi_cd, kref);
865 struct gendisk *disk = cd->disk;
866
867 spin_lock(&sr_index_lock);
868 clear_bit(disk->first_minor, sr_index_bits);
869 spin_unlock(&sr_index_lock);
870
871 unregister_cdrom(&cd->cdi);
872
873 disk->private_data = NULL;
874
875 put_disk(disk);
876
877 kfree(cd);
878}
879
880static int sr_remove(struct device *dev)
881{
882 struct scsi_cd *cd = dev_get_drvdata(dev);
883
884 del_gendisk(cd->disk);
885
0b950672 886 mutex_lock(&sr_ref_mutex);
1da177e4 887 kref_put(&cd->kref, sr_kref_release);
0b950672 888 mutex_unlock(&sr_ref_mutex);
1da177e4
LT
889
890 return 0;
891}
892
893static int __init init_sr(void)
894{
895 int rc;
896
897 rc = register_blkdev(SCSI_CDROM_MAJOR, "sr");
898 if (rc)
899 return rc;
da3962fe
AM
900 rc = scsi_register_driver(&sr_template.gendrv);
901 if (rc)
902 unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
903
904 return rc;
1da177e4
LT
905}
906
907static void __exit exit_sr(void)
908{
909 scsi_unregister_driver(&sr_template.gendrv);
910 unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
911}
912
913module_init(init_sr);
914module_exit(exit_sr);
915MODULE_LICENSE("GPL");