ide-floppy.c revision d3f208488fcd9351e01f4e0ca088873192194094
1/*
2 * IDE ATAPI floppy driver.
3 *
4 * Copyright (C) 1996-1999  Gadi Oxman <gadio@netvision.net.il>
5 * Copyright (C) 2000-2002  Paul Bristow <paul@paulbristow.net>
6 * Copyright (C) 2005       Bartlomiej Zolnierkiewicz
7 */
8
9/*
10 * The driver currently doesn't have any fancy features, just the bare
11 * minimum read/write support.
12 *
13 * This driver supports the following IDE floppy drives:
14 *
15 * LS-120/240 SuperDisk
16 * Iomega Zip 100/250
17 * Iomega PC Card Clik!/PocketZip
18 *
19 * For a historical changelog see
20 * Documentation/ide/ChangeLog.ide-floppy.1996-2002
21 */
22
23#define IDEFLOPPY_VERSION "0.99.newide"
24
25#include <linux/module.h>
26#include <linux/types.h>
27#include <linux/string.h>
28#include <linux/kernel.h>
29#include <linux/delay.h>
30#include <linux/timer.h>
31#include <linux/mm.h>
32#include <linux/interrupt.h>
33#include <linux/major.h>
34#include <linux/errno.h>
35#include <linux/genhd.h>
36#include <linux/slab.h>
37#include <linux/cdrom.h>
38#include <linux/ide.h>
39#include <linux/bitops.h>
40#include <linux/mutex.h>
41
42#include <scsi/scsi_ioctl.h>
43
44#include <asm/byteorder.h>
45#include <asm/irq.h>
46#include <asm/uaccess.h>
47#include <asm/io.h>
48#include <asm/unaligned.h>
49
50/*
51 *	The following are used to debug the driver.
52 */
53#define IDEFLOPPY_DEBUG_LOG		0
54#define IDEFLOPPY_DEBUG_INFO		0
55#define IDEFLOPPY_DEBUG_BUGS		1
56
57/* #define IDEFLOPPY_DEBUG(fmt, args...) printk(KERN_INFO fmt, ## args) */
58#define IDEFLOPPY_DEBUG( fmt, args... )
59
60#if IDEFLOPPY_DEBUG_LOG
61#define debug_log printk
62#else
63#define debug_log(fmt, args... ) do {} while(0)
64#endif
65
66
67/*
68 *	Some drives require a longer irq timeout.
69 */
70#define IDEFLOPPY_WAIT_CMD		(5 * WAIT_CMD)
71
72/*
73 *	After each failed packet command we issue a request sense command
74 *	and retry the packet command IDEFLOPPY_MAX_PC_RETRIES times.
75 */
76#define IDEFLOPPY_MAX_PC_RETRIES	3
77
78/*
79 *	With each packet command, we allocate a buffer of
80 *	IDEFLOPPY_PC_BUFFER_SIZE bytes.
81 */
82#define IDEFLOPPY_PC_BUFFER_SIZE	256
83
84/*
85 *	In various places in the driver, we need to allocate storage
86 *	for packet commands and requests, which will remain valid while
87 *	we leave the driver to wait for an interrupt or a timeout event.
88 */
89#define IDEFLOPPY_PC_STACK		(10 + IDEFLOPPY_MAX_PC_RETRIES)
90
91/*
92 *	Our view of a packet command.
93 */
94typedef struct idefloppy_packet_command_s {
95	u8 c[12];				/* Actual packet bytes */
96	int retries;				/* On each retry, we increment retries */
97	int error;				/* Error code */
98	int request_transfer;			/* Bytes to transfer */
99	int actually_transferred;		/* Bytes actually transferred */
100	int buffer_size;			/* Size of our data buffer */
101	int b_count;				/* Missing/Available data on the current buffer */
102	struct request *rq;			/* The corresponding request */
103	u8 *buffer;				/* Data buffer */
104	u8 *current_position;			/* Pointer into the above buffer */
105	void (*callback) (ide_drive_t *);	/* Called when this packet command is completed */
106	u8 pc_buffer[IDEFLOPPY_PC_BUFFER_SIZE];	/* Temporary buffer */
107	unsigned long flags;			/* Status/Action bit flags: long for set_bit */
108} idefloppy_pc_t;
109
110/*
111 *	Packet command flag bits.
112 */
113#define	PC_ABORT			0	/* Set when an error is considered normal - We won't retry */
114#define PC_DMA_RECOMMENDED		2	/* 1 when we prefer to use DMA if possible */
115#define	PC_DMA_IN_PROGRESS		3	/* 1 while DMA in progress */
116#define	PC_DMA_ERROR			4	/* 1 when encountered problem during DMA */
117#define	PC_WRITING			5	/* Data direction */
118
119#define	PC_SUPPRESS_ERROR		6	/* Suppress error reporting */
120
121/*
122 *	Removable Block Access Capabilities Page
123 */
124typedef struct {
125#if defined(__LITTLE_ENDIAN_BITFIELD)
126	unsigned	page_code	:6;	/* Page code - Should be 0x1b */
127	unsigned	reserved1_6	:1;	/* Reserved */
128	unsigned	ps		:1;	/* Should be 0 */
129#elif defined(__BIG_ENDIAN_BITFIELD)
130	unsigned	ps		:1;	/* Should be 0 */
131	unsigned	reserved1_6	:1;	/* Reserved */
132	unsigned	page_code	:6;	/* Page code - Should be 0x1b */
133#else
134#error "Bitfield endianness not defined! Check your byteorder.h"
135#endif
136	u8		page_length;		/* Page Length - Should be 0xa */
137#if defined(__LITTLE_ENDIAN_BITFIELD)
138	unsigned	reserved2	:6;
139	unsigned	srfp		:1;	/* Supports reporting progress of format */
140	unsigned	sflp		:1;	/* System floppy type device */
141	unsigned	tlun		:3;	/* Total logical units supported by the device */
142	unsigned	reserved3	:3;
143	unsigned	sml		:1;	/* Single / Multiple lun supported */
144	unsigned	ncd		:1;	/* Non cd optical device */
145#elif defined(__BIG_ENDIAN_BITFIELD)
146	unsigned	sflp		:1;	/* System floppy type device */
147	unsigned	srfp		:1;	/* Supports reporting progress of format */
148	unsigned	reserved2	:6;
149	unsigned	ncd		:1;	/* Non cd optical device */
150	unsigned	sml		:1;	/* Single / Multiple lun supported */
151	unsigned	reserved3	:3;
152	unsigned	tlun		:3;	/* Total logical units supported by the device */
153#else
154#error "Bitfield endianness not defined! Check your byteorder.h"
155#endif
156	u8		reserved[8];
157} idefloppy_capabilities_page_t;
158
159/*
160 *	Flexible disk page.
161 */
162typedef struct {
163#if defined(__LITTLE_ENDIAN_BITFIELD)
164	unsigned	page_code	:6;	/* Page code - Should be 0x5 */
165	unsigned	reserved1_6	:1;	/* Reserved */
166	unsigned	ps		:1;	/* The device is capable of saving the page */
167#elif defined(__BIG_ENDIAN_BITFIELD)
168	unsigned	ps		:1;	/* The device is capable of saving the page */
169	unsigned	reserved1_6	:1;	/* Reserved */
170	unsigned	page_code	:6;	/* Page code - Should be 0x5 */
171#else
172#error "Bitfield endianness not defined! Check your byteorder.h"
173#endif
174	u8		page_length;		/* Page Length - Should be 0x1e */
175	u16		transfer_rate;		/* In kilobits per second */
176	u8		heads, sectors;		/* Number of heads, Number of sectors per track */
177	u16		sector_size;		/* Byes per sector */
178	u16		cyls;			/* Number of cylinders */
179	u8		reserved10[10];
180	u8		motor_delay;		/* Motor off delay */
181	u8		reserved21[7];
182	u16		rpm;			/* Rotations per minute */
183	u8		reserved30[2];
184} idefloppy_flexible_disk_page_t;
185
186/*
187 *	Format capacity
188 */
189typedef struct {
190	u8		reserved[3];
191	u8		length;			/* Length of the following descriptors in bytes */
192} idefloppy_capacity_header_t;
193
194typedef struct {
195	u32		blocks;			/* Number of blocks */
196#if defined(__LITTLE_ENDIAN_BITFIELD)
197	unsigned	dc		:2;	/* Descriptor Code */
198	unsigned	reserved	:6;
199#elif defined(__BIG_ENDIAN_BITFIELD)
200	unsigned	reserved	:6;
201	unsigned	dc		:2;	/* Descriptor Code */
202#else
203#error "Bitfield endianness not defined! Check your byteorder.h"
204#endif
205	u8		length_msb;		/* Block Length (MSB)*/
206	u16		length;			/* Block Length */
207} idefloppy_capacity_descriptor_t;
208
209#define CAPACITY_INVALID	0x00
210#define CAPACITY_UNFORMATTED	0x01
211#define CAPACITY_CURRENT	0x02
212#define CAPACITY_NO_CARTRIDGE	0x03
213
214/*
215 *	Most of our global data which we need to save even as we leave the
216 *	driver due to an interrupt or a timer event is stored in a variable
217 *	of type idefloppy_floppy_t, defined below.
218 */
219typedef struct ide_floppy_obj {
220	ide_drive_t	*drive;
221	ide_driver_t	*driver;
222	struct gendisk	*disk;
223	struct kref	kref;
224	unsigned int	openers;	/* protected by BKL for now */
225
226	/* Current packet command */
227	idefloppy_pc_t *pc;
228	/* Last failed packet command */
229	idefloppy_pc_t *failed_pc;
230	/* Packet command stack */
231	idefloppy_pc_t pc_stack[IDEFLOPPY_PC_STACK];
232	/* Next free packet command storage space */
233	int pc_stack_index;
234	struct request rq_stack[IDEFLOPPY_PC_STACK];
235	/* We implement a circular array */
236	int rq_stack_index;
237
238	/*
239	 *	Last error information
240	 */
241	u8 sense_key, asc, ascq;
242	/* delay this long before sending packet command */
243	u8 ticks;
244	int progress_indication;
245
246	/*
247	 *	Device information
248	 */
249	/* Current format */
250	int blocks, block_size, bs_factor;
251	/* Last format capacity */
252	idefloppy_capacity_descriptor_t capacity;
253	/* Copy of the flexible disk page */
254	idefloppy_flexible_disk_page_t flexible_disk_page;
255	/* Write protect */
256	int wp;
257	/* Supports format progress report */
258	int srfp;
259	/* Status/Action flags */
260	unsigned long flags;
261} idefloppy_floppy_t;
262
263#define IDEFLOPPY_TICKS_DELAY	HZ/20	/* default delay for ZIP 100 (50ms) */
264
265/*
266 *	Floppy flag bits values.
267 */
268#define IDEFLOPPY_DRQ_INTERRUPT		0	/* DRQ interrupt device */
269#define IDEFLOPPY_MEDIA_CHANGED		1	/* Media may have changed */
270#define IDEFLOPPY_USE_READ12		2	/* Use READ12/WRITE12 or READ10/WRITE10 */
271#define	IDEFLOPPY_FORMAT_IN_PROGRESS	3	/* Format in progress */
272#define IDEFLOPPY_CLIK_DRIVE	        4       /* Avoid commands not supported in Clik drive */
273#define IDEFLOPPY_ZIP_DRIVE		5	/* Requires BH algorithm for packets */
274
275/*
276 *	ATAPI floppy drive packet commands
277 */
278#define IDEFLOPPY_FORMAT_UNIT_CMD	0x04
279#define IDEFLOPPY_INQUIRY_CMD		0x12
280#define IDEFLOPPY_MODE_SELECT_CMD	0x55
281#define IDEFLOPPY_MODE_SENSE_CMD	0x5a
282#define IDEFLOPPY_READ10_CMD		0x28
283#define IDEFLOPPY_READ12_CMD		0xa8
284#define IDEFLOPPY_READ_CAPACITY_CMD	0x23
285#define IDEFLOPPY_REQUEST_SENSE_CMD	0x03
286#define IDEFLOPPY_PREVENT_REMOVAL_CMD	0x1e
287#define IDEFLOPPY_SEEK_CMD		0x2b
288#define IDEFLOPPY_START_STOP_CMD	0x1b
289#define IDEFLOPPY_TEST_UNIT_READY_CMD	0x00
290#define IDEFLOPPY_VERIFY_CMD		0x2f
291#define IDEFLOPPY_WRITE10_CMD		0x2a
292#define IDEFLOPPY_WRITE12_CMD		0xaa
293#define IDEFLOPPY_WRITE_VERIFY_CMD	0x2e
294
295/*
296 *	Defines for the mode sense command
297 */
298#define MODE_SENSE_CURRENT		0x00
299#define MODE_SENSE_CHANGEABLE		0x01
300#define MODE_SENSE_DEFAULT		0x02
301#define MODE_SENSE_SAVED		0x03
302
303/*
304 *	IOCTLs used in low-level formatting.
305 */
306
307#define	IDEFLOPPY_IOCTL_FORMAT_SUPPORTED	0x4600
308#define	IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY	0x4601
309#define	IDEFLOPPY_IOCTL_FORMAT_START		0x4602
310#define IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS	0x4603
311
312/*
313 *	Error codes which are returned in rq->errors to the higher part
314 *	of the driver.
315 */
316#define	IDEFLOPPY_ERROR_GENERAL		101
317
318/*
319 *	The following is used to format the general configuration word of
320 *	the ATAPI IDENTIFY DEVICE command.
321 */
322struct idefloppy_id_gcw {
323#if defined(__LITTLE_ENDIAN_BITFIELD)
324	unsigned packet_size		:2;	/* Packet Size */
325	unsigned reserved234		:3;	/* Reserved */
326	unsigned drq_type		:2;	/* Command packet DRQ type */
327	unsigned removable		:1;	/* Removable media */
328	unsigned device_type		:5;	/* Device type */
329	unsigned reserved13		:1;	/* Reserved */
330	unsigned protocol		:2;	/* Protocol type */
331#elif defined(__BIG_ENDIAN_BITFIELD)
332	unsigned protocol		:2;	/* Protocol type */
333	unsigned reserved13		:1;	/* Reserved */
334	unsigned device_type		:5;	/* Device type */
335	unsigned removable		:1;	/* Removable media */
336	unsigned drq_type		:2;	/* Command packet DRQ type */
337	unsigned reserved234		:3;	/* Reserved */
338	unsigned packet_size		:2;	/* Packet Size */
339#else
340#error "Bitfield endianness not defined! Check your byteorder.h"
341#endif
342};
343
344/*
345 *	INQUIRY packet command - Data Format
346 */
347typedef struct {
348#if defined(__LITTLE_ENDIAN_BITFIELD)
349	unsigned	device_type	:5;	/* Peripheral Device Type */
350	unsigned	reserved0_765	:3;	/* Peripheral Qualifier - Reserved */
351	unsigned	reserved1_6t0	:7;	/* Reserved */
352	unsigned	rmb		:1;	/* Removable Medium Bit */
353	unsigned	ansi_version	:3;	/* ANSI Version */
354	unsigned	ecma_version	:3;	/* ECMA Version */
355	unsigned	iso_version	:2;	/* ISO Version */
356	unsigned	response_format :4;	/* Response Data Format */
357	unsigned	reserved3_45	:2;	/* Reserved */
358	unsigned	reserved3_6	:1;	/* TrmIOP - Reserved */
359	unsigned	reserved3_7	:1;	/* AENC - Reserved */
360#elif defined(__BIG_ENDIAN_BITFIELD)
361	unsigned	reserved0_765	:3;	/* Peripheral Qualifier - Reserved */
362	unsigned	device_type	:5;	/* Peripheral Device Type */
363	unsigned	rmb		:1;	/* Removable Medium Bit */
364	unsigned	reserved1_6t0	:7;	/* Reserved */
365	unsigned	iso_version	:2;	/* ISO Version */
366	unsigned	ecma_version	:3;	/* ECMA Version */
367	unsigned	ansi_version	:3;	/* ANSI Version */
368	unsigned	reserved3_7	:1;	/* AENC - Reserved */
369	unsigned	reserved3_6	:1;	/* TrmIOP - Reserved */
370	unsigned	reserved3_45	:2;	/* Reserved */
371	unsigned	response_format :4;	/* Response Data Format */
372#else
373#error "Bitfield endianness not defined! Check your byteorder.h"
374#endif
375	u8		additional_length;	/* Additional Length (total_length-4) */
376	u8		rsv5, rsv6, rsv7;	/* Reserved */
377	u8		vendor_id[8];		/* Vendor Identification */
378	u8		product_id[16];		/* Product Identification */
379	u8		revision_level[4];	/* Revision Level */
380	u8		vendor_specific[20];	/* Vendor Specific - Optional */
381	u8		reserved56t95[40];	/* Reserved - Optional */
382						/* Additional information may be returned */
383} idefloppy_inquiry_result_t;
384
385/*
386 *	REQUEST SENSE packet command result - Data Format.
387 */
388typedef struct {
389#if defined(__LITTLE_ENDIAN_BITFIELD)
390	unsigned	error_code	:7;	/* Current error (0x70) */
391	unsigned	valid		:1;	/* The information field conforms to SFF-8070i */
392	u8		reserved1	:8;	/* Reserved */
393	unsigned	sense_key	:4;	/* Sense Key */
394	unsigned	reserved2_4	:1;	/* Reserved */
395	unsigned	ili		:1;	/* Incorrect Length Indicator */
396	unsigned	reserved2_67	:2;
397#elif defined(__BIG_ENDIAN_BITFIELD)
398	unsigned	valid		:1;	/* The information field conforms to SFF-8070i */
399	unsigned	error_code	:7;	/* Current error (0x70) */
400	u8		reserved1	:8;	/* Reserved */
401	unsigned	reserved2_67	:2;
402	unsigned	ili		:1;	/* Incorrect Length Indicator */
403	unsigned	reserved2_4	:1;	/* Reserved */
404	unsigned	sense_key	:4;	/* Sense Key */
405#else
406#error "Bitfield endianness not defined! Check your byteorder.h"
407#endif
408	u32		information __attribute__ ((packed));
409	u8		asl;			/* Additional sense length (n-7) */
410	u32		command_specific;	/* Additional command specific information */
411	u8		asc;			/* Additional Sense Code */
412	u8		ascq;			/* Additional Sense Code Qualifier */
413	u8		replaceable_unit_code;	/* Field Replaceable Unit Code */
414	u8		sksv[3];
415	u8		pad[2];			/* Padding to 20 bytes */
416} idefloppy_request_sense_result_t;
417
418/*
419 *	Pages of the SELECT SENSE / MODE SENSE packet commands.
420 */
421#define	IDEFLOPPY_CAPABILITIES_PAGE	0x1b
422#define IDEFLOPPY_FLEXIBLE_DISK_PAGE	0x05
423
424/*
425 *	Mode Parameter Header for the MODE SENSE packet command
426 */
427typedef struct {
428	u16		mode_data_length;	/* Length of the following data transfer */
429	u8		medium_type;		/* Medium Type */
430#if defined(__LITTLE_ENDIAN_BITFIELD)
431	unsigned	reserved3	:7;
432	unsigned	wp		:1;	/* Write protect */
433#elif defined(__BIG_ENDIAN_BITFIELD)
434	unsigned	wp		:1;	/* Write protect */
435	unsigned	reserved3	:7;
436#else
437#error "Bitfield endianness not defined! Check your byteorder.h"
438#endif
439	u8		reserved[4];
440} idefloppy_mode_parameter_header_t;
441
442static DEFINE_MUTEX(idefloppy_ref_mutex);
443
444#define to_ide_floppy(obj) container_of(obj, struct ide_floppy_obj, kref)
445
446#define ide_floppy_g(disk) \
447	container_of((disk)->private_data, struct ide_floppy_obj, driver)
448
449static struct ide_floppy_obj *ide_floppy_get(struct gendisk *disk)
450{
451	struct ide_floppy_obj *floppy = NULL;
452
453	mutex_lock(&idefloppy_ref_mutex);
454	floppy = ide_floppy_g(disk);
455	if (floppy)
456		kref_get(&floppy->kref);
457	mutex_unlock(&idefloppy_ref_mutex);
458	return floppy;
459}
460
461static void ide_floppy_release(struct kref *);
462
463static void ide_floppy_put(struct ide_floppy_obj *floppy)
464{
465	mutex_lock(&idefloppy_ref_mutex);
466	kref_put(&floppy->kref, ide_floppy_release);
467	mutex_unlock(&idefloppy_ref_mutex);
468}
469
470/*
471 *	Too bad. The drive wants to send us data which we are not ready to accept.
472 *	Just throw it away.
473 */
474static void idefloppy_discard_data (ide_drive_t *drive, unsigned int bcount)
475{
476	while (bcount--)
477		(void) HWIF(drive)->INB(IDE_DATA_REG);
478}
479
480#if IDEFLOPPY_DEBUG_BUGS
481static void idefloppy_write_zeros (ide_drive_t *drive, unsigned int bcount)
482{
483	while (bcount--)
484		HWIF(drive)->OUTB(0, IDE_DATA_REG);
485}
486#endif /* IDEFLOPPY_DEBUG_BUGS */
487
488
489/*
490 *	idefloppy_do_end_request is used to finish servicing a request.
491 *
492 *	For read/write requests, we will call ide_end_request to pass to the
493 *	next buffer.
494 */
495static int idefloppy_do_end_request(ide_drive_t *drive, int uptodate, int nsecs)
496{
497	idefloppy_floppy_t *floppy = drive->driver_data;
498	struct request *rq = HWGROUP(drive)->rq;
499	int error;
500
501	debug_log(KERN_INFO "Reached idefloppy_end_request\n");
502
503	switch (uptodate) {
504		case 0: error = IDEFLOPPY_ERROR_GENERAL; break;
505		case 1: error = 0; break;
506		default: error = uptodate;
507	}
508	if (error)
509		floppy->failed_pc = NULL;
510	/* Why does this happen? */
511	if (!rq)
512		return 0;
513	if (!blk_special_request(rq)) {
514		/* our real local end request function */
515		ide_end_request(drive, uptodate, nsecs);
516		return 0;
517	}
518	rq->errors = error;
519	/* fixme: need to move this local also */
520	ide_end_drive_cmd(drive, 0, 0);
521	return 0;
522}
523
524static void idefloppy_input_buffers (ide_drive_t *drive, idefloppy_pc_t *pc, unsigned int bcount)
525{
526	struct request *rq = pc->rq;
527	struct bio_vec *bvec;
528	struct req_iterator iter;
529	unsigned long flags;
530	char *data;
531	int count, done = 0;
532
533	rq_for_each_segment(bvec, rq, iter) {
534		if (!bcount)
535			break;
536
537		count = min(bvec->bv_len, bcount);
538
539		data = bvec_kmap_irq(bvec, &flags);
540		drive->hwif->atapi_input_bytes(drive, data, count);
541		bvec_kunmap_irq(data, &flags);
542
543		bcount -= count;
544		pc->b_count += count;
545		done += count;
546	}
547
548	idefloppy_do_end_request(drive, 1, done >> 9);
549
550	if (bcount) {
551		printk(KERN_ERR "%s: leftover data in idefloppy_input_buffers, bcount == %d\n", drive->name, bcount);
552		idefloppy_discard_data(drive, bcount);
553	}
554}
555
556static void idefloppy_output_buffers (ide_drive_t *drive, idefloppy_pc_t *pc, unsigned int bcount)
557{
558	struct request *rq = pc->rq;
559	struct req_iterator iter;
560	struct bio_vec *bvec;
561	unsigned long flags;
562	int count, done = 0;
563	char *data;
564
565	rq_for_each_segment(bvec, rq, iter) {
566		if (!bcount)
567			break;
568
569		count = min(bvec->bv_len, bcount);
570
571		data = bvec_kmap_irq(bvec, &flags);
572		drive->hwif->atapi_output_bytes(drive, data, count);
573		bvec_kunmap_irq(data, &flags);
574
575		bcount -= count;
576		pc->b_count += count;
577		done += count;
578	}
579
580	idefloppy_do_end_request(drive, 1, done >> 9);
581
582#if IDEFLOPPY_DEBUG_BUGS
583	if (bcount) {
584		printk(KERN_ERR "%s: leftover data in idefloppy_output_buffers, bcount == %d\n", drive->name, bcount);
585		idefloppy_write_zeros(drive, bcount);
586	}
587#endif
588}
589
590static void idefloppy_update_buffers (ide_drive_t *drive, idefloppy_pc_t *pc)
591{
592	struct request *rq = pc->rq;
593	struct bio *bio = rq->bio;
594
595	while ((bio = rq->bio) != NULL)
596		idefloppy_do_end_request(drive, 1, 0);
597}
598
599/*
600 *	idefloppy_queue_pc_head generates a new packet command request in front
601 *	of the request queue, before the current request, so that it will be
602 *	processed immediately, on the next pass through the driver.
603 */
604static void idefloppy_queue_pc_head (ide_drive_t *drive,idefloppy_pc_t *pc,struct request *rq)
605{
606	struct ide_floppy_obj *floppy = drive->driver_data;
607
608	ide_init_drive_cmd(rq);
609	rq->buffer = (char *) pc;
610	rq->cmd_type = REQ_TYPE_SPECIAL;
611	rq->rq_disk = floppy->disk;
612	(void) ide_do_drive_cmd(drive, rq, ide_preempt);
613}
614
615static idefloppy_pc_t *idefloppy_next_pc_storage (ide_drive_t *drive)
616{
617	idefloppy_floppy_t *floppy = drive->driver_data;
618
619	if (floppy->pc_stack_index == IDEFLOPPY_PC_STACK)
620		floppy->pc_stack_index=0;
621	return (&floppy->pc_stack[floppy->pc_stack_index++]);
622}
623
624static struct request *idefloppy_next_rq_storage (ide_drive_t *drive)
625{
626	idefloppy_floppy_t *floppy = drive->driver_data;
627
628	if (floppy->rq_stack_index == IDEFLOPPY_PC_STACK)
629		floppy->rq_stack_index = 0;
630	return (&floppy->rq_stack[floppy->rq_stack_index++]);
631}
632
633/*
634 *	idefloppy_analyze_error is called on each failed packet command retry
635 *	to analyze the request sense.
636 */
637static void idefloppy_analyze_error (ide_drive_t *drive,idefloppy_request_sense_result_t *result)
638{
639	idefloppy_floppy_t *floppy = drive->driver_data;
640
641	floppy->sense_key = result->sense_key;
642	floppy->asc = result->asc;
643	floppy->ascq = result->ascq;
644	floppy->progress_indication = result->sksv[0] & 0x80 ?
645		(u16)get_unaligned((u16 *)(result->sksv+1)):0x10000;
646	if (floppy->failed_pc)
647		debug_log(KERN_INFO "ide-floppy: pc = %x, sense key = %x, "
648			"asc = %x, ascq = %x\n", floppy->failed_pc->c[0],
649			result->sense_key, result->asc, result->ascq);
650	else
651		debug_log(KERN_INFO "ide-floppy: sense key = %x, asc = %x, "
652			"ascq = %x\n", result->sense_key,
653			result->asc, result->ascq);
654}
655
656static void idefloppy_request_sense_callback (ide_drive_t *drive)
657{
658	idefloppy_floppy_t *floppy = drive->driver_data;
659
660	debug_log(KERN_INFO "ide-floppy: Reached %s\n", __FUNCTION__);
661
662	if (!floppy->pc->error) {
663		idefloppy_analyze_error(drive,(idefloppy_request_sense_result_t *) floppy->pc->buffer);
664		idefloppy_do_end_request(drive, 1, 0);
665	} else {
666		printk(KERN_ERR "Error in REQUEST SENSE itself - Aborting request!\n");
667		idefloppy_do_end_request(drive, 0, 0);
668	}
669}
670
671/*
672 *	General packet command callback function.
673 */
674static void idefloppy_pc_callback (ide_drive_t *drive)
675{
676	idefloppy_floppy_t *floppy = drive->driver_data;
677
678	debug_log(KERN_INFO "ide-floppy: Reached %s\n", __FUNCTION__);
679
680	idefloppy_do_end_request(drive, floppy->pc->error ? 0 : 1, 0);
681}
682
683/*
684 *	idefloppy_init_pc initializes a packet command.
685 */
686static void idefloppy_init_pc (idefloppy_pc_t *pc)
687{
688	memset(pc->c, 0, 12);
689	pc->retries = 0;
690	pc->flags = 0;
691	pc->request_transfer = 0;
692	pc->buffer = pc->pc_buffer;
693	pc->buffer_size = IDEFLOPPY_PC_BUFFER_SIZE;
694	pc->callback = &idefloppy_pc_callback;
695}
696
697static void idefloppy_create_request_sense_cmd (idefloppy_pc_t *pc)
698{
699	idefloppy_init_pc(pc);
700	pc->c[0] = IDEFLOPPY_REQUEST_SENSE_CMD;
701	pc->c[4] = 255;
702	pc->request_transfer = 18;
703	pc->callback = &idefloppy_request_sense_callback;
704}
705
706/*
707 *	idefloppy_retry_pc is called when an error was detected during the
708 *	last packet command. We queue a request sense packet command in
709 *	the head of the request list.
710 */
711static void idefloppy_retry_pc (ide_drive_t *drive)
712{
713	idefloppy_pc_t *pc;
714	struct request *rq;
715
716	(void)drive->hwif->INB(IDE_ERROR_REG);
717	pc = idefloppy_next_pc_storage(drive);
718	rq = idefloppy_next_rq_storage(drive);
719	idefloppy_create_request_sense_cmd(pc);
720	idefloppy_queue_pc_head(drive, pc, rq);
721}
722
723/*
724 *	idefloppy_pc_intr is the usual interrupt handler which will be called
725 *	during a packet command.
726 */
727static ide_startstop_t idefloppy_pc_intr (ide_drive_t *drive)
728{
729	idefloppy_floppy_t *floppy = drive->driver_data;
730	ide_hwif_t *hwif = drive->hwif;
731	idefloppy_pc_t *pc = floppy->pc;
732	struct request *rq = pc->rq;
733	unsigned int temp;
734	u16 bcount;
735	u8 stat, ireason;
736
737	debug_log(KERN_INFO "ide-floppy: Reached %s interrupt handler\n",
738		__FUNCTION__);
739
740	if (test_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
741		if (HWIF(drive)->ide_dma_end(drive)) {
742			set_bit(PC_DMA_ERROR, &pc->flags);
743		} else {
744			pc->actually_transferred = pc->request_transfer;
745			idefloppy_update_buffers(drive, pc);
746		}
747		debug_log(KERN_INFO "ide-floppy: DMA finished\n");
748	}
749
750	/* Clear the interrupt */
751	stat = drive->hwif->INB(IDE_STATUS_REG);
752
753	if ((stat & DRQ_STAT) == 0) {		/* No more interrupts */
754		debug_log(KERN_INFO "Packet command completed, %d bytes "
755			"transferred\n", pc->actually_transferred);
756		clear_bit(PC_DMA_IN_PROGRESS, &pc->flags);
757
758		local_irq_enable_in_hardirq();
759
760		if ((stat & ERR_STAT) || test_bit(PC_DMA_ERROR, &pc->flags)) {
761			/* Error detected */
762			debug_log(KERN_INFO "ide-floppy: %s: I/O error\n",
763				drive->name);
764			rq->errors++;
765			if (pc->c[0] == IDEFLOPPY_REQUEST_SENSE_CMD) {
766				printk(KERN_ERR "ide-floppy: I/O error in "
767					"request sense command\n");
768				return ide_do_reset(drive);
769			}
770			/* Retry operation */
771			idefloppy_retry_pc(drive);
772			/* queued, but not started */
773			return ide_stopped;
774		}
775		pc->error = 0;
776		if (floppy->failed_pc == pc)
777			floppy->failed_pc = NULL;
778		/* Command finished - Call the callback function */
779		pc->callback(drive);
780		return ide_stopped;
781	}
782
783	if (test_and_clear_bit(PC_DMA_IN_PROGRESS, &pc->flags)) {
784		printk(KERN_ERR "ide-floppy: The floppy wants to issue "
785			"more interrupts in DMA mode\n");
786		ide_dma_off(drive);
787		return ide_do_reset(drive);
788	}
789
790	/* Get the number of bytes to transfer */
791	bcount = (hwif->INB(IDE_BCOUNTH_REG) << 8) |
792		  hwif->INB(IDE_BCOUNTL_REG);
793	/* on this interrupt */
794	ireason = hwif->INB(IDE_IREASON_REG);
795
796	if (ireason & CD) {
797		printk(KERN_ERR "ide-floppy: CoD != 0 in idefloppy_pc_intr\n");
798		return ide_do_reset(drive);
799	}
800	if (((ireason & IO) == IO) == test_bit(PC_WRITING, &pc->flags)) {
801		/* Hopefully, we will never get here */
802		printk(KERN_ERR "ide-floppy: We wanted to %s, ",
803				(ireason & IO) ? "Write" : "Read");
804		printk(KERN_ERR "but the floppy wants us to %s !\n",
805				(ireason & IO) ? "Read" : "Write");
806		return ide_do_reset(drive);
807	}
808	if (!test_bit(PC_WRITING, &pc->flags)) {
809		/* Reading - Check that we have enough space */
810		temp = pc->actually_transferred + bcount;
811		if (temp > pc->request_transfer) {
812			if (temp > pc->buffer_size) {
813				printk(KERN_ERR "ide-floppy: The floppy wants "
814					"to send us more data than expected "
815					"- discarding data\n");
816				idefloppy_discard_data(drive, bcount);
817				BUG_ON(HWGROUP(drive)->handler != NULL);
818				ide_set_handler(drive,
819						&idefloppy_pc_intr,
820						IDEFLOPPY_WAIT_CMD,
821						NULL);
822				return ide_started;
823			}
824			debug_log(KERN_NOTICE "ide-floppy: The floppy wants to "
825				"send us more data than expected - "
826				"allowing transfer\n");
827		}
828	}
829	if (test_bit(PC_WRITING, &pc->flags)) {
830		if (pc->buffer != NULL)
831			/* Write the current buffer */
832			hwif->atapi_output_bytes(drive, pc->current_position,
833						 bcount);
834		else
835			idefloppy_output_buffers(drive, pc, bcount);
836	} else {
837		if (pc->buffer != NULL)
838			/* Read the current buffer */
839			hwif->atapi_input_bytes(drive, pc->current_position,
840						bcount);
841		else
842			idefloppy_input_buffers(drive, pc, bcount);
843	}
844	/* Update the current position */
845	pc->actually_transferred += bcount;
846	pc->current_position += bcount;
847
848	BUG_ON(HWGROUP(drive)->handler != NULL);
849	ide_set_handler(drive, &idefloppy_pc_intr, IDEFLOPPY_WAIT_CMD, NULL);		/* And set the interrupt handler again */
850	return ide_started;
851}
852
853/*
854 * This is the original routine that did the packet transfer.
855 * It fails at high speeds on the Iomega ZIP drive, so there's a slower version
856 * for that drive below. The algorithm is chosen based on drive type
857 */
858static ide_startstop_t idefloppy_transfer_pc (ide_drive_t *drive)
859{
860	ide_startstop_t startstop;
861	idefloppy_floppy_t *floppy = drive->driver_data;
862	u8 ireason;
863
864	if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
865		printk(KERN_ERR "ide-floppy: Strange, packet command "
866				"initiated yet DRQ isn't asserted\n");
867		return startstop;
868	}
869	ireason = drive->hwif->INB(IDE_IREASON_REG);
870	if ((ireason & CD) == 0 || (ireason & IO)) {
871		printk(KERN_ERR "ide-floppy: (IO,CoD) != (0,1) while "
872				"issuing a packet command\n");
873		return ide_do_reset(drive);
874	}
875	BUG_ON(HWGROUP(drive)->handler != NULL);
876	/* Set the interrupt routine */
877	ide_set_handler(drive, &idefloppy_pc_intr, IDEFLOPPY_WAIT_CMD, NULL);
878	/* Send the actual packet */
879	HWIF(drive)->atapi_output_bytes(drive, floppy->pc->c, 12);
880	return ide_started;
881}
882
883
884/*
885 * What we have here is a classic case of a top half / bottom half
886 * interrupt service routine. In interrupt mode, the device sends
887 * an interrupt to signal it's ready to receive a packet. However,
888 * we need to delay about 2-3 ticks before issuing the packet or we
889 * gets in trouble.
890 *
891 * So, follow carefully. transfer_pc1 is called as an interrupt (or
892 * directly). In either case, when the device says it's ready for a
893 * packet, we schedule the packet transfer to occur about 2-3 ticks
894 * later in transfer_pc2.
895 */
896static int idefloppy_transfer_pc2 (ide_drive_t *drive)
897{
898	idefloppy_floppy_t *floppy = drive->driver_data;
899
900	/* Send the actual packet */
901	HWIF(drive)->atapi_output_bytes(drive, floppy->pc->c, 12);
902	/* Timeout for the packet command */
903	return IDEFLOPPY_WAIT_CMD;
904}
905
906static ide_startstop_t idefloppy_transfer_pc1 (ide_drive_t *drive)
907{
908	idefloppy_floppy_t *floppy = drive->driver_data;
909	ide_startstop_t startstop;
910	u8 ireason;
911
912	if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
913		printk(KERN_ERR "ide-floppy: Strange, packet command "
914				"initiated yet DRQ isn't asserted\n");
915		return startstop;
916	}
917	ireason = drive->hwif->INB(IDE_IREASON_REG);
918	if ((ireason & CD) == 0 || (ireason & IO)) {
919		printk(KERN_ERR "ide-floppy: (IO,CoD) != (0,1) "
920				"while issuing a packet command\n");
921		return ide_do_reset(drive);
922	}
923	/*
924	 * The following delay solves a problem with ATAPI Zip 100 drives
925	 * where the Busy flag was apparently being deasserted before the
926	 * unit was ready to receive data. This was happening on a
927	 * 1200 MHz Athlon system. 10/26/01 25msec is too short,
928	 * 40 and 50msec work well. idefloppy_pc_intr will not be actually
929	 * used until after the packet is moved in about 50 msec.
930	 */
931	BUG_ON(HWGROUP(drive)->handler != NULL);
932	ide_set_handler(drive,
933	  &idefloppy_pc_intr, 		/* service routine for packet command */
934	  floppy->ticks,		/* wait this long before "failing" */
935	  &idefloppy_transfer_pc2);	/* fail == transfer_pc2 */
936	return ide_started;
937}
938
939/**
940 * idefloppy_should_report_error()
941 *
942 * Supresses error messages resulting from Medium not present
943 */
944static inline int idefloppy_should_report_error(idefloppy_floppy_t *floppy)
945{
946	if (floppy->sense_key == 0x02 &&
947	    floppy->asc       == 0x3a &&
948	    floppy->ascq      == 0x00)
949		return 0;
950	return 1;
951}
952
953/*
954 *	Issue a packet command
955 */
956static ide_startstop_t idefloppy_issue_pc (ide_drive_t *drive, idefloppy_pc_t *pc)
957{
958	idefloppy_floppy_t *floppy = drive->driver_data;
959	ide_hwif_t *hwif = drive->hwif;
960	ide_handler_t *pkt_xfer_routine;
961	u16 bcount;
962	u8 dma;
963
964	if (floppy->failed_pc == NULL &&
965	    pc->c[0] != IDEFLOPPY_REQUEST_SENSE_CMD)
966		floppy->failed_pc = pc;
967	/* Set the current packet command */
968	floppy->pc = pc;
969
970	if (pc->retries > IDEFLOPPY_MAX_PC_RETRIES ||
971	    test_bit(PC_ABORT, &pc->flags)) {
972		/*
973		 *	We will "abort" retrying a packet command in case
974		 *	a legitimate error code was received.
975		 */
976		if (!test_bit(PC_ABORT, &pc->flags)) {
977			if (!test_bit(PC_SUPPRESS_ERROR, &pc->flags)) {
978				if (idefloppy_should_report_error(floppy))
979					printk(KERN_ERR "ide-floppy: %s: I/O error, "
980					       "pc = %2x, key = %2x, "
981					       "asc = %2x, ascq = %2x\n",
982					       drive->name, pc->c[0],
983					       floppy->sense_key,
984					       floppy->asc, floppy->ascq);
985			}
986			/* Giving up */
987			pc->error = IDEFLOPPY_ERROR_GENERAL;
988		}
989		floppy->failed_pc = NULL;
990		pc->callback(drive);
991		return ide_stopped;
992	}
993
994	debug_log(KERN_INFO "Retry number - %d\n",pc->retries);
995
996	pc->retries++;
997	/* We haven't transferred any data yet */
998	pc->actually_transferred = 0;
999	pc->current_position = pc->buffer;
1000	bcount = min(pc->request_transfer, 63 * 1024);
1001
1002	if (test_and_clear_bit(PC_DMA_ERROR, &pc->flags))
1003		ide_dma_off(drive);
1004
1005	dma = 0;
1006
1007	if (test_bit(PC_DMA_RECOMMENDED, &pc->flags) && drive->using_dma)
1008		dma = !hwif->dma_setup(drive);
1009
1010	ide_pktcmd_tf_load(drive, IDE_TFLAG_NO_SELECT_MASK |
1011			   IDE_TFLAG_OUT_DEVICE, bcount, dma);
1012
1013	if (dma) {	/* Begin DMA, if necessary */
1014		set_bit(PC_DMA_IN_PROGRESS, &pc->flags);
1015		hwif->dma_start(drive);
1016	}
1017
1018	/* Can we transfer the packet when we get the interrupt or wait? */
1019	if (test_bit(IDEFLOPPY_ZIP_DRIVE, &floppy->flags)) {
1020		/* wait */
1021		pkt_xfer_routine = &idefloppy_transfer_pc1;
1022	} else {
1023		/* immediate */
1024		pkt_xfer_routine = &idefloppy_transfer_pc;
1025	}
1026
1027	if (test_bit (IDEFLOPPY_DRQ_INTERRUPT, &floppy->flags)) {
1028		/* Issue the packet command */
1029		ide_execute_command(drive, WIN_PACKETCMD,
1030				pkt_xfer_routine,
1031				IDEFLOPPY_WAIT_CMD,
1032				NULL);
1033		return ide_started;
1034	} else {
1035		/* Issue the packet command */
1036		HWIF(drive)->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
1037		return (*pkt_xfer_routine) (drive);
1038	}
1039}
1040
1041static void idefloppy_rw_callback (ide_drive_t *drive)
1042{
1043	debug_log(KERN_INFO "ide-floppy: Reached idefloppy_rw_callback\n");
1044
1045	idefloppy_do_end_request(drive, 1, 0);
1046	return;
1047}
1048
1049static void idefloppy_create_prevent_cmd (idefloppy_pc_t *pc, int prevent)
1050{
1051	debug_log(KERN_INFO "ide-floppy: creating prevent removal command, "
1052		"prevent = %d\n", prevent);
1053
1054	idefloppy_init_pc(pc);
1055	pc->c[0] = IDEFLOPPY_PREVENT_REMOVAL_CMD;
1056	pc->c[4] = prevent;
1057}
1058
1059static void idefloppy_create_read_capacity_cmd (idefloppy_pc_t *pc)
1060{
1061	idefloppy_init_pc(pc);
1062	pc->c[0] = IDEFLOPPY_READ_CAPACITY_CMD;
1063	pc->c[7] = 255;
1064	pc->c[8] = 255;
1065	pc->request_transfer = 255;
1066}
1067
1068static void idefloppy_create_format_unit_cmd (idefloppy_pc_t *pc, int b, int l,
1069					      int flags)
1070{
1071	idefloppy_init_pc(pc);
1072	pc->c[0] = IDEFLOPPY_FORMAT_UNIT_CMD;
1073	pc->c[1] = 0x17;
1074
1075	memset(pc->buffer, 0, 12);
1076	pc->buffer[1] = 0xA2;
1077	/* Default format list header, u8 1: FOV/DCRT/IMM bits set */
1078
1079	if (flags & 1)				/* Verify bit on... */
1080		pc->buffer[1] ^= 0x20;		/* ... turn off DCRT bit */
1081	pc->buffer[3] = 8;
1082
1083	put_unaligned(htonl(b), (unsigned int *)(&pc->buffer[4]));
1084	put_unaligned(htonl(l), (unsigned int *)(&pc->buffer[8]));
1085	pc->buffer_size=12;
1086	set_bit(PC_WRITING, &pc->flags);
1087}
1088
1089/*
1090 *	A mode sense command is used to "sense" floppy parameters.
1091 */
1092static void idefloppy_create_mode_sense_cmd (idefloppy_pc_t *pc, u8 page_code, u8 type)
1093{
1094	u16 length = sizeof(idefloppy_mode_parameter_header_t);
1095
1096	idefloppy_init_pc(pc);
1097	pc->c[0] = IDEFLOPPY_MODE_SENSE_CMD;
1098	pc->c[1] = 0;
1099	pc->c[2] = page_code + (type << 6);
1100
1101	switch (page_code) {
1102		case IDEFLOPPY_CAPABILITIES_PAGE:
1103			length += 12;
1104			break;
1105		case IDEFLOPPY_FLEXIBLE_DISK_PAGE:
1106			length += 32;
1107			break;
1108		default:
1109			printk(KERN_ERR "ide-floppy: unsupported page code "
1110				"in create_mode_sense_cmd\n");
1111	}
1112	put_unaligned(htons(length), (u16 *) &pc->c[7]);
1113	pc->request_transfer = length;
1114}
1115
1116static void idefloppy_create_start_stop_cmd (idefloppy_pc_t *pc, int start)
1117{
1118	idefloppy_init_pc(pc);
1119	pc->c[0] = IDEFLOPPY_START_STOP_CMD;
1120	pc->c[4] = start;
1121}
1122
1123static void idefloppy_create_test_unit_ready_cmd(idefloppy_pc_t *pc)
1124{
1125	idefloppy_init_pc(pc);
1126	pc->c[0] = IDEFLOPPY_TEST_UNIT_READY_CMD;
1127}
1128
1129static void idefloppy_create_rw_cmd (idefloppy_floppy_t *floppy, idefloppy_pc_t *pc, struct request *rq, unsigned long sector)
1130{
1131	int block = sector / floppy->bs_factor;
1132	int blocks = rq->nr_sectors / floppy->bs_factor;
1133	int cmd = rq_data_dir(rq);
1134
1135	debug_log("create_rw1%d_cmd: block == %d, blocks == %d\n",
1136		2 * test_bit (IDEFLOPPY_USE_READ12, &floppy->flags),
1137		block, blocks);
1138
1139	idefloppy_init_pc(pc);
1140	if (test_bit(IDEFLOPPY_USE_READ12, &floppy->flags)) {
1141		pc->c[0] = cmd == READ ? IDEFLOPPY_READ12_CMD : IDEFLOPPY_WRITE12_CMD;
1142		put_unaligned(htonl(blocks), (unsigned int *) &pc->c[6]);
1143	} else {
1144		pc->c[0] = cmd == READ ? IDEFLOPPY_READ10_CMD : IDEFLOPPY_WRITE10_CMD;
1145		put_unaligned(htons(blocks), (unsigned short *) &pc->c[7]);
1146	}
1147	put_unaligned(htonl(block), (unsigned int *) &pc->c[2]);
1148	pc->callback = &idefloppy_rw_callback;
1149	pc->rq = rq;
1150	pc->b_count = cmd == READ ? 0 : rq->bio->bi_size;
1151	if (rq->cmd_flags & REQ_RW)
1152		set_bit(PC_WRITING, &pc->flags);
1153	pc->buffer = NULL;
1154	pc->request_transfer = pc->buffer_size = blocks * floppy->block_size;
1155	set_bit(PC_DMA_RECOMMENDED, &pc->flags);
1156}
1157
1158static void
1159idefloppy_blockpc_cmd(idefloppy_floppy_t *floppy, idefloppy_pc_t *pc, struct request *rq)
1160{
1161	idefloppy_init_pc(pc);
1162	pc->callback = &idefloppy_rw_callback;
1163	memcpy(pc->c, rq->cmd, sizeof(pc->c));
1164	pc->rq = rq;
1165	pc->b_count = rq->data_len;
1166	if (rq->data_len && rq_data_dir(rq) == WRITE)
1167		set_bit(PC_WRITING, &pc->flags);
1168	pc->buffer = rq->data;
1169	if (rq->bio)
1170		set_bit(PC_DMA_RECOMMENDED, &pc->flags);
1171
1172	/*
1173	 * possibly problematic, doesn't look like ide-floppy correctly
1174	 * handled scattered requests if dma fails...
1175	 */
1176	pc->request_transfer = pc->buffer_size = rq->data_len;
1177}
1178
1179/*
1180 *	idefloppy_do_request is our request handling function.
1181 */
1182static ide_startstop_t idefloppy_do_request (ide_drive_t *drive, struct request *rq, sector_t block_s)
1183{
1184	idefloppy_floppy_t *floppy = drive->driver_data;
1185	idefloppy_pc_t *pc;
1186	unsigned long block = (unsigned long)block_s;
1187
1188	debug_log(KERN_INFO "dev: %s, flags: %lx, errors: %d\n",
1189			rq->rq_disk ? rq->rq_disk->disk_name : "?",
1190			rq->flags, rq->errors);
1191	debug_log(KERN_INFO "sector: %ld, nr_sectors: %ld, "
1192			"current_nr_sectors: %d\n", (long)rq->sector,
1193			rq->nr_sectors, rq->current_nr_sectors);
1194
1195	if (rq->errors >= ERROR_MAX) {
1196		if (floppy->failed_pc != NULL) {
1197			if (idefloppy_should_report_error(floppy))
1198				printk(KERN_ERR "ide-floppy: %s: I/O error, pc = %2x,"
1199				       " key = %2x, asc = %2x, ascq = %2x\n",
1200				       drive->name, floppy->failed_pc->c[0],
1201				       floppy->sense_key, floppy->asc, floppy->ascq);
1202		}
1203		else
1204			printk(KERN_ERR "ide-floppy: %s: I/O error\n",
1205				drive->name);
1206		idefloppy_do_end_request(drive, 0, 0);
1207		return ide_stopped;
1208	}
1209	if (blk_fs_request(rq)) {
1210		if (((long)rq->sector % floppy->bs_factor) ||
1211		    (rq->nr_sectors % floppy->bs_factor)) {
1212			printk("%s: unsupported r/w request size\n",
1213				drive->name);
1214			idefloppy_do_end_request(drive, 0, 0);
1215			return ide_stopped;
1216		}
1217		pc = idefloppy_next_pc_storage(drive);
1218		idefloppy_create_rw_cmd(floppy, pc, rq, block);
1219	} else if (blk_special_request(rq)) {
1220		pc = (idefloppy_pc_t *) rq->buffer;
1221	} else if (blk_pc_request(rq)) {
1222		pc = idefloppy_next_pc_storage(drive);
1223		idefloppy_blockpc_cmd(floppy, pc, rq);
1224	} else {
1225		blk_dump_rq_flags(rq,
1226			"ide-floppy: unsupported command in queue");
1227		idefloppy_do_end_request(drive, 0, 0);
1228		return ide_stopped;
1229	}
1230
1231	pc->rq = rq;
1232	return idefloppy_issue_pc(drive, pc);
1233}
1234
1235/*
1236 *	idefloppy_queue_pc_tail adds a special packet command request to the
1237 *	tail of the request queue, and waits for it to be serviced.
1238 */
1239static int idefloppy_queue_pc_tail (ide_drive_t *drive,idefloppy_pc_t *pc)
1240{
1241	struct ide_floppy_obj *floppy = drive->driver_data;
1242	struct request rq;
1243
1244	ide_init_drive_cmd (&rq);
1245	rq.buffer = (char *) pc;
1246	rq.cmd_type = REQ_TYPE_SPECIAL;
1247	rq.rq_disk = floppy->disk;
1248
1249	return ide_do_drive_cmd(drive, &rq, ide_wait);
1250}
1251
1252/*
1253 *	Look at the flexible disk page parameters. We will ignore the CHS
1254 *	capacity parameters and use the LBA parameters instead.
1255 */
1256static int idefloppy_get_flexible_disk_page (ide_drive_t *drive)
1257{
1258	idefloppy_floppy_t *floppy = drive->driver_data;
1259	idefloppy_pc_t pc;
1260	idefloppy_mode_parameter_header_t *header;
1261	idefloppy_flexible_disk_page_t *page;
1262	int capacity, lba_capacity;
1263
1264	idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_FLEXIBLE_DISK_PAGE, MODE_SENSE_CURRENT);
1265	if (idefloppy_queue_pc_tail(drive,&pc)) {
1266		printk(KERN_ERR "ide-floppy: Can't get flexible disk "
1267			"page parameters\n");
1268		return 1;
1269	}
1270	header = (idefloppy_mode_parameter_header_t *) pc.buffer;
1271	floppy->wp = header->wp;
1272	set_disk_ro(floppy->disk, floppy->wp);
1273	page = (idefloppy_flexible_disk_page_t *) (header + 1);
1274
1275	page->transfer_rate = ntohs(page->transfer_rate);
1276	page->sector_size = ntohs(page->sector_size);
1277	page->cyls = ntohs(page->cyls);
1278	page->rpm = ntohs(page->rpm);
1279	capacity = page->cyls * page->heads * page->sectors * page->sector_size;
1280	if (memcmp (page, &floppy->flexible_disk_page, sizeof (idefloppy_flexible_disk_page_t)))
1281		printk(KERN_INFO "%s: %dkB, %d/%d/%d CHS, %d kBps, "
1282				"%d sector size, %d rpm\n",
1283			drive->name, capacity / 1024, page->cyls,
1284			page->heads, page->sectors,
1285			page->transfer_rate / 8, page->sector_size, page->rpm);
1286
1287	floppy->flexible_disk_page = *page;
1288	drive->bios_cyl = page->cyls;
1289	drive->bios_head = page->heads;
1290	drive->bios_sect = page->sectors;
1291	lba_capacity = floppy->blocks * floppy->block_size;
1292	if (capacity < lba_capacity) {
1293		printk(KERN_NOTICE "%s: The disk reports a capacity of %d "
1294			"bytes, but the drive only handles %d\n",
1295			drive->name, lba_capacity, capacity);
1296		floppy->blocks = floppy->block_size ? capacity / floppy->block_size : 0;
1297	}
1298	return 0;
1299}
1300
1301static int idefloppy_get_capability_page(ide_drive_t *drive)
1302{
1303	idefloppy_floppy_t *floppy = drive->driver_data;
1304	idefloppy_pc_t pc;
1305	idefloppy_mode_parameter_header_t *header;
1306	idefloppy_capabilities_page_t *page;
1307
1308	floppy->srfp = 0;
1309	idefloppy_create_mode_sense_cmd(&pc, IDEFLOPPY_CAPABILITIES_PAGE,
1310						 MODE_SENSE_CURRENT);
1311
1312	set_bit(PC_SUPPRESS_ERROR, &pc.flags);
1313	if (idefloppy_queue_pc_tail(drive,&pc)) {
1314		return 1;
1315	}
1316
1317	header = (idefloppy_mode_parameter_header_t *) pc.buffer;
1318	page= (idefloppy_capabilities_page_t *)(header+1);
1319	floppy->srfp = page->srfp;
1320	return (0);
1321}
1322
1323/*
1324 *	Determine if a media is present in the floppy drive, and if so,
1325 *	its LBA capacity.
1326 */
1327static int idefloppy_get_capacity (ide_drive_t *drive)
1328{
1329	idefloppy_floppy_t *floppy = drive->driver_data;
1330	idefloppy_pc_t pc;
1331	idefloppy_capacity_header_t *header;
1332	idefloppy_capacity_descriptor_t *descriptor;
1333	int i, descriptors, rc = 1, blocks, length;
1334
1335	drive->bios_cyl = 0;
1336	drive->bios_head = drive->bios_sect = 0;
1337	floppy->blocks = 0;
1338	floppy->bs_factor = 1;
1339	set_capacity(floppy->disk, 0);
1340
1341	idefloppy_create_read_capacity_cmd(&pc);
1342	if (idefloppy_queue_pc_tail(drive, &pc)) {
1343		printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
1344		return 1;
1345	}
1346	header = (idefloppy_capacity_header_t *) pc.buffer;
1347	descriptors = header->length / sizeof(idefloppy_capacity_descriptor_t);
1348	descriptor = (idefloppy_capacity_descriptor_t *) (header + 1);
1349
1350	for (i = 0; i < descriptors; i++, descriptor++) {
1351		blocks = descriptor->blocks = ntohl(descriptor->blocks);
1352		length = descriptor->length = ntohs(descriptor->length);
1353
1354		if (!i)
1355		{
1356		switch (descriptor->dc) {
1357		/* Clik! drive returns this instead of CAPACITY_CURRENT */
1358		case CAPACITY_UNFORMATTED:
1359			if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags))
1360                                /*
1361				 * If it is not a clik drive, break out
1362				 * (maintains previous driver behaviour)
1363				 */
1364				break;
1365		case CAPACITY_CURRENT:
1366			/* Normal Zip/LS-120 disks */
1367			if (memcmp(descriptor, &floppy->capacity, sizeof (idefloppy_capacity_descriptor_t)))
1368				printk(KERN_INFO "%s: %dkB, %d blocks, %d "
1369					"sector size\n", drive->name,
1370					blocks * length / 1024, blocks, length);
1371			floppy->capacity = *descriptor;
1372			if (!length || length % 512) {
1373				printk(KERN_NOTICE "%s: %d bytes block size "
1374					"not supported\n", drive->name, length);
1375			} else {
1376                                floppy->blocks = blocks;
1377                                floppy->block_size = length;
1378                                if ((floppy->bs_factor = length / 512) != 1)
1379                                        printk(KERN_NOTICE "%s: warning: non "
1380						"512 bytes block size not "
1381						"fully supported\n",
1382						drive->name);
1383                                rc = 0;
1384			}
1385			break;
1386		case CAPACITY_NO_CARTRIDGE:
1387			/*
1388			 * This is a KERN_ERR so it appears on screen
1389			 * for the user to see
1390			 */
1391			printk(KERN_ERR "%s: No disk in drive\n", drive->name);
1392			break;
1393		case CAPACITY_INVALID:
1394			printk(KERN_ERR "%s: Invalid capacity for disk "
1395				"in drive\n", drive->name);
1396			break;
1397		}
1398		}
1399		if (!i) {
1400			debug_log( "Descriptor 0 Code: %d\n",
1401				descriptor->dc);
1402		}
1403		debug_log( "Descriptor %d: %dkB, %d blocks, %d "
1404			"sector size\n", i, blocks * length / 1024, blocks,
1405			length);
1406	}
1407
1408	/* Clik! disk does not support get_flexible_disk_page */
1409        if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags)) {
1410		(void) idefloppy_get_flexible_disk_page(drive);
1411	}
1412
1413	set_capacity(floppy->disk, floppy->blocks * floppy->bs_factor);
1414	return rc;
1415}
1416
1417/*
1418** Obtain the list of formattable capacities.
1419** Very similar to idefloppy_get_capacity, except that we push the capacity
1420** descriptors to userland, instead of our own structures.
1421**
1422** Userland gives us the following structure:
1423**
1424** struct idefloppy_format_capacities {
1425**        int nformats;
1426**        struct {
1427**                int nblocks;
1428**                int blocksize;
1429**                } formats[];
1430**        } ;
1431**
1432** userland initializes nformats to the number of allocated formats[]
1433** records.  On exit we set nformats to the number of records we've
1434** actually initialized.
1435**
1436*/
1437
1438static int idefloppy_get_format_capacities(ide_drive_t *drive, int __user *arg)
1439{
1440        idefloppy_pc_t pc;
1441	idefloppy_capacity_header_t *header;
1442        idefloppy_capacity_descriptor_t *descriptor;
1443	int i, descriptors, blocks, length;
1444	int u_array_size;
1445	int u_index;
1446	int __user *argp;
1447
1448	if (get_user(u_array_size, arg))
1449		return (-EFAULT);
1450
1451	if (u_array_size <= 0)
1452		return (-EINVAL);
1453
1454	idefloppy_create_read_capacity_cmd(&pc);
1455	if (idefloppy_queue_pc_tail(drive, &pc)) {
1456		printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
1457                return (-EIO);
1458        }
1459        header = (idefloppy_capacity_header_t *) pc.buffer;
1460        descriptors = header->length /
1461		sizeof(idefloppy_capacity_descriptor_t);
1462	descriptor = (idefloppy_capacity_descriptor_t *) (header + 1);
1463
1464	u_index = 0;
1465	argp = arg + 1;
1466
1467	/*
1468	** We always skip the first capacity descriptor.  That's the
1469	** current capacity.  We are interested in the remaining descriptors,
1470	** the formattable capacities.
1471	*/
1472
1473	for (i=0; i<descriptors; i++, descriptor++) {
1474		if (u_index >= u_array_size)
1475			break;	/* User-supplied buffer too small */
1476		if (i == 0)
1477			continue;	/* Skip the first descriptor */
1478
1479		blocks = ntohl(descriptor->blocks);
1480		length = ntohs(descriptor->length);
1481
1482		if (put_user(blocks, argp))
1483			return(-EFAULT);
1484		++argp;
1485
1486		if (put_user(length, argp))
1487			return (-EFAULT);
1488		++argp;
1489
1490		++u_index;
1491	}
1492
1493	if (put_user(u_index, arg))
1494		return (-EFAULT);
1495	return (0);
1496}
1497
1498/*
1499** Send ATAPI_FORMAT_UNIT to the drive.
1500**
1501** Userland gives us the following structure:
1502**
1503** struct idefloppy_format_command {
1504**        int nblocks;
1505**        int blocksize;
1506**        int flags;
1507**        } ;
1508**
1509** flags is a bitmask, currently, the only defined flag is:
1510**
1511**        0x01 - verify media after format.
1512*/
1513
1514static int idefloppy_begin_format(ide_drive_t *drive, int __user *arg)
1515{
1516	int blocks;
1517	int length;
1518	int flags;
1519	idefloppy_pc_t pc;
1520
1521	if (get_user(blocks, arg) ||
1522	    get_user(length, arg+1) ||
1523	    get_user(flags, arg+2)) {
1524		return (-EFAULT);
1525	}
1526
1527	/* Get the SFRP bit */
1528	(void) idefloppy_get_capability_page(drive);
1529	idefloppy_create_format_unit_cmd(&pc, blocks, length, flags);
1530	if (idefloppy_queue_pc_tail(drive, &pc)) {
1531                return (-EIO);
1532	}
1533
1534	return (0);
1535}
1536
1537/*
1538** Get ATAPI_FORMAT_UNIT progress indication.
1539**
1540** Userland gives a pointer to an int.  The int is set to a progress
1541** indicator 0-65536, with 65536=100%.
1542**
1543** If the drive does not support format progress indication, we just check
1544** the dsc bit, and return either 0 or 65536.
1545*/
1546
1547static int idefloppy_get_format_progress(ide_drive_t *drive, int __user *arg)
1548{
1549	idefloppy_floppy_t *floppy = drive->driver_data;
1550	idefloppy_pc_t pc;
1551	int progress_indication = 0x10000;
1552
1553	if (floppy->srfp) {
1554		idefloppy_create_request_sense_cmd(&pc);
1555		if (idefloppy_queue_pc_tail(drive, &pc)) {
1556			return (-EIO);
1557		}
1558
1559		if (floppy->sense_key == 2 &&
1560		    floppy->asc == 4 &&
1561		    floppy->ascq == 4) {
1562			progress_indication = floppy->progress_indication;
1563		}
1564		/* Else assume format_unit has finished, and we're
1565		** at 0x10000 */
1566	} else {
1567		unsigned long flags;
1568		u8 stat;
1569
1570		local_irq_save(flags);
1571		stat = drive->hwif->INB(IDE_STATUS_REG);
1572		local_irq_restore(flags);
1573
1574		progress_indication = ((stat & SEEK_STAT) == 0) ? 0 : 0x10000;
1575	}
1576	if (put_user(progress_indication, arg))
1577		return (-EFAULT);
1578
1579	return (0);
1580}
1581
1582/*
1583 *	Return the current floppy capacity.
1584 */
1585static sector_t idefloppy_capacity (ide_drive_t *drive)
1586{
1587	idefloppy_floppy_t *floppy = drive->driver_data;
1588	unsigned long capacity = floppy->blocks * floppy->bs_factor;
1589
1590	return capacity;
1591}
1592
1593/*
1594 *	idefloppy_identify_device checks if we can support a drive,
1595 *	based on the ATAPI IDENTIFY command results.
1596 */
1597static int idefloppy_identify_device (ide_drive_t *drive,struct hd_driveid *id)
1598{
1599	struct idefloppy_id_gcw gcw;
1600#if IDEFLOPPY_DEBUG_INFO
1601	char buffer[80];
1602#endif /* IDEFLOPPY_DEBUG_INFO */
1603
1604	*((u16 *) &gcw) = id->config;
1605
1606#ifdef CONFIG_PPC
1607	/* kludge for Apple PowerBook internal zip */
1608	if ((gcw.device_type == 5) &&
1609	    !strstr(id->model, "CD-ROM") &&
1610	    strstr(id->model, "ZIP"))
1611		gcw.device_type = 0;
1612#endif
1613
1614#if IDEFLOPPY_DEBUG_INFO
1615	printk(KERN_INFO "Dumping ATAPI Identify Device floppy parameters\n");
1616	switch (gcw.protocol) {
1617		case 0: case 1: sprintf(buffer, "ATA");break;
1618		case 2:	sprintf(buffer, "ATAPI");break;
1619		case 3: sprintf(buffer, "Reserved (Unknown to ide-floppy)");break;
1620	}
1621	printk(KERN_INFO "Protocol Type: %s\n", buffer);
1622	switch (gcw.device_type) {
1623		case 0: sprintf(buffer, "Direct-access Device");break;
1624		case 1: sprintf(buffer, "Streaming Tape Device");break;
1625		case 2: case 3: case 4: sprintf (buffer, "Reserved");break;
1626		case 5: sprintf(buffer, "CD-ROM Device");break;
1627		case 6: sprintf(buffer, "Reserved");
1628		case 7: sprintf(buffer, "Optical memory Device");break;
1629		case 0x1f: sprintf(buffer, "Unknown or no Device type");break;
1630		default: sprintf(buffer, "Reserved");
1631	}
1632	printk(KERN_INFO "Device Type: %x - %s\n", gcw.device_type, buffer);
1633	printk(KERN_INFO "Removable: %s\n",gcw.removable ? "Yes":"No");
1634	switch (gcw.drq_type) {
1635		case 0: sprintf(buffer, "Microprocessor DRQ");break;
1636		case 1: sprintf(buffer, "Interrupt DRQ");break;
1637		case 2: sprintf(buffer, "Accelerated DRQ");break;
1638		case 3: sprintf(buffer, "Reserved");break;
1639	}
1640	printk(KERN_INFO "Command Packet DRQ Type: %s\n", buffer);
1641	switch (gcw.packet_size) {
1642		case 0: sprintf(buffer, "12 bytes");break;
1643		case 1: sprintf(buffer, "16 bytes");break;
1644		default: sprintf(buffer, "Reserved");break;
1645	}
1646	printk(KERN_INFO "Command Packet Size: %s\n", buffer);
1647#endif /* IDEFLOPPY_DEBUG_INFO */
1648
1649	if (gcw.protocol != 2)
1650		printk(KERN_ERR "ide-floppy: Protocol is not ATAPI\n");
1651	else if (gcw.device_type != 0)
1652		printk(KERN_ERR "ide-floppy: Device type is not set to floppy\n");
1653	else if (!gcw.removable)
1654		printk(KERN_ERR "ide-floppy: The removable flag is not set\n");
1655	else if (gcw.drq_type == 3) {
1656		printk(KERN_ERR "ide-floppy: Sorry, DRQ type %d not supported\n", gcw.drq_type);
1657	} else if (gcw.packet_size != 0) {
1658		printk(KERN_ERR "ide-floppy: Packet size is not 12 bytes long\n");
1659	} else
1660		return 1;
1661	return 0;
1662}
1663
1664#ifdef CONFIG_IDE_PROC_FS
1665static void idefloppy_add_settings(ide_drive_t *drive)
1666{
1667	idefloppy_floppy_t *floppy = drive->driver_data;
1668
1669/*
1670 *			drive	setting name	read/write	data type	min	max	mul_factor	div_factor	data pointer		set function
1671 */
1672	ide_add_setting(drive,	"bios_cyl",	SETTING_RW,	TYPE_INT,	0,	1023,		1,		1,	&drive->bios_cyl,	NULL);
1673	ide_add_setting(drive,	"bios_head",	SETTING_RW,	TYPE_BYTE,	0,	255,		1,		1,	&drive->bios_head,	NULL);
1674	ide_add_setting(drive,	"bios_sect",	SETTING_RW,	TYPE_BYTE,	0,	63,		1,		1,	&drive->bios_sect,	NULL);
1675	ide_add_setting(drive,	"ticks",	SETTING_RW,	TYPE_BYTE,	0,	255,		1,		1,	&floppy->ticks,		NULL);
1676}
1677#else
1678static inline void idefloppy_add_settings(ide_drive_t *drive) { ; }
1679#endif
1680
1681/*
1682 *	Driver initialization.
1683 */
1684static void idefloppy_setup (ide_drive_t *drive, idefloppy_floppy_t *floppy)
1685{
1686	struct idefloppy_id_gcw gcw;
1687
1688	*((u16 *) &gcw) = drive->id->config;
1689	floppy->pc = floppy->pc_stack;
1690	if (gcw.drq_type == 1)
1691		set_bit(IDEFLOPPY_DRQ_INTERRUPT, &floppy->flags);
1692	/*
1693	 *	We used to check revisions here. At this point however
1694	 *	I'm giving up. Just assume they are all broken, its easier.
1695	 *
1696	 *	The actual reason for the workarounds was likely
1697	 *	a driver bug after all rather than a firmware bug,
1698	 *	and the workaround below used to hide it. It should
1699	 *	be fixed as of version 1.9, but to be on the safe side
1700	 *	we'll leave the limitation below for the 2.2.x tree.
1701	 */
1702
1703	if (!strncmp(drive->id->model, "IOMEGA ZIP 100 ATAPI", 20)) {
1704		set_bit(IDEFLOPPY_ZIP_DRIVE, &floppy->flags);
1705		/* This value will be visible in the /proc/ide/hdx/settings */
1706		floppy->ticks = IDEFLOPPY_TICKS_DELAY;
1707		blk_queue_max_sectors(drive->queue, 64);
1708	}
1709
1710	/*
1711	*      Guess what?  The IOMEGA Clik! drive also needs the
1712	*      above fix.  It makes nasty clicking noises without
1713	*      it, so please don't remove this.
1714	*/
1715	if (strncmp(drive->id->model, "IOMEGA Clik!", 11) == 0) {
1716		blk_queue_max_sectors(drive->queue, 64);
1717		set_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags);
1718	}
1719
1720
1721	(void) idefloppy_get_capacity(drive);
1722	idefloppy_add_settings(drive);
1723}
1724
1725static void ide_floppy_remove(ide_drive_t *drive)
1726{
1727	idefloppy_floppy_t *floppy = drive->driver_data;
1728	struct gendisk *g = floppy->disk;
1729
1730	ide_proc_unregister_driver(drive, floppy->driver);
1731
1732	del_gendisk(g);
1733
1734	ide_floppy_put(floppy);
1735}
1736
1737static void ide_floppy_release(struct kref *kref)
1738{
1739	struct ide_floppy_obj *floppy = to_ide_floppy(kref);
1740	ide_drive_t *drive = floppy->drive;
1741	struct gendisk *g = floppy->disk;
1742
1743	drive->driver_data = NULL;
1744	g->private_data = NULL;
1745	put_disk(g);
1746	kfree(floppy);
1747}
1748
1749#ifdef CONFIG_IDE_PROC_FS
1750static int proc_idefloppy_read_capacity
1751	(char *page, char **start, off_t off, int count, int *eof, void *data)
1752{
1753	ide_drive_t*drive = (ide_drive_t *)data;
1754	int len;
1755
1756	len = sprintf(page,"%llu\n", (long long)idefloppy_capacity(drive));
1757	PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
1758}
1759
1760static ide_proc_entry_t idefloppy_proc[] = {
1761	{ "capacity",	S_IFREG|S_IRUGO,	proc_idefloppy_read_capacity, NULL },
1762	{ "geometry",	S_IFREG|S_IRUGO,	proc_ide_read_geometry,	NULL },
1763	{ NULL, 0, NULL, NULL }
1764};
1765#endif	/* CONFIG_IDE_PROC_FS */
1766
1767static int ide_floppy_probe(ide_drive_t *);
1768
1769static ide_driver_t idefloppy_driver = {
1770	.gen_driver = {
1771		.owner		= THIS_MODULE,
1772		.name		= "ide-floppy",
1773		.bus		= &ide_bus_type,
1774	},
1775	.probe			= ide_floppy_probe,
1776	.remove			= ide_floppy_remove,
1777	.version		= IDEFLOPPY_VERSION,
1778	.media			= ide_floppy,
1779	.supports_dsc_overlap	= 0,
1780	.do_request		= idefloppy_do_request,
1781	.end_request		= idefloppy_do_end_request,
1782	.error			= __ide_error,
1783	.abort			= __ide_abort,
1784#ifdef CONFIG_IDE_PROC_FS
1785	.proc			= idefloppy_proc,
1786#endif
1787};
1788
1789static int idefloppy_open(struct inode *inode, struct file *filp)
1790{
1791	struct gendisk *disk = inode->i_bdev->bd_disk;
1792	struct ide_floppy_obj *floppy;
1793	ide_drive_t *drive;
1794	idefloppy_pc_t pc;
1795	int ret = 0;
1796
1797	debug_log(KERN_INFO "Reached idefloppy_open\n");
1798
1799	if (!(floppy = ide_floppy_get(disk)))
1800		return -ENXIO;
1801
1802	drive = floppy->drive;
1803
1804	floppy->openers++;
1805
1806	if (floppy->openers == 1) {
1807		clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
1808		/* Just in case */
1809
1810		idefloppy_create_test_unit_ready_cmd(&pc);
1811		if (idefloppy_queue_pc_tail(drive, &pc)) {
1812			idefloppy_create_start_stop_cmd(&pc, 1);
1813			(void) idefloppy_queue_pc_tail(drive, &pc);
1814		}
1815
1816		if (idefloppy_get_capacity (drive)
1817		   && (filp->f_flags & O_NDELAY) == 0
1818		    /*
1819		    ** Allow O_NDELAY to open a drive without a disk, or with
1820		    ** an unreadable disk, so that we can get the format
1821		    ** capacity of the drive or begin the format - Sam
1822		    */
1823		    ) {
1824			ret = -EIO;
1825			goto out_put_floppy;
1826		}
1827
1828		if (floppy->wp && (filp->f_mode & 2)) {
1829			ret = -EROFS;
1830			goto out_put_floppy;
1831		}
1832		set_bit(IDEFLOPPY_MEDIA_CHANGED, &floppy->flags);
1833		/* IOMEGA Clik! drives do not support lock/unlock commands */
1834                if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags)) {
1835			idefloppy_create_prevent_cmd(&pc, 1);
1836			(void) idefloppy_queue_pc_tail(drive, &pc);
1837		}
1838		check_disk_change(inode->i_bdev);
1839	} else if (test_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags)) {
1840		ret = -EBUSY;
1841		goto out_put_floppy;
1842	}
1843	return 0;
1844
1845out_put_floppy:
1846	floppy->openers--;
1847	ide_floppy_put(floppy);
1848	return ret;
1849}
1850
1851static int idefloppy_release(struct inode *inode, struct file *filp)
1852{
1853	struct gendisk *disk = inode->i_bdev->bd_disk;
1854	struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1855	ide_drive_t *drive = floppy->drive;
1856	idefloppy_pc_t pc;
1857
1858	debug_log(KERN_INFO "Reached idefloppy_release\n");
1859
1860	if (floppy->openers == 1) {
1861		/* IOMEGA Clik! drives do not support lock/unlock commands */
1862                if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags)) {
1863			idefloppy_create_prevent_cmd(&pc, 0);
1864			(void) idefloppy_queue_pc_tail(drive, &pc);
1865		}
1866
1867		clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
1868	}
1869
1870	floppy->openers--;
1871
1872	ide_floppy_put(floppy);
1873
1874	return 0;
1875}
1876
1877static int idefloppy_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1878{
1879	struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
1880	ide_drive_t *drive = floppy->drive;
1881
1882	geo->heads = drive->bios_head;
1883	geo->sectors = drive->bios_sect;
1884	geo->cylinders = (u16)drive->bios_cyl; /* truncate */
1885	return 0;
1886}
1887
1888static int idefloppy_ioctl(struct inode *inode, struct file *file,
1889			unsigned int cmd, unsigned long arg)
1890{
1891	struct block_device *bdev = inode->i_bdev;
1892	struct ide_floppy_obj *floppy = ide_floppy_g(bdev->bd_disk);
1893	ide_drive_t *drive = floppy->drive;
1894	void __user *argp = (void __user *)arg;
1895	int err;
1896	int prevent = (arg) ? 1 : 0;
1897	idefloppy_pc_t pc;
1898
1899	switch (cmd) {
1900	case CDROMEJECT:
1901		prevent = 0;
1902		/* fall through */
1903	case CDROM_LOCKDOOR:
1904		if (floppy->openers > 1)
1905			return -EBUSY;
1906
1907		/* The IOMEGA Clik! Drive doesn't support this command - no room for an eject mechanism */
1908                if (!test_bit(IDEFLOPPY_CLIK_DRIVE, &floppy->flags)) {
1909			idefloppy_create_prevent_cmd(&pc, prevent);
1910			(void) idefloppy_queue_pc_tail(drive, &pc);
1911		}
1912		if (cmd == CDROMEJECT) {
1913			idefloppy_create_start_stop_cmd(&pc, 2);
1914			(void) idefloppy_queue_pc_tail(drive, &pc);
1915		}
1916		return 0;
1917	case IDEFLOPPY_IOCTL_FORMAT_SUPPORTED:
1918		return 0;
1919	case IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY:
1920		return idefloppy_get_format_capacities(drive, argp);
1921	case IDEFLOPPY_IOCTL_FORMAT_START:
1922
1923		if (!(file->f_mode & 2))
1924			return -EPERM;
1925
1926		if (floppy->openers > 1) {
1927			/* Don't format if someone is using the disk */
1928
1929			clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS,
1930				  &floppy->flags);
1931			return -EBUSY;
1932		}
1933
1934		set_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
1935
1936		err = idefloppy_begin_format(drive, argp);
1937		if (err)
1938			clear_bit(IDEFLOPPY_FORMAT_IN_PROGRESS, &floppy->flags);
1939		return err;
1940		/*
1941		** Note, the bit will be cleared when the device is
1942		** closed.  This is the cleanest way to handle the
1943		** situation where the drive does not support
1944		** format progress reporting.
1945		*/
1946	case IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS:
1947		return idefloppy_get_format_progress(drive, argp);
1948	}
1949
1950	/*
1951	 * skip SCSI_IOCTL_SEND_COMMAND (deprecated)
1952	 * and CDROM_SEND_PACKET (legacy) ioctls
1953	 */
1954	if (cmd != CDROM_SEND_PACKET && cmd != SCSI_IOCTL_SEND_COMMAND)
1955		err = scsi_cmd_ioctl(file, bdev->bd_disk->queue,
1956					bdev->bd_disk, cmd, argp);
1957	else
1958		err = -ENOTTY;
1959
1960	if (err == -ENOTTY)
1961		err = generic_ide_ioctl(drive, file, bdev, cmd, arg);
1962
1963	return err;
1964}
1965
1966static int idefloppy_media_changed(struct gendisk *disk)
1967{
1968	struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1969	ide_drive_t *drive = floppy->drive;
1970
1971	/* do not scan partitions twice if this is a removable device */
1972	if (drive->attach) {
1973		drive->attach = 0;
1974		return 0;
1975	}
1976	return test_and_clear_bit(IDEFLOPPY_MEDIA_CHANGED, &floppy->flags);
1977}
1978
1979static int idefloppy_revalidate_disk(struct gendisk *disk)
1980{
1981	struct ide_floppy_obj *floppy = ide_floppy_g(disk);
1982	set_capacity(disk, idefloppy_capacity(floppy->drive));
1983	return 0;
1984}
1985
1986static struct block_device_operations idefloppy_ops = {
1987	.owner		= THIS_MODULE,
1988	.open		= idefloppy_open,
1989	.release	= idefloppy_release,
1990	.ioctl		= idefloppy_ioctl,
1991	.getgeo		= idefloppy_getgeo,
1992	.media_changed	= idefloppy_media_changed,
1993	.revalidate_disk= idefloppy_revalidate_disk
1994};
1995
1996static int ide_floppy_probe(ide_drive_t *drive)
1997{
1998	idefloppy_floppy_t *floppy;
1999	struct gendisk *g;
2000
2001	if (!strstr("ide-floppy", drive->driver_req))
2002		goto failed;
2003	if (!drive->present)
2004		goto failed;
2005	if (drive->media != ide_floppy)
2006		goto failed;
2007	if (!idefloppy_identify_device (drive, drive->id)) {
2008		printk (KERN_ERR "ide-floppy: %s: not supported by this version of ide-floppy\n", drive->name);
2009		goto failed;
2010	}
2011	if (drive->scsi) {
2012		printk("ide-floppy: passing drive %s to ide-scsi emulation.\n", drive->name);
2013		goto failed;
2014	}
2015	if ((floppy = kzalloc(sizeof (idefloppy_floppy_t), GFP_KERNEL)) == NULL) {
2016		printk (KERN_ERR "ide-floppy: %s: Can't allocate a floppy structure\n", drive->name);
2017		goto failed;
2018	}
2019
2020	g = alloc_disk(1 << PARTN_BITS);
2021	if (!g)
2022		goto out_free_floppy;
2023
2024	ide_init_disk(g, drive);
2025
2026	ide_proc_register_driver(drive, &idefloppy_driver);
2027
2028	kref_init(&floppy->kref);
2029
2030	floppy->drive = drive;
2031	floppy->driver = &idefloppy_driver;
2032	floppy->disk = g;
2033
2034	g->private_data = &floppy->driver;
2035
2036	drive->driver_data = floppy;
2037
2038	idefloppy_setup (drive, floppy);
2039
2040	g->minors = 1 << PARTN_BITS;
2041	g->driverfs_dev = &drive->gendev;
2042	g->flags = drive->removable ? GENHD_FL_REMOVABLE : 0;
2043	g->fops = &idefloppy_ops;
2044	drive->attach = 1;
2045	add_disk(g);
2046	return 0;
2047
2048out_free_floppy:
2049	kfree(floppy);
2050failed:
2051	return -ENODEV;
2052}
2053
2054MODULE_DESCRIPTION("ATAPI FLOPPY Driver");
2055
2056static void __exit idefloppy_exit (void)
2057{
2058	driver_unregister(&idefloppy_driver.gen_driver);
2059}
2060
2061static int __init idefloppy_init(void)
2062{
2063	printk("ide-floppy driver " IDEFLOPPY_VERSION "\n");
2064	return driver_register(&idefloppy_driver.gen_driver);
2065}
2066
2067MODULE_ALIAS("ide:*m-floppy*");
2068module_init(idefloppy_init);
2069module_exit(idefloppy_exit);
2070MODULE_LICENSE("GPL");
2071