sr.c 22.7 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7 8 9
/*
 *  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>
 *
10 11 12
 *	Modified by Eric Youngdale ericy@andante.org to
 *	add scatter-gather, multiple outstanding request, and other
 *	enhancements.
Linus Torvalds's avatar
Linus Torvalds committed
13
 *
14 15
 *      Modified by Eric Youngdale eric@andante.org to support loadable
 *      low-level scsi drivers.
Linus Torvalds's avatar
Linus Torvalds committed
16
 *
17 18
 *      Modified by Thomas Quinot thomas@melchior.cuivre.fdn.fr to
 *      provide auto-eject.
Linus Torvalds's avatar
Linus Torvalds committed
19
 *
20 21
 *      Modified by Gerd Knorr <kraxel@cs.tu-berlin.de> to support the
 *      generic cdrom interface
Linus Torvalds's avatar
Linus Torvalds committed
22
 *
23 24
 *      Modified by Jens Axboe <axboe@suse.de> - Uniform sr_packet()
 *      interface, capabilities probe additions, ioctl cleanups, etc.
Linus Torvalds's avatar
Linus Torvalds committed
25
 *
26
 *	Modified by Richard Gooch <rgooch@atnf.csiro.au> to support devfs
Linus Torvalds's avatar
Linus Torvalds committed
27
 *
28
 *	Modified by Jens Axboe <axboe@suse.de> - support DVD-RAM
29
 *	transparently and lose the GHOST hack
Linus Torvalds's avatar
Linus Torvalds committed
30
 *
31 32
 *	Modified by Arnaldo Carvalho de Melo <acme@conectiva.com.br>
 *	check resource allocation in sr_init and some cleanups
Linus Torvalds's avatar
Linus Torvalds committed
33 34
 */

35
#include <linux/module.h>
Linus Torvalds's avatar
Linus Torvalds committed
36 37 38 39
#include <linux/fs.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/mm.h>
40
#include <linux/bio.h>
Linus Torvalds's avatar
Linus Torvalds committed
41 42 43 44 45
#include <linux/string.h>
#include <linux/errno.h>
#include <linux/cdrom.h>
#include <linux/interrupt.h>
#include <linux/init.h>
46
#include <linux/blkdev.h>
Linus Torvalds's avatar
Linus Torvalds committed
47 48 49 50
#include <asm/uaccess.h>

#include "scsi.h"
#include "hosts.h"
51 52

#include <scsi/scsi_driver.h>
Linus Torvalds's avatar
Linus Torvalds committed
53 54
#include <scsi/scsi_ioctl.h>	/* For the door lock/unlock commands */

55
#include "scsi_logging.h"
56 57 58
#include "sr.h"


Linus Torvalds's avatar
Linus Torvalds committed
59 60
MODULE_PARM(xa_test, "i");	/* see sr_ioctl.c */

61

62
#define SR_DISKS	256
63

Linus Torvalds's avatar
Linus Torvalds committed
64 65
#define MAX_RETRIES	3
#define SR_TIMEOUT	(30 * HZ)
66 67 68 69
#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| \
	 CDC_PLAY_AUDIO|CDC_RESET|CDC_IOCTLS|CDC_DRIVE_STATUS| \
Andrew Morton's avatar
Andrew Morton committed
70 71
	 CDC_CD_R|CDC_CD_RW|CDC_DVD|CDC_DVD_R|CDC_DVD_RAM|CDC_GENERIC_PACKET| \
	 CDC_MRW|CDC_MRW_W|CDC_RAM)
Linus Torvalds's avatar
Linus Torvalds committed
72

73 74
static int sr_probe(struct device *);
static int sr_remove(struct device *);
75
static int sr_init_command(struct scsi_cmnd *);
Linus Torvalds's avatar
Linus Torvalds committed
76

77 78 79 80 81 82
static struct scsi_driver sr_template = {
	.owner			= THIS_MODULE,
	.gendrv = {
		.name   	= "sr",
		.probe		= sr_probe,
		.remove		= sr_remove,
Mike Anderson's avatar
Mike Anderson committed
83
	},
84
	.init_command		= sr_init_command,
Linus Torvalds's avatar
Linus Torvalds committed
85 86
};

87 88 89
static unsigned long sr_index_bits[SR_DISKS / BITS_PER_LONG];
static spinlock_t sr_index_lock = SPIN_LOCK_UNLOCKED;

Linus Torvalds's avatar
Linus Torvalds committed
90
static int sr_open(struct cdrom_device_info *, int);
91 92
static void sr_release(struct cdrom_device_info *);

93 94
static void get_sectorsize(struct scsi_cd *);
static void get_capabilities(struct scsi_cd *);
Linus Torvalds's avatar
Linus Torvalds committed
95 96 97 98

static int sr_media_change(struct cdrom_device_info *, int);
static int sr_packet(struct cdrom_device_info *, struct cdrom_generic_command *);

99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
static struct cdrom_device_ops sr_dops = {
	.open			= sr_open,
	.release	 	= sr_release,
	.drive_status	 	= sr_drive_status,
	.media_changed		= sr_media_change,
	.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,
	.dev_ioctl		= sr_dev_ioctl,
	.capability		= SR_CAPABILITIES,
	.generic_packet		= sr_packet,
Linus Torvalds's avatar
Linus Torvalds committed
114 115 116 117 118 119 120 121 122 123 124 125 126 127
};

/*
 * This function checks to see if the media has been changed in the
 * CDROM drive.  It is possible that we have already sensed a change,
 * or the drive may have sensed one and not yet reported it.  We must
 * be ready for either case. This function always reports the current
 * value of the changed bit.  If flag is 0, then the changed bit is reset.
 * This function could be done as an ioctl, but we would need to have
 * an inode for that to work, and we do not always have one.
 */

int sr_media_change(struct cdrom_device_info *cdi, int slot)
{
128
	struct scsi_cd *cd = cdi->handle;
Linus Torvalds's avatar
Linus Torvalds committed
129 130 131 132 133 134 135
	int retval;

	if (CDSL_CURRENT != slot) {
		/* no changer support */
		return -EINVAL;
	}

136
	retval = scsi_ioctl(cd->device, SCSI_IOCTL_TEST_UNIT_READY, 0);
Linus Torvalds's avatar
Linus Torvalds committed
137 138 139 140 141
	if (retval) {
		/* Unable to test, unit probably not ready.  This usually
		 * means there is no disc in the drive.  Mark as changed,
		 * and we will figure it out later once the drive is
		 * available again.  */
142
		cd->device->changed = 1;
Linus Torvalds's avatar
Linus Torvalds committed
143 144 145 146
		return 1;	/* This will force a flush, if called from
				 * check_disk_change */
	};

147 148
	retval = cd->device->changed;
	cd->device->changed = 0;
Linus Torvalds's avatar
Linus Torvalds committed
149 150 151 152 153 154 155 156 157 158 159 160 161
	/* If the disk changed, the capacity will now be different,
	 * so we force a re-read of this information */
	if (retval) {
		/* check multisession offset etc */
		sr_cd_check(cdi);

		/* 
		 * If the disk changed, the capacity will now be different,
		 * so we force a re-read of this information 
		 * Force 2048 for the sector size so that filesystems won't
		 * be trying to use something that is too small if the disc
		 * has changed.
		 */
162 163
		cd->needs_sector_size = 1;
		cd->device->sector_size = 2048;
Linus Torvalds's avatar
Linus Torvalds committed
164 165 166
	}
	return retval;
}
Alexander Viro's avatar
Alexander Viro committed
167 168 169 170 171
 
static inline struct scsi_cd *scsi_cd(struct gendisk *disk)
{
	return container_of(disk->private_data, struct scsi_cd, driver);
}
Linus Torvalds's avatar
Linus Torvalds committed
172 173

/*
174 175 176 177
 * rw_intr is the interrupt routine for the device driver.
 *
 * It will be notified on the end of a SCSI read / write, and will take on
 * of several actions based on success or failure.
Linus Torvalds's avatar
Linus Torvalds committed
178
 */
179
static void rw_intr(struct scsi_cmnd * SCpnt)
Linus Torvalds's avatar
Linus Torvalds committed
180 181 182 183 184
{
	int result = SCpnt->result;
	int this_count = SCpnt->bufflen >> 9;
	int good_sectors = (result == 0 ? this_count : 0);
	int block_sectors = 0;
185
	long error_sector;
Alexander Viro's avatar
Alexander Viro committed
186
	struct scsi_cd *cd = scsi_cd(SCpnt->request->rq_disk);
Linus Torvalds's avatar
Linus Torvalds committed
187 188

#ifdef DEBUG
189
	printk("sr.c done: %x %p\n", result, SCpnt->request->bh->b_data);
Linus Torvalds's avatar
Linus Torvalds committed
190
#endif
191

Linus Torvalds's avatar
Linus Torvalds committed
192
	/*
193 194 195 196
	 * 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.
Linus Torvalds's avatar
Linus Torvalds committed
197 198
	 */
	if (driver_byte(result) != 0 &&		/* An error occurred */
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
	    (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);
			good_sectors = error_sector - SCpnt->request->sector;
			if (good_sectors < 0 || good_sectors >= this_count)
				good_sectors = 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:

			/*
			 * An error occured, but it recovered.  Inform the
			 * user, but make sure that it's not treated as a
			 * hard error.
			 */
			print_sense("sr", SCpnt);
			SCpnt->result = 0;
			SCpnt->sense_buffer[0] = 0x0;
			good_sectors = this_count;
			break;

		default:
			break;
		}
Linus Torvalds's avatar
Linus Torvalds committed
250 251 252 253 254 255 256 257 258 259
	}

	/*
	 * This calls the generic completion function, now that we know
	 * how many actual sectors finished, and how many sectors we need
	 * to say have failed.
	 */
	scsi_io_completion(SCpnt, good_sectors, block_sectors);
}

260
static int sr_init_command(struct scsi_cmnd * SCpnt)
Linus Torvalds's avatar
Linus Torvalds committed
261
{
Linus Torvalds's avatar
Linus Torvalds committed
262
	int block=0, this_count, s_size, timeout = SR_TIMEOUT;
Alexander Viro's avatar
Alexander Viro committed
263
	struct scsi_cd *cd = scsi_cd(SCpnt->request->rq_disk);
Linus Torvalds's avatar
Linus Torvalds committed
264

265 266
	SCSI_LOG_HLQUEUE(1, printk("Doing sr request, dev = %s, block = %d\n",
				cd->disk->disk_name, block));
Linus Torvalds's avatar
Linus Torvalds committed
267

Alexander Viro's avatar
Alexander Viro committed
268
	if (!cd->device || !cd->device->online) {
269 270
		SCSI_LOG_HLQUEUE(2, printk("Finishing %ld sectors\n",
					SCpnt->request->nr_sectors));
Linus Torvalds's avatar
Linus Torvalds committed
271 272 273
		SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt));
		return 0;
	}
274

275
	if (cd->device->changed) {
Linus Torvalds's avatar
Linus Torvalds committed
276 277 278 279 280 281 282
		/*
		 * quietly refuse to do anything to a changed disc until the
		 * changed bit has been reset
		 */
		return 0;
	}

283 284 285 286 287 288 289 290 291 292
	/*
	 * these are already setup, just copy cdb basically
	 */
	if (SCpnt->request->flags & REQ_BLOCK_PC) {
		struct request *rq = SCpnt->request;

		if (sizeof(rq->cmd) > sizeof(SCpnt->cmnd))
			return 0;

		memcpy(SCpnt->cmnd, rq->cmd, sizeof(SCpnt->cmnd));
293 294 295
		if (!rq->data_len)
			SCpnt->sc_data_direction = SCSI_DATA_NONE;
		else if (rq_data_dir(rq) == WRITE)
296 297
			SCpnt->sc_data_direction = SCSI_DATA_WRITE;
		else
298
			SCpnt->sc_data_direction = SCSI_DATA_READ;
299 300 301 302 303

		this_count = rq->data_len;
		if (rq->timeout)
			timeout = rq->timeout;

Jens Axboe's avatar
Jens Axboe committed
304
		SCpnt->transfersize = rq->data_len;
305 306 307
		goto queue;
	}

308 309
	if (!(SCpnt->request->flags & REQ_CMD)) {
		blk_dump_rq_flags(SCpnt->request, "sr unsup command");
Linus Torvalds's avatar
Linus Torvalds committed
310 311 312
		return 0;
	}

Linus Torvalds's avatar
Linus Torvalds committed
313 314 315 316
	/*
	 * we do lazy blocksize switching (when reading XA sectors,
	 * see CDROMREADMODE2 ioctl) 
	 */
317
	s_size = cd->device->sector_size;
Linus Torvalds's avatar
Linus Torvalds committed
318 319
	if (s_size > 2048) {
		if (!in_interrupt())
320
			sr_set_blocklength(cd, 2048);
Linus Torvalds's avatar
Linus Torvalds committed
321 322 323 324 325 326 327 328 329
		else
			printk("sr: can't switch blocksize: in interrupt\n");
	}

	if (s_size != 512 && s_size != 1024 && s_size != 2048) {
		printk("sr: bad sector size %d\n", s_size);
		return 0;
	}

330
	if (rq_data_dir(SCpnt->request) == WRITE) {
331
		if (!cd->device->writeable)
Linus Torvalds's avatar
Linus Torvalds committed
332 333 334
			return 0;
		SCpnt->cmnd[0] = WRITE_10;
		SCpnt->sc_data_direction = SCSI_DATA_WRITE;
335
	} else if (rq_data_dir(SCpnt->request) == READ) {
Linus Torvalds's avatar
Linus Torvalds committed
336 337 338
		SCpnt->cmnd[0] = READ_10;
		SCpnt->sc_data_direction = SCSI_DATA_READ;
	} else {
339
		blk_dump_rq_flags(SCpnt->request, "Unknown sr command");
Linus Torvalds's avatar
Linus Torvalds committed
340 341
		return 0;
	}
Linus Torvalds's avatar
Linus Torvalds committed
342

343 344 345 346 347 348 349 350 351 352 353 354 355 356
	{
		struct scatterlist *sg = SCpnt->request_buffer;
		int i, size = 0;
		for (i = 0; i < SCpnt->use_sg; i++)
			size += sg[i].length;

		if (size != SCpnt->request_bufflen && SCpnt->use_sg) {
			printk(KERN_ERR "sr: mismatch count %d, bytes %d\n",
					size, SCpnt->request_bufflen);
			if (SCpnt->request_bufflen > size)
				SCpnt->request_bufflen = SCpnt->bufflen = size;
		}
	}

Linus Torvalds's avatar
Linus Torvalds committed
357 358 359
	/*
	 * request doesn't start on hw block boundary, add scatter pads
	 */
360 361
	if (((unsigned int)SCpnt->request->sector % (s_size >> 9)) ||
	    (SCpnt->request_bufflen % s_size)) {
Linus Torvalds's avatar
Linus Torvalds committed
362 363 364
		printk("sr: unaligned transfer\n");
		return 0;
	}
Linus Torvalds's avatar
Linus Torvalds committed
365 366 367 368

	this_count = (SCpnt->request_bufflen >> 9) / (s_size >> 9);


369
	SCSI_LOG_HLQUEUE(2, printk("%s : %s %d/%ld 512 byte blocks.\n",
370 371 372 373
				cd->cdi.name,
				(rq_data_dir(SCpnt->request) == WRITE) ?
					"writing" : "reading",
				this_count, SCpnt->request->nr_sectors));
Linus Torvalds's avatar
Linus Torvalds committed
374

375
	SCpnt->cmnd[1] = 0;
376
	block = (unsigned int)SCpnt->request->sector / (s_size >> 9);
Linus Torvalds's avatar
Linus Torvalds committed
377

378
	if (this_count > 0xffff) {
Linus Torvalds's avatar
Linus Torvalds committed
379
		this_count = 0xffff;
380 381 382
		SCpnt->request_bufflen = SCpnt->bufflen =
				this_count * s_size;
	}
Linus Torvalds's avatar
Linus Torvalds committed
383 384 385 386 387 388 389 390 391 392 393 394 395 396

	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.
	 */
397
	SCpnt->transfersize = cd->device->sector_size;
Linus Torvalds's avatar
Linus Torvalds committed
398 399
	SCpnt->underflow = this_count << 9;

Jens Axboe's avatar
Jens Axboe committed
400
queue:
Linus Torvalds's avatar
Linus Torvalds committed
401
	SCpnt->allowed = MAX_RETRIES;
402
	SCpnt->timeout_per_command = timeout;
Linus Torvalds's avatar
Linus Torvalds committed
403 404 405 406 407 408 409 410 411 412 413 414 415 416

	/*
	 * This is the completion routine we use.  This is matched in terms
	 * of capability to this function.
	 */
	SCpnt->done = rw_intr;

	/*
	 * This indicates that the command is ready from our end to be
	 * queued.
	 */
	return 1;
}

Alexander Viro's avatar
Alexander Viro committed
417 418
static int sr_block_open(struct inode *inode, struct file *file)
{
Alexander Viro's avatar
Alexander Viro committed
419
	struct scsi_cd *cd = scsi_cd(inode->i_bdev->bd_disk);
Alexander Viro's avatar
Alexander Viro committed
420 421 422 423 424
	return cdrom_open(&cd->cdi, inode, file);
}

static int sr_block_release(struct inode *inode, struct file *file)
{
Alexander Viro's avatar
Alexander Viro committed
425
	struct scsi_cd *cd = scsi_cd(inode->i_bdev->bd_disk);
Alexander Viro's avatar
Alexander Viro committed
426 427 428 429 430 431
	return cdrom_release(&cd->cdi, file);
}

static int sr_block_ioctl(struct inode *inode, struct file *file, unsigned cmd,
			  unsigned long arg)
{
Alexander Viro's avatar
Alexander Viro committed
432
	struct scsi_cd *cd = scsi_cd(inode->i_bdev->bd_disk);
433 434 435 436 437 438 439 440 441 442 443
	struct scsi_device *sdev = cd->device;

        /*
         * 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:
                        return scsi_ioctl(sdev, cmd, (void *)arg);
	}
Alexander Viro's avatar
Alexander Viro committed
444 445 446 447 448
	return cdrom_ioctl(&cd->cdi, inode, cmd, arg);
}

static int sr_block_media_changed(struct gendisk *disk)
{
Alexander Viro's avatar
Alexander Viro committed
449
	struct scsi_cd *cd = scsi_cd(disk);
Alexander Viro's avatar
Alexander Viro committed
450 451 452
	return cdrom_media_changed(&cd->cdi);
}

Linus Torvalds's avatar
Linus Torvalds committed
453 454
struct block_device_operations sr_bdops =
{
Alexander Viro's avatar
Alexander Viro committed
455 456 457 458 459
	.owner		= THIS_MODULE,
	.open		= sr_block_open,
	.release	= sr_block_release,
	.ioctl		= sr_block_ioctl,
	.media_changed	= sr_block_media_changed,
Linus Torvalds's avatar
Linus Torvalds committed
460 461
};

Linus Torvalds's avatar
Linus Torvalds committed
462 463
static int sr_open(struct cdrom_device_info *cdi, int purpose)
{
464
	struct scsi_cd *cd = cdi->handle;
465 466
	struct scsi_device *sdev = cd->device;
	int retval;
467

468 469 470 471
	retval = scsi_device_get(sdev);
	if (retval)
		return retval;
	
Linus Torvalds's avatar
Linus Torvalds committed
472 473 474 475
	/*
	 * If the device is in error recovery, wait until it is done.
	 * If the device is offline, then disallow any access to it.
	 */
476 477 478
	retval = -ENXIO;
	if (!scsi_block_when_processing_errors(sdev))
		goto error_out;
Linus Torvalds's avatar
Linus Torvalds committed
479

480 481
	/*
	 * If this device did not have media in the drive at boot time, then
Linus Torvalds's avatar
Linus Torvalds committed
482 483 484
	 * we would have been unable to get the sector size.  Check to see if
	 * this is the case, and try again.
	 */
485
	if (cd->needs_sector_size)
486
		get_sectorsize(cd);
Linus Torvalds's avatar
Linus Torvalds committed
487
	return 0;
488 489 490 491 492 493 494 495 496 497 498 499 500 501

error_out:
	scsi_device_put(sdev);
	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);

	scsi_device_put(cd->device);
Linus Torvalds's avatar
Linus Torvalds committed
502 503
}

504
static int sr_probe(struct device *dev)
Linus Torvalds's avatar
Linus Torvalds committed
505
{
506
	struct scsi_device *sdev = to_scsi_device(dev);
507 508
	struct gendisk *disk;
	struct scsi_cd *cd;
509
	int minor, error;
Linus Torvalds's avatar
Linus Torvalds committed
510

511
	error = -ENODEV;
512
	if (sdev->type != TYPE_ROM && sdev->type != TYPE_WORM)
513
		goto fail;
Linus Torvalds's avatar
Linus Torvalds committed
514

515
	error = -ENOMEM;
516 517
	cd = kmalloc(sizeof(*cd), GFP_KERNEL);
	if (!cd)
518
		goto fail;
519
	memset(cd, 0, sizeof(*cd));
520

521 522 523
	disk = alloc_disk(1);
	if (!disk)
		goto fail_free;
Linus Torvalds's avatar
Linus Torvalds committed
524

525 526 527 528 529 530 531 532 533
	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);
Linus Torvalds's avatar
Linus Torvalds committed
534

535
	disk->major = SCSI_CDROM_MAJOR;
536 537 538 539
	disk->first_minor = minor;
	sprintf(disk->disk_name, "sr%d", minor);
	disk->fops = &sr_bdops;
	disk->flags = GENHD_FL_CD;
540

541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
	cd->device = sdev;
	cd->disk = disk;
	cd->driver = &sr_template;
	cd->disk = disk;
	cd->capacity = 0x1fffff;
	cd->needs_sector_size = 1;
	cd->device->changed = 1;	/* force recheck CD type */
	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);
Linus Torvalds's avatar
Linus Torvalds committed
557

558
	sdev->sector_size = 2048;	/* A guess, just in case */
Linus Torvalds's avatar
Linus Torvalds committed
559

560 561 562 563
	/* FIXME: need to handle a get_capabilities failure properly ?? */
	get_capabilities(cd);
	sr_vendor_init(cd);

564 565
	snprintf(disk->devfs_name, sizeof(disk->devfs_name),
			"%s/cd", sdev->devfs_name);
566
	disk->driverfs_dev = &sdev->sdev_gendev;
567 568 569
	register_cdrom(&cd->cdi);
	set_capacity(disk, cd->capacity);
	disk->private_data = &cd->driver;
570
	disk->queue = sdev->request_queue;
571

572
	dev_set_drvdata(dev, cd);
573 574 575 576 577 578
	add_disk(disk);

	printk(KERN_DEBUG
	    "Attached scsi CD-ROM %s at scsi%d, channel %d, id %d, lun %d\n",
	    cd->cdi.name, sdev->host->host_no, sdev->channel,
	    sdev->id, sdev->lun);
Linus Torvalds's avatar
Linus Torvalds committed
579
	return 0;
580

581 582
fail_put:
	put_disk(disk);
583 584
fail_free:
	kfree(cd);
585
fail:
586
	return error;
Linus Torvalds's avatar
Linus Torvalds committed
587 588 589
}


590
static void get_sectorsize(struct scsi_cd *cd)
Linus Torvalds's avatar
Linus Torvalds committed
591 592 593
{
	unsigned char cmd[10];
	unsigned char *buffer;
594
	int the_result, retries = 3;
Linus Torvalds's avatar
Linus Torvalds committed
595
	int sector_size;
596
	struct scsi_request *SRpnt = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
597
	request_queue_t *queue;
598

599
	buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
600 601
	if (!buffer)
		goto Enomem;
602
	SRpnt = scsi_allocate_request(cd->device, GFP_KERNEL);
603 604
	if (!SRpnt)
		goto Enomem;
Linus Torvalds's avatar
Linus Torvalds committed
605 606 607

	do {
		cmd[0] = READ_CAPACITY;
608
		memset((void *) &cmd[1], 0, 9);
609 610
		/* Mark as really busy */
		SRpnt->sr_request->rq_status = RQ_SCSI_BUSY;
Linus Torvalds's avatar
Linus Torvalds committed
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
		SRpnt->sr_cmd_len = 0;

		memset(buffer, 0, 8);

		/* Do the command and wait.. */
		SRpnt->sr_data_direction = SCSI_DATA_READ;
		scsi_wait_req(SRpnt, (void *) cmd, (void *) buffer,
			      8, SR_TIMEOUT, MAX_RETRIES);

		the_result = SRpnt->sr_result;
		retries--;

	} while (the_result && retries);


	scsi_release_request(SRpnt);
	SRpnt = NULL;

	if (the_result) {
630
		cd->capacity = 0x1fffff;
Linus Torvalds's avatar
Linus Torvalds committed
631
		sector_size = 2048;	/* A guess, just in case */
632
		cd->needs_sector_size = 1;
Linus Torvalds's avatar
Linus Torvalds committed
633 634
	} else {
#if 0
635 636
		if (cdrom_get_last_written(&cd->cdi,
					   &cd->capacity))
Linus Torvalds's avatar
Linus Torvalds committed
637
#endif
638
			cd->capacity = 1 + ((buffer[0] << 24) |
Linus Torvalds's avatar
Linus Torvalds committed
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
						    (buffer[1] << 16) |
						    (buffer[2] << 8) |
						    buffer[3]);
		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:
657
			cd->capacity *= 4;
Linus Torvalds's avatar
Linus Torvalds committed
658 659 660 661
			/* fall through */
		case 512:
			break;
		default:
662
			printk("%s: unsupported sector size %d.\n",
663 664 665
			       cd->cdi.name, sector_size);
			cd->capacity = 0;
			cd->needs_sector_size = 1;
Linus Torvalds's avatar
Linus Torvalds committed
666 667
		}

668
		cd->device->sector_size = sector_size;
Linus Torvalds's avatar
Linus Torvalds committed
669 670 671 672 673

		/*
		 * Add this so that we have the ability to correctly gauge
		 * what the device is capable of.
		 */
674
		cd->needs_sector_size = 0;
675
		set_capacity(cd->disk, cd->capacity);
Linus Torvalds's avatar
Linus Torvalds committed
676
	}
677

678
	queue = cd->device->request_queue;
Linus Torvalds's avatar
Linus Torvalds committed
679
	blk_queue_hardsect_size(queue, sector_size);
680
out:
Linus Torvalds's avatar
Linus Torvalds committed
681
	kfree(buffer);
682 683 684
	return;

Enomem:
685
	cd->capacity = 0x1fffff;
686
	sector_size = 2048;	/* A guess, just in case */
687
	cd->needs_sector_size = 1;
688 689 690
	if (SRpnt)
		scsi_release_request(SRpnt);
	goto out;
Linus Torvalds's avatar
Linus Torvalds committed
691 692
}

693
static void get_capabilities(struct scsi_cd *cd)
Linus Torvalds's avatar
Linus Torvalds committed
694 695
{
	unsigned char *buffer;
Andrew Morton's avatar
Andrew Morton committed
696
	int rc, n, mrw_write = 0, mrw = 1;
697
	struct scsi_mode_data data;
698 699 700 701
	struct scsi_request *SRpnt;
	unsigned char cmd[MAX_COMMAND_SIZE];
	unsigned int the_result;
	int retries;
Linus Torvalds's avatar
Linus Torvalds committed
702 703 704 705 706 707 708 709 710 711 712 713 714

	static char *loadmech[] =
	{
		"caddy",
		"tray",
		"pop-up",
		"",
		"changer",
		"cartridge changer",
		"",
		""
	};

715
	/* allocate a request for the TEST_UNIT_READY */
716
	SRpnt = scsi_allocate_request(cd->device, GFP_KERNEL);
717 718 719 720 721 722 723
	if (!SRpnt) {
		printk(KERN_WARNING "(get_capabilities:) Request allocation "
		       "failure.\n");
		return;
	}

	/* allocate transfer buffer */
724 725
	buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
	if (!buffer) {
Linus Torvalds's avatar
Linus Torvalds committed
726
		printk(KERN_ERR "sr: out of memory.\n");
727
		scsi_release_request(SRpnt);
Linus Torvalds's avatar
Linus Torvalds committed
728 729
		return;
	}
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754

	/* issue TEST_UNIT_READY until the initial startup UNIT_ATTENTION
	 * conditions are gone, or a timeout happens
	 */
	retries = 0;
	do {
		memset((void *)cmd, 0, MAX_COMMAND_SIZE);
		cmd[0] = TEST_UNIT_READY;

		SRpnt->sr_cmd_len = 0;
		SRpnt->sr_sense_buffer[0] = 0;
		SRpnt->sr_sense_buffer[2] = 0;
		SRpnt->sr_data_direction = DMA_NONE;

		scsi_wait_req (SRpnt, (void *) cmd, buffer,
			       0, SR_TIMEOUT, MAX_RETRIES);

		the_result = SRpnt->sr_result;
		retries++;
	} while (retries < 5 && 
		 (!scsi_status_is_good(the_result) ||
		  ((driver_byte(the_result) & DRIVER_SENSE) &&
		   SRpnt->sr_sense_buffer[2] == UNIT_ATTENTION)));

	/* ask for mode page 0x2a */
755 756 757 758
	rc = scsi_mode_sense(cd->device, 0, 0x2a, buffer, 128,
			     SR_TIMEOUT, 3, &data);

	if (!scsi_status_is_good(rc)) {
Linus Torvalds's avatar
Linus Torvalds committed
759
		/* failed, drive doesn't have capabilities mode page */
760 761
		cd->cdi.speed = 1;
		cd->cdi.mask |= (CDC_CD_R | CDC_CD_RW | CDC_DVD_R |
Linus Torvalds's avatar
Linus Torvalds committed
762 763
					 CDC_DVD | CDC_DVD_RAM |
					 CDC_SELECT_DISC | CDC_SELECT_SPEED);
764
		scsi_release_request(SRpnt);
Linus Torvalds's avatar
Linus Torvalds committed
765
		kfree(buffer);
766
		printk("%s: scsi-1 drive\n", cd->cdi.name);
Linus Torvalds's avatar
Linus Torvalds committed
767 768
		return;
	}
Andrew Morton's avatar
Andrew Morton committed
769 770 771 772 773 774 775 776 777

	if (cdrom_is_mrw(&cd->cdi, &mrw_write)) {
		mrw = 0;
		cd->cdi.mask |= CDC_MRW;
		cd->cdi.mask |= CDC_MRW_W;
	}
	if (!mrw_write)
		cd->cdi.mask |= CDC_MRW_W;

778
	n = data.header_length + data.block_descriptor_length;
779 780 781
	cd->cdi.speed = ((buffer[n + 8] << 8) + buffer[n + 9]) / 176;
	cd->readcd_known = 1;
	cd->readcd_cdda = buffer[n + 5] & 0x01;
Linus Torvalds's avatar
Linus Torvalds committed
782
	/* print some capability bits */
783
	printk("%s: scsi3-mmc drive: %dx/%dx %s%s%s%s%s%s\n", cd->cdi.name,
Linus Torvalds's avatar
Linus Torvalds committed
784
	       ((buffer[n + 14] << 8) + buffer[n + 15]) / 176,
785
	       cd->cdi.speed,
786
	       buffer[n + 3] & 0x01 ? "writer " : "", /* CD Writer */
Linus Torvalds's avatar
Linus Torvalds committed
787
	       buffer[n + 3] & 0x20 ? "dvd-ram " : "",
788 789 790
	       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 */
Linus Torvalds's avatar
Linus Torvalds committed
791 792 793
	       loadmech[buffer[n + 6] >> 5]);
	if ((buffer[n + 6] >> 5) == 0)
		/* caddy drives can't close tray... */
794
		cd->cdi.mask |= CDC_CLOSE_TRAY;
Linus Torvalds's avatar
Linus Torvalds committed
795 796
	if ((buffer[n + 2] & 0x8) == 0)
		/* not a DVD drive */
797
		cd->cdi.mask |= CDC_DVD;
Linus Torvalds's avatar
Linus Torvalds committed
798 799
	if ((buffer[n + 3] & 0x20) == 0) {
		/* can't write DVD-RAM media */
800
		cd->cdi.mask |= CDC_DVD_RAM;
Andrew Morton's avatar
Andrew Morton committed
801
	} else
Linus Torvalds's avatar
Linus Torvalds committed
802 803
	if ((buffer[n + 3] & 0x10) == 0)
		/* can't write DVD-R media */
804
		cd->cdi.mask |= CDC_DVD_R;
Linus Torvalds's avatar
Linus Torvalds committed
805 806
	if ((buffer[n + 3] & 0x2) == 0)
		/* can't write CD-RW media */
807
		cd->cdi.mask |= CDC_CD_RW;
Linus Torvalds's avatar
Linus Torvalds committed
808 809
	if ((buffer[n + 3] & 0x1) == 0)
		/* can't write CD-R media */
810
		cd->cdi.mask |= CDC_CD_R;
Linus Torvalds's avatar
Linus Torvalds committed
811 812
	if ((buffer[n + 6] & 0x8) == 0)
		/* can't eject */
813
		cd->cdi.mask |= CDC_OPEN_TRAY;
Linus Torvalds's avatar
Linus Torvalds committed
814 815 816

	if ((buffer[n + 6] >> 5) == mechtype_individual_changer ||
	    (buffer[n + 6] >> 5) == mechtype_cartridge_changer)
817 818 819
		cd->cdi.capacity =
		    cdrom_number_of_slots(&cd->cdi);
	if (cd->cdi.capacity <= 1)
Linus Torvalds's avatar
Linus Torvalds committed
820
		/* not a changer */
821
		cd->cdi.mask |= CDC_SELECT_DISC;
Linus Torvalds's avatar
Linus Torvalds committed
822
	/*else    I don't think it can close its tray
823
		cd->cdi.mask |= CDC_CLOSE_TRAY; */
Andrew Morton's avatar
Andrew Morton committed
824 825 826 827 828 829

	/*
	 * if DVD-RAM of MRW-W, we are randomly writeable
	 */
	if ((cd->cdi.mask & (CDC_DVD_RAM | CDC_MRW_W)) != (CDC_DVD_RAM | CDC_MRW_W))
		cd->device->writeable = 1;
Linus Torvalds's avatar
Linus Torvalds committed
830

831
	scsi_release_request(SRpnt);
Linus Torvalds's avatar
Linus Torvalds committed
832
	kfree(buffer);
Linus Torvalds's avatar
Linus Torvalds committed
833 834 835 836 837 838
}

/*
 * sr_packet() is the entry point for the generic commands generated
 * by the Uniform CD-ROM layer. 
 */
839 840
static int sr_packet(struct cdrom_device_info *cdi,
		struct cdrom_generic_command *cgc)
Linus Torvalds's avatar
Linus Torvalds committed
841
{
842 843 844 845
	if (cgc->timeout <= 0)
		cgc->timeout = IOCTL_TIMEOUT;

	sr_do_ioctl(cdi->handle, cgc);
Linus Torvalds's avatar
Linus Torvalds committed
846 847 848 849

	return cgc->stat;
}

850
static int sr_remove(struct device *dev)
Linus Torvalds's avatar
Linus Torvalds committed
851
{
852
	struct scsi_cd *cd = dev_get_drvdata(dev);
853

854
	del_gendisk(cd->disk);
Linus Torvalds's avatar
Linus Torvalds committed
855

856 857 858
	spin_lock(&sr_index_lock);
	clear_bit(cd->disk->first_minor, sr_index_bits);
	spin_unlock(&sr_index_lock);
Linus Torvalds's avatar
Linus Torvalds committed
859

860 861
	put_disk(cd->disk);
	unregister_cdrom(&cd->cdi);
862
	kfree(cd);
863 864

	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
865 866 867 868
}

static int __init init_sr(void)
{
869 870
	int rc;

871
	rc = register_blkdev(SCSI_CDROM_MAJOR, "sr");
872 873
	if (rc)
		return rc;
874
	return scsi_register_driver(&sr_template.gendrv);
Linus Torvalds's avatar
Linus Torvalds committed
875 876 877 878
}

static void __exit exit_sr(void)
{
879
	scsi_unregister_driver(&sr_template.gendrv);
880
	unregister_blkdev(SCSI_CDROM_MAJOR, "sr");
Linus Torvalds's avatar
Linus Torvalds committed
881 882 883 884
}

module_init(init_sr);
module_exit(exit_sr);
Linus Torvalds's avatar
Linus Torvalds committed
885
MODULE_LICENSE("GPL");