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