2 * linux/kernel/floppy.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 * Copyright (C) 1993, 1994 Alain Knaff
6 * Copyright (C) 1998 Alan Cox
9 * 02.12.91 - Changed to static variables to indicate need for reset
10 * and recalibrate. This makes some things easier (output_byte reset
11 * checking etc), and means less interrupt jumping in case of errors,
12 * so the code is hopefully easier to understand.
16 * This file is certainly a mess. I've tried my best to get it working,
17 * but I don't like programming floppies, and I have only one anyway.
18 * Urgel. I should check for more errors, and do more graceful error
19 * recovery. Seems there are problems with several drives. I've tried to
20 * correct them. No promises.
24 * As with hd.c, all routines within this file can (and will) be called
25 * by interrupts, so extreme caution is needed. A hardware interrupt
26 * handler may not sleep, or a kernel panic will happen. Thus I cannot
27 * call "floppy-on" directly, but have to set a special timer interrupt
32 * 28.02.92 - made track-buffering routines, based on the routines written
33 * by entropy@wintermute.wpi.edu (Lawrence Foard). Linus.
37 * Automatic floppy-detection and formatting written by Werner Almesberger
38 * (almesber@nessie.cs.id.ethz.ch), who also corrected some problems with
39 * the floppy-change signal detection.
43 * 1992/7/22 -- Hennus Bergman: Added better error reporting, fixed
44 * FDC data overrun bug, added some preliminary stuff for vertical
47 * 1992/9/17: Added DMA allocation & DMA functions. -- hhb.
49 * TODO: Errors are still not counted properly.
53 * Modifications for ``Sector Shifting'' by Rob Hooft (hooft@chem.ruu.nl)
54 * modeled after the freeware MS-DOS program fdformat/88 V1.8 by
55 * Christoph H. Hochst\"atter.
56 * I have fixed the shift values to the ones I always use. Maybe a new
57 * ioctl() should be created to be able to modify them.
58 * There is a bug in the driver that makes it impossible to format a
59 * floppy as the first thing after bootup.
63 * 1993/4/29 -- Linus -- cleaned up the timer handling in the kernel, and
64 * this helped the floppy driver as well. Much cleaner, and still seems to
68 /* 1994/6/24 --bbroad-- added the floppy table entries and made
69 * minor modifications to allow 2.88 floppies to be run.
72 /* 1994/7/13 -- Paul Vojta -- modified the probing code to allow three or more
77 * 1994/8/8 -- Alain Knaff -- Switched to fdpatch driver: Support for bigger
78 * format bug fixes, but unfortunately some new bugs too...
81 /* 1994/9/17 -- Koen Holtman -- added logging of physical floppy write
82 * errors to allow safe writing by specialized programs.
85 /* 1995/4/24 -- Dan Fandrich -- added support for Commodore 1581 3.5" disks
86 * by defining bit 1 of the "stretch" parameter to mean put sectors on the
87 * opposite side of the disk, leaving the sector IDs alone (i.e. Commodore's
88 * drives are "upside-down").
92 * 1995/8/26 -- Andreas Busse -- added Mips support.
96 * 1995/10/18 -- Ralf Baechle -- Portability cleanup; move machine dependent
97 * features to asm/floppy.h.
101 * 1998/06/07 -- Alan Cox -- Merged the 2.0.34 fixes for resource allocation
106 * 1998/09/20 -- David Weinehall -- Added slow-down code for buggy PS/2-drives.
109 #define FLOPPY_SANITY_CHECK
110 #undef FLOPPY_SILENT_DCL_CLEAR
112 #define REALLY_SLOW_IO
115 #define DCL_DEBUG /* debug disk change line */
117 /* do print messages for unexpected interrupts */
118 static int print_unex
=1;
119 #include <linux/module.h>
121 /* the following is the mask of allowed drives. By default units 2 and
122 * 3 of both floppy controllers are disabled, because switching on the
123 * motor of these drives causes system hangs on some PCI computers. drive
124 * 0 is the low bit (0x1), and drive 7 is the high bit (0x80). Bits are on if
125 * a drive is allowed. */
127 static int FLOPPY_IRQ
=6;
128 static int FLOPPY_DMA
=2;
129 static int allowed_drive_mask
= 0x33;
130 static int irqdma_allocated
= 0;
133 #include <linux/sched.h>
134 #include <linux/fs.h>
135 #include <linux/kernel.h>
136 #include <linux/timer.h>
137 #include <linux/tqueue.h>
139 #include <linux/fdreg.h>
142 #include <linux/fd.h>
143 #include <linux/hdreg.h>
145 #include <linux/errno.h>
146 #include <linux/malloc.h>
147 #include <linux/mm.h>
148 #include <linux/string.h>
149 #include <linux/fcntl.h>
150 #include <linux/delay.h>
151 #include <linux/mc146818rtc.h> /* CMOS defines */
152 #include <linux/ioport.h>
153 #include <linux/interrupt.h>
154 #include <linux/init.h>
157 * PS/2 floppies have much slower step rates than regular floppies.
158 * It's been recommended that take about 1/4 of the default speed
159 * in some more extreme cases.
161 static int slow_floppy
= 0;
165 #include <asm/system.h>
167 #include <asm/uaccess.h>
169 static int can_use_virtual_dma
=2;
171 * can use virtual DMA:
172 * 0 = use of virtual DMA disallowed by config
173 * 1 = use of virtual DMA prescribed by config
174 * 2 = no virtual DMA preference configured. By default try hard DMA,
175 * but fall back on virtual DMA when not enough memory available
178 static int use_virtual_dma
=0;
182 * 1 using virtual DMA
183 * This variable is set to virtual when a DMA mem problem arises, and
184 * reset back in floppy_grab_irq_and_dma.
185 * It is not safe to reset it in other circumstances, because the floppy
186 * driver may have several buffers in use at once, and we do currently not
187 * record each buffers capabilities
190 static unsigned short virtual_dma_port
=0x3f0;
191 void floppy_interrupt(int irq
, void *dev_id
, struct pt_regs
* regs
);
192 static int set_dor(int fdc
, char mask
, char data
);
193 static inline int __get_order(unsigned long size
);
194 #define K_64 0x10000 /* 64KB */
195 #include <asm/floppy.h>
198 #define MAJOR_NR FLOPPY_MAJOR
200 #include <linux/blk.h>
201 #include <linux/blkpg.h>
202 #include <linux/cdrom.h> /* for the compatibility eject ioctl */
204 #ifndef fd_get_dma_residue
205 #define fd_get_dma_residue() get_dma_residue(FLOPPY_DMA)
208 /* Dma Memory related stuff */
210 /* Pure 2^n version of get_order */
211 static inline int __get_order(unsigned long size
)
215 size
= (size
-1) >> (PAGE_SHIFT
-1);
224 #ifndef fd_dma_mem_free
225 #define fd_dma_mem_free(addr, size) free_pages(addr, __get_order(size))
228 #ifndef fd_dma_mem_alloc
229 #define fd_dma_mem_alloc(size) __get_dma_pages(GFP_KERNEL,__get_order(size))
232 static inline void fallback_on_nodma_alloc(char **addr
, size_t l
)
234 #ifdef FLOPPY_CAN_FALLBACK_ON_NODMA
236 return; /* we have the memory */
237 if(can_use_virtual_dma
!= 2)
238 return; /* no fallback allowed */
239 printk("DMA memory shortage. Temporarily falling back on virtual DMA\n");
240 *addr
= (char *) nodma_mem_alloc(l
);
246 /* End dma memory related stuff */
248 static unsigned long fake_change
= 0;
249 static int initialising
=1;
251 static inline int TYPE(kdev_t x
) {
252 return (MINOR(x
)>>2) & 0x1f;
254 static inline int DRIVE(kdev_t x
) {
255 return (MINOR(x
)&0x03) | ((MINOR(x
)&0x80) >> 5);
257 #define ITYPE(x) (((x)>>2) & 0x1f)
258 #define TOMINOR(x) ((x & 3) | ((x & 4) << 5))
259 #define UNIT(x) ((x) & 0x03) /* drive on fdc */
260 #define FDC(x) (((x) & 0x04) >> 2) /* fdc of drive */
261 #define REVDRIVE(fdc, unit) ((unit) + ((fdc) << 2))
262 /* reverse mapping from unit and fdc to drive */
263 #define DP (&drive_params[current_drive])
264 #define DRS (&drive_state[current_drive])
265 #define DRWE (&write_errors[current_drive])
266 #define FDCS (&fdc_state[fdc])
267 #define CLEARF(x) (clear_bit(x##_BIT, &DRS->flags))
268 #define SETF(x) (set_bit(x##_BIT, &DRS->flags))
269 #define TESTF(x) (test_bit(x##_BIT, &DRS->flags))
271 #define UDP (&drive_params[drive])
272 #define UDRS (&drive_state[drive])
273 #define UDRWE (&write_errors[drive])
274 #define UFDCS (&fdc_state[FDC(drive)])
275 #define UCLEARF(x) (clear_bit(x##_BIT, &UDRS->flags))
276 #define USETF(x) (set_bit(x##_BIT, &UDRS->flags))
277 #define UTESTF(x) (test_bit(x##_BIT, &UDRS->flags))
279 #define DPRINT(format, args...) printk(DEVICE_NAME "%d: " format, current_drive , ## args)
281 #define PH_HEAD(floppy,head) (((((floppy)->stretch & 2) >>1) ^ head) << 2)
282 #define STRETCH(floppy) ((floppy)->stretch & FD_STRETCH)
284 #define CLEARSTRUCT(x) memset((x), 0, sizeof(*(x)))
286 #define INT_OFF save_flags(flags); cli()
287 #define INT_ON restore_flags(flags)
290 #define COMMAND raw_cmd->cmd[0]
291 #define DR_SELECT raw_cmd->cmd[1]
292 #define TRACK raw_cmd->cmd[2]
293 #define HEAD raw_cmd->cmd[3]
294 #define SECTOR raw_cmd->cmd[4]
295 #define SIZECODE raw_cmd->cmd[5]
296 #define SECT_PER_TRACK raw_cmd->cmd[6]
297 #define GAP raw_cmd->cmd[7]
298 #define SIZECODE2 raw_cmd->cmd[8]
302 #define F_SIZECODE raw_cmd->cmd[2]
303 #define F_SECT_PER_TRACK raw_cmd->cmd[3]
304 #define F_GAP raw_cmd->cmd[4]
305 #define F_FILL raw_cmd->cmd[5]
309 * Maximum disk size (in kilobytes). This default is used whenever the
310 * current disk size is unknown.
311 * [Now it is rather a minimum]
313 #define MAX_DISK_SIZE 4 /* 3984*/
317 * globals used by 'result()'
319 #define MAX_REPLIES 16
320 static unsigned char reply_buffer
[MAX_REPLIES
];
321 static int inr
; /* size of reply buffer, when called from interrupt */
322 #define ST0 (reply_buffer[0])
323 #define ST1 (reply_buffer[1])
324 #define ST2 (reply_buffer[2])
325 #define ST3 (reply_buffer[0]) /* result of GETSTATUS */
326 #define R_TRACK (reply_buffer[3])
327 #define R_HEAD (reply_buffer[4])
328 #define R_SECTOR (reply_buffer[5])
329 #define R_SIZECODE (reply_buffer[6])
331 #define SEL_DLY (2*HZ/100)
333 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
335 * this struct defines the different floppy drive types.
338 struct floppy_drive_params params
;
339 const char *name
; /* name printed while booting */
340 } default_drive_params
[]= {
341 /* NOTE: the time values in jiffies should be in msec!
343 | Maximum data rate supported by drive type
344 | | Head load time, msec
345 | | | Head unload time, msec (not used)
346 | | | | Step rate interval, usec
347 | | | | | Time needed for spinup time (jiffies)
348 | | | | | | Timeout for spinning down (jiffies)
349 | | | | | | | Spindown offset (where disk stops)
350 | | | | | | | | Select delay
351 | | | | | | | | | RPS
352 | | | | | | | | | | Max number of tracks
353 | | | | | | | | | | | Interrupt timeout
354 | | | | | | | | | | | | Max nonintlv. sectors
355 | | | | | | | | | | | | | -Max Errors- flags */
356 {{0, 500, 16, 16, 8000, 1*HZ
, 3*HZ
, 0, SEL_DLY
, 5, 80, 3*HZ
, 20, {3,1,2,0,2}, 0,
357 0, { 7, 4, 8, 2, 1, 5, 3,10}, 3*HZ
/2, 0 }, "unknown" },
359 {{1, 300, 16, 16, 8000, 1*HZ
, 3*HZ
, 0, SEL_DLY
, 5, 40, 3*HZ
, 17, {3,1,2,0,2}, 0,
360 0, { 1, 0, 0, 0, 0, 0, 0, 0}, 3*HZ
/2, 1 }, "360K PC" }, /*5 1/4 360 KB PC*/
362 {{2, 500, 16, 16, 6000, 4*HZ
/10, 3*HZ
, 14, SEL_DLY
, 6, 83, 3*HZ
, 17, {3,1,2,0,2}, 0,
363 0, { 2, 5, 6,23,10,20,12, 0}, 3*HZ
/2, 2 }, "1.2M" }, /*5 1/4 HD AT*/
365 {{3, 250, 16, 16, 3000, 1*HZ
, 3*HZ
, 0, SEL_DLY
, 5, 83, 3*HZ
, 20, {3,1,2,0,2}, 0,
366 0, { 4,22,21,30, 3, 0, 0, 0}, 3*HZ
/2, 4 }, "720k" }, /*3 1/2 DD*/
368 {{4, 500, 16, 16, 4000, 4*HZ
/10, 3*HZ
, 10, SEL_DLY
, 5, 83, 3*HZ
, 20, {3,1,2,0,2}, 0,
369 0, { 7, 4,25,22,31,21,29,11}, 3*HZ
/2, 7 }, "1.44M" }, /*3 1/2 HD*/
371 {{5, 1000, 15, 8, 3000, 4*HZ
/10, 3*HZ
, 10, SEL_DLY
, 5, 83, 3*HZ
, 40, {3,1,2,0,2}, 0,
372 0, { 7, 8, 4,25,28,22,31,21}, 3*HZ
/2, 8 }, "2.88M AMI BIOS" }, /*3 1/2 ED*/
374 {{6, 1000, 15, 8, 3000, 4*HZ
/10, 3*HZ
, 10, SEL_DLY
, 5, 83, 3*HZ
, 40, {3,1,2,0,2}, 0,
375 0, { 7, 8, 4,25,28,22,31,21}, 3*HZ
/2, 8 }, "2.88M" } /*3 1/2 ED*/
376 /* | --autodetected formats--- | | |
377 * read_track | | Name printed when booting
379 * Frequency of disk change checks */
382 static struct floppy_drive_params drive_params
[N_DRIVE
];
383 static struct floppy_drive_struct drive_state
[N_DRIVE
];
384 static struct floppy_write_errors write_errors
[N_DRIVE
];
385 static struct floppy_raw_cmd
*raw_cmd
, default_raw_cmd
;
388 * This struct defines the different floppy types.
390 * Bit 0 of 'stretch' tells if the tracks need to be doubled for some
391 * types (e.g. 360kB diskette in 1.2MB drive, etc.). Bit 1 of 'stretch'
392 * tells if the disk is in Commodore 1581 format, which means side 0 sectors
393 * are located on side 1 of the disk but with a side 0 ID, and vice-versa.
394 * This is the same as the Sharp MZ-80 5.25" CP/M disk format, except that the
395 * 1581's logical side 0 is on physical side 1, whereas the Sharp's logical
396 * side 0 is on physical side 0 (but with the misnamed sector IDs).
397 * 'stretch' should probably be renamed to something more general, like
398 * 'options'. Other parameters should be self-explanatory (see also
401 static struct floppy_struct floppy_type
[32] = {
402 { 0, 0,0, 0,0,0x00,0x00,0x00,0x00,NULL
}, /* 0 no testing */
403 { 720, 9,2,40,0,0x2A,0x02,0xDF,0x50,"d360" }, /* 1 360KB PC */
404 { 2400,15,2,80,0,0x1B,0x00,0xDF,0x54,"h1200" }, /* 2 1.2MB AT */
405 { 720, 9,1,80,0,0x2A,0x02,0xDF,0x50,"D360" }, /* 3 360KB SS 3.5" */
406 { 1440, 9,2,80,0,0x2A,0x02,0xDF,0x50,"D720" }, /* 4 720KB 3.5" */
407 { 720, 9,2,40,1,0x23,0x01,0xDF,0x50,"h360" }, /* 5 360KB AT */
408 { 1440, 9,2,80,0,0x23,0x01,0xDF,0x50,"h720" }, /* 6 720KB AT */
409 { 2880,18,2,80,0,0x1B,0x00,0xCF,0x6C,"H1440" }, /* 7 1.44MB 3.5" */
410 { 5760,36,2,80,0,0x1B,0x43,0xAF,0x54,"E2880" }, /* 8 2.88MB 3.5" */
411 { 6240,39,2,80,0,0x1B,0x43,0xAF,0x28,"E3120"}, /* 9 3.12MB 3.5" */
413 { 2880,18,2,80,0,0x25,0x00,0xDF,0x02,"h1440" }, /* 10 1.44MB 5.25" */
414 { 3360,21,2,80,0,0x1C,0x00,0xCF,0x0C,"H1680" }, /* 11 1.68MB 3.5" */
415 { 820,10,2,41,1,0x25,0x01,0xDF,0x2E,"h410" }, /* 12 410KB 5.25" */
416 { 1640,10,2,82,0,0x25,0x02,0xDF,0x2E,"H820" }, /* 13 820KB 3.5" */
417 { 2952,18,2,82,0,0x25,0x00,0xDF,0x02,"h1476" }, /* 14 1.48MB 5.25" */
418 { 3444,21,2,82,0,0x25,0x00,0xDF,0x0C,"H1722" }, /* 15 1.72MB 3.5" */
419 { 840,10,2,42,1,0x25,0x01,0xDF,0x2E,"h420" }, /* 16 420KB 5.25" */
420 { 1660,10,2,83,0,0x25,0x02,0xDF,0x2E,"H830" }, /* 17 830KB 3.5" */
421 { 2988,18,2,83,0,0x25,0x00,0xDF,0x02,"h1494" }, /* 18 1.49MB 5.25" */
422 { 3486,21,2,83,0,0x25,0x00,0xDF,0x0C,"H1743" }, /* 19 1.74 MB 3.5" */
424 { 1760,11,2,80,0,0x1C,0x09,0xCF,0x00,"h880" }, /* 20 880KB 5.25" */
425 { 2080,13,2,80,0,0x1C,0x01,0xCF,0x00,"D1040" }, /* 21 1.04MB 3.5" */
426 { 2240,14,2,80,0,0x1C,0x19,0xCF,0x00,"D1120" }, /* 22 1.12MB 3.5" */
427 { 3200,20,2,80,0,0x1C,0x20,0xCF,0x2C,"h1600" }, /* 23 1.6MB 5.25" */
428 { 3520,22,2,80,0,0x1C,0x08,0xCF,0x2e,"H1760" }, /* 24 1.76MB 3.5" */
429 { 3840,24,2,80,0,0x1C,0x20,0xCF,0x00,"H1920" }, /* 25 1.92MB 3.5" */
430 { 6400,40,2,80,0,0x25,0x5B,0xCF,0x00,"E3200" }, /* 26 3.20MB 3.5" */
431 { 7040,44,2,80,0,0x25,0x5B,0xCF,0x00,"E3520" }, /* 27 3.52MB 3.5" */
432 { 7680,48,2,80,0,0x25,0x63,0xCF,0x00,"E3840" }, /* 28 3.84MB 3.5" */
434 { 3680,23,2,80,0,0x1C,0x10,0xCF,0x00,"H1840" }, /* 29 1.84MB 3.5" */
435 { 1600,10,2,80,0,0x25,0x02,0xDF,0x2E,"D800" }, /* 30 800KB 3.5" */
436 { 3200,20,2,80,0,0x1C,0x00,0xCF,0x2C,"H1600" }, /* 31 1.6MB 3.5" */
439 #define NUMBER(x) (sizeof(x) / sizeof(*(x)))
440 #define SECTSIZE (_FD_SECTSIZE(*floppy))
442 /* Auto-detection: Disk type used until the next media change occurs. */
443 static struct floppy_struct
*current_type
[N_DRIVE
] = {
444 NULL
, NULL
, NULL
, NULL
,
445 NULL
, NULL
, NULL
, NULL
449 * User-provided type information. current_type points to
450 * the respective entry of this array.
452 static struct floppy_struct user_params
[N_DRIVE
];
454 static int floppy_sizes
[256];
455 static int floppy_blocksizes
[256] = { 0, };
458 * The driver is trying to determine the correct media format
459 * while probing is set. rw_interrupt() clears it after a
462 static int probing
= 0;
464 /* Synchronization of FDC access. */
465 #define FD_COMMAND_NONE -1
466 #define FD_COMMAND_ERROR 2
467 #define FD_COMMAND_OKAY 3
469 static volatile int command_status
= FD_COMMAND_NONE
, fdc_busy
= 0;
470 static DECLARE_WAIT_QUEUE_HEAD(fdc_wait
);
471 static DECLARE_WAIT_QUEUE_HEAD(command_done
);
473 #define NO_SIGNAL (!interruptible || !signal_pending(current))
474 #define CALL(x) if ((x) == -EINTR) return -EINTR
475 #define ECALL(x) if ((ret = (x))) return ret;
476 #define _WAIT(x,i) CALL(ret=wait_til_done((x),i))
477 #define WAIT(x) _WAIT((x),interruptible)
478 #define IWAIT(x) _WAIT((x),1)
480 /* Errors during formatting are counted here. */
481 static int format_errors
;
483 /* Format request descriptor. */
484 static struct format_descr format_req
;
487 * Rate is 0 for 500kb/s, 1 for 300kbps, 2 for 250kbps
488 * Spec1 is 0xSH, where S is stepping rate (F=1ms, E=2ms, D=3ms etc),
489 * H is head unload time (1=16ms, 2=32ms, etc)
494 * Because these are written to by the DMA controller, they must
495 * not contain a 64k byte boundary crossing, or data will be
498 static char *floppy_track_buffer
=0;
499 static int max_buffer_sectors
=0;
502 typedef void (*done_f
)(int);
503 static struct cont_t
{
504 void (*interrupt
)(void); /* this is called after the interrupt of the
506 void (*redo
)(void); /* this is called to retry the operation */
507 void (*error
)(void); /* this is called to tally an error */
508 done_f done
; /* this is called to say if the operation has
509 * succeeded/failed */
512 static void floppy_ready(void);
513 static void floppy_start(void);
514 static void process_fd_request(void);
515 static void recalibrate_floppy(void);
516 static void floppy_shutdown(void);
518 static int floppy_grab_irq_and_dma(void);
519 static void floppy_release_irq_and_dma(void);
522 * The "reset" variable should be tested whenever an interrupt is scheduled,
523 * after the commands have been sent. This is to ensure that the driver doesn't
524 * get wedged when the interrupt doesn't come because of a failed command.
525 * reset doesn't need to be tested before sending commands, because
526 * output_byte is automatically disabled when reset is set.
528 #define CHECK_RESET { if (FDCS->reset){ reset_fdc(); return; } }
529 static void reset_fdc(void);
532 * These are global variables, as that's the easiest way to give
533 * information to interrupts. They are the data used for the current
537 #define NEED_1_RECAL -2
538 #define NEED_2_RECAL -3
541 static int usage_count
= 0;
544 /* buffer related variables */
545 static int buffer_track
= -1;
546 static int buffer_drive
= -1;
547 static int buffer_min
= -1;
548 static int buffer_max
= -1;
550 /* fdc related variables, should end up in a struct */
551 static struct floppy_fdc_state fdc_state
[N_FDC
];
552 static int fdc
; /* current fdc */
554 static struct floppy_struct
*_floppy
= floppy_type
;
555 static unsigned char current_drive
= 0;
556 static long current_count_sectors
= 0;
557 static unsigned char sector_t
; /* sector in track */
558 static unsigned char in_sector_offset
; /* offset within physical sector,
559 * expressed in units of 512 bytes */
562 #define fd_eject(x) -EINVAL
567 static long unsigned debugtimer
;
574 static inline void set_debugt(void)
577 debugtimer
= jiffies
;
581 static inline void debugt(const char *message
)
584 if (DP
->flags
& DEBUGT
)
585 printk("%s dtime=%lu\n", message
, jiffies
-debugtimer
);
589 typedef void (*timeout_fn
)(unsigned long);
590 static struct timer_list fd_timeout
={ NULL
, NULL
, 0, 0,
591 (timeout_fn
) floppy_shutdown
};
593 static const char *timeout_message
;
595 #ifdef FLOPPY_SANITY_CHECK
596 static void is_alive(const char *message
)
598 /* this routine checks whether the floppy driver is "alive" */
599 if (fdc_busy
&& command_status
< 2 && !fd_timeout
.prev
){
600 DPRINT("timeout handler died: %s\n",message
);
605 #ifdef FLOPPY_SANITY_CHECK
609 static void (*lasthandler
)(void) = NULL
;
610 static int interruptjiffies
=0;
611 static int resultjiffies
=0;
612 static int resultsize
=0;
613 static int lastredo
=0;
615 static struct output_log
{
617 unsigned char status
;
618 unsigned long jiffies
;
619 } output_log
[OLOGSIZE
];
621 static int output_log_pos
=0;
625 #define MAXTIMEOUT -2
627 static void reschedule_timeout(int drive
, const char *message
, int marg
)
629 if (drive
== CURRENTD
)
630 drive
= current_drive
;
631 del_timer(&fd_timeout
);
632 if (drive
< 0 || drive
> N_DRIVE
) {
633 fd_timeout
.expires
= jiffies
+ 20*HZ
;
636 fd_timeout
.expires
= jiffies
+ UDP
->timeout
;
637 add_timer(&fd_timeout
);
638 if (UDP
->flags
& FD_DEBUG
){
639 DPRINT("reschedule timeout ");
640 printk(message
, marg
);
643 timeout_message
= message
;
646 static int maximum(int a
, int b
)
653 #define INFBOUND(a,b) (a)=maximum((a),(b));
655 static int minimum(int a
, int b
)
662 #define SUPBOUND(a,b) (a)=minimum((a),(b));
666 * Bottom half floppy driver.
667 * ==========================
669 * This part of the file contains the code talking directly to the hardware,
670 * and also the main service loop (seek-configure-spinup-command)
675 * This routine is responsible for maintaining the FD_DISK_CHANGE flag,
676 * and the last_checked date.
678 * last_checked is the date of the last check which showed 'no disk change'
679 * FD_DISK_CHANGE is set under two conditions:
680 * 1. The floppy has been changed after some i/o to that floppy already
682 * 2. No floppy disk is in the drive. This is done in order to ensure that
683 * requests are quickly flushed in case there is no disk in the drive. It
684 * follows that FD_DISK_CHANGE can only be cleared if there is a disk in
687 * For 1., maxblock is observed. Maxblock is 0 if no i/o has taken place yet.
688 * For 2., FD_DISK_NEWCHANGE is watched. FD_DISK_NEWCHANGE is cleared on
689 * each seek. If a disk is present, the disk change line should also be
690 * cleared on each seek. Thus, if FD_DISK_NEWCHANGE is clear, but the disk
691 * change line is set, this means either that no disk is in the drive, or
692 * that it has been removed since the last seek.
694 * This means that we really have a third possibility too:
695 * The floppy has been changed after the last seek.
698 static int disk_change(int drive
)
701 #ifdef FLOPPY_SANITY_CHECK
702 if (jiffies
- UDRS
->select_date
< UDP
->select_delay
)
703 DPRINT("WARNING disk change called early\n");
704 if (!(FDCS
->dor
& (0x10 << UNIT(drive
))) ||
705 (FDCS
->dor
& 3) != UNIT(drive
) ||
707 DPRINT("probing disk change on unselected drive\n");
708 DPRINT("drive=%d fdc=%d dor=%x\n",drive
, FDC(drive
),
714 if (UDP
->flags
& FD_DEBUG
){
715 DPRINT("checking disk change line for drive %d\n",drive
);
716 DPRINT("jiffies=%ld\n", jiffies
);
717 DPRINT("disk change line=%x\n",fd_inb(FD_DIR
)&0x80);
718 DPRINT("flags=%lx\n",UDRS
->flags
);
721 if (UDP
->flags
& FD_BROKEN_DCL
)
722 return UTESTF(FD_DISK_CHANGED
);
723 if ((fd_inb(FD_DIR
) ^ UDP
->flags
) & 0x80){
724 USETF(FD_VERIFY
); /* verify write protection */
726 /* mark it changed */
727 USETF(FD_DISK_CHANGED
);
730 /* invalidate its geometry */
731 if (UDRS
->keep_data
>= 0) {
732 if ((UDP
->flags
& FTD_MSG
) &&
733 current_type
[drive
] != NULL
)
734 DPRINT("Disk type is undefined after "
736 current_type
[drive
] = NULL
;
737 floppy_sizes
[TOMINOR(drive
)] = MAX_DISK_SIZE
;
740 /*USETF(FD_DISK_NEWCHANGE);*/
743 UDRS
->last_checked
=jiffies
;
744 UCLEARF(FD_DISK_NEWCHANGE
);
749 static inline int is_selected(int dor
, int unit
)
751 return ((dor
& (0x10 << unit
)) && (dor
&3) == unit
);
754 static int set_dor(int fdc
, char mask
, char data
)
756 register unsigned char drive
, unit
, newdor
,olddor
;
758 if (FDCS
->address
== -1)
762 newdor
= (olddor
& mask
) | data
;
763 if (newdor
!= olddor
){
765 if (is_selected(olddor
, unit
) && !is_selected(newdor
,unit
)){
766 drive
= REVDRIVE(fdc
,unit
);
768 if (UDP
->flags
& FD_DEBUG
){
769 DPRINT("calling disk change from set_dor\n");
775 fd_outb(newdor
, FD_DOR
);
778 if (!is_selected(olddor
, unit
) && is_selected(newdor
,unit
)){
779 drive
= REVDRIVE(fdc
,unit
);
780 UDRS
->select_date
= jiffies
;
784 * We should propogate failures to grab the resources back
785 * nicely from here. Actually we ought to rewrite the fd
786 * driver some day too.
788 if (newdor
& FLOPPY_MOTOR_MASK
)
789 floppy_grab_irq_and_dma();
790 if (olddor
& FLOPPY_MOTOR_MASK
)
791 floppy_release_irq_and_dma();
795 static void twaddle(void)
797 if (DP
->select_delay
)
799 fd_outb(FDCS
->dor
& ~(0x10<<UNIT(current_drive
)),FD_DOR
);
800 fd_outb(FDCS
->dor
, FD_DOR
);
801 DRS
->select_date
= jiffies
;
804 /* reset all driver information about the current fdc. This is needed after
805 * a reset, and after a raw command. */
806 static void reset_fdc_info(int mode
)
810 FDCS
->spec1
= FDCS
->spec2
= -1;
811 FDCS
->need_configure
= 1;
814 for (drive
= 0; drive
< N_DRIVE
; drive
++)
815 if (FDC(drive
) == fdc
&&
816 (mode
|| UDRS
->track
!= NEED_1_RECAL
))
817 UDRS
->track
= NEED_2_RECAL
;
820 /* selects the fdc and drive, and enables the fdc's input/dma. */
821 static void set_fdc(int drive
)
823 if (drive
>= 0 && drive
< N_DRIVE
){
825 current_drive
= drive
;
827 if (fdc
!= 1 && fdc
!= 0) {
828 printk("bad fdc value\n");
833 set_dor(1-fdc
, ~8, 0);
835 if (FDCS
->rawcmd
== 2)
837 if (fd_inb(FD_STATUS
) != STATUS_READY
)
841 /* locks the driver */
842 static int lock_fdc(int drive
, int interruptible
)
847 printk(KERN_ERR
"Trying to lock fdc while usage count=0\n");
850 if(floppy_grab_irq_and_dma()==-1)
853 while (fdc_busy
&& NO_SIGNAL
)
854 interruptible_sleep_on(&fdc_wait
);
861 command_status
= FD_COMMAND_NONE
;
862 reschedule_timeout(drive
, "lock fdc", 0);
867 #define LOCK_FDC(drive,interruptible) \
868 if (lock_fdc(drive,interruptible)) return -EINTR;
871 /* unlocks the driver */
872 static inline void unlock_fdc(void)
876 DPRINT("FDC access conflict!\n");
879 DPRINT("device interrupt still active at FDC release: %p!\n",
881 command_status
= FD_COMMAND_NONE
;
882 del_timer(&fd_timeout
);
885 floppy_release_irq_and_dma();
889 /* switches the motor off after a given timeout */
890 static void motor_off_callback(unsigned long nr
)
892 unsigned char mask
= ~(0x10 << UNIT(nr
));
894 set_dor(FDC(nr
), mask
, 0);
897 static struct timer_list motor_off_timer
[N_DRIVE
] = {
898 { NULL
, NULL
, 0, 0, motor_off_callback
},
899 { NULL
, NULL
, 0, 1, motor_off_callback
},
900 { NULL
, NULL
, 0, 2, motor_off_callback
},
901 { NULL
, NULL
, 0, 3, motor_off_callback
},
902 { NULL
, NULL
, 0, 4, motor_off_callback
},
903 { NULL
, NULL
, 0, 5, motor_off_callback
},
904 { NULL
, NULL
, 0, 6, motor_off_callback
},
905 { NULL
, NULL
, 0, 7, motor_off_callback
}
908 /* schedules motor off */
909 static void floppy_off(unsigned int drive
)
911 unsigned long volatile delta
;
912 register int fdc
=FDC(drive
);
914 if (!(FDCS
->dor
& (0x10 << UNIT(drive
))))
917 del_timer(motor_off_timer
+drive
);
919 /* make spindle stop in a position which minimizes spinup time
922 delta
= jiffies
- UDRS
->first_read_date
+ HZ
-
923 UDP
->spindown_offset
;
924 delta
= ((delta
* UDP
->rps
) % HZ
) / UDP
->rps
;
925 motor_off_timer
[drive
].expires
= jiffies
+ UDP
->spindown
- delta
;
927 add_timer(motor_off_timer
+drive
);
931 * cycle through all N_DRIVE floppy drives, for disk change testing.
932 * stopping at current drive. This is done before any long operation, to
933 * be sure to have up to date disk change information.
935 static void scandrives(void)
937 int i
, drive
, saved_drive
;
939 if (DP
->select_delay
)
942 saved_drive
= current_drive
;
943 for (i
=0; i
< N_DRIVE
; i
++){
944 drive
= (saved_drive
+ i
+ 1) % N_DRIVE
;
945 if (UDRS
->fd_ref
== 0 || UDP
->select_delay
!= 0)
946 continue; /* skip closed drives */
948 if (!(set_dor(fdc
, ~3, UNIT(drive
) | (0x10 << UNIT(drive
))) &
949 (0x10 << UNIT(drive
))))
950 /* switch the motor off again, if it was off to
952 set_dor(fdc
, ~(0x10 << UNIT(drive
)), 0);
954 set_fdc(saved_drive
);
957 static void empty(void)
961 static struct tq_struct floppy_tq
=
964 static void schedule_bh( void (*handler
)(void*) )
966 floppy_tq
.routine
= (void *)(void *) handler
;
967 queue_task(&floppy_tq
, &tq_immediate
);
968 mark_bh(IMMEDIATE_BH
);
971 static struct timer_list fd_timer
={ NULL
, NULL
, 0, 0, 0 };
973 static void cancel_activity(void)
976 floppy_tq
.routine
= (void *)(void *) empty
;
977 del_timer(&fd_timer
);
980 /* this function makes sure that the disk stays in the drive during the
982 static void fd_watchdog(void)
985 if (DP
->flags
& FD_DEBUG
){
986 DPRINT("calling disk change from watchdog\n");
990 if (disk_change(current_drive
)){
991 DPRINT("disk removed during i/o\n");
996 del_timer(&fd_timer
);
997 fd_timer
.function
= (timeout_fn
) fd_watchdog
;
998 fd_timer
.expires
= jiffies
+ HZ
/ 10;
999 add_timer(&fd_timer
);
1003 static void main_command_interrupt(void)
1005 del_timer(&fd_timer
);
1009 /* waits for a delay (spinup or select) to pass */
1010 static int wait_for_completion(int delay
, timeout_fn function
)
1013 reset_fdc(); /* do the reset during sleep to win time
1014 * if we don't need to sleep, it's a good
1015 * occasion anyways */
1019 if ((signed) (jiffies
- delay
) < 0){
1020 del_timer(&fd_timer
);
1021 fd_timer
.function
= function
;
1022 fd_timer
.expires
= delay
;
1023 add_timer(&fd_timer
);
1029 static int hlt_disabled
=0;
1030 static void floppy_disable_hlt(void)
1032 unsigned long flags
;
1037 #ifdef HAVE_DISABLE_HLT
1044 static void floppy_enable_hlt(void)
1046 unsigned long flags
;
1051 #ifdef HAVE_DISABLE_HLT
1059 static void setup_DMA(void)
1061 unsigned long flags
;
1064 #ifdef FLOPPY_SANITY_CHECK
1065 if (raw_cmd
->length
== 0){
1068 printk("zero dma transfer size:");
1069 for (i
=0; i
< raw_cmd
->cmd_count
; i
++)
1070 printk("%x,", raw_cmd
->cmd
[i
]);
1076 if (((unsigned long) raw_cmd
->kernel_data
) % 512){
1077 printk("non aligned address: %p\n", raw_cmd
->kernel_data
);
1087 if(fd_dma_setup(raw_cmd
->kernel_data
, raw_cmd
->length
,
1088 (raw_cmd
->flags
& FD_RAW_READ
)?
1089 DMA_MODE_READ
: DMA_MODE_WRITE
,
1090 FDCS
->address
) < 0) {
1091 release_dma_lock(f
);
1097 release_dma_lock(f
);
1100 fd_cacheflush(raw_cmd
->kernel_data
, raw_cmd
->length
);
1101 fd_set_dma_mode((raw_cmd
->flags
& FD_RAW_READ
)?
1102 DMA_MODE_READ
: DMA_MODE_WRITE
);
1103 fd_set_dma_addr(raw_cmd
->kernel_data
);
1104 fd_set_dma_count(raw_cmd
->length
);
1105 virtual_dma_port
= FDCS
->address
;
1107 release_dma_lock(f
);
1110 floppy_disable_hlt();
1113 static void show_floppy(void);
1115 /* waits until the fdc becomes ready */
1116 static int wait_til_ready(void)
1118 int counter
, status
;
1121 for (counter
= 0; counter
< 10000; counter
++) {
1122 status
= fd_inb(FD_STATUS
);
1123 if (status
& STATUS_READY
)
1126 if (!initialising
) {
1127 DPRINT("Getstatus times out (%x) on fdc %d\n",
1135 /* sends a command byte to the fdc */
1136 static int output_byte(char byte
)
1140 if ((status
= wait_til_ready()) < 0)
1142 if ((status
& (STATUS_READY
|STATUS_DIR
|STATUS_DMA
)) == STATUS_READY
){
1143 fd_outb(byte
,FD_DATA
);
1144 #ifdef FLOPPY_SANITY_CHECK
1145 output_log
[output_log_pos
].data
= byte
;
1146 output_log
[output_log_pos
].status
= status
;
1147 output_log
[output_log_pos
].jiffies
= jiffies
;
1148 output_log_pos
= (output_log_pos
+ 1) % OLOGSIZE
;
1153 if (!initialising
) {
1154 DPRINT("Unable to send byte %x to FDC. Fdc=%x Status=%x\n",
1160 #define LAST_OUT(x) if (output_byte(x)<0){ reset_fdc();return;}
1162 /* gets the response from the fdc */
1163 static int result(void)
1167 for(i
=0; i
< MAX_REPLIES
; i
++) {
1168 if ((status
= wait_til_ready()) < 0)
1170 status
&= STATUS_DIR
|STATUS_READY
|STATUS_BUSY
|STATUS_DMA
;
1171 if ((status
& ~STATUS_BUSY
) == STATUS_READY
){
1172 #ifdef FLOPPY_SANITY_CHECK
1173 resultjiffies
= jiffies
;
1178 if (status
== (STATUS_DIR
|STATUS_READY
|STATUS_BUSY
))
1179 reply_buffer
[i
] = fd_inb(FD_DATA
);
1184 DPRINT("get result error. Fdc=%d Last status=%x Read bytes=%d\n",
1192 #define MORE_OUTPUT -2
1193 /* does the fdc need more output? */
1194 static int need_more_output(void)
1197 if( (status
= wait_til_ready()) < 0)
1199 if ((status
& (STATUS_READY
|STATUS_DIR
|STATUS_DMA
)) == STATUS_READY
)
1204 /* Set perpendicular mode as required, based on data rate, if supported.
1205 * 82077 Now tested. 1Mbps data rate only possible with 82077-1.
1207 static inline void perpendicular_mode(void)
1209 unsigned char perp_mode
;
1211 if (raw_cmd
->rate
& 0x40){
1212 switch(raw_cmd
->rate
& 3){
1220 DPRINT("Invalid data rate for perpendicular mode!\n");
1222 FDCS
->reset
= 1; /* convenient way to return to
1223 * redo without to much hassle (deep
1230 if (FDCS
->perp_mode
== perp_mode
)
1232 if (FDCS
->version
>= FDC_82077_ORIG
) {
1233 output_byte(FD_PERPENDICULAR
);
1234 output_byte(perp_mode
);
1235 FDCS
->perp_mode
= perp_mode
;
1236 } else if (perp_mode
) {
1237 DPRINT("perpendicular mode not supported by this FDC.\n");
1239 } /* perpendicular_mode */
1241 static int fifo_depth
= 0xa;
1242 static int no_fifo
= 0;
1244 static int fdc_configure(void)
1247 output_byte(FD_CONFIGURE
);
1248 if(need_more_output() != MORE_OUTPUT
)
1251 output_byte(0x10 | (no_fifo
& 0x20) | (fifo_depth
& 0xf));
1252 output_byte(0); /* pre-compensation from track
1257 #define NOMINAL_DTR 500
1259 /* Issue a "SPECIFY" command to set the step rate time, head unload time,
1260 * head load time, and DMA disable flag to values needed by floppy.
1262 * The value "dtr" is the data transfer rate in Kbps. It is needed
1263 * to account for the data rate-based scaling done by the 82072 and 82077
1264 * FDC types. This parameter is ignored for other types of FDCs (i.e.
1267 * Note that changing the data transfer rate has a (probably deleterious)
1268 * effect on the parameters subject to scaling for 82072/82077 FDCs, so
1269 * fdc_specify is called again after each data transfer rate
1272 * srt: 1000 to 16000 in microseconds
1273 * hut: 16 to 240 milliseconds
1274 * hlt: 2 to 254 milliseconds
1276 * These values are rounded up to the next highest available delay time.
1278 static void fdc_specify(void)
1280 unsigned char spec1
, spec2
;
1282 unsigned long dtr
= NOMINAL_DTR
;
1283 unsigned long scale_dtr
= NOMINAL_DTR
;
1284 int hlt_max_code
= 0x7f;
1285 int hut_max_code
= 0xf;
1287 if (FDCS
->need_configure
&& FDCS
->version
>= FDC_82072A
) {
1289 FDCS
->need_configure
= 0;
1290 /*DPRINT("FIFO enabled\n");*/
1293 switch (raw_cmd
->rate
& 0x03) {
1299 if (FDCS
->version
>= FDC_82078
) {
1300 /* chose the default rate table, not the one
1301 * where 1 = 2 Mbps */
1302 output_byte(FD_DRIVESPEC
);
1303 if(need_more_output() == MORE_OUTPUT
) {
1304 output_byte(UNIT(current_drive
));
1314 if (FDCS
->version
>= FDC_82072
) {
1316 hlt_max_code
= 0x00; /* 0==256msec*dtr0/dtr (not linear!) */
1317 hut_max_code
= 0x0; /* 0==256msec*dtr0/dtr (not linear!) */
1320 /* Convert step rate from microseconds to milliseconds and 4 bits */
1321 srt
= 16 - (DP
->srt
*scale_dtr
/1000 + NOMINAL_DTR
- 1)/NOMINAL_DTR
;
1328 hlt
= (DP
->hlt
*scale_dtr
/2 + NOMINAL_DTR
- 1)/NOMINAL_DTR
;
1331 else if (hlt
> 0x7f)
1334 hut
= (DP
->hut
*scale_dtr
/16 + NOMINAL_DTR
- 1)/NOMINAL_DTR
;
1340 spec1
= (srt
<< 4) | hut
;
1341 spec2
= (hlt
<< 1) | (use_virtual_dma
& 1);
1343 /* If these parameters did not change, just return with success */
1344 if (FDCS
->spec1
!= spec1
|| FDCS
->spec2
!= spec2
) {
1345 /* Go ahead and set spec1 and spec2 */
1346 output_byte(FD_SPECIFY
);
1347 output_byte(FDCS
->spec1
= spec1
);
1348 output_byte(FDCS
->spec2
= spec2
);
1352 /* Set the FDC's data transfer rate on behalf of the specified drive.
1353 * NOTE: with 82072/82077 FDCs, changing the data rate requires a reissue
1354 * of the specify command (i.e. using the fdc_specify function).
1356 static int fdc_dtr(void)
1358 /* If data rate not already set to desired value, set it. */
1359 if ((raw_cmd
->rate
& 3) == FDCS
->dtr
)
1363 fd_outb(raw_cmd
->rate
& 3, FD_DCR
);
1365 /* TODO: some FDC/drive combinations (C&T 82C711 with TEAC 1.2MB)
1366 * need a stabilization period of several milliseconds to be
1367 * enforced after data rate changes before R/W operations.
1368 * Pause 5 msec to avoid trouble. (Needs to be 2 jiffies)
1370 FDCS
->dtr
= raw_cmd
->rate
& 3;
1371 return(wait_for_completion(jiffies
+2*HZ
/100,
1372 (timeout_fn
) floppy_ready
));
1375 static void tell_sector(void)
1377 printk(": track %d, head %d, sector %d, size %d",
1378 R_TRACK
, R_HEAD
, R_SECTOR
, R_SIZECODE
);
1383 * OK, this error interpreting routine is called after a
1384 * DMA read/write has succeeded
1385 * or failed, so we check the results, and copy any buffers.
1386 * hhb: Added better error reporting.
1387 * ak: Made this into a separate routine.
1389 static int interpret_errors(void)
1394 DPRINT("-- FDC reply error");
1399 /* check IC to find cause of interrupt */
1400 switch (ST0
& ST0_INTR
) {
1401 case 0x40: /* error occurred during command execution */
1403 return 0; /* occurs with pseudo-DMA */
1406 DPRINT("Drive is write protected\n");
1407 CLEARF(FD_DISK_WRITABLE
);
1410 } else if (ST1
& ST1_ND
) {
1411 SETF(FD_NEED_TWADDLE
);
1412 } else if (ST1
& ST1_OR
) {
1413 if (DP
->flags
& FTD_MSG
)
1414 DPRINT("Over/Underrun - retrying\n");
1416 }else if (*errors
>= DP
->max_errors
.reporting
){
1418 if (ST0
& ST0_ECE
) {
1419 printk("Recalibrate failed!");
1420 } else if (ST2
& ST2_CRC
) {
1421 printk("data CRC error");
1423 } else if (ST1
& ST1_CRC
) {
1424 printk("CRC error");
1426 } else if ((ST1
& (ST1_MAM
|ST1_ND
)) || (ST2
& ST2_MAM
)) {
1428 printk("sector not found");
1431 printk("probe failed...");
1432 } else if (ST2
& ST2_WC
) { /* seek error */
1433 printk("wrong cylinder");
1434 } else if (ST2
& ST2_BC
) { /* cylinder marked as bad */
1435 printk("bad cylinder");
1437 printk("unknown error. ST[0..2] are: 0x%x 0x%x 0x%x", ST0
, ST1
, ST2
);
1443 if (ST2
& ST2_WC
|| ST2
& ST2_BC
)
1444 /* wrong cylinder => recal */
1445 DRS
->track
= NEED_2_RECAL
;
1447 case 0x80: /* invalid command given */
1448 DPRINT("Invalid FDC command given!\n");
1452 DPRINT("Abnormal termination caused by polling\n");
1455 default: /* (0) Normal command termination */
1461 * This routine is called when everything should be correctly set up
1462 * for the transfer (i.e. floppy motor is on, the correct floppy is
1463 * selected, and the head is sitting on the right track).
1465 static void setup_rw_floppy(void)
1467 int i
,ready_date
,r
, flags
,dflags
;
1468 timeout_fn function
;
1470 flags
= raw_cmd
->flags
;
1471 if (flags
& (FD_RAW_READ
| FD_RAW_WRITE
))
1472 flags
|= FD_RAW_INTR
;
1474 if ((flags
& FD_RAW_SPIN
) && !(flags
& FD_RAW_NO_MOTOR
)){
1475 ready_date
= DRS
->spinup_date
+ DP
->spinup
;
1476 /* If spinup will take a long time, rerun scandrives
1477 * again just before spinup completion. Beware that
1478 * after scandrives, we must again wait for selection.
1480 if ((signed) (ready_date
- jiffies
) > DP
->select_delay
){
1481 ready_date
-= DP
->select_delay
;
1482 function
= (timeout_fn
) floppy_start
;
1484 function
= (timeout_fn
) setup_rw_floppy
;
1486 /* wait until the floppy is spinning fast enough */
1487 if (wait_for_completion(ready_date
,function
))
1490 dflags
= DRS
->flags
;
1492 if ((flags
& FD_RAW_READ
) || (flags
& FD_RAW_WRITE
))
1495 if (flags
& FD_RAW_INTR
)
1496 SET_INTR(main_command_interrupt
);
1499 for (i
=0; i
< raw_cmd
->cmd_count
; i
++)
1500 r
|=output_byte(raw_cmd
->cmd
[i
]);
1503 debugt("rw_command: ");
1511 if (!(flags
& FD_RAW_INTR
)){
1514 } else if (flags
& FD_RAW_NEED_DISK
)
1518 static int blind_seek
;
1521 * This is the routine called after every seek (or recalibrate) interrupt
1522 * from the floppy controller.
1524 static void seek_interrupt(void)
1527 debugt("seek interrupt:");
1529 if (inr
!= 2 || (ST0
& 0xF8) != 0x20) {
1530 DPRINT("seek failed\n");
1531 DRS
->track
= NEED_2_RECAL
;
1536 if (DRS
->track
>= 0 && DRS
->track
!= ST1
&& !blind_seek
){
1538 if (DP
->flags
& FD_DEBUG
){
1539 DPRINT("clearing NEWCHANGE flag because of effective seek\n");
1540 DPRINT("jiffies=%ld\n", jiffies
);
1543 CLEARF(FD_DISK_NEWCHANGE
); /* effective seek */
1544 DRS
->select_date
= jiffies
;
1550 static void check_wp(void)
1552 if (TESTF(FD_VERIFY
)) {
1553 /* check write protection */
1554 output_byte(FD_GETSTATUS
);
1555 output_byte(UNIT(current_drive
));
1561 CLEARF(FD_NEED_TWADDLE
);
1563 if (DP
->flags
& FD_DEBUG
){
1564 DPRINT("checking whether disk is write protected\n");
1565 DPRINT("wp=%x\n",ST3
& 0x40);
1569 SETF(FD_DISK_WRITABLE
);
1571 CLEARF(FD_DISK_WRITABLE
);
1575 static void seek_floppy(void)
1582 if (DP
->flags
& FD_DEBUG
){
1583 DPRINT("calling disk change from seek\n");
1587 if (!TESTF(FD_DISK_NEWCHANGE
) &&
1588 disk_change(current_drive
) &&
1589 (raw_cmd
->flags
& FD_RAW_NEED_DISK
)){
1590 /* the media changed flag should be cleared after the seek.
1591 * If it isn't, this means that there is really no disk in
1594 SETF(FD_DISK_CHANGED
);
1599 if (DRS
->track
<= NEED_1_RECAL
){
1600 recalibrate_floppy();
1602 } else if (TESTF(FD_DISK_NEWCHANGE
) &&
1603 (raw_cmd
->flags
& FD_RAW_NEED_DISK
) &&
1604 (DRS
->track
<= NO_TRACK
|| DRS
->track
== raw_cmd
->track
)) {
1605 /* we seek to clear the media-changed condition. Does anybody
1606 * know a more elegant way, which works on all drives? */
1608 track
= raw_cmd
->track
- 1;
1610 if (DP
->flags
& FD_SILENT_DCL_CLEAR
){
1611 set_dor(fdc
, ~(0x10 << UNIT(current_drive
)), 0);
1613 raw_cmd
->flags
|= FD_RAW_NEED_SEEK
;
1619 if (raw_cmd
->track
!= DRS
->track
&&
1620 (raw_cmd
->flags
& FD_RAW_NEED_SEEK
))
1621 track
= raw_cmd
->track
;
1628 SET_INTR(seek_interrupt
);
1629 output_byte(FD_SEEK
);
1630 output_byte(UNIT(current_drive
));
1633 debugt("seek command:");
1637 static void recal_interrupt(void)
1640 debugt("recal interrupt:");
1644 else if (ST0
& ST0_ECE
) {
1648 debugt("recal interrupt need 1 recal:");
1650 /* after a second recalibrate, we still haven't
1651 * reached track 0. Probably no drive. Raise an
1652 * error, as failing immediately might upset
1653 * computers possessed by the Devil :-) */
1659 debugt("recal interrupt need 2 recal:");
1661 /* If we already did a recalibrate,
1662 * and we are not at track 0, this
1663 * means we have moved. (The only way
1664 * not to move at recalibration is to
1665 * be already at track 0.) Clear the
1666 * new change flag */
1668 if (DP
->flags
& FD_DEBUG
){
1669 DPRINT("clearing NEWCHANGE flag because of second recalibrate\n");
1673 CLEARF(FD_DISK_NEWCHANGE
);
1674 DRS
->select_date
= jiffies
;
1678 debugt("recal interrupt default:");
1680 /* Recalibrate moves the head by at
1681 * most 80 steps. If after one
1682 * recalibrate we don't have reached
1683 * track 0, this might mean that we
1684 * started beyond track 80. Try
1686 DRS
->track
= NEED_1_RECAL
;
1694 static void print_result(char *message
, int inr
)
1698 DPRINT("%s ", message
);
1700 for (i
=0; i
<inr
; i
++)
1701 printk("repl[%d]=%x ", i
, reply_buffer
[i
]);
1705 /* interrupt handler. Note that this can be called externally on the Sparc */
1706 void floppy_interrupt(int irq
, void *dev_id
, struct pt_regs
* regs
)
1708 void (*handler
)(void) = DEVICE_INTR
;
1712 lasthandler
= handler
;
1713 interruptjiffies
= jiffies
;
1717 release_dma_lock(f
);
1719 floppy_enable_hlt();
1721 if (fdc
>= N_FDC
|| FDCS
->address
== -1){
1722 /* we don't even know which FDC is the culprit */
1723 printk("DOR0=%x\n", fdc_state
[0].dor
);
1724 printk("floppy interrupt on bizarre fdc %d\n",fdc
);
1725 printk("handler=%p\n", handler
);
1726 is_alive("bizarre fdc");
1731 /* We have to clear the reset flag here, because apparently on boxes
1732 * with level triggered interrupts (PS/2, Sparc, ...), it is needed to
1733 * emit SENSEI's to clear the interrupt line. And FDCS->reset blocks the
1734 * emission of the SENSEI's.
1735 * It is OK to emit floppy commands because we are in an interrupt
1736 * handler here, and thus we have to fear no interference of other
1740 do_print
= !handler
&& print_unex
&& !initialising
;
1744 print_result("unexpected interrupt", inr
);
1748 output_byte(FD_SENSEI
);
1751 print_result("sensei", inr
);
1753 } while ((ST0
& 0x83) != UNIT(current_drive
) && inr
== 2 && max_sensei
);
1756 int cpu
= smp_processor_id();
1757 if(softirq_trylock(cpu
)) {
1758 /* got the lock, call the handler immediately */
1760 softirq_endlock(cpu
);
1762 /* we interrupted a bottom half. Defer handler */
1763 schedule_bh( (void *)(void *) handler
);
1766 is_alive("normal interrupt end");
1769 static void recalibrate_floppy(void)
1772 debugt("recalibrate floppy:");
1774 SET_INTR(recal_interrupt
);
1775 output_byte(FD_RECALIBRATE
);
1776 LAST_OUT(UNIT(current_drive
));
1780 * Must do 4 FD_SENSEIs after reset because of ``drive polling''.
1782 static void reset_interrupt(void)
1785 debugt("reset interrupt:");
1787 result(); /* get the status ready for set_fdc */
1789 printk("reset set in interrupt, calling %p\n", cont
->error
);
1790 cont
->error(); /* a reset just after a reset. BAD! */
1796 * reset is done by pulling bit 2 of DOR low for a while (old FDCs),
1797 * or by setting the self clearing bit 7 of STATUS (newer FDCs)
1799 static void reset_fdc(void)
1801 unsigned long flags
;
1803 SET_INTR(reset_interrupt
);
1807 /* Pseudo-DMA may intercept 'reset finished' interrupt. */
1808 /* Irrelevant for systems with true DMA (i386). */
1810 flags
=claim_dma_lock();
1812 release_dma_lock(flags
);
1814 if (FDCS
->version
>= FDC_82072A
)
1815 fd_outb(0x80 | (FDCS
->dtr
&3), FD_STATUS
);
1817 fd_outb(FDCS
->dor
& ~0x04, FD_DOR
);
1818 udelay(FD_RESET_DELAY
);
1819 fd_outb(FDCS
->dor
, FD_DOR
);
1823 static void show_floppy(void)
1828 printk("floppy driver state\n");
1829 printk("-------------------\n");
1830 printk("now=%ld last interrupt=%d last called handler=%p\n",
1831 jiffies
, interruptjiffies
, lasthandler
);
1834 #ifdef FLOPPY_SANITY_CHECK
1835 printk("timeout_message=%s\n", timeout_message
);
1836 printk("last output bytes:\n");
1837 for (i
=0; i
< OLOGSIZE
; i
++)
1838 printk("%2x %2x %ld\n",
1839 output_log
[(i
+output_log_pos
) % OLOGSIZE
].data
,
1840 output_log
[(i
+output_log_pos
) % OLOGSIZE
].status
,
1841 output_log
[(i
+output_log_pos
) % OLOGSIZE
].jiffies
);
1842 printk("last result at %d\n", resultjiffies
);
1843 printk("last redo_fd_request at %d\n", lastredo
);
1844 for (i
=0; i
<resultsize
; i
++){
1845 printk("%2x ", reply_buffer
[i
]);
1850 printk("status=%x\n", fd_inb(FD_STATUS
));
1851 printk("fdc_busy=%d\n", fdc_busy
);
1853 printk("DEVICE_INTR=%p\n", DEVICE_INTR
);
1855 printk("floppy_tq.routine=%p\n", floppy_tq
.routine
);
1857 printk("fd_timer.function=%p\n", fd_timer
.function
);
1858 if (fd_timeout
.prev
){
1859 printk("timer_table=%p\n",fd_timeout
.function
);
1860 printk("expires=%ld\n",fd_timeout
.expires
-jiffies
);
1861 printk("now=%ld\n",jiffies
);
1863 printk("cont=%p\n", cont
);
1864 printk("CURRENT=%p\n", CURRENT
);
1865 printk("command_status=%d\n", command_status
);
1869 static void floppy_shutdown(void)
1871 unsigned long flags
;
1877 floppy_enable_hlt();
1879 flags
=claim_dma_lock();
1881 release_dma_lock(flags
);
1883 /* avoid dma going to a random drive after shutdown */
1886 DPRINT("floppy timeout called\n");
1890 cont
->redo(); /* this will recall reset when needed */
1892 printk("no cont in shutdown!\n");
1893 process_fd_request();
1895 is_alive("floppy shutdown");
1897 /*typedef void (*timeout_fn)(unsigned long);*/
1899 /* start motor, check media-changed condition and write protection */
1900 static int start_motor(void (*function
)(void) )
1905 data
= UNIT(current_drive
);
1906 if (!(raw_cmd
->flags
& FD_RAW_NO_MOTOR
)){
1907 if (!(FDCS
->dor
& (0x10 << UNIT(current_drive
)))){
1909 /* no read since this drive is running */
1910 DRS
->first_read_date
= 0;
1911 /* note motor start time if motor is not yet running */
1912 DRS
->spinup_date
= jiffies
;
1913 data
|= (0x10 << UNIT(current_drive
));
1916 if (FDCS
->dor
& (0x10 << UNIT(current_drive
)))
1917 mask
&= ~(0x10 << UNIT(current_drive
));
1919 /* starts motor and selects floppy */
1920 del_timer(motor_off_timer
+ current_drive
);
1921 set_dor(fdc
, mask
, data
);
1923 /* wait_for_completion also schedules reset if needed. */
1924 return(wait_for_completion(DRS
->select_date
+DP
->select_delay
,
1925 (timeout_fn
) function
));
1928 static void floppy_ready(void)
1931 if (start_motor(floppy_ready
)) return;
1932 if (fdc_dtr()) return;
1935 if (DP
->flags
& FD_DEBUG
){
1936 DPRINT("calling disk change from floppy_ready\n");
1939 if (!(raw_cmd
->flags
& FD_RAW_NO_MOTOR
) &&
1940 disk_change(current_drive
) &&
1942 twaddle(); /* this clears the dcl on certain drive/controller
1945 #ifdef fd_chose_dma_mode
1946 if ((raw_cmd
->flags
& FD_RAW_READ
) ||
1947 (raw_cmd
->flags
& FD_RAW_WRITE
))
1949 unsigned long flags
= claim_dma_lock();
1950 fd_chose_dma_mode(raw_cmd
->kernel_data
,
1952 release_dma_lock(flags
);
1956 if (raw_cmd
->flags
& (FD_RAW_NEED_SEEK
| FD_RAW_NEED_DISK
)){
1957 perpendicular_mode();
1958 fdc_specify(); /* must be done here because of hut, hlt ... */
1961 if ((raw_cmd
->flags
& FD_RAW_READ
) ||
1962 (raw_cmd
->flags
& FD_RAW_WRITE
))
1968 static void floppy_start(void)
1970 reschedule_timeout(CURRENTD
, "floppy start", 0);
1974 if (DP
->flags
& FD_DEBUG
){
1975 DPRINT("setting NEWCHANGE in floppy_start\n");
1978 SETF(FD_DISK_NEWCHANGE
);
1983 * ========================================================================
1984 * here ends the bottom half. Exported routines are:
1985 * floppy_start, floppy_off, floppy_ready, lock_fdc, unlock_fdc, set_fdc,
1986 * start_motor, reset_fdc, reset_fdc_info, interpret_errors.
1987 * Initialization also uses output_byte, result, set_dor, floppy_interrupt
1989 * ========================================================================
1992 * General purpose continuations.
1993 * ==============================
1996 static void do_wakeup(void)
1998 reschedule_timeout(MAXTIMEOUT
, "do wakeup", 0);
2000 command_status
+= 2;
2001 wake_up(&command_done
);
2004 static struct cont_t wakeup_cont
={
2012 static struct cont_t intr_cont
={
2019 static int wait_til_done(void (*handler
)(void), int interruptible
)
2022 unsigned long flags
;
2024 schedule_bh((void *)(void *)handler
);
2026 while(command_status
< 2 && NO_SIGNAL
){
2027 is_alive("wait_til_done");
2029 interruptible_sleep_on(&command_done
);
2031 sleep_on(&command_done
);
2033 if (command_status
< 2){
2043 command_status
= FD_COMMAND_ERROR
;
2044 if (command_status
== FD_COMMAND_OKAY
)
2048 command_status
= FD_COMMAND_NONE
;
2052 static void generic_done(int result
)
2054 command_status
= result
;
2055 cont
= &wakeup_cont
;
2058 static void generic_success(void)
2063 static void generic_failure(void)
2068 static void success_and_wakeup(void)
2076 * formatting and rw support.
2077 * ==========================
2080 static int next_valid_format(void)
2084 probed_format
= DRS
->probed_format
;
2086 if (probed_format
>= 8 ||
2087 !DP
->autodetect
[probed_format
]){
2088 DRS
->probed_format
= 0;
2091 if (floppy_type
[DP
->autodetect
[probed_format
]].sect
){
2092 DRS
->probed_format
= probed_format
;
2099 static void bad_flp_intr(void)
2102 DRS
->probed_format
++;
2103 if (!next_valid_format())
2107 INFBOUND(DRWE
->badness
, *errors
);
2108 if (*errors
> DP
->max_errors
.abort
)
2110 if (*errors
> DP
->max_errors
.reset
)
2112 else if (*errors
> DP
->max_errors
.recal
)
2113 DRS
->track
= NEED_2_RECAL
;
2116 static void set_floppy(kdev_t device
)
2119 _floppy
= TYPE(device
) + floppy_type
;
2121 _floppy
= current_type
[ DRIVE(device
) ];
2125 * formatting support.
2126 * ===================
2128 static void format_interrupt(void)
2130 switch (interpret_errors()){
2141 #define CODE2SIZE (ssize = ((1 << SIZECODE) + 3) >> 2)
2142 #define FM_MODE(x,y) ((y) & ~(((x)->rate & 0x80) >>1))
2143 #define CT(x) ((x) | 0xc0)
2144 static void setup_format_params(int track
)
2147 unsigned char track
,head
,sect
,size
;
2148 } *here
= (struct fparm
*)floppy_track_buffer
;
2150 int count
,head_shift
,track_shift
;
2152 raw_cmd
= &default_raw_cmd
;
2153 raw_cmd
->track
= track
;
2155 raw_cmd
->flags
= FD_RAW_WRITE
| FD_RAW_INTR
| FD_RAW_SPIN
|
2156 FD_RAW_NEED_DISK
| FD_RAW_NEED_SEEK
;
2157 raw_cmd
->rate
= _floppy
->rate
& 0x43;
2158 raw_cmd
->cmd_count
= NR_F
;
2159 COMMAND
= FM_MODE(_floppy
,FD_FORMAT
);
2160 DR_SELECT
= UNIT(current_drive
) + PH_HEAD(_floppy
,format_req
.head
);
2161 F_SIZECODE
= FD_SIZECODE(_floppy
);
2162 F_SECT_PER_TRACK
= _floppy
->sect
<< 2 >> F_SIZECODE
;
2163 F_GAP
= _floppy
->fmt_gap
;
2164 F_FILL
= FD_FILL_BYTE
;
2166 raw_cmd
->kernel_data
= floppy_track_buffer
;
2167 raw_cmd
->length
= 4 * F_SECT_PER_TRACK
;
2169 /* allow for about 30ms for data transport per track */
2170 head_shift
= (F_SECT_PER_TRACK
+ 5) / 6;
2172 /* a ``cylinder'' is two tracks plus a little stepping time */
2173 track_shift
= 2 * head_shift
+ 3;
2175 /* position of logical sector 1 on this track */
2176 n
= (track_shift
* format_req
.track
+ head_shift
* format_req
.head
)
2179 /* determine interleave */
2181 if (_floppy
->fmt_gap
< 0x22)
2184 /* initialize field */
2185 for (count
= 0; count
< F_SECT_PER_TRACK
; ++count
) {
2186 here
[count
].track
= format_req
.track
;
2187 here
[count
].head
= format_req
.head
;
2188 here
[count
].sect
= 0;
2189 here
[count
].size
= F_SIZECODE
;
2191 /* place logical sectors */
2192 for (count
= 1; count
<= F_SECT_PER_TRACK
; ++count
) {
2193 here
[n
].sect
= count
;
2194 n
= (n
+il
) % F_SECT_PER_TRACK
;
2195 if (here
[n
].sect
) { /* sector busy, find next free sector */
2197 if (n
>= F_SECT_PER_TRACK
) {
2198 n
-=F_SECT_PER_TRACK
;
2199 while (here
[n
].sect
) ++n
;
2205 static void redo_format(void)
2208 setup_format_params(format_req
.track
<< STRETCH(_floppy
));
2211 debugt("queue format request");
2215 static struct cont_t format_cont
={
2221 static int do_format(kdev_t device
, struct format_descr
*tmp_format_req
)
2224 int drive
=DRIVE(device
);
2229 _floppy
->track
> DP
->tracks
||
2230 tmp_format_req
->track
>= _floppy
->track
||
2231 tmp_format_req
->head
>= _floppy
->head
||
2232 (_floppy
->sect
<< 2) % (1 << FD_SIZECODE(_floppy
)) ||
2233 !_floppy
->fmt_gap
) {
2234 process_fd_request();
2237 format_req
= *tmp_format_req
;
2239 cont
= &format_cont
;
2240 errors
= &format_errors
;
2242 process_fd_request();
2247 * Buffer read/write and support
2248 * =============================
2251 /* new request_done. Can handle physical sectors which are smaller than a
2253 static void request_done(int uptodate
)
2258 reschedule_timeout(MAXTIMEOUT
, "request done %d", uptodate
);
2261 DPRINT("request list destroyed in floppy request done\n");
2266 /* maintain values for invalidation on geometry
2268 block
= current_count_sectors
+ CURRENT
->sector
;
2269 INFBOUND(DRS
->maxblock
, block
);
2270 if (block
> _floppy
->sect
)
2273 /* unlock chained buffers */
2274 while (current_count_sectors
&& CURRENT
&&
2275 current_count_sectors
>= CURRENT
->current_nr_sectors
){
2276 current_count_sectors
-= CURRENT
->current_nr_sectors
;
2277 CURRENT
->nr_sectors
-= CURRENT
->current_nr_sectors
;
2278 CURRENT
->sector
+= CURRENT
->current_nr_sectors
;
2281 if (current_count_sectors
&& CURRENT
){
2282 /* "unlock" last subsector */
2283 CURRENT
->buffer
+= current_count_sectors
<<9;
2284 CURRENT
->current_nr_sectors
-= current_count_sectors
;
2285 CURRENT
->nr_sectors
-= current_count_sectors
;
2286 CURRENT
->sector
+= current_count_sectors
;
2290 if (current_count_sectors
&& !CURRENT
)
2291 DPRINT("request list destroyed in floppy request done\n");
2294 if (CURRENT
->cmd
== WRITE
) {
2295 /* record write error information */
2296 DRWE
->write_errors
++;
2297 if (DRWE
->write_errors
== 1) {
2298 DRWE
->first_error_sector
= CURRENT
->sector
;
2299 DRWE
->first_error_generation
= DRS
->generation
;
2301 DRWE
->last_error_sector
= CURRENT
->sector
;
2302 DRWE
->last_error_generation
= DRS
->generation
;
2308 /* Interrupt handler evaluating the result of the r/w operation */
2309 static void rw_interrupt(void)
2311 int nr_sectors
, ssize
, eoc
, heads
;
2313 if (!DRS
->first_read_date
)
2314 DRS
->first_read_date
= jiffies
;
2329 nr_sectors
= (((R_TRACK
-TRACK
) * heads
+
2330 R_HEAD
-HEAD
) * SECT_PER_TRACK
+
2331 R_SECTOR
-SECTOR
+ eoc
) << SIZECODE
>> 2;
2333 #ifdef FLOPPY_SANITY_CHECK
2334 if (nr_sectors
/ ssize
>
2335 (in_sector_offset
+ current_count_sectors
+ ssize
- 1)/ssize
) {
2336 DPRINT("long rw: %x instead of %lx\n",
2337 nr_sectors
, current_count_sectors
);
2338 printk("rs=%d s=%d\n", R_SECTOR
, SECTOR
);
2339 printk("rh=%d h=%d\n", R_HEAD
, HEAD
);
2340 printk("rt=%d t=%d\n", R_TRACK
, TRACK
);
2341 printk("heads=%d eoc=%d\n", heads
, eoc
);
2342 printk("spt=%d st=%d ss=%d\n", SECT_PER_TRACK
,
2344 printk("in_sector_offset=%d\n", in_sector_offset
);
2348 nr_sectors
-= in_sector_offset
;
2349 INFBOUND(nr_sectors
,0);
2350 SUPBOUND(current_count_sectors
, nr_sectors
);
2352 switch (interpret_errors()){
2357 if (!current_count_sectors
){
2364 if (!current_count_sectors
){
2368 current_type
[current_drive
] = _floppy
;
2369 floppy_sizes
[TOMINOR(current_drive
) ]=
2370 (_floppy
->size
+1)>>1;
2375 if (DP
->flags
& FTD_MSG
)
2376 DPRINT("Auto-detected floppy type %s in fd%d\n",
2377 _floppy
->name
,current_drive
);
2378 current_type
[current_drive
] = _floppy
;
2379 floppy_sizes
[TOMINOR(current_drive
)] = (_floppy
->size
+1) >> 1;
2383 if (CT(COMMAND
) != FD_READ
||
2384 raw_cmd
->kernel_data
== CURRENT
->buffer
){
2385 /* transfer directly from buffer */
2387 } else if (CT(COMMAND
) == FD_READ
){
2388 buffer_track
= raw_cmd
->track
;
2389 buffer_drive
= current_drive
;
2390 INFBOUND(buffer_max
, nr_sectors
+ sector_t
);
2395 /* Compute maximal contiguous buffer size. */
2396 static int buffer_chain_size(void)
2398 struct buffer_head
*bh
;
2402 base
= CURRENT
->buffer
;
2403 size
= CURRENT
->current_nr_sectors
<< 9;
2408 while (bh
&& bh
->b_data
== base
+ size
){
2416 /* Compute the maximal transfer size */
2417 static int transfer_size(int ssize
, int max_sector
, int max_size
)
2419 SUPBOUND(max_sector
, sector_t
+ max_size
);
2422 max_sector
-= (max_sector
% _floppy
->sect
) % ssize
;
2424 /* transfer size, beginning not aligned */
2425 current_count_sectors
= max_sector
- sector_t
;
2431 * Move data from/to the track buffer to/from the buffer cache.
2433 static void copy_buffer(int ssize
, int max_sector
, int max_sector_2
)
2435 int remaining
; /* number of transferred 512-byte sectors */
2436 struct buffer_head
*bh
;
2437 char *buffer
, *dma_buffer
;
2440 max_sector
= transfer_size(ssize
,
2441 minimum(max_sector
, max_sector_2
),
2442 CURRENT
->nr_sectors
);
2444 if (current_count_sectors
<= 0 && CT(COMMAND
) == FD_WRITE
&&
2445 buffer_max
> sector_t
+ CURRENT
->nr_sectors
)
2446 current_count_sectors
= minimum(buffer_max
- sector_t
,
2447 CURRENT
->nr_sectors
);
2449 remaining
= current_count_sectors
<< 9;
2450 #ifdef FLOPPY_SANITY_CHECK
2451 if ((remaining
>> 9) > CURRENT
->nr_sectors
&&
2452 CT(COMMAND
) == FD_WRITE
){
2453 DPRINT("in copy buffer\n");
2454 printk("current_count_sectors=%ld\n", current_count_sectors
);
2455 printk("remaining=%d\n", remaining
>> 9);
2456 printk("CURRENT->nr_sectors=%ld\n",CURRENT
->nr_sectors
);
2457 printk("CURRENT->current_nr_sectors=%ld\n",
2458 CURRENT
->current_nr_sectors
);
2459 printk("max_sector=%d\n", max_sector
);
2460 printk("ssize=%d\n", ssize
);
2464 buffer_max
= maximum(max_sector
, buffer_max
);
2466 dma_buffer
= floppy_track_buffer
+ ((sector_t
- buffer_min
) << 9);
2469 size
= CURRENT
->current_nr_sectors
<< 9;
2470 buffer
= CURRENT
->buffer
;
2472 while (remaining
> 0){
2473 SUPBOUND(size
, remaining
);
2474 #ifdef FLOPPY_SANITY_CHECK
2475 if (dma_buffer
+ size
>
2476 floppy_track_buffer
+ (max_buffer_sectors
<< 10) ||
2477 dma_buffer
< floppy_track_buffer
){
2478 DPRINT("buffer overrun in copy buffer %d\n",
2479 (int) ((floppy_track_buffer
- dma_buffer
) >>9));
2480 printk("sector_t=%d buffer_min=%d\n",
2481 sector_t
, buffer_min
);
2482 printk("current_count_sectors=%ld\n",
2483 current_count_sectors
);
2484 if (CT(COMMAND
) == FD_READ
)
2486 if (CT(COMMAND
) == FD_READ
)
2490 if (((unsigned long)buffer
) % 512)
2491 DPRINT("%p buffer not aligned\n", buffer
);
2493 if (CT(COMMAND
) == FD_READ
)
2494 memcpy(buffer
, dma_buffer
, size
);
2496 memcpy(dma_buffer
, buffer
, size
);
2503 #ifdef FLOPPY_SANITY_CHECK
2505 DPRINT("bh=null in copy buffer after copy\n");
2510 buffer
= bh
->b_data
;
2512 #ifdef FLOPPY_SANITY_CHECK
2515 max_sector
-= remaining
>> 9;
2516 DPRINT("weirdness: remaining %d\n", remaining
>>9);
2522 static inline int check_dma_crossing(char *start
,
2523 unsigned long length
, char *message
)
2525 if (CROSS_64KB(start
, length
)) {
2526 printk("DMA xfer crosses 64KB boundary in %s %p-%p\n",
2527 message
, start
, start
+length
);
2534 /* work around a bug in pseudo DMA
2535 * (on some FDCs) pseudo DMA does not stop when the CPU stops
2536 * sending data. Hence we need a different way to signal the
2537 * transfer length: We use SECT_PER_TRACK. Unfortunately, this
2538 * does not work with MT, hence we can only transfer one head at
2541 static void virtualdmabug_workaround(void) {
2542 int hard_sectors
, end_sector
;
2543 if(CT(COMMAND
) == FD_WRITE
) {
2544 COMMAND
&= ~0x80; /* switch off multiple track mode */
2546 hard_sectors
= raw_cmd
->length
>> (7 + SIZECODE
);
2547 end_sector
= SECTOR
+ hard_sectors
- 1;
2548 #ifdef FLOPPY_SANITY_CHECK
2549 if(end_sector
> SECT_PER_TRACK
) {
2550 printk("too many sectors %d > %d\n",
2551 end_sector
, SECT_PER_TRACK
);
2555 SECT_PER_TRACK
= end_sector
; /* make sure SECT_PER_TRACK points
2556 * to end of transfer */
2561 * Formulate a read/write request.
2562 * this routine decides where to load the data (directly to buffer, or to
2563 * tmp floppy area), how much data to load (the size of the buffer, the whole
2564 * track, or a single sector)
2565 * All floppy_track_buffer handling goes in here. If we ever add track buffer
2566 * allocation on the fly, it should be done here. No other part should need
2570 static int make_raw_rw_request(void)
2572 int aligned_sector_t
;
2573 int max_sector
, max_size
, tracksize
, ssize
;
2575 set_fdc(DRIVE(CURRENT
->rq_dev
));
2577 raw_cmd
= &default_raw_cmd
;
2578 raw_cmd
->flags
= FD_RAW_SPIN
| FD_RAW_NEED_DISK
| FD_RAW_NEED_DISK
|
2580 raw_cmd
->cmd_count
= NR_RW
;
2581 if (CURRENT
->cmd
== READ
){
2582 raw_cmd
->flags
|= FD_RAW_READ
;
2583 COMMAND
= FM_MODE(_floppy
,FD_READ
);
2584 } else if (CURRENT
->cmd
== WRITE
){
2585 raw_cmd
->flags
|= FD_RAW_WRITE
;
2586 COMMAND
= FM_MODE(_floppy
,FD_WRITE
);
2588 DPRINT("make_raw_rw_request: unknown command\n");
2592 max_sector
= _floppy
->sect
* _floppy
->head
;
2594 TRACK
= CURRENT
->sector
/ max_sector
;
2595 sector_t
= CURRENT
->sector
% max_sector
;
2596 if (_floppy
->track
&& TRACK
>= _floppy
->track
) {
2597 if(CURRENT
->current_nr_sectors
& 1) {
2598 current_count_sectors
= 1;
2603 HEAD
= sector_t
/ _floppy
->sect
;
2605 if (((_floppy
->stretch
& FD_SWAPSIDES
) || TESTF(FD_NEED_TWADDLE
)) &&
2606 sector_t
< _floppy
->sect
)
2607 max_sector
= _floppy
->sect
;
2609 /* 2M disks have phantom sectors on the first track */
2610 if ((_floppy
->rate
& FD_2M
) && (!TRACK
) && (!HEAD
)){
2611 max_sector
= 2 * _floppy
->sect
/ 3;
2612 if (sector_t
>= max_sector
){
2613 current_count_sectors
= minimum(_floppy
->sect
- sector_t
,
2614 CURRENT
->nr_sectors
);
2619 SIZECODE
= FD_SIZECODE(_floppy
);
2620 raw_cmd
->rate
= _floppy
->rate
& 0x43;
2621 if ((_floppy
->rate
& FD_2M
) &&
2630 raw_cmd
->track
= TRACK
<< STRETCH(_floppy
);
2631 DR_SELECT
= UNIT(current_drive
) + PH_HEAD(_floppy
,HEAD
);
2634 SECT_PER_TRACK
= _floppy
->sect
<< 2 >> SIZECODE
;
2635 SECTOR
= ((sector_t
% _floppy
->sect
) << 2 >> SIZECODE
) + 1;
2637 /* tracksize describes the size which can be filled up with sectors
2640 tracksize
= _floppy
->sect
- _floppy
->sect
% ssize
;
2641 if (tracksize
< _floppy
->sect
){
2643 if (tracksize
<= sector_t
% _floppy
->sect
)
2646 /* if we are beyond tracksize, fill up using smaller sectors */
2647 while (tracksize
<= sector_t
% _floppy
->sect
){
2648 while(tracksize
+ ssize
> _floppy
->sect
){
2652 SECTOR
++; SECT_PER_TRACK
++;
2655 max_sector
= HEAD
* _floppy
->sect
+ tracksize
;
2656 } else if (!TRACK
&& !HEAD
&& !(_floppy
->rate
& FD_2M
) && probing
) {
2657 max_sector
= _floppy
->sect
;
2658 } else if (!HEAD
&& CT(COMMAND
) == FD_WRITE
) {
2659 /* for virtual DMA bug workaround */
2660 max_sector
= _floppy
->sect
;
2663 in_sector_offset
= (sector_t
% _floppy
->sect
) % ssize
;
2664 aligned_sector_t
= sector_t
- in_sector_offset
;
2665 max_size
= CURRENT
->nr_sectors
;
2666 if ((raw_cmd
->track
== buffer_track
) &&
2667 (current_drive
== buffer_drive
) &&
2668 (sector_t
>= buffer_min
) && (sector_t
< buffer_max
)) {
2669 /* data already in track buffer */
2670 if (CT(COMMAND
) == FD_READ
) {
2671 copy_buffer(1, max_sector
, buffer_max
);
2674 } else if (in_sector_offset
|| CURRENT
->nr_sectors
< ssize
){
2675 if (CT(COMMAND
) == FD_WRITE
){
2676 if (sector_t
+ CURRENT
->nr_sectors
> ssize
&&
2677 sector_t
+ CURRENT
->nr_sectors
< ssize
+ ssize
)
2678 max_size
= ssize
+ ssize
;
2682 raw_cmd
->flags
&= ~FD_RAW_WRITE
;
2683 raw_cmd
->flags
|= FD_RAW_READ
;
2684 COMMAND
= FM_MODE(_floppy
,FD_READ
);
2685 } else if ((unsigned long)CURRENT
->buffer
< MAX_DMA_ADDRESS
) {
2686 unsigned long dma_limit
;
2687 int direct
, indirect
;
2689 indirect
= transfer_size(ssize
,max_sector
,max_buffer_sectors
*2) -
2693 * Do NOT use minimum() here---MAX_DMA_ADDRESS is 64 bits wide
2694 * on a 64 bit machine!
2696 max_size
= buffer_chain_size();
2697 dma_limit
= (MAX_DMA_ADDRESS
- ((unsigned long) CURRENT
->buffer
)) >> 9;
2698 if ((unsigned long) max_size
> dma_limit
) {
2699 max_size
= dma_limit
;
2701 /* 64 kb boundaries */
2702 if (CROSS_64KB(CURRENT
->buffer
, max_size
<< 9))
2704 ((unsigned long)CURRENT
->buffer
) % K_64
)>>9;
2705 direct
= transfer_size(ssize
,max_sector
,max_size
) - sector_t
;
2707 * We try to read tracks, but if we get too many errors, we
2708 * go back to reading just one sector at a time.
2710 * This means we should be able to read a sector even if there
2711 * are other bad sectors on this track.
2714 (indirect
* 2 > direct
* 3 &&
2715 *errors
< DP
->max_errors
.read_track
&&
2716 /*!TESTF(FD_NEED_TWADDLE) &&*/
2717 ((!probing
|| (DP
->read_track
&(1<<DRS
->probed_format
)))))){
2718 max_size
= CURRENT
->nr_sectors
;
2720 raw_cmd
->kernel_data
= CURRENT
->buffer
;
2721 raw_cmd
->length
= current_count_sectors
<< 9;
2722 if (raw_cmd
->length
== 0){
2723 DPRINT("zero dma transfer attempted from make_raw_request\n");
2724 DPRINT("indirect=%d direct=%d sector_t=%d",
2725 indirect
, direct
, sector_t
);
2728 /* check_dma_crossing(raw_cmd->kernel_data,
2730 "end of make_raw_request [1]");*/
2732 virtualdmabug_workaround();
2737 if (CT(COMMAND
) == FD_READ
)
2738 max_size
= max_sector
; /* unbounded */
2740 /* claim buffer track if needed */
2741 if (buffer_track
!= raw_cmd
->track
|| /* bad track */
2742 buffer_drive
!=current_drive
|| /* bad drive */
2743 sector_t
> buffer_max
||
2744 sector_t
< buffer_min
||
2745 ((CT(COMMAND
) == FD_READ
||
2746 (!in_sector_offset
&& CURRENT
->nr_sectors
>= ssize
))&&
2747 max_sector
> 2 * max_buffer_sectors
+ buffer_min
&&
2748 max_size
+ sector_t
> 2 * max_buffer_sectors
+ buffer_min
)
2749 /* not enough space */){
2751 buffer_drive
= current_drive
;
2752 buffer_max
= buffer_min
= aligned_sector_t
;
2754 raw_cmd
->kernel_data
= floppy_track_buffer
+
2755 ((aligned_sector_t
-buffer_min
)<<9);
2757 if (CT(COMMAND
) == FD_WRITE
){
2758 /* copy write buffer to track buffer.
2759 * if we get here, we know that the write
2760 * is either aligned or the data already in the buffer
2761 * (buffer will be overwritten) */
2762 #ifdef FLOPPY_SANITY_CHECK
2763 if (in_sector_offset
&& buffer_track
== -1)
2764 DPRINT("internal error offset !=0 on write\n");
2766 buffer_track
= raw_cmd
->track
;
2767 buffer_drive
= current_drive
;
2768 copy_buffer(ssize
, max_sector
, 2*max_buffer_sectors
+buffer_min
);
2770 transfer_size(ssize
, max_sector
,
2771 2*max_buffer_sectors
+buffer_min
-aligned_sector_t
);
2773 /* round up current_count_sectors to get dma xfer size */
2774 raw_cmd
->length
= in_sector_offset
+current_count_sectors
;
2775 raw_cmd
->length
= ((raw_cmd
->length
-1)|(ssize
-1))+1;
2776 raw_cmd
->length
<<= 9;
2777 #ifdef FLOPPY_SANITY_CHECK
2778 /*check_dma_crossing(raw_cmd->kernel_data, raw_cmd->length,
2779 "end of make_raw_request");*/
2780 if ((raw_cmd
->length
< current_count_sectors
<< 9) ||
2781 (raw_cmd
->kernel_data
!= CURRENT
->buffer
&&
2782 CT(COMMAND
) == FD_WRITE
&&
2783 (aligned_sector_t
+ (raw_cmd
->length
>> 9) > buffer_max
||
2784 aligned_sector_t
< buffer_min
)) ||
2785 raw_cmd
->length
% (128 << SIZECODE
) ||
2786 raw_cmd
->length
<= 0 || current_count_sectors
<= 0){
2787 DPRINT("fractionary current count b=%lx s=%lx\n",
2788 raw_cmd
->length
, current_count_sectors
);
2789 if (raw_cmd
->kernel_data
!= CURRENT
->buffer
)
2790 printk("addr=%d, length=%ld\n",
2791 (int) ((raw_cmd
->kernel_data
-
2792 floppy_track_buffer
) >> 9),
2793 current_count_sectors
);
2794 printk("st=%d ast=%d mse=%d msi=%d\n",
2795 sector_t
, aligned_sector_t
, max_sector
, max_size
);
2796 printk("ssize=%x SIZECODE=%d\n", ssize
, SIZECODE
);
2797 printk("command=%x SECTOR=%d HEAD=%d, TRACK=%d\n",
2798 COMMAND
, SECTOR
, HEAD
, TRACK
);
2799 printk("buffer drive=%d\n", buffer_drive
);
2800 printk("buffer track=%d\n", buffer_track
);
2801 printk("buffer_min=%d\n", buffer_min
);
2802 printk("buffer_max=%d\n", buffer_max
);
2806 if (raw_cmd
->kernel_data
!= CURRENT
->buffer
){
2807 if (raw_cmd
->kernel_data
< floppy_track_buffer
||
2808 current_count_sectors
< 0 ||
2809 raw_cmd
->length
< 0 ||
2810 raw_cmd
->kernel_data
+ raw_cmd
->length
>
2811 floppy_track_buffer
+ (max_buffer_sectors
<< 10)){
2812 DPRINT("buffer overrun in schedule dma\n");
2813 printk("sector_t=%d buffer_min=%d current_count=%ld\n",
2814 sector_t
, buffer_min
,
2815 raw_cmd
->length
>> 9);
2816 printk("current_count_sectors=%ld\n",
2817 current_count_sectors
);
2818 if (CT(COMMAND
) == FD_READ
)
2820 if (CT(COMMAND
) == FD_READ
)
2824 } else if (raw_cmd
->length
> CURRENT
->nr_sectors
<< 9 ||
2825 current_count_sectors
> CURRENT
->nr_sectors
){
2826 DPRINT("buffer overrun in direct transfer\n");
2828 } else if (raw_cmd
->length
< current_count_sectors
<< 9){
2829 DPRINT("more sectors than bytes\n");
2830 printk("bytes=%ld\n", raw_cmd
->length
>> 9);
2831 printk("sectors=%ld\n", current_count_sectors
);
2833 if (raw_cmd
->length
== 0){
2834 DPRINT("zero dma transfer attempted from make_raw_request\n");
2839 virtualdmabug_workaround();
2843 static void redo_fd_request(void)
2845 #define REPEAT {request_done(0); continue; }
2850 if (current_drive
< N_DRIVE
)
2851 floppy_off(current_drive
);
2853 if (CURRENT
&& CURRENT
->rq_status
== RQ_INACTIVE
){
2865 if (MAJOR(CURRENT
->rq_dev
) != MAJOR_NR
)
2866 panic(DEVICE_NAME
": request list destroyed");
2867 if (CURRENT
->bh
&& !buffer_locked(CURRENT
->bh
))
2868 panic(DEVICE_NAME
": block not locked");
2870 device
= CURRENT
->rq_dev
;
2871 set_fdc(DRIVE(device
));
2872 reschedule_timeout(CURRENTD
, "redo fd request", 0);
2875 raw_cmd
= & default_raw_cmd
;
2877 if (start_motor(redo_fd_request
)) return;
2878 disk_change(current_drive
);
2879 if (test_bit(current_drive
, &fake_change
) ||
2880 TESTF(FD_DISK_CHANGED
)){
2881 DPRINT("disk absent or changed during operation\n");
2884 if (!_floppy
) { /* Autodetection */
2886 DRS
->probed_format
= 0;
2887 if (next_valid_format()){
2888 DPRINT("no autodetectable formats\n");
2894 _floppy
= floppy_type
+DP
->autodetect
[DRS
->probed_format
];
2897 errors
= & (CURRENT
->errors
);
2898 tmp
= make_raw_rw_request();
2904 if (TESTF(FD_NEED_TWADDLE
))
2906 schedule_bh( (void *)(void *) floppy_start
);
2908 debugt("queue fd request");
2915 static struct cont_t rw_cont
={
2921 static void process_fd_request(void)
2924 schedule_bh( (void *)(void *) redo_fd_request
);
2927 static void do_fd_request(void)
2929 if(usage_count
== 0) {
2930 printk("warning: usage count=0, CURRENT=%p exiting\n", CURRENT
);
2931 printk("sect=%ld cmd=%d\n", CURRENT
->sector
, CURRENT
->cmd
);
2935 /* fdc busy, this new request will be treated when the
2936 current one is done */
2937 is_alive("do fd request, old request running");
2940 lock_fdc(MAXTIMEOUT
,0);
2941 process_fd_request();
2942 is_alive("do fd request");
2945 static struct cont_t poll_cont
={
2951 static int poll_drive(int interruptible
, int flag
)
2954 /* no auto-sense, just clear dcl */
2955 raw_cmd
= &default_raw_cmd
;
2956 raw_cmd
->flags
= flag
;
2958 raw_cmd
->cmd_count
=0;
2961 if (DP
->flags
& FD_DEBUG
){
2962 DPRINT("setting NEWCHANGE in poll_drive\n");
2965 SETF(FD_DISK_NEWCHANGE
);
2971 * User triggered reset
2972 * ====================
2975 static void reset_intr(void)
2977 printk("weird, reset interrupt called\n");
2980 static struct cont_t reset_cont
={
2986 static int user_reset_fdc(int drive
, int arg
, int interruptible
)
2991 LOCK_FDC(drive
,interruptible
);
2992 if (arg
== FD_RESET_ALWAYS
)
2998 process_fd_request();
3003 * Misc Ioctl's and support
3004 * ========================
3006 static inline int fd_copyout(void *param
, const void *address
, int size
)
3008 return copy_to_user(param
,address
, size
) ? -EFAULT
: 0;
3011 static inline int fd_copyin(void *param
, void *address
, int size
)
3013 return copy_from_user(address
, param
, size
) ? -EFAULT
: 0;
3016 #define _COPYOUT(x) (copy_to_user((void *)param, &(x), sizeof(x)) ? -EFAULT : 0)
3017 #define _COPYIN(x) (copy_from_user(&(x), (void *)param, sizeof(x)) ? -EFAULT : 0)
3019 #define COPYOUT(x) ECALL(_COPYOUT(x))
3020 #define COPYIN(x) ECALL(_COPYIN(x))
3022 static inline const char *drive_name(int type
, int drive
)
3024 struct floppy_struct
*floppy
;
3027 floppy
= floppy_type
+ type
;
3029 if (UDP
->native_format
)
3030 floppy
= floppy_type
+ UDP
->native_format
;
3035 return floppy
->name
;
3042 static void raw_cmd_done(int flag
)
3047 raw_cmd
->flags
|= FD_RAW_FAILURE
;
3048 raw_cmd
->flags
|= FD_RAW_HARDFAILURE
;
3050 raw_cmd
->reply_count
= inr
;
3051 if(raw_cmd
->reply_count
> MAX_REPLIES
)
3052 raw_cmd
->reply_count
=0;
3053 for (i
=0; i
< raw_cmd
->reply_count
; i
++)
3054 raw_cmd
->reply
[i
] = reply_buffer
[i
];
3056 if (raw_cmd
->flags
& (FD_RAW_READ
| FD_RAW_WRITE
))
3058 unsigned long flags
;
3059 flags
=claim_dma_lock();
3060 raw_cmd
->length
= fd_get_dma_residue();
3061 release_dma_lock(flags
);
3064 if ((raw_cmd
->flags
& FD_RAW_SOFTFAILURE
) &&
3065 (!raw_cmd
->reply_count
|| (raw_cmd
->reply
[0] & 0xc0)))
3066 raw_cmd
->flags
|= FD_RAW_FAILURE
;
3068 if (disk_change(current_drive
))
3069 raw_cmd
->flags
|= FD_RAW_DISK_CHANGE
;
3071 raw_cmd
->flags
&= ~FD_RAW_DISK_CHANGE
;
3072 if (raw_cmd
->flags
& FD_RAW_NO_MOTOR_AFTER
)
3073 motor_off_callback(current_drive
);
3075 if (raw_cmd
->next
&&
3076 (!(raw_cmd
->flags
& FD_RAW_FAILURE
) ||
3077 !(raw_cmd
->flags
& FD_RAW_STOP_IF_FAILURE
)) &&
3078 ((raw_cmd
->flags
& FD_RAW_FAILURE
) ||
3079 !(raw_cmd
->flags
&FD_RAW_STOP_IF_SUCCESS
))) {
3080 raw_cmd
= raw_cmd
->next
;
3088 static struct cont_t raw_cmd_cont
={
3095 static inline int raw_cmd_copyout(int cmd
, char *param
,
3096 struct floppy_raw_cmd
*ptr
)
3102 param
+= sizeof(struct floppy_raw_cmd
);
3103 if ((ptr
->flags
& FD_RAW_READ
) && ptr
->buffer_length
){
3104 if (ptr
->length
>=0 && ptr
->length
<=ptr
->buffer_length
)
3105 ECALL(fd_copyout(ptr
->data
,
3107 ptr
->buffer_length
-
3116 static void raw_cmd_free(struct floppy_raw_cmd
**ptr
)
3118 struct floppy_raw_cmd
*next
,*this;
3123 if (this->buffer_length
) {
3124 fd_dma_mem_free((unsigned long)this->kernel_data
,
3125 this->buffer_length
);
3126 this->buffer_length
= 0;
3135 static inline int raw_cmd_copyin(int cmd
, char *param
,
3136 struct floppy_raw_cmd
**rcmd
)
3138 struct floppy_raw_cmd
*ptr
;
3144 ptr
= (struct floppy_raw_cmd
*)
3145 kmalloc(sizeof(struct floppy_raw_cmd
), GFP_USER
);
3151 ptr
->buffer_length
= 0;
3152 param
+= sizeof(struct floppy_raw_cmd
);
3153 if (ptr
->cmd_count
> 33)
3154 /* the command may now also take up the space
3155 * initially intended for the reply & the
3156 * reply count. Needed for long 82078 commands
3157 * such as RESTORE, which takes ... 17 command
3158 * bytes. Murphy's law #137: When you reserve
3159 * 16 bytes for a structure, you'll one day
3160 * discover that you really need 17...
3164 for (i
=0; i
< 16; i
++)
3166 ptr
->resultcode
= 0;
3167 ptr
->kernel_data
= 0;
3169 if (ptr
->flags
& (FD_RAW_READ
| FD_RAW_WRITE
)) {
3170 if (ptr
->length
<= 0)
3172 ptr
->kernel_data
=(char*)fd_dma_mem_alloc(ptr
->length
);
3173 fallback_on_nodma_alloc(&ptr
->kernel_data
,
3175 if (!ptr
->kernel_data
)
3177 ptr
->buffer_length
= ptr
->length
;
3179 if (ptr
->flags
& FD_RAW_WRITE
)
3180 ECALL(fd_copyin(ptr
->data
, ptr
->kernel_data
,
3182 rcmd
= & (ptr
->next
);
3183 if (!(ptr
->flags
& FD_RAW_MORE
))
3190 static int raw_cmd_ioctl(int cmd
, void *param
)
3192 int drive
, ret
, ret2
;
3193 struct floppy_raw_cmd
*my_raw_cmd
;
3195 if (FDCS
->rawcmd
<= 1)
3197 for (drive
= 0; drive
< N_DRIVE
; drive
++){
3198 if (FDC(drive
) != fdc
)
3200 if (drive
== current_drive
){
3201 if (UDRS
->fd_ref
> 1){
3205 } else if (UDRS
->fd_ref
){
3214 ret
= raw_cmd_copyin(cmd
, param
, &my_raw_cmd
);
3216 raw_cmd_free(&my_raw_cmd
);
3220 raw_cmd
= my_raw_cmd
;
3221 cont
= &raw_cmd_cont
;
3222 ret
=wait_til_done(floppy_start
,1);
3224 if (DP
->flags
& FD_DEBUG
){
3225 DPRINT("calling disk change from raw_cmd ioctl\n");
3229 if (ret
!= -EINTR
&& FDCS
->reset
)
3232 DRS
->track
= NO_TRACK
;
3234 ret2
= raw_cmd_copyout(cmd
, param
, my_raw_cmd
);
3237 raw_cmd_free(&my_raw_cmd
);
3241 static int invalidate_drive(kdev_t rdev
)
3243 /* invalidate the buffer track to force a reread */
3244 set_bit(DRIVE(rdev
), &fake_change
);
3245 process_fd_request();
3246 check_disk_change(rdev
);
3251 static inline void clear_write_error(int drive
)
3256 static inline int set_geometry(unsigned int cmd
, struct floppy_struct
*g
,
3257 int drive
, int type
, kdev_t device
)
3261 /* sanity checking for parameters.*/
3265 g
->track
> UDP
->tracks
>>STRETCH(g
) ||
3266 /* check if reserved bits are set */
3267 (g
->stretch
&~(FD_STRETCH
|FD_SWAPSIDES
)) != 0)
3270 if (!capable(CAP_SYS_ADMIN
))
3273 for (cnt
= 0; cnt
< N_DRIVE
; cnt
++){
3274 if (ITYPE(drive_state
[cnt
].fd_device
) == type
&&
3275 drive_state
[cnt
].fd_ref
)
3276 set_bit(drive
, &fake_change
);
3278 floppy_type
[type
] = *g
;
3279 floppy_type
[type
].name
="user format";
3280 for (cnt
= type
<< 2; cnt
< (type
<< 2) + 4; cnt
++)
3281 floppy_sizes
[cnt
]= floppy_sizes
[cnt
+0x80]=
3282 (floppy_type
[type
].size
+1)>>1;
3283 process_fd_request();
3284 for (cnt
= 0; cnt
< N_DRIVE
; cnt
++){
3285 if (ITYPE(drive_state
[cnt
].fd_device
) == type
&&
3286 drive_state
[cnt
].fd_ref
)
3289 drive_state
[cnt
].fd_device
));
3293 if (cmd
!= FDDEFPRM
)
3294 /* notice a disk change immediately, else
3295 * we lose our settings immediately*/
3296 CALL(poll_drive(1, FD_RAW_NEED_DISK
));
3297 user_params
[drive
] = *g
;
3298 if (buffer_drive
== drive
)
3299 SUPBOUND(buffer_max
, user_params
[drive
].sect
);
3300 current_type
[drive
] = &user_params
[drive
];
3301 floppy_sizes
[drive
] = (user_params
[drive
].size
+1) >> 1;
3302 if (cmd
== FDDEFPRM
)
3303 DRS
->keep_data
= -1;
3306 /* invalidation. Invalidate only when needed, i.e.
3307 * when there are already sectors in the buffer cache
3308 * whose number will change. This is useful, because
3309 * mtools often changes the geometry of the disk after
3310 * looking at the boot block */
3311 if (DRS
->maxblock
> user_params
[drive
].sect
|| DRS
->maxtrack
)
3312 invalidate_drive(device
);
3314 process_fd_request();
3319 /* handle obsolete ioctl's */
3320 static int ioctl_table
[]= {
3348 static inline int normalize_ioctl(int *cmd
, int *size
)
3352 for (i
=0; i
< ARRAY_SIZE(ioctl_table
); i
++) {
3353 if ((*cmd
& 0xffff) == (ioctl_table
[i
] & 0xffff)){
3354 *size
= _IOC_SIZE(*cmd
);
3355 *cmd
= ioctl_table
[i
];
3356 if (*size
> _IOC_SIZE(*cmd
)) {
3357 printk("ioctl not yet supported\n");
3366 static int get_floppy_geometry(int drive
, int type
, struct floppy_struct
**g
)
3369 *g
= &floppy_type
[type
];
3372 CALL(poll_drive(0,0));
3373 process_fd_request();
3374 *g
= current_type
[drive
];
3381 static int fd_ioctl(struct inode
*inode
, struct file
*filp
, unsigned int cmd
,
3382 unsigned long param
)
3384 #define IOCTL_MODE_BIT 8
3385 #define OPEN_WRITE_BIT 16
3386 #define IOCTL_ALLOWED (filp && (filp->f_mode & IOCTL_MODE_BIT))
3387 #define OUT(c,x) case c: outparam = (const char *) (x); break
3388 #define IN(c,x,tag) case c: *(x) = inparam. tag ; return 0
3395 struct floppy_struct g
; /* geometry */
3396 struct format_descr f
;
3397 struct floppy_max_errors max_errors
;
3398 struct floppy_drive_params dp
;
3399 } inparam
; /* parameters coming from user space */
3400 const char *outparam
; /* parameters passed back to user space */
3402 device
= inode
->i_rdev
;
3409 return blk_ioctl(device
, cmd
, param
);
3411 type
= TYPE(device
);
3412 drive
= DRIVE(device
);
3414 /* convert compatibility eject ioctls into floppy eject ioctl.
3415 * We do this in order to provide a means to eject floppy disks before
3416 * installing the new fdutils package */
3417 if(cmd
== CDROMEJECT
|| /* CD-ROM eject */
3418 cmd
== 0x6470 /* SunOS floppy eject */) {
3419 DPRINT("obsolete eject ioctl\n");
3420 DPRINT("please use floppycontrol --eject\n");
3424 /* generic block device ioctls */
3426 /* the following have been inspired by the corresponding
3427 * code for other block devices. */
3428 struct floppy_struct
*g
;
3431 struct hd_geometry loc
;
3432 ECALL(get_floppy_geometry(drive
, type
, &g
));
3433 loc
.heads
= g
->head
;
3434 loc
.sectors
= g
->sect
;
3435 loc
.cylinders
= g
->track
;
3437 return _COPYOUT(loc
);
3441 ECALL(get_floppy_geometry(drive
, type
, &g
));
3442 return put_user(g
->size
, (long *) param
);
3443 /* BLKRRPART is not defined as floppies don't have
3444 * partition tables */
3447 /* convert the old style command into a new style command */
3448 if ((cmd
& 0xff00) == 0x0200) {
3449 ECALL(normalize_ioctl(&cmd
, &size
));
3453 /* permission checks */
3454 if (((cmd
& 0x40) && !IOCTL_ALLOWED
) ||
3455 ((cmd
& 0x80) && !suser()))
3459 CLEARSTRUCT(&inparam
);
3460 if (_IOC_DIR(cmd
) & _IOC_WRITE
)
3461 ECALL(fd_copyin((void *)param
, &inparam
, size
))
3465 if(UDRS
->fd_ref
!= 1)
3466 /* somebody else has this drive open */
3470 /* do the actual eject. Fails on
3471 * non-Sparc architectures */
3472 ret
=fd_eject(UNIT(drive
));
3474 USETF(FD_DISK_CHANGED
);
3476 process_fd_request();
3480 current_type
[drive
] = NULL
;
3481 floppy_sizes
[drive
] = MAX_DISK_SIZE
;
3482 UDRS
->keep_data
= 0;
3483 return invalidate_drive(device
);
3486 return set_geometry(cmd
, & inparam
.g
,
3487 drive
, type
, device
);
3489 ECALL(get_floppy_geometry(drive
, type
,
3490 (struct floppy_struct
**)
3495 UDP
->flags
|= FTD_MSG
;
3498 UDP
->flags
&= ~FTD_MSG
;
3503 CALL(poll_drive(1, FD_RAW_NEED_DISK
));
3505 process_fd_request();
3508 if(!(ret
& FD_DISK_WRITABLE
))
3512 if (UDRS
->fd_ref
!= 1)
3514 return do_format(device
, &inparam
.f
);
3518 return invalidate_drive(device
);
3520 case FDSETEMSGTRESH
:
3521 UDP
->max_errors
.reporting
=
3522 (unsigned short) (param
& 0x0f);
3524 OUT(FDGETMAXERRS
, &UDP
->max_errors
);
3525 IN(FDSETMAXERRS
, &UDP
->max_errors
, max_errors
);
3528 outparam
= drive_name(type
,drive
);
3529 SUPBOUND(size
,strlen(outparam
)+1);
3532 IN(FDSETDRVPRM
, UDP
, dp
);
3533 OUT(FDGETDRVPRM
, UDP
);
3537 CALL(poll_drive(1, FD_RAW_NEED_DISK
));
3538 process_fd_request();
3540 OUT(FDGETDRVSTAT
, UDRS
);
3543 return user_reset_fdc(drive
, (int)param
, 1);
3545 OUT(FDGETFDCSTAT
,UFDCS
);
3550 OUT(FDWERRORGET
,UDRWE
);
3557 CALL(i
= raw_cmd_ioctl(cmd
,(void *) param
));
3558 process_fd_request();
3564 process_fd_request();
3571 if (_IOC_DIR(cmd
) & _IOC_READ
)
3572 return fd_copyout((void *)param
, outparam
, size
);
3575 #undef IOCTL_ALLOWED
3580 static void config_types(void)
3585 /* read drive info out of physical CMOS */
3588 UDP
->cmos
= FLOPPY0_TYPE
;
3590 if (!UDP
->cmos
&& FLOPPY1_TYPE
)
3591 UDP
->cmos
= FLOPPY1_TYPE
;
3594 /* additional physical CMOS drive detection should go here */
3596 for (drive
=0; drive
< N_DRIVE
; drive
++){
3597 unsigned int type
= UDP
->cmos
;
3598 struct floppy_drive_params
*params
;
3599 const char *name
= NULL
;
3600 static char temparea
[32];
3602 if (type
< NUMBER(default_drive_params
)) {
3603 params
= &default_drive_params
[type
].params
;
3605 name
= default_drive_params
[type
].name
;
3606 allowed_drive_mask
|= 1 << drive
;
3609 params
= &default_drive_params
[0].params
;
3610 sprintf(temparea
, "unknown type %d (usb?)", type
);
3614 const char * prepend
= ",";
3616 prepend
= KERN_INFO
"Floppy drive(s):";
3619 printk("%s fd%d is %s", prepend
, drive
, name
);
3627 static ssize_t
floppy_read(struct file
* filp
, char *buf
,
3628 size_t count
, loff_t
*ppos
)
3630 struct inode
*inode
= filp
->f_dentry
->d_inode
;
3631 int drive
= DRIVE(inode
->i_rdev
);
3633 check_disk_change(inode
->i_rdev
);
3634 if (UTESTF(FD_DISK_CHANGED
))
3636 return block_read(filp
, buf
, count
, ppos
);
3639 static ssize_t
floppy_write(struct file
* filp
, const char * buf
,
3640 size_t count
, loff_t
*ppos
)
3642 struct inode
* inode
= filp
->f_dentry
->d_inode
;
3645 int drive
= DRIVE(inode
->i_rdev
);
3647 if (!UDRS
->maxblock
)
3648 UDRS
->maxblock
=1;/* make change detectable */
3649 check_disk_change(inode
->i_rdev
);
3650 if (UTESTF(FD_DISK_CHANGED
))
3652 if (!UTESTF(FD_DISK_WRITABLE
))
3654 block
= (*ppos
+ count
) >> 9;
3655 INFBOUND(UDRS
->maxblock
, block
);
3656 ret
= block_write(filp
, buf
, count
, ppos
);
3660 static int floppy_release(struct inode
* inode
, struct file
* filp
)
3664 drive
= DRIVE(inode
->i_rdev
);
3667 * If filp is NULL, we're being called from blkdev_release
3668 * or after a failed mount attempt. In the former case the
3669 * device has already been sync'ed, and in the latter no
3670 * sync is required. Otherwise, sync if filp is writable.
3672 if (filp
&& (filp
->f_mode
& (2 | OPEN_WRITE_BIT
)))
3673 block_fsync(filp
, filp
->f_dentry
);
3675 if (UDRS
->fd_ref
< 0)
3677 else if (!UDRS
->fd_ref
--) {
3678 DPRINT("floppy_release with fd_ref == 0");
3681 floppy_release_irq_and_dma();
3686 * floppy_open check for aliasing (/dev/fd0 can be the same as
3687 * /dev/PS0 etc), and disallows simultaneous access to the same
3688 * drive with different device numbers.
3690 #define RETERR(x) do{floppy_release(inode,filp); return -(x);}while(0)
3692 static int floppy_open(struct inode
* inode
, struct file
* filp
)
3700 DPRINT("Weird, open called with filp=0\n");
3704 drive
= DRIVE(inode
->i_rdev
);
3705 if (drive
>= N_DRIVE
||
3706 !(allowed_drive_mask
& (1 << drive
)) ||
3707 fdc_state
[FDC(drive
)].version
== FDC_NONE
)
3710 if (TYPE(inode
->i_rdev
) >= NUMBER(floppy_type
))
3712 old_dev
= UDRS
->fd_device
;
3713 if (UDRS
->fd_ref
&& old_dev
!= MINOR(inode
->i_rdev
))
3716 if (!UDRS
->fd_ref
&& (UDP
->flags
& FD_BROKEN_DCL
)){
3717 USETF(FD_DISK_CHANGED
);
3721 if (UDRS
->fd_ref
== -1 ||
3722 (UDRS
->fd_ref
&& (filp
->f_flags
& O_EXCL
)))
3725 if (floppy_grab_irq_and_dma())
3728 if (filp
->f_flags
& O_EXCL
)
3733 if (!floppy_track_buffer
){
3734 /* if opening an ED drive, reserve a big buffer,
3735 * else reserve a small one */
3736 if ((UDP
->cmos
== 6) || (UDP
->cmos
== 5))
3737 try = 64; /* Only 48 actually useful */
3739 try = 32; /* Only 24 actually useful */
3741 tmp
=(char *)fd_dma_mem_alloc(1024 * try);
3742 if (!tmp
&& !floppy_track_buffer
) {
3743 try >>= 1; /* buffer only one side */
3745 tmp
= (char *)fd_dma_mem_alloc(1024*try);
3747 if(!tmp
&& !floppy_track_buffer
) {
3748 fallback_on_nodma_alloc(&tmp
, 2048 * try);
3750 if (!tmp
&& !floppy_track_buffer
) {
3751 DPRINT("Unable to allocate DMA memory\n");
3754 if (floppy_track_buffer
) {
3756 fd_dma_mem_free((unsigned long)tmp
,try*1024);
3758 buffer_min
= buffer_max
= -1;
3759 floppy_track_buffer
= tmp
;
3760 max_buffer_sectors
= try;
3764 UDRS
->fd_device
= MINOR(inode
->i_rdev
);
3765 if (old_dev
!= -1 && old_dev
!= MINOR(inode
->i_rdev
)) {
3766 if (buffer_drive
== drive
)
3768 invalidate_buffers(MKDEV(FLOPPY_MAJOR
,old_dev
));
3771 /* Allow ioctls if we have write-permissions even if read-only open */
3772 if ((filp
->f_mode
& 2) || (permission(inode
,2) == 0))
3773 filp
->f_mode
|= IOCTL_MODE_BIT
;
3774 if (filp
->f_mode
& 2)
3775 filp
->f_mode
|= OPEN_WRITE_BIT
;
3777 if (UFDCS
->rawcmd
== 1)
3780 if (filp
->f_flags
& O_NDELAY
)
3782 if (filp
->f_mode
& 3) {
3783 UDRS
->last_checked
= 0;
3784 check_disk_change(inode
->i_rdev
);
3785 if (UTESTF(FD_DISK_CHANGED
))
3788 if ((filp
->f_mode
& 2) && !(UTESTF(FD_DISK_WRITABLE
)))
3795 * Check if the disk has been changed or if a change has been faked.
3797 static int check_floppy_change(kdev_t dev
)
3799 int drive
= DRIVE(dev
);
3801 if (MAJOR(dev
) != MAJOR_NR
) {
3802 DPRINT("check_floppy_change: not a floppy\n");
3806 if (UTESTF(FD_DISK_CHANGED
) || UTESTF(FD_VERIFY
))
3809 if (UDP
->checkfreq
< jiffies
- UDRS
->last_checked
){
3812 process_fd_request();
3815 if (UTESTF(FD_DISK_CHANGED
) ||
3816 UTESTF(FD_VERIFY
) ||
3817 test_bit(drive
, &fake_change
) ||
3818 (!TYPE(dev
) && !current_type
[drive
]))
3823 /* revalidate the floppy disk, i.e. trigger format autodetection by reading
3824 * the bootblock (block 0). "Autodetection" is also needed to check whether
3825 * there is a disk in the drive at all... Thus we also do it for fixed
3826 * geometry formats */
3827 static int floppy_revalidate(kdev_t dev
)
3829 #define NO_GEOM (!current_type[drive] && !TYPE(dev))
3830 struct buffer_head
* bh
;
3831 int drive
=DRIVE(dev
);
3834 if (UTESTF(FD_DISK_CHANGED
) ||
3835 UTESTF(FD_VERIFY
) ||
3836 test_bit(drive
, &fake_change
) ||
3839 cf
= UTESTF(FD_DISK_CHANGED
) || UTESTF(FD_VERIFY
);
3840 if (!(cf
|| test_bit(drive
, &fake_change
) || NO_GEOM
)){
3841 process_fd_request(); /*already done by another thread*/
3846 if (buffer_drive
== drive
)
3848 clear_bit(drive
, &fake_change
);
3849 UCLEARF(FD_DISK_CHANGED
);
3854 int size
= floppy_blocksizes
[MINOR(dev
)];
3857 if (!(bh
= getblk(dev
,0,size
))){
3858 process_fd_request();
3861 if (bh
&& !buffer_uptodate(bh
))
3862 ll_rw_block(READ
, 1, &bh
);
3863 process_fd_request();
3869 poll_drive(0, FD_RAW_NEED_DISK
);
3870 process_fd_request();
3875 static struct file_operations floppy_fops
= {
3876 NULL
, /* lseek - default */
3877 floppy_read
, /* read - general block-dev read */
3878 floppy_write
, /* write - general block-dev write */
3879 NULL
, /* readdir - bad */
3881 fd_ioctl
, /* ioctl */
3883 floppy_open
, /* open */
3885 floppy_release
, /* release */
3886 block_fsync
, /* fsync */
3888 check_floppy_change
, /* media_change */
3889 floppy_revalidate
, /* revalidate */
3893 * Floppy Driver initialization
3894 * =============================
3897 /* Determine the floppy disk controller type */
3898 /* This routine was written by David C. Niemi */
3899 __initfunc(static char get_fdc_version(void))
3903 output_byte(FD_DUMPREGS
); /* 82072 and better know DUMPREGS */
3906 if ((r
= result()) <= 0x00)
3907 return FDC_NONE
; /* No FDC present ??? */
3908 if ((r
==1) && (reply_buffer
[0] == 0x80)){
3909 printk(KERN_INFO
"FDC %d is an 8272A\n",fdc
);
3910 return FDC_8272A
; /* 8272a/765 don't know DUMPREGS */
3913 printk("FDC %d init: DUMPREGS: unexpected return of %d bytes.\n",
3918 if(!fdc_configure()) {
3919 printk(KERN_INFO
"FDC %d is an 82072\n",fdc
);
3920 return FDC_82072
; /* 82072 doesn't know CONFIGURE */
3923 output_byte(FD_PERPENDICULAR
);
3924 if(need_more_output() == MORE_OUTPUT
) {
3927 printk(KERN_INFO
"FDC %d is an 82072A\n", fdc
);
3928 return FDC_82072A
; /* 82072A as found on Sparcs. */
3931 output_byte(FD_UNLOCK
);
3933 if ((r
== 1) && (reply_buffer
[0] == 0x80)){
3934 printk(KERN_INFO
"FDC %d is a pre-1991 82077\n", fdc
);
3935 return FDC_82077_ORIG
; /* Pre-1991 82077, doesn't know
3938 if ((r
!= 1) || (reply_buffer
[0] != 0x00)) {
3939 printk("FDC %d init: UNLOCK: unexpected return of %d bytes.\n",
3943 output_byte(FD_PARTID
);
3946 printk("FDC %d init: PARTID: unexpected return of %d bytes.\n",
3950 if (reply_buffer
[0] == 0x80) {
3951 printk(KERN_INFO
"FDC %d is a post-1991 82077\n",fdc
);
3952 return FDC_82077
; /* Revised 82077AA passes all the tests */
3954 switch (reply_buffer
[0] >> 5) {
3956 /* Either a 82078-1 or a 82078SL running at 5Volt */
3957 printk(KERN_INFO
"FDC %d is an 82078.\n",fdc
);
3960 printk(KERN_INFO
"FDC %d is a 44pin 82078\n",fdc
);
3963 printk(KERN_INFO
"FDC %d is a S82078B\n", fdc
);
3966 printk(KERN_INFO
"FDC %d is a National Semiconductor PC87306\n", fdc
);
3969 printk(KERN_INFO
"FDC %d init: 82078 variant with unknown PARTID=%d.\n",
3970 fdc
, reply_buffer
[0] >> 5);
3971 return FDC_82078_UNKN
;
3973 } /* get_fdc_version */
3975 /* lilo configuration */
3977 __initfunc(static void floppy_set_flags(int *ints
,int param
, int param2
))
3981 for (i
=0; i
< ARRAY_SIZE(default_drive_params
); i
++){
3983 default_drive_params
[i
].params
.flags
|= param2
;
3985 default_drive_params
[i
].params
.flags
&= ~param2
;
3987 DPRINT("%s flag 0x%x\n", param2
? "Setting" : "Clearing", param
);
3990 __initfunc(static void daring(int *ints
,int param
, int param2
))
3994 for (i
=0; i
< ARRAY_SIZE(default_drive_params
); i
++){
3996 default_drive_params
[i
].params
.select_delay
= 0;
3997 default_drive_params
[i
].params
.flags
|= FD_SILENT_DCL_CLEAR
;
3999 default_drive_params
[i
].params
.select_delay
= 2*HZ
/100;
4000 default_drive_params
[i
].params
.flags
&= ~FD_SILENT_DCL_CLEAR
;
4003 DPRINT("Assuming %s floppy hardware\n", param
? "standard" : "broken");
4006 __initfunc(static void set_cmos(int *ints
, int dummy
, int dummy2
))
4008 int current_drive
=0;
4011 DPRINT("wrong number of parameters for CMOS\n");
4014 current_drive
= ints
[1];
4015 if (current_drive
< 0 || current_drive
>= 8){
4016 DPRINT("bad drive for set_cmos\n");
4019 if (current_drive
>= 4 && !FDC2
)
4022 DPRINT("setting CMOS code to %d\n", ints
[2]);
4025 static struct param_table
{
4027 void (*fn
)(int *ints
, int param
, int param2
);
4032 { "allowed_drive_mask", 0, &allowed_drive_mask
, 0xff, 0}, /* obsolete */
4033 { "all_drives", 0, &allowed_drive_mask
, 0xff, 0 }, /* obsolete */
4034 { "asus_pci", 0, &allowed_drive_mask
, 0x33, 0},
4036 { "irq", 0, &FLOPPY_IRQ
, 6, 0 },
4037 { "dma", 0, &FLOPPY_DMA
, 2, 0 },
4039 { "daring", daring
, 0, 1, 0},
4041 { "two_fdc", 0, &FDC2
, 0x370, 0 },
4042 { "one_fdc", 0, &FDC2
, 0, 0 },
4044 { "thinkpad", floppy_set_flags
, 0, 1, FD_INVERTED_DCL
},
4045 { "broken_dcl", floppy_set_flags
, 0, 1, FD_BROKEN_DCL
},
4046 { "messages", floppy_set_flags
, 0, 1, FTD_MSG
},
4047 { "silent_dcl_clear", floppy_set_flags
, 0, 1, FD_SILENT_DCL_CLEAR
},
4048 { "debug", floppy_set_flags
, 0, 1, FD_DEBUG
},
4050 { "nodma", 0, &can_use_virtual_dma
, 1, 0 },
4051 { "omnibook", 0, &can_use_virtual_dma
, 1, 0 },
4052 { "yesdma", 0, &can_use_virtual_dma
, 0, 0 },
4054 { "fifo_depth", 0, &fifo_depth
, 0xa, 0 },
4055 { "nofifo", 0, &no_fifo
, 0x20, 0 },
4056 { "usefifo", 0, &no_fifo
, 0, 0 },
4058 { "cmos", set_cmos
, 0, 0, 0 },
4059 { "slow", 0, &slow_floppy
, 1, 0 },
4061 { "unexpected_interrupts", 0, &print_unex
, 1, 0 },
4062 { "no_unexpected_interrupts", 0, &print_unex
, 0, 0 },
4063 { "L40SX", 0, &print_unex
, 0, 0 } };
4065 #define FLOPPY_SETUP
4066 __initfunc(void floppy_setup(char *str
, int *ints
))
4071 for (i
=0; i
< ARRAY_SIZE(config_params
); i
++){
4072 if (strcmp(str
,config_params
[i
].name
) == 0){
4076 param
= config_params
[i
].def_param
;
4077 if(config_params
[i
].fn
)
4080 config_params
[i
].param2
);
4081 if(config_params
[i
].var
) {
4082 DPRINT("%s=%d\n", str
, param
);
4083 *config_params
[i
].var
= param
;
4090 DPRINT("unknown floppy option [%s]\n", str
);
4092 DPRINT("allowed options are:");
4093 for (i
=0; i
< ARRAY_SIZE(config_params
); i
++)
4094 printk(" %s",config_params
[i
].name
);
4097 DPRINT("botched floppy option\n");
4098 DPRINT("Read linux/drivers/block/README.fd\n");
4101 static int have_no_fdc
= -EIO
;
4104 __initfunc(int floppy_init(void))
4111 if (register_blkdev(MAJOR_NR
,"fd",&floppy_fops
)) {
4112 printk("Unable to get major %d for floppy\n",MAJOR_NR
);
4116 for (i
=0; i
<256; i
++)
4118 floppy_sizes
[i
] = (floppy_type
[ITYPE(i
)].size
+1) >> 1;
4120 floppy_sizes
[i
] = MAX_DISK_SIZE
;
4122 blk_size
[MAJOR_NR
] = floppy_sizes
;
4123 blksize_size
[MAJOR_NR
] = floppy_blocksizes
;
4124 blk_dev
[MAJOR_NR
].request_fn
= DEVICE_REQUEST
;
4125 reschedule_timeout(MAXTIMEOUT
, "floppy init", MAXTIMEOUT
);
4128 for (i
= 0; i
< N_FDC
; i
++) {
4134 /*sparcs don't have a DOR reset which we can fall back on to*/
4135 FDCS
->version
= FDC_82072A
;
4139 use_virtual_dma
= can_use_virtual_dma
& 1;
4140 fdc_state
[0].address
= FDC1
;
4141 if (fdc_state
[0].address
== -1) {
4142 unregister_blkdev(MAJOR_NR
,"fd");
4143 del_timer(&fd_timeout
);
4147 fdc_state
[1].address
= FDC2
;
4150 fdc
= 0; /* reset fdc in case of unexpected interrupt */
4151 if (floppy_grab_irq_and_dma()){
4152 del_timer(&fd_timeout
);
4153 blk_dev
[MAJOR_NR
].request_fn
= NULL
;
4154 unregister_blkdev(MAJOR_NR
,"fd");
4155 del_timer(&fd_timeout
);
4159 /* initialise drive state */
4160 for (drive
= 0; drive
< N_DRIVE
; drive
++) {
4163 USETF(FD_DISK_NEWCHANGE
);
4164 USETF(FD_DISK_CHANGED
);
4166 UDRS
->fd_device
= -1;
4167 floppy_track_buffer
= NULL
;
4168 max_buffer_sectors
= 0;
4171 for (i
= 0; i
< N_FDC
; i
++) {
4173 FDCS
->driver_version
= FD_DRIVER_VERSION
;
4174 for (unit
=0; unit
<4; unit
++)
4175 FDCS
->track
[unit
] = 0;
4176 if (FDCS
->address
== -1)
4179 if (user_reset_fdc(-1,FD_RESET_ALWAYS
,0)){
4180 /* free ioports reserved by floppy_grab_irq_and_dma() */
4181 release_region(FDCS
->address
, 6);
4182 release_region(FDCS
->address
+7, 1);
4184 FDCS
->version
= FDC_NONE
;
4187 /* Try to determine the floppy controller type */
4188 FDCS
->version
= get_fdc_version();
4189 if (FDCS
->version
== FDC_NONE
){
4190 /* free ioports reserved by floppy_grab_irq_and_dma() */
4191 release_region(FDCS
->address
, 6);
4192 release_region(FDCS
->address
+7, 1);
4196 if(can_use_virtual_dma
== 2 && FDCS
->version
< FDC_82072A
)
4197 can_use_virtual_dma
= 0;
4200 /* Not all FDCs seem to be able to handle the version command
4201 * properly, so force a reset for the standard FDC clones,
4202 * to avoid interrupt garbage.
4204 user_reset_fdc(-1,FD_RESET_ALWAYS
,0);
4207 del_timer(&fd_timeout
);
4209 floppy_release_irq_and_dma();
4213 DPRINT("no floppy controllers found\n");
4214 floppy_tq
.routine
= (void *)(void *) empty
;
4215 mark_bh(IMMEDIATE_BH
);
4218 floppy_release_irq_and_dma();
4219 blk_dev
[MAJOR_NR
].request_fn
= NULL
;
4220 unregister_blkdev(MAJOR_NR
,"fd");
4225 static int floppy_grab_irq_and_dma(void)
4227 unsigned long flags
;
4236 if (fd_request_irq()) {
4237 DPRINT("Unable to grab IRQ%d for the floppy driver\n",
4243 if (fd_request_dma()) {
4244 DPRINT("Unable to grab DMA%d for the floppy driver\n",
4252 for (fdc
=0; fdc
< N_FDC
; fdc
++){
4253 if (FDCS
->address
!= -1){
4254 if (check_region(FDCS
->address
, 6) < 0 ||
4255 check_region(FDCS
->address
+7, 1) < 0) {
4256 DPRINT("Floppy io-port 0x%04lx in use\n", FDCS
->address
);
4260 release_region(FDCS
->address
, 6);
4261 release_region(FDCS
->address
+7, 1);
4267 request_region(FDCS
->address
, 6, "floppy");
4268 request_region(FDCS
->address
+7, 1, "floppy DIR");
4269 /* address + 6 is reserved, and may be taken by IDE.
4270 * Unfortunately, Adaptec doesn't know this :-(, */
4273 for (fdc
=0; fdc
< N_FDC
; fdc
++){
4274 if (FDCS
->address
!= -1){
4276 fd_outb(FDCS
->dor
, FD_DOR
);
4280 set_dor(0, ~0, 8); /* avoid immediate interrupt */
4282 for (fdc
= 0; fdc
< N_FDC
; fdc
++)
4283 if (FDCS
->address
!= -1)
4284 fd_outb(FDCS
->dor
, FD_DOR
);
4286 * The driver will try and free resources and relies on us
4287 * to know if they were allocated or not.
4290 irqdma_allocated
= 1;
4294 static void floppy_release_irq_and_dma(void)
4297 #ifdef FLOPPY_SANITY_CHECK
4303 unsigned long tmpaddr
;
4304 unsigned long flags
;
4312 if(irqdma_allocated
)
4323 floppy_enable_hlt();
4325 if (floppy_track_buffer
&& max_buffer_sectors
) {
4326 tmpsize
= max_buffer_sectors
*1024;
4327 tmpaddr
= (unsigned long)floppy_track_buffer
;
4328 floppy_track_buffer
= 0;
4329 max_buffer_sectors
= 0;
4330 buffer_min
= buffer_max
= -1;
4331 fd_dma_mem_free(tmpaddr
, tmpsize
);
4334 #ifdef FLOPPY_SANITY_CHECK
4336 for (drive
=0; drive
< N_FDC
* 4; drive
++)
4337 if (motor_off_timer
[drive
].next
)
4338 printk("motor off timer %d still active\n", drive
);
4341 if (fd_timeout
.next
)
4342 printk("floppy timer still active:%s\n", timeout_message
);
4344 printk("auxiliary floppy timer still active\n");
4346 printk("task queue still active\n");
4349 for (fdc
= 0; fdc
< N_FDC
; fdc
++)
4350 if (FDCS
->address
!= -1) {
4351 release_region(FDCS
->address
, 6);
4352 release_region(FDCS
->address
+7, 1);
4361 extern char *get_options(char *str
, int *ints
);
4365 __initfunc(static void parse_floppy_cfg_string(char *cfg
))
4371 for(ptr
= cfg
;*cfg
&& *cfg
!= ' ' && *cfg
!= '\t'; cfg
++);
4377 floppy_setup(get_options(ptr
,ints
),ints
);
4381 __initfunc(static void mod_setup(char *pattern
, void (*setup
)(char *, int *)))
4389 int length
= strlen(pattern
)+1;
4394 for (i
=current
->mm
->env_start
; i
< current
->mm
->env_end
; i
++){
4395 get_user(c
, (char *)i
);
4400 if (!c
|| c
== ' ' || c
== '\t'){
4403 setup(get_options(buffer
,ints
),ints
);
4412 if ((!j
&& !c
) || (j
&& c
== pattern
[j
-1]))
4427 int init_module(void)
4429 printk(KERN_INFO
"inserting floppy driver for " UTS_RELEASE
"\n");
4432 parse_floppy_cfg_string(floppy
);
4434 mod_setup("floppy=", floppy_setup
);
4436 return floppy_init();
4439 void cleanup_module(void)
4443 unregister_blkdev(MAJOR_NR
, "fd");
4445 blk_dev
[MAJOR_NR
].request_fn
= 0;
4446 /* eject disk, if any */
4447 dummy
= fd_eject(0);
4450 MODULE_PARM(floppy
,"s");
4451 MODULE_PARM(FLOPPY_IRQ
,"i");
4452 MODULE_PARM(FLOPPY_DMA
,"i");
4453 MODULE_AUTHOR("Alain L. Knaff");
4454 MODULE_SUPPORTED_DEVICE("fd");
4461 /* eject the boot floppy (if we need the drive for a different root floppy) */
4462 /* This should only be called at boot time when we're sure that there's no
4463 * resource contention. */
4464 void floppy_eject(void)
4469 if(floppy_grab_irq_and_dma()==0)
4471 lock_fdc(MAXTIMEOUT
,0);
4473 process_fd_request();
4474 floppy_release_irq_and_dma();