xref: /linux/drivers/scsi/sr.c (revision b84ba30b6c7a75babdf73b83bc3c7b59b944501a)
109c434b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  sr.c Copyright (C) 1992 David Giller
41da177e4SLinus Torvalds  *           Copyright (C) 1993, 1994, 1995, 1999 Eric Youngdale
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *  adapted from:
71da177e4SLinus Torvalds  *      sd.c Copyright (C) 1992 Drew Eckhardt
81da177e4SLinus Torvalds  *      Linux scsi disk driver by
91da177e4SLinus Torvalds  *              Drew Eckhardt <drew@colorado.edu>
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  *	Modified by Eric Youngdale ericy@andante.org to
121da177e4SLinus Torvalds  *	add scatter-gather, multiple outstanding request, and other
131da177e4SLinus Torvalds  *	enhancements.
141da177e4SLinus Torvalds  *
151da177e4SLinus Torvalds  *      Modified by Eric Youngdale eric@andante.org to support loadable
161da177e4SLinus Torvalds  *      low-level scsi drivers.
171da177e4SLinus Torvalds  *
181da177e4SLinus Torvalds  *      Modified by Thomas Quinot thomas@melchior.cuivre.fdn.fr to
191da177e4SLinus Torvalds  *      provide auto-eject.
201da177e4SLinus Torvalds  *
211da177e4SLinus Torvalds  *      Modified by Gerd Knorr <kraxel@cs.tu-berlin.de> to support the
221da177e4SLinus Torvalds  *      generic cdrom interface
231da177e4SLinus Torvalds  *
241da177e4SLinus Torvalds  *      Modified by Jens Axboe <axboe@suse.de> - Uniform sr_packet()
251da177e4SLinus Torvalds  *      interface, capabilities probe additions, ioctl cleanups, etc.
261da177e4SLinus Torvalds  *
271da177e4SLinus Torvalds  *	Modified by Richard Gooch <rgooch@atnf.csiro.au> to support devfs
281da177e4SLinus Torvalds  *
291da177e4SLinus Torvalds  *	Modified by Jens Axboe <axboe@suse.de> - support DVD-RAM
301da177e4SLinus Torvalds  *	transparently and lose the GHOST hack
311da177e4SLinus Torvalds  *
321da177e4SLinus Torvalds  *	Modified by Arnaldo Carvalho de Melo <acme@conectiva.com.br>
331da177e4SLinus Torvalds  *	check resource allocation in sr_init and some cleanups
341da177e4SLinus Torvalds  */
351da177e4SLinus Torvalds 
361da177e4SLinus Torvalds #include <linux/module.h>
371da177e4SLinus Torvalds #include <linux/fs.h>
381da177e4SLinus Torvalds #include <linux/kernel.h>
391da177e4SLinus Torvalds #include <linux/mm.h>
401da177e4SLinus Torvalds #include <linux/bio.h>
41d320a955SArnd Bergmann #include <linux/compat.h>
421da177e4SLinus Torvalds #include <linux/string.h>
431da177e4SLinus Torvalds #include <linux/errno.h>
441da177e4SLinus Torvalds #include <linux/cdrom.h>
451da177e4SLinus Torvalds #include <linux/interrupt.h>
461da177e4SLinus Torvalds #include <linux/init.h>
47b81e0c23SChristoph Hellwig #include <linux/major.h>
481da177e4SLinus Torvalds #include <linux/blkdev.h>
49bca6b067SBart Van Assche #include <linux/blk-pm.h>
500b950672SArjan van de Ven #include <linux/mutex.h>
515a0e3ad6STejun Heo #include <linux/slab.h>
526c7f1e2fSAaron Lu #include <linux/pm_runtime.h>
537c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
541da177e4SLinus Torvalds 
55655da8e5SBart Van Assche #include <asm/unaligned.h>
56655da8e5SBart Van Assche 
571da177e4SLinus Torvalds #include <scsi/scsi.h>
581da177e4SLinus Torvalds #include <scsi/scsi_dbg.h>
591da177e4SLinus Torvalds #include <scsi/scsi_device.h>
601da177e4SLinus Torvalds #include <scsi/scsi_driver.h>
61820732b5SJames Bottomley #include <scsi/scsi_cmnd.h>
621da177e4SLinus Torvalds #include <scsi/scsi_eh.h>
631da177e4SLinus Torvalds #include <scsi/scsi_host.h>
641da177e4SLinus Torvalds #include <scsi/scsi_ioctl.h>	/* For the door lock/unlock commands */
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds #include "scsi_logging.h"
671da177e4SLinus Torvalds #include "sr.h"
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds 
70f018fa55SRene Herman MODULE_DESCRIPTION("SCSI cdrom (sr) driver");
71f018fa55SRene Herman MODULE_LICENSE("GPL");
72f018fa55SRene Herman MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_CDROM_MAJOR);
73d7b8bcb0SMichael Tokarev MODULE_ALIAS_SCSI_DEVICE(TYPE_ROM);
74d7b8bcb0SMichael Tokarev MODULE_ALIAS_SCSI_DEVICE(TYPE_WORM);
75f018fa55SRene Herman 
761da177e4SLinus Torvalds #define SR_DISKS	256
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds #define SR_CAPABILITIES \
791da177e4SLinus Torvalds 	(CDC_CLOSE_TRAY|CDC_OPEN_TRAY|CDC_LOCK|CDC_SELECT_SPEED| \
801da177e4SLinus Torvalds 	 CDC_SELECT_DISC|CDC_MULTI_SESSION|CDC_MCN|CDC_MEDIA_CHANGED| \
816a2900b6SChristoph Hellwig 	 CDC_PLAY_AUDIO|CDC_RESET|CDC_DRIVE_STATUS| \
821da177e4SLinus Torvalds 	 CDC_CD_R|CDC_CD_RW|CDC_DVD|CDC_DVD_R|CDC_DVD_RAM|CDC_GENERIC_PACKET| \
831da177e4SLinus Torvalds 	 CDC_MRW|CDC_MRW_W|CDC_RAM)
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds static int sr_probe(struct device *);
861da177e4SLinus Torvalds static int sr_remove(struct device *);
87159b2cbfSChristoph Hellwig static blk_status_t sr_init_command(struct scsi_cmnd *SCpnt);
887b3d9545SLinus Torvalds static int sr_done(struct scsi_cmnd *);
896c7f1e2fSAaron Lu static int sr_runtime_suspend(struct device *dev);
906c7f1e2fSAaron Lu 
91a816b4c6SJulia Lawall static const struct dev_pm_ops sr_pm_ops = {
926c7f1e2fSAaron Lu 	.runtime_suspend	= sr_runtime_suspend,
936c7f1e2fSAaron Lu };
941da177e4SLinus Torvalds 
951da177e4SLinus Torvalds static struct scsi_driver sr_template = {
961da177e4SLinus Torvalds 	.gendrv = {
971da177e4SLinus Torvalds 		.name   	= "sr",
983af6b352SChristoph Hellwig 		.owner		= THIS_MODULE,
991da177e4SLinus Torvalds 		.probe		= sr_probe,
1001da177e4SLinus Torvalds 		.remove		= sr_remove,
1016c7f1e2fSAaron Lu 		.pm		= &sr_pm_ops,
1021da177e4SLinus Torvalds 	},
103a1b73fc1SChristoph Hellwig 	.init_command		= sr_init_command,
1047b3d9545SLinus Torvalds 	.done			= sr_done,
1051da177e4SLinus Torvalds };
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds static unsigned long sr_index_bits[SR_DISKS / BITS_PER_LONG];
1081da177e4SLinus Torvalds static DEFINE_SPINLOCK(sr_index_lock);
1091da177e4SLinus Torvalds 
1109c2b9dbaSChristoph Hellwig static struct lock_class_key sr_bio_compl_lkclass;
1119c2b9dbaSChristoph Hellwig 
1121da177e4SLinus Torvalds /* This semaphore is used to mediate the 0->1 reference get in the
1131da177e4SLinus Torvalds  * face of object destruction (i.e. we can't allow a get on an
1141da177e4SLinus Torvalds  * object after last put) */
1150b950672SArjan van de Ven static DEFINE_MUTEX(sr_ref_mutex);
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds static int sr_open(struct cdrom_device_info *, int);
1181da177e4SLinus Torvalds static void sr_release(struct cdrom_device_info *);
1191da177e4SLinus Torvalds 
1201da177e4SLinus Torvalds static void get_sectorsize(struct scsi_cd *);
1211da177e4SLinus Torvalds static void get_capabilities(struct scsi_cd *);
1221da177e4SLinus Torvalds 
12393aae17aSTejun Heo static unsigned int sr_check_events(struct cdrom_device_info *cdi,
12493aae17aSTejun Heo 				    unsigned int clearing, int slot);
1251da177e4SLinus Torvalds static int sr_packet(struct cdrom_device_info *, struct packet_command *);
126ba51bdafSChristoph Hellwig static int sr_read_cdda_bpc(struct cdrom_device_info *cdi, void __user *ubuf,
127ba51bdafSChristoph Hellwig 		u32 lba, u32 nr, u8 *last_sense);
1281da177e4SLinus Torvalds 
129853fe1bfSKees Cook static const struct cdrom_device_ops sr_dops = {
1301da177e4SLinus Torvalds 	.open			= sr_open,
1311da177e4SLinus Torvalds 	.release	 	= sr_release,
1321da177e4SLinus Torvalds 	.drive_status	 	= sr_drive_status,
13393aae17aSTejun Heo 	.check_events		= sr_check_events,
1341da177e4SLinus Torvalds 	.tray_move		= sr_tray_move,
1351da177e4SLinus Torvalds 	.lock_door		= sr_lock_door,
1361da177e4SLinus Torvalds 	.select_speed		= sr_select_speed,
1371da177e4SLinus Torvalds 	.get_last_session	= sr_get_last_session,
1381da177e4SLinus Torvalds 	.get_mcn		= sr_get_mcn,
1391da177e4SLinus Torvalds 	.reset			= sr_reset,
1401da177e4SLinus Torvalds 	.audio_ioctl		= sr_audio_ioctl,
1411da177e4SLinus Torvalds 	.generic_packet		= sr_packet,
142ba51bdafSChristoph Hellwig 	.read_cdda_bpc		= sr_read_cdda_bpc,
143ba51bdafSChristoph Hellwig 	.capability		= SR_CAPABILITIES,
1441da177e4SLinus Torvalds };
1451da177e4SLinus Torvalds 
1461da177e4SLinus Torvalds static void sr_kref_release(struct kref *kref);
1471da177e4SLinus Torvalds 
1481da177e4SLinus Torvalds static inline struct scsi_cd *scsi_cd(struct gendisk *disk)
1491da177e4SLinus Torvalds {
1501da177e4SLinus Torvalds 	return container_of(disk->private_data, struct scsi_cd, driver);
1511da177e4SLinus Torvalds }
1521da177e4SLinus Torvalds 
1536c7f1e2fSAaron Lu static int sr_runtime_suspend(struct device *dev)
1546c7f1e2fSAaron Lu {
1556c7f1e2fSAaron Lu 	struct scsi_cd *cd = dev_get_drvdata(dev);
1566c7f1e2fSAaron Lu 
15713b43891SAlan Stern 	if (!cd)	/* E.g.: runtime suspend following sr_remove() */
15813b43891SAlan Stern 		return 0;
15913b43891SAlan Stern 
1606c7f1e2fSAaron Lu 	if (cd->media_present)
1616c7f1e2fSAaron Lu 		return -EBUSY;
1626c7f1e2fSAaron Lu 	else
1636c7f1e2fSAaron Lu 		return 0;
1646c7f1e2fSAaron Lu }
1656c7f1e2fSAaron Lu 
1661da177e4SLinus Torvalds /*
1671da177e4SLinus Torvalds  * The get and put routines for the struct scsi_cd.  Note this entity
1681da177e4SLinus Torvalds  * has a scsi_device pointer and owns a reference to this.
1691da177e4SLinus Torvalds  */
1701da177e4SLinus Torvalds static inline struct scsi_cd *scsi_cd_get(struct gendisk *disk)
1711da177e4SLinus Torvalds {
1721da177e4SLinus Torvalds 	struct scsi_cd *cd = NULL;
1731da177e4SLinus Torvalds 
1740b950672SArjan van de Ven 	mutex_lock(&sr_ref_mutex);
1751da177e4SLinus Torvalds 	if (disk->private_data == NULL)
1761da177e4SLinus Torvalds 		goto out;
1771da177e4SLinus Torvalds 	cd = scsi_cd(disk);
1781da177e4SLinus Torvalds 	kref_get(&cd->kref);
1796627b38fSAaron Lu 	if (scsi_device_get(cd->device)) {
1801da177e4SLinus Torvalds 		kref_put(&cd->kref, sr_kref_release);
1811da177e4SLinus Torvalds 		cd = NULL;
1826627b38fSAaron Lu 	}
1831da177e4SLinus Torvalds  out:
1840b950672SArjan van de Ven 	mutex_unlock(&sr_ref_mutex);
1851da177e4SLinus Torvalds 	return cd;
1861da177e4SLinus Torvalds }
1871da177e4SLinus Torvalds 
188858119e1SArjan van de Ven static void scsi_cd_put(struct scsi_cd *cd)
1891da177e4SLinus Torvalds {
1901da177e4SLinus Torvalds 	struct scsi_device *sdev = cd->device;
1911da177e4SLinus Torvalds 
1920b950672SArjan van de Ven 	mutex_lock(&sr_ref_mutex);
1931da177e4SLinus Torvalds 	kref_put(&cd->kref, sr_kref_release);
1941da177e4SLinus Torvalds 	scsi_device_put(sdev);
1950b950672SArjan van de Ven 	mutex_unlock(&sr_ref_mutex);
1961da177e4SLinus Torvalds }
1971da177e4SLinus Torvalds 
19893aae17aSTejun Heo static unsigned int sr_get_events(struct scsi_device *sdev)
1991da177e4SLinus Torvalds {
20093aae17aSTejun Heo 	u8 buf[8];
20193aae17aSTejun Heo 	u8 cmd[] = { GET_EVENT_STATUS_NOTIFICATION,
20293aae17aSTejun Heo 		     1,			/* polled */
20393aae17aSTejun Heo 		     0, 0,		/* reserved */
20493aae17aSTejun Heo 		     1 << 4,		/* notification class: media */
20593aae17aSTejun Heo 		     0, 0,		/* reserved */
20693aae17aSTejun Heo 		     0, sizeof(buf),	/* allocation length */
20793aae17aSTejun Heo 		     0,			/* control */
20893aae17aSTejun Heo 	};
20993aae17aSTejun Heo 	struct event_header *eh = (void *)buf;
21093aae17aSTejun Heo 	struct media_event_desc *med = (void *)(buf + 4);
21193aae17aSTejun Heo 	struct scsi_sense_hdr sshdr;
21293aae17aSTejun Heo 	int result;
2131da177e4SLinus Torvalds 
21493aae17aSTejun Heo 	result = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buf, sizeof(buf),
21593aae17aSTejun Heo 				  &sshdr, SR_TIMEOUT, MAX_RETRIES, NULL);
21693aae17aSTejun Heo 	if (scsi_sense_valid(&sshdr) && sshdr.sense_key == UNIT_ATTENTION)
21793aae17aSTejun Heo 		return DISK_EVENT_MEDIA_CHANGE;
21893aae17aSTejun Heo 
21993aae17aSTejun Heo 	if (result || be16_to_cpu(eh->data_len) < sizeof(*med))
22093aae17aSTejun Heo 		return 0;
22193aae17aSTejun Heo 
22293aae17aSTejun Heo 	if (eh->nea || eh->notification_class != 0x4)
22393aae17aSTejun Heo 		return 0;
22493aae17aSTejun Heo 
22593aae17aSTejun Heo 	if (med->media_event_code == 1)
22693aae17aSTejun Heo 		return DISK_EVENT_EJECT_REQUEST;
22793aae17aSTejun Heo 	else if (med->media_event_code == 2)
22893aae17aSTejun Heo 		return DISK_EVENT_MEDIA_CHANGE;
2297dd753caSManYi Li 	else if (med->media_event_code == 3)
2305c04243aSLi Manyi 		return DISK_EVENT_MEDIA_CHANGE;
23193aae17aSTejun Heo 	return 0;
2321da177e4SLinus Torvalds }
2331da177e4SLinus Torvalds 
23493aae17aSTejun Heo /*
23593aae17aSTejun Heo  * This function checks to see if the media has been changed or eject
23693aae17aSTejun Heo  * button has been pressed.  It is possible that we have already
23793aae17aSTejun Heo  * sensed a change, or the drive may have sensed one and not yet
23893aae17aSTejun Heo  * reported it.  The past events are accumulated in sdev->changed and
23993aae17aSTejun Heo  * returned together with the current state.
24093aae17aSTejun Heo  */
24193aae17aSTejun Heo static unsigned int sr_check_events(struct cdrom_device_info *cdi,
24293aae17aSTejun Heo 				    unsigned int clearing, int slot)
24393aae17aSTejun Heo {
24493aae17aSTejun Heo 	struct scsi_cd *cd = cdi->handle;
24593aae17aSTejun Heo 	bool last_present;
24693aae17aSTejun Heo 	struct scsi_sense_hdr sshdr;
24793aae17aSTejun Heo 	unsigned int events;
24893aae17aSTejun Heo 	int ret;
24993aae17aSTejun Heo 
25093aae17aSTejun Heo 	/* no changer support */
25193aae17aSTejun Heo 	if (CDSL_CURRENT != slot)
25293aae17aSTejun Heo 		return 0;
25393aae17aSTejun Heo 
25493aae17aSTejun Heo 	events = sr_get_events(cd->device);
25579b9677dSKay Sievers 	cd->get_event_changed |= events & DISK_EVENT_MEDIA_CHANGE;
25679b9677dSKay Sievers 
25779b9677dSKay Sievers 	/*
25879b9677dSKay Sievers 	 * If earlier GET_EVENT_STATUS_NOTIFICATION and TUR did not agree
25979b9677dSKay Sievers 	 * for several times in a row.  We rely on TUR only for this likely
26079b9677dSKay Sievers 	 * broken device, to prevent generating incorrect media changed
26179b9677dSKay Sievers 	 * events for every open().
26279b9677dSKay Sievers 	 */
26379b9677dSKay Sievers 	if (cd->ignore_get_event) {
26479b9677dSKay Sievers 		events &= ~DISK_EVENT_MEDIA_CHANGE;
26579b9677dSKay Sievers 		goto do_tur;
26679b9677dSKay Sievers 	}
26779b9677dSKay Sievers 
26893aae17aSTejun Heo 	/*
26993aae17aSTejun Heo 	 * GET_EVENT_STATUS_NOTIFICATION is enough unless MEDIA_CHANGE
27093aae17aSTejun Heo 	 * is being cleared.  Note that there are devices which hang
27193aae17aSTejun Heo 	 * if asked to execute TUR repeatedly.
27293aae17aSTejun Heo 	 */
27379b9677dSKay Sievers 	if (cd->device->changed) {
27479b9677dSKay Sievers 		events |= DISK_EVENT_MEDIA_CHANGE;
27579b9677dSKay Sievers 		cd->device->changed = 0;
27679b9677dSKay Sievers 		cd->tur_changed = true;
27779b9677dSKay Sievers 	}
27893aae17aSTejun Heo 
27979b9677dSKay Sievers 	if (!(clearing & DISK_EVENT_MEDIA_CHANGE))
28079b9677dSKay Sievers 		return events;
28179b9677dSKay Sievers do_tur:
28293aae17aSTejun Heo 	/* let's see whether the media is there with TUR */
28393aae17aSTejun Heo 	last_present = cd->media_present;
28493aae17aSTejun Heo 	ret = scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES, &sshdr);
28593aae17aSTejun Heo 
286638428ecSTejun Heo 	/*
287638428ecSTejun Heo 	 * Media is considered to be present if TUR succeeds or fails with
288638428ecSTejun Heo 	 * sense data indicating something other than media-not-present
289638428ecSTejun Heo 	 * (ASC 0x3a).
290638428ecSTejun Heo 	 */
29193aae17aSTejun Heo 	cd->media_present = scsi_status_is_good(ret) ||
29293aae17aSTejun Heo 		(scsi_sense_valid(&sshdr) && sshdr.asc != 0x3a);
2931da177e4SLinus Torvalds 
29493aae17aSTejun Heo 	if (last_present != cd->media_present)
29579b9677dSKay Sievers 		cd->device->changed = 1;
29679b9677dSKay Sievers 
29793aae17aSTejun Heo 	if (cd->device->changed) {
29893aae17aSTejun Heo 		events |= DISK_EVENT_MEDIA_CHANGE;
2991da177e4SLinus Torvalds 		cd->device->changed = 0;
30079b9677dSKay Sievers 		cd->tur_changed = true;
3011da177e4SLinus Torvalds 	}
302285e9670SKay Sievers 
30379b9677dSKay Sievers 	if (cd->ignore_get_event)
30479b9677dSKay Sievers 		return events;
30579b9677dSKay Sievers 
30679b9677dSKay Sievers 	/* check whether GET_EVENT is reporting spurious MEDIA_CHANGE */
30779b9677dSKay Sievers 	if (!cd->tur_changed) {
30879b9677dSKay Sievers 		if (cd->get_event_changed) {
30979b9677dSKay Sievers 			if (cd->tur_mismatch++ > 8) {
31096eefad2SHannes Reinecke 				sr_printk(KERN_WARNING, cd,
31179b9677dSKay Sievers 					  "GET_EVENT and TUR disagree continuously, suppress GET_EVENT events\n");
31279b9677dSKay Sievers 				cd->ignore_get_event = true;
31379b9677dSKay Sievers 			}
31479b9677dSKay Sievers 		} else {
31579b9677dSKay Sievers 			cd->tur_mismatch = 0;
31679b9677dSKay Sievers 		}
31779b9677dSKay Sievers 	}
31879b9677dSKay Sievers 	cd->tur_changed = false;
31979b9677dSKay Sievers 	cd->get_event_changed = false;
32079b9677dSKay Sievers 
32193aae17aSTejun Heo 	return events;
3221da177e4SLinus Torvalds }
3231da177e4SLinus Torvalds 
3241da177e4SLinus Torvalds /*
3257b3d9545SLinus Torvalds  * sr_done is the interrupt routine for the device driver.
3261da177e4SLinus Torvalds  *
3277b3d9545SLinus Torvalds  * It will be notified on the end of a SCSI read / write, and will take one
3281da177e4SLinus Torvalds  * of several actions based on success or failure.
3291da177e4SLinus Torvalds  */
3307b3d9545SLinus Torvalds static int sr_done(struct scsi_cmnd *SCpnt)
3311da177e4SLinus Torvalds {
3321da177e4SLinus Torvalds 	int result = SCpnt->result;
33330b0c37bSBoaz Harrosh 	int this_count = scsi_bufflen(SCpnt);
3341da177e4SLinus Torvalds 	int good_bytes = (result == 0 ? this_count : 0);
3351da177e4SLinus Torvalds 	int block_sectors = 0;
3361da177e4SLinus Torvalds 	long error_sector;
337c4deb5b5SBart Van Assche 	struct request *rq = scsi_cmd_to_rq(SCpnt);
338f3fa33acSChristoph Hellwig 	struct scsi_cd *cd = scsi_cd(rq->q->disk);
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds #ifdef DEBUG
34196eefad2SHannes Reinecke 	scmd_printk(KERN_INFO, SCpnt, "done: %x\n", result);
3421da177e4SLinus Torvalds #endif
3431da177e4SLinus Torvalds 
3441da177e4SLinus Torvalds 	/*
3451da177e4SLinus Torvalds 	 * Handle MEDIUM ERRORs or VOLUME OVERFLOWs that indicate partial
3461da177e4SLinus Torvalds 	 * success.  Since this is a relatively rare error condition, no
3471da177e4SLinus Torvalds 	 * care is taken to avoid unnecessary additional work such as
3481da177e4SLinus Torvalds 	 * memcpy's that could be avoided.
3491da177e4SLinus Torvalds 	 */
3503d45cefcSHannes Reinecke 	if (scsi_status_is_check_condition(result) &&
3511da177e4SLinus Torvalds 	    (SCpnt->sense_buffer[0] & 0x7f) == 0x70) { /* Sense current */
3521da177e4SLinus Torvalds 		switch (SCpnt->sense_buffer[2]) {
3531da177e4SLinus Torvalds 		case MEDIUM_ERROR:
3541da177e4SLinus Torvalds 		case VOLUME_OVERFLOW:
3551da177e4SLinus Torvalds 		case ILLEGAL_REQUEST:
3561da177e4SLinus Torvalds 			if (!(SCpnt->sense_buffer[0] & 0x90))
3571da177e4SLinus Torvalds 				break;
358655da8e5SBart Van Assche 			error_sector =
359655da8e5SBart Van Assche 				get_unaligned_be32(&SCpnt->sense_buffer[3]);
360c4deb5b5SBart Van Assche 			if (rq->bio != NULL)
361c4deb5b5SBart Van Assche 				block_sectors = bio_sectors(rq->bio);
3621da177e4SLinus Torvalds 			if (block_sectors < 4)
3631da177e4SLinus Torvalds 				block_sectors = 4;
3641da177e4SLinus Torvalds 			if (cd->device->sector_size == 2048)
3651da177e4SLinus Torvalds 				error_sector <<= 2;
3661da177e4SLinus Torvalds 			error_sector &= ~(block_sectors - 1);
367c4deb5b5SBart Van Assche 			good_bytes = (error_sector - blk_rq_pos(rq)) << 9;
3681da177e4SLinus Torvalds 			if (good_bytes < 0 || good_bytes >= this_count)
3691da177e4SLinus Torvalds 				good_bytes = 0;
3701da177e4SLinus Torvalds 			/*
3711da177e4SLinus Torvalds 			 * The SCSI specification allows for the value
3721da177e4SLinus Torvalds 			 * returned by READ CAPACITY to be up to 75 2K
3731da177e4SLinus Torvalds 			 * sectors past the last readable block.
3741da177e4SLinus Torvalds 			 * Therefore, if we hit a medium error within the
3751da177e4SLinus Torvalds 			 * last 75 2K sectors, we decrease the saved size
3761da177e4SLinus Torvalds 			 * value.
3771da177e4SLinus Torvalds 			 */
3781da177e4SLinus Torvalds 			if (error_sector < get_capacity(cd->disk) &&
3791da177e4SLinus Torvalds 			    cd->capacity - error_sector < 4 * 75)
3801da177e4SLinus Torvalds 				set_capacity(cd->disk, error_sector);
3811da177e4SLinus Torvalds 			break;
3821da177e4SLinus Torvalds 
3831da177e4SLinus Torvalds 		case RECOVERED_ERROR:
3841da177e4SLinus Torvalds 			good_bytes = this_count;
3851da177e4SLinus Torvalds 			break;
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds 		default:
3881da177e4SLinus Torvalds 			break;
3891da177e4SLinus Torvalds 		}
3901da177e4SLinus Torvalds 	}
3911da177e4SLinus Torvalds 
3927b3d9545SLinus Torvalds 	return good_bytes;
3931da177e4SLinus Torvalds }
3941da177e4SLinus Torvalds 
395159b2cbfSChristoph Hellwig static blk_status_t sr_init_command(struct scsi_cmnd *SCpnt)
3961da177e4SLinus Torvalds {
397242f9dcbSJens Axboe 	int block = 0, this_count, s_size;
3987f9a6bc4SJames Bottomley 	struct scsi_cd *cd;
399c4deb5b5SBart Van Assche 	struct request *rq = scsi_cmd_to_rq(SCpnt);
400159b2cbfSChristoph Hellwig 	blk_status_t ret;
4017f9a6bc4SJames Bottomley 
4027007e9ddSChristoph Hellwig 	ret = scsi_alloc_sgtables(SCpnt);
403159b2cbfSChristoph Hellwig 	if (ret != BLK_STS_OK)
4047007e9ddSChristoph Hellwig 		return ret;
405f3fa33acSChristoph Hellwig 	cd = scsi_cd(rq->q->disk);
4067f9a6bc4SJames Bottomley 
40796eefad2SHannes Reinecke 	SCSI_LOG_HLQUEUE(1, scmd_printk(KERN_INFO, SCpnt,
40896eefad2SHannes Reinecke 		"Doing sr request, block = %d\n", block));
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds 	if (!cd->device || !scsi_device_online(cd->device)) {
41196eefad2SHannes Reinecke 		SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
41296eefad2SHannes Reinecke 			"Finishing %u sectors\n", blk_rq_sectors(rq)));
41396eefad2SHannes Reinecke 		SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
41496eefad2SHannes Reinecke 			"Retry with 0x%p\n", SCpnt));
4157f9a6bc4SJames Bottomley 		goto out;
4161da177e4SLinus Torvalds 	}
4171da177e4SLinus Torvalds 
4181da177e4SLinus Torvalds 	if (cd->device->changed) {
4191da177e4SLinus Torvalds 		/*
4201da177e4SLinus Torvalds 		 * quietly refuse to do anything to a changed disc until the
4211da177e4SLinus Torvalds 		 * changed bit has been reset
4221da177e4SLinus Torvalds 		 */
4237f9a6bc4SJames Bottomley 		goto out;
4241da177e4SLinus Torvalds 	}
4251da177e4SLinus Torvalds 
4261da177e4SLinus Torvalds 	s_size = cd->device->sector_size;
4271da177e4SLinus Torvalds 	if (s_size != 512 && s_size != 1024 && s_size != 2048) {
4283bf743e7SJeff Garzik 		scmd_printk(KERN_ERR, SCpnt, "bad sector size %d\n", s_size);
4297f9a6bc4SJames Bottomley 		goto out;
4301da177e4SLinus Torvalds 	}
4311da177e4SLinus Torvalds 
432aebf526bSChristoph Hellwig 	switch (req_op(rq)) {
433aebf526bSChristoph Hellwig 	case REQ_OP_WRITE:
434fd2eb903SChristoph Hellwig 		if (!cd->writeable)
4357f9a6bc4SJames Bottomley 			goto out;
4361da177e4SLinus Torvalds 		SCpnt->cmnd[0] = WRITE_10;
4371da177e4SLinus Torvalds 		cd->cdi.media_written = 1;
438aebf526bSChristoph Hellwig 		break;
439aebf526bSChristoph Hellwig 	case REQ_OP_READ:
4401da177e4SLinus Torvalds 		SCpnt->cmnd[0] = READ_10;
441aebf526bSChristoph Hellwig 		break;
442aebf526bSChristoph Hellwig 	default:
4437f9a6bc4SJames Bottomley 		blk_dump_rq_flags(rq, "Unknown sr command");
4447f9a6bc4SJames Bottomley 		goto out;
4451da177e4SLinus Torvalds 	}
4461da177e4SLinus Torvalds 
4471da177e4SLinus Torvalds 	{
44830b0c37bSBoaz Harrosh 		struct scatterlist *sg;
44930b0c37bSBoaz Harrosh 		int i, size = 0, sg_count = scsi_sg_count(SCpnt);
4501da177e4SLinus Torvalds 
45130b0c37bSBoaz Harrosh 		scsi_for_each_sg(SCpnt, sg, sg_count, i)
45230b0c37bSBoaz Harrosh 			size += sg->length;
45330b0c37bSBoaz Harrosh 
45430b0c37bSBoaz Harrosh 		if (size != scsi_bufflen(SCpnt)) {
4553bf743e7SJeff Garzik 			scmd_printk(KERN_ERR, SCpnt,
4569ccfc756SJames Bottomley 				"mismatch count %d, bytes %d\n",
45730b0c37bSBoaz Harrosh 				size, scsi_bufflen(SCpnt));
45830b0c37bSBoaz Harrosh 			if (scsi_bufflen(SCpnt) > size)
45930b0c37bSBoaz Harrosh 				SCpnt->sdb.length = size;
4601da177e4SLinus Torvalds 		}
4611da177e4SLinus Torvalds 	}
4621da177e4SLinus Torvalds 
4631da177e4SLinus Torvalds 	/*
4641da177e4SLinus Torvalds 	 * request doesn't start on hw block boundary, add scatter pads
4651da177e4SLinus Torvalds 	 */
46683096ebfSTejun Heo 	if (((unsigned int)blk_rq_pos(rq) % (s_size >> 9)) ||
46730b0c37bSBoaz Harrosh 	    (scsi_bufflen(SCpnt) % s_size)) {
4683bf743e7SJeff Garzik 		scmd_printk(KERN_NOTICE, SCpnt, "unaligned transfer\n");
4697f9a6bc4SJames Bottomley 		goto out;
4701da177e4SLinus Torvalds 	}
4711da177e4SLinus Torvalds 
47230b0c37bSBoaz Harrosh 	this_count = (scsi_bufflen(SCpnt) >> 9) / (s_size >> 9);
4731da177e4SLinus Torvalds 
4741da177e4SLinus Torvalds 
47596eefad2SHannes Reinecke 	SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
47696eefad2SHannes Reinecke 					"%s %d/%u 512 byte blocks.\n",
4777f9a6bc4SJames Bottomley 					(rq_data_dir(rq) == WRITE) ?
4781da177e4SLinus Torvalds 					"writing" : "reading",
47983096ebfSTejun Heo 					this_count, blk_rq_sectors(rq)));
4801da177e4SLinus Torvalds 
4811da177e4SLinus Torvalds 	SCpnt->cmnd[1] = 0;
48283096ebfSTejun Heo 	block = (unsigned int)blk_rq_pos(rq) / (s_size >> 9);
4831da177e4SLinus Torvalds 
4841da177e4SLinus Torvalds 	if (this_count > 0xffff) {
4851da177e4SLinus Torvalds 		this_count = 0xffff;
48630b0c37bSBoaz Harrosh 		SCpnt->sdb.length = this_count * s_size;
4871da177e4SLinus Torvalds 	}
4881da177e4SLinus Torvalds 
489655da8e5SBart Van Assche 	put_unaligned_be32(block, &SCpnt->cmnd[2]);
4901da177e4SLinus Torvalds 	SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
491655da8e5SBart Van Assche 	put_unaligned_be16(this_count, &SCpnt->cmnd[7]);
4921da177e4SLinus Torvalds 
4931da177e4SLinus Torvalds 	/*
4941da177e4SLinus Torvalds 	 * We shouldn't disconnect in the middle of a sector, so with a dumb
4951da177e4SLinus Torvalds 	 * host adapter, it's safe to assume that we can at least transfer
4961da177e4SLinus Torvalds 	 * this many bytes between each connect / disconnect.
4971da177e4SLinus Torvalds 	 */
4981da177e4SLinus Torvalds 	SCpnt->transfersize = cd->device->sector_size;
4991da177e4SLinus Torvalds 	SCpnt->underflow = this_count << 9;
5001da177e4SLinus Torvalds 	SCpnt->allowed = MAX_RETRIES;
5019120ac54SChristoph Hellwig 	SCpnt->cmd_len = 10;
5021da177e4SLinus Torvalds 
5031da177e4SLinus Torvalds 	/*
5047007e9ddSChristoph Hellwig 	 * This indicates that the command is ready from our end to be queued.
5051da177e4SLinus Torvalds 	 */
5067007e9ddSChristoph Hellwig 	return BLK_STS_OK;
5077f9a6bc4SJames Bottomley  out:
5087007e9ddSChristoph Hellwig 	scsi_free_sgtables(SCpnt);
5097007e9ddSChristoph Hellwig 	return BLK_STS_IOERR;
5101da177e4SLinus Torvalds }
5111da177e4SLinus Torvalds 
51238a2b557SChristoph Hellwig static void sr_revalidate_disk(struct scsi_cd *cd)
51338a2b557SChristoph Hellwig {
51438a2b557SChristoph Hellwig 	struct scsi_sense_hdr sshdr;
51538a2b557SChristoph Hellwig 
51638a2b557SChristoph Hellwig 	/* if the unit is not ready, nothing more to do */
51738a2b557SChristoph Hellwig 	if (scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES, &sshdr))
51838a2b557SChristoph Hellwig 		return;
51938a2b557SChristoph Hellwig 	sr_cd_check(&cd->cdi);
52038a2b557SChristoph Hellwig 	get_sectorsize(cd);
52138a2b557SChristoph Hellwig }
52238a2b557SChristoph Hellwig 
52340cc51beSAl Viro static int sr_block_open(struct block_device *bdev, fmode_t mode)
5241da177e4SLinus Torvalds {
5256e9624b8SArnd Bergmann 	struct scsi_cd *cd;
5261214fd7bSBart Van Assche 	struct scsi_device *sdev;
52740cc51beSAl Viro 	int ret = -ENXIO;
5281da177e4SLinus Torvalds 
5291214fd7bSBart Van Assche 	cd = scsi_cd_get(bdev->bd_disk);
5301214fd7bSBart Van Assche 	if (!cd)
5311214fd7bSBart Van Assche 		goto out;
5321214fd7bSBart Van Assche 
5331214fd7bSBart Van Assche 	sdev = cd->device;
5341214fd7bSBart Van Assche 	scsi_autopm_get_device(sdev);
535afd35c4fSChristoph Hellwig 	if (bdev_check_media_change(bdev))
53638a2b557SChristoph Hellwig 		sr_revalidate_disk(cd);
5372bbea6e1SMaurizio Lombardi 
53851a85881SMerlijn Wajer 	mutex_lock(&cd->lock);
53940cc51beSAl Viro 	ret = cdrom_open(&cd->cdi, bdev, mode);
54051a85881SMerlijn Wajer 	mutex_unlock(&cd->lock);
5411214fd7bSBart Van Assche 
5421214fd7bSBart Van Assche 	scsi_autopm_put_device(sdev);
54340cc51beSAl Viro 	if (ret)
5441da177e4SLinus Torvalds 		scsi_cd_put(cd);
5451214fd7bSBart Van Assche 
5461214fd7bSBart Van Assche out:
5471da177e4SLinus Torvalds 	return ret;
5481da177e4SLinus Torvalds }
5491da177e4SLinus Torvalds 
550db2a144bSAl Viro static void sr_block_release(struct gendisk *disk, fmode_t mode)
5511da177e4SLinus Torvalds {
55240cc51beSAl Viro 	struct scsi_cd *cd = scsi_cd(disk);
55372655c0eSBart Van Assche 
55451a85881SMerlijn Wajer 	mutex_lock(&cd->lock);
55540cc51beSAl Viro 	cdrom_release(&cd->cdi, mode);
55651a85881SMerlijn Wajer 	mutex_unlock(&cd->lock);
55772655c0eSBart Van Assche 
55872655c0eSBart Van Assche 	scsi_cd_put(cd);
5591da177e4SLinus Torvalds }
5601da177e4SLinus Torvalds 
56140cc51beSAl Viro static int sr_block_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
5621da177e4SLinus Torvalds 			  unsigned long arg)
5631da177e4SLinus Torvalds {
564fb1ba406SChristoph Hellwig 	struct gendisk *disk = bdev->bd_disk;
565fb1ba406SChristoph Hellwig 	struct scsi_cd *cd = scsi_cd(disk);
5661da177e4SLinus Torvalds 	struct scsi_device *sdev = cd->device;
5676a2900b6SChristoph Hellwig 	void __user *argp = (void __user *)arg;
5686a2900b6SChristoph Hellwig 	int ret;
5691da177e4SLinus Torvalds 
5704f07bfc5SChristoph Hellwig 	if (bdev_is_partition(bdev) && !capable(CAP_SYS_RAWIO))
5714f07bfc5SChristoph Hellwig 		return -ENOIOCTLCMD;
5724f07bfc5SChristoph Hellwig 
57351a85881SMerlijn Wajer 	mutex_lock(&cd->lock);
5748a6cfeb6SArnd Bergmann 
575906d15fbSChristoph Hellwig 	ret = scsi_ioctl_block_when_processing_errors(sdev, cmd,
576906d15fbSChristoph Hellwig 			(mode & FMODE_NDELAY) != 0);
577906d15fbSChristoph Hellwig 	if (ret)
578906d15fbSChristoph Hellwig 		goto out;
579906d15fbSChristoph Hellwig 
5801214fd7bSBart Van Assche 	scsi_autopm_get_device(sdev);
5811214fd7bSBart Van Assche 
5822e27f576SChristoph Hellwig 	if (ret != CDROMCLOSETRAY && ret != CDROMEJECT) {
58340cc51beSAl Viro 		ret = cdrom_ioctl(&cd->cdi, bdev, mode, cmd, arg);
5846397256bSTejun Heo 		if (ret != -ENOSYS)
5851214fd7bSBart Van Assche 			goto put;
586558e3fbeSChristoph Hellwig 	}
5872e27f576SChristoph Hellwig 	ret = scsi_ioctl(sdev, disk, mode, cmd, argp);
5888a6cfeb6SArnd Bergmann 
5891214fd7bSBart Van Assche put:
5901214fd7bSBart Van Assche 	scsi_autopm_put_device(sdev);
5918a6cfeb6SArnd Bergmann out:
59251a85881SMerlijn Wajer 	mutex_unlock(&cd->lock);
5936a2900b6SChristoph Hellwig 	return ret;
5941da177e4SLinus Torvalds }
5951da177e4SLinus Torvalds 
59693aae17aSTejun Heo static unsigned int sr_block_check_events(struct gendisk *disk,
59793aae17aSTejun Heo 					  unsigned int clearing)
5981da177e4SLinus Torvalds {
5992d097c50SJens Axboe 	unsigned int ret = 0;
6002d097c50SJens Axboe 	struct scsi_cd *cd;
6016c7f1e2fSAaron Lu 
6022d097c50SJens Axboe 	cd = scsi_cd_get(disk);
6032d097c50SJens Axboe 	if (!cd)
6046627b38fSAaron Lu 		return 0;
6056c7f1e2fSAaron Lu 
6062d097c50SJens Axboe 	if (!atomic_read(&cd->device->disk_events_disable_depth))
6072d097c50SJens Axboe 		ret = cdrom_check_events(&cd->cdi, clearing);
6082d097c50SJens Axboe 
6092d097c50SJens Axboe 	scsi_cd_put(cd);
6102d097c50SJens Axboe 	return ret;
61193aae17aSTejun Heo }
61293aae17aSTejun Heo 
61383d5cde4SAlexey Dobriyan static const struct block_device_operations sr_bdops =
6141da177e4SLinus Torvalds {
6151da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
61640cc51beSAl Viro 	.open		= sr_block_open,
61740cc51beSAl Viro 	.release	= sr_block_release,
6188a6cfeb6SArnd Bergmann 	.ioctl		= sr_block_ioctl,
619558e3fbeSChristoph Hellwig 	.compat_ioctl	= blkdev_compat_ptr_ioctl,
62093aae17aSTejun Heo 	.check_events	= sr_block_check_events,
6211da177e4SLinus Torvalds };
6221da177e4SLinus Torvalds 
6231da177e4SLinus Torvalds static int sr_open(struct cdrom_device_info *cdi, int purpose)
6241da177e4SLinus Torvalds {
6251da177e4SLinus Torvalds 	struct scsi_cd *cd = cdi->handle;
6261da177e4SLinus Torvalds 	struct scsi_device *sdev = cd->device;
6271da177e4SLinus Torvalds 	int retval;
6281da177e4SLinus Torvalds 
6291da177e4SLinus Torvalds 	/*
6301da177e4SLinus Torvalds 	 * If the device is in error recovery, wait until it is done.
6311da177e4SLinus Torvalds 	 * If the device is offline, then disallow any access to it.
6321da177e4SLinus Torvalds 	 */
6331da177e4SLinus Torvalds 	retval = -ENXIO;
6341da177e4SLinus Torvalds 	if (!scsi_block_when_processing_errors(sdev))
6351da177e4SLinus Torvalds 		goto error_out;
6361da177e4SLinus Torvalds 
6371da177e4SLinus Torvalds 	return 0;
6381da177e4SLinus Torvalds 
6391da177e4SLinus Torvalds error_out:
6401da177e4SLinus Torvalds 	return retval;
6411da177e4SLinus Torvalds }
6421da177e4SLinus Torvalds 
6431da177e4SLinus Torvalds static void sr_release(struct cdrom_device_info *cdi)
6441da177e4SLinus Torvalds {
6451da177e4SLinus Torvalds }
6461da177e4SLinus Torvalds 
6471da177e4SLinus Torvalds static int sr_probe(struct device *dev)
6481da177e4SLinus Torvalds {
6491da177e4SLinus Torvalds 	struct scsi_device *sdev = to_scsi_device(dev);
6501da177e4SLinus Torvalds 	struct gendisk *disk;
6511da177e4SLinus Torvalds 	struct scsi_cd *cd;
6521da177e4SLinus Torvalds 	int minor, error;
6531da177e4SLinus Torvalds 
6546fe8c1dbSSubhash Jadavani 	scsi_autopm_get_device(sdev);
6551da177e4SLinus Torvalds 	error = -ENODEV;
6561da177e4SLinus Torvalds 	if (sdev->type != TYPE_ROM && sdev->type != TYPE_WORM)
6571da177e4SLinus Torvalds 		goto fail;
6581da177e4SLinus Torvalds 
6591da177e4SLinus Torvalds 	error = -ENOMEM;
66024669f75SJes Sorensen 	cd = kzalloc(sizeof(*cd), GFP_KERNEL);
6611da177e4SLinus Torvalds 	if (!cd)
6621da177e4SLinus Torvalds 		goto fail;
6631da177e4SLinus Torvalds 
6641da177e4SLinus Torvalds 	kref_init(&cd->kref);
6651da177e4SLinus Torvalds 
6664a1fa41dSChristoph Hellwig 	disk = __alloc_disk_node(sdev->request_queue, NUMA_NO_NODE,
6674a1fa41dSChristoph Hellwig 				 &sr_bio_compl_lkclass);
6681da177e4SLinus Torvalds 	if (!disk)
6691da177e4SLinus Torvalds 		goto fail_free;
67051a85881SMerlijn Wajer 	mutex_init(&cd->lock);
6711da177e4SLinus Torvalds 
6721da177e4SLinus Torvalds 	spin_lock(&sr_index_lock);
6731da177e4SLinus Torvalds 	minor = find_first_zero_bit(sr_index_bits, SR_DISKS);
6741da177e4SLinus Torvalds 	if (minor == SR_DISKS) {
6751da177e4SLinus Torvalds 		spin_unlock(&sr_index_lock);
6761da177e4SLinus Torvalds 		error = -EBUSY;
6771da177e4SLinus Torvalds 		goto fail_put;
6781da177e4SLinus Torvalds 	}
6791da177e4SLinus Torvalds 	__set_bit(minor, sr_index_bits);
6801da177e4SLinus Torvalds 	spin_unlock(&sr_index_lock);
6811da177e4SLinus Torvalds 
6821da177e4SLinus Torvalds 	disk->major = SCSI_CDROM_MAJOR;
6831da177e4SLinus Torvalds 	disk->first_minor = minor;
684a58bd768SChristoph Hellwig 	disk->minors = 1;
6851da177e4SLinus Torvalds 	sprintf(disk->disk_name, "sr%d", minor);
6861da177e4SLinus Torvalds 	disk->fops = &sr_bdops;
687a4561f9fSChristoph Hellwig 	disk->flags |= GENHD_FL_REMOVABLE | GENHD_FL_NO_PART;
68893aae17aSTejun Heo 	disk->events = DISK_EVENT_MEDIA_CHANGE | DISK_EVENT_EJECT_REQUEST;
6891545e0b4SChristoph Hellwig 	disk->event_flags = DISK_EVENT_FLAG_POLL | DISK_EVENT_FLAG_UEVENT |
6901545e0b4SChristoph Hellwig 				DISK_EVENT_FLAG_BLOCK_ON_EXCL_WRITE;
6911da177e4SLinus Torvalds 
692242f9dcbSJens Axboe 	blk_queue_rq_timeout(sdev->request_queue, SR_TIMEOUT);
693242f9dcbSJens Axboe 
6941da177e4SLinus Torvalds 	cd->device = sdev;
6951da177e4SLinus Torvalds 	cd->disk = disk;
6961da177e4SLinus Torvalds 	cd->driver = &sr_template;
6971da177e4SLinus Torvalds 	cd->capacity = 0x1fffff;
6981da177e4SLinus Torvalds 	cd->device->changed = 1;	/* force recheck CD type */
69993aae17aSTejun Heo 	cd->media_present = 1;
7001da177e4SLinus Torvalds 	cd->use = 1;
7011da177e4SLinus Torvalds 	cd->readcd_known = 0;
7021da177e4SLinus Torvalds 	cd->readcd_cdda = 0;
7031da177e4SLinus Torvalds 
7041da177e4SLinus Torvalds 	cd->cdi.ops = &sr_dops;
7051da177e4SLinus Torvalds 	cd->cdi.handle = cd;
7061da177e4SLinus Torvalds 	cd->cdi.mask = 0;
7071da177e4SLinus Torvalds 	cd->cdi.capacity = 1;
7081da177e4SLinus Torvalds 	sprintf(cd->cdi.name, "sr%d", minor);
7091da177e4SLinus Torvalds 
7101da177e4SLinus Torvalds 	sdev->sector_size = 2048;	/* A guess, just in case */
7111da177e4SLinus Torvalds 
7121da177e4SLinus Torvalds 	/* FIXME: need to handle a get_capabilities failure properly ?? */
7131da177e4SLinus Torvalds 	get_capabilities(cd);
7141da177e4SLinus Torvalds 	sr_vendor_init(cd);
7151da177e4SLinus Torvalds 
7161da177e4SLinus Torvalds 	set_capacity(disk, cd->capacity);
7171da177e4SLinus Torvalds 	disk->private_data = &cd->driver;
7181da177e4SLinus Torvalds 
719a711d91cSChristoph Hellwig 	if (register_cdrom(disk, &cd->cdi))
7206555781bSSimon Arlott 		goto fail_minor;
7211da177e4SLinus Torvalds 
7226627b38fSAaron Lu 	/*
7236627b38fSAaron Lu 	 * Initialize block layer runtime PM stuffs before the
7246627b38fSAaron Lu 	 * periodic event checking request gets started in add_disk.
7256627b38fSAaron Lu 	 */
7266627b38fSAaron Lu 	blk_pm_runtime_init(sdev->request_queue, dev);
7276627b38fSAaron Lu 
7281da177e4SLinus Torvalds 	dev_set_drvdata(dev, cd);
72938a2b557SChristoph Hellwig 	sr_revalidate_disk(cd);
730e9d658c2SLuis Chamberlain 
731e9d658c2SLuis Chamberlain 	error = device_add_disk(&sdev->sdev_gendev, disk, NULL);
732e9d658c2SLuis Chamberlain 	if (error) {
733e9d658c2SLuis Chamberlain 		kref_put(&cd->kref, sr_kref_release);
734e9d658c2SLuis Chamberlain 		goto fail;
735e9d658c2SLuis Chamberlain 	}
7361da177e4SLinus Torvalds 
7379ccfc756SJames Bottomley 	sdev_printk(KERN_DEBUG, sdev,
7389ccfc756SJames Bottomley 		    "Attached scsi CD-ROM %s\n", cd->cdi.name);
7396c7f1e2fSAaron Lu 	scsi_autopm_put_device(cd->device);
7406c7f1e2fSAaron Lu 
7411da177e4SLinus Torvalds 	return 0;
7421da177e4SLinus Torvalds 
7436555781bSSimon Arlott fail_minor:
7446555781bSSimon Arlott 	spin_lock(&sr_index_lock);
7456555781bSSimon Arlott 	clear_bit(minor, sr_index_bits);
7466555781bSSimon Arlott 	spin_unlock(&sr_index_lock);
7471da177e4SLinus Torvalds fail_put:
7481da177e4SLinus Torvalds 	put_disk(disk);
749a247e07fSSimon Arlott 	mutex_destroy(&cd->lock);
7501da177e4SLinus Torvalds fail_free:
7511da177e4SLinus Torvalds 	kfree(cd);
7521da177e4SLinus Torvalds fail:
7536fe8c1dbSSubhash Jadavani 	scsi_autopm_put_device(sdev);
7541da177e4SLinus Torvalds 	return error;
7551da177e4SLinus Torvalds }
7561da177e4SLinus Torvalds 
7571da177e4SLinus Torvalds 
7581da177e4SLinus Torvalds static void get_sectorsize(struct scsi_cd *cd)
7591da177e4SLinus Torvalds {
7601da177e4SLinus Torvalds 	unsigned char cmd[10];
76162858dacSFUJITA Tomonori 	unsigned char buffer[8];
7621da177e4SLinus Torvalds 	int the_result, retries = 3;
7631da177e4SLinus Torvalds 	int sector_size;
764165125e1SJens Axboe 	struct request_queue *queue;
7651da177e4SLinus Torvalds 
7661da177e4SLinus Torvalds 	do {
7671da177e4SLinus Torvalds 		cmd[0] = READ_CAPACITY;
7681da177e4SLinus Torvalds 		memset((void *) &cmd[1], 0, 9);
76962858dacSFUJITA Tomonori 		memset(buffer, 0, sizeof(buffer));
7701da177e4SLinus Torvalds 
7711da177e4SLinus Torvalds 		/* Do the command and wait.. */
772820732b5SJames Bottomley 		the_result = scsi_execute_req(cd->device, cmd, DMA_FROM_DEVICE,
77362858dacSFUJITA Tomonori 					      buffer, sizeof(buffer), NULL,
774f4f4e47eSFUJITA Tomonori 					      SR_TIMEOUT, MAX_RETRIES, NULL);
7751da177e4SLinus Torvalds 
7761da177e4SLinus Torvalds 		retries--;
7771da177e4SLinus Torvalds 
7781da177e4SLinus Torvalds 	} while (the_result && retries);
7791da177e4SLinus Torvalds 
7801da177e4SLinus Torvalds 
7811da177e4SLinus Torvalds 	if (the_result) {
7821da177e4SLinus Torvalds 		cd->capacity = 0x1fffff;
7831da177e4SLinus Torvalds 		sector_size = 2048;	/* A guess, just in case */
7841da177e4SLinus Torvalds 	} else {
7855915136dSTejun Heo 		long last_written;
7865915136dSTejun Heo 
787655da8e5SBart Van Assche 		cd->capacity = 1 + get_unaligned_be32(&buffer[0]);
7885915136dSTejun Heo 		/*
7895915136dSTejun Heo 		 * READ_CAPACITY doesn't return the correct size on
7905915136dSTejun Heo 		 * certain UDF media.  If last_written is larger, use
7915915136dSTejun Heo 		 * it instead.
7925915136dSTejun Heo 		 *
7935915136dSTejun Heo 		 * http://bugzilla.kernel.org/show_bug.cgi?id=9668
7945915136dSTejun Heo 		 */
7955915136dSTejun Heo 		if (!cdrom_get_last_written(&cd->cdi, &last_written))
7965915136dSTejun Heo 			cd->capacity = max_t(long, cd->capacity, last_written);
7975915136dSTejun Heo 
798655da8e5SBart Van Assche 		sector_size = get_unaligned_be32(&buffer[4]);
7991da177e4SLinus Torvalds 		switch (sector_size) {
8001da177e4SLinus Torvalds 			/*
8011da177e4SLinus Torvalds 			 * HP 4020i CD-Recorder reports 2340 byte sectors
8021da177e4SLinus Torvalds 			 * Philips CD-Writers report 2352 byte sectors
8031da177e4SLinus Torvalds 			 *
8041da177e4SLinus Torvalds 			 * Use 2k sectors for them..
8051da177e4SLinus Torvalds 			 */
8061da177e4SLinus Torvalds 		case 0:
8071da177e4SLinus Torvalds 		case 2340:
8081da177e4SLinus Torvalds 		case 2352:
8091da177e4SLinus Torvalds 			sector_size = 2048;
810df561f66SGustavo A. R. Silva 			fallthrough;
8111da177e4SLinus Torvalds 		case 2048:
8121da177e4SLinus Torvalds 			cd->capacity *= 4;
813df561f66SGustavo A. R. Silva 			fallthrough;
8141da177e4SLinus Torvalds 		case 512:
8151da177e4SLinus Torvalds 			break;
8161da177e4SLinus Torvalds 		default:
81796eefad2SHannes Reinecke 			sr_printk(KERN_INFO, cd,
81896eefad2SHannes Reinecke 				  "unsupported sector size %d.", sector_size);
8191da177e4SLinus Torvalds 			cd->capacity = 0;
8201da177e4SLinus Torvalds 		}
8211da177e4SLinus Torvalds 
8221da177e4SLinus Torvalds 		cd->device->sector_size = sector_size;
8231da177e4SLinus Torvalds 
8241da177e4SLinus Torvalds 		/*
8251da177e4SLinus Torvalds 		 * Add this so that we have the ability to correctly gauge
8261da177e4SLinus Torvalds 		 * what the device is capable of.
8271da177e4SLinus Torvalds 		 */
8281da177e4SLinus Torvalds 		set_capacity(cd->disk, cd->capacity);
8291da177e4SLinus Torvalds 	}
8301da177e4SLinus Torvalds 
8311da177e4SLinus Torvalds 	queue = cd->device->request_queue;
832e1defc4fSMartin K. Petersen 	blk_queue_logical_block_size(queue, sector_size);
8331da177e4SLinus Torvalds 
83462858dacSFUJITA Tomonori 	return;
8351da177e4SLinus Torvalds }
8361da177e4SLinus Torvalds 
8371da177e4SLinus Torvalds static void get_capabilities(struct scsi_cd *cd)
8381da177e4SLinus Torvalds {
8391da177e4SLinus Torvalds 	unsigned char *buffer;
8401da177e4SLinus Torvalds 	struct scsi_mode_data data;
841820732b5SJames Bottomley 	struct scsi_sense_hdr sshdr;
842a00a7862SMartin K. Petersen 	unsigned int ms_len = 128;
84338582a62SJames Bottomley 	int rc, n;
8441da177e4SLinus Torvalds 
8450ad78200SArjan van de Ven 	static const char *loadmech[] =
8461da177e4SLinus Torvalds 	{
8471da177e4SLinus Torvalds 		"caddy",
8481da177e4SLinus Torvalds 		"tray",
8491da177e4SLinus Torvalds 		"pop-up",
8501da177e4SLinus Torvalds 		"",
8511da177e4SLinus Torvalds 		"changer",
8521da177e4SLinus Torvalds 		"cartridge changer",
8531da177e4SLinus Torvalds 		"",
8541da177e4SLinus Torvalds 		""
8551da177e4SLinus Torvalds 	};
8561da177e4SLinus Torvalds 
8571da177e4SLinus Torvalds 
8581da177e4SLinus Torvalds 	/* allocate transfer buffer */
8591da177e4SLinus Torvalds 	buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
8601da177e4SLinus Torvalds 	if (!buffer) {
86196eefad2SHannes Reinecke 		sr_printk(KERN_ERR, cd, "out of memory.\n");
8621da177e4SLinus Torvalds 		return;
8631da177e4SLinus Torvalds 	}
8641da177e4SLinus Torvalds 
86538582a62SJames Bottomley 	/* eat unit attentions */
8669f8a2c23STejun Heo 	scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES, &sshdr);
8671da177e4SLinus Torvalds 
8681da177e4SLinus Torvalds 	/* ask for mode page 0x2a */
869a00a7862SMartin K. Petersen 	rc = scsi_mode_sense(cd->device, 0, 0x2a, buffer, ms_len,
8701cf72699SJames Bottomley 			     SR_TIMEOUT, 3, &data, NULL);
8711da177e4SLinus Torvalds 
8728793613dSHannes Reinecke 	if (rc < 0 || data.length > ms_len ||
873a00a7862SMartin K. Petersen 	    data.header_length + data.block_descriptor_length > data.length) {
8741da177e4SLinus Torvalds 		/* failed, drive doesn't have capabilities mode page */
8751da177e4SLinus Torvalds 		cd->cdi.speed = 1;
8761da177e4SLinus Torvalds 		cd->cdi.mask |= (CDC_CD_R | CDC_CD_RW | CDC_DVD_R |
8771da177e4SLinus Torvalds 				 CDC_DVD | CDC_DVD_RAM |
878bcc1e382SChuck Ebbert 				 CDC_SELECT_DISC | CDC_SELECT_SPEED |
879bcc1e382SChuck Ebbert 				 CDC_MRW | CDC_MRW_W | CDC_RAM);
8801da177e4SLinus Torvalds 		kfree(buffer);
88196eefad2SHannes Reinecke 		sr_printk(KERN_INFO, cd, "scsi-1 drive");
8821da177e4SLinus Torvalds 		return;
8831da177e4SLinus Torvalds 	}
8841da177e4SLinus Torvalds 
8851da177e4SLinus Torvalds 	n = data.header_length + data.block_descriptor_length;
886655da8e5SBart Van Assche 	cd->cdi.speed = get_unaligned_be16(&buffer[n + 8]) / 176;
8871da177e4SLinus Torvalds 	cd->readcd_known = 1;
8881da177e4SLinus Torvalds 	cd->readcd_cdda = buffer[n + 5] & 0x01;
8891da177e4SLinus Torvalds 	/* print some capability bits */
89096eefad2SHannes Reinecke 	sr_printk(KERN_INFO, cd,
89196eefad2SHannes Reinecke 		  "scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n",
892655da8e5SBart Van Assche 		  get_unaligned_be16(&buffer[n + 14]) / 176,
8931da177e4SLinus Torvalds 		  cd->cdi.speed,
8941da177e4SLinus Torvalds 		  buffer[n + 3] & 0x01 ? "writer " : "", /* CD Writer */
8951da177e4SLinus Torvalds 		  buffer[n + 3] & 0x20 ? "dvd-ram " : "",
8961da177e4SLinus Torvalds 		  buffer[n + 2] & 0x02 ? "cd/rw " : "", /* can read rewriteable */
8971da177e4SLinus Torvalds 		  buffer[n + 4] & 0x20 ? "xa/form2 " : "",	/* can read xa/from2 */
8981da177e4SLinus Torvalds 		  buffer[n + 5] & 0x01 ? "cdda " : "", /* can read audio data */
8991da177e4SLinus Torvalds 		  loadmech[buffer[n + 6] >> 5]);
9001da177e4SLinus Torvalds 	if ((buffer[n + 6] >> 5) == 0)
9011da177e4SLinus Torvalds 		/* caddy drives can't close tray... */
9021da177e4SLinus Torvalds 		cd->cdi.mask |= CDC_CLOSE_TRAY;
9031da177e4SLinus Torvalds 	if ((buffer[n + 2] & 0x8) == 0)
9041da177e4SLinus Torvalds 		/* not a DVD drive */
9051da177e4SLinus Torvalds 		cd->cdi.mask |= CDC_DVD;
9061da177e4SLinus Torvalds 	if ((buffer[n + 3] & 0x20) == 0)
9071da177e4SLinus Torvalds 		/* can't write DVD-RAM media */
9081da177e4SLinus Torvalds 		cd->cdi.mask |= CDC_DVD_RAM;
9091da177e4SLinus Torvalds 	if ((buffer[n + 3] & 0x10) == 0)
9101da177e4SLinus Torvalds 		/* can't write DVD-R media */
9111da177e4SLinus Torvalds 		cd->cdi.mask |= CDC_DVD_R;
9121da177e4SLinus Torvalds 	if ((buffer[n + 3] & 0x2) == 0)
9131da177e4SLinus Torvalds 		/* can't write CD-RW media */
9141da177e4SLinus Torvalds 		cd->cdi.mask |= CDC_CD_RW;
9151da177e4SLinus Torvalds 	if ((buffer[n + 3] & 0x1) == 0)
9161da177e4SLinus Torvalds 		/* can't write CD-R media */
9171da177e4SLinus Torvalds 		cd->cdi.mask |= CDC_CD_R;
9181da177e4SLinus Torvalds 	if ((buffer[n + 6] & 0x8) == 0)
9191da177e4SLinus Torvalds 		/* can't eject */
9201da177e4SLinus Torvalds 		cd->cdi.mask |= CDC_OPEN_TRAY;
9211da177e4SLinus Torvalds 
9221da177e4SLinus Torvalds 	if ((buffer[n + 6] >> 5) == mechtype_individual_changer ||
9231da177e4SLinus Torvalds 	    (buffer[n + 6] >> 5) == mechtype_cartridge_changer)
9241da177e4SLinus Torvalds 		cd->cdi.capacity =
9251da177e4SLinus Torvalds 		    cdrom_number_of_slots(&cd->cdi);
9261da177e4SLinus Torvalds 	if (cd->cdi.capacity <= 1)
9271da177e4SLinus Torvalds 		/* not a changer */
9281da177e4SLinus Torvalds 		cd->cdi.mask |= CDC_SELECT_DISC;
9291da177e4SLinus Torvalds 	/*else    I don't think it can close its tray
9301da177e4SLinus Torvalds 		cd->cdi.mask |= CDC_CLOSE_TRAY; */
9311da177e4SLinus Torvalds 
9321da177e4SLinus Torvalds 	/*
9331da177e4SLinus Torvalds 	 * if DVD-RAM, MRW-W or CD-RW, we are randomly writable
9341da177e4SLinus Torvalds 	 */
9351da177e4SLinus Torvalds 	if ((cd->cdi.mask & (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) !=
9361da177e4SLinus Torvalds 			(CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) {
937fd2eb903SChristoph Hellwig 		cd->writeable = 1;
9381da177e4SLinus Torvalds 	}
9391da177e4SLinus Torvalds 
9401da177e4SLinus Torvalds 	kfree(buffer);
9411da177e4SLinus Torvalds }
9421da177e4SLinus Torvalds 
9431da177e4SLinus Torvalds /*
9441da177e4SLinus Torvalds  * sr_packet() is the entry point for the generic commands generated
9451da177e4SLinus Torvalds  * by the Uniform CD-ROM layer.
9461da177e4SLinus Torvalds  */
9471da177e4SLinus Torvalds static int sr_packet(struct cdrom_device_info *cdi,
9481da177e4SLinus Torvalds 		struct packet_command *cgc)
9491da177e4SLinus Torvalds {
9508e04d805SHans de Goede 	struct scsi_cd *cd = cdi->handle;
9518e04d805SHans de Goede 	struct scsi_device *sdev = cd->device;
9528e04d805SHans de Goede 
9538e04d805SHans de Goede 	if (cgc->cmd[0] == GPCMD_READ_DISC_INFO && sdev->no_read_disc_info)
9548e04d805SHans de Goede 		return -EDRIVE_CANT_DO_THIS;
9558e04d805SHans de Goede 
9561da177e4SLinus Torvalds 	if (cgc->timeout <= 0)
9571da177e4SLinus Torvalds 		cgc->timeout = IOCTL_TIMEOUT;
9581da177e4SLinus Torvalds 
9598e04d805SHans de Goede 	sr_do_ioctl(cd, cgc);
9601da177e4SLinus Torvalds 
9611da177e4SLinus Torvalds 	return cgc->stat;
9621da177e4SLinus Torvalds }
9631da177e4SLinus Torvalds 
964ba51bdafSChristoph Hellwig static int sr_read_cdda_bpc(struct cdrom_device_info *cdi, void __user *ubuf,
965ba51bdafSChristoph Hellwig 		u32 lba, u32 nr, u8 *last_sense)
966ba51bdafSChristoph Hellwig {
967ba51bdafSChristoph Hellwig 	struct gendisk *disk = cdi->disk;
968ba51bdafSChristoph Hellwig 	u32 len = nr * CD_FRAMESIZE_RAW;
969ba51bdafSChristoph Hellwig 	struct scsi_request *req;
970ba51bdafSChristoph Hellwig 	struct request *rq;
971ba51bdafSChristoph Hellwig 	struct bio *bio;
972ba51bdafSChristoph Hellwig 	int ret;
973ba51bdafSChristoph Hellwig 
97468ec3b81SChristoph Hellwig 	rq = scsi_alloc_request(disk->queue, REQ_OP_DRV_IN, 0);
975ba51bdafSChristoph Hellwig 	if (IS_ERR(rq))
976ba51bdafSChristoph Hellwig 		return PTR_ERR(rq);
977ba51bdafSChristoph Hellwig 	req = scsi_req(rq);
978ba51bdafSChristoph Hellwig 
979ba51bdafSChristoph Hellwig 	ret = blk_rq_map_user(disk->queue, rq, NULL, ubuf, len, GFP_KERNEL);
980ba51bdafSChristoph Hellwig 	if (ret)
981ba51bdafSChristoph Hellwig 		goto out_put_request;
982ba51bdafSChristoph Hellwig 
983ba51bdafSChristoph Hellwig 	req->cmd[0] = GPCMD_READ_CD;
984ba51bdafSChristoph Hellwig 	req->cmd[1] = 1 << 2;
985ba51bdafSChristoph Hellwig 	req->cmd[2] = (lba >> 24) & 0xff;
986ba51bdafSChristoph Hellwig 	req->cmd[3] = (lba >> 16) & 0xff;
987ba51bdafSChristoph Hellwig 	req->cmd[4] = (lba >>  8) & 0xff;
988ba51bdafSChristoph Hellwig 	req->cmd[5] = lba & 0xff;
989ba51bdafSChristoph Hellwig 	req->cmd[6] = (nr >> 16) & 0xff;
990ba51bdafSChristoph Hellwig 	req->cmd[7] = (nr >>  8) & 0xff;
991ba51bdafSChristoph Hellwig 	req->cmd[8] = nr & 0xff;
992ba51bdafSChristoph Hellwig 	req->cmd[9] = 0xf8;
993ba51bdafSChristoph Hellwig 	req->cmd_len = 12;
994ba51bdafSChristoph Hellwig 	rq->timeout = 60 * HZ;
995ba51bdafSChristoph Hellwig 	bio = rq->bio;
996ba51bdafSChristoph Hellwig 
997*b84ba30bSChristoph Hellwig 	blk_execute_rq(rq, false);
998ba51bdafSChristoph Hellwig 	if (scsi_req(rq)->result) {
999ba51bdafSChristoph Hellwig 		struct scsi_sense_hdr sshdr;
1000ba51bdafSChristoph Hellwig 
1001ba51bdafSChristoph Hellwig 		scsi_normalize_sense(req->sense, req->sense_len,
1002ba51bdafSChristoph Hellwig 				     &sshdr);
1003ba51bdafSChristoph Hellwig 		*last_sense = sshdr.sense_key;
1004ba51bdafSChristoph Hellwig 		ret = -EIO;
1005ba51bdafSChristoph Hellwig 	}
1006ba51bdafSChristoph Hellwig 
1007ba51bdafSChristoph Hellwig 	if (blk_rq_unmap_user(bio))
1008ba51bdafSChristoph Hellwig 		ret = -EFAULT;
1009ba51bdafSChristoph Hellwig out_put_request:
10100bf6d96cSChristoph Hellwig 	blk_mq_free_request(rq);
1011ba51bdafSChristoph Hellwig 	return ret;
1012ba51bdafSChristoph Hellwig }
1013ba51bdafSChristoph Hellwig 
1014ba51bdafSChristoph Hellwig 
10151da177e4SLinus Torvalds /**
10161da177e4SLinus Torvalds  *	sr_kref_release - Called to free the scsi_cd structure
10171da177e4SLinus Torvalds  *	@kref: pointer to embedded kref
10181da177e4SLinus Torvalds  *
10190b950672SArjan van de Ven  *	sr_ref_mutex must be held entering this routine.  Because it is
10201da177e4SLinus Torvalds  *	called on last put, you should always use the scsi_cd_get()
10211da177e4SLinus Torvalds  *	scsi_cd_put() helpers which manipulate the semaphore directly
10221da177e4SLinus Torvalds  *	and never do a direct kref_put().
10231da177e4SLinus Torvalds  **/
10241da177e4SLinus Torvalds static void sr_kref_release(struct kref *kref)
10251da177e4SLinus Torvalds {
10261da177e4SLinus Torvalds 	struct scsi_cd *cd = container_of(kref, struct scsi_cd, kref);
10271da177e4SLinus Torvalds 	struct gendisk *disk = cd->disk;
10281da177e4SLinus Torvalds 
10291da177e4SLinus Torvalds 	spin_lock(&sr_index_lock);
1030f331c029STejun Heo 	clear_bit(MINOR(disk_devt(disk)), sr_index_bits);
10311da177e4SLinus Torvalds 	spin_unlock(&sr_index_lock);
10321da177e4SLinus Torvalds 
10331da177e4SLinus Torvalds 	unregister_cdrom(&cd->cdi);
10341da177e4SLinus Torvalds 
10351da177e4SLinus Torvalds 	disk->private_data = NULL;
10361da177e4SLinus Torvalds 
10371da177e4SLinus Torvalds 	put_disk(disk);
10381da177e4SLinus Torvalds 
103951a85881SMerlijn Wajer 	mutex_destroy(&cd->lock);
104051a85881SMerlijn Wajer 
10411da177e4SLinus Torvalds 	kfree(cd);
10421da177e4SLinus Torvalds }
10431da177e4SLinus Torvalds 
10441da177e4SLinus Torvalds static int sr_remove(struct device *dev)
10451da177e4SLinus Torvalds {
10461da177e4SLinus Torvalds 	struct scsi_cd *cd = dev_get_drvdata(dev);
10471da177e4SLinus Torvalds 
10486c7f1e2fSAaron Lu 	scsi_autopm_get_device(cd->device);
10496c7f1e2fSAaron Lu 
10501da177e4SLinus Torvalds 	del_gendisk(cd->disk);
105113b43891SAlan Stern 	dev_set_drvdata(dev, NULL);
10521da177e4SLinus Torvalds 
10530b950672SArjan van de Ven 	mutex_lock(&sr_ref_mutex);
10541da177e4SLinus Torvalds 	kref_put(&cd->kref, sr_kref_release);
10550b950672SArjan van de Ven 	mutex_unlock(&sr_ref_mutex);
10561da177e4SLinus Torvalds 
10571da177e4SLinus Torvalds 	return 0;
10581da177e4SLinus Torvalds }
10591da177e4SLinus Torvalds 
10601da177e4SLinus Torvalds static int __init init_sr(void)
10611da177e4SLinus Torvalds {
10621da177e4SLinus Torvalds 	int rc;
10631da177e4SLinus Torvalds 
10641da177e4SLinus Torvalds 	rc = register_blkdev(SCSI_CDROM_MAJOR, "sr");
10651da177e4SLinus Torvalds 	if (rc)
10661da177e4SLinus Torvalds 		return rc;
1067da3962feSAkinobu Mita 	rc = scsi_register_driver(&sr_template.gendrv);
1068da3962feSAkinobu Mita 	if (rc)
1069da3962feSAkinobu Mita 		unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
1070da3962feSAkinobu Mita 
1071da3962feSAkinobu Mita 	return rc;
10721da177e4SLinus Torvalds }
10731da177e4SLinus Torvalds 
10741da177e4SLinus Torvalds static void __exit exit_sr(void)
10751da177e4SLinus Torvalds {
10761da177e4SLinus Torvalds 	scsi_unregister_driver(&sr_template.gendrv);
10771da177e4SLinus Torvalds 	unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
10781da177e4SLinus Torvalds }
10791da177e4SLinus Torvalds 
10801da177e4SLinus Torvalds module_init(init_sr);
10811da177e4SLinus Torvalds module_exit(exit_sr);
10821da177e4SLinus Torvalds MODULE_LICENSE("GPL");
1083