pandora: defconfig: update
[pandora-kernel.git] / 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 /*
10  * 02.12.91 - Changed to static variables to indicate need for reset
11  * and recalibrate. This makes some things easier (output_byte reset
12  * checking etc), and means less interrupt jumping in case of errors,
13  * so the code is hopefully easier to understand.
14  */
15
16 /*
17  * This file is certainly a mess. I've tried my best to get it working,
18  * but I don't like programming floppies, and I have only one anyway.
19  * Urgel. I should check for more errors, and do more graceful error
20  * recovery. Seems there are problems with several drives. I've tried to
21  * correct them. No promises.
22  */
23
24 /*
25  * As with hd.c, all routines within this file can (and will) be called
26  * by interrupts, so extreme caution is needed. A hardware interrupt
27  * handler may not sleep, or a kernel panic will happen. Thus I cannot
28  * call "floppy-on" directly, but have to set a special timer interrupt
29  * etc.
30  */
31
32 /*
33  * 28.02.92 - made track-buffering routines, based on the routines written
34  * by entropy@wintermute.wpi.edu (Lawrence Foard). Linus.
35  */
36
37 /*
38  * Automatic floppy-detection and formatting written by Werner Almesberger
39  * (almesber@nessie.cs.id.ethz.ch), who also corrected some problems with
40  * the floppy-change signal detection.
41  */
42
43 /*
44  * 1992/7/22 -- Hennus Bergman: Added better error reporting, fixed
45  * FDC data overrun bug, added some preliminary stuff for vertical
46  * recording support.
47  *
48  * 1992/9/17: Added DMA allocation & DMA functions. -- hhb.
49  *
50  * TODO: Errors are still not counted properly.
51  */
52
53 /* 1992/9/20
54  * Modifications for ``Sector Shifting'' by Rob Hooft (hooft@chem.ruu.nl)
55  * modeled after the freeware MS-DOS program fdformat/88 V1.8 by
56  * Christoph H. Hochst\"atter.
57  * I have fixed the shift values to the ones I always use. Maybe a new
58  * ioctl() should be created to be able to modify them.
59  * There is a bug in the driver that makes it impossible to format a
60  * floppy as the first thing after bootup.
61  */
62
63 /*
64  * 1993/4/29 -- Linus -- cleaned up the timer handling in the kernel, and
65  * this helped the floppy driver as well. Much cleaner, and still seems to
66  * work.
67  */
68
69 /* 1994/6/24 --bbroad-- added the floppy table entries and made
70  * minor modifications to allow 2.88 floppies to be run.
71  */
72
73 /* 1994/7/13 -- Paul Vojta -- modified the probing code to allow three or more
74  * disk types.
75  */
76
77 /*
78  * 1994/8/8 -- Alain Knaff -- Switched to fdpatch driver: Support for bigger
79  * format bug fixes, but unfortunately some new bugs too...
80  */
81
82 /* 1994/9/17 -- Koen Holtman -- added logging of physical floppy write
83  * errors to allow safe writing by specialized programs.
84  */
85
86 /* 1995/4/24 -- Dan Fandrich -- added support for Commodore 1581 3.5" disks
87  * by defining bit 1 of the "stretch" parameter to mean put sectors on the
88  * opposite side of the disk, leaving the sector IDs alone (i.e. Commodore's
89  * drives are "upside-down").
90  */
91
92 /*
93  * 1995/8/26 -- Andreas Busse -- added Mips support.
94  */
95
96 /*
97  * 1995/10/18 -- Ralf Baechle -- Portability cleanup; move machine dependent
98  * features to asm/floppy.h.
99  */
100
101 /*
102  * 1998/1/21 -- Richard Gooch <rgooch@atnf.csiro.au> -- devfs support
103  */
104
105 /*
106  * 1998/05/07 -- Russell King -- More portability cleanups; moved definition of
107  * interrupt and dma channel to asm/floppy.h. Cleaned up some formatting &
108  * use of '0' for NULL.
109  */
110
111 /*
112  * 1998/06/07 -- Alan Cox -- Merged the 2.0.34 fixes for resource allocation
113  * failures.
114  */
115
116 /*
117  * 1998/09/20 -- David Weinehall -- Added slow-down code for buggy PS/2-drives.
118  */
119
120 /*
121  * 1999/08/13 -- Paul Slootman -- floppy stopped working on Alpha after 24
122  * days, 6 hours, 32 minutes and 32 seconds (i.e. MAXINT jiffies; ints were
123  * being used to store jiffies, which are unsigned longs).
124  */
125
126 /*
127  * 2000/08/28 -- Arnaldo Carvalho de Melo <acme@conectiva.com.br>
128  * - get rid of check_region
129  * - s/suser/capable/
130  */
131
132 /*
133  * 2001/08/26 -- Paul Gortmaker - fix insmod oops on machines with no
134  * floppy controller (lingering task on list after module is gone... boom.)
135  */
136
137 /*
138  * 2002/02/07 -- Anton Altaparmakov - Fix io ports reservation to correct range
139  * (0x3f2-0x3f5, 0x3f7). This fix is a bit of a hack but the proper fix
140  * requires many non-obvious changes in arch dependent code.
141  */
142
143 /* 2003/07/28 -- Daniele Bellucci <bellucda@tiscali.it>.
144  * Better audit of register_blkdev.
145  */
146
147 #undef  FLOPPY_SILENT_DCL_CLEAR
148
149 #define REALLY_SLOW_IO
150
151 #define DEBUGT 2
152
153 #define DPRINT(format, args...) \
154         pr_info("floppy%d: " format, current_drive, ##args)
155
156 #define DCL_DEBUG               /* debug disk change line */
157 #ifdef DCL_DEBUG
158 #define debug_dcl(test, fmt, args...) \
159         do { if ((test) & FD_DEBUG) DPRINT(fmt, ##args); } while (0)
160 #else
161 #define debug_dcl(test, fmt, args...) \
162         do { if (0) DPRINT(fmt, ##args); } while (0)
163 #endif
164
165 /* do print messages for unexpected interrupts */
166 static int print_unex = 1;
167 #include <linux/module.h>
168 #include <linux/sched.h>
169 #include <linux/fs.h>
170 #include <linux/kernel.h>
171 #include <linux/timer.h>
172 #include <linux/workqueue.h>
173 #define FDPATCHES
174 #include <linux/fdreg.h>
175 #include <linux/fd.h>
176 #include <linux/hdreg.h>
177 #include <linux/errno.h>
178 #include <linux/slab.h>
179 #include <linux/mm.h>
180 #include <linux/bio.h>
181 #include <linux/string.h>
182 #include <linux/jiffies.h>
183 #include <linux/fcntl.h>
184 #include <linux/delay.h>
185 #include <linux/mc146818rtc.h>  /* CMOS defines */
186 #include <linux/ioport.h>
187 #include <linux/interrupt.h>
188 #include <linux/init.h>
189 #include <linux/platform_device.h>
190 #include <linux/mod_devicetable.h>
191 #include <linux/buffer_head.h>  /* for invalidate_buffers() */
192 #include <linux/mutex.h>
193 #include <linux/io.h>
194 #include <linux/uaccess.h>
195
196 /*
197  * PS/2 floppies have much slower step rates than regular floppies.
198  * It's been recommended that take about 1/4 of the default speed
199  * in some more extreme cases.
200  */
201 static DEFINE_MUTEX(floppy_mutex);
202 static int slow_floppy;
203
204 #include <asm/dma.h>
205 #include <asm/irq.h>
206 #include <asm/system.h>
207
208 static int FLOPPY_IRQ = 6;
209 static int FLOPPY_DMA = 2;
210 static int can_use_virtual_dma = 2;
211 /* =======
212  * can use virtual DMA:
213  * 0 = use of virtual DMA disallowed by config
214  * 1 = use of virtual DMA prescribed by config
215  * 2 = no virtual DMA preference configured.  By default try hard DMA,
216  * but fall back on virtual DMA when not enough memory available
217  */
218
219 static int use_virtual_dma;
220 /* =======
221  * use virtual DMA
222  * 0 using hard DMA
223  * 1 using virtual DMA
224  * This variable is set to virtual when a DMA mem problem arises, and
225  * reset back in floppy_grab_irq_and_dma.
226  * It is not safe to reset it in other circumstances, because the floppy
227  * driver may have several buffers in use at once, and we do currently not
228  * record each buffers capabilities
229  */
230
231 static DEFINE_SPINLOCK(floppy_lock);
232
233 static unsigned short virtual_dma_port = 0x3f0;
234 irqreturn_t floppy_interrupt(int irq, void *dev_id);
235 static int set_dor(int fdc, char mask, char data);
236
237 #define K_64    0x10000         /* 64KB */
238
239 /* the following is the mask of allowed drives. By default units 2 and
240  * 3 of both floppy controllers are disabled, because switching on the
241  * motor of these drives causes system hangs on some PCI computers. drive
242  * 0 is the low bit (0x1), and drive 7 is the high bit (0x80). Bits are on if
243  * a drive is allowed.
244  *
245  * NOTE: This must come before we include the arch floppy header because
246  *       some ports reference this variable from there. -DaveM
247  */
248
249 static int allowed_drive_mask = 0x33;
250
251 #include <asm/floppy.h>
252
253 static int irqdma_allocated;
254
255 #include <linux/blkdev.h>
256 #include <linux/blkpg.h>
257 #include <linux/cdrom.h>        /* for the compatibility eject ioctl */
258 #include <linux/completion.h>
259
260 static struct request *current_req;
261 static void do_fd_request(struct request_queue *q);
262 static int set_next_request(void);
263
264 #ifndef fd_get_dma_residue
265 #define fd_get_dma_residue() get_dma_residue(FLOPPY_DMA)
266 #endif
267
268 /* Dma Memory related stuff */
269
270 #ifndef fd_dma_mem_free
271 #define fd_dma_mem_free(addr, size) free_pages(addr, get_order(size))
272 #endif
273
274 #ifndef fd_dma_mem_alloc
275 #define fd_dma_mem_alloc(size) __get_dma_pages(GFP_KERNEL, get_order(size))
276 #endif
277
278 static inline void fallback_on_nodma_alloc(char **addr, size_t l)
279 {
280 #ifdef FLOPPY_CAN_FALLBACK_ON_NODMA
281         if (*addr)
282                 return;         /* we have the memory */
283         if (can_use_virtual_dma != 2)
284                 return;         /* no fallback allowed */
285         pr_info("DMA memory shortage. Temporarily falling back on virtual DMA\n");
286         *addr = (char *)nodma_mem_alloc(l);
287 #else
288         return;
289 #endif
290 }
291
292 /* End dma memory related stuff */
293
294 static unsigned long fake_change;
295 static bool initialized;
296
297 #define ITYPE(x)        (((x) >> 2) & 0x1f)
298 #define TOMINOR(x)      ((x & 3) | ((x & 4) << 5))
299 #define UNIT(x)         ((x) & 0x03)            /* drive on fdc */
300 #define FDC(x)          (((x) & 0x04) >> 2)     /* fdc of drive */
301         /* reverse mapping from unit and fdc to drive */
302 #define REVDRIVE(fdc, unit) ((unit) + ((fdc) << 2))
303
304 #define DP      (&drive_params[current_drive])
305 #define DRS     (&drive_state[current_drive])
306 #define DRWE    (&write_errors[current_drive])
307 #define FDCS    (&fdc_state[fdc])
308
309 #define UDP     (&drive_params[drive])
310 #define UDRS    (&drive_state[drive])
311 #define UDRWE   (&write_errors[drive])
312 #define UFDCS   (&fdc_state[FDC(drive)])
313
314 #define PH_HEAD(floppy, head) (((((floppy)->stretch & 2) >> 1) ^ head) << 2)
315 #define STRETCH(floppy) ((floppy)->stretch & FD_STRETCH)
316
317 /* read/write */
318 #define COMMAND         (raw_cmd->cmd[0])
319 #define DR_SELECT       (raw_cmd->cmd[1])
320 #define TRACK           (raw_cmd->cmd[2])
321 #define HEAD            (raw_cmd->cmd[3])
322 #define SECTOR          (raw_cmd->cmd[4])
323 #define SIZECODE        (raw_cmd->cmd[5])
324 #define SECT_PER_TRACK  (raw_cmd->cmd[6])
325 #define GAP             (raw_cmd->cmd[7])
326 #define SIZECODE2       (raw_cmd->cmd[8])
327 #define NR_RW 9
328
329 /* format */
330 #define F_SIZECODE      (raw_cmd->cmd[2])
331 #define F_SECT_PER_TRACK (raw_cmd->cmd[3])
332 #define F_GAP           (raw_cmd->cmd[4])
333 #define F_FILL          (raw_cmd->cmd[5])
334 #define NR_F 6
335
336 /*
337  * Maximum disk size (in kilobytes).
338  * This default is used whenever the current disk size is unknown.
339  * [Now it is rather a minimum]
340  */
341 #define MAX_DISK_SIZE 4         /* 3984 */
342
343 /*
344  * globals used by 'result()'
345  */
346 #define MAX_REPLIES 16
347 static unsigned char reply_buffer[MAX_REPLIES];
348 static int inr;         /* size of reply buffer, when called from interrupt */
349 #define ST0             (reply_buffer[0])
350 #define ST1             (reply_buffer[1])
351 #define ST2             (reply_buffer[2])
352 #define ST3             (reply_buffer[0])       /* result of GETSTATUS */
353 #define R_TRACK         (reply_buffer[3])
354 #define R_HEAD          (reply_buffer[4])
355 #define R_SECTOR        (reply_buffer[5])
356 #define R_SIZECODE      (reply_buffer[6])
357
358 #define SEL_DLY         (2 * HZ / 100)
359
360 /*
361  * this struct defines the different floppy drive types.
362  */
363 static struct {
364         struct floppy_drive_params params;
365         const char *name;       /* name printed while booting */
366 } default_drive_params[] = {
367 /* NOTE: the time values in jiffies should be in msec!
368  CMOS drive type
369   |     Maximum data rate supported by drive type
370   |     |   Head load time, msec
371   |     |   |   Head unload time, msec (not used)
372   |     |   |   |     Step rate interval, usec
373   |     |   |   |     |       Time needed for spinup time (jiffies)
374   |     |   |   |     |       |      Timeout for spinning down (jiffies)
375   |     |   |   |     |       |      |   Spindown offset (where disk stops)
376   |     |   |   |     |       |      |   |     Select delay
377   |     |   |   |     |       |      |   |     |     RPS
378   |     |   |   |     |       |      |   |     |     |    Max number of tracks
379   |     |   |   |     |       |      |   |     |     |    |     Interrupt timeout
380   |     |   |   |     |       |      |   |     |     |    |     |   Max nonintlv. sectors
381   |     |   |   |     |       |      |   |     |     |    |     |   | -Max Errors- flags */
382 {{0,  500, 16, 16, 8000,    1*HZ, 3*HZ,  0, SEL_DLY, 5,  80, 3*HZ, 20, {3,1,2,0,2}, 0,
383       0, { 7, 4, 8, 2, 1, 5, 3,10}, 3*HZ/2, 0 }, "unknown" },
384
385 {{1,  300, 16, 16, 8000,    1*HZ, 3*HZ,  0, SEL_DLY, 5,  40, 3*HZ, 17, {3,1,2,0,2}, 0,
386       0, { 1, 0, 0, 0, 0, 0, 0, 0}, 3*HZ/2, 1 }, "360K PC" }, /*5 1/4 360 KB PC*/
387
388 {{2,  500, 16, 16, 6000, 4*HZ/10, 3*HZ, 14, SEL_DLY, 6,  83, 3*HZ, 17, {3,1,2,0,2}, 0,
389       0, { 2, 5, 6,23,10,20,12, 0}, 3*HZ/2, 2 }, "1.2M" }, /*5 1/4 HD AT*/
390
391 {{3,  250, 16, 16, 3000,    1*HZ, 3*HZ,  0, SEL_DLY, 5,  83, 3*HZ, 20, {3,1,2,0,2}, 0,
392       0, { 4,22,21,30, 3, 0, 0, 0}, 3*HZ/2, 4 }, "720k" }, /*3 1/2 DD*/
393
394 {{4,  500, 16, 16, 4000, 4*HZ/10, 3*HZ, 10, SEL_DLY, 5,  83, 3*HZ, 20, {3,1,2,0,2}, 0,
395       0, { 7, 4,25,22,31,21,29,11}, 3*HZ/2, 7 }, "1.44M" }, /*3 1/2 HD*/
396
397 {{5, 1000, 15,  8, 3000, 4*HZ/10, 3*HZ, 10, SEL_DLY, 5,  83, 3*HZ, 40, {3,1,2,0,2}, 0,
398       0, { 7, 8, 4,25,28,22,31,21}, 3*HZ/2, 8 }, "2.88M AMI BIOS" }, /*3 1/2 ED*/
399
400 {{6, 1000, 15,  8, 3000, 4*HZ/10, 3*HZ, 10, SEL_DLY, 5,  83, 3*HZ, 40, {3,1,2,0,2}, 0,
401       0, { 7, 8, 4,25,28,22,31,21}, 3*HZ/2, 8 }, "2.88M" } /*3 1/2 ED*/
402 /*    |  --autodetected formats---    |      |      |
403  *    read_track                      |      |    Name printed when booting
404  *                                    |     Native format
405  *                  Frequency of disk change checks */
406 };
407
408 static struct floppy_drive_params drive_params[N_DRIVE];
409 static struct floppy_drive_struct drive_state[N_DRIVE];
410 static struct floppy_write_errors write_errors[N_DRIVE];
411 static struct timer_list motor_off_timer[N_DRIVE];
412 static struct gendisk *disks[N_DRIVE];
413 static struct block_device *opened_bdev[N_DRIVE];
414 static DEFINE_MUTEX(open_lock);
415 static struct floppy_raw_cmd *raw_cmd, default_raw_cmd;
416 static int fdc_queue;
417
418 /*
419  * This struct defines the different floppy types.
420  *
421  * Bit 0 of 'stretch' tells if the tracks need to be doubled for some
422  * types (e.g. 360kB diskette in 1.2MB drive, etc.).  Bit 1 of 'stretch'
423  * tells if the disk is in Commodore 1581 format, which means side 0 sectors
424  * are located on side 1 of the disk but with a side 0 ID, and vice-versa.
425  * This is the same as the Sharp MZ-80 5.25" CP/M disk format, except that the
426  * 1581's logical side 0 is on physical side 1, whereas the Sharp's logical
427  * side 0 is on physical side 0 (but with the misnamed sector IDs).
428  * 'stretch' should probably be renamed to something more general, like
429  * 'options'.
430  *
431  * Bits 2 through 9 of 'stretch' tell the number of the first sector.
432  * The LSB (bit 2) is flipped. For most disks, the first sector
433  * is 1 (represented by 0x00<<2).  For some CP/M and music sampler
434  * disks (such as Ensoniq EPS 16plus) it is 0 (represented as 0x01<<2).
435  * For Amstrad CPC disks it is 0xC1 (represented as 0xC0<<2).
436  *
437  * Other parameters should be self-explanatory (see also setfdprm(8)).
438  */
439 /*
440             Size
441              |  Sectors per track
442              |  | Head
443              |  | |  Tracks
444              |  | |  | Stretch
445              |  | |  | |  Gap 1 size
446              |  | |  | |    |  Data rate, | 0x40 for perp
447              |  | |  | |    |    |  Spec1 (stepping rate, head unload
448              |  | |  | |    |    |    |    /fmt gap (gap2) */
449 static struct floppy_struct floppy_type[32] = {
450         {    0, 0,0, 0,0,0x00,0x00,0x00,0x00,NULL    }, /*  0 no testing    */
451         {  720, 9,2,40,0,0x2A,0x02,0xDF,0x50,"d360"  }, /*  1 360KB PC      */
452         { 2400,15,2,80,0,0x1B,0x00,0xDF,0x54,"h1200" }, /*  2 1.2MB AT      */
453         {  720, 9,1,80,0,0x2A,0x02,0xDF,0x50,"D360"  }, /*  3 360KB SS 3.5" */
454         { 1440, 9,2,80,0,0x2A,0x02,0xDF,0x50,"D720"  }, /*  4 720KB 3.5"    */
455         {  720, 9,2,40,1,0x23,0x01,0xDF,0x50,"h360"  }, /*  5 360KB AT      */
456         { 1440, 9,2,80,0,0x23,0x01,0xDF,0x50,"h720"  }, /*  6 720KB AT      */
457         { 2880,18,2,80,0,0x1B,0x00,0xCF,0x6C,"H1440" }, /*  7 1.44MB 3.5"   */
458         { 5760,36,2,80,0,0x1B,0x43,0xAF,0x54,"E2880" }, /*  8 2.88MB 3.5"   */
459         { 6240,39,2,80,0,0x1B,0x43,0xAF,0x28,"E3120" }, /*  9 3.12MB 3.5"   */
460
461         { 2880,18,2,80,0,0x25,0x00,0xDF,0x02,"h1440" }, /* 10 1.44MB 5.25"  */
462         { 3360,21,2,80,0,0x1C,0x00,0xCF,0x0C,"H1680" }, /* 11 1.68MB 3.5"   */
463         {  820,10,2,41,1,0x25,0x01,0xDF,0x2E,"h410"  }, /* 12 410KB 5.25"   */
464         { 1640,10,2,82,0,0x25,0x02,0xDF,0x2E,"H820"  }, /* 13 820KB 3.5"    */
465         { 2952,18,2,82,0,0x25,0x00,0xDF,0x02,"h1476" }, /* 14 1.48MB 5.25"  */
466         { 3444,21,2,82,0,0x25,0x00,0xDF,0x0C,"H1722" }, /* 15 1.72MB 3.5"   */
467         {  840,10,2,42,1,0x25,0x01,0xDF,0x2E,"h420"  }, /* 16 420KB 5.25"   */
468         { 1660,10,2,83,0,0x25,0x02,0xDF,0x2E,"H830"  }, /* 17 830KB 3.5"    */
469         { 2988,18,2,83,0,0x25,0x00,0xDF,0x02,"h1494" }, /* 18 1.49MB 5.25"  */
470         { 3486,21,2,83,0,0x25,0x00,0xDF,0x0C,"H1743" }, /* 19 1.74 MB 3.5"  */
471
472         { 1760,11,2,80,0,0x1C,0x09,0xCF,0x00,"h880"  }, /* 20 880KB 5.25"   */
473         { 2080,13,2,80,0,0x1C,0x01,0xCF,0x00,"D1040" }, /* 21 1.04MB 3.5"   */
474         { 2240,14,2,80,0,0x1C,0x19,0xCF,0x00,"D1120" }, /* 22 1.12MB 3.5"   */
475         { 3200,20,2,80,0,0x1C,0x20,0xCF,0x2C,"h1600" }, /* 23 1.6MB 5.25"   */
476         { 3520,22,2,80,0,0x1C,0x08,0xCF,0x2e,"H1760" }, /* 24 1.76MB 3.5"   */
477         { 3840,24,2,80,0,0x1C,0x20,0xCF,0x00,"H1920" }, /* 25 1.92MB 3.5"   */
478         { 6400,40,2,80,0,0x25,0x5B,0xCF,0x00,"E3200" }, /* 26 3.20MB 3.5"   */
479         { 7040,44,2,80,0,0x25,0x5B,0xCF,0x00,"E3520" }, /* 27 3.52MB 3.5"   */
480         { 7680,48,2,80,0,0x25,0x63,0xCF,0x00,"E3840" }, /* 28 3.84MB 3.5"   */
481         { 3680,23,2,80,0,0x1C,0x10,0xCF,0x00,"H1840" }, /* 29 1.84MB 3.5"   */
482
483         { 1600,10,2,80,0,0x25,0x02,0xDF,0x2E,"D800"  }, /* 30 800KB 3.5"    */
484         { 3200,20,2,80,0,0x1C,0x00,0xCF,0x2C,"H1600" }, /* 31 1.6MB 3.5"    */
485 };
486
487 #define SECTSIZE (_FD_SECTSIZE(*floppy))
488
489 /* Auto-detection: Disk type used until the next media change occurs. */
490 static struct floppy_struct *current_type[N_DRIVE];
491
492 /*
493  * User-provided type information. current_type points to
494  * the respective entry of this array.
495  */
496 static struct floppy_struct user_params[N_DRIVE];
497
498 static sector_t floppy_sizes[256];
499
500 static char floppy_device_name[] = "floppy";
501
502 /*
503  * The driver is trying to determine the correct media format
504  * while probing is set. rw_interrupt() clears it after a
505  * successful access.
506  */
507 static int probing;
508
509 /* Synchronization of FDC access. */
510 #define FD_COMMAND_NONE         -1
511 #define FD_COMMAND_ERROR        2
512 #define FD_COMMAND_OKAY         3
513
514 static volatile int command_status = FD_COMMAND_NONE;
515 static unsigned long fdc_busy;
516 static DECLARE_WAIT_QUEUE_HEAD(fdc_wait);
517 static DECLARE_WAIT_QUEUE_HEAD(command_done);
518
519 /* Errors during formatting are counted here. */
520 static int format_errors;
521
522 /* Format request descriptor. */
523 static struct format_descr format_req;
524
525 /*
526  * Rate is 0 for 500kb/s, 1 for 300kbps, 2 for 250kbps
527  * Spec1 is 0xSH, where S is stepping rate (F=1ms, E=2ms, D=3ms etc),
528  * H is head unload time (1=16ms, 2=32ms, etc)
529  */
530
531 /*
532  * Track buffer
533  * Because these are written to by the DMA controller, they must
534  * not contain a 64k byte boundary crossing, or data will be
535  * corrupted/lost.
536  */
537 static char *floppy_track_buffer;
538 static int max_buffer_sectors;
539
540 static int *errors;
541 typedef void (*done_f)(int);
542 static const struct cont_t {
543         void (*interrupt)(void);
544                                 /* this is called after the interrupt of the
545                                  * main command */
546         void (*redo)(void);     /* this is called to retry the operation */
547         void (*error)(void);    /* this is called to tally an error */
548         done_f done;            /* this is called to say if the operation has
549                                  * succeeded/failed */
550 } *cont;
551
552 static void floppy_ready(void);
553 static void floppy_start(void);
554 static void process_fd_request(void);
555 static void recalibrate_floppy(void);
556 static void floppy_shutdown(unsigned long);
557
558 static int floppy_request_regions(int);
559 static void floppy_release_regions(int);
560 static int floppy_grab_irq_and_dma(void);
561 static void floppy_release_irq_and_dma(void);
562
563 /*
564  * The "reset" variable should be tested whenever an interrupt is scheduled,
565  * after the commands have been sent. This is to ensure that the driver doesn't
566  * get wedged when the interrupt doesn't come because of a failed command.
567  * reset doesn't need to be tested before sending commands, because
568  * output_byte is automatically disabled when reset is set.
569  */
570 static void reset_fdc(void);
571
572 /*
573  * These are global variables, as that's the easiest way to give
574  * information to interrupts. They are the data used for the current
575  * request.
576  */
577 #define NO_TRACK        -1
578 #define NEED_1_RECAL    -2
579 #define NEED_2_RECAL    -3
580
581 static atomic_t usage_count = ATOMIC_INIT(0);
582
583 /* buffer related variables */
584 static int buffer_track = -1;
585 static int buffer_drive = -1;
586 static int buffer_min = -1;
587 static int buffer_max = -1;
588
589 /* fdc related variables, should end up in a struct */
590 static struct floppy_fdc_state fdc_state[N_FDC];
591 static int fdc;                 /* current fdc */
592
593 static struct floppy_struct *_floppy = floppy_type;
594 static unsigned char current_drive;
595 static long current_count_sectors;
596 static unsigned char fsector_t; /* sector in track */
597 static unsigned char in_sector_offset;  /* offset within physical sector,
598                                          * expressed in units of 512 bytes */
599
600 static inline bool drive_no_geom(int drive)
601 {
602         return !current_type[drive] && !ITYPE(UDRS->fd_device);
603 }
604
605 #ifndef fd_eject
606 static inline int fd_eject(int drive)
607 {
608         return -EINVAL;
609 }
610 #endif
611
612 /*
613  * Debugging
614  * =========
615  */
616 #ifdef DEBUGT
617 static long unsigned debugtimer;
618
619 static inline void set_debugt(void)
620 {
621         debugtimer = jiffies;
622 }
623
624 static inline void debugt(const char *func, const char *msg)
625 {
626         if (DP->flags & DEBUGT)
627                 pr_info("%s:%s dtime=%lu\n", func, msg, jiffies - debugtimer);
628 }
629 #else
630 static inline void set_debugt(void) { }
631 static inline void debugt(const char *func, const char *msg) { }
632 #endif /* DEBUGT */
633
634 typedef void (*timeout_fn)(unsigned long);
635 static DEFINE_TIMER(fd_timeout, floppy_shutdown, 0, 0);
636
637 static const char *timeout_message;
638
639 static void is_alive(const char *func, const char *message)
640 {
641         /* this routine checks whether the floppy driver is "alive" */
642         if (test_bit(0, &fdc_busy) && command_status < 2 &&
643             !timer_pending(&fd_timeout)) {
644                 DPRINT("%s: timeout handler died.  %s\n", func, message);
645         }
646 }
647
648 static void (*do_floppy)(void) = NULL;
649
650 #define OLOGSIZE 20
651
652 static void (*lasthandler)(void);
653 static unsigned long interruptjiffies;
654 static unsigned long resultjiffies;
655 static int resultsize;
656 static unsigned long lastredo;
657
658 static struct output_log {
659         unsigned char data;
660         unsigned char status;
661         unsigned long jiffies;
662 } output_log[OLOGSIZE];
663
664 static int output_log_pos;
665
666 #define current_reqD -1
667 #define MAXTIMEOUT -2
668
669 static void __reschedule_timeout(int drive, const char *message)
670 {
671         if (drive == current_reqD)
672                 drive = current_drive;
673         del_timer(&fd_timeout);
674         if (drive < 0 || drive >= N_DRIVE) {
675                 fd_timeout.expires = jiffies + 20UL * HZ;
676                 drive = 0;
677         } else
678                 fd_timeout.expires = jiffies + UDP->timeout;
679         add_timer(&fd_timeout);
680         if (UDP->flags & FD_DEBUG)
681                 DPRINT("reschedule timeout %s\n", message);
682         timeout_message = message;
683 }
684
685 static void reschedule_timeout(int drive, const char *message)
686 {
687         unsigned long flags;
688
689         spin_lock_irqsave(&floppy_lock, flags);
690         __reschedule_timeout(drive, message);
691         spin_unlock_irqrestore(&floppy_lock, flags);
692 }
693
694 #define INFBOUND(a, b) (a) = max_t(int, a, b)
695 #define SUPBOUND(a, b) (a) = min_t(int, a, b)
696
697 /*
698  * Bottom half floppy driver.
699  * ==========================
700  *
701  * This part of the file contains the code talking directly to the hardware,
702  * and also the main service loop (seek-configure-spinup-command)
703  */
704
705 /*
706  * disk change.
707  * This routine is responsible for maintaining the FD_DISK_CHANGE flag,
708  * and the last_checked date.
709  *
710  * last_checked is the date of the last check which showed 'no disk change'
711  * FD_DISK_CHANGE is set under two conditions:
712  * 1. The floppy has been changed after some i/o to that floppy already
713  *    took place.
714  * 2. No floppy disk is in the drive. This is done in order to ensure that
715  *    requests are quickly flushed in case there is no disk in the drive. It
716  *    follows that FD_DISK_CHANGE can only be cleared if there is a disk in
717  *    the drive.
718  *
719  * For 1., maxblock is observed. Maxblock is 0 if no i/o has taken place yet.
720  * For 2., FD_DISK_NEWCHANGE is watched. FD_DISK_NEWCHANGE is cleared on
721  *  each seek. If a disk is present, the disk change line should also be
722  *  cleared on each seek. Thus, if FD_DISK_NEWCHANGE is clear, but the disk
723  *  change line is set, this means either that no disk is in the drive, or
724  *  that it has been removed since the last seek.
725  *
726  * This means that we really have a third possibility too:
727  *  The floppy has been changed after the last seek.
728  */
729
730 static int disk_change(int drive)
731 {
732         int fdc = FDC(drive);
733
734         if (time_before(jiffies, UDRS->select_date + UDP->select_delay))
735                 DPRINT("WARNING disk change called early\n");
736         if (!(FDCS->dor & (0x10 << UNIT(drive))) ||
737             (FDCS->dor & 3) != UNIT(drive) || fdc != FDC(drive)) {
738                 DPRINT("probing disk change on unselected drive\n");
739                 DPRINT("drive=%d fdc=%d dor=%x\n", drive, FDC(drive),
740                        (unsigned int)FDCS->dor);
741         }
742
743         debug_dcl(UDP->flags,
744                   "checking disk change line for drive %d\n", drive);
745         debug_dcl(UDP->flags, "jiffies=%lu\n", jiffies);
746         debug_dcl(UDP->flags, "disk change line=%x\n", fd_inb(FD_DIR) & 0x80);
747         debug_dcl(UDP->flags, "flags=%lx\n", UDRS->flags);
748
749         if (UDP->flags & FD_BROKEN_DCL)
750                 return test_bit(FD_DISK_CHANGED_BIT, &UDRS->flags);
751         if ((fd_inb(FD_DIR) ^ UDP->flags) & 0x80) {
752                 set_bit(FD_VERIFY_BIT, &UDRS->flags);
753                                         /* verify write protection */
754
755                 if (UDRS->maxblock)     /* mark it changed */
756                         set_bit(FD_DISK_CHANGED_BIT, &UDRS->flags);
757
758                 /* invalidate its geometry */
759                 if (UDRS->keep_data >= 0) {
760                         if ((UDP->flags & FTD_MSG) &&
761                             current_type[drive] != NULL)
762                                 DPRINT("Disk type is undefined after disk change\n");
763                         current_type[drive] = NULL;
764                         floppy_sizes[TOMINOR(drive)] = MAX_DISK_SIZE << 1;
765                 }
766
767                 return 1;
768         } else {
769                 UDRS->last_checked = jiffies;
770                 clear_bit(FD_DISK_NEWCHANGE_BIT, &UDRS->flags);
771         }
772         return 0;
773 }
774
775 static inline int is_selected(int dor, int unit)
776 {
777         return ((dor & (0x10 << unit)) && (dor & 3) == unit);
778 }
779
780 static bool is_ready_state(int status)
781 {
782         int state = status & (STATUS_READY | STATUS_DIR | STATUS_DMA);
783         return state == STATUS_READY;
784 }
785
786 static int set_dor(int fdc, char mask, char data)
787 {
788         unsigned char unit;
789         unsigned char drive;
790         unsigned char newdor;
791         unsigned char olddor;
792
793         if (FDCS->address == -1)
794                 return -1;
795
796         olddor = FDCS->dor;
797         newdor = (olddor & mask) | data;
798         if (newdor != olddor) {
799                 unit = olddor & 0x3;
800                 if (is_selected(olddor, unit) && !is_selected(newdor, unit)) {
801                         drive = REVDRIVE(fdc, unit);
802                         debug_dcl(UDP->flags,
803                                   "calling disk change from set_dor\n");
804                         disk_change(drive);
805                 }
806                 FDCS->dor = newdor;
807                 fd_outb(newdor, FD_DOR);
808
809                 unit = newdor & 0x3;
810                 if (!is_selected(olddor, unit) && is_selected(newdor, unit)) {
811                         drive = REVDRIVE(fdc, unit);
812                         UDRS->select_date = jiffies;
813                 }
814         }
815         return olddor;
816 }
817
818 static void twaddle(void)
819 {
820         if (DP->select_delay)
821                 return;
822         fd_outb(FDCS->dor & ~(0x10 << UNIT(current_drive)), FD_DOR);
823         fd_outb(FDCS->dor, FD_DOR);
824         DRS->select_date = jiffies;
825 }
826
827 /*
828  * Reset all driver information about the current fdc.
829  * This is needed after a reset, and after a raw command.
830  */
831 static void reset_fdc_info(int mode)
832 {
833         int drive;
834
835         FDCS->spec1 = FDCS->spec2 = -1;
836         FDCS->need_configure = 1;
837         FDCS->perp_mode = 1;
838         FDCS->rawcmd = 0;
839         for (drive = 0; drive < N_DRIVE; drive++)
840                 if (FDC(drive) == fdc && (mode || UDRS->track != NEED_1_RECAL))
841                         UDRS->track = NEED_2_RECAL;
842 }
843
844 /* selects the fdc and drive, and enables the fdc's input/dma. */
845 static void set_fdc(int drive)
846 {
847         if (drive >= 0 && drive < N_DRIVE) {
848                 fdc = FDC(drive);
849                 current_drive = drive;
850         }
851         if (fdc != 1 && fdc != 0) {
852                 pr_info("bad fdc value\n");
853                 return;
854         }
855         set_dor(fdc, ~0, 8);
856 #if N_FDC > 1
857         set_dor(1 - fdc, ~8, 0);
858 #endif
859         if (FDCS->rawcmd == 2)
860                 reset_fdc_info(1);
861         if (fd_inb(FD_STATUS) != STATUS_READY)
862                 FDCS->reset = 1;
863 }
864
865 /* locks the driver */
866 static int lock_fdc(int drive, bool interruptible)
867 {
868         if (WARN(atomic_read(&usage_count) == 0,
869                  "Trying to lock fdc while usage count=0\n"))
870                 return -1;
871
872         if (wait_event_interruptible(fdc_wait, !test_and_set_bit(0, &fdc_busy)))
873                 return -EINTR;
874
875         command_status = FD_COMMAND_NONE;
876
877         __reschedule_timeout(drive, "lock fdc");
878         set_fdc(drive);
879         return 0;
880 }
881
882 /* unlocks the driver */
883 static void unlock_fdc(void)
884 {
885         unsigned long flags;
886
887         raw_cmd = NULL;
888         if (!test_bit(0, &fdc_busy))
889                 DPRINT("FDC access conflict!\n");
890
891         if (do_floppy)
892                 DPRINT("device interrupt still active at FDC release: %pf!\n",
893                        do_floppy);
894         command_status = FD_COMMAND_NONE;
895         spin_lock_irqsave(&floppy_lock, flags);
896         del_timer(&fd_timeout);
897         cont = NULL;
898         clear_bit(0, &fdc_busy);
899         if (current_req || set_next_request())
900                 do_fd_request(current_req->q);
901         spin_unlock_irqrestore(&floppy_lock, flags);
902         wake_up(&fdc_wait);
903 }
904
905 /* switches the motor off after a given timeout */
906 static void motor_off_callback(unsigned long nr)
907 {
908         unsigned char mask = ~(0x10 << UNIT(nr));
909
910         set_dor(FDC(nr), mask, 0);
911 }
912
913 /* schedules motor off */
914 static void floppy_off(unsigned int drive)
915 {
916         unsigned long volatile delta;
917         int fdc = FDC(drive);
918
919         if (!(FDCS->dor & (0x10 << UNIT(drive))))
920                 return;
921
922         del_timer(motor_off_timer + drive);
923
924         /* make spindle stop in a position which minimizes spinup time
925          * next time */
926         if (UDP->rps) {
927                 delta = jiffies - UDRS->first_read_date + HZ -
928                     UDP->spindown_offset;
929                 delta = ((delta * UDP->rps) % HZ) / UDP->rps;
930                 motor_off_timer[drive].expires =
931                     jiffies + UDP->spindown - delta;
932         }
933         add_timer(motor_off_timer + drive);
934 }
935
936 /*
937  * cycle through all N_DRIVE floppy drives, for disk change testing.
938  * stopping at current drive. This is done before any long operation, to
939  * be sure to have up to date disk change information.
940  */
941 static void scandrives(void)
942 {
943         int i;
944         int drive;
945         int saved_drive;
946
947         if (DP->select_delay)
948                 return;
949
950         saved_drive = current_drive;
951         for (i = 0; i < N_DRIVE; i++) {
952                 drive = (saved_drive + i + 1) % N_DRIVE;
953                 if (UDRS->fd_ref == 0 || UDP->select_delay != 0)
954                         continue;       /* skip closed drives */
955                 set_fdc(drive);
956                 if (!(set_dor(fdc, ~3, UNIT(drive) | (0x10 << UNIT(drive))) &
957                       (0x10 << UNIT(drive))))
958                         /* switch the motor off again, if it was off to
959                          * begin with */
960                         set_dor(fdc, ~(0x10 << UNIT(drive)), 0);
961         }
962         set_fdc(saved_drive);
963 }
964
965 static void empty(void)
966 {
967 }
968
969 static DECLARE_WORK(floppy_work, NULL);
970
971 static void schedule_bh(void (*handler)(void))
972 {
973         PREPARE_WORK(&floppy_work, (work_func_t)handler);
974         schedule_work(&floppy_work);
975 }
976
977 static DEFINE_TIMER(fd_timer, NULL, 0, 0);
978
979 static void cancel_activity(void)
980 {
981         unsigned long flags;
982
983         spin_lock_irqsave(&floppy_lock, flags);
984         do_floppy = NULL;
985         PREPARE_WORK(&floppy_work, (work_func_t)empty);
986         del_timer(&fd_timer);
987         spin_unlock_irqrestore(&floppy_lock, flags);
988 }
989
990 /* this function makes sure that the disk stays in the drive during the
991  * transfer */
992 static void fd_watchdog(void)
993 {
994         debug_dcl(DP->flags, "calling disk change from watchdog\n");
995
996         if (disk_change(current_drive)) {
997                 DPRINT("disk removed during i/o\n");
998                 cancel_activity();
999                 cont->done(0);
1000                 reset_fdc();
1001         } else {
1002                 del_timer(&fd_timer);
1003                 fd_timer.function = (timeout_fn)fd_watchdog;
1004                 fd_timer.expires = jiffies + HZ / 10;
1005                 add_timer(&fd_timer);
1006         }
1007 }
1008
1009 static void main_command_interrupt(void)
1010 {
1011         del_timer(&fd_timer);
1012         cont->interrupt();
1013 }
1014
1015 /* waits for a delay (spinup or select) to pass */
1016 static int fd_wait_for_completion(unsigned long delay, timeout_fn function)
1017 {
1018         if (FDCS->reset) {
1019                 reset_fdc();    /* do the reset during sleep to win time
1020                                  * if we don't need to sleep, it's a good
1021                                  * occasion anyways */
1022                 return 1;
1023         }
1024
1025         if (time_before(jiffies, delay)) {
1026                 del_timer(&fd_timer);
1027                 fd_timer.function = function;
1028                 fd_timer.expires = delay;
1029                 add_timer(&fd_timer);
1030                 return 1;
1031         }
1032         return 0;
1033 }
1034
1035 static void setup_DMA(void)
1036 {
1037         unsigned long f;
1038
1039         if (raw_cmd->length == 0) {
1040                 int i;
1041
1042                 pr_info("zero dma transfer size:");
1043                 for (i = 0; i < raw_cmd->cmd_count; i++)
1044                         pr_cont("%x,", raw_cmd->cmd[i]);
1045                 pr_cont("\n");
1046                 cont->done(0);
1047                 FDCS->reset = 1;
1048                 return;
1049         }
1050         if (((unsigned long)raw_cmd->kernel_data) % 512) {
1051                 pr_info("non aligned address: %p\n", raw_cmd->kernel_data);
1052                 cont->done(0);
1053                 FDCS->reset = 1;
1054                 return;
1055         }
1056         f = claim_dma_lock();
1057         fd_disable_dma();
1058 #ifdef fd_dma_setup
1059         if (fd_dma_setup(raw_cmd->kernel_data, raw_cmd->length,
1060                          (raw_cmd->flags & FD_RAW_READ) ?
1061                          DMA_MODE_READ : DMA_MODE_WRITE, FDCS->address) < 0) {
1062                 release_dma_lock(f);
1063                 cont->done(0);
1064                 FDCS->reset = 1;
1065                 return;
1066         }
1067         release_dma_lock(f);
1068 #else
1069         fd_clear_dma_ff();
1070         fd_cacheflush(raw_cmd->kernel_data, raw_cmd->length);
1071         fd_set_dma_mode((raw_cmd->flags & FD_RAW_READ) ?
1072                         DMA_MODE_READ : DMA_MODE_WRITE);
1073         fd_set_dma_addr(raw_cmd->kernel_data);
1074         fd_set_dma_count(raw_cmd->length);
1075         virtual_dma_port = FDCS->address;
1076         fd_enable_dma();
1077         release_dma_lock(f);
1078 #endif
1079 }
1080
1081 static void show_floppy(void);
1082
1083 /* waits until the fdc becomes ready */
1084 static int wait_til_ready(void)
1085 {
1086         int status;
1087         int counter;
1088
1089         if (FDCS->reset)
1090                 return -1;
1091         for (counter = 0; counter < 10000; counter++) {
1092                 status = fd_inb(FD_STATUS);
1093                 if (status & STATUS_READY)
1094                         return status;
1095         }
1096         if (initialized) {
1097                 DPRINT("Getstatus times out (%x) on fdc %d\n", status, fdc);
1098                 show_floppy();
1099         }
1100         FDCS->reset = 1;
1101         return -1;
1102 }
1103
1104 /* sends a command byte to the fdc */
1105 static int output_byte(char byte)
1106 {
1107         int status = wait_til_ready();
1108
1109         if (status < 0)
1110                 return -1;
1111
1112         if (is_ready_state(status)) {
1113                 fd_outb(byte, FD_DATA);
1114                 output_log[output_log_pos].data = byte;
1115                 output_log[output_log_pos].status = status;
1116                 output_log[output_log_pos].jiffies = jiffies;
1117                 output_log_pos = (output_log_pos + 1) % OLOGSIZE;
1118                 return 0;
1119         }
1120         FDCS->reset = 1;
1121         if (initialized) {
1122                 DPRINT("Unable to send byte %x to FDC. Fdc=%x Status=%x\n",
1123                        byte, fdc, status);
1124                 show_floppy();
1125         }
1126         return -1;
1127 }
1128
1129 /* gets the response from the fdc */
1130 static int result(void)
1131 {
1132         int i;
1133         int status = 0;
1134
1135         for (i = 0; i < MAX_REPLIES; i++) {
1136                 status = wait_til_ready();
1137                 if (status < 0)
1138                         break;
1139                 status &= STATUS_DIR | STATUS_READY | STATUS_BUSY | STATUS_DMA;
1140                 if ((status & ~STATUS_BUSY) == STATUS_READY) {
1141                         resultjiffies = jiffies;
1142                         resultsize = i;
1143                         return i;
1144                 }
1145                 if (status == (STATUS_DIR | STATUS_READY | STATUS_BUSY))
1146                         reply_buffer[i] = fd_inb(FD_DATA);
1147                 else
1148                         break;
1149         }
1150         if (initialized) {
1151                 DPRINT("get result error. Fdc=%d Last status=%x Read bytes=%d\n",
1152                        fdc, status, i);
1153                 show_floppy();
1154         }
1155         FDCS->reset = 1;
1156         return -1;
1157 }
1158
1159 #define MORE_OUTPUT -2
1160 /* does the fdc need more output? */
1161 static int need_more_output(void)
1162 {
1163         int status = wait_til_ready();
1164
1165         if (status < 0)
1166                 return -1;
1167
1168         if (is_ready_state(status))
1169                 return MORE_OUTPUT;
1170
1171         return result();
1172 }
1173
1174 /* Set perpendicular mode as required, based on data rate, if supported.
1175  * 82077 Now tested. 1Mbps data rate only possible with 82077-1.
1176  */
1177 static void perpendicular_mode(void)
1178 {
1179         unsigned char perp_mode;
1180
1181         if (raw_cmd->rate & 0x40) {
1182                 switch (raw_cmd->rate & 3) {
1183                 case 0:
1184                         perp_mode = 2;
1185                         break;
1186                 case 3:
1187                         perp_mode = 3;
1188                         break;
1189                 default:
1190                         DPRINT("Invalid data rate for perpendicular mode!\n");
1191                         cont->done(0);
1192                         FDCS->reset = 1;
1193                                         /*
1194                                          * convenient way to return to
1195                                          * redo without too much hassle
1196                                          * (deep stack et al.)
1197                                          */
1198                         return;
1199                 }
1200         } else
1201                 perp_mode = 0;
1202
1203         if (FDCS->perp_mode == perp_mode)
1204                 return;
1205         if (FDCS->version >= FDC_82077_ORIG) {
1206                 output_byte(FD_PERPENDICULAR);
1207                 output_byte(perp_mode);
1208                 FDCS->perp_mode = perp_mode;
1209         } else if (perp_mode) {
1210                 DPRINT("perpendicular mode not supported by this FDC.\n");
1211         }
1212 }                               /* perpendicular_mode */
1213
1214 static int fifo_depth = 0xa;
1215 static int no_fifo;
1216
1217 static int fdc_configure(void)
1218 {
1219         /* Turn on FIFO */
1220         output_byte(FD_CONFIGURE);
1221         if (need_more_output() != MORE_OUTPUT)
1222                 return 0;
1223         output_byte(0);
1224         output_byte(0x10 | (no_fifo & 0x20) | (fifo_depth & 0xf));
1225         output_byte(0);         /* pre-compensation from track
1226                                    0 upwards */
1227         return 1;
1228 }
1229
1230 #define NOMINAL_DTR 500
1231
1232 /* Issue a "SPECIFY" command to set the step rate time, head unload time,
1233  * head load time, and DMA disable flag to values needed by floppy.
1234  *
1235  * The value "dtr" is the data transfer rate in Kbps.  It is needed
1236  * to account for the data rate-based scaling done by the 82072 and 82077
1237  * FDC types.  This parameter is ignored for other types of FDCs (i.e.
1238  * 8272a).
1239  *
1240  * Note that changing the data transfer rate has a (probably deleterious)
1241  * effect on the parameters subject to scaling for 82072/82077 FDCs, so
1242  * fdc_specify is called again after each data transfer rate
1243  * change.
1244  *
1245  * srt: 1000 to 16000 in microseconds
1246  * hut: 16 to 240 milliseconds
1247  * hlt: 2 to 254 milliseconds
1248  *
1249  * These values are rounded up to the next highest available delay time.
1250  */
1251 static void fdc_specify(void)
1252 {
1253         unsigned char spec1;
1254         unsigned char spec2;
1255         unsigned long srt;
1256         unsigned long hlt;
1257         unsigned long hut;
1258         unsigned long dtr = NOMINAL_DTR;
1259         unsigned long scale_dtr = NOMINAL_DTR;
1260         int hlt_max_code = 0x7f;
1261         int hut_max_code = 0xf;
1262
1263         if (FDCS->need_configure && FDCS->version >= FDC_82072A) {
1264                 fdc_configure();
1265                 FDCS->need_configure = 0;
1266         }
1267
1268         switch (raw_cmd->rate & 0x03) {
1269         case 3:
1270                 dtr = 1000;
1271                 break;
1272         case 1:
1273                 dtr = 300;
1274                 if (FDCS->version >= FDC_82078) {
1275                         /* chose the default rate table, not the one
1276                          * where 1 = 2 Mbps */
1277                         output_byte(FD_DRIVESPEC);
1278                         if (need_more_output() == MORE_OUTPUT) {
1279                                 output_byte(UNIT(current_drive));
1280                                 output_byte(0xc0);
1281                         }
1282                 }
1283                 break;
1284         case 2:
1285                 dtr = 250;
1286                 break;
1287         }
1288
1289         if (FDCS->version >= FDC_82072) {
1290                 scale_dtr = dtr;
1291                 hlt_max_code = 0x00;    /* 0==256msec*dtr0/dtr (not linear!) */
1292                 hut_max_code = 0x0;     /* 0==256msec*dtr0/dtr (not linear!) */
1293         }
1294
1295         /* Convert step rate from microseconds to milliseconds and 4 bits */
1296         srt = 16 - DIV_ROUND_UP(DP->srt * scale_dtr / 1000, NOMINAL_DTR);
1297         if (slow_floppy)
1298                 srt = srt / 4;
1299
1300         SUPBOUND(srt, 0xf);
1301         INFBOUND(srt, 0);
1302
1303         hlt = DIV_ROUND_UP(DP->hlt * scale_dtr / 2, NOMINAL_DTR);
1304         if (hlt < 0x01)
1305                 hlt = 0x01;
1306         else if (hlt > 0x7f)
1307                 hlt = hlt_max_code;
1308
1309         hut = DIV_ROUND_UP(DP->hut * scale_dtr / 16, NOMINAL_DTR);
1310         if (hut < 0x1)
1311                 hut = 0x1;
1312         else if (hut > 0xf)
1313                 hut = hut_max_code;
1314
1315         spec1 = (srt << 4) | hut;
1316         spec2 = (hlt << 1) | (use_virtual_dma & 1);
1317
1318         /* If these parameters did not change, just return with success */
1319         if (FDCS->spec1 != spec1 || FDCS->spec2 != spec2) {
1320                 /* Go ahead and set spec1 and spec2 */
1321                 output_byte(FD_SPECIFY);
1322                 output_byte(FDCS->spec1 = spec1);
1323                 output_byte(FDCS->spec2 = spec2);
1324         }
1325 }                               /* fdc_specify */
1326
1327 /* Set the FDC's data transfer rate on behalf of the specified drive.
1328  * NOTE: with 82072/82077 FDCs, changing the data rate requires a reissue
1329  * of the specify command (i.e. using the fdc_specify function).
1330  */
1331 static int fdc_dtr(void)
1332 {
1333         /* If data rate not already set to desired value, set it. */
1334         if ((raw_cmd->rate & 3) == FDCS->dtr)
1335                 return 0;
1336
1337         /* Set dtr */
1338         fd_outb(raw_cmd->rate & 3, FD_DCR);
1339
1340         /* TODO: some FDC/drive combinations (C&T 82C711 with TEAC 1.2MB)
1341          * need a stabilization period of several milliseconds to be
1342          * enforced after data rate changes before R/W operations.
1343          * Pause 5 msec to avoid trouble. (Needs to be 2 jiffies)
1344          */
1345         FDCS->dtr = raw_cmd->rate & 3;
1346         return fd_wait_for_completion(jiffies + 2UL * HZ / 100,
1347                                       (timeout_fn)floppy_ready);
1348 }                               /* fdc_dtr */
1349
1350 static void tell_sector(void)
1351 {
1352         pr_cont(": track %d, head %d, sector %d, size %d",
1353                 R_TRACK, R_HEAD, R_SECTOR, R_SIZECODE);
1354 }                               /* tell_sector */
1355
1356 static void print_errors(void)
1357 {
1358         DPRINT("");
1359         if (ST0 & ST0_ECE) {
1360                 pr_cont("Recalibrate failed!");
1361         } else if (ST2 & ST2_CRC) {
1362                 pr_cont("data CRC error");
1363                 tell_sector();
1364         } else if (ST1 & ST1_CRC) {
1365                 pr_cont("CRC error");
1366                 tell_sector();
1367         } else if ((ST1 & (ST1_MAM | ST1_ND)) ||
1368                    (ST2 & ST2_MAM)) {
1369                 if (!probing) {
1370                         pr_cont("sector not found");
1371                         tell_sector();
1372                 } else
1373                         pr_cont("probe failed...");
1374         } else if (ST2 & ST2_WC) {      /* seek error */
1375                 pr_cont("wrong cylinder");
1376         } else if (ST2 & ST2_BC) {      /* cylinder marked as bad */
1377                 pr_cont("bad cylinder");
1378         } else {
1379                 pr_cont("unknown error. ST[0..2] are: 0x%x 0x%x 0x%x",
1380                         ST0, ST1, ST2);
1381                 tell_sector();
1382         }
1383         pr_cont("\n");
1384 }
1385
1386 /*
1387  * OK, this error interpreting routine is called after a
1388  * DMA read/write has succeeded
1389  * or failed, so we check the results, and copy any buffers.
1390  * hhb: Added better error reporting.
1391  * ak: Made this into a separate routine.
1392  */
1393 static int interpret_errors(void)
1394 {
1395         char bad;
1396
1397         if (inr != 7) {
1398                 DPRINT("-- FDC reply error\n");
1399                 FDCS->reset = 1;
1400                 return 1;
1401         }
1402
1403         /* check IC to find cause of interrupt */
1404         switch (ST0 & ST0_INTR) {
1405         case 0x40:              /* error occurred during command execution */
1406                 if (ST1 & ST1_EOC)
1407                         return 0;       /* occurs with pseudo-DMA */
1408                 bad = 1;
1409                 if (ST1 & ST1_WP) {
1410                         DPRINT("Drive is write protected\n");
1411                         clear_bit(FD_DISK_WRITABLE_BIT, &DRS->flags);
1412                         cont->done(0);
1413                         bad = 2;
1414                 } else if (ST1 & ST1_ND) {
1415                         set_bit(FD_NEED_TWADDLE_BIT, &DRS->flags);
1416                 } else if (ST1 & ST1_OR) {
1417                         if (DP->flags & FTD_MSG)
1418                                 DPRINT("Over/Underrun - retrying\n");
1419                         bad = 0;
1420                 } else if (*errors >= DP->max_errors.reporting) {
1421                         print_errors();
1422                 }
1423                 if (ST2 & ST2_WC || ST2 & ST2_BC)
1424                         /* wrong cylinder => recal */
1425                         DRS->track = NEED_2_RECAL;
1426                 return bad;
1427         case 0x80:              /* invalid command given */
1428                 DPRINT("Invalid FDC command given!\n");
1429                 cont->done(0);
1430                 return 2;
1431         case 0xc0:
1432                 DPRINT("Abnormal termination caused by polling\n");
1433                 cont->error();
1434                 return 2;
1435         default:                /* (0) Normal command termination */
1436                 return 0;
1437         }
1438 }
1439
1440 /*
1441  * This routine is called when everything should be correctly set up
1442  * for the transfer (i.e. floppy motor is on, the correct floppy is
1443  * selected, and the head is sitting on the right track).
1444  */
1445 static void setup_rw_floppy(void)
1446 {
1447         int i;
1448         int r;
1449         int flags;
1450         int dflags;
1451         unsigned long ready_date;
1452         timeout_fn function;
1453
1454         flags = raw_cmd->flags;
1455         if (flags & (FD_RAW_READ | FD_RAW_WRITE))
1456                 flags |= FD_RAW_INTR;
1457
1458         if ((flags & FD_RAW_SPIN) && !(flags & FD_RAW_NO_MOTOR)) {
1459                 ready_date = DRS->spinup_date + DP->spinup;
1460                 /* If spinup will take a long time, rerun scandrives
1461                  * again just before spinup completion. Beware that
1462                  * after scandrives, we must again wait for selection.
1463                  */
1464                 if (time_after(ready_date, jiffies + DP->select_delay)) {
1465                         ready_date -= DP->select_delay;
1466                         function = (timeout_fn)floppy_start;
1467                 } else
1468                         function = (timeout_fn)setup_rw_floppy;
1469
1470                 /* wait until the floppy is spinning fast enough */
1471                 if (fd_wait_for_completion(ready_date, function))
1472                         return;
1473         }
1474         dflags = DRS->flags;
1475
1476         if ((flags & FD_RAW_READ) || (flags & FD_RAW_WRITE))
1477                 setup_DMA();
1478
1479         if (flags & FD_RAW_INTR)
1480                 do_floppy = main_command_interrupt;
1481
1482         r = 0;
1483         for (i = 0; i < raw_cmd->cmd_count; i++)
1484                 r |= output_byte(raw_cmd->cmd[i]);
1485
1486         debugt(__func__, "rw_command");
1487
1488         if (r) {
1489                 cont->error();
1490                 reset_fdc();
1491                 return;
1492         }
1493
1494         if (!(flags & FD_RAW_INTR)) {
1495                 inr = result();
1496                 cont->interrupt();
1497         } else if (flags & FD_RAW_NEED_DISK)
1498                 fd_watchdog();
1499 }
1500
1501 static int blind_seek;
1502
1503 /*
1504  * This is the routine called after every seek (or recalibrate) interrupt
1505  * from the floppy controller.
1506  */
1507 static void seek_interrupt(void)
1508 {
1509         debugt(__func__, "");
1510         if (inr != 2 || (ST0 & 0xF8) != 0x20) {
1511                 DPRINT("seek failed\n");
1512                 DRS->track = NEED_2_RECAL;
1513                 cont->error();
1514                 cont->redo();
1515                 return;
1516         }
1517         if (DRS->track >= 0 && DRS->track != ST1 && !blind_seek) {
1518                 debug_dcl(DP->flags,
1519                           "clearing NEWCHANGE flag because of effective seek\n");
1520                 debug_dcl(DP->flags, "jiffies=%lu\n", jiffies);
1521                 clear_bit(FD_DISK_NEWCHANGE_BIT, &DRS->flags);
1522                                         /* effective seek */
1523                 DRS->select_date = jiffies;
1524         }
1525         DRS->track = ST1;
1526         floppy_ready();
1527 }
1528
1529 static void check_wp(void)
1530 {
1531         if (test_bit(FD_VERIFY_BIT, &DRS->flags)) {
1532                                         /* check write protection */
1533                 output_byte(FD_GETSTATUS);
1534                 output_byte(UNIT(current_drive));
1535                 if (result() != 1) {
1536                         FDCS->reset = 1;
1537                         return;
1538                 }
1539                 clear_bit(FD_VERIFY_BIT, &DRS->flags);
1540                 clear_bit(FD_NEED_TWADDLE_BIT, &DRS->flags);
1541                 debug_dcl(DP->flags,
1542                           "checking whether disk is write protected\n");
1543                 debug_dcl(DP->flags, "wp=%x\n", ST3 & 0x40);
1544                 if (!(ST3 & 0x40))
1545                         set_bit(FD_DISK_WRITABLE_BIT, &DRS->flags);
1546                 else
1547                         clear_bit(FD_DISK_WRITABLE_BIT, &DRS->flags);
1548         }
1549 }
1550
1551 static void seek_floppy(void)
1552 {
1553         int track;
1554
1555         blind_seek = 0;
1556
1557         debug_dcl(DP->flags, "calling disk change from %s\n", __func__);
1558
1559         if (!test_bit(FD_DISK_NEWCHANGE_BIT, &DRS->flags) &&
1560             disk_change(current_drive) && (raw_cmd->flags & FD_RAW_NEED_DISK)) {
1561                 /* the media changed flag should be cleared after the seek.
1562                  * If it isn't, this means that there is really no disk in
1563                  * the drive.
1564                  */
1565                 set_bit(FD_DISK_CHANGED_BIT, &DRS->flags);
1566                 cont->done(0);
1567                 cont->redo();
1568                 return;
1569         }
1570         if (DRS->track <= NEED_1_RECAL) {
1571                 recalibrate_floppy();
1572                 return;
1573         } else if (test_bit(FD_DISK_NEWCHANGE_BIT, &DRS->flags) &&
1574                    (raw_cmd->flags & FD_RAW_NEED_DISK) &&
1575                    (DRS->track <= NO_TRACK || DRS->track == raw_cmd->track)) {
1576                 /* we seek to clear the media-changed condition. Does anybody
1577                  * know a more elegant way, which works on all drives? */
1578                 if (raw_cmd->track)
1579                         track = raw_cmd->track - 1;
1580                 else {
1581                         if (DP->flags & FD_SILENT_DCL_CLEAR) {
1582                                 set_dor(fdc, ~(0x10 << UNIT(current_drive)), 0);
1583                                 blind_seek = 1;
1584                                 raw_cmd->flags |= FD_RAW_NEED_SEEK;
1585                         }
1586                         track = 1;
1587                 }
1588         } else {
1589                 check_wp();
1590                 if (raw_cmd->track != DRS->track &&
1591                     (raw_cmd->flags & FD_RAW_NEED_SEEK))
1592                         track = raw_cmd->track;
1593                 else {
1594                         setup_rw_floppy();
1595                         return;
1596                 }
1597         }
1598
1599         do_floppy = seek_interrupt;
1600         output_byte(FD_SEEK);
1601         output_byte(UNIT(current_drive));
1602         if (output_byte(track) < 0) {
1603                 reset_fdc();
1604                 return;
1605         }
1606         debugt(__func__, "");
1607 }
1608
1609 static void recal_interrupt(void)
1610 {
1611         debugt(__func__, "");
1612         if (inr != 2)
1613                 FDCS->reset = 1;
1614         else if (ST0 & ST0_ECE) {
1615                 switch (DRS->track) {
1616                 case NEED_1_RECAL:
1617                         debugt(__func__, "need 1 recal");
1618                         /* after a second recalibrate, we still haven't
1619                          * reached track 0. Probably no drive. Raise an
1620                          * error, as failing immediately might upset
1621                          * computers possessed by the Devil :-) */
1622                         cont->error();
1623                         cont->redo();
1624                         return;
1625                 case NEED_2_RECAL:
1626                         debugt(__func__, "need 2 recal");
1627                         /* If we already did a recalibrate,
1628                          * and we are not at track 0, this
1629                          * means we have moved. (The only way
1630                          * not to move at recalibration is to
1631                          * be already at track 0.) Clear the
1632                          * new change flag */
1633                         debug_dcl(DP->flags,
1634                                   "clearing NEWCHANGE flag because of second recalibrate\n");
1635
1636                         clear_bit(FD_DISK_NEWCHANGE_BIT, &DRS->flags);
1637                         DRS->select_date = jiffies;
1638                         /* fall through */
1639                 default:
1640                         debugt(__func__, "default");
1641                         /* Recalibrate moves the head by at
1642                          * most 80 steps. If after one
1643                          * recalibrate we don't have reached
1644                          * track 0, this might mean that we
1645                          * started beyond track 80.  Try
1646                          * again.  */
1647                         DRS->track = NEED_1_RECAL;
1648                         break;
1649                 }
1650         } else
1651                 DRS->track = ST1;
1652         floppy_ready();
1653 }
1654
1655 static void print_result(char *message, int inr)
1656 {
1657         int i;
1658
1659         DPRINT("%s ", message);
1660         if (inr >= 0)
1661                 for (i = 0; i < inr; i++)
1662                         pr_cont("repl[%d]=%x ", i, reply_buffer[i]);
1663         pr_cont("\n");
1664 }
1665
1666 /* interrupt handler. Note that this can be called externally on the Sparc */
1667 irqreturn_t floppy_interrupt(int irq, void *dev_id)
1668 {
1669         int do_print;
1670         unsigned long f;
1671         void (*handler)(void) = do_floppy;
1672
1673         lasthandler = handler;
1674         interruptjiffies = jiffies;
1675
1676         f = claim_dma_lock();
1677         fd_disable_dma();
1678         release_dma_lock(f);
1679
1680         do_floppy = NULL;
1681         if (fdc >= N_FDC || FDCS->address == -1) {
1682                 /* we don't even know which FDC is the culprit */
1683                 pr_info("DOR0=%x\n", fdc_state[0].dor);
1684                 pr_info("floppy interrupt on bizarre fdc %d\n", fdc);
1685                 pr_info("handler=%pf\n", handler);
1686                 is_alive(__func__, "bizarre fdc");
1687                 return IRQ_NONE;
1688         }
1689
1690         FDCS->reset = 0;
1691         /* We have to clear the reset flag here, because apparently on boxes
1692          * with level triggered interrupts (PS/2, Sparc, ...), it is needed to
1693          * emit SENSEI's to clear the interrupt line. And FDCS->reset blocks the
1694          * emission of the SENSEI's.
1695          * It is OK to emit floppy commands because we are in an interrupt
1696          * handler here, and thus we have to fear no interference of other
1697          * activity.
1698          */
1699
1700         do_print = !handler && print_unex && initialized;
1701
1702         inr = result();
1703         if (do_print)
1704                 print_result("unexpected interrupt", inr);
1705         if (inr == 0) {
1706                 int max_sensei = 4;
1707                 do {
1708                         output_byte(FD_SENSEI);
1709                         inr = result();
1710                         if (do_print)
1711                                 print_result("sensei", inr);
1712                         max_sensei--;
1713                 } while ((ST0 & 0x83) != UNIT(current_drive) &&
1714                          inr == 2 && max_sensei);
1715         }
1716         if (!handler) {
1717                 FDCS->reset = 1;
1718                 return IRQ_NONE;
1719         }
1720         schedule_bh(handler);
1721         is_alive(__func__, "normal interrupt end");
1722
1723         /* FIXME! Was it really for us? */
1724         return IRQ_HANDLED;
1725 }
1726
1727 static void recalibrate_floppy(void)
1728 {
1729         debugt(__func__, "");
1730         do_floppy = recal_interrupt;
1731         output_byte(FD_RECALIBRATE);
1732         if (output_byte(UNIT(current_drive)) < 0)
1733                 reset_fdc();
1734 }
1735
1736 /*
1737  * Must do 4 FD_SENSEIs after reset because of ``drive polling''.
1738  */
1739 static void reset_interrupt(void)
1740 {
1741         debugt(__func__, "");
1742         result();               /* get the status ready for set_fdc */
1743         if (FDCS->reset) {
1744                 pr_info("reset set in interrupt, calling %pf\n", cont->error);
1745                 cont->error();  /* a reset just after a reset. BAD! */
1746         }
1747         cont->redo();
1748 }
1749
1750 /*
1751  * reset is done by pulling bit 2 of DOR low for a while (old FDCs),
1752  * or by setting the self clearing bit 7 of STATUS (newer FDCs)
1753  */
1754 static void reset_fdc(void)
1755 {
1756         unsigned long flags;
1757
1758         do_floppy = reset_interrupt;
1759         FDCS->reset = 0;
1760         reset_fdc_info(0);
1761
1762         /* Pseudo-DMA may intercept 'reset finished' interrupt.  */
1763         /* Irrelevant for systems with true DMA (i386).          */
1764
1765         flags = claim_dma_lock();
1766         fd_disable_dma();
1767         release_dma_lock(flags);
1768
1769         if (FDCS->version >= FDC_82072A)
1770                 fd_outb(0x80 | (FDCS->dtr & 3), FD_STATUS);
1771         else {
1772                 fd_outb(FDCS->dor & ~0x04, FD_DOR);
1773                 udelay(FD_RESET_DELAY);
1774                 fd_outb(FDCS->dor, FD_DOR);
1775         }
1776 }
1777
1778 static void show_floppy(void)
1779 {
1780         int i;
1781
1782         pr_info("\n");
1783         pr_info("floppy driver state\n");
1784         pr_info("-------------------\n");
1785         pr_info("now=%lu last interrupt=%lu diff=%lu last called handler=%pf\n",
1786                 jiffies, interruptjiffies, jiffies - interruptjiffies,
1787                 lasthandler);
1788
1789         pr_info("timeout_message=%s\n", timeout_message);
1790         pr_info("last output bytes:\n");
1791         for (i = 0; i < OLOGSIZE; i++)
1792                 pr_info("%2x %2x %lu\n",
1793                         output_log[(i + output_log_pos) % OLOGSIZE].data,
1794                         output_log[(i + output_log_pos) % OLOGSIZE].status,
1795                         output_log[(i + output_log_pos) % OLOGSIZE].jiffies);
1796         pr_info("last result at %lu\n", resultjiffies);
1797         pr_info("last redo_fd_request at %lu\n", lastredo);
1798         print_hex_dump(KERN_INFO, "", DUMP_PREFIX_NONE, 16, 1,
1799                        reply_buffer, resultsize, true);
1800
1801         pr_info("status=%x\n", fd_inb(FD_STATUS));
1802         pr_info("fdc_busy=%lu\n", fdc_busy);
1803         if (do_floppy)
1804                 pr_info("do_floppy=%pf\n", do_floppy);
1805         if (work_pending(&floppy_work))
1806                 pr_info("floppy_work.func=%pf\n", floppy_work.func);
1807         if (timer_pending(&fd_timer))
1808                 pr_info("fd_timer.function=%pf\n", fd_timer.function);
1809         if (timer_pending(&fd_timeout)) {
1810                 pr_info("timer_function=%pf\n", fd_timeout.function);
1811                 pr_info("expires=%lu\n", fd_timeout.expires - jiffies);
1812                 pr_info("now=%lu\n", jiffies);
1813         }
1814         pr_info("cont=%p\n", cont);
1815         pr_info("current_req=%p\n", current_req);
1816         pr_info("command_status=%d\n", command_status);
1817         pr_info("\n");
1818 }
1819
1820 static void floppy_shutdown(unsigned long data)
1821 {
1822         unsigned long flags;
1823
1824         if (initialized)
1825                 show_floppy();
1826         cancel_activity();
1827
1828         flags = claim_dma_lock();
1829         fd_disable_dma();
1830         release_dma_lock(flags);
1831
1832         /* avoid dma going to a random drive after shutdown */
1833
1834         if (initialized)
1835                 DPRINT("floppy timeout called\n");
1836         FDCS->reset = 1;
1837         if (cont) {
1838                 cont->done(0);
1839                 cont->redo();   /* this will recall reset when needed */
1840         } else {
1841                 pr_info("no cont in shutdown!\n");
1842                 process_fd_request();
1843         }
1844         is_alive(__func__, "");
1845 }
1846
1847 /* start motor, check media-changed condition and write protection */
1848 static int start_motor(void (*function)(void))
1849 {
1850         int mask;
1851         int data;
1852
1853         mask = 0xfc;
1854         data = UNIT(current_drive);
1855         if (!(raw_cmd->flags & FD_RAW_NO_MOTOR)) {
1856                 if (!(FDCS->dor & (0x10 << UNIT(current_drive)))) {
1857                         set_debugt();
1858                         /* no read since this drive is running */
1859                         DRS->first_read_date = 0;
1860                         /* note motor start time if motor is not yet running */
1861                         DRS->spinup_date = jiffies;
1862                         data |= (0x10 << UNIT(current_drive));
1863                 }
1864         } else if (FDCS->dor & (0x10 << UNIT(current_drive)))
1865                 mask &= ~(0x10 << UNIT(current_drive));
1866
1867         /* starts motor and selects floppy */
1868         del_timer(motor_off_timer + current_drive);
1869         set_dor(fdc, mask, data);
1870
1871         /* wait_for_completion also schedules reset if needed. */
1872         return fd_wait_for_completion(DRS->select_date + DP->select_delay,
1873                                       (timeout_fn)function);
1874 }
1875
1876 static void floppy_ready(void)
1877 {
1878         if (FDCS->reset) {
1879                 reset_fdc();
1880                 return;
1881         }
1882         if (start_motor(floppy_ready))
1883                 return;
1884         if (fdc_dtr())
1885                 return;
1886
1887         debug_dcl(DP->flags, "calling disk change from floppy_ready\n");
1888         if (!(raw_cmd->flags & FD_RAW_NO_MOTOR) &&
1889             disk_change(current_drive) && !DP->select_delay)
1890                 twaddle();      /* this clears the dcl on certain
1891                                  * drive/controller combinations */
1892
1893 #ifdef fd_chose_dma_mode
1894         if ((raw_cmd->flags & FD_RAW_READ) || (raw_cmd->flags & FD_RAW_WRITE)) {
1895                 unsigned long flags = claim_dma_lock();
1896                 fd_chose_dma_mode(raw_cmd->kernel_data, raw_cmd->length);
1897                 release_dma_lock(flags);
1898         }
1899 #endif
1900
1901         if (raw_cmd->flags & (FD_RAW_NEED_SEEK | FD_RAW_NEED_DISK)) {
1902                 perpendicular_mode();
1903                 fdc_specify();  /* must be done here because of hut, hlt ... */
1904                 seek_floppy();
1905         } else {
1906                 if ((raw_cmd->flags & FD_RAW_READ) ||
1907                     (raw_cmd->flags & FD_RAW_WRITE))
1908                         fdc_specify();
1909                 setup_rw_floppy();
1910         }
1911 }
1912
1913 static void floppy_start(void)
1914 {
1915         reschedule_timeout(current_reqD, "floppy start");
1916
1917         scandrives();
1918         debug_dcl(DP->flags, "setting NEWCHANGE in floppy_start\n");
1919         set_bit(FD_DISK_NEWCHANGE_BIT, &DRS->flags);
1920         floppy_ready();
1921 }
1922
1923 /*
1924  * ========================================================================
1925  * here ends the bottom half. Exported routines are:
1926  * floppy_start, floppy_off, floppy_ready, lock_fdc, unlock_fdc, set_fdc,
1927  * start_motor, reset_fdc, reset_fdc_info, interpret_errors.
1928  * Initialization also uses output_byte, result, set_dor, floppy_interrupt
1929  * and set_dor.
1930  * ========================================================================
1931  */
1932 /*
1933  * General purpose continuations.
1934  * ==============================
1935  */
1936
1937 static void do_wakeup(void)
1938 {
1939         reschedule_timeout(MAXTIMEOUT, "do wakeup");
1940         cont = NULL;
1941         command_status += 2;
1942         wake_up(&command_done);
1943 }
1944
1945 static const struct cont_t wakeup_cont = {
1946         .interrupt      = empty,
1947         .redo           = do_wakeup,
1948         .error          = empty,
1949         .done           = (done_f)empty
1950 };
1951
1952 static const struct cont_t intr_cont = {
1953         .interrupt      = empty,
1954         .redo           = process_fd_request,
1955         .error          = empty,
1956         .done           = (done_f)empty
1957 };
1958
1959 static int wait_til_done(void (*handler)(void), bool interruptible)
1960 {
1961         int ret;
1962
1963         schedule_bh(handler);
1964
1965         if (interruptible)
1966                 wait_event_interruptible(command_done, command_status >= 2);
1967         else
1968                 wait_event(command_done, command_status >= 2);
1969
1970         if (command_status < 2) {
1971                 cancel_activity();
1972                 cont = &intr_cont;
1973                 reset_fdc();
1974                 return -EINTR;
1975         }
1976
1977         if (FDCS->reset)
1978                 command_status = FD_COMMAND_ERROR;
1979         if (command_status == FD_COMMAND_OKAY)
1980                 ret = 0;
1981         else
1982                 ret = -EIO;
1983         command_status = FD_COMMAND_NONE;
1984         return ret;
1985 }
1986
1987 static void generic_done(int result)
1988 {
1989         command_status = result;
1990         cont = &wakeup_cont;
1991 }
1992
1993 static void generic_success(void)
1994 {
1995         cont->done(1);
1996 }
1997
1998 static void generic_failure(void)
1999 {
2000         cont->done(0);
2001 }
2002
2003 static void success_and_wakeup(void)
2004 {
2005         generic_success();
2006         cont->redo();
2007 }
2008
2009 /*
2010  * formatting and rw support.
2011  * ==========================
2012  */
2013
2014 static int next_valid_format(void)
2015 {
2016         int probed_format;
2017
2018         probed_format = DRS->probed_format;
2019         while (1) {
2020                 if (probed_format >= 8 || !DP->autodetect[probed_format]) {
2021                         DRS->probed_format = 0;
2022                         return 1;
2023                 }
2024                 if (floppy_type[DP->autodetect[probed_format]].sect) {
2025                         DRS->probed_format = probed_format;
2026                         return 0;
2027                 }
2028                 probed_format++;
2029         }
2030 }
2031
2032 static void bad_flp_intr(void)
2033 {
2034         int err_count;
2035
2036         if (probing) {
2037                 DRS->probed_format++;
2038                 if (!next_valid_format())
2039                         return;
2040         }
2041         err_count = ++(*errors);
2042         INFBOUND(DRWE->badness, err_count);
2043         if (err_count > DP->max_errors.abort)
2044                 cont->done(0);
2045         if (err_count > DP->max_errors.reset)
2046                 FDCS->reset = 1;
2047         else if (err_count > DP->max_errors.recal)
2048                 DRS->track = NEED_2_RECAL;
2049 }
2050
2051 static void set_floppy(int drive)
2052 {
2053         int type = ITYPE(UDRS->fd_device);
2054
2055         if (type)
2056                 _floppy = floppy_type + type;
2057         else
2058                 _floppy = current_type[drive];
2059 }
2060
2061 /*
2062  * formatting support.
2063  * ===================
2064  */
2065 static void format_interrupt(void)
2066 {
2067         switch (interpret_errors()) {
2068         case 1:
2069                 cont->error();
2070         case 2:
2071                 break;
2072         case 0:
2073                 cont->done(1);
2074         }
2075         cont->redo();
2076 }
2077
2078 #define FM_MODE(x, y) ((y) & ~(((x)->rate & 0x80) >> 1))
2079 #define CT(x) ((x) | 0xc0)
2080
2081 static void setup_format_params(int track)
2082 {
2083         int n;
2084         int il;
2085         int count;
2086         int head_shift;
2087         int track_shift;
2088         struct fparm {
2089                 unsigned char track, head, sect, size;
2090         } *here = (struct fparm *)floppy_track_buffer;
2091
2092         raw_cmd = &default_raw_cmd;
2093         raw_cmd->track = track;
2094
2095         raw_cmd->flags = (FD_RAW_WRITE | FD_RAW_INTR | FD_RAW_SPIN |
2096                           FD_RAW_NEED_DISK | FD_RAW_NEED_SEEK);
2097         raw_cmd->rate = _floppy->rate & 0x43;
2098         raw_cmd->cmd_count = NR_F;
2099         COMMAND = FM_MODE(_floppy, FD_FORMAT);
2100         DR_SELECT = UNIT(current_drive) + PH_HEAD(_floppy, format_req.head);
2101         F_SIZECODE = FD_SIZECODE(_floppy);
2102         F_SECT_PER_TRACK = _floppy->sect << 2 >> F_SIZECODE;
2103         F_GAP = _floppy->fmt_gap;
2104         F_FILL = FD_FILL_BYTE;
2105
2106         raw_cmd->kernel_data = floppy_track_buffer;
2107         raw_cmd->length = 4 * F_SECT_PER_TRACK;
2108
2109         /* allow for about 30ms for data transport per track */
2110         head_shift = (F_SECT_PER_TRACK + 5) / 6;
2111
2112         /* a ``cylinder'' is two tracks plus a little stepping time */
2113         track_shift = 2 * head_shift + 3;
2114
2115         /* position of logical sector 1 on this track */
2116         n = (track_shift * format_req.track + head_shift * format_req.head)
2117             % F_SECT_PER_TRACK;
2118
2119         /* determine interleave */
2120         il = 1;
2121         if (_floppy->fmt_gap < 0x22)
2122                 il++;
2123
2124         /* initialize field */
2125         for (count = 0; count < F_SECT_PER_TRACK; ++count) {
2126                 here[count].track = format_req.track;
2127                 here[count].head = format_req.head;
2128                 here[count].sect = 0;
2129                 here[count].size = F_SIZECODE;
2130         }
2131         /* place logical sectors */
2132         for (count = 1; count <= F_SECT_PER_TRACK; ++count) {
2133                 here[n].sect = count;
2134                 n = (n + il) % F_SECT_PER_TRACK;
2135                 if (here[n].sect) {     /* sector busy, find next free sector */
2136                         ++n;
2137                         if (n >= F_SECT_PER_TRACK) {
2138                                 n -= F_SECT_PER_TRACK;
2139                                 while (here[n].sect)
2140                                         ++n;
2141                         }
2142                 }
2143         }
2144         if (_floppy->stretch & FD_SECTBASEMASK) {
2145                 for (count = 0; count < F_SECT_PER_TRACK; count++)
2146                         here[count].sect += FD_SECTBASE(_floppy) - 1;
2147         }
2148 }
2149
2150 static void redo_format(void)
2151 {
2152         buffer_track = -1;
2153         setup_format_params(format_req.track << STRETCH(_floppy));
2154         floppy_start();
2155         debugt(__func__, "queue format request");
2156 }
2157
2158 static const struct cont_t format_cont = {
2159         .interrupt      = format_interrupt,
2160         .redo           = redo_format,
2161         .error          = bad_flp_intr,
2162         .done           = generic_done
2163 };
2164
2165 static int do_format(int drive, struct format_descr *tmp_format_req)
2166 {
2167         int ret;
2168
2169         if (lock_fdc(drive, true))
2170                 return -EINTR;
2171
2172         set_floppy(drive);
2173         if (!_floppy ||
2174             _floppy->track > DP->tracks ||
2175             tmp_format_req->track >= _floppy->track ||
2176             tmp_format_req->head >= _floppy->head ||
2177             (_floppy->sect << 2) % (1 << FD_SIZECODE(_floppy)) ||
2178             !_floppy->fmt_gap) {
2179                 process_fd_request();
2180                 return -EINVAL;
2181         }
2182         format_req = *tmp_format_req;
2183         format_errors = 0;
2184         cont = &format_cont;
2185         errors = &format_errors;
2186         ret = wait_til_done(redo_format, true);
2187         if (ret == -EINTR)
2188                 return -EINTR;
2189         process_fd_request();
2190         return ret;
2191 }
2192
2193 /*
2194  * Buffer read/write and support
2195  * =============================
2196  */
2197
2198 static void floppy_end_request(struct request *req, int error)
2199 {
2200         unsigned int nr_sectors = current_count_sectors;
2201         unsigned int drive = (unsigned long)req->rq_disk->private_data;
2202
2203         /* current_count_sectors can be zero if transfer failed */
2204         if (error)
2205                 nr_sectors = blk_rq_cur_sectors(req);
2206         if (__blk_end_request(req, error, nr_sectors << 9))
2207                 return;
2208
2209         /* We're done with the request */
2210         floppy_off(drive);
2211         current_req = NULL;
2212 }
2213
2214 /* new request_done. Can handle physical sectors which are smaller than a
2215  * logical buffer */
2216 static void request_done(int uptodate)
2217 {
2218         struct request *req = current_req;
2219         struct request_queue *q;
2220         unsigned long flags;
2221         int block;
2222         char msg[sizeof("request done ") + sizeof(int) * 3];
2223
2224         probing = 0;
2225         snprintf(msg, sizeof(msg), "request done %d", uptodate);
2226         reschedule_timeout(MAXTIMEOUT, msg);
2227
2228         if (!req) {
2229                 pr_info("floppy.c: no request in request_done\n");
2230                 return;
2231         }
2232
2233         q = req->q;
2234
2235         if (uptodate) {
2236                 /* maintain values for invalidation on geometry
2237                  * change */
2238                 block = current_count_sectors + blk_rq_pos(req);
2239                 INFBOUND(DRS->maxblock, block);
2240                 if (block > _floppy->sect)
2241                         DRS->maxtrack = 1;
2242
2243                 /* unlock chained buffers */
2244                 spin_lock_irqsave(q->queue_lock, flags);
2245                 floppy_end_request(req, 0);
2246                 spin_unlock_irqrestore(q->queue_lock, flags);
2247         } else {
2248                 if (rq_data_dir(req) == WRITE) {
2249                         /* record write error information */
2250                         DRWE->write_errors++;
2251                         if (DRWE->write_errors == 1) {
2252                                 DRWE->first_error_sector = blk_rq_pos(req);
2253                                 DRWE->first_error_generation = DRS->generation;
2254                         }
2255                         DRWE->last_error_sector = blk_rq_pos(req);
2256                         DRWE->last_error_generation = DRS->generation;
2257                 }
2258                 spin_lock_irqsave(q->queue_lock, flags);
2259                 floppy_end_request(req, -EIO);
2260                 spin_unlock_irqrestore(q->queue_lock, flags);
2261         }
2262 }
2263
2264 /* Interrupt handler evaluating the result of the r/w operation */
2265 static void rw_interrupt(void)
2266 {
2267         int eoc;
2268         int ssize;
2269         int heads;
2270         int nr_sectors;
2271
2272         if (R_HEAD >= 2) {
2273                 /* some Toshiba floppy controllers occasionnally seem to
2274                  * return bogus interrupts after read/write operations, which
2275                  * can be recognized by a bad head number (>= 2) */
2276                 return;
2277         }
2278
2279         if (!DRS->first_read_date)
2280                 DRS->first_read_date = jiffies;
2281
2282         nr_sectors = 0;
2283         ssize = DIV_ROUND_UP(1 << SIZECODE, 4);
2284
2285         if (ST1 & ST1_EOC)
2286                 eoc = 1;
2287         else
2288                 eoc = 0;
2289
2290         if (COMMAND & 0x80)
2291                 heads = 2;
2292         else
2293                 heads = 1;
2294
2295         nr_sectors = (((R_TRACK - TRACK) * heads +
2296                        R_HEAD - HEAD) * SECT_PER_TRACK +
2297                       R_SECTOR - SECTOR + eoc) << SIZECODE >> 2;
2298
2299         if (nr_sectors / ssize >
2300             DIV_ROUND_UP(in_sector_offset + current_count_sectors, ssize)) {
2301                 DPRINT("long rw: %x instead of %lx\n",
2302                        nr_sectors, current_count_sectors);
2303                 pr_info("rs=%d s=%d\n", R_SECTOR, SECTOR);
2304                 pr_info("rh=%d h=%d\n", R_HEAD, HEAD);
2305                 pr_info("rt=%d t=%d\n", R_TRACK, TRACK);
2306                 pr_info("heads=%d eoc=%d\n", heads, eoc);
2307                 pr_info("spt=%d st=%d ss=%d\n",
2308                         SECT_PER_TRACK, fsector_t, ssize);
2309                 pr_info("in_sector_offset=%d\n", in_sector_offset);
2310         }
2311
2312         nr_sectors -= in_sector_offset;
2313         INFBOUND(nr_sectors, 0);
2314         SUPBOUND(current_count_sectors, nr_sectors);
2315
2316         switch (interpret_errors()) {
2317         case 2:
2318                 cont->redo();
2319                 return;
2320         case 1:
2321                 if (!current_count_sectors) {
2322                         cont->error();
2323                         cont->redo();
2324                         return;
2325                 }
2326                 break;
2327         case 0:
2328                 if (!current_count_sectors) {
2329                         cont->redo();
2330                         return;
2331                 }
2332                 current_type[current_drive] = _floppy;
2333                 floppy_sizes[TOMINOR(current_drive)] = _floppy->size;
2334                 break;
2335         }
2336
2337         if (probing) {
2338                 if (DP->flags & FTD_MSG)
2339                         DPRINT("Auto-detected floppy type %s in fd%d\n",
2340                                _floppy->name, current_drive);
2341                 current_type[current_drive] = _floppy;
2342                 floppy_sizes[TOMINOR(current_drive)] = _floppy->size;
2343                 probing = 0;
2344         }
2345
2346         if (CT(COMMAND) != FD_READ ||
2347             raw_cmd->kernel_data == current_req->buffer) {
2348                 /* transfer directly from buffer */
2349                 cont->done(1);
2350         } else if (CT(COMMAND) == FD_READ) {
2351                 buffer_track = raw_cmd->track;
2352                 buffer_drive = current_drive;
2353                 INFBOUND(buffer_max, nr_sectors + fsector_t);
2354         }
2355         cont->redo();
2356 }
2357
2358 /* Compute maximal contiguous buffer size. */
2359 static int buffer_chain_size(void)
2360 {
2361         struct bio_vec *bv;
2362         int size;
2363         struct req_iterator iter;
2364         char *base;
2365
2366         base = bio_data(current_req->bio);
2367         size = 0;
2368
2369         rq_for_each_segment(bv, current_req, iter) {
2370                 if (page_address(bv->bv_page) + bv->bv_offset != base + size)
2371                         break;
2372
2373                 size += bv->bv_len;
2374         }
2375
2376         return size >> 9;
2377 }
2378
2379 /* Compute the maximal transfer size */
2380 static int transfer_size(int ssize, int max_sector, int max_size)
2381 {
2382         SUPBOUND(max_sector, fsector_t + max_size);
2383
2384         /* alignment */
2385         max_sector -= (max_sector % _floppy->sect) % ssize;
2386
2387         /* transfer size, beginning not aligned */
2388         current_count_sectors = max_sector - fsector_t;
2389
2390         return max_sector;
2391 }
2392
2393 /*
2394  * Move data from/to the track buffer to/from the buffer cache.
2395  */
2396 static void copy_buffer(int ssize, int max_sector, int max_sector_2)
2397 {
2398         int remaining;          /* number of transferred 512-byte sectors */
2399         struct bio_vec *bv;
2400         char *buffer;
2401         char *dma_buffer;
2402         int size;
2403         struct req_iterator iter;
2404
2405         max_sector = transfer_size(ssize,
2406                                    min(max_sector, max_sector_2),
2407                                    blk_rq_sectors(current_req));
2408
2409         if (current_count_sectors <= 0 && CT(COMMAND) == FD_WRITE &&
2410             buffer_max > fsector_t + blk_rq_sectors(current_req))
2411                 current_count_sectors = min_t(int, buffer_max - fsector_t,
2412                                               blk_rq_sectors(current_req));
2413
2414         remaining = current_count_sectors << 9;
2415         if (remaining > blk_rq_bytes(current_req) && CT(COMMAND) == FD_WRITE) {
2416                 DPRINT("in copy buffer\n");
2417                 pr_info("current_count_sectors=%ld\n", current_count_sectors);
2418                 pr_info("remaining=%d\n", remaining >> 9);
2419                 pr_info("current_req->nr_sectors=%u\n",
2420                         blk_rq_sectors(current_req));
2421                 pr_info("current_req->current_nr_sectors=%u\n",
2422                         blk_rq_cur_sectors(current_req));
2423                 pr_info("max_sector=%d\n", max_sector);
2424                 pr_info("ssize=%d\n", ssize);
2425         }
2426
2427         buffer_max = max(max_sector, buffer_max);
2428
2429         dma_buffer = floppy_track_buffer + ((fsector_t - buffer_min) << 9);
2430
2431         size = blk_rq_cur_bytes(current_req);
2432
2433         rq_for_each_segment(bv, current_req, iter) {
2434                 if (!remaining)
2435                         break;
2436
2437                 size = bv->bv_len;
2438                 SUPBOUND(size, remaining);
2439
2440                 buffer = page_address(bv->bv_page) + bv->bv_offset;
2441                 if (dma_buffer + size >
2442                     floppy_track_buffer + (max_buffer_sectors << 10) ||
2443                     dma_buffer < floppy_track_buffer) {
2444                         DPRINT("buffer overrun in copy buffer %d\n",
2445                                (int)((floppy_track_buffer - dma_buffer) >> 9));
2446                         pr_info("fsector_t=%d buffer_min=%d\n",
2447                                 fsector_t, buffer_min);
2448                         pr_info("current_count_sectors=%ld\n",
2449                                 current_count_sectors);
2450                         if (CT(COMMAND) == FD_READ)
2451                                 pr_info("read\n");
2452                         if (CT(COMMAND) == FD_WRITE)
2453                                 pr_info("write\n");
2454                         break;
2455                 }
2456                 if (((unsigned long)buffer) % 512)
2457                         DPRINT("%p buffer not aligned\n", buffer);
2458
2459                 if (CT(COMMAND) == FD_READ)
2460                         memcpy(buffer, dma_buffer, size);
2461                 else
2462                         memcpy(dma_buffer, buffer, size);
2463
2464                 remaining -= size;
2465                 dma_buffer += size;
2466         }
2467         if (remaining) {
2468                 if (remaining > 0)
2469                         max_sector -= remaining >> 9;
2470                 DPRINT("weirdness: remaining %d\n", remaining >> 9);
2471         }
2472 }
2473
2474 /* work around a bug in pseudo DMA
2475  * (on some FDCs) pseudo DMA does not stop when the CPU stops
2476  * sending data.  Hence we need a different way to signal the
2477  * transfer length:  We use SECT_PER_TRACK.  Unfortunately, this
2478  * does not work with MT, hence we can only transfer one head at
2479  * a time
2480  */
2481 static void virtualdmabug_workaround(void)
2482 {
2483         int hard_sectors;
2484         int end_sector;
2485
2486         if (CT(COMMAND) == FD_WRITE) {
2487                 COMMAND &= ~0x80;       /* switch off multiple track mode */
2488
2489                 hard_sectors = raw_cmd->length >> (7 + SIZECODE);
2490                 end_sector = SECTOR + hard_sectors - 1;
2491                 if (end_sector > SECT_PER_TRACK) {
2492                         pr_info("too many sectors %d > %d\n",
2493                                 end_sector, SECT_PER_TRACK);
2494                         return;
2495                 }
2496                 SECT_PER_TRACK = end_sector;
2497                                         /* make sure SECT_PER_TRACK
2498                                          * points to end of transfer */
2499         }
2500 }
2501
2502 /*
2503  * Formulate a read/write request.
2504  * this routine decides where to load the data (directly to buffer, or to
2505  * tmp floppy area), how much data to load (the size of the buffer, the whole
2506  * track, or a single sector)
2507  * All floppy_track_buffer handling goes in here. If we ever add track buffer
2508  * allocation on the fly, it should be done here. No other part should need
2509  * modification.
2510  */
2511
2512 static int make_raw_rw_request(void)
2513 {
2514         int aligned_sector_t;
2515         int max_sector;
2516         int max_size;
2517         int tracksize;
2518         int ssize;
2519
2520         if (WARN(max_buffer_sectors == 0, "VFS: Block I/O scheduled on unopened device\n"))
2521                 return 0;
2522
2523         set_fdc((long)current_req->rq_disk->private_data);
2524
2525         raw_cmd = &default_raw_cmd;
2526         raw_cmd->flags = FD_RAW_SPIN | FD_RAW_NEED_DISK | FD_RAW_NEED_DISK |
2527             FD_RAW_NEED_SEEK;
2528         raw_cmd->cmd_count = NR_RW;
2529         if (rq_data_dir(current_req) == READ) {
2530                 raw_cmd->flags |= FD_RAW_READ;
2531                 COMMAND = FM_MODE(_floppy, FD_READ);
2532         } else if (rq_data_dir(current_req) == WRITE) {
2533                 raw_cmd->flags |= FD_RAW_WRITE;
2534                 COMMAND = FM_MODE(_floppy, FD_WRITE);
2535         } else {
2536                 DPRINT("%s: unknown command\n", __func__);
2537                 return 0;
2538         }
2539
2540         max_sector = _floppy->sect * _floppy->head;
2541
2542         TRACK = (int)blk_rq_pos(current_req) / max_sector;
2543         fsector_t = (int)blk_rq_pos(current_req) % max_sector;
2544         if (_floppy->track && TRACK >= _floppy->track) {
2545                 if (blk_rq_cur_sectors(current_req) & 1) {
2546                         current_count_sectors = 1;
2547                         return 1;
2548                 } else
2549                         return 0;
2550         }
2551         HEAD = fsector_t / _floppy->sect;
2552
2553         if (((_floppy->stretch & (FD_SWAPSIDES | FD_SECTBASEMASK)) ||
2554              test_bit(FD_NEED_TWADDLE_BIT, &DRS->flags)) &&
2555             fsector_t < _floppy->sect)
2556                 max_sector = _floppy->sect;
2557
2558         /* 2M disks have phantom sectors on the first track */
2559         if ((_floppy->rate & FD_2M) && (!TRACK) && (!HEAD)) {
2560                 max_sector = 2 * _floppy->sect / 3;
2561                 if (fsector_t >= max_sector) {
2562                         current_count_sectors =
2563                             min_t(int, _floppy->sect - fsector_t,
2564                                   blk_rq_sectors(current_req));
2565                         return 1;
2566                 }
2567                 SIZECODE = 2;
2568         } else
2569                 SIZECODE = FD_SIZECODE(_floppy);
2570         raw_cmd->rate = _floppy->rate & 0x43;
2571         if ((_floppy->rate & FD_2M) && (TRACK || HEAD) && raw_cmd->rate == 2)
2572                 raw_cmd->rate = 1;
2573
2574         if (SIZECODE)
2575                 SIZECODE2 = 0xff;
2576         else
2577                 SIZECODE2 = 0x80;
2578         raw_cmd->track = TRACK << STRETCH(_floppy);
2579         DR_SELECT = UNIT(current_drive) + PH_HEAD(_floppy, HEAD);
2580         GAP = _floppy->gap;
2581         ssize = DIV_ROUND_UP(1 << SIZECODE, 4);
2582         SECT_PER_TRACK = _floppy->sect << 2 >> SIZECODE;
2583         SECTOR = ((fsector_t % _floppy->sect) << 2 >> SIZECODE) +
2584             FD_SECTBASE(_floppy);
2585
2586         /* tracksize describes the size which can be filled up with sectors
2587          * of size ssize.
2588          */
2589         tracksize = _floppy->sect - _floppy->sect % ssize;
2590         if (tracksize < _floppy->sect) {
2591                 SECT_PER_TRACK++;
2592                 if (tracksize <= fsector_t % _floppy->sect)
2593                         SECTOR--;
2594
2595                 /* if we are beyond tracksize, fill up using smaller sectors */
2596                 while (tracksize <= fsector_t % _floppy->sect) {
2597                         while (tracksize + ssize > _floppy->sect) {
2598                                 SIZECODE--;
2599                                 ssize >>= 1;
2600                         }
2601                         SECTOR++;
2602                         SECT_PER_TRACK++;
2603                         tracksize += ssize;
2604                 }
2605                 max_sector = HEAD * _floppy->sect + tracksize;
2606         } else if (!TRACK && !HEAD && !(_floppy->rate & FD_2M) && probing) {
2607                 max_sector = _floppy->sect;
2608         } else if (!HEAD && CT(COMMAND) == FD_WRITE) {
2609                 /* for virtual DMA bug workaround */
2610                 max_sector = _floppy->sect;
2611         }
2612
2613         in_sector_offset = (fsector_t % _floppy->sect) % ssize;
2614         aligned_sector_t = fsector_t - in_sector_offset;
2615         max_size = blk_rq_sectors(current_req);
2616         if ((raw_cmd->track == buffer_track) &&
2617             (current_drive == buffer_drive) &&
2618             (fsector_t >= buffer_min) && (fsector_t < buffer_max)) {
2619                 /* data already in track buffer */
2620                 if (CT(COMMAND) == FD_READ) {
2621                         copy_buffer(1, max_sector, buffer_max);
2622                         return 1;
2623                 }
2624         } else if (in_sector_offset || blk_rq_sectors(current_req) < ssize) {
2625                 if (CT(COMMAND) == FD_WRITE) {
2626                         unsigned int sectors;
2627
2628                         sectors = fsector_t + blk_rq_sectors(current_req);
2629                         if (sectors > ssize && sectors < ssize + ssize)
2630                                 max_size = ssize + ssize;
2631                         else
2632                                 max_size = ssize;
2633                 }
2634                 raw_cmd->flags &= ~FD_RAW_WRITE;
2635                 raw_cmd->flags |= FD_RAW_READ;
2636                 COMMAND = FM_MODE(_floppy, FD_READ);
2637         } else if ((unsigned long)current_req->buffer < MAX_DMA_ADDRESS) {
2638                 unsigned long dma_limit;
2639                 int direct, indirect;
2640
2641                 indirect =
2642                     transfer_size(ssize, max_sector,
2643                                   max_buffer_sectors * 2) - fsector_t;
2644
2645                 /*
2646                  * Do NOT use minimum() here---MAX_DMA_ADDRESS is 64 bits wide
2647                  * on a 64 bit machine!
2648                  */
2649                 max_size = buffer_chain_size();
2650                 dma_limit = (MAX_DMA_ADDRESS -
2651                              ((unsigned long)current_req->buffer)) >> 9;
2652                 if ((unsigned long)max_size > dma_limit)
2653                         max_size = dma_limit;
2654                 /* 64 kb boundaries */
2655                 if (CROSS_64KB(current_req->buffer, max_size << 9))
2656                         max_size = (K_64 -
2657                                     ((unsigned long)current_req->buffer) %
2658                                     K_64) >> 9;
2659                 direct = transfer_size(ssize, max_sector, max_size) - fsector_t;
2660                 /*
2661                  * We try to read tracks, but if we get too many errors, we
2662                  * go back to reading just one sector at a time.
2663                  *
2664                  * This means we should be able to read a sector even if there
2665                  * are other bad sectors on this track.
2666                  */
2667                 if (!direct ||
2668                     (indirect * 2 > direct * 3 &&
2669                      *errors < DP->max_errors.read_track &&
2670                      ((!probing ||
2671                        (DP->read_track & (1 << DRS->probed_format)))))) {
2672                         max_size = blk_rq_sectors(current_req);
2673                 } else {
2674                         raw_cmd->kernel_data = current_req->buffer;
2675                         raw_cmd->length = current_count_sectors << 9;
2676                         if (raw_cmd->length == 0) {
2677                                 DPRINT("%s: zero dma transfer attempted\n", __func__);
2678                                 DPRINT("indirect=%d direct=%d fsector_t=%d\n",
2679                                        indirect, direct, fsector_t);
2680                                 return 0;
2681                         }
2682                         virtualdmabug_workaround();
2683                         return 2;
2684                 }
2685         }
2686
2687         if (CT(COMMAND) == FD_READ)
2688                 max_size = max_sector;  /* unbounded */
2689
2690         /* claim buffer track if needed */
2691         if (buffer_track != raw_cmd->track ||   /* bad track */
2692             buffer_drive != current_drive ||    /* bad drive */
2693             fsector_t > buffer_max ||
2694             fsector_t < buffer_min ||
2695             ((CT(COMMAND) == FD_READ ||
2696               (!in_sector_offset && blk_rq_sectors(current_req) >= ssize)) &&
2697              max_sector > 2 * max_buffer_sectors + buffer_min &&
2698              max_size + fsector_t > 2 * max_buffer_sectors + buffer_min)) {
2699                 /* not enough space */
2700                 buffer_track = -1;
2701                 buffer_drive = current_drive;
2702                 buffer_max = buffer_min = aligned_sector_t;
2703         }
2704         raw_cmd->kernel_data = floppy_track_buffer +
2705                 ((aligned_sector_t - buffer_min) << 9);
2706
2707         if (CT(COMMAND) == FD_WRITE) {
2708                 /* copy write buffer to track buffer.
2709                  * if we get here, we know that the write
2710                  * is either aligned or the data already in the buffer
2711                  * (buffer will be overwritten) */
2712                 if (in_sector_offset && buffer_track == -1)
2713                         DPRINT("internal error offset !=0 on write\n");
2714                 buffer_track = raw_cmd->track;
2715                 buffer_drive = current_drive;
2716                 copy_buffer(ssize, max_sector,
2717                             2 * max_buffer_sectors + buffer_min);
2718         } else
2719                 transfer_size(ssize, max_sector,
2720                               2 * max_buffer_sectors + buffer_min -
2721                               aligned_sector_t);
2722
2723         /* round up current_count_sectors to get dma xfer size */
2724         raw_cmd->length = in_sector_offset + current_count_sectors;
2725         raw_cmd->length = ((raw_cmd->length - 1) | (ssize - 1)) + 1;
2726         raw_cmd->length <<= 9;
2727         if ((raw_cmd->length < current_count_sectors << 9) ||
2728             (raw_cmd->kernel_data != current_req->buffer &&
2729              CT(COMMAND) == FD_WRITE &&
2730              (aligned_sector_t + (raw_cmd->length >> 9) > buffer_max ||
2731               aligned_sector_t < buffer_min)) ||
2732             raw_cmd->length % (128 << SIZECODE) ||
2733             raw_cmd->length <= 0 || current_count_sectors <= 0) {
2734                 DPRINT("fractionary current count b=%lx s=%lx\n",
2735                        raw_cmd->length, current_count_sectors);
2736                 if (raw_cmd->kernel_data != current_req->buffer)
2737                         pr_info("addr=%d, length=%ld\n",
2738                                 (int)((raw_cmd->kernel_data -
2739                                        floppy_track_buffer) >> 9),
2740                                 current_count_sectors);
2741                 pr_info("st=%d ast=%d mse=%d msi=%d\n",
2742                         fsector_t, aligned_sector_t, max_sector, max_size);
2743                 pr_info("ssize=%x SIZECODE=%d\n", ssize, SIZECODE);
2744                 pr_info("command=%x SECTOR=%d HEAD=%d, TRACK=%d\n",
2745                         COMMAND, SECTOR, HEAD, TRACK);
2746                 pr_info("buffer drive=%d\n", buffer_drive);
2747                 pr_info("buffer track=%d\n", buffer_track);
2748                 pr_info("buffer_min=%d\n", buffer_min);
2749                 pr_info("buffer_max=%d\n", buffer_max);
2750                 return 0;
2751         }
2752
2753         if (raw_cmd->kernel_data != current_req->buffer) {
2754                 if (raw_cmd->kernel_data < floppy_track_buffer ||
2755                     current_count_sectors < 0 ||
2756                     raw_cmd->length < 0 ||
2757                     raw_cmd->kernel_data + raw_cmd->length >
2758                     floppy_track_buffer + (max_buffer_sectors << 10)) {
2759                         DPRINT("buffer overrun in schedule dma\n");
2760                         pr_info("fsector_t=%d buffer_min=%d current_count=%ld\n",
2761                                 fsector_t, buffer_min, raw_cmd->length >> 9);
2762                         pr_info("current_count_sectors=%ld\n",
2763                                 current_count_sectors);
2764                         if (CT(COMMAND) == FD_READ)
2765                                 pr_info("read\n");
2766                         if (CT(COMMAND) == FD_WRITE)
2767                                 pr_info("write\n");
2768                         return 0;
2769                 }
2770         } else if (raw_cmd->length > blk_rq_bytes(current_req) ||
2771                    current_count_sectors > blk_rq_sectors(current_req)) {
2772                 DPRINT("buffer overrun in direct transfer\n");
2773                 return 0;
2774         } else if (raw_cmd->length < current_count_sectors << 9) {
2775                 DPRINT("more sectors than bytes\n");
2776                 pr_info("bytes=%ld\n", raw_cmd->length >> 9);
2777                 pr_info("sectors=%ld\n", current_count_sectors);
2778         }
2779         if (raw_cmd->length == 0) {
2780                 DPRINT("zero dma transfer attempted from make_raw_request\n");
2781                 return 0;
2782         }
2783
2784         virtualdmabug_workaround();
2785         return 2;
2786 }
2787
2788 /*
2789  * Round-robin between our available drives, doing one request from each
2790  */
2791 static int set_next_request(void)
2792 {
2793         struct request_queue *q;
2794         int old_pos = fdc_queue;
2795
2796         do {
2797                 q = disks[fdc_queue]->queue;
2798                 if (++fdc_queue == N_DRIVE)
2799                         fdc_queue = 0;
2800                 if (q) {
2801                         current_req = blk_fetch_request(q);
2802                         if (current_req)
2803                                 break;
2804                 }
2805         } while (fdc_queue != old_pos);
2806
2807         return current_req != NULL;
2808 }
2809
2810 static void redo_fd_request(void)
2811 {
2812         int drive;
2813         int tmp;
2814
2815         lastredo = jiffies;
2816         if (current_drive < N_DRIVE)
2817                 floppy_off(current_drive);
2818
2819 do_request:
2820         if (!current_req) {
2821                 int pending;
2822
2823                 spin_lock_irq(&floppy_lock);
2824                 pending = set_next_request();
2825                 spin_unlock_irq(&floppy_lock);
2826
2827                 if (!pending) {
2828                         do_floppy = NULL;
2829                         unlock_fdc();
2830                         return;
2831                 }
2832         }
2833         drive = (long)current_req->rq_disk->private_data;
2834         set_fdc(drive);
2835         reschedule_timeout(current_reqD, "redo fd request");
2836
2837         set_floppy(drive);
2838         raw_cmd = &default_raw_cmd;
2839         raw_cmd->flags = 0;
2840         if (start_motor(redo_fd_request))
2841                 return;
2842
2843         disk_change(current_drive);
2844         if (test_bit(current_drive, &fake_change) ||
2845             test_bit(FD_DISK_CHANGED_BIT, &DRS->flags)) {
2846                 DPRINT("disk absent or changed during operation\n");
2847                 request_done(0);
2848                 goto do_request;
2849         }
2850         if (!_floppy) { /* Autodetection */
2851                 if (!probing) {
2852                         DRS->probed_format = 0;
2853                         if (next_valid_format()) {
2854                                 DPRINT("no autodetectable formats\n");
2855                                 _floppy = NULL;
2856                                 request_done(0);
2857                                 goto do_request;
2858                         }
2859                 }
2860                 probing = 1;
2861                 _floppy = floppy_type + DP->autodetect[DRS->probed_format];
2862         } else
2863                 probing = 0;
2864         errors = &(current_req->errors);
2865         tmp = make_raw_rw_request();
2866         if (tmp < 2) {
2867                 request_done(tmp);
2868                 goto do_request;
2869         }
2870
2871         if (test_bit(FD_NEED_TWADDLE_BIT, &DRS->flags))
2872                 twaddle();
2873         schedule_bh(floppy_start);
2874         debugt(__func__, "queue fd request");
2875         return;
2876 }
2877
2878 static const struct cont_t rw_cont = {
2879         .interrupt      = rw_interrupt,
2880         .redo           = redo_fd_request,
2881         .error          = bad_flp_intr,
2882         .done           = request_done
2883 };
2884
2885 static void process_fd_request(void)
2886 {
2887         cont = &rw_cont;
2888         schedule_bh(redo_fd_request);
2889 }
2890
2891 static void do_fd_request(struct request_queue *q)
2892 {
2893         if (WARN(max_buffer_sectors == 0,
2894                  "VFS: %s called on non-open device\n", __func__))
2895                 return;
2896
2897         if (WARN(atomic_read(&usage_count) == 0,
2898                  "warning: usage count=0, current_req=%p sect=%ld type=%x flags=%x\n",
2899                  current_req, (long)blk_rq_pos(current_req), current_req->cmd_type,
2900                  current_req->cmd_flags))
2901                 return;
2902
2903         if (test_bit(0, &fdc_busy)) {
2904                 /* fdc busy, this new request will be treated when the
2905                    current one is done */
2906                 is_alive(__func__, "old request running");
2907                 return;
2908         }
2909         lock_fdc(MAXTIMEOUT, false);
2910         process_fd_request();
2911         is_alive(__func__, "");
2912 }
2913
2914 static const struct cont_t poll_cont = {
2915         .interrupt      = success_and_wakeup,
2916         .redo           = floppy_ready,
2917         .error          = generic_failure,
2918         .done           = generic_done
2919 };
2920
2921 static int poll_drive(bool interruptible, int flag)
2922 {
2923         /* no auto-sense, just clear dcl */
2924         raw_cmd = &default_raw_cmd;
2925         raw_cmd->flags = flag;
2926         raw_cmd->track = 0;
2927         raw_cmd->cmd_count = 0;
2928         cont = &poll_cont;
2929         debug_dcl(DP->flags, "setting NEWCHANGE in poll_drive\n");
2930         set_bit(FD_DISK_NEWCHANGE_BIT, &DRS->flags);
2931
2932         return wait_til_done(floppy_ready, interruptible);
2933 }
2934
2935 /*
2936  * User triggered reset
2937  * ====================
2938  */
2939
2940 static void reset_intr(void)
2941 {
2942         pr_info("weird, reset interrupt called\n");
2943 }
2944
2945 static const struct cont_t reset_cont = {
2946         .interrupt      = reset_intr,
2947         .redo           = success_and_wakeup,
2948         .error          = generic_failure,
2949         .done           = generic_done
2950 };
2951
2952 static int user_reset_fdc(int drive, int arg, bool interruptible)
2953 {
2954         int ret;
2955
2956         if (lock_fdc(drive, interruptible))
2957                 return -EINTR;
2958
2959         if (arg == FD_RESET_ALWAYS)
2960                 FDCS->reset = 1;
2961         if (FDCS->reset) {
2962                 cont = &reset_cont;
2963                 ret = wait_til_done(reset_fdc, interruptible);
2964                 if (ret == -EINTR)
2965                         return -EINTR;
2966         }
2967         process_fd_request();
2968         return 0;
2969 }
2970
2971 /*
2972  * Misc Ioctl's and support
2973  * ========================
2974  */
2975 static inline int fd_copyout(void __user *param, const void *address,
2976                              unsigned long size)
2977 {
2978         return copy_to_user(param, address, size) ? -EFAULT : 0;
2979 }
2980
2981 static inline int fd_copyin(void __user *param, void *address,
2982                             unsigned long size)
2983 {
2984         return copy_from_user(address, param, size) ? -EFAULT : 0;
2985 }
2986
2987 static const char *drive_name(int type, int drive)
2988 {
2989         struct floppy_struct *floppy;
2990
2991         if (type)
2992                 floppy = floppy_type + type;
2993         else {
2994                 if (UDP->native_format)
2995                         floppy = floppy_type + UDP->native_format;
2996                 else
2997                         return "(null)";
2998         }
2999         if (floppy->name)
3000                 return floppy->name;
3001         else
3002                 return "(null)";
3003 }
3004
3005 /* raw commands */
3006 static void raw_cmd_done(int flag)
3007 {
3008         int i;
3009
3010         if (!flag) {
3011                 raw_cmd->flags |= FD_RAW_FAILURE;
3012                 raw_cmd->flags |= FD_RAW_HARDFAILURE;
3013         } else {
3014                 raw_cmd->reply_count = inr;
3015                 if (raw_cmd->reply_count > MAX_REPLIES)
3016                         raw_cmd->reply_count = 0;
3017                 for (i = 0; i < raw_cmd->reply_count; i++)
3018                         raw_cmd->reply[i] = reply_buffer[i];
3019
3020                 if (raw_cmd->flags & (FD_RAW_READ | FD_RAW_WRITE)) {
3021                         unsigned long flags;
3022                         flags = claim_dma_lock();
3023                         raw_cmd->length = fd_get_dma_residue();
3024                         release_dma_lock(flags);
3025                 }
3026
3027                 if ((raw_cmd->flags & FD_RAW_SOFTFAILURE) &&
3028                     (!raw_cmd->reply_count || (raw_cmd->reply[0] & 0xc0)))
3029                         raw_cmd->flags |= FD_RAW_FAILURE;
3030
3031                 if (disk_change(current_drive))
3032                         raw_cmd->flags |= FD_RAW_DISK_CHANGE;
3033                 else
3034                         raw_cmd->flags &= ~FD_RAW_DISK_CHANGE;
3035                 if (raw_cmd->flags & FD_RAW_NO_MOTOR_AFTER)
3036                         motor_off_callback(current_drive);
3037
3038                 if (raw_cmd->next &&
3039                     (!(raw_cmd->flags & FD_RAW_FAILURE) ||
3040                      !(raw_cmd->flags & FD_RAW_STOP_IF_FAILURE)) &&
3041                     ((raw_cmd->flags & FD_RAW_FAILURE) ||
3042                      !(raw_cmd->flags & FD_RAW_STOP_IF_SUCCESS))) {
3043                         raw_cmd = raw_cmd->next;
3044                         return;
3045                 }
3046         }
3047         generic_done(flag);
3048 }
3049
3050 static const struct cont_t raw_cmd_cont = {
3051         .interrupt      = success_and_wakeup,
3052         .redo           = floppy_start,
3053         .error          = generic_failure,
3054         .done           = raw_cmd_done
3055 };
3056
3057 static int raw_cmd_copyout(int cmd, void __user *param,
3058                                   struct floppy_raw_cmd *ptr)
3059 {
3060         int ret;
3061
3062         while (ptr) {
3063                 struct floppy_raw_cmd cmd = *ptr;
3064                 cmd.next = NULL;
3065                 cmd.kernel_data = NULL;
3066                 ret = copy_to_user(param, &cmd, sizeof(cmd));
3067                 if (ret)
3068                         return -EFAULT;
3069                 param += sizeof(struct floppy_raw_cmd);
3070                 if ((ptr->flags & FD_RAW_READ) && ptr->buffer_length) {
3071                         if (ptr->length >= 0 &&
3072                             ptr->length <= ptr->buffer_length) {
3073                                 long length = ptr->buffer_length - ptr->length;
3074                                 ret = fd_copyout(ptr->data, ptr->kernel_data,
3075                                                  length);
3076                                 if (ret)
3077                                         return ret;
3078                         }
3079                 }
3080                 ptr = ptr->next;
3081         }
3082
3083         return 0;
3084 }
3085
3086 static void raw_cmd_free(struct floppy_raw_cmd **ptr)
3087 {
3088         struct floppy_raw_cmd *next;
3089         struct floppy_raw_cmd *this;
3090
3091         this = *ptr;
3092         *ptr = NULL;
3093         while (this) {
3094                 if (this->buffer_length) {
3095                         fd_dma_mem_free((unsigned long)this->kernel_data,
3096                                         this->buffer_length);
3097                         this->buffer_length = 0;
3098                 }
3099                 next = this->next;
3100                 kfree(this);
3101                 this = next;
3102         }
3103 }
3104
3105 static int raw_cmd_copyin(int cmd, void __user *param,
3106                                  struct floppy_raw_cmd **rcmd)
3107 {
3108         struct floppy_raw_cmd *ptr;
3109         int ret;
3110         int i;
3111
3112         *rcmd = NULL;
3113
3114 loop:
3115         ptr = kmalloc(sizeof(struct floppy_raw_cmd), GFP_USER);
3116         if (!ptr)
3117                 return -ENOMEM;
3118         *rcmd = ptr;
3119         ret = copy_from_user(ptr, param, sizeof(*ptr));
3120         ptr->next = NULL;
3121         ptr->buffer_length = 0;
3122         ptr->kernel_data = NULL;
3123         if (ret)
3124                 return -EFAULT;
3125         param += sizeof(struct floppy_raw_cmd);
3126         if (ptr->cmd_count > 33)
3127                         /* the command may now also take up the space
3128                          * initially intended for the reply & the
3129                          * reply count. Needed for long 82078 commands
3130                          * such as RESTORE, which takes ... 17 command
3131                          * bytes. Murphy's law #137: When you reserve
3132                          * 16 bytes for a structure, you'll one day
3133                          * discover that you really need 17...
3134                          */
3135                 return -EINVAL;
3136
3137         for (i = 0; i < 16; i++)
3138                 ptr->reply[i] = 0;
3139         ptr->resultcode = 0;
3140
3141         if (ptr->flags & (FD_RAW_READ | FD_RAW_WRITE)) {
3142                 if (ptr->length <= 0)
3143                         return -EINVAL;
3144                 ptr->kernel_data = (char *)fd_dma_mem_alloc(ptr->length);
3145                 fallback_on_nodma_alloc(&ptr->kernel_data, ptr->length);
3146                 if (!ptr->kernel_data)
3147                         return -ENOMEM;
3148                 ptr->buffer_length = ptr->length;
3149         }
3150         if (ptr->flags & FD_RAW_WRITE) {
3151                 ret = fd_copyin(ptr->data, ptr->kernel_data, ptr->length);
3152                 if (ret)
3153                         return ret;
3154         }
3155
3156         if (ptr->flags & FD_RAW_MORE) {
3157                 rcmd = &(ptr->next);
3158                 ptr->rate &= 0x43;
3159                 goto loop;
3160         }
3161
3162         return 0;
3163 }
3164
3165 static int raw_cmd_ioctl(int cmd, void __user *param)
3166 {
3167         struct floppy_raw_cmd *my_raw_cmd;
3168         int drive;
3169         int ret2;
3170         int ret;
3171
3172         if (FDCS->rawcmd <= 1)
3173                 FDCS->rawcmd = 1;
3174         for (drive = 0; drive < N_DRIVE; drive++) {
3175                 if (FDC(drive) != fdc)
3176                         continue;
3177                 if (drive == current_drive) {
3178                         if (UDRS->fd_ref > 1) {
3179                                 FDCS->rawcmd = 2;
3180                                 break;
3181                         }
3182                 } else if (UDRS->fd_ref) {
3183                         FDCS->rawcmd = 2;
3184                         break;
3185                 }
3186         }
3187
3188         if (FDCS->reset)
3189                 return -EIO;
3190
3191         ret = raw_cmd_copyin(cmd, param, &my_raw_cmd);
3192         if (ret) {
3193                 raw_cmd_free(&my_raw_cmd);
3194                 return ret;
3195         }
3196
3197         raw_cmd = my_raw_cmd;
3198         cont = &raw_cmd_cont;
3199         ret = wait_til_done(floppy_start, true);
3200         debug_dcl(DP->flags, "calling disk change from raw_cmd ioctl\n");
3201
3202         if (ret != -EINTR && FDCS->reset)
3203                 ret = -EIO;
3204
3205         DRS->track = NO_TRACK;
3206
3207         ret2 = raw_cmd_copyout(cmd, param, my_raw_cmd);
3208         if (!ret)
3209                 ret = ret2;
3210         raw_cmd_free(&my_raw_cmd);
3211         return ret;
3212 }
3213
3214 static int invalidate_drive(struct block_device *bdev)
3215 {
3216         /* invalidate the buffer track to force a reread */
3217         set_bit((long)bdev->bd_disk->private_data, &fake_change);
3218         process_fd_request();
3219         check_disk_change(bdev);
3220         return 0;
3221 }
3222
3223 static int set_geometry(unsigned int cmd, struct floppy_struct *g,
3224                                int drive, int type, struct block_device *bdev)
3225 {
3226         int cnt;
3227
3228         /* sanity checking for parameters. */
3229         if (g->sect <= 0 ||
3230             g->head <= 0 ||
3231             g->track <= 0 || g->track > UDP->tracks >> STRETCH(g) ||
3232             /* check if reserved bits are set */
3233             (g->stretch & ~(FD_STRETCH | FD_SWAPSIDES | FD_SECTBASEMASK)) != 0)
3234                 return -EINVAL;
3235         if (type) {
3236                 if (!capable(CAP_SYS_ADMIN))
3237                         return -EPERM;
3238                 mutex_lock(&open_lock);
3239                 if (lock_fdc(drive, true)) {
3240                         mutex_unlock(&open_lock);
3241                         return -EINTR;
3242                 }
3243                 floppy_type[type] = *g;
3244                 floppy_type[type].name = "user format";
3245                 for (cnt = type << 2; cnt < (type << 2) + 4; cnt++)
3246                         floppy_sizes[cnt] = floppy_sizes[cnt + 0x80] =
3247                             floppy_type[type].size + 1;
3248                 process_fd_request();
3249                 for (cnt = 0; cnt < N_DRIVE; cnt++) {
3250                         struct block_device *bdev = opened_bdev[cnt];
3251                         if (!bdev || ITYPE(drive_state[cnt].fd_device) != type)
3252                                 continue;
3253                         __invalidate_device(bdev, true);
3254                 }
3255                 mutex_unlock(&open_lock);
3256         } else {
3257                 int oldStretch;
3258
3259                 if (lock_fdc(drive, true))
3260                         return -EINTR;
3261                 if (cmd != FDDEFPRM) {
3262                         /* notice a disk change immediately, else
3263                          * we lose our settings immediately*/
3264                         if (poll_drive(true, FD_RAW_NEED_DISK) == -EINTR)
3265                                 return -EINTR;
3266                 }
3267                 oldStretch = g->stretch;
3268                 user_params[drive] = *g;
3269                 if (buffer_drive == drive)
3270                         SUPBOUND(buffer_max, user_params[drive].sect);
3271                 current_type[drive] = &user_params[drive];
3272                 floppy_sizes[drive] = user_params[drive].size;
3273                 if (cmd == FDDEFPRM)
3274                         DRS->keep_data = -1;
3275                 else
3276                         DRS->keep_data = 1;
3277                 /* invalidation. Invalidate only when needed, i.e.
3278                  * when there are already sectors in the buffer cache
3279                  * whose number will change. This is useful, because
3280                  * mtools often changes the geometry of the disk after
3281                  * looking at the boot block */
3282                 if (DRS->maxblock > user_params[drive].sect ||
3283                     DRS->maxtrack ||
3284                     ((user_params[drive].sect ^ oldStretch) &
3285                      (FD_SWAPSIDES | FD_SECTBASEMASK)))
3286                         invalidate_drive(bdev);
3287                 else
3288                         process_fd_request();
3289         }
3290         return 0;
3291 }
3292
3293 /* handle obsolete ioctl's */
3294 static unsigned int ioctl_table[] = {
3295         FDCLRPRM,
3296         FDSETPRM,
3297         FDDEFPRM,
3298         FDGETPRM,
3299         FDMSGON,
3300         FDMSGOFF,
3301         FDFMTBEG,
3302         FDFMTTRK,
3303         FDFMTEND,
3304         FDSETEMSGTRESH,
3305         FDFLUSH,
3306         FDSETMAXERRS,
3307         FDGETMAXERRS,
3308         FDGETDRVTYP,
3309         FDSETDRVPRM,
3310         FDGETDRVPRM,
3311         FDGETDRVSTAT,
3312         FDPOLLDRVSTAT,
3313         FDRESET,
3314         FDGETFDCSTAT,
3315         FDWERRORCLR,
3316         FDWERRORGET,
3317         FDRAWCMD,
3318         FDEJECT,
3319         FDTWADDLE
3320 };
3321
3322 static int normalize_ioctl(unsigned int *cmd, int *size)
3323 {
3324         int i;
3325
3326         for (i = 0; i < ARRAY_SIZE(ioctl_table); i++) {
3327                 if ((*cmd & 0xffff) == (ioctl_table[i] & 0xffff)) {
3328                         *size = _IOC_SIZE(*cmd);
3329                         *cmd = ioctl_table[i];
3330                         if (*size > _IOC_SIZE(*cmd)) {
3331                                 pr_info("ioctl not yet supported\n");
3332                                 return -EFAULT;
3333                         }
3334                         return 0;
3335                 }
3336         }
3337         return -EINVAL;
3338 }
3339
3340 static int get_floppy_geometry(int drive, int type, struct floppy_struct **g)
3341 {
3342         if (type)
3343                 *g = &floppy_type[type];
3344         else {
3345                 if (lock_fdc(drive, false))
3346                         return -EINTR;
3347                 if (poll_drive(false, 0) == -EINTR)
3348                         return -EINTR;
3349                 process_fd_request();
3350                 *g = current_type[drive];
3351         }
3352         if (!*g)
3353                 return -ENODEV;
3354         return 0;
3355 }
3356
3357 static int fd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
3358 {
3359         int drive = (long)bdev->bd_disk->private_data;
3360         int type = ITYPE(drive_state[drive].fd_device);
3361         struct floppy_struct *g;
3362         int ret;
3363
3364         ret = get_floppy_geometry(drive, type, &g);
3365         if (ret)
3366                 return ret;
3367
3368         geo->heads = g->head;
3369         geo->sectors = g->sect;
3370         geo->cylinders = g->track;
3371         return 0;
3372 }
3373
3374 static int fd_locked_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd,
3375                     unsigned long param)
3376 {
3377         int drive = (long)bdev->bd_disk->private_data;
3378         int type = ITYPE(UDRS->fd_device);
3379         int i;
3380         int ret;
3381         int size;
3382         union inparam {
3383                 struct floppy_struct g; /* geometry */
3384                 struct format_descr f;
3385                 struct floppy_max_errors max_errors;
3386                 struct floppy_drive_params dp;
3387         } inparam;              /* parameters coming from user space */
3388         const void *outparam;   /* parameters passed back to user space */
3389
3390         /* convert compatibility eject ioctls into floppy eject ioctl.
3391          * We do this in order to provide a means to eject floppy disks before
3392          * installing the new fdutils package */
3393         if (cmd == CDROMEJECT ||        /* CD-ROM eject */
3394             cmd == 0x6470) {            /* SunOS floppy eject */
3395                 DPRINT("obsolete eject ioctl\n");
3396                 DPRINT("please use floppycontrol --eject\n");
3397                 cmd = FDEJECT;
3398         }
3399
3400         if (!((cmd & 0xff00) == 0x0200))
3401                 return -EINVAL;
3402
3403         /* convert the old style command into a new style command */
3404         ret = normalize_ioctl(&cmd, &size);
3405         if (ret)
3406                 return ret;
3407
3408         /* permission checks */
3409         if (((cmd & 0x40) && !(mode & (FMODE_WRITE | FMODE_WRITE_IOCTL))) ||
3410             ((cmd & 0x80) && !capable(CAP_SYS_ADMIN)))
3411                 return -EPERM;
3412
3413         if (WARN_ON(size < 0 || size > sizeof(inparam)))
3414                 return -EINVAL;
3415
3416         /* copyin */
3417         memset(&inparam, 0, sizeof(inparam));
3418         if (_IOC_DIR(cmd) & _IOC_WRITE) {
3419                 ret = fd_copyin((void __user *)param, &inparam, size);
3420                 if (ret)
3421                         return ret;
3422         }
3423
3424         switch (cmd) {
3425         case FDEJECT:
3426                 if (UDRS->fd_ref != 1)
3427                         /* somebody else has this drive open */
3428                         return -EBUSY;
3429                 if (lock_fdc(drive, true))
3430                         return -EINTR;
3431
3432                 /* do the actual eject. Fails on
3433                  * non-Sparc architectures */
3434                 ret = fd_eject(UNIT(drive));
3435
3436                 set_bit(FD_DISK_CHANGED_BIT, &UDRS->flags);
3437                 set_bit(FD_VERIFY_BIT, &UDRS->flags);
3438                 process_fd_request();
3439                 return ret;
3440         case FDCLRPRM:
3441                 if (lock_fdc(drive, true))
3442                         return -EINTR;
3443                 current_type[drive] = NULL;
3444                 floppy_sizes[drive] = MAX_DISK_SIZE << 1;
3445                 UDRS->keep_data = 0;
3446                 return invalidate_drive(bdev);
3447         case FDSETPRM:
3448         case FDDEFPRM:
3449                 return set_geometry(cmd, &inparam.g, drive, type, bdev);
3450         case FDGETPRM:
3451                 ret = get_floppy_geometry(drive, type,
3452                                           (struct floppy_struct **)&outparam);
3453                 if (ret)
3454                         return ret;
3455                 break;
3456         case FDMSGON:
3457                 UDP->flags |= FTD_MSG;
3458                 return 0;
3459         case FDMSGOFF:
3460                 UDP->flags &= ~FTD_MSG;
3461                 return 0;
3462         case FDFMTBEG:
3463                 if (lock_fdc(drive, true))
3464                         return -EINTR;
3465                 if (poll_drive(true, FD_RAW_NEED_DISK) == -EINTR)
3466                         return -EINTR;
3467                 ret = UDRS->flags;
3468                 process_fd_request();
3469                 if (ret & FD_VERIFY)
3470                         return -ENODEV;
3471                 if (!(ret & FD_DISK_WRITABLE))
3472                         return -EROFS;
3473                 return 0;
3474         case FDFMTTRK:
3475                 if (UDRS->fd_ref != 1)
3476                         return -EBUSY;
3477                 return do_format(drive, &inparam.f);
3478         case FDFMTEND:
3479         case FDFLUSH:
3480                 if (lock_fdc(drive, true))
3481                         return -EINTR;
3482                 return invalidate_drive(bdev);
3483         case FDSETEMSGTRESH:
3484                 UDP->max_errors.reporting = (unsigned short)(param & 0x0f);
3485                 return 0;
3486         case FDGETMAXERRS:
3487                 outparam = &UDP->max_errors;
3488                 break;
3489         case FDSETMAXERRS:
3490                 UDP->max_errors = inparam.max_errors;
3491                 break;
3492         case FDGETDRVTYP:
3493                 outparam = drive_name(type, drive);
3494                 SUPBOUND(size, strlen((const char *)outparam) + 1);
3495                 break;
3496         case FDSETDRVPRM:
3497                 *UDP = inparam.dp;
3498                 break;
3499         case FDGETDRVPRM:
3500                 outparam = UDP;
3501                 break;
3502         case FDPOLLDRVSTAT:
3503                 if (lock_fdc(drive, true))
3504                         return -EINTR;
3505                 if (poll_drive(true, FD_RAW_NEED_DISK) == -EINTR)
3506                         return -EINTR;
3507                 process_fd_request();
3508                 /* fall through */
3509         case FDGETDRVSTAT:
3510                 outparam = UDRS;
3511                 break;
3512         case FDRESET:
3513                 return user_reset_fdc(drive, (int)param, true);
3514         case FDGETFDCSTAT:
3515                 outparam = UFDCS;
3516                 break;
3517         case FDWERRORCLR:
3518                 memset(UDRWE, 0, sizeof(*UDRWE));
3519                 return 0;
3520         case FDWERRORGET:
3521                 outparam = UDRWE;
3522                 break;
3523         case FDRAWCMD:
3524                 if (type)
3525                         return -EINVAL;
3526                 if (lock_fdc(drive, true))
3527                         return -EINTR;
3528                 set_floppy(drive);
3529                 i = raw_cmd_ioctl(cmd, (void __user *)param);
3530                 if (i == -EINTR)
3531                         return -EINTR;
3532                 process_fd_request();
3533                 return i;
3534         case FDTWADDLE:
3535                 if (lock_fdc(drive, true))
3536                         return -EINTR;
3537                 twaddle();
3538                 process_fd_request();
3539                 return 0;
3540         default:
3541                 return -EINVAL;
3542         }
3543
3544         if (_IOC_DIR(cmd) & _IOC_READ)
3545                 return fd_copyout((void __user *)param, outparam, size);
3546
3547         return 0;
3548 }
3549
3550 static int fd_ioctl(struct block_device *bdev, fmode_t mode,
3551                              unsigned int cmd, unsigned long param)
3552 {
3553         int ret;
3554
3555         mutex_lock(&floppy_mutex);
3556         ret = fd_locked_ioctl(bdev, mode, cmd, param);
3557         mutex_unlock(&floppy_mutex);
3558
3559         return ret;
3560 }
3561
3562 static void __init config_types(void)
3563 {
3564         bool has_drive = false;
3565         int drive;
3566
3567         /* read drive info out of physical CMOS */
3568         drive = 0;
3569         if (!UDP->cmos)
3570                 UDP->cmos = FLOPPY0_TYPE;
3571         drive = 1;
3572         if (!UDP->cmos && FLOPPY1_TYPE)
3573                 UDP->cmos = FLOPPY1_TYPE;
3574
3575         /* FIXME: additional physical CMOS drive detection should go here */
3576
3577         for (drive = 0; drive < N_DRIVE; drive++) {
3578                 unsigned int type = UDP->cmos;
3579                 struct floppy_drive_params *params;
3580                 const char *name = NULL;
3581                 static char temparea[32];
3582
3583                 if (type < ARRAY_SIZE(default_drive_params)) {
3584                         params = &default_drive_params[type].params;
3585                         if (type) {
3586                                 name = default_drive_params[type].name;
3587                                 allowed_drive_mask |= 1 << drive;
3588                         } else
3589                                 allowed_drive_mask &= ~(1 << drive);
3590                 } else {
3591                         params = &default_drive_params[0].params;
3592                         sprintf(temparea, "unknown type %d (usb?)", type);
3593                         name = temparea;
3594                 }
3595                 if (name) {
3596                         const char *prepend;
3597                         if (!has_drive) {
3598                                 prepend = "";
3599                                 has_drive = true;
3600                                 pr_info("Floppy drive(s):");
3601                         } else {
3602                                 prepend = ",";
3603                         }
3604
3605                         pr_cont("%s fd%d is %s", prepend, drive, name);
3606                 }
3607                 *UDP = *params;
3608         }
3609
3610         if (has_drive)
3611                 pr_cont("\n");
3612 }
3613
3614 static int floppy_release(struct gendisk *disk, fmode_t mode)
3615 {
3616         int drive = (long)disk->private_data;
3617
3618         mutex_lock(&floppy_mutex);
3619         mutex_lock(&open_lock);
3620         if (UDRS->fd_ref < 0)
3621                 UDRS->fd_ref = 0;
3622         else if (!UDRS->fd_ref--) {
3623                 DPRINT("floppy_release with fd_ref == 0");
3624                 UDRS->fd_ref = 0;
3625         }
3626         if (!UDRS->fd_ref)
3627                 opened_bdev[drive] = NULL;
3628         mutex_unlock(&open_lock);
3629         mutex_unlock(&floppy_mutex);
3630
3631         return 0;
3632 }
3633
3634 /*
3635  * floppy_open check for aliasing (/dev/fd0 can be the same as
3636  * /dev/PS0 etc), and disallows simultaneous access to the same
3637  * drive with different device numbers.
3638  */
3639 static int floppy_open(struct block_device *bdev, fmode_t mode)
3640 {
3641         int drive = (long)bdev->bd_disk->private_data;
3642         int old_dev, new_dev;
3643         int try;
3644         int res = -EBUSY;
3645         char *tmp;
3646
3647         mutex_lock(&floppy_mutex);
3648         mutex_lock(&open_lock);
3649         old_dev = UDRS->fd_device;
3650         if (opened_bdev[drive] && opened_bdev[drive] != bdev)
3651                 goto out2;
3652
3653         if (!UDRS->fd_ref && (UDP->flags & FD_BROKEN_DCL)) {
3654                 set_bit(FD_DISK_CHANGED_BIT, &UDRS->flags);
3655                 set_bit(FD_VERIFY_BIT, &UDRS->flags);
3656         }
3657
3658         if (UDRS->fd_ref == -1 || (UDRS->fd_ref && (mode & FMODE_EXCL)))
3659                 goto out2;
3660
3661         if (mode & FMODE_EXCL)
3662                 UDRS->fd_ref = -1;
3663         else
3664                 UDRS->fd_ref++;
3665
3666         opened_bdev[drive] = bdev;
3667
3668         res = -ENXIO;
3669
3670         if (!floppy_track_buffer) {
3671                 /* if opening an ED drive, reserve a big buffer,
3672                  * else reserve a small one */
3673                 if ((UDP->cmos == 6) || (UDP->cmos == 5))
3674                         try = 64;       /* Only 48 actually useful */
3675                 else
3676                         try = 32;       /* Only 24 actually useful */
3677
3678                 tmp = (char *)fd_dma_mem_alloc(1024 * try);
3679                 if (!tmp && !floppy_track_buffer) {
3680                         try >>= 1;      /* buffer only one side */
3681                         INFBOUND(try, 16);
3682                         tmp = (char *)fd_dma_mem_alloc(1024 * try);
3683                 }
3684                 if (!tmp && !floppy_track_buffer)
3685                         fallback_on_nodma_alloc(&tmp, 2048 * try);
3686                 if (!tmp && !floppy_track_buffer) {
3687                         DPRINT("Unable to allocate DMA memory\n");
3688                         goto out;
3689                 }
3690                 if (floppy_track_buffer) {
3691                         if (tmp)
3692                                 fd_dma_mem_free((unsigned long)tmp, try * 1024);
3693                 } else {
3694                         buffer_min = buffer_max = -1;
3695                         floppy_track_buffer = tmp;
3696                         max_buffer_sectors = try;
3697                 }
3698         }
3699
3700         new_dev = MINOR(bdev->bd_dev);
3701         UDRS->fd_device = new_dev;
3702         set_capacity(disks[drive], floppy_sizes[new_dev]);
3703         if (old_dev != -1 && old_dev != new_dev) {
3704                 if (buffer_drive == drive)
3705                         buffer_track = -1;
3706         }
3707
3708         if (UFDCS->rawcmd == 1)
3709                 UFDCS->rawcmd = 2;
3710
3711         if (!(mode & FMODE_NDELAY)) {
3712                 if (mode & (FMODE_READ|FMODE_WRITE)) {
3713                         UDRS->last_checked = 0;
3714                         check_disk_change(bdev);
3715                         if (test_bit(FD_DISK_CHANGED_BIT, &UDRS->flags))
3716                                 goto out;
3717                 }
3718                 res = -EROFS;
3719                 if ((mode & FMODE_WRITE) &&
3720                     !test_bit(FD_DISK_WRITABLE_BIT, &UDRS->flags))
3721                         goto out;
3722         }
3723         mutex_unlock(&open_lock);
3724         mutex_unlock(&floppy_mutex);
3725         return 0;
3726 out:
3727         if (UDRS->fd_ref < 0)
3728                 UDRS->fd_ref = 0;
3729         else
3730                 UDRS->fd_ref--;
3731         if (!UDRS->fd_ref)
3732                 opened_bdev[drive] = NULL;
3733 out2:
3734         mutex_unlock(&open_lock);
3735         mutex_unlock(&floppy_mutex);
3736         return res;
3737 }
3738
3739 /*
3740  * Check if the disk has been changed or if a change has been faked.
3741  */
3742 static unsigned int floppy_check_events(struct gendisk *disk,
3743                                         unsigned int clearing)
3744 {
3745         int drive = (long)disk->private_data;
3746
3747         if (test_bit(FD_DISK_CHANGED_BIT, &UDRS->flags) ||
3748             test_bit(FD_VERIFY_BIT, &UDRS->flags))
3749                 return DISK_EVENT_MEDIA_CHANGE;
3750
3751         if (time_after(jiffies, UDRS->last_checked + UDP->checkfreq)) {
3752                 lock_fdc(drive, false);
3753                 poll_drive(false, 0);
3754                 process_fd_request();
3755         }
3756
3757         if (test_bit(FD_DISK_CHANGED_BIT, &UDRS->flags) ||
3758             test_bit(FD_VERIFY_BIT, &UDRS->flags) ||
3759             test_bit(drive, &fake_change) ||
3760             drive_no_geom(drive))
3761                 return DISK_EVENT_MEDIA_CHANGE;
3762         return 0;
3763 }
3764
3765 /*
3766  * This implements "read block 0" for floppy_revalidate().
3767  * Needed for format autodetection, checking whether there is
3768  * a disk in the drive, and whether that disk is writable.
3769  */
3770
3771 static void floppy_rb0_complete(struct bio *bio, int err)
3772 {
3773         complete((struct completion *)bio->bi_private);
3774 }
3775
3776 static int __floppy_read_block_0(struct block_device *bdev)
3777 {
3778         struct bio bio;
3779         struct bio_vec bio_vec;
3780         struct completion complete;
3781         struct page *page;
3782         size_t size;
3783
3784         page = alloc_page(GFP_NOIO);
3785         if (!page) {
3786                 process_fd_request();
3787                 return -ENOMEM;
3788         }
3789
3790         size = bdev->bd_block_size;
3791         if (!size)
3792                 size = 1024;
3793
3794         bio_init(&bio);
3795         bio.bi_io_vec = &bio_vec;
3796         bio_vec.bv_page = page;
3797         bio_vec.bv_len = size;
3798         bio_vec.bv_offset = 0;
3799         bio.bi_vcnt = 1;
3800         bio.bi_idx = 0;
3801         bio.bi_size = size;
3802         bio.bi_bdev = bdev;
3803         bio.bi_sector = 0;
3804         bio.bi_flags = BIO_QUIET;
3805         init_completion(&complete);
3806         bio.bi_private = &complete;
3807         bio.bi_end_io = floppy_rb0_complete;
3808
3809         submit_bio(READ, &bio);
3810         process_fd_request();
3811         wait_for_completion(&complete);
3812
3813         __free_page(page);
3814
3815         return 0;
3816 }
3817
3818 /* revalidate the floppy disk, i.e. trigger format autodetection by reading
3819  * the bootblock (block 0). "Autodetection" is also needed to check whether
3820  * there is a disk in the drive at all... Thus we also do it for fixed
3821  * geometry formats */
3822 static int floppy_revalidate(struct gendisk *disk)
3823 {
3824         int drive = (long)disk->private_data;
3825         int cf;
3826         int res = 0;
3827
3828         if (test_bit(FD_DISK_CHANGED_BIT, &UDRS->flags) ||
3829             test_bit(FD_VERIFY_BIT, &UDRS->flags) ||
3830             test_bit(drive, &fake_change) ||
3831             drive_no_geom(drive)) {
3832                 if (WARN(atomic_read(&usage_count) == 0,
3833                          "VFS: revalidate called on non-open device.\n"))
3834                         return -EFAULT;
3835
3836                 lock_fdc(drive, false);
3837                 cf = (test_bit(FD_DISK_CHANGED_BIT, &UDRS->flags) ||
3838                       test_bit(FD_VERIFY_BIT, &UDRS->flags));
3839                 if (!(cf || test_bit(drive, &fake_change) || drive_no_geom(drive))) {
3840                         process_fd_request();   /*already done by another thread */
3841                         return 0;
3842                 }
3843                 UDRS->maxblock = 0;
3844                 UDRS->maxtrack = 0;
3845                 if (buffer_drive == drive)
3846                         buffer_track = -1;
3847                 clear_bit(drive, &fake_change);
3848                 clear_bit(FD_DISK_CHANGED_BIT, &UDRS->flags);
3849                 if (cf)
3850                         UDRS->generation++;
3851                 if (drive_no_geom(drive)) {
3852                         /* auto-sensing */
3853                         res = __floppy_read_block_0(opened_bdev[drive]);
3854                 } else {
3855                         if (cf)
3856                                 poll_drive(false, FD_RAW_NEED_DISK);
3857                         process_fd_request();
3858                 }
3859         }
3860         set_capacity(disk, floppy_sizes[UDRS->fd_device]);
3861         return res;
3862 }
3863
3864 static const struct block_device_operations floppy_fops = {
3865         .owner                  = THIS_MODULE,
3866         .open                   = floppy_open,
3867         .release                = floppy_release,
3868         .ioctl                  = fd_ioctl,
3869         .getgeo                 = fd_getgeo,
3870         .check_events           = floppy_check_events,
3871         .revalidate_disk        = floppy_revalidate,
3872 };
3873
3874 /*
3875  * Floppy Driver initialization
3876  * =============================
3877  */
3878
3879 /* Determine the floppy disk controller type */
3880 /* This routine was written by David C. Niemi */
3881 static char __init get_fdc_version(void)
3882 {
3883         int r;
3884
3885         output_byte(FD_DUMPREGS);       /* 82072 and better know DUMPREGS */
3886         if (FDCS->reset)
3887                 return FDC_NONE;
3888         r = result();
3889         if (r <= 0x00)
3890                 return FDC_NONE;        /* No FDC present ??? */
3891         if ((r == 1) && (reply_buffer[0] == 0x80)) {
3892                 pr_info("FDC %d is an 8272A\n", fdc);
3893                 return FDC_8272A;       /* 8272a/765 don't know DUMPREGS */
3894         }
3895         if (r != 10) {
3896                 pr_info("FDC %d init: DUMPREGS: unexpected return of %d bytes.\n",
3897                         fdc, r);
3898                 return FDC_UNKNOWN;
3899         }
3900
3901         if (!fdc_configure()) {
3902                 pr_info("FDC %d is an 82072\n", fdc);
3903                 return FDC_82072;       /* 82072 doesn't know CONFIGURE */
3904         }
3905
3906         output_byte(FD_PERPENDICULAR);
3907         if (need_more_output() == MORE_OUTPUT) {
3908                 output_byte(0);
3909         } else {
3910                 pr_info("FDC %d is an 82072A\n", fdc);
3911                 return FDC_82072A;      /* 82072A as found on Sparcs. */
3912         }
3913
3914         output_byte(FD_UNLOCK);
3915         r = result();
3916         if ((r == 1) && (reply_buffer[0] == 0x80)) {
3917                 pr_info("FDC %d is a pre-1991 82077\n", fdc);
3918                 return FDC_82077_ORIG;  /* Pre-1991 82077, doesn't know
3919                                          * LOCK/UNLOCK */
3920         }
3921         if ((r != 1) || (reply_buffer[0] != 0x00)) {
3922                 pr_info("FDC %d init: UNLOCK: unexpected return of %d bytes.\n",
3923                         fdc, r);
3924                 return FDC_UNKNOWN;
3925         }
3926         output_byte(FD_PARTID);
3927         r = result();
3928         if (r != 1) {
3929                 pr_info("FDC %d init: PARTID: unexpected return of %d bytes.\n",
3930                         fdc, r);
3931                 return FDC_UNKNOWN;
3932         }
3933         if (reply_buffer[0] == 0x80) {
3934                 pr_info("FDC %d is a post-1991 82077\n", fdc);
3935                 return FDC_82077;       /* Revised 82077AA passes all the tests */
3936         }
3937         switch (reply_buffer[0] >> 5) {
3938         case 0x0:
3939                 /* Either a 82078-1 or a 82078SL running at 5Volt */
3940                 pr_info("FDC %d is an 82078.\n", fdc);
3941                 return FDC_82078;
3942         case 0x1:
3943                 pr_info("FDC %d is a 44pin 82078\n", fdc);
3944                 return FDC_82078;
3945         case 0x2:
3946                 pr_info("FDC %d is a S82078B\n", fdc);
3947                 return FDC_S82078B;
3948         case 0x3:
3949                 pr_info("FDC %d is a National Semiconductor PC87306\n", fdc);
3950                 return FDC_87306;
3951         default:
3952                 pr_info("FDC %d init: 82078 variant with unknown PARTID=%d.\n",
3953                         fdc, reply_buffer[0] >> 5);
3954                 return FDC_82078_UNKN;
3955         }
3956 }                               /* get_fdc_version */
3957
3958 /* lilo configuration */
3959
3960 static void __init floppy_set_flags(int *ints, int param, int param2)
3961 {
3962         int i;
3963
3964         for (i = 0; i < ARRAY_SIZE(default_drive_params); i++) {
3965                 if (param)
3966                         default_drive_params[i].params.flags |= param2;
3967                 else
3968                         default_drive_params[i].params.flags &= ~param2;
3969         }
3970         DPRINT("%s flag 0x%x\n", param2 ? "Setting" : "Clearing", param);
3971 }
3972
3973 static void __init daring(int *ints, int param, int param2)
3974 {
3975         int i;
3976
3977         for (i = 0; i < ARRAY_SIZE(default_drive_params); i++) {
3978                 if (param) {
3979                         default_drive_params[i].params.select_delay = 0;
3980                         default_drive_params[i].params.flags |=
3981                             FD_SILENT_DCL_CLEAR;
3982                 } else {
3983                         default_drive_params[i].params.select_delay =
3984                             2 * HZ / 100;
3985                         default_drive_params[i].params.flags &=
3986                             ~FD_SILENT_DCL_CLEAR;
3987                 }
3988         }
3989         DPRINT("Assuming %s floppy hardware\n", param ? "standard" : "broken");
3990 }
3991
3992 static void __init set_cmos(int *ints, int dummy, int dummy2)
3993 {
3994         int current_drive = 0;
3995
3996         if (ints[0] != 2) {
3997                 DPRINT("wrong number of parameters for CMOS\n");
3998                 return;
3999         }
4000         current_drive = ints[1];
4001         if (current_drive < 0 || current_drive >= 8) {
4002                 DPRINT("bad drive for set_cmos\n");
4003                 return;
4004         }
4005 #if N_FDC > 1
4006         if (current_drive >= 4 && !FDC2)
4007                 FDC2 = 0x370;
4008 #endif
4009         DP->cmos = ints[2];
4010         DPRINT("setting CMOS code to %d\n", ints[2]);
4011 }
4012
4013 static struct param_table {
4014         const char *name;
4015         void (*fn) (int *ints, int param, int param2);
4016         int *var;
4017         int def_param;
4018         int param2;
4019 } config_params[] __initdata = {
4020         {"allowed_drive_mask", NULL, &allowed_drive_mask, 0xff, 0}, /* obsolete */
4021         {"all_drives", NULL, &allowed_drive_mask, 0xff, 0},     /* obsolete */
4022         {"asus_pci", NULL, &allowed_drive_mask, 0x33, 0},
4023         {"irq", NULL, &FLOPPY_IRQ, 6, 0},
4024         {"dma", NULL, &FLOPPY_DMA, 2, 0},
4025         {"daring", daring, NULL, 1, 0},
4026 #if N_FDC > 1
4027         {"two_fdc", NULL, &FDC2, 0x370, 0},
4028         {"one_fdc", NULL, &FDC2, 0, 0},
4029 #endif
4030         {"thinkpad", floppy_set_flags, NULL, 1, FD_INVERTED_DCL},
4031         {"broken_dcl", floppy_set_flags, NULL, 1, FD_BROKEN_DCL},
4032         {"messages", floppy_set_flags, NULL, 1, FTD_MSG},
4033         {"silent_dcl_clear", floppy_set_flags, NULL, 1, FD_SILENT_DCL_CLEAR},
4034         {"debug", floppy_set_flags, NULL, 1, FD_DEBUG},
4035         {"nodma", NULL, &can_use_virtual_dma, 1, 0},
4036         {"omnibook", NULL, &can_use_virtual_dma, 1, 0},
4037         {"yesdma", NULL, &can_use_virtual_dma, 0, 0},
4038         {"fifo_depth", NULL, &fifo_depth, 0xa, 0},
4039         {"nofifo", NULL, &no_fifo, 0x20, 0},
4040         {"usefifo", NULL, &no_fifo, 0, 0},
4041         {"cmos", set_cmos, NULL, 0, 0},
4042         {"slow", NULL, &slow_floppy, 1, 0},
4043         {"unexpected_interrupts", NULL, &print_unex, 1, 0},
4044         {"no_unexpected_interrupts", NULL, &print_unex, 0, 0},
4045         {"L40SX", NULL, &print_unex, 0, 0}
4046
4047         EXTRA_FLOPPY_PARAMS
4048 };
4049
4050 static int __init floppy_setup(char *str)
4051 {
4052         int i;
4053         int param;
4054         int ints[11];
4055
4056         str = get_options(str, ARRAY_SIZE(ints), ints);
4057         if (str) {
4058                 for (i = 0; i < ARRAY_SIZE(config_params); i++) {
4059                         if (strcmp(str, config_params[i].name) == 0) {
4060                                 if (ints[0])
4061                                         param = ints[1];
4062                                 else
4063                                         param = config_params[i].def_param;
4064                                 if (config_params[i].fn)
4065                                         config_params[i].fn(ints, param,
4066                                                             config_params[i].
4067                                                             param2);
4068                                 if (config_params[i].var) {
4069                                         DPRINT("%s=%d\n", str, param);
4070                                         *config_params[i].var = param;
4071                                 }
4072                                 return 1;
4073                         }
4074                 }
4075         }
4076         if (str) {
4077                 DPRINT("unknown floppy option [%s]\n", str);
4078
4079                 DPRINT("allowed options are:");
4080                 for (i = 0; i < ARRAY_SIZE(config_params); i++)
4081                         pr_cont(" %s", config_params[i].name);
4082                 pr_cont("\n");
4083         } else
4084                 DPRINT("botched floppy option\n");
4085         DPRINT("Read Documentation/blockdev/floppy.txt\n");
4086         return 0;
4087 }
4088
4089 static int have_no_fdc = -ENODEV;
4090
4091 static ssize_t floppy_cmos_show(struct device *dev,
4092                                 struct device_attribute *attr, char *buf)
4093 {
4094         struct platform_device *p = to_platform_device(dev);
4095         int drive;
4096
4097         drive = p->id;
4098         return sprintf(buf, "%X\n", UDP->cmos);
4099 }
4100
4101 static DEVICE_ATTR(cmos, S_IRUGO, floppy_cmos_show, NULL);
4102
4103 static void floppy_device_release(struct device *dev)
4104 {
4105 }
4106
4107 static int floppy_resume(struct device *dev)
4108 {
4109         int fdc;
4110
4111         for (fdc = 0; fdc < N_FDC; fdc++)
4112                 if (FDCS->address != -1)
4113                         user_reset_fdc(-1, FD_RESET_ALWAYS, false);
4114
4115         return 0;
4116 }
4117
4118 static const struct dev_pm_ops floppy_pm_ops = {
4119         .resume = floppy_resume,
4120         .restore = floppy_resume,
4121 };
4122
4123 static struct platform_driver floppy_driver = {
4124         .driver = {
4125                    .name = "floppy",
4126                    .pm = &floppy_pm_ops,
4127         },
4128 };
4129
4130 static struct platform_device floppy_device[N_DRIVE];
4131
4132 static struct kobject *floppy_find(dev_t dev, int *part, void *data)
4133 {
4134         int drive = (*part & 3) | ((*part & 0x80) >> 5);
4135         if (drive >= N_DRIVE ||
4136             !(allowed_drive_mask & (1 << drive)) ||
4137             fdc_state[FDC(drive)].version == FDC_NONE)
4138                 return NULL;
4139         if (((*part >> 2) & 0x1f) >= ARRAY_SIZE(floppy_type))
4140                 return NULL;
4141         *part = 0;
4142         return get_disk(disks[drive]);
4143 }
4144
4145 static int __init floppy_init(void)
4146 {
4147         int i, unit, drive;
4148         int err, dr;
4149
4150         set_debugt();
4151         interruptjiffies = resultjiffies = jiffies;
4152
4153 #if defined(CONFIG_PPC)
4154         if (check_legacy_ioport(FDC1))
4155                 return -ENODEV;
4156 #endif
4157
4158         raw_cmd = NULL;
4159
4160         for (dr = 0; dr < N_DRIVE; dr++) {
4161                 disks[dr] = alloc_disk(1);
4162                 if (!disks[dr]) {
4163                         err = -ENOMEM;
4164                         goto out_put_disk;
4165                 }
4166
4167                 disks[dr]->queue = blk_init_queue(do_fd_request, &floppy_lock);
4168                 if (!disks[dr]->queue) {
4169                         put_disk(disks[dr]);
4170                         err = -ENOMEM;
4171                         goto out_put_disk;
4172                 }
4173
4174                 blk_queue_max_hw_sectors(disks[dr]->queue, 64);
4175                 disks[dr]->major = FLOPPY_MAJOR;
4176                 disks[dr]->first_minor = TOMINOR(dr);
4177                 disks[dr]->fops = &floppy_fops;
4178                 sprintf(disks[dr]->disk_name, "fd%d", dr);
4179
4180                 init_timer(&motor_off_timer[dr]);
4181                 motor_off_timer[dr].data = dr;
4182                 motor_off_timer[dr].function = motor_off_callback;
4183         }
4184
4185         err = register_blkdev(FLOPPY_MAJOR, "fd");
4186         if (err)
4187                 goto out_put_disk;
4188
4189         err = platform_driver_register(&floppy_driver);
4190         if (err)
4191                 goto out_unreg_blkdev;
4192
4193         blk_register_region(MKDEV(FLOPPY_MAJOR, 0), 256, THIS_MODULE,
4194                             floppy_find, NULL, NULL);
4195
4196         for (i = 0; i < 256; i++)
4197                 if (ITYPE(i))
4198                         floppy_sizes[i] = floppy_type[ITYPE(i)].size;
4199                 else
4200                         floppy_sizes[i] = MAX_DISK_SIZE << 1;
4201
4202         reschedule_timeout(MAXTIMEOUT, "floppy init");
4203         config_types();
4204
4205         for (i = 0; i < N_FDC; i++) {
4206                 fdc = i;
4207                 memset(FDCS, 0, sizeof(*FDCS));
4208                 FDCS->dtr = -1;
4209                 FDCS->dor = 0x4;
4210 #if defined(__sparc__) || defined(__mc68000__)
4211         /*sparcs/sun3x don't have a DOR reset which we can fall back on to */
4212 #ifdef __mc68000__
4213                 if (MACH_IS_SUN3X)
4214 #endif
4215                         FDCS->version = FDC_82072A;
4216 #endif
4217         }
4218
4219         use_virtual_dma = can_use_virtual_dma & 1;
4220         fdc_state[0].address = FDC1;
4221         if (fdc_state[0].address == -1) {
4222                 del_timer_sync(&fd_timeout);
4223                 err = -ENODEV;
4224                 goto out_unreg_region;
4225         }
4226 #if N_FDC > 1
4227         fdc_state[1].address = FDC2;
4228 #endif
4229
4230         fdc = 0;                /* reset fdc in case of unexpected interrupt */
4231         err = floppy_grab_irq_and_dma();
4232         if (err) {
4233                 del_timer_sync(&fd_timeout);
4234                 err = -EBUSY;
4235                 goto out_unreg_region;
4236         }
4237
4238         /* initialise drive state */
4239         for (drive = 0; drive < N_DRIVE; drive++) {
4240                 memset(UDRS, 0, sizeof(*UDRS));
4241                 memset(UDRWE, 0, sizeof(*UDRWE));
4242                 set_bit(FD_DISK_NEWCHANGE_BIT, &UDRS->flags);
4243                 set_bit(FD_DISK_CHANGED_BIT, &UDRS->flags);
4244                 set_bit(FD_VERIFY_BIT, &UDRS->flags);
4245                 UDRS->fd_device = -1;
4246                 floppy_track_buffer = NULL;
4247                 max_buffer_sectors = 0;
4248         }
4249         /*
4250          * Small 10 msec delay to let through any interrupt that
4251          * initialization might have triggered, to not
4252          * confuse detection:
4253          */
4254         msleep(10);
4255
4256         for (i = 0; i < N_FDC; i++) {
4257                 fdc = i;
4258                 FDCS->driver_version = FD_DRIVER_VERSION;
4259                 for (unit = 0; unit < 4; unit++)
4260                         FDCS->track[unit] = 0;
4261                 if (FDCS->address == -1)
4262                         continue;
4263                 FDCS->rawcmd = 2;
4264                 if (user_reset_fdc(-1, FD_RESET_ALWAYS, false)) {
4265                         /* free ioports reserved by floppy_grab_irq_and_dma() */
4266                         floppy_release_regions(fdc);
4267                         FDCS->address = -1;
4268                         FDCS->version = FDC_NONE;
4269                         continue;
4270                 }
4271                 /* Try to determine the floppy controller type */
4272                 FDCS->version = get_fdc_version();
4273                 if (FDCS->version == FDC_NONE) {
4274                         /* free ioports reserved by floppy_grab_irq_and_dma() */
4275                         floppy_release_regions(fdc);
4276                         FDCS->address = -1;
4277                         continue;
4278                 }
4279                 if (can_use_virtual_dma == 2 && FDCS->version < FDC_82072A)
4280                         can_use_virtual_dma = 0;
4281
4282                 have_no_fdc = 0;
4283                 /* Not all FDCs seem to be able to handle the version command
4284                  * properly, so force a reset for the standard FDC clones,
4285                  * to avoid interrupt garbage.
4286                  */
4287                 user_reset_fdc(-1, FD_RESET_ALWAYS, false);
4288         }
4289         fdc = 0;
4290         del_timer_sync(&fd_timeout);
4291         current_drive = 0;
4292         initialized = true;
4293         if (have_no_fdc) {
4294                 DPRINT("no floppy controllers found\n");
4295                 err = have_no_fdc;
4296                 goto out_flush_work;
4297         }
4298
4299         for (drive = 0; drive < N_DRIVE; drive++) {
4300                 if (!(allowed_drive_mask & (1 << drive)))
4301                         continue;
4302                 if (fdc_state[FDC(drive)].version == FDC_NONE)
4303                         continue;
4304
4305                 floppy_device[drive].name = floppy_device_name;
4306                 floppy_device[drive].id = drive;
4307                 floppy_device[drive].dev.release = floppy_device_release;
4308
4309                 err = platform_device_register(&floppy_device[drive]);
4310                 if (err)
4311                         goto out_remove_drives;
4312
4313                 err = device_create_file(&floppy_device[drive].dev,
4314                                          &dev_attr_cmos);
4315                 if (err)
4316                         goto out_unreg_platform_dev;
4317
4318                 /* to be cleaned up... */
4319                 disks[drive]->private_data = (void *)(long)drive;
4320                 disks[drive]->flags |= GENHD_FL_REMOVABLE;
4321                 disks[drive]->driverfs_dev = &floppy_device[drive].dev;
4322                 add_disk(disks[drive]);
4323         }
4324
4325         return 0;
4326
4327 out_unreg_platform_dev:
4328         platform_device_unregister(&floppy_device[drive]);
4329 out_remove_drives:
4330         while (drive--) {
4331                 if ((allowed_drive_mask & (1 << drive)) &&
4332                     fdc_state[FDC(drive)].version != FDC_NONE) {
4333                         del_gendisk(disks[drive]);
4334                         device_remove_file(&floppy_device[drive].dev, &dev_attr_cmos);
4335                         platform_device_unregister(&floppy_device[drive]);
4336                 }
4337         }
4338 out_flush_work:
4339         flush_work_sync(&floppy_work);
4340         if (atomic_read(&usage_count))
4341                 floppy_release_irq_and_dma();
4342 out_unreg_region:
4343         blk_unregister_region(MKDEV(FLOPPY_MAJOR, 0), 256);
4344         platform_driver_unregister(&floppy_driver);
4345 out_unreg_blkdev:
4346         unregister_blkdev(FLOPPY_MAJOR, "fd");
4347 out_put_disk:
4348         while (dr--) {
4349                 del_timer_sync(&motor_off_timer[dr]);
4350                 if (disks[dr]->queue) {
4351                         blk_cleanup_queue(disks[dr]->queue);
4352                         /*
4353                          * put_disk() is not paired with add_disk() and
4354                          * will put queue reference one extra time. fix it.
4355                          */
4356                         disks[dr]->queue = NULL;
4357                 }
4358                 put_disk(disks[dr]);
4359         }
4360         return err;
4361 }
4362
4363 static const struct io_region {
4364         int offset;
4365         int size;
4366 } io_regions[] = {
4367         { 2, 1 },
4368         /* address + 3 is sometimes reserved by pnp bios for motherboard */
4369         { 4, 2 },
4370         /* address + 6 is reserved, and may be taken by IDE.
4371          * Unfortunately, Adaptec doesn't know this :-(, */
4372         { 7, 1 },
4373 };
4374
4375 static void floppy_release_allocated_regions(int fdc, const struct io_region *p)
4376 {
4377         while (p != io_regions) {
4378                 p--;
4379                 release_region(FDCS->address + p->offset, p->size);
4380         }
4381 }
4382
4383 #define ARRAY_END(X) (&((X)[ARRAY_SIZE(X)]))
4384
4385 static int floppy_request_regions(int fdc)
4386 {
4387         const struct io_region *p;
4388
4389         for (p = io_regions; p < ARRAY_END(io_regions); p++) {
4390                 if (!request_region(FDCS->address + p->offset,
4391                                     p->size, "floppy")) {
4392                         DPRINT("Floppy io-port 0x%04lx in use\n",
4393                                FDCS->address + p->offset);
4394                         floppy_release_allocated_regions(fdc, p);
4395                         return -EBUSY;
4396                 }
4397         }
4398         return 0;
4399 }
4400
4401 static void floppy_release_regions(int fdc)
4402 {
4403         floppy_release_allocated_regions(fdc, ARRAY_END(io_regions));
4404 }
4405
4406 static int floppy_grab_irq_and_dma(void)
4407 {
4408         if (atomic_inc_return(&usage_count) > 1)
4409                 return 0;
4410
4411         /*
4412          * We might have scheduled a free_irq(), wait it to
4413          * drain first:
4414          */
4415         flush_work_sync(&floppy_work);
4416
4417         if (fd_request_irq()) {
4418                 DPRINT("Unable to grab IRQ%d for the floppy driver\n",
4419                        FLOPPY_IRQ);
4420                 atomic_dec(&usage_count);
4421                 return -1;
4422         }
4423         if (fd_request_dma()) {
4424                 DPRINT("Unable to grab DMA%d for the floppy driver\n",
4425                        FLOPPY_DMA);
4426                 if (can_use_virtual_dma & 2)
4427                         use_virtual_dma = can_use_virtual_dma = 1;
4428                 if (!(can_use_virtual_dma & 1)) {
4429                         fd_free_irq();
4430                         atomic_dec(&usage_count);
4431                         return -1;
4432                 }
4433         }
4434
4435         for (fdc = 0; fdc < N_FDC; fdc++) {
4436                 if (FDCS->address != -1) {
4437                         if (floppy_request_regions(fdc))
4438                                 goto cleanup;
4439                 }
4440         }
4441         for (fdc = 0; fdc < N_FDC; fdc++) {
4442                 if (FDCS->address != -1) {
4443                         reset_fdc_info(1);
4444                         fd_outb(FDCS->dor, FD_DOR);
4445                 }
4446         }
4447         fdc = 0;
4448         set_dor(0, ~0, 8);      /* avoid immediate interrupt */
4449
4450         for (fdc = 0; fdc < N_FDC; fdc++)
4451                 if (FDCS->address != -1)
4452                         fd_outb(FDCS->dor, FD_DOR);
4453         /*
4454          * The driver will try and free resources and relies on us
4455          * to know if they were allocated or not.
4456          */
4457         fdc = 0;
4458         irqdma_allocated = 1;
4459         return 0;
4460 cleanup:
4461         fd_free_irq();
4462         fd_free_dma();
4463         while (--fdc >= 0)
4464                 floppy_release_regions(fdc);
4465         atomic_dec(&usage_count);
4466         return -1;
4467 }
4468
4469 static void floppy_release_irq_and_dma(void)
4470 {
4471         int old_fdc;
4472 #ifndef __sparc__
4473         int drive;
4474 #endif
4475         long tmpsize;
4476         unsigned long tmpaddr;
4477
4478         if (!atomic_dec_and_test(&usage_count))
4479                 return;
4480
4481         if (irqdma_allocated) {
4482                 fd_disable_dma();
4483                 fd_free_dma();
4484                 fd_free_irq();
4485                 irqdma_allocated = 0;
4486         }
4487         set_dor(0, ~0, 8);
4488 #if N_FDC > 1
4489         set_dor(1, ~8, 0);
4490 #endif
4491
4492         if (floppy_track_buffer && max_buffer_sectors) {
4493                 tmpsize = max_buffer_sectors * 1024;
4494                 tmpaddr = (unsigned long)floppy_track_buffer;
4495                 floppy_track_buffer = NULL;
4496                 max_buffer_sectors = 0;
4497                 buffer_min = buffer_max = -1;
4498                 fd_dma_mem_free(tmpaddr, tmpsize);
4499         }
4500 #ifndef __sparc__
4501         for (drive = 0; drive < N_FDC * 4; drive++)
4502                 if (timer_pending(motor_off_timer + drive))
4503                         pr_info("motor off timer %d still active\n", drive);
4504 #endif
4505
4506         if (timer_pending(&fd_timeout))
4507                 pr_info("floppy timer still active:%s\n", timeout_message);
4508         if (timer_pending(&fd_timer))
4509                 pr_info("auxiliary floppy timer still active\n");
4510         if (work_pending(&floppy_work))
4511                 pr_info("work still pending\n");
4512         old_fdc = fdc;
4513         for (fdc = 0; fdc < N_FDC; fdc++)
4514                 if (FDCS->address != -1)
4515                         floppy_release_regions(fdc);
4516         fdc = old_fdc;
4517 }
4518
4519 #ifdef MODULE
4520
4521 static char *floppy;
4522
4523 static void __init parse_floppy_cfg_string(char *cfg)
4524 {
4525         char *ptr;
4526
4527         while (*cfg) {
4528                 ptr = cfg;
4529                 while (*cfg && *cfg != ' ' && *cfg != '\t')
4530                         cfg++;
4531                 if (*cfg) {
4532                         *cfg = '\0';
4533                         cfg++;
4534                 }
4535                 if (*ptr)
4536                         floppy_setup(ptr);
4537         }
4538 }
4539
4540 static int __init floppy_module_init(void)
4541 {
4542         if (floppy)
4543                 parse_floppy_cfg_string(floppy);
4544         return floppy_init();
4545 }
4546 module_init(floppy_module_init);
4547
4548 static void __exit floppy_module_exit(void)
4549 {
4550         int drive;
4551
4552         blk_unregister_region(MKDEV(FLOPPY_MAJOR, 0), 256);
4553         unregister_blkdev(FLOPPY_MAJOR, "fd");
4554         platform_driver_unregister(&floppy_driver);
4555
4556         for (drive = 0; drive < N_DRIVE; drive++) {
4557                 del_timer_sync(&motor_off_timer[drive]);
4558
4559                 if ((allowed_drive_mask & (1 << drive)) &&
4560                     fdc_state[FDC(drive)].version != FDC_NONE) {
4561                         del_gendisk(disks[drive]);
4562                         device_remove_file(&floppy_device[drive].dev, &dev_attr_cmos);
4563                         platform_device_unregister(&floppy_device[drive]);
4564                 }
4565                 blk_cleanup_queue(disks[drive]->queue);
4566                 put_disk(disks[drive]);
4567         }
4568
4569         del_timer_sync(&fd_timeout);
4570         del_timer_sync(&fd_timer);
4571
4572         if (atomic_read(&usage_count))
4573                 floppy_release_irq_and_dma();
4574
4575         /* eject disk, if any */
4576         fd_eject(0);
4577 }
4578
4579 module_exit(floppy_module_exit);
4580
4581 module_param(floppy, charp, 0);
4582 module_param(FLOPPY_IRQ, int, 0);
4583 module_param(FLOPPY_DMA, int, 0);
4584 MODULE_AUTHOR("Alain L. Knaff");
4585 MODULE_SUPPORTED_DEVICE("fd");
4586 MODULE_LICENSE("GPL");
4587
4588 /* This doesn't actually get used other than for module information */
4589 static const struct pnp_device_id floppy_pnpids[] = {
4590         {"PNP0700", 0},
4591         {}
4592 };
4593
4594 MODULE_DEVICE_TABLE(pnp, floppy_pnpids);
4595
4596 #else
4597
4598 __setup("floppy=", floppy_setup);
4599 module_init(floppy_init)
4600 #endif
4601
4602 MODULE_ALIAS_BLOCKDEV_MAJOR(FLOPPY_MAJOR);