File: /usr/src/linux/drivers/block/floppy.c

1     /*
2      *  linux/drivers/block/floppy.c
3      *
4      *  Copyright (C) 1991, 1992  Linus Torvalds
5      *  Copyright (C) 1993, 1994  Alain Knaff
6      *  Copyright (C) 1998 Alan Cox
7      */
8     /*
9      * 02.12.91 - Changed to static variables to indicate need for reset
10      * and recalibrate. This makes some things easier (output_byte reset
11      * checking etc), and means less interrupt jumping in case of errors,
12      * so the code is hopefully easier to understand.
13      */
14     
15     /*
16      * This file is certainly a mess. I've tried my best to get it working,
17      * but I don't like programming floppies, and I have only one anyway.
18      * Urgel. I should check for more errors, and do more graceful error
19      * recovery. Seems there are problems with several drives. I've tried to
20      * correct them. No promises.
21      */
22     
23     /*
24      * As with hd.c, all routines within this file can (and will) be called
25      * by interrupts, so extreme caution is needed. A hardware interrupt
26      * handler may not sleep, or a kernel panic will happen. Thus I cannot
27      * call "floppy-on" directly, but have to set a special timer interrupt
28      * etc.
29      */
30     
31     /*
32      * 28.02.92 - made track-buffering routines, based on the routines written
33      * by entropy@wintermute.wpi.edu (Lawrence Foard). Linus.
34      */
35     
36     /*
37      * Automatic floppy-detection and formatting written by Werner Almesberger
38      * (almesber@nessie.cs.id.ethz.ch), who also corrected some problems with
39      * the floppy-change signal detection.
40      */
41     
42     /*
43      * 1992/7/22 -- Hennus Bergman: Added better error reporting, fixed
44      * FDC data overrun bug, added some preliminary stuff for vertical
45      * recording support.
46      *
47      * 1992/9/17: Added DMA allocation & DMA functions. -- hhb.
48      *
49      * TODO: Errors are still not counted properly.
50      */
51     
52     /* 1992/9/20
53      * Modifications for ``Sector Shifting'' by Rob Hooft (hooft@chem.ruu.nl)
54      * modeled after the freeware MS-DOS program fdformat/88 V1.8 by
55      * Christoph H. Hochst\"atter.
56      * I have fixed the shift values to the ones I always use. Maybe a new
57      * ioctl() should be created to be able to modify them.
58      * There is a bug in the driver that makes it impossible to format a
59      * floppy as the first thing after bootup.
60      */
61     
62     /*
63      * 1993/4/29 -- Linus -- cleaned up the timer handling in the kernel, and
64      * this helped the floppy driver as well. Much cleaner, and still seems to
65      * work.
66      */
67     
68     /* 1994/6/24 --bbroad-- added the floppy table entries and made
69      * minor modifications to allow 2.88 floppies to be run.
70      */
71     
72     /* 1994/7/13 -- Paul Vojta -- modified the probing code to allow three or more
73      * disk types.
74      */
75     
76     /*
77      * 1994/8/8 -- Alain Knaff -- Switched to fdpatch driver: Support for bigger
78      * format bug fixes, but unfortunately some new bugs too...
79      */
80     
81     /* 1994/9/17 -- Koen Holtman -- added logging of physical floppy write
82      * errors to allow safe writing by specialized programs.
83      */
84     
85     /* 1995/4/24 -- Dan Fandrich -- added support for Commodore 1581 3.5" disks
86      * by defining bit 1 of the "stretch" parameter to mean put sectors on the
87      * opposite side of the disk, leaving the sector IDs alone (i.e. Commodore's
88      * drives are "upside-down").
89      */
90     
91     /*
92      * 1995/8/26 -- Andreas Busse -- added Mips support.
93      */
94     
95     /*
96      * 1995/10/18 -- Ralf Baechle -- Portability cleanup; move machine dependent
97      * features to asm/floppy.h.
98      */
99     
100     /*
101      * 1998/05/07 -- Russell King -- More portability cleanups; moved definition of
102      * interrupt and dma channel to asm/floppy.h. Cleaned up some formatting &
103      * use of '0' for NULL.
104      */
105      
106     /*
107      * 1998/06/07 -- Alan Cox -- Merged the 2.0.34 fixes for resource allocation
108      * failures.
109      */
110     
111     /*
112      * 1998/09/20 -- David Weinehall -- Added slow-down code for buggy PS/2-drives.
113      */
114     
115     /*
116      * 1999/08/13 -- Paul Slootman -- floppy stopped working on Alpha after 24
117      * days, 6 hours, 32 minutes and 32 seconds (i.e. MAXINT jiffies; ints were
118      * being used to store jiffies, which are unsigned longs).
119      */
120     
121     /*
122      * 2000/08/28 -- Arnaldo Carvalho de Melo <acme@conectiva.com.br>
123      * - get rid of check_region
124      * - s/suser/capable/
125      */
126     
127     /*
128      * 2001/08/26 -- Paul Gortmaker - fix insmod oops on machines with no
129      * floppy controller (lingering task on list after module is gone... boom.)
130      */
131     
132     #define FLOPPY_SANITY_CHECK
133     #undef  FLOPPY_SILENT_DCL_CLEAR
134     
135     #define REALLY_SLOW_IO
136     
137     #define DEBUGT 2
138     #define DCL_DEBUG /* debug disk change line */
139     
140     /* do print messages for unexpected interrupts */
141     static int print_unex=1;
142     #include <linux/module.h>
143     #include <linux/sched.h>
144     #include <linux/fs.h>
145     #include <linux/kernel.h>
146     #include <linux/timer.h>
147     #include <linux/tqueue.h>
148     #define FDPATCHES
149     #include <linux/fdreg.h>
150     
151     /*
152      * 1998/1/21 -- Richard Gooch <rgooch@atnf.csiro.au> -- devfs support
153      */
154     
155     
156     #include <linux/fd.h>
157     #include <linux/hdreg.h>
158     
159     #include <linux/errno.h>
160     #include <linux/slab.h>
161     #include <linux/mm.h>
162     #include <linux/string.h>
163     #include <linux/fcntl.h>
164     #include <linux/delay.h>
165     #include <linux/mc146818rtc.h> /* CMOS defines */
166     #include <linux/ioport.h>
167     #include <linux/interrupt.h>
168     #include <linux/init.h>
169     #include <linux/devfs_fs_kernel.h>
170     
171     /*
172      * PS/2 floppies have much slower step rates than regular floppies.
173      * It's been recommended that take about 1/4 of the default speed
174      * in some more extreme cases.
175      */
176     static int slow_floppy;
177     
178     #include <asm/dma.h>
179     #include <asm/irq.h>
180     #include <asm/system.h>
181     #include <asm/io.h>
182     #include <asm/uaccess.h>
183     
184     static int FLOPPY_IRQ=6;
185     static int FLOPPY_DMA=2;
186     static int can_use_virtual_dma=2;
187     /* =======
188      * can use virtual DMA:
189      * 0 = use of virtual DMA disallowed by config
190      * 1 = use of virtual DMA prescribed by config
191      * 2 = no virtual DMA preference configured.  By default try hard DMA,
192      * but fall back on virtual DMA when not enough memory available
193      */
194     
195     static int use_virtual_dma;
196     /* =======
197      * use virtual DMA
198      * 0 using hard DMA
199      * 1 using virtual DMA
200      * This variable is set to virtual when a DMA mem problem arises, and
201      * reset back in floppy_grab_irq_and_dma.
202      * It is not safe to reset it in other circumstances, because the floppy
203      * driver may have several buffers in use at once, and we do currently not
204      * record each buffers capabilities
205      */
206     
207     static unsigned short virtual_dma_port=0x3f0;
208     void floppy_interrupt(int irq, void *dev_id, struct pt_regs * regs);
209     static int set_dor(int fdc, char mask, char data);
210     static void register_devfs_entries (int drive) __init;
211     static devfs_handle_t devfs_handle;
212     
213     #define K_64	0x10000		/* 64KB */
214     
215     /* the following is the mask of allowed drives. By default units 2 and
216      * 3 of both floppy controllers are disabled, because switching on the
217      * motor of these drives causes system hangs on some PCI computers. drive
218      * 0 is the low bit (0x1), and drive 7 is the high bit (0x80). Bits are on if
219      * a drive is allowed.
220      *
221      * NOTE: This must come before we include the arch floppy header because
222      *       some ports reference this variable from there. -DaveM
223      */
224     
225     static int allowed_drive_mask = 0x33;
226     
227     #include <asm/floppy.h>
228     
229     static int irqdma_allocated;
230     
231     #define MAJOR_NR FLOPPY_MAJOR
232     
233     #include <linux/blk.h>
234     #include <linux/blkpg.h>
235     #include <linux/cdrom.h> /* for the compatibility eject ioctl */
236     
237     #ifndef fd_get_dma_residue
238     #define fd_get_dma_residue() get_dma_residue(FLOPPY_DMA)
239     #endif
240     
241     /* Dma Memory related stuff */
242     
243     #ifndef fd_dma_mem_free
244     #define fd_dma_mem_free(addr, size) free_pages(addr, get_order(size))
245     #endif
246     
247     #ifndef fd_dma_mem_alloc
248     #define fd_dma_mem_alloc(size) __get_dma_pages(GFP_KERNEL,get_order(size))
249     #endif
250     
251     static inline void fallback_on_nodma_alloc(char **addr, size_t l)
252     {
253     #ifdef FLOPPY_CAN_FALLBACK_ON_NODMA
254     	if (*addr)
255     		return; /* we have the memory */
256     	if (can_use_virtual_dma != 2)
257     		return; /* no fallback allowed */
258     	printk("DMA memory shortage. Temporarily falling back on virtual DMA\n");
259     	*addr = (char *) nodma_mem_alloc(l);
260     #else
261     	return;
262     #endif
263     }
264     
265     /* End dma memory related stuff */
266     
267     static unsigned long fake_change;
268     static int initialising=1;
269     
270     static inline int TYPE(kdev_t x) {
271     	return  (MINOR(x)>>2) & 0x1f;
272     }
273     static inline int DRIVE(kdev_t x) {
274     	return (MINOR(x)&0x03) | ((MINOR(x)&0x80) >> 5);
275     }
276     #define ITYPE(x) (((x)>>2) & 0x1f)
277     #define TOMINOR(x) ((x & 3) | ((x & 4) << 5))
278     #define UNIT(x) ((x) & 0x03)		/* drive on fdc */
279     #define FDC(x) (((x) & 0x04) >> 2)  /* fdc of drive */
280     #define REVDRIVE(fdc, unit) ((unit) + ((fdc) << 2))
281     				/* reverse mapping from unit and fdc to drive */
282     #define DP (&drive_params[current_drive])
283     #define DRS (&drive_state[current_drive])
284     #define DRWE (&write_errors[current_drive])
285     #define FDCS (&fdc_state[fdc])
286     #define CLEARF(x) (clear_bit(x##_BIT, &DRS->flags))
287     #define SETF(x) (set_bit(x##_BIT, &DRS->flags))
288     #define TESTF(x) (test_bit(x##_BIT, &DRS->flags))
289     
290     #define UDP (&drive_params[drive])
291     #define UDRS (&drive_state[drive])
292     #define UDRWE (&write_errors[drive])
293     #define UFDCS (&fdc_state[FDC(drive)])
294     #define UCLEARF(x) (clear_bit(x##_BIT, &UDRS->flags))
295     #define USETF(x) (set_bit(x##_BIT, &UDRS->flags))
296     #define UTESTF(x) (test_bit(x##_BIT, &UDRS->flags))
297     
298     #define DPRINT(format, args...) printk(DEVICE_NAME "%d: " format, current_drive , ## args)
299     
300     #define PH_HEAD(floppy,head) (((((floppy)->stretch & 2) >>1) ^ head) << 2)
301     #define STRETCH(floppy) ((floppy)->stretch & FD_STRETCH)
302     
303     #define CLEARSTRUCT(x) memset((x), 0, sizeof(*(x)))
304     
305     /* read/write */
306     #define COMMAND raw_cmd->cmd[0]
307     #define DR_SELECT raw_cmd->cmd[1]
308     #define TRACK raw_cmd->cmd[2]
309     #define HEAD raw_cmd->cmd[3]
310     #define SECTOR raw_cmd->cmd[4]
311     #define SIZECODE raw_cmd->cmd[5]
312     #define SECT_PER_TRACK raw_cmd->cmd[6]
313     #define GAP raw_cmd->cmd[7]
314     #define SIZECODE2 raw_cmd->cmd[8]
315     #define NR_RW 9
316     
317     /* format */
318     #define F_SIZECODE raw_cmd->cmd[2]
319     #define F_SECT_PER_TRACK raw_cmd->cmd[3]
320     #define F_GAP raw_cmd->cmd[4]
321     #define F_FILL raw_cmd->cmd[5]
322     #define NR_F 6
323     
324     /*
325      * Maximum disk size (in kilobytes). This default is used whenever the
326      * current disk size is unknown.
327      * [Now it is rather a minimum]
328      */
329     #define MAX_DISK_SIZE 4 /* 3984*/
330     
331     
332     /*
333      * globals used by 'result()'
334      */
335     #define MAX_REPLIES 16
336     static unsigned char reply_buffer[MAX_REPLIES];
337     static int inr; /* size of reply buffer, when called from interrupt */
338     #define ST0 (reply_buffer[0])
339     #define ST1 (reply_buffer[1])
340     #define ST2 (reply_buffer[2])
341     #define ST3 (reply_buffer[0]) /* result of GETSTATUS */
342     #define R_TRACK (reply_buffer[3])
343     #define R_HEAD (reply_buffer[4])
344     #define R_SECTOR (reply_buffer[5])
345     #define R_SIZECODE (reply_buffer[6])
346     
347     #define SEL_DLY (2*HZ/100)
348     
349     /*
350      * this struct defines the different floppy drive types.
351      */
352     static struct {
353     	struct floppy_drive_params params;
354     	const char *name; /* name printed while booting */
355     } default_drive_params[]= {
356     /* NOTE: the time values in jiffies should be in msec!
357      CMOS drive type
358       |     Maximum data rate supported by drive type
359       |     |   Head load time, msec
360       |     |   |   Head unload time, msec (not used)
361       |     |   |   |     Step rate interval, usec
362       |     |   |   |     |       Time needed for spinup time (jiffies)
363       |     |   |   |     |       |      Timeout for spinning down (jiffies)
364       |     |   |   |     |       |      |   Spindown offset (where disk stops)
365       |     |   |   |     |       |      |   |     Select delay
366       |     |   |   |     |       |      |   |     |     RPS
367       |     |   |   |     |       |      |   |     |     |    Max number of tracks
368       |     |   |   |     |       |      |   |     |     |    |     Interrupt timeout
369       |     |   |   |     |       |      |   |     |     |    |     |   Max nonintlv. sectors
370       |     |   |   |     |       |      |   |     |     |    |     |   | -Max Errors- flags */
371     {{0,  500, 16, 16, 8000,    1*HZ, 3*HZ,  0, SEL_DLY, 5,  80, 3*HZ, 20, {3,1,2,0,2}, 0,
372           0, { 7, 4, 8, 2, 1, 5, 3,10}, 3*HZ/2, 0 }, "unknown" },
373     
374     {{1,  300, 16, 16, 8000,    1*HZ, 3*HZ,  0, SEL_DLY, 5,  40, 3*HZ, 17, {3,1,2,0,2}, 0,
375           0, { 1, 0, 0, 0, 0, 0, 0, 0}, 3*HZ/2, 1 }, "360K PC" }, /*5 1/4 360 KB PC*/
376     
377     {{2,  500, 16, 16, 6000, 4*HZ/10, 3*HZ, 14, SEL_DLY, 6,  83, 3*HZ, 17, {3,1,2,0,2}, 0,
378           0, { 2, 5, 6,23,10,20,12, 0}, 3*HZ/2, 2 }, "1.2M" }, /*5 1/4 HD AT*/
379     
380     {{3,  250, 16, 16, 3000,    1*HZ, 3*HZ,  0, SEL_DLY, 5,  83, 3*HZ, 20, {3,1,2,0,2}, 0,
381           0, { 4,22,21,30, 3, 0, 0, 0}, 3*HZ/2, 4 }, "720k" }, /*3 1/2 DD*/
382     
383     {{4,  500, 16, 16, 4000, 4*HZ/10, 3*HZ, 10, SEL_DLY, 5,  83, 3*HZ, 20, {3,1,2,0,2}, 0,
384           0, { 7, 4,25,22,31,21,29,11}, 3*HZ/2, 7 }, "1.44M" }, /*3 1/2 HD*/
385     
386     {{5, 1000, 15,  8, 3000, 4*HZ/10, 3*HZ, 10, SEL_DLY, 5,  83, 3*HZ, 40, {3,1,2,0,2}, 0,
387           0, { 7, 8, 4,25,28,22,31,21}, 3*HZ/2, 8 }, "2.88M AMI BIOS" }, /*3 1/2 ED*/
388     
389     {{6, 1000, 15,  8, 3000, 4*HZ/10, 3*HZ, 10, SEL_DLY, 5,  83, 3*HZ, 40, {3,1,2,0,2}, 0,
390           0, { 7, 8, 4,25,28,22,31,21}, 3*HZ/2, 8 }, "2.88M" } /*3 1/2 ED*/
391     /*    |  --autodetected formats---    |      |      |
392      *    read_track                      |      |    Name printed when booting
393      *				      |     Native format
394      *	            Frequency of disk change checks */
395     };
396     
397     static struct floppy_drive_params drive_params[N_DRIVE];
398     static struct floppy_drive_struct drive_state[N_DRIVE];
399     static struct floppy_write_errors write_errors[N_DRIVE];
400     static struct floppy_raw_cmd *raw_cmd, default_raw_cmd;
401     
402     /*
403      * This struct defines the different floppy types.
404      *
405      * Bit 0 of 'stretch' tells if the tracks need to be doubled for some
406      * types (e.g. 360kB diskette in 1.2MB drive, etc.).  Bit 1 of 'stretch'
407      * tells if the disk is in Commodore 1581 format, which means side 0 sectors
408      * are located on side 1 of the disk but with a side 0 ID, and vice-versa.
409      * This is the same as the Sharp MZ-80 5.25" CP/M disk format, except that the
410      * 1581's logical side 0 is on physical side 1, whereas the Sharp's logical
411      * side 0 is on physical side 0 (but with the misnamed sector IDs).
412      * 'stretch' should probably be renamed to something more general, like
413      * 'options'.  Other parameters should be self-explanatory (see also
414      * setfdprm(8)).
415      */
416     /*
417     	    Size
418     	     |  Sectors per track
419     	     |  | Head
420     	     |  | |  Tracks
421     	     |  | |  | Stretch
422     	     |  | |  | |  Gap 1 size
423     	     |  | |  | |    |  Data rate, | 0x40 for perp
424     	     |  | |  | |    |    |  Spec1 (stepping rate, head unload
425     	     |  | |  | |    |    |    |    /fmt gap (gap2) */
426     static struct floppy_struct floppy_type[32] = {
427     	{    0, 0,0, 0,0,0x00,0x00,0x00,0x00,NULL    },	/*  0 no testing    */
428     	{  720, 9,2,40,0,0x2A,0x02,0xDF,0x50,"d360"  }, /*  1 360KB PC      */
429     	{ 2400,15,2,80,0,0x1B,0x00,0xDF,0x54,"h1200" },	/*  2 1.2MB AT      */
430     	{  720, 9,1,80,0,0x2A,0x02,0xDF,0x50,"D360"  },	/*  3 360KB SS 3.5" */
431     	{ 1440, 9,2,80,0,0x2A,0x02,0xDF,0x50,"D720"  },	/*  4 720KB 3.5"    */
432     	{  720, 9,2,40,1,0x23,0x01,0xDF,0x50,"h360"  },	/*  5 360KB AT      */
433     	{ 1440, 9,2,80,0,0x23,0x01,0xDF,0x50,"h720"  },	/*  6 720KB AT      */
434     	{ 2880,18,2,80,0,0x1B,0x00,0xCF,0x6C,"H1440" },	/*  7 1.44MB 3.5"   */
435     	{ 5760,36,2,80,0,0x1B,0x43,0xAF,0x54,"E2880" },	/*  8 2.88MB 3.5"   */
436     	{ 6240,39,2,80,0,0x1B,0x43,0xAF,0x28,"E3120" },	/*  9 3.12MB 3.5"   */
437     
438     	{ 2880,18,2,80,0,0x25,0x00,0xDF,0x02,"h1440" }, /* 10 1.44MB 5.25"  */
439     	{ 3360,21,2,80,0,0x1C,0x00,0xCF,0x0C,"H1680" }, /* 11 1.68MB 3.5"   */
440     	{  820,10,2,41,1,0x25,0x01,0xDF,0x2E,"h410"  },	/* 12 410KB 5.25"   */
441     	{ 1640,10,2,82,0,0x25,0x02,0xDF,0x2E,"H820"  },	/* 13 820KB 3.5"    */
442     	{ 2952,18,2,82,0,0x25,0x00,0xDF,0x02,"h1476" },	/* 14 1.48MB 5.25"  */
443     	{ 3444,21,2,82,0,0x25,0x00,0xDF,0x0C,"H1722" },	/* 15 1.72MB 3.5"   */
444     	{  840,10,2,42,1,0x25,0x01,0xDF,0x2E,"h420"  },	/* 16 420KB 5.25"   */
445     	{ 1660,10,2,83,0,0x25,0x02,0xDF,0x2E,"H830"  },	/* 17 830KB 3.5"    */
446     	{ 2988,18,2,83,0,0x25,0x00,0xDF,0x02,"h1494" },	/* 18 1.49MB 5.25"  */
447     	{ 3486,21,2,83,0,0x25,0x00,0xDF,0x0C,"H1743" }, /* 19 1.74 MB 3.5"  */
448     
449     	{ 1760,11,2,80,0,0x1C,0x09,0xCF,0x00,"h880"  }, /* 20 880KB 5.25"   */
450     	{ 2080,13,2,80,0,0x1C,0x01,0xCF,0x00,"D1040" }, /* 21 1.04MB 3.5"   */
451     	{ 2240,14,2,80,0,0x1C,0x19,0xCF,0x00,"D1120" }, /* 22 1.12MB 3.5"   */
452     	{ 3200,20,2,80,0,0x1C,0x20,0xCF,0x2C,"h1600" }, /* 23 1.6MB 5.25"   */
453     	{ 3520,22,2,80,0,0x1C,0x08,0xCF,0x2e,"H1760" }, /* 24 1.76MB 3.5"   */
454     	{ 3840,24,2,80,0,0x1C,0x20,0xCF,0x00,"H1920" }, /* 25 1.92MB 3.5"   */
455     	{ 6400,40,2,80,0,0x25,0x5B,0xCF,0x00,"E3200" }, /* 26 3.20MB 3.5"   */
456     	{ 7040,44,2,80,0,0x25,0x5B,0xCF,0x00,"E3520" }, /* 27 3.52MB 3.5"   */
457     	{ 7680,48,2,80,0,0x25,0x63,0xCF,0x00,"E3840" }, /* 28 3.84MB 3.5"   */
458     
459     	{ 3680,23,2,80,0,0x1C,0x10,0xCF,0x00,"H1840" }, /* 29 1.84MB 3.5"   */
460     	{ 1600,10,2,80,0,0x25,0x02,0xDF,0x2E,"D800"  },	/* 30 800KB 3.5"    */
461     	{ 3200,20,2,80,0,0x1C,0x00,0xCF,0x2C,"H1600" }, /* 31 1.6MB 3.5"    */
462     };
463     
464     #define	NUMBER(x)	(sizeof(x) / sizeof(*(x)))
465     #define SECTSIZE (_FD_SECTSIZE(*floppy))
466     
467     /* Auto-detection: Disk type used until the next media change occurs. */
468     static struct floppy_struct *current_type[N_DRIVE];
469     
470     /*
471      * User-provided type information. current_type points to
472      * the respective entry of this array.
473      */
474     static struct floppy_struct user_params[N_DRIVE];
475     
476     static int floppy_sizes[256];
477     static int floppy_blocksizes[256];
478     
479     /*
480      * The driver is trying to determine the correct media format
481      * while probing is set. rw_interrupt() clears it after a
482      * successful access.
483      */
484     static int probing;
485     
486     /* Synchronization of FDC access. */
487     #define FD_COMMAND_NONE -1
488     #define FD_COMMAND_ERROR 2
489     #define FD_COMMAND_OKAY 3
490     
491     static volatile int command_status = FD_COMMAND_NONE;
492     static unsigned long fdc_busy;
493     static DECLARE_WAIT_QUEUE_HEAD(fdc_wait);
494     static DECLARE_WAIT_QUEUE_HEAD(command_done);
495     
496     #define NO_SIGNAL (!interruptible || !signal_pending(current))
497     #define CALL(x) if ((x) == -EINTR) return -EINTR
498     #define ECALL(x) if ((ret = (x))) return ret;
499     #define _WAIT(x,i) CALL(ret=wait_til_done((x),i))
500     #define WAIT(x) _WAIT((x),interruptible)
501     #define IWAIT(x) _WAIT((x),1)
502     
503     /* Errors during formatting are counted here. */
504     static int format_errors;
505     
506     /* Format request descriptor. */
507     static struct format_descr format_req;
508     
509     /*
510      * Rate is 0 for 500kb/s, 1 for 300kbps, 2 for 250kbps
511      * Spec1 is 0xSH, where S is stepping rate (F=1ms, E=2ms, D=3ms etc),
512      * H is head unload time (1=16ms, 2=32ms, etc)
513      */
514     
515     /*
516      * Track buffer
517      * Because these are written to by the DMA controller, they must
518      * not contain a 64k byte boundary crossing, or data will be
519      * corrupted/lost.
520      */
521     static char *floppy_track_buffer;
522     static int max_buffer_sectors;
523     
524     static int *errors;
525     typedef void (*done_f)(int);
526     static struct cont_t {
527     	void (*interrupt)(void); /* this is called after the interrupt of the
528     				  * main command */
529     	void (*redo)(void); /* this is called to retry the operation */
530     	void (*error)(void); /* this is called to tally an error */
531     	done_f done; /* this is called to say if the operation has 
532     		      * succeeded/failed */
533     } *cont;
534     
535     static void floppy_ready(void);
536     static void floppy_start(void);
537     static void process_fd_request(void);
538     static void recalibrate_floppy(void);
539     static void floppy_shutdown(void);
540     
541     static int floppy_grab_irq_and_dma(void);
542     static void floppy_release_irq_and_dma(void);
543     
544     /*
545      * The "reset" variable should be tested whenever an interrupt is scheduled,
546      * after the commands have been sent. This is to ensure that the driver doesn't
547      * get wedged when the interrupt doesn't come because of a failed command.
548      * reset doesn't need to be tested before sending commands, because
549      * output_byte is automatically disabled when reset is set.
550      */
551     #define CHECK_RESET { if (FDCS->reset){ reset_fdc(); return; } }
552     static void reset_fdc(void);
553     
554     /*
555      * These are global variables, as that's the easiest way to give
556      * information to interrupts. They are the data used for the current
557      * request.
558      */
559     #define NO_TRACK -1
560     #define NEED_1_RECAL -2
561     #define NEED_2_RECAL -3
562     
563     static int usage_count;
564     
565     /* buffer related variables */
566     static int buffer_track = -1;
567     static int buffer_drive = -1;
568     static int buffer_min = -1;
569     static int buffer_max = -1;
570     
571     /* fdc related variables, should end up in a struct */
572     static struct floppy_fdc_state fdc_state[N_FDC];
573     static int fdc; /* current fdc */
574     
575     static struct floppy_struct *_floppy = floppy_type;
576     static unsigned char current_drive;
577     static long current_count_sectors;
578     static unsigned char sector_t; /* sector in track */
579     static unsigned char in_sector_offset;	/* offset within physical sector,
580     					 * expressed in units of 512 bytes */
581     
582     #ifndef fd_eject
583     #define fd_eject(x) -EINVAL
584     #endif
585     
586     #ifdef DEBUGT
587     static long unsigned debugtimer;
588     #endif
589     
590     /*
591      * Debugging
592      * =========
593      */
594     static inline void set_debugt(void)
595     {
596     #ifdef DEBUGT
597     	debugtimer = jiffies;
598     #endif
599     }
600     
601     static inline void debugt(const char *message)
602     {
603     #ifdef DEBUGT
604     	if (DP->flags & DEBUGT)
605     		printk("%s dtime=%lu\n", message, jiffies-debugtimer);
606     #endif
607     }
608     
609     typedef void (*timeout_fn)(unsigned long);
610     static struct timer_list fd_timeout ={ function: (timeout_fn) floppy_shutdown };
611     
612     static const char *timeout_message;
613     
614     #ifdef FLOPPY_SANITY_CHECK
615     static void is_alive(const char *message)
616     {
617     	/* this routine checks whether the floppy driver is "alive" */
618     	if (fdc_busy && command_status < 2 && !timer_pending(&fd_timeout)){
619     		DPRINT("timeout handler died: %s\n",message);
620     	}
621     }
622     #endif
623     
624     #ifdef FLOPPY_SANITY_CHECK
625     
626     #define OLOGSIZE 20
627     
628     static void (*lasthandler)(void);
629     static unsigned long interruptjiffies;
630     static unsigned long resultjiffies;
631     static int resultsize;
632     static unsigned long lastredo;
633     
634     static struct output_log {
635     	unsigned char data;
636     	unsigned char status;
637     	unsigned long jiffies;
638     } output_log[OLOGSIZE];
639     
640     static int output_log_pos;
641     #endif
642     
643     #define CURRENTD -1
644     #define MAXTIMEOUT -2
645     
646     static void reschedule_timeout(int drive, const char *message, int marg)
647     {
648     	if (drive == CURRENTD)
649     		drive = current_drive;
650     	del_timer(&fd_timeout);
651     	if (drive < 0 || drive > N_DRIVE) {
652     		fd_timeout.expires = jiffies + 20UL*HZ;
653     		drive=0;
654     	} else
655     		fd_timeout.expires = jiffies + UDP->timeout;
656     	add_timer(&fd_timeout);
657     	if (UDP->flags & FD_DEBUG){
658     		DPRINT("reschedule timeout ");
659     		printk(message, marg);
660     		printk("\n");
661     	}
662     	timeout_message = message;
663     }
664     
665     static int maximum(int a, int b)
666     {
667     	if (a > b)
668     		return a;
669     	else
670     		return b;
671     }
672     #define INFBOUND(a,b) (a)=maximum((a),(b));
673     
674     static int minimum(int a, int b)
675     {
676     	if (a < b)
677     		return a;
678     	else
679     		return b;
680     }
681     #define SUPBOUND(a,b) (a)=minimum((a),(b));
682     
683     
684     /*
685      * Bottom half floppy driver.
686      * ==========================
687      *
688      * This part of the file contains the code talking directly to the hardware,
689      * and also the main service loop (seek-configure-spinup-command)
690      */
691     
692     /*
693      * disk change.
694      * This routine is responsible for maintaining the FD_DISK_CHANGE flag,
695      * and the last_checked date.
696      *
697      * last_checked is the date of the last check which showed 'no disk change'
698      * FD_DISK_CHANGE is set under two conditions:
699      * 1. The floppy has been changed after some i/o to that floppy already
700      *    took place.
701      * 2. No floppy disk is in the drive. This is done in order to ensure that
702      *    requests are quickly flushed in case there is no disk in the drive. It
703      *    follows that FD_DISK_CHANGE can only be cleared if there is a disk in
704      *    the drive.
705      *
706      * For 1., maxblock is observed. Maxblock is 0 if no i/o has taken place yet.
707      * For 2., FD_DISK_NEWCHANGE is watched. FD_DISK_NEWCHANGE is cleared on
708      *  each seek. If a disk is present, the disk change line should also be
709      *  cleared on each seek. Thus, if FD_DISK_NEWCHANGE is clear, but the disk
710      *  change line is set, this means either that no disk is in the drive, or
711      *  that it has been removed since the last seek.
712      *
713      * This means that we really have a third possibility too:
714      *  The floppy has been changed after the last seek.
715      */
716     
717     static int disk_change(int drive)
718     {
719     	int fdc=FDC(drive);
720     #ifdef FLOPPY_SANITY_CHECK
721     	if (jiffies - UDRS->select_date < UDP->select_delay)
722     		DPRINT("WARNING disk change called early\n");
723     	if (!(FDCS->dor & (0x10 << UNIT(drive))) ||
724     	    (FDCS->dor & 3) != UNIT(drive) ||
725     	    fdc != FDC(drive)){
726     		DPRINT("probing disk change on unselected drive\n");
727     		DPRINT("drive=%d fdc=%d dor=%x\n",drive, FDC(drive),
728     			(unsigned int)FDCS->dor);
729     	}
730     #endif
731     
732     #ifdef DCL_DEBUG
733     	if (UDP->flags & FD_DEBUG){
734     		DPRINT("checking disk change line for drive %d\n",drive);
735     		DPRINT("jiffies=%lu\n", jiffies);
736     		DPRINT("disk change line=%x\n",fd_inb(FD_DIR)&0x80);
737     		DPRINT("flags=%lx\n",UDRS->flags);
738     	}
739     #endif
740     	if (UDP->flags & FD_BROKEN_DCL)
741     		return UTESTF(FD_DISK_CHANGED);
742     	if ((fd_inb(FD_DIR) ^ UDP->flags) & 0x80){
743     		USETF(FD_VERIFY); /* verify write protection */
744     		if (UDRS->maxblock){
745     			/* mark it changed */
746     			USETF(FD_DISK_CHANGED);
747     		}
748     
749     		/* invalidate its geometry */
750     		if (UDRS->keep_data >= 0) {
751     			if ((UDP->flags & FTD_MSG) &&
752     			    current_type[drive] != NULL)
753     				DPRINT("Disk type is undefined after "
754     				       "disk change\n");
755     			current_type[drive] = NULL;
756     			floppy_sizes[TOMINOR(drive)] = MAX_DISK_SIZE;
757     		}
758     
759     		/*USETF(FD_DISK_NEWCHANGE);*/
760     		return 1;
761     	} else {
762     		UDRS->last_checked=jiffies;
763     		UCLEARF(FD_DISK_NEWCHANGE);
764     	}
765     	return 0;
766     }
767     
768     static inline int is_selected(int dor, int unit)
769     {
770     	return ((dor  & (0x10 << unit)) && (dor &3) == unit);
771     }
772     
773     static int set_dor(int fdc, char mask, char data)
774     {
775     	register unsigned char drive, unit, newdor,olddor;
776     
777     	if (FDCS->address == -1)
778     		return -1;
779     
780     	olddor = FDCS->dor;
781     	newdor =  (olddor & mask) | data;
782     	if (newdor != olddor){
783     		unit = olddor & 0x3;
784     		if (is_selected(olddor, unit) && !is_selected(newdor,unit)){
785     			drive = REVDRIVE(fdc,unit);
786     #ifdef DCL_DEBUG
787     			if (UDP->flags & FD_DEBUG){
788     				DPRINT("calling disk change from set_dor\n");
789     			}
790     #endif
791     			disk_change(drive);
792     		}
793     		FDCS->dor = newdor;
794     		fd_outb(newdor, FD_DOR);
795     
796     		unit = newdor & 0x3;
797     		if (!is_selected(olddor, unit) && is_selected(newdor,unit)){
798     			drive = REVDRIVE(fdc,unit);
799     			UDRS->select_date = jiffies;
800     		}
801     	}
802     	/*
803     	 *	We should propogate failures to grab the resources back
804     	 *	nicely from here. Actually we ought to rewrite the fd
805     	 *	driver some day too.
806     	 */
807     	if (newdor & FLOPPY_MOTOR_MASK)
808     		floppy_grab_irq_and_dma();
809     	if (olddor & FLOPPY_MOTOR_MASK)
810     		floppy_release_irq_and_dma();
811     	return olddor;
812     }
813     
814     static void twaddle(void)
815     {
816     	if (DP->select_delay)
817     		return;
818     	fd_outb(FDCS->dor & ~(0x10<<UNIT(current_drive)), FD_DOR);
819     	fd_outb(FDCS->dor, FD_DOR);
820     	DRS->select_date = jiffies;
821     }
822     
823     /* reset all driver information about the current fdc. This is needed after
824      * a reset, and after a raw command. */
825     static void reset_fdc_info(int mode)
826     {
827     	int drive;
828     
829     	FDCS->spec1 = FDCS->spec2 = -1;
830     	FDCS->need_configure = 1;
831     	FDCS->perp_mode = 1;
832     	FDCS->rawcmd = 0;
833     	for (drive = 0; drive < N_DRIVE; drive++)
834     		if (FDC(drive) == fdc &&
835     		    (mode || UDRS->track != NEED_1_RECAL))
836     			UDRS->track = NEED_2_RECAL;
837     }
838     
839     /* selects the fdc and drive, and enables the fdc's input/dma. */
840     static void set_fdc(int drive)
841     {
842     	if (drive >= 0 && drive < N_DRIVE){
843     		fdc = FDC(drive);
844     		current_drive = drive;
845     	}
846     	if (fdc != 1 && fdc != 0) {
847     		printk("bad fdc value\n");
848     		return;
849     	}
850     	set_dor(fdc,~0,8);
851     #if N_FDC > 1
852     	set_dor(1-fdc, ~8, 0);
853     #endif
854     	if (FDCS->rawcmd == 2)
855     		reset_fdc_info(1);
856     	if (fd_inb(FD_STATUS) != STATUS_READY)
857     		FDCS->reset = 1;
858     }
859     
860     /* locks the driver */
861     static int _lock_fdc(int drive, int interruptible, int line)
862     {
863     	if (!usage_count){
864     		printk(KERN_ERR "Trying to lock fdc while usage count=0 at line %d\n", line);
865     		return -1;
866     	}
867     	if(floppy_grab_irq_and_dma()==-1)
868     		return -EBUSY;
869     
870     	if (test_and_set_bit(0, &fdc_busy)) {
871     		DECLARE_WAITQUEUE(wait, current);
872     		add_wait_queue(&fdc_wait, &wait);
873     
874     		for (;;) {
875     			set_current_state(TASK_INTERRUPTIBLE);
876     
877     			if (!test_and_set_bit(0, &fdc_busy))
878     				break;
879     
880     			schedule();
881     
882     			if (!NO_SIGNAL) {
883     				remove_wait_queue(&fdc_wait, &wait);
884     				return -EINTR;
885     			}
886     		}
887     
888     		set_current_state(TASK_RUNNING);
889     		remove_wait_queue(&fdc_wait, &wait);
890     	}
891     	command_status = FD_COMMAND_NONE;
892     
893     	reschedule_timeout(drive, "lock fdc", 0);
894     	set_fdc(drive);
895     	return 0;
896     }
897     
898     #define lock_fdc(drive,interruptible) _lock_fdc(drive,interruptible, __LINE__)
899     
900     #define LOCK_FDC(drive,interruptible) \
901     if (lock_fdc(drive,interruptible)) return -EINTR;
902     
903     
904     /* unlocks the driver */
905     static inline void unlock_fdc(void)
906     {
907     	raw_cmd = 0;
908     	if (!fdc_busy)
909     		DPRINT("FDC access conflict!\n");
910     
911     	if (DEVICE_INTR)
912     		DPRINT("device interrupt still active at FDC release: %p!\n",
913     			DEVICE_INTR);
914     	command_status = FD_COMMAND_NONE;
915     	del_timer(&fd_timeout);
916     	cont = NULL;
917     	clear_bit(0, &fdc_busy);
918     	floppy_release_irq_and_dma();
919     	wake_up(&fdc_wait);
920     }
921     
922     /* switches the motor off after a given timeout */
923     static void motor_off_callback(unsigned long nr)
924     {
925     	unsigned char mask = ~(0x10 << UNIT(nr));
926     
927     	set_dor(FDC(nr), mask, 0);
928     }
929     
930     static struct timer_list motor_off_timer[N_DRIVE] = {
931     	{ data: 0, function: motor_off_callback },
932     	{ data: 1, function: motor_off_callback },
933     	{ data: 2, function: motor_off_callback },
934     	{ data: 3, function: motor_off_callback },
935     	{ data: 4, function: motor_off_callback },
936     	{ data: 5, function: motor_off_callback },
937     	{ data: 6, function: motor_off_callback },
938     	{ data: 7, function: motor_off_callback }
939     };
940     
941     /* schedules motor off */
942     static void floppy_off(unsigned int drive)
943     {
944     	unsigned long volatile delta;
945     	register int fdc=FDC(drive);
946     
947     	if (!(FDCS->dor & (0x10 << UNIT(drive))))
948     		return;
949     
950     	del_timer(motor_off_timer+drive);
951     
952     	/* make spindle stop in a position which minimizes spinup time
953     	 * next time */
954     	if (UDP->rps){
955     		delta = jiffies - UDRS->first_read_date + HZ -
956     			UDP->spindown_offset;
957     		delta = ((delta * UDP->rps) % HZ) / UDP->rps;
958     		motor_off_timer[drive].expires = jiffies + UDP->spindown - delta;
959     	}
960     	add_timer(motor_off_timer+drive);
961     }
962     
963     /*
964      * cycle through all N_DRIVE floppy drives, for disk change testing.
965      * stopping at current drive. This is done before any long operation, to
966      * be sure to have up to date disk change information.
967      */
968     static void scandrives(void)
969     {
970     	int i, drive, saved_drive;
971     
972     	if (DP->select_delay)
973     		return;
974     
975     	saved_drive = current_drive;
976     	for (i=0; i < N_DRIVE; i++){
977     		drive = (saved_drive + i + 1) % N_DRIVE;
978     		if (UDRS->fd_ref == 0 || UDP->select_delay != 0)
979     			continue; /* skip closed drives */
980     		set_fdc(drive);
981     		if (!(set_dor(fdc, ~3, UNIT(drive) | (0x10 << UNIT(drive))) &
982     		      (0x10 << UNIT(drive))))
983     			/* switch the motor off again, if it was off to
984     			 * begin with */
985     			set_dor(fdc, ~(0x10 << UNIT(drive)), 0);
986     	}
987     	set_fdc(saved_drive);
988     }
989     
990     static void empty(void)
991     {
992     }
993     
994     static struct tq_struct floppy_tq;
995     
996     static void schedule_bh( void (*handler)(void*) )
997     {
998     	floppy_tq.routine = (void *)(void *) handler;
999     	queue_task(&floppy_tq, &tq_immediate);
1000     	mark_bh(IMMEDIATE_BH);
1001     }
1002     
1003     static struct timer_list fd_timer;
1004     
1005     static void cancel_activity(void)
1006     {
1007     	CLEAR_INTR;
1008     	floppy_tq.routine = (void *)(void *) empty;
1009     	del_timer(&fd_timer);
1010     }
1011     
1012     /* this function makes sure that the disk stays in the drive during the
1013      * transfer */
1014     static void fd_watchdog(void)
1015     {
1016     #ifdef DCL_DEBUG
1017     	if (DP->flags & FD_DEBUG){
1018     		DPRINT("calling disk change from watchdog\n");
1019     	}
1020     #endif
1021     
1022     	if (disk_change(current_drive)){
1023     		DPRINT("disk removed during i/o\n");
1024     		cancel_activity();
1025     		cont->done(0);
1026     		reset_fdc();
1027     	} else {
1028     		del_timer(&fd_timer);
1029     		fd_timer.function = (timeout_fn) fd_watchdog;
1030     		fd_timer.expires = jiffies + HZ / 10;
1031     		add_timer(&fd_timer);
1032     	}
1033     }
1034     
1035     static void main_command_interrupt(void)
1036     {
1037     	del_timer(&fd_timer);
1038     	cont->interrupt();
1039     }
1040     
1041     /* waits for a delay (spinup or select) to pass */
1042     static int fd_wait_for_completion(unsigned long delay, timeout_fn function)
1043     {
1044     	if (FDCS->reset){
1045     		reset_fdc(); /* do the reset during sleep to win time
1046     			      * if we don't need to sleep, it's a good
1047     			      * occasion anyways */
1048     		return 1;
1049     	}
1050     
1051     	if ((signed) (jiffies - delay) < 0){
1052     		del_timer(&fd_timer);
1053     		fd_timer.function = function;
1054     		fd_timer.expires = delay;
1055     		add_timer(&fd_timer);
1056     		return 1;
1057     	}
1058     	return 0;
1059     }
1060     
1061     static spinlock_t floppy_hlt_lock = SPIN_LOCK_UNLOCKED;
1062     static int hlt_disabled;
1063     static void floppy_disable_hlt(void)
1064     {
1065     	unsigned long flags;
1066     
1067     	spin_lock_irqsave(&floppy_hlt_lock, flags);
1068     	if (!hlt_disabled) {
1069     		hlt_disabled=1;
1070     #ifdef HAVE_DISABLE_HLT
1071     		disable_hlt();
1072     #endif
1073     	}
1074     	spin_unlock_irqrestore(&floppy_hlt_lock, flags);
1075     }
1076     
1077     static void floppy_enable_hlt(void)
1078     {
1079     	unsigned long flags;
1080     
1081     	spin_lock_irqsave(&floppy_hlt_lock, flags);
1082     	if (hlt_disabled){
1083     		hlt_disabled=0;
1084     #ifdef HAVE_DISABLE_HLT
1085     		enable_hlt();
1086     #endif
1087     	}
1088     	spin_unlock_irqrestore(&floppy_hlt_lock, flags);
1089     }
1090     
1091     
1092     static void setup_DMA(void)
1093     {
1094     	unsigned long f;
1095     
1096     #ifdef FLOPPY_SANITY_CHECK
1097     	if (raw_cmd->length == 0){
1098     		int i;
1099     
1100     		printk("zero dma transfer size:");
1101     		for (i=0; i < raw_cmd->cmd_count; i++)
1102     			printk("%x,", raw_cmd->cmd[i]);
1103     		printk("\n");
1104     		cont->done(0);
1105     		FDCS->reset = 1;
1106     		return;
1107     	}
1108     	if (((unsigned long) raw_cmd->kernel_data) % 512){
1109     		printk("non aligned address: %p\n", raw_cmd->kernel_data);
1110     		cont->done(0);
1111     		FDCS->reset=1;
1112     		return;
1113     	}
1114     #endif
1115     	f=claim_dma_lock();
1116     	fd_disable_dma();
1117     #ifdef fd_dma_setup
1118     	if (fd_dma_setup(raw_cmd->kernel_data, raw_cmd->length, 
1119     			(raw_cmd->flags & FD_RAW_READ)?
1120     			DMA_MODE_READ : DMA_MODE_WRITE,
1121     			FDCS->address) < 0) {
1122     		release_dma_lock(f);
1123     		cont->done(0);
1124     		FDCS->reset=1;
1125     		return;
1126     	}
1127     	release_dma_lock(f);
1128     #else	
1129     	fd_clear_dma_ff();
1130     	fd_cacheflush(raw_cmd->kernel_data, raw_cmd->length);
1131     	fd_set_dma_mode((raw_cmd->flags & FD_RAW_READ)?
1132     			DMA_MODE_READ : DMA_MODE_WRITE);
1133     	fd_set_dma_addr(raw_cmd->kernel_data);
1134     	fd_set_dma_count(raw_cmd->length);
1135     	virtual_dma_port = FDCS->address;
1136     	fd_enable_dma();
1137     	release_dma_lock(f);
1138     #endif
1139     	floppy_disable_hlt();
1140     }
1141     
1142     static void show_floppy(void);
1143     
1144     /* waits until the fdc becomes ready */
1145     static int wait_til_ready(void)
1146     {
1147     	int counter, status;
1148     	if (FDCS->reset)
1149     		return -1;
1150     	for (counter = 0; counter < 10000; counter++) {
1151     		status = fd_inb(FD_STATUS);		
1152     		if (status & STATUS_READY)
1153     			return status;
1154     	}
1155     	if (!initialising) {
1156     		DPRINT("Getstatus times out (%x) on fdc %d\n",
1157     			status, fdc);
1158     		show_floppy();
1159     	}
1160     	FDCS->reset = 1;
1161     	return -1;
1162     }
1163     
1164     /* sends a command byte to the fdc */
1165     static int output_byte(char byte)
1166     {
1167     	int status;
1168     
1169     	if ((status = wait_til_ready()) < 0)
1170     		return -1;
1171     	if ((status & (STATUS_READY|STATUS_DIR|STATUS_DMA)) == STATUS_READY){
1172     		fd_outb(byte,FD_DATA);
1173     #ifdef FLOPPY_SANITY_CHECK
1174     		output_log[output_log_pos].data = byte;
1175     		output_log[output_log_pos].status = status;
1176     		output_log[output_log_pos].jiffies = jiffies;
1177     		output_log_pos = (output_log_pos + 1) % OLOGSIZE;
1178     #endif
1179     		return 0;
1180     	}
1181     	FDCS->reset = 1;
1182     	if (!initialising) {
1183     		DPRINT("Unable to send byte %x to FDC. Fdc=%x Status=%x\n",
1184     		       byte, fdc, status);
1185     		show_floppy();
1186     	}
1187     	return -1;
1188     }
1189     #define LAST_OUT(x) if (output_byte(x)<0){ reset_fdc();return;}
1190     
1191     /* gets the response from the fdc */
1192     static int result(void)
1193     {
1194     	int i, status=0;
1195     
1196     	for(i=0; i < MAX_REPLIES; i++) {
1197     		if ((status = wait_til_ready()) < 0)
1198     			break;
1199     		status &= STATUS_DIR|STATUS_READY|STATUS_BUSY|STATUS_DMA;
1200     		if ((status & ~STATUS_BUSY) == STATUS_READY){
1201     #ifdef FLOPPY_SANITY_CHECK
1202     			resultjiffies = jiffies;
1203     			resultsize = i;
1204     #endif
1205     			return i;
1206     		}
1207     		if (status == (STATUS_DIR|STATUS_READY|STATUS_BUSY))
1208     			reply_buffer[i] = fd_inb(FD_DATA);
1209     		else
1210     			break;
1211     	}
1212     	if (!initialising) {
1213     		DPRINT("get result error. Fdc=%d Last status=%x Read bytes=%d\n",
1214     		       fdc, status, i);
1215     		show_floppy();
1216     	}
1217     	FDCS->reset = 1;
1218     	return -1;
1219     }
1220     
1221     #define MORE_OUTPUT -2
1222     /* does the fdc need more output? */
1223     static int need_more_output(void)
1224     {
1225     	int status;
1226     	if ((status = wait_til_ready()) < 0)
1227     		return -1;
1228     	if ((status & (STATUS_READY|STATUS_DIR|STATUS_DMA)) == STATUS_READY)
1229     		return MORE_OUTPUT;
1230     	return result();
1231     }
1232     
1233     /* Set perpendicular mode as required, based on data rate, if supported.
1234      * 82077 Now tested. 1Mbps data rate only possible with 82077-1.
1235      */
1236     static inline void perpendicular_mode(void)
1237     {
1238     	unsigned char perp_mode;
1239     
1240     	if (raw_cmd->rate & 0x40){
1241     		switch(raw_cmd->rate & 3){
1242     			case 0:
1243     				perp_mode=2;
1244     				break;
1245     			case 3:
1246     				perp_mode=3;
1247     				break;
1248     			default:
1249     				DPRINT("Invalid data rate for perpendicular mode!\n");
1250     				cont->done(0);
1251     				FDCS->reset = 1; /* convenient way to return to
1252     						  * redo without to much hassle (deep
1253     						  * stack et al. */
1254     				return;
1255     		}
1256     	} else
1257     		perp_mode = 0;
1258     
1259     	if (FDCS->perp_mode == perp_mode)
1260     		return;
1261     	if (FDCS->version >= FDC_82077_ORIG) {
1262     		output_byte(FD_PERPENDICULAR);
1263     		output_byte(perp_mode);
1264     		FDCS->perp_mode = perp_mode;
1265     	} else if (perp_mode) {
1266     		DPRINT("perpendicular mode not supported by this FDC.\n");
1267     	}
1268     } /* perpendicular_mode */
1269     
1270     static int fifo_depth = 0xa;
1271     static int no_fifo;
1272     
1273     static int fdc_configure(void)
1274     {
1275     	/* Turn on FIFO */
1276     	output_byte(FD_CONFIGURE);
1277     	if (need_more_output() != MORE_OUTPUT)
1278     		return 0;
1279     	output_byte(0);
1280     	output_byte(0x10 | (no_fifo & 0x20) | (fifo_depth & 0xf));
1281     	output_byte(0);	/* pre-compensation from track 
1282     			   0 upwards */
1283     	return 1;
1284     }	
1285     
1286     #define NOMINAL_DTR 500
1287     
1288     /* Issue a "SPECIFY" command to set the step rate time, head unload time,
1289      * head load time, and DMA disable flag to values needed by floppy.
1290      *
1291      * The value "dtr" is the data transfer rate in Kbps.  It is needed
1292      * to account for the data rate-based scaling done by the 82072 and 82077
1293      * FDC types.  This parameter is ignored for other types of FDCs (i.e.
1294      * 8272a).
1295      *
1296      * Note that changing the data transfer rate has a (probably deleterious)
1297      * effect on the parameters subject to scaling for 82072/82077 FDCs, so
1298      * fdc_specify is called again after each data transfer rate
1299      * change.
1300      *
1301      * srt: 1000 to 16000 in microseconds
1302      * hut: 16 to 240 milliseconds
1303      * hlt: 2 to 254 milliseconds
1304      *
1305      * These values are rounded up to the next highest available delay time.
1306      */
1307     static void fdc_specify(void)
1308     {
1309     	unsigned char spec1, spec2;
1310     	unsigned long srt, hlt, hut;
1311     	unsigned long dtr = NOMINAL_DTR;
1312     	unsigned long scale_dtr = NOMINAL_DTR;
1313     	int hlt_max_code = 0x7f;
1314     	int hut_max_code = 0xf;
1315     
1316     	if (FDCS->need_configure && FDCS->version >= FDC_82072A) {
1317     		fdc_configure();
1318     		FDCS->need_configure = 0;
1319     		/*DPRINT("FIFO enabled\n");*/
1320     	}
1321     
1322     	switch (raw_cmd->rate & 0x03) {
1323     		case 3:
1324     			dtr = 1000;
1325     			break;
1326     		case 1:
1327     			dtr = 300;
1328     			if (FDCS->version >= FDC_82078) {
1329     				/* chose the default rate table, not the one
1330     				 * where 1 = 2 Mbps */
1331     				output_byte(FD_DRIVESPEC);
1332     				if (need_more_output() == MORE_OUTPUT) {
1333     					output_byte(UNIT(current_drive));
1334     					output_byte(0xc0);
1335     				}
1336     			}
1337     			break;
1338     		case 2:
1339     			dtr = 250;
1340     			break;
1341     	}
1342     
1343     	if (FDCS->version >= FDC_82072) {
1344     		scale_dtr = dtr;
1345     		hlt_max_code = 0x00; /* 0==256msec*dtr0/dtr (not linear!) */
1346     		hut_max_code = 0x0; /* 0==256msec*dtr0/dtr (not linear!) */
1347     	}
1348     
1349     	/* Convert step rate from microseconds to milliseconds and 4 bits */
1350     	srt = 16 - (DP->srt*scale_dtr/1000 + NOMINAL_DTR - 1)/NOMINAL_DTR;
1351     	if( slow_floppy ) {
1352     		srt = srt / 4;
1353     	}
1354     	SUPBOUND(srt, 0xf);
1355     	INFBOUND(srt, 0);
1356     
1357     	hlt = (DP->hlt*scale_dtr/2 + NOMINAL_DTR - 1)/NOMINAL_DTR;
1358     	if (hlt < 0x01)
1359     		hlt = 0x01;
1360     	else if (hlt > 0x7f)
1361     		hlt = hlt_max_code;
1362     
1363     	hut = (DP->hut*scale_dtr/16 + NOMINAL_DTR - 1)/NOMINAL_DTR;
1364     	if (hut < 0x1)
1365     		hut = 0x1;
1366     	else if (hut > 0xf)
1367     		hut = hut_max_code;
1368     
1369     	spec1 = (srt << 4) | hut;
1370     	spec2 = (hlt << 1) | (use_virtual_dma & 1);
1371     
1372     	/* If these parameters did not change, just return with success */
1373     	if (FDCS->spec1 != spec1 || FDCS->spec2 != spec2) {
1374     		/* Go ahead and set spec1 and spec2 */
1375     		output_byte(FD_SPECIFY);
1376     		output_byte(FDCS->spec1 = spec1);
1377     		output_byte(FDCS->spec2 = spec2);
1378     	}
1379     } /* fdc_specify */
1380     
1381     /* Set the FDC's data transfer rate on behalf of the specified drive.
1382      * NOTE: with 82072/82077 FDCs, changing the data rate requires a reissue
1383      * of the specify command (i.e. using the fdc_specify function).
1384      */
1385     static int fdc_dtr(void)
1386     {
1387     	/* If data rate not already set to desired value, set it. */
1388     	if ((raw_cmd->rate & 3) == FDCS->dtr)
1389     		return 0;
1390     
1391     	/* Set dtr */
1392     	fd_outb(raw_cmd->rate & 3, FD_DCR);
1393     
1394     	/* TODO: some FDC/drive combinations (C&T 82C711 with TEAC 1.2MB)
1395     	 * need a stabilization period of several milliseconds to be
1396     	 * enforced after data rate changes before R/W operations.
1397     	 * Pause 5 msec to avoid trouble. (Needs to be 2 jiffies)
1398     	 */
1399     	FDCS->dtr = raw_cmd->rate & 3;
1400     	return(fd_wait_for_completion(jiffies+2UL*HZ/100,
1401     				   (timeout_fn) floppy_ready));
1402     } /* fdc_dtr */
1403     
1404     static void tell_sector(void)
1405     {
1406     	printk(": track %d, head %d, sector %d, size %d",
1407     	       R_TRACK, R_HEAD, R_SECTOR, R_SIZECODE);
1408     } /* tell_sector */
1409     
1410     
1411     /*
1412      * OK, this error interpreting routine is called after a
1413      * DMA read/write has succeeded
1414      * or failed, so we check the results, and copy any buffers.
1415      * hhb: Added better error reporting.
1416      * ak: Made this into a separate routine.
1417      */
1418     static int interpret_errors(void)
1419     {
1420     	char bad;
1421     
1422     	if (inr!=7) {
1423     		DPRINT("-- FDC reply error");
1424     		FDCS->reset = 1;
1425     		return 1;
1426     	}
1427     
1428     	/* check IC to find cause of interrupt */
1429     	switch (ST0 & ST0_INTR) {
1430     		case 0x40:	/* error occurred during command execution */
1431     			if (ST1 & ST1_EOC)
1432     				return 0; /* occurs with pseudo-DMA */
1433     			bad = 1;
1434     			if (ST1 & ST1_WP) {
1435     				DPRINT("Drive is write protected\n");
1436     				CLEARF(FD_DISK_WRITABLE);
1437     				cont->done(0);
1438     				bad = 2;
1439     			} else if (ST1 & ST1_ND) {
1440     				SETF(FD_NEED_TWADDLE);
1441     			} else if (ST1 & ST1_OR) {
1442     				if (DP->flags & FTD_MSG)
1443     					DPRINT("Over/Underrun - retrying\n");
1444     				bad = 0;
1445     			}else if (*errors >= DP->max_errors.reporting){
1446     				DPRINT("");
1447     				if (ST0 & ST0_ECE) {
1448     					printk("Recalibrate failed!");
1449     				} else if (ST2 & ST2_CRC) {
1450     					printk("data CRC error");
1451     					tell_sector();
1452     				} else if (ST1 & ST1_CRC) {
1453     					printk("CRC error");
1454     					tell_sector();
1455     				} else if ((ST1 & (ST1_MAM|ST1_ND)) || (ST2 & ST2_MAM)) {
1456     					if (!probing) {
1457     						printk("sector not found");
1458     						tell_sector();
1459     					} else
1460     						printk("probe failed...");
1461     				} else if (ST2 & ST2_WC) {	/* seek error */
1462     					printk("wrong cylinder");
1463     				} else if (ST2 & ST2_BC) {	/* cylinder marked as bad */
1464     					printk("bad cylinder");
1465     				} else {
1466     					printk("unknown error. ST[0..2] are: 0x%x 0x%x 0x%x", ST0, ST1, ST2);
1467     					tell_sector();
1468     				}
1469     				printk("\n");
1470     
1471     			}
1472     			if (ST2 & ST2_WC || ST2 & ST2_BC)
1473     				/* wrong cylinder => recal */
1474     				DRS->track = NEED_2_RECAL;
1475     			return bad;
1476     		case 0x80: /* invalid command given */
1477     			DPRINT("Invalid FDC command given!\n");
1478     			cont->done(0);
1479     			return 2;
1480     		case 0xc0:
1481     			DPRINT("Abnormal termination caused by polling\n");
1482     			cont->error();
1483     			return 2;
1484     		default: /* (0) Normal command termination */
1485     			return 0;
1486     	}
1487     }
1488     
1489     /*
1490      * This routine is called when everything should be correctly set up
1491      * for the transfer (i.e. floppy motor is on, the correct floppy is
1492      * selected, and the head is sitting on the right track).
1493      */
1494     static void setup_rw_floppy(void)
1495     {
1496     	int i,r, flags,dflags;
1497     	unsigned long ready_date;
1498     	timeout_fn function;
1499     
1500     	flags = raw_cmd->flags;
1501     	if (flags & (FD_RAW_READ | FD_RAW_WRITE))
1502     		flags |= FD_RAW_INTR;
1503     
1504     	if ((flags & FD_RAW_SPIN) && !(flags & FD_RAW_NO_MOTOR)){
1505     		ready_date = DRS->spinup_date + DP->spinup;
1506     		/* If spinup will take a long time, rerun scandrives
1507     		 * again just before spinup completion. Beware that
1508     		 * after scandrives, we must again wait for selection.
1509     		 */
1510     		if ((signed) (ready_date - jiffies) > DP->select_delay){
1511     			ready_date -= DP->select_delay;
1512     			function = (timeout_fn) floppy_start;
1513     		} else
1514     			function = (timeout_fn) setup_rw_floppy;
1515     
1516     		/* wait until the floppy is spinning fast enough */
1517     		if (fd_wait_for_completion(ready_date,function))
1518     			return;
1519     	}
1520     	dflags = DRS->flags;
1521     
1522     	if ((flags & FD_RAW_READ) || (flags & FD_RAW_WRITE))
1523     		setup_DMA();
1524     
1525     	if (flags & FD_RAW_INTR)
1526     		SET_INTR(main_command_interrupt);
1527     
1528     	r=0;
1529     	for (i=0; i< raw_cmd->cmd_count; i++)
1530     		r|=output_byte(raw_cmd->cmd[i]);
1531     
1532     #ifdef DEBUGT
1533     	debugt("rw_command: ");
1534     #endif
1535     	if (r){
1536     		cont->error();
1537     		reset_fdc();
1538     		return;
1539     	}
1540     
1541     	if (!(flags & FD_RAW_INTR)){
1542     		inr = result();
1543     		cont->interrupt();
1544     	} else if (flags & FD_RAW_NEED_DISK)
1545     		fd_watchdog();
1546     }
1547     
1548     static int blind_seek;
1549     
1550     /*
1551      * This is the routine called after every seek (or recalibrate) interrupt
1552      * from the floppy controller.
1553      */
1554     static void seek_interrupt(void)
1555     {
1556     #ifdef DEBUGT
1557     	debugt("seek interrupt:");
1558     #endif
1559     	if (inr != 2 || (ST0 & 0xF8) != 0x20) {
1560     		DPRINT("seek failed\n");
1561     		DRS->track = NEED_2_RECAL;
1562     		cont->error();
1563     		cont->redo();
1564     		return;
1565     	}
1566     	if (DRS->track >= 0 && DRS->track != ST1 && !blind_seek){
1567     #ifdef DCL_DEBUG
1568     		if (DP->flags & FD_DEBUG){
1569     			DPRINT("clearing NEWCHANGE flag because of effective seek\n");
1570     			DPRINT("jiffies=%lu\n", jiffies);
1571     		}
1572     #endif
1573     		CLEARF(FD_DISK_NEWCHANGE); /* effective seek */
1574     		DRS->select_date = jiffies;
1575     	}
1576     	DRS->track = ST1;
1577     	floppy_ready();
1578     }
1579     
1580     static void check_wp(void)
1581     {
1582     	if (TESTF(FD_VERIFY)) {
1583     		/* check write protection */
1584     		output_byte(FD_GETSTATUS);
1585     		output_byte(UNIT(current_drive));
1586     		if (result() != 1){
1587     			FDCS->reset = 1;
1588     			return;
1589     		}
1590     		CLEARF(FD_VERIFY);
1591     		CLEARF(FD_NEED_TWADDLE);
1592     #ifdef DCL_DEBUG
1593     		if (DP->flags & FD_DEBUG){
1594     			DPRINT("checking whether disk is write protected\n");
1595     			DPRINT("wp=%x\n",ST3 & 0x40);
1596     		}
1597     #endif
1598     		if (!(ST3  & 0x40))
1599     			SETF(FD_DISK_WRITABLE);
1600     		else
1601     			CLEARF(FD_DISK_WRITABLE);
1602     	}
1603     }
1604     
1605     static void seek_floppy(void)
1606     {
1607     	int track;
1608     
1609     	blind_seek=0;
1610     
1611     #ifdef DCL_DEBUG
1612     	if (DP->flags & FD_DEBUG){
1613     		DPRINT("calling disk change from seek\n");
1614     	}
1615     #endif
1616     
1617     	if (!TESTF(FD_DISK_NEWCHANGE) &&
1618     	    disk_change(current_drive) &&
1619     	    (raw_cmd->flags & FD_RAW_NEED_DISK)){
1620     		/* the media changed flag should be cleared after the seek.
1621     		 * If it isn't, this means that there is really no disk in
1622     		 * the drive.
1623     		 */
1624     		SETF(FD_DISK_CHANGED);
1625     		cont->done(0);
1626     		cont->redo();
1627     		return;
1628     	}
1629     	if (DRS->track <= NEED_1_RECAL){
1630     		recalibrate_floppy();
1631     		return;
1632     	} else if (TESTF(FD_DISK_NEWCHANGE) &&
1633     		   (raw_cmd->flags & FD_RAW_NEED_DISK) &&
1634     		   (DRS->track <= NO_TRACK || DRS->track == raw_cmd->track)) {
1635     		/* we seek to clear the media-changed condition. Does anybody
1636     		 * know a more elegant way, which works on all drives? */
1637     		if (raw_cmd->track)
1638     			track = raw_cmd->track - 1;
1639     		else {
1640     			if (DP->flags & FD_SILENT_DCL_CLEAR){
1641     				set_dor(fdc, ~(0x10 << UNIT(current_drive)), 0);
1642     				blind_seek = 1;
1643     				raw_cmd->flags |= FD_RAW_NEED_SEEK;
1644     			}
1645     			track = 1;
1646     		}
1647     	} else {
1648     		check_wp();
1649     		if (raw_cmd->track != DRS->track &&
1650     		    (raw_cmd->flags & FD_RAW_NEED_SEEK))
1651     			track = raw_cmd->track;
1652     		else {
1653     			setup_rw_floppy();
1654     			return;
1655     		}
1656     	}
1657     
1658     	SET_INTR(seek_interrupt);
1659     	output_byte(FD_SEEK);
1660     	output_byte(UNIT(current_drive));
1661     	LAST_OUT(track);
1662     #ifdef DEBUGT
1663     	debugt("seek command:");
1664     #endif
1665     }
1666     
1667     static void recal_interrupt(void)
1668     {
1669     #ifdef DEBUGT
1670     	debugt("recal interrupt:");
1671     #endif
1672     	if (inr !=2)
1673     		FDCS->reset = 1;
1674     	else if (ST0 & ST0_ECE) {
1675     	       	switch(DRS->track){
1676     			case NEED_1_RECAL:
1677     #ifdef DEBUGT
1678     				debugt("recal interrupt need 1 recal:");
1679     #endif
1680     				/* after a second recalibrate, we still haven't
1681     				 * reached track 0. Probably no drive. Raise an
1682     				 * error, as failing immediately might upset
1683     				 * computers possessed by the Devil :-) */
1684     				cont->error();
1685     				cont->redo();
1686     				return;
1687     			case NEED_2_RECAL:
1688     #ifdef DEBUGT
1689     				debugt("recal interrupt need 2 recal:");
1690     #endif
1691     				/* If we already did a recalibrate,
1692     				 * and we are not at track 0, this
1693     				 * means we have moved. (The only way
1694     				 * not to move at recalibration is to
1695     				 * be already at track 0.) Clear the
1696     				 * new change flag */
1697     #ifdef DCL_DEBUG
1698     				if (DP->flags & FD_DEBUG){
1699     					DPRINT("clearing NEWCHANGE flag because of second recalibrate\n");
1700     				}
1701     #endif
1702     
1703     				CLEARF(FD_DISK_NEWCHANGE);
1704     				DRS->select_date = jiffies;
1705     				/* fall through */
1706     			default:
1707     #ifdef DEBUGT
1708     				debugt("recal interrupt default:");
1709     #endif
1710     				/* Recalibrate moves the head by at
1711     				 * most 80 steps. If after one
1712     				 * recalibrate we don't have reached
1713     				 * track 0, this might mean that we
1714     				 * started beyond track 80.  Try
1715     				 * again.  */
1716     				DRS->track = NEED_1_RECAL;
1717     				break;
1718     		}
1719     	} else
1720     		DRS->track = ST1;
1721     	floppy_ready();
1722     }
1723     
1724     static void print_result(char *message, int inr)
1725     {
1726     	int i;
1727     
1728     	DPRINT("%s ", message);
1729     	if (inr >= 0)
1730     		for (i=0; i<inr; i++)
1731     			printk("repl[%d]=%x ", i, reply_buffer[i]);
1732     	printk("\n");
1733     }
1734     
1735     /* interrupt handler. Note that this can be called externally on the Sparc */
1736     void floppy_interrupt(int irq, void *dev_id, struct pt_regs * regs)
1737     {
1738     	void (*handler)(void) = DEVICE_INTR;
1739     	int do_print;
1740     	unsigned long f;
1741     
1742     	lasthandler = handler;
1743     	interruptjiffies = jiffies;
1744     
1745     	f=claim_dma_lock();
1746     	fd_disable_dma();
1747     	release_dma_lock(f);
1748     	
1749     	floppy_enable_hlt();
1750     	CLEAR_INTR;
1751     	if (fdc >= N_FDC || FDCS->address == -1){
1752     		/* we don't even know which FDC is the culprit */
1753     		printk("DOR0=%x\n", fdc_state[0].dor);
1754     		printk("floppy interrupt on bizarre fdc %d\n",fdc);
1755     		printk("handler=%p\n", handler);
1756     		is_alive("bizarre fdc");
1757     		return;
1758     	}
1759     
1760     	FDCS->reset = 0;
1761     	/* We have to clear the reset flag here, because apparently on boxes
1762     	 * with level triggered interrupts (PS/2, Sparc, ...), it is needed to
1763     	 * emit SENSEI's to clear the interrupt line. And FDCS->reset blocks the
1764     	 * emission of the SENSEI's.
1765     	 * It is OK to emit floppy commands because we are in an interrupt
1766     	 * handler here, and thus we have to fear no interference of other
1767     	 * activity.
1768     	 */
1769     
1770     	do_print = !handler && print_unex && !initialising;
1771     
1772     	inr = result();
1773     	if (do_print)
1774     		print_result("unexpected interrupt", inr);
1775     	if (inr == 0){
1776     		int max_sensei = 4;
1777     		do {
1778     			output_byte(FD_SENSEI);
1779     			inr = result();
1780     			if (do_print)
1781     				print_result("sensei", inr);
1782     			max_sensei--;
1783     		} while ((ST0 & 0x83) != UNIT(current_drive) && inr == 2 && max_sensei);
1784     	}
1785     	if (handler) {
1786     		schedule_bh( (void *)(void *) handler);
1787     	} else
1788     		FDCS->reset = 1;
1789     	is_alive("normal interrupt end");
1790     }
1791     
1792     static void recalibrate_floppy(void)
1793     {
1794     #ifdef DEBUGT
1795     	debugt("recalibrate floppy:");
1796     #endif
1797     	SET_INTR(recal_interrupt);
1798     	output_byte(FD_RECALIBRATE);
1799     	LAST_OUT(UNIT(current_drive));
1800     }
1801     
1802     /*
1803      * Must do 4 FD_SENSEIs after reset because of ``drive polling''.
1804      */
1805     static void reset_interrupt(void)
1806     {
1807     #ifdef DEBUGT
1808     	debugt("reset interrupt:");
1809     #endif
1810     	result();		/* get the status ready for set_fdc */
1811     	if (FDCS->reset) {
1812     		printk("reset set in interrupt, calling %p\n", cont->error);
1813     		cont->error(); /* a reset just after a reset. BAD! */
1814     	}
1815     	cont->redo();
1816     }
1817     
1818     /*
1819      * reset is done by pulling bit 2 of DOR low for a while (old FDCs),
1820      * or by setting the self clearing bit 7 of STATUS (newer FDCs)
1821      */
1822     static void reset_fdc(void)
1823     {
1824     	unsigned long flags;
1825     	
1826     	SET_INTR(reset_interrupt);
1827     	FDCS->reset = 0;
1828     	reset_fdc_info(0);
1829     
1830     	/* Pseudo-DMA may intercept 'reset finished' interrupt.  */
1831     	/* Irrelevant for systems with true DMA (i386).          */
1832     	
1833     	flags=claim_dma_lock();
1834     	fd_disable_dma();
1835     	release_dma_lock(flags);
1836     
1837     	if (FDCS->version >= FDC_82072A)
1838     		fd_outb(0x80 | (FDCS->dtr &3), FD_STATUS);
1839     	else {
1840     		fd_outb(FDCS->dor & ~0x04, FD_DOR);
1841     		udelay(FD_RESET_DELAY);
1842     		fd_outb(FDCS->dor, FD_DOR);
1843     	}
1844     }
1845     
1846     static void show_floppy(void)
1847     {
1848     	int i;
1849     
1850     	printk("\n");
1851     	printk("floppy driver state\n");
1852     	printk("-------------------\n");
1853     	printk("now=%lu last interrupt=%lu diff=%lu last called handler=%p\n",
1854     	       jiffies, interruptjiffies, jiffies-interruptjiffies, lasthandler);
1855     
1856     
1857     #ifdef FLOPPY_SANITY_CHECK
1858     	printk("timeout_message=%s\n", timeout_message);
1859     	printk("last output bytes:\n");
1860     	for (i=0; i < OLOGSIZE; i++)
1861     		printk("%2x %2x %lu\n",
1862     		       output_log[(i+output_log_pos) % OLOGSIZE].data,
1863     		       output_log[(i+output_log_pos) % OLOGSIZE].status,
1864     		       output_log[(i+output_log_pos) % OLOGSIZE].jiffies);
1865     	printk("last result at %lu\n", resultjiffies);
1866     	printk("last redo_fd_request at %lu\n", lastredo);
1867     	for (i=0; i<resultsize; i++){
1868     		printk("%2x ", reply_buffer[i]);
1869     	}
1870     	printk("\n");
1871     #endif
1872     
1873     	printk("status=%x\n", fd_inb(FD_STATUS));
1874     	printk("fdc_busy=%lu\n", fdc_busy);
1875     	if (DEVICE_INTR)
1876     		printk("DEVICE_INTR=%p\n", DEVICE_INTR);
1877     	if (floppy_tq.sync)
1878     		printk("floppy_tq.routine=%p\n", floppy_tq.routine);
1879     	if (timer_pending(&fd_timer))
1880     		printk("fd_timer.function=%p\n", fd_timer.function);
1881     	if (timer_pending(&fd_timeout)){
1882     		printk("timer_function=%p\n",fd_timeout.function);
1883     		printk("expires=%lu\n",fd_timeout.expires-jiffies);
1884     		printk("now=%lu\n",jiffies);
1885     	}
1886     	printk("cont=%p\n", cont);
1887     	printk("CURRENT=%p\n", CURRENT);
1888     	printk("command_status=%d\n", command_status);
1889     	printk("\n");
1890     }
1891     
1892     static void floppy_shutdown(void)
1893     {
1894     	unsigned long flags;
1895     	
1896     	if (!initialising)
1897     		show_floppy();
1898     	cancel_activity();
1899     
1900     	floppy_enable_hlt();
1901     	
1902     	flags=claim_dma_lock();
1903     	fd_disable_dma();
1904     	release_dma_lock(flags);
1905     	
1906     	/* avoid dma going to a random drive after shutdown */
1907     
1908     	if (!initialising)
1909     		DPRINT("floppy timeout called\n");
1910     	FDCS->reset = 1;
1911     	if (cont){
1912     		cont->done(0);
1913     		cont->redo(); /* this will recall reset when needed */
1914     	} else {
1915     		printk("no cont in shutdown!\n");
1916     		process_fd_request();
1917     	}
1918     	is_alive("floppy shutdown");
1919     }
1920     /*typedef void (*timeout_fn)(unsigned long);*/
1921     
1922     /* start motor, check media-changed condition and write protection */
1923     static int start_motor(void (*function)(void) )
1924     {
1925     	int mask, data;
1926     
1927     	mask = 0xfc;
1928     	data = UNIT(current_drive);
1929     	if (!(raw_cmd->flags & FD_RAW_NO_MOTOR)){
1930     		if (!(FDCS->dor & (0x10 << UNIT(current_drive)))){
1931     			set_debugt();
1932     			/* no read since this drive is running */
1933     			DRS->first_read_date = 0;
1934     			/* note motor start time if motor is not yet running */
1935     			DRS->spinup_date = jiffies;
1936     			data |= (0x10 << UNIT(current_drive));
1937     		}
1938     	} else
1939     		if (FDCS->dor & (0x10 << UNIT(current_drive)))
1940     			mask &= ~(0x10 << UNIT(current_drive));
1941     
1942     	/* starts motor and selects floppy */
1943     	del_timer(motor_off_timer + current_drive);
1944     	set_dor(fdc, mask, data);
1945     
1946     	/* wait_for_completion also schedules reset if needed. */
1947     	return(fd_wait_for_completion(DRS->select_date+DP->select_delay,
1948     				   (timeout_fn) function));
1949     }
1950     
1951     static void floppy_ready(void)
1952     {
1953     	CHECK_RESET;
1954     	if (start_motor(floppy_ready)) return;
1955     	if (fdc_dtr()) return;
1956     
1957     #ifdef DCL_DEBUG
1958     	if (DP->flags & FD_DEBUG){
1959     		DPRINT("calling disk change from floppy_ready\n");
1960     	}
1961     #endif
1962     	if (!(raw_cmd->flags & FD_RAW_NO_MOTOR) &&
1963     	   disk_change(current_drive) &&
1964     	   !DP->select_delay)
1965     		twaddle(); /* this clears the dcl on certain drive/controller
1966     			    * combinations */
1967     
1968     #ifdef fd_chose_dma_mode
1969     	if ((raw_cmd->flags & FD_RAW_READ) || 
1970     	    (raw_cmd->flags & FD_RAW_WRITE))
1971     	{
1972     		unsigned long flags = claim_dma_lock();
1973     		fd_chose_dma_mode(raw_cmd->kernel_data,
1974     				  raw_cmd->length);
1975     		release_dma_lock(flags);
1976     	}
1977     #endif
1978     
1979     	if (raw_cmd->flags & (FD_RAW_NEED_SEEK | FD_RAW_NEED_DISK)){
1980     		perpendicular_mode();
1981     		fdc_specify(); /* must be done here because of hut, hlt ... */
1982     		seek_floppy();
1983     	} else {
1984     		if ((raw_cmd->flags & FD_RAW_READ) || 
1985     		    (raw_cmd->flags & FD_RAW_WRITE))
1986     			fdc_specify();
1987     		setup_rw_floppy();
1988     	}
1989     }
1990     
1991     static void floppy_start(void)
1992     {
1993     	reschedule_timeout(CURRENTD, "floppy start", 0);
1994     
1995     	scandrives();
1996     #ifdef DCL_DEBUG
1997     	if (DP->flags & FD_DEBUG){
1998     		DPRINT("setting NEWCHANGE in floppy_start\n");
1999     	}
2000     #endif
2001     	SETF(FD_DISK_NEWCHANGE);
2002     	floppy_ready();
2003     }
2004     
2005     /*
2006      * ========================================================================
2007      * here ends the bottom half. Exported routines are:
2008      * floppy_start, floppy_off, floppy_ready, lock_fdc, unlock_fdc, set_fdc,
2009      * start_motor, reset_fdc, reset_fdc_info, interpret_errors.
2010      * Initialization also uses output_byte, result, set_dor, floppy_interrupt
2011      * and set_dor.
2012      * ========================================================================
2013      */
2014     /*
2015      * General purpose continuations.
2016      * ==============================
2017      */
2018     
2019     static void do_wakeup(void)
2020     {
2021     	reschedule_timeout(MAXTIMEOUT, "do wakeup", 0);
2022     	cont = 0;
2023     	command_status += 2;
2024     	wake_up(&command_done);
2025     }
2026     
2027     static struct cont_t wakeup_cont={
2028     	empty,
2029     	do_wakeup,
2030     	empty,
2031     	(done_f)empty
2032     };
2033     
2034     
2035     static struct cont_t intr_cont={
2036     	empty,
2037     	process_fd_request,
2038     	empty,
2039     	(done_f) empty
2040     };
2041     
2042     static int wait_til_done(void (*handler)(void), int interruptible)
2043     {
2044     	int ret;
2045     
2046     	schedule_bh((void *)(void *)handler);
2047     
2048     	if (command_status < 2 && NO_SIGNAL) {
2049     		DECLARE_WAITQUEUE(wait, current);
2050     
2051     		add_wait_queue(&command_done, &wait);
2052     		for (;;) {
2053     			set_current_state(interruptible?
2054     					  TASK_INTERRUPTIBLE:
2055     					  TASK_UNINTERRUPTIBLE);
2056     
2057     			if (command_status >= 2 || !NO_SIGNAL)
2058     				break;
2059     
2060     			is_alive("wait_til_done");
2061     
2062     			schedule();
2063     		}
2064     
2065     		set_current_state(TASK_RUNNING);
2066     		remove_wait_queue(&command_done, &wait);
2067     	}
2068     
2069     	if (command_status < 2){
2070     		cancel_activity();
2071     		cont = &intr_cont;
2072     		reset_fdc();
2073     		return -EINTR;
2074     	}
2075     
2076     	if (FDCS->reset)
2077     		command_status = FD_COMMAND_ERROR;
2078     	if (command_status == FD_COMMAND_OKAY)
2079     		ret=0;
2080     	else
2081     		ret=-EIO;
2082     	command_status = FD_COMMAND_NONE;
2083     	return ret;
2084     }
2085     
2086     static void generic_done(int result)
2087     {
2088     	command_status = result;
2089     	cont = &wakeup_cont;
2090     }
2091     
2092     static void generic_success(void)
2093     {
2094     	cont->done(1);
2095     }
2096     
2097     static void generic_failure(void)
2098     {
2099     	cont->done(0);
2100     }
2101     
2102     static void success_and_wakeup(void)
2103     {
2104     	generic_success();
2105     	cont->redo();
2106     }
2107     
2108     
2109     /*
2110      * formatting and rw support.
2111      * ==========================
2112      */
2113     
2114     static int next_valid_format(void)
2115     {
2116     	int probed_format;
2117     
2118     	probed_format = DRS->probed_format;
2119     	while(1){
2120     		if (probed_format >= 8 ||
2121     		     !DP->autodetect[probed_format]){
2122     			DRS->probed_format = 0;
2123     			return 1;
2124     		}
2125     		if (floppy_type[DP->autodetect[probed_format]].sect){
2126     			DRS->probed_format = probed_format;
2127     			return 0;
2128     		}
2129     		probed_format++;
2130     	}
2131     }
2132     
2133     static void bad_flp_intr(void)
2134     {
2135     	if (probing){
2136     		DRS->probed_format++;
2137     		if (!next_valid_format())
2138     			return;
2139     	}
2140     	(*errors)++;
2141     	INFBOUND(DRWE->badness, *errors);
2142     	if (*errors > DP->max_errors.abort)
2143     		cont->done(0);
2144     	if (*errors > DP->max_errors.reset)
2145     		FDCS->reset = 1;
2146     	else if (*errors > DP->max_errors.recal)
2147     		DRS->track = NEED_2_RECAL;
2148     }
2149     
2150     static void set_floppy(kdev_t device)
2151     {
2152     	if (TYPE(device))
2153     		_floppy = TYPE(device) + floppy_type;
2154     	else
2155     		_floppy = current_type[ DRIVE(device) ];
2156     }
2157     
2158     /*
2159      * formatting support.
2160      * ===================
2161      */
2162     static void format_interrupt(void)
2163     {
2164     	switch (interpret_errors()){
2165     		case 1:
2166     			cont->error();
2167     		case 2:
2168     			break;
2169     		case 0:
2170     			cont->done(1);
2171     	}
2172     	cont->redo();
2173     }
2174     
2175     #define CODE2SIZE (ssize = ((1 << SIZECODE) + 3) >> 2)
2176     #define FM_MODE(x,y) ((y) & ~(((x)->rate & 0x80) >>1))
2177     #define CT(x) ((x) | 0xc0)
2178     static void setup_format_params(int track)
2179     {
2180     	struct fparm {
2181     		unsigned char track,head,sect,size;
2182     	} *here = (struct fparm *)floppy_track_buffer;
2183     	int il,n;
2184     	int count,head_shift,track_shift;
2185     
2186     	raw_cmd = &default_raw_cmd;
2187     	raw_cmd->track = track;
2188     
2189     	raw_cmd->flags = FD_RAW_WRITE | FD_RAW_INTR | FD_RAW_SPIN |
2190     		FD_RAW_NEED_DISK | FD_RAW_NEED_SEEK;
2191     	raw_cmd->rate = _floppy->rate & 0x43;
2192     	raw_cmd->cmd_count = NR_F;
2193     	COMMAND = FM_MODE(_floppy,FD_FORMAT);
2194     	DR_SELECT = UNIT(current_drive) + PH_HEAD(_floppy,format_req.head);
2195     	F_SIZECODE = FD_SIZECODE(_floppy);
2196     	F_SECT_PER_TRACK = _floppy->sect << 2 >> F_SIZECODE;
2197     	F_GAP = _floppy->fmt_gap;
2198     	F_FILL = FD_FILL_BYTE;
2199     
2200     	raw_cmd->kernel_data = floppy_track_buffer;
2201     	raw_cmd->length = 4 * F_SECT_PER_TRACK;
2202     
2203     	/* allow for about 30ms for data transport per track */
2204     	head_shift  = (F_SECT_PER_TRACK + 5) / 6;
2205     
2206     	/* a ``cylinder'' is two tracks plus a little stepping time */
2207     	track_shift = 2 * head_shift + 3;
2208     
2209     	/* position of logical sector 1 on this track */
2210     	n = (track_shift * format_req.track + head_shift * format_req.head)
2211     		% F_SECT_PER_TRACK;
2212     
2213     	/* determine interleave */
2214     	il = 1;
2215     	if (_floppy->fmt_gap < 0x22)
2216     		il++;
2217     
2218     	/* initialize field */
2219     	for (count = 0; count < F_SECT_PER_TRACK; ++count) {
2220     		here[count].track = format_req.track;
2221     		here[count].head = format_req.head;
2222     		here[count].sect = 0;
2223     		here[count].size = F_SIZECODE;
2224     	}
2225     	/* place logical sectors */
2226     	for (count = 1; count <= F_SECT_PER_TRACK; ++count) {
2227     		here[n].sect = count;
2228     		n = (n+il) % F_SECT_PER_TRACK;
2229     		if (here[n].sect) { /* sector busy, find next free sector */
2230     			++n;
2231     			if (n>= F_SECT_PER_TRACK) {
2232     				n-=F_SECT_PER_TRACK;
2233     				while (here[n].sect) ++n;
2234     			}
2235     		}
2236     	}
2237     }
2238     
2239     static void redo_format(void)
2240     {
2241     	buffer_track = -1;
2242     	setup_format_params(format_req.track << STRETCH(_floppy));
2243     	floppy_start();
2244     #ifdef DEBUGT
2245     	debugt("queue format request");
2246     #endif
2247     }
2248     
2249     static struct cont_t format_cont={
2250     	format_interrupt,
2251     	redo_format,
2252     	bad_flp_intr,
2253     	generic_done };
2254     
2255     static int do_format(kdev_t device, struct format_descr *tmp_format_req)
2256     {
2257     	int ret;
2258     	int drive=DRIVE(device);
2259     
2260     	LOCK_FDC(drive,1);
2261     	set_floppy(device);
2262     	if (!_floppy ||
2263     	    _floppy->track > DP->tracks ||
2264     	    tmp_format_req->track >= _floppy->track ||
2265     	    tmp_format_req->head >= _floppy->head ||
2266     	    (_floppy->sect << 2) % (1 <<  FD_SIZECODE(_floppy)) ||
2267     	    !_floppy->fmt_gap) {
2268     		process_fd_request();
2269     		return -EINVAL;
2270     	}
2271     	format_req = *tmp_format_req;
2272     	format_errors = 0;
2273     	cont = &format_cont;
2274     	errors = &format_errors;
2275     	IWAIT(redo_format);
2276     	process_fd_request();
2277     	return ret;
2278     }
2279     
2280     /*
2281      * Buffer read/write and support
2282      * =============================
2283      */
2284     
2285     /* new request_done. Can handle physical sectors which are smaller than a
2286      * logical buffer */
2287     static void request_done(int uptodate)
2288     {
2289     	int block;
2290     	unsigned long flags;
2291     
2292     	probing = 0;
2293     	reschedule_timeout(MAXTIMEOUT, "request done %d", uptodate);
2294     
2295     	if (QUEUE_EMPTY){
2296     		DPRINT("request list destroyed in floppy request done\n");
2297     		return;
2298     	}
2299     
2300     	if (uptodate){
2301     		/* maintain values for invalidation on geometry
2302     		 * change */
2303     		block = current_count_sectors + CURRENT->sector;
2304     		INFBOUND(DRS->maxblock, block);
2305     		if (block > _floppy->sect)
2306     			DRS->maxtrack = 1;
2307     
2308     		/* unlock chained buffers */
2309     		spin_lock_irqsave(&io_request_lock, flags);
2310     		while (current_count_sectors && !QUEUE_EMPTY &&
2311     		       current_count_sectors >= CURRENT->current_nr_sectors){
2312     			current_count_sectors -= CURRENT->current_nr_sectors;
2313     			CURRENT->nr_sectors -= CURRENT->current_nr_sectors;
2314     			CURRENT->sector += CURRENT->current_nr_sectors;
2315     			end_request(1);
2316     		}
2317     		spin_unlock_irqrestore(&io_request_lock, flags);
2318     
2319     		if (current_count_sectors && !QUEUE_EMPTY){
2320     			/* "unlock" last subsector */
2321     			CURRENT->buffer += current_count_sectors <<9;
2322     			CURRENT->current_nr_sectors -= current_count_sectors;
2323     			CURRENT->nr_sectors -= current_count_sectors;
2324     			CURRENT->sector += current_count_sectors;
2325     			return;
2326     		}
2327     
2328     		if (current_count_sectors && QUEUE_EMPTY)
2329     			DPRINT("request list destroyed in floppy request done\n");
2330     
2331     	} else {
2332     		if (CURRENT->cmd == WRITE) {
2333     			/* record write error information */
2334     			DRWE->write_errors++;
2335     			if (DRWE->write_errors == 1) {
2336     				DRWE->first_error_sector = CURRENT->sector;
2337     				DRWE->first_error_generation = DRS->generation;
2338     			}
2339     			DRWE->last_error_sector = CURRENT->sector;
2340     			DRWE->last_error_generation = DRS->generation;
2341     		}
2342     		spin_lock_irqsave(&io_request_lock, flags);
2343     		end_request(0);
2344     		spin_unlock_irqrestore(&io_request_lock, flags);
2345     	}
2346     }
2347     
2348     /* Interrupt handler evaluating the result of the r/w operation */
2349     static void rw_interrupt(void)
2350     {
2351     	int nr_sectors, ssize, eoc, heads;
2352     
2353     	if (R_HEAD >= 2) {
2354     	    /* some Toshiba floppy controllers occasionnally seem to
2355     	     * return bogus interrupts after read/write operations, which
2356     	     * can be recognized by a bad head number (>= 2) */
2357     	     return;
2358     	}  
2359     
2360     	if (!DRS->first_read_date)
2361     		DRS->first_read_date = jiffies;
2362     
2363     	nr_sectors = 0;
2364     	CODE2SIZE;
2365     
2366     	if (ST1 & ST1_EOC)
2367     		eoc = 1;
2368     	else
2369     		eoc = 0;
2370     
2371     	if (COMMAND & 0x80)
2372     		heads = 2;
2373     	else
2374     		heads = 1;
2375     
2376     	nr_sectors = (((R_TRACK-TRACK) * heads +
2377     				   R_HEAD-HEAD) * SECT_PER_TRACK +
2378     				   R_SECTOR-SECTOR + eoc) << SIZECODE >> 2;
2379     
2380     #ifdef FLOPPY_SANITY_CHECK
2381     	if (nr_sectors / ssize > 
2382     		(in_sector_offset + current_count_sectors + ssize - 1) / ssize) {
2383     		DPRINT("long rw: %x instead of %lx\n",
2384     			nr_sectors, current_count_sectors);
2385     		printk("rs=%d s=%d\n", R_SECTOR, SECTOR);
2386     		printk("rh=%d h=%d\n", R_HEAD, HEAD);
2387     		printk("rt=%d t=%d\n", R_TRACK, TRACK);
2388     		printk("heads=%d eoc=%d\n", heads, eoc);
2389     		printk("spt=%d st=%d ss=%d\n", SECT_PER_TRACK,
2390     		       sector_t, ssize);
2391     		printk("in_sector_offset=%d\n", in_sector_offset);
2392     	}
2393     #endif
2394     
2395     	nr_sectors -= in_sector_offset;
2396     	INFBOUND(nr_sectors,0);
2397     	SUPBOUND(current_count_sectors, nr_sectors);
2398     
2399     	switch (interpret_errors()){
2400     		case 2:
2401     			cont->redo();
2402     			return;
2403     		case 1:
2404     			if (!current_count_sectors){
2405     				cont->error();
2406     				cont->redo();
2407     				return;
2408     			}
2409     			break;
2410     		case 0:
2411     			if (!current_count_sectors){
2412     				cont->redo();
2413     				return;
2414     			}
2415     			current_type[current_drive] = _floppy;
2416     			floppy_sizes[TOMINOR(current_drive) ]= 
2417     				(_floppy->size+1)>>1;
2418     			break;
2419     	}
2420     
2421     	if (probing) {
2422     		if (DP->flags & FTD_MSG)
2423     			DPRINT("Auto-detected floppy type %s in fd%d\n",
2424     				_floppy->name,current_drive);
2425     		current_type[current_drive] = _floppy;
2426     		floppy_sizes[TOMINOR(current_drive)] = (_floppy->size+1) >> 1;
2427     		probing = 0;
2428     	}
2429     
2430     	if (CT(COMMAND) != FD_READ || 
2431     	     raw_cmd->kernel_data == CURRENT->buffer){
2432     		/* transfer directly from buffer */
2433     		cont->done(1);
2434     	} else if (CT(COMMAND) == FD_READ){
2435     		buffer_track = raw_cmd->track;
2436     		buffer_drive = current_drive;
2437     		INFBOUND(buffer_max, nr_sectors + sector_t);
2438     	}
2439     	cont->redo();
2440     }
2441     
2442     /* Compute maximal contiguous buffer size. */
2443     static int buffer_chain_size(void)
2444     {
2445     	struct buffer_head *bh;
2446     	int size;
2447     	char *base;
2448     
2449     	base = CURRENT->buffer;
2450     	size = CURRENT->current_nr_sectors << 9;
2451     	bh = CURRENT->bh;
2452     
2453     	if (bh){
2454     		bh = bh->b_reqnext;
2455     		while (bh && bh->b_data == base + size){
2456     			size += bh->b_size;
2457     			bh = bh->b_reqnext;
2458     		}
2459     	}
2460     	return size >> 9;
2461     }
2462     
2463     /* Compute the maximal transfer size */
2464     static int transfer_size(int ssize, int max_sector, int max_size)
2465     {
2466     	SUPBOUND(max_sector, sector_t + max_size);
2467     
2468     	/* alignment */
2469     	max_sector -= (max_sector % _floppy->sect) % ssize;
2470     
2471     	/* transfer size, beginning not aligned */
2472     	current_count_sectors = max_sector - sector_t ;
2473     
2474     	return max_sector;
2475     }
2476     
2477     /*
2478      * Move data from/to the track buffer to/from the buffer cache.
2479      */
2480     static void copy_buffer(int ssize, int max_sector, int max_sector_2)
2481     {
2482     	int remaining; /* number of transferred 512-byte sectors */
2483     	struct buffer_head *bh;
2484     	char *buffer, *dma_buffer;
2485     	int size;
2486     
2487     	max_sector = transfer_size(ssize,
2488     				   minimum(max_sector, max_sector_2),
2489     				   CURRENT->nr_sectors);
2490     
2491     	if (current_count_sectors <= 0 && CT(COMMAND) == FD_WRITE &&
2492     	    buffer_max > sector_t + CURRENT->nr_sectors)
2493     		current_count_sectors = minimum(buffer_max - sector_t,
2494     						CURRENT->nr_sectors);
2495     
2496     	remaining = current_count_sectors << 9;
2497     #ifdef FLOPPY_SANITY_CHECK
2498     	if ((remaining >> 9) > CURRENT->nr_sectors  &&
2499     	    CT(COMMAND) == FD_WRITE){
2500     		DPRINT("in copy buffer\n");
2501     		printk("current_count_sectors=%ld\n", current_count_sectors);
2502     		printk("remaining=%d\n", remaining >> 9);
2503     		printk("CURRENT->nr_sectors=%ld\n",CURRENT->nr_sectors);
2504     		printk("CURRENT->current_nr_sectors=%ld\n",
2505     		       CURRENT->current_nr_sectors);
2506     		printk("max_sector=%d\n", max_sector);
2507     		printk("ssize=%d\n", ssize);
2508     	}
2509     #endif
2510     
2511     	buffer_max = maximum(max_sector, buffer_max);
2512     
2513     	dma_buffer = floppy_track_buffer + ((sector_t - buffer_min) << 9);
2514     
2515     	bh = CURRENT->bh;
2516     	size = CURRENT->current_nr_sectors << 9;
2517     	buffer = CURRENT->buffer;
2518     
2519     	while (remaining > 0){
2520     		SUPBOUND(size, remaining);
2521     #ifdef FLOPPY_SANITY_CHECK
2522     		if (dma_buffer + size >
2523     		    floppy_track_buffer + (max_buffer_sectors << 10) ||
2524     		    dma_buffer < floppy_track_buffer){
2525     			DPRINT("buffer overrun in copy buffer %d\n",
2526     				(int) ((floppy_track_buffer - dma_buffer) >>9));
2527     			printk("sector_t=%d buffer_min=%d\n",
2528     			       sector_t, buffer_min);
2529     			printk("current_count_sectors=%ld\n",
2530     			       current_count_sectors);
2531     			if (CT(COMMAND) == FD_READ)
2532     				printk("read\n");
2533     			if (CT(COMMAND) == FD_READ)
2534     				printk("write\n");
2535     			break;
2536     		}
2537     		if (((unsigned long)buffer) % 512)
2538     			DPRINT("%p buffer not aligned\n", buffer);
2539     #endif
2540     		if (CT(COMMAND) == FD_READ)
2541     			memcpy(buffer, dma_buffer, size);
2542     		else
2543     			memcpy(dma_buffer, buffer, size);
2544     		remaining -= size;
2545     		if (!remaining)
2546     			break;
2547     
2548     		dma_buffer += size;
2549     		bh = bh->b_reqnext;
2550     #ifdef FLOPPY_SANITY_CHECK
2551     		if (!bh){
2552     			DPRINT("bh=null in copy buffer after copy\n");
2553     			break;
2554     		}
2555     #endif
2556     		size = bh->b_size;
2557     		buffer = bh->b_data;
2558     	}
2559     #ifdef FLOPPY_SANITY_CHECK
2560     	if (remaining){
2561     		if (remaining > 0)
2562     			max_sector -= remaining >> 9;
2563     		DPRINT("weirdness: remaining %d\n", remaining>>9);
2564     	}
2565     #endif
2566     }
2567     
2568     #if 0
2569     static inline int check_dma_crossing(char *start, 
2570     				     unsigned long length, char *message)
2571     {
2572     	if (CROSS_64KB(start, length)) {
2573     		printk("DMA xfer crosses 64KB boundary in %s %p-%p\n", 
2574     		       message, start, start+length);
2575     		return 1;
2576     	} else
2577     		return 0;
2578     }
2579     #endif
2580     
2581     /* work around a bug in pseudo DMA
2582      * (on some FDCs) pseudo DMA does not stop when the CPU stops
2583      * sending data.  Hence we need a different way to signal the
2584      * transfer length:  We use SECT_PER_TRACK.  Unfortunately, this
2585      * does not work with MT, hence we can only transfer one head at
2586      * a time
2587      */
2588     static void virtualdmabug_workaround(void)
2589     {
2590     	int hard_sectors, end_sector;
2591     
2592     	if(CT(COMMAND) == FD_WRITE) {
2593     		COMMAND &= ~0x80; /* switch off multiple track mode */
2594     
2595     		hard_sectors = raw_cmd->length >> (7 + SIZECODE);
2596     		end_sector = SECTOR + hard_sectors - 1;
2597     #ifdef FLOPPY_SANITY_CHECK
2598     		if(end_sector > SECT_PER_TRACK) {
2599     			printk("too many sectors %d > %d\n",
2600     			       end_sector, SECT_PER_TRACK);
2601     			return;
2602     		}
2603     #endif
2604     		SECT_PER_TRACK = end_sector; /* make sure SECT_PER_TRACK points
2605     					      * to end of transfer */
2606     	}
2607     }
2608     
2609     /*
2610      * Formulate a read/write request.
2611      * this routine decides where to load the data (directly to buffer, or to
2612      * tmp floppy area), how much data to load (the size of the buffer, the whole
2613      * track, or a single sector)
2614      * All floppy_track_buffer handling goes in here. If we ever add track buffer
2615      * allocation on the fly, it should be done here. No other part should need
2616      * modification.
2617      */
2618     
2619     static int make_raw_rw_request(void)
2620     {
2621     	int aligned_sector_t;
2622     	int max_sector, max_size, tracksize, ssize;
2623     
2624     	if(max_buffer_sectors == 0) {
2625     		printk("VFS: Block I/O scheduled on unopened device\n");
2626     		return 0;
2627     	}
2628     
2629     	set_fdc(DRIVE(CURRENT->rq_dev));
2630     
2631     	raw_cmd = &default_raw_cmd;
2632     	raw_cmd->flags = FD_RAW_SPIN | FD_RAW_NEED_DISK | FD_RAW_NEED_DISK |
2633     		FD_RAW_NEED_SEEK;
2634     	raw_cmd->cmd_count = NR_RW;
2635     	if (CURRENT->cmd == READ){
2636     		raw_cmd->flags |= FD_RAW_READ;
2637     		COMMAND = FM_MODE(_floppy,FD_READ);
2638     	} else if (CURRENT->cmd == WRITE){
2639     		raw_cmd->flags |= FD_RAW_WRITE;
2640     		COMMAND = FM_MODE(_floppy,FD_WRITE);
2641     	} else {
2642     		DPRINT("make_raw_rw_request: unknown command\n");
2643     		return 0;
2644     	}
2645     
2646     	max_sector = _floppy->sect * _floppy->head;
2647     
2648     	TRACK = CURRENT->sector / max_sector;
2649     	sector_t = CURRENT->sector % max_sector;
2650     	if (_floppy->track && TRACK >= _floppy->track) {
2651     		if (CURRENT->current_nr_sectors & 1) {
2652     			current_count_sectors = 1;
2653     			return 1;
2654     		} else
2655     			return 0;
2656     	}
2657     	HEAD = sector_t / _floppy->sect;
2658     
2659     	if (((_floppy->stretch & FD_SWAPSIDES) || TESTF(FD_NEED_TWADDLE)) &&
2660     	    sector_t < _floppy->sect)
2661     		max_sector = _floppy->sect;
2662     
2663     	/* 2M disks have phantom sectors on the first track */
2664     	if ((_floppy->rate & FD_2M) && (!TRACK) && (!HEAD)){
2665     		max_sector = 2 * _floppy->sect / 3;
2666     		if (sector_t >= max_sector){
2667     			current_count_sectors = minimum(_floppy->sect - sector_t,
2668     							CURRENT->nr_sectors);
2669     			return 1;
2670     		}
2671     		SIZECODE = 2;
2672     	} else
2673     		SIZECODE = FD_SIZECODE(_floppy);
2674     	raw_cmd->rate = _floppy->rate & 0x43;
2675     	if ((_floppy->rate & FD_2M) &&
2676     	    (TRACK || HEAD) &&
2677     	    raw_cmd->rate == 2)
2678     		raw_cmd->rate = 1;
2679     
2680     	if (SIZECODE)
2681     		SIZECODE2 = 0xff;
2682     	else
2683     		SIZECODE2 = 0x80;
2684     	raw_cmd->track = TRACK << STRETCH(_floppy);
2685     	DR_SELECT = UNIT(current_drive) + PH_HEAD(_floppy,HEAD);
2686     	GAP = _floppy->gap;
2687     	CODE2SIZE;
2688     	SECT_PER_TRACK = _floppy->sect << 2 >> SIZECODE;
2689     	SECTOR = ((sector_t % _floppy->sect) << 2 >> SIZECODE) + 1;
2690     
2691     	/* tracksize describes the size which can be filled up with sectors
2692     	 * of size ssize.
2693     	 */
2694     	tracksize = _floppy->sect - _floppy->sect % ssize;
2695     	if (tracksize < _floppy->sect){
2696     		SECT_PER_TRACK ++;
2697     		if (tracksize <= sector_t % _floppy->sect)
2698     			SECTOR--;
2699     
2700     		/* if we are beyond tracksize, fill up using smaller sectors */
2701     		while (tracksize <= sector_t % _floppy->sect){
2702     			while(tracksize + ssize > _floppy->sect){
2703     				SIZECODE--;
2704     				ssize >>= 1;
2705     			}
2706     			SECTOR++; SECT_PER_TRACK ++;
2707     			tracksize += ssize;
2708     		}
2709     		max_sector = HEAD * _floppy->sect + tracksize;
2710     	} else if (!TRACK && !HEAD && !(_floppy->rate & FD_2M) && probing) {
2711     		max_sector = _floppy->sect;
2712     	} else if (!HEAD && CT(COMMAND) == FD_WRITE) {
2713     		/* for virtual DMA bug workaround */
2714     		max_sector = _floppy->sect;
2715     	}
2716     
2717     	in_sector_offset = (sector_t % _floppy->sect) % ssize;
2718     	aligned_sector_t = sector_t - in_sector_offset;
2719     	max_size = CURRENT->nr_sectors;
2720     	if ((raw_cmd->track == buffer_track) && 
2721     	    (current_drive == buffer_drive) &&
2722     	    (sector_t >= buffer_min) && (sector_t < buffer_max)) {
2723     		/* data already in track buffer */
2724     		if (CT(COMMAND) == FD_READ) {
2725     			copy_buffer(1, max_sector, buffer_max);
2726     			return 1;
2727     		}
2728     	} else if (in_sector_offset || CURRENT->nr_sectors < ssize){
2729     		if (CT(COMMAND) == FD_WRITE){
2730     			if (sector_t + CURRENT->nr_sectors > ssize &&
2731     			    sector_t + CURRENT->nr_sectors < ssize + ssize)
2732     				max_size = ssize + ssize;
2733     			else
2734     				max_size = ssize;
2735     		}
2736     		raw_cmd->flags &= ~FD_RAW_WRITE;
2737     		raw_cmd->flags |= FD_RAW_READ;
2738     		COMMAND = FM_MODE(_floppy,FD_READ);
2739     	} else if ((unsigned long)CURRENT->buffer < MAX_DMA_ADDRESS) {
2740     		unsigned long dma_limit;
2741     		int direct, indirect;
2742     
2743     		indirect= transfer_size(ssize,max_sector,max_buffer_sectors*2) -
2744     			sector_t;
2745     
2746     		/*
2747     		 * Do NOT use minimum() here---MAX_DMA_ADDRESS is 64 bits wide
2748     		 * on a 64 bit machine!
2749     		 */
2750     		max_size = buffer_chain_size();
2751     		dma_limit = (MAX_DMA_ADDRESS - ((unsigned long) CURRENT->buffer)) >> 9;
2752     		if ((unsigned long) max_size > dma_limit) {
2753     			max_size = dma_limit;
2754     		}
2755     		/* 64 kb boundaries */
2756     		if (CROSS_64KB(CURRENT->buffer, max_size << 9))
2757     			max_size = (K_64 - 
2758     				    ((unsigned long)CURRENT->buffer) % K_64)>>9;
2759     		direct = transfer_size(ssize,max_sector,max_size) - sector_t;
2760     		/*
2761     		 * We try to read tracks, but if we get too many errors, we
2762     		 * go back to reading just one sector at a time.
2763     		 *
2764     		 * This means we should be able to read a sector even if there
2765     		 * are other bad sectors on this track.
2766     		 */
2767     		if (!direct ||
2768     		    (indirect * 2 > direct * 3 &&
2769     		     *errors < DP->max_errors.read_track &&
2770     		     /*!TESTF(FD_NEED_TWADDLE) &&*/
2771     		     ((!probing || (DP->read_track&(1<<DRS->probed_format)))))){
2772     			max_size = CURRENT->nr_sectors;
2773     		} else {
2774     			raw_cmd->kernel_data = CURRENT->buffer;
2775     			raw_cmd->length = current_count_sectors << 9;
2776     			if (raw_cmd->length == 0){
2777     				DPRINT("zero dma transfer attempted from make_raw_request\n");
2778     				DPRINT("indirect=%d direct=%d sector_t=%d",
2779     					indirect, direct, sector_t);
2780     				return 0;
2781     			}
2782     /*			check_dma_crossing(raw_cmd->kernel_data, 
2783     					   raw_cmd->length, 
2784     					   "end of make_raw_request [1]");*/
2785     
2786     			virtualdmabug_workaround();
2787     			return 2;
2788     		}
2789     	}
2790     
2791     	if (CT(COMMAND) == FD_READ)
2792     		max_size = max_sector; /* unbounded */
2793     
2794     	/* claim buffer track if needed */
2795     	if (buffer_track != raw_cmd->track ||  /* bad track */
2796     	    buffer_drive !=current_drive || /* bad drive */
2797     	    sector_t > buffer_max ||
2798     	    sector_t < buffer_min ||
2799     	    ((CT(COMMAND) == FD_READ ||
2800     	      (!in_sector_offset && CURRENT->nr_sectors >= ssize))&&
2801     	     max_sector > 2 * max_buffer_sectors + buffer_min &&
2802     	     max_size + sector_t > 2 * max_buffer_sectors + buffer_min)
2803     	    /* not enough space */){
2804     		buffer_track = -1;
2805     		buffer_drive = current_drive;
2806     		buffer_max = buffer_min = aligned_sector_t;
2807     	}
2808     	raw_cmd->kernel_data = floppy_track_buffer + 
2809     		((aligned_sector_t-buffer_min)<<9);
2810     
2811     	if (CT(COMMAND) == FD_WRITE){
2812     		/* copy write buffer to track buffer.
2813     		 * if we get here, we know that the write
2814     		 * is either aligned or the data already in the buffer
2815     		 * (buffer will be overwritten) */
2816     #ifdef FLOPPY_SANITY_CHECK
2817     		if (in_sector_offset && buffer_track == -1)
2818     			DPRINT("internal error offset !=0 on write\n");
2819     #endif
2820     		buffer_track = raw_cmd->track;
2821     		buffer_drive = current_drive;
2822     		copy_buffer(ssize, max_sector, 2*max_buffer_sectors+buffer_min);
2823     	} else
2824     		transfer_size(ssize, max_sector,
2825     			      2*max_buffer_sectors+buffer_min-aligned_sector_t);
2826     
2827     	/* round up current_count_sectors to get dma xfer size */
2828     	raw_cmd->length = in_sector_offset+current_count_sectors;
2829     	raw_cmd->length = ((raw_cmd->length -1)|(ssize-1))+1;
2830     	raw_cmd->length <<= 9;
2831     #ifdef FLOPPY_SANITY_CHECK
2832     	/*check_dma_crossing(raw_cmd->kernel_data, raw_cmd->length, 
2833     	  "end of make_raw_request");*/
2834     	if ((raw_cmd->length < current_count_sectors << 9) ||
2835     	    (raw_cmd->kernel_data != CURRENT->buffer &&
2836     	     CT(COMMAND) == FD_WRITE &&
2837     	     (aligned_sector_t + (raw_cmd->length >> 9) > buffer_max ||
2838     	      aligned_sector_t < buffer_min)) ||
2839     	    raw_cmd->length % (128 << SIZECODE) ||
2840     	    raw_cmd->length <= 0 || current_count_sectors <= 0){
2841     		DPRINT("fractionary current count b=%lx s=%lx\n",
2842     			raw_cmd->length, current_count_sectors);
2843     		if (raw_cmd->kernel_data != CURRENT->buffer)
2844     			printk("addr=%d, length=%ld\n",
2845     			       (int) ((raw_cmd->kernel_data - 
2846     				       floppy_track_buffer) >> 9),
2847     			       current_count_sectors);
2848     		printk("st=%d ast=%d mse=%d msi=%d\n",
2849     		       sector_t, aligned_sector_t, max_sector, max_size);
2850     		printk("ssize=%x SIZECODE=%d\n", ssize, SIZECODE);
2851     		printk("command=%x SECTOR=%d HEAD=%d, TRACK=%d\n",
2852     		       COMMAND, SECTOR, HEAD, TRACK);
2853     		printk("buffer drive=%d\n", buffer_drive);
2854     		printk("buffer track=%d\n", buffer_track);
2855     		printk("buffer_min=%d\n", buffer_min);
2856     		printk("buffer_max=%d\n", buffer_max);
2857     		return 0;
2858     	}
2859     
2860     	if (raw_cmd->kernel_data != CURRENT->buffer){
2861     		if (raw_cmd->kernel_data < floppy_track_buffer ||
2862     		    current_count_sectors < 0 ||
2863     		    raw_cmd->length < 0 ||
2864     		    raw_cmd->kernel_data + raw_cmd->length >
2865     		    floppy_track_buffer + (max_buffer_sectors  << 10)){
2866     			DPRINT("buffer overrun in schedule dma\n");
2867     			printk("sector_t=%d buffer_min=%d current_count=%ld\n",
2868     			       sector_t, buffer_min,
2869     			       raw_cmd->length >> 9);
2870     			printk("current_count_sectors=%ld\n",
2871     			       current_count_sectors);
2872     			if (CT(COMMAND) == FD_READ)
2873     				printk("read\n");
2874     			if (CT(COMMAND) == FD_READ)
2875     				printk("write\n");
2876     			return 0;
2877     		}
2878     	} else if (raw_cmd->length > CURRENT->nr_sectors << 9 ||
2879     		   current_count_sectors > CURRENT->nr_sectors){
2880     		DPRINT("buffer overrun in direct transfer\n");
2881     		return 0;
2882     	} else if (raw_cmd->length < current_count_sectors << 9){
2883     		DPRINT("more sectors than bytes\n");
2884     		printk("bytes=%ld\n", raw_cmd->length >> 9);
2885     		printk("sectors=%ld\n", current_count_sectors);
2886     	}
2887     	if (raw_cmd->length == 0){
2888     		DPRINT("zero dma transfer attempted from make_raw_request\n");
2889     		return 0;
2890     	}
2891     #endif
2892     
2893     	virtualdmabug_workaround();
2894     	return 2;
2895     }
2896     
2897     static void redo_fd_request(void)
2898     {
2899     #define REPEAT {request_done(0); continue; }
2900     	kdev_t device;
2901     	int tmp;
2902     
2903     	lastredo = jiffies;
2904     	if (current_drive < N_DRIVE)
2905     		floppy_off(current_drive);
2906     
2907     	if (!QUEUE_EMPTY && CURRENT->rq_status == RQ_INACTIVE){
2908     		CLEAR_INTR;
2909     		unlock_fdc();
2910     		return;
2911     	}
2912     
2913     	while(1){
2914     		if (QUEUE_EMPTY) {
2915     			CLEAR_INTR;
2916     			unlock_fdc();
2917     			return;
2918     		}
2919     		if (MAJOR(CURRENT->rq_dev) != MAJOR_NR)
2920     			panic(DEVICE_NAME ": request list destroyed");
2921     		if (CURRENT->bh && !buffer_locked(CURRENT->bh))
2922     			panic(DEVICE_NAME ": block not locked");
2923     
2924     		device = CURRENT->rq_dev;
2925     		set_fdc(DRIVE(device));
2926     		reschedule_timeout(CURRENTD, "redo fd request", 0);
2927     
2928     		set_floppy(device);
2929     		raw_cmd = & default_raw_cmd;
2930     		raw_cmd->flags = 0;
2931     		if (start_motor(redo_fd_request)) return;
2932     		disk_change(current_drive);
2933     		if (test_bit(current_drive, &fake_change) ||
2934     		   TESTF(FD_DISK_CHANGED)){
2935     			DPRINT("disk absent or changed during operation\n");
2936     			REPEAT;
2937     		}
2938     		if (!_floppy) { /* Autodetection */
2939     			if (!probing){
2940     				DRS->probed_format = 0;
2941     				if (next_valid_format()){
2942     					DPRINT("no autodetectable formats\n");
2943     					_floppy = NULL;
2944     					REPEAT;
2945     				}
2946     			}
2947     			probing = 1;
2948     			_floppy = floppy_type+DP->autodetect[DRS->probed_format];
2949     		} else
2950     			probing = 0;
2951     		errors = & (CURRENT->errors);
2952     		tmp = make_raw_rw_request();
2953     		if (tmp < 2){
2954     			request_done(tmp);
2955     			continue;
2956     		}
2957     
2958     		if (TESTF(FD_NEED_TWADDLE))
2959     			twaddle();
2960     		schedule_bh( (void *)(void *) floppy_start);
2961     #ifdef DEBUGT
2962     		debugt("queue fd request");
2963     #endif
2964     		return;
2965     	}
2966     #undef REPEAT
2967     }
2968     
2969     static struct cont_t rw_cont={
2970     	rw_interrupt,
2971     	redo_fd_request,
2972     	bad_flp_intr,
2973     	request_done };
2974     
2975     static void process_fd_request(void)
2976     {
2977     	cont = &rw_cont;
2978     	schedule_bh( (void *)(void *) redo_fd_request);
2979     }
2980     
2981     static void do_fd_request(request_queue_t * q)
2982     {
2983     	if(max_buffer_sectors == 0) {
2984     		printk("VFS: do_fd_request called on non-open device\n");
2985     		return;
2986     	}
2987     
2988     	if (usage_count == 0) {
2989     		printk("warning: usage count=0, CURRENT=%p exiting\n", CURRENT);
2990     		printk("sect=%ld cmd=%d\n", CURRENT->sector, CURRENT->cmd);
2991     		return;
2992     	}
2993     	if (fdc_busy){
2994     		/* fdc busy, this new request will be treated when the
2995     		   current one is done */
2996     		is_alive("do fd request, old request running");
2997     		return;
2998     	}
2999     	lock_fdc(MAXTIMEOUT,0);
3000     	process_fd_request();
3001     	is_alive("do fd request");
3002     }
3003     
3004     static struct cont_t poll_cont={
3005     	success_and_wakeup,
3006     	floppy_ready,
3007     	generic_failure,
3008     	generic_done };
3009     
3010     static int poll_drive(int interruptible, int flag)
3011     {
3012     	int ret;
3013     	/* no auto-sense, just clear dcl */
3014     	raw_cmd = &default_raw_cmd;
3015     	raw_cmd->flags= flag;
3016     	raw_cmd->track=0;
3017     	raw_cmd->cmd_count=0;
3018     	cont = &poll_cont;
3019     #ifdef DCL_DEBUG
3020     	if (DP->flags & FD_DEBUG){
3021     		DPRINT("setting NEWCHANGE in poll_drive\n");
3022     	}
3023     #endif
3024     	SETF(FD_DISK_NEWCHANGE);
3025     	WAIT(floppy_ready);
3026     	return ret;
3027     }
3028     
3029     /*
3030      * User triggered reset
3031      * ====================
3032      */
3033     
3034     static void reset_intr(void)
3035     {
3036     	printk("weird, reset interrupt called\n");
3037     }
3038     
3039     static struct cont_t reset_cont={
3040     	reset_intr,
3041     	success_and_wakeup,
3042     	generic_failure,
3043     	generic_done };
3044     
3045     static int user_reset_fdc(int drive, int arg, int interruptible)
3046     {
3047     	int ret;
3048     
3049     	ret=0;
3050     	LOCK_FDC(drive,interruptible);
3051     	if (arg == FD_RESET_ALWAYS)
3052     		FDCS->reset=1;
3053     	if (FDCS->reset){
3054     		cont = &reset_cont;
3055     		WAIT(reset_fdc);
3056     	}
3057     	process_fd_request();
3058     	return ret;
3059     }
3060     
3061     /*
3062      * Misc Ioctl's and support
3063      * ========================
3064      */
3065     static inline int fd_copyout(void *param, const void *address, unsigned long size)
3066     {
3067     	return copy_to_user(param,address, size) ? -EFAULT : 0;
3068     }
3069     
3070     static inline int fd_copyin(void *param, void *address, unsigned long size)
3071     {
3072     	return copy_from_user(address, param, size) ? -EFAULT : 0;
3073     }
3074     
3075     #define _COPYOUT(x) (copy_to_user((void *)param, &(x), sizeof(x)) ? -EFAULT : 0)
3076     #define _COPYIN(x) (copy_from_user(&(x), (void *)param, sizeof(x)) ? -EFAULT : 0)
3077     
3078     #define COPYOUT(x) ECALL(_COPYOUT(x))
3079     #define COPYIN(x) ECALL(_COPYIN(x))
3080     
3081     static inline const char *drive_name(int type, int drive)
3082     {
3083     	struct floppy_struct *floppy;
3084     
3085     	if (type)
3086     		floppy = floppy_type + type;
3087     	else {
3088     		if (UDP->native_format)
3089     			floppy = floppy_type + UDP->native_format;
3090     		else
3091     			return "(null)";
3092     	}
3093     	if (floppy->name)
3094     		return floppy->name;
3095     	else
3096     		return "(null)";
3097     }
3098     
3099     
3100     /* raw commands */
3101     static void raw_cmd_done(int flag)
3102     {
3103     	int i;
3104     
3105     	if (!flag) {
3106     		raw_cmd->flags |= FD_RAW_FAILURE;
3107     		raw_cmd->flags |= FD_RAW_HARDFAILURE;
3108     	} else {
3109     		raw_cmd->reply_count = inr;
3110     		if (raw_cmd->reply_count > MAX_REPLIES)
3111     			raw_cmd->reply_count=0;
3112     		for (i=0; i< raw_cmd->reply_count; i++)
3113     			raw_cmd->reply[i] = reply_buffer[i];
3114     
3115     		if (raw_cmd->flags & (FD_RAW_READ | FD_RAW_WRITE))
3116     		{
3117     			unsigned long flags;
3118     			flags=claim_dma_lock();
3119     			raw_cmd->length = fd_get_dma_residue();
3120     			release_dma_lock(flags);
3121     		}
3122     		
3123     		if ((raw_cmd->flags & FD_RAW_SOFTFAILURE) &&
3124     		    (!raw_cmd->reply_count || (raw_cmd->reply[0] & 0xc0)))
3125     			raw_cmd->flags |= FD_RAW_FAILURE;
3126     
3127     		if (disk_change(current_drive))
3128     			raw_cmd->flags |= FD_RAW_DISK_CHANGE;
3129     		else
3130     			raw_cmd->flags &= ~FD_RAW_DISK_CHANGE;
3131     		if (raw_cmd->flags & FD_RAW_NO_MOTOR_AFTER)
3132     			motor_off_callback(current_drive);
3133     
3134     		if (raw_cmd->next &&
3135     		   (!(raw_cmd->flags & FD_RAW_FAILURE) ||
3136     		    !(raw_cmd->flags & FD_RAW_STOP_IF_FAILURE)) &&
3137     		   ((raw_cmd->flags & FD_RAW_FAILURE) ||
3138     		    !(raw_cmd->flags &FD_RAW_STOP_IF_SUCCESS))) {
3139     			raw_cmd = raw_cmd->next;
3140     			return;
3141     		}
3142     	}
3143     	generic_done(flag);
3144     }
3145     
3146     
3147     static struct cont_t raw_cmd_cont={
3148     	success_and_wakeup,
3149     	floppy_start,
3150     	generic_failure,
3151     	raw_cmd_done
3152     };
3153     
3154     static inline int raw_cmd_copyout(int cmd, char *param,
3155     				  struct floppy_raw_cmd *ptr)
3156     {
3157     	int ret;
3158     
3159     	while(ptr) {
3160     		COPYOUT(*ptr);
3161     		param += sizeof(struct floppy_raw_cmd);
3162     		if ((ptr->flags & FD_RAW_READ) && ptr->buffer_length){
3163     			if (ptr->length>=0 && ptr->length<=ptr->buffer_length)
3164     				ECALL(fd_copyout(ptr->data, 
3165     						 ptr->kernel_data, 
3166     						 ptr->buffer_length - 
3167     						 ptr->length));
3168     		}
3169     		ptr = ptr->next;
3170     	}
3171     	return 0;
3172     }
3173     
3174     
3175     static void raw_cmd_free(struct floppy_raw_cmd **ptr)
3176     {
3177     	struct floppy_raw_cmd *next,*this;
3178     
3179     	this = *ptr;
3180     	*ptr = 0;
3181     	while(this) {
3182     		if (this->buffer_length) {
3183     			fd_dma_mem_free((unsigned long)this->kernel_data,
3184     					this->buffer_length);
3185     			this->buffer_length = 0;
3186     		}
3187     		next = this->next;
3188     		kfree(this);
3189     		this = next;
3190     	}
3191     }
3192     
3193     
3194     static inline int raw_cmd_copyin(int cmd, char *param,
3195     				 struct floppy_raw_cmd **rcmd)
3196     {
3197     	struct floppy_raw_cmd *ptr;
3198     	int ret;
3199     	int i;
3200     	
3201     	*rcmd = 0;
3202     	while(1) {
3203     		ptr = (struct floppy_raw_cmd *) 
3204     			kmalloc(sizeof(struct floppy_raw_cmd), GFP_USER);
3205     		if (!ptr)
3206     			return -ENOMEM;
3207     		*rcmd = ptr;
3208     		COPYIN(*ptr);
3209     		ptr->next = 0;
3210     		ptr->buffer_length = 0;
3211     		param += sizeof(struct floppy_raw_cmd);
3212     		if (ptr->cmd_count > 33)
3213     			/* the command may now also take up the space
3214     			 * initially intended for the reply & the
3215     			 * reply count. Needed for long 82078 commands
3216     			 * such as RESTORE, which takes ... 17 command
3217     			 * bytes. Murphy's law #137: When you reserve
3218     			 * 16 bytes for a structure, you'll one day
3219     			 * discover that you really need 17...
3220     			 */
3221     			return -EINVAL;
3222     
3223     		for (i=0; i< 16; i++)
3224     			ptr->reply[i] = 0;
3225     		ptr->resultcode = 0;
3226     		ptr->kernel_data = 0;
3227     
3228     		if (ptr->flags & (FD_RAW_READ | FD_RAW_WRITE)) {
3229     			if (ptr->length <= 0)
3230     				return -EINVAL;
3231     			ptr->kernel_data =(char*)fd_dma_mem_alloc(ptr->length);
3232     			fallback_on_nodma_alloc(&ptr->kernel_data,
3233     						ptr->length);
3234     			if (!ptr->kernel_data)
3235     				return -ENOMEM;
3236     			ptr->buffer_length = ptr->length;
3237     		}
3238     		if (ptr->flags & FD_RAW_WRITE)
3239     			ECALL(fd_copyin(ptr->data, ptr->kernel_data, 
3240     					ptr->length));
3241     		rcmd = & (ptr->next);
3242     		if (!(ptr->flags & FD_RAW_MORE))
3243     			return 0;
3244     		ptr->rate &= 0x43;
3245     	}
3246     }
3247     
3248     
3249     static int raw_cmd_ioctl(int cmd, void *param)
3250     {
3251     	int drive, ret, ret2;
3252     	struct floppy_raw_cmd *my_raw_cmd;
3253     
3254     	if (FDCS->rawcmd <= 1)
3255     		FDCS->rawcmd = 1;
3256     	for (drive= 0; drive < N_DRIVE; drive++){
3257     		if (FDC(drive) != fdc)
3258     			continue;
3259     		if (drive == current_drive){
3260     			if (UDRS->fd_ref > 1){
3261     				FDCS->rawcmd = 2;
3262     				break;
3263     			}
3264     		} else if (UDRS->fd_ref){
3265     			FDCS->rawcmd = 2;
3266     			break;
3267     		}
3268     	}
3269     
3270     	if (FDCS->reset)
3271     		return -EIO;
3272     
3273     	ret = raw_cmd_copyin(cmd, param, &my_raw_cmd);
3274     	if (ret) {
3275     		raw_cmd_free(&my_raw_cmd);
3276     		return ret;
3277     	}
3278     
3279     	raw_cmd = my_raw_cmd;
3280     	cont = &raw_cmd_cont;
3281     	ret=wait_til_done(floppy_start,1);
3282     #ifdef DCL_DEBUG
3283     	if (DP->flags & FD_DEBUG){
3284     		DPRINT("calling disk change from raw_cmd ioctl\n");
3285     	}
3286     #endif
3287     
3288     	if (ret != -EINTR && FDCS->reset)
3289     		ret = -EIO;
3290     
3291     	DRS->track = NO_TRACK;
3292     
3293     	ret2 = raw_cmd_copyout(cmd, param, my_raw_cmd);
3294     	if (!ret)
3295     		ret = ret2;
3296     	raw_cmd_free(&my_raw_cmd);
3297     	return ret;
3298     }
3299     
3300     static int invalidate_drive(kdev_t