xref: /linux/drivers/scsi/sr.c (revision 6c7f1e2f12f6f37ddda01180c9e5c407eccce616)
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>
475a0e3ad6STejun Heo #include <linux/slab.h>
48*6c7f1e2fSAaron Lu #include <linux/pm_runtime.h>
491da177e4SLinus Torvalds #include <asm/uaccess.h>
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds #include <scsi/scsi.h>
521da177e4SLinus Torvalds #include <scsi/scsi_dbg.h>
531da177e4SLinus Torvalds #include <scsi/scsi_device.h>
541da177e4SLinus Torvalds #include <scsi/scsi_driver.h>
55820732b5SJames Bottomley #include <scsi/scsi_cmnd.h>
561da177e4SLinus Torvalds #include <scsi/scsi_eh.h>
571da177e4SLinus Torvalds #include <scsi/scsi_host.h>
581da177e4SLinus Torvalds #include <scsi/scsi_ioctl.h>	/* For the door lock/unlock commands */
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds #include "scsi_logging.h"
611da177e4SLinus Torvalds #include "sr.h"
621da177e4SLinus Torvalds 
631da177e4SLinus Torvalds 
64f018fa55SRene Herman MODULE_DESCRIPTION("SCSI cdrom (sr) driver");
65f018fa55SRene Herman MODULE_LICENSE("GPL");
66f018fa55SRene Herman MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_CDROM_MAJOR);
67d7b8bcb0SMichael Tokarev MODULE_ALIAS_SCSI_DEVICE(TYPE_ROM);
68d7b8bcb0SMichael Tokarev MODULE_ALIAS_SCSI_DEVICE(TYPE_WORM);
69f018fa55SRene Herman 
701da177e4SLinus Torvalds #define SR_DISKS	256
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds #define SR_CAPABILITIES \
731da177e4SLinus Torvalds 	(CDC_CLOSE_TRAY|CDC_OPEN_TRAY|CDC_LOCK|CDC_SELECT_SPEED| \
741da177e4SLinus Torvalds 	 CDC_SELECT_DISC|CDC_MULTI_SESSION|CDC_MCN|CDC_MEDIA_CHANGED| \
756a2900b6SChristoph Hellwig 	 CDC_PLAY_AUDIO|CDC_RESET|CDC_DRIVE_STATUS| \
761da177e4SLinus Torvalds 	 CDC_CD_R|CDC_CD_RW|CDC_DVD|CDC_DVD_R|CDC_DVD_RAM|CDC_GENERIC_PACKET| \
771da177e4SLinus Torvalds 	 CDC_MRW|CDC_MRW_W|CDC_RAM)
781da177e4SLinus Torvalds 
792a48fc0aSArnd Bergmann static DEFINE_MUTEX(sr_mutex);
801da177e4SLinus Torvalds static int sr_probe(struct device *);
811da177e4SLinus Torvalds static int sr_remove(struct device *);
827b3d9545SLinus Torvalds static int sr_done(struct scsi_cmnd *);
83*6c7f1e2fSAaron Lu static int sr_runtime_suspend(struct device *dev);
84*6c7f1e2fSAaron Lu 
85*6c7f1e2fSAaron Lu static struct dev_pm_ops sr_pm_ops = {
86*6c7f1e2fSAaron Lu 	.runtime_suspend	= sr_runtime_suspend,
87*6c7f1e2fSAaron Lu };
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds static struct scsi_driver sr_template = {
901da177e4SLinus Torvalds 	.owner			= THIS_MODULE,
911da177e4SLinus Torvalds 	.gendrv = {
921da177e4SLinus Torvalds 		.name   	= "sr",
931da177e4SLinus Torvalds 		.probe		= sr_probe,
941da177e4SLinus Torvalds 		.remove		= sr_remove,
95*6c7f1e2fSAaron Lu 		.pm		= &sr_pm_ops,
961da177e4SLinus Torvalds 	},
977b3d9545SLinus Torvalds 	.done			= sr_done,
981da177e4SLinus Torvalds };
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds static unsigned long sr_index_bits[SR_DISKS / BITS_PER_LONG];
1011da177e4SLinus Torvalds static DEFINE_SPINLOCK(sr_index_lock);
1021da177e4SLinus Torvalds 
1031da177e4SLinus Torvalds /* This semaphore is used to mediate the 0->1 reference get in the
1041da177e4SLinus Torvalds  * face of object destruction (i.e. we can't allow a get on an
1051da177e4SLinus Torvalds  * object after last put) */
1060b950672SArjan van de Ven static DEFINE_MUTEX(sr_ref_mutex);
1071da177e4SLinus Torvalds 
1081da177e4SLinus Torvalds static int sr_open(struct cdrom_device_info *, int);
1091da177e4SLinus Torvalds static void sr_release(struct cdrom_device_info *);
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds static void get_sectorsize(struct scsi_cd *);
1121da177e4SLinus Torvalds static void get_capabilities(struct scsi_cd *);
1131da177e4SLinus Torvalds 
11493aae17aSTejun Heo static unsigned int sr_check_events(struct cdrom_device_info *cdi,
11593aae17aSTejun Heo 				    unsigned int clearing, int slot);
1161da177e4SLinus Torvalds static int sr_packet(struct cdrom_device_info *, struct packet_command *);
1171da177e4SLinus Torvalds 
1181da177e4SLinus Torvalds static struct cdrom_device_ops sr_dops = {
1191da177e4SLinus Torvalds 	.open			= sr_open,
1201da177e4SLinus Torvalds 	.release	 	= sr_release,
1211da177e4SLinus Torvalds 	.drive_status	 	= sr_drive_status,
12293aae17aSTejun Heo 	.check_events		= sr_check_events,
1231da177e4SLinus Torvalds 	.tray_move		= sr_tray_move,
1241da177e4SLinus Torvalds 	.lock_door		= sr_lock_door,
1251da177e4SLinus Torvalds 	.select_speed		= sr_select_speed,
1261da177e4SLinus Torvalds 	.get_last_session	= sr_get_last_session,
1271da177e4SLinus Torvalds 	.get_mcn		= sr_get_mcn,
1281da177e4SLinus Torvalds 	.reset			= sr_reset,
1291da177e4SLinus Torvalds 	.audio_ioctl		= sr_audio_ioctl,
1301da177e4SLinus Torvalds 	.capability		= SR_CAPABILITIES,
1311da177e4SLinus Torvalds 	.generic_packet		= sr_packet,
1321da177e4SLinus Torvalds };
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds static void sr_kref_release(struct kref *kref);
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds static inline struct scsi_cd *scsi_cd(struct gendisk *disk)
1371da177e4SLinus Torvalds {
1381da177e4SLinus Torvalds 	return container_of(disk->private_data, struct scsi_cd, driver);
1391da177e4SLinus Torvalds }
1401da177e4SLinus Torvalds 
141*6c7f1e2fSAaron Lu static int sr_runtime_suspend(struct device *dev)
142*6c7f1e2fSAaron Lu {
143*6c7f1e2fSAaron Lu 	struct scsi_cd *cd = dev_get_drvdata(dev);
144*6c7f1e2fSAaron Lu 
145*6c7f1e2fSAaron Lu 	if (cd->media_present)
146*6c7f1e2fSAaron Lu 		return -EBUSY;
147*6c7f1e2fSAaron Lu 	else
148*6c7f1e2fSAaron Lu 		return 0;
149*6c7f1e2fSAaron Lu }
150*6c7f1e2fSAaron Lu 
1511da177e4SLinus Torvalds /*
1521da177e4SLinus Torvalds  * The get and put routines for the struct scsi_cd.  Note this entity
1531da177e4SLinus Torvalds  * has a scsi_device pointer and owns a reference to this.
1541da177e4SLinus Torvalds  */
1551da177e4SLinus Torvalds static inline struct scsi_cd *scsi_cd_get(struct gendisk *disk)
1561da177e4SLinus Torvalds {
1571da177e4SLinus Torvalds 	struct scsi_cd *cd = NULL;
1581da177e4SLinus Torvalds 
1590b950672SArjan van de Ven 	mutex_lock(&sr_ref_mutex);
1601da177e4SLinus Torvalds 	if (disk->private_data == NULL)
1611da177e4SLinus Torvalds 		goto out;
1621da177e4SLinus Torvalds 	cd = scsi_cd(disk);
1631da177e4SLinus Torvalds 	kref_get(&cd->kref);
1641da177e4SLinus Torvalds 	if (scsi_device_get(cd->device))
1651da177e4SLinus Torvalds 		goto out_put;
166*6c7f1e2fSAaron Lu 	if (!scsi_autopm_get_device(cd->device))
1671da177e4SLinus Torvalds 		goto out;
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds  out_put:
1701da177e4SLinus Torvalds 	kref_put(&cd->kref, sr_kref_release);
1711da177e4SLinus Torvalds 	cd = NULL;
1721da177e4SLinus Torvalds  out:
1730b950672SArjan van de Ven 	mutex_unlock(&sr_ref_mutex);
1741da177e4SLinus Torvalds 	return cd;
1751da177e4SLinus Torvalds }
1761da177e4SLinus Torvalds 
177858119e1SArjan van de Ven static void scsi_cd_put(struct scsi_cd *cd)
1781da177e4SLinus Torvalds {
1791da177e4SLinus Torvalds 	struct scsi_device *sdev = cd->device;
1801da177e4SLinus Torvalds 
1810b950672SArjan van de Ven 	mutex_lock(&sr_ref_mutex);
1821da177e4SLinus Torvalds 	kref_put(&cd->kref, sr_kref_release);
183*6c7f1e2fSAaron Lu 	scsi_autopm_put_device(sdev);
1841da177e4SLinus Torvalds 	scsi_device_put(sdev);
1850b950672SArjan van de Ven 	mutex_unlock(&sr_ref_mutex);
1861da177e4SLinus Torvalds }
1871da177e4SLinus Torvalds 
18893aae17aSTejun Heo static unsigned int sr_get_events(struct scsi_device *sdev)
1891da177e4SLinus Torvalds {
19093aae17aSTejun Heo 	u8 buf[8];
19193aae17aSTejun Heo 	u8 cmd[] = { GET_EVENT_STATUS_NOTIFICATION,
19293aae17aSTejun Heo 		     1,			/* polled */
19393aae17aSTejun Heo 		     0, 0,		/* reserved */
19493aae17aSTejun Heo 		     1 << 4,		/* notification class: media */
19593aae17aSTejun Heo 		     0, 0,		/* reserved */
19693aae17aSTejun Heo 		     0, sizeof(buf),	/* allocation length */
19793aae17aSTejun Heo 		     0,			/* control */
19893aae17aSTejun Heo 	};
19993aae17aSTejun Heo 	struct event_header *eh = (void *)buf;
20093aae17aSTejun Heo 	struct media_event_desc *med = (void *)(buf + 4);
20193aae17aSTejun Heo 	struct scsi_sense_hdr sshdr;
20293aae17aSTejun Heo 	int result;
2031da177e4SLinus Torvalds 
20493aae17aSTejun Heo 	result = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buf, sizeof(buf),
20593aae17aSTejun Heo 				  &sshdr, SR_TIMEOUT, MAX_RETRIES, NULL);
20693aae17aSTejun Heo 	if (scsi_sense_valid(&sshdr) && sshdr.sense_key == UNIT_ATTENTION)
20793aae17aSTejun Heo 		return DISK_EVENT_MEDIA_CHANGE;
20893aae17aSTejun Heo 
20993aae17aSTejun Heo 	if (result || be16_to_cpu(eh->data_len) < sizeof(*med))
21093aae17aSTejun Heo 		return 0;
21193aae17aSTejun Heo 
21293aae17aSTejun Heo 	if (eh->nea || eh->notification_class != 0x4)
21393aae17aSTejun Heo 		return 0;
21493aae17aSTejun Heo 
21593aae17aSTejun Heo 	if (med->media_event_code == 1)
21693aae17aSTejun Heo 		return DISK_EVENT_EJECT_REQUEST;
21793aae17aSTejun Heo 	else if (med->media_event_code == 2)
21893aae17aSTejun Heo 		return DISK_EVENT_MEDIA_CHANGE;
21993aae17aSTejun Heo 	return 0;
2201da177e4SLinus Torvalds }
2211da177e4SLinus Torvalds 
22293aae17aSTejun Heo /*
22393aae17aSTejun Heo  * This function checks to see if the media has been changed or eject
22493aae17aSTejun Heo  * button has been pressed.  It is possible that we have already
22593aae17aSTejun Heo  * sensed a change, or the drive may have sensed one and not yet
22693aae17aSTejun Heo  * reported it.  The past events are accumulated in sdev->changed and
22793aae17aSTejun Heo  * returned together with the current state.
22893aae17aSTejun Heo  */
22993aae17aSTejun Heo static unsigned int sr_check_events(struct cdrom_device_info *cdi,
23093aae17aSTejun Heo 				    unsigned int clearing, int slot)
23193aae17aSTejun Heo {
23293aae17aSTejun Heo 	struct scsi_cd *cd = cdi->handle;
23393aae17aSTejun Heo 	bool last_present;
23493aae17aSTejun Heo 	struct scsi_sense_hdr sshdr;
23593aae17aSTejun Heo 	unsigned int events;
23693aae17aSTejun Heo 	int ret;
23793aae17aSTejun Heo 
23893aae17aSTejun Heo 	/* no changer support */
23993aae17aSTejun Heo 	if (CDSL_CURRENT != slot)
24093aae17aSTejun Heo 		return 0;
24193aae17aSTejun Heo 
24293aae17aSTejun Heo 	events = sr_get_events(cd->device);
24379b9677dSKay Sievers 	cd->get_event_changed |= events & DISK_EVENT_MEDIA_CHANGE;
24479b9677dSKay Sievers 
24579b9677dSKay Sievers 	/*
24679b9677dSKay Sievers 	 * If earlier GET_EVENT_STATUS_NOTIFICATION and TUR did not agree
24779b9677dSKay Sievers 	 * for several times in a row.  We rely on TUR only for this likely
24879b9677dSKay Sievers 	 * broken device, to prevent generating incorrect media changed
24979b9677dSKay Sievers 	 * events for every open().
25079b9677dSKay Sievers 	 */
25179b9677dSKay Sievers 	if (cd->ignore_get_event) {
25279b9677dSKay Sievers 		events &= ~DISK_EVENT_MEDIA_CHANGE;
25379b9677dSKay Sievers 		goto do_tur;
25479b9677dSKay Sievers 	}
25579b9677dSKay Sievers 
25693aae17aSTejun Heo 	/*
25793aae17aSTejun Heo 	 * GET_EVENT_STATUS_NOTIFICATION is enough unless MEDIA_CHANGE
25893aae17aSTejun Heo 	 * is being cleared.  Note that there are devices which hang
25993aae17aSTejun Heo 	 * if asked to execute TUR repeatedly.
26093aae17aSTejun Heo 	 */
26179b9677dSKay Sievers 	if (cd->device->changed) {
26279b9677dSKay Sievers 		events |= DISK_EVENT_MEDIA_CHANGE;
26379b9677dSKay Sievers 		cd->device->changed = 0;
26479b9677dSKay Sievers 		cd->tur_changed = true;
26579b9677dSKay Sievers 	}
26693aae17aSTejun Heo 
26779b9677dSKay Sievers 	if (!(clearing & DISK_EVENT_MEDIA_CHANGE))
26879b9677dSKay Sievers 		return events;
26979b9677dSKay Sievers do_tur:
27093aae17aSTejun Heo 	/* let's see whether the media is there with TUR */
27193aae17aSTejun Heo 	last_present = cd->media_present;
27293aae17aSTejun Heo 	ret = scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES, &sshdr);
27393aae17aSTejun Heo 
274638428ecSTejun Heo 	/*
275638428ecSTejun Heo 	 * Media is considered to be present if TUR succeeds or fails with
276638428ecSTejun Heo 	 * sense data indicating something other than media-not-present
277638428ecSTejun Heo 	 * (ASC 0x3a).
278638428ecSTejun Heo 	 */
27993aae17aSTejun Heo 	cd->media_present = scsi_status_is_good(ret) ||
28093aae17aSTejun Heo 		(scsi_sense_valid(&sshdr) && sshdr.asc != 0x3a);
2811da177e4SLinus Torvalds 
28293aae17aSTejun Heo 	if (last_present != cd->media_present)
28379b9677dSKay Sievers 		cd->device->changed = 1;
28479b9677dSKay Sievers 
28593aae17aSTejun Heo 	if (cd->device->changed) {
28693aae17aSTejun Heo 		events |= DISK_EVENT_MEDIA_CHANGE;
2871da177e4SLinus Torvalds 		cd->device->changed = 0;
28879b9677dSKay Sievers 		cd->tur_changed = true;
2891da177e4SLinus Torvalds 	}
290285e9670SKay Sievers 
29179b9677dSKay Sievers 	if (cd->ignore_get_event)
29279b9677dSKay Sievers 		return events;
29379b9677dSKay Sievers 
29479b9677dSKay Sievers 	/* check whether GET_EVENT is reporting spurious MEDIA_CHANGE */
29579b9677dSKay Sievers 	if (!cd->tur_changed) {
29679b9677dSKay Sievers 		if (cd->get_event_changed) {
29779b9677dSKay Sievers 			if (cd->tur_mismatch++ > 8) {
29879b9677dSKay Sievers 				sdev_printk(KERN_WARNING, cd->device,
29979b9677dSKay Sievers 					    "GET_EVENT and TUR disagree continuously, suppress GET_EVENT events\n");
30079b9677dSKay Sievers 				cd->ignore_get_event = true;
30179b9677dSKay Sievers 			}
30279b9677dSKay Sievers 		} else {
30379b9677dSKay Sievers 			cd->tur_mismatch = 0;
30479b9677dSKay Sievers 		}
30579b9677dSKay Sievers 	}
30679b9677dSKay Sievers 	cd->tur_changed = false;
30779b9677dSKay Sievers 	cd->get_event_changed = false;
30879b9677dSKay Sievers 
30993aae17aSTejun Heo 	return events;
3101da177e4SLinus Torvalds }
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds /*
3137b3d9545SLinus Torvalds  * sr_done is the interrupt routine for the device driver.
3141da177e4SLinus Torvalds  *
3157b3d9545SLinus Torvalds  * It will be notified on the end of a SCSI read / write, and will take one
3161da177e4SLinus Torvalds  * of several actions based on success or failure.
3171da177e4SLinus Torvalds  */
3187b3d9545SLinus Torvalds static int sr_done(struct scsi_cmnd *SCpnt)
3191da177e4SLinus Torvalds {
3201da177e4SLinus Torvalds 	int result = SCpnt->result;
32130b0c37bSBoaz Harrosh 	int this_count = scsi_bufflen(SCpnt);
3221da177e4SLinus Torvalds 	int good_bytes = (result == 0 ? this_count : 0);
3231da177e4SLinus Torvalds 	int block_sectors = 0;
3241da177e4SLinus Torvalds 	long error_sector;
3251da177e4SLinus Torvalds 	struct scsi_cd *cd = scsi_cd(SCpnt->request->rq_disk);
3261da177e4SLinus Torvalds 
3271da177e4SLinus Torvalds #ifdef DEBUG
3281da177e4SLinus Torvalds 	printk("sr.c done: %x\n", result);
3291da177e4SLinus Torvalds #endif
3301da177e4SLinus Torvalds 
3311da177e4SLinus Torvalds 	/*
3321da177e4SLinus Torvalds 	 * Handle MEDIUM ERRORs or VOLUME OVERFLOWs that indicate partial
3331da177e4SLinus Torvalds 	 * success.  Since this is a relatively rare error condition, no
3341da177e4SLinus Torvalds 	 * care is taken to avoid unnecessary additional work such as
3351da177e4SLinus Torvalds 	 * memcpy's that could be avoided.
3361da177e4SLinus Torvalds 	 */
3371da177e4SLinus Torvalds 	if (driver_byte(result) != 0 &&		/* An error occurred */
3381da177e4SLinus Torvalds 	    (SCpnt->sense_buffer[0] & 0x7f) == 0x70) { /* Sense current */
3391da177e4SLinus Torvalds 		switch (SCpnt->sense_buffer[2]) {
3401da177e4SLinus Torvalds 		case MEDIUM_ERROR:
3411da177e4SLinus Torvalds 		case VOLUME_OVERFLOW:
3421da177e4SLinus Torvalds 		case ILLEGAL_REQUEST:
3431da177e4SLinus Torvalds 			if (!(SCpnt->sense_buffer[0] & 0x90))
3441da177e4SLinus Torvalds 				break;
3451da177e4SLinus Torvalds 			error_sector = (SCpnt->sense_buffer[3] << 24) |
3461da177e4SLinus Torvalds 				(SCpnt->sense_buffer[4] << 16) |
3471da177e4SLinus Torvalds 				(SCpnt->sense_buffer[5] << 8) |
3481da177e4SLinus Torvalds 				SCpnt->sense_buffer[6];
3491da177e4SLinus Torvalds 			if (SCpnt->request->bio != NULL)
3501da177e4SLinus Torvalds 				block_sectors =
3511da177e4SLinus Torvalds 					bio_sectors(SCpnt->request->bio);
3521da177e4SLinus Torvalds 			if (block_sectors < 4)
3531da177e4SLinus Torvalds 				block_sectors = 4;
3541da177e4SLinus Torvalds 			if (cd->device->sector_size == 2048)
3551da177e4SLinus Torvalds 				error_sector <<= 2;
3561da177e4SLinus Torvalds 			error_sector &= ~(block_sectors - 1);
35783096ebfSTejun Heo 			good_bytes = (error_sector -
35883096ebfSTejun Heo 				      blk_rq_pos(SCpnt->request)) << 9;
3591da177e4SLinus Torvalds 			if (good_bytes < 0 || good_bytes >= this_count)
3601da177e4SLinus Torvalds 				good_bytes = 0;
3611da177e4SLinus Torvalds 			/*
3621da177e4SLinus Torvalds 			 * The SCSI specification allows for the value
3631da177e4SLinus Torvalds 			 * returned by READ CAPACITY to be up to 75 2K
3641da177e4SLinus Torvalds 			 * sectors past the last readable block.
3651da177e4SLinus Torvalds 			 * Therefore, if we hit a medium error within the
3661da177e4SLinus Torvalds 			 * last 75 2K sectors, we decrease the saved size
3671da177e4SLinus Torvalds 			 * value.
3681da177e4SLinus Torvalds 			 */
3691da177e4SLinus Torvalds 			if (error_sector < get_capacity(cd->disk) &&
3701da177e4SLinus Torvalds 			    cd->capacity - error_sector < 4 * 75)
3711da177e4SLinus Torvalds 				set_capacity(cd->disk, error_sector);
3721da177e4SLinus Torvalds 			break;
3731da177e4SLinus Torvalds 
3741da177e4SLinus Torvalds 		case RECOVERED_ERROR:
3751da177e4SLinus Torvalds 			good_bytes = this_count;
3761da177e4SLinus Torvalds 			break;
3771da177e4SLinus Torvalds 
3781da177e4SLinus Torvalds 		default:
3791da177e4SLinus Torvalds 			break;
3801da177e4SLinus Torvalds 		}
3811da177e4SLinus Torvalds 	}
3821da177e4SLinus Torvalds 
3837b3d9545SLinus Torvalds 	return good_bytes;
3841da177e4SLinus Torvalds }
3851da177e4SLinus Torvalds 
3867f9a6bc4SJames Bottomley static int sr_prep_fn(struct request_queue *q, struct request *rq)
3871da177e4SLinus Torvalds {
388242f9dcbSJens Axboe 	int block = 0, this_count, s_size;
3897f9a6bc4SJames Bottomley 	struct scsi_cd *cd;
3907f9a6bc4SJames Bottomley 	struct scsi_cmnd *SCpnt;
3917f9a6bc4SJames Bottomley 	struct scsi_device *sdp = q->queuedata;
3927f9a6bc4SJames Bottomley 	int ret;
3937f9a6bc4SJames Bottomley 
3947f9a6bc4SJames Bottomley 	if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
3957f9a6bc4SJames Bottomley 		ret = scsi_setup_blk_pc_cmnd(sdp, rq);
3967f9a6bc4SJames Bottomley 		goto out;
3977f9a6bc4SJames Bottomley 	} else if (rq->cmd_type != REQ_TYPE_FS) {
3987f9a6bc4SJames Bottomley 		ret = BLKPREP_KILL;
3997f9a6bc4SJames Bottomley 		goto out;
4007f9a6bc4SJames Bottomley 	}
4017f9a6bc4SJames Bottomley 	ret = scsi_setup_fs_cmnd(sdp, rq);
4027f9a6bc4SJames Bottomley 	if (ret != BLKPREP_OK)
4037f9a6bc4SJames Bottomley 		goto out;
4047f9a6bc4SJames Bottomley 	SCpnt = rq->special;
4057f9a6bc4SJames Bottomley 	cd = scsi_cd(rq->rq_disk);
4067f9a6bc4SJames Bottomley 
4077f9a6bc4SJames Bottomley 	/* from here on until we're complete, any goto out
4087f9a6bc4SJames Bottomley 	 * is used for a killable error condition */
4097f9a6bc4SJames Bottomley 	ret = BLKPREP_KILL;
4101da177e4SLinus Torvalds 
4111da177e4SLinus Torvalds 	SCSI_LOG_HLQUEUE(1, printk("Doing sr request, dev = %s, block = %d\n",
4121da177e4SLinus Torvalds 				cd->disk->disk_name, block));
4131da177e4SLinus Torvalds 
4141da177e4SLinus Torvalds 	if (!cd->device || !scsi_device_online(cd->device)) {
41583096ebfSTejun Heo 		SCSI_LOG_HLQUEUE(2, printk("Finishing %u sectors\n",
41683096ebfSTejun Heo 					   blk_rq_sectors(rq)));
4171da177e4SLinus Torvalds 		SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt));
4187f9a6bc4SJames Bottomley 		goto out;
4191da177e4SLinus Torvalds 	}
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds 	if (cd->device->changed) {
4221da177e4SLinus Torvalds 		/*
4231da177e4SLinus Torvalds 		 * quietly refuse to do anything to a changed disc until the
4241da177e4SLinus Torvalds 		 * changed bit has been reset
4251da177e4SLinus Torvalds 		 */
4267f9a6bc4SJames Bottomley 		goto out;
4271da177e4SLinus Torvalds 	}
4281da177e4SLinus Torvalds 
4291da177e4SLinus Torvalds 	/*
4301da177e4SLinus Torvalds 	 * we do lazy blocksize switching (when reading XA sectors,
4311da177e4SLinus Torvalds 	 * see CDROMREADMODE2 ioctl)
4321da177e4SLinus Torvalds 	 */
4331da177e4SLinus Torvalds 	s_size = cd->device->sector_size;
4341da177e4SLinus Torvalds 	if (s_size > 2048) {
4351da177e4SLinus Torvalds 		if (!in_interrupt())
4361da177e4SLinus Torvalds 			sr_set_blocklength(cd, 2048);
4371da177e4SLinus Torvalds 		else
4381da177e4SLinus Torvalds 			printk("sr: can't switch blocksize: in interrupt\n");
4391da177e4SLinus Torvalds 	}
4401da177e4SLinus Torvalds 
4411da177e4SLinus Torvalds 	if (s_size != 512 && s_size != 1024 && s_size != 2048) {
4423bf743e7SJeff Garzik 		scmd_printk(KERN_ERR, SCpnt, "bad sector size %d\n", s_size);
4437f9a6bc4SJames Bottomley 		goto out;
4441da177e4SLinus Torvalds 	}
4451da177e4SLinus Torvalds 
4467f9a6bc4SJames Bottomley 	if (rq_data_dir(rq) == WRITE) {
4471da177e4SLinus Torvalds 		if (!cd->device->writeable)
4487f9a6bc4SJames Bottomley 			goto out;
4491da177e4SLinus Torvalds 		SCpnt->cmnd[0] = WRITE_10;
4501da177e4SLinus Torvalds 		SCpnt->sc_data_direction = DMA_TO_DEVICE;
4511da177e4SLinus Torvalds  	 	cd->cdi.media_written = 1;
4527f9a6bc4SJames Bottomley 	} else if (rq_data_dir(rq) == READ) {
4531da177e4SLinus Torvalds 		SCpnt->cmnd[0] = READ_10;
4541da177e4SLinus Torvalds 		SCpnt->sc_data_direction = DMA_FROM_DEVICE;
4551da177e4SLinus Torvalds 	} else {
4567f9a6bc4SJames Bottomley 		blk_dump_rq_flags(rq, "Unknown sr command");
4577f9a6bc4SJames Bottomley 		goto out;
4581da177e4SLinus Torvalds 	}
4591da177e4SLinus Torvalds 
4601da177e4SLinus Torvalds 	{
46130b0c37bSBoaz Harrosh 		struct scatterlist *sg;
46230b0c37bSBoaz Harrosh 		int i, size = 0, sg_count = scsi_sg_count(SCpnt);
4631da177e4SLinus Torvalds 
46430b0c37bSBoaz Harrosh 		scsi_for_each_sg(SCpnt, sg, sg_count, i)
46530b0c37bSBoaz Harrosh 			size += sg->length;
46630b0c37bSBoaz Harrosh 
46730b0c37bSBoaz Harrosh 		if (size != scsi_bufflen(SCpnt)) {
4683bf743e7SJeff Garzik 			scmd_printk(KERN_ERR, SCpnt,
4699ccfc756SJames Bottomley 				"mismatch count %d, bytes %d\n",
47030b0c37bSBoaz Harrosh 				size, scsi_bufflen(SCpnt));
47130b0c37bSBoaz Harrosh 			if (scsi_bufflen(SCpnt) > size)
47230b0c37bSBoaz Harrosh 				SCpnt->sdb.length = size;
4731da177e4SLinus Torvalds 		}
4741da177e4SLinus Torvalds 	}
4751da177e4SLinus Torvalds 
4761da177e4SLinus Torvalds 	/*
4771da177e4SLinus Torvalds 	 * request doesn't start on hw block boundary, add scatter pads
4781da177e4SLinus Torvalds 	 */
47983096ebfSTejun Heo 	if (((unsigned int)blk_rq_pos(rq) % (s_size >> 9)) ||
48030b0c37bSBoaz Harrosh 	    (scsi_bufflen(SCpnt) % s_size)) {
4813bf743e7SJeff Garzik 		scmd_printk(KERN_NOTICE, SCpnt, "unaligned transfer\n");
4827f9a6bc4SJames Bottomley 		goto out;
4831da177e4SLinus Torvalds 	}
4841da177e4SLinus Torvalds 
48530b0c37bSBoaz Harrosh 	this_count = (scsi_bufflen(SCpnt) >> 9) / (s_size >> 9);
4861da177e4SLinus Torvalds 
4871da177e4SLinus Torvalds 
48883096ebfSTejun Heo 	SCSI_LOG_HLQUEUE(2, printk("%s : %s %d/%u 512 byte blocks.\n",
4891da177e4SLinus Torvalds 				cd->cdi.name,
4907f9a6bc4SJames Bottomley 				(rq_data_dir(rq) == WRITE) ?
4911da177e4SLinus Torvalds 					"writing" : "reading",
49283096ebfSTejun Heo 				this_count, blk_rq_sectors(rq)));
4931da177e4SLinus Torvalds 
4941da177e4SLinus Torvalds 	SCpnt->cmnd[1] = 0;
49583096ebfSTejun Heo 	block = (unsigned int)blk_rq_pos(rq) / (s_size >> 9);
4961da177e4SLinus Torvalds 
4971da177e4SLinus Torvalds 	if (this_count > 0xffff) {
4981da177e4SLinus Torvalds 		this_count = 0xffff;
49930b0c37bSBoaz Harrosh 		SCpnt->sdb.length = this_count * s_size;
5001da177e4SLinus Torvalds 	}
5011da177e4SLinus Torvalds 
5021da177e4SLinus Torvalds 	SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
5031da177e4SLinus Torvalds 	SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
5041da177e4SLinus Torvalds 	SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
5051da177e4SLinus Torvalds 	SCpnt->cmnd[5] = (unsigned char) block & 0xff;
5061da177e4SLinus Torvalds 	SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
5071da177e4SLinus Torvalds 	SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
5081da177e4SLinus Torvalds 	SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
5091da177e4SLinus Torvalds 
5101da177e4SLinus Torvalds 	/*
5111da177e4SLinus Torvalds 	 * We shouldn't disconnect in the middle of a sector, so with a dumb
5121da177e4SLinus Torvalds 	 * host adapter, it's safe to assume that we can at least transfer
5131da177e4SLinus Torvalds 	 * this many bytes between each connect / disconnect.
5141da177e4SLinus Torvalds 	 */
5151da177e4SLinus Torvalds 	SCpnt->transfersize = cd->device->sector_size;
5161da177e4SLinus Torvalds 	SCpnt->underflow = this_count << 9;
5171da177e4SLinus Torvalds 	SCpnt->allowed = MAX_RETRIES;
5181da177e4SLinus Torvalds 
5191da177e4SLinus Torvalds 	/*
5201da177e4SLinus Torvalds 	 * This indicates that the command is ready from our end to be
5211da177e4SLinus Torvalds 	 * queued.
5221da177e4SLinus Torvalds 	 */
5237f9a6bc4SJames Bottomley 	ret = BLKPREP_OK;
5247f9a6bc4SJames Bottomley  out:
5257f9a6bc4SJames Bottomley 	return scsi_prep_return(q, rq, ret);
5261da177e4SLinus Torvalds }
5271da177e4SLinus Torvalds 
52840cc51beSAl Viro static int sr_block_open(struct block_device *bdev, fmode_t mode)
5291da177e4SLinus Torvalds {
5306e9624b8SArnd Bergmann 	struct scsi_cd *cd;
53140cc51beSAl Viro 	int ret = -ENXIO;
5321da177e4SLinus Torvalds 
5332a48fc0aSArnd Bergmann 	mutex_lock(&sr_mutex);
5346e9624b8SArnd Bergmann 	cd = scsi_cd_get(bdev->bd_disk);
53540cc51beSAl Viro 	if (cd) {
53640cc51beSAl Viro 		ret = cdrom_open(&cd->cdi, bdev, mode);
53740cc51beSAl Viro 		if (ret)
5381da177e4SLinus Torvalds 			scsi_cd_put(cd);
53940cc51beSAl Viro 	}
5402a48fc0aSArnd Bergmann 	mutex_unlock(&sr_mutex);
5411da177e4SLinus Torvalds 	return ret;
5421da177e4SLinus Torvalds }
5431da177e4SLinus Torvalds 
54440cc51beSAl Viro static int sr_block_release(struct gendisk *disk, fmode_t mode)
5451da177e4SLinus Torvalds {
54640cc51beSAl Viro 	struct scsi_cd *cd = scsi_cd(disk);
5472a48fc0aSArnd Bergmann 	mutex_lock(&sr_mutex);
54840cc51beSAl Viro 	cdrom_release(&cd->cdi, mode);
5491da177e4SLinus Torvalds 	scsi_cd_put(cd);
5502a48fc0aSArnd Bergmann 	mutex_unlock(&sr_mutex);
5511da177e4SLinus Torvalds 	return 0;
5521da177e4SLinus Torvalds }
5531da177e4SLinus Torvalds 
55440cc51beSAl Viro static int sr_block_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
5551da177e4SLinus Torvalds 			  unsigned long arg)
5561da177e4SLinus Torvalds {
55740cc51beSAl Viro 	struct scsi_cd *cd = scsi_cd(bdev->bd_disk);
5581da177e4SLinus Torvalds 	struct scsi_device *sdev = cd->device;
5596a2900b6SChristoph Hellwig 	void __user *argp = (void __user *)arg;
5606a2900b6SChristoph Hellwig 	int ret;
5611da177e4SLinus Torvalds 
562*6c7f1e2fSAaron Lu 	scsi_autopm_get_device(cd->device);
563*6c7f1e2fSAaron Lu 
5642a48fc0aSArnd Bergmann 	mutex_lock(&sr_mutex);
5658a6cfeb6SArnd Bergmann 
5661da177e4SLinus Torvalds 	/*
5671da177e4SLinus Torvalds 	 * Send SCSI addressing ioctls directly to mid level, send other
5681da177e4SLinus Torvalds 	 * ioctls to cdrom/block level.
5691da177e4SLinus Torvalds 	 */
5701da177e4SLinus Torvalds 	switch (cmd) {
5711da177e4SLinus Torvalds 	case SCSI_IOCTL_GET_IDLUN:
5721da177e4SLinus Torvalds 	case SCSI_IOCTL_GET_BUS_NUMBER:
5738a6cfeb6SArnd Bergmann 		ret = scsi_ioctl(sdev, cmd, argp);
5748a6cfeb6SArnd Bergmann 		goto out;
5751da177e4SLinus Torvalds 	}
5766a2900b6SChristoph Hellwig 
57740cc51beSAl Viro 	ret = cdrom_ioctl(&cd->cdi, bdev, mode, cmd, arg);
5786397256bSTejun Heo 	if (ret != -ENOSYS)
5798a6cfeb6SArnd Bergmann 		goto out;
5806a2900b6SChristoph Hellwig 
5816a2900b6SChristoph Hellwig 	/*
5826a2900b6SChristoph Hellwig 	 * ENODEV means that we didn't recognise the ioctl, or that we
5836a2900b6SChristoph Hellwig 	 * cannot execute it in the current device state.  In either
5846a2900b6SChristoph Hellwig 	 * case fall through to scsi_ioctl, which will return ENDOEV again
5856a2900b6SChristoph Hellwig 	 * if it doesn't recognise the ioctl
5866a2900b6SChristoph Hellwig 	 */
58783ff6fe8SAl Viro 	ret = scsi_nonblockable_ioctl(sdev, cmd, argp,
588fd4ce1acSChristoph Hellwig 					(mode & FMODE_NDELAY) != 0);
5896a2900b6SChristoph Hellwig 	if (ret != -ENODEV)
5908a6cfeb6SArnd Bergmann 		goto out;
5918a6cfeb6SArnd Bergmann 	ret = scsi_ioctl(sdev, cmd, argp);
5928a6cfeb6SArnd Bergmann 
5938a6cfeb6SArnd Bergmann out:
5942a48fc0aSArnd Bergmann 	mutex_unlock(&sr_mutex);
595*6c7f1e2fSAaron Lu 	scsi_autopm_put_device(cd->device);
5966a2900b6SChristoph Hellwig 	return ret;
5971da177e4SLinus Torvalds }
5981da177e4SLinus Torvalds 
59993aae17aSTejun Heo static unsigned int sr_block_check_events(struct gendisk *disk,
60093aae17aSTejun Heo 					  unsigned int clearing)
6011da177e4SLinus Torvalds {
6021da177e4SLinus Torvalds 	struct scsi_cd *cd = scsi_cd(disk);
603*6c7f1e2fSAaron Lu 	unsigned int ret;
604*6c7f1e2fSAaron Lu 
605*6c7f1e2fSAaron Lu 	scsi_autopm_get_device(cd->device);
606*6c7f1e2fSAaron Lu 	ret = cdrom_check_events(&cd->cdi, clearing);
607*6c7f1e2fSAaron Lu 	scsi_autopm_put_device(cd->device);
608*6c7f1e2fSAaron Lu 
609*6c7f1e2fSAaron Lu 	return ret;
61093aae17aSTejun Heo }
61193aae17aSTejun Heo 
61293aae17aSTejun Heo static int sr_block_revalidate_disk(struct gendisk *disk)
61393aae17aSTejun Heo {
61493aae17aSTejun Heo 	struct scsi_cd *cd = scsi_cd(disk);
61593aae17aSTejun Heo 	struct scsi_sense_hdr sshdr;
61693aae17aSTejun Heo 
617*6c7f1e2fSAaron Lu 	scsi_autopm_get_device(cd->device);
618*6c7f1e2fSAaron Lu 
61993aae17aSTejun Heo 	/* if the unit is not ready, nothing more to do */
62093aae17aSTejun Heo 	if (scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES, &sshdr))
621*6c7f1e2fSAaron Lu 		goto out;
62293aae17aSTejun Heo 
62393aae17aSTejun Heo 	sr_cd_check(&cd->cdi);
62493aae17aSTejun Heo 	get_sectorsize(cd);
625*6c7f1e2fSAaron Lu out:
626*6c7f1e2fSAaron Lu 	scsi_autopm_put_device(cd->device);
62793aae17aSTejun Heo 	return 0;
6281da177e4SLinus Torvalds }
6291da177e4SLinus Torvalds 
63083d5cde4SAlexey Dobriyan static const struct block_device_operations sr_bdops =
6311da177e4SLinus Torvalds {
6321da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
63340cc51beSAl Viro 	.open		= sr_block_open,
63440cc51beSAl Viro 	.release	= sr_block_release,
6358a6cfeb6SArnd Bergmann 	.ioctl		= sr_block_ioctl,
63693aae17aSTejun Heo 	.check_events	= sr_block_check_events,
63793aae17aSTejun Heo 	.revalidate_disk = sr_block_revalidate_disk,
6381da177e4SLinus Torvalds 	/*
6391da177e4SLinus Torvalds 	 * No compat_ioctl for now because sr_block_ioctl never
64025985edcSLucas De Marchi 	 * seems to pass arbitrary ioctls down to host drivers.
6411da177e4SLinus Torvalds 	 */
6421da177e4SLinus Torvalds };
6431da177e4SLinus Torvalds 
6441da177e4SLinus Torvalds static int sr_open(struct cdrom_device_info *cdi, int purpose)
6451da177e4SLinus Torvalds {
6461da177e4SLinus Torvalds 	struct scsi_cd *cd = cdi->handle;
6471da177e4SLinus Torvalds 	struct scsi_device *sdev = cd->device;
6481da177e4SLinus Torvalds 	int retval;
6491da177e4SLinus Torvalds 
6501da177e4SLinus Torvalds 	/*
6511da177e4SLinus Torvalds 	 * If the device is in error recovery, wait until it is done.
6521da177e4SLinus Torvalds 	 * If the device is offline, then disallow any access to it.
6531da177e4SLinus Torvalds 	 */
6541da177e4SLinus Torvalds 	retval = -ENXIO;
6551da177e4SLinus Torvalds 	if (!scsi_block_when_processing_errors(sdev))
6561da177e4SLinus Torvalds 		goto error_out;
6571da177e4SLinus Torvalds 
6581da177e4SLinus Torvalds 	return 0;
6591da177e4SLinus Torvalds 
6601da177e4SLinus Torvalds error_out:
6611da177e4SLinus Torvalds 	return retval;
6621da177e4SLinus Torvalds }
6631da177e4SLinus Torvalds 
6641da177e4SLinus Torvalds static void sr_release(struct cdrom_device_info *cdi)
6651da177e4SLinus Torvalds {
6661da177e4SLinus Torvalds 	struct scsi_cd *cd = cdi->handle;
6671da177e4SLinus Torvalds 
6681da177e4SLinus Torvalds 	if (cd->device->sector_size > 2048)
6691da177e4SLinus Torvalds 		sr_set_blocklength(cd, 2048);
6701da177e4SLinus Torvalds 
6711da177e4SLinus Torvalds }
6721da177e4SLinus Torvalds 
6731da177e4SLinus Torvalds static int sr_probe(struct device *dev)
6741da177e4SLinus Torvalds {
6751da177e4SLinus Torvalds 	struct scsi_device *sdev = to_scsi_device(dev);
6761da177e4SLinus Torvalds 	struct gendisk *disk;
6771da177e4SLinus Torvalds 	struct scsi_cd *cd;
6781da177e4SLinus Torvalds 	int minor, error;
6791da177e4SLinus Torvalds 
6801da177e4SLinus Torvalds 	error = -ENODEV;
6811da177e4SLinus Torvalds 	if (sdev->type != TYPE_ROM && sdev->type != TYPE_WORM)
6821da177e4SLinus Torvalds 		goto fail;
6831da177e4SLinus Torvalds 
6841da177e4SLinus Torvalds 	error = -ENOMEM;
68524669f75SJes Sorensen 	cd = kzalloc(sizeof(*cd), GFP_KERNEL);
6861da177e4SLinus Torvalds 	if (!cd)
6871da177e4SLinus Torvalds 		goto fail;
6881da177e4SLinus Torvalds 
6891da177e4SLinus Torvalds 	kref_init(&cd->kref);
6901da177e4SLinus Torvalds 
6911da177e4SLinus Torvalds 	disk = alloc_disk(1);
6921da177e4SLinus Torvalds 	if (!disk)
6931da177e4SLinus Torvalds 		goto fail_free;
6941da177e4SLinus Torvalds 
6951da177e4SLinus Torvalds 	spin_lock(&sr_index_lock);
6961da177e4SLinus Torvalds 	minor = find_first_zero_bit(sr_index_bits, SR_DISKS);
6971da177e4SLinus Torvalds 	if (minor == SR_DISKS) {
6981da177e4SLinus Torvalds 		spin_unlock(&sr_index_lock);
6991da177e4SLinus Torvalds 		error = -EBUSY;
7001da177e4SLinus Torvalds 		goto fail_put;
7011da177e4SLinus Torvalds 	}
7021da177e4SLinus Torvalds 	__set_bit(minor, sr_index_bits);
7031da177e4SLinus Torvalds 	spin_unlock(&sr_index_lock);
7041da177e4SLinus Torvalds 
7051da177e4SLinus Torvalds 	disk->major = SCSI_CDROM_MAJOR;
7061da177e4SLinus Torvalds 	disk->first_minor = minor;
7071da177e4SLinus Torvalds 	sprintf(disk->disk_name, "sr%d", minor);
7081da177e4SLinus Torvalds 	disk->fops = &sr_bdops;
709d4dc210fSTejun Heo 	disk->flags = GENHD_FL_CD | GENHD_FL_BLOCK_EVENTS_ON_EXCL_WRITE;
71093aae17aSTejun Heo 	disk->events = DISK_EVENT_MEDIA_CHANGE | DISK_EVENT_EJECT_REQUEST;
7111da177e4SLinus Torvalds 
712242f9dcbSJens Axboe 	blk_queue_rq_timeout(sdev->request_queue, SR_TIMEOUT);
713242f9dcbSJens Axboe 
7141da177e4SLinus Torvalds 	cd->device = sdev;
7151da177e4SLinus Torvalds 	cd->disk = disk;
7161da177e4SLinus Torvalds 	cd->driver = &sr_template;
7171da177e4SLinus Torvalds 	cd->disk = disk;
7181da177e4SLinus Torvalds 	cd->capacity = 0x1fffff;
7191da177e4SLinus Torvalds 	cd->device->changed = 1;	/* force recheck CD type */
72093aae17aSTejun Heo 	cd->media_present = 1;
7211da177e4SLinus Torvalds 	cd->use = 1;
7221da177e4SLinus Torvalds 	cd->readcd_known = 0;
7231da177e4SLinus Torvalds 	cd->readcd_cdda = 0;
7241da177e4SLinus Torvalds 
7251da177e4SLinus Torvalds 	cd->cdi.ops = &sr_dops;
7261da177e4SLinus Torvalds 	cd->cdi.handle = cd;
7271da177e4SLinus Torvalds 	cd->cdi.mask = 0;
7281da177e4SLinus Torvalds 	cd->cdi.capacity = 1;
7291da177e4SLinus Torvalds 	sprintf(cd->cdi.name, "sr%d", minor);
7301da177e4SLinus Torvalds 
7311da177e4SLinus Torvalds 	sdev->sector_size = 2048;	/* A guess, just in case */
7321da177e4SLinus Torvalds 
7331da177e4SLinus Torvalds 	/* FIXME: need to handle a get_capabilities failure properly ?? */
7341da177e4SLinus Torvalds 	get_capabilities(cd);
7357f9a6bc4SJames Bottomley 	blk_queue_prep_rq(sdev->request_queue, sr_prep_fn);
7361da177e4SLinus Torvalds 	sr_vendor_init(cd);
7371da177e4SLinus Torvalds 
7381da177e4SLinus Torvalds 	disk->driverfs_dev = &sdev->sdev_gendev;
7391da177e4SLinus Torvalds 	set_capacity(disk, cd->capacity);
7401da177e4SLinus Torvalds 	disk->private_data = &cd->driver;
7411da177e4SLinus Torvalds 	disk->queue = sdev->request_queue;
7421da177e4SLinus Torvalds 	cd->cdi.disk = disk;
7431da177e4SLinus Torvalds 
7441da177e4SLinus Torvalds 	if (register_cdrom(&cd->cdi))
7451da177e4SLinus Torvalds 		goto fail_put;
7461da177e4SLinus Torvalds 
7471da177e4SLinus Torvalds 	dev_set_drvdata(dev, cd);
7481da177e4SLinus Torvalds 	disk->flags |= GENHD_FL_REMOVABLE;
7491da177e4SLinus Torvalds 	add_disk(disk);
7501da177e4SLinus Torvalds 
7519ccfc756SJames Bottomley 	sdev_printk(KERN_DEBUG, sdev,
7529ccfc756SJames Bottomley 		    "Attached scsi CD-ROM %s\n", cd->cdi.name);
753*6c7f1e2fSAaron Lu 	scsi_autopm_put_device(cd->device);
754*6c7f1e2fSAaron Lu 
7551da177e4SLinus Torvalds 	return 0;
7561da177e4SLinus Torvalds 
7571da177e4SLinus Torvalds fail_put:
7581da177e4SLinus Torvalds 	put_disk(disk);
7591da177e4SLinus Torvalds fail_free:
7601da177e4SLinus Torvalds 	kfree(cd);
7611da177e4SLinus Torvalds fail:
7621da177e4SLinus Torvalds 	return error;
7631da177e4SLinus Torvalds }
7641da177e4SLinus Torvalds 
7651da177e4SLinus Torvalds 
7661da177e4SLinus Torvalds static void get_sectorsize(struct scsi_cd *cd)
7671da177e4SLinus Torvalds {
7681da177e4SLinus Torvalds 	unsigned char cmd[10];
76962858dacSFUJITA Tomonori 	unsigned char buffer[8];
7701da177e4SLinus Torvalds 	int the_result, retries = 3;
7711da177e4SLinus Torvalds 	int sector_size;
772165125e1SJens Axboe 	struct request_queue *queue;
7731da177e4SLinus Torvalds 
7741da177e4SLinus Torvalds 	do {
7751da177e4SLinus Torvalds 		cmd[0] = READ_CAPACITY;
7761da177e4SLinus Torvalds 		memset((void *) &cmd[1], 0, 9);
77762858dacSFUJITA Tomonori 		memset(buffer, 0, sizeof(buffer));
7781da177e4SLinus Torvalds 
7791da177e4SLinus Torvalds 		/* Do the command and wait.. */
780820732b5SJames Bottomley 		the_result = scsi_execute_req(cd->device, cmd, DMA_FROM_DEVICE,
78162858dacSFUJITA Tomonori 					      buffer, sizeof(buffer), NULL,
782f4f4e47eSFUJITA Tomonori 					      SR_TIMEOUT, MAX_RETRIES, NULL);
7831da177e4SLinus Torvalds 
7841da177e4SLinus Torvalds 		retries--;
7851da177e4SLinus Torvalds 
7861da177e4SLinus Torvalds 	} while (the_result && retries);
7871da177e4SLinus Torvalds 
7881da177e4SLinus Torvalds 
7891da177e4SLinus Torvalds 	if (the_result) {
7901da177e4SLinus Torvalds 		cd->capacity = 0x1fffff;
7911da177e4SLinus Torvalds 		sector_size = 2048;	/* A guess, just in case */
7921da177e4SLinus Torvalds 	} else {
7935915136dSTejun Heo 		long last_written;
7945915136dSTejun Heo 
7955915136dSTejun Heo 		cd->capacity = 1 + ((buffer[0] << 24) | (buffer[1] << 16) |
7965915136dSTejun Heo 				    (buffer[2] << 8) | buffer[3]);
7975915136dSTejun Heo 		/*
7985915136dSTejun Heo 		 * READ_CAPACITY doesn't return the correct size on
7995915136dSTejun Heo 		 * certain UDF media.  If last_written is larger, use
8005915136dSTejun Heo 		 * it instead.
8015915136dSTejun Heo 		 *
8025915136dSTejun Heo 		 * http://bugzilla.kernel.org/show_bug.cgi?id=9668
8035915136dSTejun Heo 		 */
8045915136dSTejun Heo 		if (!cdrom_get_last_written(&cd->cdi, &last_written))
8055915136dSTejun Heo 			cd->capacity = max_t(long, cd->capacity, last_written);
8065915136dSTejun Heo 
8071da177e4SLinus Torvalds 		sector_size = (buffer[4] << 24) |
8081da177e4SLinus Torvalds 		    (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
8091da177e4SLinus Torvalds 		switch (sector_size) {
8101da177e4SLinus Torvalds 			/*
8111da177e4SLinus Torvalds 			 * HP 4020i CD-Recorder reports 2340 byte sectors
8121da177e4SLinus Torvalds 			 * Philips CD-Writers report 2352 byte sectors
8131da177e4SLinus Torvalds 			 *
8141da177e4SLinus Torvalds 			 * Use 2k sectors for them..
8151da177e4SLinus Torvalds 			 */
8161da177e4SLinus Torvalds 		case 0:
8171da177e4SLinus Torvalds 		case 2340:
8181da177e4SLinus Torvalds 		case 2352:
8191da177e4SLinus Torvalds 			sector_size = 2048;
8201da177e4SLinus Torvalds 			/* fall through */
8211da177e4SLinus Torvalds 		case 2048:
8221da177e4SLinus Torvalds 			cd->capacity *= 4;
8231da177e4SLinus Torvalds 			/* fall through */
8241da177e4SLinus Torvalds 		case 512:
8251da177e4SLinus Torvalds 			break;
8261da177e4SLinus Torvalds 		default:
8271da177e4SLinus Torvalds 			printk("%s: unsupported sector size %d.\n",
8281da177e4SLinus Torvalds 			       cd->cdi.name, sector_size);
8291da177e4SLinus Torvalds 			cd->capacity = 0;
8301da177e4SLinus Torvalds 		}
8311da177e4SLinus Torvalds 
8321da177e4SLinus Torvalds 		cd->device->sector_size = sector_size;
8331da177e4SLinus Torvalds 
8341da177e4SLinus Torvalds 		/*
8351da177e4SLinus Torvalds 		 * Add this so that we have the ability to correctly gauge
8361da177e4SLinus Torvalds 		 * what the device is capable of.
8371da177e4SLinus Torvalds 		 */
8381da177e4SLinus Torvalds 		set_capacity(cd->disk, cd->capacity);
8391da177e4SLinus Torvalds 	}
8401da177e4SLinus Torvalds 
8411da177e4SLinus Torvalds 	queue = cd->device->request_queue;
842e1defc4fSMartin K. Petersen 	blk_queue_logical_block_size(queue, sector_size);
8431da177e4SLinus Torvalds 
84462858dacSFUJITA Tomonori 	return;
8451da177e4SLinus Torvalds }
8461da177e4SLinus Torvalds 
8471da177e4SLinus Torvalds static void get_capabilities(struct scsi_cd *cd)
8481da177e4SLinus Torvalds {
8491da177e4SLinus Torvalds 	unsigned char *buffer;
8501da177e4SLinus Torvalds 	struct scsi_mode_data data;
851820732b5SJames Bottomley 	struct scsi_sense_hdr sshdr;
85238582a62SJames Bottomley 	int rc, n;
8531da177e4SLinus Torvalds 
8540ad78200SArjan van de Ven 	static const char *loadmech[] =
8551da177e4SLinus Torvalds 	{
8561da177e4SLinus Torvalds 		"caddy",
8571da177e4SLinus Torvalds 		"tray",
8581da177e4SLinus Torvalds 		"pop-up",
8591da177e4SLinus Torvalds 		"",
8601da177e4SLinus Torvalds 		"changer",
8611da177e4SLinus Torvalds 		"cartridge changer",
8621da177e4SLinus Torvalds 		"",
8631da177e4SLinus Torvalds 		""
8641da177e4SLinus Torvalds 	};
8651da177e4SLinus Torvalds 
8661da177e4SLinus Torvalds 
8671da177e4SLinus Torvalds 	/* allocate transfer buffer */
8681da177e4SLinus Torvalds 	buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
8691da177e4SLinus Torvalds 	if (!buffer) {
8701da177e4SLinus Torvalds 		printk(KERN_ERR "sr: out of memory.\n");
8711da177e4SLinus Torvalds 		return;
8721da177e4SLinus Torvalds 	}
8731da177e4SLinus Torvalds 
87438582a62SJames Bottomley 	/* eat unit attentions */
8759f8a2c23STejun Heo 	scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES, &sshdr);
8761da177e4SLinus Torvalds 
8771da177e4SLinus Torvalds 	/* ask for mode page 0x2a */
8781da177e4SLinus Torvalds 	rc = scsi_mode_sense(cd->device, 0, 0x2a, buffer, 128,
8791cf72699SJames Bottomley 			     SR_TIMEOUT, 3, &data, NULL);
8801da177e4SLinus Torvalds 
8811da177e4SLinus Torvalds 	if (!scsi_status_is_good(rc)) {
8821da177e4SLinus Torvalds 		/* failed, drive doesn't have capabilities mode page */
8831da177e4SLinus Torvalds 		cd->cdi.speed = 1;
8841da177e4SLinus Torvalds 		cd->cdi.mask |= (CDC_CD_R | CDC_CD_RW | CDC_DVD_R |
8851da177e4SLinus Torvalds 				 CDC_DVD | CDC_DVD_RAM |
886bcc1e382SChuck Ebbert 				 CDC_SELECT_DISC | CDC_SELECT_SPEED |
887bcc1e382SChuck Ebbert 				 CDC_MRW | CDC_MRW_W | CDC_RAM);
8881da177e4SLinus Torvalds 		kfree(buffer);
8891da177e4SLinus Torvalds 		printk("%s: scsi-1 drive\n", cd->cdi.name);
8901da177e4SLinus Torvalds 		return;
8911da177e4SLinus Torvalds 	}
8921da177e4SLinus Torvalds 
8931da177e4SLinus Torvalds 	n = data.header_length + data.block_descriptor_length;
8941da177e4SLinus Torvalds 	cd->cdi.speed = ((buffer[n + 8] << 8) + buffer[n + 9]) / 176;
8951da177e4SLinus Torvalds 	cd->readcd_known = 1;
8961da177e4SLinus Torvalds 	cd->readcd_cdda = buffer[n + 5] & 0x01;
8971da177e4SLinus Torvalds 	/* print some capability bits */
8981da177e4SLinus Torvalds 	printk("%s: scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n", cd->cdi.name,
8991da177e4SLinus Torvalds 	       ((buffer[n + 14] << 8) + buffer[n + 15]) / 176,
9001da177e4SLinus Torvalds 	       cd->cdi.speed,
9011da177e4SLinus Torvalds 	       buffer[n + 3] & 0x01 ? "writer " : "", /* CD Writer */
9021da177e4SLinus Torvalds 	       buffer[n + 3] & 0x20 ? "dvd-ram " : "",
9031da177e4SLinus Torvalds 	       buffer[n + 2] & 0x02 ? "cd/rw " : "", /* can read rewriteable */
9041da177e4SLinus Torvalds 	       buffer[n + 4] & 0x20 ? "xa/form2 " : "",	/* can read xa/from2 */
9051da177e4SLinus Torvalds 	       buffer[n + 5] & 0x01 ? "cdda " : "", /* can read audio data */
9061da177e4SLinus Torvalds 	       loadmech[buffer[n + 6] >> 5]);
9071da177e4SLinus Torvalds 	if ((buffer[n + 6] >> 5) == 0)
9081da177e4SLinus Torvalds 		/* caddy drives can't close tray... */
9091da177e4SLinus Torvalds 		cd->cdi.mask |= CDC_CLOSE_TRAY;
9101da177e4SLinus Torvalds 	if ((buffer[n + 2] & 0x8) == 0)
9111da177e4SLinus Torvalds 		/* not a DVD drive */
9121da177e4SLinus Torvalds 		cd->cdi.mask |= CDC_DVD;
9131da177e4SLinus Torvalds 	if ((buffer[n + 3] & 0x20) == 0)
9141da177e4SLinus Torvalds 		/* can't write DVD-RAM media */
9151da177e4SLinus Torvalds 		cd->cdi.mask |= CDC_DVD_RAM;
9161da177e4SLinus Torvalds 	if ((buffer[n + 3] & 0x10) == 0)
9171da177e4SLinus Torvalds 		/* can't write DVD-R media */
9181da177e4SLinus Torvalds 		cd->cdi.mask |= CDC_DVD_R;
9191da177e4SLinus Torvalds 	if ((buffer[n + 3] & 0x2) == 0)
9201da177e4SLinus Torvalds 		/* can't write CD-RW media */
9211da177e4SLinus Torvalds 		cd->cdi.mask |= CDC_CD_RW;
9221da177e4SLinus Torvalds 	if ((buffer[n + 3] & 0x1) == 0)
9231da177e4SLinus Torvalds 		/* can't write CD-R media */
9241da177e4SLinus Torvalds 		cd->cdi.mask |= CDC_CD_R;
9251da177e4SLinus Torvalds 	if ((buffer[n + 6] & 0x8) == 0)
9261da177e4SLinus Torvalds 		/* can't eject */
9271da177e4SLinus Torvalds 		cd->cdi.mask |= CDC_OPEN_TRAY;
9281da177e4SLinus Torvalds 
9291da177e4SLinus Torvalds 	if ((buffer[n + 6] >> 5) == mechtype_individual_changer ||
9301da177e4SLinus Torvalds 	    (buffer[n + 6] >> 5) == mechtype_cartridge_changer)
9311da177e4SLinus Torvalds 		cd->cdi.capacity =
9321da177e4SLinus Torvalds 		    cdrom_number_of_slots(&cd->cdi);
9331da177e4SLinus Torvalds 	if (cd->cdi.capacity <= 1)
9341da177e4SLinus Torvalds 		/* not a changer */
9351da177e4SLinus Torvalds 		cd->cdi.mask |= CDC_SELECT_DISC;
9361da177e4SLinus Torvalds 	/*else    I don't think it can close its tray
9371da177e4SLinus Torvalds 		cd->cdi.mask |= CDC_CLOSE_TRAY; */
9381da177e4SLinus Torvalds 
9391da177e4SLinus Torvalds 	/*
9401da177e4SLinus Torvalds 	 * if DVD-RAM, MRW-W or CD-RW, we are randomly writable
9411da177e4SLinus Torvalds 	 */
9421da177e4SLinus Torvalds 	if ((cd->cdi.mask & (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) !=
9431da177e4SLinus Torvalds 			(CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) {
9441da177e4SLinus Torvalds 		cd->device->writeable = 1;
9451da177e4SLinus Torvalds 	}
9461da177e4SLinus Torvalds 
9471da177e4SLinus Torvalds 	kfree(buffer);
9481da177e4SLinus Torvalds }
9491da177e4SLinus Torvalds 
9501da177e4SLinus Torvalds /*
9511da177e4SLinus Torvalds  * sr_packet() is the entry point for the generic commands generated
9521da177e4SLinus Torvalds  * by the Uniform CD-ROM layer.
9531da177e4SLinus Torvalds  */
9541da177e4SLinus Torvalds static int sr_packet(struct cdrom_device_info *cdi,
9551da177e4SLinus Torvalds 		struct packet_command *cgc)
9561da177e4SLinus Torvalds {
9578e04d805SHans de Goede 	struct scsi_cd *cd = cdi->handle;
9588e04d805SHans de Goede 	struct scsi_device *sdev = cd->device;
9598e04d805SHans de Goede 
9608e04d805SHans de Goede 	if (cgc->cmd[0] == GPCMD_READ_DISC_INFO && sdev->no_read_disc_info)
9618e04d805SHans de Goede 		return -EDRIVE_CANT_DO_THIS;
9628e04d805SHans de Goede 
9631da177e4SLinus Torvalds 	if (cgc->timeout <= 0)
9641da177e4SLinus Torvalds 		cgc->timeout = IOCTL_TIMEOUT;
9651da177e4SLinus Torvalds 
9668e04d805SHans de Goede 	sr_do_ioctl(cd, cgc);
9671da177e4SLinus Torvalds 
9681da177e4SLinus Torvalds 	return cgc->stat;
9691da177e4SLinus Torvalds }
9701da177e4SLinus Torvalds 
9711da177e4SLinus Torvalds /**
9721da177e4SLinus Torvalds  *	sr_kref_release - Called to free the scsi_cd structure
9731da177e4SLinus Torvalds  *	@kref: pointer to embedded kref
9741da177e4SLinus Torvalds  *
9750b950672SArjan van de Ven  *	sr_ref_mutex must be held entering this routine.  Because it is
9761da177e4SLinus Torvalds  *	called on last put, you should always use the scsi_cd_get()
9771da177e4SLinus Torvalds  *	scsi_cd_put() helpers which manipulate the semaphore directly
9781da177e4SLinus Torvalds  *	and never do a direct kref_put().
9791da177e4SLinus Torvalds  **/
9801da177e4SLinus Torvalds static void sr_kref_release(struct kref *kref)
9811da177e4SLinus Torvalds {
9821da177e4SLinus Torvalds 	struct scsi_cd *cd = container_of(kref, struct scsi_cd, kref);
9831da177e4SLinus Torvalds 	struct gendisk *disk = cd->disk;
9841da177e4SLinus Torvalds 
9851da177e4SLinus Torvalds 	spin_lock(&sr_index_lock);
986f331c029STejun Heo 	clear_bit(MINOR(disk_devt(disk)), sr_index_bits);
9871da177e4SLinus Torvalds 	spin_unlock(&sr_index_lock);
9881da177e4SLinus Torvalds 
9891da177e4SLinus Torvalds 	unregister_cdrom(&cd->cdi);
9901da177e4SLinus Torvalds 
9911da177e4SLinus Torvalds 	disk->private_data = NULL;
9921da177e4SLinus Torvalds 
9931da177e4SLinus Torvalds 	put_disk(disk);
9941da177e4SLinus Torvalds 
9951da177e4SLinus Torvalds 	kfree(cd);
9961da177e4SLinus Torvalds }
9971da177e4SLinus Torvalds 
9981da177e4SLinus Torvalds static int sr_remove(struct device *dev)
9991da177e4SLinus Torvalds {
10001da177e4SLinus Torvalds 	struct scsi_cd *cd = dev_get_drvdata(dev);
10011da177e4SLinus Torvalds 
1002*6c7f1e2fSAaron Lu 	scsi_autopm_get_device(cd->device);
1003*6c7f1e2fSAaron Lu 
1004b391277aSHannes Reinecke 	blk_queue_prep_rq(cd->device->request_queue, scsi_prep_fn);
10051da177e4SLinus Torvalds 	del_gendisk(cd->disk);
10061da177e4SLinus Torvalds 
10070b950672SArjan van de Ven 	mutex_lock(&sr_ref_mutex);
10081da177e4SLinus Torvalds 	kref_put(&cd->kref, sr_kref_release);
10090b950672SArjan van de Ven 	mutex_unlock(&sr_ref_mutex);
10101da177e4SLinus Torvalds 
10111da177e4SLinus Torvalds 	return 0;
10121da177e4SLinus Torvalds }
10131da177e4SLinus Torvalds 
10141da177e4SLinus Torvalds static int __init init_sr(void)
10151da177e4SLinus Torvalds {
10161da177e4SLinus Torvalds 	int rc;
10171da177e4SLinus Torvalds 
10181da177e4SLinus Torvalds 	rc = register_blkdev(SCSI_CDROM_MAJOR, "sr");
10191da177e4SLinus Torvalds 	if (rc)
10201da177e4SLinus Torvalds 		return rc;
1021da3962feSAkinobu Mita 	rc = scsi_register_driver(&sr_template.gendrv);
1022da3962feSAkinobu Mita 	if (rc)
1023da3962feSAkinobu Mita 		unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
1024da3962feSAkinobu Mita 
1025da3962feSAkinobu Mita 	return rc;
10261da177e4SLinus Torvalds }
10271da177e4SLinus Torvalds 
10281da177e4SLinus Torvalds static void __exit exit_sr(void)
10291da177e4SLinus Torvalds {
10301da177e4SLinus Torvalds 	scsi_unregister_driver(&sr_template.gendrv);
10311da177e4SLinus Torvalds 	unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
10321da177e4SLinus Torvalds }
10331da177e4SLinus Torvalds 
10341da177e4SLinus Torvalds module_init(init_sr);
10351da177e4SLinus Torvalds module_exit(exit_sr);
10361da177e4SLinus Torvalds MODULE_LICENSE("GPL");
1037