sr.c 24.2 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
/*
 *  sr.c Copyright (C) 1992 David Giller
 *           Copyright (C) 1993, 1994, 1995, 1999 Eric Youngdale
 *
 *  adapted from:
 *      sd.c Copyright (C) 1992 Drew Eckhardt
 *      Linux scsi disk driver by
 *              Drew Eckhardt <drew@colorado.edu>
 *
 *	Modified by Eric Youngdale ericy@andante.org to
 *	add scatter-gather, multiple outstanding request, and other
 *	enhancements.
 *
 *      Modified by Eric Youngdale eric@andante.org to support loadable
 *      low-level scsi drivers.
 *
 *      Modified by Thomas Quinot thomas@melchior.cuivre.fdn.fr to
 *      provide auto-eject.
 *
 *      Modified by Gerd Knorr <kraxel@cs.tu-berlin.de> to support the
 *      generic cdrom interface
 *
 *      Modified by Jens Axboe <axboe@suse.de> - Uniform sr_packet()
 *      interface, capabilities probe additions, ioctl cleanups, etc.
 *
 *	Modified by Richard Gooch <rgooch@atnf.csiro.au> to support devfs
 *
 *	Modified by Jens Axboe <axboe@suse.de> - support DVD-RAM
 *	transparently and lose the GHOST hack
 *
 *	Modified by Arnaldo Carvalho de Melo <acme@conectiva.com.br>
 *	check resource allocation in sr_init and some cleanups
 */

#include <linux/module.h>
#include <linux/fs.h>
#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/bio.h>
#include <linux/string.h>
#include <linux/errno.h>
#include <linux/cdrom.h>
#include <linux/interrupt.h>
#include <linux/init.h>
#include <linux/blkdev.h>
46
#include <linux/mutex.h>
47
#include <linux/slab.h>
L
Linus Torvalds 已提交
48 49 50 51 52 53
#include <asm/uaccess.h>

#include <scsi/scsi.h>
#include <scsi/scsi_dbg.h>
#include <scsi/scsi_device.h>
#include <scsi/scsi_driver.h>
54
#include <scsi/scsi_cmnd.h>
L
Linus Torvalds 已提交
55 56 57 58 59 60 61 62
#include <scsi/scsi_eh.h>
#include <scsi/scsi_host.h>
#include <scsi/scsi_ioctl.h>	/* For the door lock/unlock commands */

#include "scsi_logging.h"
#include "sr.h"


63 64 65
MODULE_DESCRIPTION("SCSI cdrom (sr) driver");
MODULE_LICENSE("GPL");
MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_CDROM_MAJOR);
66 67
MODULE_ALIAS_SCSI_DEVICE(TYPE_ROM);
MODULE_ALIAS_SCSI_DEVICE(TYPE_WORM);
68

L
Linus Torvalds 已提交
69 70 71 72 73
#define SR_DISKS	256

#define SR_CAPABILITIES \
	(CDC_CLOSE_TRAY|CDC_OPEN_TRAY|CDC_LOCK|CDC_SELECT_SPEED| \
	 CDC_SELECT_DISC|CDC_MULTI_SESSION|CDC_MCN|CDC_MEDIA_CHANGED| \
74
	 CDC_PLAY_AUDIO|CDC_RESET|CDC_DRIVE_STATUS| \
L
Linus Torvalds 已提交
75 76 77
	 CDC_CD_R|CDC_CD_RW|CDC_DVD|CDC_DVD_R|CDC_DVD_RAM|CDC_GENERIC_PACKET| \
	 CDC_MRW|CDC_MRW_W|CDC_RAM)

78
static DEFINE_MUTEX(sr_mutex);
L
Linus Torvalds 已提交
79 80
static int sr_probe(struct device *);
static int sr_remove(struct device *);
81
static int sr_done(struct scsi_cmnd *);
L
Linus Torvalds 已提交
82 83 84 85 86 87 88 89

static struct scsi_driver sr_template = {
	.owner			= THIS_MODULE,
	.gendrv = {
		.name   	= "sr",
		.probe		= sr_probe,
		.remove		= sr_remove,
	},
90
	.done			= sr_done,
L
Linus Torvalds 已提交
91 92 93 94 95 96 97 98
};

static unsigned long sr_index_bits[SR_DISKS / BITS_PER_LONG];
static DEFINE_SPINLOCK(sr_index_lock);

/* This semaphore is used to mediate the 0->1 reference get in the
 * face of object destruction (i.e. we can't allow a get on an
 * object after last put) */
99
static DEFINE_MUTEX(sr_ref_mutex);
L
Linus Torvalds 已提交
100 101 102 103 104 105 106

static int sr_open(struct cdrom_device_info *, int);
static void sr_release(struct cdrom_device_info *);

static void get_sectorsize(struct scsi_cd *);
static void get_capabilities(struct scsi_cd *);

T
Tejun Heo 已提交
107 108
static unsigned int sr_check_events(struct cdrom_device_info *cdi,
				    unsigned int clearing, int slot);
L
Linus Torvalds 已提交
109 110 111 112 113 114
static int sr_packet(struct cdrom_device_info *, struct packet_command *);

static struct cdrom_device_ops sr_dops = {
	.open			= sr_open,
	.release	 	= sr_release,
	.drive_status	 	= sr_drive_status,
T
Tejun Heo 已提交
115
	.check_events		= sr_check_events,
L
Linus Torvalds 已提交
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
	.tray_move		= sr_tray_move,
	.lock_door		= sr_lock_door,
	.select_speed		= sr_select_speed,
	.get_last_session	= sr_get_last_session,
	.get_mcn		= sr_get_mcn,
	.reset			= sr_reset,
	.audio_ioctl		= sr_audio_ioctl,
	.capability		= SR_CAPABILITIES,
	.generic_packet		= sr_packet,
};

static void sr_kref_release(struct kref *kref);

static inline struct scsi_cd *scsi_cd(struct gendisk *disk)
{
	return container_of(disk->private_data, struct scsi_cd, driver);
}

/*
 * The get and put routines for the struct scsi_cd.  Note this entity
 * has a scsi_device pointer and owns a reference to this.
 */
static inline struct scsi_cd *scsi_cd_get(struct gendisk *disk)
{
	struct scsi_cd *cd = NULL;

142
	mutex_lock(&sr_ref_mutex);
L
Linus Torvalds 已提交
143 144 145 146 147 148 149 150 151 152 153 154
	if (disk->private_data == NULL)
		goto out;
	cd = scsi_cd(disk);
	kref_get(&cd->kref);
	if (scsi_device_get(cd->device))
		goto out_put;
	goto out;

 out_put:
	kref_put(&cd->kref, sr_kref_release);
	cd = NULL;
 out:
155
	mutex_unlock(&sr_ref_mutex);
L
Linus Torvalds 已提交
156 157 158
	return cd;
}

159
static void scsi_cd_put(struct scsi_cd *cd)
L
Linus Torvalds 已提交
160 161 162
{
	struct scsi_device *sdev = cd->device;

163
	mutex_lock(&sr_ref_mutex);
L
Linus Torvalds 已提交
164 165
	kref_put(&cd->kref, sr_kref_release);
	scsi_device_put(sdev);
166
	mutex_unlock(&sr_ref_mutex);
L
Linus Torvalds 已提交
167 168
}

T
Tejun Heo 已提交
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
static unsigned int sr_get_events(struct scsi_device *sdev)
{
	u8 buf[8];
	u8 cmd[] = { GET_EVENT_STATUS_NOTIFICATION,
		     1,			/* polled */
		     0, 0,		/* reserved */
		     1 << 4,		/* notification class: media */
		     0, 0,		/* reserved */
		     0, sizeof(buf),	/* allocation length */
		     0,			/* control */
	};
	struct event_header *eh = (void *)buf;
	struct media_event_desc *med = (void *)(buf + 4);
	struct scsi_sense_hdr sshdr;
	int result;

	result = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buf, sizeof(buf),
				  &sshdr, SR_TIMEOUT, MAX_RETRIES, NULL);
	if (scsi_sense_valid(&sshdr) && sshdr.sense_key == UNIT_ATTENTION)
		return DISK_EVENT_MEDIA_CHANGE;

	if (result || be16_to_cpu(eh->data_len) < sizeof(*med))
		return 0;

	if (eh->nea || eh->notification_class != 0x4)
		return 0;

	if (med->media_event_code == 1)
		return DISK_EVENT_EJECT_REQUEST;
	else if (med->media_event_code == 2)
		return DISK_EVENT_MEDIA_CHANGE;
	return 0;
}

L
Linus Torvalds 已提交
203
/*
T
Tejun Heo 已提交
204 205 206 207 208
 * This function checks to see if the media has been changed or eject
 * button has been pressed.  It is possible that we have already
 * sensed a change, or the drive may have sensed one and not yet
 * reported it.  The past events are accumulated in sdev->changed and
 * returned together with the current state.
L
Linus Torvalds 已提交
209
 */
T
Tejun Heo 已提交
210 211
static unsigned int sr_check_events(struct cdrom_device_info *cdi,
				    unsigned int clearing, int slot)
L
Linus Torvalds 已提交
212 213
{
	struct scsi_cd *cd = cdi->handle;
T
Tejun Heo 已提交
214 215 216 217
	bool last_present;
	struct scsi_sense_hdr sshdr;
	unsigned int events;
	int ret;
L
Linus Torvalds 已提交
218

T
Tejun Heo 已提交
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
	/* no changer support */
	if (CDSL_CURRENT != slot)
		return 0;

	events = sr_get_events(cd->device);
	/*
	 * GET_EVENT_STATUS_NOTIFICATION is enough unless MEDIA_CHANGE
	 * is being cleared.  Note that there are devices which hang
	 * if asked to execute TUR repeatedly.
	 */
	if (!(clearing & DISK_EVENT_MEDIA_CHANGE))
		goto skip_tur;

	/* let's see whether the media is there with TUR */
	last_present = cd->media_present;
	ret = scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES, &sshdr);
L
Linus Torvalds 已提交
235

236 237 238 239 240
	/*
	 * Media is considered to be present if TUR succeeds or fails with
	 * sense data indicating something other than media-not-present
	 * (ASC 0x3a).
	 */
T
Tejun Heo 已提交
241 242
	cd->media_present = scsi_status_is_good(ret) ||
		(scsi_sense_valid(&sshdr) && sshdr.asc != 0x3a);
L
Linus Torvalds 已提交
243

T
Tejun Heo 已提交
244 245 246 247 248 249
	if (last_present != cd->media_present)
		events |= DISK_EVENT_MEDIA_CHANGE;
skip_tur:
	if (cd->device->changed) {
		events |= DISK_EVENT_MEDIA_CHANGE;
		cd->device->changed = 0;
L
Linus Torvalds 已提交
250
	}
251

T
Tejun Heo 已提交
252
	return events;
L
Linus Torvalds 已提交
253
}
T
Tejun Heo 已提交
254

L
Linus Torvalds 已提交
255
/*
256
 * sr_done is the interrupt routine for the device driver.
L
Linus Torvalds 已提交
257
 *
258
 * It will be notified on the end of a SCSI read / write, and will take one
L
Linus Torvalds 已提交
259 260
 * of several actions based on success or failure.
 */
261
static int sr_done(struct scsi_cmnd *SCpnt)
L
Linus Torvalds 已提交
262 263
{
	int result = SCpnt->result;
B
Boaz Harrosh 已提交
264
	int this_count = scsi_bufflen(SCpnt);
L
Linus Torvalds 已提交
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
	int good_bytes = (result == 0 ? this_count : 0);
	int block_sectors = 0;
	long error_sector;
	struct scsi_cd *cd = scsi_cd(SCpnt->request->rq_disk);

#ifdef DEBUG
	printk("sr.c done: %x\n", result);
#endif

	/*
	 * Handle MEDIUM ERRORs or VOLUME OVERFLOWs that indicate partial
	 * success.  Since this is a relatively rare error condition, no
	 * care is taken to avoid unnecessary additional work such as
	 * memcpy's that could be avoided.
	 */
	if (driver_byte(result) != 0 &&		/* An error occurred */
	    (SCpnt->sense_buffer[0] & 0x7f) == 0x70) { /* Sense current */
		switch (SCpnt->sense_buffer[2]) {
		case MEDIUM_ERROR:
		case VOLUME_OVERFLOW:
		case ILLEGAL_REQUEST:
			if (!(SCpnt->sense_buffer[0] & 0x90))
				break;
			error_sector = (SCpnt->sense_buffer[3] << 24) |
				(SCpnt->sense_buffer[4] << 16) |
				(SCpnt->sense_buffer[5] << 8) |
				SCpnt->sense_buffer[6];
			if (SCpnt->request->bio != NULL)
				block_sectors =
					bio_sectors(SCpnt->request->bio);
			if (block_sectors < 4)
				block_sectors = 4;
			if (cd->device->sector_size == 2048)
				error_sector <<= 2;
			error_sector &= ~(block_sectors - 1);
300 301
			good_bytes = (error_sector -
				      blk_rq_pos(SCpnt->request)) << 9;
L
Linus Torvalds 已提交
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
			if (good_bytes < 0 || good_bytes >= this_count)
				good_bytes = 0;
			/*
			 * The SCSI specification allows for the value
			 * returned by READ CAPACITY to be up to 75 2K
			 * sectors past the last readable block.
			 * Therefore, if we hit a medium error within the
			 * last 75 2K sectors, we decrease the saved size
			 * value.
			 */
			if (error_sector < get_capacity(cd->disk) &&
			    cd->capacity - error_sector < 4 * 75)
				set_capacity(cd->disk, error_sector);
			break;

		case RECOVERED_ERROR:
			good_bytes = this_count;
			break;

		default:
			break;
		}
	}

326
	return good_bytes;
L
Linus Torvalds 已提交
327 328
}

329
static int sr_prep_fn(struct request_queue *q, struct request *rq)
L
Linus Torvalds 已提交
330
{
J
Jens Axboe 已提交
331
	int block = 0, this_count, s_size;
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
	struct scsi_cd *cd;
	struct scsi_cmnd *SCpnt;
	struct scsi_device *sdp = q->queuedata;
	int ret;

	if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
		ret = scsi_setup_blk_pc_cmnd(sdp, rq);
		goto out;
	} else if (rq->cmd_type != REQ_TYPE_FS) {
		ret = BLKPREP_KILL;
		goto out;
	}
	ret = scsi_setup_fs_cmnd(sdp, rq);
	if (ret != BLKPREP_OK)
		goto out;
	SCpnt = rq->special;
	cd = scsi_cd(rq->rq_disk);

	/* from here on until we're complete, any goto out
	 * is used for a killable error condition */
	ret = BLKPREP_KILL;
L
Linus Torvalds 已提交
353 354 355 356 357

	SCSI_LOG_HLQUEUE(1, printk("Doing sr request, dev = %s, block = %d\n",
				cd->disk->disk_name, block));

	if (!cd->device || !scsi_device_online(cd->device)) {
358 359
		SCSI_LOG_HLQUEUE(2, printk("Finishing %u sectors\n",
					   blk_rq_sectors(rq)));
L
Linus Torvalds 已提交
360
		SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt));
361
		goto out;
L
Linus Torvalds 已提交
362 363 364 365 366 367 368
	}

	if (cd->device->changed) {
		/*
		 * quietly refuse to do anything to a changed disc until the
		 * changed bit has been reset
		 */
369
		goto out;
L
Linus Torvalds 已提交
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
	}

	/*
	 * we do lazy blocksize switching (when reading XA sectors,
	 * see CDROMREADMODE2 ioctl) 
	 */
	s_size = cd->device->sector_size;
	if (s_size > 2048) {
		if (!in_interrupt())
			sr_set_blocklength(cd, 2048);
		else
			printk("sr: can't switch blocksize: in interrupt\n");
	}

	if (s_size != 512 && s_size != 1024 && s_size != 2048) {
385
		scmd_printk(KERN_ERR, SCpnt, "bad sector size %d\n", s_size);
386
		goto out;
L
Linus Torvalds 已提交
387 388
	}

389
	if (rq_data_dir(rq) == WRITE) {
L
Linus Torvalds 已提交
390
		if (!cd->device->writeable)
391
			goto out;
L
Linus Torvalds 已提交
392 393 394
		SCpnt->cmnd[0] = WRITE_10;
		SCpnt->sc_data_direction = DMA_TO_DEVICE;
 	 	cd->cdi.media_written = 1;
395
	} else if (rq_data_dir(rq) == READ) {
L
Linus Torvalds 已提交
396 397 398
		SCpnt->cmnd[0] = READ_10;
		SCpnt->sc_data_direction = DMA_FROM_DEVICE;
	} else {
399 400
		blk_dump_rq_flags(rq, "Unknown sr command");
		goto out;
L
Linus Torvalds 已提交
401 402 403
	}

	{
B
Boaz Harrosh 已提交
404 405
		struct scatterlist *sg;
		int i, size = 0, sg_count = scsi_sg_count(SCpnt);
L
Linus Torvalds 已提交
406

B
Boaz Harrosh 已提交
407 408 409 410
		scsi_for_each_sg(SCpnt, sg, sg_count, i)
			size += sg->length;

		if (size != scsi_bufflen(SCpnt)) {
411 412
			scmd_printk(KERN_ERR, SCpnt,
				"mismatch count %d, bytes %d\n",
B
Boaz Harrosh 已提交
413 414 415
				size, scsi_bufflen(SCpnt));
			if (scsi_bufflen(SCpnt) > size)
				SCpnt->sdb.length = size;
L
Linus Torvalds 已提交
416 417 418 419 420 421
		}
	}

	/*
	 * request doesn't start on hw block boundary, add scatter pads
	 */
422
	if (((unsigned int)blk_rq_pos(rq) % (s_size >> 9)) ||
B
Boaz Harrosh 已提交
423
	    (scsi_bufflen(SCpnt) % s_size)) {
424
		scmd_printk(KERN_NOTICE, SCpnt, "unaligned transfer\n");
425
		goto out;
L
Linus Torvalds 已提交
426 427
	}

B
Boaz Harrosh 已提交
428
	this_count = (scsi_bufflen(SCpnt) >> 9) / (s_size >> 9);
L
Linus Torvalds 已提交
429 430


431
	SCSI_LOG_HLQUEUE(2, printk("%s : %s %d/%u 512 byte blocks.\n",
L
Linus Torvalds 已提交
432
				cd->cdi.name,
433
				(rq_data_dir(rq) == WRITE) ?
L
Linus Torvalds 已提交
434
					"writing" : "reading",
435
				this_count, blk_rq_sectors(rq)));
L
Linus Torvalds 已提交
436 437

	SCpnt->cmnd[1] = 0;
438
	block = (unsigned int)blk_rq_pos(rq) / (s_size >> 9);
L
Linus Torvalds 已提交
439 440 441

	if (this_count > 0xffff) {
		this_count = 0xffff;
B
Boaz Harrosh 已提交
442
		SCpnt->sdb.length = this_count * s_size;
L
Linus Torvalds 已提交
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
	}

	SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
	SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
	SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
	SCpnt->cmnd[5] = (unsigned char) block & 0xff;
	SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
	SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
	SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;

	/*
	 * We shouldn't disconnect in the middle of a sector, so with a dumb
	 * host adapter, it's safe to assume that we can at least transfer
	 * this many bytes between each connect / disconnect.
	 */
	SCpnt->transfersize = cd->device->sector_size;
	SCpnt->underflow = this_count << 9;
	SCpnt->allowed = MAX_RETRIES;

	/*
	 * This indicates that the command is ready from our end to be
	 * queued.
	 */
466 467 468
	ret = BLKPREP_OK;
 out:
	return scsi_prep_return(q, rq, ret);
L
Linus Torvalds 已提交
469 470
}

A
Al Viro 已提交
471
static int sr_block_open(struct block_device *bdev, fmode_t mode)
L
Linus Torvalds 已提交
472
{
473
	struct scsi_cd *cd;
A
Al Viro 已提交
474
	int ret = -ENXIO;
L
Linus Torvalds 已提交
475

476
	mutex_lock(&sr_mutex);
477
	cd = scsi_cd_get(bdev->bd_disk);
A
Al Viro 已提交
478 479 480 481 482
	if (cd) {
		ret = cdrom_open(&cd->cdi, bdev, mode);
		if (ret)
			scsi_cd_put(cd);
	}
483
	mutex_unlock(&sr_mutex);
L
Linus Torvalds 已提交
484 485 486
	return ret;
}

A
Al Viro 已提交
487
static int sr_block_release(struct gendisk *disk, fmode_t mode)
L
Linus Torvalds 已提交
488
{
A
Al Viro 已提交
489
	struct scsi_cd *cd = scsi_cd(disk);
490
	mutex_lock(&sr_mutex);
A
Al Viro 已提交
491
	cdrom_release(&cd->cdi, mode);
L
Linus Torvalds 已提交
492
	scsi_cd_put(cd);
493
	mutex_unlock(&sr_mutex);
L
Linus Torvalds 已提交
494 495 496
	return 0;
}

A
Al Viro 已提交
497
static int sr_block_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
L
Linus Torvalds 已提交
498 499
			  unsigned long arg)
{
A
Al Viro 已提交
500
	struct scsi_cd *cd = scsi_cd(bdev->bd_disk);
L
Linus Torvalds 已提交
501
	struct scsi_device *sdev = cd->device;
502 503
	void __user *argp = (void __user *)arg;
	int ret;
L
Linus Torvalds 已提交
504

505
	mutex_lock(&sr_mutex);
506

507 508 509 510 511 512 513
	/*
	 * Send SCSI addressing ioctls directly to mid level, send other
	 * ioctls to cdrom/block level.
	 */
	switch (cmd) {
	case SCSI_IOCTL_GET_IDLUN:
	case SCSI_IOCTL_GET_BUS_NUMBER:
514 515
		ret = scsi_ioctl(sdev, cmd, argp);
		goto out;
L
Linus Torvalds 已提交
516
	}
517

A
Al Viro 已提交
518
	ret = cdrom_ioctl(&cd->cdi, bdev, mode, cmd, arg);
519
	if (ret != -ENOSYS)
520
		goto out;
521 522 523 524 525 526 527

	/*
	 * ENODEV means that we didn't recognise the ioctl, or that we
	 * cannot execute it in the current device state.  In either
	 * case fall through to scsi_ioctl, which will return ENDOEV again
	 * if it doesn't recognise the ioctl
	 */
528
	ret = scsi_nonblockable_ioctl(sdev, cmd, argp,
529
					(mode & FMODE_NDELAY) != 0);
530
	if (ret != -ENODEV)
531 532 533 534
		goto out;
	ret = scsi_ioctl(sdev, cmd, argp);

out:
535
	mutex_unlock(&sr_mutex);
536
	return ret;
L
Linus Torvalds 已提交
537 538
}

T
Tejun Heo 已提交
539 540
static unsigned int sr_block_check_events(struct gendisk *disk,
					  unsigned int clearing)
L
Linus Torvalds 已提交
541 542
{
	struct scsi_cd *cd = scsi_cd(disk);
T
Tejun Heo 已提交
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
	return cdrom_check_events(&cd->cdi, clearing);
}

static int sr_block_revalidate_disk(struct gendisk *disk)
{
	struct scsi_cd *cd = scsi_cd(disk);
	struct scsi_sense_hdr sshdr;

	/* if the unit is not ready, nothing more to do */
	if (scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES, &sshdr))
		return 0;

	sr_cd_check(&cd->cdi);
	get_sectorsize(cd);
	return 0;
L
Linus Torvalds 已提交
558 559
}

560
static const struct block_device_operations sr_bdops =
L
Linus Torvalds 已提交
561 562
{
	.owner		= THIS_MODULE,
A
Al Viro 已提交
563 564
	.open		= sr_block_open,
	.release	= sr_block_release,
565
	.ioctl		= sr_block_ioctl,
T
Tejun Heo 已提交
566 567
	.check_events	= sr_block_check_events,
	.revalidate_disk = sr_block_revalidate_disk,
L
Linus Torvalds 已提交
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
	/* 
	 * No compat_ioctl for now because sr_block_ioctl never
	 * seems to pass arbitary ioctls down to host drivers.
	 */
};

static int sr_open(struct cdrom_device_info *cdi, int purpose)
{
	struct scsi_cd *cd = cdi->handle;
	struct scsi_device *sdev = cd->device;
	int retval;

	/*
	 * If the device is in error recovery, wait until it is done.
	 * If the device is offline, then disallow any access to it.
	 */
	retval = -ENXIO;
	if (!scsi_block_when_processing_errors(sdev))
		goto error_out;

	return 0;

error_out:
	return retval;	
}

static void sr_release(struct cdrom_device_info *cdi)
{
	struct scsi_cd *cd = cdi->handle;

	if (cd->device->sector_size > 2048)
		sr_set_blocklength(cd, 2048);

}

static int sr_probe(struct device *dev)
{
	struct scsi_device *sdev = to_scsi_device(dev);
	struct gendisk *disk;
	struct scsi_cd *cd;
	int minor, error;

	error = -ENODEV;
	if (sdev->type != TYPE_ROM && sdev->type != TYPE_WORM)
		goto fail;

	error = -ENOMEM;
J
Jes Sorensen 已提交
615
	cd = kzalloc(sizeof(*cd), GFP_KERNEL);
L
Linus Torvalds 已提交
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
	if (!cd)
		goto fail;

	kref_init(&cd->kref);

	disk = alloc_disk(1);
	if (!disk)
		goto fail_free;

	spin_lock(&sr_index_lock);
	minor = find_first_zero_bit(sr_index_bits, SR_DISKS);
	if (minor == SR_DISKS) {
		spin_unlock(&sr_index_lock);
		error = -EBUSY;
		goto fail_put;
	}
	__set_bit(minor, sr_index_bits);
	spin_unlock(&sr_index_lock);

	disk->major = SCSI_CDROM_MAJOR;
	disk->first_minor = minor;
	sprintf(disk->disk_name, "sr%d", minor);
	disk->fops = &sr_bdops;
	disk->flags = GENHD_FL_CD;
T
Tejun Heo 已提交
640
	disk->events = DISK_EVENT_MEDIA_CHANGE | DISK_EVENT_EJECT_REQUEST;
L
Linus Torvalds 已提交
641

J
Jens Axboe 已提交
642 643
	blk_queue_rq_timeout(sdev->request_queue, SR_TIMEOUT);

L
Linus Torvalds 已提交
644 645 646 647 648 649
	cd->device = sdev;
	cd->disk = disk;
	cd->driver = &sr_template;
	cd->disk = disk;
	cd->capacity = 0x1fffff;
	cd->device->changed = 1;	/* force recheck CD type */
T
Tejun Heo 已提交
650
	cd->media_present = 1;
L
Linus Torvalds 已提交
651 652 653 654 655 656 657 658 659 660 661 662 663 664
	cd->use = 1;
	cd->readcd_known = 0;
	cd->readcd_cdda = 0;

	cd->cdi.ops = &sr_dops;
	cd->cdi.handle = cd;
	cd->cdi.mask = 0;
	cd->cdi.capacity = 1;
	sprintf(cd->cdi.name, "sr%d", minor);

	sdev->sector_size = 2048;	/* A guess, just in case */

	/* FIXME: need to handle a get_capabilities failure properly ?? */
	get_capabilities(cd);
665
	blk_queue_prep_rq(sdev->request_queue, sr_prep_fn);
L
Linus Torvalds 已提交
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
	sr_vendor_init(cd);

	disk->driverfs_dev = &sdev->sdev_gendev;
	set_capacity(disk, cd->capacity);
	disk->private_data = &cd->driver;
	disk->queue = sdev->request_queue;
	cd->cdi.disk = disk;

	if (register_cdrom(&cd->cdi))
		goto fail_put;

	dev_set_drvdata(dev, cd);
	disk->flags |= GENHD_FL_REMOVABLE;
	add_disk(disk);

681 682
	sdev_printk(KERN_DEBUG, sdev,
		    "Attached scsi CD-ROM %s\n", cd->cdi.name);
L
Linus Torvalds 已提交
683 684 685 686 687 688 689 690 691 692 693 694 695 696
	return 0;

fail_put:
	put_disk(disk);
fail_free:
	kfree(cd);
fail:
	return error;
}


static void get_sectorsize(struct scsi_cd *cd)
{
	unsigned char cmd[10];
697
	unsigned char buffer[8];
L
Linus Torvalds 已提交
698 699
	int the_result, retries = 3;
	int sector_size;
700
	struct request_queue *queue;
L
Linus Torvalds 已提交
701 702 703 704

	do {
		cmd[0] = READ_CAPACITY;
		memset((void *) &cmd[1], 0, 9);
705
		memset(buffer, 0, sizeof(buffer));
L
Linus Torvalds 已提交
706 707

		/* Do the command and wait.. */
708
		the_result = scsi_execute_req(cd->device, cmd, DMA_FROM_DEVICE,
709
					      buffer, sizeof(buffer), NULL,
710
					      SR_TIMEOUT, MAX_RETRIES, NULL);
L
Linus Torvalds 已提交
711 712 713 714 715 716 717 718 719 720

		retries--;

	} while (the_result && retries);


	if (the_result) {
		cd->capacity = 0x1fffff;
		sector_size = 2048;	/* A guess, just in case */
	} else {
721 722 723 724 725 726 727 728 729 730 731 732 733 734
		long last_written;

		cd->capacity = 1 + ((buffer[0] << 24) | (buffer[1] << 16) |
				    (buffer[2] << 8) | buffer[3]);
		/*
		 * READ_CAPACITY doesn't return the correct size on
		 * certain UDF media.  If last_written is larger, use
		 * it instead.
		 *
		 * http://bugzilla.kernel.org/show_bug.cgi?id=9668
		 */
		if (!cdrom_get_last_written(&cd->cdi, &last_written))
			cd->capacity = max_t(long, cd->capacity, last_written);

L
Linus Torvalds 已提交
735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
		sector_size = (buffer[4] << 24) |
		    (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
		switch (sector_size) {
			/*
			 * HP 4020i CD-Recorder reports 2340 byte sectors
			 * Philips CD-Writers report 2352 byte sectors
			 *
			 * Use 2k sectors for them..
			 */
		case 0:
		case 2340:
		case 2352:
			sector_size = 2048;
			/* fall through */
		case 2048:
			cd->capacity *= 4;
			/* fall through */
		case 512:
			break;
		default:
			printk("%s: unsupported sector size %d.\n",
			       cd->cdi.name, sector_size);
			cd->capacity = 0;
		}

		cd->device->sector_size = sector_size;

		/*
		 * Add this so that we have the ability to correctly gauge
		 * what the device is capable of.
		 */
		set_capacity(cd->disk, cd->capacity);
	}

	queue = cd->device->request_queue;
770
	blk_queue_logical_block_size(queue, sector_size);
L
Linus Torvalds 已提交
771

772
	return;
L
Linus Torvalds 已提交
773 774 775 776 777 778
}

static void get_capabilities(struct scsi_cd *cd)
{
	unsigned char *buffer;
	struct scsi_mode_data data;
779
	struct scsi_sense_hdr sshdr;
780
	int rc, n;
L
Linus Torvalds 已提交
781

782
	static const char *loadmech[] =
L
Linus Torvalds 已提交
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801
	{
		"caddy",
		"tray",
		"pop-up",
		"",
		"changer",
		"cartridge changer",
		"",
		""
	};


	/* allocate transfer buffer */
	buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
	if (!buffer) {
		printk(KERN_ERR "sr: out of memory.\n");
		return;
	}

802
	/* eat unit attentions */
803
	scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES, &sshdr);
L
Linus Torvalds 已提交
804 805 806

	/* ask for mode page 0x2a */
	rc = scsi_mode_sense(cd->device, 0, 0x2a, buffer, 128,
807
			     SR_TIMEOUT, 3, &data, NULL);
L
Linus Torvalds 已提交
808 809 810 811 812

	if (!scsi_status_is_good(rc)) {
		/* failed, drive doesn't have capabilities mode page */
		cd->cdi.speed = 1;
		cd->cdi.mask |= (CDC_CD_R | CDC_CD_RW | CDC_DVD_R |
813 814 815
				 CDC_DVD | CDC_DVD_RAM |
				 CDC_SELECT_DISC | CDC_SELECT_SPEED |
				 CDC_MRW | CDC_MRW_W | CDC_RAM);
L
Linus Torvalds 已提交
816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884
		kfree(buffer);
		printk("%s: scsi-1 drive\n", cd->cdi.name);
		return;
	}

	n = data.header_length + data.block_descriptor_length;
	cd->cdi.speed = ((buffer[n + 8] << 8) + buffer[n + 9]) / 176;
	cd->readcd_known = 1;
	cd->readcd_cdda = buffer[n + 5] & 0x01;
	/* print some capability bits */
	printk("%s: scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n", cd->cdi.name,
	       ((buffer[n + 14] << 8) + buffer[n + 15]) / 176,
	       cd->cdi.speed,
	       buffer[n + 3] & 0x01 ? "writer " : "", /* CD Writer */
	       buffer[n + 3] & 0x20 ? "dvd-ram " : "",
	       buffer[n + 2] & 0x02 ? "cd/rw " : "", /* can read rewriteable */
	       buffer[n + 4] & 0x20 ? "xa/form2 " : "",	/* can read xa/from2 */
	       buffer[n + 5] & 0x01 ? "cdda " : "", /* can read audio data */
	       loadmech[buffer[n + 6] >> 5]);
	if ((buffer[n + 6] >> 5) == 0)
		/* caddy drives can't close tray... */
		cd->cdi.mask |= CDC_CLOSE_TRAY;
	if ((buffer[n + 2] & 0x8) == 0)
		/* not a DVD drive */
		cd->cdi.mask |= CDC_DVD;
	if ((buffer[n + 3] & 0x20) == 0) 
		/* can't write DVD-RAM media */
		cd->cdi.mask |= CDC_DVD_RAM;
	if ((buffer[n + 3] & 0x10) == 0)
		/* can't write DVD-R media */
		cd->cdi.mask |= CDC_DVD_R;
	if ((buffer[n + 3] & 0x2) == 0)
		/* can't write CD-RW media */
		cd->cdi.mask |= CDC_CD_RW;
	if ((buffer[n + 3] & 0x1) == 0)
		/* can't write CD-R media */
		cd->cdi.mask |= CDC_CD_R;
	if ((buffer[n + 6] & 0x8) == 0)
		/* can't eject */
		cd->cdi.mask |= CDC_OPEN_TRAY;

	if ((buffer[n + 6] >> 5) == mechtype_individual_changer ||
	    (buffer[n + 6] >> 5) == mechtype_cartridge_changer)
		cd->cdi.capacity =
		    cdrom_number_of_slots(&cd->cdi);
	if (cd->cdi.capacity <= 1)
		/* not a changer */
		cd->cdi.mask |= CDC_SELECT_DISC;
	/*else    I don't think it can close its tray
		cd->cdi.mask |= CDC_CLOSE_TRAY; */

	/*
	 * if DVD-RAM, MRW-W or CD-RW, we are randomly writable
	 */
	if ((cd->cdi.mask & (CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) !=
			(CDC_DVD_RAM | CDC_MRW_W | CDC_RAM | CDC_CD_RW)) {
		cd->device->writeable = 1;
	}

	kfree(buffer);
}

/*
 * sr_packet() is the entry point for the generic commands generated
 * by the Uniform CD-ROM layer. 
 */
static int sr_packet(struct cdrom_device_info *cdi,
		struct packet_command *cgc)
{
885 886 887 888 889 890
	struct scsi_cd *cd = cdi->handle;
	struct scsi_device *sdev = cd->device;

	if (cgc->cmd[0] == GPCMD_READ_DISC_INFO && sdev->no_read_disc_info)
		return -EDRIVE_CANT_DO_THIS;

L
Linus Torvalds 已提交
891 892 893
	if (cgc->timeout <= 0)
		cgc->timeout = IOCTL_TIMEOUT;

894
	sr_do_ioctl(cd, cgc);
L
Linus Torvalds 已提交
895 896 897 898 899 900 901 902

	return cgc->stat;
}

/**
 *	sr_kref_release - Called to free the scsi_cd structure
 *	@kref: pointer to embedded kref
 *
903
 *	sr_ref_mutex must be held entering this routine.  Because it is
L
Linus Torvalds 已提交
904 905 906 907 908 909 910 911 912 913
 *	called on last put, you should always use the scsi_cd_get()
 *	scsi_cd_put() helpers which manipulate the semaphore directly
 *	and never do a direct kref_put().
 **/
static void sr_kref_release(struct kref *kref)
{
	struct scsi_cd *cd = container_of(kref, struct scsi_cd, kref);
	struct gendisk *disk = cd->disk;

	spin_lock(&sr_index_lock);
914
	clear_bit(MINOR(disk_devt(disk)), sr_index_bits);
L
Linus Torvalds 已提交
915 916 917 918 919 920 921 922 923 924 925 926 927 928 929
	spin_unlock(&sr_index_lock);

	unregister_cdrom(&cd->cdi);

	disk->private_data = NULL;

	put_disk(disk);

	kfree(cd);
}

static int sr_remove(struct device *dev)
{
	struct scsi_cd *cd = dev_get_drvdata(dev);

930
	blk_queue_prep_rq(cd->device->request_queue, scsi_prep_fn);
L
Linus Torvalds 已提交
931 932
	del_gendisk(cd->disk);

933
	mutex_lock(&sr_ref_mutex);
L
Linus Torvalds 已提交
934
	kref_put(&cd->kref, sr_kref_release);
935
	mutex_unlock(&sr_ref_mutex);
L
Linus Torvalds 已提交
936 937 938 939 940 941 942 943 944 945 946

	return 0;
}

static int __init init_sr(void)
{
	int rc;

	rc = register_blkdev(SCSI_CDROM_MAJOR, "sr");
	if (rc)
		return rc;
947 948 949 950 951
	rc = scsi_register_driver(&sr_template.gendrv);
	if (rc)
		unregister_blkdev(SCSI_CDROM_MAJOR, "sr");

	return rc;
L
Linus Torvalds 已提交
952 953 954 955 956 957 958 959 960 961 962
}

static void __exit exit_sr(void)
{
	scsi_unregister_driver(&sr_template.gendrv);
	unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
}

module_init(init_sr);
module_exit(exit_sr);
MODULE_LICENSE("GPL");