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