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