ide-floppy.c 35 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1
/*
Borislav Petkov's avatar
Borislav Petkov committed
2 3
 * IDE ATAPI floppy driver.
 *
4 5 6
 * Copyright (C) 1996-1999  Gadi Oxman <gadio@netvision.net.il>
 * Copyright (C) 2000-2002  Paul Bristow <paul@paulbristow.net>
 * Copyright (C) 2005       Bartlomiej Zolnierkiewicz
7
 *
Linus Torvalds's avatar
Linus Torvalds committed
8 9 10 11 12 13
 * This driver supports the following IDE floppy drives:
 *
 * LS-120/240 SuperDisk
 * Iomega Zip 100/250
 * Iomega PC Card Clik!/PocketZip
 *
Borislav Petkov's avatar
Borislav Petkov committed
14 15
 * For a historical changelog see
 * Documentation/ide/ChangeLog.ide-floppy.1996-2002
Linus Torvalds's avatar
Linus Torvalds committed
16 17
 */

18 19
#define DRV_NAME "ide-floppy"

20
#define IDEFLOPPY_VERSION "1.00"
Linus Torvalds's avatar
Linus Torvalds committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

#include <linux/module.h>
#include <linux/types.h>
#include <linux/string.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/timer.h>
#include <linux/mm.h>
#include <linux/interrupt.h>
#include <linux/major.h>
#include <linux/errno.h>
#include <linux/genhd.h>
#include <linux/slab.h>
#include <linux/cdrom.h>
#include <linux/ide.h>
36
#include <linux/hdreg.h>
Linus Torvalds's avatar
Linus Torvalds committed
37
#include <linux/bitops.h>
38
#include <linux/mutex.h>
39
#include <linux/scatterlist.h>
Linus Torvalds's avatar
Linus Torvalds committed
40

41 42
#include <scsi/scsi_ioctl.h>

Linus Torvalds's avatar
Linus Torvalds committed
43
#include <asm/byteorder.h>
44 45 46
#include <linux/irq.h>
#include <linux/uaccess.h>
#include <linux/io.h>
Linus Torvalds's avatar
Linus Torvalds committed
47 48
#include <asm/unaligned.h>

49
/* define to see debug info */
Linus Torvalds's avatar
Linus Torvalds committed
50 51 52
#define IDEFLOPPY_DEBUG_LOG		0

/* #define IDEFLOPPY_DEBUG(fmt, args...) printk(KERN_INFO fmt, ## args) */
53
#define IDEFLOPPY_DEBUG(fmt, args...)
Linus Torvalds's avatar
Linus Torvalds committed
54 55

#if IDEFLOPPY_DEBUG_LOG
56 57
#define debug_log(fmt, args...) \
	printk(KERN_INFO "ide-floppy: " fmt, ## args)
Linus Torvalds's avatar
Linus Torvalds committed
58
#else
59
#define debug_log(fmt, args...) do {} while (0)
Linus Torvalds's avatar
Linus Torvalds committed
60 61 62
#endif


63
/* Some drives require a longer irq timeout. */
Linus Torvalds's avatar
Linus Torvalds committed
64 65 66
#define IDEFLOPPY_WAIT_CMD		(5 * WAIT_CMD)

/*
67 68
 * After each failed packet command we issue a request sense command and retry
 * the packet command IDEFLOPPY_MAX_PC_RETRIES times.
Linus Torvalds's avatar
Linus Torvalds committed
69 70 71 72
 */
#define IDEFLOPPY_MAX_PC_RETRIES	3

/*
73 74
 * With each packet command, we allocate a buffer of IDEFLOPPY_PC_BUFFER_SIZE
 * bytes.
Linus Torvalds's avatar
Linus Torvalds committed
75 76 77
 */
#define IDEFLOPPY_PC_BUFFER_SIZE	256

78
/* format capacities descriptor codes */
Linus Torvalds's avatar
Linus Torvalds committed
79 80 81 82 83 84
#define CAPACITY_INVALID	0x00
#define CAPACITY_UNFORMATTED	0x01
#define CAPACITY_CURRENT	0x02
#define CAPACITY_NO_CARTRIDGE	0x03

/*
85 86 87
 * Most of our global data which we need to save even as we leave the driver
 * due to an interrupt or a timer event is stored in a variable of type
 * idefloppy_floppy_t, defined below.
Linus Torvalds's avatar
Linus Torvalds committed
88 89 90 91 92 93
 */
typedef struct ide_floppy_obj {
	ide_drive_t	*drive;
	ide_driver_t	*driver;
	struct gendisk	*disk;
	struct kref	kref;
94
	unsigned int	openers;	/* protected by BKL for now */
Linus Torvalds's avatar
Linus Torvalds committed
95 96

	/* Current packet command */
97
	struct ide_atapi_pc *pc;
Linus Torvalds's avatar
Linus Torvalds committed
98
	/* Last failed packet command */
99
	struct ide_atapi_pc *failed_pc;
100 101
	/* used for blk_{fs,pc}_request() requests */
	struct ide_atapi_pc queued_pc;
102

103
	struct ide_atapi_pc request_sense_pc;
104
	struct request request_sense_rq;
Linus Torvalds's avatar
Linus Torvalds committed
105

106
	/* Last error information */
Linus Torvalds's avatar
Linus Torvalds committed
107 108 109 110 111
	u8 sense_key, asc, ascq;
	/* delay this long before sending packet command */
	u8 ticks;
	int progress_indication;

112
	/* Device information */
Linus Torvalds's avatar
Linus Torvalds committed
113 114
	/* Current format */
	int blocks, block_size, bs_factor;
115 116
	/* Last format capacity descriptor */
	u8 cap_desc[8];
Linus Torvalds's avatar
Linus Torvalds committed
117
	/* Copy of the flexible disk page */
118
	u8 flexible_disk_page[32];
Linus Torvalds's avatar
Linus Torvalds committed
119 120 121 122 123 124
	/* Write protect */
	int wp;
	/* Supports format progress report */
	int srfp;
} idefloppy_floppy_t;

125
#define IDEFLOPPY_TICKS_DELAY	HZ/20	/* default delay for ZIP 100 (50ms) */
Linus Torvalds's avatar
Linus Torvalds committed
126

127
/* IOCTLs used in low-level formatting. */
Linus Torvalds's avatar
Linus Torvalds committed
128 129 130 131 132
#define	IDEFLOPPY_IOCTL_FORMAT_SUPPORTED	0x4600
#define	IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY	0x4601
#define	IDEFLOPPY_IOCTL_FORMAT_START		0x4602
#define IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS	0x4603

133
/* Error code returned in rq->errors to the higher part of the driver. */
Linus Torvalds's avatar
Linus Torvalds committed
134 135 136
#define	IDEFLOPPY_ERROR_GENERAL		101

/*
137 138
 * Pages of the SELECT SENSE / MODE SENSE packet commands.
 * See SFF-8070i spec.
Linus Torvalds's avatar
Linus Torvalds committed
139 140 141 142
 */
#define	IDEFLOPPY_CAPABILITIES_PAGE	0x1b
#define IDEFLOPPY_FLEXIBLE_DISK_PAGE	0x05

143
static DEFINE_MUTEX(idefloppy_ref_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
144 145 146 147 148 149

#define to_ide_floppy(obj) container_of(obj, struct ide_floppy_obj, kref)

#define ide_floppy_g(disk) \
	container_of((disk)->private_data, struct ide_floppy_obj, driver)

150 151
static void idefloppy_cleanup_obj(struct kref *);

Linus Torvalds's avatar
Linus Torvalds committed
152 153 154 155
static struct ide_floppy_obj *ide_floppy_get(struct gendisk *disk)
{
	struct ide_floppy_obj *floppy = NULL;

156
	mutex_lock(&idefloppy_ref_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
157
	floppy = ide_floppy_g(disk);
158
	if (floppy) {
159
		if (ide_device_get(floppy->drive))
160
			floppy = NULL;
161 162
		else
			kref_get(&floppy->kref);
163
	}
164
	mutex_unlock(&idefloppy_ref_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
165 166 167 168 169
	return floppy;
}

static void ide_floppy_put(struct ide_floppy_obj *floppy)
{
170 171
	ide_drive_t *drive = floppy->drive;

172
	mutex_lock(&idefloppy_ref_mutex);
173
	kref_put(&floppy->kref, idefloppy_cleanup_obj);
174
	ide_device_put(drive);
175
	mutex_unlock(&idefloppy_ref_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
176 177 178
}

/*
179 180
 * Used to finish servicing a request. For read/write requests, we will call
 * ide_end_request to pass to the next buffer.
Linus Torvalds's avatar
Linus Torvalds committed
181
 */
182
static int idefloppy_end_request(ide_drive_t *drive, int uptodate, int nsecs)
Linus Torvalds's avatar
Linus Torvalds committed
183 184 185 186 187
{
	idefloppy_floppy_t *floppy = drive->driver_data;
	struct request *rq = HWGROUP(drive)->rq;
	int error;

188
	debug_log("Reached %s\n", __func__);
Linus Torvalds's avatar
Linus Torvalds committed
189 190

	switch (uptodate) {
191 192 193
	case 0: error = IDEFLOPPY_ERROR_GENERAL; break;
	case 1: error = 0; break;
	default: error = uptodate;
Linus Torvalds's avatar
Linus Torvalds committed
194 195 196 197 198 199
	}
	if (error)
		floppy->failed_pc = NULL;
	/* Why does this happen? */
	if (!rq)
		return 0;
200
	if (!blk_special_request(rq)) {
Linus Torvalds's avatar
Linus Torvalds committed
201 202 203 204 205 206 207 208 209 210
		/* our real local end request function */
		ide_end_request(drive, uptodate, nsecs);
		return 0;
	}
	rq->errors = error;
	/* fixme: need to move this local also */
	ide_end_drive_cmd(drive, 0, 0);
	return 0;
}

211 212
static void idefloppy_update_buffers(ide_drive_t *drive,
				struct ide_atapi_pc *pc)
Linus Torvalds's avatar
Linus Torvalds committed
213 214 215 216 217
{
	struct request *rq = pc->rq;
	struct bio *bio = rq->bio;

	while ((bio = rq->bio) != NULL)
218
		idefloppy_end_request(drive, 1, 0);
Linus Torvalds's avatar
Linus Torvalds committed
219 220 221
}

/*
222 223 224
 * Generate a new packet command request in front of the request queue, before
 * the current request so that it will be processed immediately, on the next
 * pass through the driver.
Linus Torvalds's avatar
Linus Torvalds committed
225
 */
226
static void idefloppy_queue_pc_head(ide_drive_t *drive, struct ide_atapi_pc *pc,
227
		struct request *rq)
Linus Torvalds's avatar
Linus Torvalds committed
228 229 230
{
	struct ide_floppy_obj *floppy = drive->driver_data;

231
	blk_rq_init(NULL, rq);
Linus Torvalds's avatar
Linus Torvalds committed
232
	rq->buffer = (char *) pc;
233
	rq->cmd_type = REQ_TYPE_SPECIAL;
234
	rq->cmd_flags |= REQ_PREEMPT;
Linus Torvalds's avatar
Linus Torvalds committed
235
	rq->rq_disk = floppy->disk;
236
	memcpy(rq->cmd, pc->c, 12);
237
	ide_do_drive_cmd(drive, rq);
Linus Torvalds's avatar
Linus Torvalds committed
238 239
}

240
static void ide_floppy_callback(ide_drive_t *drive)
Linus Torvalds's avatar
Linus Torvalds committed
241 242
{
	idefloppy_floppy_t *floppy = drive->driver_data;
243 244
	struct ide_atapi_pc *pc = floppy->pc;
	int uptodate = pc->error ? 0 : 1;
Linus Torvalds's avatar
Linus Torvalds committed
245

246 247
	debug_log("Reached %s\n", __func__);

248 249 250
	if (floppy->failed_pc == pc)
		floppy->failed_pc = NULL;

251 252 253 254 255
	if (pc->c[0] == GPCMD_READ_10 || pc->c[0] == GPCMD_WRITE_10 ||
	    (pc->rq && blk_pc_request(pc->rq)))
		uptodate = 1; /* FIXME */
	else if (pc->c[0] == GPCMD_REQUEST_SENSE) {
		u8 *buf = floppy->pc->buf;
256

257 258 259 260 261 262
		if (!pc->error) {
			floppy->sense_key = buf[2] & 0x0F;
			floppy->asc = buf[12];
			floppy->ascq = buf[13];
			floppy->progress_indication = buf[15] & 0x80 ?
				(u16)get_unaligned((u16 *)&buf[16]) : 0x10000;
263

264 265
			if (floppy->failed_pc)
				debug_log("pc = %x, ", floppy->failed_pc->c[0]);
266

267 268 269 270 271 272
			debug_log("sense key = %x, asc = %x, ascq = %x\n",
				  floppy->sense_key, floppy->asc, floppy->ascq);
		} else
			printk(KERN_ERR "Error in REQUEST SENSE itself - "
					"Aborting request!\n");
	}
Linus Torvalds's avatar
Linus Torvalds committed
273

274
	idefloppy_end_request(drive, uptodate, 0);
Linus Torvalds's avatar
Linus Torvalds committed
275 276
}

277
static void idefloppy_init_pc(struct ide_atapi_pc *pc)
Linus Torvalds's avatar
Linus Torvalds committed
278
{
279
	memset(pc, 0, sizeof(*pc));
280 281
	pc->buf = pc->pc_buf;
	pc->buf_size = IDEFLOPPY_PC_BUFFER_SIZE;
Linus Torvalds's avatar
Linus Torvalds committed
282 283
}

284
static void idefloppy_create_request_sense_cmd(struct ide_atapi_pc *pc)
Linus Torvalds's avatar
Linus Torvalds committed
285
{
286 287
	idefloppy_init_pc(pc);
	pc->c[0] = GPCMD_REQUEST_SENSE;
Linus Torvalds's avatar
Linus Torvalds committed
288
	pc->c[4] = 255;
289
	pc->req_xfer = 18;
Linus Torvalds's avatar
Linus Torvalds committed
290 291 292
}

/*
293 294
 * Called when an error was detected during the last packet command. We queue a
 * request sense packet command in the head of the request list.
Linus Torvalds's avatar
Linus Torvalds committed
295
 */
296
static void idefloppy_retry_pc(ide_drive_t *drive)
Linus Torvalds's avatar
Linus Torvalds committed
297
{
298 299
	struct ide_floppy_obj *floppy = drive->driver_data;
	struct request *rq = &floppy->request_sense_rq;
300
	struct ide_atapi_pc *pc = &floppy->request_sense_pc;
Linus Torvalds's avatar
Linus Torvalds committed
301

302
	(void)ide_read_error(drive);
Linus Torvalds's avatar
Linus Torvalds committed
303 304 305 306
	idefloppy_create_request_sense_cmd(pc);
	idefloppy_queue_pc_head(drive, pc, rq);
}

307
/* The usual interrupt handler called during a packet command. */
308
static ide_startstop_t idefloppy_pc_intr(ide_drive_t *drive)
Linus Torvalds's avatar
Linus Torvalds committed
309 310 311
{
	idefloppy_floppy_t *floppy = drive->driver_data;

312 313
	return ide_pc_intr(drive, floppy->pc, idefloppy_pc_intr,
			   IDEFLOPPY_WAIT_CMD, NULL, idefloppy_update_buffers,
314
			   idefloppy_retry_pc, NULL, ide_io_buffers);
Linus Torvalds's avatar
Linus Torvalds committed
315 316 317
}

/*
318 319 320 321
 * What we have here is a classic case of a top half / bottom half interrupt
 * service routine. In interrupt mode, the device sends an interrupt to signal
 * that it is ready to receive a packet. However, we need to delay about 2-3
 * ticks before issuing the packet or we gets in trouble.
Linus Torvalds's avatar
Linus Torvalds committed
322
 */
323
static int idefloppy_transfer_pc(ide_drive_t *drive)
Linus Torvalds's avatar
Linus Torvalds committed
324 325 326 327
{
	idefloppy_floppy_t *floppy = drive->driver_data;

	/* Send the actual packet */
328
	drive->hwif->tp_ops->output_data(drive, NULL, floppy->pc->c, 12);
329

Linus Torvalds's avatar
Linus Torvalds committed
330 331 332 333
	/* Timeout for the packet command */
	return IDEFLOPPY_WAIT_CMD;
}

334 335 336 337 338 339 340

/*
 * Called as an interrupt (or directly). When the device says it's ready for a
 * packet, we schedule the packet transfer to occur about 2-3 ticks later in
 * transfer_pc.
 */
static ide_startstop_t idefloppy_start_pc_transfer(ide_drive_t *drive)
Linus Torvalds's avatar
Linus Torvalds committed
341 342
{
	idefloppy_floppy_t *floppy = drive->driver_data;
343
	struct ide_atapi_pc *pc = floppy->pc;
344 345
	ide_expiry_t *expiry;
	unsigned int timeout;
Linus Torvalds's avatar
Linus Torvalds committed
346

347
	/*
Linus Torvalds's avatar
Linus Torvalds committed
348 349 350 351 352 353 354
	 * The following delay solves a problem with ATAPI Zip 100 drives
	 * where the Busy flag was apparently being deasserted before the
	 * unit was ready to receive data. This was happening on a
	 * 1200 MHz Athlon system. 10/26/01 25msec is too short,
	 * 40 and 50msec work well. idefloppy_pc_intr will not be actually
	 * used until after the packet is moved in about 50 msec.
	 */
355
	if (drive->atapi_flags & IDE_AFLAG_ZIP_DRIVE) {
356
		timeout = floppy->ticks;
357
		expiry = &idefloppy_transfer_pc;
358 359 360 361 362
	} else {
		timeout = IDEFLOPPY_WAIT_CMD;
		expiry = NULL;
	}

363
	return ide_transfer_pc(drive, pc, idefloppy_pc_intr, timeout, expiry);
Linus Torvalds's avatar
Linus Torvalds committed
364 365
}

366
static void ide_floppy_report_error(idefloppy_floppy_t *floppy,
367
				    struct ide_atapi_pc *pc)
Linus Torvalds's avatar
Linus Torvalds committed
368
{
369
	/* supress error messages resulting from Medium not present */
Linus Torvalds's avatar
Linus Torvalds committed
370 371 372
	if (floppy->sense_key == 0x02 &&
	    floppy->asc       == 0x3a &&
	    floppy->ascq      == 0x00)
373 374 375 376 377 378 379
		return;

	printk(KERN_ERR "ide-floppy: %s: I/O error, pc = %2x, key = %2x, "
			"asc = %2x, ascq = %2x\n",
			floppy->drive->name, pc->c[0], floppy->sense_key,
			floppy->asc, floppy->ascq);

Linus Torvalds's avatar
Linus Torvalds committed
380 381
}

382
static ide_startstop_t idefloppy_issue_pc(ide_drive_t *drive,
383
		struct ide_atapi_pc *pc)
Linus Torvalds's avatar
Linus Torvalds committed
384 385 386 387
{
	idefloppy_floppy_t *floppy = drive->driver_data;

	if (floppy->failed_pc == NULL &&
388
	    pc->c[0] != GPCMD_REQUEST_SENSE)
Linus Torvalds's avatar
Linus Torvalds committed
389 390 391 392
		floppy->failed_pc = pc;
	/* Set the current packet command */
	floppy->pc = pc;

393
	if (pc->retries > IDEFLOPPY_MAX_PC_RETRIES) {
394
		if (!(pc->flags & PC_FLAG_SUPPRESS_ERROR))
395 396 397 398
			ide_floppy_report_error(floppy, pc);
		/* Giving up */
		pc->error = IDEFLOPPY_ERROR_GENERAL;

Linus Torvalds's avatar
Linus Torvalds committed
399
		floppy->failed_pc = NULL;
400
		drive->pc_callback(drive);
Linus Torvalds's avatar
Linus Torvalds committed
401 402 403
		return ide_stopped;
	}

404
	debug_log("Retry number - %d\n", pc->retries);
Linus Torvalds's avatar
Linus Torvalds committed
405 406 407

	pc->retries++;

408
	return ide_issue_pc(drive, pc, idefloppy_start_pc_transfer,
409
			    IDEFLOPPY_WAIT_CMD, NULL);
Linus Torvalds's avatar
Linus Torvalds committed
410 411
}

412
static void idefloppy_create_prevent_cmd(struct ide_atapi_pc *pc, int prevent)
Linus Torvalds's avatar
Linus Torvalds committed
413
{
414
	debug_log("creating prevent removal command, prevent = %d\n", prevent);
Linus Torvalds's avatar
Linus Torvalds committed
415 416

	idefloppy_init_pc(pc);
417
	pc->c[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
Linus Torvalds's avatar
Linus Torvalds committed
418 419 420
	pc->c[4] = prevent;
}

421
static void idefloppy_create_read_capacity_cmd(struct ide_atapi_pc *pc)
Linus Torvalds's avatar
Linus Torvalds committed
422 423
{
	idefloppy_init_pc(pc);
424
	pc->c[0] = GPCMD_READ_FORMAT_CAPACITIES;
Linus Torvalds's avatar
Linus Torvalds committed
425 426
	pc->c[7] = 255;
	pc->c[8] = 255;
427
	pc->req_xfer = 255;
Linus Torvalds's avatar
Linus Torvalds committed
428 429
}

430 431
static void idefloppy_create_format_unit_cmd(struct ide_atapi_pc *pc, int b,
		int l, int flags)
Linus Torvalds's avatar
Linus Torvalds committed
432 433
{
	idefloppy_init_pc(pc);
434
	pc->c[0] = GPCMD_FORMAT_UNIT;
Linus Torvalds's avatar
Linus Torvalds committed
435 436
	pc->c[1] = 0x17;

437 438
	memset(pc->buf, 0, 12);
	pc->buf[1] = 0xA2;
Linus Torvalds's avatar
Linus Torvalds committed
439 440 441
	/* Default format list header, u8 1: FOV/DCRT/IMM bits set */

	if (flags & 1)				/* Verify bit on... */
442 443
		pc->buf[1] ^= 0x20;		/* ... turn off DCRT bit */
	pc->buf[3] = 8;
Linus Torvalds's avatar
Linus Torvalds committed
444

445 446 447
	put_unaligned(cpu_to_be32(b), (unsigned int *)(&pc->buf[4]));
	put_unaligned(cpu_to_be32(l), (unsigned int *)(&pc->buf[8]));
	pc->buf_size = 12;
448
	pc->flags |= PC_FLAG_WRITING;
Linus Torvalds's avatar
Linus Torvalds committed
449 450
}

451
/* A mode sense command is used to "sense" floppy parameters. */
452
static void idefloppy_create_mode_sense_cmd(struct ide_atapi_pc *pc,
453
					    u8 page_code)
Linus Torvalds's avatar
Linus Torvalds committed
454
{
455
	u16 length = 8; /* sizeof(Mode Parameter Header) = 8 Bytes */
456

Linus Torvalds's avatar
Linus Torvalds committed
457
	idefloppy_init_pc(pc);
458
	pc->c[0] = GPCMD_MODE_SENSE_10;
Linus Torvalds's avatar
Linus Torvalds committed
459
	pc->c[1] = 0;
460
	pc->c[2] = page_code;
Linus Torvalds's avatar
Linus Torvalds committed
461 462

	switch (page_code) {
463 464 465 466 467 468 469 470
	case IDEFLOPPY_CAPABILITIES_PAGE:
		length += 12;
		break;
	case IDEFLOPPY_FLEXIBLE_DISK_PAGE:
		length += 32;
		break;
	default:
		printk(KERN_ERR "ide-floppy: unsupported page code "
Linus Torvalds's avatar
Linus Torvalds committed
471 472
				"in create_mode_sense_cmd\n");
	}
473
	put_unaligned(cpu_to_be16(length), (u16 *) &pc->c[7]);
474
	pc->req_xfer = length;
Linus Torvalds's avatar
Linus Torvalds committed
475 476
}

477
static void idefloppy_create_start_stop_cmd(struct ide_atapi_pc *pc, int start)
Linus Torvalds's avatar
Linus Torvalds committed
478 479
{
	idefloppy_init_pc(pc);
480
	pc->c[0] = GPCMD_START_STOP_UNIT;
Linus Torvalds's avatar
Linus Torvalds committed
481 482 483
	pc->c[4] = start;
}

484
static void idefloppy_create_rw_cmd(idefloppy_floppy_t *floppy,
485
				    struct ide_atapi_pc *pc, struct request *rq,
486
				    unsigned long sector)
Linus Torvalds's avatar
Linus Torvalds committed
487 488 489 490 491
{
	int block = sector / floppy->bs_factor;
	int blocks = rq->nr_sectors / floppy->bs_factor;
	int cmd = rq_data_dir(rq);

492
	debug_log("create_rw10_cmd: block == %d, blocks == %d\n",
Linus Torvalds's avatar
Linus Torvalds committed
493 494 495
		block, blocks);

	idefloppy_init_pc(pc);
496 497
	pc->c[0] = cmd == READ ? GPCMD_READ_10 : GPCMD_WRITE_10;
	put_unaligned(cpu_to_be16(blocks), (unsigned short *)&pc->c[7]);
498
	put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[2]);
499

500 501
	memcpy(rq->cmd, pc->c, 12);

Linus Torvalds's avatar
Linus Torvalds committed
502
	pc->rq = rq;
503
	pc->b_count = 0;
504
	if (rq->cmd_flags & REQ_RW)
505
		pc->flags |= PC_FLAG_WRITING;
506 507
	pc->buf = NULL;
	pc->req_xfer = pc->buf_size = blocks * floppy->block_size;
508
	pc->flags |= PC_FLAG_DMA_OK;
Linus Torvalds's avatar
Linus Torvalds committed
509 510
}

511
static void idefloppy_blockpc_cmd(idefloppy_floppy_t *floppy,
512
		struct ide_atapi_pc *pc, struct request *rq)
Linus Torvalds's avatar
Linus Torvalds committed
513 514 515
{
	idefloppy_init_pc(pc);
	memcpy(pc->c, rq->cmd, sizeof(pc->c));
516
	pc->rq = rq;
517
	pc->b_count = 0;
518
	if (rq->data_len && rq_data_dir(rq) == WRITE)
519
		pc->flags |= PC_FLAG_WRITING;
520
	pc->buf = rq->data;
521
	if (rq->bio)
522
		pc->flags |= PC_FLAG_DMA_OK;
523 524 525 526
	/*
	 * possibly problematic, doesn't look like ide-floppy correctly
	 * handled scattered requests if dma fails...
	 */
527
	pc->req_xfer = pc->buf_size = rq->data_len;
Linus Torvalds's avatar
Linus Torvalds committed
528 529
}

530 531
static ide_startstop_t idefloppy_do_request(ide_drive_t *drive,
		struct request *rq, sector_t block_s)
Linus Torvalds's avatar
Linus Torvalds committed
532 533
{
	idefloppy_floppy_t *floppy = drive->driver_data;
534
	ide_hwif_t *hwif = drive->hwif;
535
	struct ide_atapi_pc *pc;
Linus Torvalds's avatar
Linus Torvalds committed
536 537
	unsigned long block = (unsigned long)block_s;

538 539 540 541 542 543 544
	debug_log("%s: dev: %s, cmd: 0x%x, cmd_type: %x, errors: %d\n",
		  __func__, rq->rq_disk ? rq->rq_disk->disk_name : "?",
		  rq->cmd[0], rq->cmd_type, rq->errors);

	debug_log("%s: sector: %ld, nr_sectors: %ld, current_nr_sectors: %d\n",
		  __func__, (long)rq->sector, rq->nr_sectors,
		  rq->current_nr_sectors);
Linus Torvalds's avatar
Linus Torvalds committed
545 546

	if (rq->errors >= ERROR_MAX) {
547 548
		if (floppy->failed_pc)
			ide_floppy_report_error(floppy, floppy->failed_pc);
Linus Torvalds's avatar
Linus Torvalds committed
549 550 551
		else
			printk(KERN_ERR "ide-floppy: %s: I/O error\n",
				drive->name);
552
		idefloppy_end_request(drive, 0, 0);
Linus Torvalds's avatar
Linus Torvalds committed
553 554
		return ide_stopped;
	}
555
	if (blk_fs_request(rq)) {
Linus Torvalds's avatar
Linus Torvalds committed
556 557
		if (((long)rq->sector % floppy->bs_factor) ||
		    (rq->nr_sectors % floppy->bs_factor)) {
558 559
			printk(KERN_ERR "%s: unsupported r/w request size\n",
					drive->name);
560
			idefloppy_end_request(drive, 0, 0);
Linus Torvalds's avatar
Linus Torvalds committed
561 562
			return ide_stopped;
		}
563
		pc = &floppy->queued_pc;
Linus Torvalds's avatar
Linus Torvalds committed
564
		idefloppy_create_rw_cmd(floppy, pc, rq, block);
565
	} else if (blk_special_request(rq)) {
566
		pc = (struct ide_atapi_pc *) rq->buffer;
567
	} else if (blk_pc_request(rq)) {
568
		pc = &floppy->queued_pc;
569
		idefloppy_blockpc_cmd(floppy, pc, rq);
Linus Torvalds's avatar
Linus Torvalds committed
570 571 572
	} else {
		blk_dump_rq_flags(rq,
			"ide-floppy: unsupported command in queue");
573
		idefloppy_end_request(drive, 0, 0);
Linus Torvalds's avatar
Linus Torvalds committed
574 575 576
		return ide_stopped;
	}

577 578 579 580 581 582
	ide_init_sg_cmd(drive, rq);
	ide_map_sg(drive, rq);

	pc->sg = hwif->sg_table;
	pc->sg_cnt = hwif->sg_nents;

Linus Torvalds's avatar
Linus Torvalds committed
583
	pc->rq = rq;
584

Linus Torvalds's avatar
Linus Torvalds committed
585 586 587 588
	return idefloppy_issue_pc(drive, pc);
}

/*
589 590
 * Add a special packet command request to the tail of the request queue,
 * and wait for it to be serviced.
Linus Torvalds's avatar
Linus Torvalds committed
591
 */
592
static int idefloppy_queue_pc_tail(ide_drive_t *drive, struct ide_atapi_pc *pc)
Linus Torvalds's avatar
Linus Torvalds committed
593 594
{
	struct ide_floppy_obj *floppy = drive->driver_data;
595 596
	struct request *rq;
	int error;
Linus Torvalds's avatar
Linus Torvalds committed
597

598 599 600
	rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
	rq->buffer = (char *) pc;
	rq->cmd_type = REQ_TYPE_SPECIAL;
601
	memcpy(rq->cmd, pc->c, 12);
602 603
	error = blk_execute_rq(drive->queue, floppy->disk, rq, 0);
	blk_put_request(rq);
Linus Torvalds's avatar
Linus Torvalds committed
604

605
	return error;
Linus Torvalds's avatar
Linus Torvalds committed
606 607 608
}

/*
609 610
 * Look at the flexible disk page parameters. We ignore the CHS capacity
 * parameters and use the LBA parameters instead.
Linus Torvalds's avatar
Linus Torvalds committed
611
 */
612
static int ide_floppy_get_flexible_disk_page(ide_drive_t *drive)
Linus Torvalds's avatar
Linus Torvalds committed
613 614
{
	idefloppy_floppy_t *floppy = drive->driver_data;
615
	struct ide_atapi_pc pc;
616
	u8 *page;
Linus Torvalds's avatar
Linus Torvalds committed
617
	int capacity, lba_capacity;
618 619
	u16 transfer_rate, sector_size, cyls, rpm;
	u8 heads, sectors;
Linus Torvalds's avatar
Linus Torvalds committed
620

621
	idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_FLEXIBLE_DISK_PAGE);
622 623 624 625

	if (idefloppy_queue_pc_tail(drive, &pc)) {
		printk(KERN_ERR "ide-floppy: Can't get flexible disk page"
				" parameters\n");
Linus Torvalds's avatar
Linus Torvalds committed
626 627
		return 1;
	}
628
	floppy->wp = !!(pc.buf[3] & 0x80);
Linus Torvalds's avatar
Linus Torvalds committed
629
	set_disk_ro(floppy->disk, floppy->wp);
630
	page = &pc.buf[8];
631

632 633 634 635
	transfer_rate = be16_to_cpup((__be16 *)&pc.buf[8 + 2]);
	sector_size   = be16_to_cpup((__be16 *)&pc.buf[8 + 6]);
	cyls          = be16_to_cpup((__be16 *)&pc.buf[8 + 8]);
	rpm           = be16_to_cpup((__be16 *)&pc.buf[8 + 28]);
636 637
	heads         = pc.buf[8 + 4];
	sectors       = pc.buf[8 + 5];
638 639 640 641

	capacity = cyls * heads * sectors * sector_size;

	if (memcmp(page, &floppy->flexible_disk_page, 32))
Linus Torvalds's avatar
Linus Torvalds committed
642 643
		printk(KERN_INFO "%s: %dkB, %d/%d/%d CHS, %d kBps, "
				"%d sector size, %d rpm\n",
644 645 646 647 648 649 650
				drive->name, capacity / 1024, cyls, heads,
				sectors, transfer_rate / 8, sector_size, rpm);

	memcpy(&floppy->flexible_disk_page, page, 32);
	drive->bios_cyl = cyls;
	drive->bios_head = heads;
	drive->bios_sect = sectors;
Linus Torvalds's avatar
Linus Torvalds committed
651
	lba_capacity = floppy->blocks * floppy->block_size;
652

Linus Torvalds's avatar
Linus Torvalds committed
653 654 655 656
	if (capacity < lba_capacity) {
		printk(KERN_NOTICE "%s: The disk reports a capacity of %d "
			"bytes, but the drive only handles %d\n",
			drive->name, lba_capacity, capacity);
657 658
		floppy->blocks = floppy->block_size ?
			capacity / floppy->block_size : 0;
Linus Torvalds's avatar
Linus Torvalds committed
659 660 661 662
	}
	return 0;
}

663
static int idefloppy_get_sfrp_bit(ide_drive_t *drive)
Linus Torvalds's avatar
Linus Torvalds committed
664 665
{
	idefloppy_floppy_t *floppy = drive->driver_data;
666
	struct ide_atapi_pc pc;
Linus Torvalds's avatar
Linus Torvalds committed
667 668 669

	floppy->srfp = 0;

670
	idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_CAPABILITIES_PAGE);
671
	pc.flags |= PC_FLAG_SUPPRESS_ERROR;
672

673
	if (idefloppy_queue_pc_tail(drive, &pc))
Linus Torvalds's avatar
Linus Torvalds committed
674 675
		return 1;

676
	floppy->srfp = pc.buf[8 + 2] & 0x40;
677
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
678 679 680
}

/*
681 682
 * Determine if a media is present in the floppy drive, and if so, its LBA
 * capacity.
Linus Torvalds's avatar
Linus Torvalds committed
683
 */
684
static int ide_floppy_get_capacity(ide_drive_t *drive)
Linus Torvalds's avatar
Linus Torvalds committed
685 686
{
	idefloppy_floppy_t *floppy = drive->driver_data;
687
	struct ide_atapi_pc pc;
688 689 690 691
	u8 *cap_desc;
	u8 header_len, desc_cnt;
	int i, rc = 1, blocks, length;

Linus Torvalds's avatar
Linus Torvalds committed
692 693
	drive->bios_cyl = 0;
	drive->bios_head = drive->bios_sect = 0;
694 695
	floppy->blocks = 0;
	floppy->bs_factor = 1;
Linus Torvalds's avatar
Linus Torvalds committed
696 697 698 699 700 701 702
	set_capacity(floppy->disk, 0);

	idefloppy_create_read_capacity_cmd(&pc);
	if (idefloppy_queue_pc_tail(drive, &pc)) {
		printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
		return 1;
	}
703 704
	header_len = pc.buf[3];
	cap_desc = &pc.buf[4];
705 706 707 708 709
	desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */

	for (i = 0; i < desc_cnt; i++) {
		unsigned int desc_start = 4 + i*8;

710 711
		blocks = be32_to_cpup((__be32 *)&pc.buf[desc_start]);
		length = be16_to_cpup((__be16 *)&pc.buf[desc_start + 6]);
712 713 714

		debug_log("Descriptor %d: %dkB, %d blocks, %d sector size\n",
				i, blocks * length / 1024, blocks, length);
Linus Torvalds's avatar
Linus Torvalds committed
715

716 717 718 719 720
		if (i)
			continue;
		/*
		 * the code below is valid only for the 1st descriptor, ie i=0
		 */
Linus Torvalds's avatar
Linus Torvalds committed
721

722
		switch (pc.buf[desc_start + 4] & 0x03) {
Linus Torvalds's avatar
Linus Torvalds committed
723 724
		/* Clik! drive returns this instead of CAPACITY_CURRENT */
		case CAPACITY_UNFORMATTED:
725
			if (!(drive->atapi_flags & IDE_AFLAG_CLIK_DRIVE))
726
				/*
Linus Torvalds's avatar
Linus Torvalds committed
727 728 729 730 731 732
				 * If it is not a clik drive, break out
				 * (maintains previous driver behaviour)
				 */
				break;
		case CAPACITY_CURRENT:
			/* Normal Zip/LS-120 disks */
733
			if (memcmp(cap_desc, &floppy->cap_desc, 8))
Linus Torvalds's avatar
Linus Torvalds committed
734 735 736
				printk(KERN_INFO "%s: %dkB, %d blocks, %d "
					"sector size\n", drive->name,
					blocks * length / 1024, blocks, length);
737 738
			memcpy(&floppy->cap_desc, cap_desc, 8);

Linus Torvalds's avatar
Linus Torvalds committed
739 740 741 742
			if (!length || length % 512) {
				printk(KERN_NOTICE "%s: %d bytes block size "
					"not supported\n", drive->name, length);
			} else {
743 744 745 746 747
				floppy->blocks = blocks;
				floppy->block_size = length;
				floppy->bs_factor = length / 512;
				if (floppy->bs_factor != 1)
					printk(KERN_NOTICE "%s: warning: non "
Linus Torvalds's avatar
Linus Torvalds committed
748 749 750
						"512 bytes block size not "
						"fully supported\n",
						drive->name);
751
				rc = 0;
Linus Torvalds's avatar
Linus Torvalds committed
752 753 754 755 756 757 758 759 760 761 762 763 764 765
			}
			break;
		case CAPACITY_NO_CARTRIDGE:
			/*
			 * This is a KERN_ERR so it appears on screen
			 * for the user to see
			 */
			printk(KERN_ERR "%s: No disk in drive\n", drive->name);
			break;
		case CAPACITY_INVALID:
			printk(KERN_ERR "%s: Invalid capacity for disk "
				"in drive\n", drive->name);
			break;
		}
766
		debug_log("Descriptor 0 Code: %d\n",
767
			  pc.buf[desc_start + 4] & 0x03);
Linus Torvalds's avatar
Linus Torvalds committed
768 769 770
	}

	/* Clik! disk does not support get_flexible_disk_page */
771
	if (!(drive->atapi_flags & IDE_AFLAG_CLIK_DRIVE))
772
		(void) ide_floppy_get_flexible_disk_page(drive);
Linus Torvalds's avatar
Linus Torvalds committed
773 774 775 776 777 778

	set_capacity(floppy->disk, floppy->blocks * floppy->bs_factor);
	return rc;
}

/*
779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795
 * Obtain the list of formattable capacities.
 * Very similar to ide_floppy_get_capacity, except that we push the capacity
 * descriptors to userland, instead of our own structures.
 *
 * Userland gives us the following structure:
 *
 * struct idefloppy_format_capacities {
 *	int nformats;
 *	struct {
 *		int nblocks;
 *		int blocksize;
 *	} formats[];
 * };
 *
 * userland initializes nformats to the number of allocated formats[] records.
 * On exit we set nformats to the number of records we've actually initialized.
 */
Linus Torvalds's avatar
Linus Torvalds committed
796

797
static int ide_floppy_get_format_capacities(ide_drive_t *drive, int __user *arg)
Linus Torvalds's avatar
Linus Torvalds committed
798
{
799
	struct ide_atapi_pc pc;
800 801
	u8 header_len, desc_cnt;
	int i, blocks, length, u_array_size, u_index;
Linus Torvalds's avatar
Linus Torvalds committed
802 803 804
	int __user *argp;

	if (get_user(u_array_size, arg))
805
		return -EFAULT;
Linus Torvalds's avatar
Linus Torvalds committed
806 807

	if (u_array_size <= 0)
808
		return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
809 810 811 812

	idefloppy_create_read_capacity_cmd(&pc);
	if (idefloppy_queue_pc_tail(drive, &pc)) {
		printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
813
		return -EIO;
814
	}
815

816
	header_len = pc.buf[3];
817
	desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */
Linus Torvalds's avatar
Linus Torvalds committed
818 819 820 821 822

	u_index = 0;
	argp = arg + 1;

	/*
823 824 825 826 827 828
	 * We always skip the first capacity descriptor.  That's the current
	 * capacity.  We are interested in the remaining descriptors, the
	 * formattable capacities.
	 */
	for (i = 1; i < desc_cnt; i++) {
		unsigned int desc_start = 4 + i*8;
Linus Torvalds's avatar
Linus Torvalds committed
829 830 831 832

		if (u_index >= u_array_size)
			break;	/* User-supplied buffer too small */

833 834
		blocks = be32_to_cpup((__be32 *)&pc.buf[desc_start]);
		length = be16_to_cpup((__be16 *)&pc.buf[desc_start + 6]);
Linus Torvalds's avatar
Linus Torvalds committed
835 836

		if (put_user(blocks, argp))
837 838
			return -EFAULT;

Linus Torvalds's avatar
Linus Torvalds committed
839 840 841
		++argp;

		if (put_user(length, argp))
842 843
			return -EFAULT;

Linus Torvalds's avatar
Linus Torvalds committed
844 845 846 847 848 849
		++argp;

		++u_index;
	}

	if (put_user(u_index, arg))
850 851 852
		return -EFAULT;

	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
853 854 855
}

/*
856 857 858 859 860 861 862 863
 * Get ATAPI_FORMAT_UNIT progress indication.
 *
 * Userland gives a pointer to an int.  The int is set to a progress
 * indicator 0-65536, with 65536=100%.
 *
 * If the drive does not support format progress indication, we just check
 * the dsc bit, and return either 0 or 65536.
 */
Linus Torvalds's avatar
Linus Torvalds committed
864

865
static int ide_floppy_get_format_progress(ide_drive_t *drive, int __user *arg)
Linus Torvalds's avatar
Linus Torvalds committed
866 867
{
	idefloppy_floppy_t *floppy = drive->driver_data;
868
	struct ide_atapi_pc pc;
Linus Torvalds's avatar
Linus Torvalds committed
869 870 871 872
	int progress_indication = 0x10000;

	if (floppy->srfp) {
		idefloppy_create_request_sense_cmd(&pc);
873
		if (idefloppy_queue_pc_tail(drive, &pc))
874
			return -EIO;
Linus Torvalds's avatar
Linus Torvalds committed
875 876 877

		if (floppy->sense_key == 2 &&
		    floppy->asc == 4 &&
878
		    floppy->ascq == 4)
Linus Torvalds's avatar
Linus Torvalds committed
879
			progress_indication = floppy->progress_indication;
880 881

		/* Else assume format_unit has finished, and we're at 0x10000 */
Linus Torvalds's avatar
Linus Torvalds committed
882
	} else {
883
		ide_hwif_t *hwif = drive->hwif;
Linus Torvalds's avatar
Linus Torvalds committed
884
		unsigned long flags;
885
		u8 stat;
Linus Torvalds's avatar
Linus Torvalds committed
886 887

		local_irq_save(flags);
888
		stat = hwif->tp_ops->read_status(hwif);
Linus Torvalds's avatar
Linus Torvalds committed
889 890
		local_irq_restore(flags);

891
		progress_indication = ((stat & ATA_DSC) == 0) ? 0 : 0x10000;
Linus Torvalds's avatar
Linus Torvalds committed
892
	}
893

Linus Torvalds's avatar
Linus Torvalds committed
894
	if (put_user(progress_indication, arg))
895
		return -EFAULT;
Linus Torvalds's avatar
Linus Torvalds committed
896

897
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
898 899
}

900
static sector_t idefloppy_capacity(ide_drive_t *drive)
Linus Torvalds's avatar
Linus Torvalds committed
901 902 903 904 905 906 907
{
	idefloppy_floppy_t *floppy = drive->driver_data;
	unsigned long capacity = floppy->blocks * floppy->bs_factor;

	return capacity;
}

908
#ifdef CONFIG_IDE_PROC_FS
909 910 911 912 913
ide_devset_rw(bios_cyl,  0, 1023, bios_cyl);
ide_devset_rw(bios_head, 0,  255, bios_head);
ide_devset_rw(bios_sect, 0,   63, bios_sect);

static int get_ticks(ide_drive_t *drive)
Linus Torvalds's avatar
Linus Torvalds committed
914 915
{
	idefloppy_floppy_t *floppy = drive->driver_data;
916 917
	return floppy->ticks;
}
Linus Torvalds's avatar
Linus Torvalds committed
918

919 920 921 922 923
static int set_ticks(ide_drive_t *drive, int arg)
{
	idefloppy_floppy_t *floppy = drive->driver_data;
	floppy->ticks = arg;
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
924
}
925 926 927 928 929 930 931 932 933 934

IDE_DEVSET(ticks, S_RW, 0, 255, get_ticks, set_ticks);

static const struct ide_devset *idefloppy_settings[] = {
	&ide_devset_bios_cyl,
	&ide_devset_bios_head,
	&ide_devset_bios_sect,
	&ide_devset_ticks,
	NULL
};
935
#endif
Linus Torvalds's avatar
Linus Torvalds committed
936

937
static void idefloppy_setup(ide_drive_t *drive, idefloppy_floppy_t *floppy)
Linus Torvalds's avatar
Linus Torvalds committed
938
{
939
	u16 *id = drive->id;
940
	u8 gcw[2];
Linus Torvalds's avatar
Linus Torvalds committed
941

942
	*((u16 *)&gcw) = id[ATA_ID_CONFIG];
943

944
	drive->pc_callback = ide_floppy_callback;
945 946

	if (((gcw[0] & 0x60) >> 5) == 1)
947
		drive->atapi_flags |= IDE_AFLAG_DRQ_INTERRUPT;
Linus Torvalds's avatar
Linus Torvalds committed
948
	/*
949 950
	 * We used to check revisions here. At this point however I'm giving up.
	 * Just assume they are all broken, its easier.
Linus Torvalds's avatar
Linus Torvalds committed
951
	 *
952 953 954 955
	 * The actual reason for the workarounds was likely a driver bug after
	 * all rather than a firmware bug, and the workaround below used to hide
	 * it. It should be fixed as of version 1.9, but to be on the safe side
	 * we'll leave the limitation below for the 2.2.x tree.
Linus Torvalds's avatar
Linus Torvalds committed
956
	 */
957
	if (!strncmp((char *)&id[ATA_ID_PROD], "IOMEGA ZIP 100 ATAPI", 20)) {
958
		drive->atapi_flags |= IDE_AFLAG_ZIP_DRIVE;
Linus Torvalds's avatar
Linus Torvalds committed
959 960 961 962 963 964
		/* This value will be visible in the /proc/ide/hdx/settings */
		floppy->ticks = IDEFLOPPY_TICKS_DELAY;
		blk_queue_max_sectors(drive->queue, 64);
	}

	/*
965 966 967
	 * Guess what? The IOMEGA Clik! drive also needs the above fix. It makes
	 * nasty clicking noises without it, so please don't remove this.
	 */
968
	if (strncmp((char *)&id[ATA_ID_PROD], "IOMEGA Clik!", 11) == 0) {
Linus Torvalds's avatar
Linus Torvalds committed
969
		blk_queue_max_sectors(drive->queue, 64);
970
		drive->atapi_flags |= IDE_AFLAG_CLIK_DRIVE;
Linus Torvalds's avatar
Linus Torvalds committed
971 972
	}

973
	(void) ide_floppy_get_capacity(drive);
974 975

	ide_proc_register_driver(drive, floppy->driver);
Linus Torvalds's avatar
Linus Torvalds committed
976 977
}

978
static void ide_floppy_remove(ide_drive_t *drive)
Linus Torvalds's avatar
Linus Torvalds committed
979 980 981 982
{
	idefloppy_floppy_t *floppy = drive->driver_data;
	struct gendisk *g = floppy->disk;

983
	ide_proc_unregister_driver(drive, floppy->driver);
Linus Torvalds's avatar
Linus Torvalds committed
984 985 986 987 988 989

	del_gendisk(g);

	ide_floppy_put(floppy);
}

990
static void idefloppy_cleanup_obj(struct kref *kref)
Linus Torvalds's avatar
Linus Torvalds committed
991 992 993 994 995 996 997 998 999 1000 1001
{
	struct ide_floppy_obj *floppy = to_ide_floppy(kref);
	ide_drive_t *drive = floppy->drive;
	struct gendisk *g = floppy->disk;

	drive->driver_data = NULL;
	g->private_data = NULL;
	put_disk(g);
	kfree(floppy);
}

1002
#ifdef CONFIG_IDE_PROC_FS
1003 1004
static int proc_idefloppy_read_capacity(char *page, char **start, off_t off,
		int count, int *eof, void *data)
Linus Torvalds's avatar
Linus Torvalds committed
1005 1006 1007 1008
{
	ide_drive_t*drive = (ide_drive_t *)data;
	int len;

1009 1010
	len = sprintf(page, "%llu\n", (long long)idefloppy_capacity(drive));
	PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
Linus Torvalds's avatar
Linus Torvalds committed
1011 1012 1013
}

static ide_proc_entry_t idefloppy_proc[] = {
1014 1015
	{ "capacity",	S_IFREG|S_IRUGO, proc_idefloppy_read_capacity,	NULL },
	{ "geometry",	S_IFREG|S_IRUGO, proc_ide_read_geometry,	NULL },
Linus Torvalds's avatar
Linus Torvalds committed
1016 1017
	{ NULL, 0, NULL, NULL }
};
1018
#endif	/* CONFIG_IDE_PROC_FS */
Linus Torvalds's avatar
Linus Torvalds committed
1019

1020
static int ide_floppy_probe(ide_drive_t *);
Linus Torvalds's avatar
Linus Torvalds committed
1021 1022

static ide_driver_t idefloppy_driver = {
1023
	.gen_driver = {
1024
		.owner		= THIS_MODULE,
1025 1026 1027
		.name		= "ide-floppy",
		.bus		= &ide_bus_type,
	},
1028 1029
	.probe			= ide_floppy_probe,
	.remove			= ide_floppy_remove,
Linus Torvalds's avatar
Linus Torvalds committed
1030 1031 1032
	.version		= IDEFLOPPY_VERSION,
	.media			= ide_floppy,
	.do_request		= idefloppy_do_request,
1033
	.end_request		= idefloppy_end_request,
Linus Torvalds's avatar
Linus Torvalds committed
1034
	.error			= __ide_error,
1035
#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds's avatar
Linus Torvalds committed
1036
	.proc			= idefloppy_proc,
1037
	.settings		= idefloppy_settings,
1038
#endif
Linus Torvalds's avatar
Linus Torvalds committed
1039 1040
};

1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
static void ide_floppy_set_media_lock(ide_drive_t *drive, int on)
{
	struct ide_atapi_pc pc;

	/* IOMEGA Clik! drives do not support lock/unlock commands */
	if ((drive->atapi_flags & IDE_AFLAG_CLIK_DRIVE) == 0) {
		idefloppy_create_prevent_cmd(&pc, on);
		(void)idefloppy_queue_pc_tail(drive, &pc);
	}
}

Linus Torvalds's avatar
Linus Torvalds committed
1052 1053 1054 1055 1056
static int idefloppy_open(struct inode *inode, struct file *filp)
{
	struct gendisk *disk = inode->i_bdev->bd_disk;
	struct ide_floppy_obj *floppy;
	ide_drive_t *drive;
1057
	struct ide_atapi_pc pc;
Linus Torvalds's avatar
Linus Torvalds committed
1058 1059
	int ret = 0;

1060
	debug_log("Reached %s\n", __func__);
Linus Torvalds's avatar
Linus Torvalds committed
1061

1062 1063
	floppy = ide_floppy_get(disk);
	if (!floppy)
Linus Torvalds's avatar
Linus Torvalds committed
1064 1065 1066 1067
		return -ENXIO;

	drive = floppy->drive;

1068
	floppy->openers++;
Linus Torvalds's avatar
Linus Torvalds committed
1069

1070
	if (floppy->openers == 1) {
1071
		drive->atapi_flags &= ~IDE_AFLAG_FORMAT_IN_PROGRESS;
Linus Torvalds's avatar
Linus Torvalds committed
1072 1073
		/* Just in case */

1074 1075 1076
		idefloppy_init_pc(&pc);
		pc.c[0] = GPCMD_TEST_UNIT_READY;

Linus Torvalds's avatar
Linus Torvalds committed
1077 1078 1079 1080 1081
		if (idefloppy_queue_pc_tail(drive, &pc)) {
			idefloppy_create_start_stop_cmd(&pc, 1);
			(void) idefloppy_queue_pc_tail(drive, &pc);
		}

1082
		if (ide_floppy_get_capacity(drive)
Linus Torvalds's avatar
Linus Torvalds committed
1083 1084
		   && (filp->f_flags & O_NDELAY) == 0
		    /*
1085 1086 1087 1088
		     * Allow O_NDELAY to open a drive without a disk, or with an
		     * unreadable disk, so that we can get the format capacity
		     * of the drive or begin the format - Sam
		     */
Linus Torvalds's avatar
Linus Torvalds committed
1089 1090 1091 1092 1093 1094 1095 1096 1097
		    ) {
			ret = -EIO;
			goto out_put_floppy;
		}

		if (floppy->wp && (filp->f_mode & 2)) {
			ret = -EROFS;
			goto out_put_floppy;
		}
1098

1099
		drive->atapi_flags |= IDE_AFLAG_MEDIA_CHANGED;
1100
		ide_floppy_set_media_lock(drive, 1);
Linus Torvalds's avatar
Linus Torvalds committed
1101
		check_disk_change(inode->i_bdev);
1102
	} else if (drive->atapi_flags & IDE_AFLAG_FORMAT_IN_PROGRESS) {
Linus Torvalds's avatar
Linus Torvalds committed
1103 1104 1105 1106 1107 1108
		ret = -EBUSY;
		goto out_put_floppy;
	}
	return 0;

out_put_floppy:
1109
	floppy->openers--;
Linus Torvalds's avatar
Linus Torvalds committed
1110 1111 1112 1113 1114 1115 1116 1117 1118
	ide_floppy_put(floppy);
	return ret;
}

static int idefloppy_release(struct inode *inode, struct file *filp)
{
	struct gendisk *disk = inode->i_bdev->bd_disk;
	struct ide_floppy_obj *floppy = ide_floppy_g(disk);
	ide_drive_t *drive = floppy->drive;
1119 1120

	debug_log("Reached %s\n", __func__);
Linus Torvalds's avatar
Linus Torvalds committed
1121

1122
	if (floppy->openers == 1) {
1123
		ide_floppy_set_media_lock(drive, 0);
1124
		drive->atapi_flags &= ~IDE_AFLAG_FORMAT_IN_PROGRESS;
Linus Torvalds's avatar
Linus Torvalds committed
1125
	}
1126 1127

	floppy->openers--;
Linus Torvalds's avatar
Linus Torvalds committed
1128 1129 1130 1131 1132 1133

	ide_floppy_put(floppy);

	return 0;
}

1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
static int idefloppy_getgeo(struct block_device *bdev, struct hd_geometry *geo)
{
	struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
	ide_drive_t *drive = floppy->drive;

	geo->heads = drive->bios_head;
	geo->sectors = drive->bios_sect;
	geo->cylinders = (u16)drive->bios_cyl; /* truncate */
	return 0;
}

1145 1146
static int ide_floppy_lockdoor(ide_drive_t *drive, struct ide_atapi_pc *pc,
			       unsigned long arg, unsigned int cmd)
1147
{
1148
	idefloppy_floppy_t *floppy = drive->driver_data;
1149
	int prevent = (arg && cmd != CDROMEJECT) ? 1 : 0;
1150

1151 1152 1153
	if (floppy->openers > 1)
		return -EBUSY;

1154
	ide_floppy_set_media_lock(drive, prevent);
1155 1156 1157 1158 1159 1160 1161 1162 1163

	if (cmd == CDROMEJECT) {
		idefloppy_create_start_stop_cmd(pc, 2);
		(void) idefloppy_queue_pc_tail(floppy->drive, pc);
	}

	return 0;
}

1164
static int ide_floppy_format_unit(ide_drive_t *drive, int __user *arg)
1165
{
1166
	idefloppy_floppy_t *floppy = drive->driver_data;
1167
	struct ide_atapi_pc pc;
1168
	int blocks, length, flags, err = 0;
1169 1170 1171

	if (floppy->openers > 1) {
		/* Don't format if someone is using the disk */
1172
		drive->atapi_flags &= ~IDE_AFLAG_FORMAT_IN_PROGRESS;
1173 1174 1175
		return -EBUSY;
	}

1176
	drive->atapi_flags |= IDE_AFLAG_FORMAT_IN_PROGRESS;
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199

	/*
	 * Send ATAPI_FORMAT_UNIT to the drive.
	 *
	 * Userland gives us the following structure:
	 *
	 * struct idefloppy_format_command {
	 *        int nblocks;
	 *        int blocksize;
	 *        int flags;
	 *        } ;
	 *
	 * flags is a bitmask, currently, the only defined flag is:
	 *
	 *        0x01 - verify media after format.
	 */
	if (get_user(blocks, arg) ||
			get_user(length, arg+1) ||
			get_user(flags, arg+2)) {
		err = -EFAULT;
		goto out;
	}

1200
	(void) idefloppy_get_sfrp_bit(drive);
1201 1202
	idefloppy_create_format_unit_cmd(&pc, blocks, length, flags);

1203
	if (idefloppy_queue_pc_tail(drive, &pc))
1204 1205 1206 1207
		err = -EIO;

out:
	if (err)
1208
		drive->atapi_flags &= ~IDE_AFLAG_FORMAT_IN_PROGRESS;
1209 1210 1211
	return err;
}

1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229
static int ide_floppy_format_ioctl(ide_drive_t *drive, struct file *file,
				   unsigned int cmd, void __user *argp)
{
	switch (cmd) {
	case IDEFLOPPY_IOCTL_FORMAT_SUPPORTED:
		return 0;
	case IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY:
		return ide_floppy_get_format_capacities(drive, argp);
	case IDEFLOPPY_IOCTL_FORMAT_START:
		if (!(file->f_mode & 2))
			return -EPERM;
		return ide_floppy_format_unit(drive, (int __user *)argp);
	case IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS:
		return ide_floppy_get_format_progress(drive, argp);
	default:
		return -ENOTTY;
	}
}
1230

Linus Torvalds's avatar
Linus Torvalds committed
1231 1232 1233 1234 1235 1236
static int idefloppy_ioctl(struct inode *inode, struct file *file,
			unsigned int cmd, unsigned long arg)
{
	struct block_device *bdev = inode->i_bdev;
	struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
	ide_drive_t *drive = floppy->drive;
1237
	struct ide_atapi_pc pc;
Linus Torvalds's avatar
Linus Torvalds committed
1238
	void __user *argp = (void __user *)arg;
1239
	int err;
Linus Torvalds's avatar
Linus Torvalds committed
1240

1241
	if (cmd == CDROMEJECT || cmd == CDROM_LOCKDOOR)
1242
		return ide_floppy_lockdoor(drive, &pc, arg, cmd);
Linus Torvalds's avatar
Linus Torvalds committed
1243

1244 1245 1246
	err = ide_floppy_format_ioctl(drive, file, cmd, argp);
	if (err != -ENOTTY)
		return err;
1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259

	/*
	 * skip SCSI_IOCTL_SEND_COMMAND (deprecated)
	 * and CDROM_SEND_PACKET (legacy) ioctls
	 */
	if (cmd != CDROM_SEND_PACKET && cmd != SCSI_IOCTL_SEND_COMMAND)
		err = scsi_cmd_ioctl(file, bdev->bd_disk->queue,
					bdev->bd_disk, cmd, argp);

	if (err == -ENOTTY)
		err = generic_ide_ioctl(drive, file, bdev, cmd, arg);

	return err;
Linus Torvalds's avatar
Linus Torvalds committed
1260 1261 1262 1263 1264 1265
}

static int idefloppy_media_changed(struct gendisk *disk)
{
	struct ide_floppy_obj *floppy = ide_floppy_g(disk);
	ide_drive_t *drive = floppy->drive;
1266
	int ret;
Linus Torvalds's avatar
Linus Torvalds committed
1267 1268 1269 1270 1271 1272

	/* do not scan partitions twice if this is a removable device */
	if (drive->attach) {
		drive->attach = 0;
		return 0;
	}
1273 1274
	ret = !!(drive->atapi_flags & IDE_AFLAG_MEDIA_CHANGED);
	drive->atapi_flags &= ~IDE_AFLAG_MEDIA_CHANGED;
1275
	return ret;
Linus Torvalds's avatar
Linus Torvalds committed
1276 1277 1278 1279 1280 1281 1282 1283 1284 1285
}

static int idefloppy_revalidate_disk(struct gendisk *disk)
{
	struct ide_floppy_obj *floppy = ide_floppy_g(disk);
	set_capacity(disk, idefloppy_capacity(floppy->drive));
	return 0;
}

static struct block_device_operations idefloppy_ops = {
1286 1287 1288 1289 1290 1291 1292
	.owner			= THIS_MODULE,
	.open			= idefloppy_open,
	.release		= idefloppy_release,
	.ioctl			= idefloppy_ioctl,
	.getgeo			= idefloppy_getgeo,
	.media_changed		= idefloppy_media_changed,
	.revalidate_disk	= idefloppy_revalidate_disk
Linus Torvalds's avatar
Linus Torvalds committed
1293 1294
};

1295
static int ide_floppy_probe(ide_drive_t *drive)
Linus Torvalds's avatar
Linus Torvalds committed
1296 1297 1298 1299 1300 1301
{
	idefloppy_floppy_t *floppy;
	struct gendisk *g;

	if (!strstr("ide-floppy", drive->driver_req))
		goto failed;
1302

Linus Torvalds's avatar
Linus Torvalds committed
1303 1304
	if (drive->media != ide_floppy)
		goto failed;
1305

1306
	if (!ide_check_atapi_device(drive, DRV_NAME)) {
1307 1308
		printk(KERN_ERR "ide-floppy: %s: not supported by this version"
				" of ide-floppy\n", drive->name);
Linus Torvalds's avatar
Linus Torvalds committed
1309 1310
		goto failed;
	}
1311 1312 1313 1314
	floppy = kzalloc(sizeof(idefloppy_floppy_t), GFP_KERNEL);
	if (!floppy) {
		printk(KERN_ERR "ide-floppy: %s: Can't allocate a floppy"
				" structure\n", drive->name);
Linus Torvalds's avatar
Linus Torvalds committed
1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333
		goto failed;
	}

	g = alloc_disk(1 << PARTN_BITS);
	if (!g)
		goto out_free_floppy;

	ide_init_disk(g, drive);

	kref_init(&floppy->kref);

	floppy->drive = drive;
	floppy->driver = &idefloppy_driver;
	floppy->disk = g;

	g->private_data = &floppy->driver;

	drive->driver_data = floppy;

1334
	idefloppy_setup(drive, floppy);
1335

Linus Torvalds's avatar
Linus Torvalds committed
1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346
	g->minors = 1 << PARTN_BITS;
	g->driverfs_dev = &drive->gendev;
	g->flags = drive->removable ? GENHD_FL_REMOVABLE : 0;
	g->fops = &idefloppy_ops;
	drive->attach = 1;
	add_disk(g);
	return 0;

out_free_floppy:
	kfree(floppy);
failed:
1347
	return -ENODEV;
Linus Torvalds's avatar
Linus Torvalds committed
1348 1349
}

1350
static void __exit idefloppy_exit(void)
Linus Torvalds's avatar
Linus Torvalds committed
1351
{
1352
	driver_unregister(&idefloppy_driver.gen_driver);
Linus Torvalds's avatar
Linus Torvalds committed
1353 1354
}

1355
static int __init idefloppy_init(void)
Linus Torvalds's avatar
Linus Torvalds committed
1356 1357
{
	printk("ide-floppy driver " IDEFLOPPY_VERSION "\n");
1358
	return driver_register(&idefloppy_driver.gen_driver);
Linus Torvalds's avatar
Linus Torvalds committed
1359 1360
}

1361
MODULE_ALIAS("ide:*m-floppy*");
Linus Torvalds's avatar
Linus Torvalds committed
1362 1363 1364
module_init(idefloppy_init);
module_exit(idefloppy_exit);
MODULE_LICENSE("GPL");
1365 1366
MODULE_DESCRIPTION("ATAPI FLOPPY Driver");