ide-floppy.c 27.1 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 50
#include "ide-floppy.h"

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

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

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


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

/*
69 70
 * 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
71 72 73
 */
#define IDEFLOPPY_MAX_PC_RETRIES	3

74
/* format capacities descriptor codes */
Linus Torvalds's avatar
Linus Torvalds committed
75 76 77 78 79
#define CAPACITY_INVALID	0x00
#define CAPACITY_UNFORMATTED	0x01
#define CAPACITY_CURRENT	0x02
#define CAPACITY_NO_CARTRIDGE	0x03

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

82
/* Error code returned in rq->errors to the higher part of the driver. */
Linus Torvalds's avatar
Linus Torvalds committed
83 84
#define	IDEFLOPPY_ERROR_GENERAL		101

85
static DEFINE_MUTEX(idefloppy_ref_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
86 87 88 89 90 91

#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)

92 93
static void idefloppy_cleanup_obj(struct kref *);

Linus Torvalds's avatar
Linus Torvalds committed
94 95 96 97
static struct ide_floppy_obj *ide_floppy_get(struct gendisk *disk)
{
	struct ide_floppy_obj *floppy = NULL;

98
	mutex_lock(&idefloppy_ref_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
99
	floppy = ide_floppy_g(disk);
100
	if (floppy) {
101
		if (ide_device_get(floppy->drive))
102
			floppy = NULL;
103 104
		else
			kref_get(&floppy->kref);
105
	}
106
	mutex_unlock(&idefloppy_ref_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
107 108 109 110 111
	return floppy;
}

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

114
	mutex_lock(&idefloppy_ref_mutex);
115
	kref_put(&floppy->kref, idefloppy_cleanup_obj);
116
	ide_device_put(drive);
117
	mutex_unlock(&idefloppy_ref_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
118 119 120
}

/*
121 122
 * 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
123
 */
124
static int idefloppy_end_request(ide_drive_t *drive, int uptodate, int nsecs)
Linus Torvalds's avatar
Linus Torvalds committed
125 126 127 128 129
{
	idefloppy_floppy_t *floppy = drive->driver_data;
	struct request *rq = HWGROUP(drive)->rq;
	int error;

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

	switch (uptodate) {
133 134 135
	case 0: error = IDEFLOPPY_ERROR_GENERAL; break;
	case 1: error = 0; break;
	default: error = uptodate;
Linus Torvalds's avatar
Linus Torvalds committed
136 137 138 139 140 141
	}
	if (error)
		floppy->failed_pc = NULL;
	/* Why does this happen? */
	if (!rq)
		return 0;
142
	if (!blk_special_request(rq)) {
Linus Torvalds's avatar
Linus Torvalds committed
143 144 145 146 147 148 149 150 151 152
		/* 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;
}

153 154
static void idefloppy_update_buffers(ide_drive_t *drive,
				struct ide_atapi_pc *pc)
Linus Torvalds's avatar
Linus Torvalds committed
155 156 157 158 159
{
	struct request *rq = pc->rq;
	struct bio *bio = rq->bio;

	while ((bio = rq->bio) != NULL)
160
		idefloppy_end_request(drive, 1, 0);
Linus Torvalds's avatar
Linus Torvalds committed
161 162
}

163
static void ide_floppy_callback(ide_drive_t *drive)
Linus Torvalds's avatar
Linus Torvalds committed
164 165
{
	idefloppy_floppy_t *floppy = drive->driver_data;
166 167
	struct ide_atapi_pc *pc = floppy->pc;
	int uptodate = pc->error ? 0 : 1;
Linus Torvalds's avatar
Linus Torvalds committed
168

169 170
	debug_log("Reached %s\n", __func__);

171 172 173
	if (floppy->failed_pc == pc)
		floppy->failed_pc = NULL;

174 175 176 177 178
	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;
179

180 181 182 183 184 185
		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;
186

187 188
			if (floppy->failed_pc)
				debug_log("pc = %x, ", floppy->failed_pc->c[0]);
189

190 191 192 193 194 195
			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
196

197
	idefloppy_end_request(drive, uptodate, 0);
Linus Torvalds's avatar
Linus Torvalds committed
198 199
}

200
void ide_floppy_create_request_sense_cmd(struct ide_atapi_pc *pc)
Linus Torvalds's avatar
Linus Torvalds committed
201
{
202
	ide_init_pc(pc);
203
	pc->c[0] = GPCMD_REQUEST_SENSE;
Linus Torvalds's avatar
Linus Torvalds committed
204
	pc->c[4] = 255;
205
	pc->req_xfer = 18;
Linus Torvalds's avatar
Linus Torvalds committed
206 207 208
}

/*
209 210
 * 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
211
 */
212
static void idefloppy_retry_pc(ide_drive_t *drive)
Linus Torvalds's avatar
Linus Torvalds committed
213
{
214 215
	struct ide_floppy_obj *floppy = drive->driver_data;
	struct request *rq = &floppy->request_sense_rq;
216
	struct ide_atapi_pc *pc = &floppy->request_sense_pc;
Linus Torvalds's avatar
Linus Torvalds committed
217

218
	(void)ide_read_error(drive);
219
	ide_floppy_create_request_sense_cmd(pc);
220
	ide_queue_pc_head(drive, floppy->disk, pc, rq);
Linus Torvalds's avatar
Linus Torvalds committed
221 222
}

223
/* The usual interrupt handler called during a packet command. */
224
static ide_startstop_t idefloppy_pc_intr(ide_drive_t *drive)
Linus Torvalds's avatar
Linus Torvalds committed
225 226 227
{
	idefloppy_floppy_t *floppy = drive->driver_data;

228 229
	return ide_pc_intr(drive, floppy->pc, idefloppy_pc_intr,
			   IDEFLOPPY_WAIT_CMD, NULL, idefloppy_update_buffers,
230
			   idefloppy_retry_pc, NULL, ide_io_buffers);
Linus Torvalds's avatar
Linus Torvalds committed
231 232 233
}

/*
234 235 236 237
 * 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
238
 */
239
static int idefloppy_transfer_pc(ide_drive_t *drive)
Linus Torvalds's avatar
Linus Torvalds committed
240 241 242 243
{
	idefloppy_floppy_t *floppy = drive->driver_data;

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

Linus Torvalds's avatar
Linus Torvalds committed
246 247 248 249
	/* Timeout for the packet command */
	return IDEFLOPPY_WAIT_CMD;
}

250 251 252 253 254 255 256

/*
 * 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
257 258
{
	idefloppy_floppy_t *floppy = drive->driver_data;
259
	struct ide_atapi_pc *pc = floppy->pc;
260 261
	ide_expiry_t *expiry;
	unsigned int timeout;
Linus Torvalds's avatar
Linus Torvalds committed
262

263
	/*
Linus Torvalds's avatar
Linus Torvalds committed
264 265 266 267 268 269 270
	 * 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.
	 */
271
	if (drive->atapi_flags & IDE_AFLAG_ZIP_DRIVE) {
272
		timeout = floppy->ticks;
273
		expiry = &idefloppy_transfer_pc;
274 275 276 277 278
	} else {
		timeout = IDEFLOPPY_WAIT_CMD;
		expiry = NULL;
	}

279
	return ide_transfer_pc(drive, pc, idefloppy_pc_intr, timeout, expiry);
Linus Torvalds's avatar
Linus Torvalds committed
280 281
}

282
static void ide_floppy_report_error(idefloppy_floppy_t *floppy,
283
				    struct ide_atapi_pc *pc)
Linus Torvalds's avatar
Linus Torvalds committed
284
{
285
	/* supress error messages resulting from Medium not present */
Linus Torvalds's avatar
Linus Torvalds committed
286 287 288
	if (floppy->sense_key == 0x02 &&
	    floppy->asc       == 0x3a &&
	    floppy->ascq      == 0x00)
289 290 291 292 293 294 295
		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
296 297
}

298
static ide_startstop_t idefloppy_issue_pc(ide_drive_t *drive,
299
		struct ide_atapi_pc *pc)
Linus Torvalds's avatar
Linus Torvalds committed
300 301 302 303
{
	idefloppy_floppy_t *floppy = drive->driver_data;

	if (floppy->failed_pc == NULL &&
304
	    pc->c[0] != GPCMD_REQUEST_SENSE)
Linus Torvalds's avatar
Linus Torvalds committed
305 306 307 308
		floppy->failed_pc = pc;
	/* Set the current packet command */
	floppy->pc = pc;

309
	if (pc->retries > IDEFLOPPY_MAX_PC_RETRIES) {
310
		if (!(pc->flags & PC_FLAG_SUPPRESS_ERROR))
311 312 313 314
			ide_floppy_report_error(floppy, pc);
		/* Giving up */
		pc->error = IDEFLOPPY_ERROR_GENERAL;

Linus Torvalds's avatar
Linus Torvalds committed
315
		floppy->failed_pc = NULL;
316
		drive->pc_callback(drive);
Linus Torvalds's avatar
Linus Torvalds committed
317 318 319
		return ide_stopped;
	}

320
	debug_log("Retry number - %d\n", pc->retries);
Linus Torvalds's avatar
Linus Torvalds committed
321 322 323

	pc->retries++;

324
	return ide_issue_pc(drive, pc, idefloppy_start_pc_transfer,
325
			    IDEFLOPPY_WAIT_CMD, NULL);
Linus Torvalds's avatar
Linus Torvalds committed
326 327
}

328
static void idefloppy_create_prevent_cmd(struct ide_atapi_pc *pc, int prevent)
Linus Torvalds's avatar
Linus Torvalds committed
329
{
330
	debug_log("creating prevent removal command, prevent = %d\n", prevent);
Linus Torvalds's avatar
Linus Torvalds committed
331

332
	ide_init_pc(pc);
333
	pc->c[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
Linus Torvalds's avatar
Linus Torvalds committed
334 335 336
	pc->c[4] = prevent;
}

337
void ide_floppy_create_read_capacity_cmd(struct ide_atapi_pc *pc)
Linus Torvalds's avatar
Linus Torvalds committed
338
{
339
	ide_init_pc(pc);
340
	pc->c[0] = GPCMD_READ_FORMAT_CAPACITIES;
Linus Torvalds's avatar
Linus Torvalds committed
341 342
	pc->c[7] = 255;
	pc->c[8] = 255;
343
	pc->req_xfer = 255;
Linus Torvalds's avatar
Linus Torvalds committed
344 345
}

346
/* A mode sense command is used to "sense" floppy parameters. */
347
void ide_floppy_create_mode_sense_cmd(struct ide_atapi_pc *pc, u8 page_code)
Linus Torvalds's avatar
Linus Torvalds committed
348
{
349
	u16 length = 8; /* sizeof(Mode Parameter Header) = 8 Bytes */
350

351
	ide_init_pc(pc);
352
	pc->c[0] = GPCMD_MODE_SENSE_10;
Linus Torvalds's avatar
Linus Torvalds committed
353
	pc->c[1] = 0;
354
	pc->c[2] = page_code;
Linus Torvalds's avatar
Linus Torvalds committed
355 356

	switch (page_code) {
357 358 359 360 361 362 363 364
	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
365 366
				"in create_mode_sense_cmd\n");
	}
367
	put_unaligned(cpu_to_be16(length), (u16 *) &pc->c[7]);
368
	pc->req_xfer = length;
Linus Torvalds's avatar
Linus Torvalds committed
369 370
}

371
static void idefloppy_create_start_stop_cmd(struct ide_atapi_pc *pc, int start)
Linus Torvalds's avatar
Linus Torvalds committed
372
{
373
	ide_init_pc(pc);
374
	pc->c[0] = GPCMD_START_STOP_UNIT;
Linus Torvalds's avatar
Linus Torvalds committed
375 376 377
	pc->c[4] = start;
}

378
static void idefloppy_create_rw_cmd(idefloppy_floppy_t *floppy,
379
				    struct ide_atapi_pc *pc, struct request *rq,
380
				    unsigned long sector)
Linus Torvalds's avatar
Linus Torvalds committed
381 382 383 384 385
{
	int block = sector / floppy->bs_factor;
	int blocks = rq->nr_sectors / floppy->bs_factor;
	int cmd = rq_data_dir(rq);

386
	debug_log("create_rw10_cmd: block == %d, blocks == %d\n",
Linus Torvalds's avatar
Linus Torvalds committed
387 388
		block, blocks);

389
	ide_init_pc(pc);
390 391
	pc->c[0] = cmd == READ ? GPCMD_READ_10 : GPCMD_WRITE_10;
	put_unaligned(cpu_to_be16(blocks), (unsigned short *)&pc->c[7]);
392
	put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[2]);
393

394 395
	memcpy(rq->cmd, pc->c, 12);

Linus Torvalds's avatar
Linus Torvalds committed
396
	pc->rq = rq;
397
	pc->b_count = 0;
398
	if (rq->cmd_flags & REQ_RW)
399
		pc->flags |= PC_FLAG_WRITING;
400 401
	pc->buf = NULL;
	pc->req_xfer = pc->buf_size = blocks * floppy->block_size;
402
	pc->flags |= PC_FLAG_DMA_OK;
Linus Torvalds's avatar
Linus Torvalds committed
403 404
}

405
static void idefloppy_blockpc_cmd(idefloppy_floppy_t *floppy,
406
		struct ide_atapi_pc *pc, struct request *rq)
Linus Torvalds's avatar
Linus Torvalds committed
407
{
408
	ide_init_pc(pc);
Linus Torvalds's avatar
Linus Torvalds committed
409
	memcpy(pc->c, rq->cmd, sizeof(pc->c));
410
	pc->rq = rq;
411
	pc->b_count = 0;
412
	if (rq->data_len && rq_data_dir(rq) == WRITE)
413
		pc->flags |= PC_FLAG_WRITING;
414
	pc->buf = rq->data;
415
	if (rq->bio)
416
		pc->flags |= PC_FLAG_DMA_OK;
417 418 419 420
	/*
	 * possibly problematic, doesn't look like ide-floppy correctly
	 * handled scattered requests if dma fails...
	 */
421
	pc->req_xfer = pc->buf_size = rq->data_len;
Linus Torvalds's avatar
Linus Torvalds committed
422 423
}

424 425
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
426 427
{
	idefloppy_floppy_t *floppy = drive->driver_data;
428
	ide_hwif_t *hwif = drive->hwif;
429
	struct ide_atapi_pc *pc;
Linus Torvalds's avatar
Linus Torvalds committed
430 431
	unsigned long block = (unsigned long)block_s;

432 433 434 435 436 437 438
	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
439 440

	if (rq->errors >= ERROR_MAX) {
441 442
		if (floppy->failed_pc)
			ide_floppy_report_error(floppy, floppy->failed_pc);
Linus Torvalds's avatar
Linus Torvalds committed
443 444 445
		else
			printk(KERN_ERR "ide-floppy: %s: I/O error\n",
				drive->name);
446
		idefloppy_end_request(drive, 0, 0);
Linus Torvalds's avatar
Linus Torvalds committed
447 448
		return ide_stopped;
	}
449
	if (blk_fs_request(rq)) {
Linus Torvalds's avatar
Linus Torvalds committed
450 451
		if (((long)rq->sector % floppy->bs_factor) ||
		    (rq->nr_sectors % floppy->bs_factor)) {
452 453
			printk(KERN_ERR "%s: unsupported r/w request size\n",
					drive->name);
454
			idefloppy_end_request(drive, 0, 0);
Linus Torvalds's avatar
Linus Torvalds committed
455 456
			return ide_stopped;
		}
457
		pc = &floppy->queued_pc;
Linus Torvalds's avatar
Linus Torvalds committed
458
		idefloppy_create_rw_cmd(floppy, pc, rq, block);
459
	} else if (blk_special_request(rq)) {
460
		pc = (struct ide_atapi_pc *) rq->buffer;
461
	} else if (blk_pc_request(rq)) {
462
		pc = &floppy->queued_pc;
463
		idefloppy_blockpc_cmd(floppy, pc, rq);
Linus Torvalds's avatar
Linus Torvalds committed
464 465 466
	} else {
		blk_dump_rq_flags(rq,
			"ide-floppy: unsupported command in queue");
467
		idefloppy_end_request(drive, 0, 0);
Linus Torvalds's avatar
Linus Torvalds committed
468 469 470
		return ide_stopped;
	}

471 472 473 474 475 476
	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
477
	pc->rq = rq;
478

Linus Torvalds's avatar
Linus Torvalds committed
479 480 481 482
	return idefloppy_issue_pc(drive, pc);
}

/*
483 484
 * 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
485
 */
486
static int ide_floppy_get_flexible_disk_page(ide_drive_t *drive)
Linus Torvalds's avatar
Linus Torvalds committed
487 488
{
	idefloppy_floppy_t *floppy = drive->driver_data;
489
	struct gendisk *disk = floppy->disk;
490
	struct ide_atapi_pc pc;
491
	u8 *page;
Linus Torvalds's avatar
Linus Torvalds committed
492
	int capacity, lba_capacity;
493 494
	u16 transfer_rate, sector_size, cyls, rpm;
	u8 heads, sectors;
Linus Torvalds's avatar
Linus Torvalds committed
495

496
	ide_floppy_create_mode_sense_cmd(&pc, IDEFLOPPY_FLEXIBLE_DISK_PAGE);
497

498
	if (ide_queue_pc_tail(drive, disk, &pc)) {
499 500
		printk(KERN_ERR "ide-floppy: Can't get flexible disk page"
				" parameters\n");
Linus Torvalds's avatar
Linus Torvalds committed
501 502
		return 1;
	}
503 504 505 506 507 508 509 510

	if (pc.buf[3] & 0x80)
		drive->atapi_flags |= IDE_AFLAG_WP;
	else
		drive->atapi_flags &= ~IDE_AFLAG_WP;

	set_disk_ro(disk, !!(drive->atapi_flags & IDE_AFLAG_WP));

511
	page = &pc.buf[8];
512

513 514 515 516
	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]);
517 518
	heads         = pc.buf[8 + 4];
	sectors       = pc.buf[8 + 5];
519 520 521 522

	capacity = cyls * heads * sectors * sector_size;

	if (memcmp(page, &floppy->flexible_disk_page, 32))
Linus Torvalds's avatar
Linus Torvalds committed
523 524
		printk(KERN_INFO "%s: %dkB, %d/%d/%d CHS, %d kBps, "
				"%d sector size, %d rpm\n",
525 526 527 528 529 530 531
				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
532
	lba_capacity = floppy->blocks * floppy->block_size;
533

Linus Torvalds's avatar
Linus Torvalds committed
534 535 536 537
	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);
538 539
		floppy->blocks = floppy->block_size ?
			capacity / floppy->block_size : 0;
Linus Torvalds's avatar
Linus Torvalds committed
540 541 542 543 544
	}
	return 0;
}

/*
545 546
 * Determine if a media is present in the floppy drive, and if so, its LBA
 * capacity.
Linus Torvalds's avatar
Linus Torvalds committed
547
 */
548
static int ide_floppy_get_capacity(ide_drive_t *drive)
Linus Torvalds's avatar
Linus Torvalds committed
549 550
{
	idefloppy_floppy_t *floppy = drive->driver_data;
551
	struct gendisk *disk = floppy->disk;
552
	struct ide_atapi_pc pc;
553 554 555 556
	u8 *cap_desc;
	u8 header_len, desc_cnt;
	int i, rc = 1, blocks, length;

Linus Torvalds's avatar
Linus Torvalds committed
557 558
	drive->bios_cyl = 0;
	drive->bios_head = drive->bios_sect = 0;
559 560
	floppy->blocks = 0;
	floppy->bs_factor = 1;
Linus Torvalds's avatar
Linus Torvalds committed
561 562
	set_capacity(floppy->disk, 0);

563
	ide_floppy_create_read_capacity_cmd(&pc);
564
	if (ide_queue_pc_tail(drive, disk, &pc)) {
Linus Torvalds's avatar
Linus Torvalds committed
565 566 567
		printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
		return 1;
	}
568 569
	header_len = pc.buf[3];
	cap_desc = &pc.buf[4];
570 571 572 573 574
	desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */

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

575 576
		blocks = be32_to_cpup((__be32 *)&pc.buf[desc_start]);
		length = be16_to_cpup((__be16 *)&pc.buf[desc_start + 6]);
577 578 579

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

581 582 583 584 585
		if (i)
			continue;
		/*
		 * the code below is valid only for the 1st descriptor, ie i=0
		 */
Linus Torvalds's avatar
Linus Torvalds committed
586

587
		switch (pc.buf[desc_start + 4] & 0x03) {
Linus Torvalds's avatar
Linus Torvalds committed
588 589
		/* Clik! drive returns this instead of CAPACITY_CURRENT */
		case CAPACITY_UNFORMATTED:
590
			if (!(drive->atapi_flags & IDE_AFLAG_CLIK_DRIVE))
591
				/*
Linus Torvalds's avatar
Linus Torvalds committed
592 593 594 595 596 597
				 * If it is not a clik drive, break out
				 * (maintains previous driver behaviour)
				 */
				break;
		case CAPACITY_CURRENT:
			/* Normal Zip/LS-120 disks */
598
			if (memcmp(cap_desc, &floppy->cap_desc, 8))
Linus Torvalds's avatar
Linus Torvalds committed
599 600 601
				printk(KERN_INFO "%s: %dkB, %d blocks, %d "
					"sector size\n", drive->name,
					blocks * length / 1024, blocks, length);
602 603
			memcpy(&floppy->cap_desc, cap_desc, 8);

Linus Torvalds's avatar
Linus Torvalds committed
604 605 606 607
			if (!length || length % 512) {
				printk(KERN_NOTICE "%s: %d bytes block size "
					"not supported\n", drive->name, length);
			} else {
608 609 610 611 612
				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
613 614 615
						"512 bytes block size not "
						"fully supported\n",
						drive->name);
616
				rc = 0;
Linus Torvalds's avatar
Linus Torvalds committed
617 618 619 620 621 622 623 624 625 626 627 628 629 630
			}
			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;
		}
631
		debug_log("Descriptor 0 Code: %d\n",
632
			  pc.buf[desc_start + 4] & 0x03);
Linus Torvalds's avatar
Linus Torvalds committed
633 634 635
	}

	/* Clik! disk does not support get_flexible_disk_page */
636
	if (!(drive->atapi_flags & IDE_AFLAG_CLIK_DRIVE))
637
		(void) ide_floppy_get_flexible_disk_page(drive);
Linus Torvalds's avatar
Linus Torvalds committed
638

639 640
	set_capacity(disk, floppy->blocks * floppy->bs_factor);

Linus Torvalds's avatar
Linus Torvalds committed
641 642 643
	return rc;
}

644
static sector_t idefloppy_capacity(ide_drive_t *drive)
Linus Torvalds's avatar
Linus Torvalds committed
645 646 647 648 649 650 651
{
	idefloppy_floppy_t *floppy = drive->driver_data;
	unsigned long capacity = floppy->blocks * floppy->bs_factor;

	return capacity;
}

652
#ifdef CONFIG_IDE_PROC_FS
653 654 655 656 657
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
658 659
{
	idefloppy_floppy_t *floppy = drive->driver_data;
660 661
	return floppy->ticks;
}
Linus Torvalds's avatar
Linus Torvalds committed
662

663 664 665 666 667
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
668
}
669 670 671 672 673 674 675 676 677 678

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
};
679
#endif
Linus Torvalds's avatar
Linus Torvalds committed
680

681
static void idefloppy_setup(ide_drive_t *drive, idefloppy_floppy_t *floppy)
Linus Torvalds's avatar
Linus Torvalds committed
682
{
683
	u16 *id = drive->id;
684
	u8 gcw[2];
Linus Torvalds's avatar
Linus Torvalds committed
685

686
	*((u16 *)&gcw) = id[ATA_ID_CONFIG];
687

688
	drive->pc_callback = ide_floppy_callback;
689 690

	if (((gcw[0] & 0x60) >> 5) == 1)
691
		drive->atapi_flags |= IDE_AFLAG_DRQ_INTERRUPT;
Linus Torvalds's avatar
Linus Torvalds committed
692
	/*
693 694
	 * 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
695
	 *
696 697 698 699
	 * 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
700
	 */
701
	if (!strncmp((char *)&id[ATA_ID_PROD], "IOMEGA ZIP 100 ATAPI", 20)) {
702
		drive->atapi_flags |= IDE_AFLAG_ZIP_DRIVE;
Linus Torvalds's avatar
Linus Torvalds committed
703 704 705 706 707 708
		/* This value will be visible in the /proc/ide/hdx/settings */
		floppy->ticks = IDEFLOPPY_TICKS_DELAY;
		blk_queue_max_sectors(drive->queue, 64);
	}

	/*
709 710 711
	 * Guess what? The IOMEGA Clik! drive also needs the above fix. It makes
	 * nasty clicking noises without it, so please don't remove this.
	 */
712
	if (strncmp((char *)&id[ATA_ID_PROD], "IOMEGA Clik!", 11) == 0) {
Linus Torvalds's avatar
Linus Torvalds committed
713
		blk_queue_max_sectors(drive->queue, 64);
714
		drive->atapi_flags |= IDE_AFLAG_CLIK_DRIVE;
Linus Torvalds's avatar
Linus Torvalds committed
715 716
	}

717
	(void) ide_floppy_get_capacity(drive);
718 719

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

722
static void ide_floppy_remove(ide_drive_t *drive)
Linus Torvalds's avatar
Linus Torvalds committed
723 724 725 726
{
	idefloppy_floppy_t *floppy = drive->driver_data;
	struct gendisk *g = floppy->disk;

727
	ide_proc_unregister_driver(drive, floppy->driver);
Linus Torvalds's avatar
Linus Torvalds committed
728 729 730 731 732 733

	del_gendisk(g);

	ide_floppy_put(floppy);
}

734
static void idefloppy_cleanup_obj(struct kref *kref)
Linus Torvalds's avatar
Linus Torvalds committed
735 736 737 738 739 740 741 742 743 744 745
{
	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);
}

746
#ifdef CONFIG_IDE_PROC_FS
747 748
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
749 750 751 752
{
	ide_drive_t*drive = (ide_drive_t *)data;
	int len;

753 754
	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
755 756 757
}

static ide_proc_entry_t idefloppy_proc[] = {
758 759
	{ "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
760 761
	{ NULL, 0, NULL, NULL }
};
762
#endif	/* CONFIG_IDE_PROC_FS */
Linus Torvalds's avatar
Linus Torvalds committed
763

764
static int ide_floppy_probe(ide_drive_t *);
Linus Torvalds's avatar
Linus Torvalds committed
765 766

static ide_driver_t idefloppy_driver = {
767
	.gen_driver = {
768
		.owner		= THIS_MODULE,
769 770 771
		.name		= "ide-floppy",
		.bus		= &ide_bus_type,
	},
772 773
	.probe			= ide_floppy_probe,
	.remove			= ide_floppy_remove,
Linus Torvalds's avatar
Linus Torvalds committed
774 775 776
	.version		= IDEFLOPPY_VERSION,
	.media			= ide_floppy,
	.do_request		= idefloppy_do_request,
777
	.end_request		= idefloppy_end_request,
Linus Torvalds's avatar
Linus Torvalds committed
778
	.error			= __ide_error,
779
#ifdef CONFIG_IDE_PROC_FS
Linus Torvalds's avatar
Linus Torvalds committed
780
	.proc			= idefloppy_proc,
781
	.settings		= idefloppy_settings,
782
#endif
Linus Torvalds's avatar
Linus Torvalds committed
783 784
};

785 786
static void ide_floppy_set_media_lock(ide_drive_t *drive, int on)
{
787
	struct ide_floppy_obj *floppy = drive->driver_data;
788 789 790 791 792
	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);
793
		(void)ide_queue_pc_tail(drive, floppy->disk, &pc);
794 795 796
	}
}

Linus Torvalds's avatar
Linus Torvalds committed
797 798 799 800 801
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;
802
	struct ide_atapi_pc pc;
Linus Torvalds's avatar
Linus Torvalds committed
803 804
	int ret = 0;

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

807 808
	floppy = ide_floppy_get(disk);
	if (!floppy)
Linus Torvalds's avatar
Linus Torvalds committed
809 810 811 812
		return -ENXIO;

	drive = floppy->drive;

813
	floppy->openers++;
Linus Torvalds's avatar
Linus Torvalds committed
814

815
	if (floppy->openers == 1) {
816
		drive->atapi_flags &= ~IDE_AFLAG_FORMAT_IN_PROGRESS;
Linus Torvalds's avatar
Linus Torvalds committed
817 818
		/* Just in case */

819
		ide_init_pc(&pc);
820 821
		pc.c[0] = GPCMD_TEST_UNIT_READY;

822
		if (ide_queue_pc_tail(drive, disk, &pc)) {
Linus Torvalds's avatar
Linus Torvalds committed
823
			idefloppy_create_start_stop_cmd(&pc, 1);
824
			(void)ide_queue_pc_tail(drive, disk, &pc);
Linus Torvalds's avatar
Linus Torvalds committed
825 826
		}

827
		if (ide_floppy_get_capacity(drive)
Linus Torvalds's avatar
Linus Torvalds committed
828 829
		   && (filp->f_flags & O_NDELAY) == 0
		    /*
830 831 832 833
		     * 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
834 835 836 837 838
		    ) {
			ret = -EIO;
			goto out_put_floppy;
		}

839
		if ((drive->atapi_flags & IDE_AFLAG_WP) && (filp->f_mode & 2)) {
Linus Torvalds's avatar
Linus Torvalds committed
840 841 842
			ret = -EROFS;
			goto out_put_floppy;
		}
843

844
		drive->atapi_flags |= IDE_AFLAG_MEDIA_CHANGED;
845
		ide_floppy_set_media_lock(drive, 1);
Linus Torvalds's avatar
Linus Torvalds committed
846
		check_disk_change(inode->i_bdev);
847
	} else if (drive->atapi_flags & IDE_AFLAG_FORMAT_IN_PROGRESS) {
Linus Torvalds's avatar
Linus Torvalds committed
848 849 850 851 852 853
		ret = -EBUSY;
		goto out_put_floppy;
	}
	return 0;

out_put_floppy:
854
	floppy->openers--;
Linus Torvalds's avatar
Linus Torvalds committed
855 856 857 858 859 860 861 862 863
	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;
864 865

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

867
	if (floppy->openers == 1) {
868
		ide_floppy_set_media_lock(drive, 0);
869
		drive->atapi_flags &= ~IDE_AFLAG_FORMAT_IN_PROGRESS;
Linus Torvalds's avatar
Linus Torvalds committed
870
	}
871 872

	floppy->openers--;
Linus Torvalds's avatar
Linus Torvalds committed
873 874 875 876 877 878

	ide_floppy_put(floppy);

	return 0;
}

879 880 881 882 883 884 885 886 887 888 889
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;
}

890 891
static int ide_floppy_lockdoor(ide_drive_t *drive, struct ide_atapi_pc *pc,
			       unsigned long arg, unsigned int cmd)
892
{
893
	idefloppy_floppy_t *floppy = drive->driver_data;
894
	int prevent = (arg && cmd != CDROMEJECT) ? 1 : 0;
895

896 897 898
	if (floppy->openers > 1)
		return -EBUSY;

899
	ide_floppy_set_media_lock(drive, prevent);
900 901 902

	if (cmd == CDROMEJECT) {
		idefloppy_create_start_stop_cmd(pc, 2);
903
		(void)ide_queue_pc_tail(drive, floppy->disk, pc);
904 905 906 907 908
	}

	return 0;
}

Linus Torvalds's avatar
Linus Torvalds committed
909 910 911 912 913 914
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;
915
	struct ide_atapi_pc pc;
Linus Torvalds's avatar
Linus Torvalds committed
916
	void __user *argp = (void __user *)arg;
917
	int err;
Linus Torvalds's avatar
Linus Torvalds committed
918

919
	if (cmd == CDROMEJECT || cmd == CDROM_LOCKDOOR)
920
		return ide_floppy_lockdoor(drive, &pc, arg, cmd);
Linus Torvalds's avatar
Linus Torvalds committed
921

922 923 924
	err = ide_floppy_format_ioctl(drive, file, cmd, argp);
	if (err != -ENOTTY)
		return err;
925 926 927 928 929 930 931 932 933 934 935 936 937

	/*
	 * 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
938 939 940 941 942 943
}

static int idefloppy_media_changed(struct gendisk *disk)
{
	struct ide_floppy_obj *floppy = ide_floppy_g(disk);
	ide_drive_t *drive = floppy->drive;
944
	int ret;
Linus Torvalds's avatar
Linus Torvalds committed
945 946 947 948 949 950

	/* do not scan partitions twice if this is a removable device */
	if (drive->attach) {
		drive->attach = 0;
		return 0;
	}
951 952
	ret = !!(drive->atapi_flags & IDE_AFLAG_MEDIA_CHANGED);
	drive->atapi_flags &= ~IDE_AFLAG_MEDIA_CHANGED;
953
	return ret;
Linus Torvalds's avatar
Linus Torvalds committed
954 955 956 957 958 959 960 961 962 963
}

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 = {
964 965 966 967 968 969 970
	.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
971 972
};

973
static int ide_floppy_probe(ide_drive_t *drive)
Linus Torvalds's avatar
Linus Torvalds committed
974 975 976 977 978 979
{
	idefloppy_floppy_t *floppy;
	struct gendisk *g;

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

Linus Torvalds's avatar
Linus Torvalds committed
981 982
	if (drive->media != ide_floppy)
		goto failed;
983

984
	if (!ide_check_atapi_device(drive, DRV_NAME)) {
985 986
		printk(KERN_ERR "ide-floppy: %s: not supported by this version"
				" of ide-floppy\n", drive->name);
Linus Torvalds's avatar
Linus Torvalds committed
987 988
		goto failed;
	}
989 990 991 992
	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
993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
		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;

1012
	idefloppy_setup(drive, floppy);
1013

Linus Torvalds's avatar
Linus Torvalds committed
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
	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:
1025
	return -ENODEV;
Linus Torvalds's avatar
Linus Torvalds committed
1026 1027
}

1028
static void __exit idefloppy_exit(void)
Linus Torvalds's avatar
Linus Torvalds committed
1029
{
1030
	driver_unregister(&idefloppy_driver.gen_driver);
Linus Torvalds's avatar
Linus Torvalds committed
1031 1032
}

1033
static int __init idefloppy_init(void)
Linus Torvalds's avatar
Linus Torvalds committed
1034 1035
{
	printk("ide-floppy driver " IDEFLOPPY_VERSION "\n");
1036
	return driver_register(&idefloppy_driver.gen_driver);
Linus Torvalds's avatar
Linus Torvalds committed
1037 1038
}

1039
MODULE_ALIAS("ide:*m-floppy*");
1040
MODULE_ALIAS("ide-floppy");
Linus Torvalds's avatar
Linus Torvalds committed
1041 1042 1043
module_init(idefloppy_init);
module_exit(idefloppy_exit);
MODULE_LICENSE("GPL");
1044 1045
MODULE_DESCRIPTION("ATAPI FLOPPY Driver");