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