- pre2
[davej-history.git] / drivers / block / floppy.c
blob16221e3fe96fa88da0b278ebcd3032cec01c348f
1 /*
2 * linux/drivers/block/floppy.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 * Copyright (C) 1993, 1994 Alain Knaff
6 * Copyright (C) 1998 Alan Cox
7 */
8 /*
9 * 02.12.91 - Changed to static variables to indicate need for reset
10 * and recalibrate. This makes some things easier (output_byte reset
11 * checking etc), and means less interrupt jumping in case of errors,
12 * so the code is hopefully easier to understand.
16 * This file is certainly a mess. I've tried my best to get it working,
17 * but I don't like programming floppies, and I have only one anyway.
18 * Urgel. I should check for more errors, and do more graceful error
19 * recovery. Seems there are problems with several drives. I've tried to
20 * correct them. No promises.
24 * As with hd.c, all routines within this file can (and will) be called
25 * by interrupts, so extreme caution is needed. A hardware interrupt
26 * handler may not sleep, or a kernel panic will happen. Thus I cannot
27 * call "floppy-on" directly, but have to set a special timer interrupt
28 * etc.
32 * 28.02.92 - made track-buffering routines, based on the routines written
33 * by entropy@wintermute.wpi.edu (Lawrence Foard). Linus.
37 * Automatic floppy-detection and formatting written by Werner Almesberger
38 * (almesber@nessie.cs.id.ethz.ch), who also corrected some problems with
39 * the floppy-change signal detection.
43 * 1992/7/22 -- Hennus Bergman: Added better error reporting, fixed
44 * FDC data overrun bug, added some preliminary stuff for vertical
45 * recording support.
47 * 1992/9/17: Added DMA allocation & DMA functions. -- hhb.
49 * TODO: Errors are still not counted properly.
52 /* 1992/9/20
53 * Modifications for ``Sector Shifting'' by Rob Hooft (hooft@chem.ruu.nl)
54 * modeled after the freeware MS-DOS program fdformat/88 V1.8 by
55 * Christoph H. Hochst\"atter.
56 * I have fixed the shift values to the ones I always use. Maybe a new
57 * ioctl() should be created to be able to modify them.
58 * There is a bug in the driver that makes it impossible to format a
59 * floppy as the first thing after bootup.
63 * 1993/4/29 -- Linus -- cleaned up the timer handling in the kernel, and
64 * this helped the floppy driver as well. Much cleaner, and still seems to
65 * work.
68 /* 1994/6/24 --bbroad-- added the floppy table entries and made
69 * minor modifications to allow 2.88 floppies to be run.
72 /* 1994/7/13 -- Paul Vojta -- modified the probing code to allow three or more
73 * disk types.
77 * 1994/8/8 -- Alain Knaff -- Switched to fdpatch driver: Support for bigger
78 * format bug fixes, but unfortunately some new bugs too...
81 /* 1994/9/17 -- Koen Holtman -- added logging of physical floppy write
82 * errors to allow safe writing by specialized programs.
85 /* 1995/4/24 -- Dan Fandrich -- added support for Commodore 1581 3.5" disks
86 * by defining bit 1 of the "stretch" parameter to mean put sectors on the
87 * opposite side of the disk, leaving the sector IDs alone (i.e. Commodore's
88 * drives are "upside-down").
92 * 1995/8/26 -- Andreas Busse -- added Mips support.
96 * 1995/10/18 -- Ralf Baechle -- Portability cleanup; move machine dependent
97 * features to asm/floppy.h.
101 * 1998/05/07 -- Russell King -- More portability cleanups; moved definition of
102 * interrupt and dma channel to asm/floppy.h. Cleaned up some formatting &
103 * use of '0' for NULL.
107 * 1998/06/07 -- Alan Cox -- Merged the 2.0.34 fixes for resource allocation
108 * failures.
112 * 1998/09/20 -- David Weinehall -- Added slow-down code for buggy PS/2-drives.
116 * 1999/08/13 -- Paul Slootman -- floppy stopped working on Alpha after 24
117 * days, 6 hours, 32 minutes and 32 seconds (i.e. MAXINT jiffies; ints were
118 * being used to store jiffies, which are unsigned longs).
122 * 2000/08/28 -- Arnaldo Carvalho de Melo <acme@conectiva.com.br>
123 * - get rid of check_region
124 * - s/suser/capable/
127 #define FLOPPY_SANITY_CHECK
128 #undef FLOPPY_SILENT_DCL_CLEAR
130 #define REALLY_SLOW_IO
132 #define DEBUGT 2
133 #define DCL_DEBUG /* debug disk change line */
135 /* do print messages for unexpected interrupts */
136 static int print_unex=1;
137 #include <linux/module.h>
138 #include <linux/sched.h>
139 #include <linux/fs.h>
140 #include <linux/kernel.h>
141 #include <linux/timer.h>
142 #include <linux/tqueue.h>
143 #define FDPATCHES
144 #include <linux/fdreg.h>
147 * 1998/1/21 -- Richard Gooch <rgooch@atnf.csiro.au> -- devfs support
151 #include <linux/fd.h>
152 #include <linux/hdreg.h>
154 #include <linux/errno.h>
155 #include <linux/malloc.h>
156 #include <linux/mm.h>
157 #include <linux/string.h>
158 #include <linux/fcntl.h>
159 #include <linux/delay.h>
160 #include <linux/mc146818rtc.h> /* CMOS defines */
161 #include <linux/ioport.h>
162 #include <linux/interrupt.h>
163 #include <linux/init.h>
164 #include <linux/devfs_fs_kernel.h>
167 * PS/2 floppies have much slower step rates than regular floppies.
168 * It's been recommended that take about 1/4 of the default speed
169 * in some more extreme cases.
171 static int slow_floppy = 0;
173 #include <asm/dma.h>
174 #include <asm/irq.h>
175 #include <asm/system.h>
176 #include <asm/io.h>
177 #include <asm/uaccess.h>
179 static int FLOPPY_IRQ=6;
180 static int FLOPPY_DMA=2;
181 static int can_use_virtual_dma=2;
182 /* =======
183 * can use virtual DMA:
184 * 0 = use of virtual DMA disallowed by config
185 * 1 = use of virtual DMA prescribed by config
186 * 2 = no virtual DMA preference configured. By default try hard DMA,
187 * but fall back on virtual DMA when not enough memory available
190 static int use_virtual_dma;
191 /* =======
192 * use virtual DMA
193 * 0 using hard DMA
194 * 1 using virtual DMA
195 * This variable is set to virtual when a DMA mem problem arises, and
196 * reset back in floppy_grab_irq_and_dma.
197 * It is not safe to reset it in other circumstances, because the floppy
198 * driver may have several buffers in use at once, and we do currently not
199 * record each buffers capabilities
202 static unsigned short virtual_dma_port=0x3f0;
203 void floppy_interrupt(int irq, void *dev_id, struct pt_regs * regs);
204 static int set_dor(int fdc, char mask, char data);
205 static void register_devfs_entries (int drive) __init;
206 static devfs_handle_t devfs_handle = NULL;
208 #define K_64 0x10000 /* 64KB */
210 /* the following is the mask of allowed drives. By default units 2 and
211 * 3 of both floppy controllers are disabled, because switching on the
212 * motor of these drives causes system hangs on some PCI computers. drive
213 * 0 is the low bit (0x1), and drive 7 is the high bit (0x80). Bits are on if
214 * a drive is allowed.
216 * NOTE: This must come before we include the arch floppy header because
217 * some ports reference this variable from there. -DaveM
220 static int allowed_drive_mask = 0x33;
222 #include <asm/floppy.h>
224 static int irqdma_allocated = 0;
226 #define MAJOR_NR FLOPPY_MAJOR
228 #include <linux/blk.h>
229 #include <linux/blkpg.h>
230 #include <linux/cdrom.h> /* for the compatibility eject ioctl */
232 #ifndef fd_get_dma_residue
233 #define fd_get_dma_residue() get_dma_residue(FLOPPY_DMA)
234 #endif
236 /* Dma Memory related stuff */
238 #ifndef fd_dma_mem_free
239 #define fd_dma_mem_free(addr, size) free_pages(addr, get_order(size))
240 #endif
242 #ifndef fd_dma_mem_alloc
243 #define fd_dma_mem_alloc(size) __get_dma_pages(GFP_KERNEL,get_order(size))
244 #endif
246 static inline void fallback_on_nodma_alloc(char **addr, size_t l)
248 #ifdef FLOPPY_CAN_FALLBACK_ON_NODMA
249 if (*addr)
250 return; /* we have the memory */
251 if (can_use_virtual_dma != 2)
252 return; /* no fallback allowed */
253 printk("DMA memory shortage. Temporarily falling back on virtual DMA\n");
254 *addr = (char *) nodma_mem_alloc(l);
255 #else
256 return;
257 #endif
260 /* End dma memory related stuff */
262 static unsigned long fake_change = 0;
263 static int initialising=1;
265 static inline int TYPE(kdev_t x) {
266 return (MINOR(x)>>2) & 0x1f;
268 static inline int DRIVE(kdev_t x) {
269 return (MINOR(x)&0x03) | ((MINOR(x)&0x80) >> 5);
271 #define ITYPE(x) (((x)>>2) & 0x1f)
272 #define TOMINOR(x) ((x & 3) | ((x & 4) << 5))
273 #define UNIT(x) ((x) & 0x03) /* drive on fdc */
274 #define FDC(x) (((x) & 0x04) >> 2) /* fdc of drive */
275 #define REVDRIVE(fdc, unit) ((unit) + ((fdc) << 2))
276 /* reverse mapping from unit and fdc to drive */
277 #define DP (&drive_params[current_drive])
278 #define DRS (&drive_state[current_drive])
279 #define DRWE (&write_errors[current_drive])
280 #define FDCS (&fdc_state[fdc])
281 #define CLEARF(x) (clear_bit(x##_BIT, &DRS->flags))
282 #define SETF(x) (set_bit(x##_BIT, &DRS->flags))
283 #define TESTF(x) (test_bit(x##_BIT, &DRS->flags))
285 #define UDP (&drive_params[drive])
286 #define UDRS (&drive_state[drive])
287 #define UDRWE (&write_errors[drive])
288 #define UFDCS (&fdc_state[FDC(drive)])
289 #define UCLEARF(x) (clear_bit(x##_BIT, &UDRS->flags))
290 #define USETF(x) (set_bit(x##_BIT, &UDRS->flags))
291 #define UTESTF(x) (test_bit(x##_BIT, &UDRS->flags))
293 #define DPRINT(format, args...) printk(DEVICE_NAME "%d: " format, current_drive , ## args)
295 #define PH_HEAD(floppy,head) (((((floppy)->stretch & 2) >>1) ^ head) << 2)
296 #define STRETCH(floppy) ((floppy)->stretch & FD_STRETCH)
298 #define CLEARSTRUCT(x) memset((x), 0, sizeof(*(x)))
300 /* read/write */
301 #define COMMAND raw_cmd->cmd[0]
302 #define DR_SELECT raw_cmd->cmd[1]
303 #define TRACK raw_cmd->cmd[2]
304 #define HEAD raw_cmd->cmd[3]
305 #define SECTOR raw_cmd->cmd[4]
306 #define SIZECODE raw_cmd->cmd[5]
307 #define SECT_PER_TRACK raw_cmd->cmd[6]
308 #define GAP raw_cmd->cmd[7]
309 #define SIZECODE2 raw_cmd->cmd[8]
310 #define NR_RW 9
312 /* format */
313 #define F_SIZECODE raw_cmd->cmd[2]
314 #define F_SECT_PER_TRACK raw_cmd->cmd[3]
315 #define F_GAP raw_cmd->cmd[4]
316 #define F_FILL raw_cmd->cmd[5]
317 #define NR_F 6
320 * Maximum disk size (in kilobytes). This default is used whenever the
321 * current disk size is unknown.
322 * [Now it is rather a minimum]
324 #define MAX_DISK_SIZE 4 /* 3984*/
328 * globals used by 'result()'
330 #define MAX_REPLIES 16
331 static unsigned char reply_buffer[MAX_REPLIES];
332 static int inr; /* size of reply buffer, when called from interrupt */
333 #define ST0 (reply_buffer[0])
334 #define ST1 (reply_buffer[1])
335 #define ST2 (reply_buffer[2])
336 #define ST3 (reply_buffer[0]) /* result of GETSTATUS */
337 #define R_TRACK (reply_buffer[3])
338 #define R_HEAD (reply_buffer[4])
339 #define R_SECTOR (reply_buffer[5])
340 #define R_SIZECODE (reply_buffer[6])
342 #define SEL_DLY (2*HZ/100)
345 * this struct defines the different floppy drive types.
347 static struct {
348 struct floppy_drive_params params;
349 const char *name; /* name printed while booting */
350 } default_drive_params[]= {
351 /* NOTE: the time values in jiffies should be in msec!
352 CMOS drive type
353 | Maximum data rate supported by drive type
354 | | Head load time, msec
355 | | | Head unload time, msec (not used)
356 | | | | Step rate interval, usec
357 | | | | | Time needed for spinup time (jiffies)
358 | | | | | | Timeout for spinning down (jiffies)
359 | | | | | | | Spindown offset (where disk stops)
360 | | | | | | | | Select delay
361 | | | | | | | | | RPS
362 | | | | | | | | | | Max number of tracks
363 | | | | | | | | | | | Interrupt timeout
364 | | | | | | | | | | | | Max nonintlv. sectors
365 | | | | | | | | | | | | | -Max Errors- flags */
366 {{0, 500, 16, 16, 8000, 1*HZ, 3*HZ, 0, SEL_DLY, 5, 80, 3*HZ, 20, {3,1,2,0,2}, 0,
367 0, { 7, 4, 8, 2, 1, 5, 3,10}, 3*HZ/2, 0 }, "unknown" },
369 {{1, 300, 16, 16, 8000, 1*HZ, 3*HZ, 0, SEL_DLY, 5, 40, 3*HZ, 17, {3,1,2,0,2}, 0,
370 0, { 1, 0, 0, 0, 0, 0, 0, 0}, 3*HZ/2, 1 }, "360K PC" }, /*5 1/4 360 KB PC*/
372 {{2, 500, 16, 16, 6000, 4*HZ/10, 3*HZ, 14, SEL_DLY, 6, 83, 3*HZ, 17, {3,1,2,0,2}, 0,
373 0, { 2, 5, 6,23,10,20,12, 0}, 3*HZ/2, 2 }, "1.2M" }, /*5 1/4 HD AT*/
375 {{3, 250, 16, 16, 3000, 1*HZ, 3*HZ, 0, SEL_DLY, 5, 83, 3*HZ, 20, {3,1,2,0,2}, 0,
376 0, { 4,22,21,30, 3, 0, 0, 0}, 3*HZ/2, 4 }, "720k" }, /*3 1/2 DD*/
378 {{4, 500, 16, 16, 4000, 4*HZ/10, 3*HZ, 10, SEL_DLY, 5, 83, 3*HZ, 20, {3,1,2,0,2}, 0,
379 0, { 7, 4,25,22,31,21,29,11}, 3*HZ/2, 7 }, "1.44M" }, /*3 1/2 HD*/
381 {{5, 1000, 15, 8, 3000, 4*HZ/10, 3*HZ, 10, SEL_DLY, 5, 83, 3*HZ, 40, {3,1,2,0,2}, 0,
382 0, { 7, 8, 4,25,28,22,31,21}, 3*HZ/2, 8 }, "2.88M AMI BIOS" }, /*3 1/2 ED*/
384 {{6, 1000, 15, 8, 3000, 4*HZ/10, 3*HZ, 10, SEL_DLY, 5, 83, 3*HZ, 40, {3,1,2,0,2}, 0,
385 0, { 7, 8, 4,25,28,22,31,21}, 3*HZ/2, 8 }, "2.88M" } /*3 1/2 ED*/
386 /* | --autodetected formats--- | | |
387 * read_track | | Name printed when booting
388 * | Native format
389 * Frequency of disk change checks */
392 static struct floppy_drive_params drive_params[N_DRIVE];
393 static struct floppy_drive_struct drive_state[N_DRIVE];
394 static struct floppy_write_errors write_errors[N_DRIVE];
395 static struct floppy_raw_cmd *raw_cmd, default_raw_cmd;
398 * This struct defines the different floppy types.
400 * Bit 0 of 'stretch' tells if the tracks need to be doubled for some
401 * types (e.g. 360kB diskette in 1.2MB drive, etc.). Bit 1 of 'stretch'
402 * tells if the disk is in Commodore 1581 format, which means side 0 sectors
403 * are located on side 1 of the disk but with a side 0 ID, and vice-versa.
404 * This is the same as the Sharp MZ-80 5.25" CP/M disk format, except that the
405 * 1581's logical side 0 is on physical side 1, whereas the Sharp's logical
406 * side 0 is on physical side 0 (but with the misnamed sector IDs).
407 * 'stretch' should probably be renamed to something more general, like
408 * 'options'. Other parameters should be self-explanatory (see also
409 * setfdprm(8)).
412 Size
413 | Sectors per track
414 | | Head
415 | | | Tracks
416 | | | | Stretch
417 | | | | | Gap 1 size
418 | | | | | | Data rate, | 0x40 for perp
419 | | | | | | | Spec1 (stepping rate, head unload
420 | | | | | | | | /fmt gap (gap2) */
421 static struct floppy_struct floppy_type[32] = {
422 { 0, 0,0, 0,0,0x00,0x00,0x00,0x00,NULL }, /* 0 no testing */
423 { 720, 9,2,40,0,0x2A,0x02,0xDF,0x50,"d360" }, /* 1 360KB PC */
424 { 2400,15,2,80,0,0x1B,0x00,0xDF,0x54,"h1200" }, /* 2 1.2MB AT */
425 { 720, 9,1,80,0,0x2A,0x02,0xDF,0x50,"D360" }, /* 3 360KB SS 3.5" */
426 { 1440, 9,2,80,0,0x2A,0x02,0xDF,0x50,"D720" }, /* 4 720KB 3.5" */
427 { 720, 9,2,40,1,0x23,0x01,0xDF,0x50,"h360" }, /* 5 360KB AT */
428 { 1440, 9,2,80,0,0x23,0x01,0xDF,0x50,"h720" }, /* 6 720KB AT */
429 { 2880,18,2,80,0,0x1B,0x00,0xCF,0x6C,"H1440" }, /* 7 1.44MB 3.5" */
430 { 5760,36,2,80,0,0x1B,0x43,0xAF,0x54,"E2880" }, /* 8 2.88MB 3.5" */
431 { 6240,39,2,80,0,0x1B,0x43,0xAF,0x28,"E3120" }, /* 9 3.12MB 3.5" */
433 { 2880,18,2,80,0,0x25,0x00,0xDF,0x02,"h1440" }, /* 10 1.44MB 5.25" */
434 { 3360,21,2,80,0,0x1C,0x00,0xCF,0x0C,"H1680" }, /* 11 1.68MB 3.5" */
435 { 820,10,2,41,1,0x25,0x01,0xDF,0x2E,"h410" }, /* 12 410KB 5.25" */
436 { 1640,10,2,82,0,0x25,0x02,0xDF,0x2E,"H820" }, /* 13 820KB 3.5" */
437 { 2952,18,2,82,0,0x25,0x00,0xDF,0x02,"h1476" }, /* 14 1.48MB 5.25" */
438 { 3444,21,2,82,0,0x25,0x00,0xDF,0x0C,"H1722" }, /* 15 1.72MB 3.5" */
439 { 840,10,2,42,1,0x25,0x01,0xDF,0x2E,"h420" }, /* 16 420KB 5.25" */
440 { 1660,10,2,83,0,0x25,0x02,0xDF,0x2E,"H830" }, /* 17 830KB 3.5" */
441 { 2988,18,2,83,0,0x25,0x00,0xDF,0x02,"h1494" }, /* 18 1.49MB 5.25" */
442 { 3486,21,2,83,0,0x25,0x00,0xDF,0x0C,"H1743" }, /* 19 1.74 MB 3.5" */
444 { 1760,11,2,80,0,0x1C,0x09,0xCF,0x00,"h880" }, /* 20 880KB 5.25" */
445 { 2080,13,2,80,0,0x1C,0x01,0xCF,0x00,"D1040" }, /* 21 1.04MB 3.5" */
446 { 2240,14,2,80,0,0x1C,0x19,0xCF,0x00,"D1120" }, /* 22 1.12MB 3.5" */
447 { 3200,20,2,80,0,0x1C,0x20,0xCF,0x2C,"h1600" }, /* 23 1.6MB 5.25" */
448 { 3520,22,2,80,0,0x1C,0x08,0xCF,0x2e,"H1760" }, /* 24 1.76MB 3.5" */
449 { 3840,24,2,80,0,0x1C,0x20,0xCF,0x00,"H1920" }, /* 25 1.92MB 3.5" */
450 { 6400,40,2,80,0,0x25,0x5B,0xCF,0x00,"E3200" }, /* 26 3.20MB 3.5" */
451 { 7040,44,2,80,0,0x25,0x5B,0xCF,0x00,"E3520" }, /* 27 3.52MB 3.5" */
452 { 7680,48,2,80,0,0x25,0x63,0xCF,0x00,"E3840" }, /* 28 3.84MB 3.5" */
454 { 3680,23,2,80,0,0x1C,0x10,0xCF,0x00,"H1840" }, /* 29 1.84MB 3.5" */
455 { 1600,10,2,80,0,0x25,0x02,0xDF,0x2E,"D800" }, /* 30 800KB 3.5" */
456 { 3200,20,2,80,0,0x1C,0x00,0xCF,0x2C,"H1600" }, /* 31 1.6MB 3.5" */
459 #define NUMBER(x) (sizeof(x) / sizeof(*(x)))
460 #define SECTSIZE (_FD_SECTSIZE(*floppy))
462 /* Auto-detection: Disk type used until the next media change occurs. */
463 static struct floppy_struct *current_type[N_DRIVE] = {
464 NULL, NULL, NULL, NULL,
465 NULL, NULL, NULL, NULL
469 * User-provided type information. current_type points to
470 * the respective entry of this array.
472 static struct floppy_struct user_params[N_DRIVE];
474 static int floppy_sizes[256];
475 static int floppy_blocksizes[256] = { 0, };
478 * The driver is trying to determine the correct media format
479 * while probing is set. rw_interrupt() clears it after a
480 * successful access.
482 static int probing = 0;
484 /* Synchronization of FDC access. */
485 #define FD_COMMAND_NONE -1
486 #define FD_COMMAND_ERROR 2
487 #define FD_COMMAND_OKAY 3
489 static volatile int command_status = FD_COMMAND_NONE;
490 static unsigned long fdc_busy = 0;
491 static DECLARE_WAIT_QUEUE_HEAD(fdc_wait);
492 static DECLARE_WAIT_QUEUE_HEAD(command_done);
494 #define NO_SIGNAL (!interruptible || !signal_pending(current))
495 #define CALL(x) if ((x) == -EINTR) return -EINTR
496 #define ECALL(x) if ((ret = (x))) return ret;
497 #define _WAIT(x,i) CALL(ret=wait_til_done((x),i))
498 #define WAIT(x) _WAIT((x),interruptible)
499 #define IWAIT(x) _WAIT((x),1)
501 /* Errors during formatting are counted here. */
502 static int format_errors;
504 /* Format request descriptor. */
505 static struct format_descr format_req;
508 * Rate is 0 for 500kb/s, 1 for 300kbps, 2 for 250kbps
509 * Spec1 is 0xSH, where S is stepping rate (F=1ms, E=2ms, D=3ms etc),
510 * H is head unload time (1=16ms, 2=32ms, etc)
514 * Track buffer
515 * Because these are written to by the DMA controller, they must
516 * not contain a 64k byte boundary crossing, or data will be
517 * corrupted/lost.
519 static char *floppy_track_buffer;
520 static int max_buffer_sectors;
522 static int *errors;
523 typedef void (*done_f)(int);
524 static struct cont_t {
525 void (*interrupt)(void); /* this is called after the interrupt of the
526 * main command */
527 void (*redo)(void); /* this is called to retry the operation */
528 void (*error)(void); /* this is called to tally an error */
529 done_f done; /* this is called to say if the operation has
530 * succeeded/failed */
531 } *cont;
533 static void floppy_ready(void);
534 static void floppy_start(void);
535 static void process_fd_request(void);
536 static void recalibrate_floppy(void);
537 static void floppy_shutdown(void);
539 static int floppy_grab_irq_and_dma(void);
540 static void floppy_release_irq_and_dma(void);
543 * The "reset" variable should be tested whenever an interrupt is scheduled,
544 * after the commands have been sent. This is to ensure that the driver doesn't
545 * get wedged when the interrupt doesn't come because of a failed command.
546 * reset doesn't need to be tested before sending commands, because
547 * output_byte is automatically disabled when reset is set.
549 #define CHECK_RESET { if (FDCS->reset){ reset_fdc(); return; } }
550 static void reset_fdc(void);
553 * These are global variables, as that's the easiest way to give
554 * information to interrupts. They are the data used for the current
555 * request.
557 #define NO_TRACK -1
558 #define NEED_1_RECAL -2
559 #define NEED_2_RECAL -3
561 /* */
562 static int usage_count = 0;
565 /* buffer related variables */
566 static int buffer_track = -1;
567 static int buffer_drive = -1;
568 static int buffer_min = -1;
569 static int buffer_max = -1;
571 /* fdc related variables, should end up in a struct */
572 static struct floppy_fdc_state fdc_state[N_FDC];
573 static int fdc; /* current fdc */
575 static struct floppy_struct *_floppy = floppy_type;
576 static unsigned char current_drive = 0;
577 static long current_count_sectors = 0;
578 static unsigned char sector_t; /* sector in track */
579 static unsigned char in_sector_offset; /* offset within physical sector,
580 * expressed in units of 512 bytes */
582 #ifndef fd_eject
583 #define fd_eject(x) -EINVAL
584 #endif
586 #ifdef DEBUGT
587 static long unsigned debugtimer;
588 #endif
591 * Debugging
592 * =========
594 static inline void set_debugt(void)
596 #ifdef DEBUGT
597 debugtimer = jiffies;
598 #endif
601 static inline void debugt(const char *message)
603 #ifdef DEBUGT
604 if (DP->flags & DEBUGT)
605 printk("%s dtime=%lu\n", message, jiffies-debugtimer);
606 #endif
609 typedef void (*timeout_fn)(unsigned long);
610 static struct timer_list fd_timeout ={ function: (timeout_fn) floppy_shutdown };
612 static const char *timeout_message;
614 #ifdef FLOPPY_SANITY_CHECK
615 static void is_alive(const char *message)
617 /* this routine checks whether the floppy driver is "alive" */
618 if (fdc_busy && command_status < 2 && !timer_pending(&fd_timeout)){
619 DPRINT("timeout handler died: %s\n",message);
622 #endif
624 #ifdef FLOPPY_SANITY_CHECK
626 #define OLOGSIZE 20
628 static void (*lasthandler)(void) = NULL;
629 static unsigned long interruptjiffies;
630 static unsigned long resultjiffies;
631 static int resultsize;
632 static unsigned long lastredo;
634 static struct output_log {
635 unsigned char data;
636 unsigned char status;
637 unsigned long jiffies;
638 } output_log[OLOGSIZE];
640 static int output_log_pos;
641 #endif
643 #define CURRENTD -1
644 #define MAXTIMEOUT -2
646 static void reschedule_timeout(int drive, const char *message, int marg)
648 if (drive == CURRENTD)
649 drive = current_drive;
650 del_timer(&fd_timeout);
651 if (drive < 0 || drive > N_DRIVE) {
652 fd_timeout.expires = jiffies + 20UL*HZ;
653 drive=0;
654 } else
655 fd_timeout.expires = jiffies + UDP->timeout;
656 add_timer(&fd_timeout);
657 if (UDP->flags & FD_DEBUG){
658 DPRINT("reschedule timeout ");
659 printk(message, marg);
660 printk("\n");
662 timeout_message = message;
665 static int maximum(int a, int b)
667 if (a > b)
668 return a;
669 else
670 return b;
672 #define INFBOUND(a,b) (a)=maximum((a),(b));
674 static int minimum(int a, int b)
676 if (a < b)
677 return a;
678 else
679 return b;
681 #define SUPBOUND(a,b) (a)=minimum((a),(b));
685 * Bottom half floppy driver.
686 * ==========================
688 * This part of the file contains the code talking directly to the hardware,
689 * and also the main service loop (seek-configure-spinup-command)
693 * disk change.
694 * This routine is responsible for maintaining the FD_DISK_CHANGE flag,
695 * and the last_checked date.
697 * last_checked is the date of the last check which showed 'no disk change'
698 * FD_DISK_CHANGE is set under two conditions:
699 * 1. The floppy has been changed after some i/o to that floppy already
700 * took place.
701 * 2. No floppy disk is in the drive. This is done in order to ensure that
702 * requests are quickly flushed in case there is no disk in the drive. It
703 * follows that FD_DISK_CHANGE can only be cleared if there is a disk in
704 * the drive.
706 * For 1., maxblock is observed. Maxblock is 0 if no i/o has taken place yet.
707 * For 2., FD_DISK_NEWCHANGE is watched. FD_DISK_NEWCHANGE is cleared on
708 * each seek. If a disk is present, the disk change line should also be
709 * cleared on each seek. Thus, if FD_DISK_NEWCHANGE is clear, but the disk
710 * change line is set, this means either that no disk is in the drive, or
711 * that it has been removed since the last seek.
713 * This means that we really have a third possibility too:
714 * The floppy has been changed after the last seek.
717 static int disk_change(int drive)
719 int fdc=FDC(drive);
720 #ifdef FLOPPY_SANITY_CHECK
721 if (jiffies - UDRS->select_date < UDP->select_delay)
722 DPRINT("WARNING disk change called early\n");
723 if (!(FDCS->dor & (0x10 << UNIT(drive))) ||
724 (FDCS->dor & 3) != UNIT(drive) ||
725 fdc != FDC(drive)){
726 DPRINT("probing disk change on unselected drive\n");
727 DPRINT("drive=%d fdc=%d dor=%x\n",drive, FDC(drive),
728 (unsigned int)FDCS->dor);
730 #endif
732 #ifdef DCL_DEBUG
733 if (UDP->flags & FD_DEBUG){
734 DPRINT("checking disk change line for drive %d\n",drive);
735 DPRINT("jiffies=%lu\n", jiffies);
736 DPRINT("disk change line=%x\n",fd_inb(FD_DIR)&0x80);
737 DPRINT("flags=%lx\n",UDRS->flags);
739 #endif
740 if (UDP->flags & FD_BROKEN_DCL)
741 return UTESTF(FD_DISK_CHANGED);
742 if ((fd_inb(FD_DIR) ^ UDP->flags) & 0x80){
743 USETF(FD_VERIFY); /* verify write protection */
744 if (UDRS->maxblock){
745 /* mark it changed */
746 USETF(FD_DISK_CHANGED);
749 /* invalidate its geometry */
750 if (UDRS->keep_data >= 0) {
751 if ((UDP->flags & FTD_MSG) &&
752 current_type[drive] != NULL)
753 DPRINT("Disk type is undefined after "
754 "disk change\n");
755 current_type[drive] = NULL;
756 floppy_sizes[TOMINOR(drive)] = MAX_DISK_SIZE;
759 /*USETF(FD_DISK_NEWCHANGE);*/
760 return 1;
761 } else {
762 UDRS->last_checked=jiffies;
763 UCLEARF(FD_DISK_NEWCHANGE);
765 return 0;
768 static inline int is_selected(int dor, int unit)
770 return ((dor & (0x10 << unit)) && (dor &3) == unit);
773 static int set_dor(int fdc, char mask, char data)
775 register unsigned char drive, unit, newdor,olddor;
777 if (FDCS->address == -1)
778 return -1;
780 olddor = FDCS->dor;
781 newdor = (olddor & mask) | data;
782 if (newdor != olddor){
783 unit = olddor & 0x3;
784 if (is_selected(olddor, unit) && !is_selected(newdor,unit)){
785 drive = REVDRIVE(fdc,unit);
786 #ifdef DCL_DEBUG
787 if (UDP->flags & FD_DEBUG){
788 DPRINT("calling disk change from set_dor\n");
790 #endif
791 disk_change(drive);
793 FDCS->dor = newdor;
794 fd_outb(newdor, FD_DOR);
796 unit = newdor & 0x3;
797 if (!is_selected(olddor, unit) && is_selected(newdor,unit)){
798 drive = REVDRIVE(fdc,unit);
799 UDRS->select_date = jiffies;
803 * We should propogate failures to grab the resources back
804 * nicely from here. Actually we ought to rewrite the fd
805 * driver some day too.
807 if (newdor & FLOPPY_MOTOR_MASK)
808 floppy_grab_irq_and_dma();
809 if (olddor & FLOPPY_MOTOR_MASK)
810 floppy_release_irq_and_dma();
811 return olddor;
814 static void twaddle(void)
816 if (DP->select_delay)
817 return;
818 fd_outb(FDCS->dor & ~(0x10<<UNIT(current_drive)), FD_DOR);
819 fd_outb(FDCS->dor, FD_DOR);
820 DRS->select_date = jiffies;
823 /* reset all driver information about the current fdc. This is needed after
824 * a reset, and after a raw command. */
825 static void reset_fdc_info(int mode)
827 int drive;
829 FDCS->spec1 = FDCS->spec2 = -1;
830 FDCS->need_configure = 1;
831 FDCS->perp_mode = 1;
832 FDCS->rawcmd = 0;
833 for (drive = 0; drive < N_DRIVE; drive++)
834 if (FDC(drive) == fdc &&
835 (mode || UDRS->track != NEED_1_RECAL))
836 UDRS->track = NEED_2_RECAL;
839 /* selects the fdc and drive, and enables the fdc's input/dma. */
840 static void set_fdc(int drive)
842 if (drive >= 0 && drive < N_DRIVE){
843 fdc = FDC(drive);
844 current_drive = drive;
846 if (fdc != 1 && fdc != 0) {
847 printk("bad fdc value\n");
848 return;
850 set_dor(fdc,~0,8);
851 #if N_FDC > 1
852 set_dor(1-fdc, ~8, 0);
853 #endif
854 if (FDCS->rawcmd == 2)
855 reset_fdc_info(1);
856 if (fd_inb(FD_STATUS) != STATUS_READY)
857 FDCS->reset = 1;
860 /* locks the driver */
861 static int _lock_fdc(int drive, int interruptible, int line)
863 if (!usage_count){
864 printk(KERN_ERR "Trying to lock fdc while usage count=0 at line %d\n", line);
865 return -1;
867 if(floppy_grab_irq_and_dma()==-1)
868 return -EBUSY;
870 if (test_and_set_bit(0, &fdc_busy)) {
871 DECLARE_WAITQUEUE(wait, current);
872 add_wait_queue(&fdc_wait, &wait);
874 for (;;) {
875 set_current_state(TASK_INTERRUPTIBLE);
877 if (!test_and_set_bit(0, &fdc_busy))
878 break;
880 schedule();
882 if (!NO_SIGNAL) {
883 remove_wait_queue(&fdc_wait, &wait);
884 return -EINTR;
888 set_current_state(TASK_RUNNING);
889 remove_wait_queue(&fdc_wait, &wait);
891 command_status = FD_COMMAND_NONE;
893 reschedule_timeout(drive, "lock fdc", 0);
894 set_fdc(drive);
895 return 0;
898 #define lock_fdc(drive,interruptible) _lock_fdc(drive,interruptible, __LINE__)
900 #define LOCK_FDC(drive,interruptible) \
901 if (lock_fdc(drive,interruptible)) return -EINTR;
904 /* unlocks the driver */
905 static inline void unlock_fdc(void)
907 raw_cmd = 0;
908 if (!fdc_busy)
909 DPRINT("FDC access conflict!\n");
911 if (DEVICE_INTR)
912 DPRINT("device interrupt still active at FDC release: %p!\n",
913 DEVICE_INTR);
914 command_status = FD_COMMAND_NONE;
915 del_timer(&fd_timeout);
916 cont = NULL;
917 clear_bit(0, &fdc_busy);
918 floppy_release_irq_and_dma();
919 wake_up(&fdc_wait);
922 /* switches the motor off after a given timeout */
923 static void motor_off_callback(unsigned long nr)
925 unsigned char mask = ~(0x10 << UNIT(nr));
927 set_dor(FDC(nr), mask, 0);
930 static struct timer_list motor_off_timer[N_DRIVE] = {
931 { data: 0, function: motor_off_callback },
932 { data: 1, function: motor_off_callback },
933 { data: 2, function: motor_off_callback },
934 { data: 3, function: motor_off_callback },
935 { data: 4, function: motor_off_callback },
936 { data: 5, function: motor_off_callback },
937 { data: 6, function: motor_off_callback },
938 { data: 7, function: motor_off_callback }
941 /* schedules motor off */
942 static void floppy_off(unsigned int drive)
944 unsigned long volatile delta;
945 register int fdc=FDC(drive);
947 if (!(FDCS->dor & (0x10 << UNIT(drive))))
948 return;
950 del_timer(motor_off_timer+drive);
952 /* make spindle stop in a position which minimizes spinup time
953 * next time */
954 if (UDP->rps){
955 delta = jiffies - UDRS->first_read_date + HZ -
956 UDP->spindown_offset;
957 delta = ((delta * UDP->rps) % HZ) / UDP->rps;
958 motor_off_timer[drive].expires = jiffies + UDP->spindown - delta;
960 add_timer(motor_off_timer+drive);
964 * cycle through all N_DRIVE floppy drives, for disk change testing.
965 * stopping at current drive. This is done before any long operation, to
966 * be sure to have up to date disk change information.
968 static void scandrives(void)
970 int i, drive, saved_drive;
972 if (DP->select_delay)
973 return;
975 saved_drive = current_drive;
976 for (i=0; i < N_DRIVE; i++){
977 drive = (saved_drive + i + 1) % N_DRIVE;
978 if (UDRS->fd_ref == 0 || UDP->select_delay != 0)
979 continue; /* skip closed drives */
980 set_fdc(drive);
981 if (!(set_dor(fdc, ~3, UNIT(drive) | (0x10 << UNIT(drive))) &
982 (0x10 << UNIT(drive))))
983 /* switch the motor off again, if it was off to
984 * begin with */
985 set_dor(fdc, ~(0x10 << UNIT(drive)), 0);
987 set_fdc(saved_drive);
990 static void empty(void)
994 static struct tq_struct floppy_tq =
995 { 0, 0, 0, 0 };
997 static void schedule_bh( void (*handler)(void*) )
999 floppy_tq.routine = (void *)(void *) handler;
1000 queue_task(&floppy_tq, &tq_immediate);
1001 mark_bh(IMMEDIATE_BH);
1004 static struct timer_list fd_timer;
1006 static void cancel_activity(void)
1008 CLEAR_INTR;
1009 floppy_tq.routine = (void *)(void *) empty;
1010 del_timer(&fd_timer);
1013 /* this function makes sure that the disk stays in the drive during the
1014 * transfer */
1015 static void fd_watchdog(void)
1017 #ifdef DCL_DEBUG
1018 if (DP->flags & FD_DEBUG){
1019 DPRINT("calling disk change from watchdog\n");
1021 #endif
1023 if (disk_change(current_drive)){
1024 DPRINT("disk removed during i/o\n");
1025 cancel_activity();
1026 cont->done(0);
1027 reset_fdc();
1028 } else {
1029 del_timer(&fd_timer);
1030 fd_timer.function = (timeout_fn) fd_watchdog;
1031 fd_timer.expires = jiffies + HZ / 10;
1032 add_timer(&fd_timer);
1036 static void main_command_interrupt(void)
1038 del_timer(&fd_timer);
1039 cont->interrupt();
1042 /* waits for a delay (spinup or select) to pass */
1043 static int wait_for_completion(unsigned long delay, timeout_fn function)
1045 if (FDCS->reset){
1046 reset_fdc(); /* do the reset during sleep to win time
1047 * if we don't need to sleep, it's a good
1048 * occasion anyways */
1049 return 1;
1052 if ((signed) (jiffies - delay) < 0){
1053 del_timer(&fd_timer);
1054 fd_timer.function = function;
1055 fd_timer.expires = delay;
1056 add_timer(&fd_timer);
1057 return 1;
1059 return 0;
1062 static spinlock_t floppy_hlt_lock = SPIN_LOCK_UNLOCKED;
1063 static int hlt_disabled;
1064 static void floppy_disable_hlt(void)
1066 unsigned long flags;
1068 spin_lock_irqsave(&floppy_hlt_lock, flags);
1069 if (!hlt_disabled) {
1070 hlt_disabled=1;
1071 #ifdef HAVE_DISABLE_HLT
1072 disable_hlt();
1073 #endif
1075 spin_unlock_irqrestore(&floppy_hlt_lock, flags);
1078 static void floppy_enable_hlt(void)
1080 unsigned long flags;
1082 spin_lock_irqsave(&floppy_hlt_lock, flags);
1083 if (hlt_disabled){
1084 hlt_disabled=0;
1085 #ifdef HAVE_DISABLE_HLT
1086 enable_hlt();
1087 #endif
1089 spin_unlock_irqrestore(&floppy_hlt_lock, flags);
1093 static void setup_DMA(void)
1095 unsigned long f;
1097 #ifdef FLOPPY_SANITY_CHECK
1098 if (raw_cmd->length == 0){
1099 int i;
1101 printk("zero dma transfer size:");
1102 for (i=0; i < raw_cmd->cmd_count; i++)
1103 printk("%x,", raw_cmd->cmd[i]);
1104 printk("\n");
1105 cont->done(0);
1106 FDCS->reset = 1;
1107 return;
1109 if (((unsigned long) raw_cmd->kernel_data) % 512){
1110 printk("non aligned address: %p\n", raw_cmd->kernel_data);
1111 cont->done(0);
1112 FDCS->reset=1;
1113 return;
1115 #endif
1116 f=claim_dma_lock();
1117 fd_disable_dma();
1118 #ifdef fd_dma_setup
1119 if (fd_dma_setup(raw_cmd->kernel_data, raw_cmd->length,
1120 (raw_cmd->flags & FD_RAW_READ)?
1121 DMA_MODE_READ : DMA_MODE_WRITE,
1122 FDCS->address) < 0) {
1123 release_dma_lock(f);
1124 cont->done(0);
1125 FDCS->reset=1;
1126 return;
1128 release_dma_lock(f);
1129 #else
1130 fd_clear_dma_ff();
1131 fd_cacheflush(raw_cmd->kernel_data, raw_cmd->length);
1132 fd_set_dma_mode((raw_cmd->flags & FD_RAW_READ)?
1133 DMA_MODE_READ : DMA_MODE_WRITE);
1134 fd_set_dma_addr(raw_cmd->kernel_data);
1135 fd_set_dma_count(raw_cmd->length);
1136 virtual_dma_port = FDCS->address;
1137 fd_enable_dma();
1138 release_dma_lock(f);
1139 #endif
1140 floppy_disable_hlt();
1143 static void show_floppy(void);
1145 /* waits until the fdc becomes ready */
1146 static int wait_til_ready(void)
1148 int counter, status;
1149 if (FDCS->reset)
1150 return -1;
1151 for (counter = 0; counter < 10000; counter++) {
1152 status = fd_inb(FD_STATUS);
1153 if (status & STATUS_READY)
1154 return status;
1156 if (!initialising) {
1157 DPRINT("Getstatus times out (%x) on fdc %d\n",
1158 status, fdc);
1159 show_floppy();
1161 FDCS->reset = 1;
1162 return -1;
1165 /* sends a command byte to the fdc */
1166 static int output_byte(char byte)
1168 int status;
1170 if ((status = wait_til_ready()) < 0)
1171 return -1;
1172 if ((status & (STATUS_READY|STATUS_DIR|STATUS_DMA)) == STATUS_READY){
1173 fd_outb(byte,FD_DATA);
1174 #ifdef FLOPPY_SANITY_CHECK
1175 output_log[output_log_pos].data = byte;
1176 output_log[output_log_pos].status = status;
1177 output_log[output_log_pos].jiffies = jiffies;
1178 output_log_pos = (output_log_pos + 1) % OLOGSIZE;
1179 #endif
1180 return 0;
1182 FDCS->reset = 1;
1183 if (!initialising) {
1184 DPRINT("Unable to send byte %x to FDC. Fdc=%x Status=%x\n",
1185 byte, fdc, status);
1186 show_floppy();
1188 return -1;
1190 #define LAST_OUT(x) if (output_byte(x)<0){ reset_fdc();return;}
1192 /* gets the response from the fdc */
1193 static int result(void)
1195 int i, status=0;
1197 for(i=0; i < MAX_REPLIES; i++) {
1198 if ((status = wait_til_ready()) < 0)
1199 break;
1200 status &= STATUS_DIR|STATUS_READY|STATUS_BUSY|STATUS_DMA;
1201 if ((status & ~STATUS_BUSY) == STATUS_READY){
1202 #ifdef FLOPPY_SANITY_CHECK
1203 resultjiffies = jiffies;
1204 resultsize = i;
1205 #endif
1206 return i;
1208 if (status == (STATUS_DIR|STATUS_READY|STATUS_BUSY))
1209 reply_buffer[i] = fd_inb(FD_DATA);
1210 else
1211 break;
1213 if (!initialising) {
1214 DPRINT("get result error. Fdc=%d Last status=%x Read bytes=%d\n",
1215 fdc, status, i);
1216 show_floppy();
1218 FDCS->reset = 1;
1219 return -1;
1222 #define MORE_OUTPUT -2
1223 /* does the fdc need more output? */
1224 static int need_more_output(void)
1226 int status;
1227 if ((status = wait_til_ready()) < 0)
1228 return -1;
1229 if ((status & (STATUS_READY|STATUS_DIR|STATUS_DMA)) == STATUS_READY)
1230 return MORE_OUTPUT;
1231 return result();
1234 /* Set perpendicular mode as required, based on data rate, if supported.
1235 * 82077 Now tested. 1Mbps data rate only possible with 82077-1.
1237 static inline void perpendicular_mode(void)
1239 unsigned char perp_mode;
1241 if (raw_cmd->rate & 0x40){
1242 switch(raw_cmd->rate & 3){
1243 case 0:
1244 perp_mode=2;
1245 break;
1246 case 3:
1247 perp_mode=3;
1248 break;
1249 default:
1250 DPRINT("Invalid data rate for perpendicular mode!\n");
1251 cont->done(0);
1252 FDCS->reset = 1; /* convenient way to return to
1253 * redo without to much hassle (deep
1254 * stack et al. */
1255 return;
1257 } else
1258 perp_mode = 0;
1260 if (FDCS->perp_mode == perp_mode)
1261 return;
1262 if (FDCS->version >= FDC_82077_ORIG) {
1263 output_byte(FD_PERPENDICULAR);
1264 output_byte(perp_mode);
1265 FDCS->perp_mode = perp_mode;
1266 } else if (perp_mode) {
1267 DPRINT("perpendicular mode not supported by this FDC.\n");
1269 } /* perpendicular_mode */
1271 static int fifo_depth = 0xa;
1272 static int no_fifo = 0;
1274 static int fdc_configure(void)
1276 /* Turn on FIFO */
1277 output_byte(FD_CONFIGURE);
1278 if (need_more_output() != MORE_OUTPUT)
1279 return 0;
1280 output_byte(0);
1281 output_byte(0x10 | (no_fifo & 0x20) | (fifo_depth & 0xf));
1282 output_byte(0); /* pre-compensation from track
1283 0 upwards */
1284 return 1;
1287 #define NOMINAL_DTR 500
1289 /* Issue a "SPECIFY" command to set the step rate time, head unload time,
1290 * head load time, and DMA disable flag to values needed by floppy.
1292 * The value "dtr" is the data transfer rate in Kbps. It is needed
1293 * to account for the data rate-based scaling done by the 82072 and 82077
1294 * FDC types. This parameter is ignored for other types of FDCs (i.e.
1295 * 8272a).
1297 * Note that changing the data transfer rate has a (probably deleterious)
1298 * effect on the parameters subject to scaling for 82072/82077 FDCs, so
1299 * fdc_specify is called again after each data transfer rate
1300 * change.
1302 * srt: 1000 to 16000 in microseconds
1303 * hut: 16 to 240 milliseconds
1304 * hlt: 2 to 254 milliseconds
1306 * These values are rounded up to the next highest available delay time.
1308 static void fdc_specify(void)
1310 unsigned char spec1, spec2;
1311 unsigned long srt, hlt, hut;
1312 unsigned long dtr = NOMINAL_DTR;
1313 unsigned long scale_dtr = NOMINAL_DTR;
1314 int hlt_max_code = 0x7f;
1315 int hut_max_code = 0xf;
1317 if (FDCS->need_configure && FDCS->version >= FDC_82072A) {
1318 fdc_configure();
1319 FDCS->need_configure = 0;
1320 /*DPRINT("FIFO enabled\n");*/
1323 switch (raw_cmd->rate & 0x03) {
1324 case 3:
1325 dtr = 1000;
1326 break;
1327 case 1:
1328 dtr = 300;
1329 if (FDCS->version >= FDC_82078) {
1330 /* chose the default rate table, not the one
1331 * where 1 = 2 Mbps */
1332 output_byte(FD_DRIVESPEC);
1333 if (need_more_output() == MORE_OUTPUT) {
1334 output_byte(UNIT(current_drive));
1335 output_byte(0xc0);
1338 break;
1339 case 2:
1340 dtr = 250;
1341 break;
1344 if (FDCS->version >= FDC_82072) {
1345 scale_dtr = dtr;
1346 hlt_max_code = 0x00; /* 0==256msec*dtr0/dtr (not linear!) */
1347 hut_max_code = 0x0; /* 0==256msec*dtr0/dtr (not linear!) */
1350 /* Convert step rate from microseconds to milliseconds and 4 bits */
1351 srt = 16 - (DP->srt*scale_dtr/1000 + NOMINAL_DTR - 1)/NOMINAL_DTR;
1352 if( slow_floppy ) {
1353 srt = srt / 4;
1355 SUPBOUND(srt, 0xf);
1356 INFBOUND(srt, 0);
1358 hlt = (DP->hlt*scale_dtr/2 + NOMINAL_DTR - 1)/NOMINAL_DTR;
1359 if (hlt < 0x01)
1360 hlt = 0x01;
1361 else if (hlt > 0x7f)
1362 hlt = hlt_max_code;
1364 hut = (DP->hut*scale_dtr/16 + NOMINAL_DTR - 1)/NOMINAL_DTR;
1365 if (hut < 0x1)
1366 hut = 0x1;
1367 else if (hut > 0xf)
1368 hut = hut_max_code;
1370 spec1 = (srt << 4) | hut;
1371 spec2 = (hlt << 1) | (use_virtual_dma & 1);
1373 /* If these parameters did not change, just return with success */
1374 if (FDCS->spec1 != spec1 || FDCS->spec2 != spec2) {
1375 /* Go ahead and set spec1 and spec2 */
1376 output_byte(FD_SPECIFY);
1377 output_byte(FDCS->spec1 = spec1);
1378 output_byte(FDCS->spec2 = spec2);
1380 } /* fdc_specify */
1382 /* Set the FDC's data transfer rate on behalf of the specified drive.
1383 * NOTE: with 82072/82077 FDCs, changing the data rate requires a reissue
1384 * of the specify command (i.e. using the fdc_specify function).
1386 static int fdc_dtr(void)
1388 /* If data rate not already set to desired value, set it. */
1389 if ((raw_cmd->rate & 3) == FDCS->dtr)
1390 return 0;
1392 /* Set dtr */
1393 fd_outb(raw_cmd->rate & 3, FD_DCR);
1395 /* TODO: some FDC/drive combinations (C&T 82C711 with TEAC 1.2MB)
1396 * need a stabilization period of several milliseconds to be
1397 * enforced after data rate changes before R/W operations.
1398 * Pause 5 msec to avoid trouble. (Needs to be 2 jiffies)
1400 FDCS->dtr = raw_cmd->rate & 3;
1401 return(wait_for_completion(jiffies+2UL*HZ/100,
1402 (timeout_fn) floppy_ready));
1403 } /* fdc_dtr */
1405 static void tell_sector(void)
1407 printk(": track %d, head %d, sector %d, size %d",
1408 R_TRACK, R_HEAD, R_SECTOR, R_SIZECODE);
1409 } /* tell_sector */
1413 * OK, this error interpreting routine is called after a
1414 * DMA read/write has succeeded
1415 * or failed, so we check the results, and copy any buffers.
1416 * hhb: Added better error reporting.
1417 * ak: Made this into a separate routine.
1419 static int interpret_errors(void)
1421 char bad;
1423 if (inr!=7) {
1424 DPRINT("-- FDC reply error");
1425 FDCS->reset = 1;
1426 return 1;
1429 /* check IC to find cause of interrupt */
1430 switch (ST0 & ST0_INTR) {
1431 case 0x40: /* error occurred during command execution */
1432 if (ST1 & ST1_EOC)
1433 return 0; /* occurs with pseudo-DMA */
1434 bad = 1;
1435 if (ST1 & ST1_WP) {
1436 DPRINT("Drive is write protected\n");
1437 CLEARF(FD_DISK_WRITABLE);
1438 cont->done(0);
1439 bad = 2;
1440 } else if (ST1 & ST1_ND) {
1441 SETF(FD_NEED_TWADDLE);
1442 } else if (ST1 & ST1_OR) {
1443 if (DP->flags & FTD_MSG)
1444 DPRINT("Over/Underrun - retrying\n");
1445 bad = 0;
1446 }else if (*errors >= DP->max_errors.reporting){
1447 DPRINT("");
1448 if (ST0 & ST0_ECE) {
1449 printk("Recalibrate failed!");
1450 } else if (ST2 & ST2_CRC) {
1451 printk("data CRC error");
1452 tell_sector();
1453 } else if (ST1 & ST1_CRC) {
1454 printk("CRC error");
1455 tell_sector();
1456 } else if ((ST1 & (ST1_MAM|ST1_ND)) || (ST2 & ST2_MAM)) {
1457 if (!probing) {
1458 printk("sector not found");
1459 tell_sector();
1460 } else
1461 printk("probe failed...");
1462 } else if (ST2 & ST2_WC) { /* seek error */
1463 printk("wrong cylinder");
1464 } else if (ST2 & ST2_BC) { /* cylinder marked as bad */
1465 printk("bad cylinder");
1466 } else {
1467 printk("unknown error. ST[0..2] are: 0x%x 0x%x 0x%x", ST0, ST1, ST2);
1468 tell_sector();
1470 printk("\n");
1473 if (ST2 & ST2_WC || ST2 & ST2_BC)
1474 /* wrong cylinder => recal */
1475 DRS->track = NEED_2_RECAL;
1476 return bad;
1477 case 0x80: /* invalid command given */
1478 DPRINT("Invalid FDC command given!\n");
1479 cont->done(0);
1480 return 2;
1481 case 0xc0:
1482 DPRINT("Abnormal termination caused by polling\n");
1483 cont->error();
1484 return 2;
1485 default: /* (0) Normal command termination */
1486 return 0;
1491 * This routine is called when everything should be correctly set up
1492 * for the transfer (i.e. floppy motor is on, the correct floppy is
1493 * selected, and the head is sitting on the right track).
1495 static void setup_rw_floppy(void)
1497 int i,r, flags,dflags;
1498 unsigned long ready_date;
1499 timeout_fn function;
1501 flags = raw_cmd->flags;
1502 if (flags & (FD_RAW_READ | FD_RAW_WRITE))
1503 flags |= FD_RAW_INTR;
1505 if ((flags & FD_RAW_SPIN) && !(flags & FD_RAW_NO_MOTOR)){
1506 ready_date = DRS->spinup_date + DP->spinup;
1507 /* If spinup will take a long time, rerun scandrives
1508 * again just before spinup completion. Beware that
1509 * after scandrives, we must again wait for selection.
1511 if ((signed) (ready_date - jiffies) > DP->select_delay){
1512 ready_date -= DP->select_delay;
1513 function = (timeout_fn) floppy_start;
1514 } else
1515 function = (timeout_fn) setup_rw_floppy;
1517 /* wait until the floppy is spinning fast enough */
1518 if (wait_for_completion(ready_date,function))
1519 return;
1521 dflags = DRS->flags;
1523 if ((flags & FD_RAW_READ) || (flags & FD_RAW_WRITE))
1524 setup_DMA();
1526 if (flags & FD_RAW_INTR)
1527 SET_INTR(main_command_interrupt);
1529 r=0;
1530 for (i=0; i< raw_cmd->cmd_count; i++)
1531 r|=output_byte(raw_cmd->cmd[i]);
1533 #ifdef DEBUGT
1534 debugt("rw_command: ");
1535 #endif
1536 if (r){
1537 cont->error();
1538 reset_fdc();
1539 return;
1542 if (!(flags & FD_RAW_INTR)){
1543 inr = result();
1544 cont->interrupt();
1545 } else if (flags & FD_RAW_NEED_DISK)
1546 fd_watchdog();
1549 static int blind_seek;
1552 * This is the routine called after every seek (or recalibrate) interrupt
1553 * from the floppy controller.
1555 static void seek_interrupt(void)
1557 #ifdef DEBUGT
1558 debugt("seek interrupt:");
1559 #endif
1560 if (inr != 2 || (ST0 & 0xF8) != 0x20) {
1561 DPRINT("seek failed\n");
1562 DRS->track = NEED_2_RECAL;
1563 cont->error();
1564 cont->redo();
1565 return;
1567 if (DRS->track >= 0 && DRS->track != ST1 && !blind_seek){
1568 #ifdef DCL_DEBUG
1569 if (DP->flags & FD_DEBUG){
1570 DPRINT("clearing NEWCHANGE flag because of effective seek\n");
1571 DPRINT("jiffies=%lu\n", jiffies);
1573 #endif
1574 CLEARF(FD_DISK_NEWCHANGE); /* effective seek */
1575 DRS->select_date = jiffies;
1577 DRS->track = ST1;
1578 floppy_ready();
1581 static void check_wp(void)
1583 if (TESTF(FD_VERIFY)) {
1584 /* check write protection */
1585 output_byte(FD_GETSTATUS);
1586 output_byte(UNIT(current_drive));
1587 if (result() != 1){
1588 FDCS->reset = 1;
1589 return;
1591 CLEARF(FD_VERIFY);
1592 CLEARF(FD_NEED_TWADDLE);
1593 #ifdef DCL_DEBUG
1594 if (DP->flags & FD_DEBUG){
1595 DPRINT("checking whether disk is write protected\n");
1596 DPRINT("wp=%x\n",ST3 & 0x40);
1598 #endif
1599 if (!(ST3 & 0x40))
1600 SETF(FD_DISK_WRITABLE);
1601 else
1602 CLEARF(FD_DISK_WRITABLE);
1606 static void seek_floppy(void)
1608 int track;
1610 blind_seek=0;
1612 #ifdef DCL_DEBUG
1613 if (DP->flags & FD_DEBUG){
1614 DPRINT("calling disk change from seek\n");
1616 #endif
1618 if (!TESTF(FD_DISK_NEWCHANGE) &&
1619 disk_change(current_drive) &&
1620 (raw_cmd->flags & FD_RAW_NEED_DISK)){
1621 /* the media changed flag should be cleared after the seek.
1622 * If it isn't, this means that there is really no disk in
1623 * the drive.
1625 SETF(FD_DISK_CHANGED);
1626 cont->done(0);
1627 cont->redo();
1628 return;
1630 if (DRS->track <= NEED_1_RECAL){
1631 recalibrate_floppy();
1632 return;
1633 } else if (TESTF(FD_DISK_NEWCHANGE) &&
1634 (raw_cmd->flags & FD_RAW_NEED_DISK) &&
1635 (DRS->track <= NO_TRACK || DRS->track == raw_cmd->track)) {
1636 /* we seek to clear the media-changed condition. Does anybody
1637 * know a more elegant way, which works on all drives? */
1638 if (raw_cmd->track)
1639 track = raw_cmd->track - 1;
1640 else {
1641 if (DP->flags & FD_SILENT_DCL_CLEAR){
1642 set_dor(fdc, ~(0x10 << UNIT(current_drive)), 0);
1643 blind_seek = 1;
1644 raw_cmd->flags |= FD_RAW_NEED_SEEK;
1646 track = 1;
1648 } else {
1649 check_wp();
1650 if (raw_cmd->track != DRS->track &&
1651 (raw_cmd->flags & FD_RAW_NEED_SEEK))
1652 track = raw_cmd->track;
1653 else {
1654 setup_rw_floppy();
1655 return;
1659 SET_INTR(seek_interrupt);
1660 output_byte(FD_SEEK);
1661 output_byte(UNIT(current_drive));
1662 LAST_OUT(track);
1663 #ifdef DEBUGT
1664 debugt("seek command:");
1665 #endif
1668 static void recal_interrupt(void)
1670 #ifdef DEBUGT
1671 debugt("recal interrupt:");
1672 #endif
1673 if (inr !=2)
1674 FDCS->reset = 1;
1675 else if (ST0 & ST0_ECE) {
1676 switch(DRS->track){
1677 case NEED_1_RECAL:
1678 #ifdef DEBUGT
1679 debugt("recal interrupt need 1 recal:");
1680 #endif
1681 /* after a second recalibrate, we still haven't
1682 * reached track 0. Probably no drive. Raise an
1683 * error, as failing immediately might upset
1684 * computers possessed by the Devil :-) */
1685 cont->error();
1686 cont->redo();
1687 return;
1688 case NEED_2_RECAL:
1689 #ifdef DEBUGT
1690 debugt("recal interrupt need 2 recal:");
1691 #endif
1692 /* If we already did a recalibrate,
1693 * and we are not at track 0, this
1694 * means we have moved. (The only way
1695 * not to move at recalibration is to
1696 * be already at track 0.) Clear the
1697 * new change flag */
1698 #ifdef DCL_DEBUG
1699 if (DP->flags & FD_DEBUG){
1700 DPRINT("clearing NEWCHANGE flag because of second recalibrate\n");
1702 #endif
1704 CLEARF(FD_DISK_NEWCHANGE);
1705 DRS->select_date = jiffies;
1706 /* fall through */
1707 default:
1708 #ifdef DEBUGT
1709 debugt("recal interrupt default:");
1710 #endif
1711 /* Recalibrate moves the head by at
1712 * most 80 steps. If after one
1713 * recalibrate we don't have reached
1714 * track 0, this might mean that we
1715 * started beyond track 80. Try
1716 * again. */
1717 DRS->track = NEED_1_RECAL;
1718 break;
1720 } else
1721 DRS->track = ST1;
1722 floppy_ready();
1725 static void print_result(char *message, int inr)
1727 int i;
1729 DPRINT("%s ", message);
1730 if (inr >= 0)
1731 for (i=0; i<inr; i++)
1732 printk("repl[%d]=%x ", i, reply_buffer[i]);
1733 printk("\n");
1736 /* interrupt handler. Note that this can be called externally on the Sparc */
1737 void floppy_interrupt(int irq, void *dev_id, struct pt_regs * regs)
1739 void (*handler)(void) = DEVICE_INTR;
1740 int do_print;
1741 unsigned long f;
1743 lasthandler = handler;
1744 interruptjiffies = jiffies;
1746 f=claim_dma_lock();
1747 fd_disable_dma();
1748 release_dma_lock(f);
1750 floppy_enable_hlt();
1751 CLEAR_INTR;
1752 if (fdc >= N_FDC || FDCS->address == -1){
1753 /* we don't even know which FDC is the culprit */
1754 printk("DOR0=%x\n", fdc_state[0].dor);
1755 printk("floppy interrupt on bizarre fdc %d\n",fdc);
1756 printk("handler=%p\n", handler);
1757 is_alive("bizarre fdc");
1758 return;
1761 FDCS->reset = 0;
1762 /* We have to clear the reset flag here, because apparently on boxes
1763 * with level triggered interrupts (PS/2, Sparc, ...), it is needed to
1764 * emit SENSEI's to clear the interrupt line. And FDCS->reset blocks the
1765 * emission of the SENSEI's.
1766 * It is OK to emit floppy commands because we are in an interrupt
1767 * handler here, and thus we have to fear no interference of other
1768 * activity.
1771 do_print = !handler && print_unex && !initialising;
1773 inr = result();
1774 if (do_print)
1775 print_result("unexpected interrupt", inr);
1776 if (inr == 0){
1777 int max_sensei = 4;
1778 do {
1779 output_byte(FD_SENSEI);
1780 inr = result();
1781 if (do_print)
1782 print_result("sensei", inr);
1783 max_sensei--;
1784 } while ((ST0 & 0x83) != UNIT(current_drive) && inr == 2 && max_sensei);
1786 if (handler) {
1787 schedule_bh( (void *)(void *) handler);
1788 } else
1789 FDCS->reset = 1;
1790 is_alive("normal interrupt end");
1793 static void recalibrate_floppy(void)
1795 #ifdef DEBUGT
1796 debugt("recalibrate floppy:");
1797 #endif
1798 SET_INTR(recal_interrupt);
1799 output_byte(FD_RECALIBRATE);
1800 LAST_OUT(UNIT(current_drive));
1804 * Must do 4 FD_SENSEIs after reset because of ``drive polling''.
1806 static void reset_interrupt(void)
1808 #ifdef DEBUGT
1809 debugt("reset interrupt:");
1810 #endif
1811 result(); /* get the status ready for set_fdc */
1812 if (FDCS->reset) {
1813 printk("reset set in interrupt, calling %p\n", cont->error);
1814 cont->error(); /* a reset just after a reset. BAD! */
1816 cont->redo();
1820 * reset is done by pulling bit 2 of DOR low for a while (old FDCs),
1821 * or by setting the self clearing bit 7 of STATUS (newer FDCs)
1823 static void reset_fdc(void)
1825 unsigned long flags;
1827 SET_INTR(reset_interrupt);
1828 FDCS->reset = 0;
1829 reset_fdc_info(0);
1831 /* Pseudo-DMA may intercept 'reset finished' interrupt. */
1832 /* Irrelevant for systems with true DMA (i386). */
1834 flags=claim_dma_lock();
1835 fd_disable_dma();
1836 release_dma_lock(flags);
1838 if (FDCS->version >= FDC_82072A)
1839 fd_outb(0x80 | (FDCS->dtr &3), FD_STATUS);
1840 else {
1841 fd_outb(FDCS->dor & ~0x04, FD_DOR);
1842 udelay(FD_RESET_DELAY);
1843 fd_outb(FDCS->dor, FD_DOR);
1847 static void show_floppy(void)
1849 int i;
1851 printk("\n");
1852 printk("floppy driver state\n");
1853 printk("-------------------\n");
1854 printk("now=%lu last interrupt=%lu diff=%lu last called handler=%p\n",
1855 jiffies, interruptjiffies, jiffies-interruptjiffies, lasthandler);
1858 #ifdef FLOPPY_SANITY_CHECK
1859 printk("timeout_message=%s\n", timeout_message);
1860 printk("last output bytes:\n");
1861 for (i=0; i < OLOGSIZE; i++)
1862 printk("%2x %2x %lu\n",
1863 output_log[(i+output_log_pos) % OLOGSIZE].data,
1864 output_log[(i+output_log_pos) % OLOGSIZE].status,
1865 output_log[(i+output_log_pos) % OLOGSIZE].jiffies);
1866 printk("last result at %lu\n", resultjiffies);
1867 printk("last redo_fd_request at %lu\n", lastredo);
1868 for (i=0; i<resultsize; i++){
1869 printk("%2x ", reply_buffer[i]);
1871 printk("\n");
1872 #endif
1874 printk("status=%x\n", fd_inb(FD_STATUS));
1875 printk("fdc_busy=%lu\n", fdc_busy);
1876 if (DEVICE_INTR)
1877 printk("DEVICE_INTR=%p\n", DEVICE_INTR);
1878 if (floppy_tq.sync)
1879 printk("floppy_tq.routine=%p\n", floppy_tq.routine);
1880 if (timer_pending(&fd_timer))
1881 printk("fd_timer.function=%p\n", fd_timer.function);
1882 if (timer_pending(&fd_timeout)){
1883 printk("timer_function=%p\n",fd_timeout.function);
1884 printk("expires=%lu\n",fd_timeout.expires-jiffies);
1885 printk("now=%lu\n",jiffies);
1887 printk("cont=%p\n", cont);
1888 printk("CURRENT=%p\n", CURRENT);
1889 printk("command_status=%d\n", command_status);
1890 printk("\n");
1893 static void floppy_shutdown(void)
1895 unsigned long flags;
1897 if (!initialising)
1898 show_floppy();
1899 cancel_activity();
1901 floppy_enable_hlt();
1903 flags=claim_dma_lock();
1904 fd_disable_dma();
1905 release_dma_lock(flags);
1907 /* avoid dma going to a random drive after shutdown */
1909 if (!initialising)
1910 DPRINT("floppy timeout called\n");
1911 FDCS->reset = 1;
1912 if (cont){
1913 cont->done(0);
1914 cont->redo(); /* this will recall reset when needed */
1915 } else {
1916 printk("no cont in shutdown!\n");
1917 process_fd_request();
1919 is_alive("floppy shutdown");
1921 /*typedef void (*timeout_fn)(unsigned long);*/
1923 /* start motor, check media-changed condition and write protection */
1924 static int start_motor(void (*function)(void) )
1926 int mask, data;
1928 mask = 0xfc;
1929 data = UNIT(current_drive);
1930 if (!(raw_cmd->flags & FD_RAW_NO_MOTOR)){
1931 if (!(FDCS->dor & (0x10 << UNIT(current_drive)))){
1932 set_debugt();
1933 /* no read since this drive is running */
1934 DRS->first_read_date = 0;
1935 /* note motor start time if motor is not yet running */
1936 DRS->spinup_date = jiffies;
1937 data |= (0x10 << UNIT(current_drive));
1939 } else
1940 if (FDCS->dor & (0x10 << UNIT(current_drive)))
1941 mask &= ~(0x10 << UNIT(current_drive));
1943 /* starts motor and selects floppy */
1944 del_timer(motor_off_timer + current_drive);
1945 set_dor(fdc, mask, data);
1947 /* wait_for_completion also schedules reset if needed. */
1948 return(wait_for_completion(DRS->select_date+DP->select_delay,
1949 (timeout_fn) function));
1952 static void floppy_ready(void)
1954 CHECK_RESET;
1955 if (start_motor(floppy_ready)) return;
1956 if (fdc_dtr()) return;
1958 #ifdef DCL_DEBUG
1959 if (DP->flags & FD_DEBUG){
1960 DPRINT("calling disk change from floppy_ready\n");
1962 #endif
1963 if (!(raw_cmd->flags & FD_RAW_NO_MOTOR) &&
1964 disk_change(current_drive) &&
1965 !DP->select_delay)
1966 twaddle(); /* this clears the dcl on certain drive/controller
1967 * combinations */
1969 #ifdef fd_chose_dma_mode
1970 if ((raw_cmd->flags & FD_RAW_READ) ||
1971 (raw_cmd->flags & FD_RAW_WRITE))
1973 unsigned long flags = claim_dma_lock();
1974 fd_chose_dma_mode(raw_cmd->kernel_data,
1975 raw_cmd->length);
1976 release_dma_lock(flags);
1978 #endif
1980 if (raw_cmd->flags & (FD_RAW_NEED_SEEK | FD_RAW_NEED_DISK)){
1981 perpendicular_mode();
1982 fdc_specify(); /* must be done here because of hut, hlt ... */
1983 seek_floppy();
1984 } else {
1985 if ((raw_cmd->flags & FD_RAW_READ) ||
1986 (raw_cmd->flags & FD_RAW_WRITE))
1987 fdc_specify();
1988 setup_rw_floppy();
1992 static void floppy_start(void)
1994 reschedule_timeout(CURRENTD, "floppy start", 0);
1996 scandrives();
1997 #ifdef DCL_DEBUG
1998 if (DP->flags & FD_DEBUG){
1999 DPRINT("setting NEWCHANGE in floppy_start\n");
2001 #endif
2002 SETF(FD_DISK_NEWCHANGE);
2003 floppy_ready();
2007 * ========================================================================
2008 * here ends the bottom half. Exported routines are:
2009 * floppy_start, floppy_off, floppy_ready, lock_fdc, unlock_fdc, set_fdc,
2010 * start_motor, reset_fdc, reset_fdc_info, interpret_errors.
2011 * Initialization also uses output_byte, result, set_dor, floppy_interrupt
2012 * and set_dor.
2013 * ========================================================================
2016 * General purpose continuations.
2017 * ==============================
2020 static void do_wakeup(void)
2022 reschedule_timeout(MAXTIMEOUT, "do wakeup", 0);
2023 cont = 0;
2024 command_status += 2;
2025 wake_up(&command_done);
2028 static struct cont_t wakeup_cont={
2029 empty,
2030 do_wakeup,
2031 empty,
2032 (done_f)empty
2036 static struct cont_t intr_cont={
2037 empty,
2038 process_fd_request,
2039 empty,
2040 (done_f) empty
2043 static int wait_til_done(void (*handler)(void), int interruptible)
2045 int ret;
2047 schedule_bh((void *)(void *)handler);
2049 if (command_status < 2 && NO_SIGNAL) {
2050 DECLARE_WAITQUEUE(wait, current);
2052 add_wait_queue(&command_done, &wait);
2053 for (;;) {
2054 set_current_state(interruptible?
2055 TASK_INTERRUPTIBLE:
2056 TASK_UNINTERRUPTIBLE);
2058 if (command_status >= 2 || !NO_SIGNAL)
2059 break;
2061 is_alive("wait_til_done");
2063 schedule();
2066 set_current_state(TASK_RUNNING);
2067 remove_wait_queue(&command_done, &wait);
2070 if (command_status < 2){
2071 cancel_activity();
2072 cont = &intr_cont;
2073 reset_fdc();
2074 return -EINTR;
2077 if (FDCS->reset)
2078 command_status = FD_COMMAND_ERROR;
2079 if (command_status == FD_COMMAND_OKAY)
2080 ret=0;
2081 else
2082 ret=-EIO;
2083 command_status = FD_COMMAND_NONE;
2084 return ret;
2087 static void generic_done(int result)
2089 command_status = result;
2090 cont = &wakeup_cont;
2093 static void generic_success(void)
2095 cont->done(1);
2098 static void generic_failure(void)
2100 cont->done(0);
2103 static void success_and_wakeup(void)
2105 generic_success();
2106 cont->redo();
2111 * formatting and rw support.
2112 * ==========================
2115 static int next_valid_format(void)
2117 int probed_format;
2119 probed_format = DRS->probed_format;
2120 while(1){
2121 if (probed_format >= 8 ||
2122 !DP->autodetect[probed_format]){
2123 DRS->probed_format = 0;
2124 return 1;
2126 if (floppy_type[DP->autodetect[probed_format]].sect){
2127 DRS->probed_format = probed_format;
2128 return 0;
2130 probed_format++;
2134 static void bad_flp_intr(void)
2136 if (probing){
2137 DRS->probed_format++;
2138 if (!next_valid_format())
2139 return;
2141 (*errors)++;
2142 INFBOUND(DRWE->badness, *errors);
2143 if (*errors > DP->max_errors.abort)
2144 cont->done(0);
2145 if (*errors > DP->max_errors.reset)
2146 FDCS->reset = 1;
2147 else if (*errors > DP->max_errors.recal)
2148 DRS->track = NEED_2_RECAL;
2151 static void set_floppy(kdev_t device)
2153 if (TYPE(device))
2154 _floppy = TYPE(device) + floppy_type;
2155 else
2156 _floppy = current_type[ DRIVE(device) ];
2160 * formatting support.
2161 * ===================
2163 static void format_interrupt(void)
2165 switch (interpret_errors()){
2166 case 1:
2167 cont->error();
2168 case 2:
2169 break;
2170 case 0:
2171 cont->done(1);
2173 cont->redo();
2176 #define CODE2SIZE (ssize = ((1 << SIZECODE) + 3) >> 2)
2177 #define FM_MODE(x,y) ((y) & ~(((x)->rate & 0x80) >>1))
2178 #define CT(x) ((x) | 0xc0)
2179 static void setup_format_params(int track)
2181 struct fparm {
2182 unsigned char track,head,sect,size;
2183 } *here = (struct fparm *)floppy_track_buffer;
2184 int il,n;
2185 int count,head_shift,track_shift;
2187 raw_cmd = &default_raw_cmd;
2188 raw_cmd->track = track;
2190 raw_cmd->flags = FD_RAW_WRITE | FD_RAW_INTR | FD_RAW_SPIN |
2191 FD_RAW_NEED_DISK | FD_RAW_NEED_SEEK;
2192 raw_cmd->rate = _floppy->rate & 0x43;
2193 raw_cmd->cmd_count = NR_F;
2194 COMMAND = FM_MODE(_floppy,FD_FORMAT);
2195 DR_SELECT = UNIT(current_drive) + PH_HEAD(_floppy,format_req.head);
2196 F_SIZECODE = FD_SIZECODE(_floppy);
2197 F_SECT_PER_TRACK = _floppy->sect << 2 >> F_SIZECODE;
2198 F_GAP = _floppy->fmt_gap;
2199 F_FILL = FD_FILL_BYTE;
2201 raw_cmd->kernel_data = floppy_track_buffer;
2202 raw_cmd->length = 4 * F_SECT_PER_TRACK;
2204 /* allow for about 30ms for data transport per track */
2205 head_shift = (F_SECT_PER_TRACK + 5) / 6;
2207 /* a ``cylinder'' is two tracks plus a little stepping time */
2208 track_shift = 2 * head_shift + 3;
2210 /* position of logical sector 1 on this track */
2211 n = (track_shift * format_req.track + head_shift * format_req.head)
2212 % F_SECT_PER_TRACK;
2214 /* determine interleave */
2215 il = 1;
2216 if (_floppy->fmt_gap < 0x22)
2217 il++;
2219 /* initialize field */
2220 for (count = 0; count < F_SECT_PER_TRACK; ++count) {
2221 here[count].track = format_req.track;
2222 here[count].head = format_req.head;
2223 here[count].sect = 0;
2224 here[count].size = F_SIZECODE;
2226 /* place logical sectors */
2227 for (count = 1; count <= F_SECT_PER_TRACK; ++count) {
2228 here[n].sect = count;
2229 n = (n+il) % F_SECT_PER_TRACK;
2230 if (here[n].sect) { /* sector busy, find next free sector */
2231 ++n;
2232 if (n>= F_SECT_PER_TRACK) {
2233 n-=F_SECT_PER_TRACK;
2234 while (here[n].sect) ++n;
2240 static void redo_format(void)
2242 buffer_track = -1;
2243 setup_format_params(format_req.track << STRETCH(_floppy));
2244 floppy_start();
2245 #ifdef DEBUGT
2246 debugt("queue format request");
2247 #endif
2250 static struct cont_t format_cont={
2251 format_interrupt,
2252 redo_format,
2253 bad_flp_intr,
2254 generic_done };
2256 static int do_format(kdev_t device, struct format_descr *tmp_format_req)
2258 int ret;
2259 int drive=DRIVE(device);
2261 LOCK_FDC(drive,1);
2262 set_floppy(device);
2263 if (!_floppy ||
2264 _floppy->track > DP->tracks ||
2265 tmp_format_req->track >= _floppy->track ||
2266 tmp_format_req->head >= _floppy->head ||
2267 (_floppy->sect << 2) % (1 << FD_SIZECODE(_floppy)) ||
2268 !_floppy->fmt_gap) {
2269 process_fd_request();
2270 return -EINVAL;
2272 format_req = *tmp_format_req;
2273 format_errors = 0;
2274 cont = &format_cont;
2275 errors = &format_errors;
2276 IWAIT(redo_format);
2277 process_fd_request();
2278 return ret;
2282 * Buffer read/write and support
2283 * =============================
2286 /* new request_done. Can handle physical sectors which are smaller than a
2287 * logical buffer */
2288 static void request_done(int uptodate)
2290 int block;
2292 probing = 0;
2293 reschedule_timeout(MAXTIMEOUT, "request done %d", uptodate);
2295 if (QUEUE_EMPTY){
2296 DPRINT("request list destroyed in floppy request done\n");
2297 return;
2300 if (uptodate){
2301 /* maintain values for invalidation on geometry
2302 * change */
2303 block = current_count_sectors + CURRENT->sector;
2304 INFBOUND(DRS->maxblock, block);
2305 if (block > _floppy->sect)
2306 DRS->maxtrack = 1;
2308 /* unlock chained buffers */
2309 while (current_count_sectors && !QUEUE_EMPTY &&
2310 current_count_sectors >= CURRENT->current_nr_sectors){
2311 current_count_sectors -= CURRENT->current_nr_sectors;
2312 CURRENT->nr_sectors -= CURRENT->current_nr_sectors;
2313 CURRENT->sector += CURRENT->current_nr_sectors;
2314 end_request(1);
2316 if (current_count_sectors && !QUEUE_EMPTY){
2317 /* "unlock" last subsector */
2318 CURRENT->buffer += current_count_sectors <<9;
2319 CURRENT->current_nr_sectors -= current_count_sectors;
2320 CURRENT->nr_sectors -= current_count_sectors;
2321 CURRENT->sector += current_count_sectors;
2322 return;
2325 if (current_count_sectors && QUEUE_EMPTY)
2326 DPRINT("request list destroyed in floppy request done\n");
2328 } else {
2329 if (CURRENT->cmd == WRITE) {
2330 /* record write error information */
2331 DRWE->write_errors++;
2332 if (DRWE->write_errors == 1) {
2333 DRWE->first_error_sector = CURRENT->sector;
2334 DRWE->first_error_generation = DRS->generation;
2336 DRWE->last_error_sector = CURRENT->sector;
2337 DRWE->last_error_generation = DRS->generation;
2339 end_request(0);
2343 /* Interrupt handler evaluating the result of the r/w operation */
2344 static void rw_interrupt(void)
2346 int nr_sectors, ssize, eoc, heads;
2348 if (R_HEAD >= 2) {
2349 /* some Toshiba floppy controllers occasionnally seem to
2350 * return bogus interrupts after read/write operations, which
2351 * can be recognized by a bad head number (>= 2) */
2352 return;
2355 if (!DRS->first_read_date)
2356 DRS->first_read_date = jiffies;
2358 nr_sectors = 0;
2359 CODE2SIZE;
2361 if (ST1 & ST1_EOC)
2362 eoc = 1;
2363 else
2364 eoc = 0;
2366 if (COMMAND & 0x80)
2367 heads = 2;
2368 else
2369 heads = 1;
2371 nr_sectors = (((R_TRACK-TRACK) * heads +
2372 R_HEAD-HEAD) * SECT_PER_TRACK +
2373 R_SECTOR-SECTOR + eoc) << SIZECODE >> 2;
2375 #ifdef FLOPPY_SANITY_CHECK
2376 if (nr_sectors / ssize >
2377 (in_sector_offset + current_count_sectors + ssize - 1) / ssize) {
2378 DPRINT("long rw: %x instead of %lx\n",
2379 nr_sectors, current_count_sectors);
2380 printk("rs=%d s=%d\n", R_SECTOR, SECTOR);
2381 printk("rh=%d h=%d\n", R_HEAD, HEAD);
2382 printk("rt=%d t=%d\n", R_TRACK, TRACK);
2383 printk("heads=%d eoc=%d\n", heads, eoc);
2384 printk("spt=%d st=%d ss=%d\n", SECT_PER_TRACK,
2385 sector_t, ssize);
2386 printk("in_sector_offset=%d\n", in_sector_offset);
2388 #endif
2390 nr_sectors -= in_sector_offset;
2391 INFBOUND(nr_sectors,0);
2392 SUPBOUND(current_count_sectors, nr_sectors);
2394 switch (interpret_errors()){
2395 case 2:
2396 cont->redo();
2397 return;
2398 case 1:
2399 if (!current_count_sectors){
2400 cont->error();
2401 cont->redo();
2402 return;
2404 break;
2405 case 0:
2406 if (!current_count_sectors){
2407 cont->redo();
2408 return;
2410 current_type[current_drive] = _floppy;
2411 floppy_sizes[TOMINOR(current_drive) ]=
2412 (_floppy->size+1)>>1;
2413 break;
2416 if (probing) {
2417 if (DP->flags & FTD_MSG)
2418 DPRINT("Auto-detected floppy type %s in fd%d\n",
2419 _floppy->name,current_drive);
2420 current_type[current_drive] = _floppy;
2421 floppy_sizes[TOMINOR(current_drive)] = (_floppy->size+1) >> 1;
2422 probing = 0;
2425 if (CT(COMMAND) != FD_READ ||
2426 raw_cmd->kernel_data == CURRENT->buffer){
2427 /* transfer directly from buffer */
2428 cont->done(1);
2429 } else if (CT(COMMAND) == FD_READ){
2430 buffer_track = raw_cmd->track;
2431 buffer_drive = current_drive;
2432 INFBOUND(buffer_max, nr_sectors + sector_t);
2434 cont->redo();
2437 /* Compute maximal contiguous buffer size. */
2438 static int buffer_chain_size(void)
2440 struct buffer_head *bh;
2441 int size;
2442 char *base;
2444 base = CURRENT->buffer;
2445 size = CURRENT->current_nr_sectors << 9;
2446 bh = CURRENT->bh;
2448 if (bh){
2449 bh = bh->b_reqnext;
2450 while (bh && bh->b_data == base + size){
2451 size += bh->b_size;
2452 bh = bh->b_reqnext;
2455 return size >> 9;
2458 /* Compute the maximal transfer size */
2459 static int transfer_size(int ssize, int max_sector, int max_size)
2461 SUPBOUND(max_sector, sector_t + max_size);
2463 /* alignment */
2464 max_sector -= (max_sector % _floppy->sect) % ssize;
2466 /* transfer size, beginning not aligned */
2467 current_count_sectors = max_sector - sector_t ;
2469 return max_sector;
2473 * Move data from/to the track buffer to/from the buffer cache.
2475 static void copy_buffer(int ssize, int max_sector, int max_sector_2)
2477 int remaining; /* number of transferred 512-byte sectors */
2478 struct buffer_head *bh;
2479 char *buffer, *dma_buffer;
2480 int size;
2482 max_sector = transfer_size(ssize,
2483 minimum(max_sector, max_sector_2),
2484 CURRENT->nr_sectors);
2486 if (current_count_sectors <= 0 && CT(COMMAND) == FD_WRITE &&
2487 buffer_max > sector_t + CURRENT->nr_sectors)
2488 current_count_sectors = minimum(buffer_max - sector_t,
2489 CURRENT->nr_sectors);
2491 remaining = current_count_sectors << 9;
2492 #ifdef FLOPPY_SANITY_CHECK
2493 if ((remaining >> 9) > CURRENT->nr_sectors &&
2494 CT(COMMAND) == FD_WRITE){
2495 DPRINT("in copy buffer\n");
2496 printk("current_count_sectors=%ld\n", current_count_sectors);
2497 printk("remaining=%d\n", remaining >> 9);
2498 printk("CURRENT->nr_sectors=%ld\n",CURRENT->nr_sectors);
2499 printk("CURRENT->current_nr_sectors=%ld\n",
2500 CURRENT->current_nr_sectors);
2501 printk("max_sector=%d\n", max_sector);
2502 printk("ssize=%d\n", ssize);
2504 #endif
2506 buffer_max = maximum(max_sector, buffer_max);
2508 dma_buffer = floppy_track_buffer + ((sector_t - buffer_min) << 9);
2510 bh = CURRENT->bh;
2511 size = CURRENT->current_nr_sectors << 9;
2512 buffer = CURRENT->buffer;
2514 while (remaining > 0){
2515 SUPBOUND(size, remaining);
2516 #ifdef FLOPPY_SANITY_CHECK
2517 if (dma_buffer + size >
2518 floppy_track_buffer + (max_buffer_sectors << 10) ||
2519 dma_buffer < floppy_track_buffer){
2520 DPRINT("buffer overrun in copy buffer %d\n",
2521 (int) ((floppy_track_buffer - dma_buffer) >>9));
2522 printk("sector_t=%d buffer_min=%d\n",
2523 sector_t, buffer_min);
2524 printk("current_count_sectors=%ld\n",
2525 current_count_sectors);
2526 if (CT(COMMAND) == FD_READ)
2527 printk("read\n");
2528 if (CT(COMMAND) == FD_READ)
2529 printk("write\n");
2530 break;
2532 if (((unsigned long)buffer) % 512)
2533 DPRINT("%p buffer not aligned\n", buffer);
2534 #endif
2535 if (CT(COMMAND) == FD_READ)
2536 memcpy(buffer, dma_buffer, size);
2537 else
2538 memcpy(dma_buffer, buffer, size);
2539 remaining -= size;
2540 if (!remaining)
2541 break;
2543 dma_buffer += size;
2544 bh = bh->b_reqnext;
2545 #ifdef FLOPPY_SANITY_CHECK
2546 if (!bh){
2547 DPRINT("bh=null in copy buffer after copy\n");
2548 break;
2550 #endif
2551 size = bh->b_size;
2552 buffer = bh->b_data;
2554 #ifdef FLOPPY_SANITY_CHECK
2555 if (remaining){
2556 if (remaining > 0)
2557 max_sector -= remaining >> 9;
2558 DPRINT("weirdness: remaining %d\n", remaining>>9);
2560 #endif
2563 #if 0
2564 static inline int check_dma_crossing(char *start,
2565 unsigned long length, char *message)
2567 if (CROSS_64KB(start, length)) {
2568 printk("DMA xfer crosses 64KB boundary in %s %p-%p\n",
2569 message, start, start+length);
2570 return 1;
2571 } else
2572 return 0;
2574 #endif
2576 /* work around a bug in pseudo DMA
2577 * (on some FDCs) pseudo DMA does not stop when the CPU stops
2578 * sending data. Hence we need a different way to signal the
2579 * transfer length: We use SECT_PER_TRACK. Unfortunately, this
2580 * does not work with MT, hence we can only transfer one head at
2581 * a time
2583 static void virtualdmabug_workaround(void)
2585 int hard_sectors, end_sector;
2587 if(CT(COMMAND) == FD_WRITE) {
2588 COMMAND &= ~0x80; /* switch off multiple track mode */
2590 hard_sectors = raw_cmd->length >> (7 + SIZECODE);
2591 end_sector = SECTOR + hard_sectors - 1;
2592 #ifdef FLOPPY_SANITY_CHECK
2593 if(end_sector > SECT_PER_TRACK) {
2594 printk("too many sectors %d > %d\n",
2595 end_sector, SECT_PER_TRACK);
2596 return;
2598 #endif
2599 SECT_PER_TRACK = end_sector; /* make sure SECT_PER_TRACK points
2600 * to end of transfer */
2605 * Formulate a read/write request.
2606 * this routine decides where to load the data (directly to buffer, or to
2607 * tmp floppy area), how much data to load (the size of the buffer, the whole
2608 * track, or a single sector)
2609 * All floppy_track_buffer handling goes in here. If we ever add track buffer
2610 * allocation on the fly, it should be done here. No other part should need
2611 * modification.
2614 static int make_raw_rw_request(void)
2616 int aligned_sector_t;
2617 int max_sector, max_size, tracksize, ssize;
2619 if(max_buffer_sectors == 0) {
2620 printk("VFS: Block I/O scheduled on unopened device\n");
2621 return 0;
2624 set_fdc(DRIVE(CURRENT->rq_dev));
2626 raw_cmd = &default_raw_cmd;
2627 raw_cmd->flags = FD_RAW_SPIN | FD_RAW_NEED_DISK | FD_RAW_NEED_DISK |
2628 FD_RAW_NEED_SEEK;
2629 raw_cmd->cmd_count = NR_RW;
2630 if (CURRENT->cmd == READ){
2631 raw_cmd->flags |= FD_RAW_READ;
2632 COMMAND = FM_MODE(_floppy,FD_READ);
2633 } else if (CURRENT->cmd == WRITE){
2634 raw_cmd->flags |= FD_RAW_WRITE;
2635 COMMAND = FM_MODE(_floppy,FD_WRITE);
2636 } else {
2637 DPRINT("make_raw_rw_request: unknown command\n");
2638 return 0;
2641 max_sector = _floppy->sect * _floppy->head;
2643 TRACK = CURRENT->sector / max_sector;
2644 sector_t = CURRENT->sector % max_sector;
2645 if (_floppy->track && TRACK >= _floppy->track) {
2646 if (CURRENT->current_nr_sectors & 1) {
2647 current_count_sectors = 1;
2648 return 1;
2649 } else
2650 return 0;
2652 HEAD = sector_t / _floppy->sect;
2654 if (((_floppy->stretch & FD_SWAPSIDES) || TESTF(FD_NEED_TWADDLE)) &&
2655 sector_t < _floppy->sect)
2656 max_sector = _floppy->sect;
2658 /* 2M disks have phantom sectors on the first track */
2659 if ((_floppy->rate & FD_2M) && (!TRACK) && (!HEAD)){
2660 max_sector = 2 * _floppy->sect / 3;
2661 if (sector_t >= max_sector){
2662 current_count_sectors = minimum(_floppy->sect - sector_t,
2663 CURRENT->nr_sectors);
2664 return 1;
2666 SIZECODE = 2;
2667 } else
2668 SIZECODE = FD_SIZECODE(_floppy);
2669 raw_cmd->rate = _floppy->rate & 0x43;
2670 if ((_floppy->rate & FD_2M) &&
2671 (TRACK || HEAD) &&
2672 raw_cmd->rate == 2)
2673 raw_cmd->rate = 1;
2675 if (SIZECODE)
2676 SIZECODE2 = 0xff;
2677 else
2678 SIZECODE2 = 0x80;
2679 raw_cmd->track = TRACK << STRETCH(_floppy);
2680 DR_SELECT = UNIT(current_drive) + PH_HEAD(_floppy,HEAD);
2681 GAP = _floppy->gap;
2682 CODE2SIZE;
2683 SECT_PER_TRACK = _floppy->sect << 2 >> SIZECODE;
2684 SECTOR = ((sector_t % _floppy->sect) << 2 >> SIZECODE) + 1;
2686 /* tracksize describes the size which can be filled up with sectors
2687 * of size ssize.
2689 tracksize = _floppy->sect - _floppy->sect % ssize;
2690 if (tracksize < _floppy->sect){
2691 SECT_PER_TRACK ++;
2692 if (tracksize <= sector_t % _floppy->sect)
2693 SECTOR--;
2695 /* if we are beyond tracksize, fill up using smaller sectors */
2696 while (tracksize <= sector_t % _floppy->sect){
2697 while(tracksize + ssize > _floppy->sect){
2698 SIZECODE--;
2699 ssize >>= 1;
2701 SECTOR++; SECT_PER_TRACK ++;
2702 tracksize += ssize;
2704 max_sector = HEAD * _floppy->sect + tracksize;
2705 } else if (!TRACK && !HEAD && !(_floppy->rate & FD_2M) && probing) {
2706 max_sector = _floppy->sect;
2707 } else if (!HEAD && CT(COMMAND) == FD_WRITE) {
2708 /* for virtual DMA bug workaround */
2709 max_sector = _floppy->sect;
2712 in_sector_offset = (sector_t % _floppy->sect) % ssize;
2713 aligned_sector_t = sector_t - in_sector_offset;
2714 max_size = CURRENT->nr_sectors;
2715 if ((raw_cmd->track == buffer_track) &&
2716 (current_drive == buffer_drive) &&
2717 (sector_t >= buffer_min) && (sector_t < buffer_max)) {
2718 /* data already in track buffer */
2719 if (CT(COMMAND) == FD_READ) {
2720 copy_buffer(1, max_sector, buffer_max);
2721 return 1;
2723 } else if (in_sector_offset || CURRENT->nr_sectors < ssize){
2724 if (CT(COMMAND) == FD_WRITE){
2725 if (sector_t + CURRENT->nr_sectors > ssize &&
2726 sector_t + CURRENT->nr_sectors < ssize + ssize)
2727 max_size = ssize + ssize;
2728 else
2729 max_size = ssize;
2731 raw_cmd->flags &= ~FD_RAW_WRITE;
2732 raw_cmd->flags |= FD_RAW_READ;
2733 COMMAND = FM_MODE(_floppy,FD_READ);
2734 } else if ((unsigned long)CURRENT->buffer < MAX_DMA_ADDRESS) {
2735 unsigned long dma_limit;
2736 int direct, indirect;
2738 indirect= transfer_size(ssize,max_sector,max_buffer_sectors*2) -
2739 sector_t;
2742 * Do NOT use minimum() here---MAX_DMA_ADDRESS is 64 bits wide
2743 * on a 64 bit machine!
2745 max_size = buffer_chain_size();
2746 dma_limit = (MAX_DMA_ADDRESS - ((unsigned long) CURRENT->buffer)) >> 9;
2747 if ((unsigned long) max_size > dma_limit) {
2748 max_size = dma_limit;
2750 /* 64 kb boundaries */
2751 if (CROSS_64KB(CURRENT->buffer, max_size << 9))
2752 max_size = (K_64 -
2753 ((unsigned long)CURRENT->buffer) % K_64)>>9;
2754 direct = transfer_size(ssize,max_sector,max_size) - sector_t;
2756 * We try to read tracks, but if we get too many errors, we
2757 * go back to reading just one sector at a time.
2759 * This means we should be able to read a sector even if there
2760 * are other bad sectors on this track.
2762 if (!direct ||
2763 (indirect * 2 > direct * 3 &&
2764 *errors < DP->max_errors.read_track &&
2765 /*!TESTF(FD_NEED_TWADDLE) &&*/
2766 ((!probing || (DP->read_track&(1<<DRS->probed_format)))))){
2767 max_size = CURRENT->nr_sectors;
2768 } else {
2769 raw_cmd->kernel_data = CURRENT->buffer;
2770 raw_cmd->length = current_count_sectors << 9;
2771 if (raw_cmd->length == 0){
2772 DPRINT("zero dma transfer attempted from make_raw_request\n");
2773 DPRINT("indirect=%d direct=%d sector_t=%d",
2774 indirect, direct, sector_t);
2775 return 0;
2777 /* check_dma_crossing(raw_cmd->kernel_data,
2778 raw_cmd->length,
2779 "end of make_raw_request [1]");*/
2781 virtualdmabug_workaround();
2782 return 2;
2786 if (CT(COMMAND) == FD_READ)
2787 max_size = max_sector; /* unbounded */
2789 /* claim buffer track if needed */
2790 if (buffer_track != raw_cmd->track || /* bad track */
2791 buffer_drive !=current_drive || /* bad drive */
2792 sector_t > buffer_max ||
2793 sector_t < buffer_min ||
2794 ((CT(COMMAND) == FD_READ ||
2795 (!in_sector_offset && CURRENT->nr_sectors >= ssize))&&
2796 max_sector > 2 * max_buffer_sectors + buffer_min &&
2797 max_size + sector_t > 2 * max_buffer_sectors + buffer_min)
2798 /* not enough space */){
2799 buffer_track = -1;
2800 buffer_drive = current_drive;
2801 buffer_max = buffer_min = aligned_sector_t;
2803 raw_cmd->kernel_data = floppy_track_buffer +
2804 ((aligned_sector_t-buffer_min)<<9);
2806 if (CT(COMMAND) == FD_WRITE){
2807 /* copy write buffer to track buffer.
2808 * if we get here, we know that the write
2809 * is either aligned or the data already in the buffer
2810 * (buffer will be overwritten) */
2811 #ifdef FLOPPY_SANITY_CHECK
2812 if (in_sector_offset && buffer_track == -1)
2813 DPRINT("internal error offset !=0 on write\n");
2814 #endif
2815 buffer_track = raw_cmd->track;
2816 buffer_drive = current_drive;
2817 copy_buffer(ssize, max_sector, 2*max_buffer_sectors+buffer_min);
2818 } else
2819 transfer_size(ssize, max_sector,
2820 2*max_buffer_sectors+buffer_min-aligned_sector_t);
2822 /* round up current_count_sectors to get dma xfer size */
2823 raw_cmd->length = in_sector_offset+current_count_sectors;
2824 raw_cmd->length = ((raw_cmd->length -1)|(ssize-1))+1;
2825 raw_cmd->length <<= 9;
2826 #ifdef FLOPPY_SANITY_CHECK
2827 /*check_dma_crossing(raw_cmd->kernel_data, raw_cmd->length,
2828 "end of make_raw_request");*/
2829 if ((raw_cmd->length < current_count_sectors << 9) ||
2830 (raw_cmd->kernel_data != CURRENT->buffer &&
2831 CT(COMMAND) == FD_WRITE &&
2832 (aligned_sector_t + (raw_cmd->length >> 9) > buffer_max ||
2833 aligned_sector_t < buffer_min)) ||
2834 raw_cmd->length % (128 << SIZECODE) ||
2835 raw_cmd->length <= 0 || current_count_sectors <= 0){
2836 DPRINT("fractionary current count b=%lx s=%lx\n",
2837 raw_cmd->length, current_count_sectors);
2838 if (raw_cmd->kernel_data != CURRENT->buffer)
2839 printk("addr=%d, length=%ld\n",
2840 (int) ((raw_cmd->kernel_data -
2841 floppy_track_buffer) >> 9),
2842 current_count_sectors);
2843 printk("st=%d ast=%d mse=%d msi=%d\n",
2844 sector_t, aligned_sector_t, max_sector, max_size);
2845 printk("ssize=%x SIZECODE=%d\n", ssize, SIZECODE);
2846 printk("command=%x SECTOR=%d HEAD=%d, TRACK=%d\n",
2847 COMMAND, SECTOR, HEAD, TRACK);
2848 printk("buffer drive=%d\n", buffer_drive);
2849 printk("buffer track=%d\n", buffer_track);
2850 printk("buffer_min=%d\n", buffer_min);
2851 printk("buffer_max=%d\n", buffer_max);
2852 return 0;
2855 if (raw_cmd->kernel_data != CURRENT->buffer){
2856 if (raw_cmd->kernel_data < floppy_track_buffer ||
2857 current_count_sectors < 0 ||
2858 raw_cmd->length < 0 ||
2859 raw_cmd->kernel_data + raw_cmd->length >
2860 floppy_track_buffer + (max_buffer_sectors << 10)){
2861 DPRINT("buffer overrun in schedule dma\n");
2862 printk("sector_t=%d buffer_min=%d current_count=%ld\n",
2863 sector_t, buffer_min,
2864 raw_cmd->length >> 9);
2865 printk("current_count_sectors=%ld\n",
2866 current_count_sectors);
2867 if (CT(COMMAND) == FD_READ)
2868 printk("read\n");
2869 if (CT(COMMAND) == FD_READ)
2870 printk("write\n");
2871 return 0;
2873 } else if (raw_cmd->length > CURRENT->nr_sectors << 9 ||
2874 current_count_sectors > CURRENT->nr_sectors){
2875 DPRINT("buffer overrun in direct transfer\n");
2876 return 0;
2877 } else if (raw_cmd->length < current_count_sectors << 9){
2878 DPRINT("more sectors than bytes\n");
2879 printk("bytes=%ld\n", raw_cmd->length >> 9);
2880 printk("sectors=%ld\n", current_count_sectors);
2882 if (raw_cmd->length == 0){
2883 DPRINT("zero dma transfer attempted from make_raw_request\n");
2884 return 0;
2886 #endif
2888 virtualdmabug_workaround();
2889 return 2;
2892 static void redo_fd_request(void)
2894 #define REPEAT {request_done(0); continue; }
2895 kdev_t device;
2896 int tmp;
2898 lastredo = jiffies;
2899 if (current_drive < N_DRIVE)
2900 floppy_off(current_drive);
2902 if (!QUEUE_EMPTY && CURRENT->rq_status == RQ_INACTIVE){
2903 CLEAR_INTR;
2904 unlock_fdc();
2905 return;
2908 while(1){
2909 if (QUEUE_EMPTY) {
2910 CLEAR_INTR;
2911 unlock_fdc();
2912 return;
2914 if (MAJOR(CURRENT->rq_dev) != MAJOR_NR)
2915 panic(DEVICE_NAME ": request list destroyed");
2916 if (CURRENT->bh && !buffer_locked(CURRENT->bh))
2917 panic(DEVICE_NAME ": block not locked");
2919 device = CURRENT->rq_dev;
2920 set_fdc(DRIVE(device));
2921 reschedule_timeout(CURRENTD, "redo fd request", 0);
2923 set_floppy(device);
2924 raw_cmd = & default_raw_cmd;
2925 raw_cmd->flags = 0;
2926 if (start_motor(redo_fd_request)) return;
2927 disk_change(current_drive);
2928 if (test_bit(current_drive, &fake_change) ||
2929 TESTF(FD_DISK_CHANGED)){
2930 DPRINT("disk absent or changed during operation\n");
2931 REPEAT;
2933 if (!_floppy) { /* Autodetection */
2934 if (!probing){
2935 DRS->probed_format = 0;
2936 if (next_valid_format()){
2937 DPRINT("no autodetectable formats\n");
2938 _floppy = NULL;
2939 REPEAT;
2942 probing = 1;
2943 _floppy = floppy_type+DP->autodetect[DRS->probed_format];
2944 } else
2945 probing = 0;
2946 errors = & (CURRENT->errors);
2947 tmp = make_raw_rw_request();
2948 if (tmp < 2){
2949 request_done(tmp);
2950 continue;
2953 if (TESTF(FD_NEED_TWADDLE))
2954 twaddle();
2955 schedule_bh( (void *)(void *) floppy_start);
2956 #ifdef DEBUGT
2957 debugt("queue fd request");
2958 #endif
2959 return;
2961 #undef REPEAT
2964 static struct cont_t rw_cont={
2965 rw_interrupt,
2966 redo_fd_request,
2967 bad_flp_intr,
2968 request_done };
2970 static void process_fd_request(void)
2972 cont = &rw_cont;
2973 schedule_bh( (void *)(void *) redo_fd_request);
2976 static void do_fd_request(request_queue_t * q)
2978 if(max_buffer_sectors == 0) {
2979 printk("VFS: do_fd_request called on non-open device\n");
2980 return;
2983 if (usage_count == 0) {
2984 printk("warning: usage count=0, CURRENT=%p exiting\n", CURRENT);
2985 printk("sect=%ld cmd=%d\n", CURRENT->sector, CURRENT->cmd);
2986 return;
2988 if (fdc_busy){
2989 /* fdc busy, this new request will be treated when the
2990 current one is done */
2991 is_alive("do fd request, old request running");
2992 return;
2994 lock_fdc(MAXTIMEOUT,0);
2995 process_fd_request();
2996 is_alive("do fd request");
2999 static struct cont_t poll_cont={
3000 success_and_wakeup,
3001 floppy_ready,
3002 generic_failure,
3003 generic_done };
3005 static int poll_drive(int interruptible, int flag)
3007 int ret;
3008 /* no auto-sense, just clear dcl */
3009 raw_cmd = &default_raw_cmd;
3010 raw_cmd->flags= flag;
3011 raw_cmd->track=0;
3012 raw_cmd->cmd_count=0;
3013 cont = &poll_cont;
3014 #ifdef DCL_DEBUG
3015 if (DP->flags & FD_DEBUG){
3016 DPRINT("setting NEWCHANGE in poll_drive\n");
3018 #endif
3019 SETF(FD_DISK_NEWCHANGE);
3020 WAIT(floppy_ready);
3021 return ret;
3025 * User triggered reset
3026 * ====================
3029 static void reset_intr(void)
3031 printk("weird, reset interrupt called\n");
3034 static struct cont_t reset_cont={
3035 reset_intr,
3036 success_and_wakeup,
3037 generic_failure,
3038 generic_done };
3040 static int user_reset_fdc(int drive, int arg, int interruptible)
3042 int ret;
3044 ret=0;
3045 LOCK_FDC(drive,interruptible);
3046 if (arg == FD_RESET_ALWAYS)
3047 FDCS->reset=1;
3048 if (FDCS->reset){
3049 cont = &reset_cont;
3050 WAIT(reset_fdc);
3052 process_fd_request();
3053 return ret;
3057 * Misc Ioctl's and support
3058 * ========================
3060 static inline int fd_copyout(void *param, const void *address, unsigned long size)
3062 return copy_to_user(param,address, size) ? -EFAULT : 0;
3065 static inline int fd_copyin(void *param, void *address, unsigned long size)
3067 return copy_from_user(address, param, size) ? -EFAULT : 0;
3070 #define _COPYOUT(x) (copy_to_user((void *)param, &(x), sizeof(x)) ? -EFAULT : 0)
3071 #define _COPYIN(x) (copy_from_user(&(x), (void *)param, sizeof(x)) ? -EFAULT : 0)
3073 #define COPYOUT(x) ECALL(_COPYOUT(x))
3074 #define COPYIN(x) ECALL(_COPYIN(x))
3076 static inline const char *drive_name(int type, int drive)
3078 struct floppy_struct *floppy;
3080 if (type)
3081 floppy = floppy_type + type;
3082 else {
3083 if (UDP->native_format)
3084 floppy = floppy_type + UDP->native_format;
3085 else
3086 return "(null)";
3088 if (floppy->name)
3089 return floppy->name;
3090 else
3091 return "(null)";
3095 /* raw commands */
3096 static void raw_cmd_done(int flag)
3098 int i;
3100 if (!flag) {
3101 raw_cmd->flags |= FD_RAW_FAILURE;
3102 raw_cmd->flags |= FD_RAW_HARDFAILURE;
3103 } else {
3104 raw_cmd->reply_count = inr;
3105 if (raw_cmd->reply_count > MAX_REPLIES)
3106 raw_cmd->reply_count=0;
3107 for (i=0; i< raw_cmd->reply_count; i++)
3108 raw_cmd->reply[i] = reply_buffer[i];
3110 if (raw_cmd->flags & (FD_RAW_READ | FD_RAW_WRITE))
3112 unsigned long flags;
3113 flags=claim_dma_lock();
3114 raw_cmd->length = fd_get_dma_residue();
3115 release_dma_lock(flags);
3118 if ((raw_cmd->flags & FD_RAW_SOFTFAILURE) &&
3119 (!raw_cmd->reply_count || (raw_cmd->reply[0] & 0xc0)))
3120 raw_cmd->flags |= FD_RAW_FAILURE;
3122 if (disk_change(current_drive))
3123 raw_cmd->flags |= FD_RAW_DISK_CHANGE;
3124 else
3125 raw_cmd->flags &= ~FD_RAW_DISK_CHANGE;
3126 if (raw_cmd->flags & FD_RAW_NO_MOTOR_AFTER)
3127 motor_off_callback(current_drive);
3129 if (raw_cmd->next &&
3130 (!(raw_cmd->flags & FD_RAW_FAILURE) ||
3131 !(raw_cmd->flags & FD_RAW_STOP_IF_FAILURE)) &&
3132 ((raw_cmd->flags & FD_RAW_FAILURE) ||
3133 !(raw_cmd->flags &FD_RAW_STOP_IF_SUCCESS))) {
3134 raw_cmd = raw_cmd->next;
3135 return;
3138 generic_done(flag);
3142 static struct cont_t raw_cmd_cont={
3143 success_and_wakeup,
3144 floppy_start,
3145 generic_failure,
3146 raw_cmd_done
3149 static inline int raw_cmd_copyout(int cmd, char *param,
3150 struct floppy_raw_cmd *ptr)
3152 int ret;
3154 while(ptr) {
3155 COPYOUT(*ptr);
3156 param += sizeof(struct floppy_raw_cmd);
3157 if ((ptr->flags & FD_RAW_READ) && ptr->buffer_length){
3158 if (ptr->length>=0 && ptr->length<=ptr->buffer_length)
3159 ECALL(fd_copyout(ptr->data,
3160 ptr->kernel_data,
3161 ptr->buffer_length -
3162 ptr->length));
3164 ptr = ptr->next;
3166 return 0;
3170 static void raw_cmd_free(struct floppy_raw_cmd **ptr)
3172 struct floppy_raw_cmd *next,*this;
3174 this = *ptr;
3175 *ptr = 0;
3176 while(this) {
3177 if (this->buffer_length) {
3178 fd_dma_mem_free((unsigned long)this->kernel_data,
3179 this->buffer_length);
3180 this->buffer_length = 0;
3182 next = this->next;
3183 kfree(this);
3184 this = next;
3189 static inline int raw_cmd_copyin(int cmd, char *param,
3190 struct floppy_raw_cmd **rcmd)
3192 struct floppy_raw_cmd *ptr;
3193 int ret;
3194 int i;
3196 *rcmd = 0;
3197 while(1) {
3198 ptr = (struct floppy_raw_cmd *)
3199 kmalloc(sizeof(struct floppy_raw_cmd), GFP_USER);
3200 if (!ptr)
3201 return -ENOMEM;
3202 *rcmd = ptr;
3203 COPYIN(*ptr);
3204 ptr->next = 0;
3205 ptr->buffer_length = 0;
3206 param += sizeof(struct floppy_raw_cmd);
3207 if (ptr->cmd_count > 33)
3208 /* the command may now also take up the space
3209 * initially intended for the reply & the
3210 * reply count. Needed for long 82078 commands
3211 * such as RESTORE, which takes ... 17 command
3212 * bytes. Murphy's law #137: When you reserve
3213 * 16 bytes for a structure, you'll one day
3214 * discover that you really need 17...
3216 return -EINVAL;
3218 for (i=0; i< 16; i++)
3219 ptr->reply[i] = 0;
3220 ptr->resultcode = 0;
3221 ptr->kernel_data = 0;
3223 if (ptr->flags & (FD_RAW_READ | FD_RAW_WRITE)) {
3224 if (ptr->length <= 0)
3225 return -EINVAL;
3226 ptr->kernel_data =(char*)fd_dma_mem_alloc(ptr->length);
3227 fallback_on_nodma_alloc(&ptr->kernel_data,
3228 ptr->length);
3229 if (!ptr->kernel_data)
3230 return -ENOMEM;
3231 ptr->buffer_length = ptr->length;
3233 if (ptr->flags & FD_RAW_WRITE)
3234 ECALL(fd_copyin(ptr->data, ptr->kernel_data,
3235 ptr->length));
3236 rcmd = & (ptr->next);
3237 if (!(ptr->flags & FD_RAW_MORE))
3238 return 0;
3239 ptr->rate &= 0x43;
3244 static int raw_cmd_ioctl(int cmd, void *param)
3246 int drive, ret, ret2;
3247 struct floppy_raw_cmd *my_raw_cmd;
3249 if (FDCS->rawcmd <= 1)
3250 FDCS->rawcmd = 1;
3251 for (drive= 0; drive < N_DRIVE; drive++){
3252 if (FDC(drive) != fdc)
3253 continue;
3254 if (drive == current_drive){
3255 if (UDRS->fd_ref > 1){
3256 FDCS->rawcmd = 2;
3257 break;
3259 } else if (UDRS->fd_ref){
3260 FDCS->rawcmd = 2;
3261 break;
3265 if (FDCS->reset)
3266 return -EIO;
3268 ret = raw_cmd_copyin(cmd, param, &my_raw_cmd);
3269 if (ret) {
3270 raw_cmd_free(&my_raw_cmd);
3271 return ret;
3274 raw_cmd = my_raw_cmd;
3275 cont = &raw_cmd_cont;
3276 ret=wait_til_done(floppy_start,1);
3277 #ifdef DCL_DEBUG
3278 if (DP->flags & FD_DEBUG){
3279 DPRINT("calling disk change from raw_cmd ioctl\n");
3281 #endif
3283 if (ret != -EINTR && FDCS->reset)
3284 ret = -EIO;
3286 DRS->track = NO_TRACK;
3288 ret2 = raw_cmd_copyout(cmd, param, my_raw_cmd);
3289 if (!ret)
3290 ret = ret2;
3291 raw_cmd_free(&my_raw_cmd);
3292 return ret;
3295 static int invalidate_drive(kdev_t rdev)
3297 /* invalidate the buffer track to force a reread */
3298 set_bit(DRIVE(rdev), &fake_change);
3299 process_fd_request();
3300 check_disk_change(rdev);
3301 return 0;
3305 static inline void clear_write_error(int drive)
3307 CLEARSTRUCT(UDRWE);
3310 static inline int set_geometry(unsigned int cmd, struct floppy_struct *g,
3311 int drive, int type, kdev_t device)
3313 int cnt;
3315 /* sanity checking for parameters.*/
3316 if (g->sect <= 0 ||
3317 g->head <= 0 ||
3318 g->track <= 0 ||
3319 g->track > UDP->tracks>>STRETCH(g) ||
3320 /* check if reserved bits are set */
3321 (g->stretch&~(FD_STRETCH|FD_SWAPSIDES)) != 0)
3322 return -EINVAL;
3323 if (type){
3324 if (!capable(CAP_SYS_ADMIN))
3325 return -EPERM;
3326 LOCK_FDC(drive,1);
3327 for (cnt = 0; cnt < N_DRIVE; cnt++){
3328 if (ITYPE(drive_state[cnt].fd_device) == type &&
3329 drive_state[cnt].fd_ref)
3330 set_bit(drive, &fake_change);
3332 floppy_type[type] = *g;
3333 floppy_type[type].name="user format";
3334 for (cnt = type << 2; cnt < (type << 2) + 4; cnt++)
3335 floppy_sizes[cnt]= floppy_sizes[cnt+0x80]=
3336 (floppy_type[type].size+1)>>1;
3337 process_fd_request();
3338 for (cnt = 0; cnt < N_DRIVE; cnt++){
3339 if (ITYPE(drive_state[cnt].fd_device) == type &&
3340 drive_state[cnt].fd_ref)
3341 check_disk_change(
3342 MKDEV(FLOPPY_MAJOR,
3343 drive_state[cnt].fd_device));
3345 } else {
3346 LOCK_FDC(drive,1);
3347 if (cmd != FDDEFPRM)
3348 /* notice a disk change immediately, else
3349 * we lose our settings immediately*/
3350 CALL(poll_drive(1, FD_RAW_NEED_DISK));
3351 user_params[drive] = *g;
3352 if (buffer_drive == drive)
3353 SUPBOUND(buffer_max, user_params[drive].sect);
3354 current_type[drive] = &user_params[drive];
3355 floppy_sizes[drive] = (user_params[drive].size+1) >> 1;
3356 if (cmd == FDDEFPRM)
3357 DRS->keep_data = -1;
3358 else
3359 DRS->keep_data = 1;
3360 /* invalidation. Invalidate only when needed, i.e.
3361 * when there are already sectors in the buffer cache
3362 * whose number will change. This is useful, because
3363 * mtools often changes the geometry of the disk after
3364 * looking at the boot block */
3365 if (DRS->maxblock > user_params[drive].sect || DRS->maxtrack)
3366 invalidate_drive(device);
3367 else
3368 process_fd_request();
3370 return 0;
3373 /* handle obsolete ioctl's */
3374 static int ioctl_table[]= {
3375 FDCLRPRM,
3376 FDSETPRM,
3377 FDDEFPRM,
3378 FDGETPRM,
3379 FDMSGON,
3380 FDMSGOFF,
3381 FDFMTBEG,
3382 FDFMTTRK,
3383 FDFMTEND,
3384 FDSETEMSGTRESH,
3385 FDFLUSH,
3386 FDSETMAXERRS,
3387 FDGETMAXERRS,
3388 FDGETDRVTYP,
3389 FDSETDRVPRM,
3390 FDGETDRVPRM,
3391 FDGETDRVSTAT,
3392 FDPOLLDRVSTAT,
3393 FDRESET,
3394 FDGETFDCSTAT,
3395 FDWERRORCLR,
3396 FDWERRORGET,
3397 FDRAWCMD,
3398 FDEJECT,
3399 FDTWADDLE
3402 static inline int normalize_ioctl(int *cmd, int *size)
3404 int i;
3406 for (i=0; i < ARRAY_SIZE(ioctl_table); i++) {
3407 if ((*cmd & 0xffff) == (ioctl_table[i] & 0xffff)){
3408 *size = _IOC_SIZE(*cmd);
3409 *cmd = ioctl_table[i];
3410 if (*size > _IOC_SIZE(*cmd)) {
3411 printk("ioctl not yet supported\n");
3412 return -EFAULT;
3414 return 0;
3417 return -EINVAL;
3420 static int get_floppy_geometry(int drive, int type, struct floppy_struct **g)
3422 if (type)
3423 *g = &floppy_type[type];
3424 else {
3425 LOCK_FDC(drive,0);
3426 CALL(poll_drive(0,0));
3427 process_fd_request();
3428 *g = current_type[drive];
3430 if (!*g)
3431 return -ENODEV;
3432 return 0;
3435 static int fd_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
3436 unsigned long param)
3438 #define OUT(c,x) case c: outparam = (const char *) (x); break
3439 #define IN(c,x,tag) case c: *(x) = inparam. tag ; return 0
3441 int i,drive,type;
3442 kdev_t device;
3443 int ret;
3444 int size;
3445 union inparam {
3446 struct floppy_struct g; /* geometry */
3447 struct format_descr f;
3448 struct floppy_max_errors max_errors;
3449 struct floppy_drive_params dp;
3450 } inparam; /* parameters coming from user space */
3451 const char *outparam; /* parameters passed back to user space */
3453 device = inode->i_rdev;
3454 switch (cmd) {
3455 case BLKROSET:
3456 case BLKROGET:
3457 case BLKRASET:
3458 case BLKRAGET:
3459 case BLKFLSBUF:
3460 return blk_ioctl(device, cmd, param);
3462 type = TYPE(device);
3463 drive = DRIVE(device);
3465 /* convert compatibility eject ioctls into floppy eject ioctl.
3466 * We do this in order to provide a means to eject floppy disks before
3467 * installing the new fdutils package */
3468 if (cmd == CDROMEJECT || /* CD-ROM eject */
3469 cmd == 0x6470 /* SunOS floppy eject */) {
3470 DPRINT("obsolete eject ioctl\n");
3471 DPRINT("please use floppycontrol --eject\n");
3472 cmd = FDEJECT;
3475 /* generic block device ioctls */
3476 switch(cmd) {
3477 /* the following have been inspired by the corresponding
3478 * code for other block devices. */
3479 struct floppy_struct *g;
3480 case HDIO_GETGEO:
3482 struct hd_geometry loc;
3483 ECALL(get_floppy_geometry(drive, type, &g));
3484 loc.heads = g->head;
3485 loc.sectors = g->sect;
3486 loc.cylinders = g->track;
3487 loc.start = 0;
3488 return _COPYOUT(loc);
3491 case BLKGETSIZE:
3492 ECALL(get_floppy_geometry(drive, type, &g));
3493 return put_user(g->size, (long *) param);
3494 /* BLKRRPART is not defined as floppies don't have
3495 * partition tables */
3498 /* convert the old style command into a new style command */
3499 if ((cmd & 0xff00) == 0x0200) {
3500 ECALL(normalize_ioctl(&cmd, &size));
3501 } else
3502 return -EINVAL;
3504 /* permission checks */
3505 if (((cmd & 0x40) && !(filp->f_mode & 2)) ||
3506 ((cmd & 0x80) && !capable(CAP_SYS_ADMIN)))
3507 return -EPERM;
3509 /* copyin */
3510 CLEARSTRUCT(&inparam);
3511 if (_IOC_DIR(cmd) & _IOC_WRITE)
3512 ECALL(fd_copyin((void *)param, &inparam, size))
3514 switch (cmd) {
3515 case FDEJECT:
3516 if (UDRS->fd_ref != 1)
3517 /* somebody else has this drive open */
3518 return -EBUSY;
3519 LOCK_FDC(drive,1);
3521 /* do the actual eject. Fails on
3522 * non-Sparc architectures */
3523 ret=fd_eject(UNIT(drive));
3525 USETF(FD_DISK_CHANGED);
3526 USETF(FD_VERIFY);
3527 process_fd_request();
3528 return ret;
3529 case FDCLRPRM:
3530 LOCK_FDC(drive,1);
3531 current_type[drive] = NULL;
3532 floppy_sizes[drive] = MAX_DISK_SIZE;
3533 UDRS->keep_data = 0;
3534 return invalidate_drive(device);
3535 case FDSETPRM:
3536 case FDDEFPRM:
3537 return set_geometry(cmd, & inparam.g,
3538 drive, type, device);
3539 case FDGETPRM:
3540 ECALL(get_floppy_geometry(drive, type,
3541 (struct floppy_struct**)
3542 &outparam));
3543 break;
3545 case FDMSGON:
3546 UDP->flags |= FTD_MSG;
3547 return 0;
3548 case FDMSGOFF:
3549 UDP->flags &= ~FTD_MSG;
3550 return 0;
3552 case FDFMTBEG:
3553 LOCK_FDC(drive,1);
3554 CALL(poll_drive(1, FD_RAW_NEED_DISK));
3555 ret = UDRS->flags;
3556 process_fd_request();
3557 if (ret & FD_VERIFY)
3558 return -ENODEV;
3559 if (!(ret & FD_DISK_WRITABLE))
3560 return -EROFS;
3561 return 0;
3562 case FDFMTTRK:
3563 if (UDRS->fd_ref != 1)
3564 return -EBUSY;
3565 return do_format(device, &inparam.f);
3566 case FDFMTEND:
3567 case FDFLUSH:
3568 LOCK_FDC(drive,1);
3569 return invalidate_drive(device);
3571 case FDSETEMSGTRESH:
3572 UDP->max_errors.reporting =
3573 (unsigned short) (param & 0x0f);
3574 return 0;
3575 OUT(FDGETMAXERRS, &UDP->max_errors);
3576 IN(FDSETMAXERRS, &UDP->max_errors, max_errors);
3578 case FDGETDRVTYP:
3579 outparam = drive_name(type,drive);
3580 SUPBOUND(size,strlen(outparam)+1);
3581 break;
3583 IN(FDSETDRVPRM, UDP, dp);
3584 OUT(FDGETDRVPRM, UDP);
3586 case FDPOLLDRVSTAT:
3587 LOCK_FDC(drive,1);
3588 CALL(poll_drive(1, FD_RAW_NEED_DISK));
3589 process_fd_request();
3590 /* fall through */
3591 OUT(FDGETDRVSTAT, UDRS);
3593 case FDRESET:
3594 return user_reset_fdc(drive, (int)param, 1);
3596 OUT(FDGETFDCSTAT,UFDCS);
3598 case FDWERRORCLR:
3599 CLEARSTRUCT(UDRWE);
3600 return 0;
3601 OUT(FDWERRORGET,UDRWE);
3603 case FDRAWCMD:
3604 if (type)
3605 return -EINVAL;
3606 LOCK_FDC(drive,1);
3607 set_floppy(device);
3608 CALL(i = raw_cmd_ioctl(cmd,(void *) param));
3609 process_fd_request();
3610 return i;
3612 case FDTWADDLE:
3613 LOCK_FDC(drive,1);
3614 twaddle();
3615 process_fd_request();
3616 return 0;
3618 default:
3619 return -EINVAL;
3622 if (_IOC_DIR(cmd) & _IOC_READ)
3623 return fd_copyout((void *)param, outparam, size);
3624 else
3625 return 0;
3626 #undef OUT
3627 #undef IN
3630 static void __init config_types(void)
3632 int first=1;
3633 int drive;
3635 /* read drive info out of physical CMOS */
3636 drive=0;
3637 if (!UDP->cmos)
3638 UDP->cmos = FLOPPY0_TYPE;
3639 drive=1;
3640 if (!UDP->cmos && FLOPPY1_TYPE)
3641 UDP->cmos = FLOPPY1_TYPE;
3643 /* XXX */
3644 /* additional physical CMOS drive detection should go here */
3646 for (drive=0; drive < N_DRIVE; drive++){
3647 unsigned int type = UDP->cmos;
3648 struct floppy_drive_params *params;
3649 const char *name = NULL;
3650 static char temparea[32];
3652 if (type < NUMBER(default_drive_params)) {
3653 params = &default_drive_params[type].params;
3654 if (type) {
3655 name = default_drive_params[type].name;
3656 allowed_drive_mask |= 1 << drive;
3658 } else {
3659 params = &default_drive_params[0].params;
3660 sprintf(temparea, "unknown type %d (usb?)", type);
3661 name = temparea;
3663 if (name) {
3664 const char * prepend = ",";
3665 if (first) {
3666 prepend = KERN_INFO "Floppy drive(s):";
3667 first = 0;
3669 printk("%s fd%d is %s", prepend, drive, name);
3670 register_devfs_entries (drive);
3672 *UDP = *params;
3674 if (!first)
3675 printk("\n");
3678 static int floppy_release(struct inode * inode, struct file * filp)
3680 int drive = DRIVE(inode->i_rdev);
3682 if (UDRS->fd_ref < 0)
3683 UDRS->fd_ref=0;
3684 else if (!UDRS->fd_ref--) {
3685 DPRINT("floppy_release with fd_ref == 0");
3686 UDRS->fd_ref = 0;
3688 floppy_release_irq_and_dma();
3689 return 0;
3693 * floppy_open check for aliasing (/dev/fd0 can be the same as
3694 * /dev/PS0 etc), and disallows simultaneous access to the same
3695 * drive with different device numbers.
3697 #define RETERR(x) do{floppy_release(inode,filp); return -(x);}while(0)
3699 static int floppy_open(struct inode * inode, struct file * filp)
3701 int drive;
3702 int old_dev;
3703 int try;
3704 char *tmp;
3706 if (!filp) {
3707 DPRINT("Weird, open called with filp=0\n");
3708 return -EIO;
3711 drive = DRIVE(inode->i_rdev);
3712 if (drive >= N_DRIVE ||
3713 !(allowed_drive_mask & (1 << drive)) ||
3714 fdc_state[FDC(drive)].version == FDC_NONE)
3715 return -ENXIO;
3717 if (TYPE(inode->i_rdev) >= NUMBER(floppy_type))
3718 return -ENXIO;
3719 old_dev = UDRS->fd_device;
3720 if (UDRS->fd_ref && old_dev != MINOR(inode->i_rdev))
3721 return -EBUSY;
3723 if (!UDRS->fd_ref && (UDP->flags & FD_BROKEN_DCL)){
3724 USETF(FD_DISK_CHANGED);
3725 USETF(FD_VERIFY);
3728 if (UDRS->fd_ref == -1 ||
3729 (UDRS->fd_ref && (filp->f_flags & O_EXCL)))
3730 return -EBUSY;
3732 if (floppy_grab_irq_and_dma())
3733 return -EBUSY;
3735 if (filp->f_flags & O_EXCL)
3736 UDRS->fd_ref = -1;
3737 else
3738 UDRS->fd_ref++;
3740 if (!floppy_track_buffer){
3741 /* if opening an ED drive, reserve a big buffer,
3742 * else reserve a small one */
3743 if ((UDP->cmos == 6) || (UDP->cmos == 5))
3744 try = 64; /* Only 48 actually useful */
3745 else
3746 try = 32; /* Only 24 actually useful */
3748 tmp=(char *)fd_dma_mem_alloc(1024 * try);
3749 if (!tmp && !floppy_track_buffer) {
3750 try >>= 1; /* buffer only one side */
3751 INFBOUND(try, 16);
3752 tmp= (char *)fd_dma_mem_alloc(1024*try);
3754 if (!tmp && !floppy_track_buffer) {
3755 fallback_on_nodma_alloc(&tmp, 2048 * try);
3757 if (!tmp && !floppy_track_buffer) {
3758 DPRINT("Unable to allocate DMA memory\n");
3759 RETERR(ENXIO);
3761 if (floppy_track_buffer) {
3762 if (tmp)
3763 fd_dma_mem_free((unsigned long)tmp,try*1024);
3764 } else {
3765 buffer_min = buffer_max = -1;
3766 floppy_track_buffer = tmp;
3767 max_buffer_sectors = try;
3771 UDRS->fd_device = MINOR(inode->i_rdev);
3772 if (old_dev != -1 && old_dev != MINOR(inode->i_rdev)) {
3773 if (buffer_drive == drive)
3774 buffer_track = -1;
3775 invalidate_buffers(MKDEV(FLOPPY_MAJOR,old_dev));
3778 if (UFDCS->rawcmd == 1)
3779 UFDCS->rawcmd = 2;
3781 if (filp->f_flags & O_NDELAY)
3782 return 0;
3783 if (filp->f_mode & 3) {
3784 UDRS->last_checked = 0;
3785 check_disk_change(inode->i_rdev);
3786 if (UTESTF(FD_DISK_CHANGED))
3787 RETERR(ENXIO);
3789 if ((filp->f_mode & 2) && !(UTESTF(FD_DISK_WRITABLE)))
3790 RETERR(EROFS);
3791 return 0;
3792 #undef RETERR
3796 * Check if the disk has been changed or if a change has been faked.
3798 static int check_floppy_change(kdev_t dev)
3800 int drive = DRIVE(dev);
3802 if (MAJOR(dev) != MAJOR_NR) {
3803 DPRINT("check_floppy_change: not a floppy\n");
3804 return 0;
3807 if (UTESTF(FD_DISK_CHANGED) || UTESTF(FD_VERIFY))
3808 return 1;
3810 if (UDP->checkfreq < (int)(jiffies - UDRS->last_checked)) {
3811 if(floppy_grab_irq_and_dma()) {
3812 return 1;
3815 lock_fdc(drive,0);
3816 poll_drive(0,0);
3817 process_fd_request();
3818 floppy_release_irq_and_dma();
3821 if (UTESTF(FD_DISK_CHANGED) ||
3822 UTESTF(FD_VERIFY) ||
3823 test_bit(drive, &fake_change) ||
3824 (!TYPE(dev) && !current_type[drive]))
3825 return 1;
3826 return 0;
3829 /* revalidate the floppy disk, i.e. trigger format autodetection by reading
3830 * the bootblock (block 0). "Autodetection" is also needed to check whether
3831 * there is a disk in the drive at all... Thus we also do it for fixed
3832 * geometry formats */
3833 static int floppy_revalidate(kdev_t dev)
3835 #define NO_GEOM (!current_type[drive] && !TYPE(dev))
3836 struct buffer_head * bh;
3837 int drive=DRIVE(dev);
3838 int cf;
3840 if (UTESTF(FD_DISK_CHANGED) ||
3841 UTESTF(FD_VERIFY) ||
3842 test_bit(drive, &fake_change) ||
3843 NO_GEOM){
3844 if(usage_count == 0) {
3845 printk("VFS: revalidate called on non-open device.\n");
3846 return -EFAULT;
3848 lock_fdc(drive,0);
3849 cf = UTESTF(FD_DISK_CHANGED) || UTESTF(FD_VERIFY);
3850 if (!(cf || test_bit(drive, &fake_change) || NO_GEOM)){
3851 process_fd_request(); /*already done by another thread*/
3852 return 0;
3854 UDRS->maxblock = 0;
3855 UDRS->maxtrack = 0;
3856 if (buffer_drive == drive)
3857 buffer_track = -1;
3858 clear_bit(drive, &fake_change);
3859 UCLEARF(FD_DISK_CHANGED);
3860 if (cf)
3861 UDRS->generation++;
3862 if (NO_GEOM){
3863 /* auto-sensing */
3864 int size = floppy_blocksizes[MINOR(dev)];
3865 if (!size)
3866 size = 1024;
3867 if (!(bh = getblk(dev,0,size))){
3868 process_fd_request();
3869 return -ENXIO;
3871 if (bh && !buffer_uptodate(bh))
3872 ll_rw_block(READ, 1, &bh);
3873 process_fd_request();
3874 wait_on_buffer(bh);
3875 brelse(bh);
3876 return 0;
3878 if (cf)
3879 poll_drive(0, FD_RAW_NEED_DISK);
3880 process_fd_request();
3882 return 0;
3885 static struct block_device_operations floppy_fops = {
3886 open: floppy_open,
3887 release: floppy_release,
3888 ioctl: fd_ioctl,
3889 check_media_change: check_floppy_change,
3890 revalidate: floppy_revalidate,
3893 static void __init register_devfs_entries (int drive)
3895 int base_minor, i;
3896 static char *table[] =
3897 {"", "d360", "h1200", "u360", "u720", "h360", "h720",
3898 "u1440", "u2880", "CompaQ", "h1440", "u1680", "h410",
3899 "u820", "h1476", "u1722", "h420", "u830", "h1494", "u1743",
3900 "h880", "u1040", "u1120", "h1600", "u1760", "u1920",
3901 "u3200", "u3520", "u3840", "u1840", "u800", "u1600",
3902 NULL
3904 static int t360[] = {1,0}, t1200[] = {2,5,6,10,12,14,16,18,20,23,0},
3905 t3in[] = {8,9,26,27,28, 7,11,15,19,24,25,29,31, 3,4,13,17,21,22,30,0};
3906 static int *table_sup[] =
3907 {NULL, t360, t1200, t3in+5+8, t3in+5, t3in, t3in};
3909 base_minor = (drive < 4) ? drive : (124 + drive);
3910 if (UDP->cmos <= NUMBER(default_drive_params)) {
3911 i = 0;
3912 do {
3913 char name[16];
3915 sprintf (name, "%d%s", drive, table[table_sup[UDP->cmos][i]]);
3916 devfs_register (devfs_handle, name, DEVFS_FL_DEFAULT, MAJOR_NR,
3917 base_minor + (table_sup[UDP->cmos][i] << 2),
3918 S_IFBLK | S_IRUSR | S_IWUSR | S_IRGRP |S_IWGRP,
3919 &floppy_fops, NULL);
3920 } while (table_sup[UDP->cmos][i++]);
3925 * Floppy Driver initialization
3926 * =============================
3929 /* Determine the floppy disk controller type */
3930 /* This routine was written by David C. Niemi */
3931 static char __init get_fdc_version(void)
3933 int r;
3935 output_byte(FD_DUMPREGS); /* 82072 and better know DUMPREGS */
3936 if (FDCS->reset)
3937 return FDC_NONE;
3938 if ((r = result()) <= 0x00)
3939 return FDC_NONE; /* No FDC present ??? */
3940 if ((r==1) && (reply_buffer[0] == 0x80)){
3941 printk(KERN_INFO "FDC %d is an 8272A\n",fdc);
3942 return FDC_8272A; /* 8272a/765 don't know DUMPREGS */
3944 if (r != 10) {
3945 printk("FDC %d init: DUMPREGS: unexpected return of %d bytes.\n",
3946 fdc, r);
3947 return FDC_UNKNOWN;
3950 if (!fdc_configure()) {
3951 printk(KERN_INFO "FDC %d is an 82072\n",fdc);
3952 return FDC_82072; /* 82072 doesn't know CONFIGURE */
3955 output_byte(FD_PERPENDICULAR);
3956 if (need_more_output() == MORE_OUTPUT) {
3957 output_byte(0);
3958 } else {
3959 printk(KERN_INFO "FDC %d is an 82072A\n", fdc);
3960 return FDC_82072A; /* 82072A as found on Sparcs. */
3963 output_byte(FD_UNLOCK);
3964 r = result();
3965 if ((r == 1) && (reply_buffer[0] == 0x80)){
3966 printk(KERN_INFO "FDC %d is a pre-1991 82077\n", fdc);
3967 return FDC_82077_ORIG; /* Pre-1991 82077, doesn't know
3968 * LOCK/UNLOCK */
3970 if ((r != 1) || (reply_buffer[0] != 0x00)) {
3971 printk("FDC %d init: UNLOCK: unexpected return of %d bytes.\n",
3972 fdc, r);
3973 return FDC_UNKNOWN;
3975 output_byte(FD_PARTID);
3976 r = result();
3977 if (r != 1) {
3978 printk("FDC %d init: PARTID: unexpected return of %d bytes.\n",
3979 fdc, r);
3980 return FDC_UNKNOWN;
3982 if (reply_buffer[0] == 0x80) {
3983 printk(KERN_INFO "FDC %d is a post-1991 82077\n",fdc);
3984 return FDC_82077; /* Revised 82077AA passes all the tests */
3986 switch (reply_buffer[0] >> 5) {
3987 case 0x0:
3988 /* Either a 82078-1 or a 82078SL running at 5Volt */
3989 printk(KERN_INFO "FDC %d is an 82078.\n",fdc);
3990 return FDC_82078;
3991 case 0x1:
3992 printk(KERN_INFO "FDC %d is a 44pin 82078\n",fdc);
3993 return FDC_82078;
3994 case 0x2:
3995 printk(KERN_INFO "FDC %d is a S82078B\n", fdc);
3996 return FDC_S82078B;
3997 case 0x3:
3998 printk(KERN_INFO "FDC %d is a National Semiconductor PC87306\n", fdc);
3999 return FDC_87306;
4000 default:
4001 printk(KERN_INFO "FDC %d init: 82078 variant with unknown PARTID=%d.\n",
4002 fdc, reply_buffer[0] >> 5);
4003 return FDC_82078_UNKN;
4005 } /* get_fdc_version */
4007 /* lilo configuration */
4009 static void __init floppy_set_flags(int *ints,int param, int param2)
4011 int i;
4013 for (i=0; i < ARRAY_SIZE(default_drive_params); i++){
4014 if (param)
4015 default_drive_params[i].params.flags |= param2;
4016 else
4017 default_drive_params[i].params.flags &= ~param2;
4019 DPRINT("%s flag 0x%x\n", param2 ? "Setting" : "Clearing", param);
4022 static void __init daring(int *ints,int param, int param2)
4024 int i;
4026 for (i=0; i < ARRAY_SIZE(default_drive_params); i++){
4027 if (param){
4028 default_drive_params[i].params.select_delay = 0;
4029 default_drive_params[i].params.flags |= FD_SILENT_DCL_CLEAR;
4030 } else {
4031 default_drive_params[i].params.select_delay = 2*HZ/100;
4032 default_drive_params[i].params.flags &= ~FD_SILENT_DCL_CLEAR;
4035 DPRINT("Assuming %s floppy hardware\n", param ? "standard" : "broken");
4038 static void __init set_cmos(int *ints, int dummy, int dummy2)
4040 int current_drive=0;
4042 if (ints[0] != 2){
4043 DPRINT("wrong number of parameters for CMOS\n");
4044 return;
4046 current_drive = ints[1];
4047 if (current_drive < 0 || current_drive >= 8){
4048 DPRINT("bad drive for set_cmos\n");
4049 return;
4051 if (current_drive >= 4 && !FDC2)
4052 FDC2 = 0x370;
4053 DP->cmos = ints[2];
4054 DPRINT("setting CMOS code to %d\n", ints[2]);
4057 static struct param_table {
4058 const char *name;
4059 void (*fn)(int *ints, int param, int param2);
4060 int *var;
4061 int def_param;
4062 int param2;
4063 } config_params[]={
4064 { "allowed_drive_mask", 0, &allowed_drive_mask, 0xff, 0}, /* obsolete */
4065 { "all_drives", 0, &allowed_drive_mask, 0xff, 0 }, /* obsolete */
4066 { "asus_pci", 0, &allowed_drive_mask, 0x33, 0},
4068 { "irq", 0, &FLOPPY_IRQ, 6, 0 },
4069 { "dma", 0, &FLOPPY_DMA, 2, 0 },
4071 { "daring", daring, 0, 1, 0},
4073 { "two_fdc", 0, &FDC2, 0x370, 0 },
4074 { "one_fdc", 0, &FDC2, 0, 0 },
4076 { "thinkpad", floppy_set_flags, 0, 1, FD_INVERTED_DCL },
4077 { "broken_dcl", floppy_set_flags, 0, 1, FD_BROKEN_DCL },
4078 { "messages", floppy_set_flags, 0, 1, FTD_MSG },
4079 { "silent_dcl_clear", floppy_set_flags, 0, 1, FD_SILENT_DCL_CLEAR },
4080 { "debug", floppy_set_flags, 0, 1, FD_DEBUG },
4082 { "nodma", 0, &can_use_virtual_dma, 1, 0 },
4083 { "omnibook", 0, &can_use_virtual_dma, 1, 0 },
4084 { "yesdma", 0, &can_use_virtual_dma, 0, 0 },
4086 { "fifo_depth", 0, &fifo_depth, 0xa, 0 },
4087 { "nofifo", 0, &no_fifo, 0x20, 0 },
4088 { "usefifo", 0, &no_fifo, 0, 0 },
4090 { "cmos", set_cmos, 0, 0, 0 },
4091 { "slow", 0, &slow_floppy, 1, 0 },
4093 { "unexpected_interrupts", 0, &print_unex, 1, 0 },
4094 { "no_unexpected_interrupts", 0, &print_unex, 0, 0 },
4095 { "L40SX", 0, &print_unex, 0, 0 }
4098 static int __init floppy_setup(char *str)
4100 int i;
4101 int param;
4102 int ints[11];
4104 str = get_options(str,ARRAY_SIZE(ints),ints);
4105 if (str) {
4106 for (i=0; i< ARRAY_SIZE(config_params); i++){
4107 if (strcmp(str,config_params[i].name) == 0){
4108 if (ints[0])
4109 param = ints[1];
4110 else
4111 param = config_params[i].def_param;
4112 if (config_params[i].fn)
4113 config_params[i].
4114 fn(ints,param,
4115 config_params[i].param2);
4116 if (config_params[i].var) {
4117 DPRINT("%s=%d\n", str, param);
4118 *config_params[i].var = param;
4120 return 1;
4124 if (str) {
4125 DPRINT("unknown floppy option [%s]\n", str);
4127 DPRINT("allowed options are:");
4128 for (i=0; i< ARRAY_SIZE(config_params); i++)
4129 printk(" %s",config_params[i].name);
4130 printk("\n");
4131 } else
4132 DPRINT("botched floppy option\n");
4133 DPRINT("Read linux/Documentation/floppy.txt\n");
4134 return 0;
4137 static int have_no_fdc= -EIO;
4140 int __init floppy_init(void)
4142 int i,unit,drive;
4145 raw_cmd = NULL;
4147 devfs_handle = devfs_mk_dir (NULL, "floppy", NULL);
4148 if (devfs_register_blkdev(MAJOR_NR,"fd",&floppy_fops)) {
4149 printk("Unable to get major %d for floppy\n",MAJOR_NR);
4150 return -EBUSY;
4153 for (i=0; i<256; i++)
4154 if (ITYPE(i))
4155 floppy_sizes[i] = (floppy_type[ITYPE(i)].size+1) >> 1;
4156 else
4157 floppy_sizes[i] = MAX_DISK_SIZE;
4159 blk_size[MAJOR_NR] = floppy_sizes;
4160 blksize_size[MAJOR_NR] = floppy_blocksizes;
4161 blk_init_queue(BLK_DEFAULT_QUEUE(MAJOR_NR), DEVICE_REQUEST);
4162 reschedule_timeout(MAXTIMEOUT, "floppy init", MAXTIMEOUT);
4163 config_types();
4165 for (i = 0; i < N_FDC; i++) {
4166 fdc = i;
4167 CLEARSTRUCT(FDCS);
4168 FDCS->dtr = -1;
4169 FDCS->dor = 0x4;
4170 #ifdef __sparc__
4171 /*sparcs don't have a DOR reset which we can fall back on to*/
4172 FDCS->version = FDC_82072A;
4173 #endif
4176 use_virtual_dma = can_use_virtual_dma & 1;
4177 fdc_state[0].address = FDC1;
4178 if (fdc_state[0].address == -1) {
4179 devfs_unregister_blkdev(MAJOR_NR,"fd");
4180 del_timer(&fd_timeout);
4181 blk_cleanup_queue(BLK_DEFAULT_QUEUE(MAJOR_NR));
4182 return -ENODEV;
4184 #if N_FDC > 1
4185 fdc_state[1].address = FDC2;
4186 #endif
4188 fdc = 0; /* reset fdc in case of unexpected interrupt */
4189 if (floppy_grab_irq_and_dma()){
4190 del_timer(&fd_timeout);
4191 blk_cleanup_queue(BLK_DEFAULT_QUEUE(MAJOR_NR));
4192 devfs_unregister_blkdev(MAJOR_NR,"fd");
4193 del_timer(&fd_timeout);
4194 return -EBUSY;
4197 /* initialise drive state */
4198 for (drive = 0; drive < N_DRIVE; drive++) {
4199 CLEARSTRUCT(UDRS);
4200 CLEARSTRUCT(UDRWE);
4201 USETF(FD_DISK_NEWCHANGE);
4202 USETF(FD_DISK_CHANGED);
4203 USETF(FD_VERIFY);
4204 UDRS->fd_device = -1;
4205 floppy_track_buffer = NULL;
4206 max_buffer_sectors = 0;
4209 for (i = 0; i < N_FDC; i++) {
4210 fdc = i;
4211 FDCS->driver_version = FD_DRIVER_VERSION;
4212 for (unit=0; unit<4; unit++)
4213 FDCS->track[unit] = 0;
4214 if (FDCS->address == -1)
4215 continue;
4216 FDCS->rawcmd = 2;
4217 if (user_reset_fdc(-1,FD_RESET_ALWAYS,0)){
4218 /* free ioports reserved by floppy_grab_irq_and_dma() */
4219 release_region(FDCS->address, 6);
4220 release_region(FDCS->address+7, 1);
4221 FDCS->address = -1;
4222 FDCS->version = FDC_NONE;
4223 continue;
4225 /* Try to determine the floppy controller type */
4226 FDCS->version = get_fdc_version();
4227 if (FDCS->version == FDC_NONE){
4228 /* free ioports reserved by floppy_grab_irq_and_dma() */
4229 release_region(FDCS->address, 6);
4230 release_region(FDCS->address+7, 1);
4231 FDCS->address = -1;
4232 continue;
4234 if (can_use_virtual_dma == 2 && FDCS->version < FDC_82072A)
4235 can_use_virtual_dma = 0;
4237 have_no_fdc = 0;
4238 /* Not all FDCs seem to be able to handle the version command
4239 * properly, so force a reset for the standard FDC clones,
4240 * to avoid interrupt garbage.
4242 user_reset_fdc(-1,FD_RESET_ALWAYS,0);
4244 fdc=0;
4245 del_timer(&fd_timeout);
4246 current_drive = 0;
4247 floppy_release_irq_and_dma();
4248 initialising=0;
4249 if (have_no_fdc)
4251 DPRINT("no floppy controllers found\n");
4252 floppy_tq.routine = (void *)(void *) empty;
4253 mark_bh(IMMEDIATE_BH);
4254 schedule();
4255 if (usage_count)
4256 floppy_release_irq_and_dma();
4257 blk_cleanup_queue(BLK_DEFAULT_QUEUE(MAJOR_NR));
4258 devfs_unregister_blkdev(MAJOR_NR,"fd");
4261 for (drive = 0; drive < N_DRIVE; drive++) {
4262 if (!(allowed_drive_mask & (1 << drive)))
4263 continue;
4264 if (fdc_state[FDC(drive)].version == FDC_NONE)
4265 continue;
4266 for (i = 0; i<NUMBER(floppy_type); i++)
4267 register_disk(NULL, MKDEV(MAJOR_NR,TOMINOR(drive)+i*4),
4268 1, &floppy_fops, 0);
4270 return have_no_fdc;
4273 static spinlock_t floppy_usage_lock = SPIN_LOCK_UNLOCKED;
4275 static int floppy_grab_irq_and_dma(void)
4277 unsigned long flags;
4279 spin_lock_irqsave(&floppy_usage_lock, flags);
4280 if (usage_count++){
4281 spin_unlock_irqrestore(&floppy_usage_lock, flags);
4282 return 0;
4284 spin_unlock_irqrestore(&floppy_usage_lock, flags);
4285 MOD_INC_USE_COUNT;
4286 if (fd_request_irq()) {
4287 DPRINT("Unable to grab IRQ%d for the floppy driver\n",
4288 FLOPPY_IRQ);
4289 MOD_DEC_USE_COUNT;
4290 spin_lock_irqsave(&floppy_usage_lock, flags);
4291 usage_count--;
4292 spin_unlock_irqrestore(&floppy_usage_lock, flags);
4293 return -1;
4295 if (fd_request_dma()) {
4296 DPRINT("Unable to grab DMA%d for the floppy driver\n",
4297 FLOPPY_DMA);
4298 fd_free_irq();
4299 MOD_DEC_USE_COUNT;
4300 spin_lock_irqsave(&floppy_usage_lock, flags);
4301 usage_count--;
4302 spin_unlock_irqrestore(&floppy_usage_lock, flags);
4303 return -1;
4306 for (fdc=0; fdc< N_FDC; fdc++){
4307 if (FDCS->address != -1){
4308 if (!request_region(FDCS->address, 6, "floppy")) {
4309 DPRINT("Floppy io-port 0x%04lx in use\n", FDCS->address);
4310 goto cleanup1;
4312 if (!request_region(FDCS->address + 7, 1, "floppy DIR")) {
4313 DPRINT("Floppy io-port 0x%04lx in use\n", FDCS->address + 7);
4314 goto cleanup2;
4316 /* address + 6 is reserved, and may be taken by IDE.
4317 * Unfortunately, Adaptec doesn't know this :-(, */
4320 for (fdc=0; fdc< N_FDC; fdc++){
4321 if (FDCS->address != -1){
4322 reset_fdc_info(1);
4323 fd_outb(FDCS->dor, FD_DOR);
4326 fdc = 0;
4327 set_dor(0, ~0, 8); /* avoid immediate interrupt */
4329 for (fdc = 0; fdc < N_FDC; fdc++)
4330 if (FDCS->address != -1)
4331 fd_outb(FDCS->dor, FD_DOR);
4333 * The driver will try and free resources and relies on us
4334 * to know if they were allocated or not.
4336 fdc = 0;
4337 irqdma_allocated = 1;
4338 return 0;
4339 cleanup2:
4340 release_region(FDCS->address, 6);
4341 cleanup1:
4342 fd_free_irq();
4343 fd_free_dma();
4344 while(--fdc >= 0) {
4345 release_region(FDCS->address, 6);
4346 release_region(FDCS->address + 7, 1);
4348 MOD_DEC_USE_COUNT;
4349 spin_lock_irqsave(&floppy_usage_lock, flags);
4350 usage_count--;
4351 spin_unlock_irqrestore(&floppy_usage_lock, flags);
4352 return -1;
4355 static void floppy_release_irq_and_dma(void)
4357 int old_fdc;
4358 #ifdef FLOPPY_SANITY_CHECK
4359 #ifndef __sparc__
4360 int drive;
4361 #endif
4362 #endif
4363 long tmpsize;
4364 unsigned long tmpaddr;
4365 unsigned long flags;
4367 spin_lock_irqsave(&floppy_usage_lock, flags);
4368 if (--usage_count){
4369 spin_unlock_irqrestore(&floppy_usage_lock, flags);
4370 return;
4372 spin_unlock_irqrestore(&floppy_usage_lock, flags);
4373 if(irqdma_allocated)
4375 fd_disable_dma();
4376 fd_free_dma();
4377 fd_free_irq();
4378 irqdma_allocated=0;
4380 set_dor(0, ~0, 8);
4381 #if N_FDC > 1
4382 set_dor(1, ~8, 0);
4383 #endif
4384 floppy_enable_hlt();
4386 if (floppy_track_buffer && max_buffer_sectors) {
4387 tmpsize = max_buffer_sectors*1024;
4388 tmpaddr = (unsigned long)floppy_track_buffer;
4389 floppy_track_buffer = NULL;
4390 max_buffer_sectors = 0;
4391 buffer_min = buffer_max = -1;
4392 fd_dma_mem_free(tmpaddr, tmpsize);
4395 #ifdef FLOPPY_SANITY_CHECK
4396 #ifndef __sparc__
4397 for (drive=0; drive < N_FDC * 4; drive++)
4398 if (timer_pending(motor_off_timer + drive))
4399 printk("motor off timer %d still active\n", drive);
4400 #endif
4402 if (timer_pending(&fd_timeout))
4403 printk("floppy timer still active:%s\n", timeout_message);
4404 if (timer_pending(&fd_timer))
4405 printk("auxiliary floppy timer still active\n");
4406 if (floppy_tq.sync)
4407 printk("task queue still active\n");
4408 #endif
4409 old_fdc = fdc;
4410 for (fdc = 0; fdc < N_FDC; fdc++)
4411 if (FDCS->address != -1) {
4412 release_region(FDCS->address, 6);
4413 release_region(FDCS->address+7, 1);
4415 fdc = old_fdc;
4416 MOD_DEC_USE_COUNT;
4420 #ifdef MODULE
4422 char *floppy;
4424 static void __init parse_floppy_cfg_string(char *cfg)
4426 char *ptr;
4428 while(*cfg) {
4429 for(ptr = cfg;*cfg && *cfg != ' ' && *cfg != '\t'; cfg++);
4430 if (*cfg) {
4431 *cfg = '\0';
4432 cfg++;
4434 if (*ptr)
4435 floppy_setup(ptr);
4439 int init_module(void)
4441 printk(KERN_INFO "inserting floppy driver for " UTS_RELEASE "\n");
4443 if (floppy)
4444 parse_floppy_cfg_string(floppy);
4445 return floppy_init();
4448 void cleanup_module(void)
4450 int dummy;
4452 devfs_unregister (devfs_handle);
4453 devfs_unregister_blkdev(MAJOR_NR, "fd");
4455 blk_cleanup_queue(BLK_DEFAULT_QUEUE(MAJOR_NR));
4456 /* eject disk, if any */
4457 dummy = fd_eject(0);
4460 MODULE_PARM(floppy,"s");
4461 MODULE_PARM(FLOPPY_IRQ,"i");
4462 MODULE_PARM(FLOPPY_DMA,"i");
4463 MODULE_AUTHOR("Alain L. Knaff");
4464 MODULE_SUPPORTED_DEVICE("fd");
4466 #else
4468 __setup ("floppy=", floppy_setup);
4470 /* eject the boot floppy (if we need the drive for a different root floppy) */
4471 /* This should only be called at boot time when we're sure that there's no
4472 * resource contention. */
4473 void floppy_eject(void)
4475 int dummy;
4476 if (have_no_fdc)
4477 return;
4478 if(floppy_grab_irq_and_dma()==0)
4480 lock_fdc(MAXTIMEOUT,0);
4481 dummy=fd_eject(0);
4482 process_fd_request();
4483 floppy_release_irq_and_dma();
4486 #endif