2 * linux/kernel/floppy.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 * Copyright (C) 1993, 1994 Alain Knaff
8 * 02.12.91 - Changed to static variables to indicate need for reset
9 * and recalibrate. This makes some things easier (output_byte reset
10 * checking etc), and means less interrupt jumping in case of errors,
11 * so the code is hopefully easier to understand.
15 * This file is certainly a mess. I've tried my best to get it working,
16 * but I don't like programming floppies, and I have only one anyway.
17 * Urgel. I should check for more errors, and do more graceful error
18 * recovery. Seems there are problems with several drives. I've tried to
19 * correct them. No promises.
23 * As with hd.c, all routines within this file can (and will) be called
24 * by interrupts, so extreme caution is needed. A hardware interrupt
25 * handler may not sleep, or a kernel panic will happen. Thus I cannot
26 * call "floppy-on" directly, but have to set a special timer interrupt
31 * 28.02.92 - made track-buffering routines, based on the routines written
32 * by entropy@wintermute.wpi.edu (Lawrence Foard). Linus.
36 * Automatic floppy-detection and formatting written by Werner Almesberger
37 * (almesber@nessie.cs.id.ethz.ch), who also corrected some problems with
38 * the floppy-change signal detection.
42 * 1992/7/22 -- Hennus Bergman: Added better error reporting, fixed
43 * FDC data overrun bug, added some preliminary stuff for vertical
46 * 1992/9/17: Added DMA allocation & DMA functions. -- hhb.
48 * TODO: Errors are still not counted properly.
52 * Modifications for ``Sector Shifting'' by Rob Hooft (hooft@chem.ruu.nl)
53 * modeled after the freeware MS-DOS program fdformat/88 V1.8 by
54 * Christoph H. Hochst\"atter.
55 * I have fixed the shift values to the ones I always use. Maybe a new
56 * ioctl() should be created to be able to modify them.
57 * There is a bug in the driver that makes it impossible to format a
58 * floppy as the first thing after bootup.
62 * 1993/4/29 -- Linus -- cleaned up the timer handling in the kernel, and
63 * this helped the floppy driver as well. Much cleaner, and still seems to
67 /* 1994/6/24 --bbroad-- added the floppy table entries and made
68 * minor modifications to allow 2.88 floppies to be run.
71 /* 1994/7/13 -- Paul Vojta -- modified the probing code to allow three or more
76 * 1994/8/8 -- Alain Knaff -- Switched to fdpatch driver: Support for bigger
77 * format bug fixes, but unfortunately some new bugs too...
80 /* 1994/9/17 -- Koen Holtman -- added logging of physical floppy write
81 * errors to allow safe writing by specialized programs.
84 /* 1995/4/24 -- Dan Fandrich -- added support for Commodore 1581 3.5" disks
85 * by defining bit 1 of the "stretch" parameter to mean put sectors on the
86 * opposite side of the disk, leaving the sector IDs alone (i.e. Commodore's
87 * drives are "upside-down").
91 * 1995/8/26 -- Andreas Busse -- added Mips support.
95 * 1995/10/18 -- Ralf Baechle -- Portability cleanup; move machine dependent
96 * features to asm/floppy.h.
100 #define FLOPPY_SANITY_CHECK
101 #undef FLOPPY_SILENT_DCL_CLEAR
103 #define REALLY_SLOW_IO
106 #define DCL_DEBUG /* debug disk change line */
108 /* do print messages for unexpected interrupts */
109 static int print_unex
=1;
110 #include <linux/module.h>
112 /* the following is the mask of allowed drives. By default units 2 and
113 * 3 of both floppy controllers are disabled, because switching on the
114 * motor of these drives causes system hangs on some PCI computers. drive
115 * 0 is the low bit (0x1), and drive 7 is the high bit (0x80). Bits are on if
116 * a drive is allowed. */
117 static int FLOPPY_IRQ
=6;
118 static int FLOPPY_DMA
=2;
119 static int allowed_drive_mask
= 0x33;
122 #include <linux/sched.h>
123 #include <linux/fs.h>
124 #include <linux/kernel.h>
125 #include <linux/timer.h>
126 #include <linux/tqueue.h>
128 #include <linux/fdreg.h>
131 #include <linux/fd.h>
132 #include <linux/hdreg.h>
134 #include <linux/errno.h>
135 #include <linux/malloc.h>
136 #include <linux/mm.h>
137 #include <linux/string.h>
138 #include <linux/fcntl.h>
139 #include <linux/delay.h>
140 #include <linux/mc146818rtc.h> /* CMOS defines */
141 #include <linux/ioport.h>
142 #include <linux/interrupt.h>
143 #include <linux/init.h>
147 #include <asm/system.h>
149 #include <asm/uaccess.h>
151 static int can_use_virtual_dma
=2;
153 * can use virtual DMA:
154 * 0 = use of virtual DMA disallowed by config
155 * 1 = use of virtual DMA prescribed by config
156 * 2 = no virtual DMA preference configured. By default try hard DMA,
157 * but fall back on virtual DMA when not enough memory available
160 static int use_virtual_dma
=0;
164 * 1 using virtual DMA
165 * This variable is set to virtual when a DMA mem problem arises, and
166 * reset back in floppy_grab_irq_and_dma.
167 * It is not safe to reset it in other circumstances, because the floppy
168 * driver may have several buffers in use at once, and we do currently not
169 * record each buffers capabilities
172 static unsigned short virtual_dma_port
=0x3f0;
173 void floppy_interrupt(int irq
, void *dev_id
, struct pt_regs
* regs
);
174 static int set_dor(int fdc
, char mask
, char data
);
175 static inline int __get_order(unsigned long size
);
176 #define K_64 0x10000 /* 64KB */
177 #include <asm/floppy.h>
180 #define MAJOR_NR FLOPPY_MAJOR
182 #include <linux/blk.h>
183 #include <linux/cdrom.h> /* for the compatibility eject ioctl */
185 #ifndef fd_get_dma_residue
186 #define fd_get_dma_residue() get_dma_residue(FLOPPY_DMA)
189 /* Dma Memory related stuff */
191 /* Pure 2^n version of get_order */
192 static inline int __get_order(unsigned long size
)
196 size
= (size
-1) >> (PAGE_SHIFT
-1);
205 #ifndef fd_dma_mem_free
206 #define fd_dma_mem_free(addr, size) free_pages(addr, __get_order(size))
209 #ifndef fd_dma_mem_alloc
210 #define fd_dma_mem_alloc(size) __get_dma_pages(GFP_KERNEL,__get_order(size))
213 static inline void fallback_on_nodma_alloc(char **addr
, size_t l
)
215 #ifdef FLOPPY_CAN_FALLBACK_ON_NODMA
217 return; /* we have the memory */
218 if(can_use_virtual_dma
!= 2)
219 return; /* no fallback allowed */
220 printk("DMA memory shortage. Temporarily falling back on virtual DMA\n");
221 *addr
= (char *) nodma_mem_alloc(l
);
227 /* End dma memory related stuff */
229 static unsigned int fake_change
= 0;
230 static int initialising
=1;
232 static inline int TYPE(kdev_t x
) {
233 return (MINOR(x
)>>2) & 0x1f;
235 static inline int DRIVE(kdev_t x
) {
236 return (MINOR(x
)&0x03) | ((MINOR(x
)&0x80) >> 5);
238 #define ITYPE(x) (((x)>>2) & 0x1f)
239 #define TOMINOR(x) ((x & 3) | ((x & 4) << 5))
240 #define UNIT(x) ((x) & 0x03) /* drive on fdc */
241 #define FDC(x) (((x) & 0x04) >> 2) /* fdc of drive */
242 #define REVDRIVE(fdc, unit) ((unit) + ((fdc) << 2))
243 /* reverse mapping from unit and fdc to drive */
244 #define DP (&drive_params[current_drive])
245 #define DRS (&drive_state[current_drive])
246 #define DRWE (&write_errors[current_drive])
247 #define FDCS (&fdc_state[fdc])
248 #define CLEARF(x) (clear_bit(x##_BIT, &DRS->flags))
249 #define SETF(x) (set_bit(x##_BIT, &DRS->flags))
250 #define TESTF(x) (test_bit(x##_BIT, &DRS->flags))
252 #define UDP (&drive_params[drive])
253 #define UDRS (&drive_state[drive])
254 #define UDRWE (&write_errors[drive])
255 #define UFDCS (&fdc_state[FDC(drive)])
256 #define UCLEARF(x) (clear_bit(x##_BIT, &UDRS->flags))
257 #define USETF(x) (set_bit(x##_BIT, &UDRS->flags))
258 #define UTESTF(x) (test_bit(x##_BIT, &UDRS->flags))
260 #define DPRINT(format, args...) printk(DEVICE_NAME "%d: " format, current_drive , ## args)
262 #define PH_HEAD(floppy,head) (((((floppy)->stretch & 2) >>1) ^ head) << 2)
263 #define STRETCH(floppy) ((floppy)->stretch & FD_STRETCH)
265 #define CLEARSTRUCT(x) memset((x), 0, sizeof(*(x)))
267 #define INT_OFF save_flags(flags); cli()
268 #define INT_ON restore_flags(flags)
271 #define COMMAND raw_cmd->cmd[0]
272 #define DR_SELECT raw_cmd->cmd[1]
273 #define TRACK raw_cmd->cmd[2]
274 #define HEAD raw_cmd->cmd[3]
275 #define SECTOR raw_cmd->cmd[4]
276 #define SIZECODE raw_cmd->cmd[5]
277 #define SECT_PER_TRACK raw_cmd->cmd[6]
278 #define GAP raw_cmd->cmd[7]
279 #define SIZECODE2 raw_cmd->cmd[8]
283 #define F_SIZECODE raw_cmd->cmd[2]
284 #define F_SECT_PER_TRACK raw_cmd->cmd[3]
285 #define F_GAP raw_cmd->cmd[4]
286 #define F_FILL raw_cmd->cmd[5]
290 * Maximum disk size (in kilobytes). This default is used whenever the
291 * current disk size is unknown.
292 * [Now it is rather a minimum]
294 #define MAX_DISK_SIZE 4 /* 3984*/
298 * globals used by 'result()'
300 #define MAX_REPLIES 16
301 static unsigned char reply_buffer
[MAX_REPLIES
];
302 static int inr
; /* size of reply buffer, when called from interrupt */
303 #define ST0 (reply_buffer[0])
304 #define ST1 (reply_buffer[1])
305 #define ST2 (reply_buffer[2])
306 #define ST3 (reply_buffer[0]) /* result of GETSTATUS */
307 #define R_TRACK (reply_buffer[3])
308 #define R_HEAD (reply_buffer[4])
309 #define R_SECTOR (reply_buffer[5])
310 #define R_SIZECODE (reply_buffer[6])
312 #define SEL_DLY (2*HZ/100)
314 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
316 * this struct defines the different floppy drive types.
319 struct floppy_drive_params params
;
320 const char *name
; /* name printed while booting */
321 } default_drive_params
[]= {
322 /* NOTE: the time values in jiffies should be in msec!
324 | Maximum data rate supported by drive type
325 | | Head load time, msec
326 | | | Head unload time, msec (not used)
327 | | | | Step rate interval, usec
328 | | | | | Time needed for spinup time (jiffies)
329 | | | | | | Timeout for spinning down (jiffies)
330 | | | | | | | Spindown offset (where disk stops)
331 | | | | | | | | Select delay
332 | | | | | | | | | RPS
333 | | | | | | | | | | Max number of tracks
334 | | | | | | | | | | | Interrupt timeout
335 | | | | | | | | | | | | Max nonintlv. sectors
336 | | | | | | | | | | | | | -Max Errors- flags */
337 {{0, 500, 16, 16, 8000, 1*HZ
, 3*HZ
, 0, SEL_DLY
, 5, 80, 3*HZ
, 20, {3,1,2,0,2}, 0,
338 0, { 7, 4, 8, 2, 1, 5, 3,10}, 3*HZ
/2, 0 }, "unknown" },
340 {{1, 300, 16, 16, 8000, 1*HZ
, 3*HZ
, 0, SEL_DLY
, 5, 40, 3*HZ
, 17, {3,1,2,0,2}, 0,
341 0, { 1, 0, 0, 0, 0, 0, 0, 0}, 3*HZ
/2, 1 }, "360K PC" }, /*5 1/4 360 KB PC*/
343 {{2, 500, 16, 16, 6000, 4*HZ
/10, 3*HZ
, 14, SEL_DLY
, 6, 83, 3*HZ
, 17, {3,1,2,0,2}, 0,
344 0, { 2, 5, 6,23,10,20,11, 0}, 3*HZ
/2, 2 }, "1.2M" }, /*5 1/4 HD AT*/
346 {{3, 250, 16, 16, 3000, 1*HZ
, 3*HZ
, 0, SEL_DLY
, 5, 83, 3*HZ
, 20, {3,1,2,0,2}, 0,
347 0, { 4,22,21,30, 3, 0, 0, 0}, 3*HZ
/2, 4 }, "720k" }, /*3 1/2 DD*/
349 {{4, 500, 16, 16, 4000, 4*HZ
/10, 3*HZ
, 10, SEL_DLY
, 5, 83, 3*HZ
, 20, {3,1,2,0,2}, 0,
350 0, { 7, 4,25,22,31,21,29,11}, 3*HZ
/2, 7 }, "1.44M" }, /*3 1/2 HD*/
352 {{5, 1000, 15, 8, 3000, 4*HZ
/10, 3*HZ
, 10, SEL_DLY
, 5, 83, 3*HZ
, 40, {3,1,2,0,2}, 0,
353 0, { 7, 8, 4,25,28,22,31,21}, 3*HZ
/2, 8 }, "2.88M AMI BIOS" }, /*3 1/2 ED*/
355 {{6, 1000, 15, 8, 3000, 4*HZ
/10, 3*HZ
, 10, SEL_DLY
, 5, 83, 3*HZ
, 40, {3,1,2,0,2}, 0,
356 0, { 7, 8, 4,25,28,22,31,21}, 3*HZ
/2, 8 }, "2.88M" } /*3 1/2 ED*/
357 /* | --autodetected formats--- | | |
358 * read_track | | Name printed when booting
360 * Frequency of disk change checks */
363 static struct floppy_drive_params drive_params
[N_DRIVE
];
364 static struct floppy_drive_struct drive_state
[N_DRIVE
];
365 static struct floppy_write_errors write_errors
[N_DRIVE
];
366 static struct floppy_raw_cmd
*raw_cmd
, default_raw_cmd
;
369 * This struct defines the different floppy types.
371 * Bit 0 of 'stretch' tells if the tracks need to be doubled for some
372 * types (e.g. 360kB diskette in 1.2MB drive, etc.). Bit 1 of 'stretch'
373 * tells if the disk is in Commodore 1581 format, which means side 0 sectors
374 * are located on side 1 of the disk but with a side 0 ID, and vice-versa.
375 * This is the same as the Sharp MZ-80 5.25" CP/M disk format, except that the
376 * 1581's logical side 0 is on physical side 1, whereas the Sharp's logical
377 * side 0 is on physical side 0 (but with the misnamed sector IDs).
378 * 'stretch' should probably be renamed to something more general, like
379 * 'options'. Other parameters should be self-explanatory (see also
382 static struct floppy_struct floppy_type
[32] = {
383 { 0, 0,0, 0,0,0x00,0x00,0x00,0x00,NULL
}, /* 0 no testing */
384 { 720, 9,2,40,0,0x2A,0x02,0xDF,0x50,"d360" }, /* 1 360KB PC */
385 { 2400,15,2,80,0,0x1B,0x00,0xDF,0x54,"h1200" }, /* 2 1.2MB AT */
386 { 720, 9,1,80,0,0x2A,0x02,0xDF,0x50,"D360" }, /* 3 360KB SS 3.5" */
387 { 1440, 9,2,80,0,0x2A,0x02,0xDF,0x50,"D720" }, /* 4 720KB 3.5" */
388 { 720, 9,2,40,1,0x23,0x01,0xDF,0x50,"h360" }, /* 5 360KB AT */
389 { 1440, 9,2,80,0,0x23,0x01,0xDF,0x50,"h720" }, /* 6 720KB AT */
390 { 2880,18,2,80,0,0x1B,0x00,0xCF,0x6C,"H1440" }, /* 7 1.44MB 3.5" */
391 { 5760,36,2,80,0,0x1B,0x43,0xAF,0x54,"E2880" }, /* 8 2.88MB 3.5" */
392 { 6240,39,2,80,0,0x1B,0x43,0xAF,0x28,"E3120"}, /* 9 3.12MB 3.5" */
394 { 2880,18,2,80,0,0x25,0x00,0xDF,0x02,"h1440" }, /* 10 1.44MB 5.25" */
395 { 3360,21,2,80,0,0x1C,0x00,0xCF,0x0C,"H1680" }, /* 11 1.68MB 3.5" */
396 { 820,10,2,41,1,0x25,0x01,0xDF,0x2E,"h410" }, /* 12 410KB 5.25" */
397 { 1640,10,2,82,0,0x25,0x02,0xDF,0x2E,"H820" }, /* 13 820KB 3.5" */
398 { 2952,18,2,82,0,0x25,0x00,0xDF,0x02,"h1476" }, /* 14 1.48MB 5.25" */
399 { 3444,21,2,82,0,0x25,0x00,0xDF,0x0C,"H1722" }, /* 15 1.72MB 3.5" */
400 { 840,10,2,42,1,0x25,0x01,0xDF,0x2E,"h420" }, /* 16 420KB 5.25" */
401 { 1660,10,2,83,0,0x25,0x02,0xDF,0x2E,"H830" }, /* 17 830KB 3.5" */
402 { 2988,18,2,83,0,0x25,0x00,0xDF,0x02,"h1494" }, /* 18 1.49MB 5.25" */
403 { 3486,21,2,83,0,0x25,0x00,0xDF,0x0C,"H1743" }, /* 19 1.74 MB 3.5" */
405 { 1760,11,2,80,0,0x1C,0x09,0xCF,0x00,"h880" }, /* 20 880KB 5.25" */
406 { 2080,13,2,80,0,0x1C,0x01,0xCF,0x00,"D1040" }, /* 21 1.04MB 3.5" */
407 { 2240,14,2,80,0,0x1C,0x19,0xCF,0x00,"D1120" }, /* 22 1.12MB 3.5" */
408 { 3200,20,2,80,0,0x1C,0x20,0xCF,0x2C,"h1600" }, /* 23 1.6MB 5.25" */
409 { 3520,22,2,80,0,0x1C,0x08,0xCF,0x2e,"H1760" }, /* 24 1.76MB 3.5" */
410 { 3840,24,2,80,0,0x1C,0x20,0xCF,0x00,"H1920" }, /* 25 1.92MB 3.5" */
411 { 6400,40,2,80,0,0x25,0x5B,0xCF,0x00,"E3200" }, /* 26 3.20MB 3.5" */
412 { 7040,44,2,80,0,0x25,0x5B,0xCF,0x00,"E3520" }, /* 27 3.52MB 3.5" */
413 { 7680,48,2,80,0,0x25,0x63,0xCF,0x00,"E3840" }, /* 28 3.84MB 3.5" */
415 { 3680,23,2,80,0,0x1C,0x10,0xCF,0x00,"H1840" }, /* 29 1.84MB 3.5" */
416 { 1600,10,2,80,0,0x25,0x02,0xDF,0x2E,"D800" }, /* 30 800KB 3.5" */
417 { 3200,20,2,80,0,0x1C,0x00,0xCF,0x2C,"H1600" }, /* 31 1.6MB 3.5" */
420 #define NUMBER(x) (sizeof(x) / sizeof(*(x)))
421 #define SECTSIZE (_FD_SECTSIZE(*floppy))
423 /* Auto-detection: Disk type used until the next media change occurs. */
424 static struct floppy_struct
*current_type
[N_DRIVE
] = {
425 NULL
, NULL
, NULL
, NULL
,
426 NULL
, NULL
, NULL
, NULL
430 * User-provided type information. current_type points to
431 * the respective entry of this array.
433 static struct floppy_struct user_params
[N_DRIVE
];
435 static int floppy_sizes
[256];
436 static int floppy_blocksizes
[256] = { 0, };
439 * The driver is trying to determine the correct media format
440 * while probing is set. rw_interrupt() clears it after a
443 static int probing
= 0;
445 /* Synchronization of FDC access. */
446 #define FD_COMMAND_NONE -1
447 #define FD_COMMAND_ERROR 2
448 #define FD_COMMAND_OKAY 3
450 static volatile int command_status
= FD_COMMAND_NONE
, fdc_busy
= 0;
451 static struct wait_queue
*fdc_wait
= NULL
, *command_done
= NULL
;
452 #define NO_SIGNAL (!interruptible || !signal_pending(current))
453 #define CALL(x) if ((x) == -EINTR) return -EINTR
454 #define ECALL(x) if ((ret = (x))) return ret;
455 #define _WAIT(x,i) CALL(ret=wait_til_done((x),i))
456 #define WAIT(x) _WAIT((x),interruptible)
457 #define IWAIT(x) _WAIT((x),1)
459 /* Errors during formatting are counted here. */
460 static int format_errors
;
462 /* Format request descriptor. */
463 static struct format_descr format_req
;
466 * Rate is 0 for 500kb/s, 1 for 300kbps, 2 for 250kbps
467 * Spec1 is 0xSH, where S is stepping rate (F=1ms, E=2ms, D=3ms etc),
468 * H is head unload time (1=16ms, 2=32ms, etc)
473 * Because these are written to by the DMA controller, they must
474 * not contain a 64k byte boundary crossing, or data will be
477 static char *floppy_track_buffer
=0;
478 static int max_buffer_sectors
=0;
481 typedef void (*done_f
)(int);
482 static struct cont_t
{
483 void (*interrupt
)(void); /* this is called after the interrupt of the
485 void (*redo
)(void); /* this is called to retry the operation */
486 void (*error
)(void); /* this is called to tally an error */
487 done_f done
; /* this is called to say if the operation has
488 * succeeded/failed */
491 static void floppy_ready(void);
492 static void floppy_start(void);
493 static void process_fd_request(void);
494 static void recalibrate_floppy(void);
495 static void floppy_shutdown(void);
497 static int floppy_grab_irq_and_dma(void);
498 static void floppy_release_irq_and_dma(void);
501 * The "reset" variable should be tested whenever an interrupt is scheduled,
502 * after the commands have been sent. This is to ensure that the driver doesn't
503 * get wedged when the interrupt doesn't come because of a failed command.
504 * reset doesn't need to be tested before sending commands, because
505 * output_byte is automatically disabled when reset is set.
507 #define CHECK_RESET { if (FDCS->reset){ reset_fdc(); return; } }
508 static void reset_fdc(void);
511 * These are global variables, as that's the easiest way to give
512 * information to interrupts. They are the data used for the current
516 #define NEED_1_RECAL -2
517 #define NEED_2_RECAL -3
520 static int usage_count
= 0;
523 /* buffer related variables */
524 static int buffer_track
= -1;
525 static int buffer_drive
= -1;
526 static int buffer_min
= -1;
527 static int buffer_max
= -1;
529 /* fdc related variables, should end up in a struct */
530 static struct floppy_fdc_state fdc_state
[N_FDC
];
531 static int fdc
; /* current fdc */
533 static struct floppy_struct
*_floppy
= floppy_type
;
534 static unsigned char current_drive
= 0;
535 static long current_count_sectors
= 0;
536 static unsigned char sector_t
; /* sector in track */
539 #define fd_eject(x) -EINVAL
544 static long unsigned debugtimer
;
551 static inline void set_debugt(void)
554 debugtimer
= jiffies
;
558 static inline void debugt(const char *message
)
561 if (DP
->flags
& DEBUGT
)
562 printk("%s dtime=%lu\n", message
, jiffies
-debugtimer
);
566 typedef void (*timeout_fn
)(unsigned long);
567 static struct timer_list fd_timeout
={ NULL
, NULL
, 0, 0,
568 (timeout_fn
) floppy_shutdown
};
570 static const char *timeout_message
;
572 #ifdef FLOPPY_SANITY_CHECK
573 static void is_alive(const char *message
)
575 /* this routine checks whether the floppy driver is "alive" */
576 if (fdc_busy
&& command_status
< 2 && !fd_timeout
.prev
){
577 DPRINT("timeout handler died: %s\n",message
);
582 #ifdef FLOPPY_SANITY_CHECK
586 static void (*lasthandler
)(void) = NULL
;
587 static int interruptjiffies
=0;
588 static int resultjiffies
=0;
589 static int resultsize
=0;
590 static int lastredo
=0;
592 static struct output_log
{
594 unsigned char status
;
595 unsigned long jiffies
;
596 } output_log
[OLOGSIZE
];
598 static int output_log_pos
=0;
602 #define MAXTIMEOUT -2
604 static void reschedule_timeout(int drive
, const char *message
, int marg
)
606 if (drive
== CURRENTD
)
607 drive
= current_drive
;
608 del_timer(&fd_timeout
);
609 if (drive
< 0 || drive
> N_DRIVE
) {
610 fd_timeout
.expires
= jiffies
+ 20*HZ
;
613 fd_timeout
.expires
= jiffies
+ UDP
->timeout
;
614 add_timer(&fd_timeout
);
615 if (UDP
->flags
& FD_DEBUG
){
616 DPRINT("reschedule timeout ");
617 printk(message
, marg
);
620 timeout_message
= message
;
623 static int maximum(int a
, int b
)
630 #define INFBOUND(a,b) (a)=maximum((a),(b));
632 static int minimum(int a
, int b
)
639 #define SUPBOUND(a,b) (a)=minimum((a),(b));
643 * Bottom half floppy driver.
644 * ==========================
646 * This part of the file contains the code talking directly to the hardware,
647 * and also the main service loop (seek-configure-spinup-command)
652 * This routine is responsible for maintaining the FD_DISK_CHANGE flag,
653 * and the last_checked date.
655 * last_checked is the date of the last check which showed 'no disk change'
656 * FD_DISK_CHANGE is set under two conditions:
657 * 1. The floppy has been changed after some i/o to that floppy already
659 * 2. No floppy disk is in the drive. This is done in order to ensure that
660 * requests are quickly flushed in case there is no disk in the drive. It
661 * follows that FD_DISK_CHANGE can only be cleared if there is a disk in
664 * For 1., maxblock is observed. Maxblock is 0 if no i/o has taken place yet.
665 * For 2., FD_DISK_NEWCHANGE is watched. FD_DISK_NEWCHANGE is cleared on
666 * each seek. If a disk is present, the disk change line should also be
667 * cleared on each seek. Thus, if FD_DISK_NEWCHANGE is clear, but the disk
668 * change line is set, this means either that no disk is in the drive, or
669 * that it has been removed since the last seek.
671 * This means that we really have a third possibility too:
672 * The floppy has been changed after the last seek.
675 static int disk_change(int drive
)
678 #ifdef FLOPPY_SANITY_CHECK
679 if (jiffies
- UDRS
->select_date
< UDP
->select_delay
)
680 DPRINT("WARNING disk change called early\n");
681 if (!(FDCS
->dor
& (0x10 << UNIT(drive
))) ||
682 (FDCS
->dor
& 3) != UNIT(drive
) ||
684 DPRINT("probing disk change on unselected drive\n");
685 DPRINT("drive=%d fdc=%d dor=%x\n",drive
, FDC(drive
),
691 if (UDP
->flags
& FD_DEBUG
){
692 DPRINT("checking disk change line for drive %d\n",drive
);
693 DPRINT("jiffies=%ld\n", jiffies
);
694 DPRINT("disk change line=%x\n",fd_inb(FD_DIR
)&0x80);
695 DPRINT("flags=%x\n",UDRS
->flags
);
698 if (UDP
->flags
& FD_BROKEN_DCL
)
699 return UTESTF(FD_DISK_CHANGED
);
700 if ((fd_inb(FD_DIR
) ^ UDP
->flags
) & 0x80){
701 USETF(FD_VERIFY
); /* verify write protection */
703 /* mark it changed */
704 USETF(FD_DISK_CHANGED
);
707 /* invalidate its geometry */
708 if (UDRS
->keep_data
>= 0) {
709 if ((UDP
->flags
& FTD_MSG
) &&
710 current_type
[drive
] != NULL
)
711 DPRINT("Disk type is undefined after "
713 current_type
[drive
] = NULL
;
714 floppy_sizes
[TOMINOR(drive
)] = MAX_DISK_SIZE
;
717 /*USETF(FD_DISK_NEWCHANGE);*/
720 UDRS
->last_checked
=jiffies
;
721 UCLEARF(FD_DISK_NEWCHANGE
);
726 static inline int is_selected(int dor
, int unit
)
728 return ((dor
& (0x10 << unit
)) && (dor
&3) == unit
);
731 static int set_dor(int fdc
, char mask
, char data
)
733 register unsigned char drive
, unit
, newdor
,olddor
;
735 if (FDCS
->address
== -1)
739 newdor
= (olddor
& mask
) | data
;
740 if (newdor
!= olddor
){
742 if (is_selected(olddor
, unit
) && !is_selected(newdor
,unit
)){
743 drive
= REVDRIVE(fdc
,unit
);
745 if (UDP
->flags
& FD_DEBUG
){
746 DPRINT("calling disk change from set_dor\n");
752 fd_outb(newdor
, FD_DOR
);
755 if (!is_selected(olddor
, unit
) && is_selected(newdor
,unit
)){
756 drive
= REVDRIVE(fdc
,unit
);
757 UDRS
->select_date
= jiffies
;
760 if (newdor
& FLOPPY_MOTOR_MASK
)
761 floppy_grab_irq_and_dma();
762 if (olddor
& FLOPPY_MOTOR_MASK
)
763 floppy_release_irq_and_dma();
767 static void twaddle(void)
769 if (DP
->select_delay
)
771 fd_outb(FDCS
->dor
& ~(0x10<<UNIT(current_drive
)),FD_DOR
);
772 fd_outb(FDCS
->dor
, FD_DOR
);
773 DRS
->select_date
= jiffies
;
776 /* reset all driver information about the current fdc. This is needed after
777 * a reset, and after a raw command. */
778 static void reset_fdc_info(int mode
)
782 FDCS
->spec1
= FDCS
->spec2
= -1;
783 FDCS
->need_configure
= 1;
786 for (drive
= 0; drive
< N_DRIVE
; drive
++)
787 if (FDC(drive
) == fdc
&&
788 (mode
|| UDRS
->track
!= NEED_1_RECAL
))
789 UDRS
->track
= NEED_2_RECAL
;
792 /* selects the fdc and drive, and enables the fdc's input/dma. */
793 static void set_fdc(int drive
)
795 if (drive
>= 0 && drive
< N_DRIVE
){
797 current_drive
= drive
;
799 if (fdc
!= 1 && fdc
!= 0) {
800 printk("bad fdc value\n");
805 set_dor(1-fdc
, ~8, 0);
807 if (FDCS
->rawcmd
== 2)
809 if (fd_inb(FD_STATUS
) != STATUS_READY
)
813 /* locks the driver */
814 static int lock_fdc(int drive
, int interruptible
)
819 printk("trying to lock fdc while usage count=0\n");
822 floppy_grab_irq_and_dma();
824 while (fdc_busy
&& NO_SIGNAL
)
825 interruptible_sleep_on(&fdc_wait
);
832 command_status
= FD_COMMAND_NONE
;
833 reschedule_timeout(drive
, "lock fdc", 0);
838 #define LOCK_FDC(drive,interruptible) \
839 if (lock_fdc(drive,interruptible)) return -EINTR;
842 /* unlocks the driver */
843 static inline void unlock_fdc(void)
847 DPRINT("FDC access conflict!\n");
850 DPRINT("device interrupt still active at FDC release: %p!\n",
852 command_status
= FD_COMMAND_NONE
;
853 del_timer(&fd_timeout
);
856 floppy_release_irq_and_dma();
860 /* switches the motor off after a given timeout */
861 static void motor_off_callback(unsigned long nr
)
863 unsigned char mask
= ~(0x10 << UNIT(nr
));
865 set_dor(FDC(nr
), mask
, 0);
868 static struct timer_list motor_off_timer
[N_DRIVE
] = {
869 { NULL
, NULL
, 0, 0, motor_off_callback
},
870 { NULL
, NULL
, 0, 1, motor_off_callback
},
871 { NULL
, NULL
, 0, 2, motor_off_callback
},
872 { NULL
, NULL
, 0, 3, motor_off_callback
},
873 { NULL
, NULL
, 0, 4, motor_off_callback
},
874 { NULL
, NULL
, 0, 5, motor_off_callback
},
875 { NULL
, NULL
, 0, 6, motor_off_callback
},
876 { NULL
, NULL
, 0, 7, motor_off_callback
}
879 /* schedules motor off */
880 static void floppy_off(unsigned int drive
)
882 unsigned long volatile delta
;
883 register int fdc
=FDC(drive
);
885 if (!(FDCS
->dor
& (0x10 << UNIT(drive
))))
888 del_timer(motor_off_timer
+drive
);
890 /* make spindle stop in a position which minimizes spinup time
893 delta
= jiffies
- UDRS
->first_read_date
+ HZ
-
894 UDP
->spindown_offset
;
895 delta
= ((delta
* UDP
->rps
) % HZ
) / UDP
->rps
;
896 motor_off_timer
[drive
].expires
= jiffies
+ UDP
->spindown
- delta
;
898 add_timer(motor_off_timer
+drive
);
902 * cycle through all N_DRIVE floppy drives, for disk change testing.
903 * stopping at current drive. This is done before any long operation, to
904 * be sure to have up to date disk change information.
906 static void scandrives(void)
908 int i
, drive
, saved_drive
;
910 if (DP
->select_delay
)
913 saved_drive
= current_drive
;
914 for (i
=0; i
< N_DRIVE
; i
++){
915 drive
= (saved_drive
+ i
+ 1) % N_DRIVE
;
916 if (UDRS
->fd_ref
== 0 || UDP
->select_delay
!= 0)
917 continue; /* skip closed drives */
919 if (!(set_dor(fdc
, ~3, UNIT(drive
) | (0x10 << UNIT(drive
))) &
920 (0x10 << UNIT(drive
))))
921 /* switch the motor off again, if it was off to
923 set_dor(fdc
, ~(0x10 << UNIT(drive
)), 0);
925 set_fdc(saved_drive
);
928 static void empty(void)
932 static struct tq_struct floppy_tq
=
935 static void schedule_bh( void (*handler
)(void*) )
937 floppy_tq
.routine
= (void *)(void *) handler
;
938 queue_task(&floppy_tq
, &tq_immediate
);
939 mark_bh(IMMEDIATE_BH
);
942 static struct timer_list fd_timer
={ NULL
, NULL
, 0, 0, 0 };
944 static void cancel_activity(void)
947 floppy_tq
.routine
= (void *)(void *) empty
;
948 del_timer(&fd_timer
);
951 /* this function makes sure that the disk stays in the drive during the
953 static void fd_watchdog(void)
956 if (DP
->flags
& FD_DEBUG
){
957 DPRINT("calling disk change from watchdog\n");
961 if (disk_change(current_drive
)){
962 DPRINT("disk removed during i/o\n");
967 del_timer(&fd_timer
);
968 fd_timer
.function
= (timeout_fn
) fd_watchdog
;
969 fd_timer
.expires
= jiffies
+ HZ
/ 10;
970 add_timer(&fd_timer
);
974 static void main_command_interrupt(void)
976 del_timer(&fd_timer
);
980 /* waits for a delay (spinup or select) to pass */
981 static int wait_for_completion(int delay
, timeout_fn function
)
984 reset_fdc(); /* do the reset during sleep to win time
985 * if we don't need to sleep, it's a good
986 * occasion anyways */
990 if ((signed) (jiffies
- delay
) < 0){
991 del_timer(&fd_timer
);
992 fd_timer
.function
= function
;
993 fd_timer
.expires
= delay
;
994 add_timer(&fd_timer
);
1000 static int hlt_disabled
=0;
1001 static void floppy_disable_hlt(void)
1003 unsigned long flags
;
1008 #ifdef HAVE_DISABLE_HLT
1015 static void floppy_enable_hlt(void)
1017 unsigned long flags
;
1022 #ifdef HAVE_DISABLE_HLT
1030 static void setup_DMA(void)
1032 unsigned long flags
;
1034 #ifdef FLOPPY_SANITY_CHECK
1035 if (raw_cmd
->length
== 0){
1038 printk("zero dma transfer size:");
1039 for (i
=0; i
< raw_cmd
->cmd_count
; i
++)
1040 printk("%x,", raw_cmd
->cmd
[i
]);
1046 if (((unsigned long) raw_cmd
->kernel_data
) % 512){
1047 printk("non aligned address: %p\n", raw_cmd
->kernel_data
);
1056 if(fd_dma_setup(raw_cmd
->kernel_data
, raw_cmd
->length
,
1057 (raw_cmd
->flags
& FD_RAW_READ
)?
1058 DMA_MODE_READ
: DMA_MODE_WRITE
,
1059 FDCS
->address
) < 0) {
1067 fd_cacheflush(raw_cmd
->kernel_data
, raw_cmd
->length
);
1068 fd_set_dma_mode((raw_cmd
->flags
& FD_RAW_READ
)?
1069 DMA_MODE_READ
: DMA_MODE_WRITE
);
1070 fd_set_dma_addr(raw_cmd
->kernel_data
);
1071 fd_set_dma_count(raw_cmd
->length
);
1072 virtual_dma_port
= FDCS
->address
;
1076 floppy_disable_hlt();
1079 static void show_floppy(void);
1081 /* waits until the fdc becomes ready */
1082 static int wait_til_ready(void)
1084 int counter
, status
;
1087 for (counter
= 0; counter
< 10000; counter
++) {
1088 status
= fd_inb(FD_STATUS
);
1089 if (status
& STATUS_READY
)
1092 if (!initialising
) {
1093 DPRINT("Getstatus times out (%x) on fdc %d\n",
1101 /* sends a command byte to the fdc */
1102 static int output_byte(char byte
)
1106 if ((status
= wait_til_ready()) < 0)
1108 if ((status
& (STATUS_READY
|STATUS_DIR
|STATUS_DMA
)) == STATUS_READY
){
1109 fd_outb(byte
,FD_DATA
);
1110 #ifdef FLOPPY_SANITY_CHECK
1111 output_log
[output_log_pos
].data
= byte
;
1112 output_log
[output_log_pos
].status
= status
;
1113 output_log
[output_log_pos
].jiffies
= jiffies
;
1114 output_log_pos
= (output_log_pos
+ 1) % OLOGSIZE
;
1119 if (!initialising
) {
1120 DPRINT("Unable to send byte %x to FDC. Fdc=%x Status=%x\n",
1126 #define LAST_OUT(x) if (output_byte(x)<0){ reset_fdc();return;}
1128 /* gets the response from the fdc */
1129 static int result(void)
1133 for(i
=0; i
< MAX_REPLIES
; i
++) {
1134 if ((status
= wait_til_ready()) < 0)
1136 status
&= STATUS_DIR
|STATUS_READY
|STATUS_BUSY
|STATUS_DMA
;
1137 if ((status
& ~STATUS_BUSY
) == STATUS_READY
){
1138 #ifdef FLOPPY_SANITY_CHECK
1139 resultjiffies
= jiffies
;
1144 if (status
== (STATUS_DIR
|STATUS_READY
|STATUS_BUSY
))
1145 reply_buffer
[i
] = fd_inb(FD_DATA
);
1150 DPRINT("get result error. Fdc=%d Last status=%x Read bytes=%d\n",
1158 #define MORE_OUTPUT -2
1159 /* does the fdc need more output? */
1160 static int need_more_output(void)
1163 if( (status
= wait_til_ready()) < 0)
1165 if ((status
& (STATUS_READY
|STATUS_DIR
|STATUS_DMA
)) == STATUS_READY
)
1170 /* Set perpendicular mode as required, based on data rate, if supported.
1171 * 82077 Now tested. 1Mbps data rate only possible with 82077-1.
1173 static inline void perpendicular_mode(void)
1175 unsigned char perp_mode
;
1177 if (raw_cmd
->rate
& 0x40){
1178 switch(raw_cmd
->rate
& 3){
1186 DPRINT("Invalid data rate for perpendicular mode!\n");
1188 FDCS
->reset
= 1; /* convenient way to return to
1189 * redo without to much hassle (deep
1196 if (FDCS
->perp_mode
== perp_mode
)
1198 if (FDCS
->version
>= FDC_82077_ORIG
) {
1199 output_byte(FD_PERPENDICULAR
);
1200 output_byte(perp_mode
);
1201 FDCS
->perp_mode
= perp_mode
;
1202 } else if (perp_mode
) {
1203 DPRINT("perpendicular mode not supported by this FDC.\n");
1205 } /* perpendicular_mode */
1207 static int fifo_depth
= 0xa;
1208 static int no_fifo
= 0;
1210 static int fdc_configure(void)
1213 output_byte(FD_CONFIGURE
);
1214 if(need_more_output() != MORE_OUTPUT
)
1217 output_byte(0x10 | (no_fifo
& 0x20) | (fifo_depth
& 0xf));
1218 output_byte(0); /* pre-compensation from track
1223 #define NOMINAL_DTR 500
1225 /* Issue a "SPECIFY" command to set the step rate time, head unload time,
1226 * head load time, and DMA disable flag to values needed by floppy.
1228 * The value "dtr" is the data transfer rate in Kbps. It is needed
1229 * to account for the data rate-based scaling done by the 82072 and 82077
1230 * FDC types. This parameter is ignored for other types of FDCs (i.e.
1233 * Note that changing the data transfer rate has a (probably deleterious)
1234 * effect on the parameters subject to scaling for 82072/82077 FDCs, so
1235 * fdc_specify is called again after each data transfer rate
1238 * srt: 1000 to 16000 in microseconds
1239 * hut: 16 to 240 milliseconds
1240 * hlt: 2 to 254 milliseconds
1242 * These values are rounded up to the next highest available delay time.
1244 static void fdc_specify(void)
1246 unsigned char spec1
, spec2
;
1248 unsigned long dtr
= NOMINAL_DTR
;
1249 unsigned long scale_dtr
= NOMINAL_DTR
;
1250 int hlt_max_code
= 0x7f;
1251 int hut_max_code
= 0xf;
1253 if (FDCS
->need_configure
&& FDCS
->version
>= FDC_82072A
) {
1255 FDCS
->need_configure
= 0;
1256 /*DPRINT("FIFO enabled\n");*/
1259 switch (raw_cmd
->rate
& 0x03) {
1265 if (FDCS
->version
>= FDC_82078
) {
1266 /* chose the default rate table, not the one
1267 * where 1 = 2 Mbps */
1268 output_byte(FD_DRIVESPEC
);
1269 if(need_more_output() == MORE_OUTPUT
) {
1270 output_byte(UNIT(current_drive
));
1280 if (FDCS
->version
>= FDC_82072
) {
1282 hlt_max_code
= 0x00; /* 0==256msec*dtr0/dtr (not linear!) */
1283 hut_max_code
= 0x0; /* 0==256msec*dtr0/dtr (not linear!) */
1286 /* Convert step rate from microseconds to milliseconds and 4 bits */
1287 srt
= 16 - (DP
->srt
*scale_dtr
/1000 + NOMINAL_DTR
- 1)/NOMINAL_DTR
;
1291 hlt
= (DP
->hlt
*scale_dtr
/2 + NOMINAL_DTR
- 1)/NOMINAL_DTR
;
1294 else if (hlt
> 0x7f)
1297 hut
= (DP
->hut
*scale_dtr
/16 + NOMINAL_DTR
- 1)/NOMINAL_DTR
;
1303 spec1
= (srt
<< 4) | hut
;
1304 spec2
= (hlt
<< 1) | (use_virtual_dma
& 1);
1306 /* If these parameters did not change, just return with success */
1307 if (FDCS
->spec1
!= spec1
|| FDCS
->spec2
!= spec2
) {
1308 /* Go ahead and set spec1 and spec2 */
1309 output_byte(FD_SPECIFY
);
1310 output_byte(FDCS
->spec1
= spec1
);
1311 output_byte(FDCS
->spec2
= spec2
);
1315 /* Set the FDC's data transfer rate on behalf of the specified drive.
1316 * NOTE: with 82072/82077 FDCs, changing the data rate requires a reissue
1317 * of the specify command (i.e. using the fdc_specify function).
1319 static int fdc_dtr(void)
1321 /* If data rate not already set to desired value, set it. */
1322 if ((raw_cmd
->rate
& 3) == FDCS
->dtr
)
1326 fd_outb(raw_cmd
->rate
& 3, FD_DCR
);
1328 /* TODO: some FDC/drive combinations (C&T 82C711 with TEAC 1.2MB)
1329 * need a stabilization period of several milliseconds to be
1330 * enforced after data rate changes before R/W operations.
1331 * Pause 5 msec to avoid trouble. (Needs to be 2 jiffies)
1333 FDCS
->dtr
= raw_cmd
->rate
& 3;
1334 return(wait_for_completion(jiffies
+2*HZ
/100,
1335 (timeout_fn
) floppy_ready
));
1338 static void tell_sector(void)
1340 printk(": track %d, head %d, sector %d, size %d",
1341 R_TRACK
, R_HEAD
, R_SECTOR
, R_SIZECODE
);
1346 * OK, this error interpreting routine is called after a
1347 * DMA read/write has succeeded
1348 * or failed, so we check the results, and copy any buffers.
1349 * hhb: Added better error reporting.
1350 * ak: Made this into a separate routine.
1352 static int interpret_errors(void)
1357 DPRINT("-- FDC reply error");
1362 /* check IC to find cause of interrupt */
1363 switch (ST0
& ST0_INTR
) {
1364 case 0x40: /* error occurred during command execution */
1366 return 0; /* occurs with pseudo-DMA */
1369 DPRINT("Drive is write protected\n");
1370 CLEARF(FD_DISK_WRITABLE
);
1373 } else if (ST1
& ST1_ND
) {
1374 SETF(FD_NEED_TWADDLE
);
1375 } else if (ST1
& ST1_OR
) {
1376 if (DP
->flags
& FTD_MSG
)
1377 DPRINT("Over/Underrun - retrying\n");
1379 }else if (*errors
>= DP
->max_errors
.reporting
){
1381 if (ST0
& ST0_ECE
) {
1382 printk("Recalibrate failed!");
1383 } else if (ST2
& ST2_CRC
) {
1384 printk("data CRC error");
1386 } else if (ST1
& ST1_CRC
) {
1387 printk("CRC error");
1389 } else if ((ST1
& (ST1_MAM
|ST1_ND
)) || (ST2
& ST2_MAM
)) {
1391 printk("sector not found");
1394 printk("probe failed...");
1395 } else if (ST2
& ST2_WC
) { /* seek error */
1396 printk("wrong cylinder");
1397 } else if (ST2
& ST2_BC
) { /* cylinder marked as bad */
1398 printk("bad cylinder");
1400 printk("unknown error. ST[0..2] are: 0x%x 0x%x 0x%x", ST0
, ST1
, ST2
);
1406 if (ST2
& ST2_WC
|| ST2
& ST2_BC
)
1407 /* wrong cylinder => recal */
1408 DRS
->track
= NEED_2_RECAL
;
1410 case 0x80: /* invalid command given */
1411 DPRINT("Invalid FDC command given!\n");
1415 DPRINT("Abnormal termination caused by polling\n");
1418 default: /* (0) Normal command termination */
1424 * This routine is called when everything should be correctly set up
1425 * for the transfer (i.e. floppy motor is on, the correct floppy is
1426 * selected, and the head is sitting on the right track).
1428 static void setup_rw_floppy(void)
1430 int i
,ready_date
,r
, flags
,dflags
;
1431 timeout_fn function
;
1433 flags
= raw_cmd
->flags
;
1434 if (flags
& (FD_RAW_READ
| FD_RAW_WRITE
))
1435 flags
|= FD_RAW_INTR
;
1437 if ((flags
& FD_RAW_SPIN
) && !(flags
& FD_RAW_NO_MOTOR
)){
1438 ready_date
= DRS
->spinup_date
+ DP
->spinup
;
1439 /* If spinup will take a long time, rerun scandrives
1440 * again just before spinup completion. Beware that
1441 * after scandrives, we must again wait for selection.
1443 if ((signed) (ready_date
- jiffies
) > DP
->select_delay
){
1444 ready_date
-= DP
->select_delay
;
1445 function
= (timeout_fn
) floppy_start
;
1447 function
= (timeout_fn
) setup_rw_floppy
;
1449 /* wait until the floppy is spinning fast enough */
1450 if (wait_for_completion(ready_date
,function
))
1453 dflags
= DRS
->flags
;
1455 if ((flags
& FD_RAW_READ
) || (flags
& FD_RAW_WRITE
))
1458 if (flags
& FD_RAW_INTR
)
1459 SET_INTR(main_command_interrupt
);
1462 for (i
=0; i
< raw_cmd
->cmd_count
; i
++)
1463 r
|=output_byte(raw_cmd
->cmd
[i
]);
1466 debugt("rw_command: ");
1474 if (!(flags
& FD_RAW_INTR
)){
1477 } else if (flags
& FD_RAW_NEED_DISK
)
1481 static int blind_seek
;
1484 * This is the routine called after every seek (or recalibrate) interrupt
1485 * from the floppy controller.
1487 static void seek_interrupt(void)
1490 debugt("seek interrupt:");
1492 if (inr
!= 2 || (ST0
& 0xF8) != 0x20) {
1493 DPRINT("seek failed\n");
1494 DRS
->track
= NEED_2_RECAL
;
1499 if (DRS
->track
>= 0 && DRS
->track
!= ST1
&& !blind_seek
){
1501 if (DP
->flags
& FD_DEBUG
){
1502 DPRINT("clearing NEWCHANGE flag because of effective seek\n");
1503 DPRINT("jiffies=%ld\n", jiffies
);
1506 CLEARF(FD_DISK_NEWCHANGE
); /* effective seek */
1507 DRS
->select_date
= jiffies
;
1513 static void check_wp(void)
1515 if (TESTF(FD_VERIFY
)) {
1516 /* check write protection */
1517 output_byte(FD_GETSTATUS
);
1518 output_byte(UNIT(current_drive
));
1524 CLEARF(FD_NEED_TWADDLE
);
1526 if (DP
->flags
& FD_DEBUG
){
1527 DPRINT("checking whether disk is write protected\n");
1528 DPRINT("wp=%x\n",ST3
& 0x40);
1532 SETF(FD_DISK_WRITABLE
);
1534 CLEARF(FD_DISK_WRITABLE
);
1538 static void seek_floppy(void)
1545 if (DP
->flags
& FD_DEBUG
){
1546 DPRINT("calling disk change from seek\n");
1550 if (!TESTF(FD_DISK_NEWCHANGE
) &&
1551 disk_change(current_drive
) &&
1552 (raw_cmd
->flags
& FD_RAW_NEED_DISK
)){
1553 /* the media changed flag should be cleared after the seek.
1554 * If it isn't, this means that there is really no disk in
1557 SETF(FD_DISK_CHANGED
);
1562 if (DRS
->track
<= NEED_1_RECAL
){
1563 recalibrate_floppy();
1565 } else if (TESTF(FD_DISK_NEWCHANGE
) &&
1566 (raw_cmd
->flags
& FD_RAW_NEED_DISK
) &&
1567 (DRS
->track
<= NO_TRACK
|| DRS
->track
== raw_cmd
->track
)) {
1568 /* we seek to clear the media-changed condition. Does anybody
1569 * know a more elegant way, which works on all drives? */
1571 track
= raw_cmd
->track
- 1;
1573 if (DP
->flags
& FD_SILENT_DCL_CLEAR
){
1574 set_dor(fdc
, ~(0x10 << UNIT(current_drive
)), 0);
1576 raw_cmd
->flags
|= FD_RAW_NEED_SEEK
;
1582 if (raw_cmd
->track
!= DRS
->track
&&
1583 (raw_cmd
->flags
& FD_RAW_NEED_SEEK
))
1584 track
= raw_cmd
->track
;
1591 SET_INTR(seek_interrupt
);
1592 output_byte(FD_SEEK
);
1593 output_byte(UNIT(current_drive
));
1596 debugt("seek command:");
1600 static void recal_interrupt(void)
1603 debugt("recal interrupt:");
1607 else if (ST0
& ST0_ECE
) {
1611 debugt("recal interrupt need 1 recal:");
1613 /* after a second recalibrate, we still haven't
1614 * reached track 0. Probably no drive. Raise an
1615 * error, as failing immediately might upset
1616 * computers possessed by the Devil :-) */
1622 debugt("recal interrupt need 2 recal:");
1624 /* If we already did a recalibrate,
1625 * and we are not at track 0, this
1626 * means we have moved. (The only way
1627 * not to move at recalibration is to
1628 * be already at track 0.) Clear the
1629 * new change flag */
1631 if (DP
->flags
& FD_DEBUG
){
1632 DPRINT("clearing NEWCHANGE flag because of second recalibrate\n");
1636 CLEARF(FD_DISK_NEWCHANGE
);
1637 DRS
->select_date
= jiffies
;
1641 debugt("recal interrupt default:");
1643 /* Recalibrate moves the head by at
1644 * most 80 steps. If after one
1645 * recalibrate we don't have reached
1646 * track 0, this might mean that we
1647 * started beyond track 80. Try
1649 DRS
->track
= NEED_1_RECAL
;
1657 static void print_result(char *message
, int inr
)
1661 DPRINT("%s ", message
);
1663 for (i
=0; i
<inr
; i
++)
1664 printk("repl[%d]=%x ", i
, reply_buffer
[i
]);
1668 /* interrupt handler. Note that this can be called externally on the Sparc */
1669 void floppy_interrupt(int irq
, void *dev_id
, struct pt_regs
* regs
)
1671 void (*handler
)(void) = DEVICE_INTR
;
1674 lasthandler
= handler
;
1675 interruptjiffies
= jiffies
;
1678 floppy_enable_hlt();
1680 if (fdc
>= N_FDC
|| FDCS
->address
== -1){
1681 /* we don't even know which FDC is the culprit */
1682 printk("DOR0=%x\n", fdc_state
[0].dor
);
1683 printk("floppy interrupt on bizarre fdc %d\n",fdc
);
1684 printk("handler=%p\n", handler
);
1685 is_alive("bizarre fdc");
1690 /* We have to clear the reset flag here, because apparently on boxes
1691 * with level triggered interrupts (PS/2, Sparc, ...), it is needed to
1692 * emit SENSEI's to clear the interrupt line. And FDCS->reset blocks the
1693 * emission of the SENSEI's.
1694 * It is OK to emit floppy commands because we are in an interrupt
1695 * handler here, and thus we have to fear no interference of other
1699 do_print
= !handler
&& print_unex
&& !initialising
;
1703 print_result("unexpected interrupt", inr
);
1706 output_byte(FD_SENSEI
);
1709 print_result("sensei", inr
);
1710 } while ((ST0
& 0x83) != UNIT(current_drive
) && inr
== 2);
1713 int cpu
= smp_processor_id();
1714 if(softirq_trylock(cpu
)) {
1715 /* got the lock, call the handler immediately */
1717 softirq_endlock(cpu
);
1719 /* we interrupted a bottom half. Defer handler */
1720 schedule_bh( (void *)(void *) handler
);
1723 is_alive("normal interrupt end");
1726 static void recalibrate_floppy(void)
1729 debugt("recalibrate floppy:");
1731 SET_INTR(recal_interrupt
);
1732 output_byte(FD_RECALIBRATE
);
1733 LAST_OUT(UNIT(current_drive
));
1737 * Must do 4 FD_SENSEIs after reset because of ``drive polling''.
1739 static void reset_interrupt(void)
1742 debugt("reset interrupt:");
1744 result(); /* get the status ready for set_fdc */
1746 printk("reset set in interrupt, calling %p\n", cont
->error
);
1747 cont
->error(); /* a reset just after a reset. BAD! */
1753 * reset is done by pulling bit 2 of DOR low for a while (old FDCs),
1754 * or by setting the self clearing bit 7 of STATUS (newer FDCs)
1756 static void reset_fdc(void)
1758 SET_INTR(reset_interrupt
);
1762 /* Pseudo-DMA may intercept 'reset finished' interrupt. */
1763 /* Irrelevant for systems with true DMA (i386). */
1766 if (FDCS
->version
>= FDC_82072A
)
1767 fd_outb(0x80 | (FDCS
->dtr
&3), FD_STATUS
);
1769 fd_outb(FDCS
->dor
& ~0x04, FD_DOR
);
1770 udelay(FD_RESET_DELAY
);
1771 fd_outb(FDCS
->dor
, FD_DOR
);
1775 static void show_floppy(void)
1780 printk("floppy driver state\n");
1781 printk("-------------------\n");
1782 printk("now=%ld last interrupt=%d last called handler=%p\n",
1783 jiffies
, interruptjiffies
, lasthandler
);
1786 #ifdef FLOPPY_SANITY_CHECK
1787 printk("timeout_message=%s\n", timeout_message
);
1788 printk("last output bytes:\n");
1789 for (i
=0; i
< OLOGSIZE
; i
++)
1790 printk("%2x %2x %ld\n",
1791 output_log
[(i
+output_log_pos
) % OLOGSIZE
].data
,
1792 output_log
[(i
+output_log_pos
) % OLOGSIZE
].status
,
1793 output_log
[(i
+output_log_pos
) % OLOGSIZE
].jiffies
);
1794 printk("last result at %d\n", resultjiffies
);
1795 printk("last redo_fd_request at %d\n", lastredo
);
1796 for (i
=0; i
<resultsize
; i
++){
1797 printk("%2x ", reply_buffer
[i
]);
1802 printk("status=%x\n", fd_inb(FD_STATUS
));
1803 printk("fdc_busy=%d\n", fdc_busy
);
1805 printk("DEVICE_INTR=%p\n", DEVICE_INTR
);
1807 printk("floppy_tq.routine=%p\n", floppy_tq
.routine
);
1809 printk("fd_timer.function=%p\n", fd_timer
.function
);
1810 if (fd_timeout
.prev
){
1811 printk("timer_table=%p\n",fd_timeout
.function
);
1812 printk("expires=%ld\n",fd_timeout
.expires
-jiffies
);
1813 printk("now=%ld\n",jiffies
);
1815 printk("cont=%p\n", cont
);
1816 printk("CURRENT=%p\n", CURRENT
);
1817 printk("command_status=%d\n", command_status
);
1821 static void floppy_shutdown(void)
1828 floppy_enable_hlt();
1830 /* avoid dma going to a random drive after shutdown */
1833 DPRINT("floppy timeout called\n");
1837 cont
->redo(); /* this will recall reset when needed */
1839 printk("no cont in shutdown!\n");
1840 process_fd_request();
1842 is_alive("floppy shutdown");
1844 /*typedef void (*timeout_fn)(unsigned long);*/
1846 /* start motor, check media-changed condition and write protection */
1847 static int start_motor(void (*function
)(void) )
1852 data
= UNIT(current_drive
);
1853 if (!(raw_cmd
->flags
& FD_RAW_NO_MOTOR
)){
1854 if (!(FDCS
->dor
& (0x10 << UNIT(current_drive
)))){
1856 /* no read since this drive is running */
1857 DRS
->first_read_date
= 0;
1858 /* note motor start time if motor is not yet running */
1859 DRS
->spinup_date
= jiffies
;
1860 data
|= (0x10 << UNIT(current_drive
));
1863 if (FDCS
->dor
& (0x10 << UNIT(current_drive
)))
1864 mask
&= ~(0x10 << UNIT(current_drive
));
1866 /* starts motor and selects floppy */
1867 del_timer(motor_off_timer
+ current_drive
);
1868 set_dor(fdc
, mask
, data
);
1870 /* wait_for_completion also schedules reset if needed. */
1871 return(wait_for_completion(DRS
->select_date
+DP
->select_delay
,
1872 (timeout_fn
) function
));
1875 static void floppy_ready(void)
1878 if (start_motor(floppy_ready
)) return;
1879 if (fdc_dtr()) return;
1882 if (DP
->flags
& FD_DEBUG
){
1883 DPRINT("calling disk change from floppy_ready\n");
1886 if (!(raw_cmd
->flags
& FD_RAW_NO_MOTOR
) &&
1887 disk_change(current_drive
) &&
1889 twaddle(); /* this clears the dcl on certain drive/controller
1892 #ifdef fd_chose_dma_mode
1893 if ((raw_cmd
->flags
& FD_RAW_READ
) ||
1894 (raw_cmd
->flags
& FD_RAW_WRITE
))
1895 fd_chose_dma_mode(raw_cmd
->kernel_data
,
1899 if (raw_cmd
->flags
& (FD_RAW_NEED_SEEK
| FD_RAW_NEED_DISK
)){
1900 perpendicular_mode();
1901 fdc_specify(); /* must be done here because of hut, hlt ... */
1904 if ((raw_cmd
->flags
& FD_RAW_READ
) ||
1905 (raw_cmd
->flags
& FD_RAW_WRITE
))
1911 static void floppy_start(void)
1913 reschedule_timeout(CURRENTD
, "floppy start", 0);
1917 if (DP
->flags
& FD_DEBUG
){
1918 DPRINT("setting NEWCHANGE in floppy_start\n");
1921 SETF(FD_DISK_NEWCHANGE
);
1926 * ========================================================================
1927 * here ends the bottom half. Exported routines are:
1928 * floppy_start, floppy_off, floppy_ready, lock_fdc, unlock_fdc, set_fdc,
1929 * start_motor, reset_fdc, reset_fdc_info, interpret_errors.
1930 * Initialization also uses output_byte, result, set_dor, floppy_interrupt
1932 * ========================================================================
1935 * General purpose continuations.
1936 * ==============================
1939 static void do_wakeup(void)
1941 reschedule_timeout(MAXTIMEOUT
, "do wakeup", 0);
1943 command_status
+= 2;
1944 wake_up(&command_done
);
1947 static struct cont_t wakeup_cont
={
1955 static struct cont_t intr_cont
={
1962 static int wait_til_done(void (*handler
)(void), int interruptible
)
1965 unsigned long flags
;
1967 schedule_bh((void *)(void *)handler
);
1969 while(command_status
< 2 && NO_SIGNAL
){
1970 is_alive("wait_til_done");
1972 interruptible_sleep_on(&command_done
);
1974 sleep_on(&command_done
);
1976 if (command_status
< 2){
1986 command_status
= FD_COMMAND_ERROR
;
1987 if (command_status
== FD_COMMAND_OKAY
)
1991 command_status
= FD_COMMAND_NONE
;
1995 static void generic_done(int result
)
1997 command_status
= result
;
1998 cont
= &wakeup_cont
;
2001 static void generic_success(void)
2006 static void generic_failure(void)
2011 static void success_and_wakeup(void)
2019 * formatting and rw support.
2020 * ==========================
2023 static int next_valid_format(void)
2027 probed_format
= DRS
->probed_format
;
2029 if (probed_format
>= 8 ||
2030 !DP
->autodetect
[probed_format
]){
2031 DRS
->probed_format
= 0;
2034 if (floppy_type
[DP
->autodetect
[probed_format
]].sect
){
2035 DRS
->probed_format
= probed_format
;
2042 static void bad_flp_intr(void)
2045 DRS
->probed_format
++;
2046 if (!next_valid_format())
2050 INFBOUND(DRWE
->badness
, *errors
);
2051 if (*errors
> DP
->max_errors
.abort
)
2053 if (*errors
> DP
->max_errors
.reset
)
2055 else if (*errors
> DP
->max_errors
.recal
)
2056 DRS
->track
= NEED_2_RECAL
;
2059 static void set_floppy(kdev_t device
)
2062 _floppy
= TYPE(device
) + floppy_type
;
2064 _floppy
= current_type
[ DRIVE(device
) ];
2068 * formatting support.
2069 * ===================
2071 static void format_interrupt(void)
2073 switch (interpret_errors()){
2084 #define CODE2SIZE (ssize = ((1 << SIZECODE) + 3) >> 2)
2085 #define FM_MODE(x,y) ((y) & ~(((x)->rate & 0x80) >>1))
2086 #define CT(x) ((x) | 0x40)
2087 static void setup_format_params(int track
)
2090 unsigned char track
,head
,sect
,size
;
2091 } *here
= (struct fparm
*)floppy_track_buffer
;
2093 int count
,head_shift
,track_shift
;
2095 raw_cmd
= &default_raw_cmd
;
2096 raw_cmd
->track
= track
;
2098 raw_cmd
->flags
= FD_RAW_WRITE
| FD_RAW_INTR
| FD_RAW_SPIN
|
2099 FD_RAW_NEED_DISK
| FD_RAW_NEED_SEEK
;
2100 raw_cmd
->rate
= _floppy
->rate
& 0x43;
2101 raw_cmd
->cmd_count
= NR_F
;
2102 COMMAND
= FM_MODE(_floppy
,FD_FORMAT
);
2103 DR_SELECT
= UNIT(current_drive
) + PH_HEAD(_floppy
,format_req
.head
);
2104 F_SIZECODE
= FD_SIZECODE(_floppy
);
2105 F_SECT_PER_TRACK
= _floppy
->sect
<< 2 >> F_SIZECODE
;
2106 F_GAP
= _floppy
->fmt_gap
;
2107 F_FILL
= FD_FILL_BYTE
;
2109 raw_cmd
->kernel_data
= floppy_track_buffer
;
2110 raw_cmd
->length
= 4 * F_SECT_PER_TRACK
;
2112 /* allow for about 30ms for data transport per track */
2113 head_shift
= (F_SECT_PER_TRACK
+ 5) / 6;
2115 /* a ``cylinder'' is two tracks plus a little stepping time */
2116 track_shift
= 2 * head_shift
+ 3;
2118 /* position of logical sector 1 on this track */
2119 n
= (track_shift
* format_req
.track
+ head_shift
* format_req
.head
)
2122 /* determine interleave */
2124 if (_floppy
->fmt_gap
< 0x22)
2127 /* initialize field */
2128 for (count
= 0; count
< F_SECT_PER_TRACK
; ++count
) {
2129 here
[count
].track
= format_req
.track
;
2130 here
[count
].head
= format_req
.head
;
2131 here
[count
].sect
= 0;
2132 here
[count
].size
= F_SIZECODE
;
2134 /* place logical sectors */
2135 for (count
= 1; count
<= F_SECT_PER_TRACK
; ++count
) {
2136 here
[n
].sect
= count
;
2137 n
= (n
+il
) % F_SECT_PER_TRACK
;
2138 if (here
[n
].sect
) { /* sector busy, find next free sector */
2140 if (n
>= F_SECT_PER_TRACK
) {
2141 n
-=F_SECT_PER_TRACK
;
2142 while (here
[n
].sect
) ++n
;
2148 static void redo_format(void)
2151 setup_format_params(format_req
.track
<< STRETCH(_floppy
));
2154 debugt("queue format request");
2158 static struct cont_t format_cont
={
2164 static int do_format(kdev_t device
, struct format_descr
*tmp_format_req
)
2167 int drive
=DRIVE(device
);
2172 _floppy
->track
> DP
->tracks
||
2173 tmp_format_req
->track
>= _floppy
->track
||
2174 tmp_format_req
->head
>= _floppy
->head
||
2175 (_floppy
->sect
<< 2) % (1 << FD_SIZECODE(_floppy
)) ||
2176 !_floppy
->fmt_gap
) {
2177 process_fd_request();
2180 format_req
= *tmp_format_req
;
2182 cont
= &format_cont
;
2183 errors
= &format_errors
;
2185 process_fd_request();
2190 * Buffer read/write and support
2191 * =============================
2194 /* new request_done. Can handle physical sectors which are smaller than a
2196 static void request_done(int uptodate
)
2201 reschedule_timeout(MAXTIMEOUT
, "request done %d", uptodate
);
2204 DPRINT("request list destroyed in floppy request done\n");
2209 /* maintain values for invalidation on geometry
2211 block
= current_count_sectors
+ CURRENT
->sector
;
2212 INFBOUND(DRS
->maxblock
, block
);
2213 if (block
> _floppy
->sect
)
2216 /* unlock chained buffers */
2217 while (current_count_sectors
&& CURRENT
&&
2218 current_count_sectors
>= CURRENT
->current_nr_sectors
){
2219 current_count_sectors
-= CURRENT
->current_nr_sectors
;
2220 CURRENT
->nr_sectors
-= CURRENT
->current_nr_sectors
;
2221 CURRENT
->sector
+= CURRENT
->current_nr_sectors
;
2224 if (current_count_sectors
&& CURRENT
){
2225 /* "unlock" last subsector */
2226 CURRENT
->buffer
+= current_count_sectors
<<9;
2227 CURRENT
->current_nr_sectors
-= current_count_sectors
;
2228 CURRENT
->nr_sectors
-= current_count_sectors
;
2229 CURRENT
->sector
+= current_count_sectors
;
2233 if (current_count_sectors
&& !CURRENT
)
2234 DPRINT("request list destroyed in floppy request done\n");
2237 if (CURRENT
->cmd
== WRITE
) {
2238 /* record write error information */
2239 DRWE
->write_errors
++;
2240 if (DRWE
->write_errors
== 1) {
2241 DRWE
->first_error_sector
= CURRENT
->sector
;
2242 DRWE
->first_error_generation
= DRS
->generation
;
2244 DRWE
->last_error_sector
= CURRENT
->sector
;
2245 DRWE
->last_error_generation
= DRS
->generation
;
2251 /* Interrupt handler evaluating the result of the r/w operation */
2252 static void rw_interrupt(void)
2254 int nr_sectors
, ssize
, eoc
;
2256 if (!DRS
->first_read_date
)
2257 DRS
->first_read_date
= jiffies
;
2266 nr_sectors
= ((R_TRACK
-TRACK
)*_floppy
->head
+R_HEAD
-HEAD
) *
2267 _floppy
->sect
+ ((R_SECTOR
-SECTOR
+eoc
) << SIZECODE
>> 2) -
2268 (sector_t
% _floppy
->sect
) % ssize
;
2270 #ifdef FLOPPY_SANITY_CHECK
2271 if (nr_sectors
> current_count_sectors
+ ssize
-
2272 (current_count_sectors
+ sector_t
) % ssize
+
2274 DPRINT("long rw: %x instead of %lx\n",
2275 nr_sectors
, current_count_sectors
);
2276 printk("rs=%d s=%d\n", R_SECTOR
, SECTOR
);
2277 printk("rh=%d h=%d\n", R_HEAD
, HEAD
);
2278 printk("rt=%d t=%d\n", R_TRACK
, TRACK
);
2279 printk("spt=%d st=%d ss=%d\n", SECT_PER_TRACK
,
2283 INFBOUND(nr_sectors
,0);
2284 SUPBOUND(current_count_sectors
, nr_sectors
);
2286 switch (interpret_errors()){
2291 if (!current_count_sectors
){
2298 if (!current_count_sectors
){
2302 current_type
[current_drive
] = _floppy
;
2303 floppy_sizes
[TOMINOR(current_drive
) ]=
2304 (_floppy
->size
+1)>>1;
2309 if (DP
->flags
& FTD_MSG
)
2310 DPRINT("Auto-detected floppy type %s in fd%d\n",
2311 _floppy
->name
,current_drive
);
2312 current_type
[current_drive
] = _floppy
;
2313 floppy_sizes
[TOMINOR(current_drive
)] = (_floppy
->size
+1) >> 1;
2317 if (CT(COMMAND
) != FD_READ
||
2318 raw_cmd
->kernel_data
== CURRENT
->buffer
){
2319 /* transfer directly from buffer */
2321 } else if (CT(COMMAND
) == FD_READ
){
2322 buffer_track
= raw_cmd
->track
;
2323 buffer_drive
= current_drive
;
2324 INFBOUND(buffer_max
, nr_sectors
+ sector_t
);
2329 /* Compute maximal contiguous buffer size. */
2330 static int buffer_chain_size(void)
2332 struct buffer_head
*bh
;
2336 base
= CURRENT
->buffer
;
2337 size
= CURRENT
->current_nr_sectors
<< 9;
2342 while (bh
&& bh
->b_data
== base
+ size
){
2350 /* Compute the maximal transfer size */
2351 static int transfer_size(int ssize
, int max_sector
, int max_size
)
2353 SUPBOUND(max_sector
, sector_t
+ max_size
);
2356 max_sector
-= (max_sector
% _floppy
->sect
) % ssize
;
2358 /* transfer size, beginning not aligned */
2359 current_count_sectors
= max_sector
- sector_t
;
2365 * Move data from/to the track buffer to/from the buffer cache.
2367 static void copy_buffer(int ssize
, int max_sector
, int max_sector_2
)
2369 int remaining
; /* number of transferred 512-byte sectors */
2370 struct buffer_head
*bh
;
2371 char *buffer
, *dma_buffer
;
2374 max_sector
= transfer_size(ssize
,
2375 minimum(max_sector
, max_sector_2
),
2376 CURRENT
->nr_sectors
);
2378 if (current_count_sectors
<= 0 && CT(COMMAND
) == FD_WRITE
&&
2379 buffer_max
> sector_t
+ CURRENT
->nr_sectors
)
2380 current_count_sectors
= minimum(buffer_max
- sector_t
,
2381 CURRENT
->nr_sectors
);
2383 remaining
= current_count_sectors
<< 9;
2384 #ifdef FLOPPY_SANITY_CHECK
2385 if ((remaining
>> 9) > CURRENT
->nr_sectors
&&
2386 CT(COMMAND
) == FD_WRITE
){
2387 DPRINT("in copy buffer\n");
2388 printk("current_count_sectors=%ld\n", current_count_sectors
);
2389 printk("remaining=%d\n", remaining
>> 9);
2390 printk("CURRENT->nr_sectors=%ld\n",CURRENT
->nr_sectors
);
2391 printk("CURRENT->current_nr_sectors=%ld\n",
2392 CURRENT
->current_nr_sectors
);
2393 printk("max_sector=%d\n", max_sector
);
2394 printk("ssize=%d\n", ssize
);
2398 buffer_max
= maximum(max_sector
, buffer_max
);
2400 dma_buffer
= floppy_track_buffer
+ ((sector_t
- buffer_min
) << 9);
2403 size
= CURRENT
->current_nr_sectors
<< 9;
2404 buffer
= CURRENT
->buffer
;
2406 while (remaining
> 0){
2407 SUPBOUND(size
, remaining
);
2408 #ifdef FLOPPY_SANITY_CHECK
2409 if (dma_buffer
+ size
>
2410 floppy_track_buffer
+ (max_buffer_sectors
<< 10) ||
2411 dma_buffer
< floppy_track_buffer
){
2412 DPRINT("buffer overrun in copy buffer %d\n",
2413 (int) ((floppy_track_buffer
- dma_buffer
) >>9));
2414 printk("sector_t=%d buffer_min=%d\n",
2415 sector_t
, buffer_min
);
2416 printk("current_count_sectors=%ld\n",
2417 current_count_sectors
);
2418 if (CT(COMMAND
) == FD_READ
)
2420 if (CT(COMMAND
) == FD_READ
)
2424 if (((unsigned long)buffer
) % 512)
2425 DPRINT("%p buffer not aligned\n", buffer
);
2427 if (CT(COMMAND
) == FD_READ
)
2428 memcpy(buffer
, dma_buffer
, size
);
2430 memcpy(dma_buffer
, buffer
, size
);
2437 #ifdef FLOPPY_SANITY_CHECK
2439 DPRINT("bh=null in copy buffer after copy\n");
2444 buffer
= bh
->b_data
;
2446 #ifdef FLOPPY_SANITY_CHECK
2449 max_sector
-= remaining
>> 9;
2450 DPRINT("weirdness: remaining %d\n", remaining
>>9);
2456 static inline int check_dma_crossing(char *start
,
2457 unsigned long length
, char *message
)
2459 if (CROSS_64KB(start
, length
)) {
2460 printk("DMA xfer crosses 64KB boundary in %s %p-%p\n",
2461 message
, start
, start
+length
);
2469 * Formulate a read/write request.
2470 * this routine decides where to load the data (directly to buffer, or to
2471 * tmp floppy area), how much data to load (the size of the buffer, the whole
2472 * track, or a single sector)
2473 * All floppy_track_buffer handling goes in here. If we ever add track buffer
2474 * allocation on the fly, it should be done here. No other part should need
2478 static int make_raw_rw_request(void)
2480 int aligned_sector_t
;
2481 int max_sector
, max_size
, tracksize
, ssize
;
2483 set_fdc(DRIVE(CURRENT
->rq_dev
));
2485 raw_cmd
= &default_raw_cmd
;
2486 raw_cmd
->flags
= FD_RAW_SPIN
| FD_RAW_NEED_DISK
| FD_RAW_NEED_DISK
|
2488 raw_cmd
->cmd_count
= NR_RW
;
2489 if (CURRENT
->cmd
== READ
){
2490 raw_cmd
->flags
|= FD_RAW_READ
;
2491 COMMAND
= FM_MODE(_floppy
,FD_READ
);
2492 } else if (CURRENT
->cmd
== WRITE
){
2493 raw_cmd
->flags
|= FD_RAW_WRITE
;
2494 COMMAND
= FM_MODE(_floppy
,FD_WRITE
);
2496 DPRINT("make_raw_rw_request: unknown command\n");
2500 max_sector
= _floppy
->sect
* _floppy
->head
;
2502 TRACK
= CURRENT
->sector
/ max_sector
;
2503 sector_t
= CURRENT
->sector
% max_sector
;
2504 if (_floppy
->track
&& TRACK
>= _floppy
->track
) {
2505 if(CURRENT
->current_nr_sectors
& 1) {
2506 current_count_sectors
= 1;
2511 HEAD
= sector_t
/ _floppy
->sect
;
2513 if (((_floppy
->stretch
& FD_SWAPSIDES
) || TESTF(FD_NEED_TWADDLE
)) &&
2514 sector_t
< _floppy
->sect
)
2515 max_sector
= _floppy
->sect
;
2517 /* 2M disks have phantom sectors on the first track */
2518 if ((_floppy
->rate
& FD_2M
) && (!TRACK
) && (!HEAD
)){
2519 max_sector
= 2 * _floppy
->sect
/ 3;
2520 if (sector_t
>= max_sector
){
2521 current_count_sectors
= minimum(_floppy
->sect
- sector_t
,
2522 CURRENT
->nr_sectors
);
2527 SIZECODE
= FD_SIZECODE(_floppy
);
2528 raw_cmd
->rate
= _floppy
->rate
& 0x43;
2529 if ((_floppy
->rate
& FD_2M
) &&
2538 raw_cmd
->track
= TRACK
<< STRETCH(_floppy
);
2539 DR_SELECT
= UNIT(current_drive
) + PH_HEAD(_floppy
,HEAD
);
2542 SECT_PER_TRACK
= _floppy
->sect
<< 2 >> SIZECODE
;
2543 SECTOR
= ((sector_t
% _floppy
->sect
) << 2 >> SIZECODE
) + 1;
2544 tracksize
= _floppy
->sect
- _floppy
->sect
% ssize
;
2545 if (tracksize
< _floppy
->sect
){
2547 if (tracksize
<= sector_t
% _floppy
->sect
)
2549 while (tracksize
<= sector_t
% _floppy
->sect
){
2550 while(tracksize
+ ssize
> _floppy
->sect
){
2554 SECTOR
++; SECT_PER_TRACK
++;
2557 max_sector
= HEAD
* _floppy
->sect
+ tracksize
;
2558 } else if (!TRACK
&& !HEAD
&& !(_floppy
->rate
& FD_2M
) && probing
)
2559 max_sector
= _floppy
->sect
;
2561 aligned_sector_t
= sector_t
- (sector_t
% _floppy
->sect
) % ssize
;
2562 max_size
= CURRENT
->nr_sectors
;
2563 if ((raw_cmd
->track
== buffer_track
) &&
2564 (current_drive
== buffer_drive
) &&
2565 (sector_t
>= buffer_min
) && (sector_t
< buffer_max
)) {
2566 /* data already in track buffer */
2567 if (CT(COMMAND
) == FD_READ
) {
2568 copy_buffer(1, max_sector
, buffer_max
);
2571 } else if (aligned_sector_t
!= sector_t
|| CURRENT
->nr_sectors
< ssize
){
2572 if (CT(COMMAND
) == FD_WRITE
){
2573 if (sector_t
+ CURRENT
->nr_sectors
> ssize
&&
2574 sector_t
+ CURRENT
->nr_sectors
< ssize
+ ssize
)
2575 max_size
= ssize
+ ssize
;
2579 raw_cmd
->flags
&= ~FD_RAW_WRITE
;
2580 raw_cmd
->flags
|= FD_RAW_READ
;
2581 COMMAND
= FM_MODE(_floppy
,FD_READ
);
2582 } else if ((unsigned long)CURRENT
->buffer
< MAX_DMA_ADDRESS
) {
2583 unsigned long dma_limit
;
2584 int direct
, indirect
;
2586 indirect
= transfer_size(ssize
,max_sector
,max_buffer_sectors
*2) -
2590 * Do NOT use minimum() here---MAX_DMA_ADDRESS is 64 bits wide
2591 * on a 64 bit machine!
2593 max_size
= buffer_chain_size();
2594 dma_limit
= (MAX_DMA_ADDRESS
- ((unsigned long) CURRENT
->buffer
)) >> 9;
2595 if ((unsigned long) max_size
> dma_limit
) {
2596 max_size
= dma_limit
;
2598 /* 64 kb boundaries */
2599 if (CROSS_64KB(CURRENT
->buffer
, max_size
<< 9))
2601 ((unsigned long)CURRENT
->buffer
) % K_64
)>>9;
2602 direct
= transfer_size(ssize
,max_sector
,max_size
) - sector_t
;
2604 * We try to read tracks, but if we get too many errors, we
2605 * go back to reading just one sector at a time.
2607 * This means we should be able to read a sector even if there
2608 * are other bad sectors on this track.
2611 (indirect
* 2 > direct
* 3 &&
2612 *errors
< DP
->max_errors
.read_track
&&
2613 /*!TESTF(FD_NEED_TWADDLE) &&*/
2614 ((!probing
|| (DP
->read_track
&(1<<DRS
->probed_format
)))))){
2615 max_size
= CURRENT
->nr_sectors
;
2617 raw_cmd
->kernel_data
= CURRENT
->buffer
;
2618 raw_cmd
->length
= current_count_sectors
<< 9;
2619 if (raw_cmd
->length
== 0){
2620 DPRINT("zero dma transfer attempted from make_raw_request\n");
2621 DPRINT("indirect=%d direct=%d sector_t=%d",
2622 indirect
, direct
, sector_t
);
2625 /* check_dma_crossing(raw_cmd->kernel_data,
2627 "end of make_raw_request [1]");*/
2632 if (CT(COMMAND
) == FD_READ
)
2633 max_size
= max_sector
; /* unbounded */
2635 /* claim buffer track if needed */
2636 if (buffer_track
!= raw_cmd
->track
|| /* bad track */
2637 buffer_drive
!=current_drive
|| /* bad drive */
2638 sector_t
> buffer_max
||
2639 sector_t
< buffer_min
||
2640 ((CT(COMMAND
) == FD_READ
||
2641 (aligned_sector_t
== sector_t
&& CURRENT
->nr_sectors
>= ssize
))&&
2642 max_sector
> 2 * max_buffer_sectors
+ buffer_min
&&
2643 max_size
+ sector_t
> 2 * max_buffer_sectors
+ buffer_min
)
2644 /* not enough space */){
2646 buffer_drive
= current_drive
;
2647 buffer_max
= buffer_min
= aligned_sector_t
;
2649 raw_cmd
->kernel_data
= floppy_track_buffer
+
2650 ((aligned_sector_t
-buffer_min
)<<9);
2652 if (CT(COMMAND
) == FD_WRITE
){
2653 /* copy write buffer to track buffer.
2654 * if we get here, we know that the write
2655 * is either aligned or the data already in the buffer
2656 * (buffer will be overwritten) */
2657 #ifdef FLOPPY_SANITY_CHECK
2658 if (sector_t
!= aligned_sector_t
&& buffer_track
== -1)
2659 DPRINT("internal error offset !=0 on write\n");
2661 buffer_track
= raw_cmd
->track
;
2662 buffer_drive
= current_drive
;
2663 copy_buffer(ssize
, max_sector
, 2*max_buffer_sectors
+buffer_min
);
2665 transfer_size(ssize
, max_sector
,
2666 2*max_buffer_sectors
+buffer_min
-aligned_sector_t
);
2668 /* round up current_count_sectors to get dma xfer size */
2669 raw_cmd
->length
= sector_t
+current_count_sectors
-aligned_sector_t
;
2670 raw_cmd
->length
= ((raw_cmd
->length
-1)|(ssize
-1))+1;
2671 raw_cmd
->length
<<= 9;
2672 #ifdef FLOPPY_SANITY_CHECK
2673 /*check_dma_crossing(raw_cmd->kernel_data, raw_cmd->length,
2674 "end of make_raw_request");*/
2675 if ((raw_cmd
->length
< current_count_sectors
<< 9) ||
2676 (raw_cmd
->kernel_data
!= CURRENT
->buffer
&&
2677 CT(COMMAND
) == FD_WRITE
&&
2678 (aligned_sector_t
+ (raw_cmd
->length
>> 9) > buffer_max
||
2679 aligned_sector_t
< buffer_min
)) ||
2680 raw_cmd
->length
% (128 << SIZECODE
) ||
2681 raw_cmd
->length
<= 0 || current_count_sectors
<= 0){
2682 DPRINT("fractionary current count b=%lx s=%lx\n",
2683 raw_cmd
->length
, current_count_sectors
);
2684 if (raw_cmd
->kernel_data
!= CURRENT
->buffer
)
2685 printk("addr=%d, length=%ld\n",
2686 (int) ((raw_cmd
->kernel_data
-
2687 floppy_track_buffer
) >> 9),
2688 current_count_sectors
);
2689 printk("st=%d ast=%d mse=%d msi=%d\n",
2690 sector_t
, aligned_sector_t
, max_sector
, max_size
);
2691 printk("ssize=%x SIZECODE=%d\n", ssize
, SIZECODE
);
2692 printk("command=%x SECTOR=%d HEAD=%d, TRACK=%d\n",
2693 COMMAND
, SECTOR
, HEAD
, TRACK
);
2694 printk("buffer drive=%d\n", buffer_drive
);
2695 printk("buffer track=%d\n", buffer_track
);
2696 printk("buffer_min=%d\n", buffer_min
);
2697 printk("buffer_max=%d\n", buffer_max
);
2701 if (raw_cmd
->kernel_data
!= CURRENT
->buffer
){
2702 if (raw_cmd
->kernel_data
< floppy_track_buffer
||
2703 current_count_sectors
< 0 ||
2704 raw_cmd
->length
< 0 ||
2705 raw_cmd
->kernel_data
+ raw_cmd
->length
>
2706 floppy_track_buffer
+ (max_buffer_sectors
<< 10)){
2707 DPRINT("buffer overrun in schedule dma\n");
2708 printk("sector_t=%d buffer_min=%d current_count=%ld\n",
2709 sector_t
, buffer_min
,
2710 raw_cmd
->length
>> 9);
2711 printk("current_count_sectors=%ld\n",
2712 current_count_sectors
);
2713 if (CT(COMMAND
) == FD_READ
)
2715 if (CT(COMMAND
) == FD_READ
)
2719 } else if (raw_cmd
->length
> CURRENT
->nr_sectors
<< 9 ||
2720 current_count_sectors
> CURRENT
->nr_sectors
){
2721 DPRINT("buffer overrun in direct transfer\n");
2723 } else if (raw_cmd
->length
< current_count_sectors
<< 9){
2724 DPRINT("more sectors than bytes\n");
2725 printk("bytes=%ld\n", raw_cmd
->length
>> 9);
2726 printk("sectors=%ld\n", current_count_sectors
);
2728 if (raw_cmd
->length
== 0){
2729 DPRINT("zero dma transfer attempted from make_raw_request\n");
2736 static void redo_fd_request(void)
2738 #define REPEAT {request_done(0); continue; }
2743 if (current_drive
< N_DRIVE
)
2744 floppy_off(current_drive
);
2746 if (CURRENT
&& CURRENT
->rq_status
== RQ_INACTIVE
){
2758 if (MAJOR(CURRENT
->rq_dev
) != MAJOR_NR
)
2759 panic(DEVICE_NAME
": request list destroyed");
2760 if (CURRENT
->bh
&& !buffer_locked(CURRENT
->bh
))
2761 panic(DEVICE_NAME
": block not locked");
2763 device
= CURRENT
->rq_dev
;
2764 set_fdc(DRIVE(device
));
2765 reschedule_timeout(CURRENTD
, "redo fd request", 0);
2768 raw_cmd
= & default_raw_cmd
;
2770 if (start_motor(redo_fd_request
)) return;
2771 disk_change(current_drive
);
2772 if (test_bit(current_drive
, &fake_change
) ||
2773 TESTF(FD_DISK_CHANGED
)){
2774 DPRINT("disk absent or changed during operation\n");
2777 if (!_floppy
) { /* Autodetection */
2779 DRS
->probed_format
= 0;
2780 if (next_valid_format()){
2781 DPRINT("no autodetectable formats\n");
2787 _floppy
= floppy_type
+DP
->autodetect
[DRS
->probed_format
];
2790 errors
= & (CURRENT
->errors
);
2791 tmp
= make_raw_rw_request();
2797 if (TESTF(FD_NEED_TWADDLE
))
2799 schedule_bh( (void *)(void *) floppy_start
);
2801 debugt("queue fd request");
2808 static struct cont_t rw_cont
={
2814 static void process_fd_request(void)
2817 schedule_bh( (void *)(void *) redo_fd_request
);
2820 static void do_fd_request(void)
2822 if(usage_count
== 0) {
2823 printk("warning: usage count=0, CURRENT=%p exiting\n", CURRENT
);
2824 printk("sect=%ld cmd=%d\n", CURRENT
->sector
, CURRENT
->cmd
);
2829 /* fdc busy, this new request will be treated when the
2830 current one is done */
2831 is_alive("do fd request, old request running");
2834 lock_fdc(MAXTIMEOUT
,0);
2835 process_fd_request();
2836 is_alive("do fd request");
2839 static struct cont_t poll_cont
={
2845 static int poll_drive(int interruptible
, int flag
)
2848 /* no auto-sense, just clear dcl */
2849 raw_cmd
= &default_raw_cmd
;
2850 raw_cmd
->flags
= flag
;
2852 raw_cmd
->cmd_count
=0;
2855 if (DP
->flags
& FD_DEBUG
){
2856 DPRINT("setting NEWCHANGE in poll_drive\n");
2859 SETF(FD_DISK_NEWCHANGE
);
2865 * User triggered reset
2866 * ====================
2869 static void reset_intr(void)
2871 printk("weird, reset interrupt called\n");
2874 static struct cont_t reset_cont
={
2880 static int user_reset_fdc(int drive
, int arg
, int interruptible
)
2885 LOCK_FDC(drive
,interruptible
);
2886 if (arg
== FD_RESET_ALWAYS
)
2892 process_fd_request();
2897 * Misc Ioctl's and support
2898 * ========================
2900 static inline int fd_copyout(void *param
, const void *address
, int size
)
2902 return copy_to_user(param
,address
, size
) ? -EFAULT
: 0;
2905 static inline int fd_copyin(void *param
, void *address
, int size
)
2907 return copy_from_user(address
, param
, size
) ? -EFAULT
: 0;
2910 #define _COPYOUT(x) (copy_to_user((void *)param, &(x), sizeof(x)) ? -EFAULT : 0)
2911 #define _COPYIN(x) (copy_from_user(&(x), (void *)param, sizeof(x)) ? -EFAULT : 0)
2913 #define COPYOUT(x) ECALL(_COPYOUT(x))
2914 #define COPYIN(x) ECALL(_COPYIN(x))
2916 static inline const char *drive_name(int type
, int drive
)
2918 struct floppy_struct
*floppy
;
2921 floppy
= floppy_type
+ type
;
2923 if (UDP
->native_format
)
2924 floppy
= floppy_type
+ UDP
->native_format
;
2929 return floppy
->name
;
2936 static void raw_cmd_done(int flag
)
2941 raw_cmd
->flags
|= FD_RAW_FAILURE
;
2942 raw_cmd
->flags
|= FD_RAW_HARDFAILURE
;
2944 raw_cmd
->reply_count
= inr
;
2945 if(raw_cmd
->reply_count
> MAX_REPLIES
)
2946 raw_cmd
->reply_count
=0;
2947 for (i
=0; i
< raw_cmd
->reply_count
; i
++)
2948 raw_cmd
->reply
[i
] = reply_buffer
[i
];
2950 if (raw_cmd
->flags
& (FD_RAW_READ
| FD_RAW_WRITE
))
2951 raw_cmd
->length
= fd_get_dma_residue();
2953 if ((raw_cmd
->flags
& FD_RAW_SOFTFAILURE
) &&
2954 (!raw_cmd
->reply_count
|| (raw_cmd
->reply
[0] & 0xc0)))
2955 raw_cmd
->flags
|= FD_RAW_FAILURE
;
2957 if (disk_change(current_drive
))
2958 raw_cmd
->flags
|= FD_RAW_DISK_CHANGE
;
2960 raw_cmd
->flags
&= ~FD_RAW_DISK_CHANGE
;
2961 if (raw_cmd
->flags
& FD_RAW_NO_MOTOR_AFTER
)
2962 motor_off_callback(current_drive
);
2964 if (raw_cmd
->next
&&
2965 (!(raw_cmd
->flags
& FD_RAW_FAILURE
) ||
2966 !(raw_cmd
->flags
& FD_RAW_STOP_IF_FAILURE
)) &&
2967 ((raw_cmd
->flags
& FD_RAW_FAILURE
) ||
2968 !(raw_cmd
->flags
&FD_RAW_STOP_IF_SUCCESS
))) {
2969 raw_cmd
= raw_cmd
->next
;
2977 static struct cont_t raw_cmd_cont
={
2984 static inline int raw_cmd_copyout(int cmd
, char *param
,
2985 struct floppy_raw_cmd
*ptr
)
2991 param
+= sizeof(struct floppy_raw_cmd
);
2992 if ((ptr
->flags
& FD_RAW_READ
) && ptr
->buffer_length
){
2993 if (ptr
->length
>=0 && ptr
->length
<=ptr
->buffer_length
)
2994 ECALL(fd_copyout(ptr
->data
,
2996 ptr
->buffer_length
-
3005 static void raw_cmd_free(struct floppy_raw_cmd
**ptr
)
3007 struct floppy_raw_cmd
*next
,*this;
3012 if (this->buffer_length
) {
3013 fd_dma_mem_free((unsigned long)this->kernel_data
,
3014 this->buffer_length
);
3015 this->buffer_length
= 0;
3024 static inline int raw_cmd_copyin(int cmd
, char *param
,
3025 struct floppy_raw_cmd
**rcmd
)
3027 struct floppy_raw_cmd
*ptr
;
3033 ptr
= (struct floppy_raw_cmd
*)
3034 kmalloc(sizeof(struct floppy_raw_cmd
), GFP_USER
);
3040 ptr
->buffer_length
= 0;
3041 param
+= sizeof(struct floppy_raw_cmd
);
3042 if (ptr
->cmd_count
> 33)
3043 /* the command may now also take up the space
3044 * initially intended for the reply & the
3045 * reply count. Needed for long 82078 commands
3046 * such as RESTORE, which takes ... 17 command
3047 * bytes. Murphy's law #137: When you reserve
3048 * 16 bytes for a structure, you'll one day
3049 * discover that you really need 17...
3053 for (i
=0; i
< 16; i
++)
3055 ptr
->resultcode
= 0;
3056 ptr
->kernel_data
= 0;
3058 if (ptr
->flags
& (FD_RAW_READ
| FD_RAW_WRITE
)) {
3059 if (ptr
->length
<= 0)
3061 ptr
->kernel_data
=(char*)fd_dma_mem_alloc(ptr
->length
);
3062 fallback_on_nodma_alloc(&ptr
->kernel_data
,
3064 if (!ptr
->kernel_data
)
3066 ptr
->buffer_length
= ptr
->length
;
3068 if (ptr
->flags
& FD_RAW_WRITE
)
3069 ECALL(fd_copyin(ptr
->data
, ptr
->kernel_data
,
3071 rcmd
= & (ptr
->next
);
3072 if (!(ptr
->flags
& FD_RAW_MORE
))
3079 static int raw_cmd_ioctl(int cmd
, void *param
)
3081 int drive
, ret
, ret2
;
3082 struct floppy_raw_cmd
*my_raw_cmd
;
3084 if (FDCS
->rawcmd
<= 1)
3086 for (drive
= 0; drive
< N_DRIVE
; drive
++){
3087 if (FDC(drive
) != fdc
)
3089 if (drive
== current_drive
){
3090 if (UDRS
->fd_ref
> 1){
3094 } else if (UDRS
->fd_ref
){
3103 ret
= raw_cmd_copyin(cmd
, param
, &my_raw_cmd
);
3105 raw_cmd_free(&my_raw_cmd
);
3109 raw_cmd
= my_raw_cmd
;
3110 cont
= &raw_cmd_cont
;
3111 ret
=wait_til_done(floppy_start
,1);
3113 if (DP
->flags
& FD_DEBUG
){
3114 DPRINT("calling disk change from raw_cmd ioctl\n");
3118 if (ret
!= -EINTR
&& FDCS
->reset
)
3121 DRS
->track
= NO_TRACK
;
3123 ret2
= raw_cmd_copyout(cmd
, param
, my_raw_cmd
);
3126 raw_cmd_free(&my_raw_cmd
);
3130 static int invalidate_drive(kdev_t rdev
)
3132 /* invalidate the buffer track to force a reread */
3133 set_bit(DRIVE(rdev
), &fake_change
);
3134 process_fd_request();
3135 check_disk_change(rdev
);
3140 static inline void clear_write_error(int drive
)
3145 static inline int set_geometry(unsigned int cmd
, struct floppy_struct
*g
,
3146 int drive
, int type
, kdev_t device
)
3150 /* sanity checking for parameters.*/
3154 g
->track
> UDP
->tracks
>>STRETCH(g
) ||
3155 /* check if reserved bits are set */
3156 (g
->stretch
&~(FD_STRETCH
|FD_SWAPSIDES
)) != 0)
3162 for (cnt
= 0; cnt
< N_DRIVE
; cnt
++){
3163 if (ITYPE(drive_state
[cnt
].fd_device
) == type
&&
3164 drive_state
[cnt
].fd_ref
)
3165 set_bit(drive
, &fake_change
);
3167 floppy_type
[type
] = *g
;
3168 floppy_type
[type
].name
="user format";
3169 for (cnt
= type
<< 2; cnt
< (type
<< 2) + 4; cnt
++)
3170 floppy_sizes
[cnt
]= floppy_sizes
[cnt
+0x80]=
3171 (floppy_type
[type
].size
+1)>>1;
3172 process_fd_request();
3173 for (cnt
= 0; cnt
< N_DRIVE
; cnt
++){
3174 if (ITYPE(drive_state
[cnt
].fd_device
) == type
&&
3175 drive_state
[cnt
].fd_ref
)
3178 drive_state
[cnt
].fd_device
));
3182 if (cmd
!= FDDEFPRM
)
3183 /* notice a disk change immediately, else
3184 * we loose our settings immediately*/
3185 CALL(poll_drive(1, FD_RAW_NEED_DISK
));
3186 user_params
[drive
] = *g
;
3187 if (buffer_drive
== drive
)
3188 SUPBOUND(buffer_max
, user_params
[drive
].sect
);
3189 current_type
[drive
] = &user_params
[drive
];
3190 floppy_sizes
[drive
] = (user_params
[drive
].size
+1) >> 1;
3191 if (cmd
== FDDEFPRM
)
3192 DRS
->keep_data
= -1;
3195 /* invalidation. Invalidate only when needed, i.e.
3196 * when there are already sectors in the buffer cache
3197 * whose number will change. This is useful, because
3198 * mtools often changes the geometry of the disk after
3199 * looking at the boot block */
3200 if (DRS
->maxblock
> user_params
[drive
].sect
|| DRS
->maxtrack
)
3201 invalidate_drive(device
);
3203 process_fd_request();
3208 /* handle obsolete ioctl's */
3209 static int ioctl_table
[]= {
3237 static inline int normalize_ioctl(int *cmd
, int *size
)
3241 for (i
=0; i
< ARRAY_SIZE(ioctl_table
); i
++) {
3242 if ((*cmd
& 0xffff) == (ioctl_table
[i
] & 0xffff)){
3243 *size
= _IOC_SIZE(*cmd
);
3244 *cmd
= ioctl_table
[i
];
3245 if (*size
> _IOC_SIZE(*cmd
)) {
3246 printk("ioctl not yet supported\n");
3255 static int get_floppy_geometry(int drive
, int type
, struct floppy_struct
**g
)
3258 *g
= &floppy_type
[type
];
3261 CALL(poll_drive(0,0));
3262 process_fd_request();
3263 *g
= current_type
[drive
];
3270 static int fd_ioctl(struct inode
*inode
, struct file
*filp
, unsigned int cmd
,
3271 unsigned long param
)
3273 #define IOCTL_MODE_BIT 8
3274 #define OPEN_WRITE_BIT 16
3275 #define IOCTL_ALLOWED (filp && (filp->f_mode & IOCTL_MODE_BIT))
3276 #define OUT(c,x) case c: outparam = (const char *) (x); break
3277 #define IN(c,x,tag) case c: *(x) = inparam. tag ; return 0
3284 struct floppy_struct g
; /* geometry */
3285 struct format_descr f
;
3286 struct floppy_max_errors max_errors
;
3287 struct floppy_drive_params dp
;
3288 } inparam
; /* parameters coming from user space */
3289 const char *outparam
; /* parameters passed back to user space */
3291 device
= inode
->i_rdev
;
3293 RO_IOCTLS(device
,param
);
3295 type
= TYPE(device
);
3296 drive
= DRIVE(device
);
3298 /* convert compatibility eject ioctls into floppy eject ioctl.
3299 * We do this in order to provide a means to eject floppy disks before
3300 * installing the new fdutils package */
3301 if(cmd
== CDROMEJECT
|| /* CD-ROM eject */
3302 cmd
== 0x6470 /* SunOS floppy eject */) {
3303 DPRINT("obsolete eject ioctl\n");
3304 DPRINT("please use floppycontrol --eject\n");
3308 /* generic block device ioctls */
3310 /* the following have been inspired by the corresponding
3311 * code for other block devices. */
3312 struct floppy_struct
*g
;
3315 struct hd_geometry loc
;
3316 ECALL(get_floppy_geometry(drive
, type
, &g
));
3317 loc
.heads
= g
->head
;
3318 loc
.sectors
= g
->sect
;
3319 loc
.cylinders
= g
->track
;
3321 return _COPYOUT(loc
);
3324 if(!suser()) return -EACCES
;
3325 if(param
> 0xff) return -EINVAL
;
3326 read_ahead
[MAJOR(inode
->i_rdev
)] = param
;
3329 return put_user(read_ahead
[MAJOR(inode
->i_rdev
)],
3332 if(!suser()) return -EACCES
;
3333 fsync_dev(inode
->i_rdev
);
3334 invalidate_buffers(inode
->i_rdev
);
3338 ECALL(get_floppy_geometry(drive
, type
, &g
));
3339 return put_user(g
->size
, (long *) param
);
3340 /* BLKRRPART is not defined as floppies don't have
3341 * partition tables */
3344 /* convert the old style command into a new style command */
3345 if ((cmd
& 0xff00) == 0x0200) {
3346 ECALL(normalize_ioctl(&cmd
, &size
));
3350 /* permission checks */
3351 if (((cmd
& 0x40) && !IOCTL_ALLOWED
) ||
3352 ((cmd
& 0x80) && !suser()))
3356 CLEARSTRUCT(&inparam
);
3357 if (_IOC_DIR(cmd
) & _IOC_WRITE
)
3358 ECALL(fd_copyin((void *)param
, &inparam
, size
))
3362 if(UDRS
->fd_ref
!= 1)
3363 /* somebody else has this drive open */
3367 /* do the actual eject. Fails on
3368 * non-Sparc architectures */
3369 ret
=fd_eject(UNIT(drive
));
3371 USETF(FD_DISK_CHANGED
);
3373 process_fd_request();
3377 current_type
[drive
] = NULL
;
3378 floppy_sizes
[drive
] = MAX_DISK_SIZE
;
3379 UDRS
->keep_data
= 0;
3380 return invalidate_drive(device
);
3383 return set_geometry(cmd
, & inparam
.g
,
3384 drive
, type
, device
);
3386 ECALL(get_floppy_geometry(drive
, type
,
3387 (struct floppy_struct
**)
3392 UDP
->flags
|= FTD_MSG
;
3395 UDP
->flags
&= ~FTD_MSG
;
3400 CALL(poll_drive(1, FD_RAW_NEED_DISK
));
3402 process_fd_request();
3405 if(!(ret
& FD_DISK_WRITABLE
))
3409 if (UDRS
->fd_ref
!= 1)
3411 return do_format(device
, &inparam
.f
);
3415 return invalidate_drive(device
);
3417 case FDSETEMSGTRESH
:
3418 UDP
->max_errors
.reporting
=
3419 (unsigned short) (param
& 0x0f);
3421 OUT(FDGETMAXERRS
, &UDP
->max_errors
);
3422 IN(FDSETMAXERRS
, &UDP
->max_errors
, max_errors
);
3425 outparam
= drive_name(type
,drive
);
3426 SUPBOUND(size
,strlen(outparam
)+1);
3429 IN(FDSETDRVPRM
, UDP
, dp
);
3430 OUT(FDGETDRVPRM
, UDP
);
3434 CALL(poll_drive(1, FD_RAW_NEED_DISK
));
3435 process_fd_request();
3437 OUT(FDGETDRVSTAT
, UDRS
);
3440 return user_reset_fdc(drive
, (int)param
, 1);
3442 OUT(FDGETFDCSTAT
,UFDCS
);
3447 OUT(FDWERRORGET
,UDRWE
);
3454 CALL(i
= raw_cmd_ioctl(cmd
,(void *) param
));
3455 process_fd_request();
3461 process_fd_request();
3468 if (_IOC_DIR(cmd
) & _IOC_READ
)
3469 return fd_copyout((void *)param
, outparam
, size
);
3472 #undef IOCTL_ALLOWED
3477 static void config_types(void)
3482 /* read drive info out of physical CMOS */
3485 UDP
->cmos
= FLOPPY0_TYPE
;
3487 if (!UDP
->cmos
&& FLOPPY1_TYPE
)
3488 UDP
->cmos
= FLOPPY1_TYPE
;
3491 /* additional physical CMOS drive detection should go here */
3493 for (drive
=0; drive
< N_DRIVE
; drive
++){
3494 if (UDP
->cmos
>= 16)
3496 if (UDP
->cmos
>= 0 && UDP
->cmos
<= NUMBER(default_drive_params
))
3497 memcpy((char *) UDP
,
3498 (char *) (&default_drive_params
[(int)UDP
->cmos
].params
),
3499 sizeof(struct floppy_drive_params
));
3502 printk(KERN_INFO
"Floppy drive(s): ");
3507 allowed_drive_mask
|= 1 << drive
;
3508 printk("fd%d is %s", drive
,
3509 default_drive_params
[(int)UDP
->cmos
].name
);
3511 printk("fd%d is unknown type %d",drive
,
3519 static ssize_t
floppy_read(struct file
* filp
, char *buf
,
3520 size_t count
, loff_t
*ppos
)
3522 struct inode
*inode
= filp
->f_dentry
->d_inode
;
3523 int drive
= DRIVE(inode
->i_rdev
);
3525 check_disk_change(inode
->i_rdev
);
3526 if (UTESTF(FD_DISK_CHANGED
))
3528 return block_read(filp
, buf
, count
, ppos
);
3531 static ssize_t
floppy_write(struct file
* filp
, const char * buf
,
3532 size_t count
, loff_t
*ppos
)
3534 struct inode
* inode
= filp
->f_dentry
->d_inode
;
3537 int drive
= DRIVE(inode
->i_rdev
);
3539 if (!UDRS
->maxblock
)
3540 UDRS
->maxblock
=1;/* make change detectable */
3541 check_disk_change(inode
->i_rdev
);
3542 if (UTESTF(FD_DISK_CHANGED
))
3544 if (!UTESTF(FD_DISK_WRITABLE
))
3546 block
= (*ppos
+ count
) >> 9;
3547 INFBOUND(UDRS
->maxblock
, block
);
3548 ret
= block_write(filp
, buf
, count
, ppos
);
3552 static int floppy_release(struct inode
* inode
, struct file
* filp
)
3556 drive
= DRIVE(inode
->i_rdev
);
3559 * If filp is NULL, we're being called from blkdev_release
3560 * or after a failed mount attempt. In the former case the
3561 * device has already been sync'ed, and in the latter no
3562 * sync is required. Otherwise, sync if filp is writable.
3564 if (filp
&& (filp
->f_mode
& (2 | OPEN_WRITE_BIT
)))
3565 block_fsync(filp
, filp
->f_dentry
);
3567 if (UDRS
->fd_ref
< 0)
3569 else if (!UDRS
->fd_ref
--) {
3570 DPRINT("floppy_release with fd_ref == 0");
3573 floppy_release_irq_and_dma();
3578 * floppy_open check for aliasing (/dev/fd0 can be the same as
3579 * /dev/PS0 etc), and disallows simultaneous access to the same
3580 * drive with different device numbers.
3582 #define RETERR(x) do{floppy_release(inode,filp); return -(x);}while(0)
3584 static int floppy_open(struct inode
* inode
, struct file
* filp
)
3592 DPRINT("Weird, open called with filp=0\n");
3596 drive
= DRIVE(inode
->i_rdev
);
3597 if (drive
>= N_DRIVE
||
3598 !(allowed_drive_mask
& (1 << drive
)) ||
3599 fdc_state
[FDC(drive
)].version
== FDC_NONE
)
3602 if (TYPE(inode
->i_rdev
) >= NUMBER(floppy_type
))
3604 old_dev
= UDRS
->fd_device
;
3605 if (UDRS
->fd_ref
&& old_dev
!= MINOR(inode
->i_rdev
))
3608 if (!UDRS
->fd_ref
&& (UDP
->flags
& FD_BROKEN_DCL
)){
3609 USETF(FD_DISK_CHANGED
);
3613 if (UDRS
->fd_ref
== -1 ||
3614 (UDRS
->fd_ref
&& (filp
->f_flags
& O_EXCL
)))
3617 if (floppy_grab_irq_and_dma())
3620 if (filp
->f_flags
& O_EXCL
)
3625 if (!floppy_track_buffer
){
3626 /* if opening an ED drive, reserve a big buffer,
3627 * else reserve a small one */
3628 if ((UDP
->cmos
== 6) || (UDP
->cmos
== 5))
3629 try = 64; /* Only 48 actually useful */
3631 try = 32; /* Only 24 actually useful */
3633 tmp
=(char *)fd_dma_mem_alloc(1024 * try);
3634 if (!tmp
&& !floppy_track_buffer
) {
3635 try >>= 1; /* buffer only one side */
3637 tmp
= (char *)fd_dma_mem_alloc(1024*try);
3639 if(!tmp
&& !floppy_track_buffer
) {
3640 fallback_on_nodma_alloc(&tmp
, 2048 * try);
3642 if (!tmp
&& !floppy_track_buffer
) {
3643 DPRINT("Unable to allocate DMA memory\n");
3646 if (floppy_track_buffer
) {
3648 fd_dma_mem_free((unsigned long)tmp
,try*1024);
3650 buffer_min
= buffer_max
= -1;
3651 floppy_track_buffer
= tmp
;
3652 max_buffer_sectors
= try;
3656 UDRS
->fd_device
= MINOR(inode
->i_rdev
);
3657 if (old_dev
!= -1 && old_dev
!= MINOR(inode
->i_rdev
)) {
3658 if (buffer_drive
== drive
)
3660 invalidate_buffers(MKDEV(FLOPPY_MAJOR
,old_dev
));
3663 /* Allow ioctls if we have write-permissions even if read-only open */
3664 if ((filp
->f_mode
& 2) || (permission(inode
,2) == 0))
3665 filp
->f_mode
|= IOCTL_MODE_BIT
;
3666 if (filp
->f_mode
& 2)
3667 filp
->f_mode
|= OPEN_WRITE_BIT
;
3669 if (UFDCS
->rawcmd
== 1)
3672 if (filp
->f_flags
& O_NDELAY
)
3674 if (filp
->f_mode
& 3) {
3675 UDRS
->last_checked
= 0;
3676 check_disk_change(inode
->i_rdev
);
3677 if (UTESTF(FD_DISK_CHANGED
))
3680 if ((filp
->f_mode
& 2) && !(UTESTF(FD_DISK_WRITABLE
)))
3687 * Check if the disk has been changed or if a change has been faked.
3689 static int check_floppy_change(kdev_t dev
)
3691 int drive
= DRIVE(dev
);
3693 if (MAJOR(dev
) != MAJOR_NR
) {
3694 DPRINT("check_floppy_change: not a floppy\n");
3698 if (UTESTF(FD_DISK_CHANGED
) || UTESTF(FD_VERIFY
))
3701 if (UDP
->checkfreq
< jiffies
- UDRS
->last_checked
){
3704 process_fd_request();
3707 if (UTESTF(FD_DISK_CHANGED
) ||
3708 UTESTF(FD_VERIFY
) ||
3709 test_bit(drive
, &fake_change
) ||
3710 (!TYPE(dev
) && !current_type
[drive
]))
3715 /* revalidate the floppy disk, i.e. trigger format autodetection by reading
3716 * the bootblock (block 0). "Autodetection" is also needed to check whether
3717 * there is a disk in the drive at all... Thus we also do it for fixed
3718 * geometry formats */
3719 static int floppy_revalidate(kdev_t dev
)
3721 #define NO_GEOM (!current_type[drive] && !TYPE(dev))
3722 struct buffer_head
* bh
;
3723 int drive
=DRIVE(dev
);
3726 if (UTESTF(FD_DISK_CHANGED
) ||
3727 UTESTF(FD_VERIFY
) ||
3728 test_bit(drive
, &fake_change
) ||
3731 cf
= UTESTF(FD_DISK_CHANGED
) || UTESTF(FD_VERIFY
);
3732 if (!(cf
|| test_bit(drive
, &fake_change
) || NO_GEOM
)){
3733 process_fd_request(); /*already done by another thread*/
3738 if (buffer_drive
== drive
)
3740 clear_bit(drive
, &fake_change
);
3741 UCLEARF(FD_DISK_CHANGED
);
3746 int size
= floppy_blocksizes
[MINOR(dev
)];
3749 if (!(bh
= getblk(dev
,0,size
))){
3750 process_fd_request();
3753 if (bh
&& !buffer_uptodate(bh
))
3754 ll_rw_block(READ
, 1, &bh
);
3755 process_fd_request();
3761 poll_drive(0, FD_RAW_NEED_DISK
);
3762 process_fd_request();
3767 static struct file_operations floppy_fops
= {
3768 NULL
, /* lseek - default */
3769 floppy_read
, /* read - general block-dev read */
3770 floppy_write
, /* write - general block-dev write */
3771 NULL
, /* readdir - bad */
3773 fd_ioctl
, /* ioctl */
3775 floppy_open
, /* open */
3776 floppy_release
, /* release */
3777 block_fsync
, /* fsync */
3779 check_floppy_change
, /* media_change */
3780 floppy_revalidate
, /* revalidate */
3784 * Floppy Driver initialization
3785 * =============================
3788 /* Determine the floppy disk controller type */
3789 /* This routine was written by David C. Niemi */
3790 __initfunc(static char get_fdc_version(void))
3794 output_byte(FD_DUMPREGS
); /* 82072 and better know DUMPREGS */
3797 if ((r
= result()) <= 0x00)
3798 return FDC_NONE
; /* No FDC present ??? */
3799 if ((r
==1) && (reply_buffer
[0] == 0x80)){
3800 printk(KERN_INFO
"FDC %d is an 8272A\n",fdc
);
3801 return FDC_8272A
; /* 8272a/765 don't know DUMPREGS */
3804 printk("FDC %d init: DUMPREGS: unexpected return of %d bytes.\n",
3809 if(!fdc_configure()) {
3810 printk(KERN_INFO
"FDC %d is an 82072\n",fdc
);
3811 return FDC_82072
; /* 82072 doesn't know CONFIGURE */
3814 output_byte(FD_PERPENDICULAR
);
3815 if(need_more_output() == MORE_OUTPUT
) {
3818 printk(KERN_INFO
"FDC %d is an 82072A\n", fdc
);
3819 return FDC_82072A
; /* 82072A as found on Sparcs. */
3822 output_byte(FD_UNLOCK
);
3824 if ((r
== 1) && (reply_buffer
[0] == 0x80)){
3825 printk(KERN_INFO
"FDC %d is a pre-1991 82077\n", fdc
);
3826 return FDC_82077_ORIG
; /* Pre-1991 82077, doesn't know
3829 if ((r
!= 1) || (reply_buffer
[0] != 0x00)) {
3830 printk("FDC %d init: UNLOCK: unexpected return of %d bytes.\n",
3834 output_byte(FD_PARTID
);
3837 printk("FDC %d init: PARTID: unexpected return of %d bytes.\n",
3841 if (reply_buffer
[0] == 0x80) {
3842 printk(KERN_INFO
"FDC %d is a post-1991 82077\n",fdc
);
3843 return FDC_82077
; /* Revised 82077AA passes all the tests */
3845 switch (reply_buffer
[0] >> 5) {
3847 /* Either a 82078-1 or a 82078SL running at 5Volt */
3848 printk(KERN_INFO
"FDC %d is an 82078.\n",fdc
);
3851 printk(KERN_INFO
"FDC %d is a 44pin 82078\n",fdc
);
3854 printk(KERN_INFO
"FDC %d is a S82078B\n", fdc
);
3857 printk(KERN_INFO
"FDC %d is a National Semiconductor PC87306\n", fdc
);
3860 printk(KERN_INFO
"FDC %d init: 82078 variant with unknown PARTID=%d.\n",
3861 fdc
, reply_buffer
[0] >> 5);
3862 return FDC_82078_UNKN
;
3864 } /* get_fdc_version */
3866 /* lilo configuration */
3868 __initfunc(static void floppy_set_flags(int *ints
,int param
, int param2
))
3872 for (i
=0; i
< ARRAY_SIZE(default_drive_params
); i
++){
3874 default_drive_params
[i
].params
.flags
|= param2
;
3876 default_drive_params
[i
].params
.flags
&= ~param2
;
3878 DPRINT("%s flag 0x%x\n", param2
? "Setting" : "Clearing", param
);
3881 __initfunc(static void daring(int *ints
,int param
, int param2
))
3885 for (i
=0; i
< ARRAY_SIZE(default_drive_params
); i
++){
3887 default_drive_params
[i
].params
.select_delay
= 0;
3888 default_drive_params
[i
].params
.flags
|= FD_SILENT_DCL_CLEAR
;
3890 default_drive_params
[i
].params
.select_delay
= 2*HZ
/100;
3891 default_drive_params
[i
].params
.flags
&= ~FD_SILENT_DCL_CLEAR
;
3894 DPRINT("Assuming %s floppy hardware\n", param
? "standard" : "broken");
3897 __initfunc(static void set_cmos(int *ints
, int dummy
, int dummy2
))
3899 int current_drive
=0;
3902 DPRINT("wrong number of parameter for cmos\n");
3905 current_drive
= ints
[1];
3906 if (current_drive
< 0 || current_drive
>= 8){
3907 DPRINT("bad drive for set_cmos\n");
3910 if (current_drive
>= 4 && !FDC2
)
3913 (ints
[2] >= NUMBER(default_drive_params
) && ints
[2] != 16)){
3914 DPRINT("bad cmos code %d\n", ints
[2]);
3918 DPRINT("setting cmos code to %d\n", ints
[2]);
3921 static struct param_table
{
3923 void (*fn
)(int *ints
, int param
, int param2
);
3928 { "allowed_drive_mask", 0, &allowed_drive_mask
, 0xff, 0}, /* obsolete */
3929 { "all_drives", 0, &allowed_drive_mask
, 0xff, 0 }, /* obsolete */
3930 { "asus_pci", 0, &allowed_drive_mask
, 0x33, 0},
3932 { "irq", 0, &FLOPPY_IRQ
, 6, 0 },
3933 { "dma", 0, &FLOPPY_DMA
, 2, 0 },
3935 { "daring", daring
, 0, 1, 0},
3937 { "two_fdc", 0, &FDC2
, 0x370, 0 },
3938 { "one_fdc", 0, &FDC2
, 0, 0 },
3940 { "thinkpad", floppy_set_flags
, 0, 1, FD_INVERTED_DCL
},
3941 { "broken_dcl", floppy_set_flags
, 0, 1, FD_BROKEN_DCL
},
3942 { "messages", floppy_set_flags
, 0, 1, FTD_MSG
},
3943 { "silent_dcl_clear", floppy_set_flags
, 0, 1, FD_SILENT_DCL_CLEAR
},
3944 { "debug", floppy_set_flags
, 0, 1, FD_DEBUG
},
3946 { "nodma", 0, &can_use_virtual_dma
, 1, 0 },
3947 { "omnibook", 0, &can_use_virtual_dma
, 1, 0 },
3948 { "yesdma", 0, &can_use_virtual_dma
, 0, 0 },
3950 { "fifo_depth", 0, &fifo_depth
, 0xa, 0 },
3951 { "nofifo", 0, &no_fifo
, 0x20, 0 },
3952 { "usefifo", 0, &no_fifo
, 0, 0 },
3954 { "cmos", set_cmos
, 0, 0, 0 },
3956 { "unexpected_interrupts", 0, &print_unex
, 1, 0 },
3957 { "no_unexpected_interrupts", 0, &print_unex
, 0, 0 },
3958 { "L40SX", 0, &print_unex
, 0, 0 } };
3960 #define FLOPPY_SETUP
3961 __initfunc(void floppy_setup(char *str
, int *ints
))
3966 for (i
=0; i
< ARRAY_SIZE(config_params
); i
++){
3967 if (strcmp(str
,config_params
[i
].name
) == 0){
3971 param
= config_params
[i
].def_param
;
3972 if(config_params
[i
].fn
)
3975 config_params
[i
].param2
);
3976 if(config_params
[i
].var
) {
3977 DPRINT("%s=%d\n", str
, param
);
3978 *config_params
[i
].var
= param
;
3984 DPRINT("unknown floppy option [%s]\n", str
);
3986 DPRINT("allowed options are:");
3987 for (i
=0; i
< ARRAY_SIZE(config_params
); i
++)
3988 printk(" %s",config_params
[i
].name
);
3991 DPRINT("botched floppy option\n");
3992 DPRINT("Read linux/drivers/block/README.fd\n");
3995 static int have_no_fdc
= -EIO
;
3998 __initfunc(int floppy_init(void))
4005 if (register_blkdev(MAJOR_NR
,"fd",&floppy_fops
)) {
4006 printk("Unable to get major %d for floppy\n",MAJOR_NR
);
4010 for (i
=0; i
<256; i
++)
4012 floppy_sizes
[i
] = (floppy_type
[ITYPE(i
)].size
+1) >> 1;
4014 floppy_sizes
[i
] = MAX_DISK_SIZE
;
4016 blk_size
[MAJOR_NR
] = floppy_sizes
;
4017 blksize_size
[MAJOR_NR
] = floppy_blocksizes
;
4018 blk_dev
[MAJOR_NR
].request_fn
= DEVICE_REQUEST
;
4019 reschedule_timeout(MAXTIMEOUT
, "floppy init", MAXTIMEOUT
);
4022 for (i
= 0; i
< N_FDC
; i
++) {
4028 /*sparcs don't have a DOR reset which we can fall back on to*/
4029 FDCS
->version
= FDC_82072A
;
4033 use_virtual_dma
= can_use_virtual_dma
& 1;
4034 fdc_state
[0].address
= FDC1
;
4035 if (fdc_state
[0].address
== -1) {
4036 unregister_blkdev(MAJOR_NR
,"fd");
4037 del_timer(&fd_timeout
);
4041 fdc_state
[1].address
= FDC2
;
4044 if (floppy_grab_irq_and_dma()){
4045 unregister_blkdev(MAJOR_NR
,"fd");
4046 del_timer(&fd_timeout
);
4050 /* initialise drive state */
4051 for (drive
= 0; drive
< N_DRIVE
; drive
++) {
4054 UDRS
->flags
= FD_VERIFY
| FD_DISK_NEWCHANGE
| FD_DISK_CHANGED
;
4055 UDRS
->fd_device
= -1;
4056 floppy_track_buffer
= NULL
;
4057 max_buffer_sectors
= 0;
4060 for (i
= 0; i
< N_FDC
; i
++) {
4062 FDCS
->driver_version
= FD_DRIVER_VERSION
;
4063 for (unit
=0; unit
<4; unit
++)
4064 FDCS
->track
[unit
] = 0;
4065 if (FDCS
->address
== -1)
4068 if (user_reset_fdc(-1,FD_RESET_ALWAYS
,0)){
4070 FDCS
->version
= FDC_NONE
;
4073 /* Try to determine the floppy controller type */
4074 FDCS
->version
= get_fdc_version();
4075 if (FDCS
->version
== FDC_NONE
){
4079 if(can_use_virtual_dma
== 2 && FDCS
->version
< FDC_82072A
)
4080 can_use_virtual_dma
= 0;
4083 /* Not all FDCs seem to be able to handle the version command
4084 * properly, so force a reset for the standard FDC clones,
4085 * to avoid interrupt garbage.
4087 user_reset_fdc(-1,FD_RESET_ALWAYS
,0);
4090 del_timer(&fd_timeout
);
4092 floppy_release_irq_and_dma();
4095 DPRINT("no floppy controllers found\n");
4096 unregister_blkdev(MAJOR_NR
,"fd");
4101 static int floppy_grab_irq_and_dma(void)
4103 unsigned long flags
;
4112 if (fd_request_irq()) {
4113 DPRINT("Unable to grab IRQ%d for the floppy driver\n",
4119 if (fd_request_dma()) {
4120 DPRINT("Unable to grab DMA%d for the floppy driver\n",
4128 for (fdc
=0; fdc
< N_FDC
; fdc
++){
4129 if (FDCS
->address
!= -1){
4130 if (check_region(FDCS
->address
, 6) < 0 ||
4131 check_region(FDCS
->address
+7, 1) < 0) {
4132 DPRINT("Floppy io-port 0x%04lx in use\n", FDCS
->address
);
4136 release_region(FDCS
->address
, 6);
4137 release_region(FDCS
->address
+7, 1);
4143 request_region(FDCS
->address
, 6, "floppy");
4144 request_region(FDCS
->address
+7, 1, "floppy DIR");
4145 /* address + 6 is reserved, and may be taken by IDE.
4146 * Unfortunately, Adaptec doesn't know this :-(, */
4149 for (fdc
=0; fdc
< N_FDC
; fdc
++){
4150 if (FDCS
->address
!= -1){
4152 fd_outb(FDCS
->dor
, FD_DOR
);
4156 set_dor(0, ~0, 8); /* avoid immediate interrupt */
4158 for (fdc
= 0; fdc
< N_FDC
; fdc
++)
4159 if (FDCS
->address
!= -1)
4160 fd_outb(FDCS
->dor
, FD_DOR
);
4166 static void floppy_release_irq_and_dma(void)
4169 #ifdef FLOPPY_SANITY_CHECK
4175 unsigned long tmpaddr
;
4176 unsigned long flags
;
4193 floppy_enable_hlt();
4195 if (floppy_track_buffer
&& max_buffer_sectors
) {
4196 tmpsize
= max_buffer_sectors
*1024;
4197 tmpaddr
= (unsigned long)floppy_track_buffer
;
4198 floppy_track_buffer
= 0;
4199 max_buffer_sectors
= 0;
4200 buffer_min
= buffer_max
= -1;
4201 fd_dma_mem_free(tmpaddr
, tmpsize
);
4204 #ifdef FLOPPY_SANITY_CHECK
4206 for (drive
=0; drive
< N_FDC
* 4; drive
++)
4207 if (motor_off_timer
[drive
].next
)
4208 printk("motor off timer %d still active\n", drive
);
4211 if (fd_timeout
.next
)
4212 printk("floppy timer still active:%s\n", timeout_message
);
4214 printk("auxiliary floppy timer still active\n");
4216 printk("task queue still active\n");
4219 for (fdc
= 0; fdc
< N_FDC
; fdc
++)
4220 if (FDCS
->address
!= -1) {
4221 release_region(FDCS
->address
, 6);
4222 release_region(FDCS
->address
+7, 1);
4231 extern char *get_options(char *str
, int *ints
);
4235 __initfunc(static void parse_floppy_cfg_string(char *cfg
))
4241 for(ptr
= cfg
;*cfg
&& *cfg
!= ' ' && *cfg
!= '\t'; cfg
++);
4247 floppy_setup(get_options(ptr
,ints
),ints
);
4251 __initfunc(static void mod_setup(char *pattern
, void (*setup
)(char *, int *)))
4259 int length
= strlen(pattern
)+1;
4264 for (i
=current
->mm
->env_start
; i
< current
->mm
->env_end
; i
++){
4265 get_user(c
, (char *)i
);
4270 if (!c
|| c
== ' ' || c
== '\t'){
4273 setup(get_options(buffer
,ints
),ints
);
4282 if ((!j
&& !c
) || (j
&& c
== pattern
[j
-1]))
4297 int init_module(void)
4299 printk(KERN_INFO
"inserting floppy driver for " UTS_RELEASE
"\n");
4302 parse_floppy_cfg_string(floppy
);
4304 mod_setup("floppy=", floppy_setup
);
4306 return floppy_init();
4309 void cleanup_module(void)
4313 unregister_blkdev(MAJOR_NR
, "fd");
4315 blk_dev
[MAJOR_NR
].request_fn
= 0;
4316 /* eject disk, if any */
4317 dummy
= fd_eject(0);
4320 MODULE_PARM(floppy
,"s");
4321 MODULE_PARM(FLOPPY_IRQ
,"i");
4322 MODULE_PARM(FLOPPY_DMA
,"i");
4323 MODULE_AUTHOR("Alain L. Knaff");
4324 MODULE_SUPPORTED_DEVICE("fd");
4331 /* eject the boot floppy (if we need the drive for a different root floppy) */
4332 /* This should only be called at boot time when we're sure that there's no
4333 * resource contention. */
4334 void floppy_eject(void)
4339 floppy_grab_irq_and_dma();
4340 lock_fdc(MAXTIMEOUT
,0);
4342 process_fd_request();
4343 floppy_release_irq_and_dma();