xref: /linux/drivers/scsi/sr.c (revision 83d5cde47dedf01b6a4a4331882cbc0a7eea3c2e)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  sr.c Copyright (C) 1992 David Giller
31da177e4SLinus Torvalds  *           Copyright (C) 1993, 1994, 1995, 1999 Eric Youngdale
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *  adapted from:
61da177e4SLinus Torvalds  *      sd.c Copyright (C) 1992 Drew Eckhardt
71da177e4SLinus Torvalds  *      Linux scsi disk driver by
81da177e4SLinus Torvalds  *              Drew Eckhardt <drew@colorado.edu>
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  *	Modified by Eric Youngdale ericy@andante.org to
111da177e4SLinus Torvalds  *	add scatter-gather, multiple outstanding request, and other
121da177e4SLinus Torvalds  *	enhancements.
131da177e4SLinus Torvalds  *
141da177e4SLinus Torvalds  *      Modified by Eric Youngdale eric@andante.org to support loadable
151da177e4SLinus Torvalds  *      low-level scsi drivers.
161da177e4SLinus Torvalds  *
171da177e4SLinus Torvalds  *      Modified by Thomas Quinot thomas@melchior.cuivre.fdn.fr to
181da177e4SLinus Torvalds  *      provide auto-eject.
191da177e4SLinus Torvalds  *
201da177e4SLinus Torvalds  *      Modified by Gerd Knorr <kraxel@cs.tu-berlin.de> to support the
211da177e4SLinus Torvalds  *      generic cdrom interface
221da177e4SLinus Torvalds  *
231da177e4SLinus Torvalds  *      Modified by Jens Axboe <axboe@suse.de> - Uniform sr_packet()
241da177e4SLinus Torvalds  *      interface, capabilities probe additions, ioctl cleanups, etc.
251da177e4SLinus Torvalds  *
261da177e4SLinus Torvalds  *	Modified by Richard Gooch <rgooch@atnf.csiro.au> to support devfs
271da177e4SLinus Torvalds  *
281da177e4SLinus Torvalds  *	Modified by Jens Axboe <axboe@suse.de> - support DVD-RAM
291da177e4SLinus Torvalds  *	transparently and lose the GHOST hack
301da177e4SLinus Torvalds  *
311da177e4SLinus Torvalds  *	Modified by Arnaldo Carvalho de Melo <acme@conectiva.com.br>
321da177e4SLinus Torvalds  *	check resource allocation in sr_init and some cleanups
331da177e4SLinus Torvalds  */
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds #include <linux/module.h>
361da177e4SLinus Torvalds #include <linux/fs.h>
371da177e4SLinus Torvalds #include <linux/kernel.h>
381da177e4SLinus Torvalds #include <linux/mm.h>
391da177e4SLinus Torvalds #include <linux/bio.h>
401da177e4SLinus Torvalds #include <linux/string.h>
411da177e4SLinus Torvalds #include <linux/errno.h>
421da177e4SLinus Torvalds #include <linux/cdrom.h>
431da177e4SLinus Torvalds #include <linux/interrupt.h>
441da177e4SLinus Torvalds #include <linux/init.h>
451da177e4SLinus Torvalds #include <linux/blkdev.h>
460b950672SArjan van de Ven #include <linux/mutex.h>
471da177e4SLinus Torvalds #include <asm/uaccess.h>
481da177e4SLinus Torvalds 
491da177e4SLinus Torvalds #include <scsi/scsi.h>
501da177e4SLinus Torvalds #include <scsi/scsi_dbg.h>
511da177e4SLinus Torvalds #include <scsi/scsi_device.h>
521da177e4SLinus Torvalds #include <scsi/scsi_driver.h>
53820732b5SJames Bottomley #include <scsi/scsi_cmnd.h>
541da177e4SLinus Torvalds #include <scsi/scsi_eh.h>
551da177e4SLinus Torvalds #include <scsi/scsi_host.h>
561da177e4SLinus Torvalds #include <scsi/scsi_ioctl.h>	/* For the door lock/unlock commands */
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds #include "scsi_logging.h"
591da177e4SLinus Torvalds #include "sr.h"
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds 
62f018fa55SRene Herman MODULE_DESCRIPTION("SCSI cdrom (sr) driver");
63f018fa55SRene Herman MODULE_LICENSE("GPL");
64f018fa55SRene Herman MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_CDROM_MAJOR);
65d7b8bcb0SMichael Tokarev MODULE_ALIAS_SCSI_DEVICE(TYPE_ROM);
66d7b8bcb0SMichael Tokarev MODULE_ALIAS_SCSI_DEVICE(TYPE_WORM);
67f018fa55SRene Herman 
681da177e4SLinus Torvalds #define SR_DISKS	256
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds #define SR_CAPABILITIES \
711da177e4SLinus Torvalds 	(CDC_CLOSE_TRAY|CDC_OPEN_TRAY|CDC_LOCK|CDC_SELECT_SPEED| \
721da177e4SLinus Torvalds 	 CDC_SELECT_DISC|CDC_MULTI_SESSION|CDC_MCN|CDC_MEDIA_CHANGED| \
736a2900b6SChristoph Hellwig 	 CDC_PLAY_AUDIO|CDC_RESET|CDC_DRIVE_STATUS| \
741da177e4SLinus Torvalds 	 CDC_CD_R|CDC_CD_RW|CDC_DVD|CDC_DVD_R|CDC_DVD_RAM|CDC_GENERIC_PACKET| \
751da177e4SLinus Torvalds 	 CDC_MRW|CDC_MRW_W|CDC_RAM)
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds static int sr_probe(struct device *);
781da177e4SLinus Torvalds static int sr_remove(struct device *);
797b3d9545SLinus Torvalds static int sr_done(struct scsi_cmnd *);
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds static struct scsi_driver sr_template = {
821da177e4SLinus Torvalds 	.owner			= THIS_MODULE,
831da177e4SLinus Torvalds 	.gendrv = {
841da177e4SLinus Torvalds 		.name   	= "sr",
851da177e4SLinus Torvalds 		.probe		= sr_probe,
861da177e4SLinus Torvalds 		.remove		= sr_remove,
871da177e4SLinus Torvalds 	},
887b3d9545SLinus Torvalds 	.done			= sr_done,
891da177e4SLinus Torvalds };
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds static unsigned long sr_index_bits[SR_DISKS / BITS_PER_LONG];
921da177e4SLinus Torvalds static DEFINE_SPINLOCK(sr_index_lock);
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds /* This semaphore is used to mediate the 0->1 reference get in the
951da177e4SLinus Torvalds  * face of object destruction (i.e. we can't allow a get on an
961da177e4SLinus Torvalds  * object after last put) */
970b950672SArjan van de Ven static DEFINE_MUTEX(sr_ref_mutex);
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds static int sr_open(struct cdrom_device_info *, int);
1001da177e4SLinus Torvalds static void sr_release(struct cdrom_device_info *);
1011da177e4SLinus Torvalds 
1021da177e4SLinus Torvalds static void get_sectorsize(struct scsi_cd *);
1031da177e4SLinus Torvalds static void get_capabilities(struct scsi_cd *);
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds static int sr_media_change(struct cdrom_device_info *, int);
1061da177e4SLinus Torvalds static int sr_packet(struct cdrom_device_info *, struct packet_command *);
1071da177e4SLinus Torvalds 
1081da177e4SLinus Torvalds static struct cdrom_device_ops sr_dops = {
1091da177e4SLinus Torvalds 	.open			= sr_open,
1101da177e4SLinus Torvalds 	.release	 	= sr_release,
1111da177e4SLinus Torvalds 	.drive_status	 	= sr_drive_status,
1121da177e4SLinus Torvalds 	.media_changed		= sr_media_change,
1131da177e4SLinus Torvalds 	.tray_move		= sr_tray_move,
1141da177e4SLinus Torvalds 	.lock_door		= sr_lock_door,
1151da177e4SLinus Torvalds 	.select_speed		= sr_select_speed,
1161da177e4SLinus Torvalds 	.get_last_session	= sr_get_last_session,
1171da177e4SLinus Torvalds 	.get_mcn		= sr_get_mcn,
1181da177e4SLinus Torvalds 	.reset			= sr_reset,
1191da177e4SLinus Torvalds 	.audio_ioctl		= sr_audio_ioctl,
1201da177e4SLinus Torvalds 	.capability		= SR_CAPABILITIES,
1211da177e4SLinus Torvalds 	.generic_packet		= sr_packet,
1221da177e4SLinus Torvalds };
1231da177e4SLinus Torvalds 
1241da177e4SLinus Torvalds static void sr_kref_release(struct kref *kref);
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds static inline struct scsi_cd *scsi_cd(struct gendisk *disk)
1271da177e4SLinus Torvalds {
1281da177e4SLinus Torvalds 	return container_of(disk->private_data, struct scsi_cd, driver);
1291da177e4SLinus Torvalds }
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds /*
1321da177e4SLinus Torvalds  * The get and put routines for the struct scsi_cd.  Note this entity
1331da177e4SLinus Torvalds  * has a scsi_device pointer and owns a reference to this.
1341da177e4SLinus Torvalds  */
1351da177e4SLinus Torvalds static inline struct scsi_cd *scsi_cd_get(struct gendisk *disk)
1361da177e4SLinus Torvalds {
1371da177e4SLinus Torvalds 	struct scsi_cd *cd = NULL;
1381da177e4SLinus Torvalds 
1390b950672SArjan van de Ven 	mutex_lock(&sr_ref_mutex);
1401da177e4SLinus Torvalds 	if (disk->private_data == NULL)
1411da177e4SLinus Torvalds 		goto out;
1421da177e4SLinus Torvalds 	cd = scsi_cd(disk);
1431da177e4SLinus Torvalds 	kref_get(&cd->kref);
1441da177e4SLinus Torvalds 	if (scsi_device_get(cd->device))
1451da177e4SLinus Torvalds 		goto out_put;
1461da177e4SLinus Torvalds 	goto out;
1471da177e4SLinus Torvalds 
1481da177e4SLinus Torvalds  out_put:
1491da177e4SLinus Torvalds 	kref_put(&cd->kref, sr_kref_release);
1501da177e4SLinus Torvalds 	cd = NULL;
1511da177e4SLinus Torvalds  out:
1520b950672SArjan van de Ven 	mutex_unlock(&sr_ref_mutex);
1531da177e4SLinus Torvalds 	return cd;
1541da177e4SLinus Torvalds }
1551da177e4SLinus Torvalds 
156858119e1SArjan van de Ven static void scsi_cd_put(struct scsi_cd *cd)
1571da177e4SLinus Torvalds {
1581da177e4SLinus Torvalds 	struct scsi_device *sdev = cd->device;
1591da177e4SLinus Torvalds 
1600b950672SArjan van de Ven 	mutex_lock(&sr_ref_mutex);
1611da177e4SLinus Torvalds 	kref_put(&cd->kref, sr_kref_release);
1621da177e4SLinus Torvalds 	scsi_device_put(sdev);
1630b950672SArjan van de Ven 	mutex_unlock(&sr_ref_mutex);
1641da177e4SLinus Torvalds }
1651da177e4SLinus Torvalds 
16638582a62SJames Bottomley /* identical to scsi_test_unit_ready except that it doesn't
16738582a62SJames Bottomley  * eat the NOT_READY returns for removable media */
16838582a62SJames Bottomley int sr_test_unit_ready(struct scsi_device *sdev, struct scsi_sense_hdr *sshdr)
16938582a62SJames Bottomley {
17038582a62SJames Bottomley 	int retries = MAX_RETRIES;
17138582a62SJames Bottomley 	int the_result;
17238582a62SJames Bottomley 	u8 cmd[] = {TEST_UNIT_READY, 0, 0, 0, 0, 0 };
17338582a62SJames Bottomley 
17438582a62SJames Bottomley 	/* issue TEST_UNIT_READY until the initial startup UNIT_ATTENTION
17538582a62SJames Bottomley 	 * conditions are gone, or a timeout happens
17638582a62SJames Bottomley 	 */
17738582a62SJames Bottomley 	do {
17838582a62SJames Bottomley 		the_result = scsi_execute_req(sdev, cmd, DMA_NONE, NULL,
17938582a62SJames Bottomley 					      0, sshdr, SR_TIMEOUT,
180f4f4e47eSFUJITA Tomonori 					      retries--, NULL);
181d1daeabfSJames Bottomley 		if (scsi_sense_valid(sshdr) &&
182d1daeabfSJames Bottomley 		    sshdr->sense_key == UNIT_ATTENTION)
183d1daeabfSJames Bottomley 			sdev->changed = 1;
18438582a62SJames Bottomley 
18538582a62SJames Bottomley 	} while (retries > 0 &&
18638582a62SJames Bottomley 		 (!scsi_status_is_good(the_result) ||
18738582a62SJames Bottomley 		  (scsi_sense_valid(sshdr) &&
18838582a62SJames Bottomley 		   sshdr->sense_key == UNIT_ATTENTION)));
18938582a62SJames Bottomley 	return the_result;
19038582a62SJames Bottomley }
19138582a62SJames Bottomley 
1921da177e4SLinus Torvalds /*
1931da177e4SLinus Torvalds  * This function checks to see if the media has been changed in the
1941da177e4SLinus Torvalds  * CDROM drive.  It is possible that we have already sensed a change,
1951da177e4SLinus Torvalds  * or the drive may have sensed one and not yet reported it.  We must
1961da177e4SLinus Torvalds  * be ready for either case. This function always reports the current
1971da177e4SLinus Torvalds  * value of the changed bit.  If flag is 0, then the changed bit is reset.
1981da177e4SLinus Torvalds  * This function could be done as an ioctl, but we would need to have
1991da177e4SLinus Torvalds  * an inode for that to work, and we do not always have one.
2001da177e4SLinus Torvalds  */
2011da177e4SLinus Torvalds 
20244818efbSAdrian Bunk static int sr_media_change(struct cdrom_device_info *cdi, int slot)
2031da177e4SLinus Torvalds {
2041da177e4SLinus Torvalds 	struct scsi_cd *cd = cdi->handle;
2051da177e4SLinus Torvalds 	int retval;
206001aac25SJames Bottomley 	struct scsi_sense_hdr *sshdr;
2071da177e4SLinus Torvalds 
2081da177e4SLinus Torvalds 	if (CDSL_CURRENT != slot) {
2091da177e4SLinus Torvalds 		/* no changer support */
2101da177e4SLinus Torvalds 		return -EINVAL;
2111da177e4SLinus Torvalds 	}
2121da177e4SLinus Torvalds 
213001aac25SJames Bottomley 	sshdr =  kzalloc(sizeof(*sshdr), GFP_KERNEL);
21438582a62SJames Bottomley 	retval = sr_test_unit_ready(cd->device, sshdr);
215001aac25SJames Bottomley 	if (retval || (scsi_sense_valid(sshdr) &&
216001aac25SJames Bottomley 		       /* 0x3a is medium not present */
217001aac25SJames Bottomley 		       sshdr->asc == 0x3a)) {
218001aac25SJames Bottomley 		/* Media not present or unable to test, unit probably not
219001aac25SJames Bottomley 		 * ready. This usually means there is no disc in the drive.
220001aac25SJames Bottomley 		 * Mark as changed, and we will figure it out later once
221001aac25SJames Bottomley 		 * the drive is available again.
222001aac25SJames Bottomley 		 */
2231da177e4SLinus Torvalds 		cd->device->changed = 1;
224285e9670SKay Sievers 		/* This will force a flush, if called from check_disk_change */
225285e9670SKay Sievers 		retval = 1;
226285e9670SKay Sievers 		goto out;
2271da177e4SLinus Torvalds 	};
2281da177e4SLinus Torvalds 
2291da177e4SLinus Torvalds 	retval = cd->device->changed;
2301da177e4SLinus Torvalds 	cd->device->changed = 0;
2311da177e4SLinus Torvalds 	/* If the disk changed, the capacity will now be different,
2321da177e4SLinus Torvalds 	 * so we force a re-read of this information */
2331da177e4SLinus Torvalds 	if (retval) {
2341da177e4SLinus Torvalds 		/* check multisession offset etc */
2351da177e4SLinus Torvalds 		sr_cd_check(cdi);
23651490c89SPete Zaitcev 		get_sectorsize(cd);
2371da177e4SLinus Torvalds 	}
238285e9670SKay Sievers 
239285e9670SKay Sievers out:
240285e9670SKay Sievers 	/* Notify userspace, that media has changed. */
241285e9670SKay Sievers 	if (retval != cd->previous_state)
242285e9670SKay Sievers 		sdev_evt_send_simple(cd->device, SDEV_EVT_MEDIA_CHANGE,
243285e9670SKay Sievers 				     GFP_KERNEL);
244285e9670SKay Sievers 	cd->previous_state = retval;
245001aac25SJames Bottomley 	kfree(sshdr);
246285e9670SKay Sievers 
2471da177e4SLinus Torvalds 	return retval;
2481da177e4SLinus Torvalds }
2491da177e4SLinus Torvalds 
2501da177e4SLinus Torvalds /*
2517b3d9545SLinus Torvalds  * sr_done is the interrupt routine for the device driver.
2521da177e4SLinus Torvalds  *
2537b3d9545SLinus Torvalds  * It will be notified on the end of a SCSI read / write, and will take one
2541da177e4SLinus Torvalds  * of several actions based on success or failure.
2551da177e4SLinus Torvalds  */
2567b3d9545SLinus Torvalds static int sr_done(struct scsi_cmnd *SCpnt)
2571da177e4SLinus Torvalds {
2581da177e4SLinus Torvalds 	int result = SCpnt->result;
25930b0c37bSBoaz Harrosh 	int this_count = scsi_bufflen(SCpnt);
2601da177e4SLinus Torvalds 	int good_bytes = (result == 0 ? this_count : 0);
2611da177e4SLinus Torvalds 	int block_sectors = 0;
2621da177e4SLinus Torvalds 	long error_sector;
2631da177e4SLinus Torvalds 	struct scsi_cd *cd = scsi_cd(SCpnt->request->rq_disk);
2641da177e4SLinus Torvalds 
2651da177e4SLinus Torvalds #ifdef DEBUG
2661da177e4SLinus Torvalds 	printk("sr.c done: %x\n", result);
2671da177e4SLinus Torvalds #endif
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds 	/*
2701da177e4SLinus Torvalds 	 * Handle MEDIUM ERRORs or VOLUME OVERFLOWs that indicate partial
2711da177e4SLinus Torvalds 	 * success.  Since this is a relatively rare error condition, no
2721da177e4SLinus Torvalds 	 * care is taken to avoid unnecessary additional work such as
2731da177e4SLinus Torvalds 	 * memcpy's that could be avoided.
2741da177e4SLinus Torvalds 	 */
2751da177e4SLinus Torvalds 	if (driver_byte(result) != 0 &&		/* An error occurred */
2761da177e4SLinus Torvalds 	    (SCpnt->sense_buffer[0] & 0x7f) == 0x70) { /* Sense current */
2771da177e4SLinus Torvalds 		switch (SCpnt->sense_buffer[2]) {
2781da177e4SLinus Torvalds 		case MEDIUM_ERROR:
2791da177e4SLinus Torvalds 		case VOLUME_OVERFLOW:
2801da177e4SLinus Torvalds 		case ILLEGAL_REQUEST:
2811da177e4SLinus Torvalds 			if (!(SCpnt->sense_buffer[0] & 0x90))
2821da177e4SLinus Torvalds 				break;
2831da177e4SLinus Torvalds 			error_sector = (SCpnt->sense_buffer[3] << 24) |
2841da177e4SLinus Torvalds 				(SCpnt->sense_buffer[4] << 16) |
2851da177e4SLinus Torvalds 				(SCpnt->sense_buffer[5] << 8) |
2861da177e4SLinus Torvalds 				SCpnt->sense_buffer[6];
2871da177e4SLinus Torvalds 			if (SCpnt->request->bio != NULL)
2881da177e4SLinus Torvalds 				block_sectors =
2891da177e4SLinus Torvalds 					bio_sectors(SCpnt->request->bio);
2901da177e4SLinus Torvalds 			if (block_sectors < 4)
2911da177e4SLinus Torvalds 				block_sectors = 4;
2921da177e4SLinus Torvalds 			if (cd->device->sector_size == 2048)
2931da177e4SLinus Torvalds 				error_sector <<= 2;
2941da177e4SLinus Torvalds 			error_sector &= ~(block_sectors - 1);
29583096ebfSTejun Heo 			good_bytes = (error_sector -
29683096ebfSTejun Heo 				      blk_rq_pos(SCpnt->request)) << 9;
2971da177e4SLinus Torvalds 			if (good_bytes < 0 || good_bytes >= this_count)
2981da177e4SLinus Torvalds 				good_bytes = 0;
2991da177e4SLinus Torvalds 			/*
3001da177e4SLinus Torvalds 			 * The SCSI specification allows for the value
3011da177e4SLinus Torvalds 			 * returned by READ CAPACITY to be up to 75 2K
3021da177e4SLinus Torvalds 			 * sectors past the last readable block.
3031da177e4SLinus Torvalds 			 * Therefore, if we hit a medium error within the
3041da177e4SLinus Torvalds 			 * last 75 2K sectors, we decrease the saved size
3051da177e4SLinus Torvalds 			 * value.
3061da177e4SLinus Torvalds 			 */
3071da177e4SLinus Torvalds 			if (error_sector < get_capacity(cd->disk) &&
3081da177e4SLinus Torvalds 			    cd->capacity - error_sector < 4 * 75)
3091da177e4SLinus Torvalds 				set_capacity(cd->disk, error_sector);
3101da177e4SLinus Torvalds 			break;
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds 		case RECOVERED_ERROR:
3131da177e4SLinus Torvalds 			good_bytes = this_count;
3141da177e4SLinus Torvalds 			break;
3151da177e4SLinus Torvalds 
3161da177e4SLinus Torvalds 		default:
3171da177e4SLinus Torvalds 			break;
3181da177e4SLinus Torvalds 		}
3191da177e4SLinus Torvalds 	}
3201da177e4SLinus Torvalds 
3217b3d9545SLinus Torvalds 	return good_bytes;
3221da177e4SLinus Torvalds }
3231da177e4SLinus Torvalds 
3247f9a6bc4SJames Bottomley static int sr_prep_fn(struct request_queue *q, struct request *rq)
3251da177e4SLinus Torvalds {
326242f9dcbSJens Axboe 	int block = 0, this_count, s_size;
3277f9a6bc4SJames Bottomley 	struct scsi_cd *cd;
3287f9a6bc4SJames Bottomley 	struct scsi_cmnd *SCpnt;
3297f9a6bc4SJames Bottomley 	struct scsi_device *sdp = q->queuedata;
3307f9a6bc4SJames Bottomley 	int ret;
3317f9a6bc4SJames Bottomley 
3327f9a6bc4SJames Bottomley 	if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
3337f9a6bc4SJames Bottomley 		ret = scsi_setup_blk_pc_cmnd(sdp, rq);
3347f9a6bc4SJames Bottomley 		goto out;
3357f9a6bc4SJames Bottomley 	} else if (rq->cmd_type != REQ_TYPE_FS) {
3367f9a6bc4SJames Bottomley 		ret = BLKPREP_KILL;
3377f9a6bc4SJames Bottomley 		goto out;
3387f9a6bc4SJames Bottomley 	}
3397f9a6bc4SJames Bottomley 	ret = scsi_setup_fs_cmnd(sdp, rq);
3407f9a6bc4SJames Bottomley 	if (ret != BLKPREP_OK)
3417f9a6bc4SJames Bottomley 		goto out;
3427f9a6bc4SJames Bottomley 	SCpnt = rq->special;
3437f9a6bc4SJames Bottomley 	cd = scsi_cd(rq->rq_disk);
3447f9a6bc4SJames Bottomley 
3457f9a6bc4SJames Bottomley 	/* from here on until we're complete, any goto out
3467f9a6bc4SJames Bottomley 	 * is used for a killable error condition */
3477f9a6bc4SJames Bottomley 	ret = BLKPREP_KILL;
3481da177e4SLinus Torvalds 
3491da177e4SLinus Torvalds 	SCSI_LOG_HLQUEUE(1, printk("Doing sr request, dev = %s, block = %d\n",
3501da177e4SLinus Torvalds 				cd->disk->disk_name, block));
3511da177e4SLinus Torvalds 
3521da177e4SLinus Torvalds 	if (!cd->device || !scsi_device_online(cd->device)) {
35383096ebfSTejun Heo 		SCSI_LOG_HLQUEUE(2, printk("Finishing %u sectors\n",
35483096ebfSTejun Heo 					   blk_rq_sectors(rq)));
3551da177e4SLinus Torvalds 		SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt));
3567f9a6bc4SJames Bottomley 		goto out;
3571da177e4SLinus Torvalds 	}
3581da177e4SLinus Torvalds 
3591da177e4SLinus Torvalds 	if (cd->device->changed) {
3601da177e4SLinus Torvalds 		/*
3611da177e4SLinus Torvalds 		 * quietly refuse to do anything to a changed disc until the
3621da177e4SLinus Torvalds 		 * changed bit has been reset
3631da177e4SLinus Torvalds 		 */
3647f9a6bc4SJames Bottomley 		goto out;
3651da177e4SLinus Torvalds 	}
3661da177e4SLinus Torvalds 
3671da177e4SLinus Torvalds 	/*
3681da177e4SLinus Torvalds 	 * we do lazy blocksize switching (when reading XA sectors,
3691da177e4SLinus Torvalds 	 * see CDROMREADMODE2 ioctl)
3701da177e4SLinus Torvalds 	 */
3711da177e4SLinus Torvalds 	s_size = cd->device->sector_size;
3721da177e4SLinus Torvalds 	if (s_size > 2048) {
3731da177e4SLinus Torvalds 		if (!in_interrupt())
3741da177e4SLinus Torvalds 			sr_set_blocklength(cd, 2048);
3751da177e4SLinus Torvalds 		else
3761da177e4SLinus Torvalds 			printk("sr: can't switch blocksize: in interrupt\n");
3771da177e4SLinus Torvalds 	}
3781da177e4SLinus Torvalds 
3791da177e4SLinus Torvalds 	if (s_size != 512 && s_size != 1024 && s_size != 2048) {
3803bf743e7SJeff Garzik 		scmd_printk(KERN_ERR, SCpnt, "bad sector size %d\n", s_size);
3817f9a6bc4SJames Bottomley 		goto out;
3821da177e4SLinus Torvalds 	}
3831da177e4SLinus Torvalds 
3847f9a6bc4SJames Bottomley 	if (rq_data_dir(rq) == WRITE) {
3851da177e4SLinus Torvalds 		if (!cd->device->writeable)
3867f9a6bc4SJames Bottomley 			goto out;
3871da177e4SLinus Torvalds 		SCpnt->cmnd[0] = WRITE_10;
3881da177e4SLinus Torvalds 		SCpnt->sc_data_direction = DMA_TO_DEVICE;
3891da177e4SLinus Torvalds  	 	cd->cdi.media_written = 1;
3907f9a6bc4SJames Bottomley 	} else if (rq_data_dir(rq) == READ) {
3911da177e4SLinus Torvalds 		SCpnt->cmnd[0] = READ_10;
3921da177e4SLinus Torvalds 		SCpnt->sc_data_direction = DMA_FROM_DEVICE;
3931da177e4SLinus Torvalds 	} else {
3947f9a6bc4SJames Bottomley 		blk_dump_rq_flags(rq, "Unknown sr command");
3957f9a6bc4SJames Bottomley 		goto out;
3961da177e4SLinus Torvalds 	}
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds 	{
39930b0c37bSBoaz Harrosh 		struct scatterlist *sg;
40030b0c37bSBoaz Harrosh 		int i, size = 0, sg_count = scsi_sg_count(SCpnt);
4011da177e4SLinus Torvalds 
40230b0c37bSBoaz Harrosh 		scsi_for_each_sg(SCpnt, sg, sg_count, i)
40330b0c37bSBoaz Harrosh 			size += sg->length;
40430b0c37bSBoaz Harrosh 
40530b0c37bSBoaz Harrosh 		if (size != scsi_bufflen(SCpnt)) {
4063bf743e7SJeff Garzik 			scmd_printk(KERN_ERR, SCpnt,
4079ccfc756SJames Bottomley 				"mismatch count %d, bytes %d\n",
40830b0c37bSBoaz Harrosh 				size, scsi_bufflen(SCpnt));
40930b0c37bSBoaz Harrosh 			if (scsi_bufflen(SCpnt) > size)
41030b0c37bSBoaz Harrosh 				SCpnt->sdb.length = size;
4111da177e4SLinus Torvalds 		}
4121da177e4SLinus Torvalds 	}
4131da177e4SLinus Torvalds 
4141da177e4SLinus Torvalds 	/*
4151da177e4SLinus Torvalds 	 * request doesn't start on hw block boundary, add scatter pads
4161da177e4SLinus Torvalds 	 */
41783096ebfSTejun Heo 	if (((unsigned int)blk_rq_pos(rq) % (s_size >> 9)) ||
41830b0c37bSBoaz Harrosh 	    (scsi_bufflen(SCpnt) % s_size)) {
4193bf743e7SJeff Garzik 		scmd_printk(KERN_NOTICE, SCpnt, "unaligned transfer\n");
4207f9a6bc4SJames Bottomley 		goto out;
4211da177e4SLinus Torvalds 	}
4221da177e4SLinus Torvalds 
42330b0c37bSBoaz Harrosh 	this_count = (scsi_bufflen(SCpnt) >> 9) / (s_size >> 9);
4241da177e4SLinus Torvalds 
4251da177e4SLinus Torvalds 
42683096ebfSTejun Heo 	SCSI_LOG_HLQUEUE(2, printk("%s : %s %d/%u 512 byte blocks.\n",
4271da177e4SLinus Torvalds 				cd->cdi.name,
4287f9a6bc4SJames Bottomley 				(rq_data_dir(rq) == WRITE) ?
4291da177e4SLinus Torvalds 					"writing" : "reading",
43083096ebfSTejun Heo 				this_count, blk_rq_sectors(rq)));
4311da177e4SLinus Torvalds 
4321da177e4SLinus Torvalds 	SCpnt->cmnd[1] = 0;
43383096ebfSTejun Heo 	block = (unsigned int)blk_rq_pos(rq) / (s_size >> 9);
4341da177e4SLinus Torvalds 
4351da177e4SLinus Torvalds 	if (this_count > 0xffff) {
4361da177e4SLinus Torvalds 		this_count = 0xffff;
43730b0c37bSBoaz Harrosh 		SCpnt->sdb.length = this_count * s_size;
4381da177e4SLinus Torvalds 	}
4391da177e4SLinus Torvalds 
4401da177e4SLinus Torvalds 	SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
4411da177e4SLinus Torvalds 	SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
4421da177e4SLinus Torvalds 	SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
4431da177e4SLinus Torvalds 	SCpnt->cmnd[5] = (unsigned char) block & 0xff;
4441da177e4SLinus Torvalds 	SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
4451da177e4SLinus Torvalds 	SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
4461da177e4SLinus Torvalds 	SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
4471da177e4SLinus Torvalds 
4481da177e4SLinus Torvalds 	/*
4491da177e4SLinus Torvalds 	 * We shouldn't disconnect in the middle of a sector, so with a dumb
4501da177e4SLinus Torvalds 	 * host adapter, it's safe to assume that we can at least transfer
4511da177e4SLinus Torvalds 	 * this many bytes between each connect / disconnect.
4521da177e4SLinus Torvalds 	 */
4531da177e4SLinus Torvalds 	SCpnt->transfersize = cd->device->sector_size;
4541da177e4SLinus Torvalds 	SCpnt->underflow = this_count << 9;
4551da177e4SLinus Torvalds 	SCpnt->allowed = MAX_RETRIES;
4561da177e4SLinus Torvalds 
4571da177e4SLinus Torvalds 	/*
4581da177e4SLinus Torvalds 	 * This indicates that the command is ready from our end to be
4591da177e4SLinus Torvalds 	 * queued.
4601da177e4SLinus Torvalds 	 */
4617f9a6bc4SJames Bottomley 	ret = BLKPREP_OK;
4627f9a6bc4SJames Bottomley  out:
4637f9a6bc4SJames Bottomley 	return scsi_prep_return(q, rq, ret);
4641da177e4SLinus Torvalds }
4651da177e4SLinus Torvalds 
46640cc51beSAl Viro static int sr_block_open(struct block_device *bdev, fmode_t mode)
4671da177e4SLinus Torvalds {
46840cc51beSAl Viro 	struct scsi_cd *cd = scsi_cd_get(bdev->bd_disk);
46940cc51beSAl Viro 	int ret = -ENXIO;
4701da177e4SLinus Torvalds 
47140cc51beSAl Viro 	if (cd) {
47240cc51beSAl Viro 		ret = cdrom_open(&cd->cdi, bdev, mode);
47340cc51beSAl Viro 		if (ret)
4741da177e4SLinus Torvalds 			scsi_cd_put(cd);
47540cc51beSAl Viro 	}
4761da177e4SLinus Torvalds 	return ret;
4771da177e4SLinus Torvalds }
4781da177e4SLinus Torvalds 
47940cc51beSAl Viro static int sr_block_release(struct gendisk *disk, fmode_t mode)
4801da177e4SLinus Torvalds {
48140cc51beSAl Viro 	struct scsi_cd *cd = scsi_cd(disk);
48240cc51beSAl Viro 	cdrom_release(&cd->cdi, mode);
4831da177e4SLinus Torvalds 	scsi_cd_put(cd);
4841da177e4SLinus Torvalds 	return 0;
4851da177e4SLinus Torvalds }
4861da177e4SLinus Torvalds 
48740cc51beSAl Viro static int sr_block_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
4881da177e4SLinus Torvalds 			  unsigned long arg)
4891da177e4SLinus Torvalds {
49040cc51beSAl Viro 	struct scsi_cd *cd = scsi_cd(bdev->bd_disk);
4911da177e4SLinus Torvalds 	struct scsi_device *sdev = cd->device;
4926a2900b6SChristoph Hellwig 	void __user *argp = (void __user *)arg;
4936a2900b6SChristoph Hellwig 	int ret;
4941da177e4SLinus Torvalds 
4951da177e4SLinus Torvalds 	/*
4961da177e4SLinus Torvalds 	 * Send SCSI addressing ioctls directly to mid level, send other
4971da177e4SLinus Torvalds 	 * ioctls to cdrom/block level.
4981da177e4SLinus Torvalds 	 */
4991da177e4SLinus Torvalds 	switch (cmd) {
5001da177e4SLinus Torvalds 	case SCSI_IOCTL_GET_IDLUN:
5011da177e4SLinus Torvalds 	case SCSI_IOCTL_GET_BUS_NUMBER:
5026a2900b6SChristoph Hellwig 		return scsi_ioctl(sdev, cmd, argp);
5031da177e4SLinus Torvalds 	}
5046a2900b6SChristoph Hellwig 
50540cc51beSAl Viro 	ret = cdrom_ioctl(&cd->cdi, bdev, mode, cmd, arg);
5066397256bSTejun Heo 	if (ret != -ENOSYS)
5076a2900b6SChristoph Hellwig 		return ret;
5086a2900b6SChristoph Hellwig 
5096a2900b6SChristoph Hellwig 	/*
5106a2900b6SChristoph Hellwig 	 * ENODEV means that we didn't recognise the ioctl, or that we
5116a2900b6SChristoph Hellwig 	 * cannot execute it in the current device state.  In either
5126a2900b6SChristoph Hellwig 	 * case fall through to scsi_ioctl, which will return ENDOEV again
5136a2900b6SChristoph Hellwig 	 * if it doesn't recognise the ioctl
5146a2900b6SChristoph Hellwig 	 */
51583ff6fe8SAl Viro 	ret = scsi_nonblockable_ioctl(sdev, cmd, argp,
516fd4ce1acSChristoph Hellwig 					(mode & FMODE_NDELAY) != 0);
5176a2900b6SChristoph Hellwig 	if (ret != -ENODEV)
5186a2900b6SChristoph Hellwig 		return ret;
5196a2900b6SChristoph Hellwig 	return scsi_ioctl(sdev, cmd, argp);
5201da177e4SLinus Torvalds }
5211da177e4SLinus Torvalds 
5221da177e4SLinus Torvalds static int sr_block_media_changed(struct gendisk *disk)
5231da177e4SLinus Torvalds {
5241da177e4SLinus Torvalds 	struct scsi_cd *cd = scsi_cd(disk);
5251da177e4SLinus Torvalds 	return cdrom_media_changed(&cd->cdi);
5261da177e4SLinus Torvalds }
5271da177e4SLinus Torvalds 
528*83d5cde4SAlexey Dobriyan static const struct block_device_operations sr_bdops =
5291da177e4SLinus Torvalds {
5301da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
53140cc51beSAl Viro 	.open		= sr_block_open,
53240cc51beSAl Viro 	.release	= sr_block_release,
53340cc51beSAl Viro 	.locked_ioctl	= sr_block_ioctl,
5341da177e4SLinus Torvalds 	.media_changed	= sr_block_media_changed,
5351da177e4SLinus Torvalds 	/*
5361da177e4SLinus Torvalds 	 * No compat_ioctl for now because sr_block_ioctl never
5371da177e4SLinus Torvalds 	 * seems to pass arbitary ioctls down to host drivers.
5381da177e4SLinus Torvalds 	 */
5391da177e4SLinus Torvalds };
5401da177e4SLinus Torvalds 
5411da177e4SLinus Torvalds static int sr_open(struct cdrom_device_info *cdi, int purpose)
5421da177e4SLinus Torvalds {
5431da177e4SLinus Torvalds 	struct scsi_cd *cd = cdi->handle;
5441da177e4SLinus Torvalds 	struct scsi_device *sdev = cd->device;
5451da177e4SLinus Torvalds 	int retval;
5461da177e4SLinus Torvalds 
5471da177e4SLinus Torvalds 	/*
5481da177e4SLinus Torvalds 	 * If the device is in error recovery, wait until it is done.
5491da177e4SLinus Torvalds 	 * If the device is offline, then disallow any access to it.
5501da177e4SLinus Torvalds 	 */
5511da177e4SLinus Torvalds 	retval = -ENXIO;
5521da177e4SLinus Torvalds 	if (!scsi_block_when_processing_errors(sdev))
5531da177e4SLinus Torvalds 		goto error_out;
5541da177e4SLinus Torvalds 
5551da177e4SLinus Torvalds 	return 0;
5561da177e4SLinus Torvalds 
5571da177e4SLinus Torvalds error_out:
5581da177e4SLinus Torvalds 	return retval;
5591da177e4SLinus Torvalds }
5601da177e4SLinus Torvalds 
5611da177e4SLinus Torvalds static void sr_release(struct cdrom_device_info *cdi)
5621da177e4SLinus Torvalds {
5631da177e4SLinus Torvalds 	struct scsi_cd *cd = cdi->handle;
5641da177e4SLinus Torvalds 
5651da177e4SLinus Torvalds 	if (cd->device->sector_size > 2048)
5661da177e4SLinus Torvalds 		sr_set_blocklength(cd, 2048);
5671da177e4SLinus Torvalds 
5681da177e4SLinus Torvalds }
5691da177e4SLinus Torvalds 
5701da177e4SLinus Torvalds static int sr_probe(struct device *dev)
5711da177e4SLinus Torvalds {
5721da177e4SLinus Torvalds 	struct scsi_device *sdev = to_scsi_device(dev);
5731da177e4SLinus Torvalds 	struct gendisk *disk;
5741da177e4SLinus Torvalds 	struct scsi_cd *cd;
5751da177e4SLinus Torvalds 	int minor, error;
5761da177e4SLinus Torvalds 
5771da177e4SLinus Torvalds 	error = -ENODEV;
5781da177e4SLinus Torvalds 	if (sdev->type != TYPE_ROM && sdev->type != TYPE_WORM)
5791da177e4SLinus Torvalds 		goto fail;
5801da177e4SLinus Torvalds 
5811da177e4SLinus Torvalds 	error = -ENOMEM;
58224669f75SJes Sorensen 	cd = kzalloc(sizeof(*cd), GFP_KERNEL);
5831da177e4SLinus Torvalds 	if (!cd)
5841da177e4SLinus Torvalds 		goto fail;
5851da177e4SLinus Torvalds 
5861da177e4SLinus Torvalds 	kref_init(&cd->kref);
5871da177e4SLinus Torvalds 
5881da177e4SLinus Torvalds 	disk = alloc_disk(1);
5891da177e4SLinus Torvalds 	if (!disk)
5901da177e4SLinus Torvalds 		goto fail_free;
5911da177e4SLinus Torvalds 
5921da177e4SLinus Torvalds 	spin_lock(&sr_index_lock);
5931da177e4SLinus Torvalds 	minor = find_first_zero_bit(sr_index_bits, SR_DISKS);
5941da177e4SLinus Torvalds 	if (minor == SR_DISKS) {
5951da177e4SLinus Torvalds 		spin_unlock(&sr_index_lock);
5961da177e4SLinus Torvalds 		error = -EBUSY;
5971da177e4SLinus Torvalds 		goto fail_put;
5981da177e4SLinus Torvalds 	}
5991da177e4SLinus Torvalds 	__set_bit(minor, sr_index_bits);
6001da177e4SLinus Torvalds 	spin_unlock(&sr_index_lock);
6011da177e4SLinus Torvalds 
6021da177e4SLinus Torvalds 	disk->major = SCSI_CDROM_MAJOR;
6031da177e4SLinus Torvalds 	disk->first_minor = minor;
6041da177e4SLinus Torvalds 	sprintf(disk->disk_name, "sr%d", minor);
6051da177e4SLinus Torvalds 	disk->fops = &sr_bdops;
6061da177e4SLinus Torvalds 	disk->flags = GENHD_FL_CD;
6071da177e4SLinus Torvalds 
608242f9dcbSJens Axboe 	blk_queue_rq_timeout(sdev->request_queue, SR_TIMEOUT);
609242f9dcbSJens Axboe 
6101da177e4SLinus Torvalds 	cd->device = sdev;
6111da177e4SLinus Torvalds 	cd->disk = disk;
6121da177e4SLinus Torvalds 	cd->driver = &sr_template;
6131da177e4SLinus Torvalds 	cd->disk = disk;
6141da177e4SLinus Torvalds 	cd->capacity = 0x1fffff;
6151da177e4SLinus Torvalds 	cd->device->changed = 1;	/* force recheck CD type */
616c02e6002SKay Sievers 	cd->previous_state = 1;
6171da177e4SLinus Torvalds 	cd->use = 1;
6181da177e4SLinus Torvalds 	cd->readcd_known = 0;
6191da177e4SLinus Torvalds 	cd->readcd_cdda = 0;
6201da177e4SLinus Torvalds 
6211da177e4SLinus Torvalds 	cd->cdi.ops = &sr_dops;
6221da177e4SLinus Torvalds 	cd->cdi.handle = cd;
6231da177e4SLinus Torvalds 	cd->cdi.mask = 0;
6241da177e4SLinus Torvalds 	cd->cdi.capacity = 1;
6251da177e4SLinus Torvalds 	sprintf(cd->cdi.name, "sr%d", minor);
6261da177e4SLinus Torvalds 
6271da177e4SLinus Torvalds 	sdev->sector_size = 2048;	/* A guess, just in case */
6281da177e4SLinus Torvalds 
6291da177e4SLinus Torvalds 	/* FIXME: need to handle a get_capabilities failure properly ?? */
6301da177e4SLinus Torvalds 	get_capabilities(cd);
6317f9a6bc4SJames Bottomley 	blk_queue_prep_rq(sdev->request_queue, sr_prep_fn);
6321da177e4SLinus Torvalds 	sr_vendor_init(cd);
6331da177e4SLinus Torvalds 
6341da177e4SLinus Torvalds 	disk->driverfs_dev = &sdev->sdev_gendev;
6351da177e4SLinus Torvalds 	set_capacity(disk, cd->capacity);
6361da177e4SLinus Torvalds 	disk->private_data = &cd->driver;
6371da177e4SLinus Torvalds 	disk->queue = sdev->request_queue;
6381da177e4SLinus Torvalds 	cd->cdi.disk = disk;
6391da177e4SLinus Torvalds 
6401da177e4SLinus Torvalds 	if (register_cdrom(&cd->cdi))
6411da177e4SLinus Torvalds 		goto fail_put;
6421da177e4SLinus Torvalds 
6431da177e4SLinus Torvalds 	dev_set_drvdata(dev, cd);
6441da177e4SLinus Torvalds 	disk->flags |= GENHD_FL_REMOVABLE;
6451da177e4SLinus Torvalds 	add_disk(disk);
6461da177e4SLinus Torvalds 
6479ccfc756SJames Bottomley 	sdev_printk(KERN_DEBUG, sdev,
6489ccfc756SJames Bottomley 		    "Attached scsi CD-ROM %s\n", cd->cdi.name);
6491da177e4SLinus Torvalds 	return 0;
6501da177e4SLinus Torvalds 
6511da177e4SLinus Torvalds fail_put:
6521da177e4SLinus Torvalds 	put_disk(disk);
6531da177e4SLinus Torvalds fail_free:
6541da177e4SLinus Torvalds 	kfree(cd);
6551da177e4SLinus Torvalds fail:
6561da177e4SLinus Torvalds 	return error;
6571da177e4SLinus Torvalds }
6581da177e4SLinus Torvalds 
6591da177e4SLinus Torvalds 
6601da177e4SLinus Torvalds static void get_sectorsize(struct scsi_cd *cd)
6611da177e4SLinus Torvalds {
6621da177e4SLinus Torvalds 	unsigned char cmd[10];
66362858dacSFUJITA Tomonori 	unsigned char buffer[8];
6641da177e4SLinus Torvalds 	int the_result, retries = 3;
6651da177e4SLinus Torvalds 	int sector_size;
666165125e1SJens Axboe 	struct request_queue *queue;
6671da177e4SLinus Torvalds 
6681da177e4SLinus Torvalds 	do {
6691da177e4SLinus Torvalds 		cmd[0] = READ_CAPACITY;
6701da177e4SLinus Torvalds 		memset((void *) &cmd[1], 0, 9);
67162858dacSFUJITA Tomonori 		memset(buffer, 0, sizeof(buffer));
6721da177e4SLinus Torvalds 
6731da177e4SLinus Torvalds 		/* Do the command and wait.. */
674820732b5SJames Bottomley 		the_result = scsi_execute_req(cd->device, cmd, DMA_FROM_DEVICE,
67562858dacSFUJITA Tomonori 					      buffer, sizeof(buffer), NULL,
676f4f4e47eSFUJITA Tomonori 					      SR_TIMEOUT, MAX_RETRIES, NULL);
6771da177e4SLinus Torvalds 
6781da177e4SLinus Torvalds 		retries--;
6791da177e4SLinus Torvalds 
6801da177e4SLinus Torvalds 	} while (the_result && retries);
6811da177e4SLinus Torvalds 
6821da177e4SLinus Torvalds 
6831da177e4SLinus Torvalds 	if (the_result) {
6841da177e4SLinus Torvalds 		cd->capacity = 0x1fffff;
6851da177e4SLinus Torvalds 		sector_size = 2048;	/* A guess, just in case */
6861da177e4SLinus Torvalds 	} else {
6871da177e4SLinus Torvalds #if 0
6881da177e4SLinus Torvalds 		if (cdrom_get_last_written(&cd->cdi,
6891da177e4SLinus Torvalds 					   &cd->capacity))
6901da177e4SLinus Torvalds #endif
6911da177e4SLinus Torvalds 			cd->capacity = 1 + ((buffer[0] << 24) |
6921da177e4SLinus Torvalds 						    (buffer[1] << 16) |
6931da177e4SLinus Torvalds 						    (buffer[2] << 8) |
6941da177e4SLinus Torvalds 						    buffer[3]);
6951da177e4SLinus Torvalds 		sector_size = (buffer[4] << 24) |
6961da177e4SLinus Torvalds 		    (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
6971da177e4SLinus Torvalds 		switch (sector_size) {
6981da177e4SLinus Torvalds 			/*
6991da177e4SLinus Torvalds 			 * HP 4020i CD-Recorder reports 2340 byte sectors
7001da177e4SLinus Torvalds 			 * Philips CD-Writers report 2352 byte sectors
7011da177e4SLinus Torvalds 			 *
7021da177e4SLinus Torvalds 			 * Use 2k sectors for them..
7031da177e4SLinus Torvalds 			 */
7041da177e4SLinus Torvalds 		case 0:
7051da177e4SLinus Torvalds 		case 2340:
7061da177e4SLinus Torvalds 		case 2352:
7071da177e4SLinus Torvalds 			sector_size = 2048;
7081da177e4SLinus Torvalds 			/* fall through */
7091da177e4SLinus Torvalds 		case 2048:
7101da177e4SLinus Torvalds 			cd->capacity *= 4;
7111da177e4SLinus Torvalds 			/* fall through */
7121da177e4SLinus Torvalds 		case 512:
7131da177e4SLinus Torvalds 			break;
7141da177e4SLinus Torvalds 		default:
7151da177e4SLinus Torvalds 			printk("%s: unsupported sector size %d.\n",
7161da177e4SLinus Torvalds 			       cd->cdi.name, sector_size);
7171da177e4SLinus Torvalds 			cd->capacity = 0;
7181da177e4SLinus Torvalds 		}
7191da177e4SLinus Torvalds 
7201da177e4SLinus Torvalds 		cd->device->sector_size = sector_size;
7211da177e4SLinus Torvalds 
7221da177e4SLinus Torvalds 		/*
7231da177e4SLinus Torvalds 		 * Add this so that we have the ability to correctly gauge
7241da177e4SLinus Torvalds 		 * what the device is capable of.
7251da177e4SLinus Torvalds 		 */
7261da177e4SLinus Torvalds 		set_capacity(cd->disk, cd->capacity);
7271da177e4SLinus Torvalds 	}
7281da177e4SLinus Torvalds 
7291da177e4SLinus Torvalds 	queue = cd->device->request_queue;
730e1defc4fSMartin K. Petersen 	blk_queue_logical_block_size(queue, sector_size);
7311da177e4SLinus Torvalds 
73262858dacSFUJITA Tomonori 	return;
7331da177e4SLinus Torvalds }
7341da177e4SLinus Torvalds 
7351da177e4SLinus Torvalds static void get_capabilities(struct scsi_cd *cd)
7361da177e4SLinus Torvalds {
7371da177e4SLinus Torvalds 	unsigned char *buffer;
7381da177e4SLinus Torvalds 	struct scsi_mode_data data;
739820732b5SJames Bottomley 	struct scsi_sense_hdr sshdr;
74038582a62SJames Bottomley 	int rc, n;
7411da177e4SLinus Torvalds 
7420ad78200SArjan van de Ven 	static const char *loadmech[] =
7431da177e4SLinus Torvalds 	{
7441da177e4SLinus Torvalds 		"caddy",
7451da177e4SLinus Torvalds 		"tray",
7461da177e4SLinus Torvalds 		"pop-up",
7471da177e4SLinus Torvalds 		"",
7481da177e4SLinus Torvalds 		"changer",
7491da177e4SLinus Torvalds 		"cartridge changer",
7501da177e4SLinus Torvalds 		"",
7511da177e4SLinus Torvalds 		""
7521da177e4SLinus Torvalds 	};
7531da177e4SLinus Torvalds 
7541da177e4SLinus Torvalds 
7551da177e4SLinus Torvalds 	/* allocate transfer buffer */
7561da177e4SLinus Torvalds 	buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
7571da177e4SLinus Torvalds 	if (!buffer) {
7581da177e4SLinus Torvalds 		printk(KERN_ERR "sr: out of memory.\n");
7591da177e4SLinus Torvalds 		return;
7601da177e4SLinus Torvalds 	}
7611da177e4SLinus Torvalds 
76238582a62SJames Bottomley 	/* eat unit attentions */
76338582a62SJames Bottomley 	sr_test_unit_ready(cd->device, &sshdr);
7641da177e4SLinus Torvalds 
7651da177e4SLinus Torvalds 	/* ask for mode page 0x2a */
7661da177e4SLinus Torvalds 	rc = scsi_mode_sense(cd->device, 0, 0x2a, buffer, 128,
7671cf72699SJames Bottomley 			     SR_TIMEOUT, 3, &data, NULL);
7681da177e4SLinus Torvalds 
7691da177e4SLinus Torvalds 	if (!scsi_status_is_good(rc)) {
7701da177e4SLinus Torvalds 		/* failed, drive doesn't have capabilities mode page */
7711da177e4SLinus Torvalds 		cd->cdi.speed = 1;
7721da177e4SLinus Torvalds 		cd->cdi.mask |= (CDC_CD_R | CDC_CD_RW | CDC_DVD_R |
7731da177e4SLinus Torvalds 				 CDC_DVD | CDC_DVD_RAM |
774bcc1e382SChuck Ebbert 				 CDC_SELECT_DISC | CDC_SELECT_SPEED |
775bcc1e382SChuck Ebbert 				 CDC_MRW | CDC_MRW_W | CDC_RAM);
7761da177e4SLinus Torvalds 		kfree(buffer);
7771da177e4SLinus Torvalds 		printk("%s: scsi-1 drive\n", cd->cdi.name);
7781da177e4SLinus Torvalds 		return;
7791da177e4SLinus Torvalds 	}
7801da177e4SLinus Torvalds 
7811da177e4SLinus Torvalds 	n = data.header_length + data.block_descriptor_length;
7821da177e4SLinus Torvalds 	cd->cdi.speed = ((buffer[n + 8] << 8) + buffer[n + 9]) / 176;
7831da177e4SLinus Torvalds 	cd->readcd_known = 1;
7841da177e4SLinus Torvalds 	cd->readcd_cdda = buffer[n + 5] & 0x01;
7851da177e4SLinus Torvalds 	/* print some capability bits */
7861da177e4SLinus Torvalds 	printk("%s: scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n", cd->cdi.name,
7871da177e4SLinus Torvalds 	       ((buffer[n + 14] << 8) + buffer[n + 15]) / 176,
7881da177e4SLinus Torvalds 	       cd->cdi.speed,
7891da177e4SLinus Torvalds 	       buffer[n + 3] & 0x01 ? "writer " : "", /* CD Writer */
7901da177e4SLinus Torvalds 	       buffer[n + 3] & 0x20 ? "dvd-ram " : "",
7911da177e4SLinus Torvalds 	       buffer[n + 2] & 0x02 ? "cd/rw " : "", /* can read rewriteable */
7921da177e4SLinus Torvalds 	       buffer[n + 4] & 0x20 ? "xa/form2 " : "",	/* can read xa/from2 */
7931da177e4SLinus Torvalds 	       buffer[n + 5] & 0x01 ? "cdda " : "", /* can read audio data */
7941da177e4SLinus Torvalds 	       loadmech[buffer[n + 6] >> 5]);
7951da177e4SLinus Torvalds 	if ((buffer[n + 6] >> 5) == 0)
7961da177e4SLinus Torvalds 		/* caddy drives can't close tray... */
7971da177e4SLinus Torvalds 		cd->cdi.mask |= CDC_CLOSE_TRAY;
7981da177e4SLinus Torvalds 	if ((buffer[n + 2] & 0x8) == 0)
7991da177e4SLinus Torvalds 		/* not a DVD drive */
8001da177e4SLinus Torvalds 		cd->cdi.mask |= CDC_DVD;
8011da177e4SLinus Torvalds 	if ((buffer[n + 3] & 0x20) == 0)
8021da177e4SLinus Torvalds 		/* can't write DVD-RAM media */
8031da177e4SLinus Torvalds 		cd->cdi.mask |= CDC_DVD_RAM;
8041da177e4SLinus Torvalds 	if ((buffer[n + 3] & 0x10) == 0)
8051da177e4SLinus Torvalds 		/* can't write DVD-R media */
8061da177e4SLinus Torvalds 		cd->cdi.mask |= CDC_DVD_R;
8071da177e4SLinus Torvalds 	if ((buffer[n + 3] & 0x2) == 0)
8081da177e4SLinus Torvalds 		/* can't write CD-RW media */
8091da177e4SLinus Torvalds 		cd->cdi.mask |= CDC_CD_RW;
8101da177e4SLinus Torvalds 	if ((buffer[n + 3] & 0x1) == 0)
8111da177e4SLinus Torvalds 		/* can't write CD-R media */
8121da177e4SLinus Torvalds 		cd->cdi.mask |= CDC_CD_R;
8131da177e4SLinus Torvalds 	if ((buffer[n + 6] & 0x8) == 0)
8141da177e4SLinus Torvalds 		/* can't eject */
8151da177e4SLinus Torvalds 		cd->cdi.mask |= CDC_OPEN_TRAY;
8161da177e4SLinus Torvalds 
8171da177e4SLinus Torvalds 	if ((buffer[n + 6] >> 5) == mechtype_individual_changer ||
8181da177e4SLinus Torvalds 	    (buffer[n + 6] >> 5) == mechtype_cartridge_changer)
8191da177e4SLinus Torvalds 		cd->cdi.capacity =
8201da177e4SLinus Torvalds 		    cdrom_number_of_slots(&cd->cdi);
8211da177e4SLinus Torvalds 	if (cd->cdi.capacity <= 1)
8221da177e4SLinus Torvalds 		/* not a changer */
8231da177e4SLinus Torvalds 		cd->cdi.mask |= CDC_SELECT_DISC;
8241da177e4SLinus Torvalds 	/*else    I don't think it can close its tray
8251da177e4SLinus Torvalds 		cd->cdi.mask |= CDC_CLOSE_TRAY; */
8261da177e4SLinus Torvalds 
8271da177e4SLinus Torvalds 	/*
8281da177e4SLinus Torvalds 	 * if DVD-RAM, MRW-W or CD-RW, we are randomly writable
8291da177e4SLinus Torvalds 	 */
8301da177e4SLinus Torvalds 	if ((cd->cdi.mask & (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) !=
8311da177e4SLinus Torvalds 			(CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) {
8321da177e4SLinus Torvalds 		cd->device->writeable = 1;
8331da177e4SLinus Torvalds 	}
8341da177e4SLinus Torvalds 
8351da177e4SLinus Torvalds 	kfree(buffer);
8361da177e4SLinus Torvalds }
8371da177e4SLinus Torvalds 
8381da177e4SLinus Torvalds /*
8391da177e4SLinus Torvalds  * sr_packet() is the entry point for the generic commands generated
8401da177e4SLinus Torvalds  * by the Uniform CD-ROM layer.
8411da177e4SLinus Torvalds  */
8421da177e4SLinus Torvalds static int sr_packet(struct cdrom_device_info *cdi,
8431da177e4SLinus Torvalds 		struct packet_command *cgc)
8441da177e4SLinus Torvalds {
8451da177e4SLinus Torvalds 	if (cgc->timeout <= 0)
8461da177e4SLinus Torvalds 		cgc->timeout = IOCTL_TIMEOUT;
8471da177e4SLinus Torvalds 
8481da177e4SLinus Torvalds 	sr_do_ioctl(cdi->handle, cgc);
8491da177e4SLinus Torvalds 
8501da177e4SLinus Torvalds 	return cgc->stat;
8511da177e4SLinus Torvalds }
8521da177e4SLinus Torvalds 
8531da177e4SLinus Torvalds /**
8541da177e4SLinus Torvalds  *	sr_kref_release - Called to free the scsi_cd structure
8551da177e4SLinus Torvalds  *	@kref: pointer to embedded kref
8561da177e4SLinus Torvalds  *
8570b950672SArjan van de Ven  *	sr_ref_mutex must be held entering this routine.  Because it is
8581da177e4SLinus Torvalds  *	called on last put, you should always use the scsi_cd_get()
8591da177e4SLinus Torvalds  *	scsi_cd_put() helpers which manipulate the semaphore directly
8601da177e4SLinus Torvalds  *	and never do a direct kref_put().
8611da177e4SLinus Torvalds  **/
8621da177e4SLinus Torvalds static void sr_kref_release(struct kref *kref)
8631da177e4SLinus Torvalds {
8641da177e4SLinus Torvalds 	struct scsi_cd *cd = container_of(kref, struct scsi_cd, kref);
8651da177e4SLinus Torvalds 	struct gendisk *disk = cd->disk;
8661da177e4SLinus Torvalds 
8671da177e4SLinus Torvalds 	spin_lock(&sr_index_lock);
868f331c029STejun Heo 	clear_bit(MINOR(disk_devt(disk)), sr_index_bits);
8691da177e4SLinus Torvalds 	spin_unlock(&sr_index_lock);
8701da177e4SLinus Torvalds 
8711da177e4SLinus Torvalds 	unregister_cdrom(&cd->cdi);
8721da177e4SLinus Torvalds 
8731da177e4SLinus Torvalds 	disk->private_data = NULL;
8741da177e4SLinus Torvalds 
8751da177e4SLinus Torvalds 	put_disk(disk);
8761da177e4SLinus Torvalds 
8771da177e4SLinus Torvalds 	kfree(cd);
8781da177e4SLinus Torvalds }
8791da177e4SLinus Torvalds 
8801da177e4SLinus Torvalds static int sr_remove(struct device *dev)
8811da177e4SLinus Torvalds {
8821da177e4SLinus Torvalds 	struct scsi_cd *cd = dev_get_drvdata(dev);
8831da177e4SLinus Torvalds 
884b391277aSHannes Reinecke 	blk_queue_prep_rq(cd->device->request_queue, scsi_prep_fn);
8851da177e4SLinus Torvalds 	del_gendisk(cd->disk);
8861da177e4SLinus Torvalds 
8870b950672SArjan van de Ven 	mutex_lock(&sr_ref_mutex);
8881da177e4SLinus Torvalds 	kref_put(&cd->kref, sr_kref_release);
8890b950672SArjan van de Ven 	mutex_unlock(&sr_ref_mutex);
8901da177e4SLinus Torvalds 
8911da177e4SLinus Torvalds 	return 0;
8921da177e4SLinus Torvalds }
8931da177e4SLinus Torvalds 
8941da177e4SLinus Torvalds static int __init init_sr(void)
8951da177e4SLinus Torvalds {
8961da177e4SLinus Torvalds 	int rc;
8971da177e4SLinus Torvalds 
8981da177e4SLinus Torvalds 	rc = register_blkdev(SCSI_CDROM_MAJOR, "sr");
8991da177e4SLinus Torvalds 	if (rc)
9001da177e4SLinus Torvalds 		return rc;
901da3962feSAkinobu Mita 	rc = scsi_register_driver(&sr_template.gendrv);
902da3962feSAkinobu Mita 	if (rc)
903da3962feSAkinobu Mita 		unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
904da3962feSAkinobu Mita 
905da3962feSAkinobu Mita 	return rc;
9061da177e4SLinus Torvalds }
9071da177e4SLinus Torvalds 
9081da177e4SLinus Torvalds static void __exit exit_sr(void)
9091da177e4SLinus Torvalds {
9101da177e4SLinus Torvalds 	scsi_unregister_driver(&sr_template.gendrv);
9111da177e4SLinus Torvalds 	unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
9121da177e4SLinus Torvalds }
9131da177e4SLinus Torvalds 
9141da177e4SLinus Torvalds module_init(init_sr);
9151da177e4SLinus Torvalds module_exit(exit_sr);
9161da177e4SLinus Torvalds MODULE_LICENSE("GPL");
917