Import 2.1.118
[davej-history.git] / drivers / block / floppy.c
blobb8ef979224bd8263ea5542708deb50110cb93c4a
1 /*
2 * linux/kernel/floppy.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 * Copyright (C) 1993, 1994 Alain Knaff
6 * Copyright (C) 1998 Alan Cox
7 */
8 /*
9 * 02.12.91 - Changed to static variables to indicate need for reset
10 * and recalibrate. This makes some things easier (output_byte reset
11 * checking etc), and means less interrupt jumping in case of errors,
12 * so the code is hopefully easier to understand.
16 * This file is certainly a mess. I've tried my best to get it working,
17 * but I don't like programming floppies, and I have only one anyway.
18 * Urgel. I should check for more errors, and do more graceful error
19 * recovery. Seems there are problems with several drives. I've tried to
20 * correct them. No promises.
24 * As with hd.c, all routines within this file can (and will) be called
25 * by interrupts, so extreme caution is needed. A hardware interrupt
26 * handler may not sleep, or a kernel panic will happen. Thus I cannot
27 * call "floppy-on" directly, but have to set a special timer interrupt
28 * etc.
32 * 28.02.92 - made track-buffering routines, based on the routines written
33 * by entropy@wintermute.wpi.edu (Lawrence Foard). Linus.
37 * Automatic floppy-detection and formatting written by Werner Almesberger
38 * (almesber@nessie.cs.id.ethz.ch), who also corrected some problems with
39 * the floppy-change signal detection.
43 * 1992/7/22 -- Hennus Bergman: Added better error reporting, fixed
44 * FDC data overrun bug, added some preliminary stuff for vertical
45 * recording support.
47 * 1992/9/17: Added DMA allocation & DMA functions. -- hhb.
49 * TODO: Errors are still not counted properly.
52 /* 1992/9/20
53 * Modifications for ``Sector Shifting'' by Rob Hooft (hooft@chem.ruu.nl)
54 * modeled after the freeware MS-DOS program fdformat/88 V1.8 by
55 * Christoph H. Hochst\"atter.
56 * I have fixed the shift values to the ones I always use. Maybe a new
57 * ioctl() should be created to be able to modify them.
58 * There is a bug in the driver that makes it impossible to format a
59 * floppy as the first thing after bootup.
63 * 1993/4/29 -- Linus -- cleaned up the timer handling in the kernel, and
64 * this helped the floppy driver as well. Much cleaner, and still seems to
65 * work.
68 /* 1994/6/24 --bbroad-- added the floppy table entries and made
69 * minor modifications to allow 2.88 floppies to be run.
72 /* 1994/7/13 -- Paul Vojta -- modified the probing code to allow three or more
73 * disk types.
77 * 1994/8/8 -- Alain Knaff -- Switched to fdpatch driver: Support for bigger
78 * format bug fixes, but unfortunately some new bugs too...
81 /* 1994/9/17 -- Koen Holtman -- added logging of physical floppy write
82 * errors to allow safe writing by specialized programs.
85 /* 1995/4/24 -- Dan Fandrich -- added support for Commodore 1581 3.5" disks
86 * by defining bit 1 of the "stretch" parameter to mean put sectors on the
87 * opposite side of the disk, leaving the sector IDs alone (i.e. Commodore's
88 * drives are "upside-down").
92 * 1995/8/26 -- Andreas Busse -- added Mips support.
96 * 1995/10/18 -- Ralf Baechle -- Portability cleanup; move machine dependent
97 * features to asm/floppy.h.
101 * 1998/06/07 -- Alan Cox -- Merged the 2.0.34 fixes for resource allocation
102 * failures.
105 #define FLOPPY_SANITY_CHECK
106 #undef FLOPPY_SILENT_DCL_CLEAR
108 #define REALLY_SLOW_IO
110 #define DEBUGT 2
111 #define DCL_DEBUG /* debug disk change line */
113 /* do print messages for unexpected interrupts */
114 static int print_unex=1;
115 #include <linux/module.h>
117 /* the following is the mask of allowed drives. By default units 2 and
118 * 3 of both floppy controllers are disabled, because switching on the
119 * motor of these drives causes system hangs on some PCI computers. drive
120 * 0 is the low bit (0x1), and drive 7 is the high bit (0x80). Bits are on if
121 * a drive is allowed. */
123 static int FLOPPY_IRQ=6;
124 static int FLOPPY_DMA=2;
125 static int allowed_drive_mask = 0x33;
126 static int irqdma_allocated = 0;
129 #include <linux/sched.h>
130 #include <linux/fs.h>
131 #include <linux/kernel.h>
132 #include <linux/timer.h>
133 #include <linux/tqueue.h>
134 #define FDPATCHES
135 #include <linux/fdreg.h>
138 #include <linux/fd.h>
139 #include <linux/hdreg.h>
141 #include <linux/errno.h>
142 #include <linux/malloc.h>
143 #include <linux/mm.h>
144 #include <linux/string.h>
145 #include <linux/fcntl.h>
146 #include <linux/delay.h>
147 #include <linux/mc146818rtc.h> /* CMOS defines */
148 #include <linux/ioport.h>
149 #include <linux/interrupt.h>
150 #include <linux/init.h>
152 #include <asm/dma.h>
153 #include <asm/irq.h>
154 #include <asm/system.h>
155 #include <asm/io.h>
156 #include <asm/uaccess.h>
158 static int can_use_virtual_dma=2;
159 /* =======
160 * can use virtual DMA:
161 * 0 = use of virtual DMA disallowed by config
162 * 1 = use of virtual DMA prescribed by config
163 * 2 = no virtual DMA preference configured. By default try hard DMA,
164 * but fall back on virtual DMA when not enough memory available
167 static int use_virtual_dma=0;
168 /* =======
169 * use virtual DMA
170 * 0 using hard DMA
171 * 1 using virtual DMA
172 * This variable is set to virtual when a DMA mem problem arises, and
173 * reset back in floppy_grab_irq_and_dma.
174 * It is not safe to reset it in other circumstances, because the floppy
175 * driver may have several buffers in use at once, and we do currently not
176 * record each buffers capabilities
179 static unsigned short virtual_dma_port=0x3f0;
180 void floppy_interrupt(int irq, void *dev_id, struct pt_regs * regs);
181 static int set_dor(int fdc, char mask, char data);
182 static inline int __get_order(unsigned long size);
183 #define K_64 0x10000 /* 64KB */
184 #include <asm/floppy.h>
187 #define MAJOR_NR FLOPPY_MAJOR
189 #include <linux/blk.h>
190 #include <linux/cdrom.h> /* for the compatibility eject ioctl */
192 #ifndef fd_get_dma_residue
193 #define fd_get_dma_residue() get_dma_residue(FLOPPY_DMA)
194 #endif
196 /* Dma Memory related stuff */
198 /* Pure 2^n version of get_order */
199 static inline int __get_order(unsigned long size)
201 int order;
203 size = (size-1) >> (PAGE_SHIFT-1);
204 order = -1;
205 do {
206 size >>= 1;
207 order++;
208 } while (size);
209 return order;
212 #ifndef fd_dma_mem_free
213 #define fd_dma_mem_free(addr, size) free_pages(addr, __get_order(size))
214 #endif
216 #ifndef fd_dma_mem_alloc
217 #define fd_dma_mem_alloc(size) __get_dma_pages(GFP_KERNEL,__get_order(size))
218 #endif
220 static inline void fallback_on_nodma_alloc(char **addr, size_t l)
222 #ifdef FLOPPY_CAN_FALLBACK_ON_NODMA
223 if(*addr)
224 return; /* we have the memory */
225 if(can_use_virtual_dma != 2)
226 return; /* no fallback allowed */
227 printk("DMA memory shortage. Temporarily falling back on virtual DMA\n");
228 *addr = (char *) nodma_mem_alloc(l);
229 #else
230 return;
231 #endif
234 /* End dma memory related stuff */
236 static unsigned int fake_change = 0;
237 static int initialising=1;
239 static inline int TYPE(kdev_t x) {
240 return (MINOR(x)>>2) & 0x1f;
242 static inline int DRIVE(kdev_t x) {
243 return (MINOR(x)&0x03) | ((MINOR(x)&0x80) >> 5);
245 #define ITYPE(x) (((x)>>2) & 0x1f)
246 #define TOMINOR(x) ((x & 3) | ((x & 4) << 5))
247 #define UNIT(x) ((x) & 0x03) /* drive on fdc */
248 #define FDC(x) (((x) & 0x04) >> 2) /* fdc of drive */
249 #define REVDRIVE(fdc, unit) ((unit) + ((fdc) << 2))
250 /* reverse mapping from unit and fdc to drive */
251 #define DP (&drive_params[current_drive])
252 #define DRS (&drive_state[current_drive])
253 #define DRWE (&write_errors[current_drive])
254 #define FDCS (&fdc_state[fdc])
255 #define CLEARF(x) (clear_bit(x##_BIT, &DRS->flags))
256 #define SETF(x) (set_bit(x##_BIT, &DRS->flags))
257 #define TESTF(x) (test_bit(x##_BIT, &DRS->flags))
259 #define UDP (&drive_params[drive])
260 #define UDRS (&drive_state[drive])
261 #define UDRWE (&write_errors[drive])
262 #define UFDCS (&fdc_state[FDC(drive)])
263 #define UCLEARF(x) (clear_bit(x##_BIT, &UDRS->flags))
264 #define USETF(x) (set_bit(x##_BIT, &UDRS->flags))
265 #define UTESTF(x) (test_bit(x##_BIT, &UDRS->flags))
267 #define DPRINT(format, args...) printk(DEVICE_NAME "%d: " format, current_drive , ## args)
269 #define PH_HEAD(floppy,head) (((((floppy)->stretch & 2) >>1) ^ head) << 2)
270 #define STRETCH(floppy) ((floppy)->stretch & FD_STRETCH)
272 #define CLEARSTRUCT(x) memset((x), 0, sizeof(*(x)))
274 #define INT_OFF save_flags(flags); cli()
275 #define INT_ON restore_flags(flags)
277 /* read/write */
278 #define COMMAND raw_cmd->cmd[0]
279 #define DR_SELECT raw_cmd->cmd[1]
280 #define TRACK raw_cmd->cmd[2]
281 #define HEAD raw_cmd->cmd[3]
282 #define SECTOR raw_cmd->cmd[4]
283 #define SIZECODE raw_cmd->cmd[5]
284 #define SECT_PER_TRACK raw_cmd->cmd[6]
285 #define GAP raw_cmd->cmd[7]
286 #define SIZECODE2 raw_cmd->cmd[8]
287 #define NR_RW 9
289 /* format */
290 #define F_SIZECODE raw_cmd->cmd[2]
291 #define F_SECT_PER_TRACK raw_cmd->cmd[3]
292 #define F_GAP raw_cmd->cmd[4]
293 #define F_FILL raw_cmd->cmd[5]
294 #define NR_F 6
297 * Maximum disk size (in kilobytes). This default is used whenever the
298 * current disk size is unknown.
299 * [Now it is rather a minimum]
301 #define MAX_DISK_SIZE 4 /* 3984*/
305 * globals used by 'result()'
307 #define MAX_REPLIES 16
308 static unsigned char reply_buffer[MAX_REPLIES];
309 static int inr; /* size of reply buffer, when called from interrupt */
310 #define ST0 (reply_buffer[0])
311 #define ST1 (reply_buffer[1])
312 #define ST2 (reply_buffer[2])
313 #define ST3 (reply_buffer[0]) /* result of GETSTATUS */
314 #define R_TRACK (reply_buffer[3])
315 #define R_HEAD (reply_buffer[4])
316 #define R_SECTOR (reply_buffer[5])
317 #define R_SIZECODE (reply_buffer[6])
319 #define SEL_DLY (2*HZ/100)
321 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
323 * this struct defines the different floppy drive types.
325 static struct {
326 struct floppy_drive_params params;
327 const char *name; /* name printed while booting */
328 } default_drive_params[]= {
329 /* NOTE: the time values in jiffies should be in msec!
330 CMOS drive type
331 | Maximum data rate supported by drive type
332 | | Head load time, msec
333 | | | Head unload time, msec (not used)
334 | | | | Step rate interval, usec
335 | | | | | Time needed for spinup time (jiffies)
336 | | | | | | Timeout for spinning down (jiffies)
337 | | | | | | | Spindown offset (where disk stops)
338 | | | | | | | | Select delay
339 | | | | | | | | | RPS
340 | | | | | | | | | | Max number of tracks
341 | | | | | | | | | | | Interrupt timeout
342 | | | | | | | | | | | | Max nonintlv. sectors
343 | | | | | | | | | | | | | -Max Errors- flags */
344 {{0, 500, 16, 16, 8000, 1*HZ, 3*HZ, 0, SEL_DLY, 5, 80, 3*HZ, 20, {3,1,2,0,2}, 0,
345 0, { 7, 4, 8, 2, 1, 5, 3,10}, 3*HZ/2, 0 }, "unknown" },
347 {{1, 300, 16, 16, 8000, 1*HZ, 3*HZ, 0, SEL_DLY, 5, 40, 3*HZ, 17, {3,1,2,0,2}, 0,
348 0, { 1, 0, 0, 0, 0, 0, 0, 0}, 3*HZ/2, 1 }, "360K PC" }, /*5 1/4 360 KB PC*/
350 {{2, 500, 16, 16, 6000, 4*HZ/10, 3*HZ, 14, SEL_DLY, 6, 83, 3*HZ, 17, {3,1,2,0,2}, 0,
351 0, { 2, 5, 6,23,10,20,11, 0}, 3*HZ/2, 2 }, "1.2M" }, /*5 1/4 HD AT*/
353 {{3, 250, 16, 16, 3000, 1*HZ, 3*HZ, 0, SEL_DLY, 5, 83, 3*HZ, 20, {3,1,2,0,2}, 0,
354 0, { 4,22,21,30, 3, 0, 0, 0}, 3*HZ/2, 4 }, "720k" }, /*3 1/2 DD*/
356 {{4, 500, 16, 16, 4000, 4*HZ/10, 3*HZ, 10, SEL_DLY, 5, 83, 3*HZ, 20, {3,1,2,0,2}, 0,
357 0, { 7, 4,25,22,31,21,29,11}, 3*HZ/2, 7 }, "1.44M" }, /*3 1/2 HD*/
359 {{5, 1000, 15, 8, 3000, 4*HZ/10, 3*HZ, 10, SEL_DLY, 5, 83, 3*HZ, 40, {3,1,2,0,2}, 0,
360 0, { 7, 8, 4,25,28,22,31,21}, 3*HZ/2, 8 }, "2.88M AMI BIOS" }, /*3 1/2 ED*/
362 {{6, 1000, 15, 8, 3000, 4*HZ/10, 3*HZ, 10, SEL_DLY, 5, 83, 3*HZ, 40, {3,1,2,0,2}, 0,
363 0, { 7, 8, 4,25,28,22,31,21}, 3*HZ/2, 8 }, "2.88M" } /*3 1/2 ED*/
364 /* | --autodetected formats--- | | |
365 * read_track | | Name printed when booting
366 * | Native format
367 * Frequency of disk change checks */
370 static struct floppy_drive_params drive_params[N_DRIVE];
371 static struct floppy_drive_struct drive_state[N_DRIVE];
372 static struct floppy_write_errors write_errors[N_DRIVE];
373 static struct floppy_raw_cmd *raw_cmd, default_raw_cmd;
376 * This struct defines the different floppy types.
378 * Bit 0 of 'stretch' tells if the tracks need to be doubled for some
379 * types (e.g. 360kB diskette in 1.2MB drive, etc.). Bit 1 of 'stretch'
380 * tells if the disk is in Commodore 1581 format, which means side 0 sectors
381 * are located on side 1 of the disk but with a side 0 ID, and vice-versa.
382 * This is the same as the Sharp MZ-80 5.25" CP/M disk format, except that the
383 * 1581's logical side 0 is on physical side 1, whereas the Sharp's logical
384 * side 0 is on physical side 0 (but with the misnamed sector IDs).
385 * 'stretch' should probably be renamed to something more general, like
386 * 'options'. Other parameters should be self-explanatory (see also
387 * setfdprm(8)).
389 static struct floppy_struct floppy_type[32] = {
390 { 0, 0,0, 0,0,0x00,0x00,0x00,0x00,NULL }, /* 0 no testing */
391 { 720, 9,2,40,0,0x2A,0x02,0xDF,0x50,"d360" }, /* 1 360KB PC */
392 { 2400,15,2,80,0,0x1B,0x00,0xDF,0x54,"h1200" }, /* 2 1.2MB AT */
393 { 720, 9,1,80,0,0x2A,0x02,0xDF,0x50,"D360" }, /* 3 360KB SS 3.5" */
394 { 1440, 9,2,80,0,0x2A,0x02,0xDF,0x50,"D720" }, /* 4 720KB 3.5" */
395 { 720, 9,2,40,1,0x23,0x01,0xDF,0x50,"h360" }, /* 5 360KB AT */
396 { 1440, 9,2,80,0,0x23,0x01,0xDF,0x50,"h720" }, /* 6 720KB AT */
397 { 2880,18,2,80,0,0x1B,0x00,0xCF,0x6C,"H1440" }, /* 7 1.44MB 3.5" */
398 { 5760,36,2,80,0,0x1B,0x43,0xAF,0x54,"E2880" }, /* 8 2.88MB 3.5" */
399 { 6240,39,2,80,0,0x1B,0x43,0xAF,0x28,"E3120"}, /* 9 3.12MB 3.5" */
401 { 2880,18,2,80,0,0x25,0x00,0xDF,0x02,"h1440" }, /* 10 1.44MB 5.25" */
402 { 3360,21,2,80,0,0x1C,0x00,0xCF,0x0C,"H1680" }, /* 11 1.68MB 3.5" */
403 { 820,10,2,41,1,0x25,0x01,0xDF,0x2E,"h410" }, /* 12 410KB 5.25" */
404 { 1640,10,2,82,0,0x25,0x02,0xDF,0x2E,"H820" }, /* 13 820KB 3.5" */
405 { 2952,18,2,82,0,0x25,0x00,0xDF,0x02,"h1476" }, /* 14 1.48MB 5.25" */
406 { 3444,21,2,82,0,0x25,0x00,0xDF,0x0C,"H1722" }, /* 15 1.72MB 3.5" */
407 { 840,10,2,42,1,0x25,0x01,0xDF,0x2E,"h420" }, /* 16 420KB 5.25" */
408 { 1660,10,2,83,0,0x25,0x02,0xDF,0x2E,"H830" }, /* 17 830KB 3.5" */
409 { 2988,18,2,83,0,0x25,0x00,0xDF,0x02,"h1494" }, /* 18 1.49MB 5.25" */
410 { 3486,21,2,83,0,0x25,0x00,0xDF,0x0C,"H1743" }, /* 19 1.74 MB 3.5" */
412 { 1760,11,2,80,0,0x1C,0x09,0xCF,0x00,"h880" }, /* 20 880KB 5.25" */
413 { 2080,13,2,80,0,0x1C,0x01,0xCF,0x00,"D1040" }, /* 21 1.04MB 3.5" */
414 { 2240,14,2,80,0,0x1C,0x19,0xCF,0x00,"D1120" }, /* 22 1.12MB 3.5" */
415 { 3200,20,2,80,0,0x1C,0x20,0xCF,0x2C,"h1600" }, /* 23 1.6MB 5.25" */
416 { 3520,22,2,80,0,0x1C,0x08,0xCF,0x2e,"H1760" }, /* 24 1.76MB 3.5" */
417 { 3840,24,2,80,0,0x1C,0x20,0xCF,0x00,"H1920" }, /* 25 1.92MB 3.5" */
418 { 6400,40,2,80,0,0x25,0x5B,0xCF,0x00,"E3200" }, /* 26 3.20MB 3.5" */
419 { 7040,44,2,80,0,0x25,0x5B,0xCF,0x00,"E3520" }, /* 27 3.52MB 3.5" */
420 { 7680,48,2,80,0,0x25,0x63,0xCF,0x00,"E3840" }, /* 28 3.84MB 3.5" */
422 { 3680,23,2,80,0,0x1C,0x10,0xCF,0x00,"H1840" }, /* 29 1.84MB 3.5" */
423 { 1600,10,2,80,0,0x25,0x02,0xDF,0x2E,"D800" }, /* 30 800KB 3.5" */
424 { 3200,20,2,80,0,0x1C,0x00,0xCF,0x2C,"H1600" }, /* 31 1.6MB 3.5" */
427 #define NUMBER(x) (sizeof(x) / sizeof(*(x)))
428 #define SECTSIZE (_FD_SECTSIZE(*floppy))
430 /* Auto-detection: Disk type used until the next media change occurs. */
431 static struct floppy_struct *current_type[N_DRIVE] = {
432 NULL, NULL, NULL, NULL,
433 NULL, NULL, NULL, NULL
437 * User-provided type information. current_type points to
438 * the respective entry of this array.
440 static struct floppy_struct user_params[N_DRIVE];
442 static int floppy_sizes[256];
443 static int floppy_blocksizes[256] = { 0, };
446 * The driver is trying to determine the correct media format
447 * while probing is set. rw_interrupt() clears it after a
448 * successful access.
450 static int probing = 0;
452 /* Synchronization of FDC access. */
453 #define FD_COMMAND_NONE -1
454 #define FD_COMMAND_ERROR 2
455 #define FD_COMMAND_OKAY 3
457 static volatile int command_status = FD_COMMAND_NONE, fdc_busy = 0;
458 static struct wait_queue *fdc_wait = NULL, *command_done = NULL;
459 #define NO_SIGNAL (!interruptible || !signal_pending(current))
460 #define CALL(x) if ((x) == -EINTR) return -EINTR
461 #define ECALL(x) if ((ret = (x))) return ret;
462 #define _WAIT(x,i) CALL(ret=wait_til_done((x),i))
463 #define WAIT(x) _WAIT((x),interruptible)
464 #define IWAIT(x) _WAIT((x),1)
466 /* Errors during formatting are counted here. */
467 static int format_errors;
469 /* Format request descriptor. */
470 static struct format_descr format_req;
473 * Rate is 0 for 500kb/s, 1 for 300kbps, 2 for 250kbps
474 * Spec1 is 0xSH, where S is stepping rate (F=1ms, E=2ms, D=3ms etc),
475 * H is head unload time (1=16ms, 2=32ms, etc)
479 * Track buffer
480 * Because these are written to by the DMA controller, they must
481 * not contain a 64k byte boundary crossing, or data will be
482 * corrupted/lost.
484 static char *floppy_track_buffer=0;
485 static int max_buffer_sectors=0;
487 static int *errors;
488 typedef void (*done_f)(int);
489 static struct cont_t {
490 void (*interrupt)(void); /* this is called after the interrupt of the
491 * main command */
492 void (*redo)(void); /* this is called to retry the operation */
493 void (*error)(void); /* this is called to tally an error */
494 done_f done; /* this is called to say if the operation has
495 * succeeded/failed */
496 } *cont=NULL;
498 static void floppy_ready(void);
499 static void floppy_start(void);
500 static void process_fd_request(void);
501 static void recalibrate_floppy(void);
502 static void floppy_shutdown(void);
504 static int floppy_grab_irq_and_dma(void);
505 static void floppy_release_irq_and_dma(void);
508 * The "reset" variable should be tested whenever an interrupt is scheduled,
509 * after the commands have been sent. This is to ensure that the driver doesn't
510 * get wedged when the interrupt doesn't come because of a failed command.
511 * reset doesn't need to be tested before sending commands, because
512 * output_byte is automatically disabled when reset is set.
514 #define CHECK_RESET { if (FDCS->reset){ reset_fdc(); return; } }
515 static void reset_fdc(void);
518 * These are global variables, as that's the easiest way to give
519 * information to interrupts. They are the data used for the current
520 * request.
522 #define NO_TRACK -1
523 #define NEED_1_RECAL -2
524 #define NEED_2_RECAL -3
526 /* */
527 static int usage_count = 0;
530 /* buffer related variables */
531 static int buffer_track = -1;
532 static int buffer_drive = -1;
533 static int buffer_min = -1;
534 static int buffer_max = -1;
536 /* fdc related variables, should end up in a struct */
537 static struct floppy_fdc_state fdc_state[N_FDC];
538 static int fdc; /* current fdc */
540 static struct floppy_struct *_floppy = floppy_type;
541 static unsigned char current_drive = 0;
542 static long current_count_sectors = 0;
543 static unsigned char sector_t; /* sector in track */
544 static unsigned char in_sector_offset; /* offset within physical sector,
545 * expressed in units of 512 bytes */
547 #ifndef fd_eject
548 #define fd_eject(x) -EINVAL
549 #endif
552 #ifdef DEBUGT
553 static long unsigned debugtimer;
554 #endif
557 * Debugging
558 * =========
560 static inline void set_debugt(void)
562 #ifdef DEBUGT
563 debugtimer = jiffies;
564 #endif
567 static inline void debugt(const char *message)
569 #ifdef DEBUGT
570 if (DP->flags & DEBUGT)
571 printk("%s dtime=%lu\n", message, jiffies-debugtimer);
572 #endif
575 typedef void (*timeout_fn)(unsigned long);
576 static struct timer_list fd_timeout ={ NULL, NULL, 0, 0,
577 (timeout_fn) floppy_shutdown };
579 static const char *timeout_message;
581 #ifdef FLOPPY_SANITY_CHECK
582 static void is_alive(const char *message)
584 /* this routine checks whether the floppy driver is "alive" */
585 if (fdc_busy && command_status < 2 && !fd_timeout.prev){
586 DPRINT("timeout handler died: %s\n",message);
589 #endif
591 #ifdef FLOPPY_SANITY_CHECK
593 #define OLOGSIZE 20
595 static void (*lasthandler)(void) = NULL;
596 static int interruptjiffies=0;
597 static int resultjiffies=0;
598 static int resultsize=0;
599 static int lastredo=0;
601 static struct output_log {
602 unsigned char data;
603 unsigned char status;
604 unsigned long jiffies;
605 } output_log[OLOGSIZE];
607 static int output_log_pos=0;
608 #endif
610 #define CURRENTD -1
611 #define MAXTIMEOUT -2
613 static void reschedule_timeout(int drive, const char *message, int marg)
615 if (drive == CURRENTD)
616 drive = current_drive;
617 del_timer(&fd_timeout);
618 if (drive < 0 || drive > N_DRIVE) {
619 fd_timeout.expires = jiffies + 20*HZ;
620 drive=0;
621 } else
622 fd_timeout.expires = jiffies + UDP->timeout;
623 add_timer(&fd_timeout);
624 if (UDP->flags & FD_DEBUG){
625 DPRINT("reschedule timeout ");
626 printk(message, marg);
627 printk("\n");
629 timeout_message = message;
632 static int maximum(int a, int b)
634 if(a > b)
635 return a;
636 else
637 return b;
639 #define INFBOUND(a,b) (a)=maximum((a),(b));
641 static int minimum(int a, int b)
643 if(a < b)
644 return a;
645 else
646 return b;
648 #define SUPBOUND(a,b) (a)=minimum((a),(b));
652 * Bottom half floppy driver.
653 * ==========================
655 * This part of the file contains the code talking directly to the hardware,
656 * and also the main service loop (seek-configure-spinup-command)
660 * disk change.
661 * This routine is responsible for maintaining the FD_DISK_CHANGE flag,
662 * and the last_checked date.
664 * last_checked is the date of the last check which showed 'no disk change'
665 * FD_DISK_CHANGE is set under two conditions:
666 * 1. The floppy has been changed after some i/o to that floppy already
667 * took place.
668 * 2. No floppy disk is in the drive. This is done in order to ensure that
669 * requests are quickly flushed in case there is no disk in the drive. It
670 * follows that FD_DISK_CHANGE can only be cleared if there is a disk in
671 * the drive.
673 * For 1., maxblock is observed. Maxblock is 0 if no i/o has taken place yet.
674 * For 2., FD_DISK_NEWCHANGE is watched. FD_DISK_NEWCHANGE is cleared on
675 * each seek. If a disk is present, the disk change line should also be
676 * cleared on each seek. Thus, if FD_DISK_NEWCHANGE is clear, but the disk
677 * change line is set, this means either that no disk is in the drive, or
678 * that it has been removed since the last seek.
680 * This means that we really have a third possibility too:
681 * The floppy has been changed after the last seek.
684 static int disk_change(int drive)
686 int fdc=FDC(drive);
687 #ifdef FLOPPY_SANITY_CHECK
688 if (jiffies - UDRS->select_date < UDP->select_delay)
689 DPRINT("WARNING disk change called early\n");
690 if (!(FDCS->dor & (0x10 << UNIT(drive))) ||
691 (FDCS->dor & 3) != UNIT(drive) ||
692 fdc != FDC(drive)){
693 DPRINT("probing disk change on unselected drive\n");
694 DPRINT("drive=%d fdc=%d dor=%x\n",drive, FDC(drive),
695 FDCS->dor);
697 #endif
699 #ifdef DCL_DEBUG
700 if (UDP->flags & FD_DEBUG){
701 DPRINT("checking disk change line for drive %d\n",drive);
702 DPRINT("jiffies=%ld\n", jiffies);
703 DPRINT("disk change line=%x\n",fd_inb(FD_DIR)&0x80);
704 DPRINT("flags=%lx\n",UDRS->flags);
706 #endif
707 if (UDP->flags & FD_BROKEN_DCL)
708 return UTESTF(FD_DISK_CHANGED);
709 if ((fd_inb(FD_DIR) ^ UDP->flags) & 0x80){
710 USETF(FD_VERIFY); /* verify write protection */
711 if (UDRS->maxblock){
712 /* mark it changed */
713 USETF(FD_DISK_CHANGED);
716 /* invalidate its geometry */
717 if (UDRS->keep_data >= 0) {
718 if ((UDP->flags & FTD_MSG) &&
719 current_type[drive] != NULL)
720 DPRINT("Disk type is undefined after "
721 "disk change\n");
722 current_type[drive] = NULL;
723 floppy_sizes[TOMINOR(drive)] = MAX_DISK_SIZE;
726 /*USETF(FD_DISK_NEWCHANGE);*/
727 return 1;
728 } else {
729 UDRS->last_checked=jiffies;
730 UCLEARF(FD_DISK_NEWCHANGE);
732 return 0;
735 static inline int is_selected(int dor, int unit)
737 return ((dor & (0x10 << unit)) && (dor &3) == unit);
740 static int set_dor(int fdc, char mask, char data)
742 register unsigned char drive, unit, newdor,olddor;
744 if (FDCS->address == -1)
745 return -1;
747 olddor = FDCS->dor;
748 newdor = (olddor & mask) | data;
749 if (newdor != olddor){
750 unit = olddor & 0x3;
751 if (is_selected(olddor, unit) && !is_selected(newdor,unit)){
752 drive = REVDRIVE(fdc,unit);
753 #ifdef DCL_DEBUG
754 if (UDP->flags & FD_DEBUG){
755 DPRINT("calling disk change from set_dor\n");
757 #endif
758 disk_change(drive);
760 FDCS->dor = newdor;
761 fd_outb(newdor, FD_DOR);
763 unit = newdor & 0x3;
764 if (!is_selected(olddor, unit) && is_selected(newdor,unit)){
765 drive = REVDRIVE(fdc,unit);
766 UDRS->select_date = jiffies;
770 * We should propogate failures to grab the resources back
771 * nicely from here. Actually we ought to rewrite the fd
772 * driver some day too.
774 if (newdor & FLOPPY_MOTOR_MASK)
775 floppy_grab_irq_and_dma();
776 if (olddor & FLOPPY_MOTOR_MASK)
777 floppy_release_irq_and_dma();
778 return olddor;
781 static void twaddle(void)
783 if (DP->select_delay)
784 return;
785 fd_outb(FDCS->dor & ~(0x10<<UNIT(current_drive)),FD_DOR);
786 fd_outb(FDCS->dor, FD_DOR);
787 DRS->select_date = jiffies;
790 /* reset all driver information about the current fdc. This is needed after
791 * a reset, and after a raw command. */
792 static void reset_fdc_info(int mode)
794 int drive;
796 FDCS->spec1 = FDCS->spec2 = -1;
797 FDCS->need_configure = 1;
798 FDCS->perp_mode = 1;
799 FDCS->rawcmd = 0;
800 for (drive = 0; drive < N_DRIVE; drive++)
801 if (FDC(drive) == fdc &&
802 (mode || UDRS->track != NEED_1_RECAL))
803 UDRS->track = NEED_2_RECAL;
806 /* selects the fdc and drive, and enables the fdc's input/dma. */
807 static void set_fdc(int drive)
809 if (drive >= 0 && drive < N_DRIVE){
810 fdc = FDC(drive);
811 current_drive = drive;
813 if (fdc != 1 && fdc != 0) {
814 printk("bad fdc value\n");
815 return;
817 set_dor(fdc,~0,8);
818 #if N_FDC > 1
819 set_dor(1-fdc, ~8, 0);
820 #endif
821 if (FDCS->rawcmd == 2)
822 reset_fdc_info(1);
823 if (fd_inb(FD_STATUS) != STATUS_READY)
824 FDCS->reset = 1;
827 /* locks the driver */
828 static int lock_fdc(int drive, int interruptible)
830 unsigned long flags;
832 if (!usage_count){
833 printk(KERN_ERR "Trying to lock fdc while usage count=0\n");
834 return -1;
836 if(floppy_grab_irq_and_dma()==-1)
837 return -EBUSY;
838 INT_OFF;
839 while (fdc_busy && NO_SIGNAL)
840 interruptible_sleep_on(&fdc_wait);
841 if (fdc_busy){
842 INT_ON;
843 return -EINTR;
845 fdc_busy = 1;
846 INT_ON;
847 command_status = FD_COMMAND_NONE;
848 reschedule_timeout(drive, "lock fdc", 0);
849 set_fdc(drive);
850 return 0;
853 #define LOCK_FDC(drive,interruptible) \
854 if (lock_fdc(drive,interruptible)) return -EINTR;
857 /* unlocks the driver */
858 static inline void unlock_fdc(void)
860 raw_cmd = 0;
861 if (!fdc_busy)
862 DPRINT("FDC access conflict!\n");
864 if (DEVICE_INTR)
865 DPRINT("device interrupt still active at FDC release: %p!\n",
866 DEVICE_INTR);
867 command_status = FD_COMMAND_NONE;
868 del_timer(&fd_timeout);
869 cont = NULL;
870 fdc_busy = 0;
871 floppy_release_irq_and_dma();
872 wake_up(&fdc_wait);
875 /* switches the motor off after a given timeout */
876 static void motor_off_callback(unsigned long nr)
878 unsigned char mask = ~(0x10 << UNIT(nr));
880 set_dor(FDC(nr), mask, 0);
883 static struct timer_list motor_off_timer[N_DRIVE] = {
884 { NULL, NULL, 0, 0, motor_off_callback },
885 { NULL, NULL, 0, 1, motor_off_callback },
886 { NULL, NULL, 0, 2, motor_off_callback },
887 { NULL, NULL, 0, 3, motor_off_callback },
888 { NULL, NULL, 0, 4, motor_off_callback },
889 { NULL, NULL, 0, 5, motor_off_callback },
890 { NULL, NULL, 0, 6, motor_off_callback },
891 { NULL, NULL, 0, 7, motor_off_callback }
894 /* schedules motor off */
895 static void floppy_off(unsigned int drive)
897 unsigned long volatile delta;
898 register int fdc=FDC(drive);
900 if (!(FDCS->dor & (0x10 << UNIT(drive))))
901 return;
903 del_timer(motor_off_timer+drive);
905 /* make spindle stop in a position which minimizes spinup time
906 * next time */
907 if (UDP->rps){
908 delta = jiffies - UDRS->first_read_date + HZ -
909 UDP->spindown_offset;
910 delta = ((delta * UDP->rps) % HZ) / UDP->rps;
911 motor_off_timer[drive].expires = jiffies + UDP->spindown - delta;
913 add_timer(motor_off_timer+drive);
917 * cycle through all N_DRIVE floppy drives, for disk change testing.
918 * stopping at current drive. This is done before any long operation, to
919 * be sure to have up to date disk change information.
921 static void scandrives(void)
923 int i, drive, saved_drive;
925 if (DP->select_delay)
926 return;
928 saved_drive = current_drive;
929 for (i=0; i < N_DRIVE; i++){
930 drive = (saved_drive + i + 1) % N_DRIVE;
931 if (UDRS->fd_ref == 0 || UDP->select_delay != 0)
932 continue; /* skip closed drives */
933 set_fdc(drive);
934 if (!(set_dor(fdc, ~3, UNIT(drive) | (0x10 << UNIT(drive))) &
935 (0x10 << UNIT(drive))))
936 /* switch the motor off again, if it was off to
937 * begin with */
938 set_dor(fdc, ~(0x10 << UNIT(drive)), 0);
940 set_fdc(saved_drive);
943 static void empty(void)
947 static struct tq_struct floppy_tq =
948 { 0, 0, 0, 0 };
950 static void schedule_bh( void (*handler)(void*) )
952 floppy_tq.routine = (void *)(void *) handler;
953 queue_task(&floppy_tq, &tq_immediate);
954 mark_bh(IMMEDIATE_BH);
957 static struct timer_list fd_timer ={ NULL, NULL, 0, 0, 0 };
959 static void cancel_activity(void)
961 CLEAR_INTR;
962 floppy_tq.routine = (void *)(void *) empty;
963 del_timer(&fd_timer);
966 /* this function makes sure that the disk stays in the drive during the
967 * transfer */
968 static void fd_watchdog(void)
970 #ifdef DCL_DEBUG
971 if (DP->flags & FD_DEBUG){
972 DPRINT("calling disk change from watchdog\n");
974 #endif
976 if (disk_change(current_drive)){
977 DPRINT("disk removed during i/o\n");
978 cancel_activity();
979 cont->done(0);
980 reset_fdc();
981 } else {
982 del_timer(&fd_timer);
983 fd_timer.function = (timeout_fn) fd_watchdog;
984 fd_timer.expires = jiffies + HZ / 10;
985 add_timer(&fd_timer);
989 static void main_command_interrupt(void)
991 del_timer(&fd_timer);
992 cont->interrupt();
995 /* waits for a delay (spinup or select) to pass */
996 static int wait_for_completion(int delay, timeout_fn function)
998 if (FDCS->reset){
999 reset_fdc(); /* do the reset during sleep to win time
1000 * if we don't need to sleep, it's a good
1001 * occasion anyways */
1002 return 1;
1005 if ((signed) (jiffies - delay) < 0){
1006 del_timer(&fd_timer);
1007 fd_timer.function = function;
1008 fd_timer.expires = delay;
1009 add_timer(&fd_timer);
1010 return 1;
1012 return 0;
1015 static int hlt_disabled=0;
1016 static void floppy_disable_hlt(void)
1018 unsigned long flags;
1020 INT_OFF;
1021 if (!hlt_disabled){
1022 hlt_disabled=1;
1023 #ifdef HAVE_DISABLE_HLT
1024 disable_hlt();
1025 #endif
1027 INT_ON;
1030 static void floppy_enable_hlt(void)
1032 unsigned long flags;
1034 INT_OFF;
1035 if (hlt_disabled){
1036 hlt_disabled=0;
1037 #ifdef HAVE_DISABLE_HLT
1038 enable_hlt();
1039 #endif
1041 INT_ON;
1045 static void setup_DMA(void)
1047 unsigned long flags;
1049 #ifdef FLOPPY_SANITY_CHECK
1050 if (raw_cmd->length == 0){
1051 int i;
1053 printk("zero dma transfer size:");
1054 for (i=0; i < raw_cmd->cmd_count; i++)
1055 printk("%x,", raw_cmd->cmd[i]);
1056 printk("\n");
1057 cont->done(0);
1058 FDCS->reset = 1;
1059 return;
1061 if (((unsigned long) raw_cmd->kernel_data) % 512){
1062 printk("non aligned address: %p\n", raw_cmd->kernel_data);
1063 cont->done(0);
1064 FDCS->reset=1;
1065 return;
1067 #endif
1068 INT_OFF;
1069 fd_disable_dma();
1070 #ifdef fd_dma_setup
1071 if(fd_dma_setup(raw_cmd->kernel_data, raw_cmd->length,
1072 (raw_cmd->flags & FD_RAW_READ)?
1073 DMA_MODE_READ : DMA_MODE_WRITE,
1074 FDCS->address) < 0) {
1075 INT_ON;
1076 cont->done(0);
1077 FDCS->reset=1;
1078 return;
1080 #else
1081 fd_clear_dma_ff();
1082 fd_cacheflush(raw_cmd->kernel_data, raw_cmd->length);
1083 fd_set_dma_mode((raw_cmd->flags & FD_RAW_READ)?
1084 DMA_MODE_READ : DMA_MODE_WRITE);
1085 fd_set_dma_addr(raw_cmd->kernel_data);
1086 fd_set_dma_count(raw_cmd->length);
1087 virtual_dma_port = FDCS->address;
1088 fd_enable_dma();
1089 #endif
1090 INT_ON;
1091 floppy_disable_hlt();
1094 static void show_floppy(void);
1096 /* waits until the fdc becomes ready */
1097 static int wait_til_ready(void)
1099 int counter, status;
1100 if(FDCS->reset)
1101 return -1;
1102 for (counter = 0; counter < 10000; counter++) {
1103 status = fd_inb(FD_STATUS);
1104 if (status & STATUS_READY)
1105 return status;
1107 if (!initialising) {
1108 DPRINT("Getstatus times out (%x) on fdc %d\n",
1109 status, fdc);
1110 show_floppy();
1112 FDCS->reset = 1;
1113 return -1;
1116 /* sends a command byte to the fdc */
1117 static int output_byte(char byte)
1119 int status;
1121 if ((status = wait_til_ready()) < 0)
1122 return -1;
1123 if ((status & (STATUS_READY|STATUS_DIR|STATUS_DMA)) == STATUS_READY){
1124 fd_outb(byte,FD_DATA);
1125 #ifdef FLOPPY_SANITY_CHECK
1126 output_log[output_log_pos].data = byte;
1127 output_log[output_log_pos].status = status;
1128 output_log[output_log_pos].jiffies = jiffies;
1129 output_log_pos = (output_log_pos + 1) % OLOGSIZE;
1130 #endif
1131 return 0;
1133 FDCS->reset = 1;
1134 if (!initialising) {
1135 DPRINT("Unable to send byte %x to FDC. Fdc=%x Status=%x\n",
1136 byte, fdc, status);
1137 show_floppy();
1139 return -1;
1141 #define LAST_OUT(x) if (output_byte(x)<0){ reset_fdc();return;}
1143 /* gets the response from the fdc */
1144 static int result(void)
1146 int i, status;
1148 for(i=0; i < MAX_REPLIES; i++) {
1149 if ((status = wait_til_ready()) < 0)
1150 break;
1151 status &= STATUS_DIR|STATUS_READY|STATUS_BUSY|STATUS_DMA;
1152 if ((status & ~STATUS_BUSY) == STATUS_READY){
1153 #ifdef FLOPPY_SANITY_CHECK
1154 resultjiffies = jiffies;
1155 resultsize = i;
1156 #endif
1157 return i;
1159 if (status == (STATUS_DIR|STATUS_READY|STATUS_BUSY))
1160 reply_buffer[i] = fd_inb(FD_DATA);
1161 else
1162 break;
1164 if(!initialising) {
1165 DPRINT("get result error. Fdc=%d Last status=%x Read bytes=%d\n",
1166 fdc, status, i);
1167 show_floppy();
1169 FDCS->reset = 1;
1170 return -1;
1173 #define MORE_OUTPUT -2
1174 /* does the fdc need more output? */
1175 static int need_more_output(void)
1177 int status;
1178 if( (status = wait_til_ready()) < 0)
1179 return -1;
1180 if ((status & (STATUS_READY|STATUS_DIR|STATUS_DMA)) == STATUS_READY)
1181 return MORE_OUTPUT;
1182 return result();
1185 /* Set perpendicular mode as required, based on data rate, if supported.
1186 * 82077 Now tested. 1Mbps data rate only possible with 82077-1.
1188 static inline void perpendicular_mode(void)
1190 unsigned char perp_mode;
1192 if (raw_cmd->rate & 0x40){
1193 switch(raw_cmd->rate & 3){
1194 case 0:
1195 perp_mode=2;
1196 break;
1197 case 3:
1198 perp_mode=3;
1199 break;
1200 default:
1201 DPRINT("Invalid data rate for perpendicular mode!\n");
1202 cont->done(0);
1203 FDCS->reset = 1; /* convenient way to return to
1204 * redo without to much hassle (deep
1205 * stack et al. */
1206 return;
1208 } else
1209 perp_mode = 0;
1211 if (FDCS->perp_mode == perp_mode)
1212 return;
1213 if (FDCS->version >= FDC_82077_ORIG) {
1214 output_byte(FD_PERPENDICULAR);
1215 output_byte(perp_mode);
1216 FDCS->perp_mode = perp_mode;
1217 } else if (perp_mode) {
1218 DPRINT("perpendicular mode not supported by this FDC.\n");
1220 } /* perpendicular_mode */
1222 static int fifo_depth = 0xa;
1223 static int no_fifo = 0;
1225 static int fdc_configure(void)
1227 /* Turn on FIFO */
1228 output_byte(FD_CONFIGURE);
1229 if(need_more_output() != MORE_OUTPUT)
1230 return 0;
1231 output_byte(0);
1232 output_byte(0x10 | (no_fifo & 0x20) | (fifo_depth & 0xf));
1233 output_byte(0); /* pre-compensation from track
1234 0 upwards */
1235 return 1;
1238 #define NOMINAL_DTR 500
1240 /* Issue a "SPECIFY" command to set the step rate time, head unload time,
1241 * head load time, and DMA disable flag to values needed by floppy.
1243 * The value "dtr" is the data transfer rate in Kbps. It is needed
1244 * to account for the data rate-based scaling done by the 82072 and 82077
1245 * FDC types. This parameter is ignored for other types of FDCs (i.e.
1246 * 8272a).
1248 * Note that changing the data transfer rate has a (probably deleterious)
1249 * effect on the parameters subject to scaling for 82072/82077 FDCs, so
1250 * fdc_specify is called again after each data transfer rate
1251 * change.
1253 * srt: 1000 to 16000 in microseconds
1254 * hut: 16 to 240 milliseconds
1255 * hlt: 2 to 254 milliseconds
1257 * These values are rounded up to the next highest available delay time.
1259 static void fdc_specify(void)
1261 unsigned char spec1, spec2;
1262 int srt, hlt, hut;
1263 unsigned long dtr = NOMINAL_DTR;
1264 unsigned long scale_dtr = NOMINAL_DTR;
1265 int hlt_max_code = 0x7f;
1266 int hut_max_code = 0xf;
1268 if (FDCS->need_configure && FDCS->version >= FDC_82072A) {
1269 fdc_configure();
1270 FDCS->need_configure = 0;
1271 /*DPRINT("FIFO enabled\n");*/
1274 switch (raw_cmd->rate & 0x03) {
1275 case 3:
1276 dtr = 1000;
1277 break;
1278 case 1:
1279 dtr = 300;
1280 if (FDCS->version >= FDC_82078) {
1281 /* chose the default rate table, not the one
1282 * where 1 = 2 Mbps */
1283 output_byte(FD_DRIVESPEC);
1284 if(need_more_output() == MORE_OUTPUT) {
1285 output_byte(UNIT(current_drive));
1286 output_byte(0xc0);
1289 break;
1290 case 2:
1291 dtr = 250;
1292 break;
1295 if (FDCS->version >= FDC_82072) {
1296 scale_dtr = dtr;
1297 hlt_max_code = 0x00; /* 0==256msec*dtr0/dtr (not linear!) */
1298 hut_max_code = 0x0; /* 0==256msec*dtr0/dtr (not linear!) */
1301 /* Convert step rate from microseconds to milliseconds and 4 bits */
1302 srt = 16 - (DP->srt*scale_dtr/1000 + NOMINAL_DTR - 1)/NOMINAL_DTR;
1303 SUPBOUND(srt, 0xf);
1304 INFBOUND(srt, 0);
1306 hlt = (DP->hlt*scale_dtr/2 + NOMINAL_DTR - 1)/NOMINAL_DTR;
1307 if (hlt < 0x01)
1308 hlt = 0x01;
1309 else if (hlt > 0x7f)
1310 hlt = hlt_max_code;
1312 hut = (DP->hut*scale_dtr/16 + NOMINAL_DTR - 1)/NOMINAL_DTR;
1313 if (hut < 0x1)
1314 hut = 0x1;
1315 else if (hut > 0xf)
1316 hut = hut_max_code;
1318 spec1 = (srt << 4) | hut;
1319 spec2 = (hlt << 1) | (use_virtual_dma & 1);
1321 /* If these parameters did not change, just return with success */
1322 if (FDCS->spec1 != spec1 || FDCS->spec2 != spec2) {
1323 /* Go ahead and set spec1 and spec2 */
1324 output_byte(FD_SPECIFY);
1325 output_byte(FDCS->spec1 = spec1);
1326 output_byte(FDCS->spec2 = spec2);
1328 } /* fdc_specify */
1330 /* Set the FDC's data transfer rate on behalf of the specified drive.
1331 * NOTE: with 82072/82077 FDCs, changing the data rate requires a reissue
1332 * of the specify command (i.e. using the fdc_specify function).
1334 static int fdc_dtr(void)
1336 /* If data rate not already set to desired value, set it. */
1337 if ((raw_cmd->rate & 3) == FDCS->dtr)
1338 return 0;
1340 /* Set dtr */
1341 fd_outb(raw_cmd->rate & 3, FD_DCR);
1343 /* TODO: some FDC/drive combinations (C&T 82C711 with TEAC 1.2MB)
1344 * need a stabilization period of several milliseconds to be
1345 * enforced after data rate changes before R/W operations.
1346 * Pause 5 msec to avoid trouble. (Needs to be 2 jiffies)
1348 FDCS->dtr = raw_cmd->rate & 3;
1349 return(wait_for_completion(jiffies+2*HZ/100,
1350 (timeout_fn) floppy_ready));
1351 } /* fdc_dtr */
1353 static void tell_sector(void)
1355 printk(": track %d, head %d, sector %d, size %d",
1356 R_TRACK, R_HEAD, R_SECTOR, R_SIZECODE);
1357 } /* tell_sector */
1361 * OK, this error interpreting routine is called after a
1362 * DMA read/write has succeeded
1363 * or failed, so we check the results, and copy any buffers.
1364 * hhb: Added better error reporting.
1365 * ak: Made this into a separate routine.
1367 static int interpret_errors(void)
1369 char bad;
1371 if (inr!=7) {
1372 DPRINT("-- FDC reply error");
1373 FDCS->reset = 1;
1374 return 1;
1377 /* check IC to find cause of interrupt */
1378 switch (ST0 & ST0_INTR) {
1379 case 0x40: /* error occurred during command execution */
1380 if (ST1 & ST1_EOC)
1381 return 0; /* occurs with pseudo-DMA */
1382 bad = 1;
1383 if (ST1 & ST1_WP) {
1384 DPRINT("Drive is write protected\n");
1385 CLEARF(FD_DISK_WRITABLE);
1386 cont->done(0);
1387 bad = 2;
1388 } else if (ST1 & ST1_ND) {
1389 SETF(FD_NEED_TWADDLE);
1390 } else if (ST1 & ST1_OR) {
1391 if (DP->flags & FTD_MSG)
1392 DPRINT("Over/Underrun - retrying\n");
1393 bad = 0;
1394 }else if (*errors >= DP->max_errors.reporting){
1395 DPRINT("");
1396 if (ST0 & ST0_ECE) {
1397 printk("Recalibrate failed!");
1398 } else if (ST2 & ST2_CRC) {
1399 printk("data CRC error");
1400 tell_sector();
1401 } else if (ST1 & ST1_CRC) {
1402 printk("CRC error");
1403 tell_sector();
1404 } else if ((ST1 & (ST1_MAM|ST1_ND)) || (ST2 & ST2_MAM)) {
1405 if (!probing) {
1406 printk("sector not found");
1407 tell_sector();
1408 } else
1409 printk("probe failed...");
1410 } else if (ST2 & ST2_WC) { /* seek error */
1411 printk("wrong cylinder");
1412 } else if (ST2 & ST2_BC) { /* cylinder marked as bad */
1413 printk("bad cylinder");
1414 } else {
1415 printk("unknown error. ST[0..2] are: 0x%x 0x%x 0x%x", ST0, ST1, ST2);
1416 tell_sector();
1418 printk("\n");
1421 if (ST2 & ST2_WC || ST2 & ST2_BC)
1422 /* wrong cylinder => recal */
1423 DRS->track = NEED_2_RECAL;
1424 return bad;
1425 case 0x80: /* invalid command given */
1426 DPRINT("Invalid FDC command given!\n");
1427 cont->done(0);
1428 return 2;
1429 case 0xc0:
1430 DPRINT("Abnormal termination caused by polling\n");
1431 cont->error();
1432 return 2;
1433 default: /* (0) Normal command termination */
1434 return 0;
1439 * This routine is called when everything should be correctly set up
1440 * for the transfer (i.e. floppy motor is on, the correct floppy is
1441 * selected, and the head is sitting on the right track).
1443 static void setup_rw_floppy(void)
1445 int i,ready_date,r, flags,dflags;
1446 timeout_fn function;
1448 flags = raw_cmd->flags;
1449 if (flags & (FD_RAW_READ | FD_RAW_WRITE))
1450 flags |= FD_RAW_INTR;
1452 if ((flags & FD_RAW_SPIN) && !(flags & FD_RAW_NO_MOTOR)){
1453 ready_date = DRS->spinup_date + DP->spinup;
1454 /* If spinup will take a long time, rerun scandrives
1455 * again just before spinup completion. Beware that
1456 * after scandrives, we must again wait for selection.
1458 if ((signed) (ready_date - jiffies) > DP->select_delay){
1459 ready_date -= DP->select_delay;
1460 function = (timeout_fn) floppy_start;
1461 } else
1462 function = (timeout_fn) setup_rw_floppy;
1464 /* wait until the floppy is spinning fast enough */
1465 if (wait_for_completion(ready_date,function))
1466 return;
1468 dflags = DRS->flags;
1470 if ((flags & FD_RAW_READ) || (flags & FD_RAW_WRITE))
1471 setup_DMA();
1473 if (flags & FD_RAW_INTR)
1474 SET_INTR(main_command_interrupt);
1476 r=0;
1477 for (i=0; i< raw_cmd->cmd_count; i++)
1478 r|=output_byte(raw_cmd->cmd[i]);
1480 #ifdef DEBUGT
1481 debugt("rw_command: ");
1482 #endif
1483 if (r){
1484 cont->error();
1485 reset_fdc();
1486 return;
1489 if (!(flags & FD_RAW_INTR)){
1490 inr = result();
1491 cont->interrupt();
1492 } else if (flags & FD_RAW_NEED_DISK)
1493 fd_watchdog();
1496 static int blind_seek;
1499 * This is the routine called after every seek (or recalibrate) interrupt
1500 * from the floppy controller.
1502 static void seek_interrupt(void)
1504 #ifdef DEBUGT
1505 debugt("seek interrupt:");
1506 #endif
1507 if (inr != 2 || (ST0 & 0xF8) != 0x20) {
1508 DPRINT("seek failed\n");
1509 DRS->track = NEED_2_RECAL;
1510 cont->error();
1511 cont->redo();
1512 return;
1514 if (DRS->track >= 0 && DRS->track != ST1 && !blind_seek){
1515 #ifdef DCL_DEBUG
1516 if (DP->flags & FD_DEBUG){
1517 DPRINT("clearing NEWCHANGE flag because of effective seek\n");
1518 DPRINT("jiffies=%ld\n", jiffies);
1520 #endif
1521 CLEARF(FD_DISK_NEWCHANGE); /* effective seek */
1522 DRS->select_date = jiffies;
1524 DRS->track = ST1;
1525 floppy_ready();
1528 static void check_wp(void)
1530 if (TESTF(FD_VERIFY)) {
1531 /* check write protection */
1532 output_byte(FD_GETSTATUS);
1533 output_byte(UNIT(current_drive));
1534 if (result() != 1){
1535 FDCS->reset = 1;
1536 return;
1538 CLEARF(FD_VERIFY);
1539 CLEARF(FD_NEED_TWADDLE);
1540 #ifdef DCL_DEBUG
1541 if (DP->flags & FD_DEBUG){
1542 DPRINT("checking whether disk is write protected\n");
1543 DPRINT("wp=%x\n",ST3 & 0x40);
1545 #endif
1546 if (!(ST3 & 0x40))
1547 SETF(FD_DISK_WRITABLE);
1548 else
1549 CLEARF(FD_DISK_WRITABLE);
1553 static void seek_floppy(void)
1555 int track;
1557 blind_seek=0;
1559 #ifdef DCL_DEBUG
1560 if (DP->flags & FD_DEBUG){
1561 DPRINT("calling disk change from seek\n");
1563 #endif
1565 if (!TESTF(FD_DISK_NEWCHANGE) &&
1566 disk_change(current_drive) &&
1567 (raw_cmd->flags & FD_RAW_NEED_DISK)){
1568 /* the media changed flag should be cleared after the seek.
1569 * If it isn't, this means that there is really no disk in
1570 * the drive.
1572 SETF(FD_DISK_CHANGED);
1573 cont->done(0);
1574 cont->redo();
1575 return;
1577 if (DRS->track <= NEED_1_RECAL){
1578 recalibrate_floppy();
1579 return;
1580 } else if (TESTF(FD_DISK_NEWCHANGE) &&
1581 (raw_cmd->flags & FD_RAW_NEED_DISK) &&
1582 (DRS->track <= NO_TRACK || DRS->track == raw_cmd->track)) {
1583 /* we seek to clear the media-changed condition. Does anybody
1584 * know a more elegant way, which works on all drives? */
1585 if (raw_cmd->track)
1586 track = raw_cmd->track - 1;
1587 else {
1588 if (DP->flags & FD_SILENT_DCL_CLEAR){
1589 set_dor(fdc, ~(0x10 << UNIT(current_drive)), 0);
1590 blind_seek = 1;
1591 raw_cmd->flags |= FD_RAW_NEED_SEEK;
1593 track = 1;
1595 } else {
1596 check_wp();
1597 if (raw_cmd->track != DRS->track &&
1598 (raw_cmd->flags & FD_RAW_NEED_SEEK))
1599 track = raw_cmd->track;
1600 else {
1601 setup_rw_floppy();
1602 return;
1606 SET_INTR(seek_interrupt);
1607 output_byte(FD_SEEK);
1608 output_byte(UNIT(current_drive));
1609 LAST_OUT(track);
1610 #ifdef DEBUGT
1611 debugt("seek command:");
1612 #endif
1615 static void recal_interrupt(void)
1617 #ifdef DEBUGT
1618 debugt("recal interrupt:");
1619 #endif
1620 if (inr !=2)
1621 FDCS->reset = 1;
1622 else if (ST0 & ST0_ECE) {
1623 switch(DRS->track){
1624 case NEED_1_RECAL:
1625 #ifdef DEBUGT
1626 debugt("recal interrupt need 1 recal:");
1627 #endif
1628 /* after a second recalibrate, we still haven't
1629 * reached track 0. Probably no drive. Raise an
1630 * error, as failing immediately might upset
1631 * computers possessed by the Devil :-) */
1632 cont->error();
1633 cont->redo();
1634 return;
1635 case NEED_2_RECAL:
1636 #ifdef DEBUGT
1637 debugt("recal interrupt need 2 recal:");
1638 #endif
1639 /* If we already did a recalibrate,
1640 * and we are not at track 0, this
1641 * means we have moved. (The only way
1642 * not to move at recalibration is to
1643 * be already at track 0.) Clear the
1644 * new change flag */
1645 #ifdef DCL_DEBUG
1646 if (DP->flags & FD_DEBUG){
1647 DPRINT("clearing NEWCHANGE flag because of second recalibrate\n");
1649 #endif
1651 CLEARF(FD_DISK_NEWCHANGE);
1652 DRS->select_date = jiffies;
1653 /* fall through */
1654 default:
1655 #ifdef DEBUGT
1656 debugt("recal interrupt default:");
1657 #endif
1658 /* Recalibrate moves the head by at
1659 * most 80 steps. If after one
1660 * recalibrate we don't have reached
1661 * track 0, this might mean that we
1662 * started beyond track 80. Try
1663 * again. */
1664 DRS->track = NEED_1_RECAL;
1665 break;
1667 } else
1668 DRS->track = ST1;
1669 floppy_ready();
1672 static void print_result(char *message, int inr)
1674 int i;
1676 DPRINT("%s ", message);
1677 if (inr >= 0)
1678 for (i=0; i<inr; i++)
1679 printk("repl[%d]=%x ", i, reply_buffer[i]);
1680 printk("\n");
1683 /* interrupt handler. Note that this can be called externally on the Sparc */
1684 void floppy_interrupt(int irq, void *dev_id, struct pt_regs * regs)
1686 void (*handler)(void) = DEVICE_INTR;
1687 int do_print;
1689 lasthandler = handler;
1690 interruptjiffies = jiffies;
1692 fd_disable_dma();
1693 floppy_enable_hlt();
1694 CLEAR_INTR;
1695 if (fdc >= N_FDC || FDCS->address == -1){
1696 /* we don't even know which FDC is the culprit */
1697 printk("DOR0=%x\n", fdc_state[0].dor);
1698 printk("floppy interrupt on bizarre fdc %d\n",fdc);
1699 printk("handler=%p\n", handler);
1700 is_alive("bizarre fdc");
1701 return;
1704 FDCS->reset = 0;
1705 /* We have to clear the reset flag here, because apparently on boxes
1706 * with level triggered interrupts (PS/2, Sparc, ...), it is needed to
1707 * emit SENSEI's to clear the interrupt line. And FDCS->reset blocks the
1708 * emission of the SENSEI's.
1709 * It is OK to emit floppy commands because we are in an interrupt
1710 * handler here, and thus we have to fear no interference of other
1711 * activity.
1714 do_print = !handler && print_unex && !initialising;
1716 inr = result();
1717 if(do_print)
1718 print_result("unexpected interrupt", inr);
1719 if (inr == 0){
1720 do {
1721 output_byte(FD_SENSEI);
1722 inr = result();
1723 if(do_print)
1724 print_result("sensei", inr);
1725 } while ((ST0 & 0x83) != UNIT(current_drive) && inr == 2);
1727 if (handler) {
1728 int cpu = smp_processor_id();
1729 if(softirq_trylock(cpu)) {
1730 /* got the lock, call the handler immediately */
1731 handler();
1732 softirq_endlock(cpu);
1733 } else
1734 /* we interrupted a bottom half. Defer handler */
1735 schedule_bh( (void *)(void *) handler);
1736 } else
1737 FDCS->reset = 1;
1738 is_alive("normal interrupt end");
1741 static void recalibrate_floppy(void)
1743 #ifdef DEBUGT
1744 debugt("recalibrate floppy:");
1745 #endif
1746 SET_INTR(recal_interrupt);
1747 output_byte(FD_RECALIBRATE);
1748 LAST_OUT(UNIT(current_drive));
1752 * Must do 4 FD_SENSEIs after reset because of ``drive polling''.
1754 static void reset_interrupt(void)
1756 #ifdef DEBUGT
1757 debugt("reset interrupt:");
1758 #endif
1759 result(); /* get the status ready for set_fdc */
1760 if (FDCS->reset) {
1761 printk("reset set in interrupt, calling %p\n", cont->error);
1762 cont->error(); /* a reset just after a reset. BAD! */
1764 cont->redo();
1768 * reset is done by pulling bit 2 of DOR low for a while (old FDCs),
1769 * or by setting the self clearing bit 7 of STATUS (newer FDCs)
1771 static void reset_fdc(void)
1773 SET_INTR(reset_interrupt);
1774 FDCS->reset = 0;
1775 reset_fdc_info(0);
1777 /* Pseudo-DMA may intercept 'reset finished' interrupt. */
1778 /* Irrelevant for systems with true DMA (i386). */
1779 fd_disable_dma();
1781 if (FDCS->version >= FDC_82072A)
1782 fd_outb(0x80 | (FDCS->dtr &3), FD_STATUS);
1783 else {
1784 fd_outb(FDCS->dor & ~0x04, FD_DOR);
1785 udelay(FD_RESET_DELAY);
1786 fd_outb(FDCS->dor, FD_DOR);
1790 static void show_floppy(void)
1792 int i;
1794 printk("\n");
1795 printk("floppy driver state\n");
1796 printk("-------------------\n");
1797 printk("now=%ld last interrupt=%d last called handler=%p\n",
1798 jiffies, interruptjiffies, lasthandler);
1801 #ifdef FLOPPY_SANITY_CHECK
1802 printk("timeout_message=%s\n", timeout_message);
1803 printk("last output bytes:\n");
1804 for (i=0; i < OLOGSIZE; i++)
1805 printk("%2x %2x %ld\n",
1806 output_log[(i+output_log_pos) % OLOGSIZE].data,
1807 output_log[(i+output_log_pos) % OLOGSIZE].status,
1808 output_log[(i+output_log_pos) % OLOGSIZE].jiffies);
1809 printk("last result at %d\n", resultjiffies);
1810 printk("last redo_fd_request at %d\n", lastredo);
1811 for (i=0; i<resultsize; i++){
1812 printk("%2x ", reply_buffer[i]);
1814 printk("\n");
1815 #endif
1817 printk("status=%x\n", fd_inb(FD_STATUS));
1818 printk("fdc_busy=%d\n", fdc_busy);
1819 if (DEVICE_INTR)
1820 printk("DEVICE_INTR=%p\n", DEVICE_INTR);
1821 if (floppy_tq.sync)
1822 printk("floppy_tq.routine=%p\n", floppy_tq.routine);
1823 if (fd_timer.prev)
1824 printk("fd_timer.function=%p\n", fd_timer.function);
1825 if (fd_timeout.prev){
1826 printk("timer_table=%p\n",fd_timeout.function);
1827 printk("expires=%ld\n",fd_timeout.expires-jiffies);
1828 printk("now=%ld\n",jiffies);
1830 printk("cont=%p\n", cont);
1831 printk("CURRENT=%p\n", CURRENT);
1832 printk("command_status=%d\n", command_status);
1833 printk("\n");
1836 static void floppy_shutdown(void)
1838 if (!initialising)
1839 show_floppy();
1840 cancel_activity();
1842 floppy_enable_hlt();
1843 fd_disable_dma();
1844 /* avoid dma going to a random drive after shutdown */
1846 if (!initialising)
1847 DPRINT("floppy timeout called\n");
1848 FDCS->reset = 1;
1849 if (cont){
1850 cont->done(0);
1851 cont->redo(); /* this will recall reset when needed */
1852 } else {
1853 printk("no cont in shutdown!\n");
1854 process_fd_request();
1856 is_alive("floppy shutdown");
1858 /*typedef void (*timeout_fn)(unsigned long);*/
1860 /* start motor, check media-changed condition and write protection */
1861 static int start_motor(void (*function)(void) )
1863 int mask, data;
1865 mask = 0xfc;
1866 data = UNIT(current_drive);
1867 if (!(raw_cmd->flags & FD_RAW_NO_MOTOR)){
1868 if (!(FDCS->dor & (0x10 << UNIT(current_drive)))){
1869 set_debugt();
1870 /* no read since this drive is running */
1871 DRS->first_read_date = 0;
1872 /* note motor start time if motor is not yet running */
1873 DRS->spinup_date = jiffies;
1874 data |= (0x10 << UNIT(current_drive));
1876 } else
1877 if (FDCS->dor & (0x10 << UNIT(current_drive)))
1878 mask &= ~(0x10 << UNIT(current_drive));
1880 /* starts motor and selects floppy */
1881 del_timer(motor_off_timer + current_drive);
1882 set_dor(fdc, mask, data);
1884 /* wait_for_completion also schedules reset if needed. */
1885 return(wait_for_completion(DRS->select_date+DP->select_delay,
1886 (timeout_fn) function));
1889 static void floppy_ready(void)
1891 CHECK_RESET;
1892 if (start_motor(floppy_ready)) return;
1893 if (fdc_dtr()) return;
1895 #ifdef DCL_DEBUG
1896 if (DP->flags & FD_DEBUG){
1897 DPRINT("calling disk change from floppy_ready\n");
1899 #endif
1900 if (!(raw_cmd->flags & FD_RAW_NO_MOTOR) &&
1901 disk_change(current_drive) &&
1902 !DP->select_delay)
1903 twaddle(); /* this clears the dcl on certain drive/controller
1904 * combinations */
1906 #ifdef fd_chose_dma_mode
1907 if ((raw_cmd->flags & FD_RAW_READ) ||
1908 (raw_cmd->flags & FD_RAW_WRITE))
1909 fd_chose_dma_mode(raw_cmd->kernel_data,
1910 raw_cmd->length);
1911 #endif
1913 if (raw_cmd->flags & (FD_RAW_NEED_SEEK | FD_RAW_NEED_DISK)){
1914 perpendicular_mode();
1915 fdc_specify(); /* must be done here because of hut, hlt ... */
1916 seek_floppy();
1917 } else {
1918 if ((raw_cmd->flags & FD_RAW_READ) ||
1919 (raw_cmd->flags & FD_RAW_WRITE))
1920 fdc_specify();
1921 setup_rw_floppy();
1925 static void floppy_start(void)
1927 reschedule_timeout(CURRENTD, "floppy start", 0);
1929 scandrives();
1930 #ifdef DCL_DEBUG
1931 if (DP->flags & FD_DEBUG){
1932 DPRINT("setting NEWCHANGE in floppy_start\n");
1934 #endif
1935 SETF(FD_DISK_NEWCHANGE);
1936 floppy_ready();
1940 * ========================================================================
1941 * here ends the bottom half. Exported routines are:
1942 * floppy_start, floppy_off, floppy_ready, lock_fdc, unlock_fdc, set_fdc,
1943 * start_motor, reset_fdc, reset_fdc_info, interpret_errors.
1944 * Initialization also uses output_byte, result, set_dor, floppy_interrupt
1945 * and set_dor.
1946 * ========================================================================
1949 * General purpose continuations.
1950 * ==============================
1953 static void do_wakeup(void)
1955 reschedule_timeout(MAXTIMEOUT, "do wakeup", 0);
1956 cont = 0;
1957 command_status += 2;
1958 wake_up(&command_done);
1961 static struct cont_t wakeup_cont={
1962 empty,
1963 do_wakeup,
1964 empty,
1965 (done_f)empty
1969 static struct cont_t intr_cont={
1970 empty,
1971 process_fd_request,
1972 empty,
1973 (done_f) empty
1976 static int wait_til_done(void (*handler)(void), int interruptible)
1978 int ret;
1979 unsigned long flags;
1981 schedule_bh((void *)(void *)handler);
1982 INT_OFF;
1983 while(command_status < 2 && NO_SIGNAL){
1984 is_alive("wait_til_done");
1985 if (interruptible)
1986 interruptible_sleep_on(&command_done);
1987 else
1988 sleep_on(&command_done);
1990 if (command_status < 2){
1991 cancel_activity();
1992 cont = &intr_cont;
1993 reset_fdc();
1994 INT_ON;
1995 return -EINTR;
1997 INT_ON;
1999 if (FDCS->reset)
2000 command_status = FD_COMMAND_ERROR;
2001 if (command_status == FD_COMMAND_OKAY)
2002 ret=0;
2003 else
2004 ret=-EIO;
2005 command_status = FD_COMMAND_NONE;
2006 return ret;
2009 static void generic_done(int result)
2011 command_status = result;
2012 cont = &wakeup_cont;
2015 static void generic_success(void)
2017 cont->done(1);
2020 static void generic_failure(void)
2022 cont->done(0);
2025 static void success_and_wakeup(void)
2027 generic_success();
2028 cont->redo();
2033 * formatting and rw support.
2034 * ==========================
2037 static int next_valid_format(void)
2039 int probed_format;
2041 probed_format = DRS->probed_format;
2042 while(1){
2043 if (probed_format >= 8 ||
2044 !DP->autodetect[probed_format]){
2045 DRS->probed_format = 0;
2046 return 1;
2048 if (floppy_type[DP->autodetect[probed_format]].sect){
2049 DRS->probed_format = probed_format;
2050 return 0;
2052 probed_format++;
2056 static void bad_flp_intr(void)
2058 if (probing){
2059 DRS->probed_format++;
2060 if (!next_valid_format())
2061 return;
2063 (*errors)++;
2064 INFBOUND(DRWE->badness, *errors);
2065 if (*errors > DP->max_errors.abort)
2066 cont->done(0);
2067 if (*errors > DP->max_errors.reset)
2068 FDCS->reset = 1;
2069 else if (*errors > DP->max_errors.recal)
2070 DRS->track = NEED_2_RECAL;
2073 static void set_floppy(kdev_t device)
2075 if (TYPE(device))
2076 _floppy = TYPE(device) + floppy_type;
2077 else
2078 _floppy = current_type[ DRIVE(device) ];
2082 * formatting support.
2083 * ===================
2085 static void format_interrupt(void)
2087 switch (interpret_errors()){
2088 case 1:
2089 cont->error();
2090 case 2:
2091 break;
2092 case 0:
2093 cont->done(1);
2095 cont->redo();
2098 #define CODE2SIZE (ssize = ((1 << SIZECODE) + 3) >> 2)
2099 #define FM_MODE(x,y) ((y) & ~(((x)->rate & 0x80) >>1))
2100 #define CT(x) ((x) | 0xc0)
2101 static void setup_format_params(int track)
2103 struct fparm {
2104 unsigned char track,head,sect,size;
2105 } *here = (struct fparm *)floppy_track_buffer;
2106 int il,n;
2107 int count,head_shift,track_shift;
2109 raw_cmd = &default_raw_cmd;
2110 raw_cmd->track = track;
2112 raw_cmd->flags = FD_RAW_WRITE | FD_RAW_INTR | FD_RAW_SPIN |
2113 FD_RAW_NEED_DISK | FD_RAW_NEED_SEEK;
2114 raw_cmd->rate = _floppy->rate & 0x43;
2115 raw_cmd->cmd_count = NR_F;
2116 COMMAND = FM_MODE(_floppy,FD_FORMAT);
2117 DR_SELECT = UNIT(current_drive) + PH_HEAD(_floppy,format_req.head);
2118 F_SIZECODE = FD_SIZECODE(_floppy);
2119 F_SECT_PER_TRACK = _floppy->sect << 2 >> F_SIZECODE;
2120 F_GAP = _floppy->fmt_gap;
2121 F_FILL = FD_FILL_BYTE;
2123 raw_cmd->kernel_data = floppy_track_buffer;
2124 raw_cmd->length = 4 * F_SECT_PER_TRACK;
2126 /* allow for about 30ms for data transport per track */
2127 head_shift = (F_SECT_PER_TRACK + 5) / 6;
2129 /* a ``cylinder'' is two tracks plus a little stepping time */
2130 track_shift = 2 * head_shift + 3;
2132 /* position of logical sector 1 on this track */
2133 n = (track_shift * format_req.track + head_shift * format_req.head)
2134 % F_SECT_PER_TRACK;
2136 /* determine interleave */
2137 il = 1;
2138 if (_floppy->fmt_gap < 0x22)
2139 il++;
2141 /* initialize field */
2142 for (count = 0; count < F_SECT_PER_TRACK; ++count) {
2143 here[count].track = format_req.track;
2144 here[count].head = format_req.head;
2145 here[count].sect = 0;
2146 here[count].size = F_SIZECODE;
2148 /* place logical sectors */
2149 for (count = 1; count <= F_SECT_PER_TRACK; ++count) {
2150 here[n].sect = count;
2151 n = (n+il) % F_SECT_PER_TRACK;
2152 if (here[n].sect) { /* sector busy, find next free sector */
2153 ++n;
2154 if (n>= F_SECT_PER_TRACK) {
2155 n-=F_SECT_PER_TRACK;
2156 while (here[n].sect) ++n;
2162 static void redo_format(void)
2164 buffer_track = -1;
2165 setup_format_params(format_req.track << STRETCH(_floppy));
2166 floppy_start();
2167 #ifdef DEBUGT
2168 debugt("queue format request");
2169 #endif
2172 static struct cont_t format_cont={
2173 format_interrupt,
2174 redo_format,
2175 bad_flp_intr,
2176 generic_done };
2178 static int do_format(kdev_t device, struct format_descr *tmp_format_req)
2180 int ret;
2181 int drive=DRIVE(device);
2183 LOCK_FDC(drive,1);
2184 set_floppy(device);
2185 if (!_floppy ||
2186 _floppy->track > DP->tracks ||
2187 tmp_format_req->track >= _floppy->track ||
2188 tmp_format_req->head >= _floppy->head ||
2189 (_floppy->sect << 2) % (1 << FD_SIZECODE(_floppy)) ||
2190 !_floppy->fmt_gap) {
2191 process_fd_request();
2192 return -EINVAL;
2194 format_req = *tmp_format_req;
2195 format_errors = 0;
2196 cont = &format_cont;
2197 errors = &format_errors;
2198 IWAIT(redo_format);
2199 process_fd_request();
2200 return ret;
2204 * Buffer read/write and support
2205 * =============================
2208 /* new request_done. Can handle physical sectors which are smaller than a
2209 * logical buffer */
2210 static void request_done(int uptodate)
2212 int block;
2214 probing = 0;
2215 reschedule_timeout(MAXTIMEOUT, "request done %d", uptodate);
2217 if (!CURRENT){
2218 DPRINT("request list destroyed in floppy request done\n");
2219 return;
2222 if (uptodate){
2223 /* maintain values for invalidation on geometry
2224 * change */
2225 block = current_count_sectors + CURRENT->sector;
2226 INFBOUND(DRS->maxblock, block);
2227 if (block > _floppy->sect)
2228 DRS->maxtrack = 1;
2230 /* unlock chained buffers */
2231 while (current_count_sectors && CURRENT &&
2232 current_count_sectors >= CURRENT->current_nr_sectors){
2233 current_count_sectors -= CURRENT->current_nr_sectors;
2234 CURRENT->nr_sectors -= CURRENT->current_nr_sectors;
2235 CURRENT->sector += CURRENT->current_nr_sectors;
2236 end_request(1);
2238 if (current_count_sectors && CURRENT){
2239 /* "unlock" last subsector */
2240 CURRENT->buffer += current_count_sectors <<9;
2241 CURRENT->current_nr_sectors -= current_count_sectors;
2242 CURRENT->nr_sectors -= current_count_sectors;
2243 CURRENT->sector += current_count_sectors;
2244 return;
2247 if (current_count_sectors && !CURRENT)
2248 DPRINT("request list destroyed in floppy request done\n");
2250 } else {
2251 if (CURRENT->cmd == WRITE) {
2252 /* record write error information */
2253 DRWE->write_errors++;
2254 if (DRWE->write_errors == 1) {
2255 DRWE->first_error_sector = CURRENT->sector;
2256 DRWE->first_error_generation = DRS->generation;
2258 DRWE->last_error_sector = CURRENT->sector;
2259 DRWE->last_error_generation = DRS->generation;
2261 end_request(0);
2265 /* Interrupt handler evaluating the result of the r/w operation */
2266 static void rw_interrupt(void)
2268 int nr_sectors, ssize, eoc, heads;
2270 if (!DRS->first_read_date)
2271 DRS->first_read_date = jiffies;
2273 nr_sectors = 0;
2274 CODE2SIZE;
2276 if(ST1 & ST1_EOC)
2277 eoc = 1;
2278 else
2279 eoc = 0;
2281 if(COMMAND & 0x80)
2282 heads = 2;
2283 else
2284 heads = 1;
2286 nr_sectors = (((R_TRACK-TRACK) * heads +
2287 R_HEAD-HEAD) * SECT_PER_TRACK +
2288 R_SECTOR-SECTOR + eoc) << SIZECODE >> 2;
2290 #ifdef FLOPPY_SANITY_CHECK
2291 if (nr_sectors / ssize >
2292 (in_sector_offset + current_count_sectors + ssize - 1)/ssize) {
2293 DPRINT("long rw: %x instead of %lx\n",
2294 nr_sectors, current_count_sectors);
2295 printk("rs=%d s=%d\n", R_SECTOR, SECTOR);
2296 printk("rh=%d h=%d\n", R_HEAD, HEAD);
2297 printk("rt=%d t=%d\n", R_TRACK, TRACK);
2298 printk("heads=%d eoc=%d\n", heads, eoc);
2299 printk("spt=%d st=%d ss=%d\n", SECT_PER_TRACK,
2300 sector_t, ssize);
2301 printk("in_sector_offset=%d\n", in_sector_offset);
2303 #endif
2305 nr_sectors -= in_sector_offset;
2306 INFBOUND(nr_sectors,0);
2307 SUPBOUND(current_count_sectors, nr_sectors);
2309 switch (interpret_errors()){
2310 case 2:
2311 cont->redo();
2312 return;
2313 case 1:
2314 if (!current_count_sectors){
2315 cont->error();
2316 cont->redo();
2317 return;
2319 break;
2320 case 0:
2321 if (!current_count_sectors){
2322 cont->redo();
2323 return;
2325 current_type[current_drive] = _floppy;
2326 floppy_sizes[TOMINOR(current_drive) ]=
2327 (_floppy->size+1)>>1;
2328 break;
2331 if (probing) {
2332 if (DP->flags & FTD_MSG)
2333 DPRINT("Auto-detected floppy type %s in fd%d\n",
2334 _floppy->name,current_drive);
2335 current_type[current_drive] = _floppy;
2336 floppy_sizes[TOMINOR(current_drive)] = (_floppy->size+1) >> 1;
2337 probing = 0;
2340 if (CT(COMMAND) != FD_READ ||
2341 raw_cmd->kernel_data == CURRENT->buffer){
2342 /* transfer directly from buffer */
2343 cont->done(1);
2344 } else if (CT(COMMAND) == FD_READ){
2345 buffer_track = raw_cmd->track;
2346 buffer_drive = current_drive;
2347 INFBOUND(buffer_max, nr_sectors + sector_t);
2349 cont->redo();
2352 /* Compute maximal contiguous buffer size. */
2353 static int buffer_chain_size(void)
2355 struct buffer_head *bh;
2356 int size;
2357 char *base;
2359 base = CURRENT->buffer;
2360 size = CURRENT->current_nr_sectors << 9;
2361 bh = CURRENT->bh;
2363 if (bh){
2364 bh = bh->b_reqnext;
2365 while (bh && bh->b_data == base + size){
2366 size += bh->b_size;
2367 bh = bh->b_reqnext;
2370 return size >> 9;
2373 /* Compute the maximal transfer size */
2374 static int transfer_size(int ssize, int max_sector, int max_size)
2376 SUPBOUND(max_sector, sector_t + max_size);
2378 /* alignment */
2379 max_sector -= (max_sector % _floppy->sect) % ssize;
2381 /* transfer size, beginning not aligned */
2382 current_count_sectors = max_sector - sector_t ;
2384 return max_sector;
2388 * Move data from/to the track buffer to/from the buffer cache.
2390 static void copy_buffer(int ssize, int max_sector, int max_sector_2)
2392 int remaining; /* number of transferred 512-byte sectors */
2393 struct buffer_head *bh;
2394 char *buffer, *dma_buffer;
2395 int size;
2397 max_sector = transfer_size(ssize,
2398 minimum(max_sector, max_sector_2),
2399 CURRENT->nr_sectors);
2401 if (current_count_sectors <= 0 && CT(COMMAND) == FD_WRITE &&
2402 buffer_max > sector_t + CURRENT->nr_sectors)
2403 current_count_sectors = minimum(buffer_max - sector_t,
2404 CURRENT->nr_sectors);
2406 remaining = current_count_sectors << 9;
2407 #ifdef FLOPPY_SANITY_CHECK
2408 if ((remaining >> 9) > CURRENT->nr_sectors &&
2409 CT(COMMAND) == FD_WRITE){
2410 DPRINT("in copy buffer\n");
2411 printk("current_count_sectors=%ld\n", current_count_sectors);
2412 printk("remaining=%d\n", remaining >> 9);
2413 printk("CURRENT->nr_sectors=%ld\n",CURRENT->nr_sectors);
2414 printk("CURRENT->current_nr_sectors=%ld\n",
2415 CURRENT->current_nr_sectors);
2416 printk("max_sector=%d\n", max_sector);
2417 printk("ssize=%d\n", ssize);
2419 #endif
2421 buffer_max = maximum(max_sector, buffer_max);
2423 dma_buffer = floppy_track_buffer + ((sector_t - buffer_min) << 9);
2425 bh = CURRENT->bh;
2426 size = CURRENT->current_nr_sectors << 9;
2427 buffer = CURRENT->buffer;
2429 while (remaining > 0){
2430 SUPBOUND(size, remaining);
2431 #ifdef FLOPPY_SANITY_CHECK
2432 if (dma_buffer + size >
2433 floppy_track_buffer + (max_buffer_sectors << 10) ||
2434 dma_buffer < floppy_track_buffer){
2435 DPRINT("buffer overrun in copy buffer %d\n",
2436 (int) ((floppy_track_buffer - dma_buffer) >>9));
2437 printk("sector_t=%d buffer_min=%d\n",
2438 sector_t, buffer_min);
2439 printk("current_count_sectors=%ld\n",
2440 current_count_sectors);
2441 if (CT(COMMAND) == FD_READ)
2442 printk("read\n");
2443 if (CT(COMMAND) == FD_READ)
2444 printk("write\n");
2445 break;
2447 if (((unsigned long)buffer) % 512)
2448 DPRINT("%p buffer not aligned\n", buffer);
2449 #endif
2450 if (CT(COMMAND) == FD_READ)
2451 memcpy(buffer, dma_buffer, size);
2452 else
2453 memcpy(dma_buffer, buffer, size);
2454 remaining -= size;
2455 if (!remaining)
2456 break;
2458 dma_buffer += size;
2459 bh = bh->b_reqnext;
2460 #ifdef FLOPPY_SANITY_CHECK
2461 if (!bh){
2462 DPRINT("bh=null in copy buffer after copy\n");
2463 break;
2465 #endif
2466 size = bh->b_size;
2467 buffer = bh->b_data;
2469 #ifdef FLOPPY_SANITY_CHECK
2470 if (remaining){
2471 if (remaining > 0)
2472 max_sector -= remaining >> 9;
2473 DPRINT("weirdness: remaining %d\n", remaining>>9);
2475 #endif
2478 #if 0
2479 static inline int check_dma_crossing(char *start,
2480 unsigned long length, char *message)
2482 if (CROSS_64KB(start, length)) {
2483 printk("DMA xfer crosses 64KB boundary in %s %p-%p\n",
2484 message, start, start+length);
2485 return 1;
2486 } else
2487 return 0;
2489 #endif
2491 /* work around a bug in pseudo DMA
2492 * (on some FDCs) pseudo DMA does not stop when the CPU stops
2493 * sending data. Hence we need a different way to signal the
2494 * transfer length: We use SECT_PER_TRACK. Unfortunately, this
2495 * does not work with MT, hence we can only transfer one head at
2496 * a time
2498 static void virtualdmabug_workaround(void) {
2499 int hard_sectors, end_sector;
2500 if(CT(COMMAND) == FD_WRITE) {
2501 COMMAND &= ~0x80; /* switch off multiple track mode */
2503 hard_sectors = raw_cmd->length >> (7 + SIZECODE);
2504 end_sector = SECTOR + hard_sectors - 1;
2505 #ifdef FLOPPY_SANITY_CHECK
2506 if(end_sector > SECT_PER_TRACK) {
2507 printk("too many sectors %d > %d\n",
2508 end_sector, SECT_PER_TRACK);
2509 return;
2511 #endif
2512 SECT_PER_TRACK = end_sector; /* make sure SECT_PER_TRACK points
2513 * to end of transfer */
2518 * Formulate a read/write request.
2519 * this routine decides where to load the data (directly to buffer, or to
2520 * tmp floppy area), how much data to load (the size of the buffer, the whole
2521 * track, or a single sector)
2522 * All floppy_track_buffer handling goes in here. If we ever add track buffer
2523 * allocation on the fly, it should be done here. No other part should need
2524 * modification.
2527 static int make_raw_rw_request(void)
2529 int aligned_sector_t;
2530 int max_sector, max_size, tracksize, ssize;
2532 set_fdc(DRIVE(CURRENT->rq_dev));
2534 raw_cmd = &default_raw_cmd;
2535 raw_cmd->flags = FD_RAW_SPIN | FD_RAW_NEED_DISK | FD_RAW_NEED_DISK |
2536 FD_RAW_NEED_SEEK;
2537 raw_cmd->cmd_count = NR_RW;
2538 if (CURRENT->cmd == READ){
2539 raw_cmd->flags |= FD_RAW_READ;
2540 COMMAND = FM_MODE(_floppy,FD_READ);
2541 } else if (CURRENT->cmd == WRITE){
2542 raw_cmd->flags |= FD_RAW_WRITE;
2543 COMMAND = FM_MODE(_floppy,FD_WRITE);
2544 } else {
2545 DPRINT("make_raw_rw_request: unknown command\n");
2546 return 0;
2549 max_sector = _floppy->sect * _floppy->head;
2551 TRACK = CURRENT->sector / max_sector;
2552 sector_t = CURRENT->sector % max_sector;
2553 if (_floppy->track && TRACK >= _floppy->track) {
2554 if(CURRENT->current_nr_sectors & 1) {
2555 current_count_sectors = 1;
2556 return 1;
2557 } else
2558 return 0;
2560 HEAD = sector_t / _floppy->sect;
2562 if (((_floppy->stretch & FD_SWAPSIDES) || TESTF(FD_NEED_TWADDLE)) &&
2563 sector_t < _floppy->sect)
2564 max_sector = _floppy->sect;
2566 /* 2M disks have phantom sectors on the first track */
2567 if ((_floppy->rate & FD_2M) && (!TRACK) && (!HEAD)){
2568 max_sector = 2 * _floppy->sect / 3;
2569 if (sector_t >= max_sector){
2570 current_count_sectors = minimum(_floppy->sect - sector_t,
2571 CURRENT->nr_sectors);
2572 return 1;
2574 SIZECODE = 2;
2575 } else
2576 SIZECODE = FD_SIZECODE(_floppy);
2577 raw_cmd->rate = _floppy->rate & 0x43;
2578 if ((_floppy->rate & FD_2M) &&
2579 (TRACK || HEAD) &&
2580 raw_cmd->rate == 2)
2581 raw_cmd->rate = 1;
2583 if (SIZECODE)
2584 SIZECODE2 = 0xff;
2585 else
2586 SIZECODE2 = 0x80;
2587 raw_cmd->track = TRACK << STRETCH(_floppy);
2588 DR_SELECT = UNIT(current_drive) + PH_HEAD(_floppy,HEAD);
2589 GAP = _floppy->gap;
2590 CODE2SIZE;
2591 SECT_PER_TRACK = _floppy->sect << 2 >> SIZECODE;
2592 SECTOR = ((sector_t % _floppy->sect) << 2 >> SIZECODE) + 1;
2594 /* tracksize describes the size which can be filled up with sectors
2595 * of size ssize.
2597 tracksize = _floppy->sect - _floppy->sect % ssize;
2598 if (tracksize < _floppy->sect){
2599 SECT_PER_TRACK ++;
2600 if (tracksize <= sector_t % _floppy->sect)
2601 SECTOR--;
2603 /* if we are beyond tracksize, fill up using smaller sectors */
2604 while (tracksize <= sector_t % _floppy->sect){
2605 while(tracksize + ssize > _floppy->sect){
2606 SIZECODE--;
2607 ssize >>= 1;
2609 SECTOR++; SECT_PER_TRACK ++;
2610 tracksize += ssize;
2612 max_sector = HEAD * _floppy->sect + tracksize;
2613 } else if (!TRACK && !HEAD && !(_floppy->rate & FD_2M) && probing) {
2614 max_sector = _floppy->sect;
2615 } else if (!HEAD && CT(COMMAND) == FD_WRITE) {
2616 /* for virtual DMA bug workaround */
2617 max_sector = _floppy->sect;
2620 in_sector_offset = (sector_t % _floppy->sect) % ssize;
2621 aligned_sector_t = sector_t - in_sector_offset;
2622 max_size = CURRENT->nr_sectors;
2623 if ((raw_cmd->track == buffer_track) &&
2624 (current_drive == buffer_drive) &&
2625 (sector_t >= buffer_min) && (sector_t < buffer_max)) {
2626 /* data already in track buffer */
2627 if (CT(COMMAND) == FD_READ) {
2628 copy_buffer(1, max_sector, buffer_max);
2629 return 1;
2631 } else if (in_sector_offset || CURRENT->nr_sectors < ssize){
2632 if (CT(COMMAND) == FD_WRITE){
2633 if (sector_t + CURRENT->nr_sectors > ssize &&
2634 sector_t + CURRENT->nr_sectors < ssize + ssize)
2635 max_size = ssize + ssize;
2636 else
2637 max_size = ssize;
2639 raw_cmd->flags &= ~FD_RAW_WRITE;
2640 raw_cmd->flags |= FD_RAW_READ;
2641 COMMAND = FM_MODE(_floppy,FD_READ);
2642 } else if ((unsigned long)CURRENT->buffer < MAX_DMA_ADDRESS) {
2643 unsigned long dma_limit;
2644 int direct, indirect;
2646 indirect= transfer_size(ssize,max_sector,max_buffer_sectors*2) -
2647 sector_t;
2650 * Do NOT use minimum() here---MAX_DMA_ADDRESS is 64 bits wide
2651 * on a 64 bit machine!
2653 max_size = buffer_chain_size();
2654 dma_limit = (MAX_DMA_ADDRESS - ((unsigned long) CURRENT->buffer)) >> 9;
2655 if ((unsigned long) max_size > dma_limit) {
2656 max_size = dma_limit;
2658 /* 64 kb boundaries */
2659 if (CROSS_64KB(CURRENT->buffer, max_size << 9))
2660 max_size = (K_64 -
2661 ((unsigned long)CURRENT->buffer) % K_64)>>9;
2662 direct = transfer_size(ssize,max_sector,max_size) - sector_t;
2664 * We try to read tracks, but if we get too many errors, we
2665 * go back to reading just one sector at a time.
2667 * This means we should be able to read a sector even if there
2668 * are other bad sectors on this track.
2670 if (!direct ||
2671 (indirect * 2 > direct * 3 &&
2672 *errors < DP->max_errors.read_track &&
2673 /*!TESTF(FD_NEED_TWADDLE) &&*/
2674 ((!probing || (DP->read_track&(1<<DRS->probed_format)))))){
2675 max_size = CURRENT->nr_sectors;
2676 } else {
2677 raw_cmd->kernel_data = CURRENT->buffer;
2678 raw_cmd->length = current_count_sectors << 9;
2679 if (raw_cmd->length == 0){
2680 DPRINT("zero dma transfer attempted from make_raw_request\n");
2681 DPRINT("indirect=%d direct=%d sector_t=%d",
2682 indirect, direct, sector_t);
2683 return 0;
2685 /* check_dma_crossing(raw_cmd->kernel_data,
2686 raw_cmd->length,
2687 "end of make_raw_request [1]");*/
2689 virtualdmabug_workaround();
2690 return 2;
2694 if (CT(COMMAND) == FD_READ)
2695 max_size = max_sector; /* unbounded */
2697 /* claim buffer track if needed */
2698 if (buffer_track != raw_cmd->track || /* bad track */
2699 buffer_drive !=current_drive || /* bad drive */
2700 sector_t > buffer_max ||
2701 sector_t < buffer_min ||
2702 ((CT(COMMAND) == FD_READ ||
2703 (!in_sector_offset && CURRENT->nr_sectors >= ssize))&&
2704 max_sector > 2 * max_buffer_sectors + buffer_min &&
2705 max_size + sector_t > 2 * max_buffer_sectors + buffer_min)
2706 /* not enough space */){
2707 buffer_track = -1;
2708 buffer_drive = current_drive;
2709 buffer_max = buffer_min = aligned_sector_t;
2711 raw_cmd->kernel_data = floppy_track_buffer +
2712 ((aligned_sector_t-buffer_min)<<9);
2714 if (CT(COMMAND) == FD_WRITE){
2715 /* copy write buffer to track buffer.
2716 * if we get here, we know that the write
2717 * is either aligned or the data already in the buffer
2718 * (buffer will be overwritten) */
2719 #ifdef FLOPPY_SANITY_CHECK
2720 if (in_sector_offset && buffer_track == -1)
2721 DPRINT("internal error offset !=0 on write\n");
2722 #endif
2723 buffer_track = raw_cmd->track;
2724 buffer_drive = current_drive;
2725 copy_buffer(ssize, max_sector, 2*max_buffer_sectors+buffer_min);
2726 } else
2727 transfer_size(ssize, max_sector,
2728 2*max_buffer_sectors+buffer_min-aligned_sector_t);
2730 /* round up current_count_sectors to get dma xfer size */
2731 raw_cmd->length = in_sector_offset+current_count_sectors;
2732 raw_cmd->length = ((raw_cmd->length -1)|(ssize-1))+1;
2733 raw_cmd->length <<= 9;
2734 #ifdef FLOPPY_SANITY_CHECK
2735 /*check_dma_crossing(raw_cmd->kernel_data, raw_cmd->length,
2736 "end of make_raw_request");*/
2737 if ((raw_cmd->length < current_count_sectors << 9) ||
2738 (raw_cmd->kernel_data != CURRENT->buffer &&
2739 CT(COMMAND) == FD_WRITE &&
2740 (aligned_sector_t + (raw_cmd->length >> 9) > buffer_max ||
2741 aligned_sector_t < buffer_min)) ||
2742 raw_cmd->length % (128 << SIZECODE) ||
2743 raw_cmd->length <= 0 || current_count_sectors <= 0){
2744 DPRINT("fractionary current count b=%lx s=%lx\n",
2745 raw_cmd->length, current_count_sectors);
2746 if (raw_cmd->kernel_data != CURRENT->buffer)
2747 printk("addr=%d, length=%ld\n",
2748 (int) ((raw_cmd->kernel_data -
2749 floppy_track_buffer) >> 9),
2750 current_count_sectors);
2751 printk("st=%d ast=%d mse=%d msi=%d\n",
2752 sector_t, aligned_sector_t, max_sector, max_size);
2753 printk("ssize=%x SIZECODE=%d\n", ssize, SIZECODE);
2754 printk("command=%x SECTOR=%d HEAD=%d, TRACK=%d\n",
2755 COMMAND, SECTOR, HEAD, TRACK);
2756 printk("buffer drive=%d\n", buffer_drive);
2757 printk("buffer track=%d\n", buffer_track);
2758 printk("buffer_min=%d\n", buffer_min);
2759 printk("buffer_max=%d\n", buffer_max);
2760 return 0;
2763 if (raw_cmd->kernel_data != CURRENT->buffer){
2764 if (raw_cmd->kernel_data < floppy_track_buffer ||
2765 current_count_sectors < 0 ||
2766 raw_cmd->length < 0 ||
2767 raw_cmd->kernel_data + raw_cmd->length >
2768 floppy_track_buffer + (max_buffer_sectors << 10)){
2769 DPRINT("buffer overrun in schedule dma\n");
2770 printk("sector_t=%d buffer_min=%d current_count=%ld\n",
2771 sector_t, buffer_min,
2772 raw_cmd->length >> 9);
2773 printk("current_count_sectors=%ld\n",
2774 current_count_sectors);
2775 if (CT(COMMAND) == FD_READ)
2776 printk("read\n");
2777 if (CT(COMMAND) == FD_READ)
2778 printk("write\n");
2779 return 0;
2781 } else if (raw_cmd->length > CURRENT->nr_sectors << 9 ||
2782 current_count_sectors > CURRENT->nr_sectors){
2783 DPRINT("buffer overrun in direct transfer\n");
2784 return 0;
2785 } else if (raw_cmd->length < current_count_sectors << 9){
2786 DPRINT("more sectors than bytes\n");
2787 printk("bytes=%ld\n", raw_cmd->length >> 9);
2788 printk("sectors=%ld\n", current_count_sectors);
2790 if (raw_cmd->length == 0){
2791 DPRINT("zero dma transfer attempted from make_raw_request\n");
2792 return 0;
2794 #endif
2796 virtualdmabug_workaround();
2797 return 2;
2800 static void redo_fd_request(void)
2802 #define REPEAT {request_done(0); continue; }
2803 kdev_t device;
2804 int tmp;
2806 lastredo = jiffies;
2807 if (current_drive < N_DRIVE)
2808 floppy_off(current_drive);
2810 if (CURRENT && CURRENT->rq_status == RQ_INACTIVE){
2811 CLEAR_INTR;
2812 unlock_fdc();
2813 return;
2816 while(1){
2817 if (!CURRENT) {
2818 CLEAR_INTR;
2819 unlock_fdc();
2820 return;
2822 if (MAJOR(CURRENT->rq_dev) != MAJOR_NR)
2823 panic(DEVICE_NAME ": request list destroyed");
2824 if (CURRENT->bh && !buffer_locked(CURRENT->bh))
2825 panic(DEVICE_NAME ": block not locked");
2827 device = CURRENT->rq_dev;
2828 set_fdc(DRIVE(device));
2829 reschedule_timeout(CURRENTD, "redo fd request", 0);
2831 set_floppy(device);
2832 raw_cmd = & default_raw_cmd;
2833 raw_cmd->flags = 0;
2834 if (start_motor(redo_fd_request)) return;
2835 disk_change(current_drive);
2836 if (test_bit(current_drive, &fake_change) ||
2837 TESTF(FD_DISK_CHANGED)){
2838 DPRINT("disk absent or changed during operation\n");
2839 REPEAT;
2841 if (!_floppy) { /* Autodetection */
2842 if (!probing){
2843 DRS->probed_format = 0;
2844 if (next_valid_format()){
2845 DPRINT("no autodetectable formats\n");
2846 _floppy = NULL;
2847 REPEAT;
2850 probing = 1;
2851 _floppy = floppy_type+DP->autodetect[DRS->probed_format];
2852 } else
2853 probing = 0;
2854 errors = & (CURRENT->errors);
2855 tmp = make_raw_rw_request();
2856 if (tmp < 2){
2857 request_done(tmp);
2858 continue;
2861 if (TESTF(FD_NEED_TWADDLE))
2862 twaddle();
2863 schedule_bh( (void *)(void *) floppy_start);
2864 #ifdef DEBUGT
2865 debugt("queue fd request");
2866 #endif
2867 return;
2869 #undef REPEAT
2872 static struct cont_t rw_cont={
2873 rw_interrupt,
2874 redo_fd_request,
2875 bad_flp_intr,
2876 request_done };
2878 static void process_fd_request(void)
2880 cont = &rw_cont;
2881 schedule_bh( (void *)(void *) redo_fd_request);
2884 static void do_fd_request(void)
2886 if(usage_count == 0) {
2887 printk("warning: usage count=0, CURRENT=%p exiting\n", CURRENT);
2888 printk("sect=%ld cmd=%d\n", CURRENT->sector, CURRENT->cmd);
2889 return;
2891 if (fdc_busy){
2892 /* fdc busy, this new request will be treated when the
2893 current one is done */
2894 is_alive("do fd request, old request running");
2895 return;
2897 lock_fdc(MAXTIMEOUT,0);
2898 process_fd_request();
2899 is_alive("do fd request");
2902 static struct cont_t poll_cont={
2903 success_and_wakeup,
2904 floppy_ready,
2905 generic_failure,
2906 generic_done };
2908 static int poll_drive(int interruptible, int flag)
2910 int ret;
2911 /* no auto-sense, just clear dcl */
2912 raw_cmd = &default_raw_cmd;
2913 raw_cmd->flags= flag;
2914 raw_cmd->track=0;
2915 raw_cmd->cmd_count=0;
2916 cont = &poll_cont;
2917 #ifdef DCL_DEBUG
2918 if (DP->flags & FD_DEBUG){
2919 DPRINT("setting NEWCHANGE in poll_drive\n");
2921 #endif
2922 SETF(FD_DISK_NEWCHANGE);
2923 WAIT(floppy_ready);
2924 return ret;
2928 * User triggered reset
2929 * ====================
2932 static void reset_intr(void)
2934 printk("weird, reset interrupt called\n");
2937 static struct cont_t reset_cont={
2938 reset_intr,
2939 success_and_wakeup,
2940 generic_failure,
2941 generic_done };
2943 static int user_reset_fdc(int drive, int arg, int interruptible)
2945 int ret;
2947 ret=0;
2948 LOCK_FDC(drive,interruptible);
2949 if (arg == FD_RESET_ALWAYS)
2950 FDCS->reset=1;
2951 if (FDCS->reset){
2952 cont = &reset_cont;
2953 WAIT(reset_fdc);
2955 process_fd_request();
2956 return ret;
2960 * Misc Ioctl's and support
2961 * ========================
2963 static inline int fd_copyout(void *param, const void *address, int size)
2965 return copy_to_user(param,address, size) ? -EFAULT : 0;
2968 static inline int fd_copyin(void *param, void *address, int size)
2970 return copy_from_user(address, param, size) ? -EFAULT : 0;
2973 #define _COPYOUT(x) (copy_to_user((void *)param, &(x), sizeof(x)) ? -EFAULT : 0)
2974 #define _COPYIN(x) (copy_from_user(&(x), (void *)param, sizeof(x)) ? -EFAULT : 0)
2976 #define COPYOUT(x) ECALL(_COPYOUT(x))
2977 #define COPYIN(x) ECALL(_COPYIN(x))
2979 static inline const char *drive_name(int type, int drive)
2981 struct floppy_struct *floppy;
2983 if (type)
2984 floppy = floppy_type + type;
2985 else {
2986 if (UDP->native_format)
2987 floppy = floppy_type + UDP->native_format;
2988 else
2989 return "(null)";
2991 if (floppy->name)
2992 return floppy->name;
2993 else
2994 return "(null)";
2998 /* raw commands */
2999 static void raw_cmd_done(int flag)
3001 int i;
3003 if (!flag) {
3004 raw_cmd->flags |= FD_RAW_FAILURE;
3005 raw_cmd->flags |= FD_RAW_HARDFAILURE;
3006 } else {
3007 raw_cmd->reply_count = inr;
3008 if(raw_cmd->reply_count > MAX_REPLIES)
3009 raw_cmd->reply_count=0;
3010 for (i=0; i< raw_cmd->reply_count; i++)
3011 raw_cmd->reply[i] = reply_buffer[i];
3013 if (raw_cmd->flags & (FD_RAW_READ | FD_RAW_WRITE))
3014 raw_cmd->length = fd_get_dma_residue();
3016 if ((raw_cmd->flags & FD_RAW_SOFTFAILURE) &&
3017 (!raw_cmd->reply_count || (raw_cmd->reply[0] & 0xc0)))
3018 raw_cmd->flags |= FD_RAW_FAILURE;
3020 if (disk_change(current_drive))
3021 raw_cmd->flags |= FD_RAW_DISK_CHANGE;
3022 else
3023 raw_cmd->flags &= ~FD_RAW_DISK_CHANGE;
3024 if (raw_cmd->flags & FD_RAW_NO_MOTOR_AFTER)
3025 motor_off_callback(current_drive);
3027 if (raw_cmd->next &&
3028 (!(raw_cmd->flags & FD_RAW_FAILURE) ||
3029 !(raw_cmd->flags & FD_RAW_STOP_IF_FAILURE)) &&
3030 ((raw_cmd->flags & FD_RAW_FAILURE) ||
3031 !(raw_cmd->flags &FD_RAW_STOP_IF_SUCCESS))) {
3032 raw_cmd = raw_cmd->next;
3033 return;
3036 generic_done(flag);
3040 static struct cont_t raw_cmd_cont={
3041 success_and_wakeup,
3042 floppy_start,
3043 generic_failure,
3044 raw_cmd_done
3047 static inline int raw_cmd_copyout(int cmd, char *param,
3048 struct floppy_raw_cmd *ptr)
3050 int ret;
3052 while(ptr) {
3053 COPYOUT(*ptr);
3054 param += sizeof(struct floppy_raw_cmd);
3055 if ((ptr->flags & FD_RAW_READ) && ptr->buffer_length){
3056 if (ptr->length>=0 && ptr->length<=ptr->buffer_length)
3057 ECALL(fd_copyout(ptr->data,
3058 ptr->kernel_data,
3059 ptr->buffer_length -
3060 ptr->length));
3062 ptr = ptr->next;
3064 return 0;
3068 static void raw_cmd_free(struct floppy_raw_cmd **ptr)
3070 struct floppy_raw_cmd *next,*this;
3072 this = *ptr;
3073 *ptr = 0;
3074 while(this) {
3075 if (this->buffer_length) {
3076 fd_dma_mem_free((unsigned long)this->kernel_data,
3077 this->buffer_length);
3078 this->buffer_length = 0;
3080 next = this->next;
3081 kfree(this);
3082 this = next;
3087 static inline int raw_cmd_copyin(int cmd, char *param,
3088 struct floppy_raw_cmd **rcmd)
3090 struct floppy_raw_cmd *ptr;
3091 int ret;
3092 int i;
3094 *rcmd = 0;
3095 while(1) {
3096 ptr = (struct floppy_raw_cmd *)
3097 kmalloc(sizeof(struct floppy_raw_cmd), GFP_USER);
3098 if (!ptr)
3099 return -ENOMEM;
3100 *rcmd = ptr;
3101 COPYIN(*ptr);
3102 ptr->next = 0;
3103 ptr->buffer_length = 0;
3104 param += sizeof(struct floppy_raw_cmd);
3105 if (ptr->cmd_count > 33)
3106 /* the command may now also take up the space
3107 * initially intended for the reply & the
3108 * reply count. Needed for long 82078 commands
3109 * such as RESTORE, which takes ... 17 command
3110 * bytes. Murphy's law #137: When you reserve
3111 * 16 bytes for a structure, you'll one day
3112 * discover that you really need 17...
3114 return -EINVAL;
3116 for (i=0; i< 16; i++)
3117 ptr->reply[i] = 0;
3118 ptr->resultcode = 0;
3119 ptr->kernel_data = 0;
3121 if (ptr->flags & (FD_RAW_READ | FD_RAW_WRITE)) {
3122 if (ptr->length <= 0)
3123 return -EINVAL;
3124 ptr->kernel_data =(char*)fd_dma_mem_alloc(ptr->length);
3125 fallback_on_nodma_alloc(&ptr->kernel_data,
3126 ptr->length);
3127 if (!ptr->kernel_data)
3128 return -ENOMEM;
3129 ptr->buffer_length = ptr->length;
3131 if (ptr->flags & FD_RAW_WRITE)
3132 ECALL(fd_copyin(ptr->data, ptr->kernel_data,
3133 ptr->length));
3134 rcmd = & (ptr->next);
3135 if (!(ptr->flags & FD_RAW_MORE))
3136 return 0;
3137 ptr->rate &= 0x43;
3142 static int raw_cmd_ioctl(int cmd, void *param)
3144 int drive, ret, ret2;
3145 struct floppy_raw_cmd *my_raw_cmd;
3147 if (FDCS->rawcmd <= 1)
3148 FDCS->rawcmd = 1;
3149 for (drive= 0; drive < N_DRIVE; drive++){
3150 if (FDC(drive) != fdc)
3151 continue;
3152 if (drive == current_drive){
3153 if (UDRS->fd_ref > 1){
3154 FDCS->rawcmd = 2;
3155 break;
3157 } else if (UDRS->fd_ref){
3158 FDCS->rawcmd = 2;
3159 break;
3163 if (FDCS->reset)
3164 return -EIO;
3166 ret = raw_cmd_copyin(cmd, param, &my_raw_cmd);
3167 if (ret) {
3168 raw_cmd_free(&my_raw_cmd);
3169 return ret;
3172 raw_cmd = my_raw_cmd;
3173 cont = &raw_cmd_cont;
3174 ret=wait_til_done(floppy_start,1);
3175 #ifdef DCL_DEBUG
3176 if (DP->flags & FD_DEBUG){
3177 DPRINT("calling disk change from raw_cmd ioctl\n");
3179 #endif
3181 if (ret != -EINTR && FDCS->reset)
3182 ret = -EIO;
3184 DRS->track = NO_TRACK;
3186 ret2 = raw_cmd_copyout(cmd, param, my_raw_cmd);
3187 if (!ret)
3188 ret = ret2;
3189 raw_cmd_free(&my_raw_cmd);
3190 return ret;
3193 static int invalidate_drive(kdev_t rdev)
3195 /* invalidate the buffer track to force a reread */
3196 set_bit(DRIVE(rdev), &fake_change);
3197 process_fd_request();
3198 check_disk_change(rdev);
3199 return 0;
3203 static inline void clear_write_error(int drive)
3205 CLEARSTRUCT(UDRWE);
3208 static inline int set_geometry(unsigned int cmd, struct floppy_struct *g,
3209 int drive, int type, kdev_t device)
3211 int cnt;
3213 /* sanity checking for parameters.*/
3214 if (g->sect <= 0 ||
3215 g->head <= 0 ||
3216 g->track <= 0 ||
3217 g->track > UDP->tracks>>STRETCH(g) ||
3218 /* check if reserved bits are set */
3219 (g->stretch&~(FD_STRETCH|FD_SWAPSIDES)) != 0)
3220 return -EINVAL;
3221 if (type){
3222 if (!capable(CAP_SYS_ADMIN))
3223 return -EPERM;
3224 LOCK_FDC(drive,1);
3225 for (cnt = 0; cnt < N_DRIVE; cnt++){
3226 if (ITYPE(drive_state[cnt].fd_device) == type &&
3227 drive_state[cnt].fd_ref)
3228 set_bit(drive, &fake_change);
3230 floppy_type[type] = *g;
3231 floppy_type[type].name="user format";
3232 for (cnt = type << 2; cnt < (type << 2) + 4; cnt++)
3233 floppy_sizes[cnt]= floppy_sizes[cnt+0x80]=
3234 (floppy_type[type].size+1)>>1;
3235 process_fd_request();
3236 for (cnt = 0; cnt < N_DRIVE; cnt++){
3237 if (ITYPE(drive_state[cnt].fd_device) == type &&
3238 drive_state[cnt].fd_ref)
3239 check_disk_change(
3240 MKDEV(FLOPPY_MAJOR,
3241 drive_state[cnt].fd_device));
3243 } else {
3244 LOCK_FDC(drive,1);
3245 if (cmd != FDDEFPRM)
3246 /* notice a disk change immediately, else
3247 * we loose our settings immediately*/
3248 CALL(poll_drive(1, FD_RAW_NEED_DISK));
3249 user_params[drive] = *g;
3250 if (buffer_drive == drive)
3251 SUPBOUND(buffer_max, user_params[drive].sect);
3252 current_type[drive] = &user_params[drive];
3253 floppy_sizes[drive] = (user_params[drive].size+1) >> 1;
3254 if (cmd == FDDEFPRM)
3255 DRS->keep_data = -1;
3256 else
3257 DRS->keep_data = 1;
3258 /* invalidation. Invalidate only when needed, i.e.
3259 * when there are already sectors in the buffer cache
3260 * whose number will change. This is useful, because
3261 * mtools often changes the geometry of the disk after
3262 * looking at the boot block */
3263 if (DRS->maxblock > user_params[drive].sect || DRS->maxtrack)
3264 invalidate_drive(device);
3265 else
3266 process_fd_request();
3268 return 0;
3271 /* handle obsolete ioctl's */
3272 static int ioctl_table[]= {
3273 FDCLRPRM,
3274 FDSETPRM,
3275 FDDEFPRM,
3276 FDGETPRM,
3277 FDMSGON,
3278 FDMSGOFF,
3279 FDFMTBEG,
3280 FDFMTTRK,
3281 FDFMTEND,
3282 FDSETEMSGTRESH,
3283 FDFLUSH,
3284 FDSETMAXERRS,
3285 FDGETMAXERRS,
3286 FDGETDRVTYP,
3287 FDSETDRVPRM,
3288 FDGETDRVPRM,
3289 FDGETDRVSTAT,
3290 FDPOLLDRVSTAT,
3291 FDRESET,
3292 FDGETFDCSTAT,
3293 FDWERRORCLR,
3294 FDWERRORGET,
3295 FDRAWCMD,
3296 FDEJECT,
3297 FDTWADDLE
3300 static inline int normalize_ioctl(int *cmd, int *size)
3302 int i;
3304 for (i=0; i < ARRAY_SIZE(ioctl_table); i++) {
3305 if ((*cmd & 0xffff) == (ioctl_table[i] & 0xffff)){
3306 *size = _IOC_SIZE(*cmd);
3307 *cmd = ioctl_table[i];
3308 if (*size > _IOC_SIZE(*cmd)) {
3309 printk("ioctl not yet supported\n");
3310 return -EFAULT;
3312 return 0;
3315 return -EINVAL;
3318 static int get_floppy_geometry(int drive, int type, struct floppy_struct **g)
3320 if (type)
3321 *g = &floppy_type[type];
3322 else {
3323 LOCK_FDC(drive,0);
3324 CALL(poll_drive(0,0));
3325 process_fd_request();
3326 *g = current_type[drive];
3328 if(!*g)
3329 return -ENODEV;
3330 return 0;
3333 static int fd_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
3334 unsigned long param)
3336 #define IOCTL_MODE_BIT 8
3337 #define OPEN_WRITE_BIT 16
3338 #define IOCTL_ALLOWED (filp && (filp->f_mode & IOCTL_MODE_BIT))
3339 #define OUT(c,x) case c: outparam = (const char *) (x); break
3340 #define IN(c,x,tag) case c: *(x) = inparam. tag ; return 0
3342 int i,drive,type;
3343 kdev_t device;
3344 int ret;
3345 int size;
3346 union inparam {
3347 struct floppy_struct g; /* geometry */
3348 struct format_descr f;
3349 struct floppy_max_errors max_errors;
3350 struct floppy_drive_params dp;
3351 } inparam; /* parameters coming from user space */
3352 const char *outparam; /* parameters passed back to user space */
3354 device = inode->i_rdev;
3355 switch (cmd) {
3356 RO_IOCTLS(device,param);
3358 type = TYPE(device);
3359 drive = DRIVE(device);
3361 /* convert compatibility eject ioctls into floppy eject ioctl.
3362 * We do this in order to provide a means to eject floppy disks before
3363 * installing the new fdutils package */
3364 if(cmd == CDROMEJECT || /* CD-ROM eject */
3365 cmd == 0x6470 /* SunOS floppy eject */) {
3366 DPRINT("obsolete eject ioctl\n");
3367 DPRINT("please use floppycontrol --eject\n");
3368 cmd = FDEJECT;
3371 /* generic block device ioctls */
3372 switch(cmd) {
3373 /* the following have been inspired by the corresponding
3374 * code for other block devices. */
3375 struct floppy_struct *g;
3376 case HDIO_GETGEO:
3378 struct hd_geometry loc;
3379 ECALL(get_floppy_geometry(drive, type, &g));
3380 loc.heads = g->head;
3381 loc.sectors = g->sect;
3382 loc.cylinders = g->track;
3383 loc.start = 0;
3384 return _COPYOUT(loc);
3386 case BLKRASET:
3387 if(!capable(CAP_SYS_ADMIN)) return -EACCES;
3388 if(param > 0xff) return -EINVAL;
3389 read_ahead[MAJOR(inode->i_rdev)] = param;
3390 return 0;
3391 case BLKRAGET:
3392 return put_user(read_ahead[MAJOR(inode->i_rdev)],
3393 (long *) param);
3394 case BLKFLSBUF:
3395 if(!capable(CAP_SYS_ADMIN)) return -EACCES;
3396 fsync_dev(inode->i_rdev);
3397 invalidate_buffers(inode->i_rdev);
3398 return 0;
3400 case BLKGETSIZE:
3401 ECALL(get_floppy_geometry(drive, type, &g));
3402 return put_user(g->size, (long *) param);
3403 /* BLKRRPART is not defined as floppies don't have
3404 * partition tables */
3407 /* convert the old style command into a new style command */
3408 if ((cmd & 0xff00) == 0x0200) {
3409 ECALL(normalize_ioctl(&cmd, &size));
3410 } else
3411 return -EINVAL;
3413 /* permission checks */
3414 if (((cmd & 0x40) && !IOCTL_ALLOWED) ||
3415 ((cmd & 0x80) && !suser()))
3416 return -EPERM;
3418 /* copyin */
3419 CLEARSTRUCT(&inparam);
3420 if (_IOC_DIR(cmd) & _IOC_WRITE)
3421 ECALL(fd_copyin((void *)param, &inparam, size))
3423 switch (cmd) {
3424 case FDEJECT:
3425 if(UDRS->fd_ref != 1)
3426 /* somebody else has this drive open */
3427 return -EBUSY;
3428 LOCK_FDC(drive,1);
3430 /* do the actual eject. Fails on
3431 * non-Sparc architectures */
3432 ret=fd_eject(UNIT(drive));
3434 USETF(FD_DISK_CHANGED);
3435 USETF(FD_VERIFY);
3436 process_fd_request();
3437 return ret;
3438 case FDCLRPRM:
3439 LOCK_FDC(drive,1);
3440 current_type[drive] = NULL;
3441 floppy_sizes[drive] = MAX_DISK_SIZE;
3442 UDRS->keep_data = 0;
3443 return invalidate_drive(device);
3444 case FDSETPRM:
3445 case FDDEFPRM:
3446 return set_geometry(cmd, & inparam.g,
3447 drive, type, device);
3448 case FDGETPRM:
3449 ECALL(get_floppy_geometry(drive, type,
3450 (struct floppy_struct**)
3451 &outparam));
3452 break;
3454 case FDMSGON:
3455 UDP->flags |= FTD_MSG;
3456 return 0;
3457 case FDMSGOFF:
3458 UDP->flags &= ~FTD_MSG;
3459 return 0;
3461 case FDFMTBEG:
3462 LOCK_FDC(drive,1);
3463 CALL(poll_drive(1, FD_RAW_NEED_DISK));
3464 ret = UDRS->flags;
3465 process_fd_request();
3466 if(ret & FD_VERIFY)
3467 return -ENODEV;
3468 if(!(ret & FD_DISK_WRITABLE))
3469 return -EROFS;
3470 return 0;
3471 case FDFMTTRK:
3472 if (UDRS->fd_ref != 1)
3473 return -EBUSY;
3474 return do_format(device, &inparam.f);
3475 case FDFMTEND:
3476 case FDFLUSH:
3477 LOCK_FDC(drive,1);
3478 return invalidate_drive(device);
3480 case FDSETEMSGTRESH:
3481 UDP->max_errors.reporting =
3482 (unsigned short) (param & 0x0f);
3483 return 0;
3484 OUT(FDGETMAXERRS, &UDP->max_errors);
3485 IN(FDSETMAXERRS, &UDP->max_errors, max_errors);
3487 case FDGETDRVTYP:
3488 outparam = drive_name(type,drive);
3489 SUPBOUND(size,strlen(outparam)+1);
3490 break;
3492 IN(FDSETDRVPRM, UDP, dp);
3493 OUT(FDGETDRVPRM, UDP);
3495 case FDPOLLDRVSTAT:
3496 LOCK_FDC(drive,1);
3497 CALL(poll_drive(1, FD_RAW_NEED_DISK));
3498 process_fd_request();
3499 /* fall through */
3500 OUT(FDGETDRVSTAT, UDRS);
3502 case FDRESET:
3503 return user_reset_fdc(drive, (int)param, 1);
3505 OUT(FDGETFDCSTAT,UFDCS);
3507 case FDWERRORCLR:
3508 CLEARSTRUCT(UDRWE);
3509 return 0;
3510 OUT(FDWERRORGET,UDRWE);
3512 case FDRAWCMD:
3513 if (type)
3514 return -EINVAL;
3515 LOCK_FDC(drive,1);
3516 set_floppy(device);
3517 CALL(i = raw_cmd_ioctl(cmd,(void *) param));
3518 process_fd_request();
3519 return i;
3521 case FDTWADDLE:
3522 LOCK_FDC(drive,1);
3523 twaddle();
3524 process_fd_request();
3525 return 0;
3527 default:
3528 return -EINVAL;
3531 if (_IOC_DIR(cmd) & _IOC_READ)
3532 return fd_copyout((void *)param, outparam, size);
3533 else
3534 return 0;
3535 #undef IOCTL_ALLOWED
3536 #undef OUT
3537 #undef IN
3540 static void config_types(void)
3542 int first=1;
3543 int drive;
3545 /* read drive info out of physical CMOS */
3546 drive=0;
3547 if (!UDP->cmos)
3548 UDP->cmos= FLOPPY0_TYPE;
3549 drive=1;
3550 if (!UDP->cmos && FLOPPY1_TYPE)
3551 UDP->cmos = FLOPPY1_TYPE;
3553 /* XXX */
3554 /* additional physical CMOS drive detection should go here */
3556 for (drive=0; drive < N_DRIVE; drive++){
3557 if (UDP->cmos >= 16)
3558 UDP->cmos = 0;
3559 if (UDP->cmos >= 0 && UDP->cmos <= NUMBER(default_drive_params))
3560 memcpy((char *) UDP,
3561 (char *) (&default_drive_params[(int)UDP->cmos].params),
3562 sizeof(struct floppy_drive_params));
3563 if (UDP->cmos){
3564 if (first)
3565 printk(KERN_INFO "Floppy drive(s): ");
3566 else
3567 printk(", ");
3568 first=0;
3569 if (UDP->cmos > 0){
3570 allowed_drive_mask |= 1 << drive;
3571 printk("fd%d is %s", drive,
3572 default_drive_params[(int)UDP->cmos].name);
3573 } else
3574 printk("fd%d is unknown type %d",drive,
3575 UDP->cmos);
3578 if (!first)
3579 printk("\n");
3582 static ssize_t floppy_read(struct file * filp, char *buf,
3583 size_t count, loff_t *ppos)
3585 struct inode *inode = filp->f_dentry->d_inode;
3586 int drive = DRIVE(inode->i_rdev);
3588 check_disk_change(inode->i_rdev);
3589 if (UTESTF(FD_DISK_CHANGED))
3590 return -ENXIO;
3591 return block_read(filp, buf, count, ppos);
3594 static ssize_t floppy_write(struct file * filp, const char * buf,
3595 size_t count, loff_t *ppos)
3597 struct inode * inode = filp->f_dentry->d_inode;
3598 int block;
3599 int ret;
3600 int drive = DRIVE(inode->i_rdev);
3602 if (!UDRS->maxblock)
3603 UDRS->maxblock=1;/* make change detectable */
3604 check_disk_change(inode->i_rdev);
3605 if (UTESTF(FD_DISK_CHANGED))
3606 return -ENXIO;
3607 if (!UTESTF(FD_DISK_WRITABLE))
3608 return -EROFS;
3609 block = (*ppos + count) >> 9;
3610 INFBOUND(UDRS->maxblock, block);
3611 ret= block_write(filp, buf, count, ppos);
3612 return ret;
3615 static int floppy_release(struct inode * inode, struct file * filp)
3617 int drive;
3619 drive = DRIVE(inode->i_rdev);
3622 * If filp is NULL, we're being called from blkdev_release
3623 * or after a failed mount attempt. In the former case the
3624 * device has already been sync'ed, and in the latter no
3625 * sync is required. Otherwise, sync if filp is writable.
3627 if (filp && (filp->f_mode & (2 | OPEN_WRITE_BIT)))
3628 block_fsync(filp, filp->f_dentry);
3630 if (UDRS->fd_ref < 0)
3631 UDRS->fd_ref=0;
3632 else if (!UDRS->fd_ref--) {
3633 DPRINT("floppy_release with fd_ref == 0");
3634 UDRS->fd_ref = 0;
3636 floppy_release_irq_and_dma();
3637 return 0;
3641 * floppy_open check for aliasing (/dev/fd0 can be the same as
3642 * /dev/PS0 etc), and disallows simultaneous access to the same
3643 * drive with different device numbers.
3645 #define RETERR(x) do{floppy_release(inode,filp); return -(x);}while(0)
3647 static int floppy_open(struct inode * inode, struct file * filp)
3649 int drive;
3650 int old_dev;
3651 int try;
3652 char *tmp;
3654 if (!filp) {
3655 DPRINT("Weird, open called with filp=0\n");
3656 return -EIO;
3659 drive = DRIVE(inode->i_rdev);
3660 if (drive >= N_DRIVE ||
3661 !(allowed_drive_mask & (1 << drive)) ||
3662 fdc_state[FDC(drive)].version == FDC_NONE)
3663 return -ENXIO;
3665 if (TYPE(inode->i_rdev) >= NUMBER(floppy_type))
3666 return -ENXIO;
3667 old_dev = UDRS->fd_device;
3668 if (UDRS->fd_ref && old_dev != MINOR(inode->i_rdev))
3669 return -EBUSY;
3671 if (!UDRS->fd_ref && (UDP->flags & FD_BROKEN_DCL)){
3672 USETF(FD_DISK_CHANGED);
3673 USETF(FD_VERIFY);
3676 if (UDRS->fd_ref == -1 ||
3677 (UDRS->fd_ref && (filp->f_flags & O_EXCL)))
3678 return -EBUSY;
3680 if (floppy_grab_irq_and_dma())
3681 return -EBUSY;
3683 if (filp->f_flags & O_EXCL)
3684 UDRS->fd_ref = -1;
3685 else
3686 UDRS->fd_ref++;
3688 if (!floppy_track_buffer){
3689 /* if opening an ED drive, reserve a big buffer,
3690 * else reserve a small one */
3691 if ((UDP->cmos == 6) || (UDP->cmos == 5))
3692 try = 64; /* Only 48 actually useful */
3693 else
3694 try = 32; /* Only 24 actually useful */
3696 tmp=(char *)fd_dma_mem_alloc(1024 * try);
3697 if (!tmp && !floppy_track_buffer) {
3698 try >>= 1; /* buffer only one side */
3699 INFBOUND(try, 16);
3700 tmp= (char *)fd_dma_mem_alloc(1024*try);
3702 if(!tmp && !floppy_track_buffer) {
3703 fallback_on_nodma_alloc(&tmp, 2048 * try);
3705 if (!tmp && !floppy_track_buffer) {
3706 DPRINT("Unable to allocate DMA memory\n");
3707 RETERR(ENXIO);
3709 if (floppy_track_buffer) {
3710 if(tmp)
3711 fd_dma_mem_free((unsigned long)tmp,try*1024);
3712 } else {
3713 buffer_min = buffer_max = -1;
3714 floppy_track_buffer = tmp;
3715 max_buffer_sectors = try;
3719 UDRS->fd_device = MINOR(inode->i_rdev);
3720 if (old_dev != -1 && old_dev != MINOR(inode->i_rdev)) {
3721 if (buffer_drive == drive)
3722 buffer_track = -1;
3723 invalidate_buffers(MKDEV(FLOPPY_MAJOR,old_dev));
3726 /* Allow ioctls if we have write-permissions even if read-only open */
3727 if ((filp->f_mode & 2) || (permission(inode,2) == 0))
3728 filp->f_mode |= IOCTL_MODE_BIT;
3729 if (filp->f_mode & 2)
3730 filp->f_mode |= OPEN_WRITE_BIT;
3732 if (UFDCS->rawcmd == 1)
3733 UFDCS->rawcmd = 2;
3735 if (filp->f_flags & O_NDELAY)
3736 return 0;
3737 if (filp->f_mode & 3) {
3738 UDRS->last_checked = 0;
3739 check_disk_change(inode->i_rdev);
3740 if (UTESTF(FD_DISK_CHANGED))
3741 RETERR(ENXIO);
3743 if ((filp->f_mode & 2) && !(UTESTF(FD_DISK_WRITABLE)))
3744 RETERR(EROFS);
3745 return 0;
3746 #undef RETERR
3750 * Check if the disk has been changed or if a change has been faked.
3752 static int check_floppy_change(kdev_t dev)
3754 int drive = DRIVE(dev);
3756 if (MAJOR(dev) != MAJOR_NR) {
3757 DPRINT("check_floppy_change: not a floppy\n");
3758 return 0;
3761 if (UTESTF(FD_DISK_CHANGED) || UTESTF(FD_VERIFY))
3762 return 1;
3764 if (UDP->checkfreq < jiffies - UDRS->last_checked){
3765 lock_fdc(drive,0);
3766 poll_drive(0,0);
3767 process_fd_request();
3770 if (UTESTF(FD_DISK_CHANGED) ||
3771 UTESTF(FD_VERIFY) ||
3772 test_bit(drive, &fake_change) ||
3773 (!TYPE(dev) && !current_type[drive]))
3774 return 1;
3775 return 0;
3778 /* revalidate the floppy disk, i.e. trigger format autodetection by reading
3779 * the bootblock (block 0). "Autodetection" is also needed to check whether
3780 * there is a disk in the drive at all... Thus we also do it for fixed
3781 * geometry formats */
3782 static int floppy_revalidate(kdev_t dev)
3784 #define NO_GEOM (!current_type[drive] && !TYPE(dev))
3785 struct buffer_head * bh;
3786 int drive=DRIVE(dev);
3787 int cf;
3789 if (UTESTF(FD_DISK_CHANGED) ||
3790 UTESTF(FD_VERIFY) ||
3791 test_bit(drive, &fake_change) ||
3792 NO_GEOM){
3793 lock_fdc(drive,0);
3794 cf = UTESTF(FD_DISK_CHANGED) || UTESTF(FD_VERIFY);
3795 if (!(cf || test_bit(drive, &fake_change) || NO_GEOM)){
3796 process_fd_request(); /*already done by another thread*/
3797 return 0;
3799 UDRS->maxblock = 0;
3800 UDRS->maxtrack = 0;
3801 if (buffer_drive == drive)
3802 buffer_track = -1;
3803 clear_bit(drive, &fake_change);
3804 UCLEARF(FD_DISK_CHANGED);
3805 if (cf)
3806 UDRS->generation++;
3807 if (NO_GEOM){
3808 /* auto-sensing */
3809 int size = floppy_blocksizes[MINOR(dev)];
3810 if (!size)
3811 size = 1024;
3812 if (!(bh = getblk(dev,0,size))){
3813 process_fd_request();
3814 return 1;
3816 if (bh && !buffer_uptodate(bh))
3817 ll_rw_block(READ, 1, &bh);
3818 process_fd_request();
3819 wait_on_buffer(bh);
3820 brelse(bh);
3821 return 0;
3823 if (cf)
3824 poll_drive(0, FD_RAW_NEED_DISK);
3825 process_fd_request();
3827 return 0;
3830 static struct file_operations floppy_fops = {
3831 NULL, /* lseek - default */
3832 floppy_read, /* read - general block-dev read */
3833 floppy_write, /* write - general block-dev write */
3834 NULL, /* readdir - bad */
3835 NULL, /* poll */
3836 fd_ioctl, /* ioctl */
3837 NULL, /* mmap */
3838 floppy_open, /* open */
3839 NULL, /* flush */
3840 floppy_release, /* release */
3841 block_fsync, /* fsync */
3842 NULL, /* fasync */
3843 check_floppy_change, /* media_change */
3844 floppy_revalidate, /* revalidate */
3848 * Floppy Driver initialization
3849 * =============================
3852 /* Determine the floppy disk controller type */
3853 /* This routine was written by David C. Niemi */
3854 __initfunc(static char get_fdc_version(void))
3856 int r;
3858 output_byte(FD_DUMPREGS); /* 82072 and better know DUMPREGS */
3859 if (FDCS->reset)
3860 return FDC_NONE;
3861 if ((r = result()) <= 0x00)
3862 return FDC_NONE; /* No FDC present ??? */
3863 if ((r==1) && (reply_buffer[0] == 0x80)){
3864 printk(KERN_INFO "FDC %d is an 8272A\n",fdc);
3865 return FDC_8272A; /* 8272a/765 don't know DUMPREGS */
3867 if (r != 10) {
3868 printk("FDC %d init: DUMPREGS: unexpected return of %d bytes.\n",
3869 fdc, r);
3870 return FDC_UNKNOWN;
3873 if(!fdc_configure()) {
3874 printk(KERN_INFO "FDC %d is an 82072\n",fdc);
3875 return FDC_82072; /* 82072 doesn't know CONFIGURE */
3878 output_byte(FD_PERPENDICULAR);
3879 if(need_more_output() == MORE_OUTPUT) {
3880 output_byte(0);
3881 } else {
3882 printk(KERN_INFO "FDC %d is an 82072A\n", fdc);
3883 return FDC_82072A; /* 82072A as found on Sparcs. */
3886 output_byte(FD_UNLOCK);
3887 r = result();
3888 if ((r == 1) && (reply_buffer[0] == 0x80)){
3889 printk(KERN_INFO "FDC %d is a pre-1991 82077\n", fdc);
3890 return FDC_82077_ORIG; /* Pre-1991 82077, doesn't know
3891 * LOCK/UNLOCK */
3893 if ((r != 1) || (reply_buffer[0] != 0x00)) {
3894 printk("FDC %d init: UNLOCK: unexpected return of %d bytes.\n",
3895 fdc, r);
3896 return FDC_UNKNOWN;
3898 output_byte(FD_PARTID);
3899 r = result();
3900 if (r != 1) {
3901 printk("FDC %d init: PARTID: unexpected return of %d bytes.\n",
3902 fdc, r);
3903 return FDC_UNKNOWN;
3905 if (reply_buffer[0] == 0x80) {
3906 printk(KERN_INFO "FDC %d is a post-1991 82077\n",fdc);
3907 return FDC_82077; /* Revised 82077AA passes all the tests */
3909 switch (reply_buffer[0] >> 5) {
3910 case 0x0:
3911 /* Either a 82078-1 or a 82078SL running at 5Volt */
3912 printk(KERN_INFO "FDC %d is an 82078.\n",fdc);
3913 return FDC_82078;
3914 case 0x1:
3915 printk(KERN_INFO "FDC %d is a 44pin 82078\n",fdc);
3916 return FDC_82078;
3917 case 0x2:
3918 printk(KERN_INFO "FDC %d is a S82078B\n", fdc);
3919 return FDC_S82078B;
3920 case 0x3:
3921 printk(KERN_INFO "FDC %d is a National Semiconductor PC87306\n", fdc);
3922 return FDC_87306;
3923 default:
3924 printk(KERN_INFO "FDC %d init: 82078 variant with unknown PARTID=%d.\n",
3925 fdc, reply_buffer[0] >> 5);
3926 return FDC_82078_UNKN;
3928 } /* get_fdc_version */
3930 /* lilo configuration */
3932 __initfunc(static void floppy_set_flags(int *ints,int param, int param2))
3934 int i;
3936 for (i=0; i < ARRAY_SIZE(default_drive_params); i++){
3937 if (param)
3938 default_drive_params[i].params.flags |= param2;
3939 else
3940 default_drive_params[i].params.flags &= ~param2;
3942 DPRINT("%s flag 0x%x\n", param2 ? "Setting" : "Clearing", param);
3945 __initfunc(static void daring(int *ints,int param, int param2))
3947 int i;
3949 for (i=0; i < ARRAY_SIZE(default_drive_params); i++){
3950 if (param){
3951 default_drive_params[i].params.select_delay = 0;
3952 default_drive_params[i].params.flags |= FD_SILENT_DCL_CLEAR;
3953 } else {
3954 default_drive_params[i].params.select_delay = 2*HZ/100;
3955 default_drive_params[i].params.flags &= ~FD_SILENT_DCL_CLEAR;
3958 DPRINT("Assuming %s floppy hardware\n", param ? "standard" : "broken");
3961 __initfunc(static void set_cmos(int *ints, int dummy, int dummy2))
3963 int current_drive=0;
3965 if (ints[0] != 2){
3966 DPRINT("wrong number of parameters for CMOS\n");
3967 return;
3969 current_drive = ints[1];
3970 if (current_drive < 0 || current_drive >= 8){
3971 DPRINT("bad drive for set_cmos\n");
3972 return;
3974 if (current_drive >= 4 && !FDC2)
3975 FDC2 = 0x370;
3976 if (ints[2] <= 0 ||
3977 (ints[2] >= NUMBER(default_drive_params) && ints[2] != 16)){
3978 DPRINT("bad CMOS code %d\n", ints[2]);
3979 return;
3981 DP->cmos = ints[2];
3982 DPRINT("setting CMOS code to %d\n", ints[2]);
3985 static struct param_table {
3986 const char *name;
3987 void (*fn)(int *ints, int param, int param2);
3988 int *var;
3989 int def_param;
3990 int param2;
3991 } config_params[]={
3992 { "allowed_drive_mask", 0, &allowed_drive_mask, 0xff, 0}, /* obsolete */
3993 { "all_drives", 0, &allowed_drive_mask, 0xff, 0 }, /* obsolete */
3994 { "asus_pci", 0, &allowed_drive_mask, 0x33, 0},
3996 { "irq", 0, &FLOPPY_IRQ, 6, 0 },
3997 { "dma", 0, &FLOPPY_DMA, 2, 0 },
3999 { "daring", daring, 0, 1, 0},
4001 { "two_fdc", 0, &FDC2, 0x370, 0 },
4002 { "one_fdc", 0, &FDC2, 0, 0 },
4004 { "thinkpad", floppy_set_flags, 0, 1, FD_INVERTED_DCL },
4005 { "broken_dcl", floppy_set_flags, 0, 1, FD_BROKEN_DCL },
4006 { "messages", floppy_set_flags, 0, 1, FTD_MSG },
4007 { "silent_dcl_clear", floppy_set_flags, 0, 1, FD_SILENT_DCL_CLEAR },
4008 { "debug", floppy_set_flags, 0, 1, FD_DEBUG },
4010 { "nodma", 0, &can_use_virtual_dma, 1, 0 },
4011 { "omnibook", 0, &can_use_virtual_dma, 1, 0 },
4012 { "yesdma", 0, &can_use_virtual_dma, 0, 0 },
4014 { "fifo_depth", 0, &fifo_depth, 0xa, 0 },
4015 { "nofifo", 0, &no_fifo, 0x20, 0 },
4016 { "usefifo", 0, &no_fifo, 0, 0 },
4018 { "cmos", set_cmos, 0, 0, 0 },
4020 { "unexpected_interrupts", 0, &print_unex, 1, 0 },
4021 { "no_unexpected_interrupts", 0, &print_unex, 0, 0 },
4022 { "L40SX", 0, &print_unex, 0, 0 } };
4024 #define FLOPPY_SETUP
4025 __initfunc(void floppy_setup(char *str, int *ints))
4027 int i;
4028 int param;
4029 if (str)
4030 for (i=0; i< ARRAY_SIZE(config_params); i++){
4031 if (strcmp(str,config_params[i].name) == 0){
4032 if (ints[0])
4033 param = ints[1];
4034 else
4035 param = config_params[i].def_param;
4036 if(config_params[i].fn)
4037 config_params[i].
4038 fn(ints,param,
4039 config_params[i].param2);
4040 if(config_params[i].var) {
4041 DPRINT("%s=%d\n", str, param);
4042 *config_params[i].var = param;
4044 return;
4047 if (str) {
4048 DPRINT("unknown floppy option [%s]\n", str);
4050 DPRINT("allowed options are:");
4051 for (i=0; i< ARRAY_SIZE(config_params); i++)
4052 printk(" %s",config_params[i].name);
4053 printk("\n");
4054 } else
4055 DPRINT("botched floppy option\n");
4056 DPRINT("Read linux/drivers/block/README.fd\n");
4059 static int have_no_fdc= -EIO;
4062 __initfunc(int floppy_init(void))
4064 int i,unit,drive;
4067 raw_cmd = 0;
4069 if (register_blkdev(MAJOR_NR,"fd",&floppy_fops)) {
4070 printk("Unable to get major %d for floppy\n",MAJOR_NR);
4071 return -EBUSY;
4074 for (i=0; i<256; i++)
4075 if (ITYPE(i))
4076 floppy_sizes[i] = (floppy_type[ITYPE(i)].size+1) >> 1;
4077 else
4078 floppy_sizes[i] = MAX_DISK_SIZE;
4080 blk_size[MAJOR_NR] = floppy_sizes;
4081 blksize_size[MAJOR_NR] = floppy_blocksizes;
4082 blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
4083 reschedule_timeout(MAXTIMEOUT, "floppy init", MAXTIMEOUT);
4084 config_types();
4086 for (i = 0; i < N_FDC; i++) {
4087 fdc = i;
4088 CLEARSTRUCT(FDCS);
4089 FDCS->dtr = -1;
4090 FDCS->dor = 0x4;
4091 #ifdef __sparc__
4092 /*sparcs don't have a DOR reset which we can fall back on to*/
4093 FDCS->version = FDC_82072A;
4094 #endif
4097 use_virtual_dma = can_use_virtual_dma & 1;
4098 fdc_state[0].address = FDC1;
4099 if (fdc_state[0].address == -1) {
4100 unregister_blkdev(MAJOR_NR,"fd");
4101 del_timer(&fd_timeout);
4102 return -ENODEV;
4104 #if N_FDC > 1
4105 fdc_state[1].address = FDC2;
4106 #endif
4108 if (floppy_grab_irq_and_dma()){
4109 unregister_blkdev(MAJOR_NR,"fd");
4110 del_timer(&fd_timeout);
4111 return -EBUSY;
4114 /* initialise drive state */
4115 for (drive = 0; drive < N_DRIVE; drive++) {
4116 CLEARSTRUCT(UDRS);
4117 CLEARSTRUCT(UDRWE);
4118 UDRS->flags = FD_VERIFY | FD_DISK_NEWCHANGE | FD_DISK_CHANGED;
4119 UDRS->fd_device = -1;
4120 floppy_track_buffer = NULL;
4121 max_buffer_sectors = 0;
4124 for (i = 0; i < N_FDC; i++) {
4125 fdc = i;
4126 FDCS->driver_version = FD_DRIVER_VERSION;
4127 for (unit=0; unit<4; unit++)
4128 FDCS->track[unit] = 0;
4129 if (FDCS->address == -1)
4130 continue;
4131 FDCS->rawcmd = 2;
4132 if (user_reset_fdc(-1,FD_RESET_ALWAYS,0)){
4133 FDCS->address = -1;
4134 FDCS->version = FDC_NONE;
4135 continue;
4137 /* Try to determine the floppy controller type */
4138 FDCS->version = get_fdc_version();
4139 if (FDCS->version == FDC_NONE){
4140 FDCS->address = -1;
4141 continue;
4143 if(can_use_virtual_dma == 2 && FDCS->version < FDC_82072A)
4144 can_use_virtual_dma = 0;
4146 have_no_fdc = 0;
4147 /* Not all FDCs seem to be able to handle the version command
4148 * properly, so force a reset for the standard FDC clones,
4149 * to avoid interrupt garbage.
4151 user_reset_fdc(-1,FD_RESET_ALWAYS,0);
4153 fdc=0;
4154 del_timer(&fd_timeout);
4155 current_drive = 0;
4156 floppy_release_irq_and_dma();
4157 initialising=0;
4158 if (have_no_fdc) {
4159 DPRINT("no floppy controllers found\n");
4160 unregister_blkdev(MAJOR_NR,"fd");
4162 return have_no_fdc;
4165 static int floppy_grab_irq_and_dma(void)
4167 unsigned long flags;
4169 INT_OFF;
4170 if (usage_count++){
4171 INT_ON;
4172 return 0;
4174 INT_ON;
4175 MOD_INC_USE_COUNT;
4176 if (fd_request_irq()) {
4177 DPRINT("Unable to grab IRQ%d for the floppy driver\n",
4178 FLOPPY_IRQ);
4179 MOD_DEC_USE_COUNT;
4180 usage_count--;
4181 return -1;
4183 if (fd_request_dma()) {
4184 DPRINT("Unable to grab DMA%d for the floppy driver\n",
4185 FLOPPY_DMA);
4186 fd_free_irq();
4187 MOD_DEC_USE_COUNT;
4188 usage_count--;
4189 return -1;
4192 for (fdc=0; fdc< N_FDC; fdc++){
4193 if (FDCS->address != -1){
4194 if (check_region(FDCS->address, 6) < 0 ||
4195 check_region(FDCS->address+7, 1) < 0) {
4196 DPRINT("Floppy io-port 0x%04lx in use\n", FDCS->address);
4197 fd_free_irq();
4198 fd_free_dma();
4199 while(--fdc >= 0) {
4200 release_region(FDCS->address, 6);
4201 release_region(FDCS->address+7, 1);
4203 MOD_DEC_USE_COUNT;
4204 usage_count--;
4205 return -1;
4207 request_region(FDCS->address, 6, "floppy");
4208 request_region(FDCS->address+7, 1, "floppy DIR");
4209 /* address + 6 is reserved, and may be taken by IDE.
4210 * Unfortunately, Adaptec doesn't know this :-(, */
4213 for (fdc=0; fdc< N_FDC; fdc++){
4214 if (FDCS->address != -1){
4215 reset_fdc_info(1);
4216 fd_outb(FDCS->dor, FD_DOR);
4219 fdc = 0;
4220 set_dor(0, ~0, 8); /* avoid immediate interrupt */
4222 for (fdc = 0; fdc < N_FDC; fdc++)
4223 if (FDCS->address != -1)
4224 fd_outb(FDCS->dor, FD_DOR);
4226 * The driver will try and free resources and relies on us
4227 * to know if they were allocated or not.
4229 fdc = 0;
4230 irqdma_allocated = 1;
4231 return 0;
4234 static void floppy_release_irq_and_dma(void)
4236 int old_fdc;
4237 #ifdef FLOPPY_SANITY_CHECK
4238 #ifndef __sparc__
4239 int drive;
4240 #endif
4241 #endif
4242 long tmpsize;
4243 unsigned long tmpaddr;
4244 unsigned long flags;
4246 INT_OFF;
4247 if (--usage_count){
4248 INT_ON;
4249 return;
4251 INT_ON;
4252 if(irqdma_allocated)
4254 fd_disable_dma();
4255 fd_free_dma();
4256 fd_free_irq();
4257 irqdma_allocated=0;
4259 set_dor(0, ~0, 8);
4260 #if N_FDC > 1
4261 set_dor(1, ~8, 0);
4262 #endif
4263 floppy_enable_hlt();
4265 if (floppy_track_buffer && max_buffer_sectors) {
4266 tmpsize = max_buffer_sectors*1024;
4267 tmpaddr = (unsigned long)floppy_track_buffer;
4268 floppy_track_buffer = 0;
4269 max_buffer_sectors = 0;
4270 buffer_min = buffer_max = -1;
4271 fd_dma_mem_free(tmpaddr, tmpsize);
4274 #ifdef FLOPPY_SANITY_CHECK
4275 #ifndef __sparc__
4276 for (drive=0; drive < N_FDC * 4; drive++)
4277 if (motor_off_timer[drive].next)
4278 printk("motor off timer %d still active\n", drive);
4279 #endif
4281 if (fd_timeout.next)
4282 printk("floppy timer still active:%s\n", timeout_message);
4283 if (fd_timer.next)
4284 printk("auxiliary floppy timer still active\n");
4285 if (floppy_tq.sync)
4286 printk("task queue still active\n");
4287 #endif
4288 old_fdc = fdc;
4289 for (fdc = 0; fdc < N_FDC; fdc++)
4290 if (FDCS->address != -1) {
4291 release_region(FDCS->address, 6);
4292 release_region(FDCS->address+7, 1);
4294 fdc = old_fdc;
4295 MOD_DEC_USE_COUNT;
4299 #ifdef MODULE
4301 extern char *get_options(char *str, int *ints);
4303 char *floppy=NULL;
4305 __initfunc(static void parse_floppy_cfg_string(char *cfg))
4307 char *ptr;
4308 int ints[11];
4310 while(*cfg) {
4311 for(ptr = cfg;*cfg && *cfg != ' ' && *cfg != '\t'; cfg++);
4312 if(*cfg) {
4313 *cfg = '\0';
4314 cfg++;
4316 if(*ptr)
4317 floppy_setup(get_options(ptr,ints),ints);
4321 __initfunc(static void mod_setup(char *pattern, void (*setup)(char *, int *)))
4323 unsigned long i;
4324 char c;
4325 int j;
4326 int match;
4327 char buffer[100];
4328 int ints[11];
4329 int length = strlen(pattern)+1;
4331 match=0;
4332 j=1;
4334 for (i=current->mm->env_start; i< current->mm->env_end; i ++){
4335 get_user(c, (char *)i);
4336 if (match){
4337 if (j==99)
4338 c='\0';
4339 buffer[j] = c;
4340 if (!c || c == ' ' || c == '\t'){
4341 if (j){
4342 buffer[j] = '\0';
4343 setup(get_options(buffer,ints),ints);
4345 j=0;
4346 } else
4347 j++;
4348 if (!c)
4349 break;
4350 continue;
4352 if ((!j && !c) || (j && c == pattern[j-1]))
4353 j++;
4354 else
4355 j=0;
4356 if (j==length){
4357 match=1;
4358 j=0;
4364 #ifdef __cplusplus
4365 extern "C" {
4366 #endif
4367 int init_module(void)
4369 printk(KERN_INFO "inserting floppy driver for " UTS_RELEASE "\n");
4371 if(floppy)
4372 parse_floppy_cfg_string(floppy);
4373 else
4374 mod_setup("floppy=", floppy_setup);
4376 return floppy_init();
4379 void cleanup_module(void)
4381 int dummy;
4383 unregister_blkdev(MAJOR_NR, "fd");
4385 blk_dev[MAJOR_NR].request_fn = 0;
4386 /* eject disk, if any */
4387 dummy = fd_eject(0);
4390 MODULE_PARM(floppy,"s");
4391 MODULE_PARM(FLOPPY_IRQ,"i");
4392 MODULE_PARM(FLOPPY_DMA,"i");
4393 MODULE_AUTHOR("Alain L. Knaff");
4394 MODULE_SUPPORTED_DEVICE("fd");
4396 #ifdef __cplusplus
4398 #endif
4400 #else
4401 /* eject the boot floppy (if we need the drive for a different root floppy) */
4402 /* This should only be called at boot time when we're sure that there's no
4403 * resource contention. */
4404 void floppy_eject(void)
4406 int dummy;
4407 if(have_no_fdc)
4408 return;
4409 if(floppy_grab_irq_and_dma()==0)
4411 lock_fdc(MAXTIMEOUT,0);
4412 dummy=fd_eject(0);
4413 process_fd_request();
4414 floppy_release_irq_and_dma();
4417 #endif