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