Import 2.4.0-test3pre5
[davej-history.git] / drivers / block / floppy.c
blobb8426a6ddfc4a5acfb22654c28e124cf93b1f3d9
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 (!DRS->first_read_date)
2343 DRS->first_read_date = jiffies;
2345 nr_sectors = 0;
2346 CODE2SIZE;
2348 if (ST1 & ST1_EOC)
2349 eoc = 1;
2350 else
2351 eoc = 0;
2353 if (COMMAND & 0x80)
2354 heads = 2;
2355 else
2356 heads = 1;
2358 nr_sectors = (((R_TRACK-TRACK) * heads +
2359 R_HEAD-HEAD) * SECT_PER_TRACK +
2360 R_SECTOR-SECTOR + eoc) << SIZECODE >> 2;
2362 #ifdef FLOPPY_SANITY_CHECK
2363 if (nr_sectors / ssize >
2364 (in_sector_offset + current_count_sectors + ssize - 1) / ssize) {
2365 DPRINT("long rw: %x instead of %lx\n",
2366 nr_sectors, current_count_sectors);
2367 printk("rs=%d s=%d\n", R_SECTOR, SECTOR);
2368 printk("rh=%d h=%d\n", R_HEAD, HEAD);
2369 printk("rt=%d t=%d\n", R_TRACK, TRACK);
2370 printk("heads=%d eoc=%d\n", heads, eoc);
2371 printk("spt=%d st=%d ss=%d\n", SECT_PER_TRACK,
2372 sector_t, ssize);
2373 printk("in_sector_offset=%d\n", in_sector_offset);
2375 #endif
2377 nr_sectors -= in_sector_offset;
2378 INFBOUND(nr_sectors,0);
2379 SUPBOUND(current_count_sectors, nr_sectors);
2381 switch (interpret_errors()){
2382 case 2:
2383 cont->redo();
2384 return;
2385 case 1:
2386 if (!current_count_sectors){
2387 cont->error();
2388 cont->redo();
2389 return;
2391 break;
2392 case 0:
2393 if (!current_count_sectors){
2394 cont->redo();
2395 return;
2397 current_type[current_drive] = _floppy;
2398 floppy_sizes[TOMINOR(current_drive) ]=
2399 (_floppy->size+1)>>1;
2400 break;
2403 if (probing) {
2404 if (DP->flags & FTD_MSG)
2405 DPRINT("Auto-detected floppy type %s in fd%d\n",
2406 _floppy->name,current_drive);
2407 current_type[current_drive] = _floppy;
2408 floppy_sizes[TOMINOR(current_drive)] = (_floppy->size+1) >> 1;
2409 probing = 0;
2412 if (CT(COMMAND) != FD_READ ||
2413 raw_cmd->kernel_data == CURRENT->buffer){
2414 /* transfer directly from buffer */
2415 cont->done(1);
2416 } else if (CT(COMMAND) == FD_READ){
2417 buffer_track = raw_cmd->track;
2418 buffer_drive = current_drive;
2419 INFBOUND(buffer_max, nr_sectors + sector_t);
2421 cont->redo();
2424 /* Compute maximal contiguous buffer size. */
2425 static int buffer_chain_size(void)
2427 struct buffer_head *bh;
2428 int size;
2429 char *base;
2431 base = CURRENT->buffer;
2432 size = CURRENT->current_nr_sectors << 9;
2433 bh = CURRENT->bh;
2435 if (bh){
2436 bh = bh->b_reqnext;
2437 while (bh && bh->b_data == base + size){
2438 size += bh->b_size;
2439 bh = bh->b_reqnext;
2442 return size >> 9;
2445 /* Compute the maximal transfer size */
2446 static int transfer_size(int ssize, int max_sector, int max_size)
2448 SUPBOUND(max_sector, sector_t + max_size);
2450 /* alignment */
2451 max_sector -= (max_sector % _floppy->sect) % ssize;
2453 /* transfer size, beginning not aligned */
2454 current_count_sectors = max_sector - sector_t ;
2456 return max_sector;
2460 * Move data from/to the track buffer to/from the buffer cache.
2462 static void copy_buffer(int ssize, int max_sector, int max_sector_2)
2464 int remaining; /* number of transferred 512-byte sectors */
2465 struct buffer_head *bh;
2466 char *buffer, *dma_buffer;
2467 int size;
2469 max_sector = transfer_size(ssize,
2470 minimum(max_sector, max_sector_2),
2471 CURRENT->nr_sectors);
2473 if (current_count_sectors <= 0 && CT(COMMAND) == FD_WRITE &&
2474 buffer_max > sector_t + CURRENT->nr_sectors)
2475 current_count_sectors = minimum(buffer_max - sector_t,
2476 CURRENT->nr_sectors);
2478 remaining = current_count_sectors << 9;
2479 #ifdef FLOPPY_SANITY_CHECK
2480 if ((remaining >> 9) > CURRENT->nr_sectors &&
2481 CT(COMMAND) == FD_WRITE){
2482 DPRINT("in copy buffer\n");
2483 printk("current_count_sectors=%ld\n", current_count_sectors);
2484 printk("remaining=%d\n", remaining >> 9);
2485 printk("CURRENT->nr_sectors=%ld\n",CURRENT->nr_sectors);
2486 printk("CURRENT->current_nr_sectors=%ld\n",
2487 CURRENT->current_nr_sectors);
2488 printk("max_sector=%d\n", max_sector);
2489 printk("ssize=%d\n", ssize);
2491 #endif
2493 buffer_max = maximum(max_sector, buffer_max);
2495 dma_buffer = floppy_track_buffer + ((sector_t - buffer_min) << 9);
2497 bh = CURRENT->bh;
2498 size = CURRENT->current_nr_sectors << 9;
2499 buffer = CURRENT->buffer;
2501 while (remaining > 0){
2502 SUPBOUND(size, remaining);
2503 #ifdef FLOPPY_SANITY_CHECK
2504 if (dma_buffer + size >
2505 floppy_track_buffer + (max_buffer_sectors << 10) ||
2506 dma_buffer < floppy_track_buffer){
2507 DPRINT("buffer overrun in copy buffer %d\n",
2508 (int) ((floppy_track_buffer - dma_buffer) >>9));
2509 printk("sector_t=%d buffer_min=%d\n",
2510 sector_t, buffer_min);
2511 printk("current_count_sectors=%ld\n",
2512 current_count_sectors);
2513 if (CT(COMMAND) == FD_READ)
2514 printk("read\n");
2515 if (CT(COMMAND) == FD_READ)
2516 printk("write\n");
2517 break;
2519 if (((unsigned long)buffer) % 512)
2520 DPRINT("%p buffer not aligned\n", buffer);
2521 #endif
2522 if (CT(COMMAND) == FD_READ)
2523 memcpy(buffer, dma_buffer, size);
2524 else
2525 memcpy(dma_buffer, buffer, size);
2526 remaining -= size;
2527 if (!remaining)
2528 break;
2530 dma_buffer += size;
2531 bh = bh->b_reqnext;
2532 #ifdef FLOPPY_SANITY_CHECK
2533 if (!bh){
2534 DPRINT("bh=null in copy buffer after copy\n");
2535 break;
2537 #endif
2538 size = bh->b_size;
2539 buffer = bh->b_data;
2541 #ifdef FLOPPY_SANITY_CHECK
2542 if (remaining){
2543 if (remaining > 0)
2544 max_sector -= remaining >> 9;
2545 DPRINT("weirdness: remaining %d\n", remaining>>9);
2547 #endif
2550 #if 0
2551 static inline int check_dma_crossing(char *start,
2552 unsigned long length, char *message)
2554 if (CROSS_64KB(start, length)) {
2555 printk("DMA xfer crosses 64KB boundary in %s %p-%p\n",
2556 message, start, start+length);
2557 return 1;
2558 } else
2559 return 0;
2561 #endif
2563 /* work around a bug in pseudo DMA
2564 * (on some FDCs) pseudo DMA does not stop when the CPU stops
2565 * sending data. Hence we need a different way to signal the
2566 * transfer length: We use SECT_PER_TRACK. Unfortunately, this
2567 * does not work with MT, hence we can only transfer one head at
2568 * a time
2570 static void virtualdmabug_workaround(void)
2572 int hard_sectors, end_sector;
2574 if(CT(COMMAND) == FD_WRITE) {
2575 COMMAND &= ~0x80; /* switch off multiple track mode */
2577 hard_sectors = raw_cmd->length >> (7 + SIZECODE);
2578 end_sector = SECTOR + hard_sectors - 1;
2579 #ifdef FLOPPY_SANITY_CHECK
2580 if(end_sector > SECT_PER_TRACK) {
2581 printk("too many sectors %d > %d\n",
2582 end_sector, SECT_PER_TRACK);
2583 return;
2585 #endif
2586 SECT_PER_TRACK = end_sector; /* make sure SECT_PER_TRACK points
2587 * to end of transfer */
2592 * Formulate a read/write request.
2593 * this routine decides where to load the data (directly to buffer, or to
2594 * tmp floppy area), how much data to load (the size of the buffer, the whole
2595 * track, or a single sector)
2596 * All floppy_track_buffer handling goes in here. If we ever add track buffer
2597 * allocation on the fly, it should be done here. No other part should need
2598 * modification.
2601 static int make_raw_rw_request(void)
2603 int aligned_sector_t;
2604 int max_sector, max_size, tracksize, ssize;
2606 if(max_buffer_sectors == 0) {
2607 printk("VFS: Block I/O scheduled on unopened device\n");
2608 return 0;
2611 set_fdc(DRIVE(CURRENT->rq_dev));
2613 raw_cmd = &default_raw_cmd;
2614 raw_cmd->flags = FD_RAW_SPIN | FD_RAW_NEED_DISK | FD_RAW_NEED_DISK |
2615 FD_RAW_NEED_SEEK;
2616 raw_cmd->cmd_count = NR_RW;
2617 if (CURRENT->cmd == READ){
2618 raw_cmd->flags |= FD_RAW_READ;
2619 COMMAND = FM_MODE(_floppy,FD_READ);
2620 } else if (CURRENT->cmd == WRITE){
2621 raw_cmd->flags |= FD_RAW_WRITE;
2622 COMMAND = FM_MODE(_floppy,FD_WRITE);
2623 } else {
2624 DPRINT("make_raw_rw_request: unknown command\n");
2625 return 0;
2628 max_sector = _floppy->sect * _floppy->head;
2630 TRACK = CURRENT->sector / max_sector;
2631 sector_t = CURRENT->sector % max_sector;
2632 if (_floppy->track && TRACK >= _floppy->track) {
2633 if (CURRENT->current_nr_sectors & 1) {
2634 current_count_sectors = 1;
2635 return 1;
2636 } else
2637 return 0;
2639 HEAD = sector_t / _floppy->sect;
2641 if (((_floppy->stretch & FD_SWAPSIDES) || TESTF(FD_NEED_TWADDLE)) &&
2642 sector_t < _floppy->sect)
2643 max_sector = _floppy->sect;
2645 /* 2M disks have phantom sectors on the first track */
2646 if ((_floppy->rate & FD_2M) && (!TRACK) && (!HEAD)){
2647 max_sector = 2 * _floppy->sect / 3;
2648 if (sector_t >= max_sector){
2649 current_count_sectors = minimum(_floppy->sect - sector_t,
2650 CURRENT->nr_sectors);
2651 return 1;
2653 SIZECODE = 2;
2654 } else
2655 SIZECODE = FD_SIZECODE(_floppy);
2656 raw_cmd->rate = _floppy->rate & 0x43;
2657 if ((_floppy->rate & FD_2M) &&
2658 (TRACK || HEAD) &&
2659 raw_cmd->rate == 2)
2660 raw_cmd->rate = 1;
2662 if (SIZECODE)
2663 SIZECODE2 = 0xff;
2664 else
2665 SIZECODE2 = 0x80;
2666 raw_cmd->track = TRACK << STRETCH(_floppy);
2667 DR_SELECT = UNIT(current_drive) + PH_HEAD(_floppy,HEAD);
2668 GAP = _floppy->gap;
2669 CODE2SIZE;
2670 SECT_PER_TRACK = _floppy->sect << 2 >> SIZECODE;
2671 SECTOR = ((sector_t % _floppy->sect) << 2 >> SIZECODE) + 1;
2673 /* tracksize describes the size which can be filled up with sectors
2674 * of size ssize.
2676 tracksize = _floppy->sect - _floppy->sect % ssize;
2677 if (tracksize < _floppy->sect){
2678 SECT_PER_TRACK ++;
2679 if (tracksize <= sector_t % _floppy->sect)
2680 SECTOR--;
2682 /* if we are beyond tracksize, fill up using smaller sectors */
2683 while (tracksize <= sector_t % _floppy->sect){
2684 while(tracksize + ssize > _floppy->sect){
2685 SIZECODE--;
2686 ssize >>= 1;
2688 SECTOR++; SECT_PER_TRACK ++;
2689 tracksize += ssize;
2691 max_sector = HEAD * _floppy->sect + tracksize;
2692 } else if (!TRACK && !HEAD && !(_floppy->rate & FD_2M) && probing) {
2693 max_sector = _floppy->sect;
2694 } else if (!HEAD && CT(COMMAND) == FD_WRITE) {
2695 /* for virtual DMA bug workaround */
2696 max_sector = _floppy->sect;
2699 in_sector_offset = (sector_t % _floppy->sect) % ssize;
2700 aligned_sector_t = sector_t - in_sector_offset;
2701 max_size = CURRENT->nr_sectors;
2702 if ((raw_cmd->track == buffer_track) &&
2703 (current_drive == buffer_drive) &&
2704 (sector_t >= buffer_min) && (sector_t < buffer_max)) {
2705 /* data already in track buffer */
2706 if (CT(COMMAND) == FD_READ) {
2707 copy_buffer(1, max_sector, buffer_max);
2708 return 1;
2710 } else if (in_sector_offset || CURRENT->nr_sectors < ssize){
2711 if (CT(COMMAND) == FD_WRITE){
2712 if (sector_t + CURRENT->nr_sectors > ssize &&
2713 sector_t + CURRENT->nr_sectors < ssize + ssize)
2714 max_size = ssize + ssize;
2715 else
2716 max_size = ssize;
2718 raw_cmd->flags &= ~FD_RAW_WRITE;
2719 raw_cmd->flags |= FD_RAW_READ;
2720 COMMAND = FM_MODE(_floppy,FD_READ);
2721 } else if ((unsigned long)CURRENT->buffer < MAX_DMA_ADDRESS) {
2722 unsigned long dma_limit;
2723 int direct, indirect;
2725 indirect= transfer_size(ssize,max_sector,max_buffer_sectors*2) -
2726 sector_t;
2729 * Do NOT use minimum() here---MAX_DMA_ADDRESS is 64 bits wide
2730 * on a 64 bit machine!
2732 max_size = buffer_chain_size();
2733 dma_limit = (MAX_DMA_ADDRESS - ((unsigned long) CURRENT->buffer)) >> 9;
2734 if ((unsigned long) max_size > dma_limit) {
2735 max_size = dma_limit;
2737 /* 64 kb boundaries */
2738 if (CROSS_64KB(CURRENT->buffer, max_size << 9))
2739 max_size = (K_64 -
2740 ((unsigned long)CURRENT->buffer) % K_64)>>9;
2741 direct = transfer_size(ssize,max_sector,max_size) - sector_t;
2743 * We try to read tracks, but if we get too many errors, we
2744 * go back to reading just one sector at a time.
2746 * This means we should be able to read a sector even if there
2747 * are other bad sectors on this track.
2749 if (!direct ||
2750 (indirect * 2 > direct * 3 &&
2751 *errors < DP->max_errors.read_track &&
2752 /*!TESTF(FD_NEED_TWADDLE) &&*/
2753 ((!probing || (DP->read_track&(1<<DRS->probed_format)))))){
2754 max_size = CURRENT->nr_sectors;
2755 } else {
2756 raw_cmd->kernel_data = CURRENT->buffer;
2757 raw_cmd->length = current_count_sectors << 9;
2758 if (raw_cmd->length == 0){
2759 DPRINT("zero dma transfer attempted from make_raw_request\n");
2760 DPRINT("indirect=%d direct=%d sector_t=%d",
2761 indirect, direct, sector_t);
2762 return 0;
2764 /* check_dma_crossing(raw_cmd->kernel_data,
2765 raw_cmd->length,
2766 "end of make_raw_request [1]");*/
2768 virtualdmabug_workaround();
2769 return 2;
2773 if (CT(COMMAND) == FD_READ)
2774 max_size = max_sector; /* unbounded */
2776 /* claim buffer track if needed */
2777 if (buffer_track != raw_cmd->track || /* bad track */
2778 buffer_drive !=current_drive || /* bad drive */
2779 sector_t > buffer_max ||
2780 sector_t < buffer_min ||
2781 ((CT(COMMAND) == FD_READ ||
2782 (!in_sector_offset && CURRENT->nr_sectors >= ssize))&&
2783 max_sector > 2 * max_buffer_sectors + buffer_min &&
2784 max_size + sector_t > 2 * max_buffer_sectors + buffer_min)
2785 /* not enough space */){
2786 buffer_track = -1;
2787 buffer_drive = current_drive;
2788 buffer_max = buffer_min = aligned_sector_t;
2790 raw_cmd->kernel_data = floppy_track_buffer +
2791 ((aligned_sector_t-buffer_min)<<9);
2793 if (CT(COMMAND) == FD_WRITE){
2794 /* copy write buffer to track buffer.
2795 * if we get here, we know that the write
2796 * is either aligned or the data already in the buffer
2797 * (buffer will be overwritten) */
2798 #ifdef FLOPPY_SANITY_CHECK
2799 if (in_sector_offset && buffer_track == -1)
2800 DPRINT("internal error offset !=0 on write\n");
2801 #endif
2802 buffer_track = raw_cmd->track;
2803 buffer_drive = current_drive;
2804 copy_buffer(ssize, max_sector, 2*max_buffer_sectors+buffer_min);
2805 } else
2806 transfer_size(ssize, max_sector,
2807 2*max_buffer_sectors+buffer_min-aligned_sector_t);
2809 /* round up current_count_sectors to get dma xfer size */
2810 raw_cmd->length = in_sector_offset+current_count_sectors;
2811 raw_cmd->length = ((raw_cmd->length -1)|(ssize-1))+1;
2812 raw_cmd->length <<= 9;
2813 #ifdef FLOPPY_SANITY_CHECK
2814 /*check_dma_crossing(raw_cmd->kernel_data, raw_cmd->length,
2815 "end of make_raw_request");*/
2816 if ((raw_cmd->length < current_count_sectors << 9) ||
2817 (raw_cmd->kernel_data != CURRENT->buffer &&
2818 CT(COMMAND) == FD_WRITE &&
2819 (aligned_sector_t + (raw_cmd->length >> 9) > buffer_max ||
2820 aligned_sector_t < buffer_min)) ||
2821 raw_cmd->length % (128 << SIZECODE) ||
2822 raw_cmd->length <= 0 || current_count_sectors <= 0){
2823 DPRINT("fractionary current count b=%lx s=%lx\n",
2824 raw_cmd->length, current_count_sectors);
2825 if (raw_cmd->kernel_data != CURRENT->buffer)
2826 printk("addr=%d, length=%ld\n",
2827 (int) ((raw_cmd->kernel_data -
2828 floppy_track_buffer) >> 9),
2829 current_count_sectors);
2830 printk("st=%d ast=%d mse=%d msi=%d\n",
2831 sector_t, aligned_sector_t, max_sector, max_size);
2832 printk("ssize=%x SIZECODE=%d\n", ssize, SIZECODE);
2833 printk("command=%x SECTOR=%d HEAD=%d, TRACK=%d\n",
2834 COMMAND, SECTOR, HEAD, TRACK);
2835 printk("buffer drive=%d\n", buffer_drive);
2836 printk("buffer track=%d\n", buffer_track);
2837 printk("buffer_min=%d\n", buffer_min);
2838 printk("buffer_max=%d\n", buffer_max);
2839 return 0;
2842 if (raw_cmd->kernel_data != CURRENT->buffer){
2843 if (raw_cmd->kernel_data < floppy_track_buffer ||
2844 current_count_sectors < 0 ||
2845 raw_cmd->length < 0 ||
2846 raw_cmd->kernel_data + raw_cmd->length >
2847 floppy_track_buffer + (max_buffer_sectors << 10)){
2848 DPRINT("buffer overrun in schedule dma\n");
2849 printk("sector_t=%d buffer_min=%d current_count=%ld\n",
2850 sector_t, buffer_min,
2851 raw_cmd->length >> 9);
2852 printk("current_count_sectors=%ld\n",
2853 current_count_sectors);
2854 if (CT(COMMAND) == FD_READ)
2855 printk("read\n");
2856 if (CT(COMMAND) == FD_READ)
2857 printk("write\n");
2858 return 0;
2860 } else if (raw_cmd->length > CURRENT->nr_sectors << 9 ||
2861 current_count_sectors > CURRENT->nr_sectors){
2862 DPRINT("buffer overrun in direct transfer\n");
2863 return 0;
2864 } else if (raw_cmd->length < current_count_sectors << 9){
2865 DPRINT("more sectors than bytes\n");
2866 printk("bytes=%ld\n", raw_cmd->length >> 9);
2867 printk("sectors=%ld\n", current_count_sectors);
2869 if (raw_cmd->length == 0){
2870 DPRINT("zero dma transfer attempted from make_raw_request\n");
2871 return 0;
2873 #endif
2875 virtualdmabug_workaround();
2876 return 2;
2879 static void redo_fd_request(void)
2881 #define REPEAT {request_done(0); continue; }
2882 kdev_t device;
2883 int tmp;
2885 lastredo = jiffies;
2886 if (current_drive < N_DRIVE)
2887 floppy_off(current_drive);
2889 if (!QUEUE_EMPTY && CURRENT->rq_status == RQ_INACTIVE){
2890 CLEAR_INTR;
2891 unlock_fdc();
2892 return;
2895 while(1){
2896 if (QUEUE_EMPTY) {
2897 CLEAR_INTR;
2898 unlock_fdc();
2899 return;
2901 if (MAJOR(CURRENT->rq_dev) != MAJOR_NR)
2902 panic(DEVICE_NAME ": request list destroyed");
2903 if (CURRENT->bh && !buffer_locked(CURRENT->bh))
2904 panic(DEVICE_NAME ": block not locked");
2906 device = CURRENT->rq_dev;
2907 set_fdc(DRIVE(device));
2908 reschedule_timeout(CURRENTD, "redo fd request", 0);
2910 set_floppy(device);
2911 raw_cmd = & default_raw_cmd;
2912 raw_cmd->flags = 0;
2913 if (start_motor(redo_fd_request)) return;
2914 disk_change(current_drive);
2915 if (test_bit(current_drive, &fake_change) ||
2916 TESTF(FD_DISK_CHANGED)){
2917 DPRINT("disk absent or changed during operation\n");
2918 REPEAT;
2920 if (!_floppy) { /* Autodetection */
2921 if (!probing){
2922 DRS->probed_format = 0;
2923 if (next_valid_format()){
2924 DPRINT("no autodetectable formats\n");
2925 _floppy = NULL;
2926 REPEAT;
2929 probing = 1;
2930 _floppy = floppy_type+DP->autodetect[DRS->probed_format];
2931 } else
2932 probing = 0;
2933 errors = & (CURRENT->errors);
2934 tmp = make_raw_rw_request();
2935 if (tmp < 2){
2936 request_done(tmp);
2937 continue;
2940 if (TESTF(FD_NEED_TWADDLE))
2941 twaddle();
2942 schedule_bh( (void *)(void *) floppy_start);
2943 #ifdef DEBUGT
2944 debugt("queue fd request");
2945 #endif
2946 return;
2948 #undef REPEAT
2951 static struct cont_t rw_cont={
2952 rw_interrupt,
2953 redo_fd_request,
2954 bad_flp_intr,
2955 request_done };
2957 static void process_fd_request(void)
2959 cont = &rw_cont;
2960 schedule_bh( (void *)(void *) redo_fd_request);
2963 static void do_fd_request(request_queue_t * q)
2965 if(max_buffer_sectors == 0) {
2966 printk("VFS: do_fd_request called on non-open device\n");
2967 return;
2970 if (usage_count == 0) {
2971 printk("warning: usage count=0, CURRENT=%p exiting\n", CURRENT);
2972 printk("sect=%ld cmd=%d\n", CURRENT->sector, CURRENT->cmd);
2973 return;
2975 if (fdc_busy){
2976 /* fdc busy, this new request will be treated when the
2977 current one is done */
2978 is_alive("do fd request, old request running");
2979 return;
2981 lock_fdc(MAXTIMEOUT,0);
2982 process_fd_request();
2983 is_alive("do fd request");
2986 static struct cont_t poll_cont={
2987 success_and_wakeup,
2988 floppy_ready,
2989 generic_failure,
2990 generic_done };
2992 static int poll_drive(int interruptible, int flag)
2994 int ret;
2995 /* no auto-sense, just clear dcl */
2996 raw_cmd = &default_raw_cmd;
2997 raw_cmd->flags= flag;
2998 raw_cmd->track=0;
2999 raw_cmd->cmd_count=0;
3000 cont = &poll_cont;
3001 #ifdef DCL_DEBUG
3002 if (DP->flags & FD_DEBUG){
3003 DPRINT("setting NEWCHANGE in poll_drive\n");
3005 #endif
3006 SETF(FD_DISK_NEWCHANGE);
3007 WAIT(floppy_ready);
3008 return ret;
3012 * User triggered reset
3013 * ====================
3016 static void reset_intr(void)
3018 printk("weird, reset interrupt called\n");
3021 static struct cont_t reset_cont={
3022 reset_intr,
3023 success_and_wakeup,
3024 generic_failure,
3025 generic_done };
3027 static int user_reset_fdc(int drive, int arg, int interruptible)
3029 int ret;
3031 ret=0;
3032 LOCK_FDC(drive,interruptible);
3033 if (arg == FD_RESET_ALWAYS)
3034 FDCS->reset=1;
3035 if (FDCS->reset){
3036 cont = &reset_cont;
3037 WAIT(reset_fdc);
3039 process_fd_request();
3040 return ret;
3044 * Misc Ioctl's and support
3045 * ========================
3047 static inline int fd_copyout(void *param, const void *address, unsigned long size)
3049 return copy_to_user(param,address, size) ? -EFAULT : 0;
3052 static inline int fd_copyin(void *param, void *address, unsigned long size)
3054 return copy_from_user(address, param, size) ? -EFAULT : 0;
3057 #define _COPYOUT(x) (copy_to_user((void *)param, &(x), sizeof(x)) ? -EFAULT : 0)
3058 #define _COPYIN(x) (copy_from_user(&(x), (void *)param, sizeof(x)) ? -EFAULT : 0)
3060 #define COPYOUT(x) ECALL(_COPYOUT(x))
3061 #define COPYIN(x) ECALL(_COPYIN(x))
3063 static inline const char *drive_name(int type, int drive)
3065 struct floppy_struct *floppy;
3067 if (type)
3068 floppy = floppy_type + type;
3069 else {
3070 if (UDP->native_format)
3071 floppy = floppy_type + UDP->native_format;
3072 else
3073 return "(null)";
3075 if (floppy->name)
3076 return floppy->name;
3077 else
3078 return "(null)";
3082 /* raw commands */
3083 static void raw_cmd_done(int flag)
3085 int i;
3087 if (!flag) {
3088 raw_cmd->flags |= FD_RAW_FAILURE;
3089 raw_cmd->flags |= FD_RAW_HARDFAILURE;
3090 } else {
3091 raw_cmd->reply_count = inr;
3092 if (raw_cmd->reply_count > MAX_REPLIES)
3093 raw_cmd->reply_count=0;
3094 for (i=0; i< raw_cmd->reply_count; i++)
3095 raw_cmd->reply[i] = reply_buffer[i];
3097 if (raw_cmd->flags & (FD_RAW_READ | FD_RAW_WRITE))
3099 unsigned long flags;
3100 flags=claim_dma_lock();
3101 raw_cmd->length = fd_get_dma_residue();
3102 release_dma_lock(flags);
3105 if ((raw_cmd->flags & FD_RAW_SOFTFAILURE) &&
3106 (!raw_cmd->reply_count || (raw_cmd->reply[0] & 0xc0)))
3107 raw_cmd->flags |= FD_RAW_FAILURE;
3109 if (disk_change(current_drive))
3110 raw_cmd->flags |= FD_RAW_DISK_CHANGE;
3111 else
3112 raw_cmd->flags &= ~FD_RAW_DISK_CHANGE;
3113 if (raw_cmd->flags & FD_RAW_NO_MOTOR_AFTER)
3114 motor_off_callback(current_drive);
3116 if (raw_cmd->next &&
3117 (!(raw_cmd->flags & FD_RAW_FAILURE) ||
3118 !(raw_cmd->flags & FD_RAW_STOP_IF_FAILURE)) &&
3119 ((raw_cmd->flags & FD_RAW_FAILURE) ||
3120 !(raw_cmd->flags &FD_RAW_STOP_IF_SUCCESS))) {
3121 raw_cmd = raw_cmd->next;
3122 return;
3125 generic_done(flag);
3129 static struct cont_t raw_cmd_cont={
3130 success_and_wakeup,
3131 floppy_start,
3132 generic_failure,
3133 raw_cmd_done
3136 static inline int raw_cmd_copyout(int cmd, char *param,
3137 struct floppy_raw_cmd *ptr)
3139 int ret;
3141 while(ptr) {
3142 COPYOUT(*ptr);
3143 param += sizeof(struct floppy_raw_cmd);
3144 if ((ptr->flags & FD_RAW_READ) && ptr->buffer_length){
3145 if (ptr->length>=0 && ptr->length<=ptr->buffer_length)
3146 ECALL(fd_copyout(ptr->data,
3147 ptr->kernel_data,
3148 ptr->buffer_length -
3149 ptr->length));
3151 ptr = ptr->next;
3153 return 0;
3157 static void raw_cmd_free(struct floppy_raw_cmd **ptr)
3159 struct floppy_raw_cmd *next,*this;
3161 this = *ptr;
3162 *ptr = 0;
3163 while(this) {
3164 if (this->buffer_length) {
3165 fd_dma_mem_free((unsigned long)this->kernel_data,
3166 this->buffer_length);
3167 this->buffer_length = 0;
3169 next = this->next;
3170 kfree(this);
3171 this = next;
3176 static inline int raw_cmd_copyin(int cmd, char *param,
3177 struct floppy_raw_cmd **rcmd)
3179 struct floppy_raw_cmd *ptr;
3180 int ret;
3181 int i;
3183 *rcmd = 0;
3184 while(1) {
3185 ptr = (struct floppy_raw_cmd *)
3186 kmalloc(sizeof(struct floppy_raw_cmd), GFP_USER);
3187 if (!ptr)
3188 return -ENOMEM;
3189 *rcmd = ptr;
3190 COPYIN(*ptr);
3191 ptr->next = 0;
3192 ptr->buffer_length = 0;
3193 param += sizeof(struct floppy_raw_cmd);
3194 if (ptr->cmd_count > 33)
3195 /* the command may now also take up the space
3196 * initially intended for the reply & the
3197 * reply count. Needed for long 82078 commands
3198 * such as RESTORE, which takes ... 17 command
3199 * bytes. Murphy's law #137: When you reserve
3200 * 16 bytes for a structure, you'll one day
3201 * discover that you really need 17...
3203 return -EINVAL;
3205 for (i=0; i< 16; i++)
3206 ptr->reply[i] = 0;
3207 ptr->resultcode = 0;
3208 ptr->kernel_data = 0;
3210 if (ptr->flags & (FD_RAW_READ | FD_RAW_WRITE)) {
3211 if (ptr->length <= 0)
3212 return -EINVAL;
3213 ptr->kernel_data =(char*)fd_dma_mem_alloc(ptr->length);
3214 fallback_on_nodma_alloc(&ptr->kernel_data,
3215 ptr->length);
3216 if (!ptr->kernel_data)
3217 return -ENOMEM;
3218 ptr->buffer_length = ptr->length;
3220 if (ptr->flags & FD_RAW_WRITE)
3221 ECALL(fd_copyin(ptr->data, ptr->kernel_data,
3222 ptr->length));
3223 rcmd = & (ptr->next);
3224 if (!(ptr->flags & FD_RAW_MORE))
3225 return 0;
3226 ptr->rate &= 0x43;
3231 static int raw_cmd_ioctl(int cmd, void *param)
3233 int drive, ret, ret2;
3234 struct floppy_raw_cmd *my_raw_cmd;
3236 if (FDCS->rawcmd <= 1)
3237 FDCS->rawcmd = 1;
3238 for (drive= 0; drive < N_DRIVE; drive++){
3239 if (FDC(drive) != fdc)
3240 continue;
3241 if (drive == current_drive){
3242 if (UDRS->fd_ref > 1){
3243 FDCS->rawcmd = 2;
3244 break;
3246 } else if (UDRS->fd_ref){
3247 FDCS->rawcmd = 2;
3248 break;
3252 if (FDCS->reset)
3253 return -EIO;
3255 ret = raw_cmd_copyin(cmd, param, &my_raw_cmd);
3256 if (ret) {
3257 raw_cmd_free(&my_raw_cmd);
3258 return ret;
3261 raw_cmd = my_raw_cmd;
3262 cont = &raw_cmd_cont;
3263 ret=wait_til_done(floppy_start,1);
3264 #ifdef DCL_DEBUG
3265 if (DP->flags & FD_DEBUG){
3266 DPRINT("calling disk change from raw_cmd ioctl\n");
3268 #endif
3270 if (ret != -EINTR && FDCS->reset)
3271 ret = -EIO;
3273 DRS->track = NO_TRACK;
3275 ret2 = raw_cmd_copyout(cmd, param, my_raw_cmd);
3276 if (!ret)
3277 ret = ret2;
3278 raw_cmd_free(&my_raw_cmd);
3279 return ret;
3282 static int invalidate_drive(kdev_t rdev)
3284 /* invalidate the buffer track to force a reread */
3285 set_bit(DRIVE(rdev), &fake_change);
3286 process_fd_request();
3287 check_disk_change(rdev);
3288 return 0;
3292 static inline void clear_write_error(int drive)
3294 CLEARSTRUCT(UDRWE);
3297 static inline int set_geometry(unsigned int cmd, struct floppy_struct *g,
3298 int drive, int type, kdev_t device)
3300 int cnt;
3302 /* sanity checking for parameters.*/
3303 if (g->sect <= 0 ||
3304 g->head <= 0 ||
3305 g->track <= 0 ||
3306 g->track > UDP->tracks>>STRETCH(g) ||
3307 /* check if reserved bits are set */
3308 (g->stretch&~(FD_STRETCH|FD_SWAPSIDES)) != 0)
3309 return -EINVAL;
3310 if (type){
3311 if (!capable(CAP_SYS_ADMIN))
3312 return -EPERM;
3313 LOCK_FDC(drive,1);
3314 for (cnt = 0; cnt < N_DRIVE; cnt++){
3315 if (ITYPE(drive_state[cnt].fd_device) == type &&
3316 drive_state[cnt].fd_ref)
3317 set_bit(drive, &fake_change);
3319 floppy_type[type] = *g;
3320 floppy_type[type].name="user format";
3321 for (cnt = type << 2; cnt < (type << 2) + 4; cnt++)
3322 floppy_sizes[cnt]= floppy_sizes[cnt+0x80]=
3323 (floppy_type[type].size+1)>>1;
3324 process_fd_request();
3325 for (cnt = 0; cnt < N_DRIVE; cnt++){
3326 if (ITYPE(drive_state[cnt].fd_device) == type &&
3327 drive_state[cnt].fd_ref)
3328 check_disk_change(
3329 MKDEV(FLOPPY_MAJOR,
3330 drive_state[cnt].fd_device));
3332 } else {
3333 LOCK_FDC(drive,1);
3334 if (cmd != FDDEFPRM)
3335 /* notice a disk change immediately, else
3336 * we lose our settings immediately*/
3337 CALL(poll_drive(1, FD_RAW_NEED_DISK));
3338 user_params[drive] = *g;
3339 if (buffer_drive == drive)
3340 SUPBOUND(buffer_max, user_params[drive].sect);
3341 current_type[drive] = &user_params[drive];
3342 floppy_sizes[drive] = (user_params[drive].size+1) >> 1;
3343 if (cmd == FDDEFPRM)
3344 DRS->keep_data = -1;
3345 else
3346 DRS->keep_data = 1;
3347 /* invalidation. Invalidate only when needed, i.e.
3348 * when there are already sectors in the buffer cache
3349 * whose number will change. This is useful, because
3350 * mtools often changes the geometry of the disk after
3351 * looking at the boot block */
3352 if (DRS->maxblock > user_params[drive].sect || DRS->maxtrack)
3353 invalidate_drive(device);
3354 else
3355 process_fd_request();
3357 return 0;
3360 /* handle obsolete ioctl's */
3361 static int ioctl_table[]= {
3362 FDCLRPRM,
3363 FDSETPRM,
3364 FDDEFPRM,
3365 FDGETPRM,
3366 FDMSGON,
3367 FDMSGOFF,
3368 FDFMTBEG,
3369 FDFMTTRK,
3370 FDFMTEND,
3371 FDSETEMSGTRESH,
3372 FDFLUSH,
3373 FDSETMAXERRS,
3374 FDGETMAXERRS,
3375 FDGETDRVTYP,
3376 FDSETDRVPRM,
3377 FDGETDRVPRM,
3378 FDGETDRVSTAT,
3379 FDPOLLDRVSTAT,
3380 FDRESET,
3381 FDGETFDCSTAT,
3382 FDWERRORCLR,
3383 FDWERRORGET,
3384 FDRAWCMD,
3385 FDEJECT,
3386 FDTWADDLE
3389 static inline int normalize_ioctl(int *cmd, int *size)
3391 int i;
3393 for (i=0; i < ARRAY_SIZE(ioctl_table); i++) {
3394 if ((*cmd & 0xffff) == (ioctl_table[i] & 0xffff)){
3395 *size = _IOC_SIZE(*cmd);
3396 *cmd = ioctl_table[i];
3397 if (*size > _IOC_SIZE(*cmd)) {
3398 printk("ioctl not yet supported\n");
3399 return -EFAULT;
3401 return 0;
3404 return -EINVAL;
3407 static int get_floppy_geometry(int drive, int type, struct floppy_struct **g)
3409 if (type)
3410 *g = &floppy_type[type];
3411 else {
3412 LOCK_FDC(drive,0);
3413 CALL(poll_drive(0,0));
3414 process_fd_request();
3415 *g = current_type[drive];
3417 if (!*g)
3418 return -ENODEV;
3419 return 0;
3422 static int fd_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
3423 unsigned long param)
3425 #define OUT(c,x) case c: outparam = (const char *) (x); break
3426 #define IN(c,x,tag) case c: *(x) = inparam. tag ; return 0
3428 int i,drive,type;
3429 kdev_t device;
3430 int ret;
3431 int size;
3432 union inparam {
3433 struct floppy_struct g; /* geometry */
3434 struct format_descr f;
3435 struct floppy_max_errors max_errors;
3436 struct floppy_drive_params dp;
3437 } inparam; /* parameters coming from user space */
3438 const char *outparam; /* parameters passed back to user space */
3440 device = inode->i_rdev;
3441 switch (cmd) {
3442 case BLKROSET:
3443 case BLKROGET:
3444 case BLKRASET:
3445 case BLKRAGET:
3446 case BLKFLSBUF:
3447 return blk_ioctl(device, cmd, param);
3449 type = TYPE(device);
3450 drive = DRIVE(device);
3452 /* convert compatibility eject ioctls into floppy eject ioctl.
3453 * We do this in order to provide a means to eject floppy disks before
3454 * installing the new fdutils package */
3455 if (cmd == CDROMEJECT || /* CD-ROM eject */
3456 cmd == 0x6470 /* SunOS floppy eject */) {
3457 DPRINT("obsolete eject ioctl\n");
3458 DPRINT("please use floppycontrol --eject\n");
3459 cmd = FDEJECT;
3462 /* generic block device ioctls */
3463 switch(cmd) {
3464 /* the following have been inspired by the corresponding
3465 * code for other block devices. */
3466 struct floppy_struct *g;
3467 case HDIO_GETGEO:
3469 struct hd_geometry loc;
3470 ECALL(get_floppy_geometry(drive, type, &g));
3471 loc.heads = g->head;
3472 loc.sectors = g->sect;
3473 loc.cylinders = g->track;
3474 loc.start = 0;
3475 return _COPYOUT(loc);
3478 case BLKGETSIZE:
3479 ECALL(get_floppy_geometry(drive, type, &g));
3480 return put_user(g->size, (long *) param);
3481 /* BLKRRPART is not defined as floppies don't have
3482 * partition tables */
3485 /* convert the old style command into a new style command */
3486 if ((cmd & 0xff00) == 0x0200) {
3487 ECALL(normalize_ioctl(&cmd, &size));
3488 } else
3489 return -EINVAL;
3491 /* permission checks */
3492 if ((cmd & 0x80) && !suser())
3493 return -EPERM;
3495 /* copyin */
3496 CLEARSTRUCT(&inparam);
3497 if (_IOC_DIR(cmd) & _IOC_WRITE)
3498 ECALL(fd_copyin((void *)param, &inparam, size))
3500 switch (cmd) {
3501 case FDEJECT:
3502 if (UDRS->fd_ref != 1)
3503 /* somebody else has this drive open */
3504 return -EBUSY;
3505 LOCK_FDC(drive,1);
3507 /* do the actual eject. Fails on
3508 * non-Sparc architectures */
3509 ret=fd_eject(UNIT(drive));
3511 USETF(FD_DISK_CHANGED);
3512 USETF(FD_VERIFY);
3513 process_fd_request();
3514 return ret;
3515 case FDCLRPRM:
3516 LOCK_FDC(drive,1);
3517 current_type[drive] = NULL;
3518 floppy_sizes[drive] = MAX_DISK_SIZE;
3519 UDRS->keep_data = 0;
3520 return invalidate_drive(device);
3521 case FDSETPRM:
3522 case FDDEFPRM:
3523 return set_geometry(cmd, & inparam.g,
3524 drive, type, device);
3525 case FDGETPRM:
3526 ECALL(get_floppy_geometry(drive, type,
3527 (struct floppy_struct**)
3528 &outparam));
3529 break;
3531 case FDMSGON:
3532 UDP->flags |= FTD_MSG;
3533 return 0;
3534 case FDMSGOFF:
3535 UDP->flags &= ~FTD_MSG;
3536 return 0;
3538 case FDFMTBEG:
3539 LOCK_FDC(drive,1);
3540 CALL(poll_drive(1, FD_RAW_NEED_DISK));
3541 ret = UDRS->flags;
3542 process_fd_request();
3543 if (ret & FD_VERIFY)
3544 return -ENODEV;
3545 if (!(ret & FD_DISK_WRITABLE))
3546 return -EROFS;
3547 return 0;
3548 case FDFMTTRK:
3549 if (UDRS->fd_ref != 1)
3550 return -EBUSY;
3551 return do_format(device, &inparam.f);
3552 case FDFMTEND:
3553 case FDFLUSH:
3554 LOCK_FDC(drive,1);
3555 return invalidate_drive(device);
3557 case FDSETEMSGTRESH:
3558 UDP->max_errors.reporting =
3559 (unsigned short) (param & 0x0f);
3560 return 0;
3561 OUT(FDGETMAXERRS, &UDP->max_errors);
3562 IN(FDSETMAXERRS, &UDP->max_errors, max_errors);
3564 case FDGETDRVTYP:
3565 outparam = drive_name(type,drive);
3566 SUPBOUND(size,strlen(outparam)+1);
3567 break;
3569 IN(FDSETDRVPRM, UDP, dp);
3570 OUT(FDGETDRVPRM, UDP);
3572 case FDPOLLDRVSTAT:
3573 LOCK_FDC(drive,1);
3574 CALL(poll_drive(1, FD_RAW_NEED_DISK));
3575 process_fd_request();
3576 /* fall through */
3577 OUT(FDGETDRVSTAT, UDRS);
3579 case FDRESET:
3580 return user_reset_fdc(drive, (int)param, 1);
3582 OUT(FDGETFDCSTAT,UFDCS);
3584 case FDWERRORCLR:
3585 CLEARSTRUCT(UDRWE);
3586 return 0;
3587 OUT(FDWERRORGET,UDRWE);
3589 case FDRAWCMD:
3590 if (type)
3591 return -EINVAL;
3592 LOCK_FDC(drive,1);
3593 set_floppy(device);
3594 CALL(i = raw_cmd_ioctl(cmd,(void *) param));
3595 process_fd_request();
3596 return i;
3598 case FDTWADDLE:
3599 LOCK_FDC(drive,1);
3600 twaddle();
3601 process_fd_request();
3602 return 0;
3604 default:
3605 return -EINVAL;
3608 if (_IOC_DIR(cmd) & _IOC_READ)
3609 return fd_copyout((void *)param, outparam, size);
3610 else
3611 return 0;
3612 #undef OUT
3613 #undef IN
3616 static void __init config_types(void)
3618 int first=1;
3619 int drive;
3621 /* read drive info out of physical CMOS */
3622 drive=0;
3623 if (!UDP->cmos)
3624 UDP->cmos = FLOPPY0_TYPE;
3625 drive=1;
3626 if (!UDP->cmos && FLOPPY1_TYPE)
3627 UDP->cmos = FLOPPY1_TYPE;
3629 /* XXX */
3630 /* additional physical CMOS drive detection should go here */
3632 for (drive=0; drive < N_DRIVE; drive++){
3633 unsigned int type = UDP->cmos;
3634 struct floppy_drive_params *params;
3635 const char *name = NULL;
3636 static char temparea[32];
3638 if (type < NUMBER(default_drive_params)) {
3639 params = &default_drive_params[type].params;
3640 if (type) {
3641 name = default_drive_params[type].name;
3642 allowed_drive_mask |= 1 << drive;
3644 } else {
3645 params = &default_drive_params[0].params;
3646 sprintf(temparea, "unknown type %d (usb?)", type);
3647 name = temparea;
3649 if (name) {
3650 const char * prepend = ",";
3651 if (first) {
3652 prepend = KERN_INFO "Floppy drive(s):";
3653 first = 0;
3655 printk("%s fd%d is %s", prepend, drive, name);
3656 register_devfs_entries (drive);
3658 *UDP = *params;
3660 if (!first)
3661 printk("\n");
3664 static int floppy_release(struct inode * inode, struct file * filp)
3666 int drive = DRIVE(inode->i_rdev);
3668 if (UDRS->fd_ref < 0)
3669 UDRS->fd_ref=0;
3670 else if (!UDRS->fd_ref--) {
3671 DPRINT("floppy_release with fd_ref == 0");
3672 UDRS->fd_ref = 0;
3674 floppy_release_irq_and_dma();
3675 return 0;
3679 * floppy_open check for aliasing (/dev/fd0 can be the same as
3680 * /dev/PS0 etc), and disallows simultaneous access to the same
3681 * drive with different device numbers.
3683 #define RETERR(x) do{floppy_release(inode,filp); return -(x);}while(0)
3685 static int floppy_open(struct inode * inode, struct file * filp)
3687 int drive;
3688 int old_dev;
3689 int try;
3690 char *tmp;
3692 if (!filp) {
3693 DPRINT("Weird, open called with filp=0\n");
3694 return -EIO;
3697 drive = DRIVE(inode->i_rdev);
3698 if (drive >= N_DRIVE ||
3699 !(allowed_drive_mask & (1 << drive)) ||
3700 fdc_state[FDC(drive)].version == FDC_NONE)
3701 return -ENXIO;
3703 if (TYPE(inode->i_rdev) >= NUMBER(floppy_type))
3704 return -ENXIO;
3705 old_dev = UDRS->fd_device;
3706 if (UDRS->fd_ref && old_dev != MINOR(inode->i_rdev))
3707 return -EBUSY;
3709 if (!UDRS->fd_ref && (UDP->flags & FD_BROKEN_DCL)){
3710 USETF(FD_DISK_CHANGED);
3711 USETF(FD_VERIFY);
3714 if (UDRS->fd_ref == -1 ||
3715 (UDRS->fd_ref && (filp->f_flags & O_EXCL)))
3716 return -EBUSY;
3718 if (floppy_grab_irq_and_dma())
3719 return -EBUSY;
3721 if (filp->f_flags & O_EXCL)
3722 UDRS->fd_ref = -1;
3723 else
3724 UDRS->fd_ref++;
3726 if (!floppy_track_buffer){
3727 /* if opening an ED drive, reserve a big buffer,
3728 * else reserve a small one */
3729 if ((UDP->cmos == 6) || (UDP->cmos == 5))
3730 try = 64; /* Only 48 actually useful */
3731 else
3732 try = 32; /* Only 24 actually useful */
3734 tmp=(char *)fd_dma_mem_alloc(1024 * try);
3735 if (!tmp && !floppy_track_buffer) {
3736 try >>= 1; /* buffer only one side */
3737 INFBOUND(try, 16);
3738 tmp= (char *)fd_dma_mem_alloc(1024*try);
3740 if (!tmp && !floppy_track_buffer) {
3741 fallback_on_nodma_alloc(&tmp, 2048 * try);
3743 if (!tmp && !floppy_track_buffer) {
3744 DPRINT("Unable to allocate DMA memory\n");
3745 RETERR(ENXIO);
3747 if (floppy_track_buffer) {
3748 if (tmp)
3749 fd_dma_mem_free((unsigned long)tmp,try*1024);
3750 } else {
3751 buffer_min = buffer_max = -1;
3752 floppy_track_buffer = tmp;
3753 max_buffer_sectors = try;
3757 UDRS->fd_device = MINOR(inode->i_rdev);
3758 if (old_dev != -1 && old_dev != MINOR(inode->i_rdev)) {
3759 if (buffer_drive == drive)
3760 buffer_track = -1;
3761 invalidate_buffers(MKDEV(FLOPPY_MAJOR,old_dev));
3764 if (UFDCS->rawcmd == 1)
3765 UFDCS->rawcmd = 2;
3767 if (filp->f_flags & O_NDELAY)
3768 return 0;
3769 if (filp->f_mode & 3) {
3770 UDRS->last_checked = 0;
3771 check_disk_change(inode->i_rdev);
3772 if (UTESTF(FD_DISK_CHANGED))
3773 RETERR(ENXIO);
3775 if ((filp->f_mode & 2) && !(UTESTF(FD_DISK_WRITABLE)))
3776 RETERR(EROFS);
3777 return 0;
3778 #undef RETERR
3782 * Check if the disk has been changed or if a change has been faked.
3784 static int check_floppy_change(kdev_t dev)
3786 int drive = DRIVE(dev);
3788 if (MAJOR(dev) != MAJOR_NR) {
3789 DPRINT("check_floppy_change: not a floppy\n");
3790 return 0;
3793 if (UTESTF(FD_DISK_CHANGED) || UTESTF(FD_VERIFY))
3794 return 1;
3796 if (UDP->checkfreq < (int)(jiffies - UDRS->last_checked)) {
3797 if(floppy_grab_irq_and_dma()) {
3798 return 1;
3801 lock_fdc(drive,0);
3802 poll_drive(0,0);
3803 process_fd_request();
3804 floppy_release_irq_and_dma();
3807 if (UTESTF(FD_DISK_CHANGED) ||
3808 UTESTF(FD_VERIFY) ||
3809 test_bit(drive, &fake_change) ||
3810 (!TYPE(dev) && !current_type[drive]))
3811 return 1;
3812 return 0;
3815 /* revalidate the floppy disk, i.e. trigger format autodetection by reading
3816 * the bootblock (block 0). "Autodetection" is also needed to check whether
3817 * there is a disk in the drive at all... Thus we also do it for fixed
3818 * geometry formats */
3819 static int floppy_revalidate(kdev_t dev)
3821 #define NO_GEOM (!current_type[drive] && !TYPE(dev))
3822 struct buffer_head * bh;
3823 int drive=DRIVE(dev);
3824 int cf;
3826 if (UTESTF(FD_DISK_CHANGED) ||
3827 UTESTF(FD_VERIFY) ||
3828 test_bit(drive, &fake_change) ||
3829 NO_GEOM){
3830 if(usage_count == 0) {
3831 printk("VFS: revalidate called on non-open device.\n");
3832 return -EFAULT;
3834 lock_fdc(drive,0);
3835 cf = UTESTF(FD_DISK_CHANGED) || UTESTF(FD_VERIFY);
3836 if (!(cf || test_bit(drive, &fake_change) || NO_GEOM)){
3837 process_fd_request(); /*already done by another thread*/
3838 return 0;
3840 UDRS->maxblock = 0;
3841 UDRS->maxtrack = 0;
3842 if (buffer_drive == drive)
3843 buffer_track = -1;
3844 clear_bit(drive, &fake_change);
3845 UCLEARF(FD_DISK_CHANGED);
3846 if (cf)
3847 UDRS->generation++;
3848 if (NO_GEOM){
3849 /* auto-sensing */
3850 int size = floppy_blocksizes[MINOR(dev)];
3851 if (!size)
3852 size = 1024;
3853 if (!(bh = getblk(dev,0,size))){
3854 process_fd_request();
3855 return -ENXIO;
3857 if (bh && !buffer_uptodate(bh))
3858 ll_rw_block(READ, 1, &bh);
3859 process_fd_request();
3860 wait_on_buffer(bh);
3861 brelse(bh);
3862 return 0;
3864 if (cf)
3865 poll_drive(0, FD_RAW_NEED_DISK);
3866 process_fd_request();
3868 return 0;
3871 static struct block_device_operations floppy_fops = {
3872 open: floppy_open,
3873 release: floppy_release,
3874 ioctl: fd_ioctl,
3875 check_media_change: check_floppy_change,
3876 revalidate: floppy_revalidate,
3879 static void __init register_devfs_entries (int drive)
3881 int base_minor, i;
3882 static char *table[] =
3883 {"", "d360", "h1200", "u360", "u720", "h360", "h720",
3884 "u1440", "u2880", "CompaQ", "h1440", "u1680", "h410",
3885 "u820", "h1476", "u1722", "h420", "u830", "h1494", "u1743",
3886 "h880", "u1040", "u1120", "h1600", "u1760", "u1920",
3887 "u3200", "u3520", "u3840", "u1840", "u800", "u1600",
3888 NULL
3890 static int t360[] = {1,0}, t1200[] = {2,5,6,10,12,14,16,18,20,23,0},
3891 t3in[] = {8,9,26,27,28, 7,11,15,19,24,25,29,31, 3,4,13,17,21,22,30,0};
3892 static int *table_sup[] =
3893 {NULL, t360, t1200, t3in+5+8, t3in+5, t3in, t3in};
3895 base_minor = (drive < 4) ? drive : (124 + drive);
3896 if (UDP->cmos <= NUMBER(default_drive_params)) {
3897 i = 0;
3898 do {
3899 char name[16];
3901 sprintf (name, "%d%s", drive, table[table_sup[UDP->cmos][i]]);
3902 devfs_register (devfs_handle, name, DEVFS_FL_DEFAULT, MAJOR_NR,
3903 base_minor + (table_sup[UDP->cmos][i] << 2),
3904 S_IFBLK | S_IRUSR | S_IWUSR | S_IRGRP |S_IWGRP,
3905 &floppy_fops, NULL);
3906 } while (table_sup[UDP->cmos][i++]);
3911 * Floppy Driver initialization
3912 * =============================
3915 /* Determine the floppy disk controller type */
3916 /* This routine was written by David C. Niemi */
3917 static char __init get_fdc_version(void)
3919 int r;
3921 output_byte(FD_DUMPREGS); /* 82072 and better know DUMPREGS */
3922 if (FDCS->reset)
3923 return FDC_NONE;
3924 if ((r = result()) <= 0x00)
3925 return FDC_NONE; /* No FDC present ??? */
3926 if ((r==1) && (reply_buffer[0] == 0x80)){
3927 printk(KERN_INFO "FDC %d is an 8272A\n",fdc);
3928 return FDC_8272A; /* 8272a/765 don't know DUMPREGS */
3930 if (r != 10) {
3931 printk("FDC %d init: DUMPREGS: unexpected return of %d bytes.\n",
3932 fdc, r);
3933 return FDC_UNKNOWN;
3936 if (!fdc_configure()) {
3937 printk(KERN_INFO "FDC %d is an 82072\n",fdc);
3938 return FDC_82072; /* 82072 doesn't know CONFIGURE */
3941 output_byte(FD_PERPENDICULAR);
3942 if (need_more_output() == MORE_OUTPUT) {
3943 output_byte(0);
3944 } else {
3945 printk(KERN_INFO "FDC %d is an 82072A\n", fdc);
3946 return FDC_82072A; /* 82072A as found on Sparcs. */
3949 output_byte(FD_UNLOCK);
3950 r = result();
3951 if ((r == 1) && (reply_buffer[0] == 0x80)){
3952 printk(KERN_INFO "FDC %d is a pre-1991 82077\n", fdc);
3953 return FDC_82077_ORIG; /* Pre-1991 82077, doesn't know
3954 * LOCK/UNLOCK */
3956 if ((r != 1) || (reply_buffer[0] != 0x00)) {
3957 printk("FDC %d init: UNLOCK: unexpected return of %d bytes.\n",
3958 fdc, r);
3959 return FDC_UNKNOWN;
3961 output_byte(FD_PARTID);
3962 r = result();
3963 if (r != 1) {
3964 printk("FDC %d init: PARTID: unexpected return of %d bytes.\n",
3965 fdc, r);
3966 return FDC_UNKNOWN;
3968 if (reply_buffer[0] == 0x80) {
3969 printk(KERN_INFO "FDC %d is a post-1991 82077\n",fdc);
3970 return FDC_82077; /* Revised 82077AA passes all the tests */
3972 switch (reply_buffer[0] >> 5) {
3973 case 0x0:
3974 /* Either a 82078-1 or a 82078SL running at 5Volt */
3975 printk(KERN_INFO "FDC %d is an 82078.\n",fdc);
3976 return FDC_82078;
3977 case 0x1:
3978 printk(KERN_INFO "FDC %d is a 44pin 82078\n",fdc);
3979 return FDC_82078;
3980 case 0x2:
3981 printk(KERN_INFO "FDC %d is a S82078B\n", fdc);
3982 return FDC_S82078B;
3983 case 0x3:
3984 printk(KERN_INFO "FDC %d is a National Semiconductor PC87306\n", fdc);
3985 return FDC_87306;
3986 default:
3987 printk(KERN_INFO "FDC %d init: 82078 variant with unknown PARTID=%d.\n",
3988 fdc, reply_buffer[0] >> 5);
3989 return FDC_82078_UNKN;
3991 } /* get_fdc_version */
3993 /* lilo configuration */
3995 static void __init floppy_set_flags(int *ints,int param, int param2)
3997 int i;
3999 for (i=0; i < ARRAY_SIZE(default_drive_params); i++){
4000 if (param)
4001 default_drive_params[i].params.flags |= param2;
4002 else
4003 default_drive_params[i].params.flags &= ~param2;
4005 DPRINT("%s flag 0x%x\n", param2 ? "Setting" : "Clearing", param);
4008 static void __init daring(int *ints,int param, int param2)
4010 int i;
4012 for (i=0; i < ARRAY_SIZE(default_drive_params); i++){
4013 if (param){
4014 default_drive_params[i].params.select_delay = 0;
4015 default_drive_params[i].params.flags |= FD_SILENT_DCL_CLEAR;
4016 } else {
4017 default_drive_params[i].params.select_delay = 2*HZ/100;
4018 default_drive_params[i].params.flags &= ~FD_SILENT_DCL_CLEAR;
4021 DPRINT("Assuming %s floppy hardware\n", param ? "standard" : "broken");
4024 static void __init set_cmos(int *ints, int dummy, int dummy2)
4026 int current_drive=0;
4028 if (ints[0] != 2){
4029 DPRINT("wrong number of parameters for CMOS\n");
4030 return;
4032 current_drive = ints[1];
4033 if (current_drive < 0 || current_drive >= 8){
4034 DPRINT("bad drive for set_cmos\n");
4035 return;
4037 if (current_drive >= 4 && !FDC2)
4038 FDC2 = 0x370;
4039 DP->cmos = ints[2];
4040 DPRINT("setting CMOS code to %d\n", ints[2]);
4043 static struct param_table {
4044 const char *name;
4045 void (*fn)(int *ints, int param, int param2);
4046 int *var;
4047 int def_param;
4048 int param2;
4049 } config_params[]={
4050 { "allowed_drive_mask", 0, &allowed_drive_mask, 0xff, 0}, /* obsolete */
4051 { "all_drives", 0, &allowed_drive_mask, 0xff, 0 }, /* obsolete */
4052 { "asus_pci", 0, &allowed_drive_mask, 0x33, 0},
4054 { "irq", 0, &FLOPPY_IRQ, 6, 0 },
4055 { "dma", 0, &FLOPPY_DMA, 2, 0 },
4057 { "daring", daring, 0, 1, 0},
4059 { "two_fdc", 0, &FDC2, 0x370, 0 },
4060 { "one_fdc", 0, &FDC2, 0, 0 },
4062 { "thinkpad", floppy_set_flags, 0, 1, FD_INVERTED_DCL },
4063 { "broken_dcl", floppy_set_flags, 0, 1, FD_BROKEN_DCL },
4064 { "messages", floppy_set_flags, 0, 1, FTD_MSG },
4065 { "silent_dcl_clear", floppy_set_flags, 0, 1, FD_SILENT_DCL_CLEAR },
4066 { "debug", floppy_set_flags, 0, 1, FD_DEBUG },
4068 { "nodma", 0, &can_use_virtual_dma, 1, 0 },
4069 { "omnibook", 0, &can_use_virtual_dma, 1, 0 },
4070 { "yesdma", 0, &can_use_virtual_dma, 0, 0 },
4072 { "fifo_depth", 0, &fifo_depth, 0xa, 0 },
4073 { "nofifo", 0, &no_fifo, 0x20, 0 },
4074 { "usefifo", 0, &no_fifo, 0, 0 },
4076 { "cmos", set_cmos, 0, 0, 0 },
4077 { "slow", 0, &slow_floppy, 1, 0 },
4079 { "unexpected_interrupts", 0, &print_unex, 1, 0 },
4080 { "no_unexpected_interrupts", 0, &print_unex, 0, 0 },
4081 { "L40SX", 0, &print_unex, 0, 0 }
4084 static int __init floppy_setup(char *str)
4086 int i;
4087 int param;
4088 int ints[11];
4090 str = get_options(str,ARRAY_SIZE(ints),ints);
4091 if (str) {
4092 for (i=0; i< ARRAY_SIZE(config_params); i++){
4093 if (strcmp(str,config_params[i].name) == 0){
4094 if (ints[0])
4095 param = ints[1];
4096 else
4097 param = config_params[i].def_param;
4098 if (config_params[i].fn)
4099 config_params[i].
4100 fn(ints,param,
4101 config_params[i].param2);
4102 if (config_params[i].var) {
4103 DPRINT("%s=%d\n", str, param);
4104 *config_params[i].var = param;
4106 return 1;
4110 if (str) {
4111 DPRINT("unknown floppy option [%s]\n", str);
4113 DPRINT("allowed options are:");
4114 for (i=0; i< ARRAY_SIZE(config_params); i++)
4115 printk(" %s",config_params[i].name);
4116 printk("\n");
4117 } else
4118 DPRINT("botched floppy option\n");
4119 DPRINT("Read linux/Documentation/floppy.txt\n");
4120 return 0;
4123 static int have_no_fdc= -EIO;
4126 int __init floppy_init(void)
4128 int i,unit,drive;
4131 raw_cmd = NULL;
4133 devfs_handle = devfs_mk_dir (NULL, "floppy", NULL);
4134 if (devfs_register_blkdev(MAJOR_NR,"fd",&floppy_fops)) {
4135 printk("Unable to get major %d for floppy\n",MAJOR_NR);
4136 return -EBUSY;
4139 for (i=0; i<256; i++)
4140 if (ITYPE(i))
4141 floppy_sizes[i] = (floppy_type[ITYPE(i)].size+1) >> 1;
4142 else
4143 floppy_sizes[i] = MAX_DISK_SIZE;
4145 blk_size[MAJOR_NR] = floppy_sizes;
4146 blksize_size[MAJOR_NR] = floppy_blocksizes;
4147 blk_init_queue(BLK_DEFAULT_QUEUE(MAJOR_NR), DEVICE_REQUEST);
4148 reschedule_timeout(MAXTIMEOUT, "floppy init", MAXTIMEOUT);
4149 config_types();
4151 for (i = 0; i < N_FDC; i++) {
4152 fdc = i;
4153 CLEARSTRUCT(FDCS);
4154 FDCS->dtr = -1;
4155 FDCS->dor = 0x4;
4156 #ifdef __sparc__
4157 /*sparcs don't have a DOR reset which we can fall back on to*/
4158 FDCS->version = FDC_82072A;
4159 #endif
4162 use_virtual_dma = can_use_virtual_dma & 1;
4163 fdc_state[0].address = FDC1;
4164 if (fdc_state[0].address == -1) {
4165 devfs_unregister_blkdev(MAJOR_NR,"fd");
4166 del_timer(&fd_timeout);
4167 blk_cleanup_queue(BLK_DEFAULT_QUEUE(MAJOR_NR));
4168 return -ENODEV;
4170 #if N_FDC > 1
4171 fdc_state[1].address = FDC2;
4172 #endif
4174 fdc = 0; /* reset fdc in case of unexpected interrupt */
4175 if (floppy_grab_irq_and_dma()){
4176 del_timer(&fd_timeout);
4177 blk_cleanup_queue(BLK_DEFAULT_QUEUE(MAJOR_NR));
4178 devfs_unregister_blkdev(MAJOR_NR,"fd");
4179 del_timer(&fd_timeout);
4180 return -EBUSY;
4183 /* initialise drive state */
4184 for (drive = 0; drive < N_DRIVE; drive++) {
4185 CLEARSTRUCT(UDRS);
4186 CLEARSTRUCT(UDRWE);
4187 USETF(FD_DISK_NEWCHANGE);
4188 USETF(FD_DISK_CHANGED);
4189 USETF(FD_VERIFY);
4190 UDRS->fd_device = -1;
4191 floppy_track_buffer = NULL;
4192 max_buffer_sectors = 0;
4195 for (i = 0; i < N_FDC; i++) {
4196 fdc = i;
4197 FDCS->driver_version = FD_DRIVER_VERSION;
4198 for (unit=0; unit<4; unit++)
4199 FDCS->track[unit] = 0;
4200 if (FDCS->address == -1)
4201 continue;
4202 FDCS->rawcmd = 2;
4203 if (user_reset_fdc(-1,FD_RESET_ALWAYS,0)){
4204 /* free ioports reserved by floppy_grab_irq_and_dma() */
4205 release_region(FDCS->address, 6);
4206 release_region(FDCS->address+7, 1);
4207 FDCS->address = -1;
4208 FDCS->version = FDC_NONE;
4209 continue;
4211 /* Try to determine the floppy controller type */
4212 FDCS->version = get_fdc_version();
4213 if (FDCS->version == FDC_NONE){
4214 /* free ioports reserved by floppy_grab_irq_and_dma() */
4215 release_region(FDCS->address, 6);
4216 release_region(FDCS->address+7, 1);
4217 FDCS->address = -1;
4218 continue;
4220 if (can_use_virtual_dma == 2 && FDCS->version < FDC_82072A)
4221 can_use_virtual_dma = 0;
4223 have_no_fdc = 0;
4224 /* Not all FDCs seem to be able to handle the version command
4225 * properly, so force a reset for the standard FDC clones,
4226 * to avoid interrupt garbage.
4228 user_reset_fdc(-1,FD_RESET_ALWAYS,0);
4230 fdc=0;
4231 del_timer(&fd_timeout);
4232 current_drive = 0;
4233 floppy_release_irq_and_dma();
4234 initialising=0;
4235 if (have_no_fdc)
4237 DPRINT("no floppy controllers found\n");
4238 floppy_tq.routine = (void *)(void *) empty;
4239 mark_bh(IMMEDIATE_BH);
4240 schedule();
4241 if (usage_count)
4242 floppy_release_irq_and_dma();
4243 blk_cleanup_queue(BLK_DEFAULT_QUEUE(MAJOR_NR));
4244 devfs_unregister_blkdev(MAJOR_NR,"fd");
4247 for (drive = 0; drive < N_DRIVE; drive++) {
4248 if (!(allowed_drive_mask & (1 << drive)))
4249 continue;
4250 if (fdc_state[FDC(drive)].version == FDC_NONE)
4251 continue;
4252 for (i = 0; i<NUMBER(floppy_type); i++)
4253 register_disk(NULL, MKDEV(MAJOR_NR,TOMINOR(drive)+i*4),
4254 1, &floppy_fops, 0);
4256 return have_no_fdc;
4259 static spinlock_t floppy_usage_lock = SPIN_LOCK_UNLOCKED;
4261 static int floppy_grab_irq_and_dma(void)
4263 unsigned long flags;
4265 spin_lock_irqsave(&floppy_usage_lock, flags);
4266 if (usage_count++){
4267 spin_unlock_irqrestore(&floppy_usage_lock, flags);
4268 return 0;
4270 spin_unlock_irqrestore(&floppy_usage_lock, flags);
4271 MOD_INC_USE_COUNT;
4272 if (fd_request_irq()) {
4273 DPRINT("Unable to grab IRQ%d for the floppy driver\n",
4274 FLOPPY_IRQ);
4275 MOD_DEC_USE_COUNT;
4276 spin_lock_irqsave(&floppy_usage_lock, flags);
4277 usage_count--;
4278 spin_unlock_irqrestore(&floppy_usage_lock, flags);
4279 return -1;
4281 if (fd_request_dma()) {
4282 DPRINT("Unable to grab DMA%d for the floppy driver\n",
4283 FLOPPY_DMA);
4284 fd_free_irq();
4285 MOD_DEC_USE_COUNT;
4286 spin_lock_irqsave(&floppy_usage_lock, flags);
4287 usage_count--;
4288 spin_unlock_irqrestore(&floppy_usage_lock, flags);
4289 return -1;
4292 for (fdc=0; fdc< N_FDC; fdc++){
4293 if (FDCS->address != -1){
4294 if (check_region(FDCS->address, 6) < 0 ||
4295 check_region(FDCS->address+7, 1) < 0) {
4296 DPRINT("Floppy io-port 0x%04lx in use\n", FDCS->address);
4297 fd_free_irq();
4298 fd_free_dma();
4299 while(--fdc >= 0) {
4300 release_region(FDCS->address, 6);
4301 release_region(FDCS->address+7, 1);
4303 MOD_DEC_USE_COUNT;
4304 spin_lock_irqsave(&floppy_usage_lock, flags);
4305 usage_count--;
4306 spin_unlock_irqrestore(&floppy_usage_lock, flags);
4307 return -1;
4309 request_region(FDCS->address, 6, "floppy");
4310 request_region(FDCS->address+7, 1, "floppy DIR");
4311 /* address + 6 is reserved, and may be taken by IDE.
4312 * Unfortunately, Adaptec doesn't know this :-(, */
4315 for (fdc=0; fdc< N_FDC; fdc++){
4316 if (FDCS->address != -1){
4317 reset_fdc_info(1);
4318 fd_outb(FDCS->dor, FD_DOR);
4321 fdc = 0;
4322 set_dor(0, ~0, 8); /* avoid immediate interrupt */
4324 for (fdc = 0; fdc < N_FDC; fdc++)
4325 if (FDCS->address != -1)
4326 fd_outb(FDCS->dor, FD_DOR);
4328 * The driver will try and free resources and relies on us
4329 * to know if they were allocated or not.
4331 fdc = 0;
4332 irqdma_allocated = 1;
4333 return 0;
4336 static void floppy_release_irq_and_dma(void)
4338 int old_fdc;
4339 #ifdef FLOPPY_SANITY_CHECK
4340 #ifndef __sparc__
4341 int drive;
4342 #endif
4343 #endif
4344 long tmpsize;
4345 unsigned long tmpaddr;
4346 unsigned long flags;
4348 spin_lock_irqsave(&floppy_usage_lock, flags);
4349 if (--usage_count){
4350 spin_unlock_irqrestore(&floppy_usage_lock, flags);
4351 return;
4353 spin_unlock_irqrestore(&floppy_usage_lock, flags);
4354 if(irqdma_allocated)
4356 fd_disable_dma();
4357 fd_free_dma();
4358 fd_free_irq();
4359 irqdma_allocated=0;
4361 set_dor(0, ~0, 8);
4362 #if N_FDC > 1
4363 set_dor(1, ~8, 0);
4364 #endif
4365 floppy_enable_hlt();
4367 if (floppy_track_buffer && max_buffer_sectors) {
4368 tmpsize = max_buffer_sectors*1024;
4369 tmpaddr = (unsigned long)floppy_track_buffer;
4370 floppy_track_buffer = NULL;
4371 max_buffer_sectors = 0;
4372 buffer_min = buffer_max = -1;
4373 fd_dma_mem_free(tmpaddr, tmpsize);
4376 #ifdef FLOPPY_SANITY_CHECK
4377 #ifndef __sparc__
4378 for (drive=0; drive < N_FDC * 4; drive++)
4379 if (timer_pending(motor_off_timer + drive))
4380 printk("motor off timer %d still active\n", drive);
4381 #endif
4383 if (timer_pending(&fd_timeout))
4384 printk("floppy timer still active:%s\n", timeout_message);
4385 if (timer_pending(&fd_timer))
4386 printk("auxiliary floppy timer still active\n");
4387 if (floppy_tq.sync)
4388 printk("task queue still active\n");
4389 #endif
4390 old_fdc = fdc;
4391 for (fdc = 0; fdc < N_FDC; fdc++)
4392 if (FDCS->address != -1) {
4393 release_region(FDCS->address, 6);
4394 release_region(FDCS->address+7, 1);
4396 fdc = old_fdc;
4397 MOD_DEC_USE_COUNT;
4401 #ifdef MODULE
4403 char *floppy;
4405 static void __init parse_floppy_cfg_string(char *cfg)
4407 char *ptr;
4409 while(*cfg) {
4410 for(ptr = cfg;*cfg && *cfg != ' ' && *cfg != '\t'; cfg++);
4411 if (*cfg) {
4412 *cfg = '\0';
4413 cfg++;
4415 if (*ptr)
4416 floppy_setup(ptr);
4420 int init_module(void)
4422 printk(KERN_INFO "inserting floppy driver for " UTS_RELEASE "\n");
4424 if (floppy)
4425 parse_floppy_cfg_string(floppy);
4426 return floppy_init();
4429 void cleanup_module(void)
4431 int dummy;
4433 devfs_unregister (devfs_handle);
4434 devfs_unregister_blkdev(MAJOR_NR, "fd");
4436 blk_cleanup_queue(BLK_DEFAULT_QUEUE(MAJOR_NR));
4437 /* eject disk, if any */
4438 dummy = fd_eject(0);
4441 MODULE_PARM(floppy,"s");
4442 MODULE_PARM(FLOPPY_IRQ,"i");
4443 MODULE_PARM(FLOPPY_DMA,"i");
4444 MODULE_AUTHOR("Alain L. Knaff");
4445 MODULE_SUPPORTED_DEVICE("fd");
4447 #else
4449 __setup ("floppy=", floppy_setup);
4451 /* eject the boot floppy (if we need the drive for a different root floppy) */
4452 /* This should only be called at boot time when we're sure that there's no
4453 * resource contention. */
4454 void floppy_eject(void)
4456 int dummy;
4457 if (have_no_fdc)
4458 return;
4459 if(floppy_grab_irq_and_dma()==0)
4461 lock_fdc(MAXTIMEOUT,0);
4462 dummy=fd_eject(0);
4463 process_fd_request();
4464 floppy_release_irq_and_dma();
4467 #endif