Import 2.1.81
[davej-history.git] / drivers / block / floppy.c
blob94b90dd94231322afeab66f3111433481fa5396c
1 /*
2 * linux/kernel/floppy.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 * Copyright (C) 1993, 1994 Alain Knaff
6 */
7 /*
8 * 02.12.91 - Changed to static variables to indicate need for reset
9 * and recalibrate. This makes some things easier (output_byte reset
10 * checking etc), and means less interrupt jumping in case of errors,
11 * so the code is hopefully easier to understand.
15 * This file is certainly a mess. I've tried my best to get it working,
16 * but I don't like programming floppies, and I have only one anyway.
17 * Urgel. I should check for more errors, and do more graceful error
18 * recovery. Seems there are problems with several drives. I've tried to
19 * correct them. No promises.
23 * As with hd.c, all routines within this file can (and will) be called
24 * by interrupts, so extreme caution is needed. A hardware interrupt
25 * handler may not sleep, or a kernel panic will happen. Thus I cannot
26 * call "floppy-on" directly, but have to set a special timer interrupt
27 * etc.
31 * 28.02.92 - made track-buffering routines, based on the routines written
32 * by entropy@wintermute.wpi.edu (Lawrence Foard). Linus.
36 * Automatic floppy-detection and formatting written by Werner Almesberger
37 * (almesber@nessie.cs.id.ethz.ch), who also corrected some problems with
38 * the floppy-change signal detection.
42 * 1992/7/22 -- Hennus Bergman: Added better error reporting, fixed
43 * FDC data overrun bug, added some preliminary stuff for vertical
44 * recording support.
46 * 1992/9/17: Added DMA allocation & DMA functions. -- hhb.
48 * TODO: Errors are still not counted properly.
51 /* 1992/9/20
52 * Modifications for ``Sector Shifting'' by Rob Hooft (hooft@chem.ruu.nl)
53 * modeled after the freeware MS-DOS program fdformat/88 V1.8 by
54 * Christoph H. Hochst\"atter.
55 * I have fixed the shift values to the ones I always use. Maybe a new
56 * ioctl() should be created to be able to modify them.
57 * There is a bug in the driver that makes it impossible to format a
58 * floppy as the first thing after bootup.
62 * 1993/4/29 -- Linus -- cleaned up the timer handling in the kernel, and
63 * this helped the floppy driver as well. Much cleaner, and still seems to
64 * work.
67 /* 1994/6/24 --bbroad-- added the floppy table entries and made
68 * minor modifications to allow 2.88 floppies to be run.
71 /* 1994/7/13 -- Paul Vojta -- modified the probing code to allow three or more
72 * disk types.
76 * 1994/8/8 -- Alain Knaff -- Switched to fdpatch driver: Support for bigger
77 * format bug fixes, but unfortunately some new bugs too...
80 /* 1994/9/17 -- Koen Holtman -- added logging of physical floppy write
81 * errors to allow safe writing by specialized programs.
84 /* 1995/4/24 -- Dan Fandrich -- added support for Commodore 1581 3.5" disks
85 * by defining bit 1 of the "stretch" parameter to mean put sectors on the
86 * opposite side of the disk, leaving the sector IDs alone (i.e. Commodore's
87 * drives are "upside-down").
91 * 1995/8/26 -- Andreas Busse -- added Mips support.
95 * 1995/10/18 -- Ralf Baechle -- Portability cleanup; move machine dependent
96 * features to asm/floppy.h.
100 #define FLOPPY_SANITY_CHECK
101 #undef FLOPPY_SILENT_DCL_CLEAR
103 #define REALLY_SLOW_IO
105 #define DEBUGT 2
106 #define DCL_DEBUG /* debug disk change line */
108 /* do print messages for unexpected interrupts */
109 static int print_unex=1;
110 #include <linux/module.h>
112 /* the following is the mask of allowed drives. By default units 2 and
113 * 3 of both floppy controllers are disabled, because switching on the
114 * motor of these drives causes system hangs on some PCI computers. drive
115 * 0 is the low bit (0x1), and drive 7 is the high bit (0x80). Bits are on if
116 * a drive is allowed. */
117 static int FLOPPY_IRQ=6;
118 static int FLOPPY_DMA=2;
119 static int allowed_drive_mask = 0x33;
122 #include <linux/sched.h>
123 #include <linux/fs.h>
124 #include <linux/kernel.h>
125 #include <linux/timer.h>
126 #include <linux/tqueue.h>
127 #define FDPATCHES
128 #include <linux/fdreg.h>
131 #include <linux/fd.h>
132 #include <linux/hdreg.h>
134 #include <linux/errno.h>
135 #include <linux/malloc.h>
136 #include <linux/mm.h>
137 #include <linux/string.h>
138 #include <linux/fcntl.h>
139 #include <linux/delay.h>
140 #include <linux/mc146818rtc.h> /* CMOS defines */
141 #include <linux/ioport.h>
142 #include <linux/interrupt.h>
143 #include <linux/init.h>
145 #include <asm/dma.h>
146 #include <asm/irq.h>
147 #include <asm/system.h>
148 #include <asm/io.h>
149 #include <asm/uaccess.h>
151 static int can_use_virtual_dma=2;
152 /* =======
153 * can use virtual DMA:
154 * 0 = use of virtual DMA disallowed by config
155 * 1 = use of virtual DMA prescribed by config
156 * 2 = no virtual DMA preference configured. By default try hard DMA,
157 * but fall back on virtual DMA when not enough memory available
160 static int use_virtual_dma=0;
161 /* =======
162 * use virtual DMA
163 * 0 using hard DMA
164 * 1 using virtual DMA
165 * This variable is set to virtual when a DMA mem problem arises, and
166 * reset back in floppy_grab_irq_and_dma.
167 * It is not safe to reset it in other circumstances, because the floppy
168 * driver may have several buffers in use at once, and we do currently not
169 * record each buffers capabilities
172 static unsigned short virtual_dma_port=0x3f0;
173 void floppy_interrupt(int irq, void *dev_id, struct pt_regs * regs);
174 static int set_dor(int fdc, char mask, char data);
175 static inline int __get_order(unsigned long size);
176 #define K_64 0x10000 /* 64KB */
177 #include <asm/floppy.h>
180 #define MAJOR_NR FLOPPY_MAJOR
182 #include <linux/blk.h>
183 #include <linux/cdrom.h> /* for the compatibility eject ioctl */
185 #ifndef fd_get_dma_residue
186 #define fd_get_dma_residue() get_dma_residue(FLOPPY_DMA)
187 #endif
189 /* Dma Memory related stuff */
191 /* Pure 2^n version of get_order */
192 static inline int __get_order(unsigned long size)
194 int order;
196 size = (size-1) >> (PAGE_SHIFT-1);
197 order = -1;
198 do {
199 size >>= 1;
200 order++;
201 } while (size);
202 return order;
205 #ifndef fd_dma_mem_free
206 #define fd_dma_mem_free(addr, size) free_pages(addr, __get_order(size))
207 #endif
209 #ifndef fd_dma_mem_alloc
210 #define fd_dma_mem_alloc(size) __get_dma_pages(GFP_KERNEL,__get_order(size))
211 #endif
213 static inline void fallback_on_nodma_alloc(char **addr, size_t l)
215 #ifdef FLOPPY_CAN_FALLBACK_ON_NODMA
216 if(*addr)
217 return; /* we have the memory */
218 if(can_use_virtual_dma != 2)
219 return; /* no fallback allowed */
220 printk("DMA memory shortage. Temporarily falling back on virtual DMA\n");
221 *addr = (char *) nodma_mem_alloc(l);
222 #else
223 return;
224 #endif
227 /* End dma memory related stuff */
229 static unsigned int fake_change = 0;
230 static int initialising=1;
232 static inline int TYPE(kdev_t x) {
233 return (MINOR(x)>>2) & 0x1f;
235 static inline int DRIVE(kdev_t x) {
236 return (MINOR(x)&0x03) | ((MINOR(x)&0x80) >> 5);
238 #define ITYPE(x) (((x)>>2) & 0x1f)
239 #define TOMINOR(x) ((x & 3) | ((x & 4) << 5))
240 #define UNIT(x) ((x) & 0x03) /* drive on fdc */
241 #define FDC(x) (((x) & 0x04) >> 2) /* fdc of drive */
242 #define REVDRIVE(fdc, unit) ((unit) + ((fdc) << 2))
243 /* reverse mapping from unit and fdc to drive */
244 #define DP (&drive_params[current_drive])
245 #define DRS (&drive_state[current_drive])
246 #define DRWE (&write_errors[current_drive])
247 #define FDCS (&fdc_state[fdc])
248 #define CLEARF(x) (clear_bit(x##_BIT, &DRS->flags))
249 #define SETF(x) (set_bit(x##_BIT, &DRS->flags))
250 #define TESTF(x) (test_bit(x##_BIT, &DRS->flags))
252 #define UDP (&drive_params[drive])
253 #define UDRS (&drive_state[drive])
254 #define UDRWE (&write_errors[drive])
255 #define UFDCS (&fdc_state[FDC(drive)])
256 #define UCLEARF(x) (clear_bit(x##_BIT, &UDRS->flags))
257 #define USETF(x) (set_bit(x##_BIT, &UDRS->flags))
258 #define UTESTF(x) (test_bit(x##_BIT, &UDRS->flags))
260 #define DPRINT(format, args...) printk(DEVICE_NAME "%d: " format, current_drive , ## args)
262 #define PH_HEAD(floppy,head) (((((floppy)->stretch & 2) >>1) ^ head) << 2)
263 #define STRETCH(floppy) ((floppy)->stretch & FD_STRETCH)
265 #define CLEARSTRUCT(x) memset((x), 0, sizeof(*(x)))
267 #define INT_OFF save_flags(flags); cli()
268 #define INT_ON restore_flags(flags)
270 /* read/write */
271 #define COMMAND raw_cmd->cmd[0]
272 #define DR_SELECT raw_cmd->cmd[1]
273 #define TRACK raw_cmd->cmd[2]
274 #define HEAD raw_cmd->cmd[3]
275 #define SECTOR raw_cmd->cmd[4]
276 #define SIZECODE raw_cmd->cmd[5]
277 #define SECT_PER_TRACK raw_cmd->cmd[6]
278 #define GAP raw_cmd->cmd[7]
279 #define SIZECODE2 raw_cmd->cmd[8]
280 #define NR_RW 9
282 /* format */
283 #define F_SIZECODE raw_cmd->cmd[2]
284 #define F_SECT_PER_TRACK raw_cmd->cmd[3]
285 #define F_GAP raw_cmd->cmd[4]
286 #define F_FILL raw_cmd->cmd[5]
287 #define NR_F 6
290 * Maximum disk size (in kilobytes). This default is used whenever the
291 * current disk size is unknown.
292 * [Now it is rather a minimum]
294 #define MAX_DISK_SIZE 4 /* 3984*/
298 * globals used by 'result()'
300 #define MAX_REPLIES 16
301 static unsigned char reply_buffer[MAX_REPLIES];
302 static int inr; /* size of reply buffer, when called from interrupt */
303 #define ST0 (reply_buffer[0])
304 #define ST1 (reply_buffer[1])
305 #define ST2 (reply_buffer[2])
306 #define ST3 (reply_buffer[0]) /* result of GETSTATUS */
307 #define R_TRACK (reply_buffer[3])
308 #define R_HEAD (reply_buffer[4])
309 #define R_SECTOR (reply_buffer[5])
310 #define R_SIZECODE (reply_buffer[6])
312 #define SEL_DLY (2*HZ/100)
314 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
316 * this struct defines the different floppy drive types.
318 static struct {
319 struct floppy_drive_params params;
320 const char *name; /* name printed while booting */
321 } default_drive_params[]= {
322 /* NOTE: the time values in jiffies should be in msec!
323 CMOS drive type
324 | Maximum data rate supported by drive type
325 | | Head load time, msec
326 | | | Head unload time, msec (not used)
327 | | | | Step rate interval, usec
328 | | | | | Time needed for spinup time (jiffies)
329 | | | | | | Timeout for spinning down (jiffies)
330 | | | | | | | Spindown offset (where disk stops)
331 | | | | | | | | Select delay
332 | | | | | | | | | RPS
333 | | | | | | | | | | Max number of tracks
334 | | | | | | | | | | | Interrupt timeout
335 | | | | | | | | | | | | Max nonintlv. sectors
336 | | | | | | | | | | | | | -Max Errors- flags */
337 {{0, 500, 16, 16, 8000, 1*HZ, 3*HZ, 0, SEL_DLY, 5, 80, 3*HZ, 20, {3,1,2,0,2}, 0,
338 0, { 7, 4, 8, 2, 1, 5, 3,10}, 3*HZ/2, 0 }, "unknown" },
340 {{1, 300, 16, 16, 8000, 1*HZ, 3*HZ, 0, SEL_DLY, 5, 40, 3*HZ, 17, {3,1,2,0,2}, 0,
341 0, { 1, 0, 0, 0, 0, 0, 0, 0}, 3*HZ/2, 1 }, "360K PC" }, /*5 1/4 360 KB PC*/
343 {{2, 500, 16, 16, 6000, 4*HZ/10, 3*HZ, 14, SEL_DLY, 6, 83, 3*HZ, 17, {3,1,2,0,2}, 0,
344 0, { 2, 5, 6,23,10,20,11, 0}, 3*HZ/2, 2 }, "1.2M" }, /*5 1/4 HD AT*/
346 {{3, 250, 16, 16, 3000, 1*HZ, 3*HZ, 0, SEL_DLY, 5, 83, 3*HZ, 20, {3,1,2,0,2}, 0,
347 0, { 4,22,21,30, 3, 0, 0, 0}, 3*HZ/2, 4 }, "720k" }, /*3 1/2 DD*/
349 {{4, 500, 16, 16, 4000, 4*HZ/10, 3*HZ, 10, SEL_DLY, 5, 83, 3*HZ, 20, {3,1,2,0,2}, 0,
350 0, { 7, 4,25,22,31,21,29,11}, 3*HZ/2, 7 }, "1.44M" }, /*3 1/2 HD*/
352 {{5, 1000, 15, 8, 3000, 4*HZ/10, 3*HZ, 10, SEL_DLY, 5, 83, 3*HZ, 40, {3,1,2,0,2}, 0,
353 0, { 7, 8, 4,25,28,22,31,21}, 3*HZ/2, 8 }, "2.88M AMI BIOS" }, /*3 1/2 ED*/
355 {{6, 1000, 15, 8, 3000, 4*HZ/10, 3*HZ, 10, SEL_DLY, 5, 83, 3*HZ, 40, {3,1,2,0,2}, 0,
356 0, { 7, 8, 4,25,28,22,31,21}, 3*HZ/2, 8 }, "2.88M" } /*3 1/2 ED*/
357 /* | --autodetected formats--- | | |
358 * read_track | | Name printed when booting
359 * | Native format
360 * Frequency of disk change checks */
363 static struct floppy_drive_params drive_params[N_DRIVE];
364 static struct floppy_drive_struct drive_state[N_DRIVE];
365 static struct floppy_write_errors write_errors[N_DRIVE];
366 static struct floppy_raw_cmd *raw_cmd, default_raw_cmd;
369 * This struct defines the different floppy types.
371 * Bit 0 of 'stretch' tells if the tracks need to be doubled for some
372 * types (e.g. 360kB diskette in 1.2MB drive, etc.). Bit 1 of 'stretch'
373 * tells if the disk is in Commodore 1581 format, which means side 0 sectors
374 * are located on side 1 of the disk but with a side 0 ID, and vice-versa.
375 * This is the same as the Sharp MZ-80 5.25" CP/M disk format, except that the
376 * 1581's logical side 0 is on physical side 1, whereas the Sharp's logical
377 * side 0 is on physical side 0 (but with the misnamed sector IDs).
378 * 'stretch' should probably be renamed to something more general, like
379 * 'options'. Other parameters should be self-explanatory (see also
380 * setfdprm(8)).
382 static struct floppy_struct floppy_type[32] = {
383 { 0, 0,0, 0,0,0x00,0x00,0x00,0x00,NULL }, /* 0 no testing */
384 { 720, 9,2,40,0,0x2A,0x02,0xDF,0x50,"d360" }, /* 1 360KB PC */
385 { 2400,15,2,80,0,0x1B,0x00,0xDF,0x54,"h1200" }, /* 2 1.2MB AT */
386 { 720, 9,1,80,0,0x2A,0x02,0xDF,0x50,"D360" }, /* 3 360KB SS 3.5" */
387 { 1440, 9,2,80,0,0x2A,0x02,0xDF,0x50,"D720" }, /* 4 720KB 3.5" */
388 { 720, 9,2,40,1,0x23,0x01,0xDF,0x50,"h360" }, /* 5 360KB AT */
389 { 1440, 9,2,80,0,0x23,0x01,0xDF,0x50,"h720" }, /* 6 720KB AT */
390 { 2880,18,2,80,0,0x1B,0x00,0xCF,0x6C,"H1440" }, /* 7 1.44MB 3.5" */
391 { 5760,36,2,80,0,0x1B,0x43,0xAF,0x54,"E2880" }, /* 8 2.88MB 3.5" */
392 { 6240,39,2,80,0,0x1B,0x43,0xAF,0x28,"E3120"}, /* 9 3.12MB 3.5" */
394 { 2880,18,2,80,0,0x25,0x00,0xDF,0x02,"h1440" }, /* 10 1.44MB 5.25" */
395 { 3360,21,2,80,0,0x1C,0x00,0xCF,0x0C,"H1680" }, /* 11 1.68MB 3.5" */
396 { 820,10,2,41,1,0x25,0x01,0xDF,0x2E,"h410" }, /* 12 410KB 5.25" */
397 { 1640,10,2,82,0,0x25,0x02,0xDF,0x2E,"H820" }, /* 13 820KB 3.5" */
398 { 2952,18,2,82,0,0x25,0x00,0xDF,0x02,"h1476" }, /* 14 1.48MB 5.25" */
399 { 3444,21,2,82,0,0x25,0x00,0xDF,0x0C,"H1722" }, /* 15 1.72MB 3.5" */
400 { 840,10,2,42,1,0x25,0x01,0xDF,0x2E,"h420" }, /* 16 420KB 5.25" */
401 { 1660,10,2,83,0,0x25,0x02,0xDF,0x2E,"H830" }, /* 17 830KB 3.5" */
402 { 2988,18,2,83,0,0x25,0x00,0xDF,0x02,"h1494" }, /* 18 1.49MB 5.25" */
403 { 3486,21,2,83,0,0x25,0x00,0xDF,0x0C,"H1743" }, /* 19 1.74 MB 3.5" */
405 { 1760,11,2,80,0,0x1C,0x09,0xCF,0x00,"h880" }, /* 20 880KB 5.25" */
406 { 2080,13,2,80,0,0x1C,0x01,0xCF,0x00,"D1040" }, /* 21 1.04MB 3.5" */
407 { 2240,14,2,80,0,0x1C,0x19,0xCF,0x00,"D1120" }, /* 22 1.12MB 3.5" */
408 { 3200,20,2,80,0,0x1C,0x20,0xCF,0x2C,"h1600" }, /* 23 1.6MB 5.25" */
409 { 3520,22,2,80,0,0x1C,0x08,0xCF,0x2e,"H1760" }, /* 24 1.76MB 3.5" */
410 { 3840,24,2,80,0,0x1C,0x20,0xCF,0x00,"H1920" }, /* 25 1.92MB 3.5" */
411 { 6400,40,2,80,0,0x25,0x5B,0xCF,0x00,"E3200" }, /* 26 3.20MB 3.5" */
412 { 7040,44,2,80,0,0x25,0x5B,0xCF,0x00,"E3520" }, /* 27 3.52MB 3.5" */
413 { 7680,48,2,80,0,0x25,0x63,0xCF,0x00,"E3840" }, /* 28 3.84MB 3.5" */
415 { 3680,23,2,80,0,0x1C,0x10,0xCF,0x00,"H1840" }, /* 29 1.84MB 3.5" */
416 { 1600,10,2,80,0,0x25,0x02,0xDF,0x2E,"D800" }, /* 30 800KB 3.5" */
417 { 3200,20,2,80,0,0x1C,0x00,0xCF,0x2C,"H1600" }, /* 31 1.6MB 3.5" */
420 #define NUMBER(x) (sizeof(x) / sizeof(*(x)))
421 #define SECTSIZE (_FD_SECTSIZE(*floppy))
423 /* Auto-detection: Disk type used until the next media change occurs. */
424 static struct floppy_struct *current_type[N_DRIVE] = {
425 NULL, NULL, NULL, NULL,
426 NULL, NULL, NULL, NULL
430 * User-provided type information. current_type points to
431 * the respective entry of this array.
433 static struct floppy_struct user_params[N_DRIVE];
435 static int floppy_sizes[256];
436 static int floppy_blocksizes[256] = { 0, };
439 * The driver is trying to determine the correct media format
440 * while probing is set. rw_interrupt() clears it after a
441 * successful access.
443 static int probing = 0;
445 /* Synchronization of FDC access. */
446 #define FD_COMMAND_NONE -1
447 #define FD_COMMAND_ERROR 2
448 #define FD_COMMAND_OKAY 3
450 static volatile int command_status = FD_COMMAND_NONE, fdc_busy = 0;
451 static struct wait_queue *fdc_wait = NULL, *command_done = NULL;
452 #define NO_SIGNAL (!interruptible || !signal_pending(current))
453 #define CALL(x) if ((x) == -EINTR) return -EINTR
454 #define ECALL(x) if ((ret = (x))) return ret;
455 #define _WAIT(x,i) CALL(ret=wait_til_done((x),i))
456 #define WAIT(x) _WAIT((x),interruptible)
457 #define IWAIT(x) _WAIT((x),1)
459 /* Errors during formatting are counted here. */
460 static int format_errors;
462 /* Format request descriptor. */
463 static struct format_descr format_req;
466 * Rate is 0 for 500kb/s, 1 for 300kbps, 2 for 250kbps
467 * Spec1 is 0xSH, where S is stepping rate (F=1ms, E=2ms, D=3ms etc),
468 * H is head unload time (1=16ms, 2=32ms, etc)
472 * Track buffer
473 * Because these are written to by the DMA controller, they must
474 * not contain a 64k byte boundary crossing, or data will be
475 * corrupted/lost.
477 static char *floppy_track_buffer=0;
478 static int max_buffer_sectors=0;
480 static int *errors;
481 typedef void (*done_f)(int);
482 static struct cont_t {
483 void (*interrupt)(void); /* this is called after the interrupt of the
484 * main command */
485 void (*redo)(void); /* this is called to retry the operation */
486 void (*error)(void); /* this is called to tally an error */
487 done_f done; /* this is called to say if the operation has
488 * succeeded/failed */
489 } *cont=NULL;
491 static void floppy_ready(void);
492 static void floppy_start(void);
493 static void process_fd_request(void);
494 static void recalibrate_floppy(void);
495 static void floppy_shutdown(void);
497 static int floppy_grab_irq_and_dma(void);
498 static void floppy_release_irq_and_dma(void);
501 * The "reset" variable should be tested whenever an interrupt is scheduled,
502 * after the commands have been sent. This is to ensure that the driver doesn't
503 * get wedged when the interrupt doesn't come because of a failed command.
504 * reset doesn't need to be tested before sending commands, because
505 * output_byte is automatically disabled when reset is set.
507 #define CHECK_RESET { if (FDCS->reset){ reset_fdc(); return; } }
508 static void reset_fdc(void);
511 * These are global variables, as that's the easiest way to give
512 * information to interrupts. They are the data used for the current
513 * request.
515 #define NO_TRACK -1
516 #define NEED_1_RECAL -2
517 #define NEED_2_RECAL -3
519 /* */
520 static int usage_count = 0;
523 /* buffer related variables */
524 static int buffer_track = -1;
525 static int buffer_drive = -1;
526 static int buffer_min = -1;
527 static int buffer_max = -1;
529 /* fdc related variables, should end up in a struct */
530 static struct floppy_fdc_state fdc_state[N_FDC];
531 static int fdc; /* current fdc */
533 static struct floppy_struct *_floppy = floppy_type;
534 static unsigned char current_drive = 0;
535 static long current_count_sectors = 0;
536 static unsigned char sector_t; /* sector in track */
538 #ifndef fd_eject
539 #define fd_eject(x) -EINVAL
540 #endif
543 #ifdef DEBUGT
544 static long unsigned debugtimer;
545 #endif
548 * Debugging
549 * =========
551 static inline void set_debugt(void)
553 #ifdef DEBUGT
554 debugtimer = jiffies;
555 #endif
558 static inline void debugt(const char *message)
560 #ifdef DEBUGT
561 if (DP->flags & DEBUGT)
562 printk("%s dtime=%lu\n", message, jiffies-debugtimer);
563 #endif
566 typedef void (*timeout_fn)(unsigned long);
567 static struct timer_list fd_timeout ={ NULL, NULL, 0, 0,
568 (timeout_fn) floppy_shutdown };
570 static const char *timeout_message;
572 #ifdef FLOPPY_SANITY_CHECK
573 static void is_alive(const char *message)
575 /* this routine checks whether the floppy driver is "alive" */
576 if (fdc_busy && command_status < 2 && !fd_timeout.prev){
577 DPRINT("timeout handler died: %s\n",message);
580 #endif
582 #ifdef FLOPPY_SANITY_CHECK
584 #define OLOGSIZE 20
586 static void (*lasthandler)(void) = NULL;
587 static int interruptjiffies=0;
588 static int resultjiffies=0;
589 static int resultsize=0;
590 static int lastredo=0;
592 static struct output_log {
593 unsigned char data;
594 unsigned char status;
595 unsigned long jiffies;
596 } output_log[OLOGSIZE];
598 static int output_log_pos=0;
599 #endif
601 #define CURRENTD -1
602 #define MAXTIMEOUT -2
604 static void reschedule_timeout(int drive, const char *message, int marg)
606 if (drive == CURRENTD)
607 drive = current_drive;
608 del_timer(&fd_timeout);
609 if (drive < 0 || drive > N_DRIVE) {
610 fd_timeout.expires = jiffies + 20*HZ;
611 drive=0;
612 } else
613 fd_timeout.expires = jiffies + UDP->timeout;
614 add_timer(&fd_timeout);
615 if (UDP->flags & FD_DEBUG){
616 DPRINT("reschedule timeout ");
617 printk(message, marg);
618 printk("\n");
620 timeout_message = message;
623 static int maximum(int a, int b)
625 if(a > b)
626 return a;
627 else
628 return b;
630 #define INFBOUND(a,b) (a)=maximum((a),(b));
632 static int minimum(int a, int b)
634 if(a < b)
635 return a;
636 else
637 return b;
639 #define SUPBOUND(a,b) (a)=minimum((a),(b));
643 * Bottom half floppy driver.
644 * ==========================
646 * This part of the file contains the code talking directly to the hardware,
647 * and also the main service loop (seek-configure-spinup-command)
651 * disk change.
652 * This routine is responsible for maintaining the FD_DISK_CHANGE flag,
653 * and the last_checked date.
655 * last_checked is the date of the last check which showed 'no disk change'
656 * FD_DISK_CHANGE is set under two conditions:
657 * 1. The floppy has been changed after some i/o to that floppy already
658 * took place.
659 * 2. No floppy disk is in the drive. This is done in order to ensure that
660 * requests are quickly flushed in case there is no disk in the drive. It
661 * follows that FD_DISK_CHANGE can only be cleared if there is a disk in
662 * the drive.
664 * For 1., maxblock is observed. Maxblock is 0 if no i/o has taken place yet.
665 * For 2., FD_DISK_NEWCHANGE is watched. FD_DISK_NEWCHANGE is cleared on
666 * each seek. If a disk is present, the disk change line should also be
667 * cleared on each seek. Thus, if FD_DISK_NEWCHANGE is clear, but the disk
668 * change line is set, this means either that no disk is in the drive, or
669 * that it has been removed since the last seek.
671 * This means that we really have a third possibility too:
672 * The floppy has been changed after the last seek.
675 static int disk_change(int drive)
677 int fdc=FDC(drive);
678 #ifdef FLOPPY_SANITY_CHECK
679 if (jiffies - UDRS->select_date < UDP->select_delay)
680 DPRINT("WARNING disk change called early\n");
681 if (!(FDCS->dor & (0x10 << UNIT(drive))) ||
682 (FDCS->dor & 3) != UNIT(drive) ||
683 fdc != FDC(drive)){
684 DPRINT("probing disk change on unselected drive\n");
685 DPRINT("drive=%d fdc=%d dor=%x\n",drive, FDC(drive),
686 FDCS->dor);
688 #endif
690 #ifdef DCL_DEBUG
691 if (UDP->flags & FD_DEBUG){
692 DPRINT("checking disk change line for drive %d\n",drive);
693 DPRINT("jiffies=%ld\n", jiffies);
694 DPRINT("disk change line=%x\n",fd_inb(FD_DIR)&0x80);
695 DPRINT("flags=%x\n",UDRS->flags);
697 #endif
698 if (UDP->flags & FD_BROKEN_DCL)
699 return UTESTF(FD_DISK_CHANGED);
700 if ((fd_inb(FD_DIR) ^ UDP->flags) & 0x80){
701 USETF(FD_VERIFY); /* verify write protection */
702 if (UDRS->maxblock){
703 /* mark it changed */
704 USETF(FD_DISK_CHANGED);
707 /* invalidate its geometry */
708 if (UDRS->keep_data >= 0) {
709 if ((UDP->flags & FTD_MSG) &&
710 current_type[drive] != NULL)
711 DPRINT("Disk type is undefined after "
712 "disk change\n");
713 current_type[drive] = NULL;
714 floppy_sizes[TOMINOR(drive)] = MAX_DISK_SIZE;
717 /*USETF(FD_DISK_NEWCHANGE);*/
718 return 1;
719 } else {
720 UDRS->last_checked=jiffies;
721 UCLEARF(FD_DISK_NEWCHANGE);
723 return 0;
726 static inline int is_selected(int dor, int unit)
728 return ((dor & (0x10 << unit)) && (dor &3) == unit);
731 static int set_dor(int fdc, char mask, char data)
733 register unsigned char drive, unit, newdor,olddor;
735 if (FDCS->address == -1)
736 return -1;
738 olddor = FDCS->dor;
739 newdor = (olddor & mask) | data;
740 if (newdor != olddor){
741 unit = olddor & 0x3;
742 if (is_selected(olddor, unit) && !is_selected(newdor,unit)){
743 drive = REVDRIVE(fdc,unit);
744 #ifdef DCL_DEBUG
745 if (UDP->flags & FD_DEBUG){
746 DPRINT("calling disk change from set_dor\n");
748 #endif
749 disk_change(drive);
751 FDCS->dor = newdor;
752 fd_outb(newdor, FD_DOR);
754 unit = newdor & 0x3;
755 if (!is_selected(olddor, unit) && is_selected(newdor,unit)){
756 drive = REVDRIVE(fdc,unit);
757 UDRS->select_date = jiffies;
760 if (newdor & FLOPPY_MOTOR_MASK)
761 floppy_grab_irq_and_dma();
762 if (olddor & FLOPPY_MOTOR_MASK)
763 floppy_release_irq_and_dma();
764 return olddor;
767 static void twaddle(void)
769 if (DP->select_delay)
770 return;
771 fd_outb(FDCS->dor & ~(0x10<<UNIT(current_drive)),FD_DOR);
772 fd_outb(FDCS->dor, FD_DOR);
773 DRS->select_date = jiffies;
776 /* reset all driver information about the current fdc. This is needed after
777 * a reset, and after a raw command. */
778 static void reset_fdc_info(int mode)
780 int drive;
782 FDCS->spec1 = FDCS->spec2 = -1;
783 FDCS->need_configure = 1;
784 FDCS->perp_mode = 1;
785 FDCS->rawcmd = 0;
786 for (drive = 0; drive < N_DRIVE; drive++)
787 if (FDC(drive) == fdc &&
788 (mode || UDRS->track != NEED_1_RECAL))
789 UDRS->track = NEED_2_RECAL;
792 /* selects the fdc and drive, and enables the fdc's input/dma. */
793 static void set_fdc(int drive)
795 if (drive >= 0 && drive < N_DRIVE){
796 fdc = FDC(drive);
797 current_drive = drive;
799 if (fdc != 1 && fdc != 0) {
800 printk("bad fdc value\n");
801 return;
803 set_dor(fdc,~0,8);
804 #if N_FDC > 1
805 set_dor(1-fdc, ~8, 0);
806 #endif
807 if (FDCS->rawcmd == 2)
808 reset_fdc_info(1);
809 if (fd_inb(FD_STATUS) != STATUS_READY)
810 FDCS->reset = 1;
813 /* locks the driver */
814 static int lock_fdc(int drive, int interruptible)
816 unsigned long flags;
818 if (!usage_count){
819 printk("trying to lock fdc while usage count=0\n");
820 return -1;
822 floppy_grab_irq_and_dma();
823 INT_OFF;
824 while (fdc_busy && NO_SIGNAL)
825 interruptible_sleep_on(&fdc_wait);
826 if (fdc_busy){
827 INT_ON;
828 return -EINTR;
830 fdc_busy = 1;
831 INT_ON;
832 command_status = FD_COMMAND_NONE;
833 reschedule_timeout(drive, "lock fdc", 0);
834 set_fdc(drive);
835 return 0;
838 #define LOCK_FDC(drive,interruptible) \
839 if (lock_fdc(drive,interruptible)) return -EINTR;
842 /* unlocks the driver */
843 static inline void unlock_fdc(void)
845 raw_cmd = 0;
846 if (!fdc_busy)
847 DPRINT("FDC access conflict!\n");
849 if (DEVICE_INTR)
850 DPRINT("device interrupt still active at FDC release: %p!\n",
851 DEVICE_INTR);
852 command_status = FD_COMMAND_NONE;
853 del_timer(&fd_timeout);
854 cont = NULL;
855 fdc_busy = 0;
856 floppy_release_irq_and_dma();
857 wake_up(&fdc_wait);
860 /* switches the motor off after a given timeout */
861 static void motor_off_callback(unsigned long nr)
863 unsigned char mask = ~(0x10 << UNIT(nr));
865 set_dor(FDC(nr), mask, 0);
868 static struct timer_list motor_off_timer[N_DRIVE] = {
869 { NULL, NULL, 0, 0, motor_off_callback },
870 { NULL, NULL, 0, 1, motor_off_callback },
871 { NULL, NULL, 0, 2, motor_off_callback },
872 { NULL, NULL, 0, 3, motor_off_callback },
873 { NULL, NULL, 0, 4, motor_off_callback },
874 { NULL, NULL, 0, 5, motor_off_callback },
875 { NULL, NULL, 0, 6, motor_off_callback },
876 { NULL, NULL, 0, 7, motor_off_callback }
879 /* schedules motor off */
880 static void floppy_off(unsigned int drive)
882 unsigned long volatile delta;
883 register int fdc=FDC(drive);
885 if (!(FDCS->dor & (0x10 << UNIT(drive))))
886 return;
888 del_timer(motor_off_timer+drive);
890 /* make spindle stop in a position which minimizes spinup time
891 * next time */
892 if (UDP->rps){
893 delta = jiffies - UDRS->first_read_date + HZ -
894 UDP->spindown_offset;
895 delta = ((delta * UDP->rps) % HZ) / UDP->rps;
896 motor_off_timer[drive].expires = jiffies + UDP->spindown - delta;
898 add_timer(motor_off_timer+drive);
902 * cycle through all N_DRIVE floppy drives, for disk change testing.
903 * stopping at current drive. This is done before any long operation, to
904 * be sure to have up to date disk change information.
906 static void scandrives(void)
908 int i, drive, saved_drive;
910 if (DP->select_delay)
911 return;
913 saved_drive = current_drive;
914 for (i=0; i < N_DRIVE; i++){
915 drive = (saved_drive + i + 1) % N_DRIVE;
916 if (UDRS->fd_ref == 0 || UDP->select_delay != 0)
917 continue; /* skip closed drives */
918 set_fdc(drive);
919 if (!(set_dor(fdc, ~3, UNIT(drive) | (0x10 << UNIT(drive))) &
920 (0x10 << UNIT(drive))))
921 /* switch the motor off again, if it was off to
922 * begin with */
923 set_dor(fdc, ~(0x10 << UNIT(drive)), 0);
925 set_fdc(saved_drive);
928 static void empty(void)
932 static struct tq_struct floppy_tq =
933 { 0, 0, 0, 0 };
935 static void schedule_bh( void (*handler)(void*) )
937 floppy_tq.routine = (void *)(void *) handler;
938 queue_task(&floppy_tq, &tq_immediate);
939 mark_bh(IMMEDIATE_BH);
942 static struct timer_list fd_timer ={ NULL, NULL, 0, 0, 0 };
944 static void cancel_activity(void)
946 CLEAR_INTR;
947 floppy_tq.routine = (void *)(void *) empty;
948 del_timer(&fd_timer);
951 /* this function makes sure that the disk stays in the drive during the
952 * transfer */
953 static void fd_watchdog(void)
955 #ifdef DCL_DEBUG
956 if (DP->flags & FD_DEBUG){
957 DPRINT("calling disk change from watchdog\n");
959 #endif
961 if (disk_change(current_drive)){
962 DPRINT("disk removed during i/o\n");
963 cancel_activity();
964 cont->done(0);
965 reset_fdc();
966 } else {
967 del_timer(&fd_timer);
968 fd_timer.function = (timeout_fn) fd_watchdog;
969 fd_timer.expires = jiffies + HZ / 10;
970 add_timer(&fd_timer);
974 static void main_command_interrupt(void)
976 del_timer(&fd_timer);
977 cont->interrupt();
980 /* waits for a delay (spinup or select) to pass */
981 static int wait_for_completion(int delay, timeout_fn function)
983 if (FDCS->reset){
984 reset_fdc(); /* do the reset during sleep to win time
985 * if we don't need to sleep, it's a good
986 * occasion anyways */
987 return 1;
990 if ((signed) (jiffies - delay) < 0){
991 del_timer(&fd_timer);
992 fd_timer.function = function;
993 fd_timer.expires = delay;
994 add_timer(&fd_timer);
995 return 1;
997 return 0;
1000 static int hlt_disabled=0;
1001 static void floppy_disable_hlt(void)
1003 unsigned long flags;
1005 INT_OFF;
1006 if (!hlt_disabled){
1007 hlt_disabled=1;
1008 #ifdef HAVE_DISABLE_HLT
1009 disable_hlt();
1010 #endif
1012 INT_ON;
1015 static void floppy_enable_hlt(void)
1017 unsigned long flags;
1019 INT_OFF;
1020 if (hlt_disabled){
1021 hlt_disabled=0;
1022 #ifdef HAVE_DISABLE_HLT
1023 enable_hlt();
1024 #endif
1026 INT_ON;
1030 static void setup_DMA(void)
1032 unsigned long flags;
1034 #ifdef FLOPPY_SANITY_CHECK
1035 if (raw_cmd->length == 0){
1036 int i;
1038 printk("zero dma transfer size:");
1039 for (i=0; i < raw_cmd->cmd_count; i++)
1040 printk("%x,", raw_cmd->cmd[i]);
1041 printk("\n");
1042 cont->done(0);
1043 FDCS->reset = 1;
1044 return;
1046 if (((unsigned long) raw_cmd->kernel_data) % 512){
1047 printk("non aligned address: %p\n", raw_cmd->kernel_data);
1048 cont->done(0);
1049 FDCS->reset=1;
1050 return;
1052 #endif
1053 INT_OFF;
1054 fd_disable_dma();
1055 #ifdef fd_dma_setup
1056 if(fd_dma_setup(raw_cmd->kernel_data, raw_cmd->length,
1057 (raw_cmd->flags & FD_RAW_READ)?
1058 DMA_MODE_READ : DMA_MODE_WRITE,
1059 FDCS->address) < 0) {
1060 INT_ON;
1061 cont->done(0);
1062 FDCS->reset=1;
1063 return;
1065 #else
1066 fd_clear_dma_ff();
1067 fd_cacheflush(raw_cmd->kernel_data, raw_cmd->length);
1068 fd_set_dma_mode((raw_cmd->flags & FD_RAW_READ)?
1069 DMA_MODE_READ : DMA_MODE_WRITE);
1070 fd_set_dma_addr(raw_cmd->kernel_data);
1071 fd_set_dma_count(raw_cmd->length);
1072 virtual_dma_port = FDCS->address;
1073 fd_enable_dma();
1074 #endif
1075 INT_ON;
1076 floppy_disable_hlt();
1079 static void show_floppy(void);
1081 /* waits until the fdc becomes ready */
1082 static int wait_til_ready(void)
1084 int counter, status;
1085 if(FDCS->reset)
1086 return -1;
1087 for (counter = 0; counter < 10000; counter++) {
1088 status = fd_inb(FD_STATUS);
1089 if (status & STATUS_READY)
1090 return status;
1092 if (!initialising) {
1093 DPRINT("Getstatus times out (%x) on fdc %d\n",
1094 status, fdc);
1095 show_floppy();
1097 FDCS->reset = 1;
1098 return -1;
1101 /* sends a command byte to the fdc */
1102 static int output_byte(char byte)
1104 int status;
1106 if ((status = wait_til_ready()) < 0)
1107 return -1;
1108 if ((status & (STATUS_READY|STATUS_DIR|STATUS_DMA)) == STATUS_READY){
1109 fd_outb(byte,FD_DATA);
1110 #ifdef FLOPPY_SANITY_CHECK
1111 output_log[output_log_pos].data = byte;
1112 output_log[output_log_pos].status = status;
1113 output_log[output_log_pos].jiffies = jiffies;
1114 output_log_pos = (output_log_pos + 1) % OLOGSIZE;
1115 #endif
1116 return 0;
1118 FDCS->reset = 1;
1119 if (!initialising) {
1120 DPRINT("Unable to send byte %x to FDC. Fdc=%x Status=%x\n",
1121 byte, fdc, status);
1122 show_floppy();
1124 return -1;
1126 #define LAST_OUT(x) if (output_byte(x)<0){ reset_fdc();return;}
1128 /* gets the response from the fdc */
1129 static int result(void)
1131 int i, status;
1133 for(i=0; i < MAX_REPLIES; i++) {
1134 if ((status = wait_til_ready()) < 0)
1135 break;
1136 status &= STATUS_DIR|STATUS_READY|STATUS_BUSY|STATUS_DMA;
1137 if ((status & ~STATUS_BUSY) == STATUS_READY){
1138 #ifdef FLOPPY_SANITY_CHECK
1139 resultjiffies = jiffies;
1140 resultsize = i;
1141 #endif
1142 return i;
1144 if (status == (STATUS_DIR|STATUS_READY|STATUS_BUSY))
1145 reply_buffer[i] = fd_inb(FD_DATA);
1146 else
1147 break;
1149 if(!initialising) {
1150 DPRINT("get result error. Fdc=%d Last status=%x Read bytes=%d\n",
1151 fdc, status, i);
1152 show_floppy();
1154 FDCS->reset = 1;
1155 return -1;
1158 #define MORE_OUTPUT -2
1159 /* does the fdc need more output? */
1160 static int need_more_output(void)
1162 int status;
1163 if( (status = wait_til_ready()) < 0)
1164 return -1;
1165 if ((status & (STATUS_READY|STATUS_DIR|STATUS_DMA)) == STATUS_READY)
1166 return MORE_OUTPUT;
1167 return result();
1170 /* Set perpendicular mode as required, based on data rate, if supported.
1171 * 82077 Now tested. 1Mbps data rate only possible with 82077-1.
1173 static inline void perpendicular_mode(void)
1175 unsigned char perp_mode;
1177 if (raw_cmd->rate & 0x40){
1178 switch(raw_cmd->rate & 3){
1179 case 0:
1180 perp_mode=2;
1181 break;
1182 case 3:
1183 perp_mode=3;
1184 break;
1185 default:
1186 DPRINT("Invalid data rate for perpendicular mode!\n");
1187 cont->done(0);
1188 FDCS->reset = 1; /* convenient way to return to
1189 * redo without to much hassle (deep
1190 * stack et al. */
1191 return;
1193 } else
1194 perp_mode = 0;
1196 if (FDCS->perp_mode == perp_mode)
1197 return;
1198 if (FDCS->version >= FDC_82077_ORIG) {
1199 output_byte(FD_PERPENDICULAR);
1200 output_byte(perp_mode);
1201 FDCS->perp_mode = perp_mode;
1202 } else if (perp_mode) {
1203 DPRINT("perpendicular mode not supported by this FDC.\n");
1205 } /* perpendicular_mode */
1207 static int fifo_depth = 0xa;
1208 static int no_fifo = 0;
1210 static int fdc_configure(void)
1212 /* Turn on FIFO */
1213 output_byte(FD_CONFIGURE);
1214 if(need_more_output() != MORE_OUTPUT)
1215 return 0;
1216 output_byte(0);
1217 output_byte(0x10 | (no_fifo & 0x20) | (fifo_depth & 0xf));
1218 output_byte(0); /* pre-compensation from track
1219 0 upwards */
1220 return 1;
1223 #define NOMINAL_DTR 500
1225 /* Issue a "SPECIFY" command to set the step rate time, head unload time,
1226 * head load time, and DMA disable flag to values needed by floppy.
1228 * The value "dtr" is the data transfer rate in Kbps. It is needed
1229 * to account for the data rate-based scaling done by the 82072 and 82077
1230 * FDC types. This parameter is ignored for other types of FDCs (i.e.
1231 * 8272a).
1233 * Note that changing the data transfer rate has a (probably deleterious)
1234 * effect on the parameters subject to scaling for 82072/82077 FDCs, so
1235 * fdc_specify is called again after each data transfer rate
1236 * change.
1238 * srt: 1000 to 16000 in microseconds
1239 * hut: 16 to 240 milliseconds
1240 * hlt: 2 to 254 milliseconds
1242 * These values are rounded up to the next highest available delay time.
1244 static void fdc_specify(void)
1246 unsigned char spec1, spec2;
1247 int srt, hlt, hut;
1248 unsigned long dtr = NOMINAL_DTR;
1249 unsigned long scale_dtr = NOMINAL_DTR;
1250 int hlt_max_code = 0x7f;
1251 int hut_max_code = 0xf;
1253 if (FDCS->need_configure && FDCS->version >= FDC_82072A) {
1254 fdc_configure();
1255 FDCS->need_configure = 0;
1256 /*DPRINT("FIFO enabled\n");*/
1259 switch (raw_cmd->rate & 0x03) {
1260 case 3:
1261 dtr = 1000;
1262 break;
1263 case 1:
1264 dtr = 300;
1265 if (FDCS->version >= FDC_82078) {
1266 /* chose the default rate table, not the one
1267 * where 1 = 2 Mbps */
1268 output_byte(FD_DRIVESPEC);
1269 if(need_more_output() == MORE_OUTPUT) {
1270 output_byte(UNIT(current_drive));
1271 output_byte(0xc0);
1274 break;
1275 case 2:
1276 dtr = 250;
1277 break;
1280 if (FDCS->version >= FDC_82072) {
1281 scale_dtr = dtr;
1282 hlt_max_code = 0x00; /* 0==256msec*dtr0/dtr (not linear!) */
1283 hut_max_code = 0x0; /* 0==256msec*dtr0/dtr (not linear!) */
1286 /* Convert step rate from microseconds to milliseconds and 4 bits */
1287 srt = 16 - (DP->srt*scale_dtr/1000 + NOMINAL_DTR - 1)/NOMINAL_DTR;
1288 SUPBOUND(srt, 0xf);
1289 INFBOUND(srt, 0);
1291 hlt = (DP->hlt*scale_dtr/2 + NOMINAL_DTR - 1)/NOMINAL_DTR;
1292 if (hlt < 0x01)
1293 hlt = 0x01;
1294 else if (hlt > 0x7f)
1295 hlt = hlt_max_code;
1297 hut = (DP->hut*scale_dtr/16 + NOMINAL_DTR - 1)/NOMINAL_DTR;
1298 if (hut < 0x1)
1299 hut = 0x1;
1300 else if (hut > 0xf)
1301 hut = hut_max_code;
1303 spec1 = (srt << 4) | hut;
1304 spec2 = (hlt << 1) | (use_virtual_dma & 1);
1306 /* If these parameters did not change, just return with success */
1307 if (FDCS->spec1 != spec1 || FDCS->spec2 != spec2) {
1308 /* Go ahead and set spec1 and spec2 */
1309 output_byte(FD_SPECIFY);
1310 output_byte(FDCS->spec1 = spec1);
1311 output_byte(FDCS->spec2 = spec2);
1313 } /* fdc_specify */
1315 /* Set the FDC's data transfer rate on behalf of the specified drive.
1316 * NOTE: with 82072/82077 FDCs, changing the data rate requires a reissue
1317 * of the specify command (i.e. using the fdc_specify function).
1319 static int fdc_dtr(void)
1321 /* If data rate not already set to desired value, set it. */
1322 if ((raw_cmd->rate & 3) == FDCS->dtr)
1323 return 0;
1325 /* Set dtr */
1326 fd_outb(raw_cmd->rate & 3, FD_DCR);
1328 /* TODO: some FDC/drive combinations (C&T 82C711 with TEAC 1.2MB)
1329 * need a stabilization period of several milliseconds to be
1330 * enforced after data rate changes before R/W operations.
1331 * Pause 5 msec to avoid trouble. (Needs to be 2 jiffies)
1333 FDCS->dtr = raw_cmd->rate & 3;
1334 return(wait_for_completion(jiffies+2*HZ/100,
1335 (timeout_fn) floppy_ready));
1336 } /* fdc_dtr */
1338 static void tell_sector(void)
1340 printk(": track %d, head %d, sector %d, size %d",
1341 R_TRACK, R_HEAD, R_SECTOR, R_SIZECODE);
1342 } /* tell_sector */
1346 * OK, this error interpreting routine is called after a
1347 * DMA read/write has succeeded
1348 * or failed, so we check the results, and copy any buffers.
1349 * hhb: Added better error reporting.
1350 * ak: Made this into a separate routine.
1352 static int interpret_errors(void)
1354 char bad;
1356 if (inr!=7) {
1357 DPRINT("-- FDC reply error");
1358 FDCS->reset = 1;
1359 return 1;
1362 /* check IC to find cause of interrupt */
1363 switch (ST0 & ST0_INTR) {
1364 case 0x40: /* error occurred during command execution */
1365 if (ST1 & ST1_EOC)
1366 return 0; /* occurs with pseudo-DMA */
1367 bad = 1;
1368 if (ST1 & ST1_WP) {
1369 DPRINT("Drive is write protected\n");
1370 CLEARF(FD_DISK_WRITABLE);
1371 cont->done(0);
1372 bad = 2;
1373 } else if (ST1 & ST1_ND) {
1374 SETF(FD_NEED_TWADDLE);
1375 } else if (ST1 & ST1_OR) {
1376 if (DP->flags & FTD_MSG)
1377 DPRINT("Over/Underrun - retrying\n");
1378 bad = 0;
1379 }else if (*errors >= DP->max_errors.reporting){
1380 DPRINT("");
1381 if (ST0 & ST0_ECE) {
1382 printk("Recalibrate failed!");
1383 } else if (ST2 & ST2_CRC) {
1384 printk("data CRC error");
1385 tell_sector();
1386 } else if (ST1 & ST1_CRC) {
1387 printk("CRC error");
1388 tell_sector();
1389 } else if ((ST1 & (ST1_MAM|ST1_ND)) || (ST2 & ST2_MAM)) {
1390 if (!probing) {
1391 printk("sector not found");
1392 tell_sector();
1393 } else
1394 printk("probe failed...");
1395 } else if (ST2 & ST2_WC) { /* seek error */
1396 printk("wrong cylinder");
1397 } else if (ST2 & ST2_BC) { /* cylinder marked as bad */
1398 printk("bad cylinder");
1399 } else {
1400 printk("unknown error. ST[0..2] are: 0x%x 0x%x 0x%x", ST0, ST1, ST2);
1401 tell_sector();
1403 printk("\n");
1406 if (ST2 & ST2_WC || ST2 & ST2_BC)
1407 /* wrong cylinder => recal */
1408 DRS->track = NEED_2_RECAL;
1409 return bad;
1410 case 0x80: /* invalid command given */
1411 DPRINT("Invalid FDC command given!\n");
1412 cont->done(0);
1413 return 2;
1414 case 0xc0:
1415 DPRINT("Abnormal termination caused by polling\n");
1416 cont->error();
1417 return 2;
1418 default: /* (0) Normal command termination */
1419 return 0;
1424 * This routine is called when everything should be correctly set up
1425 * for the transfer (i.e. floppy motor is on, the correct floppy is
1426 * selected, and the head is sitting on the right track).
1428 static void setup_rw_floppy(void)
1430 int i,ready_date,r, flags,dflags;
1431 timeout_fn function;
1433 flags = raw_cmd->flags;
1434 if (flags & (FD_RAW_READ | FD_RAW_WRITE))
1435 flags |= FD_RAW_INTR;
1437 if ((flags & FD_RAW_SPIN) && !(flags & FD_RAW_NO_MOTOR)){
1438 ready_date = DRS->spinup_date + DP->spinup;
1439 /* If spinup will take a long time, rerun scandrives
1440 * again just before spinup completion. Beware that
1441 * after scandrives, we must again wait for selection.
1443 if ((signed) (ready_date - jiffies) > DP->select_delay){
1444 ready_date -= DP->select_delay;
1445 function = (timeout_fn) floppy_start;
1446 } else
1447 function = (timeout_fn) setup_rw_floppy;
1449 /* wait until the floppy is spinning fast enough */
1450 if (wait_for_completion(ready_date,function))
1451 return;
1453 dflags = DRS->flags;
1455 if ((flags & FD_RAW_READ) || (flags & FD_RAW_WRITE))
1456 setup_DMA();
1458 if (flags & FD_RAW_INTR)
1459 SET_INTR(main_command_interrupt);
1461 r=0;
1462 for (i=0; i< raw_cmd->cmd_count; i++)
1463 r|=output_byte(raw_cmd->cmd[i]);
1465 #ifdef DEBUGT
1466 debugt("rw_command: ");
1467 #endif
1468 if (r){
1469 cont->error();
1470 reset_fdc();
1471 return;
1474 if (!(flags & FD_RAW_INTR)){
1475 inr = result();
1476 cont->interrupt();
1477 } else if (flags & FD_RAW_NEED_DISK)
1478 fd_watchdog();
1481 static int blind_seek;
1484 * This is the routine called after every seek (or recalibrate) interrupt
1485 * from the floppy controller.
1487 static void seek_interrupt(void)
1489 #ifdef DEBUGT
1490 debugt("seek interrupt:");
1491 #endif
1492 if (inr != 2 || (ST0 & 0xF8) != 0x20) {
1493 DPRINT("seek failed\n");
1494 DRS->track = NEED_2_RECAL;
1495 cont->error();
1496 cont->redo();
1497 return;
1499 if (DRS->track >= 0 && DRS->track != ST1 && !blind_seek){
1500 #ifdef DCL_DEBUG
1501 if (DP->flags & FD_DEBUG){
1502 DPRINT("clearing NEWCHANGE flag because of effective seek\n");
1503 DPRINT("jiffies=%ld\n", jiffies);
1505 #endif
1506 CLEARF(FD_DISK_NEWCHANGE); /* effective seek */
1507 DRS->select_date = jiffies;
1509 DRS->track = ST1;
1510 floppy_ready();
1513 static void check_wp(void)
1515 if (TESTF(FD_VERIFY)) {
1516 /* check write protection */
1517 output_byte(FD_GETSTATUS);
1518 output_byte(UNIT(current_drive));
1519 if (result() != 1){
1520 FDCS->reset = 1;
1521 return;
1523 CLEARF(FD_VERIFY);
1524 CLEARF(FD_NEED_TWADDLE);
1525 #ifdef DCL_DEBUG
1526 if (DP->flags & FD_DEBUG){
1527 DPRINT("checking whether disk is write protected\n");
1528 DPRINT("wp=%x\n",ST3 & 0x40);
1530 #endif
1531 if (!(ST3 & 0x40))
1532 SETF(FD_DISK_WRITABLE);
1533 else
1534 CLEARF(FD_DISK_WRITABLE);
1538 static void seek_floppy(void)
1540 int track;
1542 blind_seek=0;
1544 #ifdef DCL_DEBUG
1545 if (DP->flags & FD_DEBUG){
1546 DPRINT("calling disk change from seek\n");
1548 #endif
1550 if (!TESTF(FD_DISK_NEWCHANGE) &&
1551 disk_change(current_drive) &&
1552 (raw_cmd->flags & FD_RAW_NEED_DISK)){
1553 /* the media changed flag should be cleared after the seek.
1554 * If it isn't, this means that there is really no disk in
1555 * the drive.
1557 SETF(FD_DISK_CHANGED);
1558 cont->done(0);
1559 cont->redo();
1560 return;
1562 if (DRS->track <= NEED_1_RECAL){
1563 recalibrate_floppy();
1564 return;
1565 } else if (TESTF(FD_DISK_NEWCHANGE) &&
1566 (raw_cmd->flags & FD_RAW_NEED_DISK) &&
1567 (DRS->track <= NO_TRACK || DRS->track == raw_cmd->track)) {
1568 /* we seek to clear the media-changed condition. Does anybody
1569 * know a more elegant way, which works on all drives? */
1570 if (raw_cmd->track)
1571 track = raw_cmd->track - 1;
1572 else {
1573 if (DP->flags & FD_SILENT_DCL_CLEAR){
1574 set_dor(fdc, ~(0x10 << UNIT(current_drive)), 0);
1575 blind_seek = 1;
1576 raw_cmd->flags |= FD_RAW_NEED_SEEK;
1578 track = 1;
1580 } else {
1581 check_wp();
1582 if (raw_cmd->track != DRS->track &&
1583 (raw_cmd->flags & FD_RAW_NEED_SEEK))
1584 track = raw_cmd->track;
1585 else {
1586 setup_rw_floppy();
1587 return;
1591 SET_INTR(seek_interrupt);
1592 output_byte(FD_SEEK);
1593 output_byte(UNIT(current_drive));
1594 LAST_OUT(track);
1595 #ifdef DEBUGT
1596 debugt("seek command:");
1597 #endif
1600 static void recal_interrupt(void)
1602 #ifdef DEBUGT
1603 debugt("recal interrupt:");
1604 #endif
1605 if (inr !=2)
1606 FDCS->reset = 1;
1607 else if (ST0 & ST0_ECE) {
1608 switch(DRS->track){
1609 case NEED_1_RECAL:
1610 #ifdef DEBUGT
1611 debugt("recal interrupt need 1 recal:");
1612 #endif
1613 /* after a second recalibrate, we still haven't
1614 * reached track 0. Probably no drive. Raise an
1615 * error, as failing immediately might upset
1616 * computers possessed by the Devil :-) */
1617 cont->error();
1618 cont->redo();
1619 return;
1620 case NEED_2_RECAL:
1621 #ifdef DEBUGT
1622 debugt("recal interrupt need 2 recal:");
1623 #endif
1624 /* If we already did a recalibrate,
1625 * and we are not at track 0, this
1626 * means we have moved. (The only way
1627 * not to move at recalibration is to
1628 * be already at track 0.) Clear the
1629 * new change flag */
1630 #ifdef DCL_DEBUG
1631 if (DP->flags & FD_DEBUG){
1632 DPRINT("clearing NEWCHANGE flag because of second recalibrate\n");
1634 #endif
1636 CLEARF(FD_DISK_NEWCHANGE);
1637 DRS->select_date = jiffies;
1638 /* fall through */
1639 default:
1640 #ifdef DEBUGT
1641 debugt("recal interrupt default:");
1642 #endif
1643 /* Recalibrate moves the head by at
1644 * most 80 steps. If after one
1645 * recalibrate we don't have reached
1646 * track 0, this might mean that we
1647 * started beyond track 80. Try
1648 * again. */
1649 DRS->track = NEED_1_RECAL;
1650 break;
1652 } else
1653 DRS->track = ST1;
1654 floppy_ready();
1657 static void print_result(char *message, int inr)
1659 int i;
1661 DPRINT("%s ", message);
1662 if (inr >= 0)
1663 for (i=0; i<inr; i++)
1664 printk("repl[%d]=%x ", i, reply_buffer[i]);
1665 printk("\n");
1668 /* interrupt handler. Note that this can be called externally on the Sparc */
1669 void floppy_interrupt(int irq, void *dev_id, struct pt_regs * regs)
1671 void (*handler)(void) = DEVICE_INTR;
1672 int do_print;
1674 lasthandler = handler;
1675 interruptjiffies = jiffies;
1677 fd_disable_dma();
1678 floppy_enable_hlt();
1679 CLEAR_INTR;
1680 if (fdc >= N_FDC || FDCS->address == -1){
1681 /* we don't even know which FDC is the culprit */
1682 printk("DOR0=%x\n", fdc_state[0].dor);
1683 printk("floppy interrupt on bizarre fdc %d\n",fdc);
1684 printk("handler=%p\n", handler);
1685 is_alive("bizarre fdc");
1686 return;
1689 FDCS->reset = 0;
1690 /* We have to clear the reset flag here, because apparently on boxes
1691 * with level triggered interrupts (PS/2, Sparc, ...), it is needed to
1692 * emit SENSEI's to clear the interrupt line. And FDCS->reset blocks the
1693 * emission of the SENSEI's.
1694 * It is OK to emit floppy commands because we are in an interrupt
1695 * handler here, and thus we have to fear no interference of other
1696 * activity.
1699 do_print = !handler && print_unex && !initialising;
1701 inr = result();
1702 if(do_print)
1703 print_result("unexpected interrupt", inr);
1704 if (inr == 0){
1705 do {
1706 output_byte(FD_SENSEI);
1707 inr = result();
1708 if(do_print)
1709 print_result("sensei", inr);
1710 } while ((ST0 & 0x83) != UNIT(current_drive) && inr == 2);
1712 if (handler) {
1713 int cpu = smp_processor_id();
1714 if(softirq_trylock(cpu)) {
1715 /* got the lock, call the handler immediately */
1716 handler();
1717 softirq_endlock(cpu);
1718 } else
1719 /* we interrupted a bottom half. Defer handler */
1720 schedule_bh( (void *)(void *) handler);
1721 } else
1722 FDCS->reset = 1;
1723 is_alive("normal interrupt end");
1726 static void recalibrate_floppy(void)
1728 #ifdef DEBUGT
1729 debugt("recalibrate floppy:");
1730 #endif
1731 SET_INTR(recal_interrupt);
1732 output_byte(FD_RECALIBRATE);
1733 LAST_OUT(UNIT(current_drive));
1737 * Must do 4 FD_SENSEIs after reset because of ``drive polling''.
1739 static void reset_interrupt(void)
1741 #ifdef DEBUGT
1742 debugt("reset interrupt:");
1743 #endif
1744 result(); /* get the status ready for set_fdc */
1745 if (FDCS->reset) {
1746 printk("reset set in interrupt, calling %p\n", cont->error);
1747 cont->error(); /* a reset just after a reset. BAD! */
1749 cont->redo();
1753 * reset is done by pulling bit 2 of DOR low for a while (old FDCs),
1754 * or by setting the self clearing bit 7 of STATUS (newer FDCs)
1756 static void reset_fdc(void)
1758 SET_INTR(reset_interrupt);
1759 FDCS->reset = 0;
1760 reset_fdc_info(0);
1762 /* Pseudo-DMA may intercept 'reset finished' interrupt. */
1763 /* Irrelevant for systems with true DMA (i386). */
1764 fd_disable_dma();
1766 if (FDCS->version >= FDC_82072A)
1767 fd_outb(0x80 | (FDCS->dtr &3), FD_STATUS);
1768 else {
1769 fd_outb(FDCS->dor & ~0x04, FD_DOR);
1770 udelay(FD_RESET_DELAY);
1771 fd_outb(FDCS->dor, FD_DOR);
1775 static void show_floppy(void)
1777 int i;
1779 printk("\n");
1780 printk("floppy driver state\n");
1781 printk("-------------------\n");
1782 printk("now=%ld last interrupt=%d last called handler=%p\n",
1783 jiffies, interruptjiffies, lasthandler);
1786 #ifdef FLOPPY_SANITY_CHECK
1787 printk("timeout_message=%s\n", timeout_message);
1788 printk("last output bytes:\n");
1789 for (i=0; i < OLOGSIZE; i++)
1790 printk("%2x %2x %ld\n",
1791 output_log[(i+output_log_pos) % OLOGSIZE].data,
1792 output_log[(i+output_log_pos) % OLOGSIZE].status,
1793 output_log[(i+output_log_pos) % OLOGSIZE].jiffies);
1794 printk("last result at %d\n", resultjiffies);
1795 printk("last redo_fd_request at %d\n", lastredo);
1796 for (i=0; i<resultsize; i++){
1797 printk("%2x ", reply_buffer[i]);
1799 printk("\n");
1800 #endif
1802 printk("status=%x\n", fd_inb(FD_STATUS));
1803 printk("fdc_busy=%d\n", fdc_busy);
1804 if (DEVICE_INTR)
1805 printk("DEVICE_INTR=%p\n", DEVICE_INTR);
1806 if (floppy_tq.sync)
1807 printk("floppy_tq.routine=%p\n", floppy_tq.routine);
1808 if (fd_timer.prev)
1809 printk("fd_timer.function=%p\n", fd_timer.function);
1810 if (fd_timeout.prev){
1811 printk("timer_table=%p\n",fd_timeout.function);
1812 printk("expires=%ld\n",fd_timeout.expires-jiffies);
1813 printk("now=%ld\n",jiffies);
1815 printk("cont=%p\n", cont);
1816 printk("CURRENT=%p\n", CURRENT);
1817 printk("command_status=%d\n", command_status);
1818 printk("\n");
1821 static void floppy_shutdown(void)
1823 if (!initialising)
1824 show_floppy();
1825 cancel_activity();
1826 sti();
1828 floppy_enable_hlt();
1829 fd_disable_dma();
1830 /* avoid dma going to a random drive after shutdown */
1832 if (!initialising)
1833 DPRINT("floppy timeout called\n");
1834 FDCS->reset = 1;
1835 if (cont){
1836 cont->done(0);
1837 cont->redo(); /* this will recall reset when needed */
1838 } else {
1839 printk("no cont in shutdown!\n");
1840 process_fd_request();
1842 is_alive("floppy shutdown");
1844 /*typedef void (*timeout_fn)(unsigned long);*/
1846 /* start motor, check media-changed condition and write protection */
1847 static int start_motor(void (*function)(void) )
1849 int mask, data;
1851 mask = 0xfc;
1852 data = UNIT(current_drive);
1853 if (!(raw_cmd->flags & FD_RAW_NO_MOTOR)){
1854 if (!(FDCS->dor & (0x10 << UNIT(current_drive)))){
1855 set_debugt();
1856 /* no read since this drive is running */
1857 DRS->first_read_date = 0;
1858 /* note motor start time if motor is not yet running */
1859 DRS->spinup_date = jiffies;
1860 data |= (0x10 << UNIT(current_drive));
1862 } else
1863 if (FDCS->dor & (0x10 << UNIT(current_drive)))
1864 mask &= ~(0x10 << UNIT(current_drive));
1866 /* starts motor and selects floppy */
1867 del_timer(motor_off_timer + current_drive);
1868 set_dor(fdc, mask, data);
1870 /* wait_for_completion also schedules reset if needed. */
1871 return(wait_for_completion(DRS->select_date+DP->select_delay,
1872 (timeout_fn) function));
1875 static void floppy_ready(void)
1877 CHECK_RESET;
1878 if (start_motor(floppy_ready)) return;
1879 if (fdc_dtr()) return;
1881 #ifdef DCL_DEBUG
1882 if (DP->flags & FD_DEBUG){
1883 DPRINT("calling disk change from floppy_ready\n");
1885 #endif
1886 if (!(raw_cmd->flags & FD_RAW_NO_MOTOR) &&
1887 disk_change(current_drive) &&
1888 !DP->select_delay)
1889 twaddle(); /* this clears the dcl on certain drive/controller
1890 * combinations */
1892 #ifdef fd_chose_dma_mode
1893 if ((raw_cmd->flags & FD_RAW_READ) ||
1894 (raw_cmd->flags & FD_RAW_WRITE))
1895 fd_chose_dma_mode(raw_cmd->kernel_data,
1896 raw_cmd->length);
1897 #endif
1899 if (raw_cmd->flags & (FD_RAW_NEED_SEEK | FD_RAW_NEED_DISK)){
1900 perpendicular_mode();
1901 fdc_specify(); /* must be done here because of hut, hlt ... */
1902 seek_floppy();
1903 } else {
1904 if ((raw_cmd->flags & FD_RAW_READ) ||
1905 (raw_cmd->flags & FD_RAW_WRITE))
1906 fdc_specify();
1907 setup_rw_floppy();
1911 static void floppy_start(void)
1913 reschedule_timeout(CURRENTD, "floppy start", 0);
1915 scandrives();
1916 #ifdef DCL_DEBUG
1917 if (DP->flags & FD_DEBUG){
1918 DPRINT("setting NEWCHANGE in floppy_start\n");
1920 #endif
1921 SETF(FD_DISK_NEWCHANGE);
1922 floppy_ready();
1926 * ========================================================================
1927 * here ends the bottom half. Exported routines are:
1928 * floppy_start, floppy_off, floppy_ready, lock_fdc, unlock_fdc, set_fdc,
1929 * start_motor, reset_fdc, reset_fdc_info, interpret_errors.
1930 * Initialization also uses output_byte, result, set_dor, floppy_interrupt
1931 * and set_dor.
1932 * ========================================================================
1935 * General purpose continuations.
1936 * ==============================
1939 static void do_wakeup(void)
1941 reschedule_timeout(MAXTIMEOUT, "do wakeup", 0);
1942 cont = 0;
1943 command_status += 2;
1944 wake_up(&command_done);
1947 static struct cont_t wakeup_cont={
1948 empty,
1949 do_wakeup,
1950 empty,
1951 (done_f)empty
1955 static struct cont_t intr_cont={
1956 empty,
1957 process_fd_request,
1958 empty,
1959 (done_f) empty
1962 static int wait_til_done(void (*handler)(void), int interruptible)
1964 int ret;
1965 unsigned long flags;
1967 schedule_bh((void *)(void *)handler);
1968 INT_OFF;
1969 while(command_status < 2 && NO_SIGNAL){
1970 is_alive("wait_til_done");
1971 if (interruptible)
1972 interruptible_sleep_on(&command_done);
1973 else
1974 sleep_on(&command_done);
1976 if (command_status < 2){
1977 cancel_activity();
1978 cont = &intr_cont;
1979 reset_fdc();
1980 INT_ON;
1981 return -EINTR;
1983 INT_ON;
1985 if (FDCS->reset)
1986 command_status = FD_COMMAND_ERROR;
1987 if (command_status == FD_COMMAND_OKAY)
1988 ret=0;
1989 else
1990 ret=-EIO;
1991 command_status = FD_COMMAND_NONE;
1992 return ret;
1995 static void generic_done(int result)
1997 command_status = result;
1998 cont = &wakeup_cont;
2001 static void generic_success(void)
2003 cont->done(1);
2006 static void generic_failure(void)
2008 cont->done(0);
2011 static void success_and_wakeup(void)
2013 generic_success();
2014 cont->redo();
2019 * formatting and rw support.
2020 * ==========================
2023 static int next_valid_format(void)
2025 int probed_format;
2027 probed_format = DRS->probed_format;
2028 while(1){
2029 if (probed_format >= 8 ||
2030 !DP->autodetect[probed_format]){
2031 DRS->probed_format = 0;
2032 return 1;
2034 if (floppy_type[DP->autodetect[probed_format]].sect){
2035 DRS->probed_format = probed_format;
2036 return 0;
2038 probed_format++;
2042 static void bad_flp_intr(void)
2044 if (probing){
2045 DRS->probed_format++;
2046 if (!next_valid_format())
2047 return;
2049 (*errors)++;
2050 INFBOUND(DRWE->badness, *errors);
2051 if (*errors > DP->max_errors.abort)
2052 cont->done(0);
2053 if (*errors > DP->max_errors.reset)
2054 FDCS->reset = 1;
2055 else if (*errors > DP->max_errors.recal)
2056 DRS->track = NEED_2_RECAL;
2059 static void set_floppy(kdev_t device)
2061 if (TYPE(device))
2062 _floppy = TYPE(device) + floppy_type;
2063 else
2064 _floppy = current_type[ DRIVE(device) ];
2068 * formatting support.
2069 * ===================
2071 static void format_interrupt(void)
2073 switch (interpret_errors()){
2074 case 1:
2075 cont->error();
2076 case 2:
2077 break;
2078 case 0:
2079 cont->done(1);
2081 cont->redo();
2084 #define CODE2SIZE (ssize = ((1 << SIZECODE) + 3) >> 2)
2085 #define FM_MODE(x,y) ((y) & ~(((x)->rate & 0x80) >>1))
2086 #define CT(x) ((x) | 0x40)
2087 static void setup_format_params(int track)
2089 struct fparm {
2090 unsigned char track,head,sect,size;
2091 } *here = (struct fparm *)floppy_track_buffer;
2092 int il,n;
2093 int count,head_shift,track_shift;
2095 raw_cmd = &default_raw_cmd;
2096 raw_cmd->track = track;
2098 raw_cmd->flags = FD_RAW_WRITE | FD_RAW_INTR | FD_RAW_SPIN |
2099 FD_RAW_NEED_DISK | FD_RAW_NEED_SEEK;
2100 raw_cmd->rate = _floppy->rate & 0x43;
2101 raw_cmd->cmd_count = NR_F;
2102 COMMAND = FM_MODE(_floppy,FD_FORMAT);
2103 DR_SELECT = UNIT(current_drive) + PH_HEAD(_floppy,format_req.head);
2104 F_SIZECODE = FD_SIZECODE(_floppy);
2105 F_SECT_PER_TRACK = _floppy->sect << 2 >> F_SIZECODE;
2106 F_GAP = _floppy->fmt_gap;
2107 F_FILL = FD_FILL_BYTE;
2109 raw_cmd->kernel_data = floppy_track_buffer;
2110 raw_cmd->length = 4 * F_SECT_PER_TRACK;
2112 /* allow for about 30ms for data transport per track */
2113 head_shift = (F_SECT_PER_TRACK + 5) / 6;
2115 /* a ``cylinder'' is two tracks plus a little stepping time */
2116 track_shift = 2 * head_shift + 3;
2118 /* position of logical sector 1 on this track */
2119 n = (track_shift * format_req.track + head_shift * format_req.head)
2120 % F_SECT_PER_TRACK;
2122 /* determine interleave */
2123 il = 1;
2124 if (_floppy->fmt_gap < 0x22)
2125 il++;
2127 /* initialize field */
2128 for (count = 0; count < F_SECT_PER_TRACK; ++count) {
2129 here[count].track = format_req.track;
2130 here[count].head = format_req.head;
2131 here[count].sect = 0;
2132 here[count].size = F_SIZECODE;
2134 /* place logical sectors */
2135 for (count = 1; count <= F_SECT_PER_TRACK; ++count) {
2136 here[n].sect = count;
2137 n = (n+il) % F_SECT_PER_TRACK;
2138 if (here[n].sect) { /* sector busy, find next free sector */
2139 ++n;
2140 if (n>= F_SECT_PER_TRACK) {
2141 n-=F_SECT_PER_TRACK;
2142 while (here[n].sect) ++n;
2148 static void redo_format(void)
2150 buffer_track = -1;
2151 setup_format_params(format_req.track << STRETCH(_floppy));
2152 floppy_start();
2153 #ifdef DEBUGT
2154 debugt("queue format request");
2155 #endif
2158 static struct cont_t format_cont={
2159 format_interrupt,
2160 redo_format,
2161 bad_flp_intr,
2162 generic_done };
2164 static int do_format(kdev_t device, struct format_descr *tmp_format_req)
2166 int ret;
2167 int drive=DRIVE(device);
2169 LOCK_FDC(drive,1);
2170 set_floppy(device);
2171 if (!_floppy ||
2172 _floppy->track > DP->tracks ||
2173 tmp_format_req->track >= _floppy->track ||
2174 tmp_format_req->head >= _floppy->head ||
2175 (_floppy->sect << 2) % (1 << FD_SIZECODE(_floppy)) ||
2176 !_floppy->fmt_gap) {
2177 process_fd_request();
2178 return -EINVAL;
2180 format_req = *tmp_format_req;
2181 format_errors = 0;
2182 cont = &format_cont;
2183 errors = &format_errors;
2184 IWAIT(redo_format);
2185 process_fd_request();
2186 return ret;
2190 * Buffer read/write and support
2191 * =============================
2194 /* new request_done. Can handle physical sectors which are smaller than a
2195 * logical buffer */
2196 static void request_done(int uptodate)
2198 int block;
2200 probing = 0;
2201 reschedule_timeout(MAXTIMEOUT, "request done %d", uptodate);
2203 if (!CURRENT){
2204 DPRINT("request list destroyed in floppy request done\n");
2205 return;
2208 if (uptodate){
2209 /* maintain values for invalidation on geometry
2210 * change */
2211 block = current_count_sectors + CURRENT->sector;
2212 INFBOUND(DRS->maxblock, block);
2213 if (block > _floppy->sect)
2214 DRS->maxtrack = 1;
2216 /* unlock chained buffers */
2217 while (current_count_sectors && CURRENT &&
2218 current_count_sectors >= CURRENT->current_nr_sectors){
2219 current_count_sectors -= CURRENT->current_nr_sectors;
2220 CURRENT->nr_sectors -= CURRENT->current_nr_sectors;
2221 CURRENT->sector += CURRENT->current_nr_sectors;
2222 end_request(1);
2224 if (current_count_sectors && CURRENT){
2225 /* "unlock" last subsector */
2226 CURRENT->buffer += current_count_sectors <<9;
2227 CURRENT->current_nr_sectors -= current_count_sectors;
2228 CURRENT->nr_sectors -= current_count_sectors;
2229 CURRENT->sector += current_count_sectors;
2230 return;
2233 if (current_count_sectors && !CURRENT)
2234 DPRINT("request list destroyed in floppy request done\n");
2236 } else {
2237 if (CURRENT->cmd == WRITE) {
2238 /* record write error information */
2239 DRWE->write_errors++;
2240 if (DRWE->write_errors == 1) {
2241 DRWE->first_error_sector = CURRENT->sector;
2242 DRWE->first_error_generation = DRS->generation;
2244 DRWE->last_error_sector = CURRENT->sector;
2245 DRWE->last_error_generation = DRS->generation;
2247 end_request(0);
2251 /* Interrupt handler evaluating the result of the r/w operation */
2252 static void rw_interrupt(void)
2254 int nr_sectors, ssize, eoc;
2256 if (!DRS->first_read_date)
2257 DRS->first_read_date = jiffies;
2259 nr_sectors = 0;
2260 CODE2SIZE;
2262 if(ST1 & ST1_EOC)
2263 eoc = 1;
2264 else
2265 eoc = 0;
2266 nr_sectors = ((R_TRACK-TRACK)*_floppy->head+R_HEAD-HEAD) *
2267 _floppy->sect + ((R_SECTOR-SECTOR+eoc) << SIZECODE >> 2) -
2268 (sector_t % _floppy->sect) % ssize;
2270 #ifdef FLOPPY_SANITY_CHECK
2271 if (nr_sectors > current_count_sectors + ssize -
2272 (current_count_sectors + sector_t) % ssize +
2273 sector_t % ssize){
2274 DPRINT("long rw: %x instead of %lx\n",
2275 nr_sectors, current_count_sectors);
2276 printk("rs=%d s=%d\n", R_SECTOR, SECTOR);
2277 printk("rh=%d h=%d\n", R_HEAD, HEAD);
2278 printk("rt=%d t=%d\n", R_TRACK, TRACK);
2279 printk("spt=%d st=%d ss=%d\n", SECT_PER_TRACK,
2280 sector_t, ssize);
2282 #endif
2283 INFBOUND(nr_sectors,0);
2284 SUPBOUND(current_count_sectors, nr_sectors);
2286 switch (interpret_errors()){
2287 case 2:
2288 cont->redo();
2289 return;
2290 case 1:
2291 if (!current_count_sectors){
2292 cont->error();
2293 cont->redo();
2294 return;
2296 break;
2297 case 0:
2298 if (!current_count_sectors){
2299 cont->redo();
2300 return;
2302 current_type[current_drive] = _floppy;
2303 floppy_sizes[TOMINOR(current_drive) ]=
2304 (_floppy->size+1)>>1;
2305 break;
2308 if (probing) {
2309 if (DP->flags & FTD_MSG)
2310 DPRINT("Auto-detected floppy type %s in fd%d\n",
2311 _floppy->name,current_drive);
2312 current_type[current_drive] = _floppy;
2313 floppy_sizes[TOMINOR(current_drive)] = (_floppy->size+1) >> 1;
2314 probing = 0;
2317 if (CT(COMMAND) != FD_READ ||
2318 raw_cmd->kernel_data == CURRENT->buffer){
2319 /* transfer directly from buffer */
2320 cont->done(1);
2321 } else if (CT(COMMAND) == FD_READ){
2322 buffer_track = raw_cmd->track;
2323 buffer_drive = current_drive;
2324 INFBOUND(buffer_max, nr_sectors + sector_t);
2326 cont->redo();
2329 /* Compute maximal contiguous buffer size. */
2330 static int buffer_chain_size(void)
2332 struct buffer_head *bh;
2333 int size;
2334 char *base;
2336 base = CURRENT->buffer;
2337 size = CURRENT->current_nr_sectors << 9;
2338 bh = CURRENT->bh;
2340 if (bh){
2341 bh = bh->b_reqnext;
2342 while (bh && bh->b_data == base + size){
2343 size += bh->b_size;
2344 bh = bh->b_reqnext;
2347 return size >> 9;
2350 /* Compute the maximal transfer size */
2351 static int transfer_size(int ssize, int max_sector, int max_size)
2353 SUPBOUND(max_sector, sector_t + max_size);
2355 /* alignment */
2356 max_sector -= (max_sector % _floppy->sect) % ssize;
2358 /* transfer size, beginning not aligned */
2359 current_count_sectors = max_sector - sector_t ;
2361 return max_sector;
2365 * Move data from/to the track buffer to/from the buffer cache.
2367 static void copy_buffer(int ssize, int max_sector, int max_sector_2)
2369 int remaining; /* number of transferred 512-byte sectors */
2370 struct buffer_head *bh;
2371 char *buffer, *dma_buffer;
2372 int size;
2374 max_sector = transfer_size(ssize,
2375 minimum(max_sector, max_sector_2),
2376 CURRENT->nr_sectors);
2378 if (current_count_sectors <= 0 && CT(COMMAND) == FD_WRITE &&
2379 buffer_max > sector_t + CURRENT->nr_sectors)
2380 current_count_sectors = minimum(buffer_max - sector_t,
2381 CURRENT->nr_sectors);
2383 remaining = current_count_sectors << 9;
2384 #ifdef FLOPPY_SANITY_CHECK
2385 if ((remaining >> 9) > CURRENT->nr_sectors &&
2386 CT(COMMAND) == FD_WRITE){
2387 DPRINT("in copy buffer\n");
2388 printk("current_count_sectors=%ld\n", current_count_sectors);
2389 printk("remaining=%d\n", remaining >> 9);
2390 printk("CURRENT->nr_sectors=%ld\n",CURRENT->nr_sectors);
2391 printk("CURRENT->current_nr_sectors=%ld\n",
2392 CURRENT->current_nr_sectors);
2393 printk("max_sector=%d\n", max_sector);
2394 printk("ssize=%d\n", ssize);
2396 #endif
2398 buffer_max = maximum(max_sector, buffer_max);
2400 dma_buffer = floppy_track_buffer + ((sector_t - buffer_min) << 9);
2402 bh = CURRENT->bh;
2403 size = CURRENT->current_nr_sectors << 9;
2404 buffer = CURRENT->buffer;
2406 while (remaining > 0){
2407 SUPBOUND(size, remaining);
2408 #ifdef FLOPPY_SANITY_CHECK
2409 if (dma_buffer + size >
2410 floppy_track_buffer + (max_buffer_sectors << 10) ||
2411 dma_buffer < floppy_track_buffer){
2412 DPRINT("buffer overrun in copy buffer %d\n",
2413 (int) ((floppy_track_buffer - dma_buffer) >>9));
2414 printk("sector_t=%d buffer_min=%d\n",
2415 sector_t, buffer_min);
2416 printk("current_count_sectors=%ld\n",
2417 current_count_sectors);
2418 if (CT(COMMAND) == FD_READ)
2419 printk("read\n");
2420 if (CT(COMMAND) == FD_READ)
2421 printk("write\n");
2422 break;
2424 if (((unsigned long)buffer) % 512)
2425 DPRINT("%p buffer not aligned\n", buffer);
2426 #endif
2427 if (CT(COMMAND) == FD_READ)
2428 memcpy(buffer, dma_buffer, size);
2429 else
2430 memcpy(dma_buffer, buffer, size);
2431 remaining -= size;
2432 if (!remaining)
2433 break;
2435 dma_buffer += size;
2436 bh = bh->b_reqnext;
2437 #ifdef FLOPPY_SANITY_CHECK
2438 if (!bh){
2439 DPRINT("bh=null in copy buffer after copy\n");
2440 break;
2442 #endif
2443 size = bh->b_size;
2444 buffer = bh->b_data;
2446 #ifdef FLOPPY_SANITY_CHECK
2447 if (remaining){
2448 if (remaining > 0)
2449 max_sector -= remaining >> 9;
2450 DPRINT("weirdness: remaining %d\n", remaining>>9);
2452 #endif
2455 #if 0
2456 static inline int check_dma_crossing(char *start,
2457 unsigned long length, char *message)
2459 if (CROSS_64KB(start, length)) {
2460 printk("DMA xfer crosses 64KB boundary in %s %p-%p\n",
2461 message, start, start+length);
2462 return 1;
2463 } else
2464 return 0;
2466 #endif
2469 * Formulate a read/write request.
2470 * this routine decides where to load the data (directly to buffer, or to
2471 * tmp floppy area), how much data to load (the size of the buffer, the whole
2472 * track, or a single sector)
2473 * All floppy_track_buffer handling goes in here. If we ever add track buffer
2474 * allocation on the fly, it should be done here. No other part should need
2475 * modification.
2478 static int make_raw_rw_request(void)
2480 int aligned_sector_t;
2481 int max_sector, max_size, tracksize, ssize;
2483 set_fdc(DRIVE(CURRENT->rq_dev));
2485 raw_cmd = &default_raw_cmd;
2486 raw_cmd->flags = FD_RAW_SPIN | FD_RAW_NEED_DISK | FD_RAW_NEED_DISK |
2487 FD_RAW_NEED_SEEK;
2488 raw_cmd->cmd_count = NR_RW;
2489 if (CURRENT->cmd == READ){
2490 raw_cmd->flags |= FD_RAW_READ;
2491 COMMAND = FM_MODE(_floppy,FD_READ);
2492 } else if (CURRENT->cmd == WRITE){
2493 raw_cmd->flags |= FD_RAW_WRITE;
2494 COMMAND = FM_MODE(_floppy,FD_WRITE);
2495 } else {
2496 DPRINT("make_raw_rw_request: unknown command\n");
2497 return 0;
2500 max_sector = _floppy->sect * _floppy->head;
2502 TRACK = CURRENT->sector / max_sector;
2503 sector_t = CURRENT->sector % max_sector;
2504 if (_floppy->track && TRACK >= _floppy->track) {
2505 if(CURRENT->current_nr_sectors & 1) {
2506 current_count_sectors = 1;
2507 return 1;
2508 } else
2509 return 0;
2511 HEAD = sector_t / _floppy->sect;
2513 if (((_floppy->stretch & FD_SWAPSIDES) || TESTF(FD_NEED_TWADDLE)) &&
2514 sector_t < _floppy->sect)
2515 max_sector = _floppy->sect;
2517 /* 2M disks have phantom sectors on the first track */
2518 if ((_floppy->rate & FD_2M) && (!TRACK) && (!HEAD)){
2519 max_sector = 2 * _floppy->sect / 3;
2520 if (sector_t >= max_sector){
2521 current_count_sectors = minimum(_floppy->sect - sector_t,
2522 CURRENT->nr_sectors);
2523 return 1;
2525 SIZECODE = 2;
2526 } else
2527 SIZECODE = FD_SIZECODE(_floppy);
2528 raw_cmd->rate = _floppy->rate & 0x43;
2529 if ((_floppy->rate & FD_2M) &&
2530 (TRACK || HEAD) &&
2531 raw_cmd->rate == 2)
2532 raw_cmd->rate = 1;
2534 if (SIZECODE)
2535 SIZECODE2 = 0xff;
2536 else
2537 SIZECODE2 = 0x80;
2538 raw_cmd->track = TRACK << STRETCH(_floppy);
2539 DR_SELECT = UNIT(current_drive) + PH_HEAD(_floppy,HEAD);
2540 GAP = _floppy->gap;
2541 CODE2SIZE;
2542 SECT_PER_TRACK = _floppy->sect << 2 >> SIZECODE;
2543 SECTOR = ((sector_t % _floppy->sect) << 2 >> SIZECODE) + 1;
2544 tracksize = _floppy->sect - _floppy->sect % ssize;
2545 if (tracksize < _floppy->sect){
2546 SECT_PER_TRACK ++;
2547 if (tracksize <= sector_t % _floppy->sect)
2548 SECTOR--;
2549 while (tracksize <= sector_t % _floppy->sect){
2550 while(tracksize + ssize > _floppy->sect){
2551 SIZECODE--;
2552 ssize >>= 1;
2554 SECTOR++; SECT_PER_TRACK ++;
2555 tracksize += ssize;
2557 max_sector = HEAD * _floppy->sect + tracksize;
2558 } else if (!TRACK && !HEAD && !(_floppy->rate & FD_2M) && probing)
2559 max_sector = _floppy->sect;
2561 aligned_sector_t = sector_t - (sector_t % _floppy->sect) % ssize;
2562 max_size = CURRENT->nr_sectors;
2563 if ((raw_cmd->track == buffer_track) &&
2564 (current_drive == buffer_drive) &&
2565 (sector_t >= buffer_min) && (sector_t < buffer_max)) {
2566 /* data already in track buffer */
2567 if (CT(COMMAND) == FD_READ) {
2568 copy_buffer(1, max_sector, buffer_max);
2569 return 1;
2571 } else if (aligned_sector_t != sector_t || CURRENT->nr_sectors < ssize){
2572 if (CT(COMMAND) == FD_WRITE){
2573 if (sector_t + CURRENT->nr_sectors > ssize &&
2574 sector_t + CURRENT->nr_sectors < ssize + ssize)
2575 max_size = ssize + ssize;
2576 else
2577 max_size = ssize;
2579 raw_cmd->flags &= ~FD_RAW_WRITE;
2580 raw_cmd->flags |= FD_RAW_READ;
2581 COMMAND = FM_MODE(_floppy,FD_READ);
2582 } else if ((unsigned long)CURRENT->buffer < MAX_DMA_ADDRESS) {
2583 unsigned long dma_limit;
2584 int direct, indirect;
2586 indirect= transfer_size(ssize,max_sector,max_buffer_sectors*2) -
2587 sector_t;
2590 * Do NOT use minimum() here---MAX_DMA_ADDRESS is 64 bits wide
2591 * on a 64 bit machine!
2593 max_size = buffer_chain_size();
2594 dma_limit = (MAX_DMA_ADDRESS - ((unsigned long) CURRENT->buffer)) >> 9;
2595 if ((unsigned long) max_size > dma_limit) {
2596 max_size = dma_limit;
2598 /* 64 kb boundaries */
2599 if (CROSS_64KB(CURRENT->buffer, max_size << 9))
2600 max_size = (K_64 -
2601 ((unsigned long)CURRENT->buffer) % K_64)>>9;
2602 direct = transfer_size(ssize,max_sector,max_size) - sector_t;
2604 * We try to read tracks, but if we get too many errors, we
2605 * go back to reading just one sector at a time.
2607 * This means we should be able to read a sector even if there
2608 * are other bad sectors on this track.
2610 if (!direct ||
2611 (indirect * 2 > direct * 3 &&
2612 *errors < DP->max_errors.read_track &&
2613 /*!TESTF(FD_NEED_TWADDLE) &&*/
2614 ((!probing || (DP->read_track&(1<<DRS->probed_format)))))){
2615 max_size = CURRENT->nr_sectors;
2616 } else {
2617 raw_cmd->kernel_data = CURRENT->buffer;
2618 raw_cmd->length = current_count_sectors << 9;
2619 if (raw_cmd->length == 0){
2620 DPRINT("zero dma transfer attempted from make_raw_request\n");
2621 DPRINT("indirect=%d direct=%d sector_t=%d",
2622 indirect, direct, sector_t);
2623 return 0;
2625 /* check_dma_crossing(raw_cmd->kernel_data,
2626 raw_cmd->length,
2627 "end of make_raw_request [1]");*/
2628 return 2;
2632 if (CT(COMMAND) == FD_READ)
2633 max_size = max_sector; /* unbounded */
2635 /* claim buffer track if needed */
2636 if (buffer_track != raw_cmd->track || /* bad track */
2637 buffer_drive !=current_drive || /* bad drive */
2638 sector_t > buffer_max ||
2639 sector_t < buffer_min ||
2640 ((CT(COMMAND) == FD_READ ||
2641 (aligned_sector_t == sector_t && CURRENT->nr_sectors >= ssize))&&
2642 max_sector > 2 * max_buffer_sectors + buffer_min &&
2643 max_size + sector_t > 2 * max_buffer_sectors + buffer_min)
2644 /* not enough space */){
2645 buffer_track = -1;
2646 buffer_drive = current_drive;
2647 buffer_max = buffer_min = aligned_sector_t;
2649 raw_cmd->kernel_data = floppy_track_buffer +
2650 ((aligned_sector_t-buffer_min)<<9);
2652 if (CT(COMMAND) == FD_WRITE){
2653 /* copy write buffer to track buffer.
2654 * if we get here, we know that the write
2655 * is either aligned or the data already in the buffer
2656 * (buffer will be overwritten) */
2657 #ifdef FLOPPY_SANITY_CHECK
2658 if (sector_t != aligned_sector_t && buffer_track == -1)
2659 DPRINT("internal error offset !=0 on write\n");
2660 #endif
2661 buffer_track = raw_cmd->track;
2662 buffer_drive = current_drive;
2663 copy_buffer(ssize, max_sector, 2*max_buffer_sectors+buffer_min);
2664 } else
2665 transfer_size(ssize, max_sector,
2666 2*max_buffer_sectors+buffer_min-aligned_sector_t);
2668 /* round up current_count_sectors to get dma xfer size */
2669 raw_cmd->length = sector_t+current_count_sectors-aligned_sector_t;
2670 raw_cmd->length = ((raw_cmd->length -1)|(ssize-1))+1;
2671 raw_cmd->length <<= 9;
2672 #ifdef FLOPPY_SANITY_CHECK
2673 /*check_dma_crossing(raw_cmd->kernel_data, raw_cmd->length,
2674 "end of make_raw_request");*/
2675 if ((raw_cmd->length < current_count_sectors << 9) ||
2676 (raw_cmd->kernel_data != CURRENT->buffer &&
2677 CT(COMMAND) == FD_WRITE &&
2678 (aligned_sector_t + (raw_cmd->length >> 9) > buffer_max ||
2679 aligned_sector_t < buffer_min)) ||
2680 raw_cmd->length % (128 << SIZECODE) ||
2681 raw_cmd->length <= 0 || current_count_sectors <= 0){
2682 DPRINT("fractionary current count b=%lx s=%lx\n",
2683 raw_cmd->length, current_count_sectors);
2684 if (raw_cmd->kernel_data != CURRENT->buffer)
2685 printk("addr=%d, length=%ld\n",
2686 (int) ((raw_cmd->kernel_data -
2687 floppy_track_buffer) >> 9),
2688 current_count_sectors);
2689 printk("st=%d ast=%d mse=%d msi=%d\n",
2690 sector_t, aligned_sector_t, max_sector, max_size);
2691 printk("ssize=%x SIZECODE=%d\n", ssize, SIZECODE);
2692 printk("command=%x SECTOR=%d HEAD=%d, TRACK=%d\n",
2693 COMMAND, SECTOR, HEAD, TRACK);
2694 printk("buffer drive=%d\n", buffer_drive);
2695 printk("buffer track=%d\n", buffer_track);
2696 printk("buffer_min=%d\n", buffer_min);
2697 printk("buffer_max=%d\n", buffer_max);
2698 return 0;
2701 if (raw_cmd->kernel_data != CURRENT->buffer){
2702 if (raw_cmd->kernel_data < floppy_track_buffer ||
2703 current_count_sectors < 0 ||
2704 raw_cmd->length < 0 ||
2705 raw_cmd->kernel_data + raw_cmd->length >
2706 floppy_track_buffer + (max_buffer_sectors << 10)){
2707 DPRINT("buffer overrun in schedule dma\n");
2708 printk("sector_t=%d buffer_min=%d current_count=%ld\n",
2709 sector_t, buffer_min,
2710 raw_cmd->length >> 9);
2711 printk("current_count_sectors=%ld\n",
2712 current_count_sectors);
2713 if (CT(COMMAND) == FD_READ)
2714 printk("read\n");
2715 if (CT(COMMAND) == FD_READ)
2716 printk("write\n");
2717 return 0;
2719 } else if (raw_cmd->length > CURRENT->nr_sectors << 9 ||
2720 current_count_sectors > CURRENT->nr_sectors){
2721 DPRINT("buffer overrun in direct transfer\n");
2722 return 0;
2723 } else if (raw_cmd->length < current_count_sectors << 9){
2724 DPRINT("more sectors than bytes\n");
2725 printk("bytes=%ld\n", raw_cmd->length >> 9);
2726 printk("sectors=%ld\n", current_count_sectors);
2728 if (raw_cmd->length == 0){
2729 DPRINT("zero dma transfer attempted from make_raw_request\n");
2730 return 0;
2732 #endif
2733 return 2;
2736 static void redo_fd_request(void)
2738 #define REPEAT {request_done(0); continue; }
2739 kdev_t device;
2740 int tmp;
2742 lastredo = jiffies;
2743 if (current_drive < N_DRIVE)
2744 floppy_off(current_drive);
2746 if (CURRENT && CURRENT->rq_status == RQ_INACTIVE){
2747 CLEAR_INTR;
2748 unlock_fdc();
2749 return;
2752 while(1){
2753 if (!CURRENT) {
2754 CLEAR_INTR;
2755 unlock_fdc();
2756 return;
2758 if (MAJOR(CURRENT->rq_dev) != MAJOR_NR)
2759 panic(DEVICE_NAME ": request list destroyed");
2760 if (CURRENT->bh && !buffer_locked(CURRENT->bh))
2761 panic(DEVICE_NAME ": block not locked");
2763 device = CURRENT->rq_dev;
2764 set_fdc(DRIVE(device));
2765 reschedule_timeout(CURRENTD, "redo fd request", 0);
2767 set_floppy(device);
2768 raw_cmd = & default_raw_cmd;
2769 raw_cmd->flags = 0;
2770 if (start_motor(redo_fd_request)) return;
2771 disk_change(current_drive);
2772 if (test_bit(current_drive, &fake_change) ||
2773 TESTF(FD_DISK_CHANGED)){
2774 DPRINT("disk absent or changed during operation\n");
2775 REPEAT;
2777 if (!_floppy) { /* Autodetection */
2778 if (!probing){
2779 DRS->probed_format = 0;
2780 if (next_valid_format()){
2781 DPRINT("no autodetectable formats\n");
2782 _floppy = NULL;
2783 REPEAT;
2786 probing = 1;
2787 _floppy = floppy_type+DP->autodetect[DRS->probed_format];
2788 } else
2789 probing = 0;
2790 errors = & (CURRENT->errors);
2791 tmp = make_raw_rw_request();
2792 if (tmp < 2){
2793 request_done(tmp);
2794 continue;
2797 if (TESTF(FD_NEED_TWADDLE))
2798 twaddle();
2799 schedule_bh( (void *)(void *) floppy_start);
2800 #ifdef DEBUGT
2801 debugt("queue fd request");
2802 #endif
2803 return;
2805 #undef REPEAT
2808 static struct cont_t rw_cont={
2809 rw_interrupt,
2810 redo_fd_request,
2811 bad_flp_intr,
2812 request_done };
2814 static void process_fd_request(void)
2816 cont = &rw_cont;
2817 schedule_bh( (void *)(void *) redo_fd_request);
2820 static void do_fd_request(void)
2822 if(usage_count == 0) {
2823 printk("warning: usage count=0, CURRENT=%p exiting\n", CURRENT);
2824 printk("sect=%ld cmd=%d\n", CURRENT->sector, CURRENT->cmd);
2825 return;
2827 sti();
2828 if (fdc_busy){
2829 /* fdc busy, this new request will be treated when the
2830 current one is done */
2831 is_alive("do fd request, old request running");
2832 return;
2834 lock_fdc(MAXTIMEOUT,0);
2835 process_fd_request();
2836 is_alive("do fd request");
2839 static struct cont_t poll_cont={
2840 success_and_wakeup,
2841 floppy_ready,
2842 generic_failure,
2843 generic_done };
2845 static int poll_drive(int interruptible, int flag)
2847 int ret;
2848 /* no auto-sense, just clear dcl */
2849 raw_cmd = &default_raw_cmd;
2850 raw_cmd->flags= flag;
2851 raw_cmd->track=0;
2852 raw_cmd->cmd_count=0;
2853 cont = &poll_cont;
2854 #ifdef DCL_DEBUG
2855 if (DP->flags & FD_DEBUG){
2856 DPRINT("setting NEWCHANGE in poll_drive\n");
2858 #endif
2859 SETF(FD_DISK_NEWCHANGE);
2860 WAIT(floppy_ready);
2861 return ret;
2865 * User triggered reset
2866 * ====================
2869 static void reset_intr(void)
2871 printk("weird, reset interrupt called\n");
2874 static struct cont_t reset_cont={
2875 reset_intr,
2876 success_and_wakeup,
2877 generic_failure,
2878 generic_done };
2880 static int user_reset_fdc(int drive, int arg, int interruptible)
2882 int ret;
2884 ret=0;
2885 LOCK_FDC(drive,interruptible);
2886 if (arg == FD_RESET_ALWAYS)
2887 FDCS->reset=1;
2888 if (FDCS->reset){
2889 cont = &reset_cont;
2890 WAIT(reset_fdc);
2892 process_fd_request();
2893 return ret;
2897 * Misc Ioctl's and support
2898 * ========================
2900 static inline int fd_copyout(void *param, const void *address, int size)
2902 return copy_to_user(param,address, size) ? -EFAULT : 0;
2905 static inline int fd_copyin(void *param, void *address, int size)
2907 return copy_from_user(address, param, size) ? -EFAULT : 0;
2910 #define _COPYOUT(x) (copy_to_user((void *)param, &(x), sizeof(x)) ? -EFAULT : 0)
2911 #define _COPYIN(x) (copy_from_user(&(x), (void *)param, sizeof(x)) ? -EFAULT : 0)
2913 #define COPYOUT(x) ECALL(_COPYOUT(x))
2914 #define COPYIN(x) ECALL(_COPYIN(x))
2916 static inline const char *drive_name(int type, int drive)
2918 struct floppy_struct *floppy;
2920 if (type)
2921 floppy = floppy_type + type;
2922 else {
2923 if (UDP->native_format)
2924 floppy = floppy_type + UDP->native_format;
2925 else
2926 return "(null)";
2928 if (floppy->name)
2929 return floppy->name;
2930 else
2931 return "(null)";
2935 /* raw commands */
2936 static void raw_cmd_done(int flag)
2938 int i;
2940 if (!flag) {
2941 raw_cmd->flags |= FD_RAW_FAILURE;
2942 raw_cmd->flags |= FD_RAW_HARDFAILURE;
2943 } else {
2944 raw_cmd->reply_count = inr;
2945 if(raw_cmd->reply_count > MAX_REPLIES)
2946 raw_cmd->reply_count=0;
2947 for (i=0; i< raw_cmd->reply_count; i++)
2948 raw_cmd->reply[i] = reply_buffer[i];
2950 if (raw_cmd->flags & (FD_RAW_READ | FD_RAW_WRITE))
2951 raw_cmd->length = fd_get_dma_residue();
2953 if ((raw_cmd->flags & FD_RAW_SOFTFAILURE) &&
2954 (!raw_cmd->reply_count || (raw_cmd->reply[0] & 0xc0)))
2955 raw_cmd->flags |= FD_RAW_FAILURE;
2957 if (disk_change(current_drive))
2958 raw_cmd->flags |= FD_RAW_DISK_CHANGE;
2959 else
2960 raw_cmd->flags &= ~FD_RAW_DISK_CHANGE;
2961 if (raw_cmd->flags & FD_RAW_NO_MOTOR_AFTER)
2962 motor_off_callback(current_drive);
2964 if (raw_cmd->next &&
2965 (!(raw_cmd->flags & FD_RAW_FAILURE) ||
2966 !(raw_cmd->flags & FD_RAW_STOP_IF_FAILURE)) &&
2967 ((raw_cmd->flags & FD_RAW_FAILURE) ||
2968 !(raw_cmd->flags &FD_RAW_STOP_IF_SUCCESS))) {
2969 raw_cmd = raw_cmd->next;
2970 return;
2973 generic_done(flag);
2977 static struct cont_t raw_cmd_cont={
2978 success_and_wakeup,
2979 floppy_start,
2980 generic_failure,
2981 raw_cmd_done
2984 static inline int raw_cmd_copyout(int cmd, char *param,
2985 struct floppy_raw_cmd *ptr)
2987 int ret;
2989 while(ptr) {
2990 COPYOUT(*ptr);
2991 param += sizeof(struct floppy_raw_cmd);
2992 if ((ptr->flags & FD_RAW_READ) && ptr->buffer_length){
2993 if (ptr->length>=0 && ptr->length<=ptr->buffer_length)
2994 ECALL(fd_copyout(ptr->data,
2995 ptr->kernel_data,
2996 ptr->buffer_length -
2997 ptr->length));
2999 ptr = ptr->next;
3001 return 0;
3005 static void raw_cmd_free(struct floppy_raw_cmd **ptr)
3007 struct floppy_raw_cmd *next,*this;
3009 this = *ptr;
3010 *ptr = 0;
3011 while(this) {
3012 if (this->buffer_length) {
3013 fd_dma_mem_free((unsigned long)this->kernel_data,
3014 this->buffer_length);
3015 this->buffer_length = 0;
3017 next = this->next;
3018 kfree(this);
3019 this = next;
3024 static inline int raw_cmd_copyin(int cmd, char *param,
3025 struct floppy_raw_cmd **rcmd)
3027 struct floppy_raw_cmd *ptr;
3028 int ret;
3029 int i;
3031 *rcmd = 0;
3032 while(1) {
3033 ptr = (struct floppy_raw_cmd *)
3034 kmalloc(sizeof(struct floppy_raw_cmd), GFP_USER);
3035 if (!ptr)
3036 return -ENOMEM;
3037 *rcmd = ptr;
3038 COPYIN(*ptr);
3039 ptr->next = 0;
3040 ptr->buffer_length = 0;
3041 param += sizeof(struct floppy_raw_cmd);
3042 if (ptr->cmd_count > 33)
3043 /* the command may now also take up the space
3044 * initially intended for the reply & the
3045 * reply count. Needed for long 82078 commands
3046 * such as RESTORE, which takes ... 17 command
3047 * bytes. Murphy's law #137: When you reserve
3048 * 16 bytes for a structure, you'll one day
3049 * discover that you really need 17...
3051 return -EINVAL;
3053 for (i=0; i< 16; i++)
3054 ptr->reply[i] = 0;
3055 ptr->resultcode = 0;
3056 ptr->kernel_data = 0;
3058 if (ptr->flags & (FD_RAW_READ | FD_RAW_WRITE)) {
3059 if (ptr->length <= 0)
3060 return -EINVAL;
3061 ptr->kernel_data =(char*)fd_dma_mem_alloc(ptr->length);
3062 fallback_on_nodma_alloc(&ptr->kernel_data,
3063 ptr->length);
3064 if (!ptr->kernel_data)
3065 return -ENOMEM;
3066 ptr->buffer_length = ptr->length;
3068 if (ptr->flags & FD_RAW_WRITE)
3069 ECALL(fd_copyin(ptr->data, ptr->kernel_data,
3070 ptr->length));
3071 rcmd = & (ptr->next);
3072 if (!(ptr->flags & FD_RAW_MORE))
3073 return 0;
3074 ptr->rate &= 0x43;
3079 static int raw_cmd_ioctl(int cmd, void *param)
3081 int drive, ret, ret2;
3082 struct floppy_raw_cmd *my_raw_cmd;
3084 if (FDCS->rawcmd <= 1)
3085 FDCS->rawcmd = 1;
3086 for (drive= 0; drive < N_DRIVE; drive++){
3087 if (FDC(drive) != fdc)
3088 continue;
3089 if (drive == current_drive){
3090 if (UDRS->fd_ref > 1){
3091 FDCS->rawcmd = 2;
3092 break;
3094 } else if (UDRS->fd_ref){
3095 FDCS->rawcmd = 2;
3096 break;
3100 if (FDCS->reset)
3101 return -EIO;
3103 ret = raw_cmd_copyin(cmd, param, &my_raw_cmd);
3104 if (ret) {
3105 raw_cmd_free(&my_raw_cmd);
3106 return ret;
3109 raw_cmd = my_raw_cmd;
3110 cont = &raw_cmd_cont;
3111 ret=wait_til_done(floppy_start,1);
3112 #ifdef DCL_DEBUG
3113 if (DP->flags & FD_DEBUG){
3114 DPRINT("calling disk change from raw_cmd ioctl\n");
3116 #endif
3118 if (ret != -EINTR && FDCS->reset)
3119 ret = -EIO;
3121 DRS->track = NO_TRACK;
3123 ret2 = raw_cmd_copyout(cmd, param, my_raw_cmd);
3124 if (!ret)
3125 ret = ret2;
3126 raw_cmd_free(&my_raw_cmd);
3127 return ret;
3130 static int invalidate_drive(kdev_t rdev)
3132 /* invalidate the buffer track to force a reread */
3133 set_bit(DRIVE(rdev), &fake_change);
3134 process_fd_request();
3135 check_disk_change(rdev);
3136 return 0;
3140 static inline void clear_write_error(int drive)
3142 CLEARSTRUCT(UDRWE);
3145 static inline int set_geometry(unsigned int cmd, struct floppy_struct *g,
3146 int drive, int type, kdev_t device)
3148 int cnt;
3150 /* sanity checking for parameters.*/
3151 if (g->sect <= 0 ||
3152 g->head <= 0 ||
3153 g->track <= 0 ||
3154 g->track > UDP->tracks>>STRETCH(g) ||
3155 /* check if reserved bits are set */
3156 (g->stretch&~(FD_STRETCH|FD_SWAPSIDES)) != 0)
3157 return -EINVAL;
3158 if (type){
3159 if (!suser())
3160 return -EPERM;
3161 LOCK_FDC(drive,1);
3162 for (cnt = 0; cnt < N_DRIVE; cnt++){
3163 if (ITYPE(drive_state[cnt].fd_device) == type &&
3164 drive_state[cnt].fd_ref)
3165 set_bit(drive, &fake_change);
3167 floppy_type[type] = *g;
3168 floppy_type[type].name="user format";
3169 for (cnt = type << 2; cnt < (type << 2) + 4; cnt++)
3170 floppy_sizes[cnt]= floppy_sizes[cnt+0x80]=
3171 (floppy_type[type].size+1)>>1;
3172 process_fd_request();
3173 for (cnt = 0; cnt < N_DRIVE; cnt++){
3174 if (ITYPE(drive_state[cnt].fd_device) == type &&
3175 drive_state[cnt].fd_ref)
3176 check_disk_change(
3177 MKDEV(FLOPPY_MAJOR,
3178 drive_state[cnt].fd_device));
3180 } else {
3181 LOCK_FDC(drive,1);
3182 if (cmd != FDDEFPRM)
3183 /* notice a disk change immediately, else
3184 * we loose our settings immediately*/
3185 CALL(poll_drive(1, FD_RAW_NEED_DISK));
3186 user_params[drive] = *g;
3187 if (buffer_drive == drive)
3188 SUPBOUND(buffer_max, user_params[drive].sect);
3189 current_type[drive] = &user_params[drive];
3190 floppy_sizes[drive] = (user_params[drive].size+1) >> 1;
3191 if (cmd == FDDEFPRM)
3192 DRS->keep_data = -1;
3193 else
3194 DRS->keep_data = 1;
3195 /* invalidation. Invalidate only when needed, i.e.
3196 * when there are already sectors in the buffer cache
3197 * whose number will change. This is useful, because
3198 * mtools often changes the geometry of the disk after
3199 * looking at the boot block */
3200 if (DRS->maxblock > user_params[drive].sect || DRS->maxtrack)
3201 invalidate_drive(device);
3202 else
3203 process_fd_request();
3205 return 0;
3208 /* handle obsolete ioctl's */
3209 static int ioctl_table[]= {
3210 FDCLRPRM,
3211 FDSETPRM,
3212 FDDEFPRM,
3213 FDGETPRM,
3214 FDMSGON,
3215 FDMSGOFF,
3216 FDFMTBEG,
3217 FDFMTTRK,
3218 FDFMTEND,
3219 FDSETEMSGTRESH,
3220 FDFLUSH,
3221 FDSETMAXERRS,
3222 FDGETMAXERRS,
3223 FDGETDRVTYP,
3224 FDSETDRVPRM,
3225 FDGETDRVPRM,
3226 FDGETDRVSTAT,
3227 FDPOLLDRVSTAT,
3228 FDRESET,
3229 FDGETFDCSTAT,
3230 FDWERRORCLR,
3231 FDWERRORGET,
3232 FDRAWCMD,
3233 FDEJECT,
3234 FDTWADDLE
3237 static inline int normalize_ioctl(int *cmd, int *size)
3239 int i;
3241 for (i=0; i < ARRAY_SIZE(ioctl_table); i++) {
3242 if ((*cmd & 0xffff) == (ioctl_table[i] & 0xffff)){
3243 *size = _IOC_SIZE(*cmd);
3244 *cmd = ioctl_table[i];
3245 if (*size > _IOC_SIZE(*cmd)) {
3246 printk("ioctl not yet supported\n");
3247 return -EFAULT;
3249 return 0;
3252 return -EINVAL;
3255 static int get_floppy_geometry(int drive, int type, struct floppy_struct **g)
3257 if (type)
3258 *g = &floppy_type[type];
3259 else {
3260 LOCK_FDC(drive,0);
3261 CALL(poll_drive(0,0));
3262 process_fd_request();
3263 *g = current_type[drive];
3265 if(!*g)
3266 return -ENODEV;
3267 return 0;
3270 static int fd_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
3271 unsigned long param)
3273 #define IOCTL_MODE_BIT 8
3274 #define OPEN_WRITE_BIT 16
3275 #define IOCTL_ALLOWED (filp && (filp->f_mode & IOCTL_MODE_BIT))
3276 #define OUT(c,x) case c: outparam = (const char *) (x); break
3277 #define IN(c,x,tag) case c: *(x) = inparam. tag ; return 0
3279 int i,drive,type;
3280 kdev_t device;
3281 int ret;
3282 int size;
3283 union inparam {
3284 struct floppy_struct g; /* geometry */
3285 struct format_descr f;
3286 struct floppy_max_errors max_errors;
3287 struct floppy_drive_params dp;
3288 } inparam; /* parameters coming from user space */
3289 const char *outparam; /* parameters passed back to user space */
3291 device = inode->i_rdev;
3292 switch (cmd) {
3293 RO_IOCTLS(device,param);
3295 type = TYPE(device);
3296 drive = DRIVE(device);
3298 /* convert compatibility eject ioctls into floppy eject ioctl.
3299 * We do this in order to provide a means to eject floppy disks before
3300 * installing the new fdutils package */
3301 if(cmd == CDROMEJECT || /* CD-ROM eject */
3302 cmd == 0x6470 /* SunOS floppy eject */) {
3303 DPRINT("obsolete eject ioctl\n");
3304 DPRINT("please use floppycontrol --eject\n");
3305 cmd = FDEJECT;
3308 /* generic block device ioctls */
3309 switch(cmd) {
3310 /* the following have been inspired by the corresponding
3311 * code for other block devices. */
3312 struct floppy_struct *g;
3313 case HDIO_GETGEO:
3315 struct hd_geometry loc;
3316 ECALL(get_floppy_geometry(drive, type, &g));
3317 loc.heads = g->head;
3318 loc.sectors = g->sect;
3319 loc.cylinders = g->track;
3320 loc.start = 0;
3321 return _COPYOUT(loc);
3323 case BLKRASET:
3324 if(!suser()) return -EACCES;
3325 if(param > 0xff) return -EINVAL;
3326 read_ahead[MAJOR(inode->i_rdev)] = param;
3327 return 0;
3328 case BLKRAGET:
3329 return put_user(read_ahead[MAJOR(inode->i_rdev)],
3330 (long *) param);
3331 case BLKFLSBUF:
3332 if(!suser()) return -EACCES;
3333 fsync_dev(inode->i_rdev);
3334 invalidate_buffers(inode->i_rdev);
3335 return 0;
3337 case BLKGETSIZE:
3338 ECALL(get_floppy_geometry(drive, type, &g));
3339 return put_user(g->size, (long *) param);
3340 /* BLKRRPART is not defined as floppies don't have
3341 * partition tables */
3344 /* convert the old style command into a new style command */
3345 if ((cmd & 0xff00) == 0x0200) {
3346 ECALL(normalize_ioctl(&cmd, &size));
3347 } else
3348 return -EINVAL;
3350 /* permission checks */
3351 if (((cmd & 0x40) && !IOCTL_ALLOWED) ||
3352 ((cmd & 0x80) && !suser()))
3353 return -EPERM;
3355 /* copyin */
3356 CLEARSTRUCT(&inparam);
3357 if (_IOC_DIR(cmd) & _IOC_WRITE)
3358 ECALL(fd_copyin((void *)param, &inparam, size))
3360 switch (cmd) {
3361 case FDEJECT:
3362 if(UDRS->fd_ref != 1)
3363 /* somebody else has this drive open */
3364 return -EBUSY;
3365 LOCK_FDC(drive,1);
3367 /* do the actual eject. Fails on
3368 * non-Sparc architectures */
3369 ret=fd_eject(UNIT(drive));
3371 USETF(FD_DISK_CHANGED);
3372 USETF(FD_VERIFY);
3373 process_fd_request();
3374 return ret;
3375 case FDCLRPRM:
3376 LOCK_FDC(drive,1);
3377 current_type[drive] = NULL;
3378 floppy_sizes[drive] = MAX_DISK_SIZE;
3379 UDRS->keep_data = 0;
3380 return invalidate_drive(device);
3381 case FDSETPRM:
3382 case FDDEFPRM:
3383 return set_geometry(cmd, & inparam.g,
3384 drive, type, device);
3385 case FDGETPRM:
3386 ECALL(get_floppy_geometry(drive, type,
3387 (struct floppy_struct**)
3388 &outparam));
3389 break;
3391 case FDMSGON:
3392 UDP->flags |= FTD_MSG;
3393 return 0;
3394 case FDMSGOFF:
3395 UDP->flags &= ~FTD_MSG;
3396 return 0;
3398 case FDFMTBEG:
3399 LOCK_FDC(drive,1);
3400 CALL(poll_drive(1, FD_RAW_NEED_DISK));
3401 ret = UDRS->flags;
3402 process_fd_request();
3403 if(ret & FD_VERIFY)
3404 return -ENODEV;
3405 if(!(ret & FD_DISK_WRITABLE))
3406 return -EROFS;
3407 return 0;
3408 case FDFMTTRK:
3409 if (UDRS->fd_ref != 1)
3410 return -EBUSY;
3411 return do_format(device, &inparam.f);
3412 case FDFMTEND:
3413 case FDFLUSH:
3414 LOCK_FDC(drive,1);
3415 return invalidate_drive(device);
3417 case FDSETEMSGTRESH:
3418 UDP->max_errors.reporting =
3419 (unsigned short) (param & 0x0f);
3420 return 0;
3421 OUT(FDGETMAXERRS, &UDP->max_errors);
3422 IN(FDSETMAXERRS, &UDP->max_errors, max_errors);
3424 case FDGETDRVTYP:
3425 outparam = drive_name(type,drive);
3426 SUPBOUND(size,strlen(outparam)+1);
3427 break;
3429 IN(FDSETDRVPRM, UDP, dp);
3430 OUT(FDGETDRVPRM, UDP);
3432 case FDPOLLDRVSTAT:
3433 LOCK_FDC(drive,1);
3434 CALL(poll_drive(1, FD_RAW_NEED_DISK));
3435 process_fd_request();
3436 /* fall through */
3437 OUT(FDGETDRVSTAT, UDRS);
3439 case FDRESET:
3440 return user_reset_fdc(drive, (int)param, 1);
3442 OUT(FDGETFDCSTAT,UFDCS);
3444 case FDWERRORCLR:
3445 CLEARSTRUCT(UDRWE);
3446 return 0;
3447 OUT(FDWERRORGET,UDRWE);
3449 case FDRAWCMD:
3450 if (type)
3451 return -EINVAL;
3452 LOCK_FDC(drive,1);
3453 set_floppy(device);
3454 CALL(i = raw_cmd_ioctl(cmd,(void *) param));
3455 process_fd_request();
3456 return i;
3458 case FDTWADDLE:
3459 LOCK_FDC(drive,1);
3460 twaddle();
3461 process_fd_request();
3462 return 0;
3464 default:
3465 return -EINVAL;
3468 if (_IOC_DIR(cmd) & _IOC_READ)
3469 return fd_copyout((void *)param, outparam, size);
3470 else
3471 return 0;
3472 #undef IOCTL_ALLOWED
3473 #undef OUT
3474 #undef IN
3477 static void config_types(void)
3479 int first=1;
3480 int drive;
3482 /* read drive info out of physical CMOS */
3483 drive=0;
3484 if (!UDP->cmos)
3485 UDP->cmos= FLOPPY0_TYPE;
3486 drive=1;
3487 if (!UDP->cmos && FLOPPY1_TYPE)
3488 UDP->cmos = FLOPPY1_TYPE;
3490 /* XXX */
3491 /* additional physical CMOS drive detection should go here */
3493 for (drive=0; drive < N_DRIVE; drive++){
3494 if (UDP->cmos >= 16)
3495 UDP->cmos = 0;
3496 if (UDP->cmos >= 0 && UDP->cmos <= NUMBER(default_drive_params))
3497 memcpy((char *) UDP,
3498 (char *) (&default_drive_params[(int)UDP->cmos].params),
3499 sizeof(struct floppy_drive_params));
3500 if (UDP->cmos){
3501 if (first)
3502 printk(KERN_INFO "Floppy drive(s): ");
3503 else
3504 printk(", ");
3505 first=0;
3506 if (UDP->cmos > 0){
3507 allowed_drive_mask |= 1 << drive;
3508 printk("fd%d is %s", drive,
3509 default_drive_params[(int)UDP->cmos].name);
3510 } else
3511 printk("fd%d is unknown type %d",drive,
3512 UDP->cmos);
3515 if (!first)
3516 printk("\n");
3519 static ssize_t floppy_read(struct file * filp, char *buf,
3520 size_t count, loff_t *ppos)
3522 struct inode *inode = filp->f_dentry->d_inode;
3523 int drive = DRIVE(inode->i_rdev);
3525 check_disk_change(inode->i_rdev);
3526 if (UTESTF(FD_DISK_CHANGED))
3527 return -ENXIO;
3528 return block_read(filp, buf, count, ppos);
3531 static ssize_t floppy_write(struct file * filp, const char * buf,
3532 size_t count, loff_t *ppos)
3534 struct inode * inode = filp->f_dentry->d_inode;
3535 int block;
3536 int ret;
3537 int drive = DRIVE(inode->i_rdev);
3539 if (!UDRS->maxblock)
3540 UDRS->maxblock=1;/* make change detectable */
3541 check_disk_change(inode->i_rdev);
3542 if (UTESTF(FD_DISK_CHANGED))
3543 return -ENXIO;
3544 if (!UTESTF(FD_DISK_WRITABLE))
3545 return -EROFS;
3546 block = (*ppos + count) >> 9;
3547 INFBOUND(UDRS->maxblock, block);
3548 ret= block_write(filp, buf, count, ppos);
3549 return ret;
3552 static int floppy_release(struct inode * inode, struct file * filp)
3554 int drive;
3556 drive = DRIVE(inode->i_rdev);
3559 * If filp is NULL, we're being called from blkdev_release
3560 * or after a failed mount attempt. In the former case the
3561 * device has already been sync'ed, and in the latter no
3562 * sync is required. Otherwise, sync if filp is writable.
3564 if (filp && (filp->f_mode & (2 | OPEN_WRITE_BIT)))
3565 block_fsync(filp, filp->f_dentry);
3567 if (UDRS->fd_ref < 0)
3568 UDRS->fd_ref=0;
3569 else if (!UDRS->fd_ref--) {
3570 DPRINT("floppy_release with fd_ref == 0");
3571 UDRS->fd_ref = 0;
3573 floppy_release_irq_and_dma();
3574 return 0;
3578 * floppy_open check for aliasing (/dev/fd0 can be the same as
3579 * /dev/PS0 etc), and disallows simultaneous access to the same
3580 * drive with different device numbers.
3582 #define RETERR(x) do{floppy_release(inode,filp); return -(x);}while(0)
3584 static int floppy_open(struct inode * inode, struct file * filp)
3586 int drive;
3587 int old_dev;
3588 int try;
3589 char *tmp;
3591 if (!filp) {
3592 DPRINT("Weird, open called with filp=0\n");
3593 return -EIO;
3596 drive = DRIVE(inode->i_rdev);
3597 if (drive >= N_DRIVE ||
3598 !(allowed_drive_mask & (1 << drive)) ||
3599 fdc_state[FDC(drive)].version == FDC_NONE)
3600 return -ENXIO;
3602 if (TYPE(inode->i_rdev) >= NUMBER(floppy_type))
3603 return -ENXIO;
3604 old_dev = UDRS->fd_device;
3605 if (UDRS->fd_ref && old_dev != MINOR(inode->i_rdev))
3606 return -EBUSY;
3608 if (!UDRS->fd_ref && (UDP->flags & FD_BROKEN_DCL)){
3609 USETF(FD_DISK_CHANGED);
3610 USETF(FD_VERIFY);
3613 if (UDRS->fd_ref == -1 ||
3614 (UDRS->fd_ref && (filp->f_flags & O_EXCL)))
3615 return -EBUSY;
3617 if (floppy_grab_irq_and_dma())
3618 return -EBUSY;
3620 if (filp->f_flags & O_EXCL)
3621 UDRS->fd_ref = -1;
3622 else
3623 UDRS->fd_ref++;
3625 if (!floppy_track_buffer){
3626 /* if opening an ED drive, reserve a big buffer,
3627 * else reserve a small one */
3628 if ((UDP->cmos == 6) || (UDP->cmos == 5))
3629 try = 64; /* Only 48 actually useful */
3630 else
3631 try = 32; /* Only 24 actually useful */
3633 tmp=(char *)fd_dma_mem_alloc(1024 * try);
3634 if (!tmp && !floppy_track_buffer) {
3635 try >>= 1; /* buffer only one side */
3636 INFBOUND(try, 16);
3637 tmp= (char *)fd_dma_mem_alloc(1024*try);
3639 if(!tmp && !floppy_track_buffer) {
3640 fallback_on_nodma_alloc(&tmp, 2048 * try);
3642 if (!tmp && !floppy_track_buffer) {
3643 DPRINT("Unable to allocate DMA memory\n");
3644 RETERR(ENXIO);
3646 if (floppy_track_buffer) {
3647 if(tmp)
3648 fd_dma_mem_free((unsigned long)tmp,try*1024);
3649 } else {
3650 buffer_min = buffer_max = -1;
3651 floppy_track_buffer = tmp;
3652 max_buffer_sectors = try;
3656 UDRS->fd_device = MINOR(inode->i_rdev);
3657 if (old_dev != -1 && old_dev != MINOR(inode->i_rdev)) {
3658 if (buffer_drive == drive)
3659 buffer_track = -1;
3660 invalidate_buffers(MKDEV(FLOPPY_MAJOR,old_dev));
3663 /* Allow ioctls if we have write-permissions even if read-only open */
3664 if ((filp->f_mode & 2) || (permission(inode,2) == 0))
3665 filp->f_mode |= IOCTL_MODE_BIT;
3666 if (filp->f_mode & 2)
3667 filp->f_mode |= OPEN_WRITE_BIT;
3669 if (UFDCS->rawcmd == 1)
3670 UFDCS->rawcmd = 2;
3672 if (filp->f_flags & O_NDELAY)
3673 return 0;
3674 if (filp->f_mode & 3) {
3675 UDRS->last_checked = 0;
3676 check_disk_change(inode->i_rdev);
3677 if (UTESTF(FD_DISK_CHANGED))
3678 RETERR(ENXIO);
3680 if ((filp->f_mode & 2) && !(UTESTF(FD_DISK_WRITABLE)))
3681 RETERR(EROFS);
3682 return 0;
3683 #undef RETERR
3687 * Check if the disk has been changed or if a change has been faked.
3689 static int check_floppy_change(kdev_t dev)
3691 int drive = DRIVE(dev);
3693 if (MAJOR(dev) != MAJOR_NR) {
3694 DPRINT("check_floppy_change: not a floppy\n");
3695 return 0;
3698 if (UTESTF(FD_DISK_CHANGED) || UTESTF(FD_VERIFY))
3699 return 1;
3701 if (UDP->checkfreq < jiffies - UDRS->last_checked){
3702 lock_fdc(drive,0);
3703 poll_drive(0,0);
3704 process_fd_request();
3707 if (UTESTF(FD_DISK_CHANGED) ||
3708 UTESTF(FD_VERIFY) ||
3709 test_bit(drive, &fake_change) ||
3710 (!TYPE(dev) && !current_type[drive]))
3711 return 1;
3712 return 0;
3715 /* revalidate the floppy disk, i.e. trigger format autodetection by reading
3716 * the bootblock (block 0). "Autodetection" is also needed to check whether
3717 * there is a disk in the drive at all... Thus we also do it for fixed
3718 * geometry formats */
3719 static int floppy_revalidate(kdev_t dev)
3721 #define NO_GEOM (!current_type[drive] && !TYPE(dev))
3722 struct buffer_head * bh;
3723 int drive=DRIVE(dev);
3724 int cf;
3726 if (UTESTF(FD_DISK_CHANGED) ||
3727 UTESTF(FD_VERIFY) ||
3728 test_bit(drive, &fake_change) ||
3729 NO_GEOM){
3730 lock_fdc(drive,0);
3731 cf = UTESTF(FD_DISK_CHANGED) || UTESTF(FD_VERIFY);
3732 if (!(cf || test_bit(drive, &fake_change) || NO_GEOM)){
3733 process_fd_request(); /*already done by another thread*/
3734 return 0;
3736 UDRS->maxblock = 0;
3737 UDRS->maxtrack = 0;
3738 if (buffer_drive == drive)
3739 buffer_track = -1;
3740 clear_bit(drive, &fake_change);
3741 UCLEARF(FD_DISK_CHANGED);
3742 if (cf)
3743 UDRS->generation++;
3744 if (NO_GEOM){
3745 /* auto-sensing */
3746 int size = floppy_blocksizes[MINOR(dev)];
3747 if (!size)
3748 size = 1024;
3749 if (!(bh = getblk(dev,0,size))){
3750 process_fd_request();
3751 return 1;
3753 if (bh && !buffer_uptodate(bh))
3754 ll_rw_block(READ, 1, &bh);
3755 process_fd_request();
3756 wait_on_buffer(bh);
3757 brelse(bh);
3758 return 0;
3760 if (cf)
3761 poll_drive(0, FD_RAW_NEED_DISK);
3762 process_fd_request();
3764 return 0;
3767 static struct file_operations floppy_fops = {
3768 NULL, /* lseek - default */
3769 floppy_read, /* read - general block-dev read */
3770 floppy_write, /* write - general block-dev write */
3771 NULL, /* readdir - bad */
3772 NULL, /* poll */
3773 fd_ioctl, /* ioctl */
3774 NULL, /* mmap */
3775 floppy_open, /* open */
3776 floppy_release, /* release */
3777 block_fsync, /* fsync */
3778 NULL, /* fasync */
3779 check_floppy_change, /* media_change */
3780 floppy_revalidate, /* revalidate */
3784 * Floppy Driver initialization
3785 * =============================
3788 /* Determine the floppy disk controller type */
3789 /* This routine was written by David C. Niemi */
3790 __initfunc(static char get_fdc_version(void))
3792 int r;
3794 output_byte(FD_DUMPREGS); /* 82072 and better know DUMPREGS */
3795 if (FDCS->reset)
3796 return FDC_NONE;
3797 if ((r = result()) <= 0x00)
3798 return FDC_NONE; /* No FDC present ??? */
3799 if ((r==1) && (reply_buffer[0] == 0x80)){
3800 printk(KERN_INFO "FDC %d is an 8272A\n",fdc);
3801 return FDC_8272A; /* 8272a/765 don't know DUMPREGS */
3803 if (r != 10) {
3804 printk("FDC %d init: DUMPREGS: unexpected return of %d bytes.\n",
3805 fdc, r);
3806 return FDC_UNKNOWN;
3809 if(!fdc_configure()) {
3810 printk(KERN_INFO "FDC %d is an 82072\n",fdc);
3811 return FDC_82072; /* 82072 doesn't know CONFIGURE */
3814 output_byte(FD_PERPENDICULAR);
3815 if(need_more_output() == MORE_OUTPUT) {
3816 output_byte(0);
3817 } else {
3818 printk(KERN_INFO "FDC %d is an 82072A\n", fdc);
3819 return FDC_82072A; /* 82072A as found on Sparcs. */
3822 output_byte(FD_UNLOCK);
3823 r = result();
3824 if ((r == 1) && (reply_buffer[0] == 0x80)){
3825 printk(KERN_INFO "FDC %d is a pre-1991 82077\n", fdc);
3826 return FDC_82077_ORIG; /* Pre-1991 82077, doesn't know
3827 * LOCK/UNLOCK */
3829 if ((r != 1) || (reply_buffer[0] != 0x00)) {
3830 printk("FDC %d init: UNLOCK: unexpected return of %d bytes.\n",
3831 fdc, r);
3832 return FDC_UNKNOWN;
3834 output_byte(FD_PARTID);
3835 r = result();
3836 if (r != 1) {
3837 printk("FDC %d init: PARTID: unexpected return of %d bytes.\n",
3838 fdc, r);
3839 return FDC_UNKNOWN;
3841 if (reply_buffer[0] == 0x80) {
3842 printk(KERN_INFO "FDC %d is a post-1991 82077\n",fdc);
3843 return FDC_82077; /* Revised 82077AA passes all the tests */
3845 switch (reply_buffer[0] >> 5) {
3846 case 0x0:
3847 /* Either a 82078-1 or a 82078SL running at 5Volt */
3848 printk(KERN_INFO "FDC %d is an 82078.\n",fdc);
3849 return FDC_82078;
3850 case 0x1:
3851 printk(KERN_INFO "FDC %d is a 44pin 82078\n",fdc);
3852 return FDC_82078;
3853 case 0x2:
3854 printk(KERN_INFO "FDC %d is a S82078B\n", fdc);
3855 return FDC_S82078B;
3856 case 0x3:
3857 printk(KERN_INFO "FDC %d is a National Semiconductor PC87306\n", fdc);
3858 return FDC_87306;
3859 default:
3860 printk(KERN_INFO "FDC %d init: 82078 variant with unknown PARTID=%d.\n",
3861 fdc, reply_buffer[0] >> 5);
3862 return FDC_82078_UNKN;
3864 } /* get_fdc_version */
3866 /* lilo configuration */
3868 __initfunc(static void floppy_set_flags(int *ints,int param, int param2))
3870 int i;
3872 for (i=0; i < ARRAY_SIZE(default_drive_params); i++){
3873 if (param)
3874 default_drive_params[i].params.flags |= param2;
3875 else
3876 default_drive_params[i].params.flags &= ~param2;
3878 DPRINT("%s flag 0x%x\n", param2 ? "Setting" : "Clearing", param);
3881 __initfunc(static void daring(int *ints,int param, int param2))
3883 int i;
3885 for (i=0; i < ARRAY_SIZE(default_drive_params); i++){
3886 if (param){
3887 default_drive_params[i].params.select_delay = 0;
3888 default_drive_params[i].params.flags |= FD_SILENT_DCL_CLEAR;
3889 } else {
3890 default_drive_params[i].params.select_delay = 2*HZ/100;
3891 default_drive_params[i].params.flags &= ~FD_SILENT_DCL_CLEAR;
3894 DPRINT("Assuming %s floppy hardware\n", param ? "standard" : "broken");
3897 __initfunc(static void set_cmos(int *ints, int dummy, int dummy2))
3899 int current_drive=0;
3901 if (ints[0] != 2){
3902 DPRINT("wrong number of parameter for cmos\n");
3903 return;
3905 current_drive = ints[1];
3906 if (current_drive < 0 || current_drive >= 8){
3907 DPRINT("bad drive for set_cmos\n");
3908 return;
3910 if (current_drive >= 4 && !FDC2)
3911 FDC2 = 0x370;
3912 if (ints[2] <= 0 ||
3913 (ints[2] >= NUMBER(default_drive_params) && ints[2] != 16)){
3914 DPRINT("bad cmos code %d\n", ints[2]);
3915 return;
3917 DP->cmos = ints[2];
3918 DPRINT("setting cmos code to %d\n", ints[2]);
3921 static struct param_table {
3922 const char *name;
3923 void (*fn)(int *ints, int param, int param2);
3924 int *var;
3925 int def_param;
3926 int param2;
3927 } config_params[]={
3928 { "allowed_drive_mask", 0, &allowed_drive_mask, 0xff, 0}, /* obsolete */
3929 { "all_drives", 0, &allowed_drive_mask, 0xff, 0 }, /* obsolete */
3930 { "asus_pci", 0, &allowed_drive_mask, 0x33, 0},
3932 { "irq", 0, &FLOPPY_IRQ, 6, 0 },
3933 { "dma", 0, &FLOPPY_DMA, 2, 0 },
3935 { "daring", daring, 0, 1, 0},
3937 { "two_fdc", 0, &FDC2, 0x370, 0 },
3938 { "one_fdc", 0, &FDC2, 0, 0 },
3940 { "thinkpad", floppy_set_flags, 0, 1, FD_INVERTED_DCL },
3941 { "broken_dcl", floppy_set_flags, 0, 1, FD_BROKEN_DCL },
3942 { "messages", floppy_set_flags, 0, 1, FTD_MSG },
3943 { "silent_dcl_clear", floppy_set_flags, 0, 1, FD_SILENT_DCL_CLEAR },
3944 { "debug", floppy_set_flags, 0, 1, FD_DEBUG },
3946 { "nodma", 0, &can_use_virtual_dma, 1, 0 },
3947 { "omnibook", 0, &can_use_virtual_dma, 1, 0 },
3948 { "yesdma", 0, &can_use_virtual_dma, 0, 0 },
3950 { "fifo_depth", 0, &fifo_depth, 0xa, 0 },
3951 { "nofifo", 0, &no_fifo, 0x20, 0 },
3952 { "usefifo", 0, &no_fifo, 0, 0 },
3954 { "cmos", set_cmos, 0, 0, 0 },
3956 { "unexpected_interrupts", 0, &print_unex, 1, 0 },
3957 { "no_unexpected_interrupts", 0, &print_unex, 0, 0 },
3958 { "L40SX", 0, &print_unex, 0, 0 } };
3960 #define FLOPPY_SETUP
3961 __initfunc(void floppy_setup(char *str, int *ints))
3963 int i;
3964 int param;
3965 if (str)
3966 for (i=0; i< ARRAY_SIZE(config_params); i++){
3967 if (strcmp(str,config_params[i].name) == 0){
3968 if (ints[0])
3969 param = ints[1];
3970 else
3971 param = config_params[i].def_param;
3972 if(config_params[i].fn)
3973 config_params[i].
3974 fn(ints,param,
3975 config_params[i].param2);
3976 if(config_params[i].var) {
3977 DPRINT("%s=%d\n", str, param);
3978 *config_params[i].var = param;
3980 return;
3983 if (str) {
3984 DPRINT("unknown floppy option [%s]\n", str);
3986 DPRINT("allowed options are:");
3987 for (i=0; i< ARRAY_SIZE(config_params); i++)
3988 printk(" %s",config_params[i].name);
3989 printk("\n");
3990 } else
3991 DPRINT("botched floppy option\n");
3992 DPRINT("Read linux/drivers/block/README.fd\n");
3995 static int have_no_fdc= -EIO;
3998 __initfunc(int floppy_init(void))
4000 int i,unit,drive;
4003 raw_cmd = 0;
4005 if (register_blkdev(MAJOR_NR,"fd",&floppy_fops)) {
4006 printk("Unable to get major %d for floppy\n",MAJOR_NR);
4007 return -EBUSY;
4010 for (i=0; i<256; i++)
4011 if (ITYPE(i))
4012 floppy_sizes[i] = (floppy_type[ITYPE(i)].size+1) >> 1;
4013 else
4014 floppy_sizes[i] = MAX_DISK_SIZE;
4016 blk_size[MAJOR_NR] = floppy_sizes;
4017 blksize_size[MAJOR_NR] = floppy_blocksizes;
4018 blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
4019 reschedule_timeout(MAXTIMEOUT, "floppy init", MAXTIMEOUT);
4020 config_types();
4022 for (i = 0; i < N_FDC; i++) {
4023 fdc = i;
4024 CLEARSTRUCT(FDCS);
4025 FDCS->dtr = -1;
4026 FDCS->dor = 0x4;
4027 #ifdef __sparc__
4028 /*sparcs don't have a DOR reset which we can fall back on to*/
4029 FDCS->version = FDC_82072A;
4030 #endif
4033 use_virtual_dma = can_use_virtual_dma & 1;
4034 fdc_state[0].address = FDC1;
4035 if (fdc_state[0].address == -1) {
4036 unregister_blkdev(MAJOR_NR,"fd");
4037 del_timer(&fd_timeout);
4038 return -ENODEV;
4040 #if N_FDC > 1
4041 fdc_state[1].address = FDC2;
4042 #endif
4044 if (floppy_grab_irq_and_dma()){
4045 unregister_blkdev(MAJOR_NR,"fd");
4046 del_timer(&fd_timeout);
4047 return -EBUSY;
4050 /* initialise drive state */
4051 for (drive = 0; drive < N_DRIVE; drive++) {
4052 CLEARSTRUCT(UDRS);
4053 CLEARSTRUCT(UDRWE);
4054 UDRS->flags = FD_VERIFY | FD_DISK_NEWCHANGE | FD_DISK_CHANGED;
4055 UDRS->fd_device = -1;
4056 floppy_track_buffer = NULL;
4057 max_buffer_sectors = 0;
4060 for (i = 0; i < N_FDC; i++) {
4061 fdc = i;
4062 FDCS->driver_version = FD_DRIVER_VERSION;
4063 for (unit=0; unit<4; unit++)
4064 FDCS->track[unit] = 0;
4065 if (FDCS->address == -1)
4066 continue;
4067 FDCS->rawcmd = 2;
4068 if (user_reset_fdc(-1,FD_RESET_ALWAYS,0)){
4069 FDCS->address = -1;
4070 FDCS->version = FDC_NONE;
4071 continue;
4073 /* Try to determine the floppy controller type */
4074 FDCS->version = get_fdc_version();
4075 if (FDCS->version == FDC_NONE){
4076 FDCS->address = -1;
4077 continue;
4079 if(can_use_virtual_dma == 2 && FDCS->version < FDC_82072A)
4080 can_use_virtual_dma = 0;
4082 have_no_fdc = 0;
4083 /* Not all FDCs seem to be able to handle the version command
4084 * properly, so force a reset for the standard FDC clones,
4085 * to avoid interrupt garbage.
4087 user_reset_fdc(-1,FD_RESET_ALWAYS,0);
4089 fdc=0;
4090 del_timer(&fd_timeout);
4091 current_drive = 0;
4092 floppy_release_irq_and_dma();
4093 initialising=0;
4094 if (have_no_fdc) {
4095 DPRINT("no floppy controllers found\n");
4096 unregister_blkdev(MAJOR_NR,"fd");
4098 return have_no_fdc;
4101 static int floppy_grab_irq_and_dma(void)
4103 unsigned long flags;
4105 INT_OFF;
4106 if (usage_count++){
4107 INT_ON;
4108 return 0;
4110 INT_ON;
4111 MOD_INC_USE_COUNT;
4112 if (fd_request_irq()) {
4113 DPRINT("Unable to grab IRQ%d for the floppy driver\n",
4114 FLOPPY_IRQ);
4115 MOD_DEC_USE_COUNT;
4116 usage_count--;
4117 return -1;
4119 if (fd_request_dma()) {
4120 DPRINT("Unable to grab DMA%d for the floppy driver\n",
4121 FLOPPY_DMA);
4122 fd_free_irq();
4123 MOD_DEC_USE_COUNT;
4124 usage_count--;
4125 return -1;
4128 for (fdc=0; fdc< N_FDC; fdc++){
4129 if (FDCS->address != -1){
4130 if (check_region(FDCS->address, 6) < 0 ||
4131 check_region(FDCS->address+7, 1) < 0) {
4132 DPRINT("Floppy io-port 0x%04lx in use\n", FDCS->address);
4133 fd_free_irq();
4134 fd_free_dma();
4135 while(--fdc >= 0) {
4136 release_region(FDCS->address, 6);
4137 release_region(FDCS->address+7, 1);
4139 MOD_DEC_USE_COUNT;
4140 usage_count--;
4141 return -1;
4143 request_region(FDCS->address, 6, "floppy");
4144 request_region(FDCS->address+7, 1, "floppy DIR");
4145 /* address + 6 is reserved, and may be taken by IDE.
4146 * Unfortunately, Adaptec doesn't know this :-(, */
4149 for (fdc=0; fdc< N_FDC; fdc++){
4150 if (FDCS->address != -1){
4151 reset_fdc_info(1);
4152 fd_outb(FDCS->dor, FD_DOR);
4155 fdc = 0;
4156 set_dor(0, ~0, 8); /* avoid immediate interrupt */
4158 for (fdc = 0; fdc < N_FDC; fdc++)
4159 if (FDCS->address != -1)
4160 fd_outb(FDCS->dor, FD_DOR);
4161 fdc = 0;
4162 fd_enable_irq();
4163 return 0;
4166 static void floppy_release_irq_and_dma(void)
4168 int old_fdc;
4169 #ifdef FLOPPY_SANITY_CHECK
4170 #ifndef __sparc__
4171 int drive;
4172 #endif
4173 #endif
4174 long tmpsize;
4175 unsigned long tmpaddr;
4176 unsigned long flags;
4178 INT_OFF;
4179 if (--usage_count){
4180 INT_ON;
4181 return;
4183 INT_ON;
4184 fd_disable_dma();
4185 fd_free_dma();
4186 fd_disable_irq();
4187 fd_free_irq();
4189 set_dor(0, ~0, 8);
4190 #if N_FDC > 1
4191 set_dor(1, ~8, 0);
4192 #endif
4193 floppy_enable_hlt();
4195 if (floppy_track_buffer && max_buffer_sectors) {
4196 tmpsize = max_buffer_sectors*1024;
4197 tmpaddr = (unsigned long)floppy_track_buffer;
4198 floppy_track_buffer = 0;
4199 max_buffer_sectors = 0;
4200 buffer_min = buffer_max = -1;
4201 fd_dma_mem_free(tmpaddr, tmpsize);
4204 #ifdef FLOPPY_SANITY_CHECK
4205 #ifndef __sparc__
4206 for (drive=0; drive < N_FDC * 4; drive++)
4207 if (motor_off_timer[drive].next)
4208 printk("motor off timer %d still active\n", drive);
4209 #endif
4211 if (fd_timeout.next)
4212 printk("floppy timer still active:%s\n", timeout_message);
4213 if (fd_timer.next)
4214 printk("auxiliary floppy timer still active\n");
4215 if (floppy_tq.sync)
4216 printk("task queue still active\n");
4217 #endif
4218 old_fdc = fdc;
4219 for (fdc = 0; fdc < N_FDC; fdc++)
4220 if (FDCS->address != -1) {
4221 release_region(FDCS->address, 6);
4222 release_region(FDCS->address+7, 1);
4224 fdc = old_fdc;
4225 MOD_DEC_USE_COUNT;
4229 #ifdef MODULE
4231 extern char *get_options(char *str, int *ints);
4233 char *floppy=NULL;
4235 __initfunc(static void parse_floppy_cfg_string(char *cfg))
4237 char *ptr;
4238 int ints[11];
4240 while(*cfg) {
4241 for(ptr = cfg;*cfg && *cfg != ' ' && *cfg != '\t'; cfg++);
4242 if(*cfg) {
4243 *cfg = '\0';
4244 cfg++;
4246 if(*ptr)
4247 floppy_setup(get_options(ptr,ints),ints);
4251 __initfunc(static void mod_setup(char *pattern, void (*setup)(char *, int *)))
4253 unsigned long i;
4254 char c;
4255 int j;
4256 int match;
4257 char buffer[100];
4258 int ints[11];
4259 int length = strlen(pattern)+1;
4261 match=0;
4262 j=1;
4264 for (i=current->mm->env_start; i< current->mm->env_end; i ++){
4265 get_user(c, (char *)i);
4266 if (match){
4267 if (j==99)
4268 c='\0';
4269 buffer[j] = c;
4270 if (!c || c == ' ' || c == '\t'){
4271 if (j){
4272 buffer[j] = '\0';
4273 setup(get_options(buffer,ints),ints);
4275 j=0;
4276 } else
4277 j++;
4278 if (!c)
4279 break;
4280 continue;
4282 if ((!j && !c) || (j && c == pattern[j-1]))
4283 j++;
4284 else
4285 j=0;
4286 if (j==length){
4287 match=1;
4288 j=0;
4294 #ifdef __cplusplus
4295 extern "C" {
4296 #endif
4297 int init_module(void)
4299 printk(KERN_INFO "inserting floppy driver for " UTS_RELEASE "\n");
4301 if(floppy)
4302 parse_floppy_cfg_string(floppy);
4303 else
4304 mod_setup("floppy=", floppy_setup);
4306 return floppy_init();
4309 void cleanup_module(void)
4311 int dummy;
4313 unregister_blkdev(MAJOR_NR, "fd");
4315 blk_dev[MAJOR_NR].request_fn = 0;
4316 /* eject disk, if any */
4317 dummy = fd_eject(0);
4320 MODULE_PARM(floppy,"s");
4321 MODULE_PARM(FLOPPY_IRQ,"i");
4322 MODULE_PARM(FLOPPY_DMA,"i");
4323 MODULE_AUTHOR("Alain L. Knaff");
4324 MODULE_SUPPORTED_DEVICE("fd");
4326 #ifdef __cplusplus
4328 #endif
4330 #else
4331 /* eject the boot floppy (if we need the drive for a different root floppy) */
4332 /* This should only be called at boot time when we're sure that there's no
4333 * resource contention. */
4334 void floppy_eject(void)
4336 int dummy;
4337 if(have_no_fdc)
4338 return;
4339 floppy_grab_irq_and_dma();
4340 lock_fdc(MAXTIMEOUT,0);
4341 dummy=fd_eject(0);
4342 process_fd_request();
4343 floppy_release_irq_and_dma();
4345 #endif