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
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
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
47 * 1992/9/17: Added DMA allocation & DMA functions. -- hhb.
49 * TODO: Errors are still not counted properly.
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
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
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
112 * 1998/09/20 -- David Weinehall -- Added slow-down code for buggy PS/2-drives.
116 * 1999/08/13 -- Paul Slootman -- floppy stopped working on Alpha after 24
117 * days, 6 hours, 32 minutes and 32 seconds (i.e. MAXINT jiffies; ints were
118 * being used to store jiffies, which are unsigned longs).
122 * 2000/08/28 -- Arnaldo Carvalho de Melo <acme@conectiva.com.br>
123 * - get rid of check_region
127 #define FLOPPY_SANITY_CHECK
128 #undef FLOPPY_SILENT_DCL_CLEAR
130 #define REALLY_SLOW_IO
133 #define DCL_DEBUG /* debug disk change line */
135 /* do print messages for unexpected interrupts */
136 static int print_unex
=1;
137 #include <linux/module.h>
138 #include <linux/sched.h>
139 #include <linux/fs.h>
140 #include <linux/kernel.h>
141 #include <linux/timer.h>
142 #include <linux/tqueue.h>
144 #include <linux/fdreg.h>
147 * 1998/1/21 -- Richard Gooch <rgooch@atnf.csiro.au> -- devfs support
151 #include <linux/fd.h>
152 #include <linux/hdreg.h>
154 #include <linux/errno.h>
155 #include <linux/malloc.h>
156 #include <linux/mm.h>
157 #include <linux/string.h>
158 #include <linux/fcntl.h>
159 #include <linux/delay.h>
160 #include <linux/mc146818rtc.h> /* CMOS defines */
161 #include <linux/ioport.h>
162 #include <linux/interrupt.h>
163 #include <linux/init.h>
164 #include <linux/devfs_fs_kernel.h>
167 * PS/2 floppies have much slower step rates than regular floppies.
168 * It's been recommended that take about 1/4 of the default speed
169 * in some more extreme cases.
171 static int slow_floppy
= 0;
175 #include <asm/system.h>
177 #include <asm/uaccess.h>
179 static int FLOPPY_IRQ
=6;
180 static int FLOPPY_DMA
=2;
181 static int can_use_virtual_dma
=2;
183 * can use virtual DMA:
184 * 0 = use of virtual DMA disallowed by config
185 * 1 = use of virtual DMA prescribed by config
186 * 2 = no virtual DMA preference configured. By default try hard DMA,
187 * but fall back on virtual DMA when not enough memory available
190 static int use_virtual_dma
;
194 * 1 using virtual DMA
195 * This variable is set to virtual when a DMA mem problem arises, and
196 * reset back in floppy_grab_irq_and_dma.
197 * It is not safe to reset it in other circumstances, because the floppy
198 * driver may have several buffers in use at once, and we do currently not
199 * record each buffers capabilities
202 static unsigned short virtual_dma_port
=0x3f0;
203 void floppy_interrupt(int irq
, void *dev_id
, struct pt_regs
* regs
);
204 static int set_dor(int fdc
, char mask
, char data
);
205 static void register_devfs_entries (int drive
) __init
;
206 static devfs_handle_t devfs_handle
= NULL
;
208 #define K_64 0x10000 /* 64KB */
210 /* the following is the mask of allowed drives. By default units 2 and
211 * 3 of both floppy controllers are disabled, because switching on the
212 * motor of these drives causes system hangs on some PCI computers. drive
213 * 0 is the low bit (0x1), and drive 7 is the high bit (0x80). Bits are on if
214 * a drive is allowed.
216 * NOTE: This must come before we include the arch floppy header because
217 * some ports reference this variable from there. -DaveM
220 static int allowed_drive_mask
= 0x33;
222 #include <asm/floppy.h>
224 static int irqdma_allocated
= 0;
226 #define MAJOR_NR FLOPPY_MAJOR
228 #include <linux/blk.h>
229 #include <linux/blkpg.h>
230 #include <linux/cdrom.h> /* for the compatibility eject ioctl */
232 #ifndef fd_get_dma_residue
233 #define fd_get_dma_residue() get_dma_residue(FLOPPY_DMA)
236 /* Dma Memory related stuff */
238 #ifndef fd_dma_mem_free
239 #define fd_dma_mem_free(addr, size) free_pages(addr, get_order(size))
242 #ifndef fd_dma_mem_alloc
243 #define fd_dma_mem_alloc(size) __get_dma_pages(GFP_KERNEL,get_order(size))
246 static inline void fallback_on_nodma_alloc(char **addr
, size_t l
)
248 #ifdef FLOPPY_CAN_FALLBACK_ON_NODMA
250 return; /* we have the memory */
251 if (can_use_virtual_dma
!= 2)
252 return; /* no fallback allowed */
253 printk("DMA memory shortage. Temporarily falling back on virtual DMA\n");
254 *addr
= (char *) nodma_mem_alloc(l
);
260 /* End dma memory related stuff */
262 static unsigned long fake_change
= 0;
263 static int initialising
=1;
265 static inline int TYPE(kdev_t x
) {
266 return (MINOR(x
)>>2) & 0x1f;
268 static inline int DRIVE(kdev_t x
) {
269 return (MINOR(x
)&0x03) | ((MINOR(x
)&0x80) >> 5);
271 #define ITYPE(x) (((x)>>2) & 0x1f)
272 #define TOMINOR(x) ((x & 3) | ((x & 4) << 5))
273 #define UNIT(x) ((x) & 0x03) /* drive on fdc */
274 #define FDC(x) (((x) & 0x04) >> 2) /* fdc of drive */
275 #define REVDRIVE(fdc, unit) ((unit) + ((fdc) << 2))
276 /* reverse mapping from unit and fdc to drive */
277 #define DP (&drive_params[current_drive])
278 #define DRS (&drive_state[current_drive])
279 #define DRWE (&write_errors[current_drive])
280 #define FDCS (&fdc_state[fdc])
281 #define CLEARF(x) (clear_bit(x##_BIT, &DRS->flags))
282 #define SETF(x) (set_bit(x##_BIT, &DRS->flags))
283 #define TESTF(x) (test_bit(x##_BIT, &DRS->flags))
285 #define UDP (&drive_params[drive])
286 #define UDRS (&drive_state[drive])
287 #define UDRWE (&write_errors[drive])
288 #define UFDCS (&fdc_state[FDC(drive)])
289 #define UCLEARF(x) (clear_bit(x##_BIT, &UDRS->flags))
290 #define USETF(x) (set_bit(x##_BIT, &UDRS->flags))
291 #define UTESTF(x) (test_bit(x##_BIT, &UDRS->flags))
293 #define DPRINT(format, args...) printk(DEVICE_NAME "%d: " format, current_drive , ## args)
295 #define PH_HEAD(floppy,head) (((((floppy)->stretch & 2) >>1) ^ head) << 2)
296 #define STRETCH(floppy) ((floppy)->stretch & FD_STRETCH)
298 #define CLEARSTRUCT(x) memset((x), 0, sizeof(*(x)))
301 #define COMMAND raw_cmd->cmd[0]
302 #define DR_SELECT raw_cmd->cmd[1]
303 #define TRACK raw_cmd->cmd[2]
304 #define HEAD raw_cmd->cmd[3]
305 #define SECTOR raw_cmd->cmd[4]
306 #define SIZECODE raw_cmd->cmd[5]
307 #define SECT_PER_TRACK raw_cmd->cmd[6]
308 #define GAP raw_cmd->cmd[7]
309 #define SIZECODE2 raw_cmd->cmd[8]
313 #define F_SIZECODE raw_cmd->cmd[2]
314 #define F_SECT_PER_TRACK raw_cmd->cmd[3]
315 #define F_GAP raw_cmd->cmd[4]
316 #define F_FILL raw_cmd->cmd[5]
320 * Maximum disk size (in kilobytes). This default is used whenever the
321 * current disk size is unknown.
322 * [Now it is rather a minimum]
324 #define MAX_DISK_SIZE 4 /* 3984*/
328 * globals used by 'result()'
330 #define MAX_REPLIES 16
331 static unsigned char reply_buffer
[MAX_REPLIES
];
332 static int inr
; /* size of reply buffer, when called from interrupt */
333 #define ST0 (reply_buffer[0])
334 #define ST1 (reply_buffer[1])
335 #define ST2 (reply_buffer[2])
336 #define ST3 (reply_buffer[0]) /* result of GETSTATUS */
337 #define R_TRACK (reply_buffer[3])
338 #define R_HEAD (reply_buffer[4])
339 #define R_SECTOR (reply_buffer[5])
340 #define R_SIZECODE (reply_buffer[6])
342 #define SEL_DLY (2*HZ/100)
345 * this struct defines the different floppy drive types.
348 struct floppy_drive_params params
;
349 const char *name
; /* name printed while booting */
350 } default_drive_params
[]= {
351 /* NOTE: the time values in jiffies should be in msec!
353 | Maximum data rate supported by drive type
354 | | Head load time, msec
355 | | | Head unload time, msec (not used)
356 | | | | Step rate interval, usec
357 | | | | | Time needed for spinup time (jiffies)
358 | | | | | | Timeout for spinning down (jiffies)
359 | | | | | | | Spindown offset (where disk stops)
360 | | | | | | | | Select delay
361 | | | | | | | | | RPS
362 | | | | | | | | | | Max number of tracks
363 | | | | | | | | | | | Interrupt timeout
364 | | | | | | | | | | | | Max nonintlv. sectors
365 | | | | | | | | | | | | | -Max Errors- flags */
366 {{0, 500, 16, 16, 8000, 1*HZ
, 3*HZ
, 0, SEL_DLY
, 5, 80, 3*HZ
, 20, {3,1,2,0,2}, 0,
367 0, { 7, 4, 8, 2, 1, 5, 3,10}, 3*HZ
/2, 0 }, "unknown" },
369 {{1, 300, 16, 16, 8000, 1*HZ
, 3*HZ
, 0, SEL_DLY
, 5, 40, 3*HZ
, 17, {3,1,2,0,2}, 0,
370 0, { 1, 0, 0, 0, 0, 0, 0, 0}, 3*HZ
/2, 1 }, "360K PC" }, /*5 1/4 360 KB PC*/
372 {{2, 500, 16, 16, 6000, 4*HZ
/10, 3*HZ
, 14, SEL_DLY
, 6, 83, 3*HZ
, 17, {3,1,2,0,2}, 0,
373 0, { 2, 5, 6,23,10,20,12, 0}, 3*HZ
/2, 2 }, "1.2M" }, /*5 1/4 HD AT*/
375 {{3, 250, 16, 16, 3000, 1*HZ
, 3*HZ
, 0, SEL_DLY
, 5, 83, 3*HZ
, 20, {3,1,2,0,2}, 0,
376 0, { 4,22,21,30, 3, 0, 0, 0}, 3*HZ
/2, 4 }, "720k" }, /*3 1/2 DD*/
378 {{4, 500, 16, 16, 4000, 4*HZ
/10, 3*HZ
, 10, SEL_DLY
, 5, 83, 3*HZ
, 20, {3,1,2,0,2}, 0,
379 0, { 7, 4,25,22,31,21,29,11}, 3*HZ
/2, 7 }, "1.44M" }, /*3 1/2 HD*/
381 {{5, 1000, 15, 8, 3000, 4*HZ
/10, 3*HZ
, 10, SEL_DLY
, 5, 83, 3*HZ
, 40, {3,1,2,0,2}, 0,
382 0, { 7, 8, 4,25,28,22,31,21}, 3*HZ
/2, 8 }, "2.88M AMI BIOS" }, /*3 1/2 ED*/
384 {{6, 1000, 15, 8, 3000, 4*HZ
/10, 3*HZ
, 10, SEL_DLY
, 5, 83, 3*HZ
, 40, {3,1,2,0,2}, 0,
385 0, { 7, 8, 4,25,28,22,31,21}, 3*HZ
/2, 8 }, "2.88M" } /*3 1/2 ED*/
386 /* | --autodetected formats--- | | |
387 * read_track | | Name printed when booting
389 * Frequency of disk change checks */
392 static struct floppy_drive_params drive_params
[N_DRIVE
];
393 static struct floppy_drive_struct drive_state
[N_DRIVE
];
394 static struct floppy_write_errors write_errors
[N_DRIVE
];
395 static struct floppy_raw_cmd
*raw_cmd
, default_raw_cmd
;
398 * This struct defines the different floppy types.
400 * Bit 0 of 'stretch' tells if the tracks need to be doubled for some
401 * types (e.g. 360kB diskette in 1.2MB drive, etc.). Bit 1 of 'stretch'
402 * tells if the disk is in Commodore 1581 format, which means side 0 sectors
403 * are located on side 1 of the disk but with a side 0 ID, and vice-versa.
404 * This is the same as the Sharp MZ-80 5.25" CP/M disk format, except that the
405 * 1581's logical side 0 is on physical side 1, whereas the Sharp's logical
406 * side 0 is on physical side 0 (but with the misnamed sector IDs).
407 * 'stretch' should probably be renamed to something more general, like
408 * 'options'. Other parameters should be self-explanatory (see also
418 | | | | | | Data rate, | 0x40 for perp
419 | | | | | | | Spec1 (stepping rate, head unload
420 | | | | | | | | /fmt gap (gap2) */
421 static struct floppy_struct floppy_type
[32] = {
422 { 0, 0,0, 0,0,0x00,0x00,0x00,0x00,NULL
}, /* 0 no testing */
423 { 720, 9,2,40,0,0x2A,0x02,0xDF,0x50,"d360" }, /* 1 360KB PC */
424 { 2400,15,2,80,0,0x1B,0x00,0xDF,0x54,"h1200" }, /* 2 1.2MB AT */
425 { 720, 9,1,80,0,0x2A,0x02,0xDF,0x50,"D360" }, /* 3 360KB SS 3.5" */
426 { 1440, 9,2,80,0,0x2A,0x02,0xDF,0x50,"D720" }, /* 4 720KB 3.5" */
427 { 720, 9,2,40,1,0x23,0x01,0xDF,0x50,"h360" }, /* 5 360KB AT */
428 { 1440, 9,2,80,0,0x23,0x01,0xDF,0x50,"h720" }, /* 6 720KB AT */
429 { 2880,18,2,80,0,0x1B,0x00,0xCF,0x6C,"H1440" }, /* 7 1.44MB 3.5" */
430 { 5760,36,2,80,0,0x1B,0x43,0xAF,0x54,"E2880" }, /* 8 2.88MB 3.5" */
431 { 6240,39,2,80,0,0x1B,0x43,0xAF,0x28,"E3120" }, /* 9 3.12MB 3.5" */
433 { 2880,18,2,80,0,0x25,0x00,0xDF,0x02,"h1440" }, /* 10 1.44MB 5.25" */
434 { 3360,21,2,80,0,0x1C,0x00,0xCF,0x0C,"H1680" }, /* 11 1.68MB 3.5" */
435 { 820,10,2,41,1,0x25,0x01,0xDF,0x2E,"h410" }, /* 12 410KB 5.25" */
436 { 1640,10,2,82,0,0x25,0x02,0xDF,0x2E,"H820" }, /* 13 820KB 3.5" */
437 { 2952,18,2,82,0,0x25,0x00,0xDF,0x02,"h1476" }, /* 14 1.48MB 5.25" */
438 { 3444,21,2,82,0,0x25,0x00,0xDF,0x0C,"H1722" }, /* 15 1.72MB 3.5" */
439 { 840,10,2,42,1,0x25,0x01,0xDF,0x2E,"h420" }, /* 16 420KB 5.25" */
440 { 1660,10,2,83,0,0x25,0x02,0xDF,0x2E,"H830" }, /* 17 830KB 3.5" */
441 { 2988,18,2,83,0,0x25,0x00,0xDF,0x02,"h1494" }, /* 18 1.49MB 5.25" */
442 { 3486,21,2,83,0,0x25,0x00,0xDF,0x0C,"H1743" }, /* 19 1.74 MB 3.5" */
444 { 1760,11,2,80,0,0x1C,0x09,0xCF,0x00,"h880" }, /* 20 880KB 5.25" */
445 { 2080,13,2,80,0,0x1C,0x01,0xCF,0x00,"D1040" }, /* 21 1.04MB 3.5" */
446 { 2240,14,2,80,0,0x1C,0x19,0xCF,0x00,"D1120" }, /* 22 1.12MB 3.5" */
447 { 3200,20,2,80,0,0x1C,0x20,0xCF,0x2C,"h1600" }, /* 23 1.6MB 5.25" */
448 { 3520,22,2,80,0,0x1C,0x08,0xCF,0x2e,"H1760" }, /* 24 1.76MB 3.5" */
449 { 3840,24,2,80,0,0x1C,0x20,0xCF,0x00,"H1920" }, /* 25 1.92MB 3.5" */
450 { 6400,40,2,80,0,0x25,0x5B,0xCF,0x00,"E3200" }, /* 26 3.20MB 3.5" */
451 { 7040,44,2,80,0,0x25,0x5B,0xCF,0x00,"E3520" }, /* 27 3.52MB 3.5" */
452 { 7680,48,2,80,0,0x25,0x63,0xCF,0x00,"E3840" }, /* 28 3.84MB 3.5" */
454 { 3680,23,2,80,0,0x1C,0x10,0xCF,0x00,"H1840" }, /* 29 1.84MB 3.5" */
455 { 1600,10,2,80,0,0x25,0x02,0xDF,0x2E,"D800" }, /* 30 800KB 3.5" */
456 { 3200,20,2,80,0,0x1C,0x00,0xCF,0x2C,"H1600" }, /* 31 1.6MB 3.5" */
459 #define NUMBER(x) (sizeof(x) / sizeof(*(x)))
460 #define SECTSIZE (_FD_SECTSIZE(*floppy))
462 /* Auto-detection: Disk type used until the next media change occurs. */
463 static struct floppy_struct
*current_type
[N_DRIVE
] = {
464 NULL
, NULL
, NULL
, NULL
,
465 NULL
, NULL
, NULL
, NULL
469 * User-provided type information. current_type points to
470 * the respective entry of this array.
472 static struct floppy_struct user_params
[N_DRIVE
];
474 static int floppy_sizes
[256];
475 static int floppy_blocksizes
[256] = { 0, };
478 * The driver is trying to determine the correct media format
479 * while probing is set. rw_interrupt() clears it after a
482 static int probing
= 0;
484 /* Synchronization of FDC access. */
485 #define FD_COMMAND_NONE -1
486 #define FD_COMMAND_ERROR 2
487 #define FD_COMMAND_OKAY 3
489 static volatile int command_status
= FD_COMMAND_NONE
;
490 static unsigned long fdc_busy
= 0;
491 static DECLARE_WAIT_QUEUE_HEAD(fdc_wait
);
492 static DECLARE_WAIT_QUEUE_HEAD(command_done
);
494 #define NO_SIGNAL (!interruptible || !signal_pending(current))
495 #define CALL(x) if ((x) == -EINTR) return -EINTR
496 #define ECALL(x) if ((ret = (x))) return ret;
497 #define _WAIT(x,i) CALL(ret=wait_til_done((x),i))
498 #define WAIT(x) _WAIT((x),interruptible)
499 #define IWAIT(x) _WAIT((x),1)
501 /* Errors during formatting are counted here. */
502 static int format_errors
;
504 /* Format request descriptor. */
505 static struct format_descr format_req
;
508 * Rate is 0 for 500kb/s, 1 for 300kbps, 2 for 250kbps
509 * Spec1 is 0xSH, where S is stepping rate (F=1ms, E=2ms, D=3ms etc),
510 * H is head unload time (1=16ms, 2=32ms, etc)
515 * Because these are written to by the DMA controller, they must
516 * not contain a 64k byte boundary crossing, or data will be
519 static char *floppy_track_buffer
;
520 static int max_buffer_sectors
;
523 typedef void (*done_f
)(int);
524 static struct cont_t
{
525 void (*interrupt
)(void); /* this is called after the interrupt of the
527 void (*redo
)(void); /* this is called to retry the operation */
528 void (*error
)(void); /* this is called to tally an error */
529 done_f done
; /* this is called to say if the operation has
530 * succeeded/failed */
533 static void floppy_ready(void);
534 static void floppy_start(void);
535 static void process_fd_request(void);
536 static void recalibrate_floppy(void);
537 static void floppy_shutdown(void);
539 static int floppy_grab_irq_and_dma(void);
540 static void floppy_release_irq_and_dma(void);
543 * The "reset" variable should be tested whenever an interrupt is scheduled,
544 * after the commands have been sent. This is to ensure that the driver doesn't
545 * get wedged when the interrupt doesn't come because of a failed command.
546 * reset doesn't need to be tested before sending commands, because
547 * output_byte is automatically disabled when reset is set.
549 #define CHECK_RESET { if (FDCS->reset){ reset_fdc(); return; } }
550 static void reset_fdc(void);
553 * These are global variables, as that's the easiest way to give
554 * information to interrupts. They are the data used for the current
558 #define NEED_1_RECAL -2
559 #define NEED_2_RECAL -3
562 static int usage_count
= 0;
565 /* buffer related variables */
566 static int buffer_track
= -1;
567 static int buffer_drive
= -1;
568 static int buffer_min
= -1;
569 static int buffer_max
= -1;
571 /* fdc related variables, should end up in a struct */
572 static struct floppy_fdc_state fdc_state
[N_FDC
];
573 static int fdc
; /* current fdc */
575 static struct floppy_struct
*_floppy
= floppy_type
;
576 static unsigned char current_drive
= 0;
577 static long current_count_sectors
= 0;
578 static unsigned char sector_t
; /* sector in track */
579 static unsigned char in_sector_offset
; /* offset within physical sector,
580 * expressed in units of 512 bytes */
583 #define fd_eject(x) -EINVAL
587 static long unsigned debugtimer
;
594 static inline void set_debugt(void)
597 debugtimer
= jiffies
;
601 static inline void debugt(const char *message
)
604 if (DP
->flags
& DEBUGT
)
605 printk("%s dtime=%lu\n", message
, jiffies
-debugtimer
);
609 typedef void (*timeout_fn
)(unsigned long);
610 static struct timer_list fd_timeout
={ function
: (timeout_fn
) floppy_shutdown
};
612 static const char *timeout_message
;
614 #ifdef FLOPPY_SANITY_CHECK
615 static void is_alive(const char *message
)
617 /* this routine checks whether the floppy driver is "alive" */
618 if (fdc_busy
&& command_status
< 2 && !timer_pending(&fd_timeout
)){
619 DPRINT("timeout handler died: %s\n",message
);
624 #ifdef FLOPPY_SANITY_CHECK
628 static void (*lasthandler
)(void) = NULL
;
629 static unsigned long interruptjiffies
;
630 static unsigned long resultjiffies
;
631 static int resultsize
;
632 static unsigned long lastredo
;
634 static struct output_log
{
636 unsigned char status
;
637 unsigned long jiffies
;
638 } output_log
[OLOGSIZE
];
640 static int output_log_pos
;
644 #define MAXTIMEOUT -2
646 static void reschedule_timeout(int drive
, const char *message
, int marg
)
648 if (drive
== CURRENTD
)
649 drive
= current_drive
;
650 del_timer(&fd_timeout
);
651 if (drive
< 0 || drive
> N_DRIVE
) {
652 fd_timeout
.expires
= jiffies
+ 20UL*HZ
;
655 fd_timeout
.expires
= jiffies
+ UDP
->timeout
;
656 add_timer(&fd_timeout
);
657 if (UDP
->flags
& FD_DEBUG
){
658 DPRINT("reschedule timeout ");
659 printk(message
, marg
);
662 timeout_message
= message
;
665 static int maximum(int a
, int b
)
672 #define INFBOUND(a,b) (a)=maximum((a),(b));
674 static int minimum(int a
, int b
)
681 #define SUPBOUND(a,b) (a)=minimum((a),(b));
685 * Bottom half floppy driver.
686 * ==========================
688 * This part of the file contains the code talking directly to the hardware,
689 * and also the main service loop (seek-configure-spinup-command)
694 * This routine is responsible for maintaining the FD_DISK_CHANGE flag,
695 * and the last_checked date.
697 * last_checked is the date of the last check which showed 'no disk change'
698 * FD_DISK_CHANGE is set under two conditions:
699 * 1. The floppy has been changed after some i/o to that floppy already
701 * 2. No floppy disk is in the drive. This is done in order to ensure that
702 * requests are quickly flushed in case there is no disk in the drive. It
703 * follows that FD_DISK_CHANGE can only be cleared if there is a disk in
706 * For 1., maxblock is observed. Maxblock is 0 if no i/o has taken place yet.
707 * For 2., FD_DISK_NEWCHANGE is watched. FD_DISK_NEWCHANGE is cleared on
708 * each seek. If a disk is present, the disk change line should also be
709 * cleared on each seek. Thus, if FD_DISK_NEWCHANGE is clear, but the disk
710 * change line is set, this means either that no disk is in the drive, or
711 * that it has been removed since the last seek.
713 * This means that we really have a third possibility too:
714 * The floppy has been changed after the last seek.
717 static int disk_change(int drive
)
720 #ifdef FLOPPY_SANITY_CHECK
721 if (jiffies
- UDRS
->select_date
< UDP
->select_delay
)
722 DPRINT("WARNING disk change called early\n");
723 if (!(FDCS
->dor
& (0x10 << UNIT(drive
))) ||
724 (FDCS
->dor
& 3) != UNIT(drive
) ||
726 DPRINT("probing disk change on unselected drive\n");
727 DPRINT("drive=%d fdc=%d dor=%x\n",drive
, FDC(drive
),
728 (unsigned int)FDCS
->dor
);
733 if (UDP
->flags
& FD_DEBUG
){
734 DPRINT("checking disk change line for drive %d\n",drive
);
735 DPRINT("jiffies=%lu\n", jiffies
);
736 DPRINT("disk change line=%x\n",fd_inb(FD_DIR
)&0x80);
737 DPRINT("flags=%lx\n",UDRS
->flags
);
740 if (UDP
->flags
& FD_BROKEN_DCL
)
741 return UTESTF(FD_DISK_CHANGED
);
742 if ((fd_inb(FD_DIR
) ^ UDP
->flags
) & 0x80){
743 USETF(FD_VERIFY
); /* verify write protection */
745 /* mark it changed */
746 USETF(FD_DISK_CHANGED
);
749 /* invalidate its geometry */
750 if (UDRS
->keep_data
>= 0) {
751 if ((UDP
->flags
& FTD_MSG
) &&
752 current_type
[drive
] != NULL
)
753 DPRINT("Disk type is undefined after "
755 current_type
[drive
] = NULL
;
756 floppy_sizes
[TOMINOR(drive
)] = MAX_DISK_SIZE
;
759 /*USETF(FD_DISK_NEWCHANGE);*/
762 UDRS
->last_checked
=jiffies
;
763 UCLEARF(FD_DISK_NEWCHANGE
);
768 static inline int is_selected(int dor
, int unit
)
770 return ((dor
& (0x10 << unit
)) && (dor
&3) == unit
);
773 static int set_dor(int fdc
, char mask
, char data
)
775 register unsigned char drive
, unit
, newdor
,olddor
;
777 if (FDCS
->address
== -1)
781 newdor
= (olddor
& mask
) | data
;
782 if (newdor
!= olddor
){
784 if (is_selected(olddor
, unit
) && !is_selected(newdor
,unit
)){
785 drive
= REVDRIVE(fdc
,unit
);
787 if (UDP
->flags
& FD_DEBUG
){
788 DPRINT("calling disk change from set_dor\n");
794 fd_outb(newdor
, FD_DOR
);
797 if (!is_selected(olddor
, unit
) && is_selected(newdor
,unit
)){
798 drive
= REVDRIVE(fdc
,unit
);
799 UDRS
->select_date
= jiffies
;
803 * We should propogate failures to grab the resources back
804 * nicely from here. Actually we ought to rewrite the fd
805 * driver some day too.
807 if (newdor
& FLOPPY_MOTOR_MASK
)
808 floppy_grab_irq_and_dma();
809 if (olddor
& FLOPPY_MOTOR_MASK
)
810 floppy_release_irq_and_dma();
814 static void twaddle(void)
816 if (DP
->select_delay
)
818 fd_outb(FDCS
->dor
& ~(0x10<<UNIT(current_drive
)), FD_DOR
);
819 fd_outb(FDCS
->dor
, FD_DOR
);
820 DRS
->select_date
= jiffies
;
823 /* reset all driver information about the current fdc. This is needed after
824 * a reset, and after a raw command. */
825 static void reset_fdc_info(int mode
)
829 FDCS
->spec1
= FDCS
->spec2
= -1;
830 FDCS
->need_configure
= 1;
833 for (drive
= 0; drive
< N_DRIVE
; drive
++)
834 if (FDC(drive
) == fdc
&&
835 (mode
|| UDRS
->track
!= NEED_1_RECAL
))
836 UDRS
->track
= NEED_2_RECAL
;
839 /* selects the fdc and drive, and enables the fdc's input/dma. */
840 static void set_fdc(int drive
)
842 if (drive
>= 0 && drive
< N_DRIVE
){
844 current_drive
= drive
;
846 if (fdc
!= 1 && fdc
!= 0) {
847 printk("bad fdc value\n");
852 set_dor(1-fdc
, ~8, 0);
854 if (FDCS
->rawcmd
== 2)
856 if (fd_inb(FD_STATUS
) != STATUS_READY
)
860 /* locks the driver */
861 static int _lock_fdc(int drive
, int interruptible
, int line
)
864 printk(KERN_ERR
"Trying to lock fdc while usage count=0 at line %d\n", line
);
867 if(floppy_grab_irq_and_dma()==-1)
870 if (test_and_set_bit(0, &fdc_busy
)) {
871 DECLARE_WAITQUEUE(wait
, current
);
872 add_wait_queue(&fdc_wait
, &wait
);
875 set_current_state(TASK_INTERRUPTIBLE
);
877 if (!test_and_set_bit(0, &fdc_busy
))
883 remove_wait_queue(&fdc_wait
, &wait
);
888 set_current_state(TASK_RUNNING
);
889 remove_wait_queue(&fdc_wait
, &wait
);
891 command_status
= FD_COMMAND_NONE
;
893 reschedule_timeout(drive
, "lock fdc", 0);
898 #define lock_fdc(drive,interruptible) _lock_fdc(drive,interruptible, __LINE__)
900 #define LOCK_FDC(drive,interruptible) \
901 if (lock_fdc(drive,interruptible)) return -EINTR;
904 /* unlocks the driver */
905 static inline void unlock_fdc(void)
909 DPRINT("FDC access conflict!\n");
912 DPRINT("device interrupt still active at FDC release: %p!\n",
914 command_status
= FD_COMMAND_NONE
;
915 del_timer(&fd_timeout
);
917 clear_bit(0, &fdc_busy
);
918 floppy_release_irq_and_dma();
922 /* switches the motor off after a given timeout */
923 static void motor_off_callback(unsigned long nr
)
925 unsigned char mask
= ~(0x10 << UNIT(nr
));
927 set_dor(FDC(nr
), mask
, 0);
930 static struct timer_list motor_off_timer
[N_DRIVE
] = {
931 { data
: 0, function
: motor_off_callback
},
932 { data
: 1, function
: motor_off_callback
},
933 { data
: 2, function
: motor_off_callback
},
934 { data
: 3, function
: motor_off_callback
},
935 { data
: 4, function
: motor_off_callback
},
936 { data
: 5, function
: motor_off_callback
},
937 { data
: 6, function
: motor_off_callback
},
938 { data
: 7, function
: motor_off_callback
}
941 /* schedules motor off */
942 static void floppy_off(unsigned int drive
)
944 unsigned long volatile delta
;
945 register int fdc
=FDC(drive
);
947 if (!(FDCS
->dor
& (0x10 << UNIT(drive
))))
950 del_timer(motor_off_timer
+drive
);
952 /* make spindle stop in a position which minimizes spinup time
955 delta
= jiffies
- UDRS
->first_read_date
+ HZ
-
956 UDP
->spindown_offset
;
957 delta
= ((delta
* UDP
->rps
) % HZ
) / UDP
->rps
;
958 motor_off_timer
[drive
].expires
= jiffies
+ UDP
->spindown
- delta
;
960 add_timer(motor_off_timer
+drive
);
964 * cycle through all N_DRIVE floppy drives, for disk change testing.
965 * stopping at current drive. This is done before any long operation, to
966 * be sure to have up to date disk change information.
968 static void scandrives(void)
970 int i
, drive
, saved_drive
;
972 if (DP
->select_delay
)
975 saved_drive
= current_drive
;
976 for (i
=0; i
< N_DRIVE
; i
++){
977 drive
= (saved_drive
+ i
+ 1) % N_DRIVE
;
978 if (UDRS
->fd_ref
== 0 || UDP
->select_delay
!= 0)
979 continue; /* skip closed drives */
981 if (!(set_dor(fdc
, ~3, UNIT(drive
) | (0x10 << UNIT(drive
))) &
982 (0x10 << UNIT(drive
))))
983 /* switch the motor off again, if it was off to
985 set_dor(fdc
, ~(0x10 << UNIT(drive
)), 0);
987 set_fdc(saved_drive
);
990 static void empty(void)
994 static struct tq_struct floppy_tq
=
997 static void schedule_bh( void (*handler
)(void*) )
999 floppy_tq
.routine
= (void *)(void *) handler
;
1000 queue_task(&floppy_tq
, &tq_immediate
);
1001 mark_bh(IMMEDIATE_BH
);
1004 static struct timer_list fd_timer
;
1006 static void cancel_activity(void)
1009 floppy_tq
.routine
= (void *)(void *) empty
;
1010 del_timer(&fd_timer
);
1013 /* this function makes sure that the disk stays in the drive during the
1015 static void fd_watchdog(void)
1018 if (DP
->flags
& FD_DEBUG
){
1019 DPRINT("calling disk change from watchdog\n");
1023 if (disk_change(current_drive
)){
1024 DPRINT("disk removed during i/o\n");
1029 del_timer(&fd_timer
);
1030 fd_timer
.function
= (timeout_fn
) fd_watchdog
;
1031 fd_timer
.expires
= jiffies
+ HZ
/ 10;
1032 add_timer(&fd_timer
);
1036 static void main_command_interrupt(void)
1038 del_timer(&fd_timer
);
1042 /* waits for a delay (spinup or select) to pass */
1043 static int wait_for_completion(unsigned long delay
, timeout_fn function
)
1046 reset_fdc(); /* do the reset during sleep to win time
1047 * if we don't need to sleep, it's a good
1048 * occasion anyways */
1052 if ((signed) (jiffies
- delay
) < 0){
1053 del_timer(&fd_timer
);
1054 fd_timer
.function
= function
;
1055 fd_timer
.expires
= delay
;
1056 add_timer(&fd_timer
);
1062 static spinlock_t floppy_hlt_lock
= SPIN_LOCK_UNLOCKED
;
1063 static int hlt_disabled
;
1064 static void floppy_disable_hlt(void)
1066 unsigned long flags
;
1068 spin_lock_irqsave(&floppy_hlt_lock
, flags
);
1069 if (!hlt_disabled
) {
1071 #ifdef HAVE_DISABLE_HLT
1075 spin_unlock_irqrestore(&floppy_hlt_lock
, flags
);
1078 static void floppy_enable_hlt(void)
1080 unsigned long flags
;
1082 spin_lock_irqsave(&floppy_hlt_lock
, flags
);
1085 #ifdef HAVE_DISABLE_HLT
1089 spin_unlock_irqrestore(&floppy_hlt_lock
, flags
);
1093 static void setup_DMA(void)
1097 #ifdef FLOPPY_SANITY_CHECK
1098 if (raw_cmd
->length
== 0){
1101 printk("zero dma transfer size:");
1102 for (i
=0; i
< raw_cmd
->cmd_count
; i
++)
1103 printk("%x,", raw_cmd
->cmd
[i
]);
1109 if (((unsigned long) raw_cmd
->kernel_data
) % 512){
1110 printk("non aligned address: %p\n", raw_cmd
->kernel_data
);
1119 if (fd_dma_setup(raw_cmd
->kernel_data
, raw_cmd
->length
,
1120 (raw_cmd
->flags
& FD_RAW_READ
)?
1121 DMA_MODE_READ
: DMA_MODE_WRITE
,
1122 FDCS
->address
) < 0) {
1123 release_dma_lock(f
);
1128 release_dma_lock(f
);
1131 fd_cacheflush(raw_cmd
->kernel_data
, raw_cmd
->length
);
1132 fd_set_dma_mode((raw_cmd
->flags
& FD_RAW_READ
)?
1133 DMA_MODE_READ
: DMA_MODE_WRITE
);
1134 fd_set_dma_addr(raw_cmd
->kernel_data
);
1135 fd_set_dma_count(raw_cmd
->length
);
1136 virtual_dma_port
= FDCS
->address
;
1138 release_dma_lock(f
);
1140 floppy_disable_hlt();
1143 static void show_floppy(void);
1145 /* waits until the fdc becomes ready */
1146 static int wait_til_ready(void)
1148 int counter
, status
;
1151 for (counter
= 0; counter
< 10000; counter
++) {
1152 status
= fd_inb(FD_STATUS
);
1153 if (status
& STATUS_READY
)
1156 if (!initialising
) {
1157 DPRINT("Getstatus times out (%x) on fdc %d\n",
1165 /* sends a command byte to the fdc */
1166 static int output_byte(char byte
)
1170 if ((status
= wait_til_ready()) < 0)
1172 if ((status
& (STATUS_READY
|STATUS_DIR
|STATUS_DMA
)) == STATUS_READY
){
1173 fd_outb(byte
,FD_DATA
);
1174 #ifdef FLOPPY_SANITY_CHECK
1175 output_log
[output_log_pos
].data
= byte
;
1176 output_log
[output_log_pos
].status
= status
;
1177 output_log
[output_log_pos
].jiffies
= jiffies
;
1178 output_log_pos
= (output_log_pos
+ 1) % OLOGSIZE
;
1183 if (!initialising
) {
1184 DPRINT("Unable to send byte %x to FDC. Fdc=%x Status=%x\n",
1190 #define LAST_OUT(x) if (output_byte(x)<0){ reset_fdc();return;}
1192 /* gets the response from the fdc */
1193 static int result(void)
1197 for(i
=0; i
< MAX_REPLIES
; i
++) {
1198 if ((status
= wait_til_ready()) < 0)
1200 status
&= STATUS_DIR
|STATUS_READY
|STATUS_BUSY
|STATUS_DMA
;
1201 if ((status
& ~STATUS_BUSY
) == STATUS_READY
){
1202 #ifdef FLOPPY_SANITY_CHECK
1203 resultjiffies
= jiffies
;
1208 if (status
== (STATUS_DIR
|STATUS_READY
|STATUS_BUSY
))
1209 reply_buffer
[i
] = fd_inb(FD_DATA
);
1213 if (!initialising
) {
1214 DPRINT("get result error. Fdc=%d Last status=%x Read bytes=%d\n",
1222 #define MORE_OUTPUT -2
1223 /* does the fdc need more output? */
1224 static int need_more_output(void)
1227 if ((status
= wait_til_ready()) < 0)
1229 if ((status
& (STATUS_READY
|STATUS_DIR
|STATUS_DMA
)) == STATUS_READY
)
1234 /* Set perpendicular mode as required, based on data rate, if supported.
1235 * 82077 Now tested. 1Mbps data rate only possible with 82077-1.
1237 static inline void perpendicular_mode(void)
1239 unsigned char perp_mode
;
1241 if (raw_cmd
->rate
& 0x40){
1242 switch(raw_cmd
->rate
& 3){
1250 DPRINT("Invalid data rate for perpendicular mode!\n");
1252 FDCS
->reset
= 1; /* convenient way to return to
1253 * redo without to much hassle (deep
1260 if (FDCS
->perp_mode
== perp_mode
)
1262 if (FDCS
->version
>= FDC_82077_ORIG
) {
1263 output_byte(FD_PERPENDICULAR
);
1264 output_byte(perp_mode
);
1265 FDCS
->perp_mode
= perp_mode
;
1266 } else if (perp_mode
) {
1267 DPRINT("perpendicular mode not supported by this FDC.\n");
1269 } /* perpendicular_mode */
1271 static int fifo_depth
= 0xa;
1272 static int no_fifo
= 0;
1274 static int fdc_configure(void)
1277 output_byte(FD_CONFIGURE
);
1278 if (need_more_output() != MORE_OUTPUT
)
1281 output_byte(0x10 | (no_fifo
& 0x20) | (fifo_depth
& 0xf));
1282 output_byte(0); /* pre-compensation from track
1287 #define NOMINAL_DTR 500
1289 /* Issue a "SPECIFY" command to set the step rate time, head unload time,
1290 * head load time, and DMA disable flag to values needed by floppy.
1292 * The value "dtr" is the data transfer rate in Kbps. It is needed
1293 * to account for the data rate-based scaling done by the 82072 and 82077
1294 * FDC types. This parameter is ignored for other types of FDCs (i.e.
1297 * Note that changing the data transfer rate has a (probably deleterious)
1298 * effect on the parameters subject to scaling for 82072/82077 FDCs, so
1299 * fdc_specify is called again after each data transfer rate
1302 * srt: 1000 to 16000 in microseconds
1303 * hut: 16 to 240 milliseconds
1304 * hlt: 2 to 254 milliseconds
1306 * These values are rounded up to the next highest available delay time.
1308 static void fdc_specify(void)
1310 unsigned char spec1
, spec2
;
1311 unsigned long srt
, hlt
, hut
;
1312 unsigned long dtr
= NOMINAL_DTR
;
1313 unsigned long scale_dtr
= NOMINAL_DTR
;
1314 int hlt_max_code
= 0x7f;
1315 int hut_max_code
= 0xf;
1317 if (FDCS
->need_configure
&& FDCS
->version
>= FDC_82072A
) {
1319 FDCS
->need_configure
= 0;
1320 /*DPRINT("FIFO enabled\n");*/
1323 switch (raw_cmd
->rate
& 0x03) {
1329 if (FDCS
->version
>= FDC_82078
) {
1330 /* chose the default rate table, not the one
1331 * where 1 = 2 Mbps */
1332 output_byte(FD_DRIVESPEC
);
1333 if (need_more_output() == MORE_OUTPUT
) {
1334 output_byte(UNIT(current_drive
));
1344 if (FDCS
->version
>= FDC_82072
) {
1346 hlt_max_code
= 0x00; /* 0==256msec*dtr0/dtr (not linear!) */
1347 hut_max_code
= 0x0; /* 0==256msec*dtr0/dtr (not linear!) */
1350 /* Convert step rate from microseconds to milliseconds and 4 bits */
1351 srt
= 16 - (DP
->srt
*scale_dtr
/1000 + NOMINAL_DTR
- 1)/NOMINAL_DTR
;
1358 hlt
= (DP
->hlt
*scale_dtr
/2 + NOMINAL_DTR
- 1)/NOMINAL_DTR
;
1361 else if (hlt
> 0x7f)
1364 hut
= (DP
->hut
*scale_dtr
/16 + NOMINAL_DTR
- 1)/NOMINAL_DTR
;
1370 spec1
= (srt
<< 4) | hut
;
1371 spec2
= (hlt
<< 1) | (use_virtual_dma
& 1);
1373 /* If these parameters did not change, just return with success */
1374 if (FDCS
->spec1
!= spec1
|| FDCS
->spec2
!= spec2
) {
1375 /* Go ahead and set spec1 and spec2 */
1376 output_byte(FD_SPECIFY
);
1377 output_byte(FDCS
->spec1
= spec1
);
1378 output_byte(FDCS
->spec2
= spec2
);
1382 /* Set the FDC's data transfer rate on behalf of the specified drive.
1383 * NOTE: with 82072/82077 FDCs, changing the data rate requires a reissue
1384 * of the specify command (i.e. using the fdc_specify function).
1386 static int fdc_dtr(void)
1388 /* If data rate not already set to desired value, set it. */
1389 if ((raw_cmd
->rate
& 3) == FDCS
->dtr
)
1393 fd_outb(raw_cmd
->rate
& 3, FD_DCR
);
1395 /* TODO: some FDC/drive combinations (C&T 82C711 with TEAC 1.2MB)
1396 * need a stabilization period of several milliseconds to be
1397 * enforced after data rate changes before R/W operations.
1398 * Pause 5 msec to avoid trouble. (Needs to be 2 jiffies)
1400 FDCS
->dtr
= raw_cmd
->rate
& 3;
1401 return(wait_for_completion(jiffies
+2UL*HZ
/100,
1402 (timeout_fn
) floppy_ready
));
1405 static void tell_sector(void)
1407 printk(": track %d, head %d, sector %d, size %d",
1408 R_TRACK
, R_HEAD
, R_SECTOR
, R_SIZECODE
);
1413 * OK, this error interpreting routine is called after a
1414 * DMA read/write has succeeded
1415 * or failed, so we check the results, and copy any buffers.
1416 * hhb: Added better error reporting.
1417 * ak: Made this into a separate routine.
1419 static int interpret_errors(void)
1424 DPRINT("-- FDC reply error");
1429 /* check IC to find cause of interrupt */
1430 switch (ST0
& ST0_INTR
) {
1431 case 0x40: /* error occurred during command execution */
1433 return 0; /* occurs with pseudo-DMA */
1436 DPRINT("Drive is write protected\n");
1437 CLEARF(FD_DISK_WRITABLE
);
1440 } else if (ST1
& ST1_ND
) {
1441 SETF(FD_NEED_TWADDLE
);
1442 } else if (ST1
& ST1_OR
) {
1443 if (DP
->flags
& FTD_MSG
)
1444 DPRINT("Over/Underrun - retrying\n");
1446 }else if (*errors
>= DP
->max_errors
.reporting
){
1448 if (ST0
& ST0_ECE
) {
1449 printk("Recalibrate failed!");
1450 } else if (ST2
& ST2_CRC
) {
1451 printk("data CRC error");
1453 } else if (ST1
& ST1_CRC
) {
1454 printk("CRC error");
1456 } else if ((ST1
& (ST1_MAM
|ST1_ND
)) || (ST2
& ST2_MAM
)) {
1458 printk("sector not found");
1461 printk("probe failed...");
1462 } else if (ST2
& ST2_WC
) { /* seek error */
1463 printk("wrong cylinder");
1464 } else if (ST2
& ST2_BC
) { /* cylinder marked as bad */
1465 printk("bad cylinder");
1467 printk("unknown error. ST[0..2] are: 0x%x 0x%x 0x%x", ST0
, ST1
, ST2
);
1473 if (ST2
& ST2_WC
|| ST2
& ST2_BC
)
1474 /* wrong cylinder => recal */
1475 DRS
->track
= NEED_2_RECAL
;
1477 case 0x80: /* invalid command given */
1478 DPRINT("Invalid FDC command given!\n");
1482 DPRINT("Abnormal termination caused by polling\n");
1485 default: /* (0) Normal command termination */
1491 * This routine is called when everything should be correctly set up
1492 * for the transfer (i.e. floppy motor is on, the correct floppy is
1493 * selected, and the head is sitting on the right track).
1495 static void setup_rw_floppy(void)
1497 int i
,r
, flags
,dflags
;
1498 unsigned long ready_date
;
1499 timeout_fn function
;
1501 flags
= raw_cmd
->flags
;
1502 if (flags
& (FD_RAW_READ
| FD_RAW_WRITE
))
1503 flags
|= FD_RAW_INTR
;
1505 if ((flags
& FD_RAW_SPIN
) && !(flags
& FD_RAW_NO_MOTOR
)){
1506 ready_date
= DRS
->spinup_date
+ DP
->spinup
;
1507 /* If spinup will take a long time, rerun scandrives
1508 * again just before spinup completion. Beware that
1509 * after scandrives, we must again wait for selection.
1511 if ((signed) (ready_date
- jiffies
) > DP
->select_delay
){
1512 ready_date
-= DP
->select_delay
;
1513 function
= (timeout_fn
) floppy_start
;
1515 function
= (timeout_fn
) setup_rw_floppy
;
1517 /* wait until the floppy is spinning fast enough */
1518 if (wait_for_completion(ready_date
,function
))
1521 dflags
= DRS
->flags
;
1523 if ((flags
& FD_RAW_READ
) || (flags
& FD_RAW_WRITE
))
1526 if (flags
& FD_RAW_INTR
)
1527 SET_INTR(main_command_interrupt
);
1530 for (i
=0; i
< raw_cmd
->cmd_count
; i
++)
1531 r
|=output_byte(raw_cmd
->cmd
[i
]);
1534 debugt("rw_command: ");
1542 if (!(flags
& FD_RAW_INTR
)){
1545 } else if (flags
& FD_RAW_NEED_DISK
)
1549 static int blind_seek
;
1552 * This is the routine called after every seek (or recalibrate) interrupt
1553 * from the floppy controller.
1555 static void seek_interrupt(void)
1558 debugt("seek interrupt:");
1560 if (inr
!= 2 || (ST0
& 0xF8) != 0x20) {
1561 DPRINT("seek failed\n");
1562 DRS
->track
= NEED_2_RECAL
;
1567 if (DRS
->track
>= 0 && DRS
->track
!= ST1
&& !blind_seek
){
1569 if (DP
->flags
& FD_DEBUG
){
1570 DPRINT("clearing NEWCHANGE flag because of effective seek\n");
1571 DPRINT("jiffies=%lu\n", jiffies
);
1574 CLEARF(FD_DISK_NEWCHANGE
); /* effective seek */
1575 DRS
->select_date
= jiffies
;
1581 static void check_wp(void)
1583 if (TESTF(FD_VERIFY
)) {
1584 /* check write protection */
1585 output_byte(FD_GETSTATUS
);
1586 output_byte(UNIT(current_drive
));
1592 CLEARF(FD_NEED_TWADDLE
);
1594 if (DP
->flags
& FD_DEBUG
){
1595 DPRINT("checking whether disk is write protected\n");
1596 DPRINT("wp=%x\n",ST3
& 0x40);
1600 SETF(FD_DISK_WRITABLE
);
1602 CLEARF(FD_DISK_WRITABLE
);
1606 static void seek_floppy(void)
1613 if (DP
->flags
& FD_DEBUG
){
1614 DPRINT("calling disk change from seek\n");
1618 if (!TESTF(FD_DISK_NEWCHANGE
) &&
1619 disk_change(current_drive
) &&
1620 (raw_cmd
->flags
& FD_RAW_NEED_DISK
)){
1621 /* the media changed flag should be cleared after the seek.
1622 * If it isn't, this means that there is really no disk in
1625 SETF(FD_DISK_CHANGED
);
1630 if (DRS
->track
<= NEED_1_RECAL
){
1631 recalibrate_floppy();
1633 } else if (TESTF(FD_DISK_NEWCHANGE
) &&
1634 (raw_cmd
->flags
& FD_RAW_NEED_DISK
) &&
1635 (DRS
->track
<= NO_TRACK
|| DRS
->track
== raw_cmd
->track
)) {
1636 /* we seek to clear the media-changed condition. Does anybody
1637 * know a more elegant way, which works on all drives? */
1639 track
= raw_cmd
->track
- 1;
1641 if (DP
->flags
& FD_SILENT_DCL_CLEAR
){
1642 set_dor(fdc
, ~(0x10 << UNIT(current_drive
)), 0);
1644 raw_cmd
->flags
|= FD_RAW_NEED_SEEK
;
1650 if (raw_cmd
->track
!= DRS
->track
&&
1651 (raw_cmd
->flags
& FD_RAW_NEED_SEEK
))
1652 track
= raw_cmd
->track
;
1659 SET_INTR(seek_interrupt
);
1660 output_byte(FD_SEEK
);
1661 output_byte(UNIT(current_drive
));
1664 debugt("seek command:");
1668 static void recal_interrupt(void)
1671 debugt("recal interrupt:");
1675 else if (ST0
& ST0_ECE
) {
1679 debugt("recal interrupt need 1 recal:");
1681 /* after a second recalibrate, we still haven't
1682 * reached track 0. Probably no drive. Raise an
1683 * error, as failing immediately might upset
1684 * computers possessed by the Devil :-) */
1690 debugt("recal interrupt need 2 recal:");
1692 /* If we already did a recalibrate,
1693 * and we are not at track 0, this
1694 * means we have moved. (The only way
1695 * not to move at recalibration is to
1696 * be already at track 0.) Clear the
1697 * new change flag */
1699 if (DP
->flags
& FD_DEBUG
){
1700 DPRINT("clearing NEWCHANGE flag because of second recalibrate\n");
1704 CLEARF(FD_DISK_NEWCHANGE
);
1705 DRS
->select_date
= jiffies
;
1709 debugt("recal interrupt default:");
1711 /* Recalibrate moves the head by at
1712 * most 80 steps. If after one
1713 * recalibrate we don't have reached
1714 * track 0, this might mean that we
1715 * started beyond track 80. Try
1717 DRS
->track
= NEED_1_RECAL
;
1725 static void print_result(char *message
, int inr
)
1729 DPRINT("%s ", message
);
1731 for (i
=0; i
<inr
; i
++)
1732 printk("repl[%d]=%x ", i
, reply_buffer
[i
]);
1736 /* interrupt handler. Note that this can be called externally on the Sparc */
1737 void floppy_interrupt(int irq
, void *dev_id
, struct pt_regs
* regs
)
1739 void (*handler
)(void) = DEVICE_INTR
;
1743 lasthandler
= handler
;
1744 interruptjiffies
= jiffies
;
1748 release_dma_lock(f
);
1750 floppy_enable_hlt();
1752 if (fdc
>= N_FDC
|| FDCS
->address
== -1){
1753 /* we don't even know which FDC is the culprit */
1754 printk("DOR0=%x\n", fdc_state
[0].dor
);
1755 printk("floppy interrupt on bizarre fdc %d\n",fdc
);
1756 printk("handler=%p\n", handler
);
1757 is_alive("bizarre fdc");
1762 /* We have to clear the reset flag here, because apparently on boxes
1763 * with level triggered interrupts (PS/2, Sparc, ...), it is needed to
1764 * emit SENSEI's to clear the interrupt line. And FDCS->reset blocks the
1765 * emission of the SENSEI's.
1766 * It is OK to emit floppy commands because we are in an interrupt
1767 * handler here, and thus we have to fear no interference of other
1771 do_print
= !handler
&& print_unex
&& !initialising
;
1775 print_result("unexpected interrupt", inr
);
1779 output_byte(FD_SENSEI
);
1782 print_result("sensei", inr
);
1784 } while ((ST0
& 0x83) != UNIT(current_drive
) && inr
== 2 && max_sensei
);
1787 schedule_bh( (void *)(void *) handler
);
1790 is_alive("normal interrupt end");
1793 static void recalibrate_floppy(void)
1796 debugt("recalibrate floppy:");
1798 SET_INTR(recal_interrupt
);
1799 output_byte(FD_RECALIBRATE
);
1800 LAST_OUT(UNIT(current_drive
));
1804 * Must do 4 FD_SENSEIs after reset because of ``drive polling''.
1806 static void reset_interrupt(void)
1809 debugt("reset interrupt:");
1811 result(); /* get the status ready for set_fdc */
1813 printk("reset set in interrupt, calling %p\n", cont
->error
);
1814 cont
->error(); /* a reset just after a reset. BAD! */
1820 * reset is done by pulling bit 2 of DOR low for a while (old FDCs),
1821 * or by setting the self clearing bit 7 of STATUS (newer FDCs)
1823 static void reset_fdc(void)
1825 unsigned long flags
;
1827 SET_INTR(reset_interrupt
);
1831 /* Pseudo-DMA may intercept 'reset finished' interrupt. */
1832 /* Irrelevant for systems with true DMA (i386). */
1834 flags
=claim_dma_lock();
1836 release_dma_lock(flags
);
1838 if (FDCS
->version
>= FDC_82072A
)
1839 fd_outb(0x80 | (FDCS
->dtr
&3), FD_STATUS
);
1841 fd_outb(FDCS
->dor
& ~0x04, FD_DOR
);
1842 udelay(FD_RESET_DELAY
);
1843 fd_outb(FDCS
->dor
, FD_DOR
);
1847 static void show_floppy(void)
1852 printk("floppy driver state\n");
1853 printk("-------------------\n");
1854 printk("now=%lu last interrupt=%lu diff=%lu last called handler=%p\n",
1855 jiffies
, interruptjiffies
, jiffies
-interruptjiffies
, lasthandler
);
1858 #ifdef FLOPPY_SANITY_CHECK
1859 printk("timeout_message=%s\n", timeout_message
);
1860 printk("last output bytes:\n");
1861 for (i
=0; i
< OLOGSIZE
; i
++)
1862 printk("%2x %2x %lu\n",
1863 output_log
[(i
+output_log_pos
) % OLOGSIZE
].data
,
1864 output_log
[(i
+output_log_pos
) % OLOGSIZE
].status
,
1865 output_log
[(i
+output_log_pos
) % OLOGSIZE
].jiffies
);
1866 printk("last result at %lu\n", resultjiffies
);
1867 printk("last redo_fd_request at %lu\n", lastredo
);
1868 for (i
=0; i
<resultsize
; i
++){
1869 printk("%2x ", reply_buffer
[i
]);
1874 printk("status=%x\n", fd_inb(FD_STATUS
));
1875 printk("fdc_busy=%lu\n", fdc_busy
);
1877 printk("DEVICE_INTR=%p\n", DEVICE_INTR
);
1879 printk("floppy_tq.routine=%p\n", floppy_tq
.routine
);
1880 if (timer_pending(&fd_timer
))
1881 printk("fd_timer.function=%p\n", fd_timer
.function
);
1882 if (timer_pending(&fd_timeout
)){
1883 printk("timer_function=%p\n",fd_timeout
.function
);
1884 printk("expires=%lu\n",fd_timeout
.expires
-jiffies
);
1885 printk("now=%lu\n",jiffies
);
1887 printk("cont=%p\n", cont
);
1888 printk("CURRENT=%p\n", CURRENT
);
1889 printk("command_status=%d\n", command_status
);
1893 static void floppy_shutdown(void)
1895 unsigned long flags
;
1901 floppy_enable_hlt();
1903 flags
=claim_dma_lock();
1905 release_dma_lock(flags
);
1907 /* avoid dma going to a random drive after shutdown */
1910 DPRINT("floppy timeout called\n");
1914 cont
->redo(); /* this will recall reset when needed */
1916 printk("no cont in shutdown!\n");
1917 process_fd_request();
1919 is_alive("floppy shutdown");
1921 /*typedef void (*timeout_fn)(unsigned long);*/
1923 /* start motor, check media-changed condition and write protection */
1924 static int start_motor(void (*function
)(void) )
1929 data
= UNIT(current_drive
);
1930 if (!(raw_cmd
->flags
& FD_RAW_NO_MOTOR
)){
1931 if (!(FDCS
->dor
& (0x10 << UNIT(current_drive
)))){
1933 /* no read since this drive is running */
1934 DRS
->first_read_date
= 0;
1935 /* note motor start time if motor is not yet running */
1936 DRS
->spinup_date
= jiffies
;
1937 data
|= (0x10 << UNIT(current_drive
));
1940 if (FDCS
->dor
& (0x10 << UNIT(current_drive
)))
1941 mask
&= ~(0x10 << UNIT(current_drive
));
1943 /* starts motor and selects floppy */
1944 del_timer(motor_off_timer
+ current_drive
);
1945 set_dor(fdc
, mask
, data
);
1947 /* wait_for_completion also schedules reset if needed. */
1948 return(wait_for_completion(DRS
->select_date
+DP
->select_delay
,
1949 (timeout_fn
) function
));
1952 static void floppy_ready(void)
1955 if (start_motor(floppy_ready
)) return;
1956 if (fdc_dtr()) return;
1959 if (DP
->flags
& FD_DEBUG
){
1960 DPRINT("calling disk change from floppy_ready\n");
1963 if (!(raw_cmd
->flags
& FD_RAW_NO_MOTOR
) &&
1964 disk_change(current_drive
) &&
1966 twaddle(); /* this clears the dcl on certain drive/controller
1969 #ifdef fd_chose_dma_mode
1970 if ((raw_cmd
->flags
& FD_RAW_READ
) ||
1971 (raw_cmd
->flags
& FD_RAW_WRITE
))
1973 unsigned long flags
= claim_dma_lock();
1974 fd_chose_dma_mode(raw_cmd
->kernel_data
,
1976 release_dma_lock(flags
);
1980 if (raw_cmd
->flags
& (FD_RAW_NEED_SEEK
| FD_RAW_NEED_DISK
)){
1981 perpendicular_mode();
1982 fdc_specify(); /* must be done here because of hut, hlt ... */
1985 if ((raw_cmd
->flags
& FD_RAW_READ
) ||
1986 (raw_cmd
->flags
& FD_RAW_WRITE
))
1992 static void floppy_start(void)
1994 reschedule_timeout(CURRENTD
, "floppy start", 0);
1998 if (DP
->flags
& FD_DEBUG
){
1999 DPRINT("setting NEWCHANGE in floppy_start\n");
2002 SETF(FD_DISK_NEWCHANGE
);
2007 * ========================================================================
2008 * here ends the bottom half. Exported routines are:
2009 * floppy_start, floppy_off, floppy_ready, lock_fdc, unlock_fdc, set_fdc,
2010 * start_motor, reset_fdc, reset_fdc_info, interpret_errors.
2011 * Initialization also uses output_byte, result, set_dor, floppy_interrupt
2013 * ========================================================================
2016 * General purpose continuations.
2017 * ==============================
2020 static void do_wakeup(void)
2022 reschedule_timeout(MAXTIMEOUT
, "do wakeup", 0);
2024 command_status
+= 2;
2025 wake_up(&command_done
);
2028 static struct cont_t wakeup_cont
={
2036 static struct cont_t intr_cont
={
2043 static int wait_til_done(void (*handler
)(void), int interruptible
)
2047 schedule_bh((void *)(void *)handler
);
2049 if (command_status
< 2 && NO_SIGNAL
) {
2050 DECLARE_WAITQUEUE(wait
, current
);
2052 add_wait_queue(&command_done
, &wait
);
2054 set_current_state(interruptible
?
2056 TASK_UNINTERRUPTIBLE
);
2058 if (command_status
>= 2 || !NO_SIGNAL
)
2061 is_alive("wait_til_done");
2066 set_current_state(TASK_RUNNING
);
2067 remove_wait_queue(&command_done
, &wait
);
2070 if (command_status
< 2){
2078 command_status
= FD_COMMAND_ERROR
;
2079 if (command_status
== FD_COMMAND_OKAY
)
2083 command_status
= FD_COMMAND_NONE
;
2087 static void generic_done(int result
)
2089 command_status
= result
;
2090 cont
= &wakeup_cont
;
2093 static void generic_success(void)
2098 static void generic_failure(void)
2103 static void success_and_wakeup(void)
2111 * formatting and rw support.
2112 * ==========================
2115 static int next_valid_format(void)
2119 probed_format
= DRS
->probed_format
;
2121 if (probed_format
>= 8 ||
2122 !DP
->autodetect
[probed_format
]){
2123 DRS
->probed_format
= 0;
2126 if (floppy_type
[DP
->autodetect
[probed_format
]].sect
){
2127 DRS
->probed_format
= probed_format
;
2134 static void bad_flp_intr(void)
2137 DRS
->probed_format
++;
2138 if (!next_valid_format())
2142 INFBOUND(DRWE
->badness
, *errors
);
2143 if (*errors
> DP
->max_errors
.abort
)
2145 if (*errors
> DP
->max_errors
.reset
)
2147 else if (*errors
> DP
->max_errors
.recal
)
2148 DRS
->track
= NEED_2_RECAL
;
2151 static void set_floppy(kdev_t device
)
2154 _floppy
= TYPE(device
) + floppy_type
;
2156 _floppy
= current_type
[ DRIVE(device
) ];
2160 * formatting support.
2161 * ===================
2163 static void format_interrupt(void)
2165 switch (interpret_errors()){
2176 #define CODE2SIZE (ssize = ((1 << SIZECODE) + 3) >> 2)
2177 #define FM_MODE(x,y) ((y) & ~(((x)->rate & 0x80) >>1))
2178 #define CT(x) ((x) | 0xc0)
2179 static void setup_format_params(int track
)
2182 unsigned char track
,head
,sect
,size
;
2183 } *here
= (struct fparm
*)floppy_track_buffer
;
2185 int count
,head_shift
,track_shift
;
2187 raw_cmd
= &default_raw_cmd
;
2188 raw_cmd
->track
= track
;
2190 raw_cmd
->flags
= FD_RAW_WRITE
| FD_RAW_INTR
| FD_RAW_SPIN
|
2191 FD_RAW_NEED_DISK
| FD_RAW_NEED_SEEK
;
2192 raw_cmd
->rate
= _floppy
->rate
& 0x43;
2193 raw_cmd
->cmd_count
= NR_F
;
2194 COMMAND
= FM_MODE(_floppy
,FD_FORMAT
);
2195 DR_SELECT
= UNIT(current_drive
) + PH_HEAD(_floppy
,format_req
.head
);
2196 F_SIZECODE
= FD_SIZECODE(_floppy
);
2197 F_SECT_PER_TRACK
= _floppy
->sect
<< 2 >> F_SIZECODE
;
2198 F_GAP
= _floppy
->fmt_gap
;
2199 F_FILL
= FD_FILL_BYTE
;
2201 raw_cmd
->kernel_data
= floppy_track_buffer
;
2202 raw_cmd
->length
= 4 * F_SECT_PER_TRACK
;
2204 /* allow for about 30ms for data transport per track */
2205 head_shift
= (F_SECT_PER_TRACK
+ 5) / 6;
2207 /* a ``cylinder'' is two tracks plus a little stepping time */
2208 track_shift
= 2 * head_shift
+ 3;
2210 /* position of logical sector 1 on this track */
2211 n
= (track_shift
* format_req
.track
+ head_shift
* format_req
.head
)
2214 /* determine interleave */
2216 if (_floppy
->fmt_gap
< 0x22)
2219 /* initialize field */
2220 for (count
= 0; count
< F_SECT_PER_TRACK
; ++count
) {
2221 here
[count
].track
= format_req
.track
;
2222 here
[count
].head
= format_req
.head
;
2223 here
[count
].sect
= 0;
2224 here
[count
].size
= F_SIZECODE
;
2226 /* place logical sectors */
2227 for (count
= 1; count
<= F_SECT_PER_TRACK
; ++count
) {
2228 here
[n
].sect
= count
;
2229 n
= (n
+il
) % F_SECT_PER_TRACK
;
2230 if (here
[n
].sect
) { /* sector busy, find next free sector */
2232 if (n
>= F_SECT_PER_TRACK
) {
2233 n
-=F_SECT_PER_TRACK
;
2234 while (here
[n
].sect
) ++n
;
2240 static void redo_format(void)
2243 setup_format_params(format_req
.track
<< STRETCH(_floppy
));
2246 debugt("queue format request");
2250 static struct cont_t format_cont
={
2256 static int do_format(kdev_t device
, struct format_descr
*tmp_format_req
)
2259 int drive
=DRIVE(device
);
2264 _floppy
->track
> DP
->tracks
||
2265 tmp_format_req
->track
>= _floppy
->track
||
2266 tmp_format_req
->head
>= _floppy
->head
||
2267 (_floppy
->sect
<< 2) % (1 << FD_SIZECODE(_floppy
)) ||
2268 !_floppy
->fmt_gap
) {
2269 process_fd_request();
2272 format_req
= *tmp_format_req
;
2274 cont
= &format_cont
;
2275 errors
= &format_errors
;
2277 process_fd_request();
2282 * Buffer read/write and support
2283 * =============================
2286 /* new request_done. Can handle physical sectors which are smaller than a
2288 static void request_done(int uptodate
)
2293 reschedule_timeout(MAXTIMEOUT
, "request done %d", uptodate
);
2296 DPRINT("request list destroyed in floppy request done\n");
2301 /* maintain values for invalidation on geometry
2303 block
= current_count_sectors
+ CURRENT
->sector
;
2304 INFBOUND(DRS
->maxblock
, block
);
2305 if (block
> _floppy
->sect
)
2308 /* unlock chained buffers */
2309 while (current_count_sectors
&& !QUEUE_EMPTY
&&
2310 current_count_sectors
>= CURRENT
->current_nr_sectors
){
2311 current_count_sectors
-= CURRENT
->current_nr_sectors
;
2312 CURRENT
->nr_sectors
-= CURRENT
->current_nr_sectors
;
2313 CURRENT
->sector
+= CURRENT
->current_nr_sectors
;
2316 if (current_count_sectors
&& !QUEUE_EMPTY
){
2317 /* "unlock" last subsector */
2318 CURRENT
->buffer
+= current_count_sectors
<<9;
2319 CURRENT
->current_nr_sectors
-= current_count_sectors
;
2320 CURRENT
->nr_sectors
-= current_count_sectors
;
2321 CURRENT
->sector
+= current_count_sectors
;
2325 if (current_count_sectors
&& QUEUE_EMPTY
)
2326 DPRINT("request list destroyed in floppy request done\n");
2329 if (CURRENT
->cmd
== WRITE
) {
2330 /* record write error information */
2331 DRWE
->write_errors
++;
2332 if (DRWE
->write_errors
== 1) {
2333 DRWE
->first_error_sector
= CURRENT
->sector
;
2334 DRWE
->first_error_generation
= DRS
->generation
;
2336 DRWE
->last_error_sector
= CURRENT
->sector
;
2337 DRWE
->last_error_generation
= DRS
->generation
;
2343 /* Interrupt handler evaluating the result of the r/w operation */
2344 static void rw_interrupt(void)
2346 int nr_sectors
, ssize
, eoc
, heads
;
2349 /* some Toshiba floppy controllers occasionnally seem to
2350 * return bogus interrupts after read/write operations, which
2351 * can be recognized by a bad head number (>= 2) */
2355 if (!DRS
->first_read_date
)
2356 DRS
->first_read_date
= jiffies
;
2371 nr_sectors
= (((R_TRACK
-TRACK
) * heads
+
2372 R_HEAD
-HEAD
) * SECT_PER_TRACK
+
2373 R_SECTOR
-SECTOR
+ eoc
) << SIZECODE
>> 2;
2375 #ifdef FLOPPY_SANITY_CHECK
2376 if (nr_sectors
/ ssize
>
2377 (in_sector_offset
+ current_count_sectors
+ ssize
- 1) / ssize
) {
2378 DPRINT("long rw: %x instead of %lx\n",
2379 nr_sectors
, current_count_sectors
);
2380 printk("rs=%d s=%d\n", R_SECTOR
, SECTOR
);
2381 printk("rh=%d h=%d\n", R_HEAD
, HEAD
);
2382 printk("rt=%d t=%d\n", R_TRACK
, TRACK
);
2383 printk("heads=%d eoc=%d\n", heads
, eoc
);
2384 printk("spt=%d st=%d ss=%d\n", SECT_PER_TRACK
,
2386 printk("in_sector_offset=%d\n", in_sector_offset
);
2390 nr_sectors
-= in_sector_offset
;
2391 INFBOUND(nr_sectors
,0);
2392 SUPBOUND(current_count_sectors
, nr_sectors
);
2394 switch (interpret_errors()){
2399 if (!current_count_sectors
){
2406 if (!current_count_sectors
){
2410 current_type
[current_drive
] = _floppy
;
2411 floppy_sizes
[TOMINOR(current_drive
) ]=
2412 (_floppy
->size
+1)>>1;
2417 if (DP
->flags
& FTD_MSG
)
2418 DPRINT("Auto-detected floppy type %s in fd%d\n",
2419 _floppy
->name
,current_drive
);
2420 current_type
[current_drive
] = _floppy
;
2421 floppy_sizes
[TOMINOR(current_drive
)] = (_floppy
->size
+1) >> 1;
2425 if (CT(COMMAND
) != FD_READ
||
2426 raw_cmd
->kernel_data
== CURRENT
->buffer
){
2427 /* transfer directly from buffer */
2429 } else if (CT(COMMAND
) == FD_READ
){
2430 buffer_track
= raw_cmd
->track
;
2431 buffer_drive
= current_drive
;
2432 INFBOUND(buffer_max
, nr_sectors
+ sector_t
);
2437 /* Compute maximal contiguous buffer size. */
2438 static int buffer_chain_size(void)
2440 struct buffer_head
*bh
;
2444 base
= CURRENT
->buffer
;
2445 size
= CURRENT
->current_nr_sectors
<< 9;
2450 while (bh
&& bh
->b_data
== base
+ size
){
2458 /* Compute the maximal transfer size */
2459 static int transfer_size(int ssize
, int max_sector
, int max_size
)
2461 SUPBOUND(max_sector
, sector_t
+ max_size
);
2464 max_sector
-= (max_sector
% _floppy
->sect
) % ssize
;
2466 /* transfer size, beginning not aligned */
2467 current_count_sectors
= max_sector
- sector_t
;
2473 * Move data from/to the track buffer to/from the buffer cache.
2475 static void copy_buffer(int ssize
, int max_sector
, int max_sector_2
)
2477 int remaining
; /* number of transferred 512-byte sectors */
2478 struct buffer_head
*bh
;
2479 char *buffer
, *dma_buffer
;
2482 max_sector
= transfer_size(ssize
,
2483 minimum(max_sector
, max_sector_2
),
2484 CURRENT
->nr_sectors
);
2486 if (current_count_sectors
<= 0 && CT(COMMAND
) == FD_WRITE
&&
2487 buffer_max
> sector_t
+ CURRENT
->nr_sectors
)
2488 current_count_sectors
= minimum(buffer_max
- sector_t
,
2489 CURRENT
->nr_sectors
);
2491 remaining
= current_count_sectors
<< 9;
2492 #ifdef FLOPPY_SANITY_CHECK
2493 if ((remaining
>> 9) > CURRENT
->nr_sectors
&&
2494 CT(COMMAND
) == FD_WRITE
){
2495 DPRINT("in copy buffer\n");
2496 printk("current_count_sectors=%ld\n", current_count_sectors
);
2497 printk("remaining=%d\n", remaining
>> 9);
2498 printk("CURRENT->nr_sectors=%ld\n",CURRENT
->nr_sectors
);
2499 printk("CURRENT->current_nr_sectors=%ld\n",
2500 CURRENT
->current_nr_sectors
);
2501 printk("max_sector=%d\n", max_sector
);
2502 printk("ssize=%d\n", ssize
);
2506 buffer_max
= maximum(max_sector
, buffer_max
);
2508 dma_buffer
= floppy_track_buffer
+ ((sector_t
- buffer_min
) << 9);
2511 size
= CURRENT
->current_nr_sectors
<< 9;
2512 buffer
= CURRENT
->buffer
;
2514 while (remaining
> 0){
2515 SUPBOUND(size
, remaining
);
2516 #ifdef FLOPPY_SANITY_CHECK
2517 if (dma_buffer
+ size
>
2518 floppy_track_buffer
+ (max_buffer_sectors
<< 10) ||
2519 dma_buffer
< floppy_track_buffer
){
2520 DPRINT("buffer overrun in copy buffer %d\n",
2521 (int) ((floppy_track_buffer
- dma_buffer
) >>9));
2522 printk("sector_t=%d buffer_min=%d\n",
2523 sector_t
, buffer_min
);
2524 printk("current_count_sectors=%ld\n",
2525 current_count_sectors
);
2526 if (CT(COMMAND
) == FD_READ
)
2528 if (CT(COMMAND
) == FD_READ
)
2532 if (((unsigned long)buffer
) % 512)
2533 DPRINT("%p buffer not aligned\n", buffer
);
2535 if (CT(COMMAND
) == FD_READ
)
2536 memcpy(buffer
, dma_buffer
, size
);
2538 memcpy(dma_buffer
, buffer
, size
);
2545 #ifdef FLOPPY_SANITY_CHECK
2547 DPRINT("bh=null in copy buffer after copy\n");
2552 buffer
= bh
->b_data
;
2554 #ifdef FLOPPY_SANITY_CHECK
2557 max_sector
-= remaining
>> 9;
2558 DPRINT("weirdness: remaining %d\n", remaining
>>9);
2564 static inline int check_dma_crossing(char *start
,
2565 unsigned long length
, char *message
)
2567 if (CROSS_64KB(start
, length
)) {
2568 printk("DMA xfer crosses 64KB boundary in %s %p-%p\n",
2569 message
, start
, start
+length
);
2576 /* work around a bug in pseudo DMA
2577 * (on some FDCs) pseudo DMA does not stop when the CPU stops
2578 * sending data. Hence we need a different way to signal the
2579 * transfer length: We use SECT_PER_TRACK. Unfortunately, this
2580 * does not work with MT, hence we can only transfer one head at
2583 static void virtualdmabug_workaround(void)
2585 int hard_sectors
, end_sector
;
2587 if(CT(COMMAND
) == FD_WRITE
) {
2588 COMMAND
&= ~0x80; /* switch off multiple track mode */
2590 hard_sectors
= raw_cmd
->length
>> (7 + SIZECODE
);
2591 end_sector
= SECTOR
+ hard_sectors
- 1;
2592 #ifdef FLOPPY_SANITY_CHECK
2593 if(end_sector
> SECT_PER_TRACK
) {
2594 printk("too many sectors %d > %d\n",
2595 end_sector
, SECT_PER_TRACK
);
2599 SECT_PER_TRACK
= end_sector
; /* make sure SECT_PER_TRACK points
2600 * to end of transfer */
2605 * Formulate a read/write request.
2606 * this routine decides where to load the data (directly to buffer, or to
2607 * tmp floppy area), how much data to load (the size of the buffer, the whole
2608 * track, or a single sector)
2609 * All floppy_track_buffer handling goes in here. If we ever add track buffer
2610 * allocation on the fly, it should be done here. No other part should need
2614 static int make_raw_rw_request(void)
2616 int aligned_sector_t
;
2617 int max_sector
, max_size
, tracksize
, ssize
;
2619 if(max_buffer_sectors
== 0) {
2620 printk("VFS: Block I/O scheduled on unopened device\n");
2624 set_fdc(DRIVE(CURRENT
->rq_dev
));
2626 raw_cmd
= &default_raw_cmd
;
2627 raw_cmd
->flags
= FD_RAW_SPIN
| FD_RAW_NEED_DISK
| FD_RAW_NEED_DISK
|
2629 raw_cmd
->cmd_count
= NR_RW
;
2630 if (CURRENT
->cmd
== READ
){
2631 raw_cmd
->flags
|= FD_RAW_READ
;
2632 COMMAND
= FM_MODE(_floppy
,FD_READ
);
2633 } else if (CURRENT
->cmd
== WRITE
){
2634 raw_cmd
->flags
|= FD_RAW_WRITE
;
2635 COMMAND
= FM_MODE(_floppy
,FD_WRITE
);
2637 DPRINT("make_raw_rw_request: unknown command\n");
2641 max_sector
= _floppy
->sect
* _floppy
->head
;
2643 TRACK
= CURRENT
->sector
/ max_sector
;
2644 sector_t
= CURRENT
->sector
% max_sector
;
2645 if (_floppy
->track
&& TRACK
>= _floppy
->track
) {
2646 if (CURRENT
->current_nr_sectors
& 1) {
2647 current_count_sectors
= 1;
2652 HEAD
= sector_t
/ _floppy
->sect
;
2654 if (((_floppy
->stretch
& FD_SWAPSIDES
) || TESTF(FD_NEED_TWADDLE
)) &&
2655 sector_t
< _floppy
->sect
)
2656 max_sector
= _floppy
->sect
;
2658 /* 2M disks have phantom sectors on the first track */
2659 if ((_floppy
->rate
& FD_2M
) && (!TRACK
) && (!HEAD
)){
2660 max_sector
= 2 * _floppy
->sect
/ 3;
2661 if (sector_t
>= max_sector
){
2662 current_count_sectors
= minimum(_floppy
->sect
- sector_t
,
2663 CURRENT
->nr_sectors
);
2668 SIZECODE
= FD_SIZECODE(_floppy
);
2669 raw_cmd
->rate
= _floppy
->rate
& 0x43;
2670 if ((_floppy
->rate
& FD_2M
) &&
2679 raw_cmd
->track
= TRACK
<< STRETCH(_floppy
);
2680 DR_SELECT
= UNIT(current_drive
) + PH_HEAD(_floppy
,HEAD
);
2683 SECT_PER_TRACK
= _floppy
->sect
<< 2 >> SIZECODE
;
2684 SECTOR
= ((sector_t
% _floppy
->sect
) << 2 >> SIZECODE
) + 1;
2686 /* tracksize describes the size which can be filled up with sectors
2689 tracksize
= _floppy
->sect
- _floppy
->sect
% ssize
;
2690 if (tracksize
< _floppy
->sect
){
2692 if (tracksize
<= sector_t
% _floppy
->sect
)
2695 /* if we are beyond tracksize, fill up using smaller sectors */
2696 while (tracksize
<= sector_t
% _floppy
->sect
){
2697 while(tracksize
+ ssize
> _floppy
->sect
){
2701 SECTOR
++; SECT_PER_TRACK
++;
2704 max_sector
= HEAD
* _floppy
->sect
+ tracksize
;
2705 } else if (!TRACK
&& !HEAD
&& !(_floppy
->rate
& FD_2M
) && probing
) {
2706 max_sector
= _floppy
->sect
;
2707 } else if (!HEAD
&& CT(COMMAND
) == FD_WRITE
) {
2708 /* for virtual DMA bug workaround */
2709 max_sector
= _floppy
->sect
;
2712 in_sector_offset
= (sector_t
% _floppy
->sect
) % ssize
;
2713 aligned_sector_t
= sector_t
- in_sector_offset
;
2714 max_size
= CURRENT
->nr_sectors
;
2715 if ((raw_cmd
->track
== buffer_track
) &&
2716 (current_drive
== buffer_drive
) &&
2717 (sector_t
>= buffer_min
) && (sector_t
< buffer_max
)) {
2718 /* data already in track buffer */
2719 if (CT(COMMAND
) == FD_READ
) {
2720 copy_buffer(1, max_sector
, buffer_max
);
2723 } else if (in_sector_offset
|| CURRENT
->nr_sectors
< ssize
){
2724 if (CT(COMMAND
) == FD_WRITE
){
2725 if (sector_t
+ CURRENT
->nr_sectors
> ssize
&&
2726 sector_t
+ CURRENT
->nr_sectors
< ssize
+ ssize
)
2727 max_size
= ssize
+ ssize
;
2731 raw_cmd
->flags
&= ~FD_RAW_WRITE
;
2732 raw_cmd
->flags
|= FD_RAW_READ
;
2733 COMMAND
= FM_MODE(_floppy
,FD_READ
);
2734 } else if ((unsigned long)CURRENT
->buffer
< MAX_DMA_ADDRESS
) {
2735 unsigned long dma_limit
;
2736 int direct
, indirect
;
2738 indirect
= transfer_size(ssize
,max_sector
,max_buffer_sectors
*2) -
2742 * Do NOT use minimum() here---MAX_DMA_ADDRESS is 64 bits wide
2743 * on a 64 bit machine!
2745 max_size
= buffer_chain_size();
2746 dma_limit
= (MAX_DMA_ADDRESS
- ((unsigned long) CURRENT
->buffer
)) >> 9;
2747 if ((unsigned long) max_size
> dma_limit
) {
2748 max_size
= dma_limit
;
2750 /* 64 kb boundaries */
2751 if (CROSS_64KB(CURRENT
->buffer
, max_size
<< 9))
2753 ((unsigned long)CURRENT
->buffer
) % K_64
)>>9;
2754 direct
= transfer_size(ssize
,max_sector
,max_size
) - sector_t
;
2756 * We try to read tracks, but if we get too many errors, we
2757 * go back to reading just one sector at a time.
2759 * This means we should be able to read a sector even if there
2760 * are other bad sectors on this track.
2763 (indirect
* 2 > direct
* 3 &&
2764 *errors
< DP
->max_errors
.read_track
&&
2765 /*!TESTF(FD_NEED_TWADDLE) &&*/
2766 ((!probing
|| (DP
->read_track
&(1<<DRS
->probed_format
)))))){
2767 max_size
= CURRENT
->nr_sectors
;
2769 raw_cmd
->kernel_data
= CURRENT
->buffer
;
2770 raw_cmd
->length
= current_count_sectors
<< 9;
2771 if (raw_cmd
->length
== 0){
2772 DPRINT("zero dma transfer attempted from make_raw_request\n");
2773 DPRINT("indirect=%d direct=%d sector_t=%d",
2774 indirect
, direct
, sector_t
);
2777 /* check_dma_crossing(raw_cmd->kernel_data,
2779 "end of make_raw_request [1]");*/
2781 virtualdmabug_workaround();
2786 if (CT(COMMAND
) == FD_READ
)
2787 max_size
= max_sector
; /* unbounded */
2789 /* claim buffer track if needed */
2790 if (buffer_track
!= raw_cmd
->track
|| /* bad track */
2791 buffer_drive
!=current_drive
|| /* bad drive */
2792 sector_t
> buffer_max
||
2793 sector_t
< buffer_min
||
2794 ((CT(COMMAND
) == FD_READ
||
2795 (!in_sector_offset
&& CURRENT
->nr_sectors
>= ssize
))&&
2796 max_sector
> 2 * max_buffer_sectors
+ buffer_min
&&
2797 max_size
+ sector_t
> 2 * max_buffer_sectors
+ buffer_min
)
2798 /* not enough space */){
2800 buffer_drive
= current_drive
;
2801 buffer_max
= buffer_min
= aligned_sector_t
;
2803 raw_cmd
->kernel_data
= floppy_track_buffer
+
2804 ((aligned_sector_t
-buffer_min
)<<9);
2806 if (CT(COMMAND
) == FD_WRITE
){
2807 /* copy write buffer to track buffer.
2808 * if we get here, we know that the write
2809 * is either aligned or the data already in the buffer
2810 * (buffer will be overwritten) */
2811 #ifdef FLOPPY_SANITY_CHECK
2812 if (in_sector_offset
&& buffer_track
== -1)
2813 DPRINT("internal error offset !=0 on write\n");
2815 buffer_track
= raw_cmd
->track
;
2816 buffer_drive
= current_drive
;
2817 copy_buffer(ssize
, max_sector
, 2*max_buffer_sectors
+buffer_min
);
2819 transfer_size(ssize
, max_sector
,
2820 2*max_buffer_sectors
+buffer_min
-aligned_sector_t
);
2822 /* round up current_count_sectors to get dma xfer size */
2823 raw_cmd
->length
= in_sector_offset
+current_count_sectors
;
2824 raw_cmd
->length
= ((raw_cmd
->length
-1)|(ssize
-1))+1;
2825 raw_cmd
->length
<<= 9;
2826 #ifdef FLOPPY_SANITY_CHECK
2827 /*check_dma_crossing(raw_cmd->kernel_data, raw_cmd->length,
2828 "end of make_raw_request");*/
2829 if ((raw_cmd
->length
< current_count_sectors
<< 9) ||
2830 (raw_cmd
->kernel_data
!= CURRENT
->buffer
&&
2831 CT(COMMAND
) == FD_WRITE
&&
2832 (aligned_sector_t
+ (raw_cmd
->length
>> 9) > buffer_max
||
2833 aligned_sector_t
< buffer_min
)) ||
2834 raw_cmd
->length
% (128 << SIZECODE
) ||
2835 raw_cmd
->length
<= 0 || current_count_sectors
<= 0){
2836 DPRINT("fractionary current count b=%lx s=%lx\n",
2837 raw_cmd
->length
, current_count_sectors
);
2838 if (raw_cmd
->kernel_data
!= CURRENT
->buffer
)
2839 printk("addr=%d, length=%ld\n",
2840 (int) ((raw_cmd
->kernel_data
-
2841 floppy_track_buffer
) >> 9),
2842 current_count_sectors
);
2843 printk("st=%d ast=%d mse=%d msi=%d\n",
2844 sector_t
, aligned_sector_t
, max_sector
, max_size
);
2845 printk("ssize=%x SIZECODE=%d\n", ssize
, SIZECODE
);
2846 printk("command=%x SECTOR=%d HEAD=%d, TRACK=%d\n",
2847 COMMAND
, SECTOR
, HEAD
, TRACK
);
2848 printk("buffer drive=%d\n", buffer_drive
);
2849 printk("buffer track=%d\n", buffer_track
);
2850 printk("buffer_min=%d\n", buffer_min
);
2851 printk("buffer_max=%d\n", buffer_max
);
2855 if (raw_cmd
->kernel_data
!= CURRENT
->buffer
){
2856 if (raw_cmd
->kernel_data
< floppy_track_buffer
||
2857 current_count_sectors
< 0 ||
2858 raw_cmd
->length
< 0 ||
2859 raw_cmd
->kernel_data
+ raw_cmd
->length
>
2860 floppy_track_buffer
+ (max_buffer_sectors
<< 10)){
2861 DPRINT("buffer overrun in schedule dma\n");
2862 printk("sector_t=%d buffer_min=%d current_count=%ld\n",
2863 sector_t
, buffer_min
,
2864 raw_cmd
->length
>> 9);
2865 printk("current_count_sectors=%ld\n",
2866 current_count_sectors
);
2867 if (CT(COMMAND
) == FD_READ
)
2869 if (CT(COMMAND
) == FD_READ
)
2873 } else if (raw_cmd
->length
> CURRENT
->nr_sectors
<< 9 ||
2874 current_count_sectors
> CURRENT
->nr_sectors
){
2875 DPRINT("buffer overrun in direct transfer\n");
2877 } else if (raw_cmd
->length
< current_count_sectors
<< 9){
2878 DPRINT("more sectors than bytes\n");
2879 printk("bytes=%ld\n", raw_cmd
->length
>> 9);
2880 printk("sectors=%ld\n", current_count_sectors
);
2882 if (raw_cmd
->length
== 0){
2883 DPRINT("zero dma transfer attempted from make_raw_request\n");
2888 virtualdmabug_workaround();
2892 static void redo_fd_request(void)
2894 #define REPEAT {request_done(0); continue; }
2899 if (current_drive
< N_DRIVE
)
2900 floppy_off(current_drive
);
2902 if (!QUEUE_EMPTY
&& CURRENT
->rq_status
== RQ_INACTIVE
){
2914 if (MAJOR(CURRENT
->rq_dev
) != MAJOR_NR
)
2915 panic(DEVICE_NAME
": request list destroyed");
2916 if (CURRENT
->bh
&& !buffer_locked(CURRENT
->bh
))
2917 panic(DEVICE_NAME
": block not locked");
2919 device
= CURRENT
->rq_dev
;
2920 set_fdc(DRIVE(device
));
2921 reschedule_timeout(CURRENTD
, "redo fd request", 0);
2924 raw_cmd
= & default_raw_cmd
;
2926 if (start_motor(redo_fd_request
)) return;
2927 disk_change(current_drive
);
2928 if (test_bit(current_drive
, &fake_change
) ||
2929 TESTF(FD_DISK_CHANGED
)){
2930 DPRINT("disk absent or changed during operation\n");
2933 if (!_floppy
) { /* Autodetection */
2935 DRS
->probed_format
= 0;
2936 if (next_valid_format()){
2937 DPRINT("no autodetectable formats\n");
2943 _floppy
= floppy_type
+DP
->autodetect
[DRS
->probed_format
];
2946 errors
= & (CURRENT
->errors
);
2947 tmp
= make_raw_rw_request();
2953 if (TESTF(FD_NEED_TWADDLE
))
2955 schedule_bh( (void *)(void *) floppy_start
);
2957 debugt("queue fd request");
2964 static struct cont_t rw_cont
={
2970 static void process_fd_request(void)
2973 schedule_bh( (void *)(void *) redo_fd_request
);
2976 static void do_fd_request(request_queue_t
* q
)
2978 if(max_buffer_sectors
== 0) {
2979 printk("VFS: do_fd_request called on non-open device\n");
2983 if (usage_count
== 0) {
2984 printk("warning: usage count=0, CURRENT=%p exiting\n", CURRENT
);
2985 printk("sect=%ld cmd=%d\n", CURRENT
->sector
, CURRENT
->cmd
);
2989 /* fdc busy, this new request will be treated when the
2990 current one is done */
2991 is_alive("do fd request, old request running");
2994 lock_fdc(MAXTIMEOUT
,0);
2995 process_fd_request();
2996 is_alive("do fd request");
2999 static struct cont_t poll_cont
={
3005 static int poll_drive(int interruptible
, int flag
)
3008 /* no auto-sense, just clear dcl */
3009 raw_cmd
= &default_raw_cmd
;
3010 raw_cmd
->flags
= flag
;
3012 raw_cmd
->cmd_count
=0;
3015 if (DP
->flags
& FD_DEBUG
){
3016 DPRINT("setting NEWCHANGE in poll_drive\n");
3019 SETF(FD_DISK_NEWCHANGE
);
3025 * User triggered reset
3026 * ====================
3029 static void reset_intr(void)
3031 printk("weird, reset interrupt called\n");
3034 static struct cont_t reset_cont
={
3040 static int user_reset_fdc(int drive
, int arg
, int interruptible
)
3045 LOCK_FDC(drive
,interruptible
);
3046 if (arg
== FD_RESET_ALWAYS
)
3052 process_fd_request();
3057 * Misc Ioctl's and support
3058 * ========================
3060 static inline int fd_copyout(void *param
, const void *address
, unsigned long size
)
3062 return copy_to_user(param
,address
, size
) ? -EFAULT
: 0;
3065 static inline int fd_copyin(void *param
, void *address
, unsigned long size
)
3067 return copy_from_user(address
, param
, size
) ? -EFAULT
: 0;
3070 #define _COPYOUT(x) (copy_to_user((void *)param, &(x), sizeof(x)) ? -EFAULT : 0)
3071 #define _COPYIN(x) (copy_from_user(&(x), (void *)param, sizeof(x)) ? -EFAULT : 0)
3073 #define COPYOUT(x) ECALL(_COPYOUT(x))
3074 #define COPYIN(x) ECALL(_COPYIN(x))
3076 static inline const char *drive_name(int type
, int drive
)
3078 struct floppy_struct
*floppy
;
3081 floppy
= floppy_type
+ type
;
3083 if (UDP
->native_format
)
3084 floppy
= floppy_type
+ UDP
->native_format
;
3089 return floppy
->name
;
3096 static void raw_cmd_done(int flag
)
3101 raw_cmd
->flags
|= FD_RAW_FAILURE
;
3102 raw_cmd
->flags
|= FD_RAW_HARDFAILURE
;
3104 raw_cmd
->reply_count
= inr
;
3105 if (raw_cmd
->reply_count
> MAX_REPLIES
)
3106 raw_cmd
->reply_count
=0;
3107 for (i
=0; i
< raw_cmd
->reply_count
; i
++)
3108 raw_cmd
->reply
[i
] = reply_buffer
[i
];
3110 if (raw_cmd
->flags
& (FD_RAW_READ
| FD_RAW_WRITE
))
3112 unsigned long flags
;
3113 flags
=claim_dma_lock();
3114 raw_cmd
->length
= fd_get_dma_residue();
3115 release_dma_lock(flags
);
3118 if ((raw_cmd
->flags
& FD_RAW_SOFTFAILURE
) &&
3119 (!raw_cmd
->reply_count
|| (raw_cmd
->reply
[0] & 0xc0)))
3120 raw_cmd
->flags
|= FD_RAW_FAILURE
;
3122 if (disk_change(current_drive
))
3123 raw_cmd
->flags
|= FD_RAW_DISK_CHANGE
;
3125 raw_cmd
->flags
&= ~FD_RAW_DISK_CHANGE
;
3126 if (raw_cmd
->flags
& FD_RAW_NO_MOTOR_AFTER
)
3127 motor_off_callback(current_drive
);
3129 if (raw_cmd
->next
&&
3130 (!(raw_cmd
->flags
& FD_RAW_FAILURE
) ||
3131 !(raw_cmd
->flags
& FD_RAW_STOP_IF_FAILURE
)) &&
3132 ((raw_cmd
->flags
& FD_RAW_FAILURE
) ||
3133 !(raw_cmd
->flags
&FD_RAW_STOP_IF_SUCCESS
))) {
3134 raw_cmd
= raw_cmd
->next
;
3142 static struct cont_t raw_cmd_cont
={
3149 static inline int raw_cmd_copyout(int cmd
, char *param
,
3150 struct floppy_raw_cmd
*ptr
)
3156 param
+= sizeof(struct floppy_raw_cmd
);
3157 if ((ptr
->flags
& FD_RAW_READ
) && ptr
->buffer_length
){
3158 if (ptr
->length
>=0 && ptr
->length
<=ptr
->buffer_length
)
3159 ECALL(fd_copyout(ptr
->data
,
3161 ptr
->buffer_length
-
3170 static void raw_cmd_free(struct floppy_raw_cmd
**ptr
)
3172 struct floppy_raw_cmd
*next
,*this;
3177 if (this->buffer_length
) {
3178 fd_dma_mem_free((unsigned long)this->kernel_data
,
3179 this->buffer_length
);
3180 this->buffer_length
= 0;
3189 static inline int raw_cmd_copyin(int cmd
, char *param
,
3190 struct floppy_raw_cmd
**rcmd
)
3192 struct floppy_raw_cmd
*ptr
;
3198 ptr
= (struct floppy_raw_cmd
*)
3199 kmalloc(sizeof(struct floppy_raw_cmd
), GFP_USER
);
3205 ptr
->buffer_length
= 0;
3206 param
+= sizeof(struct floppy_raw_cmd
);
3207 if (ptr
->cmd_count
> 33)
3208 /* the command may now also take up the space
3209 * initially intended for the reply & the
3210 * reply count. Needed for long 82078 commands
3211 * such as RESTORE, which takes ... 17 command
3212 * bytes. Murphy's law #137: When you reserve
3213 * 16 bytes for a structure, you'll one day
3214 * discover that you really need 17...
3218 for (i
=0; i
< 16; i
++)
3220 ptr
->resultcode
= 0;
3221 ptr
->kernel_data
= 0;
3223 if (ptr
->flags
& (FD_RAW_READ
| FD_RAW_WRITE
)) {
3224 if (ptr
->length
<= 0)
3226 ptr
->kernel_data
=(char*)fd_dma_mem_alloc(ptr
->length
);
3227 fallback_on_nodma_alloc(&ptr
->kernel_data
,
3229 if (!ptr
->kernel_data
)
3231 ptr
->buffer_length
= ptr
->length
;
3233 if (ptr
->flags
& FD_RAW_WRITE
)
3234 ECALL(fd_copyin(ptr
->data
, ptr
->kernel_data
,
3236 rcmd
= & (ptr
->next
);
3237 if (!(ptr
->flags
& FD_RAW_MORE
))
3244 static int raw_cmd_ioctl(int cmd
, void *param
)
3246 int drive
, ret
, ret2
;
3247 struct floppy_raw_cmd
*my_raw_cmd
;
3249 if (FDCS
->rawcmd
<= 1)
3251 for (drive
= 0; drive
< N_DRIVE
; drive
++){
3252 if (FDC(drive
) != fdc
)
3254 if (drive
== current_drive
){
3255 if (UDRS
->fd_ref
> 1){
3259 } else if (UDRS
->fd_ref
){
3268 ret
= raw_cmd_copyin(cmd
, param
, &my_raw_cmd
);
3270 raw_cmd_free(&my_raw_cmd
);
3274 raw_cmd
= my_raw_cmd
;
3275 cont
= &raw_cmd_cont
;
3276 ret
=wait_til_done(floppy_start
,1);
3278 if (DP
->flags
& FD_DEBUG
){
3279 DPRINT("calling disk change from raw_cmd ioctl\n");
3283 if (ret
!= -EINTR
&& FDCS
->reset
)
3286 DRS
->track
= NO_TRACK
;
3288 ret2
= raw_cmd_copyout(cmd
, param
, my_raw_cmd
);
3291 raw_cmd_free(&my_raw_cmd
);
3295 static int invalidate_drive(kdev_t rdev
)
3297 /* invalidate the buffer track to force a reread */
3298 set_bit(DRIVE(rdev
), &fake_change
);
3299 process_fd_request();
3300 check_disk_change(rdev
);
3305 static inline void clear_write_error(int drive
)
3310 static inline int set_geometry(unsigned int cmd
, struct floppy_struct
*g
,
3311 int drive
, int type
, kdev_t device
)
3315 /* sanity checking for parameters.*/
3319 g
->track
> UDP
->tracks
>>STRETCH(g
) ||
3320 /* check if reserved bits are set */
3321 (g
->stretch
&~(FD_STRETCH
|FD_SWAPSIDES
)) != 0)
3324 if (!capable(CAP_SYS_ADMIN
))
3327 for (cnt
= 0; cnt
< N_DRIVE
; cnt
++){
3328 if (ITYPE(drive_state
[cnt
].fd_device
) == type
&&
3329 drive_state
[cnt
].fd_ref
)
3330 set_bit(drive
, &fake_change
);
3332 floppy_type
[type
] = *g
;
3333 floppy_type
[type
].name
="user format";
3334 for (cnt
= type
<< 2; cnt
< (type
<< 2) + 4; cnt
++)
3335 floppy_sizes
[cnt
]= floppy_sizes
[cnt
+0x80]=
3336 (floppy_type
[type
].size
+1)>>1;
3337 process_fd_request();
3338 for (cnt
= 0; cnt
< N_DRIVE
; cnt
++){
3339 if (ITYPE(drive_state
[cnt
].fd_device
) == type
&&
3340 drive_state
[cnt
].fd_ref
)
3343 drive_state
[cnt
].fd_device
));
3347 if (cmd
!= FDDEFPRM
)
3348 /* notice a disk change immediately, else
3349 * we lose our settings immediately*/
3350 CALL(poll_drive(1, FD_RAW_NEED_DISK
));
3351 user_params
[drive
] = *g
;
3352 if (buffer_drive
== drive
)
3353 SUPBOUND(buffer_max
, user_params
[drive
].sect
);
3354 current_type
[drive
] = &user_params
[drive
];
3355 floppy_sizes
[drive
] = (user_params
[drive
].size
+1) >> 1;
3356 if (cmd
== FDDEFPRM
)
3357 DRS
->keep_data
= -1;
3360 /* invalidation. Invalidate only when needed, i.e.
3361 * when there are already sectors in the buffer cache
3362 * whose number will change. This is useful, because
3363 * mtools often changes the geometry of the disk after
3364 * looking at the boot block */
3365 if (DRS
->maxblock
> user_params
[drive
].sect
|| DRS
->maxtrack
)
3366 invalidate_drive(device
);
3368 process_fd_request();
3373 /* handle obsolete ioctl's */
3374 static int ioctl_table
[]= {
3402 static inline int normalize_ioctl(int *cmd
, int *size
)
3406 for (i
=0; i
< ARRAY_SIZE(ioctl_table
); i
++) {
3407 if ((*cmd
& 0xffff) == (ioctl_table
[i
] & 0xffff)){
3408 *size
= _IOC_SIZE(*cmd
);
3409 *cmd
= ioctl_table
[i
];
3410 if (*size
> _IOC_SIZE(*cmd
)) {
3411 printk("ioctl not yet supported\n");
3420 static int get_floppy_geometry(int drive
, int type
, struct floppy_struct
**g
)
3423 *g
= &floppy_type
[type
];
3426 CALL(poll_drive(0,0));
3427 process_fd_request();
3428 *g
= current_type
[drive
];
3435 static int fd_ioctl(struct inode
*inode
, struct file
*filp
, unsigned int cmd
,
3436 unsigned long param
)
3438 #define OUT(c,x) case c: outparam = (const char *) (x); break
3439 #define IN(c,x,tag) case c: *(x) = inparam. tag ; return 0
3446 struct floppy_struct g
; /* geometry */
3447 struct format_descr f
;
3448 struct floppy_max_errors max_errors
;
3449 struct floppy_drive_params dp
;
3450 } inparam
; /* parameters coming from user space */
3451 const char *outparam
; /* parameters passed back to user space */
3453 device
= inode
->i_rdev
;
3460 return blk_ioctl(device
, cmd
, param
);
3462 type
= TYPE(device
);
3463 drive
= DRIVE(device
);
3465 /* convert compatibility eject ioctls into floppy eject ioctl.
3466 * We do this in order to provide a means to eject floppy disks before
3467 * installing the new fdutils package */
3468 if (cmd
== CDROMEJECT
|| /* CD-ROM eject */
3469 cmd
== 0x6470 /* SunOS floppy eject */) {
3470 DPRINT("obsolete eject ioctl\n");
3471 DPRINT("please use floppycontrol --eject\n");
3475 /* generic block device ioctls */
3477 /* the following have been inspired by the corresponding
3478 * code for other block devices. */
3479 struct floppy_struct
*g
;
3482 struct hd_geometry loc
;
3483 ECALL(get_floppy_geometry(drive
, type
, &g
));
3484 loc
.heads
= g
->head
;
3485 loc
.sectors
= g
->sect
;
3486 loc
.cylinders
= g
->track
;
3488 return _COPYOUT(loc
);
3492 ECALL(get_floppy_geometry(drive
, type
, &g
));
3493 return put_user(g
->size
, (long *) param
);
3494 /* BLKRRPART is not defined as floppies don't have
3495 * partition tables */
3498 /* convert the old style command into a new style command */
3499 if ((cmd
& 0xff00) == 0x0200) {
3500 ECALL(normalize_ioctl(&cmd
, &size
));
3504 /* permission checks */
3505 if (((cmd
& 0x40) && !(filp
->f_mode
& 2)) ||
3506 ((cmd
& 0x80) && !capable(CAP_SYS_ADMIN
)))
3510 CLEARSTRUCT(&inparam
);
3511 if (_IOC_DIR(cmd
) & _IOC_WRITE
)
3512 ECALL(fd_copyin((void *)param
, &inparam
, size
))
3516 if (UDRS
->fd_ref
!= 1)
3517 /* somebody else has this drive open */
3521 /* do the actual eject. Fails on
3522 * non-Sparc architectures */
3523 ret
=fd_eject(UNIT(drive
));
3525 USETF(FD_DISK_CHANGED
);
3527 process_fd_request();
3531 current_type
[drive
] = NULL
;
3532 floppy_sizes
[drive
] = MAX_DISK_SIZE
;
3533 UDRS
->keep_data
= 0;
3534 return invalidate_drive(device
);
3537 return set_geometry(cmd
, & inparam
.g
,
3538 drive
, type
, device
);
3540 ECALL(get_floppy_geometry(drive
, type
,
3541 (struct floppy_struct
**)
3546 UDP
->flags
|= FTD_MSG
;
3549 UDP
->flags
&= ~FTD_MSG
;
3554 CALL(poll_drive(1, FD_RAW_NEED_DISK
));
3556 process_fd_request();
3557 if (ret
& FD_VERIFY
)
3559 if (!(ret
& FD_DISK_WRITABLE
))
3563 if (UDRS
->fd_ref
!= 1)
3565 return do_format(device
, &inparam
.f
);
3569 return invalidate_drive(device
);
3571 case FDSETEMSGTRESH
:
3572 UDP
->max_errors
.reporting
=
3573 (unsigned short) (param
& 0x0f);
3575 OUT(FDGETMAXERRS
, &UDP
->max_errors
);
3576 IN(FDSETMAXERRS
, &UDP
->max_errors
, max_errors
);
3579 outparam
= drive_name(type
,drive
);
3580 SUPBOUND(size
,strlen(outparam
)+1);
3583 IN(FDSETDRVPRM
, UDP
, dp
);
3584 OUT(FDGETDRVPRM
, UDP
);
3588 CALL(poll_drive(1, FD_RAW_NEED_DISK
));
3589 process_fd_request();
3591 OUT(FDGETDRVSTAT
, UDRS
);
3594 return user_reset_fdc(drive
, (int)param
, 1);
3596 OUT(FDGETFDCSTAT
,UFDCS
);
3601 OUT(FDWERRORGET
,UDRWE
);
3608 CALL(i
= raw_cmd_ioctl(cmd
,(void *) param
));
3609 process_fd_request();
3615 process_fd_request();
3622 if (_IOC_DIR(cmd
) & _IOC_READ
)
3623 return fd_copyout((void *)param
, outparam
, size
);
3630 static void __init
config_types(void)
3635 /* read drive info out of physical CMOS */
3638 UDP
->cmos
= FLOPPY0_TYPE
;
3640 if (!UDP
->cmos
&& FLOPPY1_TYPE
)
3641 UDP
->cmos
= FLOPPY1_TYPE
;
3644 /* additional physical CMOS drive detection should go here */
3646 for (drive
=0; drive
< N_DRIVE
; drive
++){
3647 unsigned int type
= UDP
->cmos
;
3648 struct floppy_drive_params
*params
;
3649 const char *name
= NULL
;
3650 static char temparea
[32];
3652 if (type
< NUMBER(default_drive_params
)) {
3653 params
= &default_drive_params
[type
].params
;
3655 name
= default_drive_params
[type
].name
;
3656 allowed_drive_mask
|= 1 << drive
;
3659 params
= &default_drive_params
[0].params
;
3660 sprintf(temparea
, "unknown type %d (usb?)", type
);
3664 const char * prepend
= ",";
3666 prepend
= KERN_INFO
"Floppy drive(s):";
3669 printk("%s fd%d is %s", prepend
, drive
, name
);
3670 register_devfs_entries (drive
);
3678 static int floppy_release(struct inode
* inode
, struct file
* filp
)
3680 int drive
= DRIVE(inode
->i_rdev
);
3682 if (UDRS
->fd_ref
< 0)
3684 else if (!UDRS
->fd_ref
--) {
3685 DPRINT("floppy_release with fd_ref == 0");
3688 floppy_release_irq_and_dma();
3693 * floppy_open check for aliasing (/dev/fd0 can be the same as
3694 * /dev/PS0 etc), and disallows simultaneous access to the same
3695 * drive with different device numbers.
3697 #define RETERR(x) do{floppy_release(inode,filp); return -(x);}while(0)
3699 static int floppy_open(struct inode
* inode
, struct file
* filp
)
3707 DPRINT("Weird, open called with filp=0\n");
3711 drive
= DRIVE(inode
->i_rdev
);
3712 if (drive
>= N_DRIVE
||
3713 !(allowed_drive_mask
& (1 << drive
)) ||
3714 fdc_state
[FDC(drive
)].version
== FDC_NONE
)
3717 if (TYPE(inode
->i_rdev
) >= NUMBER(floppy_type
))
3719 old_dev
= UDRS
->fd_device
;
3720 if (UDRS
->fd_ref
&& old_dev
!= MINOR(inode
->i_rdev
))
3723 if (!UDRS
->fd_ref
&& (UDP
->flags
& FD_BROKEN_DCL
)){
3724 USETF(FD_DISK_CHANGED
);
3728 if (UDRS
->fd_ref
== -1 ||
3729 (UDRS
->fd_ref
&& (filp
->f_flags
& O_EXCL
)))
3732 if (floppy_grab_irq_and_dma())
3735 if (filp
->f_flags
& O_EXCL
)
3740 if (!floppy_track_buffer
){
3741 /* if opening an ED drive, reserve a big buffer,
3742 * else reserve a small one */
3743 if ((UDP
->cmos
== 6) || (UDP
->cmos
== 5))
3744 try = 64; /* Only 48 actually useful */
3746 try = 32; /* Only 24 actually useful */
3748 tmp
=(char *)fd_dma_mem_alloc(1024 * try);
3749 if (!tmp
&& !floppy_track_buffer
) {
3750 try >>= 1; /* buffer only one side */
3752 tmp
= (char *)fd_dma_mem_alloc(1024*try);
3754 if (!tmp
&& !floppy_track_buffer
) {
3755 fallback_on_nodma_alloc(&tmp
, 2048 * try);
3757 if (!tmp
&& !floppy_track_buffer
) {
3758 DPRINT("Unable to allocate DMA memory\n");
3761 if (floppy_track_buffer
) {
3763 fd_dma_mem_free((unsigned long)tmp
,try*1024);
3765 buffer_min
= buffer_max
= -1;
3766 floppy_track_buffer
= tmp
;
3767 max_buffer_sectors
= try;
3771 UDRS
->fd_device
= MINOR(inode
->i_rdev
);
3772 if (old_dev
!= -1 && old_dev
!= MINOR(inode
->i_rdev
)) {
3773 if (buffer_drive
== drive
)
3775 invalidate_buffers(MKDEV(FLOPPY_MAJOR
,old_dev
));
3778 if (UFDCS
->rawcmd
== 1)
3781 if (filp
->f_flags
& O_NDELAY
)
3783 if (filp
->f_mode
& 3) {
3784 UDRS
->last_checked
= 0;
3785 check_disk_change(inode
->i_rdev
);
3786 if (UTESTF(FD_DISK_CHANGED
))
3789 if ((filp
->f_mode
& 2) && !(UTESTF(FD_DISK_WRITABLE
)))
3796 * Check if the disk has been changed or if a change has been faked.
3798 static int check_floppy_change(kdev_t dev
)
3800 int drive
= DRIVE(dev
);
3802 if (MAJOR(dev
) != MAJOR_NR
) {
3803 DPRINT("check_floppy_change: not a floppy\n");
3807 if (UTESTF(FD_DISK_CHANGED
) || UTESTF(FD_VERIFY
))
3810 if (UDP
->checkfreq
< (int)(jiffies
- UDRS
->last_checked
)) {
3811 if(floppy_grab_irq_and_dma()) {
3817 process_fd_request();
3818 floppy_release_irq_and_dma();
3821 if (UTESTF(FD_DISK_CHANGED
) ||
3822 UTESTF(FD_VERIFY
) ||
3823 test_bit(drive
, &fake_change
) ||
3824 (!TYPE(dev
) && !current_type
[drive
]))
3829 /* revalidate the floppy disk, i.e. trigger format autodetection by reading
3830 * the bootblock (block 0). "Autodetection" is also needed to check whether
3831 * there is a disk in the drive at all... Thus we also do it for fixed
3832 * geometry formats */
3833 static int floppy_revalidate(kdev_t dev
)
3835 #define NO_GEOM (!current_type[drive] && !TYPE(dev))
3836 struct buffer_head
* bh
;
3837 int drive
=DRIVE(dev
);
3840 if (UTESTF(FD_DISK_CHANGED
) ||
3841 UTESTF(FD_VERIFY
) ||
3842 test_bit(drive
, &fake_change
) ||
3844 if(usage_count
== 0) {
3845 printk("VFS: revalidate called on non-open device.\n");
3849 cf
= UTESTF(FD_DISK_CHANGED
) || UTESTF(FD_VERIFY
);
3850 if (!(cf
|| test_bit(drive
, &fake_change
) || NO_GEOM
)){
3851 process_fd_request(); /*already done by another thread*/
3856 if (buffer_drive
== drive
)
3858 clear_bit(drive
, &fake_change
);
3859 UCLEARF(FD_DISK_CHANGED
);
3864 int size
= floppy_blocksizes
[MINOR(dev
)];
3867 if (!(bh
= getblk(dev
,0,size
))){
3868 process_fd_request();
3871 if (bh
&& !buffer_uptodate(bh
))
3872 ll_rw_block(READ
, 1, &bh
);
3873 process_fd_request();
3879 poll_drive(0, FD_RAW_NEED_DISK
);
3880 process_fd_request();
3885 static struct block_device_operations floppy_fops
= {
3887 release
: floppy_release
,
3889 check_media_change
: check_floppy_change
,
3890 revalidate
: floppy_revalidate
,
3893 static void __init
register_devfs_entries (int drive
)
3896 static char *table
[] =
3897 {"", "d360", "h1200", "u360", "u720", "h360", "h720",
3898 "u1440", "u2880", "CompaQ", "h1440", "u1680", "h410",
3899 "u820", "h1476", "u1722", "h420", "u830", "h1494", "u1743",
3900 "h880", "u1040", "u1120", "h1600", "u1760", "u1920",
3901 "u3200", "u3520", "u3840", "u1840", "u800", "u1600",
3904 static int t360
[] = {1,0}, t1200
[] = {2,5,6,10,12,14,16,18,20,23,0},
3905 t3in
[] = {8,9,26,27,28, 7,11,15,19,24,25,29,31, 3,4,13,17,21,22,30,0};
3906 static int *table_sup
[] =
3907 {NULL
, t360
, t1200
, t3in
+5+8, t3in
+5, t3in
, t3in
};
3909 base_minor
= (drive
< 4) ? drive
: (124 + drive
);
3910 if (UDP
->cmos
<= NUMBER(default_drive_params
)) {
3915 sprintf (name
, "%d%s", drive
, table
[table_sup
[UDP
->cmos
][i
]]);
3916 devfs_register (devfs_handle
, name
, DEVFS_FL_DEFAULT
, MAJOR_NR
,
3917 base_minor
+ (table_sup
[UDP
->cmos
][i
] << 2),
3918 S_IFBLK
| S_IRUSR
| S_IWUSR
| S_IRGRP
|S_IWGRP
,
3919 &floppy_fops
, NULL
);
3920 } while (table_sup
[UDP
->cmos
][i
++]);
3925 * Floppy Driver initialization
3926 * =============================
3929 /* Determine the floppy disk controller type */
3930 /* This routine was written by David C. Niemi */
3931 static char __init
get_fdc_version(void)
3935 output_byte(FD_DUMPREGS
); /* 82072 and better know DUMPREGS */
3938 if ((r
= result()) <= 0x00)
3939 return FDC_NONE
; /* No FDC present ??? */
3940 if ((r
==1) && (reply_buffer
[0] == 0x80)){
3941 printk(KERN_INFO
"FDC %d is an 8272A\n",fdc
);
3942 return FDC_8272A
; /* 8272a/765 don't know DUMPREGS */
3945 printk("FDC %d init: DUMPREGS: unexpected return of %d bytes.\n",
3950 if (!fdc_configure()) {
3951 printk(KERN_INFO
"FDC %d is an 82072\n",fdc
);
3952 return FDC_82072
; /* 82072 doesn't know CONFIGURE */
3955 output_byte(FD_PERPENDICULAR
);
3956 if (need_more_output() == MORE_OUTPUT
) {
3959 printk(KERN_INFO
"FDC %d is an 82072A\n", fdc
);
3960 return FDC_82072A
; /* 82072A as found on Sparcs. */
3963 output_byte(FD_UNLOCK
);
3965 if ((r
== 1) && (reply_buffer
[0] == 0x80)){
3966 printk(KERN_INFO
"FDC %d is a pre-1991 82077\n", fdc
);
3967 return FDC_82077_ORIG
; /* Pre-1991 82077, doesn't know
3970 if ((r
!= 1) || (reply_buffer
[0] != 0x00)) {
3971 printk("FDC %d init: UNLOCK: unexpected return of %d bytes.\n",
3975 output_byte(FD_PARTID
);
3978 printk("FDC %d init: PARTID: unexpected return of %d bytes.\n",
3982 if (reply_buffer
[0] == 0x80) {
3983 printk(KERN_INFO
"FDC %d is a post-1991 82077\n",fdc
);
3984 return FDC_82077
; /* Revised 82077AA passes all the tests */
3986 switch (reply_buffer
[0] >> 5) {
3988 /* Either a 82078-1 or a 82078SL running at 5Volt */
3989 printk(KERN_INFO
"FDC %d is an 82078.\n",fdc
);
3992 printk(KERN_INFO
"FDC %d is a 44pin 82078\n",fdc
);
3995 printk(KERN_INFO
"FDC %d is a S82078B\n", fdc
);
3998 printk(KERN_INFO
"FDC %d is a National Semiconductor PC87306\n", fdc
);
4001 printk(KERN_INFO
"FDC %d init: 82078 variant with unknown PARTID=%d.\n",
4002 fdc
, reply_buffer
[0] >> 5);
4003 return FDC_82078_UNKN
;
4005 } /* get_fdc_version */
4007 /* lilo configuration */
4009 static void __init
floppy_set_flags(int *ints
,int param
, int param2
)
4013 for (i
=0; i
< ARRAY_SIZE(default_drive_params
); i
++){
4015 default_drive_params
[i
].params
.flags
|= param2
;
4017 default_drive_params
[i
].params
.flags
&= ~param2
;
4019 DPRINT("%s flag 0x%x\n", param2
? "Setting" : "Clearing", param
);
4022 static void __init
daring(int *ints
,int param
, int param2
)
4026 for (i
=0; i
< ARRAY_SIZE(default_drive_params
); i
++){
4028 default_drive_params
[i
].params
.select_delay
= 0;
4029 default_drive_params
[i
].params
.flags
|= FD_SILENT_DCL_CLEAR
;
4031 default_drive_params
[i
].params
.select_delay
= 2*HZ
/100;
4032 default_drive_params
[i
].params
.flags
&= ~FD_SILENT_DCL_CLEAR
;
4035 DPRINT("Assuming %s floppy hardware\n", param
? "standard" : "broken");
4038 static void __init
set_cmos(int *ints
, int dummy
, int dummy2
)
4040 int current_drive
=0;
4043 DPRINT("wrong number of parameters for CMOS\n");
4046 current_drive
= ints
[1];
4047 if (current_drive
< 0 || current_drive
>= 8){
4048 DPRINT("bad drive for set_cmos\n");
4051 if (current_drive
>= 4 && !FDC2
)
4054 DPRINT("setting CMOS code to %d\n", ints
[2]);
4057 static struct param_table
{
4059 void (*fn
)(int *ints
, int param
, int param2
);
4064 { "allowed_drive_mask", 0, &allowed_drive_mask
, 0xff, 0}, /* obsolete */
4065 { "all_drives", 0, &allowed_drive_mask
, 0xff, 0 }, /* obsolete */
4066 { "asus_pci", 0, &allowed_drive_mask
, 0x33, 0},
4068 { "irq", 0, &FLOPPY_IRQ
, 6, 0 },
4069 { "dma", 0, &FLOPPY_DMA
, 2, 0 },
4071 { "daring", daring
, 0, 1, 0},
4073 { "two_fdc", 0, &FDC2
, 0x370, 0 },
4074 { "one_fdc", 0, &FDC2
, 0, 0 },
4076 { "thinkpad", floppy_set_flags
, 0, 1, FD_INVERTED_DCL
},
4077 { "broken_dcl", floppy_set_flags
, 0, 1, FD_BROKEN_DCL
},
4078 { "messages", floppy_set_flags
, 0, 1, FTD_MSG
},
4079 { "silent_dcl_clear", floppy_set_flags
, 0, 1, FD_SILENT_DCL_CLEAR
},
4080 { "debug", floppy_set_flags
, 0, 1, FD_DEBUG
},
4082 { "nodma", 0, &can_use_virtual_dma
, 1, 0 },
4083 { "omnibook", 0, &can_use_virtual_dma
, 1, 0 },
4084 { "yesdma", 0, &can_use_virtual_dma
, 0, 0 },
4086 { "fifo_depth", 0, &fifo_depth
, 0xa, 0 },
4087 { "nofifo", 0, &no_fifo
, 0x20, 0 },
4088 { "usefifo", 0, &no_fifo
, 0, 0 },
4090 { "cmos", set_cmos
, 0, 0, 0 },
4091 { "slow", 0, &slow_floppy
, 1, 0 },
4093 { "unexpected_interrupts", 0, &print_unex
, 1, 0 },
4094 { "no_unexpected_interrupts", 0, &print_unex
, 0, 0 },
4095 { "L40SX", 0, &print_unex
, 0, 0 }
4098 static int __init
floppy_setup(char *str
)
4104 str
= get_options(str
,ARRAY_SIZE(ints
),ints
);
4106 for (i
=0; i
< ARRAY_SIZE(config_params
); i
++){
4107 if (strcmp(str
,config_params
[i
].name
) == 0){
4111 param
= config_params
[i
].def_param
;
4112 if (config_params
[i
].fn
)
4115 config_params
[i
].param2
);
4116 if (config_params
[i
].var
) {
4117 DPRINT("%s=%d\n", str
, param
);
4118 *config_params
[i
].var
= param
;
4125 DPRINT("unknown floppy option [%s]\n", str
);
4127 DPRINT("allowed options are:");
4128 for (i
=0; i
< ARRAY_SIZE(config_params
); i
++)
4129 printk(" %s",config_params
[i
].name
);
4132 DPRINT("botched floppy option\n");
4133 DPRINT("Read linux/Documentation/floppy.txt\n");
4137 static int have_no_fdc
= -EIO
;
4140 int __init
floppy_init(void)
4147 devfs_handle
= devfs_mk_dir (NULL
, "floppy", NULL
);
4148 if (devfs_register_blkdev(MAJOR_NR
,"fd",&floppy_fops
)) {
4149 printk("Unable to get major %d for floppy\n",MAJOR_NR
);
4153 for (i
=0; i
<256; i
++)
4155 floppy_sizes
[i
] = (floppy_type
[ITYPE(i
)].size
+1) >> 1;
4157 floppy_sizes
[i
] = MAX_DISK_SIZE
;
4159 blk_size
[MAJOR_NR
] = floppy_sizes
;
4160 blksize_size
[MAJOR_NR
] = floppy_blocksizes
;
4161 blk_init_queue(BLK_DEFAULT_QUEUE(MAJOR_NR
), DEVICE_REQUEST
);
4162 reschedule_timeout(MAXTIMEOUT
, "floppy init", MAXTIMEOUT
);
4165 for (i
= 0; i
< N_FDC
; i
++) {
4171 /*sparcs don't have a DOR reset which we can fall back on to*/
4172 FDCS
->version
= FDC_82072A
;
4176 use_virtual_dma
= can_use_virtual_dma
& 1;
4177 fdc_state
[0].address
= FDC1
;
4178 if (fdc_state
[0].address
== -1) {
4179 devfs_unregister_blkdev(MAJOR_NR
,"fd");
4180 del_timer(&fd_timeout
);
4181 blk_cleanup_queue(BLK_DEFAULT_QUEUE(MAJOR_NR
));
4185 fdc_state
[1].address
= FDC2
;
4188 fdc
= 0; /* reset fdc in case of unexpected interrupt */
4189 if (floppy_grab_irq_and_dma()){
4190 del_timer(&fd_timeout
);
4191 blk_cleanup_queue(BLK_DEFAULT_QUEUE(MAJOR_NR
));
4192 devfs_unregister_blkdev(MAJOR_NR
,"fd");
4193 del_timer(&fd_timeout
);
4197 /* initialise drive state */
4198 for (drive
= 0; drive
< N_DRIVE
; drive
++) {
4201 USETF(FD_DISK_NEWCHANGE
);
4202 USETF(FD_DISK_CHANGED
);
4204 UDRS
->fd_device
= -1;
4205 floppy_track_buffer
= NULL
;
4206 max_buffer_sectors
= 0;
4209 for (i
= 0; i
< N_FDC
; i
++) {
4211 FDCS
->driver_version
= FD_DRIVER_VERSION
;
4212 for (unit
=0; unit
<4; unit
++)
4213 FDCS
->track
[unit
] = 0;
4214 if (FDCS
->address
== -1)
4217 if (user_reset_fdc(-1,FD_RESET_ALWAYS
,0)){
4218 /* free ioports reserved by floppy_grab_irq_and_dma() */
4219 release_region(FDCS
->address
, 6);
4220 release_region(FDCS
->address
+7, 1);
4222 FDCS
->version
= FDC_NONE
;
4225 /* Try to determine the floppy controller type */
4226 FDCS
->version
= get_fdc_version();
4227 if (FDCS
->version
== FDC_NONE
){
4228 /* free ioports reserved by floppy_grab_irq_and_dma() */
4229 release_region(FDCS
->address
, 6);
4230 release_region(FDCS
->address
+7, 1);
4234 if (can_use_virtual_dma
== 2 && FDCS
->version
< FDC_82072A
)
4235 can_use_virtual_dma
= 0;
4238 /* Not all FDCs seem to be able to handle the version command
4239 * properly, so force a reset for the standard FDC clones,
4240 * to avoid interrupt garbage.
4242 user_reset_fdc(-1,FD_RESET_ALWAYS
,0);
4245 del_timer(&fd_timeout
);
4247 floppy_release_irq_and_dma();
4251 DPRINT("no floppy controllers found\n");
4252 floppy_tq
.routine
= (void *)(void *) empty
;
4253 mark_bh(IMMEDIATE_BH
);
4256 floppy_release_irq_and_dma();
4257 blk_cleanup_queue(BLK_DEFAULT_QUEUE(MAJOR_NR
));
4258 devfs_unregister_blkdev(MAJOR_NR
,"fd");
4261 for (drive
= 0; drive
< N_DRIVE
; drive
++) {
4262 if (!(allowed_drive_mask
& (1 << drive
)))
4264 if (fdc_state
[FDC(drive
)].version
== FDC_NONE
)
4266 for (i
= 0; i
<NUMBER(floppy_type
); i
++)
4267 register_disk(NULL
, MKDEV(MAJOR_NR
,TOMINOR(drive
)+i
*4),
4268 1, &floppy_fops
, 0);
4273 static spinlock_t floppy_usage_lock
= SPIN_LOCK_UNLOCKED
;
4275 static int floppy_grab_irq_and_dma(void)
4277 unsigned long flags
;
4279 spin_lock_irqsave(&floppy_usage_lock
, flags
);
4281 spin_unlock_irqrestore(&floppy_usage_lock
, flags
);
4284 spin_unlock_irqrestore(&floppy_usage_lock
, flags
);
4286 if (fd_request_irq()) {
4287 DPRINT("Unable to grab IRQ%d for the floppy driver\n",
4290 spin_lock_irqsave(&floppy_usage_lock
, flags
);
4292 spin_unlock_irqrestore(&floppy_usage_lock
, flags
);
4295 if (fd_request_dma()) {
4296 DPRINT("Unable to grab DMA%d for the floppy driver\n",
4300 spin_lock_irqsave(&floppy_usage_lock
, flags
);
4302 spin_unlock_irqrestore(&floppy_usage_lock
, flags
);
4306 for (fdc
=0; fdc
< N_FDC
; fdc
++){
4307 if (FDCS
->address
!= -1){
4308 if (!request_region(FDCS
->address
, 6, "floppy")) {
4309 DPRINT("Floppy io-port 0x%04lx in use\n", FDCS
->address
);
4312 if (!request_region(FDCS
->address
+ 7, 1, "floppy DIR")) {
4313 DPRINT("Floppy io-port 0x%04lx in use\n", FDCS
->address
+ 7);
4316 /* address + 6 is reserved, and may be taken by IDE.
4317 * Unfortunately, Adaptec doesn't know this :-(, */
4320 for (fdc
=0; fdc
< N_FDC
; fdc
++){
4321 if (FDCS
->address
!= -1){
4323 fd_outb(FDCS
->dor
, FD_DOR
);
4327 set_dor(0, ~0, 8); /* avoid immediate interrupt */
4329 for (fdc
= 0; fdc
< N_FDC
; fdc
++)
4330 if (FDCS
->address
!= -1)
4331 fd_outb(FDCS
->dor
, FD_DOR
);
4333 * The driver will try and free resources and relies on us
4334 * to know if they were allocated or not.
4337 irqdma_allocated
= 1;
4340 release_region(FDCS
->address
, 6);
4345 release_region(FDCS
->address
, 6);
4346 release_region(FDCS
->address
+ 7, 1);
4349 spin_lock_irqsave(&floppy_usage_lock
, flags
);
4351 spin_unlock_irqrestore(&floppy_usage_lock
, flags
);
4355 static void floppy_release_irq_and_dma(void)
4358 #ifdef FLOPPY_SANITY_CHECK
4364 unsigned long tmpaddr
;
4365 unsigned long flags
;
4367 spin_lock_irqsave(&floppy_usage_lock
, flags
);
4369 spin_unlock_irqrestore(&floppy_usage_lock
, flags
);
4372 spin_unlock_irqrestore(&floppy_usage_lock
, flags
);
4373 if(irqdma_allocated
)
4384 floppy_enable_hlt();
4386 if (floppy_track_buffer
&& max_buffer_sectors
) {
4387 tmpsize
= max_buffer_sectors
*1024;
4388 tmpaddr
= (unsigned long)floppy_track_buffer
;
4389 floppy_track_buffer
= NULL
;
4390 max_buffer_sectors
= 0;
4391 buffer_min
= buffer_max
= -1;
4392 fd_dma_mem_free(tmpaddr
, tmpsize
);
4395 #ifdef FLOPPY_SANITY_CHECK
4397 for (drive
=0; drive
< N_FDC
* 4; drive
++)
4398 if (timer_pending(motor_off_timer
+ drive
))
4399 printk("motor off timer %d still active\n", drive
);
4402 if (timer_pending(&fd_timeout
))
4403 printk("floppy timer still active:%s\n", timeout_message
);
4404 if (timer_pending(&fd_timer
))
4405 printk("auxiliary floppy timer still active\n");
4407 printk("task queue still active\n");
4410 for (fdc
= 0; fdc
< N_FDC
; fdc
++)
4411 if (FDCS
->address
!= -1) {
4412 release_region(FDCS
->address
, 6);
4413 release_region(FDCS
->address
+7, 1);
4424 static void __init
parse_floppy_cfg_string(char *cfg
)
4429 for(ptr
= cfg
;*cfg
&& *cfg
!= ' ' && *cfg
!= '\t'; cfg
++);
4439 int init_module(void)
4441 printk(KERN_INFO
"inserting floppy driver for " UTS_RELEASE
"\n");
4444 parse_floppy_cfg_string(floppy
);
4445 return floppy_init();
4448 void cleanup_module(void)
4452 devfs_unregister (devfs_handle
);
4453 devfs_unregister_blkdev(MAJOR_NR
, "fd");
4455 blk_cleanup_queue(BLK_DEFAULT_QUEUE(MAJOR_NR
));
4456 /* eject disk, if any */
4457 dummy
= fd_eject(0);
4460 MODULE_PARM(floppy
,"s");
4461 MODULE_PARM(FLOPPY_IRQ
,"i");
4462 MODULE_PARM(FLOPPY_DMA
,"i");
4463 MODULE_AUTHOR("Alain L. Knaff");
4464 MODULE_SUPPORTED_DEVICE("fd");
4468 __setup ("floppy=", floppy_setup
);
4470 /* eject the boot floppy (if we need the drive for a different root floppy) */
4471 /* This should only be called at boot time when we're sure that there's no
4472 * resource contention. */
4473 void floppy_eject(void)
4478 if(floppy_grab_irq_and_dma()==0)
4480 lock_fdc(MAXTIMEOUT
,0);
4482 process_fd_request();
4483 floppy_release_irq_and_dma();