Merge with Linux 2.4.0-test6-pre9.
[linux-2.6/linux-mips.git] / drivers / block / floppy.c
blobdcd9d1036eace8a711a5c809a0b56b69c3c6628b
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).
121 #define FLOPPY_SANITY_CHECK
122 #undef FLOPPY_SILENT_DCL_CLEAR
124 #define REALLY_SLOW_IO
126 #define DEBUGT 2
127 #define DCL_DEBUG /* debug disk change line */
129 /* do print messages for unexpected interrupts */
130 static int print_unex=1;
131 #include <linux/module.h>
132 #include <linux/sched.h>
133 #include <linux/fs.h>
134 #include <linux/kernel.h>
135 #include <linux/timer.h>
136 #include <linux/tqueue.h>
137 #define FDPATCHES
138 #include <linux/fdreg.h>
141 * 1998/1/21 -- Richard Gooch <rgooch@atnf.csiro.au> -- devfs support
145 #include <linux/fd.h>
146 #include <linux/hdreg.h>
148 #include <linux/errno.h>
149 #include <linux/malloc.h>
150 #include <linux/mm.h>
151 #include <linux/string.h>
152 #include <linux/fcntl.h>
153 #include <linux/delay.h>
154 #include <linux/mc146818rtc.h> /* CMOS defines */
155 #include <linux/ioport.h>
156 #include <linux/interrupt.h>
157 #include <linux/init.h>
158 #include <linux/devfs_fs_kernel.h>
161 * PS/2 floppies have much slower step rates than regular floppies.
162 * It's been recommended that take about 1/4 of the default speed
163 * in some more extreme cases.
165 static int slow_floppy = 0;
167 #include <asm/dma.h>
168 #include <asm/irq.h>
169 #include <asm/system.h>
170 #include <asm/io.h>
171 #include <asm/uaccess.h>
173 static int FLOPPY_IRQ=6;
174 static int FLOPPY_DMA=2;
175 static int can_use_virtual_dma=2;
176 /* =======
177 * can use virtual DMA:
178 * 0 = use of virtual DMA disallowed by config
179 * 1 = use of virtual DMA prescribed by config
180 * 2 = no virtual DMA preference configured. By default try hard DMA,
181 * but fall back on virtual DMA when not enough memory available
184 static int use_virtual_dma;
185 /* =======
186 * use virtual DMA
187 * 0 using hard DMA
188 * 1 using virtual DMA
189 * This variable is set to virtual when a DMA mem problem arises, and
190 * reset back in floppy_grab_irq_and_dma.
191 * It is not safe to reset it in other circumstances, because the floppy
192 * driver may have several buffers in use at once, and we do currently not
193 * record each buffers capabilities
196 static unsigned short virtual_dma_port=0x3f0;
197 void floppy_interrupt(int irq, void *dev_id, struct pt_regs * regs);
198 static int set_dor(int fdc, char mask, char data);
199 static void register_devfs_entries (int drive) __init;
200 static devfs_handle_t devfs_handle = NULL;
202 #define K_64 0x10000 /* 64KB */
204 /* the following is the mask of allowed drives. By default units 2 and
205 * 3 of both floppy controllers are disabled, because switching on the
206 * motor of these drives causes system hangs on some PCI computers. drive
207 * 0 is the low bit (0x1), and drive 7 is the high bit (0x80). Bits are on if
208 * a drive is allowed.
210 * NOTE: This must come before we include the arch floppy header because
211 * some ports reference this variable from there. -DaveM
214 static int allowed_drive_mask = 0x33;
216 #include <asm/floppy.h>
218 static int irqdma_allocated = 0;
220 #define MAJOR_NR FLOPPY_MAJOR
222 #include <linux/blk.h>
223 #include <linux/blkpg.h>
224 #include <linux/cdrom.h> /* for the compatibility eject ioctl */
226 #ifndef fd_get_dma_residue
227 #define fd_get_dma_residue() get_dma_residue(FLOPPY_DMA)
228 #endif
230 /* Dma Memory related stuff */
232 #ifndef fd_dma_mem_free
233 #define fd_dma_mem_free(addr, size) free_pages(addr, get_order(size))
234 #endif
236 #ifndef fd_dma_mem_alloc
237 #define fd_dma_mem_alloc(size) __get_dma_pages(GFP_KERNEL,get_order(size))
238 #endif
240 static inline void fallback_on_nodma_alloc(char **addr, size_t l)
242 #ifdef FLOPPY_CAN_FALLBACK_ON_NODMA
243 if (*addr)
244 return; /* we have the memory */
245 if (can_use_virtual_dma != 2)
246 return; /* no fallback allowed */
247 printk("DMA memory shortage. Temporarily falling back on virtual DMA\n");
248 *addr = (char *) nodma_mem_alloc(l);
249 #else
250 return;
251 #endif
254 /* End dma memory related stuff */
256 static unsigned long fake_change = 0;
257 static int initialising=1;
259 static inline int TYPE(kdev_t x) {
260 return (MINOR(x)>>2) & 0x1f;
262 static inline int DRIVE(kdev_t x) {
263 return (MINOR(x)&0x03) | ((MINOR(x)&0x80) >> 5);
265 #define ITYPE(x) (((x)>>2) & 0x1f)
266 #define TOMINOR(x) ((x & 3) | ((x & 4) << 5))
267 #define UNIT(x) ((x) & 0x03) /* drive on fdc */
268 #define FDC(x) (((x) & 0x04) >> 2) /* fdc of drive */
269 #define REVDRIVE(fdc, unit) ((unit) + ((fdc) << 2))
270 /* reverse mapping from unit and fdc to drive */
271 #define DP (&drive_params[current_drive])
272 #define DRS (&drive_state[current_drive])
273 #define DRWE (&write_errors[current_drive])
274 #define FDCS (&fdc_state[fdc])
275 #define CLEARF(x) (clear_bit(x##_BIT, &DRS->flags))
276 #define SETF(x) (set_bit(x##_BIT, &DRS->flags))
277 #define TESTF(x) (test_bit(x##_BIT, &DRS->flags))
279 #define UDP (&drive_params[drive])
280 #define UDRS (&drive_state[drive])
281 #define UDRWE (&write_errors[drive])
282 #define UFDCS (&fdc_state[FDC(drive)])
283 #define UCLEARF(x) (clear_bit(x##_BIT, &UDRS->flags))
284 #define USETF(x) (set_bit(x##_BIT, &UDRS->flags))
285 #define UTESTF(x) (test_bit(x##_BIT, &UDRS->flags))
287 #define DPRINT(format, args...) printk(DEVICE_NAME "%d: " format, current_drive , ## args)
289 #define PH_HEAD(floppy,head) (((((floppy)->stretch & 2) >>1) ^ head) << 2)
290 #define STRETCH(floppy) ((floppy)->stretch & FD_STRETCH)
292 #define CLEARSTRUCT(x) memset((x), 0, sizeof(*(x)))
294 /* read/write */
295 #define COMMAND raw_cmd->cmd[0]
296 #define DR_SELECT raw_cmd->cmd[1]
297 #define TRACK raw_cmd->cmd[2]
298 #define HEAD raw_cmd->cmd[3]
299 #define SECTOR raw_cmd->cmd[4]
300 #define SIZECODE raw_cmd->cmd[5]
301 #define SECT_PER_TRACK raw_cmd->cmd[6]
302 #define GAP raw_cmd->cmd[7]
303 #define SIZECODE2 raw_cmd->cmd[8]
304 #define NR_RW 9
306 /* format */
307 #define F_SIZECODE raw_cmd->cmd[2]
308 #define F_SECT_PER_TRACK raw_cmd->cmd[3]
309 #define F_GAP raw_cmd->cmd[4]
310 #define F_FILL raw_cmd->cmd[5]
311 #define NR_F 6
314 * Maximum disk size (in kilobytes). This default is used whenever the
315 * current disk size is unknown.
316 * [Now it is rather a minimum]
318 #define MAX_DISK_SIZE 4 /* 3984*/
322 * globals used by 'result()'
324 #define MAX_REPLIES 16
325 static unsigned char reply_buffer[MAX_REPLIES];
326 static int inr; /* size of reply buffer, when called from interrupt */
327 #define ST0 (reply_buffer[0])
328 #define ST1 (reply_buffer[1])
329 #define ST2 (reply_buffer[2])
330 #define ST3 (reply_buffer[0]) /* result of GETSTATUS */
331 #define R_TRACK (reply_buffer[3])
332 #define R_HEAD (reply_buffer[4])
333 #define R_SECTOR (reply_buffer[5])
334 #define R_SIZECODE (reply_buffer[6])
336 #define SEL_DLY (2*HZ/100)
339 * this struct defines the different floppy drive types.
341 static struct {
342 struct floppy_drive_params params;
343 const char *name; /* name printed while booting */
344 } default_drive_params[]= {
345 /* NOTE: the time values in jiffies should be in msec!
346 CMOS drive type
347 | Maximum data rate supported by drive type
348 | | Head load time, msec
349 | | | Head unload time, msec (not used)
350 | | | | Step rate interval, usec
351 | | | | | Time needed for spinup time (jiffies)
352 | | | | | | Timeout for spinning down (jiffies)
353 | | | | | | | Spindown offset (where disk stops)
354 | | | | | | | | Select delay
355 | | | | | | | | | RPS
356 | | | | | | | | | | Max number of tracks
357 | | | | | | | | | | | Interrupt timeout
358 | | | | | | | | | | | | Max nonintlv. sectors
359 | | | | | | | | | | | | | -Max Errors- flags */
360 {{0, 500, 16, 16, 8000, 1*HZ, 3*HZ, 0, SEL_DLY, 5, 80, 3*HZ, 20, {3,1,2,0,2}, 0,
361 0, { 7, 4, 8, 2, 1, 5, 3,10}, 3*HZ/2, 0 }, "unknown" },
363 {{1, 300, 16, 16, 8000, 1*HZ, 3*HZ, 0, SEL_DLY, 5, 40, 3*HZ, 17, {3,1,2,0,2}, 0,
364 0, { 1, 0, 0, 0, 0, 0, 0, 0}, 3*HZ/2, 1 }, "360K PC" }, /*5 1/4 360 KB PC*/
366 {{2, 500, 16, 16, 6000, 4*HZ/10, 3*HZ, 14, SEL_DLY, 6, 83, 3*HZ, 17, {3,1,2,0,2}, 0,
367 0, { 2, 5, 6,23,10,20,12, 0}, 3*HZ/2, 2 }, "1.2M" }, /*5 1/4 HD AT*/
369 {{3, 250, 16, 16, 3000, 1*HZ, 3*HZ, 0, SEL_DLY, 5, 83, 3*HZ, 20, {3,1,2,0,2}, 0,
370 0, { 4,22,21,30, 3, 0, 0, 0}, 3*HZ/2, 4 }, "720k" }, /*3 1/2 DD*/
372 {{4, 500, 16, 16, 4000, 4*HZ/10, 3*HZ, 10, SEL_DLY, 5, 83, 3*HZ, 20, {3,1,2,0,2}, 0,
373 0, { 7, 4,25,22,31,21,29,11}, 3*HZ/2, 7 }, "1.44M" }, /*3 1/2 HD*/
375 {{5, 1000, 15, 8, 3000, 4*HZ/10, 3*HZ, 10, SEL_DLY, 5, 83, 3*HZ, 40, {3,1,2,0,2}, 0,
376 0, { 7, 8, 4,25,28,22,31,21}, 3*HZ/2, 8 }, "2.88M AMI BIOS" }, /*3 1/2 ED*/
378 {{6, 1000, 15, 8, 3000, 4*HZ/10, 3*HZ, 10, SEL_DLY, 5, 83, 3*HZ, 40, {3,1,2,0,2}, 0,
379 0, { 7, 8, 4,25,28,22,31,21}, 3*HZ/2, 8 }, "2.88M" } /*3 1/2 ED*/
380 /* | --autodetected formats--- | | |
381 * read_track | | Name printed when booting
382 * | Native format
383 * Frequency of disk change checks */
386 static struct floppy_drive_params drive_params[N_DRIVE];
387 static struct floppy_drive_struct drive_state[N_DRIVE];
388 static struct floppy_write_errors write_errors[N_DRIVE];
389 static struct floppy_raw_cmd *raw_cmd, default_raw_cmd;
392 * This struct defines the different floppy types.
394 * Bit 0 of 'stretch' tells if the tracks need to be doubled for some
395 * types (e.g. 360kB diskette in 1.2MB drive, etc.). Bit 1 of 'stretch'
396 * tells if the disk is in Commodore 1581 format, which means side 0 sectors
397 * are located on side 1 of the disk but with a side 0 ID, and vice-versa.
398 * This is the same as the Sharp MZ-80 5.25" CP/M disk format, except that the
399 * 1581's logical side 0 is on physical side 1, whereas the Sharp's logical
400 * side 0 is on physical side 0 (but with the misnamed sector IDs).
401 * 'stretch' should probably be renamed to something more general, like
402 * 'options'. Other parameters should be self-explanatory (see also
403 * setfdprm(8)).
406 Size
407 | Sectors per track
408 | | Head
409 | | | Tracks
410 | | | | Stretch
411 | | | | | Gap 1 size
412 | | | | | | Data rate, | 0x40 for perp
413 | | | | | | | Spec1 (stepping rate, head unload
414 | | | | | | | | /fmt gap (gap2) */
415 static struct floppy_struct floppy_type[32] = {
416 { 0, 0,0, 0,0,0x00,0x00,0x00,0x00,NULL }, /* 0 no testing */
417 { 720, 9,2,40,0,0x2A,0x02,0xDF,0x50,"d360" }, /* 1 360KB PC */
418 { 2400,15,2,80,0,0x1B,0x00,0xDF,0x54,"h1200" }, /* 2 1.2MB AT */
419 { 720, 9,1,80,0,0x2A,0x02,0xDF,0x50,"D360" }, /* 3 360KB SS 3.5" */
420 { 1440, 9,2,80,0,0x2A,0x02,0xDF,0x50,"D720" }, /* 4 720KB 3.5" */
421 { 720, 9,2,40,1,0x23,0x01,0xDF,0x50,"h360" }, /* 5 360KB AT */
422 { 1440, 9,2,80,0,0x23,0x01,0xDF,0x50,"h720" }, /* 6 720KB AT */
423 { 2880,18,2,80,0,0x1B,0x00,0xCF,0x6C,"H1440" }, /* 7 1.44MB 3.5" */
424 { 5760,36,2,80,0,0x1B,0x43,0xAF,0x54,"E2880" }, /* 8 2.88MB 3.5" */
425 { 6240,39,2,80,0,0x1B,0x43,0xAF,0x28,"E3120" }, /* 9 3.12MB 3.5" */
427 { 2880,18,2,80,0,0x25,0x00,0xDF,0x02,"h1440" }, /* 10 1.44MB 5.25" */
428 { 3360,21,2,80,0,0x1C,0x00,0xCF,0x0C,"H1680" }, /* 11 1.68MB 3.5" */
429 { 820,10,2,41,1,0x25,0x01,0xDF,0x2E,"h410" }, /* 12 410KB 5.25" */
430 { 1640,10,2,82,0,0x25,0x02,0xDF,0x2E,"H820" }, /* 13 820KB 3.5" */
431 { 2952,18,2,82,0,0x25,0x00,0xDF,0x02,"h1476" }, /* 14 1.48MB 5.25" */
432 { 3444,21,2,82,0,0x25,0x00,0xDF,0x0C,"H1722" }, /* 15 1.72MB 3.5" */
433 { 840,10,2,42,1,0x25,0x01,0xDF,0x2E,"h420" }, /* 16 420KB 5.25" */
434 { 1660,10,2,83,0,0x25,0x02,0xDF,0x2E,"H830" }, /* 17 830KB 3.5" */
435 { 2988,18,2,83,0,0x25,0x00,0xDF,0x02,"h1494" }, /* 18 1.49MB 5.25" */
436 { 3486,21,2,83,0,0x25,0x00,0xDF,0x0C,"H1743" }, /* 19 1.74 MB 3.5" */
438 { 1760,11,2,80,0,0x1C,0x09,0xCF,0x00,"h880" }, /* 20 880KB 5.25" */
439 { 2080,13,2,80,0,0x1C,0x01,0xCF,0x00,"D1040" }, /* 21 1.04MB 3.5" */
440 { 2240,14,2,80,0,0x1C,0x19,0xCF,0x00,"D1120" }, /* 22 1.12MB 3.5" */
441 { 3200,20,2,80,0,0x1C,0x20,0xCF,0x2C,"h1600" }, /* 23 1.6MB 5.25" */
442 { 3520,22,2,80,0,0x1C,0x08,0xCF,0x2e,"H1760" }, /* 24 1.76MB 3.5" */
443 { 3840,24,2,80,0,0x1C,0x20,0xCF,0x00,"H1920" }, /* 25 1.92MB 3.5" */
444 { 6400,40,2,80,0,0x25,0x5B,0xCF,0x00,"E3200" }, /* 26 3.20MB 3.5" */
445 { 7040,44,2,80,0,0x25,0x5B,0xCF,0x00,"E3520" }, /* 27 3.52MB 3.5" */
446 { 7680,48,2,80,0,0x25,0x63,0xCF,0x00,"E3840" }, /* 28 3.84MB 3.5" */
448 { 3680,23,2,80,0,0x1C,0x10,0xCF,0x00,"H1840" }, /* 29 1.84MB 3.5" */
449 { 1600,10,2,80,0,0x25,0x02,0xDF,0x2E,"D800" }, /* 30 800KB 3.5" */
450 { 3200,20,2,80,0,0x1C,0x00,0xCF,0x2C,"H1600" }, /* 31 1.6MB 3.5" */
453 #define NUMBER(x) (sizeof(x) / sizeof(*(x)))
454 #define SECTSIZE (_FD_SECTSIZE(*floppy))
456 /* Auto-detection: Disk type used until the next media change occurs. */
457 static struct floppy_struct *current_type[N_DRIVE] = {
458 NULL, NULL, NULL, NULL,
459 NULL, NULL, NULL, NULL
463 * User-provided type information. current_type points to
464 * the respective entry of this array.
466 static struct floppy_struct user_params[N_DRIVE];
468 static int floppy_sizes[256];
469 static int floppy_blocksizes[256] = { 0, };
472 * The driver is trying to determine the correct media format
473 * while probing is set. rw_interrupt() clears it after a
474 * successful access.
476 static int probing = 0;
478 /* Synchronization of FDC access. */
479 #define FD_COMMAND_NONE -1
480 #define FD_COMMAND_ERROR 2
481 #define FD_COMMAND_OKAY 3
483 static volatile int command_status = FD_COMMAND_NONE;
484 static unsigned long fdc_busy = 0;
485 static DECLARE_WAIT_QUEUE_HEAD(fdc_wait);
486 static DECLARE_WAIT_QUEUE_HEAD(command_done);
488 #define NO_SIGNAL (!interruptible || !signal_pending(current))
489 #define CALL(x) if ((x) == -EINTR) return -EINTR
490 #define ECALL(x) if ((ret = (x))) return ret;
491 #define _WAIT(x,i) CALL(ret=wait_til_done((x),i))
492 #define WAIT(x) _WAIT((x),interruptible)
493 #define IWAIT(x) _WAIT((x),1)
495 /* Errors during formatting are counted here. */
496 static int format_errors;
498 /* Format request descriptor. */
499 static struct format_descr format_req;
502 * Rate is 0 for 500kb/s, 1 for 300kbps, 2 for 250kbps
503 * Spec1 is 0xSH, where S is stepping rate (F=1ms, E=2ms, D=3ms etc),
504 * H is head unload time (1=16ms, 2=32ms, etc)
508 * Track buffer
509 * Because these are written to by the DMA controller, they must
510 * not contain a 64k byte boundary crossing, or data will be
511 * corrupted/lost.
513 static char *floppy_track_buffer;
514 static int max_buffer_sectors;
516 static int *errors;
517 typedef void (*done_f)(int);
518 static struct cont_t {
519 void (*interrupt)(void); /* this is called after the interrupt of the
520 * main command */
521 void (*redo)(void); /* this is called to retry the operation */
522 void (*error)(void); /* this is called to tally an error */
523 done_f done; /* this is called to say if the operation has
524 * succeeded/failed */
525 } *cont;
527 static void floppy_ready(void);
528 static void floppy_start(void);
529 static void process_fd_request(void);
530 static void recalibrate_floppy(void);
531 static void floppy_shutdown(void);
533 static int floppy_grab_irq_and_dma(void);
534 static void floppy_release_irq_and_dma(void);
537 * The "reset" variable should be tested whenever an interrupt is scheduled,
538 * after the commands have been sent. This is to ensure that the driver doesn't
539 * get wedged when the interrupt doesn't come because of a failed command.
540 * reset doesn't need to be tested before sending commands, because
541 * output_byte is automatically disabled when reset is set.
543 #define CHECK_RESET { if (FDCS->reset){ reset_fdc(); return; } }
544 static void reset_fdc(void);
547 * These are global variables, as that's the easiest way to give
548 * information to interrupts. They are the data used for the current
549 * request.
551 #define NO_TRACK -1
552 #define NEED_1_RECAL -2
553 #define NEED_2_RECAL -3
555 /* */
556 static int usage_count = 0;
559 /* buffer related variables */
560 static int buffer_track = -1;
561 static int buffer_drive = -1;
562 static int buffer_min = -1;
563 static int buffer_max = -1;
565 /* fdc related variables, should end up in a struct */
566 static struct floppy_fdc_state fdc_state[N_FDC];
567 static int fdc; /* current fdc */
569 static struct floppy_struct *_floppy = floppy_type;
570 static unsigned char current_drive = 0;
571 static long current_count_sectors = 0;
572 static unsigned char sector_t; /* sector in track */
573 static unsigned char in_sector_offset; /* offset within physical sector,
574 * expressed in units of 512 bytes */
576 #ifndef fd_eject
577 #define fd_eject(x) -EINVAL
578 #endif
580 #ifdef DEBUGT
581 static long unsigned debugtimer;
582 #endif
585 * Debugging
586 * =========
588 static inline void set_debugt(void)
590 #ifdef DEBUGT
591 debugtimer = jiffies;
592 #endif
595 static inline void debugt(const char *message)
597 #ifdef DEBUGT
598 if (DP->flags & DEBUGT)
599 printk("%s dtime=%lu\n", message, jiffies-debugtimer);
600 #endif
603 typedef void (*timeout_fn)(unsigned long);
604 static struct timer_list fd_timeout ={ function: (timeout_fn) floppy_shutdown };
606 static const char *timeout_message;
608 #ifdef FLOPPY_SANITY_CHECK
609 static void is_alive(const char *message)
611 /* this routine checks whether the floppy driver is "alive" */
612 if (fdc_busy && command_status < 2 && !timer_pending(&fd_timeout)){
613 DPRINT("timeout handler died: %s\n",message);
616 #endif
618 #ifdef FLOPPY_SANITY_CHECK
620 #define OLOGSIZE 20
622 static void (*lasthandler)(void) = NULL;
623 static unsigned long interruptjiffies;
624 static unsigned long resultjiffies;
625 static int resultsize;
626 static unsigned long lastredo;
628 static struct output_log {
629 unsigned char data;
630 unsigned char status;
631 unsigned long jiffies;
632 } output_log[OLOGSIZE];
634 static int output_log_pos;
635 #endif
637 #define CURRENTD -1
638 #define MAXTIMEOUT -2
640 static void reschedule_timeout(int drive, const char *message, int marg)
642 if (drive == CURRENTD)
643 drive = current_drive;
644 del_timer(&fd_timeout);
645 if (drive < 0 || drive > N_DRIVE) {
646 fd_timeout.expires = jiffies + 20UL*HZ;
647 drive=0;
648 } else
649 fd_timeout.expires = jiffies + UDP->timeout;
650 add_timer(&fd_timeout);
651 if (UDP->flags & FD_DEBUG){
652 DPRINT("reschedule timeout ");
653 printk(message, marg);
654 printk("\n");
656 timeout_message = message;
659 static int maximum(int a, int b)
661 if (a > b)
662 return a;
663 else
664 return b;
666 #define INFBOUND(a,b) (a)=maximum((a),(b));
668 static int minimum(int a, int b)
670 if (a < b)
671 return a;
672 else
673 return b;
675 #define SUPBOUND(a,b) (a)=minimum((a),(b));
679 * Bottom half floppy driver.
680 * ==========================
682 * This part of the file contains the code talking directly to the hardware,
683 * and also the main service loop (seek-configure-spinup-command)
687 * disk change.
688 * This routine is responsible for maintaining the FD_DISK_CHANGE flag,
689 * and the last_checked date.
691 * last_checked is the date of the last check which showed 'no disk change'
692 * FD_DISK_CHANGE is set under two conditions:
693 * 1. The floppy has been changed after some i/o to that floppy already
694 * took place.
695 * 2. No floppy disk is in the drive. This is done in order to ensure that
696 * requests are quickly flushed in case there is no disk in the drive. It
697 * follows that FD_DISK_CHANGE can only be cleared if there is a disk in
698 * the drive.
700 * For 1., maxblock is observed. Maxblock is 0 if no i/o has taken place yet.
701 * For 2., FD_DISK_NEWCHANGE is watched. FD_DISK_NEWCHANGE is cleared on
702 * each seek. If a disk is present, the disk change line should also be
703 * cleared on each seek. Thus, if FD_DISK_NEWCHANGE is clear, but the disk
704 * change line is set, this means either that no disk is in the drive, or
705 * that it has been removed since the last seek.
707 * This means that we really have a third possibility too:
708 * The floppy has been changed after the last seek.
711 static int disk_change(int drive)
713 int fdc=FDC(drive);
714 #ifdef FLOPPY_SANITY_CHECK
715 if (jiffies - UDRS->select_date < UDP->select_delay)
716 DPRINT("WARNING disk change called early\n");
717 if (!(FDCS->dor & (0x10 << UNIT(drive))) ||
718 (FDCS->dor & 3) != UNIT(drive) ||
719 fdc != FDC(drive)){
720 DPRINT("probing disk change on unselected drive\n");
721 DPRINT("drive=%d fdc=%d dor=%x\n",drive, FDC(drive),
722 (unsigned int)FDCS->dor);
724 #endif
726 #ifdef DCL_DEBUG
727 if (UDP->flags & FD_DEBUG){
728 DPRINT("checking disk change line for drive %d\n",drive);
729 DPRINT("jiffies=%lu\n", jiffies);
730 DPRINT("disk change line=%x\n",fd_inb(FD_DIR)&0x80);
731 DPRINT("flags=%lx\n",UDRS->flags);
733 #endif
734 if (UDP->flags & FD_BROKEN_DCL)
735 return UTESTF(FD_DISK_CHANGED);
736 if ((fd_inb(FD_DIR) ^ UDP->flags) & 0x80){
737 USETF(FD_VERIFY); /* verify write protection */
738 if (UDRS->maxblock){
739 /* mark it changed */
740 USETF(FD_DISK_CHANGED);
743 /* invalidate its geometry */
744 if (UDRS->keep_data >= 0) {
745 if ((UDP->flags & FTD_MSG) &&
746 current_type[drive] != NULL)
747 DPRINT("Disk type is undefined after "
748 "disk change\n");
749 current_type[drive] = NULL;
750 floppy_sizes[TOMINOR(drive)] = MAX_DISK_SIZE;
753 /*USETF(FD_DISK_NEWCHANGE);*/
754 return 1;
755 } else {
756 UDRS->last_checked=jiffies;
757 UCLEARF(FD_DISK_NEWCHANGE);
759 return 0;
762 static inline int is_selected(int dor, int unit)
764 return ((dor & (0x10 << unit)) && (dor &3) == unit);
767 static int set_dor(int fdc, char mask, char data)
769 register unsigned char drive, unit, newdor,olddor;
771 if (FDCS->address == -1)
772 return -1;
774 olddor = FDCS->dor;
775 newdor = (olddor & mask) | data;
776 if (newdor != olddor){
777 unit = olddor & 0x3;
778 if (is_selected(olddor, unit) && !is_selected(newdor,unit)){
779 drive = REVDRIVE(fdc,unit);
780 #ifdef DCL_DEBUG
781 if (UDP->flags & FD_DEBUG){
782 DPRINT("calling disk change from set_dor\n");
784 #endif
785 disk_change(drive);
787 FDCS->dor = newdor;
788 fd_outb(newdor, FD_DOR);
790 unit = newdor & 0x3;
791 if (!is_selected(olddor, unit) && is_selected(newdor,unit)){
792 drive = REVDRIVE(fdc,unit);
793 UDRS->select_date = jiffies;
797 * We should propogate failures to grab the resources back
798 * nicely from here. Actually we ought to rewrite the fd
799 * driver some day too.
801 if (newdor & FLOPPY_MOTOR_MASK)
802 floppy_grab_irq_and_dma();
803 if (olddor & FLOPPY_MOTOR_MASK)
804 floppy_release_irq_and_dma();
805 return olddor;
808 static void twaddle(void)
810 if (DP->select_delay)
811 return;
812 fd_outb(FDCS->dor & ~(0x10<<UNIT(current_drive)), FD_DOR);
813 fd_outb(FDCS->dor, FD_DOR);
814 DRS->select_date = jiffies;
817 /* reset all driver information about the current fdc. This is needed after
818 * a reset, and after a raw command. */
819 static void reset_fdc_info(int mode)
821 int drive;
823 FDCS->spec1 = FDCS->spec2 = -1;
824 FDCS->need_configure = 1;
825 FDCS->perp_mode = 1;
826 FDCS->rawcmd = 0;
827 for (drive = 0; drive < N_DRIVE; drive++)
828 if (FDC(drive) == fdc &&
829 (mode || UDRS->track != NEED_1_RECAL))
830 UDRS->track = NEED_2_RECAL;
833 /* selects the fdc and drive, and enables the fdc's input/dma. */
834 static void set_fdc(int drive)
836 if (drive >= 0 && drive < N_DRIVE){
837 fdc = FDC(drive);
838 current_drive = drive;
840 if (fdc != 1 && fdc != 0) {
841 printk("bad fdc value\n");
842 return;
844 set_dor(fdc,~0,8);
845 #if N_FDC > 1
846 set_dor(1-fdc, ~8, 0);
847 #endif
848 if (FDCS->rawcmd == 2)
849 reset_fdc_info(1);
850 if (fd_inb(FD_STATUS) != STATUS_READY)
851 FDCS->reset = 1;
854 /* locks the driver */
855 static int _lock_fdc(int drive, int interruptible, int line)
857 if (!usage_count){
858 printk(KERN_ERR "Trying to lock fdc while usage count=0 at line %d\n", line);
859 return -1;
861 if(floppy_grab_irq_and_dma()==-1)
862 return -EBUSY;
864 if (test_and_set_bit(0, &fdc_busy)) {
865 DECLARE_WAITQUEUE(wait, current);
866 add_wait_queue(&fdc_wait, &wait);
868 for (;;) {
869 set_current_state(TASK_INTERRUPTIBLE);
871 if (!test_and_set_bit(0, &fdc_busy))
872 break;
874 schedule();
876 if (!NO_SIGNAL) {
877 remove_wait_queue(&fdc_wait, &wait);
878 return -EINTR;
882 set_current_state(TASK_RUNNING);
883 remove_wait_queue(&fdc_wait, &wait);
885 command_status = FD_COMMAND_NONE;
887 reschedule_timeout(drive, "lock fdc", 0);
888 set_fdc(drive);
889 return 0;
892 #define lock_fdc(drive,interruptible) _lock_fdc(drive,interruptible, __LINE__)
894 #define LOCK_FDC(drive,interruptible) \
895 if (lock_fdc(drive,interruptible)) return -EINTR;
898 /* unlocks the driver */
899 static inline void unlock_fdc(void)
901 raw_cmd = 0;
902 if (!fdc_busy)
903 DPRINT("FDC access conflict!\n");
905 if (DEVICE_INTR)
906 DPRINT("device interrupt still active at FDC release: %p!\n",
907 DEVICE_INTR);
908 command_status = FD_COMMAND_NONE;
909 del_timer(&fd_timeout);
910 cont = NULL;
911 clear_bit(0, &fdc_busy);
912 floppy_release_irq_and_dma();
913 wake_up(&fdc_wait);
916 /* switches the motor off after a given timeout */
917 static void motor_off_callback(unsigned long nr)
919 unsigned char mask = ~(0x10 << UNIT(nr));
921 set_dor(FDC(nr), mask, 0);
924 static struct timer_list motor_off_timer[N_DRIVE] = {
925 { data: 0, function: motor_off_callback },
926 { data: 1, function: motor_off_callback },
927 { data: 2, function: motor_off_callback },
928 { data: 3, function: motor_off_callback },
929 { data: 4, function: motor_off_callback },
930 { data: 5, function: motor_off_callback },
931 { data: 6, function: motor_off_callback },
932 { data: 7, function: motor_off_callback }
935 /* schedules motor off */
936 static void floppy_off(unsigned int drive)
938 unsigned long volatile delta;
939 register int fdc=FDC(drive);
941 if (!(FDCS->dor & (0x10 << UNIT(drive))))
942 return;
944 del_timer(motor_off_timer+drive);
946 /* make spindle stop in a position which minimizes spinup time
947 * next time */
948 if (UDP->rps){
949 delta = jiffies - UDRS->first_read_date + HZ -
950 UDP->spindown_offset;
951 delta = ((delta * UDP->rps) % HZ) / UDP->rps;
952 motor_off_timer[drive].expires = jiffies + UDP->spindown - delta;
954 add_timer(motor_off_timer+drive);
958 * cycle through all N_DRIVE floppy drives, for disk change testing.
959 * stopping at current drive. This is done before any long operation, to
960 * be sure to have up to date disk change information.
962 static void scandrives(void)
964 int i, drive, saved_drive;
966 if (DP->select_delay)
967 return;
969 saved_drive = current_drive;
970 for (i=0; i < N_DRIVE; i++){
971 drive = (saved_drive + i + 1) % N_DRIVE;
972 if (UDRS->fd_ref == 0 || UDP->select_delay != 0)
973 continue; /* skip closed drives */
974 set_fdc(drive);
975 if (!(set_dor(fdc, ~3, UNIT(drive) | (0x10 << UNIT(drive))) &
976 (0x10 << UNIT(drive))))
977 /* switch the motor off again, if it was off to
978 * begin with */
979 set_dor(fdc, ~(0x10 << UNIT(drive)), 0);
981 set_fdc(saved_drive);
984 static void empty(void)
988 static struct tq_struct floppy_tq =
989 { 0, 0, 0, 0 };
991 static void schedule_bh( void (*handler)(void*) )
993 floppy_tq.routine = (void *)(void *) handler;
994 queue_task(&floppy_tq, &tq_immediate);
995 mark_bh(IMMEDIATE_BH);
998 static struct timer_list fd_timer;
1000 static void cancel_activity(void)
1002 CLEAR_INTR;
1003 floppy_tq.routine = (void *)(void *) empty;
1004 del_timer(&fd_timer);
1007 /* this function makes sure that the disk stays in the drive during the
1008 * transfer */
1009 static void fd_watchdog(void)
1011 #ifdef DCL_DEBUG
1012 if (DP->flags & FD_DEBUG){
1013 DPRINT("calling disk change from watchdog\n");
1015 #endif
1017 if (disk_change(current_drive)){
1018 DPRINT("disk removed during i/o\n");
1019 cancel_activity();
1020 cont->done(0);
1021 reset_fdc();
1022 } else {
1023 del_timer(&fd_timer);
1024 fd_timer.function = (timeout_fn) fd_watchdog;
1025 fd_timer.expires = jiffies + HZ / 10;
1026 add_timer(&fd_timer);
1030 static void main_command_interrupt(void)
1032 del_timer(&fd_timer);
1033 cont->interrupt();
1036 /* waits for a delay (spinup or select) to pass */
1037 static int wait_for_completion(unsigned long delay, timeout_fn function)
1039 if (FDCS->reset){
1040 reset_fdc(); /* do the reset during sleep to win time
1041 * if we don't need to sleep, it's a good
1042 * occasion anyways */
1043 return 1;
1046 if ((signed) (jiffies - delay) < 0){
1047 del_timer(&fd_timer);
1048 fd_timer.function = function;
1049 fd_timer.expires = delay;
1050 add_timer(&fd_timer);
1051 return 1;
1053 return 0;
1056 static spinlock_t floppy_hlt_lock = SPIN_LOCK_UNLOCKED;
1057 static int hlt_disabled;
1058 static void floppy_disable_hlt(void)
1060 unsigned long flags;
1062 spin_lock_irqsave(&floppy_hlt_lock, flags);
1063 if (!hlt_disabled) {
1064 hlt_disabled=1;
1065 #ifdef HAVE_DISABLE_HLT
1066 disable_hlt();
1067 #endif
1069 spin_unlock_irqrestore(&floppy_hlt_lock, flags);
1072 static void floppy_enable_hlt(void)
1074 unsigned long flags;
1076 spin_lock_irqsave(&floppy_hlt_lock, flags);
1077 if (hlt_disabled){
1078 hlt_disabled=0;
1079 #ifdef HAVE_DISABLE_HLT
1080 enable_hlt();
1081 #endif
1083 spin_unlock_irqrestore(&floppy_hlt_lock, flags);
1087 static void setup_DMA(void)
1089 unsigned long f;
1091 #ifdef FLOPPY_SANITY_CHECK
1092 if (raw_cmd->length == 0){
1093 int i;
1095 printk("zero dma transfer size:");
1096 for (i=0; i < raw_cmd->cmd_count; i++)
1097 printk("%x,", raw_cmd->cmd[i]);
1098 printk("\n");
1099 cont->done(0);
1100 FDCS->reset = 1;
1101 return;
1103 if (((unsigned long) raw_cmd->kernel_data) % 512){
1104 printk("non aligned address: %p\n", raw_cmd->kernel_data);
1105 cont->done(0);
1106 FDCS->reset=1;
1107 return;
1109 #endif
1110 f=claim_dma_lock();
1111 fd_disable_dma();
1112 #ifdef fd_dma_setup
1113 if (fd_dma_setup(raw_cmd->kernel_data, raw_cmd->length,
1114 (raw_cmd->flags & FD_RAW_READ)?
1115 DMA_MODE_READ : DMA_MODE_WRITE,
1116 FDCS->address) < 0) {
1117 release_dma_lock(f);
1118 cont->done(0);
1119 FDCS->reset=1;
1120 return;
1122 release_dma_lock(f);
1123 #else
1124 fd_clear_dma_ff();
1125 fd_cacheflush(raw_cmd->kernel_data, raw_cmd->length);
1126 fd_set_dma_mode((raw_cmd->flags & FD_RAW_READ)?
1127 DMA_MODE_READ : DMA_MODE_WRITE);
1128 fd_set_dma_addr(raw_cmd->kernel_data);
1129 fd_set_dma_count(raw_cmd->length);
1130 virtual_dma_port = FDCS->address;
1131 fd_enable_dma();
1132 release_dma_lock(f);
1133 #endif
1134 floppy_disable_hlt();
1137 static void show_floppy(void);
1139 /* waits until the fdc becomes ready */
1140 static int wait_til_ready(void)
1142 int counter, status;
1143 if (FDCS->reset)
1144 return -1;
1145 for (counter = 0; counter < 10000; counter++) {
1146 status = fd_inb(FD_STATUS);
1147 if (status & STATUS_READY)
1148 return status;
1150 if (!initialising) {
1151 DPRINT("Getstatus times out (%x) on fdc %d\n",
1152 status, fdc);
1153 show_floppy();
1155 FDCS->reset = 1;
1156 return -1;
1159 /* sends a command byte to the fdc */
1160 static int output_byte(char byte)
1162 int status;
1164 if ((status = wait_til_ready()) < 0)
1165 return -1;
1166 if ((status & (STATUS_READY|STATUS_DIR|STATUS_DMA)) == STATUS_READY){
1167 fd_outb(byte,FD_DATA);
1168 #ifdef FLOPPY_SANITY_CHECK
1169 output_log[output_log_pos].data = byte;
1170 output_log[output_log_pos].status = status;
1171 output_log[output_log_pos].jiffies = jiffies;
1172 output_log_pos = (output_log_pos + 1) % OLOGSIZE;
1173 #endif
1174 return 0;
1176 FDCS->reset = 1;
1177 if (!initialising) {
1178 DPRINT("Unable to send byte %x to FDC. Fdc=%x Status=%x\n",
1179 byte, fdc, status);
1180 show_floppy();
1182 return -1;
1184 #define LAST_OUT(x) if (output_byte(x)<0){ reset_fdc();return;}
1186 /* gets the response from the fdc */
1187 static int result(void)
1189 int i, status=0;
1191 for(i=0; i < MAX_REPLIES; i++) {
1192 if ((status = wait_til_ready()) < 0)
1193 break;
1194 status &= STATUS_DIR|STATUS_READY|STATUS_BUSY|STATUS_DMA;
1195 if ((status & ~STATUS_BUSY) == STATUS_READY){
1196 #ifdef FLOPPY_SANITY_CHECK
1197 resultjiffies = jiffies;
1198 resultsize = i;
1199 #endif
1200 return i;
1202 if (status == (STATUS_DIR|STATUS_READY|STATUS_BUSY))
1203 reply_buffer[i] = fd_inb(FD_DATA);
1204 else
1205 break;
1207 if (!initialising) {
1208 DPRINT("get result error. Fdc=%d Last status=%x Read bytes=%d\n",
1209 fdc, status, i);
1210 show_floppy();
1212 FDCS->reset = 1;
1213 return -1;
1216 #define MORE_OUTPUT -2
1217 /* does the fdc need more output? */
1218 static int need_more_output(void)
1220 int status;
1221 if ((status = wait_til_ready()) < 0)
1222 return -1;
1223 if ((status & (STATUS_READY|STATUS_DIR|STATUS_DMA)) == STATUS_READY)
1224 return MORE_OUTPUT;
1225 return result();
1228 /* Set perpendicular mode as required, based on data rate, if supported.
1229 * 82077 Now tested. 1Mbps data rate only possible with 82077-1.
1231 static inline void perpendicular_mode(void)
1233 unsigned char perp_mode;
1235 if (raw_cmd->rate & 0x40){
1236 switch(raw_cmd->rate & 3){
1237 case 0:
1238 perp_mode=2;
1239 break;
1240 case 3:
1241 perp_mode=3;
1242 break;
1243 default:
1244 DPRINT("Invalid data rate for perpendicular mode!\n");
1245 cont->done(0);
1246 FDCS->reset = 1; /* convenient way to return to
1247 * redo without to much hassle (deep
1248 * stack et al. */
1249 return;
1251 } else
1252 perp_mode = 0;
1254 if (FDCS->perp_mode == perp_mode)
1255 return;
1256 if (FDCS->version >= FDC_82077_ORIG) {
1257 output_byte(FD_PERPENDICULAR);
1258 output_byte(perp_mode);
1259 FDCS->perp_mode = perp_mode;
1260 } else if (perp_mode) {
1261 DPRINT("perpendicular mode not supported by this FDC.\n");
1263 } /* perpendicular_mode */
1265 static int fifo_depth = 0xa;
1266 static int no_fifo = 0;
1268 static int fdc_configure(void)
1270 /* Turn on FIFO */
1271 output_byte(FD_CONFIGURE);
1272 if (need_more_output() != MORE_OUTPUT)
1273 return 0;
1274 output_byte(0);
1275 output_byte(0x10 | (no_fifo & 0x20) | (fifo_depth & 0xf));
1276 output_byte(0); /* pre-compensation from track
1277 0 upwards */
1278 return 1;
1281 #define NOMINAL_DTR 500
1283 /* Issue a "SPECIFY" command to set the step rate time, head unload time,
1284 * head load time, and DMA disable flag to values needed by floppy.
1286 * The value "dtr" is the data transfer rate in Kbps. It is needed
1287 * to account for the data rate-based scaling done by the 82072 and 82077
1288 * FDC types. This parameter is ignored for other types of FDCs (i.e.
1289 * 8272a).
1291 * Note that changing the data transfer rate has a (probably deleterious)
1292 * effect on the parameters subject to scaling for 82072/82077 FDCs, so
1293 * fdc_specify is called again after each data transfer rate
1294 * change.
1296 * srt: 1000 to 16000 in microseconds
1297 * hut: 16 to 240 milliseconds
1298 * hlt: 2 to 254 milliseconds
1300 * These values are rounded up to the next highest available delay time.
1302 static void fdc_specify(void)
1304 unsigned char spec1, spec2;
1305 unsigned long srt, hlt, hut;
1306 unsigned long dtr = NOMINAL_DTR;
1307 unsigned long scale_dtr = NOMINAL_DTR;
1308 int hlt_max_code = 0x7f;
1309 int hut_max_code = 0xf;
1311 if (FDCS->need_configure && FDCS->version >= FDC_82072A) {
1312 fdc_configure();
1313 FDCS->need_configure = 0;
1314 /*DPRINT("FIFO enabled\n");*/
1317 switch (raw_cmd->rate & 0x03) {
1318 case 3:
1319 dtr = 1000;
1320 break;
1321 case 1:
1322 dtr = 300;
1323 if (FDCS->version >= FDC_82078) {
1324 /* chose the default rate table, not the one
1325 * where 1 = 2 Mbps */
1326 output_byte(FD_DRIVESPEC);
1327 if (need_more_output() == MORE_OUTPUT) {
1328 output_byte(UNIT(current_drive));
1329 output_byte(0xc0);
1332 break;
1333 case 2:
1334 dtr = 250;
1335 break;
1338 if (FDCS->version >= FDC_82072) {
1339 scale_dtr = dtr;
1340 hlt_max_code = 0x00; /* 0==256msec*dtr0/dtr (not linear!) */
1341 hut_max_code = 0x0; /* 0==256msec*dtr0/dtr (not linear!) */
1344 /* Convert step rate from microseconds to milliseconds and 4 bits */
1345 srt = 16 - (DP->srt*scale_dtr/1000 + NOMINAL_DTR - 1)/NOMINAL_DTR;
1346 if( slow_floppy ) {
1347 srt = srt / 4;
1349 SUPBOUND(srt, 0xf);
1350 INFBOUND(srt, 0);
1352 hlt = (DP->hlt*scale_dtr/2 + NOMINAL_DTR - 1)/NOMINAL_DTR;
1353 if (hlt < 0x01)
1354 hlt = 0x01;
1355 else if (hlt > 0x7f)
1356 hlt = hlt_max_code;
1358 hut = (DP->hut*scale_dtr/16 + NOMINAL_DTR - 1)/NOMINAL_DTR;
1359 if (hut < 0x1)
1360 hut = 0x1;
1361 else if (hut > 0xf)
1362 hut = hut_max_code;
1364 spec1 = (srt << 4) | hut;
1365 spec2 = (hlt << 1) | (use_virtual_dma & 1);
1367 /* If these parameters did not change, just return with success */
1368 if (FDCS->spec1 != spec1 || FDCS->spec2 != spec2) {
1369 /* Go ahead and set spec1 and spec2 */
1370 output_byte(FD_SPECIFY);
1371 output_byte(FDCS->spec1 = spec1);
1372 output_byte(FDCS->spec2 = spec2);
1374 } /* fdc_specify */
1376 /* Set the FDC's data transfer rate on behalf of the specified drive.
1377 * NOTE: with 82072/82077 FDCs, changing the data rate requires a reissue
1378 * of the specify command (i.e. using the fdc_specify function).
1380 static int fdc_dtr(void)
1382 /* If data rate not already set to desired value, set it. */
1383 if ((raw_cmd->rate & 3) == FDCS->dtr)
1384 return 0;
1386 /* Set dtr */
1387 fd_outb(raw_cmd->rate & 3, FD_DCR);
1389 /* TODO: some FDC/drive combinations (C&T 82C711 with TEAC 1.2MB)
1390 * need a stabilization period of several milliseconds to be
1391 * enforced after data rate changes before R/W operations.
1392 * Pause 5 msec to avoid trouble. (Needs to be 2 jiffies)
1394 FDCS->dtr = raw_cmd->rate & 3;
1395 return(wait_for_completion(jiffies+2UL*HZ/100,
1396 (timeout_fn) floppy_ready));
1397 } /* fdc_dtr */
1399 static void tell_sector(void)
1401 printk(": track %d, head %d, sector %d, size %d",
1402 R_TRACK, R_HEAD, R_SECTOR, R_SIZECODE);
1403 } /* tell_sector */
1407 * OK, this error interpreting routine is called after a
1408 * DMA read/write has succeeded
1409 * or failed, so we check the results, and copy any buffers.
1410 * hhb: Added better error reporting.
1411 * ak: Made this into a separate routine.
1413 static int interpret_errors(void)
1415 char bad;
1417 if (inr!=7) {
1418 DPRINT("-- FDC reply error");
1419 FDCS->reset = 1;
1420 return 1;
1423 /* check IC to find cause of interrupt */
1424 switch (ST0 & ST0_INTR) {
1425 case 0x40: /* error occurred during command execution */
1426 if (ST1 & ST1_EOC)
1427 return 0; /* occurs with pseudo-DMA */
1428 bad = 1;
1429 if (ST1 & ST1_WP) {
1430 DPRINT("Drive is write protected\n");
1431 CLEARF(FD_DISK_WRITABLE);
1432 cont->done(0);
1433 bad = 2;
1434 } else if (ST1 & ST1_ND) {
1435 SETF(FD_NEED_TWADDLE);
1436 } else if (ST1 & ST1_OR) {
1437 if (DP->flags & FTD_MSG)
1438 DPRINT("Over/Underrun - retrying\n");
1439 bad = 0;
1440 }else if (*errors >= DP->max_errors.reporting){
1441 DPRINT("");
1442 if (ST0 & ST0_ECE) {
1443 printk("Recalibrate failed!");
1444 } else if (ST2 & ST2_CRC) {
1445 printk("data CRC error");
1446 tell_sector();
1447 } else if (ST1 & ST1_CRC) {
1448 printk("CRC error");
1449 tell_sector();
1450 } else if ((ST1 & (ST1_MAM|ST1_ND)) || (ST2 & ST2_MAM)) {
1451 if (!probing) {
1452 printk("sector not found");
1453 tell_sector();
1454 } else
1455 printk("probe failed...");
1456 } else if (ST2 & ST2_WC) { /* seek error */
1457 printk("wrong cylinder");
1458 } else if (ST2 & ST2_BC) { /* cylinder marked as bad */
1459 printk("bad cylinder");
1460 } else {
1461 printk("unknown error. ST[0..2] are: 0x%x 0x%x 0x%x", ST0, ST1, ST2);
1462 tell_sector();
1464 printk("\n");
1467 if (ST2 & ST2_WC || ST2 & ST2_BC)
1468 /* wrong cylinder => recal */
1469 DRS->track = NEED_2_RECAL;
1470 return bad;
1471 case 0x80: /* invalid command given */
1472 DPRINT("Invalid FDC command given!\n");
1473 cont->done(0);
1474 return 2;
1475 case 0xc0:
1476 DPRINT("Abnormal termination caused by polling\n");
1477 cont->error();
1478 return 2;
1479 default: /* (0) Normal command termination */
1480 return 0;
1485 * This routine is called when everything should be correctly set up
1486 * for the transfer (i.e. floppy motor is on, the correct floppy is
1487 * selected, and the head is sitting on the right track).
1489 static void setup_rw_floppy(void)
1491 int i,r, flags,dflags;
1492 unsigned long ready_date;
1493 timeout_fn function;
1495 flags = raw_cmd->flags;
1496 if (flags & (FD_RAW_READ | FD_RAW_WRITE))
1497 flags |= FD_RAW_INTR;
1499 if ((flags & FD_RAW_SPIN) && !(flags & FD_RAW_NO_MOTOR)){
1500 ready_date = DRS->spinup_date + DP->spinup;
1501 /* If spinup will take a long time, rerun scandrives
1502 * again just before spinup completion. Beware that
1503 * after scandrives, we must again wait for selection.
1505 if ((signed) (ready_date - jiffies) > DP->select_delay){
1506 ready_date -= DP->select_delay;
1507 function = (timeout_fn) floppy_start;
1508 } else
1509 function = (timeout_fn) setup_rw_floppy;
1511 /* wait until the floppy is spinning fast enough */
1512 if (wait_for_completion(ready_date,function))
1513 return;
1515 dflags = DRS->flags;
1517 if ((flags & FD_RAW_READ) || (flags & FD_RAW_WRITE))
1518 setup_DMA();
1520 if (flags & FD_RAW_INTR)
1521 SET_INTR(main_command_interrupt);
1523 r=0;
1524 for (i=0; i< raw_cmd->cmd_count; i++)
1525 r|=output_byte(raw_cmd->cmd[i]);
1527 #ifdef DEBUGT
1528 debugt("rw_command: ");
1529 #endif
1530 if (r){
1531 cont->error();
1532 reset_fdc();
1533 return;
1536 if (!(flags & FD_RAW_INTR)){
1537 inr = result();
1538 cont->interrupt();
1539 } else if (flags & FD_RAW_NEED_DISK)
1540 fd_watchdog();
1543 static int blind_seek;
1546 * This is the routine called after every seek (or recalibrate) interrupt
1547 * from the floppy controller.
1549 static void seek_interrupt(void)
1551 #ifdef DEBUGT
1552 debugt("seek interrupt:");
1553 #endif
1554 if (inr != 2 || (ST0 & 0xF8) != 0x20) {
1555 DPRINT("seek failed\n");
1556 DRS->track = NEED_2_RECAL;
1557 cont->error();
1558 cont->redo();
1559 return;
1561 if (DRS->track >= 0 && DRS->track != ST1 && !blind_seek){
1562 #ifdef DCL_DEBUG
1563 if (DP->flags & FD_DEBUG){
1564 DPRINT("clearing NEWCHANGE flag because of effective seek\n");
1565 DPRINT("jiffies=%lu\n", jiffies);
1567 #endif
1568 CLEARF(FD_DISK_NEWCHANGE); /* effective seek */
1569 DRS->select_date = jiffies;
1571 DRS->track = ST1;
1572 floppy_ready();
1575 static void check_wp(void)
1577 if (TESTF(FD_VERIFY)) {
1578 /* check write protection */
1579 output_byte(FD_GETSTATUS);
1580 output_byte(UNIT(current_drive));
1581 if (result() != 1){
1582 FDCS->reset = 1;
1583 return;
1585 CLEARF(FD_VERIFY);
1586 CLEARF(FD_NEED_TWADDLE);
1587 #ifdef DCL_DEBUG
1588 if (DP->flags & FD_DEBUG){
1589 DPRINT("checking whether disk is write protected\n");
1590 DPRINT("wp=%x\n",ST3 & 0x40);
1592 #endif
1593 if (!(ST3 & 0x40))
1594 SETF(FD_DISK_WRITABLE);
1595 else
1596 CLEARF(FD_DISK_WRITABLE);
1600 static void seek_floppy(void)
1602 int track;
1604 blind_seek=0;
1606 #ifdef DCL_DEBUG
1607 if (DP->flags & FD_DEBUG){
1608 DPRINT("calling disk change from seek\n");
1610 #endif
1612 if (!TESTF(FD_DISK_NEWCHANGE) &&
1613 disk_change(current_drive) &&
1614 (raw_cmd->flags & FD_RAW_NEED_DISK)){
1615 /* the media changed flag should be cleared after the seek.
1616 * If it isn't, this means that there is really no disk in
1617 * the drive.
1619 SETF(FD_DISK_CHANGED);
1620 cont->done(0);
1621 cont->redo();
1622 return;
1624 if (DRS->track <= NEED_1_RECAL){
1625 recalibrate_floppy();
1626 return;
1627 } else if (TESTF(FD_DISK_NEWCHANGE) &&
1628 (raw_cmd->flags & FD_RAW_NEED_DISK) &&
1629 (DRS->track <= NO_TRACK || DRS->track == raw_cmd->track)) {
1630 /* we seek to clear the media-changed condition. Does anybody
1631 * know a more elegant way, which works on all drives? */
1632 if (raw_cmd->track)
1633 track = raw_cmd->track - 1;
1634 else {
1635 if (DP->flags & FD_SILENT_DCL_CLEAR){
1636 set_dor(fdc, ~(0x10 << UNIT(current_drive)), 0);
1637 blind_seek = 1;
1638 raw_cmd->flags |= FD_RAW_NEED_SEEK;
1640 track = 1;
1642 } else {
1643 check_wp();
1644 if (raw_cmd->track != DRS->track &&
1645 (raw_cmd->flags & FD_RAW_NEED_SEEK))
1646 track = raw_cmd->track;
1647 else {
1648 setup_rw_floppy();
1649 return;
1653 SET_INTR(seek_interrupt);
1654 output_byte(FD_SEEK);
1655 output_byte(UNIT(current_drive));
1656 LAST_OUT(track);
1657 #ifdef DEBUGT
1658 debugt("seek command:");
1659 #endif
1662 static void recal_interrupt(void)
1664 #ifdef DEBUGT
1665 debugt("recal interrupt:");
1666 #endif
1667 if (inr !=2)
1668 FDCS->reset = 1;
1669 else if (ST0 & ST0_ECE) {
1670 switch(DRS->track){
1671 case NEED_1_RECAL:
1672 #ifdef DEBUGT
1673 debugt("recal interrupt need 1 recal:");
1674 #endif
1675 /* after a second recalibrate, we still haven't
1676 * reached track 0. Probably no drive. Raise an
1677 * error, as failing immediately might upset
1678 * computers possessed by the Devil :-) */
1679 cont->error();
1680 cont->redo();
1681 return;
1682 case NEED_2_RECAL:
1683 #ifdef DEBUGT
1684 debugt("recal interrupt need 2 recal:");
1685 #endif
1686 /* If we already did a recalibrate,
1687 * and we are not at track 0, this
1688 * means we have moved. (The only way
1689 * not to move at recalibration is to
1690 * be already at track 0.) Clear the
1691 * new change flag */
1692 #ifdef DCL_DEBUG
1693 if (DP->flags & FD_DEBUG){
1694 DPRINT("clearing NEWCHANGE flag because of second recalibrate\n");
1696 #endif
1698 CLEARF(FD_DISK_NEWCHANGE);
1699 DRS->select_date = jiffies;
1700 /* fall through */
1701 default:
1702 #ifdef DEBUGT
1703 debugt("recal interrupt default:");
1704 #endif
1705 /* Recalibrate moves the head by at
1706 * most 80 steps. If after one
1707 * recalibrate we don't have reached
1708 * track 0, this might mean that we
1709 * started beyond track 80. Try
1710 * again. */
1711 DRS->track = NEED_1_RECAL;
1712 break;
1714 } else
1715 DRS->track = ST1;
1716 floppy_ready();
1719 static void print_result(char *message, int inr)
1721 int i;
1723 DPRINT("%s ", message);
1724 if (inr >= 0)
1725 for (i=0; i<inr; i++)
1726 printk("repl[%d]=%x ", i, reply_buffer[i]);
1727 printk("\n");
1730 /* interrupt handler. Note that this can be called externally on the Sparc */
1731 void floppy_interrupt(int irq, void *dev_id, struct pt_regs * regs)
1733 void (*handler)(void) = DEVICE_INTR;
1734 int do_print;
1735 unsigned long f;
1737 lasthandler = handler;
1738 interruptjiffies = jiffies;
1740 f=claim_dma_lock();
1741 fd_disable_dma();
1742 release_dma_lock(f);
1744 floppy_enable_hlt();
1745 CLEAR_INTR;
1746 if (fdc >= N_FDC || FDCS->address == -1){
1747 /* we don't even know which FDC is the culprit */
1748 printk("DOR0=%x\n", fdc_state[0].dor);
1749 printk("floppy interrupt on bizarre fdc %d\n",fdc);
1750 printk("handler=%p\n", handler);
1751 is_alive("bizarre fdc");
1752 return;
1755 FDCS->reset = 0;
1756 /* We have to clear the reset flag here, because apparently on boxes
1757 * with level triggered interrupts (PS/2, Sparc, ...), it is needed to
1758 * emit SENSEI's to clear the interrupt line. And FDCS->reset blocks the
1759 * emission of the SENSEI's.
1760 * It is OK to emit floppy commands because we are in an interrupt
1761 * handler here, and thus we have to fear no interference of other
1762 * activity.
1765 do_print = !handler && print_unex && !initialising;
1767 inr = result();
1768 if (do_print)
1769 print_result("unexpected interrupt", inr);
1770 if (inr == 0){
1771 int max_sensei = 4;
1772 do {
1773 output_byte(FD_SENSEI);
1774 inr = result();
1775 if (do_print)
1776 print_result("sensei", inr);
1777 max_sensei--;
1778 } while ((ST0 & 0x83) != UNIT(current_drive) && inr == 2 && max_sensei);
1780 if (handler) {
1781 schedule_bh( (void *)(void *) handler);
1782 } else
1783 FDCS->reset = 1;
1784 is_alive("normal interrupt end");
1787 static void recalibrate_floppy(void)
1789 #ifdef DEBUGT
1790 debugt("recalibrate floppy:");
1791 #endif
1792 SET_INTR(recal_interrupt);
1793 output_byte(FD_RECALIBRATE);
1794 LAST_OUT(UNIT(current_drive));
1798 * Must do 4 FD_SENSEIs after reset because of ``drive polling''.
1800 static void reset_interrupt(void)
1802 #ifdef DEBUGT
1803 debugt("reset interrupt:");
1804 #endif
1805 result(); /* get the status ready for set_fdc */
1806 if (FDCS->reset) {
1807 printk("reset set in interrupt, calling %p\n", cont->error);
1808 cont->error(); /* a reset just after a reset. BAD! */
1810 cont->redo();
1814 * reset is done by pulling bit 2 of DOR low for a while (old FDCs),
1815 * or by setting the self clearing bit 7 of STATUS (newer FDCs)
1817 static void reset_fdc(void)
1819 unsigned long flags;
1821 SET_INTR(reset_interrupt);
1822 FDCS->reset = 0;
1823 reset_fdc_info(0);
1825 /* Pseudo-DMA may intercept 'reset finished' interrupt. */
1826 /* Irrelevant for systems with true DMA (i386). */
1828 flags=claim_dma_lock();
1829 fd_disable_dma();
1830 release_dma_lock(flags);
1832 if (FDCS->version >= FDC_82072A)
1833 fd_outb(0x80 | (FDCS->dtr &3), FD_STATUS);
1834 else {
1835 fd_outb(FDCS->dor & ~0x04, FD_DOR);
1836 udelay(FD_RESET_DELAY);
1837 fd_outb(FDCS->dor, FD_DOR);
1841 static void show_floppy(void)
1843 int i;
1845 printk("\n");
1846 printk("floppy driver state\n");
1847 printk("-------------------\n");
1848 printk("now=%lu last interrupt=%lu diff=%lu last called handler=%p\n",
1849 jiffies, interruptjiffies, jiffies-interruptjiffies, lasthandler);
1852 #ifdef FLOPPY_SANITY_CHECK
1853 printk("timeout_message=%s\n", timeout_message);
1854 printk("last output bytes:\n");
1855 for (i=0; i < OLOGSIZE; i++)
1856 printk("%2x %2x %lu\n",
1857 output_log[(i+output_log_pos) % OLOGSIZE].data,
1858 output_log[(i+output_log_pos) % OLOGSIZE].status,
1859 output_log[(i+output_log_pos) % OLOGSIZE].jiffies);
1860 printk("last result at %lu\n", resultjiffies);
1861 printk("last redo_fd_request at %lu\n", lastredo);
1862 for (i=0; i<resultsize; i++){
1863 printk("%2x ", reply_buffer[i]);
1865 printk("\n");
1866 #endif
1868 printk("status=%x\n", fd_inb(FD_STATUS));
1869 printk("fdc_busy=%lu\n", fdc_busy);
1870 if (DEVICE_INTR)
1871 printk("DEVICE_INTR=%p\n", DEVICE_INTR);
1872 if (floppy_tq.sync)
1873 printk("floppy_tq.routine=%p\n", floppy_tq.routine);
1874 if (timer_pending(&fd_timer))
1875 printk("fd_timer.function=%p\n", fd_timer.function);
1876 if (timer_pending(&fd_timeout)){
1877 printk("timer_function=%p\n",fd_timeout.function);
1878 printk("expires=%lu\n",fd_timeout.expires-jiffies);
1879 printk("now=%lu\n",jiffies);
1881 printk("cont=%p\n", cont);
1882 printk("CURRENT=%p\n", CURRENT);
1883 printk("command_status=%d\n", command_status);
1884 printk("\n");
1887 static void floppy_shutdown(void)
1889 unsigned long flags;
1891 if (!initialising)
1892 show_floppy();
1893 cancel_activity();
1895 floppy_enable_hlt();
1897 flags=claim_dma_lock();
1898 fd_disable_dma();
1899 release_dma_lock(flags);
1901 /* avoid dma going to a random drive after shutdown */
1903 if (!initialising)
1904 DPRINT("floppy timeout called\n");
1905 FDCS->reset = 1;
1906 if (cont){
1907 cont->done(0);
1908 cont->redo(); /* this will recall reset when needed */
1909 } else {
1910 printk("no cont in shutdown!\n");
1911 process_fd_request();
1913 is_alive("floppy shutdown");
1915 /*typedef void (*timeout_fn)(unsigned long);*/
1917 /* start motor, check media-changed condition and write protection */
1918 static int start_motor(void (*function)(void) )
1920 int mask, data;
1922 mask = 0xfc;
1923 data = UNIT(current_drive);
1924 if (!(raw_cmd->flags & FD_RAW_NO_MOTOR)){
1925 if (!(FDCS->dor & (0x10 << UNIT(current_drive)))){
1926 set_debugt();
1927 /* no read since this drive is running */
1928 DRS->first_read_date = 0;
1929 /* note motor start time if motor is not yet running */
1930 DRS->spinup_date = jiffies;
1931 data |= (0x10 << UNIT(current_drive));
1933 } else
1934 if (FDCS->dor & (0x10 << UNIT(current_drive)))
1935 mask &= ~(0x10 << UNIT(current_drive));
1937 /* starts motor and selects floppy */
1938 del_timer(motor_off_timer + current_drive);
1939 set_dor(fdc, mask, data);
1941 /* wait_for_completion also schedules reset if needed. */
1942 return(wait_for_completion(DRS->select_date+DP->select_delay,
1943 (timeout_fn) function));
1946 static void floppy_ready(void)
1948 CHECK_RESET;
1949 if (start_motor(floppy_ready)) return;
1950 if (fdc_dtr()) return;
1952 #ifdef DCL_DEBUG
1953 if (DP->flags & FD_DEBUG){
1954 DPRINT("calling disk change from floppy_ready\n");
1956 #endif
1957 if (!(raw_cmd->flags & FD_RAW_NO_MOTOR) &&
1958 disk_change(current_drive) &&
1959 !DP->select_delay)
1960 twaddle(); /* this clears the dcl on certain drive/controller
1961 * combinations */
1963 #ifdef fd_chose_dma_mode
1964 if ((raw_cmd->flags & FD_RAW_READ) ||
1965 (raw_cmd->flags & FD_RAW_WRITE))
1967 unsigned long flags = claim_dma_lock();
1968 fd_chose_dma_mode(raw_cmd->kernel_data,
1969 raw_cmd->length);
1970 release_dma_lock(flags);
1972 #endif
1974 if (raw_cmd->flags & (FD_RAW_NEED_SEEK | FD_RAW_NEED_DISK)){
1975 perpendicular_mode();
1976 fdc_specify(); /* must be done here because of hut, hlt ... */
1977 seek_floppy();
1978 } else {
1979 if ((raw_cmd->flags & FD_RAW_READ) ||
1980 (raw_cmd->flags & FD_RAW_WRITE))
1981 fdc_specify();
1982 setup_rw_floppy();
1986 static void floppy_start(void)
1988 reschedule_timeout(CURRENTD, "floppy start", 0);
1990 scandrives();
1991 #ifdef DCL_DEBUG
1992 if (DP->flags & FD_DEBUG){
1993 DPRINT("setting NEWCHANGE in floppy_start\n");
1995 #endif
1996 SETF(FD_DISK_NEWCHANGE);
1997 floppy_ready();
2001 * ========================================================================
2002 * here ends the bottom half. Exported routines are:
2003 * floppy_start, floppy_off, floppy_ready, lock_fdc, unlock_fdc, set_fdc,
2004 * start_motor, reset_fdc, reset_fdc_info, interpret_errors.
2005 * Initialization also uses output_byte, result, set_dor, floppy_interrupt
2006 * and set_dor.
2007 * ========================================================================
2010 * General purpose continuations.
2011 * ==============================
2014 static void do_wakeup(void)
2016 reschedule_timeout(MAXTIMEOUT, "do wakeup", 0);
2017 cont = 0;
2018 command_status += 2;
2019 wake_up(&command_done);
2022 static struct cont_t wakeup_cont={
2023 empty,
2024 do_wakeup,
2025 empty,
2026 (done_f)empty
2030 static struct cont_t intr_cont={
2031 empty,
2032 process_fd_request,
2033 empty,
2034 (done_f) empty
2037 static int wait_til_done(void (*handler)(void), int interruptible)
2039 int ret;
2041 schedule_bh((void *)(void *)handler);
2043 if (command_status < 2 && NO_SIGNAL) {
2044 DECLARE_WAITQUEUE(wait, current);
2046 add_wait_queue(&command_done, &wait);
2047 for (;;) {
2048 set_current_state(interruptible?
2049 TASK_INTERRUPTIBLE:
2050 TASK_UNINTERRUPTIBLE);
2052 if (command_status >= 2 || !NO_SIGNAL)
2053 break;
2055 is_alive("wait_til_done");
2057 schedule();
2060 set_current_state(TASK_RUNNING);
2061 remove_wait_queue(&command_done, &wait);
2064 if (command_status < 2){
2065 cancel_activity();
2066 cont = &intr_cont;
2067 reset_fdc();
2068 return -EINTR;
2071 if (FDCS->reset)
2072 command_status = FD_COMMAND_ERROR;
2073 if (command_status == FD_COMMAND_OKAY)
2074 ret=0;
2075 else
2076 ret=-EIO;
2077 command_status = FD_COMMAND_NONE;
2078 return ret;
2081 static void generic_done(int result)
2083 command_status = result;
2084 cont = &wakeup_cont;
2087 static void generic_success(void)
2089 cont->done(1);
2092 static void generic_failure(void)
2094 cont->done(0);
2097 static void success_and_wakeup(void)
2099 generic_success();
2100 cont->redo();
2105 * formatting and rw support.
2106 * ==========================
2109 static int next_valid_format(void)
2111 int probed_format;
2113 probed_format = DRS->probed_format;
2114 while(1){
2115 if (probed_format >= 8 ||
2116 !DP->autodetect[probed_format]){
2117 DRS->probed_format = 0;
2118 return 1;
2120 if (floppy_type[DP->autodetect[probed_format]].sect){
2121 DRS->probed_format = probed_format;
2122 return 0;
2124 probed_format++;
2128 static void bad_flp_intr(void)
2130 if (probing){
2131 DRS->probed_format++;
2132 if (!next_valid_format())
2133 return;
2135 (*errors)++;
2136 INFBOUND(DRWE->badness, *errors);
2137 if (*errors > DP->max_errors.abort)
2138 cont->done(0);
2139 if (*errors > DP->max_errors.reset)
2140 FDCS->reset = 1;
2141 else if (*errors > DP->max_errors.recal)
2142 DRS->track = NEED_2_RECAL;
2145 static void set_floppy(kdev_t device)
2147 if (TYPE(device))
2148 _floppy = TYPE(device) + floppy_type;
2149 else
2150 _floppy = current_type[ DRIVE(device) ];
2154 * formatting support.
2155 * ===================
2157 static void format_interrupt(void)
2159 switch (interpret_errors()){
2160 case 1:
2161 cont->error();
2162 case 2:
2163 break;
2164 case 0:
2165 cont->done(1);
2167 cont->redo();
2170 #define CODE2SIZE (ssize = ((1 << SIZECODE) + 3) >> 2)
2171 #define FM_MODE(x,y) ((y) & ~(((x)->rate & 0x80) >>1))
2172 #define CT(x) ((x) | 0xc0)
2173 static void setup_format_params(int track)
2175 struct fparm {
2176 unsigned char track,head,sect,size;
2177 } *here = (struct fparm *)floppy_track_buffer;
2178 int il,n;
2179 int count,head_shift,track_shift;
2181 raw_cmd = &default_raw_cmd;
2182 raw_cmd->track = track;
2184 raw_cmd->flags = FD_RAW_WRITE | FD_RAW_INTR | FD_RAW_SPIN |
2185 FD_RAW_NEED_DISK | FD_RAW_NEED_SEEK;
2186 raw_cmd->rate = _floppy->rate & 0x43;
2187 raw_cmd->cmd_count = NR_F;
2188 COMMAND = FM_MODE(_floppy,FD_FORMAT);
2189 DR_SELECT = UNIT(current_drive) + PH_HEAD(_floppy,format_req.head);
2190 F_SIZECODE = FD_SIZECODE(_floppy);
2191 F_SECT_PER_TRACK = _floppy->sect << 2 >> F_SIZECODE;
2192 F_GAP = _floppy->fmt_gap;
2193 F_FILL = FD_FILL_BYTE;
2195 raw_cmd->kernel_data = floppy_track_buffer;
2196 raw_cmd->length = 4 * F_SECT_PER_TRACK;
2198 /* allow for about 30ms for data transport per track */
2199 head_shift = (F_SECT_PER_TRACK + 5) / 6;
2201 /* a ``cylinder'' is two tracks plus a little stepping time */
2202 track_shift = 2 * head_shift + 3;
2204 /* position of logical sector 1 on this track */
2205 n = (track_shift * format_req.track + head_shift * format_req.head)
2206 % F_SECT_PER_TRACK;
2208 /* determine interleave */
2209 il = 1;
2210 if (_floppy->fmt_gap < 0x22)
2211 il++;
2213 /* initialize field */
2214 for (count = 0; count < F_SECT_PER_TRACK; ++count) {
2215 here[count].track = format_req.track;
2216 here[count].head = format_req.head;
2217 here[count].sect = 0;
2218 here[count].size = F_SIZECODE;
2220 /* place logical sectors */
2221 for (count = 1; count <= F_SECT_PER_TRACK; ++count) {
2222 here[n].sect = count;
2223 n = (n+il) % F_SECT_PER_TRACK;
2224 if (here[n].sect) { /* sector busy, find next free sector */
2225 ++n;
2226 if (n>= F_SECT_PER_TRACK) {
2227 n-=F_SECT_PER_TRACK;
2228 while (here[n].sect) ++n;
2234 static void redo_format(void)
2236 buffer_track = -1;
2237 setup_format_params(format_req.track << STRETCH(_floppy));
2238 floppy_start();
2239 #ifdef DEBUGT
2240 debugt("queue format request");
2241 #endif
2244 static struct cont_t format_cont={
2245 format_interrupt,
2246 redo_format,
2247 bad_flp_intr,
2248 generic_done };
2250 static int do_format(kdev_t device, struct format_descr *tmp_format_req)
2252 int ret;
2253 int drive=DRIVE(device);
2255 LOCK_FDC(drive,1);
2256 set_floppy(device);
2257 if (!_floppy ||
2258 _floppy->track > DP->tracks ||
2259 tmp_format_req->track >= _floppy->track ||
2260 tmp_format_req->head >= _floppy->head ||
2261 (_floppy->sect << 2) % (1 << FD_SIZECODE(_floppy)) ||
2262 !_floppy->fmt_gap) {
2263 process_fd_request();
2264 return -EINVAL;
2266 format_req = *tmp_format_req;
2267 format_errors = 0;
2268 cont = &format_cont;
2269 errors = &format_errors;
2270 IWAIT(redo_format);
2271 process_fd_request();
2272 return ret;
2276 * Buffer read/write and support
2277 * =============================
2280 /* new request_done. Can handle physical sectors which are smaller than a
2281 * logical buffer */
2282 static void request_done(int uptodate)
2284 int block;
2286 probing = 0;
2287 reschedule_timeout(MAXTIMEOUT, "request done %d", uptodate);
2289 if (QUEUE_EMPTY){
2290 DPRINT("request list destroyed in floppy request done\n");
2291 return;
2294 if (uptodate){
2295 /* maintain values for invalidation on geometry
2296 * change */
2297 block = current_count_sectors + CURRENT->sector;
2298 INFBOUND(DRS->maxblock, block);
2299 if (block > _floppy->sect)
2300 DRS->maxtrack = 1;
2302 /* unlock chained buffers */
2303 while (current_count_sectors && !QUEUE_EMPTY &&
2304 current_count_sectors >= CURRENT->current_nr_sectors){
2305 current_count_sectors -= CURRENT->current_nr_sectors;
2306 CURRENT->nr_sectors -= CURRENT->current_nr_sectors;
2307 CURRENT->sector += CURRENT->current_nr_sectors;
2308 end_request(1);
2310 if (current_count_sectors && !QUEUE_EMPTY){
2311 /* "unlock" last subsector */
2312 CURRENT->buffer += current_count_sectors <<9;
2313 CURRENT->current_nr_sectors -= current_count_sectors;
2314 CURRENT->nr_sectors -= current_count_sectors;
2315 CURRENT->sector += current_count_sectors;
2316 return;
2319 if (current_count_sectors && QUEUE_EMPTY)
2320 DPRINT("request list destroyed in floppy request done\n");
2322 } else {
2323 if (CURRENT->cmd == WRITE) {
2324 /* record write error information */
2325 DRWE->write_errors++;
2326 if (DRWE->write_errors == 1) {
2327 DRWE->first_error_sector = CURRENT->sector;
2328 DRWE->first_error_generation = DRS->generation;
2330 DRWE->last_error_sector = CURRENT->sector;
2331 DRWE->last_error_generation = DRS->generation;
2333 end_request(0);
2337 /* Interrupt handler evaluating the result of the r/w operation */
2338 static void rw_interrupt(void)
2340 int nr_sectors, ssize, eoc, heads;
2342 if (R_HEAD >= 2) {
2343 /* some Toshiba floppy controllers occasionnally seem to
2344 * return bogus interrupts after read/write operations, which
2345 * can be recognized by a bad head number (>= 2) */
2346 return;
2349 if (!DRS->first_read_date)
2350 DRS->first_read_date = jiffies;
2352 nr_sectors = 0;
2353 CODE2SIZE;
2355 if (ST1 & ST1_EOC)
2356 eoc = 1;
2357 else
2358 eoc = 0;
2360 if (COMMAND & 0x80)
2361 heads = 2;
2362 else
2363 heads = 1;
2365 nr_sectors = (((R_TRACK-TRACK) * heads +
2366 R_HEAD-HEAD) * SECT_PER_TRACK +
2367 R_SECTOR-SECTOR + eoc) << SIZECODE >> 2;
2369 #ifdef FLOPPY_SANITY_CHECK
2370 if (nr_sectors / ssize >
2371 (in_sector_offset + current_count_sectors + ssize - 1) / ssize) {
2372 DPRINT("long rw: %x instead of %lx\n",
2373 nr_sectors, current_count_sectors);
2374 printk("rs=%d s=%d\n", R_SECTOR, SECTOR);
2375 printk("rh=%d h=%d\n", R_HEAD, HEAD);
2376 printk("rt=%d t=%d\n", R_TRACK, TRACK);
2377 printk("heads=%d eoc=%d\n", heads, eoc);
2378 printk("spt=%d st=%d ss=%d\n", SECT_PER_TRACK,
2379 sector_t, ssize);
2380 printk("in_sector_offset=%d\n", in_sector_offset);
2382 #endif
2384 nr_sectors -= in_sector_offset;
2385 INFBOUND(nr_sectors,0);
2386 SUPBOUND(current_count_sectors, nr_sectors);
2388 switch (interpret_errors()){
2389 case 2:
2390 cont->redo();
2391 return;
2392 case 1:
2393 if (!current_count_sectors){
2394 cont->error();
2395 cont->redo();
2396 return;
2398 break;
2399 case 0:
2400 if (!current_count_sectors){
2401 cont->redo();
2402 return;
2404 current_type[current_drive] = _floppy;
2405 floppy_sizes[TOMINOR(current_drive) ]=
2406 (_floppy->size+1)>>1;
2407 break;
2410 if (probing) {
2411 if (DP->flags & FTD_MSG)
2412 DPRINT("Auto-detected floppy type %s in fd%d\n",
2413 _floppy->name,current_drive);
2414 current_type[current_drive] = _floppy;
2415 floppy_sizes[TOMINOR(current_drive)] = (_floppy->size+1) >> 1;
2416 probing = 0;
2419 if (CT(COMMAND) != FD_READ ||
2420 raw_cmd->kernel_data == CURRENT->buffer){
2421 /* transfer directly from buffer */
2422 cont->done(1);
2423 } else if (CT(COMMAND) == FD_READ){
2424 buffer_track = raw_cmd->track;
2425 buffer_drive = current_drive;
2426 INFBOUND(buffer_max, nr_sectors + sector_t);
2428 cont->redo();
2431 /* Compute maximal contiguous buffer size. */
2432 static int buffer_chain_size(void)
2434 struct buffer_head *bh;
2435 int size;
2436 char *base;
2438 base = CURRENT->buffer;
2439 size = CURRENT->current_nr_sectors << 9;
2440 bh = CURRENT->bh;
2442 if (bh){
2443 bh = bh->b_reqnext;
2444 while (bh && bh->b_data == base + size){
2445 size += bh->b_size;
2446 bh = bh->b_reqnext;
2449 return size >> 9;
2452 /* Compute the maximal transfer size */
2453 static int transfer_size(int ssize, int max_sector, int max_size)
2455 SUPBOUND(max_sector, sector_t + max_size);
2457 /* alignment */
2458 max_sector -= (max_sector % _floppy->sect) % ssize;
2460 /* transfer size, beginning not aligned */
2461 current_count_sectors = max_sector - sector_t ;
2463 return max_sector;
2467 * Move data from/to the track buffer to/from the buffer cache.
2469 static void copy_buffer(int ssize, int max_sector, int max_sector_2)
2471 int remaining; /* number of transferred 512-byte sectors */
2472 struct buffer_head *bh;
2473 char *buffer, *dma_buffer;
2474 int size;
2476 max_sector = transfer_size(ssize,
2477 minimum(max_sector, max_sector_2),
2478 CURRENT->nr_sectors);
2480 if (current_count_sectors <= 0 && CT(COMMAND) == FD_WRITE &&
2481 buffer_max > sector_t + CURRENT->nr_sectors)
2482 current_count_sectors = minimum(buffer_max - sector_t,
2483 CURRENT->nr_sectors);
2485 remaining = current_count_sectors << 9;
2486 #ifdef FLOPPY_SANITY_CHECK
2487 if ((remaining >> 9) > CURRENT->nr_sectors &&
2488 CT(COMMAND) == FD_WRITE){
2489 DPRINT("in copy buffer\n");
2490 printk("current_count_sectors=%ld\n", current_count_sectors);
2491 printk("remaining=%d\n", remaining >> 9);
2492 printk("CURRENT->nr_sectors=%ld\n",CURRENT->nr_sectors);
2493 printk("CURRENT->current_nr_sectors=%ld\n",
2494 CURRENT->current_nr_sectors);
2495 printk("max_sector=%d\n", max_sector);
2496 printk("ssize=%d\n", ssize);
2498 #endif
2500 buffer_max = maximum(max_sector, buffer_max);
2502 dma_buffer = floppy_track_buffer + ((sector_t - buffer_min) << 9);
2504 bh = CURRENT->bh;
2505 size = CURRENT->current_nr_sectors << 9;
2506 buffer = CURRENT->buffer;
2508 while (remaining > 0){
2509 SUPBOUND(size, remaining);
2510 #ifdef FLOPPY_SANITY_CHECK
2511 if (dma_buffer + size >
2512 floppy_track_buffer + (max_buffer_sectors << 10) ||
2513 dma_buffer < floppy_track_buffer){
2514 DPRINT("buffer overrun in copy buffer %d\n",
2515 (int) ((floppy_track_buffer - dma_buffer) >>9));
2516 printk("sector_t=%d buffer_min=%d\n",
2517 sector_t, buffer_min);
2518 printk("current_count_sectors=%ld\n",
2519 current_count_sectors);
2520 if (CT(COMMAND) == FD_READ)
2521 printk("read\n");
2522 if (CT(COMMAND) == FD_READ)
2523 printk("write\n");
2524 break;
2526 if (((unsigned long)buffer) % 512)
2527 DPRINT("%p buffer not aligned\n", buffer);
2528 #endif
2529 if (CT(COMMAND) == FD_READ)
2530 memcpy(buffer, dma_buffer, size);
2531 else
2532 memcpy(dma_buffer, buffer, size);
2533 remaining -= size;
2534 if (!remaining)
2535 break;
2537 dma_buffer += size;
2538 bh = bh->b_reqnext;
2539 #ifdef FLOPPY_SANITY_CHECK
2540 if (!bh){
2541 DPRINT("bh=null in copy buffer after copy\n");
2542 break;
2544 #endif
2545 size = bh->b_size;
2546 buffer = bh->b_data;
2548 #ifdef FLOPPY_SANITY_CHECK
2549 if (remaining){
2550 if (remaining > 0)
2551 max_sector -= remaining >> 9;
2552 DPRINT("weirdness: remaining %d\n", remaining>>9);
2554 #endif
2557 #if 0
2558 static inline int check_dma_crossing(char *start,
2559 unsigned long length, char *message)
2561 if (CROSS_64KB(start, length)) {
2562 printk("DMA xfer crosses 64KB boundary in %s %p-%p\n",
2563 message, start, start+length);
2564 return 1;
2565 } else
2566 return 0;
2568 #endif
2570 /* work around a bug in pseudo DMA
2571 * (on some FDCs) pseudo DMA does not stop when the CPU stops
2572 * sending data. Hence we need a different way to signal the
2573 * transfer length: We use SECT_PER_TRACK. Unfortunately, this
2574 * does not work with MT, hence we can only transfer one head at
2575 * a time
2577 static void virtualdmabug_workaround(void)
2579 int hard_sectors, end_sector;
2581 if(CT(COMMAND) == FD_WRITE) {
2582 COMMAND &= ~0x80; /* switch off multiple track mode */
2584 hard_sectors = raw_cmd->length >> (7 + SIZECODE);
2585 end_sector = SECTOR + hard_sectors - 1;
2586 #ifdef FLOPPY_SANITY_CHECK
2587 if(end_sector > SECT_PER_TRACK) {
2588 printk("too many sectors %d > %d\n",
2589 end_sector, SECT_PER_TRACK);
2590 return;
2592 #endif
2593 SECT_PER_TRACK = end_sector; /* make sure SECT_PER_TRACK points
2594 * to end of transfer */
2599 * Formulate a read/write request.
2600 * this routine decides where to load the data (directly to buffer, or to
2601 * tmp floppy area), how much data to load (the size of the buffer, the whole
2602 * track, or a single sector)
2603 * All floppy_track_buffer handling goes in here. If we ever add track buffer
2604 * allocation on the fly, it should be done here. No other part should need
2605 * modification.
2608 static int make_raw_rw_request(void)
2610 int aligned_sector_t;
2611 int max_sector, max_size, tracksize, ssize;
2613 if(max_buffer_sectors == 0) {
2614 printk("VFS: Block I/O scheduled on unopened device\n");
2615 return 0;
2618 set_fdc(DRIVE(CURRENT->rq_dev));
2620 raw_cmd = &default_raw_cmd;
2621 raw_cmd->flags = FD_RAW_SPIN | FD_RAW_NEED_DISK | FD_RAW_NEED_DISK |
2622 FD_RAW_NEED_SEEK;
2623 raw_cmd->cmd_count = NR_RW;
2624 if (CURRENT->cmd == READ){
2625 raw_cmd->flags |= FD_RAW_READ;
2626 COMMAND = FM_MODE(_floppy,FD_READ);
2627 } else if (CURRENT->cmd == WRITE){
2628 raw_cmd->flags |= FD_RAW_WRITE;
2629 COMMAND = FM_MODE(_floppy,FD_WRITE);
2630 } else {
2631 DPRINT("make_raw_rw_request: unknown command\n");
2632 return 0;
2635 max_sector = _floppy->sect * _floppy->head;
2637 TRACK = CURRENT->sector / max_sector;
2638 sector_t = CURRENT->sector % max_sector;
2639 if (_floppy->track && TRACK >= _floppy->track) {
2640 if (CURRENT->current_nr_sectors & 1) {
2641 current_count_sectors = 1;
2642 return 1;
2643 } else
2644 return 0;
2646 HEAD = sector_t / _floppy->sect;
2648 if (((_floppy->stretch & FD_SWAPSIDES) || TESTF(FD_NEED_TWADDLE)) &&
2649 sector_t < _floppy->sect)
2650 max_sector = _floppy->sect;
2652 /* 2M disks have phantom sectors on the first track */
2653 if ((_floppy->rate & FD_2M) && (!TRACK) && (!HEAD)){
2654 max_sector = 2 * _floppy->sect / 3;
2655 if (sector_t >= max_sector){
2656 current_count_sectors = minimum(_floppy->sect - sector_t,
2657 CURRENT->nr_sectors);
2658 return 1;
2660 SIZECODE = 2;
2661 } else
2662 SIZECODE = FD_SIZECODE(_floppy);
2663 raw_cmd->rate = _floppy->rate & 0x43;
2664 if ((_floppy->rate & FD_2M) &&
2665 (TRACK || HEAD) &&
2666 raw_cmd->rate == 2)
2667 raw_cmd->rate = 1;
2669 if (SIZECODE)
2670 SIZECODE2 = 0xff;
2671 else
2672 SIZECODE2 = 0x80;
2673 raw_cmd->track = TRACK << STRETCH(_floppy);
2674 DR_SELECT = UNIT(current_drive) + PH_HEAD(_floppy,HEAD);
2675 GAP = _floppy->gap;
2676 CODE2SIZE;
2677 SECT_PER_TRACK = _floppy->sect << 2 >> SIZECODE;
2678 SECTOR = ((sector_t % _floppy->sect) << 2 >> SIZECODE) + 1;
2680 /* tracksize describes the size which can be filled up with sectors
2681 * of size ssize.
2683 tracksize = _floppy->sect - _floppy->sect % ssize;
2684 if (tracksize < _floppy->sect){
2685 SECT_PER_TRACK ++;
2686 if (tracksize <= sector_t % _floppy->sect)
2687 SECTOR--;
2689 /* if we are beyond tracksize, fill up using smaller sectors */
2690 while (tracksize <= sector_t % _floppy->sect){
2691 while(tracksize + ssize > _floppy->sect){
2692 SIZECODE--;
2693 ssize >>= 1;
2695 SECTOR++; SECT_PER_TRACK ++;
2696 tracksize += ssize;
2698 max_sector = HEAD * _floppy->sect + tracksize;
2699 } else if (!TRACK && !HEAD && !(_floppy->rate & FD_2M) && probing) {
2700 max_sector = _floppy->sect;
2701 } else if (!HEAD && CT(COMMAND) == FD_WRITE) {
2702 /* for virtual DMA bug workaround */
2703 max_sector = _floppy->sect;
2706 in_sector_offset = (sector_t % _floppy->sect) % ssize;
2707 aligned_sector_t = sector_t - in_sector_offset;
2708 max_size = CURRENT->nr_sectors;
2709 if ((raw_cmd->track == buffer_track) &&
2710 (current_drive == buffer_drive) &&
2711 (sector_t >= buffer_min) && (sector_t < buffer_max)) {
2712 /* data already in track buffer */
2713 if (CT(COMMAND) == FD_READ) {
2714 copy_buffer(1, max_sector, buffer_max);
2715 return 1;
2717 } else if (in_sector_offset || CURRENT->nr_sectors < ssize){
2718 if (CT(COMMAND) == FD_WRITE){
2719 if (sector_t + CURRENT->nr_sectors > ssize &&
2720 sector_t + CURRENT->nr_sectors < ssize + ssize)
2721 max_size = ssize + ssize;
2722 else
2723 max_size = ssize;
2725 raw_cmd->flags &= ~FD_RAW_WRITE;
2726 raw_cmd->flags |= FD_RAW_READ;
2727 COMMAND = FM_MODE(_floppy,FD_READ);
2728 } else if ((unsigned long)CURRENT->buffer < MAX_DMA_ADDRESS) {
2729 unsigned long dma_limit;
2730 int direct, indirect;
2732 indirect= transfer_size(ssize,max_sector,max_buffer_sectors*2) -
2733 sector_t;
2736 * Do NOT use minimum() here---MAX_DMA_ADDRESS is 64 bits wide
2737 * on a 64 bit machine!
2739 max_size = buffer_chain_size();
2740 dma_limit = (MAX_DMA_ADDRESS - ((unsigned long) CURRENT->buffer)) >> 9;
2741 if ((unsigned long) max_size > dma_limit) {
2742 max_size = dma_limit;
2744 /* 64 kb boundaries */
2745 if (CROSS_64KB(CURRENT->buffer, max_size << 9))
2746 max_size = (K_64 -
2747 ((unsigned long)CURRENT->buffer) % K_64)>>9;
2748 direct = transfer_size(ssize,max_sector,max_size) - sector_t;
2750 * We try to read tracks, but if we get too many errors, we
2751 * go back to reading just one sector at a time.
2753 * This means we should be able to read a sector even if there
2754 * are other bad sectors on this track.
2756 if (!direct ||
2757 (indirect * 2 > direct * 3 &&
2758 *errors < DP->max_errors.read_track &&
2759 /*!TESTF(FD_NEED_TWADDLE) &&*/
2760 ((!probing || (DP->read_track&(1<<DRS->probed_format)))))){
2761 max_size = CURRENT->nr_sectors;
2762 } else {
2763 raw_cmd->kernel_data = CURRENT->buffer;
2764 raw_cmd->length = current_count_sectors << 9;
2765 if (raw_cmd->length == 0){
2766 DPRINT("zero dma transfer attempted from make_raw_request\n");
2767 DPRINT("indirect=%d direct=%d sector_t=%d",
2768 indirect, direct, sector_t);
2769 return 0;
2771 /* check_dma_crossing(raw_cmd->kernel_data,
2772 raw_cmd->length,
2773 "end of make_raw_request [1]");*/
2775 virtualdmabug_workaround();
2776 return 2;
2780 if (CT(COMMAND) == FD_READ)
2781 max_size = max_sector; /* unbounded */
2783 /* claim buffer track if needed */
2784 if (buffer_track != raw_cmd->track || /* bad track */
2785 buffer_drive !=current_drive || /* bad drive */
2786 sector_t > buffer_max ||
2787 sector_t < buffer_min ||
2788 ((CT(COMMAND) == FD_READ ||
2789 (!in_sector_offset && CURRENT->nr_sectors >= ssize))&&
2790 max_sector > 2 * max_buffer_sectors + buffer_min &&
2791 max_size + sector_t > 2 * max_buffer_sectors + buffer_min)
2792 /* not enough space */){
2793 buffer_track = -1;
2794 buffer_drive = current_drive;
2795 buffer_max = buffer_min = aligned_sector_t;
2797 raw_cmd->kernel_data = floppy_track_buffer +
2798 ((aligned_sector_t-buffer_min)<<9);
2800 if (CT(COMMAND) == FD_WRITE){
2801 /* copy write buffer to track buffer.
2802 * if we get here, we know that the write
2803 * is either aligned or the data already in the buffer
2804 * (buffer will be overwritten) */
2805 #ifdef FLOPPY_SANITY_CHECK
2806 if (in_sector_offset && buffer_track == -1)
2807 DPRINT("internal error offset !=0 on write\n");
2808 #endif
2809 buffer_track = raw_cmd->track;
2810 buffer_drive = current_drive;
2811 copy_buffer(ssize, max_sector, 2*max_buffer_sectors+buffer_min);
2812 } else
2813 transfer_size(ssize, max_sector,
2814 2*max_buffer_sectors+buffer_min-aligned_sector_t);
2816 /* round up current_count_sectors to get dma xfer size */
2817 raw_cmd->length = in_sector_offset+current_count_sectors;
2818 raw_cmd->length = ((raw_cmd->length -1)|(ssize-1))+1;
2819 raw_cmd->length <<= 9;
2820 #ifdef FLOPPY_SANITY_CHECK
2821 /*check_dma_crossing(raw_cmd->kernel_data, raw_cmd->length,
2822 "end of make_raw_request");*/
2823 if ((raw_cmd->length < current_count_sectors << 9) ||
2824 (raw_cmd->kernel_data != CURRENT->buffer &&
2825 CT(COMMAND) == FD_WRITE &&
2826 (aligned_sector_t + (raw_cmd->length >> 9) > buffer_max ||
2827 aligned_sector_t < buffer_min)) ||
2828 raw_cmd->length % (128 << SIZECODE) ||
2829 raw_cmd->length <= 0 || current_count_sectors <= 0){
2830 DPRINT("fractionary current count b=%lx s=%lx\n",
2831 raw_cmd->length, current_count_sectors);
2832 if (raw_cmd->kernel_data != CURRENT->buffer)
2833 printk("addr=%d, length=%ld\n",
2834 (int) ((raw_cmd->kernel_data -
2835 floppy_track_buffer) >> 9),
2836 current_count_sectors);
2837 printk("st=%d ast=%d mse=%d msi=%d\n",
2838 sector_t, aligned_sector_t, max_sector, max_size);
2839 printk("ssize=%x SIZECODE=%d\n", ssize, SIZECODE);
2840 printk("command=%x SECTOR=%d HEAD=%d, TRACK=%d\n",
2841 COMMAND, SECTOR, HEAD, TRACK);
2842 printk("buffer drive=%d\n", buffer_drive);
2843 printk("buffer track=%d\n", buffer_track);
2844 printk("buffer_min=%d\n", buffer_min);
2845 printk("buffer_max=%d\n", buffer_max);
2846 return 0;
2849 if (raw_cmd->kernel_data != CURRENT->buffer){
2850 if (raw_cmd->kernel_data < floppy_track_buffer ||
2851 current_count_sectors < 0 ||
2852 raw_cmd->length < 0 ||
2853 raw_cmd->kernel_data + raw_cmd->length >
2854 floppy_track_buffer + (max_buffer_sectors << 10)){
2855 DPRINT("buffer overrun in schedule dma\n");
2856 printk("sector_t=%d buffer_min=%d current_count=%ld\n",
2857 sector_t, buffer_min,
2858 raw_cmd->length >> 9);
2859 printk("current_count_sectors=%ld\n",
2860 current_count_sectors);
2861 if (CT(COMMAND) == FD_READ)
2862 printk("read\n");
2863 if (CT(COMMAND) == FD_READ)
2864 printk("write\n");
2865 return 0;
2867 } else if (raw_cmd->length > CURRENT->nr_sectors << 9 ||
2868 current_count_sectors > CURRENT->nr_sectors){
2869 DPRINT("buffer overrun in direct transfer\n");
2870 return 0;
2871 } else if (raw_cmd->length < current_count_sectors << 9){
2872 DPRINT("more sectors than bytes\n");
2873 printk("bytes=%ld\n", raw_cmd->length >> 9);
2874 printk("sectors=%ld\n", current_count_sectors);
2876 if (raw_cmd->length == 0){
2877 DPRINT("zero dma transfer attempted from make_raw_request\n");
2878 return 0;
2880 #endif
2882 virtualdmabug_workaround();
2883 return 2;
2886 static void redo_fd_request(void)
2888 #define REPEAT {request_done(0); continue; }
2889 kdev_t device;
2890 int tmp;
2892 lastredo = jiffies;
2893 if (current_drive < N_DRIVE)
2894 floppy_off(current_drive);
2896 if (!QUEUE_EMPTY && CURRENT->rq_status == RQ_INACTIVE){
2897 CLEAR_INTR;
2898 unlock_fdc();
2899 return;
2902 while(1){
2903 if (QUEUE_EMPTY) {
2904 CLEAR_INTR;
2905 unlock_fdc();
2906 return;
2908 if (MAJOR(CURRENT->rq_dev) != MAJOR_NR)
2909 panic(DEVICE_NAME ": request list destroyed");
2910 if (CURRENT->bh && !buffer_locked(CURRENT->bh))
2911 panic(DEVICE_NAME ": block not locked");
2913 device = CURRENT->rq_dev;
2914 set_fdc(DRIVE(device));
2915 reschedule_timeout(CURRENTD, "redo fd request", 0);
2917 set_floppy(device);
2918 raw_cmd = & default_raw_cmd;
2919 raw_cmd->flags = 0;
2920 if (start_motor(redo_fd_request)) return;
2921 disk_change(current_drive);
2922 if (test_bit(current_drive, &fake_change) ||
2923 TESTF(FD_DISK_CHANGED)){
2924 DPRINT("disk absent or changed during operation\n");
2925 REPEAT;
2927 if (!_floppy) { /* Autodetection */
2928 if (!probing){
2929 DRS->probed_format = 0;
2930 if (next_valid_format()){
2931 DPRINT("no autodetectable formats\n");
2932 _floppy = NULL;
2933 REPEAT;
2936 probing = 1;
2937 _floppy = floppy_type+DP->autodetect[DRS->probed_format];
2938 } else
2939 probing = 0;
2940 errors = & (CURRENT->errors);
2941 tmp = make_raw_rw_request();
2942 if (tmp < 2){
2943 request_done(tmp);
2944 continue;
2947 if (TESTF(FD_NEED_TWADDLE))
2948 twaddle();
2949 schedule_bh( (void *)(void *) floppy_start);
2950 #ifdef DEBUGT
2951 debugt("queue fd request");
2952 #endif
2953 return;
2955 #undef REPEAT
2958 static struct cont_t rw_cont={
2959 rw_interrupt,
2960 redo_fd_request,
2961 bad_flp_intr,
2962 request_done };
2964 static void process_fd_request(void)
2966 cont = &rw_cont;
2967 schedule_bh( (void *)(void *) redo_fd_request);
2970 static void do_fd_request(request_queue_t * q)
2972 if(max_buffer_sectors == 0) {
2973 printk("VFS: do_fd_request called on non-open device\n");
2974 return;
2977 if (usage_count == 0) {
2978 printk("warning: usage count=0, CURRENT=%p exiting\n", CURRENT);
2979 printk("sect=%ld cmd=%d\n", CURRENT->sector, CURRENT->cmd);
2980 return;
2982 if (fdc_busy){
2983 /* fdc busy, this new request will be treated when the
2984 current one is done */
2985 is_alive("do fd request, old request running");
2986 return;
2988 lock_fdc(MAXTIMEOUT,0);
2989 process_fd_request();
2990 is_alive("do fd request");
2993 static struct cont_t poll_cont={
2994 success_and_wakeup,
2995 floppy_ready,
2996 generic_failure,
2997 generic_done };
2999 static int poll_drive(int interruptible, int flag)
3001 int ret;
3002 /* no auto-sense, just clear dcl */
3003 raw_cmd = &default_raw_cmd;
3004 raw_cmd->flags= flag;
3005 raw_cmd->track=0;
3006 raw_cmd->cmd_count=0;
3007 cont = &poll_cont;
3008 #ifdef DCL_DEBUG
3009 if (DP->flags & FD_DEBUG){
3010 DPRINT("setting NEWCHANGE in poll_drive\n");
3012 #endif
3013 SETF(FD_DISK_NEWCHANGE);
3014 WAIT(floppy_ready);
3015 return ret;
3019 * User triggered reset
3020 * ====================
3023 static void reset_intr(void)
3025 printk("weird, reset interrupt called\n");
3028 static struct cont_t reset_cont={
3029 reset_intr,
3030 success_and_wakeup,
3031 generic_failure,
3032 generic_done };
3034 static int user_reset_fdc(int drive, int arg, int interruptible)
3036 int ret;
3038 ret=0;
3039 LOCK_FDC(drive,interruptible);
3040 if (arg == FD_RESET_ALWAYS)
3041 FDCS->reset=1;
3042 if (FDCS->reset){
3043 cont = &reset_cont;
3044 WAIT(reset_fdc);
3046 process_fd_request();
3047 return ret;
3051 * Misc Ioctl's and support
3052 * ========================
3054 static inline int fd_copyout(void *param, const void *address, unsigned long size)
3056 return copy_to_user(param,address, size) ? -EFAULT : 0;
3059 static inline int fd_copyin(void *param, void *address, unsigned long size)
3061 return copy_from_user(address, param, size) ? -EFAULT : 0;
3064 #define _COPYOUT(x) (copy_to_user((void *)param, &(x), sizeof(x)) ? -EFAULT : 0)
3065 #define _COPYIN(x) (copy_from_user(&(x), (void *)param, sizeof(x)) ? -EFAULT : 0)
3067 #define COPYOUT(x) ECALL(_COPYOUT(x))
3068 #define COPYIN(x) ECALL(_COPYIN(x))
3070 static inline const char *drive_name(int type, int drive)
3072 struct floppy_struct *floppy;
3074 if (type)
3075 floppy = floppy_type + type;
3076 else {
3077 if (UDP->native_format)
3078 floppy = floppy_type + UDP->native_format;
3079 else
3080 return "(null)";
3082 if (floppy->name)
3083 return floppy->name;
3084 else
3085 return "(null)";
3089 /* raw commands */
3090 static void raw_cmd_done(int flag)
3092 int i;
3094 if (!flag) {
3095 raw_cmd->flags |= FD_RAW_FAILURE;
3096 raw_cmd->flags |= FD_RAW_HARDFAILURE;
3097 } else {
3098 raw_cmd->reply_count = inr;
3099 if (raw_cmd->reply_count > MAX_REPLIES)
3100 raw_cmd->reply_count=0;
3101 for (i=0; i< raw_cmd->reply_count; i++)
3102 raw_cmd->reply[i] = reply_buffer[i];
3104 if (raw_cmd->flags & (FD_RAW_READ | FD_RAW_WRITE))
3106 unsigned long flags;
3107 flags=claim_dma_lock();
3108 raw_cmd->length = fd_get_dma_residue();
3109 release_dma_lock(flags);
3112 if ((raw_cmd->flags & FD_RAW_SOFTFAILURE) &&
3113 (!raw_cmd->reply_count || (raw_cmd->reply[0] & 0xc0)))
3114 raw_cmd->flags |= FD_RAW_FAILURE;
3116 if (disk_change(current_drive))
3117 raw_cmd->flags |= FD_RAW_DISK_CHANGE;
3118 else
3119 raw_cmd->flags &= ~FD_RAW_DISK_CHANGE;
3120 if (raw_cmd->flags & FD_RAW_NO_MOTOR_AFTER)
3121 motor_off_callback(current_drive);
3123 if (raw_cmd->next &&
3124 (!(raw_cmd->flags & FD_RAW_FAILURE) ||
3125 !(raw_cmd->flags & FD_RAW_STOP_IF_FAILURE)) &&
3126 ((raw_cmd->flags & FD_RAW_FAILURE) ||
3127 !(raw_cmd->flags &FD_RAW_STOP_IF_SUCCESS))) {
3128 raw_cmd = raw_cmd->next;
3129 return;
3132 generic_done(flag);
3136 static struct cont_t raw_cmd_cont={
3137 success_and_wakeup,
3138 floppy_start,
3139 generic_failure,
3140 raw_cmd_done
3143 static inline int raw_cmd_copyout(int cmd, char *param,
3144 struct floppy_raw_cmd *ptr)
3146 int ret;
3148 while(ptr) {
3149 COPYOUT(*ptr);
3150 param += sizeof(struct floppy_raw_cmd);
3151 if ((ptr->flags & FD_RAW_READ) && ptr->buffer_length){
3152 if (ptr->length>=0 && ptr->length<=ptr->buffer_length)
3153 ECALL(fd_copyout(ptr->data,
3154 ptr->kernel_data,
3155 ptr->buffer_length -
3156 ptr->length));
3158 ptr = ptr->next;
3160 return 0;
3164 static void raw_cmd_free(struct floppy_raw_cmd **ptr)
3166 struct floppy_raw_cmd *next,*this;
3168 this = *ptr;
3169 *ptr = 0;
3170 while(this) {
3171 if (this->buffer_length) {
3172 fd_dma_mem_free((unsigned long)this->kernel_data,
3173 this->buffer_length);
3174 this->buffer_length = 0;
3176 next = this->next;
3177 kfree(this);
3178 this = next;
3183 static inline int raw_cmd_copyin(int cmd, char *param,
3184 struct floppy_raw_cmd **rcmd)
3186 struct floppy_raw_cmd *ptr;
3187 int ret;
3188 int i;
3190 *rcmd = 0;
3191 while(1) {
3192 ptr = (struct floppy_raw_cmd *)
3193 kmalloc(sizeof(struct floppy_raw_cmd), GFP_USER);
3194 if (!ptr)
3195 return -ENOMEM;
3196 *rcmd = ptr;
3197 COPYIN(*ptr);
3198 ptr->next = 0;
3199 ptr->buffer_length = 0;
3200 param += sizeof(struct floppy_raw_cmd);
3201 if (ptr->cmd_count > 33)
3202 /* the command may now also take up the space
3203 * initially intended for the reply & the
3204 * reply count. Needed for long 82078 commands
3205 * such as RESTORE, which takes ... 17 command
3206 * bytes. Murphy's law #137: When you reserve
3207 * 16 bytes for a structure, you'll one day
3208 * discover that you really need 17...
3210 return -EINVAL;
3212 for (i=0; i< 16; i++)
3213 ptr->reply[i] = 0;
3214 ptr->resultcode = 0;
3215 ptr->kernel_data = 0;
3217 if (ptr->flags & (FD_RAW_READ | FD_RAW_WRITE)) {
3218 if (ptr->length <= 0)
3219 return -EINVAL;
3220 ptr->kernel_data =(char*)fd_dma_mem_alloc(ptr->length);
3221 fallback_on_nodma_alloc(&ptr->kernel_data,
3222 ptr->length);
3223 if (!ptr->kernel_data)
3224 return -ENOMEM;
3225 ptr->buffer_length = ptr->length;
3227 if (ptr->flags & FD_RAW_WRITE)
3228 ECALL(fd_copyin(ptr->data, ptr->kernel_data,
3229 ptr->length));
3230 rcmd = & (ptr->next);
3231 if (!(ptr->flags & FD_RAW_MORE))
3232 return 0;
3233 ptr->rate &= 0x43;
3238 static int raw_cmd_ioctl(int cmd, void *param)
3240 int drive, ret, ret2;
3241 struct floppy_raw_cmd *my_raw_cmd;
3243 if (FDCS->rawcmd <= 1)
3244 FDCS->rawcmd = 1;
3245 for (drive= 0; drive < N_DRIVE; drive++){
3246 if (FDC(drive) != fdc)
3247 continue;
3248 if (drive == current_drive){
3249 if (UDRS->fd_ref > 1){
3250 FDCS->rawcmd = 2;
3251 break;
3253 } else if (UDRS->fd_ref){
3254 FDCS->rawcmd = 2;
3255 break;
3259 if (FDCS->reset)
3260 return -EIO;
3262 ret = raw_cmd_copyin(cmd, param, &my_raw_cmd);
3263 if (ret) {
3264 raw_cmd_free(&my_raw_cmd);
3265 return ret;
3268 raw_cmd = my_raw_cmd;
3269 cont = &raw_cmd_cont;
3270 ret=wait_til_done(floppy_start,1);
3271 #ifdef DCL_DEBUG
3272 if (DP->flags & FD_DEBUG){
3273 DPRINT("calling disk change from raw_cmd ioctl\n");
3275 #endif
3277 if (ret != -EINTR && FDCS->reset)
3278 ret = -EIO;
3280 DRS->track = NO_TRACK;
3282 ret2 = raw_cmd_copyout(cmd, param, my_raw_cmd);
3283 if (!ret)
3284 ret = ret2;
3285 raw_cmd_free(&my_raw_cmd);
3286 return ret;
3289 static int invalidate_drive(kdev_t rdev)
3291 /* invalidate the buffer track to force a reread */
3292 set_bit(DRIVE(rdev), &fake_change);
3293 process_fd_request();
3294 check_disk_change(rdev);
3295 return 0;
3299 static inline void clear_write_error(int drive)
3301 CLEARSTRUCT(UDRWE);
3304 static inline int set_geometry(unsigned int cmd, struct floppy_struct *g,
3305 int drive, int type, kdev_t device)
3307 int cnt;
3309 /* sanity checking for parameters.*/
3310 if (g->sect <= 0 ||
3311 g->head <= 0 ||
3312 g->track <= 0 ||
3313 g->track > UDP->tracks>>STRETCH(g) ||
3314 /* check if reserved bits are set */
3315 (g->stretch&~(FD_STRETCH|FD_SWAPSIDES)) != 0)
3316 return -EINVAL;
3317 if (type){
3318 if (!capable(CAP_SYS_ADMIN))
3319 return -EPERM;
3320 LOCK_FDC(drive,1);
3321 for (cnt = 0; cnt < N_DRIVE; cnt++){
3322 if (ITYPE(drive_state[cnt].fd_device) == type &&
3323 drive_state[cnt].fd_ref)
3324 set_bit(drive, &fake_change);
3326 floppy_type[type] = *g;
3327 floppy_type[type].name="user format";
3328 for (cnt = type << 2; cnt < (type << 2) + 4; cnt++)
3329 floppy_sizes[cnt]= floppy_sizes[cnt+0x80]=
3330 (floppy_type[type].size+1)>>1;
3331 process_fd_request();
3332 for (cnt = 0; cnt < N_DRIVE; cnt++){
3333 if (ITYPE(drive_state[cnt].fd_device) == type &&
3334 drive_state[cnt].fd_ref)
3335 check_disk_change(
3336 MKDEV(FLOPPY_MAJOR,
3337 drive_state[cnt].fd_device));
3339 } else {
3340 LOCK_FDC(drive,1);
3341 if (cmd != FDDEFPRM)
3342 /* notice a disk change immediately, else
3343 * we lose our settings immediately*/
3344 CALL(poll_drive(1, FD_RAW_NEED_DISK));
3345 user_params[drive] = *g;
3346 if (buffer_drive == drive)
3347 SUPBOUND(buffer_max, user_params[drive].sect);
3348 current_type[drive] = &user_params[drive];
3349 floppy_sizes[drive] = (user_params[drive].size+1) >> 1;
3350 if (cmd == FDDEFPRM)
3351 DRS->keep_data = -1;
3352 else
3353 DRS->keep_data = 1;
3354 /* invalidation. Invalidate only when needed, i.e.
3355 * when there are already sectors in the buffer cache
3356 * whose number will change. This is useful, because
3357 * mtools often changes the geometry of the disk after
3358 * looking at the boot block */
3359 if (DRS->maxblock > user_params[drive].sect || DRS->maxtrack)
3360 invalidate_drive(device);
3361 else
3362 process_fd_request();
3364 return 0;
3367 /* handle obsolete ioctl's */
3368 static int ioctl_table[]= {
3369 FDCLRPRM,
3370 FDSETPRM,
3371 FDDEFPRM,
3372 FDGETPRM,
3373 FDMSGON,
3374 FDMSGOFF,
3375 FDFMTBEG,
3376 FDFMTTRK,
3377 FDFMTEND,
3378 FDSETEMSGTRESH,
3379 FDFLUSH,
3380 FDSETMAXERRS,
3381 FDGETMAXERRS,
3382 FDGETDRVTYP,
3383 FDSETDRVPRM,
3384 FDGETDRVPRM,
3385 FDGETDRVSTAT,
3386 FDPOLLDRVSTAT,
3387 FDRESET,
3388 FDGETFDCSTAT,
3389 FDWERRORCLR,
3390 FDWERRORGET,
3391 FDRAWCMD,
3392 FDEJECT,
3393 FDTWADDLE
3396 static inline int normalize_ioctl(int *cmd, int *size)
3398 int i;
3400 for (i=0; i < ARRAY_SIZE(ioctl_table); i++) {
3401 if ((*cmd & 0xffff) == (ioctl_table[i] & 0xffff)){
3402 *size = _IOC_SIZE(*cmd);
3403 *cmd = ioctl_table[i];
3404 if (*size > _IOC_SIZE(*cmd)) {
3405 printk("ioctl not yet supported\n");
3406 return -EFAULT;
3408 return 0;
3411 return -EINVAL;
3414 static int get_floppy_geometry(int drive, int type, struct floppy_struct **g)
3416 if (type)
3417 *g = &floppy_type[type];
3418 else {
3419 LOCK_FDC(drive,0);
3420 CALL(poll_drive(0,0));
3421 process_fd_request();
3422 *g = current_type[drive];
3424 if (!*g)
3425 return -ENODEV;
3426 return 0;
3429 static int fd_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
3430 unsigned long param)
3432 #define OUT(c,x) case c: outparam = (const char *) (x); break
3433 #define IN(c,x,tag) case c: *(x) = inparam. tag ; return 0
3435 int i,drive,type;
3436 kdev_t device;
3437 int ret;
3438 int size;
3439 union inparam {
3440 struct floppy_struct g; /* geometry */
3441 struct format_descr f;
3442 struct floppy_max_errors max_errors;
3443 struct floppy_drive_params dp;
3444 } inparam; /* parameters coming from user space */
3445 const char *outparam; /* parameters passed back to user space */
3447 device = inode->i_rdev;
3448 switch (cmd) {
3449 case BLKROSET:
3450 case BLKROGET:
3451 case BLKRASET:
3452 case BLKRAGET:
3453 case BLKFLSBUF:
3454 return blk_ioctl(device, cmd, param);
3456 type = TYPE(device);
3457 drive = DRIVE(device);
3459 /* convert compatibility eject ioctls into floppy eject ioctl.
3460 * We do this in order to provide a means to eject floppy disks before
3461 * installing the new fdutils package */
3462 if (cmd == CDROMEJECT || /* CD-ROM eject */
3463 cmd == 0x6470 /* SunOS floppy eject */) {
3464 DPRINT("obsolete eject ioctl\n");
3465 DPRINT("please use floppycontrol --eject\n");
3466 cmd = FDEJECT;
3469 /* generic block device ioctls */
3470 switch(cmd) {
3471 /* the following have been inspired by the corresponding
3472 * code for other block devices. */
3473 struct floppy_struct *g;
3474 case HDIO_GETGEO:
3476 struct hd_geometry loc;
3477 ECALL(get_floppy_geometry(drive, type, &g));
3478 loc.heads = g->head;
3479 loc.sectors = g->sect;
3480 loc.cylinders = g->track;
3481 loc.start = 0;
3482 return _COPYOUT(loc);
3485 case BLKGETSIZE:
3486 ECALL(get_floppy_geometry(drive, type, &g));
3487 return put_user(g->size, (long *) param);
3488 /* BLKRRPART is not defined as floppies don't have
3489 * partition tables */
3492 /* convert the old style command into a new style command */
3493 if ((cmd & 0xff00) == 0x0200) {
3494 ECALL(normalize_ioctl(&cmd, &size));
3495 } else
3496 return -EINVAL;
3498 /* permission checks */
3499 if (((cmd & 0x40) && !(filp->f_mode & 2)) ||
3500 ((cmd & 0x80) && !suser()))
3501 return -EPERM;
3503 /* copyin */
3504 CLEARSTRUCT(&inparam);
3505 if (_IOC_DIR(cmd) & _IOC_WRITE)
3506 ECALL(fd_copyin((void *)param, &inparam, size))
3508 switch (cmd) {
3509 case FDEJECT:
3510 if (UDRS->fd_ref != 1)
3511 /* somebody else has this drive open */
3512 return -EBUSY;
3513 LOCK_FDC(drive,1);
3515 /* do the actual eject. Fails on
3516 * non-Sparc architectures */
3517 ret=fd_eject(UNIT(drive));
3519 USETF(FD_DISK_CHANGED);
3520 USETF(FD_VERIFY);
3521 process_fd_request();
3522 return ret;
3523 case FDCLRPRM:
3524 LOCK_FDC(drive,1);
3525 current_type[drive] = NULL;
3526 floppy_sizes[drive] = MAX_DISK_SIZE;
3527 UDRS->keep_data = 0;
3528 return invalidate_drive(device);
3529 case FDSETPRM:
3530 case FDDEFPRM:
3531 return set_geometry(cmd, & inparam.g,
3532 drive, type, device);
3533 case FDGETPRM:
3534 ECALL(get_floppy_geometry(drive, type,
3535 (struct floppy_struct**)
3536 &outparam));
3537 break;
3539 case FDMSGON:
3540 UDP->flags |= FTD_MSG;
3541 return 0;
3542 case FDMSGOFF:
3543 UDP->flags &= ~FTD_MSG;
3544 return 0;
3546 case FDFMTBEG:
3547 LOCK_FDC(drive,1);
3548 CALL(poll_drive(1, FD_RAW_NEED_DISK));
3549 ret = UDRS->flags;
3550 process_fd_request();
3551 if (ret & FD_VERIFY)
3552 return -ENODEV;
3553 if (!(ret & FD_DISK_WRITABLE))
3554 return -EROFS;
3555 return 0;
3556 case FDFMTTRK:
3557 if (UDRS->fd_ref != 1)
3558 return -EBUSY;
3559 return do_format(device, &inparam.f);
3560 case FDFMTEND:
3561 case FDFLUSH:
3562 LOCK_FDC(drive,1);
3563 return invalidate_drive(device);
3565 case FDSETEMSGTRESH:
3566 UDP->max_errors.reporting =
3567 (unsigned short) (param & 0x0f);
3568 return 0;
3569 OUT(FDGETMAXERRS, &UDP->max_errors);
3570 IN(FDSETMAXERRS, &UDP->max_errors, max_errors);
3572 case FDGETDRVTYP:
3573 outparam = drive_name(type,drive);
3574 SUPBOUND(size,strlen(outparam)+1);
3575 break;
3577 IN(FDSETDRVPRM, UDP, dp);
3578 OUT(FDGETDRVPRM, UDP);
3580 case FDPOLLDRVSTAT:
3581 LOCK_FDC(drive,1);
3582 CALL(poll_drive(1, FD_RAW_NEED_DISK));
3583 process_fd_request();
3584 /* fall through */
3585 OUT(FDGETDRVSTAT, UDRS);
3587 case FDRESET:
3588 return user_reset_fdc(drive, (int)param, 1);
3590 OUT(FDGETFDCSTAT,UFDCS);
3592 case FDWERRORCLR:
3593 CLEARSTRUCT(UDRWE);
3594 return 0;
3595 OUT(FDWERRORGET,UDRWE);
3597 case FDRAWCMD:
3598 if (type)
3599 return -EINVAL;
3600 LOCK_FDC(drive,1);
3601 set_floppy(device);
3602 CALL(i = raw_cmd_ioctl(cmd,(void *) param));
3603 process_fd_request();
3604 return i;
3606 case FDTWADDLE:
3607 LOCK_FDC(drive,1);
3608 twaddle();
3609 process_fd_request();
3610 return 0;
3612 default:
3613 return -EINVAL;
3616 if (_IOC_DIR(cmd) & _IOC_READ)
3617 return fd_copyout((void *)param, outparam, size);
3618 else
3619 return 0;
3620 #undef OUT
3621 #undef IN
3624 static void __init config_types(void)
3626 int first=1;
3627 int drive;
3629 /* read drive info out of physical CMOS */
3630 drive=0;
3631 if (!UDP->cmos)
3632 UDP->cmos = FLOPPY0_TYPE;
3633 drive=1;
3634 if (!UDP->cmos && FLOPPY1_TYPE)
3635 UDP->cmos = FLOPPY1_TYPE;
3637 /* XXX */
3638 /* additional physical CMOS drive detection should go here */
3640 for (drive=0; drive < N_DRIVE; drive++){
3641 unsigned int type = UDP->cmos;
3642 struct floppy_drive_params *params;
3643 const char *name = NULL;
3644 static char temparea[32];
3646 if (type < NUMBER(default_drive_params)) {
3647 params = &default_drive_params[type].params;
3648 if (type) {
3649 name = default_drive_params[type].name;
3650 allowed_drive_mask |= 1 << drive;
3652 } else {
3653 params = &default_drive_params[0].params;
3654 sprintf(temparea, "unknown type %d (usb?)", type);
3655 name = temparea;
3657 if (name) {
3658 const char * prepend = ",";
3659 if (first) {
3660 prepend = KERN_INFO "Floppy drive(s):";
3661 first = 0;
3663 printk("%s fd%d is %s", prepend, drive, name);
3664 register_devfs_entries (drive);
3666 *UDP = *params;
3668 if (!first)
3669 printk("\n");
3672 static int floppy_release(struct inode * inode, struct file * filp)
3674 int drive = DRIVE(inode->i_rdev);
3676 if (UDRS->fd_ref < 0)
3677 UDRS->fd_ref=0;
3678 else if (!UDRS->fd_ref--) {
3679 DPRINT("floppy_release with fd_ref == 0");
3680 UDRS->fd_ref = 0;
3682 floppy_release_irq_and_dma();
3683 return 0;
3687 * floppy_open check for aliasing (/dev/fd0 can be the same as
3688 * /dev/PS0 etc), and disallows simultaneous access to the same
3689 * drive with different device numbers.
3691 #define RETERR(x) do{floppy_release(inode,filp); return -(x);}while(0)
3693 static int floppy_open(struct inode * inode, struct file * filp)
3695 int drive;
3696 int old_dev;
3697 int try;
3698 char *tmp;
3700 if (!filp) {
3701 DPRINT("Weird, open called with filp=0\n");
3702 return -EIO;
3705 drive = DRIVE(inode->i_rdev);
3706 if (drive >= N_DRIVE ||
3707 !(allowed_drive_mask & (1 << drive)) ||
3708 fdc_state[FDC(drive)].version == FDC_NONE)
3709 return -ENXIO;
3711 if (TYPE(inode->i_rdev) >= NUMBER(floppy_type))
3712 return -ENXIO;
3713 old_dev = UDRS->fd_device;
3714 if (UDRS->fd_ref && old_dev != MINOR(inode->i_rdev))
3715 return -EBUSY;
3717 if (!UDRS->fd_ref && (UDP->flags & FD_BROKEN_DCL)){
3718 USETF(FD_DISK_CHANGED);
3719 USETF(FD_VERIFY);
3722 if (UDRS->fd_ref == -1 ||
3723 (UDRS->fd_ref && (filp->f_flags & O_EXCL)))
3724 return -EBUSY;
3726 if (floppy_grab_irq_and_dma())
3727 return -EBUSY;
3729 if (filp->f_flags & O_EXCL)
3730 UDRS->fd_ref = -1;
3731 else
3732 UDRS->fd_ref++;
3734 if (!floppy_track_buffer){
3735 /* if opening an ED drive, reserve a big buffer,
3736 * else reserve a small one */
3737 if ((UDP->cmos == 6) || (UDP->cmos == 5))
3738 try = 64; /* Only 48 actually useful */
3739 else
3740 try = 32; /* Only 24 actually useful */
3742 tmp=(char *)fd_dma_mem_alloc(1024 * try);
3743 if (!tmp && !floppy_track_buffer) {
3744 try >>= 1; /* buffer only one side */
3745 INFBOUND(try, 16);
3746 tmp= (char *)fd_dma_mem_alloc(1024*try);
3748 if (!tmp && !floppy_track_buffer) {
3749 fallback_on_nodma_alloc(&tmp, 2048 * try);
3751 if (!tmp && !floppy_track_buffer) {
3752 DPRINT("Unable to allocate DMA memory\n");
3753 RETERR(ENXIO);
3755 if (floppy_track_buffer) {
3756 if (tmp)
3757 fd_dma_mem_free((unsigned long)tmp,try*1024);
3758 } else {
3759 buffer_min = buffer_max = -1;
3760 floppy_track_buffer = tmp;
3761 max_buffer_sectors = try;
3765 UDRS->fd_device = MINOR(inode->i_rdev);
3766 if (old_dev != -1 && old_dev != MINOR(inode->i_rdev)) {
3767 if (buffer_drive == drive)
3768 buffer_track = -1;
3769 invalidate_buffers(MKDEV(FLOPPY_MAJOR,old_dev));
3772 if (UFDCS->rawcmd == 1)
3773 UFDCS->rawcmd = 2;
3775 if (filp->f_flags & O_NDELAY)
3776 return 0;
3777 if (filp->f_mode & 3) {
3778 UDRS->last_checked = 0;
3779 check_disk_change(inode->i_rdev);
3780 if (UTESTF(FD_DISK_CHANGED))
3781 RETERR(ENXIO);
3783 if ((filp->f_mode & 2) && !(UTESTF(FD_DISK_WRITABLE)))
3784 RETERR(EROFS);
3785 return 0;
3786 #undef RETERR
3790 * Check if the disk has been changed or if a change has been faked.
3792 static int check_floppy_change(kdev_t dev)
3794 int drive = DRIVE(dev);
3796 if (MAJOR(dev) != MAJOR_NR) {
3797 DPRINT("check_floppy_change: not a floppy\n");
3798 return 0;
3801 if (UTESTF(FD_DISK_CHANGED) || UTESTF(FD_VERIFY))
3802 return 1;
3804 if (UDP->checkfreq < (int)(jiffies - UDRS->last_checked)) {
3805 if(floppy_grab_irq_and_dma()) {
3806 return 1;
3809 lock_fdc(drive,0);
3810 poll_drive(0,0);
3811 process_fd_request();
3812 floppy_release_irq_and_dma();
3815 if (UTESTF(FD_DISK_CHANGED) ||
3816 UTESTF(FD_VERIFY) ||
3817 test_bit(drive, &fake_change) ||
3818 (!TYPE(dev) && !current_type[drive]))
3819 return 1;
3820 return 0;
3823 /* revalidate the floppy disk, i.e. trigger format autodetection by reading
3824 * the bootblock (block 0). "Autodetection" is also needed to check whether
3825 * there is a disk in the drive at all... Thus we also do it for fixed
3826 * geometry formats */
3827 static int floppy_revalidate(kdev_t dev)
3829 #define NO_GEOM (!current_type[drive] && !TYPE(dev))
3830 struct buffer_head * bh;
3831 int drive=DRIVE(dev);
3832 int cf;
3834 if (UTESTF(FD_DISK_CHANGED) ||
3835 UTESTF(FD_VERIFY) ||
3836 test_bit(drive, &fake_change) ||
3837 NO_GEOM){
3838 if(usage_count == 0) {
3839 printk("VFS: revalidate called on non-open device.\n");
3840 return -EFAULT;
3842 lock_fdc(drive,0);
3843 cf = UTESTF(FD_DISK_CHANGED) || UTESTF(FD_VERIFY);
3844 if (!(cf || test_bit(drive, &fake_change) || NO_GEOM)){
3845 process_fd_request(); /*already done by another thread*/
3846 return 0;
3848 UDRS->maxblock = 0;
3849 UDRS->maxtrack = 0;
3850 if (buffer_drive == drive)
3851 buffer_track = -1;
3852 clear_bit(drive, &fake_change);
3853 UCLEARF(FD_DISK_CHANGED);
3854 if (cf)
3855 UDRS->generation++;
3856 if (NO_GEOM){
3857 /* auto-sensing */
3858 int size = floppy_blocksizes[MINOR(dev)];
3859 if (!size)
3860 size = 1024;
3861 if (!(bh = getblk(dev,0,size))){
3862 process_fd_request();
3863 return -ENXIO;
3865 if (bh && !buffer_uptodate(bh))
3866 ll_rw_block(READ, 1, &bh);
3867 process_fd_request();
3868 wait_on_buffer(bh);
3869 brelse(bh);
3870 return 0;
3872 if (cf)
3873 poll_drive(0, FD_RAW_NEED_DISK);
3874 process_fd_request();
3876 return 0;
3879 static struct block_device_operations floppy_fops = {
3880 open: floppy_open,
3881 release: floppy_release,
3882 ioctl: fd_ioctl,
3883 check_media_change: check_floppy_change,
3884 revalidate: floppy_revalidate,
3887 static void __init register_devfs_entries (int drive)
3889 int base_minor, i;
3890 static char *table[] =
3891 {"", "d360", "h1200", "u360", "u720", "h360", "h720",
3892 "u1440", "u2880", "CompaQ", "h1440", "u1680", "h410",
3893 "u820", "h1476", "u1722", "h420", "u830", "h1494", "u1743",
3894 "h880", "u1040", "u1120", "h1600", "u1760", "u1920",
3895 "u3200", "u3520", "u3840", "u1840", "u800", "u1600",
3896 NULL
3898 static int t360[] = {1,0}, t1200[] = {2,5,6,10,12,14,16,18,20,23,0},
3899 t3in[] = {8,9,26,27,28, 7,11,15,19,24,25,29,31, 3,4,13,17,21,22,30,0};
3900 static int *table_sup[] =
3901 {NULL, t360, t1200, t3in+5+8, t3in+5, t3in, t3in};
3903 base_minor = (drive < 4) ? drive : (124 + drive);
3904 if (UDP->cmos <= NUMBER(default_drive_params)) {
3905 i = 0;
3906 do {
3907 char name[16];
3909 sprintf (name, "%d%s", drive, table[table_sup[UDP->cmos][i]]);
3910 devfs_register (devfs_handle, name, DEVFS_FL_DEFAULT, MAJOR_NR,
3911 base_minor + (table_sup[UDP->cmos][i] << 2),
3912 S_IFBLK | S_IRUSR | S_IWUSR | S_IRGRP |S_IWGRP,
3913 &floppy_fops, NULL);
3914 } while (table_sup[UDP->cmos][i++]);
3919 * Floppy Driver initialization
3920 * =============================
3923 /* Determine the floppy disk controller type */
3924 /* This routine was written by David C. Niemi */
3925 static char __init get_fdc_version(void)
3927 int r;
3929 output_byte(FD_DUMPREGS); /* 82072 and better know DUMPREGS */
3930 if (FDCS->reset)
3931 return FDC_NONE;
3932 if ((r = result()) <= 0x00)
3933 return FDC_NONE; /* No FDC present ??? */
3934 if ((r==1) && (reply_buffer[0] == 0x80)){
3935 printk(KERN_INFO "FDC %d is an 8272A\n",fdc);
3936 return FDC_8272A; /* 8272a/765 don't know DUMPREGS */
3938 if (r != 10) {
3939 printk("FDC %d init: DUMPREGS: unexpected return of %d bytes.\n",
3940 fdc, r);
3941 return FDC_UNKNOWN;
3944 if (!fdc_configure()) {
3945 printk(KERN_INFO "FDC %d is an 82072\n",fdc);
3946 return FDC_82072; /* 82072 doesn't know CONFIGURE */
3949 output_byte(FD_PERPENDICULAR);
3950 if (need_more_output() == MORE_OUTPUT) {
3951 output_byte(0);
3952 } else {
3953 printk(KERN_INFO "FDC %d is an 82072A\n", fdc);
3954 return FDC_82072A; /* 82072A as found on Sparcs. */
3957 output_byte(FD_UNLOCK);
3958 r = result();
3959 if ((r == 1) && (reply_buffer[0] == 0x80)){
3960 printk(KERN_INFO "FDC %d is a pre-1991 82077\n", fdc);
3961 return FDC_82077_ORIG; /* Pre-1991 82077, doesn't know
3962 * LOCK/UNLOCK */
3964 if ((r != 1) || (reply_buffer[0] != 0x00)) {
3965 printk("FDC %d init: UNLOCK: unexpected return of %d bytes.\n",
3966 fdc, r);
3967 return FDC_UNKNOWN;
3969 output_byte(FD_PARTID);
3970 r = result();
3971 if (r != 1) {
3972 printk("FDC %d init: PARTID: unexpected return of %d bytes.\n",
3973 fdc, r);
3974 return FDC_UNKNOWN;
3976 if (reply_buffer[0] == 0x80) {
3977 printk(KERN_INFO "FDC %d is a post-1991 82077\n",fdc);
3978 return FDC_82077; /* Revised 82077AA passes all the tests */
3980 switch (reply_buffer[0] >> 5) {
3981 case 0x0:
3982 /* Either a 82078-1 or a 82078SL running at 5Volt */
3983 printk(KERN_INFO "FDC %d is an 82078.\n",fdc);
3984 return FDC_82078;
3985 case 0x1:
3986 printk(KERN_INFO "FDC %d is a 44pin 82078\n",fdc);
3987 return FDC_82078;
3988 case 0x2:
3989 printk(KERN_INFO "FDC %d is a S82078B\n", fdc);
3990 return FDC_S82078B;
3991 case 0x3:
3992 printk(KERN_INFO "FDC %d is a National Semiconductor PC87306\n", fdc);
3993 return FDC_87306;
3994 default:
3995 printk(KERN_INFO "FDC %d init: 82078 variant with unknown PARTID=%d.\n",
3996 fdc, reply_buffer[0] >> 5);
3997 return FDC_82078_UNKN;
3999 } /* get_fdc_version */
4001 /* lilo configuration */
4003 static void __init floppy_set_flags(int *ints,int param, int param2)
4005 int i;
4007 for (i=0; i < ARRAY_SIZE(default_drive_params); i++){
4008 if (param)
4009 default_drive_params[i].params.flags |= param2;
4010 else
4011 default_drive_params[i].params.flags &= ~param2;
4013 DPRINT("%s flag 0x%x\n", param2 ? "Setting" : "Clearing", param);
4016 static void __init daring(int *ints,int param, int param2)
4018 int i;
4020 for (i=0; i < ARRAY_SIZE(default_drive_params); i++){
4021 if (param){
4022 default_drive_params[i].params.select_delay = 0;
4023 default_drive_params[i].params.flags |= FD_SILENT_DCL_CLEAR;
4024 } else {
4025 default_drive_params[i].params.select_delay = 2*HZ/100;
4026 default_drive_params[i].params.flags &= ~FD_SILENT_DCL_CLEAR;
4029 DPRINT("Assuming %s floppy hardware\n", param ? "standard" : "broken");
4032 static void __init set_cmos(int *ints, int dummy, int dummy2)
4034 int current_drive=0;
4036 if (ints[0] != 2){
4037 DPRINT("wrong number of parameters for CMOS\n");
4038 return;
4040 current_drive = ints[1];
4041 if (current_drive < 0 || current_drive >= 8){
4042 DPRINT("bad drive for set_cmos\n");
4043 return;
4045 if (current_drive >= 4 && !FDC2)
4046 FDC2 = 0x370;
4047 DP->cmos = ints[2];
4048 DPRINT("setting CMOS code to %d\n", ints[2]);
4051 static struct param_table {
4052 const char *name;
4053 void (*fn)(int *ints, int param, int param2);
4054 int *var;
4055 int def_param;
4056 int param2;
4057 } config_params[]={
4058 { "allowed_drive_mask", 0, &allowed_drive_mask, 0xff, 0}, /* obsolete */
4059 { "all_drives", 0, &allowed_drive_mask, 0xff, 0 }, /* obsolete */
4060 { "asus_pci", 0, &allowed_drive_mask, 0x33, 0},
4062 { "irq", 0, &FLOPPY_IRQ, 6, 0 },
4063 { "dma", 0, &FLOPPY_DMA, 2, 0 },
4065 { "daring", daring, 0, 1, 0},
4067 { "two_fdc", 0, &FDC2, 0x370, 0 },
4068 { "one_fdc", 0, &FDC2, 0, 0 },
4070 { "thinkpad", floppy_set_flags, 0, 1, FD_INVERTED_DCL },
4071 { "broken_dcl", floppy_set_flags, 0, 1, FD_BROKEN_DCL },
4072 { "messages", floppy_set_flags, 0, 1, FTD_MSG },
4073 { "silent_dcl_clear", floppy_set_flags, 0, 1, FD_SILENT_DCL_CLEAR },
4074 { "debug", floppy_set_flags, 0, 1, FD_DEBUG },
4076 { "nodma", 0, &can_use_virtual_dma, 1, 0 },
4077 { "omnibook", 0, &can_use_virtual_dma, 1, 0 },
4078 { "yesdma", 0, &can_use_virtual_dma, 0, 0 },
4080 { "fifo_depth", 0, &fifo_depth, 0xa, 0 },
4081 { "nofifo", 0, &no_fifo, 0x20, 0 },
4082 { "usefifo", 0, &no_fifo, 0, 0 },
4084 { "cmos", set_cmos, 0, 0, 0 },
4085 { "slow", 0, &slow_floppy, 1, 0 },
4087 { "unexpected_interrupts", 0, &print_unex, 1, 0 },
4088 { "no_unexpected_interrupts", 0, &print_unex, 0, 0 },
4089 { "L40SX", 0, &print_unex, 0, 0 }
4092 static int __init floppy_setup(char *str)
4094 int i;
4095 int param;
4096 int ints[11];
4098 str = get_options(str,ARRAY_SIZE(ints),ints);
4099 if (str) {
4100 for (i=0; i< ARRAY_SIZE(config_params); i++){
4101 if (strcmp(str,config_params[i].name) == 0){
4102 if (ints[0])
4103 param = ints[1];
4104 else
4105 param = config_params[i].def_param;
4106 if (config_params[i].fn)
4107 config_params[i].
4108 fn(ints,param,
4109 config_params[i].param2);
4110 if (config_params[i].var) {
4111 DPRINT("%s=%d\n", str, param);
4112 *config_params[i].var = param;
4114 return 1;
4118 if (str) {
4119 DPRINT("unknown floppy option [%s]\n", str);
4121 DPRINT("allowed options are:");
4122 for (i=0; i< ARRAY_SIZE(config_params); i++)
4123 printk(" %s",config_params[i].name);
4124 printk("\n");
4125 } else
4126 DPRINT("botched floppy option\n");
4127 DPRINT("Read linux/Documentation/floppy.txt\n");
4128 return 0;
4131 static int have_no_fdc= -EIO;
4134 int __init floppy_init(void)
4136 int i,unit,drive;
4139 raw_cmd = NULL;
4141 devfs_handle = devfs_mk_dir (NULL, "floppy", NULL);
4142 if (devfs_register_blkdev(MAJOR_NR,"fd",&floppy_fops)) {
4143 printk("Unable to get major %d for floppy\n",MAJOR_NR);
4144 return -EBUSY;
4147 for (i=0; i<256; i++)
4148 if (ITYPE(i))
4149 floppy_sizes[i] = (floppy_type[ITYPE(i)].size+1) >> 1;
4150 else
4151 floppy_sizes[i] = MAX_DISK_SIZE;
4153 blk_size[MAJOR_NR] = floppy_sizes;
4154 blksize_size[MAJOR_NR] = floppy_blocksizes;
4155 blk_init_queue(BLK_DEFAULT_QUEUE(MAJOR_NR), DEVICE_REQUEST);
4156 reschedule_timeout(MAXTIMEOUT, "floppy init", MAXTIMEOUT);
4157 config_types();
4159 for (i = 0; i < N_FDC; i++) {
4160 fdc = i;
4161 CLEARSTRUCT(FDCS);
4162 FDCS->dtr = -1;
4163 FDCS->dor = 0x4;
4164 #ifdef __sparc__
4165 /*sparcs don't have a DOR reset which we can fall back on to*/
4166 FDCS->version = FDC_82072A;
4167 #endif
4170 use_virtual_dma = can_use_virtual_dma & 1;
4171 fdc_state[0].address = FDC1;
4172 if (fdc_state[0].address == -1) {
4173 devfs_unregister_blkdev(MAJOR_NR,"fd");
4174 del_timer(&fd_timeout);
4175 blk_cleanup_queue(BLK_DEFAULT_QUEUE(MAJOR_NR));
4176 return -ENODEV;
4178 #if N_FDC > 1
4179 fdc_state[1].address = FDC2;
4180 #endif
4182 fdc = 0; /* reset fdc in case of unexpected interrupt */
4183 if (floppy_grab_irq_and_dma()){
4184 del_timer(&fd_timeout);
4185 blk_cleanup_queue(BLK_DEFAULT_QUEUE(MAJOR_NR));
4186 devfs_unregister_blkdev(MAJOR_NR,"fd");
4187 del_timer(&fd_timeout);
4188 return -EBUSY;
4191 /* initialise drive state */
4192 for (drive = 0; drive < N_DRIVE; drive++) {
4193 CLEARSTRUCT(UDRS);
4194 CLEARSTRUCT(UDRWE);
4195 USETF(FD_DISK_NEWCHANGE);
4196 USETF(FD_DISK_CHANGED);
4197 USETF(FD_VERIFY);
4198 UDRS->fd_device = -1;
4199 floppy_track_buffer = NULL;
4200 max_buffer_sectors = 0;
4203 for (i = 0; i < N_FDC; i++) {
4204 fdc = i;
4205 FDCS->driver_version = FD_DRIVER_VERSION;
4206 for (unit=0; unit<4; unit++)
4207 FDCS->track[unit] = 0;
4208 if (FDCS->address == -1)
4209 continue;
4210 FDCS->rawcmd = 2;
4211 if (user_reset_fdc(-1,FD_RESET_ALWAYS,0)){
4212 /* free ioports reserved by floppy_grab_irq_and_dma() */
4213 release_region(FDCS->address, 6);
4214 release_region(FDCS->address+7, 1);
4215 FDCS->address = -1;
4216 FDCS->version = FDC_NONE;
4217 continue;
4219 /* Try to determine the floppy controller type */
4220 FDCS->version = get_fdc_version();
4221 if (FDCS->version == FDC_NONE){
4222 /* free ioports reserved by floppy_grab_irq_and_dma() */
4223 release_region(FDCS->address, 6);
4224 release_region(FDCS->address+7, 1);
4225 FDCS->address = -1;
4226 continue;
4228 if (can_use_virtual_dma == 2 && FDCS->version < FDC_82072A)
4229 can_use_virtual_dma = 0;
4231 have_no_fdc = 0;
4232 /* Not all FDCs seem to be able to handle the version command
4233 * properly, so force a reset for the standard FDC clones,
4234 * to avoid interrupt garbage.
4236 user_reset_fdc(-1,FD_RESET_ALWAYS,0);
4238 fdc=0;
4239 del_timer(&fd_timeout);
4240 current_drive = 0;
4241 floppy_release_irq_and_dma();
4242 initialising=0;
4243 if (have_no_fdc)
4245 DPRINT("no floppy controllers found\n");
4246 floppy_tq.routine = (void *)(void *) empty;
4247 mark_bh(IMMEDIATE_BH);
4248 schedule();
4249 if (usage_count)
4250 floppy_release_irq_and_dma();
4251 blk_cleanup_queue(BLK_DEFAULT_QUEUE(MAJOR_NR));
4252 devfs_unregister_blkdev(MAJOR_NR,"fd");
4255 for (drive = 0; drive < N_DRIVE; drive++) {
4256 if (!(allowed_drive_mask & (1 << drive)))
4257 continue;
4258 if (fdc_state[FDC(drive)].version == FDC_NONE)
4259 continue;
4260 for (i = 0; i<NUMBER(floppy_type); i++)
4261 register_disk(NULL, MKDEV(MAJOR_NR,TOMINOR(drive)+i*4),
4262 1, &floppy_fops, 0);
4264 return have_no_fdc;
4267 static spinlock_t floppy_usage_lock = SPIN_LOCK_UNLOCKED;
4269 static int floppy_grab_irq_and_dma(void)
4271 unsigned long flags;
4273 spin_lock_irqsave(&floppy_usage_lock, flags);
4274 if (usage_count++){
4275 spin_unlock_irqrestore(&floppy_usage_lock, flags);
4276 return 0;
4278 spin_unlock_irqrestore(&floppy_usage_lock, flags);
4279 MOD_INC_USE_COUNT;
4280 if (fd_request_irq()) {
4281 DPRINT("Unable to grab IRQ%d for the floppy driver\n",
4282 FLOPPY_IRQ);
4283 MOD_DEC_USE_COUNT;
4284 spin_lock_irqsave(&floppy_usage_lock, flags);
4285 usage_count--;
4286 spin_unlock_irqrestore(&floppy_usage_lock, flags);
4287 return -1;
4289 if (fd_request_dma()) {
4290 DPRINT("Unable to grab DMA%d for the floppy driver\n",
4291 FLOPPY_DMA);
4292 fd_free_irq();
4293 MOD_DEC_USE_COUNT;
4294 spin_lock_irqsave(&floppy_usage_lock, flags);
4295 usage_count--;
4296 spin_unlock_irqrestore(&floppy_usage_lock, flags);
4297 return -1;
4300 for (fdc=0; fdc< N_FDC; fdc++){
4301 if (FDCS->address != -1){
4302 if (check_region(FDCS->address, 6) < 0 ||
4303 check_region(FDCS->address+7, 1) < 0) {
4304 DPRINT("Floppy io-port 0x%04lx in use\n", FDCS->address);
4305 fd_free_irq();
4306 fd_free_dma();
4307 while(--fdc >= 0) {
4308 release_region(FDCS->address, 6);
4309 release_region(FDCS->address+7, 1);
4311 MOD_DEC_USE_COUNT;
4312 spin_lock_irqsave(&floppy_usage_lock, flags);
4313 usage_count--;
4314 spin_unlock_irqrestore(&floppy_usage_lock, flags);
4315 return -1;
4317 request_region(FDCS->address, 6, "floppy");
4318 request_region(FDCS->address+7, 1, "floppy DIR");
4319 /* address + 6 is reserved, and may be taken by IDE.
4320 * Unfortunately, Adaptec doesn't know this :-(, */
4323 for (fdc=0; fdc< N_FDC; fdc++){
4324 if (FDCS->address != -1){
4325 reset_fdc_info(1);
4326 fd_outb(FDCS->dor, FD_DOR);
4329 fdc = 0;
4330 set_dor(0, ~0, 8); /* avoid immediate interrupt */
4332 for (fdc = 0; fdc < N_FDC; fdc++)
4333 if (FDCS->address != -1)
4334 fd_outb(FDCS->dor, FD_DOR);
4336 * The driver will try and free resources and relies on us
4337 * to know if they were allocated or not.
4339 fdc = 0;
4340 irqdma_allocated = 1;
4341 return 0;
4344 static void floppy_release_irq_and_dma(void)
4346 int old_fdc;
4347 #ifdef FLOPPY_SANITY_CHECK
4348 #ifndef __sparc__
4349 int drive;
4350 #endif
4351 #endif
4352 long tmpsize;
4353 unsigned long tmpaddr;
4354 unsigned long flags;
4356 spin_lock_irqsave(&floppy_usage_lock, flags);
4357 if (--usage_count){
4358 spin_unlock_irqrestore(&floppy_usage_lock, flags);
4359 return;
4361 spin_unlock_irqrestore(&floppy_usage_lock, flags);
4362 if(irqdma_allocated)
4364 fd_disable_dma();
4365 fd_free_dma();
4366 fd_free_irq();
4367 irqdma_allocated=0;
4369 set_dor(0, ~0, 8);
4370 #if N_FDC > 1
4371 set_dor(1, ~8, 0);
4372 #endif
4373 floppy_enable_hlt();
4375 if (floppy_track_buffer && max_buffer_sectors) {
4376 tmpsize = max_buffer_sectors*1024;
4377 tmpaddr = (unsigned long)floppy_track_buffer;
4378 floppy_track_buffer = NULL;
4379 max_buffer_sectors = 0;
4380 buffer_min = buffer_max = -1;
4381 fd_dma_mem_free(tmpaddr, tmpsize);
4384 #ifdef FLOPPY_SANITY_CHECK
4385 #ifndef __sparc__
4386 for (drive=0; drive < N_FDC * 4; drive++)
4387 if (timer_pending(motor_off_timer + drive))
4388 printk("motor off timer %d still active\n", drive);
4389 #endif
4391 if (timer_pending(&fd_timeout))
4392 printk("floppy timer still active:%s\n", timeout_message);
4393 if (timer_pending(&fd_timer))
4394 printk("auxiliary floppy timer still active\n");
4395 if (floppy_tq.sync)
4396 printk("task queue still active\n");
4397 #endif
4398 old_fdc = fdc;
4399 for (fdc = 0; fdc < N_FDC; fdc++)
4400 if (FDCS->address != -1) {
4401 release_region(FDCS->address, 6);
4402 release_region(FDCS->address+7, 1);
4404 fdc = old_fdc;
4405 MOD_DEC_USE_COUNT;
4409 #ifdef MODULE
4411 char *floppy;
4413 static void __init parse_floppy_cfg_string(char *cfg)
4415 char *ptr;
4417 while(*cfg) {
4418 for(ptr = cfg;*cfg && *cfg != ' ' && *cfg != '\t'; cfg++);
4419 if (*cfg) {
4420 *cfg = '\0';
4421 cfg++;
4423 if (*ptr)
4424 floppy_setup(ptr);
4428 int init_module(void)
4430 printk(KERN_INFO "inserting floppy driver for " UTS_RELEASE "\n");
4432 if (floppy)
4433 parse_floppy_cfg_string(floppy);
4434 return floppy_init();
4437 void cleanup_module(void)
4439 int dummy;
4441 devfs_unregister (devfs_handle);
4442 devfs_unregister_blkdev(MAJOR_NR, "fd");
4444 blk_cleanup_queue(BLK_DEFAULT_QUEUE(MAJOR_NR));
4445 /* eject disk, if any */
4446 dummy = fd_eject(0);
4449 MODULE_PARM(floppy,"s");
4450 MODULE_PARM(FLOPPY_IRQ,"i");
4451 MODULE_PARM(FLOPPY_DMA,"i");
4452 MODULE_AUTHOR("Alain L. Knaff");
4453 MODULE_SUPPORTED_DEVICE("fd");
4455 #else
4457 __setup ("floppy=", floppy_setup);
4459 /* eject the boot floppy (if we need the drive for a different root floppy) */
4460 /* This should only be called at boot time when we're sure that there's no
4461 * resource contention. */
4462 void floppy_eject(void)
4464 int dummy;
4465 if (have_no_fdc)
4466 return;
4467 if(floppy_grab_irq_and_dma()==0)
4469 lock_fdc(MAXTIMEOUT,0);
4470 dummy=fd_eject(0);
4471 process_fd_request();
4472 floppy_release_irq_and_dma();
4475 #endif