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