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