2 * linux/kernel/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/06/07 -- Alan Cox -- Merged the 2.0.34 fixes for resource allocation
105 #define FLOPPY_SANITY_CHECK
106 #undef FLOPPY_SILENT_DCL_CLEAR
108 #define REALLY_SLOW_IO
111 #define DCL_DEBUG /* debug disk change line */
113 /* do print messages for unexpected interrupts */
114 static int print_unex
=1;
115 #include <linux/module.h>
117 /* the following is the mask of allowed drives. By default units 2 and
118 * 3 of both floppy controllers are disabled, because switching on the
119 * motor of these drives causes system hangs on some PCI computers. drive
120 * 0 is the low bit (0x1), and drive 7 is the high bit (0x80). Bits are on if
121 * a drive is allowed. */
123 static int FLOPPY_IRQ
=6;
124 static int FLOPPY_DMA
=2;
125 static int allowed_drive_mask
= 0x33;
126 static int irqdma_allocated
= 0;
129 #include <linux/sched.h>
130 #include <linux/fs.h>
131 #include <linux/kernel.h>
132 #include <linux/timer.h>
133 #include <linux/tqueue.h>
135 #include <linux/fdreg.h>
138 #include <linux/fd.h>
139 #include <linux/hdreg.h>
141 #include <linux/errno.h>
142 #include <linux/malloc.h>
143 #include <linux/mm.h>
144 #include <linux/string.h>
145 #include <linux/fcntl.h>
146 #include <linux/delay.h>
147 #include <linux/mc146818rtc.h> /* CMOS defines */
148 #include <linux/ioport.h>
149 #include <linux/interrupt.h>
150 #include <linux/init.h>
154 #include <asm/system.h>
156 #include <asm/uaccess.h>
158 static int can_use_virtual_dma
=2;
160 * can use virtual DMA:
161 * 0 = use of virtual DMA disallowed by config
162 * 1 = use of virtual DMA prescribed by config
163 * 2 = no virtual DMA preference configured. By default try hard DMA,
164 * but fall back on virtual DMA when not enough memory available
167 static int use_virtual_dma
=0;
171 * 1 using virtual DMA
172 * This variable is set to virtual when a DMA mem problem arises, and
173 * reset back in floppy_grab_irq_and_dma.
174 * It is not safe to reset it in other circumstances, because the floppy
175 * driver may have several buffers in use at once, and we do currently not
176 * record each buffers capabilities
179 static unsigned short virtual_dma_port
=0x3f0;
180 void floppy_interrupt(int irq
, void *dev_id
, struct pt_regs
* regs
);
181 static int set_dor(int fdc
, char mask
, char data
);
182 static inline int __get_order(unsigned long size
);
183 #define K_64 0x10000 /* 64KB */
184 #include <asm/floppy.h>
187 #define MAJOR_NR FLOPPY_MAJOR
189 #include <linux/blk.h>
190 #include <linux/cdrom.h> /* for the compatibility eject ioctl */
192 #ifndef fd_get_dma_residue
193 #define fd_get_dma_residue() get_dma_residue(FLOPPY_DMA)
196 /* Dma Memory related stuff */
198 /* Pure 2^n version of get_order */
199 static inline int __get_order(unsigned long size
)
203 size
= (size
-1) >> (PAGE_SHIFT
-1);
212 #ifndef fd_dma_mem_free
213 #define fd_dma_mem_free(addr, size) free_pages(addr, __get_order(size))
216 #ifndef fd_dma_mem_alloc
217 #define fd_dma_mem_alloc(size) __get_dma_pages(GFP_KERNEL,__get_order(size))
220 static inline void fallback_on_nodma_alloc(char **addr
, size_t l
)
222 #ifdef FLOPPY_CAN_FALLBACK_ON_NODMA
224 return; /* we have the memory */
225 if(can_use_virtual_dma
!= 2)
226 return; /* no fallback allowed */
227 printk("DMA memory shortage. Temporarily falling back on virtual DMA\n");
228 *addr
= (char *) nodma_mem_alloc(l
);
234 /* End dma memory related stuff */
236 static unsigned int fake_change
= 0;
237 static int initialising
=1;
239 static inline int TYPE(kdev_t x
) {
240 return (MINOR(x
)>>2) & 0x1f;
242 static inline int DRIVE(kdev_t x
) {
243 return (MINOR(x
)&0x03) | ((MINOR(x
)&0x80) >> 5);
245 #define ITYPE(x) (((x)>>2) & 0x1f)
246 #define TOMINOR(x) ((x & 3) | ((x & 4) << 5))
247 #define UNIT(x) ((x) & 0x03) /* drive on fdc */
248 #define FDC(x) (((x) & 0x04) >> 2) /* fdc of drive */
249 #define REVDRIVE(fdc, unit) ((unit) + ((fdc) << 2))
250 /* reverse mapping from unit and fdc to drive */
251 #define DP (&drive_params[current_drive])
252 #define DRS (&drive_state[current_drive])
253 #define DRWE (&write_errors[current_drive])
254 #define FDCS (&fdc_state[fdc])
255 #define CLEARF(x) (clear_bit(x##_BIT, &DRS->flags))
256 #define SETF(x) (set_bit(x##_BIT, &DRS->flags))
257 #define TESTF(x) (test_bit(x##_BIT, &DRS->flags))
259 #define UDP (&drive_params[drive])
260 #define UDRS (&drive_state[drive])
261 #define UDRWE (&write_errors[drive])
262 #define UFDCS (&fdc_state[FDC(drive)])
263 #define UCLEARF(x) (clear_bit(x##_BIT, &UDRS->flags))
264 #define USETF(x) (set_bit(x##_BIT, &UDRS->flags))
265 #define UTESTF(x) (test_bit(x##_BIT, &UDRS->flags))
267 #define DPRINT(format, args...) printk(DEVICE_NAME "%d: " format, current_drive , ## args)
269 #define PH_HEAD(floppy,head) (((((floppy)->stretch & 2) >>1) ^ head) << 2)
270 #define STRETCH(floppy) ((floppy)->stretch & FD_STRETCH)
272 #define CLEARSTRUCT(x) memset((x), 0, sizeof(*(x)))
274 #define INT_OFF save_flags(flags); cli()
275 #define INT_ON restore_flags(flags)
278 #define COMMAND raw_cmd->cmd[0]
279 #define DR_SELECT raw_cmd->cmd[1]
280 #define TRACK raw_cmd->cmd[2]
281 #define HEAD raw_cmd->cmd[3]
282 #define SECTOR raw_cmd->cmd[4]
283 #define SIZECODE raw_cmd->cmd[5]
284 #define SECT_PER_TRACK raw_cmd->cmd[6]
285 #define GAP raw_cmd->cmd[7]
286 #define SIZECODE2 raw_cmd->cmd[8]
290 #define F_SIZECODE raw_cmd->cmd[2]
291 #define F_SECT_PER_TRACK raw_cmd->cmd[3]
292 #define F_GAP raw_cmd->cmd[4]
293 #define F_FILL raw_cmd->cmd[5]
297 * Maximum disk size (in kilobytes). This default is used whenever the
298 * current disk size is unknown.
299 * [Now it is rather a minimum]
301 #define MAX_DISK_SIZE 4 /* 3984*/
305 * globals used by 'result()'
307 #define MAX_REPLIES 16
308 static unsigned char reply_buffer
[MAX_REPLIES
];
309 static int inr
; /* size of reply buffer, when called from interrupt */
310 #define ST0 (reply_buffer[0])
311 #define ST1 (reply_buffer[1])
312 #define ST2 (reply_buffer[2])
313 #define ST3 (reply_buffer[0]) /* result of GETSTATUS */
314 #define R_TRACK (reply_buffer[3])
315 #define R_HEAD (reply_buffer[4])
316 #define R_SECTOR (reply_buffer[5])
317 #define R_SIZECODE (reply_buffer[6])
319 #define SEL_DLY (2*HZ/100)
321 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
323 * this struct defines the different floppy drive types.
326 struct floppy_drive_params params
;
327 const char *name
; /* name printed while booting */
328 } default_drive_params
[]= {
329 /* NOTE: the time values in jiffies should be in msec!
331 | Maximum data rate supported by drive type
332 | | Head load time, msec
333 | | | Head unload time, msec (not used)
334 | | | | Step rate interval, usec
335 | | | | | Time needed for spinup time (jiffies)
336 | | | | | | Timeout for spinning down (jiffies)
337 | | | | | | | Spindown offset (where disk stops)
338 | | | | | | | | Select delay
339 | | | | | | | | | RPS
340 | | | | | | | | | | Max number of tracks
341 | | | | | | | | | | | Interrupt timeout
342 | | | | | | | | | | | | Max nonintlv. sectors
343 | | | | | | | | | | | | | -Max Errors- flags */
344 {{0, 500, 16, 16, 8000, 1*HZ
, 3*HZ
, 0, SEL_DLY
, 5, 80, 3*HZ
, 20, {3,1,2,0,2}, 0,
345 0, { 7, 4, 8, 2, 1, 5, 3,10}, 3*HZ
/2, 0 }, "unknown" },
347 {{1, 300, 16, 16, 8000, 1*HZ
, 3*HZ
, 0, SEL_DLY
, 5, 40, 3*HZ
, 17, {3,1,2,0,2}, 0,
348 0, { 1, 0, 0, 0, 0, 0, 0, 0}, 3*HZ
/2, 1 }, "360K PC" }, /*5 1/4 360 KB PC*/
350 {{2, 500, 16, 16, 6000, 4*HZ
/10, 3*HZ
, 14, SEL_DLY
, 6, 83, 3*HZ
, 17, {3,1,2,0,2}, 0,
351 0, { 2, 5, 6,23,10,20,11, 0}, 3*HZ
/2, 2 }, "1.2M" }, /*5 1/4 HD AT*/
353 {{3, 250, 16, 16, 3000, 1*HZ
, 3*HZ
, 0, SEL_DLY
, 5, 83, 3*HZ
, 20, {3,1,2,0,2}, 0,
354 0, { 4,22,21,30, 3, 0, 0, 0}, 3*HZ
/2, 4 }, "720k" }, /*3 1/2 DD*/
356 {{4, 500, 16, 16, 4000, 4*HZ
/10, 3*HZ
, 10, SEL_DLY
, 5, 83, 3*HZ
, 20, {3,1,2,0,2}, 0,
357 0, { 7, 4,25,22,31,21,29,11}, 3*HZ
/2, 7 }, "1.44M" }, /*3 1/2 HD*/
359 {{5, 1000, 15, 8, 3000, 4*HZ
/10, 3*HZ
, 10, SEL_DLY
, 5, 83, 3*HZ
, 40, {3,1,2,0,2}, 0,
360 0, { 7, 8, 4,25,28,22,31,21}, 3*HZ
/2, 8 }, "2.88M AMI BIOS" }, /*3 1/2 ED*/
362 {{6, 1000, 15, 8, 3000, 4*HZ
/10, 3*HZ
, 10, SEL_DLY
, 5, 83, 3*HZ
, 40, {3,1,2,0,2}, 0,
363 0, { 7, 8, 4,25,28,22,31,21}, 3*HZ
/2, 8 }, "2.88M" } /*3 1/2 ED*/
364 /* | --autodetected formats--- | | |
365 * read_track | | Name printed when booting
367 * Frequency of disk change checks */
370 static struct floppy_drive_params drive_params
[N_DRIVE
];
371 static struct floppy_drive_struct drive_state
[N_DRIVE
];
372 static struct floppy_write_errors write_errors
[N_DRIVE
];
373 static struct floppy_raw_cmd
*raw_cmd
, default_raw_cmd
;
376 * This struct defines the different floppy types.
378 * Bit 0 of 'stretch' tells if the tracks need to be doubled for some
379 * types (e.g. 360kB diskette in 1.2MB drive, etc.). Bit 1 of 'stretch'
380 * tells if the disk is in Commodore 1581 format, which means side 0 sectors
381 * are located on side 1 of the disk but with a side 0 ID, and vice-versa.
382 * This is the same as the Sharp MZ-80 5.25" CP/M disk format, except that the
383 * 1581's logical side 0 is on physical side 1, whereas the Sharp's logical
384 * side 0 is on physical side 0 (but with the misnamed sector IDs).
385 * 'stretch' should probably be renamed to something more general, like
386 * 'options'. Other parameters should be self-explanatory (see also
389 static struct floppy_struct floppy_type
[32] = {
390 { 0, 0,0, 0,0,0x00,0x00,0x00,0x00,NULL
}, /* 0 no testing */
391 { 720, 9,2,40,0,0x2A,0x02,0xDF,0x50,"d360" }, /* 1 360KB PC */
392 { 2400,15,2,80,0,0x1B,0x00,0xDF,0x54,"h1200" }, /* 2 1.2MB AT */
393 { 720, 9,1,80,0,0x2A,0x02,0xDF,0x50,"D360" }, /* 3 360KB SS 3.5" */
394 { 1440, 9,2,80,0,0x2A,0x02,0xDF,0x50,"D720" }, /* 4 720KB 3.5" */
395 { 720, 9,2,40,1,0x23,0x01,0xDF,0x50,"h360" }, /* 5 360KB AT */
396 { 1440, 9,2,80,0,0x23,0x01,0xDF,0x50,"h720" }, /* 6 720KB AT */
397 { 2880,18,2,80,0,0x1B,0x00,0xCF,0x6C,"H1440" }, /* 7 1.44MB 3.5" */
398 { 5760,36,2,80,0,0x1B,0x43,0xAF,0x54,"E2880" }, /* 8 2.88MB 3.5" */
399 { 6240,39,2,80,0,0x1B,0x43,0xAF,0x28,"E3120"}, /* 9 3.12MB 3.5" */
401 { 2880,18,2,80,0,0x25,0x00,0xDF,0x02,"h1440" }, /* 10 1.44MB 5.25" */
402 { 3360,21,2,80,0,0x1C,0x00,0xCF,0x0C,"H1680" }, /* 11 1.68MB 3.5" */
403 { 820,10,2,41,1,0x25,0x01,0xDF,0x2E,"h410" }, /* 12 410KB 5.25" */
404 { 1640,10,2,82,0,0x25,0x02,0xDF,0x2E,"H820" }, /* 13 820KB 3.5" */
405 { 2952,18,2,82,0,0x25,0x00,0xDF,0x02,"h1476" }, /* 14 1.48MB 5.25" */
406 { 3444,21,2,82,0,0x25,0x00,0xDF,0x0C,"H1722" }, /* 15 1.72MB 3.5" */
407 { 840,10,2,42,1,0x25,0x01,0xDF,0x2E,"h420" }, /* 16 420KB 5.25" */
408 { 1660,10,2,83,0,0x25,0x02,0xDF,0x2E,"H830" }, /* 17 830KB 3.5" */
409 { 2988,18,2,83,0,0x25,0x00,0xDF,0x02,"h1494" }, /* 18 1.49MB 5.25" */
410 { 3486,21,2,83,0,0x25,0x00,0xDF,0x0C,"H1743" }, /* 19 1.74 MB 3.5" */
412 { 1760,11,2,80,0,0x1C,0x09,0xCF,0x00,"h880" }, /* 20 880KB 5.25" */
413 { 2080,13,2,80,0,0x1C,0x01,0xCF,0x00,"D1040" }, /* 21 1.04MB 3.5" */
414 { 2240,14,2,80,0,0x1C,0x19,0xCF,0x00,"D1120" }, /* 22 1.12MB 3.5" */
415 { 3200,20,2,80,0,0x1C,0x20,0xCF,0x2C,"h1600" }, /* 23 1.6MB 5.25" */
416 { 3520,22,2,80,0,0x1C,0x08,0xCF,0x2e,"H1760" }, /* 24 1.76MB 3.5" */
417 { 3840,24,2,80,0,0x1C,0x20,0xCF,0x00,"H1920" }, /* 25 1.92MB 3.5" */
418 { 6400,40,2,80,0,0x25,0x5B,0xCF,0x00,"E3200" }, /* 26 3.20MB 3.5" */
419 { 7040,44,2,80,0,0x25,0x5B,0xCF,0x00,"E3520" }, /* 27 3.52MB 3.5" */
420 { 7680,48,2,80,0,0x25,0x63,0xCF,0x00,"E3840" }, /* 28 3.84MB 3.5" */
422 { 3680,23,2,80,0,0x1C,0x10,0xCF,0x00,"H1840" }, /* 29 1.84MB 3.5" */
423 { 1600,10,2,80,0,0x25,0x02,0xDF,0x2E,"D800" }, /* 30 800KB 3.5" */
424 { 3200,20,2,80,0,0x1C,0x00,0xCF,0x2C,"H1600" }, /* 31 1.6MB 3.5" */
427 #define NUMBER(x) (sizeof(x) / sizeof(*(x)))
428 #define SECTSIZE (_FD_SECTSIZE(*floppy))
430 /* Auto-detection: Disk type used until the next media change occurs. */
431 static struct floppy_struct
*current_type
[N_DRIVE
] = {
432 NULL
, NULL
, NULL
, NULL
,
433 NULL
, NULL
, NULL
, NULL
437 * User-provided type information. current_type points to
438 * the respective entry of this array.
440 static struct floppy_struct user_params
[N_DRIVE
];
442 static int floppy_sizes
[256];
443 static int floppy_blocksizes
[256] = { 0, };
446 * The driver is trying to determine the correct media format
447 * while probing is set. rw_interrupt() clears it after a
450 static int probing
= 0;
452 /* Synchronization of FDC access. */
453 #define FD_COMMAND_NONE -1
454 #define FD_COMMAND_ERROR 2
455 #define FD_COMMAND_OKAY 3
457 static volatile int command_status
= FD_COMMAND_NONE
, fdc_busy
= 0;
458 static struct wait_queue
*fdc_wait
= NULL
, *command_done
= NULL
;
459 #define NO_SIGNAL (!interruptible || !signal_pending(current))
460 #define CALL(x) if ((x) == -EINTR) return -EINTR
461 #define ECALL(x) if ((ret = (x))) return ret;
462 #define _WAIT(x,i) CALL(ret=wait_til_done((x),i))
463 #define WAIT(x) _WAIT((x),interruptible)
464 #define IWAIT(x) _WAIT((x),1)
466 /* Errors during formatting are counted here. */
467 static int format_errors
;
469 /* Format request descriptor. */
470 static struct format_descr format_req
;
473 * Rate is 0 for 500kb/s, 1 for 300kbps, 2 for 250kbps
474 * Spec1 is 0xSH, where S is stepping rate (F=1ms, E=2ms, D=3ms etc),
475 * H is head unload time (1=16ms, 2=32ms, etc)
480 * Because these are written to by the DMA controller, they must
481 * not contain a 64k byte boundary crossing, or data will be
484 static char *floppy_track_buffer
=0;
485 static int max_buffer_sectors
=0;
488 typedef void (*done_f
)(int);
489 static struct cont_t
{
490 void (*interrupt
)(void); /* this is called after the interrupt of the
492 void (*redo
)(void); /* this is called to retry the operation */
493 void (*error
)(void); /* this is called to tally an error */
494 done_f done
; /* this is called to say if the operation has
495 * succeeded/failed */
498 static void floppy_ready(void);
499 static void floppy_start(void);
500 static void process_fd_request(void);
501 static void recalibrate_floppy(void);
502 static void floppy_shutdown(void);
504 static int floppy_grab_irq_and_dma(void);
505 static void floppy_release_irq_and_dma(void);
508 * The "reset" variable should be tested whenever an interrupt is scheduled,
509 * after the commands have been sent. This is to ensure that the driver doesn't
510 * get wedged when the interrupt doesn't come because of a failed command.
511 * reset doesn't need to be tested before sending commands, because
512 * output_byte is automatically disabled when reset is set.
514 #define CHECK_RESET { if (FDCS->reset){ reset_fdc(); return; } }
515 static void reset_fdc(void);
518 * These are global variables, as that's the easiest way to give
519 * information to interrupts. They are the data used for the current
523 #define NEED_1_RECAL -2
524 #define NEED_2_RECAL -3
527 static int usage_count
= 0;
530 /* buffer related variables */
531 static int buffer_track
= -1;
532 static int buffer_drive
= -1;
533 static int buffer_min
= -1;
534 static int buffer_max
= -1;
536 /* fdc related variables, should end up in a struct */
537 static struct floppy_fdc_state fdc_state
[N_FDC
];
538 static int fdc
; /* current fdc */
540 static struct floppy_struct
*_floppy
= floppy_type
;
541 static unsigned char current_drive
= 0;
542 static long current_count_sectors
= 0;
543 static unsigned char sector_t
; /* sector in track */
544 static unsigned char in_sector_offset
; /* offset within physical sector,
545 * expressed in units of 512 bytes */
548 #define fd_eject(x) -EINVAL
553 static long unsigned debugtimer
;
560 static inline void set_debugt(void)
563 debugtimer
= jiffies
;
567 static inline void debugt(const char *message
)
570 if (DP
->flags
& DEBUGT
)
571 printk("%s dtime=%lu\n", message
, jiffies
-debugtimer
);
575 typedef void (*timeout_fn
)(unsigned long);
576 static struct timer_list fd_timeout
={ NULL
, NULL
, 0, 0,
577 (timeout_fn
) floppy_shutdown
};
579 static const char *timeout_message
;
581 #ifdef FLOPPY_SANITY_CHECK
582 static void is_alive(const char *message
)
584 /* this routine checks whether the floppy driver is "alive" */
585 if (fdc_busy
&& command_status
< 2 && !fd_timeout
.prev
){
586 DPRINT("timeout handler died: %s\n",message
);
591 #ifdef FLOPPY_SANITY_CHECK
595 static void (*lasthandler
)(void) = NULL
;
596 static int interruptjiffies
=0;
597 static int resultjiffies
=0;
598 static int resultsize
=0;
599 static int lastredo
=0;
601 static struct output_log
{
603 unsigned char status
;
604 unsigned long jiffies
;
605 } output_log
[OLOGSIZE
];
607 static int output_log_pos
=0;
611 #define MAXTIMEOUT -2
613 static void reschedule_timeout(int drive
, const char *message
, int marg
)
615 if (drive
== CURRENTD
)
616 drive
= current_drive
;
617 del_timer(&fd_timeout
);
618 if (drive
< 0 || drive
> N_DRIVE
) {
619 fd_timeout
.expires
= jiffies
+ 20*HZ
;
622 fd_timeout
.expires
= jiffies
+ UDP
->timeout
;
623 add_timer(&fd_timeout
);
624 if (UDP
->flags
& FD_DEBUG
){
625 DPRINT("reschedule timeout ");
626 printk(message
, marg
);
629 timeout_message
= message
;
632 static int maximum(int a
, int b
)
639 #define INFBOUND(a,b) (a)=maximum((a),(b));
641 static int minimum(int a
, int b
)
648 #define SUPBOUND(a,b) (a)=minimum((a),(b));
652 * Bottom half floppy driver.
653 * ==========================
655 * This part of the file contains the code talking directly to the hardware,
656 * and also the main service loop (seek-configure-spinup-command)
661 * This routine is responsible for maintaining the FD_DISK_CHANGE flag,
662 * and the last_checked date.
664 * last_checked is the date of the last check which showed 'no disk change'
665 * FD_DISK_CHANGE is set under two conditions:
666 * 1. The floppy has been changed after some i/o to that floppy already
668 * 2. No floppy disk is in the drive. This is done in order to ensure that
669 * requests are quickly flushed in case there is no disk in the drive. It
670 * follows that FD_DISK_CHANGE can only be cleared if there is a disk in
673 * For 1., maxblock is observed. Maxblock is 0 if no i/o has taken place yet.
674 * For 2., FD_DISK_NEWCHANGE is watched. FD_DISK_NEWCHANGE is cleared on
675 * each seek. If a disk is present, the disk change line should also be
676 * cleared on each seek. Thus, if FD_DISK_NEWCHANGE is clear, but the disk
677 * change line is set, this means either that no disk is in the drive, or
678 * that it has been removed since the last seek.
680 * This means that we really have a third possibility too:
681 * The floppy has been changed after the last seek.
684 static int disk_change(int drive
)
687 #ifdef FLOPPY_SANITY_CHECK
688 if (jiffies
- UDRS
->select_date
< UDP
->select_delay
)
689 DPRINT("WARNING disk change called early\n");
690 if (!(FDCS
->dor
& (0x10 << UNIT(drive
))) ||
691 (FDCS
->dor
& 3) != UNIT(drive
) ||
693 DPRINT("probing disk change on unselected drive\n");
694 DPRINT("drive=%d fdc=%d dor=%x\n",drive
, FDC(drive
),
700 if (UDP
->flags
& FD_DEBUG
){
701 DPRINT("checking disk change line for drive %d\n",drive
);
702 DPRINT("jiffies=%ld\n", jiffies
);
703 DPRINT("disk change line=%x\n",fd_inb(FD_DIR
)&0x80);
704 DPRINT("flags=%lx\n",UDRS
->flags
);
707 if (UDP
->flags
& FD_BROKEN_DCL
)
708 return UTESTF(FD_DISK_CHANGED
);
709 if ((fd_inb(FD_DIR
) ^ UDP
->flags
) & 0x80){
710 USETF(FD_VERIFY
); /* verify write protection */
712 /* mark it changed */
713 USETF(FD_DISK_CHANGED
);
716 /* invalidate its geometry */
717 if (UDRS
->keep_data
>= 0) {
718 if ((UDP
->flags
& FTD_MSG
) &&
719 current_type
[drive
] != NULL
)
720 DPRINT("Disk type is undefined after "
722 current_type
[drive
] = NULL
;
723 floppy_sizes
[TOMINOR(drive
)] = MAX_DISK_SIZE
;
726 /*USETF(FD_DISK_NEWCHANGE);*/
729 UDRS
->last_checked
=jiffies
;
730 UCLEARF(FD_DISK_NEWCHANGE
);
735 static inline int is_selected(int dor
, int unit
)
737 return ((dor
& (0x10 << unit
)) && (dor
&3) == unit
);
740 static int set_dor(int fdc
, char mask
, char data
)
742 register unsigned char drive
, unit
, newdor
,olddor
;
744 if (FDCS
->address
== -1)
748 newdor
= (olddor
& mask
) | data
;
749 if (newdor
!= olddor
){
751 if (is_selected(olddor
, unit
) && !is_selected(newdor
,unit
)){
752 drive
= REVDRIVE(fdc
,unit
);
754 if (UDP
->flags
& FD_DEBUG
){
755 DPRINT("calling disk change from set_dor\n");
761 fd_outb(newdor
, FD_DOR
);
764 if (!is_selected(olddor
, unit
) && is_selected(newdor
,unit
)){
765 drive
= REVDRIVE(fdc
,unit
);
766 UDRS
->select_date
= jiffies
;
770 * We should propogate failures to grab the resources back
771 * nicely from here. Actually we ought to rewrite the fd
772 * driver some day too.
774 if (newdor
& FLOPPY_MOTOR_MASK
)
775 floppy_grab_irq_and_dma();
776 if (olddor
& FLOPPY_MOTOR_MASK
)
777 floppy_release_irq_and_dma();
781 static void twaddle(void)
783 if (DP
->select_delay
)
785 fd_outb(FDCS
->dor
& ~(0x10<<UNIT(current_drive
)),FD_DOR
);
786 fd_outb(FDCS
->dor
, FD_DOR
);
787 DRS
->select_date
= jiffies
;
790 /* reset all driver information about the current fdc. This is needed after
791 * a reset, and after a raw command. */
792 static void reset_fdc_info(int mode
)
796 FDCS
->spec1
= FDCS
->spec2
= -1;
797 FDCS
->need_configure
= 1;
800 for (drive
= 0; drive
< N_DRIVE
; drive
++)
801 if (FDC(drive
) == fdc
&&
802 (mode
|| UDRS
->track
!= NEED_1_RECAL
))
803 UDRS
->track
= NEED_2_RECAL
;
806 /* selects the fdc and drive, and enables the fdc's input/dma. */
807 static void set_fdc(int drive
)
809 if (drive
>= 0 && drive
< N_DRIVE
){
811 current_drive
= drive
;
813 if (fdc
!= 1 && fdc
!= 0) {
814 printk("bad fdc value\n");
819 set_dor(1-fdc
, ~8, 0);
821 if (FDCS
->rawcmd
== 2)
823 if (fd_inb(FD_STATUS
) != STATUS_READY
)
827 /* locks the driver */
828 static int lock_fdc(int drive
, int interruptible
)
833 printk(KERN_ERR
"Trying to lock fdc while usage count=0\n");
836 if(floppy_grab_irq_and_dma()==-1)
839 while (fdc_busy
&& NO_SIGNAL
)
840 interruptible_sleep_on(&fdc_wait
);
847 command_status
= FD_COMMAND_NONE
;
848 reschedule_timeout(drive
, "lock fdc", 0);
853 #define LOCK_FDC(drive,interruptible) \
854 if (lock_fdc(drive,interruptible)) return -EINTR;
857 /* unlocks the driver */
858 static inline void unlock_fdc(void)
862 DPRINT("FDC access conflict!\n");
865 DPRINT("device interrupt still active at FDC release: %p!\n",
867 command_status
= FD_COMMAND_NONE
;
868 del_timer(&fd_timeout
);
871 floppy_release_irq_and_dma();
875 /* switches the motor off after a given timeout */
876 static void motor_off_callback(unsigned long nr
)
878 unsigned char mask
= ~(0x10 << UNIT(nr
));
880 set_dor(FDC(nr
), mask
, 0);
883 static struct timer_list motor_off_timer
[N_DRIVE
] = {
884 { NULL
, NULL
, 0, 0, motor_off_callback
},
885 { NULL
, NULL
, 0, 1, motor_off_callback
},
886 { NULL
, NULL
, 0, 2, motor_off_callback
},
887 { NULL
, NULL
, 0, 3, motor_off_callback
},
888 { NULL
, NULL
, 0, 4, motor_off_callback
},
889 { NULL
, NULL
, 0, 5, motor_off_callback
},
890 { NULL
, NULL
, 0, 6, motor_off_callback
},
891 { NULL
, NULL
, 0, 7, motor_off_callback
}
894 /* schedules motor off */
895 static void floppy_off(unsigned int drive
)
897 unsigned long volatile delta
;
898 register int fdc
=FDC(drive
);
900 if (!(FDCS
->dor
& (0x10 << UNIT(drive
))))
903 del_timer(motor_off_timer
+drive
);
905 /* make spindle stop in a position which minimizes spinup time
908 delta
= jiffies
- UDRS
->first_read_date
+ HZ
-
909 UDP
->spindown_offset
;
910 delta
= ((delta
* UDP
->rps
) % HZ
) / UDP
->rps
;
911 motor_off_timer
[drive
].expires
= jiffies
+ UDP
->spindown
- delta
;
913 add_timer(motor_off_timer
+drive
);
917 * cycle through all N_DRIVE floppy drives, for disk change testing.
918 * stopping at current drive. This is done before any long operation, to
919 * be sure to have up to date disk change information.
921 static void scandrives(void)
923 int i
, drive
, saved_drive
;
925 if (DP
->select_delay
)
928 saved_drive
= current_drive
;
929 for (i
=0; i
< N_DRIVE
; i
++){
930 drive
= (saved_drive
+ i
+ 1) % N_DRIVE
;
931 if (UDRS
->fd_ref
== 0 || UDP
->select_delay
!= 0)
932 continue; /* skip closed drives */
934 if (!(set_dor(fdc
, ~3, UNIT(drive
) | (0x10 << UNIT(drive
))) &
935 (0x10 << UNIT(drive
))))
936 /* switch the motor off again, if it was off to
938 set_dor(fdc
, ~(0x10 << UNIT(drive
)), 0);
940 set_fdc(saved_drive
);
943 static void empty(void)
947 static struct tq_struct floppy_tq
=
950 static void schedule_bh( void (*handler
)(void*) )
952 floppy_tq
.routine
= (void *)(void *) handler
;
953 queue_task(&floppy_tq
, &tq_immediate
);
954 mark_bh(IMMEDIATE_BH
);
957 static struct timer_list fd_timer
={ NULL
, NULL
, 0, 0, 0 };
959 static void cancel_activity(void)
962 floppy_tq
.routine
= (void *)(void *) empty
;
963 del_timer(&fd_timer
);
966 /* this function makes sure that the disk stays in the drive during the
968 static void fd_watchdog(void)
971 if (DP
->flags
& FD_DEBUG
){
972 DPRINT("calling disk change from watchdog\n");
976 if (disk_change(current_drive
)){
977 DPRINT("disk removed during i/o\n");
982 del_timer(&fd_timer
);
983 fd_timer
.function
= (timeout_fn
) fd_watchdog
;
984 fd_timer
.expires
= jiffies
+ HZ
/ 10;
985 add_timer(&fd_timer
);
989 static void main_command_interrupt(void)
991 del_timer(&fd_timer
);
995 /* waits for a delay (spinup or select) to pass */
996 static int wait_for_completion(int delay
, timeout_fn function
)
999 reset_fdc(); /* do the reset during sleep to win time
1000 * if we don't need to sleep, it's a good
1001 * occasion anyways */
1005 if ((signed) (jiffies
- delay
) < 0){
1006 del_timer(&fd_timer
);
1007 fd_timer
.function
= function
;
1008 fd_timer
.expires
= delay
;
1009 add_timer(&fd_timer
);
1015 static int hlt_disabled
=0;
1016 static void floppy_disable_hlt(void)
1018 unsigned long flags
;
1023 #ifdef HAVE_DISABLE_HLT
1030 static void floppy_enable_hlt(void)
1032 unsigned long flags
;
1037 #ifdef HAVE_DISABLE_HLT
1045 static void setup_DMA(void)
1047 unsigned long flags
;
1049 #ifdef FLOPPY_SANITY_CHECK
1050 if (raw_cmd
->length
== 0){
1053 printk("zero dma transfer size:");
1054 for (i
=0; i
< raw_cmd
->cmd_count
; i
++)
1055 printk("%x,", raw_cmd
->cmd
[i
]);
1061 if (((unsigned long) raw_cmd
->kernel_data
) % 512){
1062 printk("non aligned address: %p\n", raw_cmd
->kernel_data
);
1071 if(fd_dma_setup(raw_cmd
->kernel_data
, raw_cmd
->length
,
1072 (raw_cmd
->flags
& FD_RAW_READ
)?
1073 DMA_MODE_READ
: DMA_MODE_WRITE
,
1074 FDCS
->address
) < 0) {
1082 fd_cacheflush(raw_cmd
->kernel_data
, raw_cmd
->length
);
1083 fd_set_dma_mode((raw_cmd
->flags
& FD_RAW_READ
)?
1084 DMA_MODE_READ
: DMA_MODE_WRITE
);
1085 fd_set_dma_addr(raw_cmd
->kernel_data
);
1086 fd_set_dma_count(raw_cmd
->length
);
1087 virtual_dma_port
= FDCS
->address
;
1091 floppy_disable_hlt();
1094 static void show_floppy(void);
1096 /* waits until the fdc becomes ready */
1097 static int wait_til_ready(void)
1099 int counter
, status
;
1102 for (counter
= 0; counter
< 10000; counter
++) {
1103 status
= fd_inb(FD_STATUS
);
1104 if (status
& STATUS_READY
)
1107 if (!initialising
) {
1108 DPRINT("Getstatus times out (%x) on fdc %d\n",
1116 /* sends a command byte to the fdc */
1117 static int output_byte(char byte
)
1121 if ((status
= wait_til_ready()) < 0)
1123 if ((status
& (STATUS_READY
|STATUS_DIR
|STATUS_DMA
)) == STATUS_READY
){
1124 fd_outb(byte
,FD_DATA
);
1125 #ifdef FLOPPY_SANITY_CHECK
1126 output_log
[output_log_pos
].data
= byte
;
1127 output_log
[output_log_pos
].status
= status
;
1128 output_log
[output_log_pos
].jiffies
= jiffies
;
1129 output_log_pos
= (output_log_pos
+ 1) % OLOGSIZE
;
1134 if (!initialising
) {
1135 DPRINT("Unable to send byte %x to FDC. Fdc=%x Status=%x\n",
1141 #define LAST_OUT(x) if (output_byte(x)<0){ reset_fdc();return;}
1143 /* gets the response from the fdc */
1144 static int result(void)
1148 for(i
=0; i
< MAX_REPLIES
; i
++) {
1149 if ((status
= wait_til_ready()) < 0)
1151 status
&= STATUS_DIR
|STATUS_READY
|STATUS_BUSY
|STATUS_DMA
;
1152 if ((status
& ~STATUS_BUSY
) == STATUS_READY
){
1153 #ifdef FLOPPY_SANITY_CHECK
1154 resultjiffies
= jiffies
;
1159 if (status
== (STATUS_DIR
|STATUS_READY
|STATUS_BUSY
))
1160 reply_buffer
[i
] = fd_inb(FD_DATA
);
1165 DPRINT("get result error. Fdc=%d Last status=%x Read bytes=%d\n",
1173 #define MORE_OUTPUT -2
1174 /* does the fdc need more output? */
1175 static int need_more_output(void)
1178 if( (status
= wait_til_ready()) < 0)
1180 if ((status
& (STATUS_READY
|STATUS_DIR
|STATUS_DMA
)) == STATUS_READY
)
1185 /* Set perpendicular mode as required, based on data rate, if supported.
1186 * 82077 Now tested. 1Mbps data rate only possible with 82077-1.
1188 static inline void perpendicular_mode(void)
1190 unsigned char perp_mode
;
1192 if (raw_cmd
->rate
& 0x40){
1193 switch(raw_cmd
->rate
& 3){
1201 DPRINT("Invalid data rate for perpendicular mode!\n");
1203 FDCS
->reset
= 1; /* convenient way to return to
1204 * redo without to much hassle (deep
1211 if (FDCS
->perp_mode
== perp_mode
)
1213 if (FDCS
->version
>= FDC_82077_ORIG
) {
1214 output_byte(FD_PERPENDICULAR
);
1215 output_byte(perp_mode
);
1216 FDCS
->perp_mode
= perp_mode
;
1217 } else if (perp_mode
) {
1218 DPRINT("perpendicular mode not supported by this FDC.\n");
1220 } /* perpendicular_mode */
1222 static int fifo_depth
= 0xa;
1223 static int no_fifo
= 0;
1225 static int fdc_configure(void)
1228 output_byte(FD_CONFIGURE
);
1229 if(need_more_output() != MORE_OUTPUT
)
1232 output_byte(0x10 | (no_fifo
& 0x20) | (fifo_depth
& 0xf));
1233 output_byte(0); /* pre-compensation from track
1238 #define NOMINAL_DTR 500
1240 /* Issue a "SPECIFY" command to set the step rate time, head unload time,
1241 * head load time, and DMA disable flag to values needed by floppy.
1243 * The value "dtr" is the data transfer rate in Kbps. It is needed
1244 * to account for the data rate-based scaling done by the 82072 and 82077
1245 * FDC types. This parameter is ignored for other types of FDCs (i.e.
1248 * Note that changing the data transfer rate has a (probably deleterious)
1249 * effect on the parameters subject to scaling for 82072/82077 FDCs, so
1250 * fdc_specify is called again after each data transfer rate
1253 * srt: 1000 to 16000 in microseconds
1254 * hut: 16 to 240 milliseconds
1255 * hlt: 2 to 254 milliseconds
1257 * These values are rounded up to the next highest available delay time.
1259 static void fdc_specify(void)
1261 unsigned char spec1
, spec2
;
1263 unsigned long dtr
= NOMINAL_DTR
;
1264 unsigned long scale_dtr
= NOMINAL_DTR
;
1265 int hlt_max_code
= 0x7f;
1266 int hut_max_code
= 0xf;
1268 if (FDCS
->need_configure
&& FDCS
->version
>= FDC_82072A
) {
1270 FDCS
->need_configure
= 0;
1271 /*DPRINT("FIFO enabled\n");*/
1274 switch (raw_cmd
->rate
& 0x03) {
1280 if (FDCS
->version
>= FDC_82078
) {
1281 /* chose the default rate table, not the one
1282 * where 1 = 2 Mbps */
1283 output_byte(FD_DRIVESPEC
);
1284 if(need_more_output() == MORE_OUTPUT
) {
1285 output_byte(UNIT(current_drive
));
1295 if (FDCS
->version
>= FDC_82072
) {
1297 hlt_max_code
= 0x00; /* 0==256msec*dtr0/dtr (not linear!) */
1298 hut_max_code
= 0x0; /* 0==256msec*dtr0/dtr (not linear!) */
1301 /* Convert step rate from microseconds to milliseconds and 4 bits */
1302 srt
= 16 - (DP
->srt
*scale_dtr
/1000 + NOMINAL_DTR
- 1)/NOMINAL_DTR
;
1306 hlt
= (DP
->hlt
*scale_dtr
/2 + NOMINAL_DTR
- 1)/NOMINAL_DTR
;
1309 else if (hlt
> 0x7f)
1312 hut
= (DP
->hut
*scale_dtr
/16 + NOMINAL_DTR
- 1)/NOMINAL_DTR
;
1318 spec1
= (srt
<< 4) | hut
;
1319 spec2
= (hlt
<< 1) | (use_virtual_dma
& 1);
1321 /* If these parameters did not change, just return with success */
1322 if (FDCS
->spec1
!= spec1
|| FDCS
->spec2
!= spec2
) {
1323 /* Go ahead and set spec1 and spec2 */
1324 output_byte(FD_SPECIFY
);
1325 output_byte(FDCS
->spec1
= spec1
);
1326 output_byte(FDCS
->spec2
= spec2
);
1330 /* Set the FDC's data transfer rate on behalf of the specified drive.
1331 * NOTE: with 82072/82077 FDCs, changing the data rate requires a reissue
1332 * of the specify command (i.e. using the fdc_specify function).
1334 static int fdc_dtr(void)
1336 /* If data rate not already set to desired value, set it. */
1337 if ((raw_cmd
->rate
& 3) == FDCS
->dtr
)
1341 fd_outb(raw_cmd
->rate
& 3, FD_DCR
);
1343 /* TODO: some FDC/drive combinations (C&T 82C711 with TEAC 1.2MB)
1344 * need a stabilization period of several milliseconds to be
1345 * enforced after data rate changes before R/W operations.
1346 * Pause 5 msec to avoid trouble. (Needs to be 2 jiffies)
1348 FDCS
->dtr
= raw_cmd
->rate
& 3;
1349 return(wait_for_completion(jiffies
+2*HZ
/100,
1350 (timeout_fn
) floppy_ready
));
1353 static void tell_sector(void)
1355 printk(": track %d, head %d, sector %d, size %d",
1356 R_TRACK
, R_HEAD
, R_SECTOR
, R_SIZECODE
);
1361 * OK, this error interpreting routine is called after a
1362 * DMA read/write has succeeded
1363 * or failed, so we check the results, and copy any buffers.
1364 * hhb: Added better error reporting.
1365 * ak: Made this into a separate routine.
1367 static int interpret_errors(void)
1372 DPRINT("-- FDC reply error");
1377 /* check IC to find cause of interrupt */
1378 switch (ST0
& ST0_INTR
) {
1379 case 0x40: /* error occurred during command execution */
1381 return 0; /* occurs with pseudo-DMA */
1384 DPRINT("Drive is write protected\n");
1385 CLEARF(FD_DISK_WRITABLE
);
1388 } else if (ST1
& ST1_ND
) {
1389 SETF(FD_NEED_TWADDLE
);
1390 } else if (ST1
& ST1_OR
) {
1391 if (DP
->flags
& FTD_MSG
)
1392 DPRINT("Over/Underrun - retrying\n");
1394 }else if (*errors
>= DP
->max_errors
.reporting
){
1396 if (ST0
& ST0_ECE
) {
1397 printk("Recalibrate failed!");
1398 } else if (ST2
& ST2_CRC
) {
1399 printk("data CRC error");
1401 } else if (ST1
& ST1_CRC
) {
1402 printk("CRC error");
1404 } else if ((ST1
& (ST1_MAM
|ST1_ND
)) || (ST2
& ST2_MAM
)) {
1406 printk("sector not found");
1409 printk("probe failed...");
1410 } else if (ST2
& ST2_WC
) { /* seek error */
1411 printk("wrong cylinder");
1412 } else if (ST2
& ST2_BC
) { /* cylinder marked as bad */
1413 printk("bad cylinder");
1415 printk("unknown error. ST[0..2] are: 0x%x 0x%x 0x%x", ST0
, ST1
, ST2
);
1421 if (ST2
& ST2_WC
|| ST2
& ST2_BC
)
1422 /* wrong cylinder => recal */
1423 DRS
->track
= NEED_2_RECAL
;
1425 case 0x80: /* invalid command given */
1426 DPRINT("Invalid FDC command given!\n");
1430 DPRINT("Abnormal termination caused by polling\n");
1433 default: /* (0) Normal command termination */
1439 * This routine is called when everything should be correctly set up
1440 * for the transfer (i.e. floppy motor is on, the correct floppy is
1441 * selected, and the head is sitting on the right track).
1443 static void setup_rw_floppy(void)
1445 int i
,ready_date
,r
, flags
,dflags
;
1446 timeout_fn function
;
1448 flags
= raw_cmd
->flags
;
1449 if (flags
& (FD_RAW_READ
| FD_RAW_WRITE
))
1450 flags
|= FD_RAW_INTR
;
1452 if ((flags
& FD_RAW_SPIN
) && !(flags
& FD_RAW_NO_MOTOR
)){
1453 ready_date
= DRS
->spinup_date
+ DP
->spinup
;
1454 /* If spinup will take a long time, rerun scandrives
1455 * again just before spinup completion. Beware that
1456 * after scandrives, we must again wait for selection.
1458 if ((signed) (ready_date
- jiffies
) > DP
->select_delay
){
1459 ready_date
-= DP
->select_delay
;
1460 function
= (timeout_fn
) floppy_start
;
1462 function
= (timeout_fn
) setup_rw_floppy
;
1464 /* wait until the floppy is spinning fast enough */
1465 if (wait_for_completion(ready_date
,function
))
1468 dflags
= DRS
->flags
;
1470 if ((flags
& FD_RAW_READ
) || (flags
& FD_RAW_WRITE
))
1473 if (flags
& FD_RAW_INTR
)
1474 SET_INTR(main_command_interrupt
);
1477 for (i
=0; i
< raw_cmd
->cmd_count
; i
++)
1478 r
|=output_byte(raw_cmd
->cmd
[i
]);
1481 debugt("rw_command: ");
1489 if (!(flags
& FD_RAW_INTR
)){
1492 } else if (flags
& FD_RAW_NEED_DISK
)
1496 static int blind_seek
;
1499 * This is the routine called after every seek (or recalibrate) interrupt
1500 * from the floppy controller.
1502 static void seek_interrupt(void)
1505 debugt("seek interrupt:");
1507 if (inr
!= 2 || (ST0
& 0xF8) != 0x20) {
1508 DPRINT("seek failed\n");
1509 DRS
->track
= NEED_2_RECAL
;
1514 if (DRS
->track
>= 0 && DRS
->track
!= ST1
&& !blind_seek
){
1516 if (DP
->flags
& FD_DEBUG
){
1517 DPRINT("clearing NEWCHANGE flag because of effective seek\n");
1518 DPRINT("jiffies=%ld\n", jiffies
);
1521 CLEARF(FD_DISK_NEWCHANGE
); /* effective seek */
1522 DRS
->select_date
= jiffies
;
1528 static void check_wp(void)
1530 if (TESTF(FD_VERIFY
)) {
1531 /* check write protection */
1532 output_byte(FD_GETSTATUS
);
1533 output_byte(UNIT(current_drive
));
1539 CLEARF(FD_NEED_TWADDLE
);
1541 if (DP
->flags
& FD_DEBUG
){
1542 DPRINT("checking whether disk is write protected\n");
1543 DPRINT("wp=%x\n",ST3
& 0x40);
1547 SETF(FD_DISK_WRITABLE
);
1549 CLEARF(FD_DISK_WRITABLE
);
1553 static void seek_floppy(void)
1560 if (DP
->flags
& FD_DEBUG
){
1561 DPRINT("calling disk change from seek\n");
1565 if (!TESTF(FD_DISK_NEWCHANGE
) &&
1566 disk_change(current_drive
) &&
1567 (raw_cmd
->flags
& FD_RAW_NEED_DISK
)){
1568 /* the media changed flag should be cleared after the seek.
1569 * If it isn't, this means that there is really no disk in
1572 SETF(FD_DISK_CHANGED
);
1577 if (DRS
->track
<= NEED_1_RECAL
){
1578 recalibrate_floppy();
1580 } else if (TESTF(FD_DISK_NEWCHANGE
) &&
1581 (raw_cmd
->flags
& FD_RAW_NEED_DISK
) &&
1582 (DRS
->track
<= NO_TRACK
|| DRS
->track
== raw_cmd
->track
)) {
1583 /* we seek to clear the media-changed condition. Does anybody
1584 * know a more elegant way, which works on all drives? */
1586 track
= raw_cmd
->track
- 1;
1588 if (DP
->flags
& FD_SILENT_DCL_CLEAR
){
1589 set_dor(fdc
, ~(0x10 << UNIT(current_drive
)), 0);
1591 raw_cmd
->flags
|= FD_RAW_NEED_SEEK
;
1597 if (raw_cmd
->track
!= DRS
->track
&&
1598 (raw_cmd
->flags
& FD_RAW_NEED_SEEK
))
1599 track
= raw_cmd
->track
;
1606 SET_INTR(seek_interrupt
);
1607 output_byte(FD_SEEK
);
1608 output_byte(UNIT(current_drive
));
1611 debugt("seek command:");
1615 static void recal_interrupt(void)
1618 debugt("recal interrupt:");
1622 else if (ST0
& ST0_ECE
) {
1626 debugt("recal interrupt need 1 recal:");
1628 /* after a second recalibrate, we still haven't
1629 * reached track 0. Probably no drive. Raise an
1630 * error, as failing immediately might upset
1631 * computers possessed by the Devil :-) */
1637 debugt("recal interrupt need 2 recal:");
1639 /* If we already did a recalibrate,
1640 * and we are not at track 0, this
1641 * means we have moved. (The only way
1642 * not to move at recalibration is to
1643 * be already at track 0.) Clear the
1644 * new change flag */
1646 if (DP
->flags
& FD_DEBUG
){
1647 DPRINT("clearing NEWCHANGE flag because of second recalibrate\n");
1651 CLEARF(FD_DISK_NEWCHANGE
);
1652 DRS
->select_date
= jiffies
;
1656 debugt("recal interrupt default:");
1658 /* Recalibrate moves the head by at
1659 * most 80 steps. If after one
1660 * recalibrate we don't have reached
1661 * track 0, this might mean that we
1662 * started beyond track 80. Try
1664 DRS
->track
= NEED_1_RECAL
;
1672 static void print_result(char *message
, int inr
)
1676 DPRINT("%s ", message
);
1678 for (i
=0; i
<inr
; i
++)
1679 printk("repl[%d]=%x ", i
, reply_buffer
[i
]);
1683 /* interrupt handler. Note that this can be called externally on the Sparc */
1684 void floppy_interrupt(int irq
, void *dev_id
, struct pt_regs
* regs
)
1686 void (*handler
)(void) = DEVICE_INTR
;
1689 lasthandler
= handler
;
1690 interruptjiffies
= jiffies
;
1693 floppy_enable_hlt();
1695 if (fdc
>= N_FDC
|| FDCS
->address
== -1){
1696 /* we don't even know which FDC is the culprit */
1697 printk("DOR0=%x\n", fdc_state
[0].dor
);
1698 printk("floppy interrupt on bizarre fdc %d\n",fdc
);
1699 printk("handler=%p\n", handler
);
1700 is_alive("bizarre fdc");
1705 /* We have to clear the reset flag here, because apparently on boxes
1706 * with level triggered interrupts (PS/2, Sparc, ...), it is needed to
1707 * emit SENSEI's to clear the interrupt line. And FDCS->reset blocks the
1708 * emission of the SENSEI's.
1709 * It is OK to emit floppy commands because we are in an interrupt
1710 * handler here, and thus we have to fear no interference of other
1714 do_print
= !handler
&& print_unex
&& !initialising
;
1718 print_result("unexpected interrupt", inr
);
1721 output_byte(FD_SENSEI
);
1724 print_result("sensei", inr
);
1725 } while ((ST0
& 0x83) != UNIT(current_drive
) && inr
== 2);
1728 int cpu
= smp_processor_id();
1729 if(softirq_trylock(cpu
)) {
1730 /* got the lock, call the handler immediately */
1732 softirq_endlock(cpu
);
1734 /* we interrupted a bottom half. Defer handler */
1735 schedule_bh( (void *)(void *) handler
);
1738 is_alive("normal interrupt end");
1741 static void recalibrate_floppy(void)
1744 debugt("recalibrate floppy:");
1746 SET_INTR(recal_interrupt
);
1747 output_byte(FD_RECALIBRATE
);
1748 LAST_OUT(UNIT(current_drive
));
1752 * Must do 4 FD_SENSEIs after reset because of ``drive polling''.
1754 static void reset_interrupt(void)
1757 debugt("reset interrupt:");
1759 result(); /* get the status ready for set_fdc */
1761 printk("reset set in interrupt, calling %p\n", cont
->error
);
1762 cont
->error(); /* a reset just after a reset. BAD! */
1768 * reset is done by pulling bit 2 of DOR low for a while (old FDCs),
1769 * or by setting the self clearing bit 7 of STATUS (newer FDCs)
1771 static void reset_fdc(void)
1773 SET_INTR(reset_interrupt
);
1777 /* Pseudo-DMA may intercept 'reset finished' interrupt. */
1778 /* Irrelevant for systems with true DMA (i386). */
1781 if (FDCS
->version
>= FDC_82072A
)
1782 fd_outb(0x80 | (FDCS
->dtr
&3), FD_STATUS
);
1784 fd_outb(FDCS
->dor
& ~0x04, FD_DOR
);
1785 udelay(FD_RESET_DELAY
);
1786 fd_outb(FDCS
->dor
, FD_DOR
);
1790 static void show_floppy(void)
1795 printk("floppy driver state\n");
1796 printk("-------------------\n");
1797 printk("now=%ld last interrupt=%d last called handler=%p\n",
1798 jiffies
, interruptjiffies
, lasthandler
);
1801 #ifdef FLOPPY_SANITY_CHECK
1802 printk("timeout_message=%s\n", timeout_message
);
1803 printk("last output bytes:\n");
1804 for (i
=0; i
< OLOGSIZE
; i
++)
1805 printk("%2x %2x %ld\n",
1806 output_log
[(i
+output_log_pos
) % OLOGSIZE
].data
,
1807 output_log
[(i
+output_log_pos
) % OLOGSIZE
].status
,
1808 output_log
[(i
+output_log_pos
) % OLOGSIZE
].jiffies
);
1809 printk("last result at %d\n", resultjiffies
);
1810 printk("last redo_fd_request at %d\n", lastredo
);
1811 for (i
=0; i
<resultsize
; i
++){
1812 printk("%2x ", reply_buffer
[i
]);
1817 printk("status=%x\n", fd_inb(FD_STATUS
));
1818 printk("fdc_busy=%d\n", fdc_busy
);
1820 printk("DEVICE_INTR=%p\n", DEVICE_INTR
);
1822 printk("floppy_tq.routine=%p\n", floppy_tq
.routine
);
1824 printk("fd_timer.function=%p\n", fd_timer
.function
);
1825 if (fd_timeout
.prev
){
1826 printk("timer_table=%p\n",fd_timeout
.function
);
1827 printk("expires=%ld\n",fd_timeout
.expires
-jiffies
);
1828 printk("now=%ld\n",jiffies
);
1830 printk("cont=%p\n", cont
);
1831 printk("CURRENT=%p\n", CURRENT
);
1832 printk("command_status=%d\n", command_status
);
1836 static void floppy_shutdown(void)
1842 floppy_enable_hlt();
1844 /* avoid dma going to a random drive after shutdown */
1847 DPRINT("floppy timeout called\n");
1851 cont
->redo(); /* this will recall reset when needed */
1853 printk("no cont in shutdown!\n");
1854 process_fd_request();
1856 is_alive("floppy shutdown");
1858 /*typedef void (*timeout_fn)(unsigned long);*/
1860 /* start motor, check media-changed condition and write protection */
1861 static int start_motor(void (*function
)(void) )
1866 data
= UNIT(current_drive
);
1867 if (!(raw_cmd
->flags
& FD_RAW_NO_MOTOR
)){
1868 if (!(FDCS
->dor
& (0x10 << UNIT(current_drive
)))){
1870 /* no read since this drive is running */
1871 DRS
->first_read_date
= 0;
1872 /* note motor start time if motor is not yet running */
1873 DRS
->spinup_date
= jiffies
;
1874 data
|= (0x10 << UNIT(current_drive
));
1877 if (FDCS
->dor
& (0x10 << UNIT(current_drive
)))
1878 mask
&= ~(0x10 << UNIT(current_drive
));
1880 /* starts motor and selects floppy */
1881 del_timer(motor_off_timer
+ current_drive
);
1882 set_dor(fdc
, mask
, data
);
1884 /* wait_for_completion also schedules reset if needed. */
1885 return(wait_for_completion(DRS
->select_date
+DP
->select_delay
,
1886 (timeout_fn
) function
));
1889 static void floppy_ready(void)
1892 if (start_motor(floppy_ready
)) return;
1893 if (fdc_dtr()) return;
1896 if (DP
->flags
& FD_DEBUG
){
1897 DPRINT("calling disk change from floppy_ready\n");
1900 if (!(raw_cmd
->flags
& FD_RAW_NO_MOTOR
) &&
1901 disk_change(current_drive
) &&
1903 twaddle(); /* this clears the dcl on certain drive/controller
1906 #ifdef fd_chose_dma_mode
1907 if ((raw_cmd
->flags
& FD_RAW_READ
) ||
1908 (raw_cmd
->flags
& FD_RAW_WRITE
))
1909 fd_chose_dma_mode(raw_cmd
->kernel_data
,
1913 if (raw_cmd
->flags
& (FD_RAW_NEED_SEEK
| FD_RAW_NEED_DISK
)){
1914 perpendicular_mode();
1915 fdc_specify(); /* must be done here because of hut, hlt ... */
1918 if ((raw_cmd
->flags
& FD_RAW_READ
) ||
1919 (raw_cmd
->flags
& FD_RAW_WRITE
))
1925 static void floppy_start(void)
1927 reschedule_timeout(CURRENTD
, "floppy start", 0);
1931 if (DP
->flags
& FD_DEBUG
){
1932 DPRINT("setting NEWCHANGE in floppy_start\n");
1935 SETF(FD_DISK_NEWCHANGE
);
1940 * ========================================================================
1941 * here ends the bottom half. Exported routines are:
1942 * floppy_start, floppy_off, floppy_ready, lock_fdc, unlock_fdc, set_fdc,
1943 * start_motor, reset_fdc, reset_fdc_info, interpret_errors.
1944 * Initialization also uses output_byte, result, set_dor, floppy_interrupt
1946 * ========================================================================
1949 * General purpose continuations.
1950 * ==============================
1953 static void do_wakeup(void)
1955 reschedule_timeout(MAXTIMEOUT
, "do wakeup", 0);
1957 command_status
+= 2;
1958 wake_up(&command_done
);
1961 static struct cont_t wakeup_cont
={
1969 static struct cont_t intr_cont
={
1976 static int wait_til_done(void (*handler
)(void), int interruptible
)
1979 unsigned long flags
;
1981 schedule_bh((void *)(void *)handler
);
1983 while(command_status
< 2 && NO_SIGNAL
){
1984 is_alive("wait_til_done");
1986 interruptible_sleep_on(&command_done
);
1988 sleep_on(&command_done
);
1990 if (command_status
< 2){
2000 command_status
= FD_COMMAND_ERROR
;
2001 if (command_status
== FD_COMMAND_OKAY
)
2005 command_status
= FD_COMMAND_NONE
;
2009 static void generic_done(int result
)
2011 command_status
= result
;
2012 cont
= &wakeup_cont
;
2015 static void generic_success(void)
2020 static void generic_failure(void)
2025 static void success_and_wakeup(void)
2033 * formatting and rw support.
2034 * ==========================
2037 static int next_valid_format(void)
2041 probed_format
= DRS
->probed_format
;
2043 if (probed_format
>= 8 ||
2044 !DP
->autodetect
[probed_format
]){
2045 DRS
->probed_format
= 0;
2048 if (floppy_type
[DP
->autodetect
[probed_format
]].sect
){
2049 DRS
->probed_format
= probed_format
;
2056 static void bad_flp_intr(void)
2059 DRS
->probed_format
++;
2060 if (!next_valid_format())
2064 INFBOUND(DRWE
->badness
, *errors
);
2065 if (*errors
> DP
->max_errors
.abort
)
2067 if (*errors
> DP
->max_errors
.reset
)
2069 else if (*errors
> DP
->max_errors
.recal
)
2070 DRS
->track
= NEED_2_RECAL
;
2073 static void set_floppy(kdev_t device
)
2076 _floppy
= TYPE(device
) + floppy_type
;
2078 _floppy
= current_type
[ DRIVE(device
) ];
2082 * formatting support.
2083 * ===================
2085 static void format_interrupt(void)
2087 switch (interpret_errors()){
2098 #define CODE2SIZE (ssize = ((1 << SIZECODE) + 3) >> 2)
2099 #define FM_MODE(x,y) ((y) & ~(((x)->rate & 0x80) >>1))
2100 #define CT(x) ((x) | 0xc0)
2101 static void setup_format_params(int track
)
2104 unsigned char track
,head
,sect
,size
;
2105 } *here
= (struct fparm
*)floppy_track_buffer
;
2107 int count
,head_shift
,track_shift
;
2109 raw_cmd
= &default_raw_cmd
;
2110 raw_cmd
->track
= track
;
2112 raw_cmd
->flags
= FD_RAW_WRITE
| FD_RAW_INTR
| FD_RAW_SPIN
|
2113 FD_RAW_NEED_DISK
| FD_RAW_NEED_SEEK
;
2114 raw_cmd
->rate
= _floppy
->rate
& 0x43;
2115 raw_cmd
->cmd_count
= NR_F
;
2116 COMMAND
= FM_MODE(_floppy
,FD_FORMAT
);
2117 DR_SELECT
= UNIT(current_drive
) + PH_HEAD(_floppy
,format_req
.head
);
2118 F_SIZECODE
= FD_SIZECODE(_floppy
);
2119 F_SECT_PER_TRACK
= _floppy
->sect
<< 2 >> F_SIZECODE
;
2120 F_GAP
= _floppy
->fmt_gap
;
2121 F_FILL
= FD_FILL_BYTE
;
2123 raw_cmd
->kernel_data
= floppy_track_buffer
;
2124 raw_cmd
->length
= 4 * F_SECT_PER_TRACK
;
2126 /* allow for about 30ms for data transport per track */
2127 head_shift
= (F_SECT_PER_TRACK
+ 5) / 6;
2129 /* a ``cylinder'' is two tracks plus a little stepping time */
2130 track_shift
= 2 * head_shift
+ 3;
2132 /* position of logical sector 1 on this track */
2133 n
= (track_shift
* format_req
.track
+ head_shift
* format_req
.head
)
2136 /* determine interleave */
2138 if (_floppy
->fmt_gap
< 0x22)
2141 /* initialize field */
2142 for (count
= 0; count
< F_SECT_PER_TRACK
; ++count
) {
2143 here
[count
].track
= format_req
.track
;
2144 here
[count
].head
= format_req
.head
;
2145 here
[count
].sect
= 0;
2146 here
[count
].size
= F_SIZECODE
;
2148 /* place logical sectors */
2149 for (count
= 1; count
<= F_SECT_PER_TRACK
; ++count
) {
2150 here
[n
].sect
= count
;
2151 n
= (n
+il
) % F_SECT_PER_TRACK
;
2152 if (here
[n
].sect
) { /* sector busy, find next free sector */
2154 if (n
>= F_SECT_PER_TRACK
) {
2155 n
-=F_SECT_PER_TRACK
;
2156 while (here
[n
].sect
) ++n
;
2162 static void redo_format(void)
2165 setup_format_params(format_req
.track
<< STRETCH(_floppy
));
2168 debugt("queue format request");
2172 static struct cont_t format_cont
={
2178 static int do_format(kdev_t device
, struct format_descr
*tmp_format_req
)
2181 int drive
=DRIVE(device
);
2186 _floppy
->track
> DP
->tracks
||
2187 tmp_format_req
->track
>= _floppy
->track
||
2188 tmp_format_req
->head
>= _floppy
->head
||
2189 (_floppy
->sect
<< 2) % (1 << FD_SIZECODE(_floppy
)) ||
2190 !_floppy
->fmt_gap
) {
2191 process_fd_request();
2194 format_req
= *tmp_format_req
;
2196 cont
= &format_cont
;
2197 errors
= &format_errors
;
2199 process_fd_request();
2204 * Buffer read/write and support
2205 * =============================
2208 /* new request_done. Can handle physical sectors which are smaller than a
2210 static void request_done(int uptodate
)
2215 reschedule_timeout(MAXTIMEOUT
, "request done %d", uptodate
);
2218 DPRINT("request list destroyed in floppy request done\n");
2223 /* maintain values for invalidation on geometry
2225 block
= current_count_sectors
+ CURRENT
->sector
;
2226 INFBOUND(DRS
->maxblock
, block
);
2227 if (block
> _floppy
->sect
)
2230 /* unlock chained buffers */
2231 while (current_count_sectors
&& CURRENT
&&
2232 current_count_sectors
>= CURRENT
->current_nr_sectors
){
2233 current_count_sectors
-= CURRENT
->current_nr_sectors
;
2234 CURRENT
->nr_sectors
-= CURRENT
->current_nr_sectors
;
2235 CURRENT
->sector
+= CURRENT
->current_nr_sectors
;
2238 if (current_count_sectors
&& CURRENT
){
2239 /* "unlock" last subsector */
2240 CURRENT
->buffer
+= current_count_sectors
<<9;
2241 CURRENT
->current_nr_sectors
-= current_count_sectors
;
2242 CURRENT
->nr_sectors
-= current_count_sectors
;
2243 CURRENT
->sector
+= current_count_sectors
;
2247 if (current_count_sectors
&& !CURRENT
)
2248 DPRINT("request list destroyed in floppy request done\n");
2251 if (CURRENT
->cmd
== WRITE
) {
2252 /* record write error information */
2253 DRWE
->write_errors
++;
2254 if (DRWE
->write_errors
== 1) {
2255 DRWE
->first_error_sector
= CURRENT
->sector
;
2256 DRWE
->first_error_generation
= DRS
->generation
;
2258 DRWE
->last_error_sector
= CURRENT
->sector
;
2259 DRWE
->last_error_generation
= DRS
->generation
;
2265 /* Interrupt handler evaluating the result of the r/w operation */
2266 static void rw_interrupt(void)
2268 int nr_sectors
, ssize
, eoc
, heads
;
2270 if (!DRS
->first_read_date
)
2271 DRS
->first_read_date
= jiffies
;
2286 nr_sectors
= (((R_TRACK
-TRACK
) * heads
+
2287 R_HEAD
-HEAD
) * SECT_PER_TRACK
+
2288 R_SECTOR
-SECTOR
+ eoc
) << SIZECODE
>> 2;
2290 #ifdef FLOPPY_SANITY_CHECK
2291 if (nr_sectors
/ ssize
>
2292 (in_sector_offset
+ current_count_sectors
+ ssize
- 1)/ssize
) {
2293 DPRINT("long rw: %x instead of %lx\n",
2294 nr_sectors
, current_count_sectors
);
2295 printk("rs=%d s=%d\n", R_SECTOR
, SECTOR
);
2296 printk("rh=%d h=%d\n", R_HEAD
, HEAD
);
2297 printk("rt=%d t=%d\n", R_TRACK
, TRACK
);
2298 printk("heads=%d eoc=%d\n", heads
, eoc
);
2299 printk("spt=%d st=%d ss=%d\n", SECT_PER_TRACK
,
2301 printk("in_sector_offset=%d\n", in_sector_offset
);
2305 nr_sectors
-= in_sector_offset
;
2306 INFBOUND(nr_sectors
,0);
2307 SUPBOUND(current_count_sectors
, nr_sectors
);
2309 switch (interpret_errors()){
2314 if (!current_count_sectors
){
2321 if (!current_count_sectors
){
2325 current_type
[current_drive
] = _floppy
;
2326 floppy_sizes
[TOMINOR(current_drive
) ]=
2327 (_floppy
->size
+1)>>1;
2332 if (DP
->flags
& FTD_MSG
)
2333 DPRINT("Auto-detected floppy type %s in fd%d\n",
2334 _floppy
->name
,current_drive
);
2335 current_type
[current_drive
] = _floppy
;
2336 floppy_sizes
[TOMINOR(current_drive
)] = (_floppy
->size
+1) >> 1;
2340 if (CT(COMMAND
) != FD_READ
||
2341 raw_cmd
->kernel_data
== CURRENT
->buffer
){
2342 /* transfer directly from buffer */
2344 } else if (CT(COMMAND
) == FD_READ
){
2345 buffer_track
= raw_cmd
->track
;
2346 buffer_drive
= current_drive
;
2347 INFBOUND(buffer_max
, nr_sectors
+ sector_t
);
2352 /* Compute maximal contiguous buffer size. */
2353 static int buffer_chain_size(void)
2355 struct buffer_head
*bh
;
2359 base
= CURRENT
->buffer
;
2360 size
= CURRENT
->current_nr_sectors
<< 9;
2365 while (bh
&& bh
->b_data
== base
+ size
){
2373 /* Compute the maximal transfer size */
2374 static int transfer_size(int ssize
, int max_sector
, int max_size
)
2376 SUPBOUND(max_sector
, sector_t
+ max_size
);
2379 max_sector
-= (max_sector
% _floppy
->sect
) % ssize
;
2381 /* transfer size, beginning not aligned */
2382 current_count_sectors
= max_sector
- sector_t
;
2388 * Move data from/to the track buffer to/from the buffer cache.
2390 static void copy_buffer(int ssize
, int max_sector
, int max_sector_2
)
2392 int remaining
; /* number of transferred 512-byte sectors */
2393 struct buffer_head
*bh
;
2394 char *buffer
, *dma_buffer
;
2397 max_sector
= transfer_size(ssize
,
2398 minimum(max_sector
, max_sector_2
),
2399 CURRENT
->nr_sectors
);
2401 if (current_count_sectors
<= 0 && CT(COMMAND
) == FD_WRITE
&&
2402 buffer_max
> sector_t
+ CURRENT
->nr_sectors
)
2403 current_count_sectors
= minimum(buffer_max
- sector_t
,
2404 CURRENT
->nr_sectors
);
2406 remaining
= current_count_sectors
<< 9;
2407 #ifdef FLOPPY_SANITY_CHECK
2408 if ((remaining
>> 9) > CURRENT
->nr_sectors
&&
2409 CT(COMMAND
) == FD_WRITE
){
2410 DPRINT("in copy buffer\n");
2411 printk("current_count_sectors=%ld\n", current_count_sectors
);
2412 printk("remaining=%d\n", remaining
>> 9);
2413 printk("CURRENT->nr_sectors=%ld\n",CURRENT
->nr_sectors
);
2414 printk("CURRENT->current_nr_sectors=%ld\n",
2415 CURRENT
->current_nr_sectors
);
2416 printk("max_sector=%d\n", max_sector
);
2417 printk("ssize=%d\n", ssize
);
2421 buffer_max
= maximum(max_sector
, buffer_max
);
2423 dma_buffer
= floppy_track_buffer
+ ((sector_t
- buffer_min
) << 9);
2426 size
= CURRENT
->current_nr_sectors
<< 9;
2427 buffer
= CURRENT
->buffer
;
2429 while (remaining
> 0){
2430 SUPBOUND(size
, remaining
);
2431 #ifdef FLOPPY_SANITY_CHECK
2432 if (dma_buffer
+ size
>
2433 floppy_track_buffer
+ (max_buffer_sectors
<< 10) ||
2434 dma_buffer
< floppy_track_buffer
){
2435 DPRINT("buffer overrun in copy buffer %d\n",
2436 (int) ((floppy_track_buffer
- dma_buffer
) >>9));
2437 printk("sector_t=%d buffer_min=%d\n",
2438 sector_t
, buffer_min
);
2439 printk("current_count_sectors=%ld\n",
2440 current_count_sectors
);
2441 if (CT(COMMAND
) == FD_READ
)
2443 if (CT(COMMAND
) == FD_READ
)
2447 if (((unsigned long)buffer
) % 512)
2448 DPRINT("%p buffer not aligned\n", buffer
);
2450 if (CT(COMMAND
) == FD_READ
)
2451 memcpy(buffer
, dma_buffer
, size
);
2453 memcpy(dma_buffer
, buffer
, size
);
2460 #ifdef FLOPPY_SANITY_CHECK
2462 DPRINT("bh=null in copy buffer after copy\n");
2467 buffer
= bh
->b_data
;
2469 #ifdef FLOPPY_SANITY_CHECK
2472 max_sector
-= remaining
>> 9;
2473 DPRINT("weirdness: remaining %d\n", remaining
>>9);
2479 static inline int check_dma_crossing(char *start
,
2480 unsigned long length
, char *message
)
2482 if (CROSS_64KB(start
, length
)) {
2483 printk("DMA xfer crosses 64KB boundary in %s %p-%p\n",
2484 message
, start
, start
+length
);
2491 /* work around a bug in pseudo DMA
2492 * (on some FDCs) pseudo DMA does not stop when the CPU stops
2493 * sending data. Hence we need a different way to signal the
2494 * transfer length: We use SECT_PER_TRACK. Unfortunately, this
2495 * does not work with MT, hence we can only transfer one head at
2498 static void virtualdmabug_workaround(void) {
2499 int hard_sectors
, end_sector
;
2500 if(CT(COMMAND
) == FD_WRITE
) {
2501 COMMAND
&= ~0x80; /* switch off multiple track mode */
2503 hard_sectors
= raw_cmd
->length
>> (7 + SIZECODE
);
2504 end_sector
= SECTOR
+ hard_sectors
- 1;
2505 #ifdef FLOPPY_SANITY_CHECK
2506 if(end_sector
> SECT_PER_TRACK
) {
2507 printk("too many sectors %d > %d\n",
2508 end_sector
, SECT_PER_TRACK
);
2512 SECT_PER_TRACK
= end_sector
; /* make sure SECT_PER_TRACK points
2513 * to end of transfer */
2518 * Formulate a read/write request.
2519 * this routine decides where to load the data (directly to buffer, or to
2520 * tmp floppy area), how much data to load (the size of the buffer, the whole
2521 * track, or a single sector)
2522 * All floppy_track_buffer handling goes in here. If we ever add track buffer
2523 * allocation on the fly, it should be done here. No other part should need
2527 static int make_raw_rw_request(void)
2529 int aligned_sector_t
;
2530 int max_sector
, max_size
, tracksize
, ssize
;
2532 set_fdc(DRIVE(CURRENT
->rq_dev
));
2534 raw_cmd
= &default_raw_cmd
;
2535 raw_cmd
->flags
= FD_RAW_SPIN
| FD_RAW_NEED_DISK
| FD_RAW_NEED_DISK
|
2537 raw_cmd
->cmd_count
= NR_RW
;
2538 if (CURRENT
->cmd
== READ
){
2539 raw_cmd
->flags
|= FD_RAW_READ
;
2540 COMMAND
= FM_MODE(_floppy
,FD_READ
);
2541 } else if (CURRENT
->cmd
== WRITE
){
2542 raw_cmd
->flags
|= FD_RAW_WRITE
;
2543 COMMAND
= FM_MODE(_floppy
,FD_WRITE
);
2545 DPRINT("make_raw_rw_request: unknown command\n");
2549 max_sector
= _floppy
->sect
* _floppy
->head
;
2551 TRACK
= CURRENT
->sector
/ max_sector
;
2552 sector_t
= CURRENT
->sector
% max_sector
;
2553 if (_floppy
->track
&& TRACK
>= _floppy
->track
) {
2554 if(CURRENT
->current_nr_sectors
& 1) {
2555 current_count_sectors
= 1;
2560 HEAD
= sector_t
/ _floppy
->sect
;
2562 if (((_floppy
->stretch
& FD_SWAPSIDES
) || TESTF(FD_NEED_TWADDLE
)) &&
2563 sector_t
< _floppy
->sect
)
2564 max_sector
= _floppy
->sect
;
2566 /* 2M disks have phantom sectors on the first track */
2567 if ((_floppy
->rate
& FD_2M
) && (!TRACK
) && (!HEAD
)){
2568 max_sector
= 2 * _floppy
->sect
/ 3;
2569 if (sector_t
>= max_sector
){
2570 current_count_sectors
= minimum(_floppy
->sect
- sector_t
,
2571 CURRENT
->nr_sectors
);
2576 SIZECODE
= FD_SIZECODE(_floppy
);
2577 raw_cmd
->rate
= _floppy
->rate
& 0x43;
2578 if ((_floppy
->rate
& FD_2M
) &&
2587 raw_cmd
->track
= TRACK
<< STRETCH(_floppy
);
2588 DR_SELECT
= UNIT(current_drive
) + PH_HEAD(_floppy
,HEAD
);
2591 SECT_PER_TRACK
= _floppy
->sect
<< 2 >> SIZECODE
;
2592 SECTOR
= ((sector_t
% _floppy
->sect
) << 2 >> SIZECODE
) + 1;
2594 /* tracksize describes the size which can be filled up with sectors
2597 tracksize
= _floppy
->sect
- _floppy
->sect
% ssize
;
2598 if (tracksize
< _floppy
->sect
){
2600 if (tracksize
<= sector_t
% _floppy
->sect
)
2603 /* if we are beyond tracksize, fill up using smaller sectors */
2604 while (tracksize
<= sector_t
% _floppy
->sect
){
2605 while(tracksize
+ ssize
> _floppy
->sect
){
2609 SECTOR
++; SECT_PER_TRACK
++;
2612 max_sector
= HEAD
* _floppy
->sect
+ tracksize
;
2613 } else if (!TRACK
&& !HEAD
&& !(_floppy
->rate
& FD_2M
) && probing
) {
2614 max_sector
= _floppy
->sect
;
2615 } else if (!HEAD
&& CT(COMMAND
) == FD_WRITE
) {
2616 /* for virtual DMA bug workaround */
2617 max_sector
= _floppy
->sect
;
2620 in_sector_offset
= (sector_t
% _floppy
->sect
) % ssize
;
2621 aligned_sector_t
= sector_t
- in_sector_offset
;
2622 max_size
= CURRENT
->nr_sectors
;
2623 if ((raw_cmd
->track
== buffer_track
) &&
2624 (current_drive
== buffer_drive
) &&
2625 (sector_t
>= buffer_min
) && (sector_t
< buffer_max
)) {
2626 /* data already in track buffer */
2627 if (CT(COMMAND
) == FD_READ
) {
2628 copy_buffer(1, max_sector
, buffer_max
);
2631 } else if (in_sector_offset
|| CURRENT
->nr_sectors
< ssize
){
2632 if (CT(COMMAND
) == FD_WRITE
){
2633 if (sector_t
+ CURRENT
->nr_sectors
> ssize
&&
2634 sector_t
+ CURRENT
->nr_sectors
< ssize
+ ssize
)
2635 max_size
= ssize
+ ssize
;
2639 raw_cmd
->flags
&= ~FD_RAW_WRITE
;
2640 raw_cmd
->flags
|= FD_RAW_READ
;
2641 COMMAND
= FM_MODE(_floppy
,FD_READ
);
2642 } else if ((unsigned long)CURRENT
->buffer
< MAX_DMA_ADDRESS
) {
2643 unsigned long dma_limit
;
2644 int direct
, indirect
;
2646 indirect
= transfer_size(ssize
,max_sector
,max_buffer_sectors
*2) -
2650 * Do NOT use minimum() here---MAX_DMA_ADDRESS is 64 bits wide
2651 * on a 64 bit machine!
2653 max_size
= buffer_chain_size();
2654 dma_limit
= (MAX_DMA_ADDRESS
- ((unsigned long) CURRENT
->buffer
)) >> 9;
2655 if ((unsigned long) max_size
> dma_limit
) {
2656 max_size
= dma_limit
;
2658 /* 64 kb boundaries */
2659 if (CROSS_64KB(CURRENT
->buffer
, max_size
<< 9))
2661 ((unsigned long)CURRENT
->buffer
) % K_64
)>>9;
2662 direct
= transfer_size(ssize
,max_sector
,max_size
) - sector_t
;
2664 * We try to read tracks, but if we get too many errors, we
2665 * go back to reading just one sector at a time.
2667 * This means we should be able to read a sector even if there
2668 * are other bad sectors on this track.
2671 (indirect
* 2 > direct
* 3 &&
2672 *errors
< DP
->max_errors
.read_track
&&
2673 /*!TESTF(FD_NEED_TWADDLE) &&*/
2674 ((!probing
|| (DP
->read_track
&(1<<DRS
->probed_format
)))))){
2675 max_size
= CURRENT
->nr_sectors
;
2677 raw_cmd
->kernel_data
= CURRENT
->buffer
;
2678 raw_cmd
->length
= current_count_sectors
<< 9;
2679 if (raw_cmd
->length
== 0){
2680 DPRINT("zero dma transfer attempted from make_raw_request\n");
2681 DPRINT("indirect=%d direct=%d sector_t=%d",
2682 indirect
, direct
, sector_t
);
2685 /* check_dma_crossing(raw_cmd->kernel_data,
2687 "end of make_raw_request [1]");*/
2689 virtualdmabug_workaround();
2694 if (CT(COMMAND
) == FD_READ
)
2695 max_size
= max_sector
; /* unbounded */
2697 /* claim buffer track if needed */
2698 if (buffer_track
!= raw_cmd
->track
|| /* bad track */
2699 buffer_drive
!=current_drive
|| /* bad drive */
2700 sector_t
> buffer_max
||
2701 sector_t
< buffer_min
||
2702 ((CT(COMMAND
) == FD_READ
||
2703 (!in_sector_offset
&& CURRENT
->nr_sectors
>= ssize
))&&
2704 max_sector
> 2 * max_buffer_sectors
+ buffer_min
&&
2705 max_size
+ sector_t
> 2 * max_buffer_sectors
+ buffer_min
)
2706 /* not enough space */){
2708 buffer_drive
= current_drive
;
2709 buffer_max
= buffer_min
= aligned_sector_t
;
2711 raw_cmd
->kernel_data
= floppy_track_buffer
+
2712 ((aligned_sector_t
-buffer_min
)<<9);
2714 if (CT(COMMAND
) == FD_WRITE
){
2715 /* copy write buffer to track buffer.
2716 * if we get here, we know that the write
2717 * is either aligned or the data already in the buffer
2718 * (buffer will be overwritten) */
2719 #ifdef FLOPPY_SANITY_CHECK
2720 if (in_sector_offset
&& buffer_track
== -1)
2721 DPRINT("internal error offset !=0 on write\n");
2723 buffer_track
= raw_cmd
->track
;
2724 buffer_drive
= current_drive
;
2725 copy_buffer(ssize
, max_sector
, 2*max_buffer_sectors
+buffer_min
);
2727 transfer_size(ssize
, max_sector
,
2728 2*max_buffer_sectors
+buffer_min
-aligned_sector_t
);
2730 /* round up current_count_sectors to get dma xfer size */
2731 raw_cmd
->length
= in_sector_offset
+current_count_sectors
;
2732 raw_cmd
->length
= ((raw_cmd
->length
-1)|(ssize
-1))+1;
2733 raw_cmd
->length
<<= 9;
2734 #ifdef FLOPPY_SANITY_CHECK
2735 /*check_dma_crossing(raw_cmd->kernel_data, raw_cmd->length,
2736 "end of make_raw_request");*/
2737 if ((raw_cmd
->length
< current_count_sectors
<< 9) ||
2738 (raw_cmd
->kernel_data
!= CURRENT
->buffer
&&
2739 CT(COMMAND
) == FD_WRITE
&&
2740 (aligned_sector_t
+ (raw_cmd
->length
>> 9) > buffer_max
||
2741 aligned_sector_t
< buffer_min
)) ||
2742 raw_cmd
->length
% (128 << SIZECODE
) ||
2743 raw_cmd
->length
<= 0 || current_count_sectors
<= 0){
2744 DPRINT("fractionary current count b=%lx s=%lx\n",
2745 raw_cmd
->length
, current_count_sectors
);
2746 if (raw_cmd
->kernel_data
!= CURRENT
->buffer
)
2747 printk("addr=%d, length=%ld\n",
2748 (int) ((raw_cmd
->kernel_data
-
2749 floppy_track_buffer
) >> 9),
2750 current_count_sectors
);
2751 printk("st=%d ast=%d mse=%d msi=%d\n",
2752 sector_t
, aligned_sector_t
, max_sector
, max_size
);
2753 printk("ssize=%x SIZECODE=%d\n", ssize
, SIZECODE
);
2754 printk("command=%x SECTOR=%d HEAD=%d, TRACK=%d\n",
2755 COMMAND
, SECTOR
, HEAD
, TRACK
);
2756 printk("buffer drive=%d\n", buffer_drive
);
2757 printk("buffer track=%d\n", buffer_track
);
2758 printk("buffer_min=%d\n", buffer_min
);
2759 printk("buffer_max=%d\n", buffer_max
);
2763 if (raw_cmd
->kernel_data
!= CURRENT
->buffer
){
2764 if (raw_cmd
->kernel_data
< floppy_track_buffer
||
2765 current_count_sectors
< 0 ||
2766 raw_cmd
->length
< 0 ||
2767 raw_cmd
->kernel_data
+ raw_cmd
->length
>
2768 floppy_track_buffer
+ (max_buffer_sectors
<< 10)){
2769 DPRINT("buffer overrun in schedule dma\n");
2770 printk("sector_t=%d buffer_min=%d current_count=%ld\n",
2771 sector_t
, buffer_min
,
2772 raw_cmd
->length
>> 9);
2773 printk("current_count_sectors=%ld\n",
2774 current_count_sectors
);
2775 if (CT(COMMAND
) == FD_READ
)
2777 if (CT(COMMAND
) == FD_READ
)
2781 } else if (raw_cmd
->length
> CURRENT
->nr_sectors
<< 9 ||
2782 current_count_sectors
> CURRENT
->nr_sectors
){
2783 DPRINT("buffer overrun in direct transfer\n");
2785 } else if (raw_cmd
->length
< current_count_sectors
<< 9){
2786 DPRINT("more sectors than bytes\n");
2787 printk("bytes=%ld\n", raw_cmd
->length
>> 9);
2788 printk("sectors=%ld\n", current_count_sectors
);
2790 if (raw_cmd
->length
== 0){
2791 DPRINT("zero dma transfer attempted from make_raw_request\n");
2796 virtualdmabug_workaround();
2800 static void redo_fd_request(void)
2802 #define REPEAT {request_done(0); continue; }
2807 if (current_drive
< N_DRIVE
)
2808 floppy_off(current_drive
);
2810 if (CURRENT
&& CURRENT
->rq_status
== RQ_INACTIVE
){
2822 if (MAJOR(CURRENT
->rq_dev
) != MAJOR_NR
)
2823 panic(DEVICE_NAME
": request list destroyed");
2824 if (CURRENT
->bh
&& !buffer_locked(CURRENT
->bh
))
2825 panic(DEVICE_NAME
": block not locked");
2827 device
= CURRENT
->rq_dev
;
2828 set_fdc(DRIVE(device
));
2829 reschedule_timeout(CURRENTD
, "redo fd request", 0);
2832 raw_cmd
= & default_raw_cmd
;
2834 if (start_motor(redo_fd_request
)) return;
2835 disk_change(current_drive
);
2836 if (test_bit(current_drive
, &fake_change
) ||
2837 TESTF(FD_DISK_CHANGED
)){
2838 DPRINT("disk absent or changed during operation\n");
2841 if (!_floppy
) { /* Autodetection */
2843 DRS
->probed_format
= 0;
2844 if (next_valid_format()){
2845 DPRINT("no autodetectable formats\n");
2851 _floppy
= floppy_type
+DP
->autodetect
[DRS
->probed_format
];
2854 errors
= & (CURRENT
->errors
);
2855 tmp
= make_raw_rw_request();
2861 if (TESTF(FD_NEED_TWADDLE
))
2863 schedule_bh( (void *)(void *) floppy_start
);
2865 debugt("queue fd request");
2872 static struct cont_t rw_cont
={
2878 static void process_fd_request(void)
2881 schedule_bh( (void *)(void *) redo_fd_request
);
2884 static void do_fd_request(void)
2886 if(usage_count
== 0) {
2887 printk("warning: usage count=0, CURRENT=%p exiting\n", CURRENT
);
2888 printk("sect=%ld cmd=%d\n", CURRENT
->sector
, CURRENT
->cmd
);
2892 /* fdc busy, this new request will be treated when the
2893 current one is done */
2894 is_alive("do fd request, old request running");
2897 lock_fdc(MAXTIMEOUT
,0);
2898 process_fd_request();
2899 is_alive("do fd request");
2902 static struct cont_t poll_cont
={
2908 static int poll_drive(int interruptible
, int flag
)
2911 /* no auto-sense, just clear dcl */
2912 raw_cmd
= &default_raw_cmd
;
2913 raw_cmd
->flags
= flag
;
2915 raw_cmd
->cmd_count
=0;
2918 if (DP
->flags
& FD_DEBUG
){
2919 DPRINT("setting NEWCHANGE in poll_drive\n");
2922 SETF(FD_DISK_NEWCHANGE
);
2928 * User triggered reset
2929 * ====================
2932 static void reset_intr(void)
2934 printk("weird, reset interrupt called\n");
2937 static struct cont_t reset_cont
={
2943 static int user_reset_fdc(int drive
, int arg
, int interruptible
)
2948 LOCK_FDC(drive
,interruptible
);
2949 if (arg
== FD_RESET_ALWAYS
)
2955 process_fd_request();
2960 * Misc Ioctl's and support
2961 * ========================
2963 static inline int fd_copyout(void *param
, const void *address
, int size
)
2965 return copy_to_user(param
,address
, size
) ? -EFAULT
: 0;
2968 static inline int fd_copyin(void *param
, void *address
, int size
)
2970 return copy_from_user(address
, param
, size
) ? -EFAULT
: 0;
2973 #define _COPYOUT(x) (copy_to_user((void *)param, &(x), sizeof(x)) ? -EFAULT : 0)
2974 #define _COPYIN(x) (copy_from_user(&(x), (void *)param, sizeof(x)) ? -EFAULT : 0)
2976 #define COPYOUT(x) ECALL(_COPYOUT(x))
2977 #define COPYIN(x) ECALL(_COPYIN(x))
2979 static inline const char *drive_name(int type
, int drive
)
2981 struct floppy_struct
*floppy
;
2984 floppy
= floppy_type
+ type
;
2986 if (UDP
->native_format
)
2987 floppy
= floppy_type
+ UDP
->native_format
;
2992 return floppy
->name
;
2999 static void raw_cmd_done(int flag
)
3004 raw_cmd
->flags
|= FD_RAW_FAILURE
;
3005 raw_cmd
->flags
|= FD_RAW_HARDFAILURE
;
3007 raw_cmd
->reply_count
= inr
;
3008 if(raw_cmd
->reply_count
> MAX_REPLIES
)
3009 raw_cmd
->reply_count
=0;
3010 for (i
=0; i
< raw_cmd
->reply_count
; i
++)
3011 raw_cmd
->reply
[i
] = reply_buffer
[i
];
3013 if (raw_cmd
->flags
& (FD_RAW_READ
| FD_RAW_WRITE
))
3014 raw_cmd
->length
= fd_get_dma_residue();
3016 if ((raw_cmd
->flags
& FD_RAW_SOFTFAILURE
) &&
3017 (!raw_cmd
->reply_count
|| (raw_cmd
->reply
[0] & 0xc0)))
3018 raw_cmd
->flags
|= FD_RAW_FAILURE
;
3020 if (disk_change(current_drive
))
3021 raw_cmd
->flags
|= FD_RAW_DISK_CHANGE
;
3023 raw_cmd
->flags
&= ~FD_RAW_DISK_CHANGE
;
3024 if (raw_cmd
->flags
& FD_RAW_NO_MOTOR_AFTER
)
3025 motor_off_callback(current_drive
);
3027 if (raw_cmd
->next
&&
3028 (!(raw_cmd
->flags
& FD_RAW_FAILURE
) ||
3029 !(raw_cmd
->flags
& FD_RAW_STOP_IF_FAILURE
)) &&
3030 ((raw_cmd
->flags
& FD_RAW_FAILURE
) ||
3031 !(raw_cmd
->flags
&FD_RAW_STOP_IF_SUCCESS
))) {
3032 raw_cmd
= raw_cmd
->next
;
3040 static struct cont_t raw_cmd_cont
={
3047 static inline int raw_cmd_copyout(int cmd
, char *param
,
3048 struct floppy_raw_cmd
*ptr
)
3054 param
+= sizeof(struct floppy_raw_cmd
);
3055 if ((ptr
->flags
& FD_RAW_READ
) && ptr
->buffer_length
){
3056 if (ptr
->length
>=0 && ptr
->length
<=ptr
->buffer_length
)
3057 ECALL(fd_copyout(ptr
->data
,
3059 ptr
->buffer_length
-
3068 static void raw_cmd_free(struct floppy_raw_cmd
**ptr
)
3070 struct floppy_raw_cmd
*next
,*this;
3075 if (this->buffer_length
) {
3076 fd_dma_mem_free((unsigned long)this->kernel_data
,
3077 this->buffer_length
);
3078 this->buffer_length
= 0;
3087 static inline int raw_cmd_copyin(int cmd
, char *param
,
3088 struct floppy_raw_cmd
**rcmd
)
3090 struct floppy_raw_cmd
*ptr
;
3096 ptr
= (struct floppy_raw_cmd
*)
3097 kmalloc(sizeof(struct floppy_raw_cmd
), GFP_USER
);
3103 ptr
->buffer_length
= 0;
3104 param
+= sizeof(struct floppy_raw_cmd
);
3105 if (ptr
->cmd_count
> 33)
3106 /* the command may now also take up the space
3107 * initially intended for the reply & the
3108 * reply count. Needed for long 82078 commands
3109 * such as RESTORE, which takes ... 17 command
3110 * bytes. Murphy's law #137: When you reserve
3111 * 16 bytes for a structure, you'll one day
3112 * discover that you really need 17...
3116 for (i
=0; i
< 16; i
++)
3118 ptr
->resultcode
= 0;
3119 ptr
->kernel_data
= 0;
3121 if (ptr
->flags
& (FD_RAW_READ
| FD_RAW_WRITE
)) {
3122 if (ptr
->length
<= 0)
3124 ptr
->kernel_data
=(char*)fd_dma_mem_alloc(ptr
->length
);
3125 fallback_on_nodma_alloc(&ptr
->kernel_data
,
3127 if (!ptr
->kernel_data
)
3129 ptr
->buffer_length
= ptr
->length
;
3131 if (ptr
->flags
& FD_RAW_WRITE
)
3132 ECALL(fd_copyin(ptr
->data
, ptr
->kernel_data
,
3134 rcmd
= & (ptr
->next
);
3135 if (!(ptr
->flags
& FD_RAW_MORE
))
3142 static int raw_cmd_ioctl(int cmd
, void *param
)
3144 int drive
, ret
, ret2
;
3145 struct floppy_raw_cmd
*my_raw_cmd
;
3147 if (FDCS
->rawcmd
<= 1)
3149 for (drive
= 0; drive
< N_DRIVE
; drive
++){
3150 if (FDC(drive
) != fdc
)
3152 if (drive
== current_drive
){
3153 if (UDRS
->fd_ref
> 1){
3157 } else if (UDRS
->fd_ref
){
3166 ret
= raw_cmd_copyin(cmd
, param
, &my_raw_cmd
);
3168 raw_cmd_free(&my_raw_cmd
);
3172 raw_cmd
= my_raw_cmd
;
3173 cont
= &raw_cmd_cont
;
3174 ret
=wait_til_done(floppy_start
,1);
3176 if (DP
->flags
& FD_DEBUG
){
3177 DPRINT("calling disk change from raw_cmd ioctl\n");
3181 if (ret
!= -EINTR
&& FDCS
->reset
)
3184 DRS
->track
= NO_TRACK
;
3186 ret2
= raw_cmd_copyout(cmd
, param
, my_raw_cmd
);
3189 raw_cmd_free(&my_raw_cmd
);
3193 static int invalidate_drive(kdev_t rdev
)
3195 /* invalidate the buffer track to force a reread */
3196 set_bit(DRIVE(rdev
), &fake_change
);
3197 process_fd_request();
3198 check_disk_change(rdev
);
3203 static inline void clear_write_error(int drive
)
3208 static inline int set_geometry(unsigned int cmd
, struct floppy_struct
*g
,
3209 int drive
, int type
, kdev_t device
)
3213 /* sanity checking for parameters.*/
3217 g
->track
> UDP
->tracks
>>STRETCH(g
) ||
3218 /* check if reserved bits are set */
3219 (g
->stretch
&~(FD_STRETCH
|FD_SWAPSIDES
)) != 0)
3222 if (!capable(CAP_SYS_ADMIN
))
3225 for (cnt
= 0; cnt
< N_DRIVE
; cnt
++){
3226 if (ITYPE(drive_state
[cnt
].fd_device
) == type
&&
3227 drive_state
[cnt
].fd_ref
)
3228 set_bit(drive
, &fake_change
);
3230 floppy_type
[type
] = *g
;
3231 floppy_type
[type
].name
="user format";
3232 for (cnt
= type
<< 2; cnt
< (type
<< 2) + 4; cnt
++)
3233 floppy_sizes
[cnt
]= floppy_sizes
[cnt
+0x80]=
3234 (floppy_type
[type
].size
+1)>>1;
3235 process_fd_request();
3236 for (cnt
= 0; cnt
< N_DRIVE
; cnt
++){
3237 if (ITYPE(drive_state
[cnt
].fd_device
) == type
&&
3238 drive_state
[cnt
].fd_ref
)
3241 drive_state
[cnt
].fd_device
));
3245 if (cmd
!= FDDEFPRM
)
3246 /* notice a disk change immediately, else
3247 * we loose our settings immediately*/
3248 CALL(poll_drive(1, FD_RAW_NEED_DISK
));
3249 user_params
[drive
] = *g
;
3250 if (buffer_drive
== drive
)
3251 SUPBOUND(buffer_max
, user_params
[drive
].sect
);
3252 current_type
[drive
] = &user_params
[drive
];
3253 floppy_sizes
[drive
] = (user_params
[drive
].size
+1) >> 1;
3254 if (cmd
== FDDEFPRM
)
3255 DRS
->keep_data
= -1;
3258 /* invalidation. Invalidate only when needed, i.e.
3259 * when there are already sectors in the buffer cache
3260 * whose number will change. This is useful, because
3261 * mtools often changes the geometry of the disk after
3262 * looking at the boot block */
3263 if (DRS
->maxblock
> user_params
[drive
].sect
|| DRS
->maxtrack
)
3264 invalidate_drive(device
);
3266 process_fd_request();
3271 /* handle obsolete ioctl's */
3272 static int ioctl_table
[]= {
3300 static inline int normalize_ioctl(int *cmd
, int *size
)
3304 for (i
=0; i
< ARRAY_SIZE(ioctl_table
); i
++) {
3305 if ((*cmd
& 0xffff) == (ioctl_table
[i
] & 0xffff)){
3306 *size
= _IOC_SIZE(*cmd
);
3307 *cmd
= ioctl_table
[i
];
3308 if (*size
> _IOC_SIZE(*cmd
)) {
3309 printk("ioctl not yet supported\n");
3318 static int get_floppy_geometry(int drive
, int type
, struct floppy_struct
**g
)
3321 *g
= &floppy_type
[type
];
3324 CALL(poll_drive(0,0));
3325 process_fd_request();
3326 *g
= current_type
[drive
];
3333 static int fd_ioctl(struct inode
*inode
, struct file
*filp
, unsigned int cmd
,
3334 unsigned long param
)
3336 #define IOCTL_MODE_BIT 8
3337 #define OPEN_WRITE_BIT 16
3338 #define IOCTL_ALLOWED (filp && (filp->f_mode & IOCTL_MODE_BIT))
3339 #define OUT(c,x) case c: outparam = (const char *) (x); break
3340 #define IN(c,x,tag) case c: *(x) = inparam. tag ; return 0
3347 struct floppy_struct g
; /* geometry */
3348 struct format_descr f
;
3349 struct floppy_max_errors max_errors
;
3350 struct floppy_drive_params dp
;
3351 } inparam
; /* parameters coming from user space */
3352 const char *outparam
; /* parameters passed back to user space */
3354 device
= inode
->i_rdev
;
3356 RO_IOCTLS(device
,param
);
3358 type
= TYPE(device
);
3359 drive
= DRIVE(device
);
3361 /* convert compatibility eject ioctls into floppy eject ioctl.
3362 * We do this in order to provide a means to eject floppy disks before
3363 * installing the new fdutils package */
3364 if(cmd
== CDROMEJECT
|| /* CD-ROM eject */
3365 cmd
== 0x6470 /* SunOS floppy eject */) {
3366 DPRINT("obsolete eject ioctl\n");
3367 DPRINT("please use floppycontrol --eject\n");
3371 /* generic block device ioctls */
3373 /* the following have been inspired by the corresponding
3374 * code for other block devices. */
3375 struct floppy_struct
*g
;
3378 struct hd_geometry loc
;
3379 ECALL(get_floppy_geometry(drive
, type
, &g
));
3380 loc
.heads
= g
->head
;
3381 loc
.sectors
= g
->sect
;
3382 loc
.cylinders
= g
->track
;
3384 return _COPYOUT(loc
);
3387 if(!capable(CAP_SYS_ADMIN
)) return -EACCES
;
3388 if(param
> 0xff) return -EINVAL
;
3389 read_ahead
[MAJOR(inode
->i_rdev
)] = param
;
3392 return put_user(read_ahead
[MAJOR(inode
->i_rdev
)],
3395 if(!capable(CAP_SYS_ADMIN
)) return -EACCES
;
3396 fsync_dev(inode
->i_rdev
);
3397 invalidate_buffers(inode
->i_rdev
);
3401 ECALL(get_floppy_geometry(drive
, type
, &g
));
3402 return put_user(g
->size
, (long *) param
);
3403 /* BLKRRPART is not defined as floppies don't have
3404 * partition tables */
3407 /* convert the old style command into a new style command */
3408 if ((cmd
& 0xff00) == 0x0200) {
3409 ECALL(normalize_ioctl(&cmd
, &size
));
3413 /* permission checks */
3414 if (((cmd
& 0x40) && !IOCTL_ALLOWED
) ||
3415 ((cmd
& 0x80) && !suser()))
3419 CLEARSTRUCT(&inparam
);
3420 if (_IOC_DIR(cmd
) & _IOC_WRITE
)
3421 ECALL(fd_copyin((void *)param
, &inparam
, size
))
3425 if(UDRS
->fd_ref
!= 1)
3426 /* somebody else has this drive open */
3430 /* do the actual eject. Fails on
3431 * non-Sparc architectures */
3432 ret
=fd_eject(UNIT(drive
));
3434 USETF(FD_DISK_CHANGED
);
3436 process_fd_request();
3440 current_type
[drive
] = NULL
;
3441 floppy_sizes
[drive
] = MAX_DISK_SIZE
;
3442 UDRS
->keep_data
= 0;
3443 return invalidate_drive(device
);
3446 return set_geometry(cmd
, & inparam
.g
,
3447 drive
, type
, device
);
3449 ECALL(get_floppy_geometry(drive
, type
,
3450 (struct floppy_struct
**)
3455 UDP
->flags
|= FTD_MSG
;
3458 UDP
->flags
&= ~FTD_MSG
;
3463 CALL(poll_drive(1, FD_RAW_NEED_DISK
));
3465 process_fd_request();
3468 if(!(ret
& FD_DISK_WRITABLE
))
3472 if (UDRS
->fd_ref
!= 1)
3474 return do_format(device
, &inparam
.f
);
3478 return invalidate_drive(device
);
3480 case FDSETEMSGTRESH
:
3481 UDP
->max_errors
.reporting
=
3482 (unsigned short) (param
& 0x0f);
3484 OUT(FDGETMAXERRS
, &UDP
->max_errors
);
3485 IN(FDSETMAXERRS
, &UDP
->max_errors
, max_errors
);
3488 outparam
= drive_name(type
,drive
);
3489 SUPBOUND(size
,strlen(outparam
)+1);
3492 IN(FDSETDRVPRM
, UDP
, dp
);
3493 OUT(FDGETDRVPRM
, UDP
);
3497 CALL(poll_drive(1, FD_RAW_NEED_DISK
));
3498 process_fd_request();
3500 OUT(FDGETDRVSTAT
, UDRS
);
3503 return user_reset_fdc(drive
, (int)param
, 1);
3505 OUT(FDGETFDCSTAT
,UFDCS
);
3510 OUT(FDWERRORGET
,UDRWE
);
3517 CALL(i
= raw_cmd_ioctl(cmd
,(void *) param
));
3518 process_fd_request();
3524 process_fd_request();
3531 if (_IOC_DIR(cmd
) & _IOC_READ
)
3532 return fd_copyout((void *)param
, outparam
, size
);
3535 #undef IOCTL_ALLOWED
3540 static void config_types(void)
3545 /* read drive info out of physical CMOS */
3548 UDP
->cmos
= FLOPPY0_TYPE
;
3550 if (!UDP
->cmos
&& FLOPPY1_TYPE
)
3551 UDP
->cmos
= FLOPPY1_TYPE
;
3554 /* additional physical CMOS drive detection should go here */
3556 for (drive
=0; drive
< N_DRIVE
; drive
++){
3557 if (UDP
->cmos
>= 16)
3559 if (UDP
->cmos
>= 0 && UDP
->cmos
<= NUMBER(default_drive_params
))
3560 memcpy((char *) UDP
,
3561 (char *) (&default_drive_params
[(int)UDP
->cmos
].params
),
3562 sizeof(struct floppy_drive_params
));
3565 printk(KERN_INFO
"Floppy drive(s): ");
3570 allowed_drive_mask
|= 1 << drive
;
3571 printk("fd%d is %s", drive
,
3572 default_drive_params
[(int)UDP
->cmos
].name
);
3574 printk("fd%d is unknown type %d",drive
,
3582 static ssize_t
floppy_read(struct file
* filp
, char *buf
,
3583 size_t count
, loff_t
*ppos
)
3585 struct inode
*inode
= filp
->f_dentry
->d_inode
;
3586 int drive
= DRIVE(inode
->i_rdev
);
3588 check_disk_change(inode
->i_rdev
);
3589 if (UTESTF(FD_DISK_CHANGED
))
3591 return block_read(filp
, buf
, count
, ppos
);
3594 static ssize_t
floppy_write(struct file
* filp
, const char * buf
,
3595 size_t count
, loff_t
*ppos
)
3597 struct inode
* inode
= filp
->f_dentry
->d_inode
;
3600 int drive
= DRIVE(inode
->i_rdev
);
3602 if (!UDRS
->maxblock
)
3603 UDRS
->maxblock
=1;/* make change detectable */
3604 check_disk_change(inode
->i_rdev
);
3605 if (UTESTF(FD_DISK_CHANGED
))
3607 if (!UTESTF(FD_DISK_WRITABLE
))
3609 block
= (*ppos
+ count
) >> 9;
3610 INFBOUND(UDRS
->maxblock
, block
);
3611 ret
= block_write(filp
, buf
, count
, ppos
);
3615 static int floppy_release(struct inode
* inode
, struct file
* filp
)
3619 drive
= DRIVE(inode
->i_rdev
);
3622 * If filp is NULL, we're being called from blkdev_release
3623 * or after a failed mount attempt. In the former case the
3624 * device has already been sync'ed, and in the latter no
3625 * sync is required. Otherwise, sync if filp is writable.
3627 if (filp
&& (filp
->f_mode
& (2 | OPEN_WRITE_BIT
)))
3628 block_fsync(filp
, filp
->f_dentry
);
3630 if (UDRS
->fd_ref
< 0)
3632 else if (!UDRS
->fd_ref
--) {
3633 DPRINT("floppy_release with fd_ref == 0");
3636 floppy_release_irq_and_dma();
3641 * floppy_open check for aliasing (/dev/fd0 can be the same as
3642 * /dev/PS0 etc), and disallows simultaneous access to the same
3643 * drive with different device numbers.
3645 #define RETERR(x) do{floppy_release(inode,filp); return -(x);}while(0)
3647 static int floppy_open(struct inode
* inode
, struct file
* filp
)
3655 DPRINT("Weird, open called with filp=0\n");
3659 drive
= DRIVE(inode
->i_rdev
);
3660 if (drive
>= N_DRIVE
||
3661 !(allowed_drive_mask
& (1 << drive
)) ||
3662 fdc_state
[FDC(drive
)].version
== FDC_NONE
)
3665 if (TYPE(inode
->i_rdev
) >= NUMBER(floppy_type
))
3667 old_dev
= UDRS
->fd_device
;
3668 if (UDRS
->fd_ref
&& old_dev
!= MINOR(inode
->i_rdev
))
3671 if (!UDRS
->fd_ref
&& (UDP
->flags
& FD_BROKEN_DCL
)){
3672 USETF(FD_DISK_CHANGED
);
3676 if (UDRS
->fd_ref
== -1 ||
3677 (UDRS
->fd_ref
&& (filp
->f_flags
& O_EXCL
)))
3680 if (floppy_grab_irq_and_dma())
3683 if (filp
->f_flags
& O_EXCL
)
3688 if (!floppy_track_buffer
){
3689 /* if opening an ED drive, reserve a big buffer,
3690 * else reserve a small one */
3691 if ((UDP
->cmos
== 6) || (UDP
->cmos
== 5))
3692 try = 64; /* Only 48 actually useful */
3694 try = 32; /* Only 24 actually useful */
3696 tmp
=(char *)fd_dma_mem_alloc(1024 * try);
3697 if (!tmp
&& !floppy_track_buffer
) {
3698 try >>= 1; /* buffer only one side */
3700 tmp
= (char *)fd_dma_mem_alloc(1024*try);
3702 if(!tmp
&& !floppy_track_buffer
) {
3703 fallback_on_nodma_alloc(&tmp
, 2048 * try);
3705 if (!tmp
&& !floppy_track_buffer
) {
3706 DPRINT("Unable to allocate DMA memory\n");
3709 if (floppy_track_buffer
) {
3711 fd_dma_mem_free((unsigned long)tmp
,try*1024);
3713 buffer_min
= buffer_max
= -1;
3714 floppy_track_buffer
= tmp
;
3715 max_buffer_sectors
= try;
3719 UDRS
->fd_device
= MINOR(inode
->i_rdev
);
3720 if (old_dev
!= -1 && old_dev
!= MINOR(inode
->i_rdev
)) {
3721 if (buffer_drive
== drive
)
3723 invalidate_buffers(MKDEV(FLOPPY_MAJOR
,old_dev
));
3726 /* Allow ioctls if we have write-permissions even if read-only open */
3727 if ((filp
->f_mode
& 2) || (permission(inode
,2) == 0))
3728 filp
->f_mode
|= IOCTL_MODE_BIT
;
3729 if (filp
->f_mode
& 2)
3730 filp
->f_mode
|= OPEN_WRITE_BIT
;
3732 if (UFDCS
->rawcmd
== 1)
3735 if (filp
->f_flags
& O_NDELAY
)
3737 if (filp
->f_mode
& 3) {
3738 UDRS
->last_checked
= 0;
3739 check_disk_change(inode
->i_rdev
);
3740 if (UTESTF(FD_DISK_CHANGED
))
3743 if ((filp
->f_mode
& 2) && !(UTESTF(FD_DISK_WRITABLE
)))
3750 * Check if the disk has been changed or if a change has been faked.
3752 static int check_floppy_change(kdev_t dev
)
3754 int drive
= DRIVE(dev
);
3756 if (MAJOR(dev
) != MAJOR_NR
) {
3757 DPRINT("check_floppy_change: not a floppy\n");
3761 if (UTESTF(FD_DISK_CHANGED
) || UTESTF(FD_VERIFY
))
3764 if (UDP
->checkfreq
< jiffies
- UDRS
->last_checked
){
3767 process_fd_request();
3770 if (UTESTF(FD_DISK_CHANGED
) ||
3771 UTESTF(FD_VERIFY
) ||
3772 test_bit(drive
, &fake_change
) ||
3773 (!TYPE(dev
) && !current_type
[drive
]))
3778 /* revalidate the floppy disk, i.e. trigger format autodetection by reading
3779 * the bootblock (block 0). "Autodetection" is also needed to check whether
3780 * there is a disk in the drive at all... Thus we also do it for fixed
3781 * geometry formats */
3782 static int floppy_revalidate(kdev_t dev
)
3784 #define NO_GEOM (!current_type[drive] && !TYPE(dev))
3785 struct buffer_head
* bh
;
3786 int drive
=DRIVE(dev
);
3789 if (UTESTF(FD_DISK_CHANGED
) ||
3790 UTESTF(FD_VERIFY
) ||
3791 test_bit(drive
, &fake_change
) ||
3794 cf
= UTESTF(FD_DISK_CHANGED
) || UTESTF(FD_VERIFY
);
3795 if (!(cf
|| test_bit(drive
, &fake_change
) || NO_GEOM
)){
3796 process_fd_request(); /*already done by another thread*/
3801 if (buffer_drive
== drive
)
3803 clear_bit(drive
, &fake_change
);
3804 UCLEARF(FD_DISK_CHANGED
);
3809 int size
= floppy_blocksizes
[MINOR(dev
)];
3812 if (!(bh
= getblk(dev
,0,size
))){
3813 process_fd_request();
3816 if (bh
&& !buffer_uptodate(bh
))
3817 ll_rw_block(READ
, 1, &bh
);
3818 process_fd_request();
3824 poll_drive(0, FD_RAW_NEED_DISK
);
3825 process_fd_request();
3830 static struct file_operations floppy_fops
= {
3831 NULL
, /* lseek - default */
3832 floppy_read
, /* read - general block-dev read */
3833 floppy_write
, /* write - general block-dev write */
3834 NULL
, /* readdir - bad */
3836 fd_ioctl
, /* ioctl */
3838 floppy_open
, /* open */
3840 floppy_release
, /* release */
3841 block_fsync
, /* fsync */
3843 check_floppy_change
, /* media_change */
3844 floppy_revalidate
, /* revalidate */
3848 * Floppy Driver initialization
3849 * =============================
3852 /* Determine the floppy disk controller type */
3853 /* This routine was written by David C. Niemi */
3854 __initfunc(static char get_fdc_version(void))
3858 output_byte(FD_DUMPREGS
); /* 82072 and better know DUMPREGS */
3861 if ((r
= result()) <= 0x00)
3862 return FDC_NONE
; /* No FDC present ??? */
3863 if ((r
==1) && (reply_buffer
[0] == 0x80)){
3864 printk(KERN_INFO
"FDC %d is an 8272A\n",fdc
);
3865 return FDC_8272A
; /* 8272a/765 don't know DUMPREGS */
3868 printk("FDC %d init: DUMPREGS: unexpected return of %d bytes.\n",
3873 if(!fdc_configure()) {
3874 printk(KERN_INFO
"FDC %d is an 82072\n",fdc
);
3875 return FDC_82072
; /* 82072 doesn't know CONFIGURE */
3878 output_byte(FD_PERPENDICULAR
);
3879 if(need_more_output() == MORE_OUTPUT
) {
3882 printk(KERN_INFO
"FDC %d is an 82072A\n", fdc
);
3883 return FDC_82072A
; /* 82072A as found on Sparcs. */
3886 output_byte(FD_UNLOCK
);
3888 if ((r
== 1) && (reply_buffer
[0] == 0x80)){
3889 printk(KERN_INFO
"FDC %d is a pre-1991 82077\n", fdc
);
3890 return FDC_82077_ORIG
; /* Pre-1991 82077, doesn't know
3893 if ((r
!= 1) || (reply_buffer
[0] != 0x00)) {
3894 printk("FDC %d init: UNLOCK: unexpected return of %d bytes.\n",
3898 output_byte(FD_PARTID
);
3901 printk("FDC %d init: PARTID: unexpected return of %d bytes.\n",
3905 if (reply_buffer
[0] == 0x80) {
3906 printk(KERN_INFO
"FDC %d is a post-1991 82077\n",fdc
);
3907 return FDC_82077
; /* Revised 82077AA passes all the tests */
3909 switch (reply_buffer
[0] >> 5) {
3911 /* Either a 82078-1 or a 82078SL running at 5Volt */
3912 printk(KERN_INFO
"FDC %d is an 82078.\n",fdc
);
3915 printk(KERN_INFO
"FDC %d is a 44pin 82078\n",fdc
);
3918 printk(KERN_INFO
"FDC %d is a S82078B\n", fdc
);
3921 printk(KERN_INFO
"FDC %d is a National Semiconductor PC87306\n", fdc
);
3924 printk(KERN_INFO
"FDC %d init: 82078 variant with unknown PARTID=%d.\n",
3925 fdc
, reply_buffer
[0] >> 5);
3926 return FDC_82078_UNKN
;
3928 } /* get_fdc_version */
3930 /* lilo configuration */
3932 __initfunc(static void floppy_set_flags(int *ints
,int param
, int param2
))
3936 for (i
=0; i
< ARRAY_SIZE(default_drive_params
); i
++){
3938 default_drive_params
[i
].params
.flags
|= param2
;
3940 default_drive_params
[i
].params
.flags
&= ~param2
;
3942 DPRINT("%s flag 0x%x\n", param2
? "Setting" : "Clearing", param
);
3945 __initfunc(static void daring(int *ints
,int param
, int param2
))
3949 for (i
=0; i
< ARRAY_SIZE(default_drive_params
); i
++){
3951 default_drive_params
[i
].params
.select_delay
= 0;
3952 default_drive_params
[i
].params
.flags
|= FD_SILENT_DCL_CLEAR
;
3954 default_drive_params
[i
].params
.select_delay
= 2*HZ
/100;
3955 default_drive_params
[i
].params
.flags
&= ~FD_SILENT_DCL_CLEAR
;
3958 DPRINT("Assuming %s floppy hardware\n", param
? "standard" : "broken");
3961 __initfunc(static void set_cmos(int *ints
, int dummy
, int dummy2
))
3963 int current_drive
=0;
3966 DPRINT("wrong number of parameters for CMOS\n");
3969 current_drive
= ints
[1];
3970 if (current_drive
< 0 || current_drive
>= 8){
3971 DPRINT("bad drive for set_cmos\n");
3974 if (current_drive
>= 4 && !FDC2
)
3977 (ints
[2] >= NUMBER(default_drive_params
) && ints
[2] != 16)){
3978 DPRINT("bad CMOS code %d\n", ints
[2]);
3982 DPRINT("setting CMOS code to %d\n", ints
[2]);
3985 static struct param_table
{
3987 void (*fn
)(int *ints
, int param
, int param2
);
3992 { "allowed_drive_mask", 0, &allowed_drive_mask
, 0xff, 0}, /* obsolete */
3993 { "all_drives", 0, &allowed_drive_mask
, 0xff, 0 }, /* obsolete */
3994 { "asus_pci", 0, &allowed_drive_mask
, 0x33, 0},
3996 { "irq", 0, &FLOPPY_IRQ
, 6, 0 },
3997 { "dma", 0, &FLOPPY_DMA
, 2, 0 },
3999 { "daring", daring
, 0, 1, 0},
4001 { "two_fdc", 0, &FDC2
, 0x370, 0 },
4002 { "one_fdc", 0, &FDC2
, 0, 0 },
4004 { "thinkpad", floppy_set_flags
, 0, 1, FD_INVERTED_DCL
},
4005 { "broken_dcl", floppy_set_flags
, 0, 1, FD_BROKEN_DCL
},
4006 { "messages", floppy_set_flags
, 0, 1, FTD_MSG
},
4007 { "silent_dcl_clear", floppy_set_flags
, 0, 1, FD_SILENT_DCL_CLEAR
},
4008 { "debug", floppy_set_flags
, 0, 1, FD_DEBUG
},
4010 { "nodma", 0, &can_use_virtual_dma
, 1, 0 },
4011 { "omnibook", 0, &can_use_virtual_dma
, 1, 0 },
4012 { "yesdma", 0, &can_use_virtual_dma
, 0, 0 },
4014 { "fifo_depth", 0, &fifo_depth
, 0xa, 0 },
4015 { "nofifo", 0, &no_fifo
, 0x20, 0 },
4016 { "usefifo", 0, &no_fifo
, 0, 0 },
4018 { "cmos", set_cmos
, 0, 0, 0 },
4020 { "unexpected_interrupts", 0, &print_unex
, 1, 0 },
4021 { "no_unexpected_interrupts", 0, &print_unex
, 0, 0 },
4022 { "L40SX", 0, &print_unex
, 0, 0 } };
4024 #define FLOPPY_SETUP
4025 __initfunc(void floppy_setup(char *str
, int *ints
))
4030 for (i
=0; i
< ARRAY_SIZE(config_params
); i
++){
4031 if (strcmp(str
,config_params
[i
].name
) == 0){
4035 param
= config_params
[i
].def_param
;
4036 if(config_params
[i
].fn
)
4039 config_params
[i
].param2
);
4040 if(config_params
[i
].var
) {
4041 DPRINT("%s=%d\n", str
, param
);
4042 *config_params
[i
].var
= param
;
4048 DPRINT("unknown floppy option [%s]\n", str
);
4050 DPRINT("allowed options are:");
4051 for (i
=0; i
< ARRAY_SIZE(config_params
); i
++)
4052 printk(" %s",config_params
[i
].name
);
4055 DPRINT("botched floppy option\n");
4056 DPRINT("Read linux/drivers/block/README.fd\n");
4059 static int have_no_fdc
= -EIO
;
4062 __initfunc(int floppy_init(void))
4069 if (register_blkdev(MAJOR_NR
,"fd",&floppy_fops
)) {
4070 printk("Unable to get major %d for floppy\n",MAJOR_NR
);
4074 for (i
=0; i
<256; i
++)
4076 floppy_sizes
[i
] = (floppy_type
[ITYPE(i
)].size
+1) >> 1;
4078 floppy_sizes
[i
] = MAX_DISK_SIZE
;
4080 blk_size
[MAJOR_NR
] = floppy_sizes
;
4081 blksize_size
[MAJOR_NR
] = floppy_blocksizes
;
4082 blk_dev
[MAJOR_NR
].request_fn
= DEVICE_REQUEST
;
4083 reschedule_timeout(MAXTIMEOUT
, "floppy init", MAXTIMEOUT
);
4086 for (i
= 0; i
< N_FDC
; i
++) {
4092 /*sparcs don't have a DOR reset which we can fall back on to*/
4093 FDCS
->version
= FDC_82072A
;
4097 use_virtual_dma
= can_use_virtual_dma
& 1;
4098 fdc_state
[0].address
= FDC1
;
4099 if (fdc_state
[0].address
== -1) {
4100 unregister_blkdev(MAJOR_NR
,"fd");
4101 del_timer(&fd_timeout
);
4105 fdc_state
[1].address
= FDC2
;
4108 if (floppy_grab_irq_and_dma()){
4109 unregister_blkdev(MAJOR_NR
,"fd");
4110 del_timer(&fd_timeout
);
4114 /* initialise drive state */
4115 for (drive
= 0; drive
< N_DRIVE
; drive
++) {
4118 UDRS
->flags
= FD_VERIFY
| FD_DISK_NEWCHANGE
| FD_DISK_CHANGED
;
4119 UDRS
->fd_device
= -1;
4120 floppy_track_buffer
= NULL
;
4121 max_buffer_sectors
= 0;
4124 for (i
= 0; i
< N_FDC
; i
++) {
4126 FDCS
->driver_version
= FD_DRIVER_VERSION
;
4127 for (unit
=0; unit
<4; unit
++)
4128 FDCS
->track
[unit
] = 0;
4129 if (FDCS
->address
== -1)
4132 if (user_reset_fdc(-1,FD_RESET_ALWAYS
,0)){
4134 FDCS
->version
= FDC_NONE
;
4137 /* Try to determine the floppy controller type */
4138 FDCS
->version
= get_fdc_version();
4139 if (FDCS
->version
== FDC_NONE
){
4143 if(can_use_virtual_dma
== 2 && FDCS
->version
< FDC_82072A
)
4144 can_use_virtual_dma
= 0;
4147 /* Not all FDCs seem to be able to handle the version command
4148 * properly, so force a reset for the standard FDC clones,
4149 * to avoid interrupt garbage.
4151 user_reset_fdc(-1,FD_RESET_ALWAYS
,0);
4154 del_timer(&fd_timeout
);
4156 floppy_release_irq_and_dma();
4159 DPRINT("no floppy controllers found\n");
4160 unregister_blkdev(MAJOR_NR
,"fd");
4165 static int floppy_grab_irq_and_dma(void)
4167 unsigned long flags
;
4176 if (fd_request_irq()) {
4177 DPRINT("Unable to grab IRQ%d for the floppy driver\n",
4183 if (fd_request_dma()) {
4184 DPRINT("Unable to grab DMA%d for the floppy driver\n",
4192 for (fdc
=0; fdc
< N_FDC
; fdc
++){
4193 if (FDCS
->address
!= -1){
4194 if (check_region(FDCS
->address
, 6) < 0 ||
4195 check_region(FDCS
->address
+7, 1) < 0) {
4196 DPRINT("Floppy io-port 0x%04lx in use\n", FDCS
->address
);
4200 release_region(FDCS
->address
, 6);
4201 release_region(FDCS
->address
+7, 1);
4207 request_region(FDCS
->address
, 6, "floppy");
4208 request_region(FDCS
->address
+7, 1, "floppy DIR");
4209 /* address + 6 is reserved, and may be taken by IDE.
4210 * Unfortunately, Adaptec doesn't know this :-(, */
4213 for (fdc
=0; fdc
< N_FDC
; fdc
++){
4214 if (FDCS
->address
!= -1){
4216 fd_outb(FDCS
->dor
, FD_DOR
);
4220 set_dor(0, ~0, 8); /* avoid immediate interrupt */
4222 for (fdc
= 0; fdc
< N_FDC
; fdc
++)
4223 if (FDCS
->address
!= -1)
4224 fd_outb(FDCS
->dor
, FD_DOR
);
4226 * The driver will try and free resources and relies on us
4227 * to know if they were allocated or not.
4230 irqdma_allocated
= 1;
4234 static void floppy_release_irq_and_dma(void)
4237 #ifdef FLOPPY_SANITY_CHECK
4243 unsigned long tmpaddr
;
4244 unsigned long flags
;
4252 if(irqdma_allocated
)
4263 floppy_enable_hlt();
4265 if (floppy_track_buffer
&& max_buffer_sectors
) {
4266 tmpsize
= max_buffer_sectors
*1024;
4267 tmpaddr
= (unsigned long)floppy_track_buffer
;
4268 floppy_track_buffer
= 0;
4269 max_buffer_sectors
= 0;
4270 buffer_min
= buffer_max
= -1;
4271 fd_dma_mem_free(tmpaddr
, tmpsize
);
4274 #ifdef FLOPPY_SANITY_CHECK
4276 for (drive
=0; drive
< N_FDC
* 4; drive
++)
4277 if (motor_off_timer
[drive
].next
)
4278 printk("motor off timer %d still active\n", drive
);
4281 if (fd_timeout
.next
)
4282 printk("floppy timer still active:%s\n", timeout_message
);
4284 printk("auxiliary floppy timer still active\n");
4286 printk("task queue still active\n");
4289 for (fdc
= 0; fdc
< N_FDC
; fdc
++)
4290 if (FDCS
->address
!= -1) {
4291 release_region(FDCS
->address
, 6);
4292 release_region(FDCS
->address
+7, 1);
4301 extern char *get_options(char *str
, int *ints
);
4305 __initfunc(static void parse_floppy_cfg_string(char *cfg
))
4311 for(ptr
= cfg
;*cfg
&& *cfg
!= ' ' && *cfg
!= '\t'; cfg
++);
4317 floppy_setup(get_options(ptr
,ints
),ints
);
4321 __initfunc(static void mod_setup(char *pattern
, void (*setup
)(char *, int *)))
4329 int length
= strlen(pattern
)+1;
4334 for (i
=current
->mm
->env_start
; i
< current
->mm
->env_end
; i
++){
4335 get_user(c
, (char *)i
);
4340 if (!c
|| c
== ' ' || c
== '\t'){
4343 setup(get_options(buffer
,ints
),ints
);
4352 if ((!j
&& !c
) || (j
&& c
== pattern
[j
-1]))
4367 int init_module(void)
4369 printk(KERN_INFO
"inserting floppy driver for " UTS_RELEASE
"\n");
4372 parse_floppy_cfg_string(floppy
);
4374 mod_setup("floppy=", floppy_setup
);
4376 return floppy_init();
4379 void cleanup_module(void)
4383 unregister_blkdev(MAJOR_NR
, "fd");
4385 blk_dev
[MAJOR_NR
].request_fn
= 0;
4386 /* eject disk, if any */
4387 dummy
= fd_eject(0);
4390 MODULE_PARM(floppy
,"s");
4391 MODULE_PARM(FLOPPY_IRQ
,"i");
4392 MODULE_PARM(FLOPPY_DMA
,"i");
4393 MODULE_AUTHOR("Alain L. Knaff");
4394 MODULE_SUPPORTED_DEVICE("fd");
4401 /* eject the boot floppy (if we need the drive for a different root floppy) */
4402 /* This should only be called at boot time when we're sure that there's no
4403 * resource contention. */
4404 void floppy_eject(void)
4409 if(floppy_grab_irq_and_dma()==0)
4411 lock_fdc(MAXTIMEOUT
,0);
4413 process_fd_request();
4414 floppy_release_irq_and_dma();