Import 2.3.9pre5
[davej-history.git] / drivers / block / floppy.c
blob7cfb115b93e96a456f53d568ea0cec7b6c4405c6
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.
106 * 1998/09/20 -- David Weinehall -- Added slow-down code for buggy PS/2-drives.
109 #define FLOPPY_SANITY_CHECK
110 #undef FLOPPY_SILENT_DCL_CLEAR
112 #define REALLY_SLOW_IO
114 #define DEBUGT 2
115 #define DCL_DEBUG /* debug disk change line */
117 /* do print messages for unexpected interrupts */
118 static int print_unex=1;
119 #include <linux/module.h>
121 /* the following is the mask of allowed drives. By default units 2 and
122 * 3 of both floppy controllers are disabled, because switching on the
123 * motor of these drives causes system hangs on some PCI computers. drive
124 * 0 is the low bit (0x1), and drive 7 is the high bit (0x80). Bits are on if
125 * a drive is allowed. */
127 static int FLOPPY_IRQ=6;
128 static int FLOPPY_DMA=2;
129 static int allowed_drive_mask = 0x33;
130 static int irqdma_allocated = 0;
133 #include <linux/sched.h>
134 #include <linux/fs.h>
135 #include <linux/kernel.h>
136 #include <linux/timer.h>
137 #include <linux/tqueue.h>
138 #define FDPATCHES
139 #include <linux/fdreg.h>
142 #include <linux/fd.h>
143 #include <linux/hdreg.h>
145 #include <linux/errno.h>
146 #include <linux/malloc.h>
147 #include <linux/mm.h>
148 #include <linux/string.h>
149 #include <linux/fcntl.h>
150 #include <linux/delay.h>
151 #include <linux/mc146818rtc.h> /* CMOS defines */
152 #include <linux/ioport.h>
153 #include <linux/interrupt.h>
154 #include <linux/init.h>
157 * PS/2 floppies have much slower step rates than regular floppies.
158 * It's been recommended that take about 1/4 of the default speed
159 * in some more extreme cases.
161 static int slow_floppy = 0;
163 #include <asm/dma.h>
164 #include <asm/irq.h>
165 #include <asm/system.h>
166 #include <asm/io.h>
167 #include <asm/uaccess.h>
169 static int can_use_virtual_dma=2;
170 /* =======
171 * can use virtual DMA:
172 * 0 = use of virtual DMA disallowed by config
173 * 1 = use of virtual DMA prescribed by config
174 * 2 = no virtual DMA preference configured. By default try hard DMA,
175 * but fall back on virtual DMA when not enough memory available
178 static int use_virtual_dma=0;
179 /* =======
180 * use virtual DMA
181 * 0 using hard DMA
182 * 1 using virtual DMA
183 * This variable is set to virtual when a DMA mem problem arises, and
184 * reset back in floppy_grab_irq_and_dma.
185 * It is not safe to reset it in other circumstances, because the floppy
186 * driver may have several buffers in use at once, and we do currently not
187 * record each buffers capabilities
190 static unsigned short virtual_dma_port=0x3f0;
191 void floppy_interrupt(int irq, void *dev_id, struct pt_regs * regs);
192 static int set_dor(int fdc, char mask, char data);
193 static inline int __get_order(unsigned long size);
194 #define K_64 0x10000 /* 64KB */
195 #include <asm/floppy.h>
198 #define MAJOR_NR FLOPPY_MAJOR
200 #include <linux/blk.h>
201 #include <linux/blkpg.h>
202 #include <linux/cdrom.h> /* for the compatibility eject ioctl */
204 #ifndef fd_get_dma_residue
205 #define fd_get_dma_residue() get_dma_residue(FLOPPY_DMA)
206 #endif
208 /* Dma Memory related stuff */
210 /* Pure 2^n version of get_order */
211 static inline int __get_order(unsigned long size)
213 int order;
215 size = (size-1) >> (PAGE_SHIFT-1);
216 order = -1;
217 do {
218 size >>= 1;
219 order++;
220 } while (size);
221 return order;
224 #ifndef fd_dma_mem_free
225 #define fd_dma_mem_free(addr, size) free_pages(addr, __get_order(size))
226 #endif
228 #ifndef fd_dma_mem_alloc
229 #define fd_dma_mem_alloc(size) __get_dma_pages(GFP_KERNEL,__get_order(size))
230 #endif
232 static inline void fallback_on_nodma_alloc(char **addr, size_t l)
234 #ifdef FLOPPY_CAN_FALLBACK_ON_NODMA
235 if(*addr)
236 return; /* we have the memory */
237 if(can_use_virtual_dma != 2)
238 return; /* no fallback allowed */
239 printk("DMA memory shortage. Temporarily falling back on virtual DMA\n");
240 *addr = (char *) nodma_mem_alloc(l);
241 #else
242 return;
243 #endif
246 /* End dma memory related stuff */
248 static unsigned long fake_change = 0;
249 static int initialising=1;
251 static inline int TYPE(kdev_t x) {
252 return (MINOR(x)>>2) & 0x1f;
254 static inline int DRIVE(kdev_t x) {
255 return (MINOR(x)&0x03) | ((MINOR(x)&0x80) >> 5);
257 #define ITYPE(x) (((x)>>2) & 0x1f)
258 #define TOMINOR(x) ((x & 3) | ((x & 4) << 5))
259 #define UNIT(x) ((x) & 0x03) /* drive on fdc */
260 #define FDC(x) (((x) & 0x04) >> 2) /* fdc of drive */
261 #define REVDRIVE(fdc, unit) ((unit) + ((fdc) << 2))
262 /* reverse mapping from unit and fdc to drive */
263 #define DP (&drive_params[current_drive])
264 #define DRS (&drive_state[current_drive])
265 #define DRWE (&write_errors[current_drive])
266 #define FDCS (&fdc_state[fdc])
267 #define CLEARF(x) (clear_bit(x##_BIT, &DRS->flags))
268 #define SETF(x) (set_bit(x##_BIT, &DRS->flags))
269 #define TESTF(x) (test_bit(x##_BIT, &DRS->flags))
271 #define UDP (&drive_params[drive])
272 #define UDRS (&drive_state[drive])
273 #define UDRWE (&write_errors[drive])
274 #define UFDCS (&fdc_state[FDC(drive)])
275 #define UCLEARF(x) (clear_bit(x##_BIT, &UDRS->flags))
276 #define USETF(x) (set_bit(x##_BIT, &UDRS->flags))
277 #define UTESTF(x) (test_bit(x##_BIT, &UDRS->flags))
279 #define DPRINT(format, args...) printk(DEVICE_NAME "%d: " format, current_drive , ## args)
281 #define PH_HEAD(floppy,head) (((((floppy)->stretch & 2) >>1) ^ head) << 2)
282 #define STRETCH(floppy) ((floppy)->stretch & FD_STRETCH)
284 #define CLEARSTRUCT(x) memset((x), 0, sizeof(*(x)))
286 #define INT_OFF save_flags(flags); cli()
287 #define INT_ON restore_flags(flags)
289 /* read/write */
290 #define COMMAND raw_cmd->cmd[0]
291 #define DR_SELECT raw_cmd->cmd[1]
292 #define TRACK raw_cmd->cmd[2]
293 #define HEAD raw_cmd->cmd[3]
294 #define SECTOR raw_cmd->cmd[4]
295 #define SIZECODE raw_cmd->cmd[5]
296 #define SECT_PER_TRACK raw_cmd->cmd[6]
297 #define GAP raw_cmd->cmd[7]
298 #define SIZECODE2 raw_cmd->cmd[8]
299 #define NR_RW 9
301 /* format */
302 #define F_SIZECODE raw_cmd->cmd[2]
303 #define F_SECT_PER_TRACK raw_cmd->cmd[3]
304 #define F_GAP raw_cmd->cmd[4]
305 #define F_FILL raw_cmd->cmd[5]
306 #define NR_F 6
309 * Maximum disk size (in kilobytes). This default is used whenever the
310 * current disk size is unknown.
311 * [Now it is rather a minimum]
313 #define MAX_DISK_SIZE 4 /* 3984*/
317 * globals used by 'result()'
319 #define MAX_REPLIES 16
320 static unsigned char reply_buffer[MAX_REPLIES];
321 static int inr; /* size of reply buffer, when called from interrupt */
322 #define ST0 (reply_buffer[0])
323 #define ST1 (reply_buffer[1])
324 #define ST2 (reply_buffer[2])
325 #define ST3 (reply_buffer[0]) /* result of GETSTATUS */
326 #define R_TRACK (reply_buffer[3])
327 #define R_HEAD (reply_buffer[4])
328 #define R_SECTOR (reply_buffer[5])
329 #define R_SIZECODE (reply_buffer[6])
331 #define SEL_DLY (2*HZ/100)
333 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
335 * this struct defines the different floppy drive types.
337 static struct {
338 struct floppy_drive_params params;
339 const char *name; /* name printed while booting */
340 } default_drive_params[]= {
341 /* NOTE: the time values in jiffies should be in msec!
342 CMOS drive type
343 | Maximum data rate supported by drive type
344 | | Head load time, msec
345 | | | Head unload time, msec (not used)
346 | | | | Step rate interval, usec
347 | | | | | Time needed for spinup time (jiffies)
348 | | | | | | Timeout for spinning down (jiffies)
349 | | | | | | | Spindown offset (where disk stops)
350 | | | | | | | | Select delay
351 | | | | | | | | | RPS
352 | | | | | | | | | | Max number of tracks
353 | | | | | | | | | | | Interrupt timeout
354 | | | | | | | | | | | | Max nonintlv. sectors
355 | | | | | | | | | | | | | -Max Errors- flags */
356 {{0, 500, 16, 16, 8000, 1*HZ, 3*HZ, 0, SEL_DLY, 5, 80, 3*HZ, 20, {3,1,2,0,2}, 0,
357 0, { 7, 4, 8, 2, 1, 5, 3,10}, 3*HZ/2, 0 }, "unknown" },
359 {{1, 300, 16, 16, 8000, 1*HZ, 3*HZ, 0, SEL_DLY, 5, 40, 3*HZ, 17, {3,1,2,0,2}, 0,
360 0, { 1, 0, 0, 0, 0, 0, 0, 0}, 3*HZ/2, 1 }, "360K PC" }, /*5 1/4 360 KB PC*/
362 {{2, 500, 16, 16, 6000, 4*HZ/10, 3*HZ, 14, SEL_DLY, 6, 83, 3*HZ, 17, {3,1,2,0,2}, 0,
363 0, { 2, 5, 6,23,10,20,12, 0}, 3*HZ/2, 2 }, "1.2M" }, /*5 1/4 HD AT*/
365 {{3, 250, 16, 16, 3000, 1*HZ, 3*HZ, 0, SEL_DLY, 5, 83, 3*HZ, 20, {3,1,2,0,2}, 0,
366 0, { 4,22,21,30, 3, 0, 0, 0}, 3*HZ/2, 4 }, "720k" }, /*3 1/2 DD*/
368 {{4, 500, 16, 16, 4000, 4*HZ/10, 3*HZ, 10, SEL_DLY, 5, 83, 3*HZ, 20, {3,1,2,0,2}, 0,
369 0, { 7, 4,25,22,31,21,29,11}, 3*HZ/2, 7 }, "1.44M" }, /*3 1/2 HD*/
371 {{5, 1000, 15, 8, 3000, 4*HZ/10, 3*HZ, 10, SEL_DLY, 5, 83, 3*HZ, 40, {3,1,2,0,2}, 0,
372 0, { 7, 8, 4,25,28,22,31,21}, 3*HZ/2, 8 }, "2.88M AMI BIOS" }, /*3 1/2 ED*/
374 {{6, 1000, 15, 8, 3000, 4*HZ/10, 3*HZ, 10, SEL_DLY, 5, 83, 3*HZ, 40, {3,1,2,0,2}, 0,
375 0, { 7, 8, 4,25,28,22,31,21}, 3*HZ/2, 8 }, "2.88M" } /*3 1/2 ED*/
376 /* | --autodetected formats--- | | |
377 * read_track | | Name printed when booting
378 * | Native format
379 * Frequency of disk change checks */
382 static struct floppy_drive_params drive_params[N_DRIVE];
383 static struct floppy_drive_struct drive_state[N_DRIVE];
384 static struct floppy_write_errors write_errors[N_DRIVE];
385 static struct floppy_raw_cmd *raw_cmd, default_raw_cmd;
388 * This struct defines the different floppy types.
390 * Bit 0 of 'stretch' tells if the tracks need to be doubled for some
391 * types (e.g. 360kB diskette in 1.2MB drive, etc.). Bit 1 of 'stretch'
392 * tells if the disk is in Commodore 1581 format, which means side 0 sectors
393 * are located on side 1 of the disk but with a side 0 ID, and vice-versa.
394 * This is the same as the Sharp MZ-80 5.25" CP/M disk format, except that the
395 * 1581's logical side 0 is on physical side 1, whereas the Sharp's logical
396 * side 0 is on physical side 0 (but with the misnamed sector IDs).
397 * 'stretch' should probably be renamed to something more general, like
398 * 'options'. Other parameters should be self-explanatory (see also
399 * setfdprm(8)).
401 static struct floppy_struct floppy_type[32] = {
402 { 0, 0,0, 0,0,0x00,0x00,0x00,0x00,NULL }, /* 0 no testing */
403 { 720, 9,2,40,0,0x2A,0x02,0xDF,0x50,"d360" }, /* 1 360KB PC */
404 { 2400,15,2,80,0,0x1B,0x00,0xDF,0x54,"h1200" }, /* 2 1.2MB AT */
405 { 720, 9,1,80,0,0x2A,0x02,0xDF,0x50,"D360" }, /* 3 360KB SS 3.5" */
406 { 1440, 9,2,80,0,0x2A,0x02,0xDF,0x50,"D720" }, /* 4 720KB 3.5" */
407 { 720, 9,2,40,1,0x23,0x01,0xDF,0x50,"h360" }, /* 5 360KB AT */
408 { 1440, 9,2,80,0,0x23,0x01,0xDF,0x50,"h720" }, /* 6 720KB AT */
409 { 2880,18,2,80,0,0x1B,0x00,0xCF,0x6C,"H1440" }, /* 7 1.44MB 3.5" */
410 { 5760,36,2,80,0,0x1B,0x43,0xAF,0x54,"E2880" }, /* 8 2.88MB 3.5" */
411 { 6240,39,2,80,0,0x1B,0x43,0xAF,0x28,"E3120"}, /* 9 3.12MB 3.5" */
413 { 2880,18,2,80,0,0x25,0x00,0xDF,0x02,"h1440" }, /* 10 1.44MB 5.25" */
414 { 3360,21,2,80,0,0x1C,0x00,0xCF,0x0C,"H1680" }, /* 11 1.68MB 3.5" */
415 { 820,10,2,41,1,0x25,0x01,0xDF,0x2E,"h410" }, /* 12 410KB 5.25" */
416 { 1640,10,2,82,0,0x25,0x02,0xDF,0x2E,"H820" }, /* 13 820KB 3.5" */
417 { 2952,18,2,82,0,0x25,0x00,0xDF,0x02,"h1476" }, /* 14 1.48MB 5.25" */
418 { 3444,21,2,82,0,0x25,0x00,0xDF,0x0C,"H1722" }, /* 15 1.72MB 3.5" */
419 { 840,10,2,42,1,0x25,0x01,0xDF,0x2E,"h420" }, /* 16 420KB 5.25" */
420 { 1660,10,2,83,0,0x25,0x02,0xDF,0x2E,"H830" }, /* 17 830KB 3.5" */
421 { 2988,18,2,83,0,0x25,0x00,0xDF,0x02,"h1494" }, /* 18 1.49MB 5.25" */
422 { 3486,21,2,83,0,0x25,0x00,0xDF,0x0C,"H1743" }, /* 19 1.74 MB 3.5" */
424 { 1760,11,2,80,0,0x1C,0x09,0xCF,0x00,"h880" }, /* 20 880KB 5.25" */
425 { 2080,13,2,80,0,0x1C,0x01,0xCF,0x00,"D1040" }, /* 21 1.04MB 3.5" */
426 { 2240,14,2,80,0,0x1C,0x19,0xCF,0x00,"D1120" }, /* 22 1.12MB 3.5" */
427 { 3200,20,2,80,0,0x1C,0x20,0xCF,0x2C,"h1600" }, /* 23 1.6MB 5.25" */
428 { 3520,22,2,80,0,0x1C,0x08,0xCF,0x2e,"H1760" }, /* 24 1.76MB 3.5" */
429 { 3840,24,2,80,0,0x1C,0x20,0xCF,0x00,"H1920" }, /* 25 1.92MB 3.5" */
430 { 6400,40,2,80,0,0x25,0x5B,0xCF,0x00,"E3200" }, /* 26 3.20MB 3.5" */
431 { 7040,44,2,80,0,0x25,0x5B,0xCF,0x00,"E3520" }, /* 27 3.52MB 3.5" */
432 { 7680,48,2,80,0,0x25,0x63,0xCF,0x00,"E3840" }, /* 28 3.84MB 3.5" */
434 { 3680,23,2,80,0,0x1C,0x10,0xCF,0x00,"H1840" }, /* 29 1.84MB 3.5" */
435 { 1600,10,2,80,0,0x25,0x02,0xDF,0x2E,"D800" }, /* 30 800KB 3.5" */
436 { 3200,20,2,80,0,0x1C,0x00,0xCF,0x2C,"H1600" }, /* 31 1.6MB 3.5" */
439 #define NUMBER(x) (sizeof(x) / sizeof(*(x)))
440 #define SECTSIZE (_FD_SECTSIZE(*floppy))
442 /* Auto-detection: Disk type used until the next media change occurs. */
443 static struct floppy_struct *current_type[N_DRIVE] = {
444 NULL, NULL, NULL, NULL,
445 NULL, NULL, NULL, NULL
449 * User-provided type information. current_type points to
450 * the respective entry of this array.
452 static struct floppy_struct user_params[N_DRIVE];
454 static int floppy_sizes[256];
455 static int floppy_blocksizes[256] = { 0, };
458 * The driver is trying to determine the correct media format
459 * while probing is set. rw_interrupt() clears it after a
460 * successful access.
462 static int probing = 0;
464 /* Synchronization of FDC access. */
465 #define FD_COMMAND_NONE -1
466 #define FD_COMMAND_ERROR 2
467 #define FD_COMMAND_OKAY 3
469 static volatile int command_status = FD_COMMAND_NONE, fdc_busy = 0;
470 static DECLARE_WAIT_QUEUE_HEAD(fdc_wait);
471 static DECLARE_WAIT_QUEUE_HEAD(command_done);
473 #define NO_SIGNAL (!interruptible || !signal_pending(current))
474 #define CALL(x) if ((x) == -EINTR) return -EINTR
475 #define ECALL(x) if ((ret = (x))) return ret;
476 #define _WAIT(x,i) CALL(ret=wait_til_done((x),i))
477 #define WAIT(x) _WAIT((x),interruptible)
478 #define IWAIT(x) _WAIT((x),1)
480 /* Errors during formatting are counted here. */
481 static int format_errors;
483 /* Format request descriptor. */
484 static struct format_descr format_req;
487 * Rate is 0 for 500kb/s, 1 for 300kbps, 2 for 250kbps
488 * Spec1 is 0xSH, where S is stepping rate (F=1ms, E=2ms, D=3ms etc),
489 * H is head unload time (1=16ms, 2=32ms, etc)
493 * Track buffer
494 * Because these are written to by the DMA controller, they must
495 * not contain a 64k byte boundary crossing, or data will be
496 * corrupted/lost.
498 static char *floppy_track_buffer=0;
499 static int max_buffer_sectors=0;
501 static int *errors;
502 typedef void (*done_f)(int);
503 static struct cont_t {
504 void (*interrupt)(void); /* this is called after the interrupt of the
505 * main command */
506 void (*redo)(void); /* this is called to retry the operation */
507 void (*error)(void); /* this is called to tally an error */
508 done_f done; /* this is called to say if the operation has
509 * succeeded/failed */
510 } *cont=NULL;
512 static void floppy_ready(void);
513 static void floppy_start(void);
514 static void process_fd_request(void);
515 static void recalibrate_floppy(void);
516 static void floppy_shutdown(void);
518 static int floppy_grab_irq_and_dma(void);
519 static void floppy_release_irq_and_dma(void);
522 * The "reset" variable should be tested whenever an interrupt is scheduled,
523 * after the commands have been sent. This is to ensure that the driver doesn't
524 * get wedged when the interrupt doesn't come because of a failed command.
525 * reset doesn't need to be tested before sending commands, because
526 * output_byte is automatically disabled when reset is set.
528 #define CHECK_RESET { if (FDCS->reset){ reset_fdc(); return; } }
529 static void reset_fdc(void);
532 * These are global variables, as that's the easiest way to give
533 * information to interrupts. They are the data used for the current
534 * request.
536 #define NO_TRACK -1
537 #define NEED_1_RECAL -2
538 #define NEED_2_RECAL -3
540 /* */
541 static int usage_count = 0;
544 /* buffer related variables */
545 static int buffer_track = -1;
546 static int buffer_drive = -1;
547 static int buffer_min = -1;
548 static int buffer_max = -1;
550 /* fdc related variables, should end up in a struct */
551 static struct floppy_fdc_state fdc_state[N_FDC];
552 static int fdc; /* current fdc */
554 static struct floppy_struct *_floppy = floppy_type;
555 static unsigned char current_drive = 0;
556 static long current_count_sectors = 0;
557 static unsigned char sector_t; /* sector in track */
558 static unsigned char in_sector_offset; /* offset within physical sector,
559 * expressed in units of 512 bytes */
561 #ifndef fd_eject
562 #define fd_eject(x) -EINVAL
563 #endif
566 #ifdef DEBUGT
567 static long unsigned debugtimer;
568 #endif
571 * Debugging
572 * =========
574 static inline void set_debugt(void)
576 #ifdef DEBUGT
577 debugtimer = jiffies;
578 #endif
581 static inline void debugt(const char *message)
583 #ifdef DEBUGT
584 if (DP->flags & DEBUGT)
585 printk("%s dtime=%lu\n", message, jiffies-debugtimer);
586 #endif
589 typedef void (*timeout_fn)(unsigned long);
590 static struct timer_list fd_timeout ={ NULL, NULL, 0, 0,
591 (timeout_fn) floppy_shutdown };
593 static const char *timeout_message;
595 #ifdef FLOPPY_SANITY_CHECK
596 static void is_alive(const char *message)
598 /* this routine checks whether the floppy driver is "alive" */
599 if (fdc_busy && command_status < 2 && !fd_timeout.prev){
600 DPRINT("timeout handler died: %s\n",message);
603 #endif
605 #ifdef FLOPPY_SANITY_CHECK
607 #define OLOGSIZE 20
609 static void (*lasthandler)(void) = NULL;
610 static int interruptjiffies=0;
611 static int resultjiffies=0;
612 static int resultsize=0;
613 static int lastredo=0;
615 static struct output_log {
616 unsigned char data;
617 unsigned char status;
618 unsigned long jiffies;
619 } output_log[OLOGSIZE];
621 static int output_log_pos=0;
622 #endif
624 #define CURRENTD -1
625 #define MAXTIMEOUT -2
627 static void reschedule_timeout(int drive, const char *message, int marg)
629 if (drive == CURRENTD)
630 drive = current_drive;
631 del_timer(&fd_timeout);
632 if (drive < 0 || drive > N_DRIVE) {
633 fd_timeout.expires = jiffies + 20*HZ;
634 drive=0;
635 } else
636 fd_timeout.expires = jiffies + UDP->timeout;
637 add_timer(&fd_timeout);
638 if (UDP->flags & FD_DEBUG){
639 DPRINT("reschedule timeout ");
640 printk(message, marg);
641 printk("\n");
643 timeout_message = message;
646 static int maximum(int a, int b)
648 if(a > b)
649 return a;
650 else
651 return b;
653 #define INFBOUND(a,b) (a)=maximum((a),(b));
655 static int minimum(int a, int b)
657 if(a < b)
658 return a;
659 else
660 return b;
662 #define SUPBOUND(a,b) (a)=minimum((a),(b));
666 * Bottom half floppy driver.
667 * ==========================
669 * This part of the file contains the code talking directly to the hardware,
670 * and also the main service loop (seek-configure-spinup-command)
674 * disk change.
675 * This routine is responsible for maintaining the FD_DISK_CHANGE flag,
676 * and the last_checked date.
678 * last_checked is the date of the last check which showed 'no disk change'
679 * FD_DISK_CHANGE is set under two conditions:
680 * 1. The floppy has been changed after some i/o to that floppy already
681 * took place.
682 * 2. No floppy disk is in the drive. This is done in order to ensure that
683 * requests are quickly flushed in case there is no disk in the drive. It
684 * follows that FD_DISK_CHANGE can only be cleared if there is a disk in
685 * the drive.
687 * For 1., maxblock is observed. Maxblock is 0 if no i/o has taken place yet.
688 * For 2., FD_DISK_NEWCHANGE is watched. FD_DISK_NEWCHANGE is cleared on
689 * each seek. If a disk is present, the disk change line should also be
690 * cleared on each seek. Thus, if FD_DISK_NEWCHANGE is clear, but the disk
691 * change line is set, this means either that no disk is in the drive, or
692 * that it has been removed since the last seek.
694 * This means that we really have a third possibility too:
695 * The floppy has been changed after the last seek.
698 static int disk_change(int drive)
700 int fdc=FDC(drive);
701 #ifdef FLOPPY_SANITY_CHECK
702 if (jiffies - UDRS->select_date < UDP->select_delay)
703 DPRINT("WARNING disk change called early\n");
704 if (!(FDCS->dor & (0x10 << UNIT(drive))) ||
705 (FDCS->dor & 3) != UNIT(drive) ||
706 fdc != FDC(drive)){
707 DPRINT("probing disk change on unselected drive\n");
708 DPRINT("drive=%d fdc=%d dor=%x\n",drive, FDC(drive),
709 FDCS->dor);
711 #endif
713 #ifdef DCL_DEBUG
714 if (UDP->flags & FD_DEBUG){
715 DPRINT("checking disk change line for drive %d\n",drive);
716 DPRINT("jiffies=%ld\n", jiffies);
717 DPRINT("disk change line=%x\n",fd_inb(FD_DIR)&0x80);
718 DPRINT("flags=%lx\n",UDRS->flags);
720 #endif
721 if (UDP->flags & FD_BROKEN_DCL)
722 return UTESTF(FD_DISK_CHANGED);
723 if ((fd_inb(FD_DIR) ^ UDP->flags) & 0x80){
724 USETF(FD_VERIFY); /* verify write protection */
725 if (UDRS->maxblock){
726 /* mark it changed */
727 USETF(FD_DISK_CHANGED);
730 /* invalidate its geometry */
731 if (UDRS->keep_data >= 0) {
732 if ((UDP->flags & FTD_MSG) &&
733 current_type[drive] != NULL)
734 DPRINT("Disk type is undefined after "
735 "disk change\n");
736 current_type[drive] = NULL;
737 floppy_sizes[TOMINOR(drive)] = MAX_DISK_SIZE;
740 /*USETF(FD_DISK_NEWCHANGE);*/
741 return 1;
742 } else {
743 UDRS->last_checked=jiffies;
744 UCLEARF(FD_DISK_NEWCHANGE);
746 return 0;
749 static inline int is_selected(int dor, int unit)
751 return ((dor & (0x10 << unit)) && (dor &3) == unit);
754 static int set_dor(int fdc, char mask, char data)
756 register unsigned char drive, unit, newdor,olddor;
758 if (FDCS->address == -1)
759 return -1;
761 olddor = FDCS->dor;
762 newdor = (olddor & mask) | data;
763 if (newdor != olddor){
764 unit = olddor & 0x3;
765 if (is_selected(olddor, unit) && !is_selected(newdor,unit)){
766 drive = REVDRIVE(fdc,unit);
767 #ifdef DCL_DEBUG
768 if (UDP->flags & FD_DEBUG){
769 DPRINT("calling disk change from set_dor\n");
771 #endif
772 disk_change(drive);
774 FDCS->dor = newdor;
775 fd_outb(newdor, FD_DOR);
777 unit = newdor & 0x3;
778 if (!is_selected(olddor, unit) && is_selected(newdor,unit)){
779 drive = REVDRIVE(fdc,unit);
780 UDRS->select_date = jiffies;
784 * We should propogate failures to grab the resources back
785 * nicely from here. Actually we ought to rewrite the fd
786 * driver some day too.
788 if (newdor & FLOPPY_MOTOR_MASK)
789 floppy_grab_irq_and_dma();
790 if (olddor & FLOPPY_MOTOR_MASK)
791 floppy_release_irq_and_dma();
792 return olddor;
795 static void twaddle(void)
797 if (DP->select_delay)
798 return;
799 fd_outb(FDCS->dor & ~(0x10<<UNIT(current_drive)),FD_DOR);
800 fd_outb(FDCS->dor, FD_DOR);
801 DRS->select_date = jiffies;
804 /* reset all driver information about the current fdc. This is needed after
805 * a reset, and after a raw command. */
806 static void reset_fdc_info(int mode)
808 int drive;
810 FDCS->spec1 = FDCS->spec2 = -1;
811 FDCS->need_configure = 1;
812 FDCS->perp_mode = 1;
813 FDCS->rawcmd = 0;
814 for (drive = 0; drive < N_DRIVE; drive++)
815 if (FDC(drive) == fdc &&
816 (mode || UDRS->track != NEED_1_RECAL))
817 UDRS->track = NEED_2_RECAL;
820 /* selects the fdc and drive, and enables the fdc's input/dma. */
821 static void set_fdc(int drive)
823 if (drive >= 0 && drive < N_DRIVE){
824 fdc = FDC(drive);
825 current_drive = drive;
827 if (fdc != 1 && fdc != 0) {
828 printk("bad fdc value\n");
829 return;
831 set_dor(fdc,~0,8);
832 #if N_FDC > 1
833 set_dor(1-fdc, ~8, 0);
834 #endif
835 if (FDCS->rawcmd == 2)
836 reset_fdc_info(1);
837 if (fd_inb(FD_STATUS) != STATUS_READY)
838 FDCS->reset = 1;
841 /* locks the driver */
842 static int lock_fdc(int drive, int interruptible)
844 unsigned long flags;
846 if (!usage_count){
847 printk(KERN_ERR "Trying to lock fdc while usage count=0\n");
848 return -1;
850 if(floppy_grab_irq_and_dma()==-1)
851 return -EBUSY;
852 INT_OFF;
853 while (fdc_busy && NO_SIGNAL)
854 interruptible_sleep_on(&fdc_wait);
855 if (fdc_busy){
856 INT_ON;
857 return -EINTR;
859 fdc_busy = 1;
860 INT_ON;
861 command_status = FD_COMMAND_NONE;
862 reschedule_timeout(drive, "lock fdc", 0);
863 set_fdc(drive);
864 return 0;
867 #define LOCK_FDC(drive,interruptible) \
868 if (lock_fdc(drive,interruptible)) return -EINTR;
871 /* unlocks the driver */
872 static inline void unlock_fdc(void)
874 raw_cmd = 0;
875 if (!fdc_busy)
876 DPRINT("FDC access conflict!\n");
878 if (DEVICE_INTR)
879 DPRINT("device interrupt still active at FDC release: %p!\n",
880 DEVICE_INTR);
881 command_status = FD_COMMAND_NONE;
882 del_timer(&fd_timeout);
883 cont = NULL;
884 fdc_busy = 0;
885 floppy_release_irq_and_dma();
886 wake_up(&fdc_wait);
889 /* switches the motor off after a given timeout */
890 static void motor_off_callback(unsigned long nr)
892 unsigned char mask = ~(0x10 << UNIT(nr));
894 set_dor(FDC(nr), mask, 0);
897 static struct timer_list motor_off_timer[N_DRIVE] = {
898 { NULL, NULL, 0, 0, motor_off_callback },
899 { NULL, NULL, 0, 1, motor_off_callback },
900 { NULL, NULL, 0, 2, motor_off_callback },
901 { NULL, NULL, 0, 3, motor_off_callback },
902 { NULL, NULL, 0, 4, motor_off_callback },
903 { NULL, NULL, 0, 5, motor_off_callback },
904 { NULL, NULL, 0, 6, motor_off_callback },
905 { NULL, NULL, 0, 7, motor_off_callback }
908 /* schedules motor off */
909 static void floppy_off(unsigned int drive)
911 unsigned long volatile delta;
912 register int fdc=FDC(drive);
914 if (!(FDCS->dor & (0x10 << UNIT(drive))))
915 return;
917 del_timer(motor_off_timer+drive);
919 /* make spindle stop in a position which minimizes spinup time
920 * next time */
921 if (UDP->rps){
922 delta = jiffies - UDRS->first_read_date + HZ -
923 UDP->spindown_offset;
924 delta = ((delta * UDP->rps) % HZ) / UDP->rps;
925 motor_off_timer[drive].expires = jiffies + UDP->spindown - delta;
927 add_timer(motor_off_timer+drive);
931 * cycle through all N_DRIVE floppy drives, for disk change testing.
932 * stopping at current drive. This is done before any long operation, to
933 * be sure to have up to date disk change information.
935 static void scandrives(void)
937 int i, drive, saved_drive;
939 if (DP->select_delay)
940 return;
942 saved_drive = current_drive;
943 for (i=0; i < N_DRIVE; i++){
944 drive = (saved_drive + i + 1) % N_DRIVE;
945 if (UDRS->fd_ref == 0 || UDP->select_delay != 0)
946 continue; /* skip closed drives */
947 set_fdc(drive);
948 if (!(set_dor(fdc, ~3, UNIT(drive) | (0x10 << UNIT(drive))) &
949 (0x10 << UNIT(drive))))
950 /* switch the motor off again, if it was off to
951 * begin with */
952 set_dor(fdc, ~(0x10 << UNIT(drive)), 0);
954 set_fdc(saved_drive);
957 static void empty(void)
961 static struct tq_struct floppy_tq =
962 { 0, 0, 0, 0 };
964 static void schedule_bh( void (*handler)(void*) )
966 floppy_tq.routine = (void *)(void *) handler;
967 queue_task(&floppy_tq, &tq_immediate);
968 mark_bh(IMMEDIATE_BH);
971 static struct timer_list fd_timer ={ NULL, NULL, 0, 0, 0 };
973 static void cancel_activity(void)
975 CLEAR_INTR;
976 floppy_tq.routine = (void *)(void *) empty;
977 del_timer(&fd_timer);
980 /* this function makes sure that the disk stays in the drive during the
981 * transfer */
982 static void fd_watchdog(void)
984 #ifdef DCL_DEBUG
985 if (DP->flags & FD_DEBUG){
986 DPRINT("calling disk change from watchdog\n");
988 #endif
990 if (disk_change(current_drive)){
991 DPRINT("disk removed during i/o\n");
992 cancel_activity();
993 cont->done(0);
994 reset_fdc();
995 } else {
996 del_timer(&fd_timer);
997 fd_timer.function = (timeout_fn) fd_watchdog;
998 fd_timer.expires = jiffies + HZ / 10;
999 add_timer(&fd_timer);
1003 static void main_command_interrupt(void)
1005 del_timer(&fd_timer);
1006 cont->interrupt();
1009 /* waits for a delay (spinup or select) to pass */
1010 static int wait_for_completion(int delay, timeout_fn function)
1012 if (FDCS->reset){
1013 reset_fdc(); /* do the reset during sleep to win time
1014 * if we don't need to sleep, it's a good
1015 * occasion anyways */
1016 return 1;
1019 if ((signed) (jiffies - delay) < 0){
1020 del_timer(&fd_timer);
1021 fd_timer.function = function;
1022 fd_timer.expires = delay;
1023 add_timer(&fd_timer);
1024 return 1;
1026 return 0;
1029 static int hlt_disabled=0;
1030 static void floppy_disable_hlt(void)
1032 unsigned long flags;
1034 INT_OFF;
1035 if (!hlt_disabled){
1036 hlt_disabled=1;
1037 #ifdef HAVE_DISABLE_HLT
1038 disable_hlt();
1039 #endif
1041 INT_ON;
1044 static void floppy_enable_hlt(void)
1046 unsigned long flags;
1048 INT_OFF;
1049 if (hlt_disabled){
1050 hlt_disabled=0;
1051 #ifdef HAVE_DISABLE_HLT
1052 enable_hlt();
1053 #endif
1055 INT_ON;
1059 static void setup_DMA(void)
1061 unsigned long flags;
1062 unsigned long f;
1064 #ifdef FLOPPY_SANITY_CHECK
1065 if (raw_cmd->length == 0){
1066 int i;
1068 printk("zero dma transfer size:");
1069 for (i=0; i < raw_cmd->cmd_count; i++)
1070 printk("%x,", raw_cmd->cmd[i]);
1071 printk("\n");
1072 cont->done(0);
1073 FDCS->reset = 1;
1074 return;
1076 if (((unsigned long) raw_cmd->kernel_data) % 512){
1077 printk("non aligned address: %p\n", raw_cmd->kernel_data);
1078 cont->done(0);
1079 FDCS->reset=1;
1080 return;
1082 #endif
1083 INT_OFF;
1084 f=claim_dma_lock();
1085 fd_disable_dma();
1086 #ifdef fd_dma_setup
1087 if(fd_dma_setup(raw_cmd->kernel_data, raw_cmd->length,
1088 (raw_cmd->flags & FD_RAW_READ)?
1089 DMA_MODE_READ : DMA_MODE_WRITE,
1090 FDCS->address) < 0) {
1091 release_dma_lock(f);
1092 INT_ON;
1093 cont->done(0);
1094 FDCS->reset=1;
1095 return;
1097 release_dma_lock(f);
1098 #else
1099 fd_clear_dma_ff();
1100 fd_cacheflush(raw_cmd->kernel_data, raw_cmd->length);
1101 fd_set_dma_mode((raw_cmd->flags & FD_RAW_READ)?
1102 DMA_MODE_READ : DMA_MODE_WRITE);
1103 fd_set_dma_addr(raw_cmd->kernel_data);
1104 fd_set_dma_count(raw_cmd->length);
1105 virtual_dma_port = FDCS->address;
1106 fd_enable_dma();
1107 release_dma_lock(f);
1108 #endif
1109 INT_ON;
1110 floppy_disable_hlt();
1113 static void show_floppy(void);
1115 /* waits until the fdc becomes ready */
1116 static int wait_til_ready(void)
1118 int counter, status;
1119 if(FDCS->reset)
1120 return -1;
1121 for (counter = 0; counter < 10000; counter++) {
1122 status = fd_inb(FD_STATUS);
1123 if (status & STATUS_READY)
1124 return status;
1126 if (!initialising) {
1127 DPRINT("Getstatus times out (%x) on fdc %d\n",
1128 status, fdc);
1129 show_floppy();
1131 FDCS->reset = 1;
1132 return -1;
1135 /* sends a command byte to the fdc */
1136 static int output_byte(char byte)
1138 int status;
1140 if ((status = wait_til_ready()) < 0)
1141 return -1;
1142 if ((status & (STATUS_READY|STATUS_DIR|STATUS_DMA)) == STATUS_READY){
1143 fd_outb(byte,FD_DATA);
1144 #ifdef FLOPPY_SANITY_CHECK
1145 output_log[output_log_pos].data = byte;
1146 output_log[output_log_pos].status = status;
1147 output_log[output_log_pos].jiffies = jiffies;
1148 output_log_pos = (output_log_pos + 1) % OLOGSIZE;
1149 #endif
1150 return 0;
1152 FDCS->reset = 1;
1153 if (!initialising) {
1154 DPRINT("Unable to send byte %x to FDC. Fdc=%x Status=%x\n",
1155 byte, fdc, status);
1156 show_floppy();
1158 return -1;
1160 #define LAST_OUT(x) if (output_byte(x)<0){ reset_fdc();return;}
1162 /* gets the response from the fdc */
1163 static int result(void)
1165 int i, status;
1167 for(i=0; i < MAX_REPLIES; i++) {
1168 if ((status = wait_til_ready()) < 0)
1169 break;
1170 status &= STATUS_DIR|STATUS_READY|STATUS_BUSY|STATUS_DMA;
1171 if ((status & ~STATUS_BUSY) == STATUS_READY){
1172 #ifdef FLOPPY_SANITY_CHECK
1173 resultjiffies = jiffies;
1174 resultsize = i;
1175 #endif
1176 return i;
1178 if (status == (STATUS_DIR|STATUS_READY|STATUS_BUSY))
1179 reply_buffer[i] = fd_inb(FD_DATA);
1180 else
1181 break;
1183 if(!initialising) {
1184 DPRINT("get result error. Fdc=%d Last status=%x Read bytes=%d\n",
1185 fdc, status, i);
1186 show_floppy();
1188 FDCS->reset = 1;
1189 return -1;
1192 #define MORE_OUTPUT -2
1193 /* does the fdc need more output? */
1194 static int need_more_output(void)
1196 int status;
1197 if( (status = wait_til_ready()) < 0)
1198 return -1;
1199 if ((status & (STATUS_READY|STATUS_DIR|STATUS_DMA)) == STATUS_READY)
1200 return MORE_OUTPUT;
1201 return result();
1204 /* Set perpendicular mode as required, based on data rate, if supported.
1205 * 82077 Now tested. 1Mbps data rate only possible with 82077-1.
1207 static inline void perpendicular_mode(void)
1209 unsigned char perp_mode;
1211 if (raw_cmd->rate & 0x40){
1212 switch(raw_cmd->rate & 3){
1213 case 0:
1214 perp_mode=2;
1215 break;
1216 case 3:
1217 perp_mode=3;
1218 break;
1219 default:
1220 DPRINT("Invalid data rate for perpendicular mode!\n");
1221 cont->done(0);
1222 FDCS->reset = 1; /* convenient way to return to
1223 * redo without to much hassle (deep
1224 * stack et al. */
1225 return;
1227 } else
1228 perp_mode = 0;
1230 if (FDCS->perp_mode == perp_mode)
1231 return;
1232 if (FDCS->version >= FDC_82077_ORIG) {
1233 output_byte(FD_PERPENDICULAR);
1234 output_byte(perp_mode);
1235 FDCS->perp_mode = perp_mode;
1236 } else if (perp_mode) {
1237 DPRINT("perpendicular mode not supported by this FDC.\n");
1239 } /* perpendicular_mode */
1241 static int fifo_depth = 0xa;
1242 static int no_fifo = 0;
1244 static int fdc_configure(void)
1246 /* Turn on FIFO */
1247 output_byte(FD_CONFIGURE);
1248 if(need_more_output() != MORE_OUTPUT)
1249 return 0;
1250 output_byte(0);
1251 output_byte(0x10 | (no_fifo & 0x20) | (fifo_depth & 0xf));
1252 output_byte(0); /* pre-compensation from track
1253 0 upwards */
1254 return 1;
1257 #define NOMINAL_DTR 500
1259 /* Issue a "SPECIFY" command to set the step rate time, head unload time,
1260 * head load time, and DMA disable flag to values needed by floppy.
1262 * The value "dtr" is the data transfer rate in Kbps. It is needed
1263 * to account for the data rate-based scaling done by the 82072 and 82077
1264 * FDC types. This parameter is ignored for other types of FDCs (i.e.
1265 * 8272a).
1267 * Note that changing the data transfer rate has a (probably deleterious)
1268 * effect on the parameters subject to scaling for 82072/82077 FDCs, so
1269 * fdc_specify is called again after each data transfer rate
1270 * change.
1272 * srt: 1000 to 16000 in microseconds
1273 * hut: 16 to 240 milliseconds
1274 * hlt: 2 to 254 milliseconds
1276 * These values are rounded up to the next highest available delay time.
1278 static void fdc_specify(void)
1280 unsigned char spec1, spec2;
1281 int srt, hlt, hut;
1282 unsigned long dtr = NOMINAL_DTR;
1283 unsigned long scale_dtr = NOMINAL_DTR;
1284 int hlt_max_code = 0x7f;
1285 int hut_max_code = 0xf;
1287 if (FDCS->need_configure && FDCS->version >= FDC_82072A) {
1288 fdc_configure();
1289 FDCS->need_configure = 0;
1290 /*DPRINT("FIFO enabled\n");*/
1293 switch (raw_cmd->rate & 0x03) {
1294 case 3:
1295 dtr = 1000;
1296 break;
1297 case 1:
1298 dtr = 300;
1299 if (FDCS->version >= FDC_82078) {
1300 /* chose the default rate table, not the one
1301 * where 1 = 2 Mbps */
1302 output_byte(FD_DRIVESPEC);
1303 if(need_more_output() == MORE_OUTPUT) {
1304 output_byte(UNIT(current_drive));
1305 output_byte(0xc0);
1308 break;
1309 case 2:
1310 dtr = 250;
1311 break;
1314 if (FDCS->version >= FDC_82072) {
1315 scale_dtr = dtr;
1316 hlt_max_code = 0x00; /* 0==256msec*dtr0/dtr (not linear!) */
1317 hut_max_code = 0x0; /* 0==256msec*dtr0/dtr (not linear!) */
1320 /* Convert step rate from microseconds to milliseconds and 4 bits */
1321 srt = 16 - (DP->srt*scale_dtr/1000 + NOMINAL_DTR - 1)/NOMINAL_DTR;
1322 if( slow_floppy ) {
1323 srt = srt / 4;
1325 SUPBOUND(srt, 0xf);
1326 INFBOUND(srt, 0);
1328 hlt = (DP->hlt*scale_dtr/2 + NOMINAL_DTR - 1)/NOMINAL_DTR;
1329 if (hlt < 0x01)
1330 hlt = 0x01;
1331 else if (hlt > 0x7f)
1332 hlt = hlt_max_code;
1334 hut = (DP->hut*scale_dtr/16 + NOMINAL_DTR - 1)/NOMINAL_DTR;
1335 if (hut < 0x1)
1336 hut = 0x1;
1337 else if (hut > 0xf)
1338 hut = hut_max_code;
1340 spec1 = (srt << 4) | hut;
1341 spec2 = (hlt << 1) | (use_virtual_dma & 1);
1343 /* If these parameters did not change, just return with success */
1344 if (FDCS->spec1 != spec1 || FDCS->spec2 != spec2) {
1345 /* Go ahead and set spec1 and spec2 */
1346 output_byte(FD_SPECIFY);
1347 output_byte(FDCS->spec1 = spec1);
1348 output_byte(FDCS->spec2 = spec2);
1350 } /* fdc_specify */
1352 /* Set the FDC's data transfer rate on behalf of the specified drive.
1353 * NOTE: with 82072/82077 FDCs, changing the data rate requires a reissue
1354 * of the specify command (i.e. using the fdc_specify function).
1356 static int fdc_dtr(void)
1358 /* If data rate not already set to desired value, set it. */
1359 if ((raw_cmd->rate & 3) == FDCS->dtr)
1360 return 0;
1362 /* Set dtr */
1363 fd_outb(raw_cmd->rate & 3, FD_DCR);
1365 /* TODO: some FDC/drive combinations (C&T 82C711 with TEAC 1.2MB)
1366 * need a stabilization period of several milliseconds to be
1367 * enforced after data rate changes before R/W operations.
1368 * Pause 5 msec to avoid trouble. (Needs to be 2 jiffies)
1370 FDCS->dtr = raw_cmd->rate & 3;
1371 return(wait_for_completion(jiffies+2*HZ/100,
1372 (timeout_fn) floppy_ready));
1373 } /* fdc_dtr */
1375 static void tell_sector(void)
1377 printk(": track %d, head %d, sector %d, size %d",
1378 R_TRACK, R_HEAD, R_SECTOR, R_SIZECODE);
1379 } /* tell_sector */
1383 * OK, this error interpreting routine is called after a
1384 * DMA read/write has succeeded
1385 * or failed, so we check the results, and copy any buffers.
1386 * hhb: Added better error reporting.
1387 * ak: Made this into a separate routine.
1389 static int interpret_errors(void)
1391 char bad;
1393 if (inr!=7) {
1394 DPRINT("-- FDC reply error");
1395 FDCS->reset = 1;
1396 return 1;
1399 /* check IC to find cause of interrupt */
1400 switch (ST0 & ST0_INTR) {
1401 case 0x40: /* error occurred during command execution */
1402 if (ST1 & ST1_EOC)
1403 return 0; /* occurs with pseudo-DMA */
1404 bad = 1;
1405 if (ST1 & ST1_WP) {
1406 DPRINT("Drive is write protected\n");
1407 CLEARF(FD_DISK_WRITABLE);
1408 cont->done(0);
1409 bad = 2;
1410 } else if (ST1 & ST1_ND) {
1411 SETF(FD_NEED_TWADDLE);
1412 } else if (ST1 & ST1_OR) {
1413 if (DP->flags & FTD_MSG)
1414 DPRINT("Over/Underrun - retrying\n");
1415 bad = 0;
1416 }else if (*errors >= DP->max_errors.reporting){
1417 DPRINT("");
1418 if (ST0 & ST0_ECE) {
1419 printk("Recalibrate failed!");
1420 } else if (ST2 & ST2_CRC) {
1421 printk("data CRC error");
1422 tell_sector();
1423 } else if (ST1 & ST1_CRC) {
1424 printk("CRC error");
1425 tell_sector();
1426 } else if ((ST1 & (ST1_MAM|ST1_ND)) || (ST2 & ST2_MAM)) {
1427 if (!probing) {
1428 printk("sector not found");
1429 tell_sector();
1430 } else
1431 printk("probe failed...");
1432 } else if (ST2 & ST2_WC) { /* seek error */
1433 printk("wrong cylinder");
1434 } else if (ST2 & ST2_BC) { /* cylinder marked as bad */
1435 printk("bad cylinder");
1436 } else {
1437 printk("unknown error. ST[0..2] are: 0x%x 0x%x 0x%x", ST0, ST1, ST2);
1438 tell_sector();
1440 printk("\n");
1443 if (ST2 & ST2_WC || ST2 & ST2_BC)
1444 /* wrong cylinder => recal */
1445 DRS->track = NEED_2_RECAL;
1446 return bad;
1447 case 0x80: /* invalid command given */
1448 DPRINT("Invalid FDC command given!\n");
1449 cont->done(0);
1450 return 2;
1451 case 0xc0:
1452 DPRINT("Abnormal termination caused by polling\n");
1453 cont->error();
1454 return 2;
1455 default: /* (0) Normal command termination */
1456 return 0;
1461 * This routine is called when everything should be correctly set up
1462 * for the transfer (i.e. floppy motor is on, the correct floppy is
1463 * selected, and the head is sitting on the right track).
1465 static void setup_rw_floppy(void)
1467 int i,ready_date,r, flags,dflags;
1468 timeout_fn function;
1470 flags = raw_cmd->flags;
1471 if (flags & (FD_RAW_READ | FD_RAW_WRITE))
1472 flags |= FD_RAW_INTR;
1474 if ((flags & FD_RAW_SPIN) && !(flags & FD_RAW_NO_MOTOR)){
1475 ready_date = DRS->spinup_date + DP->spinup;
1476 /* If spinup will take a long time, rerun scandrives
1477 * again just before spinup completion. Beware that
1478 * after scandrives, we must again wait for selection.
1480 if ((signed) (ready_date - jiffies) > DP->select_delay){
1481 ready_date -= DP->select_delay;
1482 function = (timeout_fn) floppy_start;
1483 } else
1484 function = (timeout_fn) setup_rw_floppy;
1486 /* wait until the floppy is spinning fast enough */
1487 if (wait_for_completion(ready_date,function))
1488 return;
1490 dflags = DRS->flags;
1492 if ((flags & FD_RAW_READ) || (flags & FD_RAW_WRITE))
1493 setup_DMA();
1495 if (flags & FD_RAW_INTR)
1496 SET_INTR(main_command_interrupt);
1498 r=0;
1499 for (i=0; i< raw_cmd->cmd_count; i++)
1500 r|=output_byte(raw_cmd->cmd[i]);
1502 #ifdef DEBUGT
1503 debugt("rw_command: ");
1504 #endif
1505 if (r){
1506 cont->error();
1507 reset_fdc();
1508 return;
1511 if (!(flags & FD_RAW_INTR)){
1512 inr = result();
1513 cont->interrupt();
1514 } else if (flags & FD_RAW_NEED_DISK)
1515 fd_watchdog();
1518 static int blind_seek;
1521 * This is the routine called after every seek (or recalibrate) interrupt
1522 * from the floppy controller.
1524 static void seek_interrupt(void)
1526 #ifdef DEBUGT
1527 debugt("seek interrupt:");
1528 #endif
1529 if (inr != 2 || (ST0 & 0xF8) != 0x20) {
1530 DPRINT("seek failed\n");
1531 DRS->track = NEED_2_RECAL;
1532 cont->error();
1533 cont->redo();
1534 return;
1536 if (DRS->track >= 0 && DRS->track != ST1 && !blind_seek){
1537 #ifdef DCL_DEBUG
1538 if (DP->flags & FD_DEBUG){
1539 DPRINT("clearing NEWCHANGE flag because of effective seek\n");
1540 DPRINT("jiffies=%ld\n", jiffies);
1542 #endif
1543 CLEARF(FD_DISK_NEWCHANGE); /* effective seek */
1544 DRS->select_date = jiffies;
1546 DRS->track = ST1;
1547 floppy_ready();
1550 static void check_wp(void)
1552 if (TESTF(FD_VERIFY)) {
1553 /* check write protection */
1554 output_byte(FD_GETSTATUS);
1555 output_byte(UNIT(current_drive));
1556 if (result() != 1){
1557 FDCS->reset = 1;
1558 return;
1560 CLEARF(FD_VERIFY);
1561 CLEARF(FD_NEED_TWADDLE);
1562 #ifdef DCL_DEBUG
1563 if (DP->flags & FD_DEBUG){
1564 DPRINT("checking whether disk is write protected\n");
1565 DPRINT("wp=%x\n",ST3 & 0x40);
1567 #endif
1568 if (!(ST3 & 0x40))
1569 SETF(FD_DISK_WRITABLE);
1570 else
1571 CLEARF(FD_DISK_WRITABLE);
1575 static void seek_floppy(void)
1577 int track;
1579 blind_seek=0;
1581 #ifdef DCL_DEBUG
1582 if (DP->flags & FD_DEBUG){
1583 DPRINT("calling disk change from seek\n");
1585 #endif
1587 if (!TESTF(FD_DISK_NEWCHANGE) &&
1588 disk_change(current_drive) &&
1589 (raw_cmd->flags & FD_RAW_NEED_DISK)){
1590 /* the media changed flag should be cleared after the seek.
1591 * If it isn't, this means that there is really no disk in
1592 * the drive.
1594 SETF(FD_DISK_CHANGED);
1595 cont->done(0);
1596 cont->redo();
1597 return;
1599 if (DRS->track <= NEED_1_RECAL){
1600 recalibrate_floppy();
1601 return;
1602 } else if (TESTF(FD_DISK_NEWCHANGE) &&
1603 (raw_cmd->flags & FD_RAW_NEED_DISK) &&
1604 (DRS->track <= NO_TRACK || DRS->track == raw_cmd->track)) {
1605 /* we seek to clear the media-changed condition. Does anybody
1606 * know a more elegant way, which works on all drives? */
1607 if (raw_cmd->track)
1608 track = raw_cmd->track - 1;
1609 else {
1610 if (DP->flags & FD_SILENT_DCL_CLEAR){
1611 set_dor(fdc, ~(0x10 << UNIT(current_drive)), 0);
1612 blind_seek = 1;
1613 raw_cmd->flags |= FD_RAW_NEED_SEEK;
1615 track = 1;
1617 } else {
1618 check_wp();
1619 if (raw_cmd->track != DRS->track &&
1620 (raw_cmd->flags & FD_RAW_NEED_SEEK))
1621 track = raw_cmd->track;
1622 else {
1623 setup_rw_floppy();
1624 return;
1628 SET_INTR(seek_interrupt);
1629 output_byte(FD_SEEK);
1630 output_byte(UNIT(current_drive));
1631 LAST_OUT(track);
1632 #ifdef DEBUGT
1633 debugt("seek command:");
1634 #endif
1637 static void recal_interrupt(void)
1639 #ifdef DEBUGT
1640 debugt("recal interrupt:");
1641 #endif
1642 if (inr !=2)
1643 FDCS->reset = 1;
1644 else if (ST0 & ST0_ECE) {
1645 switch(DRS->track){
1646 case NEED_1_RECAL:
1647 #ifdef DEBUGT
1648 debugt("recal interrupt need 1 recal:");
1649 #endif
1650 /* after a second recalibrate, we still haven't
1651 * reached track 0. Probably no drive. Raise an
1652 * error, as failing immediately might upset
1653 * computers possessed by the Devil :-) */
1654 cont->error();
1655 cont->redo();
1656 return;
1657 case NEED_2_RECAL:
1658 #ifdef DEBUGT
1659 debugt("recal interrupt need 2 recal:");
1660 #endif
1661 /* If we already did a recalibrate,
1662 * and we are not at track 0, this
1663 * means we have moved. (The only way
1664 * not to move at recalibration is to
1665 * be already at track 0.) Clear the
1666 * new change flag */
1667 #ifdef DCL_DEBUG
1668 if (DP->flags & FD_DEBUG){
1669 DPRINT("clearing NEWCHANGE flag because of second recalibrate\n");
1671 #endif
1673 CLEARF(FD_DISK_NEWCHANGE);
1674 DRS->select_date = jiffies;
1675 /* fall through */
1676 default:
1677 #ifdef DEBUGT
1678 debugt("recal interrupt default:");
1679 #endif
1680 /* Recalibrate moves the head by at
1681 * most 80 steps. If after one
1682 * recalibrate we don't have reached
1683 * track 0, this might mean that we
1684 * started beyond track 80. Try
1685 * again. */
1686 DRS->track = NEED_1_RECAL;
1687 break;
1689 } else
1690 DRS->track = ST1;
1691 floppy_ready();
1694 static void print_result(char *message, int inr)
1696 int i;
1698 DPRINT("%s ", message);
1699 if (inr >= 0)
1700 for (i=0; i<inr; i++)
1701 printk("repl[%d]=%x ", i, reply_buffer[i]);
1702 printk("\n");
1705 /* interrupt handler. Note that this can be called externally on the Sparc */
1706 void floppy_interrupt(int irq, void *dev_id, struct pt_regs * regs)
1708 void (*handler)(void) = DEVICE_INTR;
1709 int do_print;
1710 unsigned long f;
1712 lasthandler = handler;
1713 interruptjiffies = jiffies;
1715 f=claim_dma_lock();
1716 fd_disable_dma();
1717 release_dma_lock(f);
1719 floppy_enable_hlt();
1720 CLEAR_INTR;
1721 if (fdc >= N_FDC || FDCS->address == -1){
1722 /* we don't even know which FDC is the culprit */
1723 printk("DOR0=%x\n", fdc_state[0].dor);
1724 printk("floppy interrupt on bizarre fdc %d\n",fdc);
1725 printk("handler=%p\n", handler);
1726 is_alive("bizarre fdc");
1727 return;
1730 FDCS->reset = 0;
1731 /* We have to clear the reset flag here, because apparently on boxes
1732 * with level triggered interrupts (PS/2, Sparc, ...), it is needed to
1733 * emit SENSEI's to clear the interrupt line. And FDCS->reset blocks the
1734 * emission of the SENSEI's.
1735 * It is OK to emit floppy commands because we are in an interrupt
1736 * handler here, and thus we have to fear no interference of other
1737 * activity.
1740 do_print = !handler && print_unex && !initialising;
1742 inr = result();
1743 if(do_print)
1744 print_result("unexpected interrupt", inr);
1745 if (inr == 0){
1746 int max_sensei = 4;
1747 do {
1748 output_byte(FD_SENSEI);
1749 inr = result();
1750 if(do_print)
1751 print_result("sensei", inr);
1752 max_sensei--;
1753 } while ((ST0 & 0x83) != UNIT(current_drive) && inr == 2 && max_sensei);
1755 if (handler) {
1756 int cpu = smp_processor_id();
1757 if(softirq_trylock(cpu)) {
1758 /* got the lock, call the handler immediately */
1759 handler();
1760 softirq_endlock(cpu);
1761 } else
1762 /* we interrupted a bottom half. Defer handler */
1763 schedule_bh( (void *)(void *) handler);
1764 } else
1765 FDCS->reset = 1;
1766 is_alive("normal interrupt end");
1769 static void recalibrate_floppy(void)
1771 #ifdef DEBUGT
1772 debugt("recalibrate floppy:");
1773 #endif
1774 SET_INTR(recal_interrupt);
1775 output_byte(FD_RECALIBRATE);
1776 LAST_OUT(UNIT(current_drive));
1780 * Must do 4 FD_SENSEIs after reset because of ``drive polling''.
1782 static void reset_interrupt(void)
1784 #ifdef DEBUGT
1785 debugt("reset interrupt:");
1786 #endif
1787 result(); /* get the status ready for set_fdc */
1788 if (FDCS->reset) {
1789 printk("reset set in interrupt, calling %p\n", cont->error);
1790 cont->error(); /* a reset just after a reset. BAD! */
1792 cont->redo();
1796 * reset is done by pulling bit 2 of DOR low for a while (old FDCs),
1797 * or by setting the self clearing bit 7 of STATUS (newer FDCs)
1799 static void reset_fdc(void)
1801 unsigned long flags;
1803 SET_INTR(reset_interrupt);
1804 FDCS->reset = 0;
1805 reset_fdc_info(0);
1807 /* Pseudo-DMA may intercept 'reset finished' interrupt. */
1808 /* Irrelevant for systems with true DMA (i386). */
1810 flags=claim_dma_lock();
1811 fd_disable_dma();
1812 release_dma_lock(flags);
1814 if (FDCS->version >= FDC_82072A)
1815 fd_outb(0x80 | (FDCS->dtr &3), FD_STATUS);
1816 else {
1817 fd_outb(FDCS->dor & ~0x04, FD_DOR);
1818 udelay(FD_RESET_DELAY);
1819 fd_outb(FDCS->dor, FD_DOR);
1823 static void show_floppy(void)
1825 int i;
1827 printk("\n");
1828 printk("floppy driver state\n");
1829 printk("-------------------\n");
1830 printk("now=%ld last interrupt=%d last called handler=%p\n",
1831 jiffies, interruptjiffies, lasthandler);
1834 #ifdef FLOPPY_SANITY_CHECK
1835 printk("timeout_message=%s\n", timeout_message);
1836 printk("last output bytes:\n");
1837 for (i=0; i < OLOGSIZE; i++)
1838 printk("%2x %2x %ld\n",
1839 output_log[(i+output_log_pos) % OLOGSIZE].data,
1840 output_log[(i+output_log_pos) % OLOGSIZE].status,
1841 output_log[(i+output_log_pos) % OLOGSIZE].jiffies);
1842 printk("last result at %d\n", resultjiffies);
1843 printk("last redo_fd_request at %d\n", lastredo);
1844 for (i=0; i<resultsize; i++){
1845 printk("%2x ", reply_buffer[i]);
1847 printk("\n");
1848 #endif
1850 printk("status=%x\n", fd_inb(FD_STATUS));
1851 printk("fdc_busy=%d\n", fdc_busy);
1852 if (DEVICE_INTR)
1853 printk("DEVICE_INTR=%p\n", DEVICE_INTR);
1854 if (floppy_tq.sync)
1855 printk("floppy_tq.routine=%p\n", floppy_tq.routine);
1856 if (fd_timer.prev)
1857 printk("fd_timer.function=%p\n", fd_timer.function);
1858 if (fd_timeout.prev){
1859 printk("timer_table=%p\n",fd_timeout.function);
1860 printk("expires=%ld\n",fd_timeout.expires-jiffies);
1861 printk("now=%ld\n",jiffies);
1863 printk("cont=%p\n", cont);
1864 printk("CURRENT=%p\n", CURRENT);
1865 printk("command_status=%d\n", command_status);
1866 printk("\n");
1869 static void floppy_shutdown(void)
1871 unsigned long flags;
1873 if (!initialising)
1874 show_floppy();
1875 cancel_activity();
1877 floppy_enable_hlt();
1879 flags=claim_dma_lock();
1880 fd_disable_dma();
1881 release_dma_lock(flags);
1883 /* avoid dma going to a random drive after shutdown */
1885 if (!initialising)
1886 DPRINT("floppy timeout called\n");
1887 FDCS->reset = 1;
1888 if (cont){
1889 cont->done(0);
1890 cont->redo(); /* this will recall reset when needed */
1891 } else {
1892 printk("no cont in shutdown!\n");
1893 process_fd_request();
1895 is_alive("floppy shutdown");
1897 /*typedef void (*timeout_fn)(unsigned long);*/
1899 /* start motor, check media-changed condition and write protection */
1900 static int start_motor(void (*function)(void) )
1902 int mask, data;
1904 mask = 0xfc;
1905 data = UNIT(current_drive);
1906 if (!(raw_cmd->flags & FD_RAW_NO_MOTOR)){
1907 if (!(FDCS->dor & (0x10 << UNIT(current_drive)))){
1908 set_debugt();
1909 /* no read since this drive is running */
1910 DRS->first_read_date = 0;
1911 /* note motor start time if motor is not yet running */
1912 DRS->spinup_date = jiffies;
1913 data |= (0x10 << UNIT(current_drive));
1915 } else
1916 if (FDCS->dor & (0x10 << UNIT(current_drive)))
1917 mask &= ~(0x10 << UNIT(current_drive));
1919 /* starts motor and selects floppy */
1920 del_timer(motor_off_timer + current_drive);
1921 set_dor(fdc, mask, data);
1923 /* wait_for_completion also schedules reset if needed. */
1924 return(wait_for_completion(DRS->select_date+DP->select_delay,
1925 (timeout_fn) function));
1928 static void floppy_ready(void)
1930 CHECK_RESET;
1931 if (start_motor(floppy_ready)) return;
1932 if (fdc_dtr()) return;
1934 #ifdef DCL_DEBUG
1935 if (DP->flags & FD_DEBUG){
1936 DPRINT("calling disk change from floppy_ready\n");
1938 #endif
1939 if (!(raw_cmd->flags & FD_RAW_NO_MOTOR) &&
1940 disk_change(current_drive) &&
1941 !DP->select_delay)
1942 twaddle(); /* this clears the dcl on certain drive/controller
1943 * combinations */
1945 #ifdef fd_chose_dma_mode
1946 if ((raw_cmd->flags & FD_RAW_READ) ||
1947 (raw_cmd->flags & FD_RAW_WRITE))
1949 unsigned long flags = claim_dma_lock();
1950 fd_chose_dma_mode(raw_cmd->kernel_data,
1951 raw_cmd->length);
1952 release_dma_lock(flags);
1954 #endif
1956 if (raw_cmd->flags & (FD_RAW_NEED_SEEK | FD_RAW_NEED_DISK)){
1957 perpendicular_mode();
1958 fdc_specify(); /* must be done here because of hut, hlt ... */
1959 seek_floppy();
1960 } else {
1961 if ((raw_cmd->flags & FD_RAW_READ) ||
1962 (raw_cmd->flags & FD_RAW_WRITE))
1963 fdc_specify();
1964 setup_rw_floppy();
1968 static void floppy_start(void)
1970 reschedule_timeout(CURRENTD, "floppy start", 0);
1972 scandrives();
1973 #ifdef DCL_DEBUG
1974 if (DP->flags & FD_DEBUG){
1975 DPRINT("setting NEWCHANGE in floppy_start\n");
1977 #endif
1978 SETF(FD_DISK_NEWCHANGE);
1979 floppy_ready();
1983 * ========================================================================
1984 * here ends the bottom half. Exported routines are:
1985 * floppy_start, floppy_off, floppy_ready, lock_fdc, unlock_fdc, set_fdc,
1986 * start_motor, reset_fdc, reset_fdc_info, interpret_errors.
1987 * Initialization also uses output_byte, result, set_dor, floppy_interrupt
1988 * and set_dor.
1989 * ========================================================================
1992 * General purpose continuations.
1993 * ==============================
1996 static void do_wakeup(void)
1998 reschedule_timeout(MAXTIMEOUT, "do wakeup", 0);
1999 cont = 0;
2000 command_status += 2;
2001 wake_up(&command_done);
2004 static struct cont_t wakeup_cont={
2005 empty,
2006 do_wakeup,
2007 empty,
2008 (done_f)empty
2012 static struct cont_t intr_cont={
2013 empty,
2014 process_fd_request,
2015 empty,
2016 (done_f) empty
2019 static int wait_til_done(void (*handler)(void), int interruptible)
2021 int ret;
2022 unsigned long flags;
2024 schedule_bh((void *)(void *)handler);
2025 INT_OFF;
2026 while(command_status < 2 && NO_SIGNAL){
2027 is_alive("wait_til_done");
2028 if (interruptible)
2029 interruptible_sleep_on(&command_done);
2030 else
2031 sleep_on(&command_done);
2033 if (command_status < 2){
2034 cancel_activity();
2035 cont = &intr_cont;
2036 reset_fdc();
2037 INT_ON;
2038 return -EINTR;
2040 INT_ON;
2042 if (FDCS->reset)
2043 command_status = FD_COMMAND_ERROR;
2044 if (command_status == FD_COMMAND_OKAY)
2045 ret=0;
2046 else
2047 ret=-EIO;
2048 command_status = FD_COMMAND_NONE;
2049 return ret;
2052 static void generic_done(int result)
2054 command_status = result;
2055 cont = &wakeup_cont;
2058 static void generic_success(void)
2060 cont->done(1);
2063 static void generic_failure(void)
2065 cont->done(0);
2068 static void success_and_wakeup(void)
2070 generic_success();
2071 cont->redo();
2076 * formatting and rw support.
2077 * ==========================
2080 static int next_valid_format(void)
2082 int probed_format;
2084 probed_format = DRS->probed_format;
2085 while(1){
2086 if (probed_format >= 8 ||
2087 !DP->autodetect[probed_format]){
2088 DRS->probed_format = 0;
2089 return 1;
2091 if (floppy_type[DP->autodetect[probed_format]].sect){
2092 DRS->probed_format = probed_format;
2093 return 0;
2095 probed_format++;
2099 static void bad_flp_intr(void)
2101 if (probing){
2102 DRS->probed_format++;
2103 if (!next_valid_format())
2104 return;
2106 (*errors)++;
2107 INFBOUND(DRWE->badness, *errors);
2108 if (*errors > DP->max_errors.abort)
2109 cont->done(0);
2110 if (*errors > DP->max_errors.reset)
2111 FDCS->reset = 1;
2112 else if (*errors > DP->max_errors.recal)
2113 DRS->track = NEED_2_RECAL;
2116 static void set_floppy(kdev_t device)
2118 if (TYPE(device))
2119 _floppy = TYPE(device) + floppy_type;
2120 else
2121 _floppy = current_type[ DRIVE(device) ];
2125 * formatting support.
2126 * ===================
2128 static void format_interrupt(void)
2130 switch (interpret_errors()){
2131 case 1:
2132 cont->error();
2133 case 2:
2134 break;
2135 case 0:
2136 cont->done(1);
2138 cont->redo();
2141 #define CODE2SIZE (ssize = ((1 << SIZECODE) + 3) >> 2)
2142 #define FM_MODE(x,y) ((y) & ~(((x)->rate & 0x80) >>1))
2143 #define CT(x) ((x) | 0xc0)
2144 static void setup_format_params(int track)
2146 struct fparm {
2147 unsigned char track,head,sect,size;
2148 } *here = (struct fparm *)floppy_track_buffer;
2149 int il,n;
2150 int count,head_shift,track_shift;
2152 raw_cmd = &default_raw_cmd;
2153 raw_cmd->track = track;
2155 raw_cmd->flags = FD_RAW_WRITE | FD_RAW_INTR | FD_RAW_SPIN |
2156 FD_RAW_NEED_DISK | FD_RAW_NEED_SEEK;
2157 raw_cmd->rate = _floppy->rate & 0x43;
2158 raw_cmd->cmd_count = NR_F;
2159 COMMAND = FM_MODE(_floppy,FD_FORMAT);
2160 DR_SELECT = UNIT(current_drive) + PH_HEAD(_floppy,format_req.head);
2161 F_SIZECODE = FD_SIZECODE(_floppy);
2162 F_SECT_PER_TRACK = _floppy->sect << 2 >> F_SIZECODE;
2163 F_GAP = _floppy->fmt_gap;
2164 F_FILL = FD_FILL_BYTE;
2166 raw_cmd->kernel_data = floppy_track_buffer;
2167 raw_cmd->length = 4 * F_SECT_PER_TRACK;
2169 /* allow for about 30ms for data transport per track */
2170 head_shift = (F_SECT_PER_TRACK + 5) / 6;
2172 /* a ``cylinder'' is two tracks plus a little stepping time */
2173 track_shift = 2 * head_shift + 3;
2175 /* position of logical sector 1 on this track */
2176 n = (track_shift * format_req.track + head_shift * format_req.head)
2177 % F_SECT_PER_TRACK;
2179 /* determine interleave */
2180 il = 1;
2181 if (_floppy->fmt_gap < 0x22)
2182 il++;
2184 /* initialize field */
2185 for (count = 0; count < F_SECT_PER_TRACK; ++count) {
2186 here[count].track = format_req.track;
2187 here[count].head = format_req.head;
2188 here[count].sect = 0;
2189 here[count].size = F_SIZECODE;
2191 /* place logical sectors */
2192 for (count = 1; count <= F_SECT_PER_TRACK; ++count) {
2193 here[n].sect = count;
2194 n = (n+il) % F_SECT_PER_TRACK;
2195 if (here[n].sect) { /* sector busy, find next free sector */
2196 ++n;
2197 if (n>= F_SECT_PER_TRACK) {
2198 n-=F_SECT_PER_TRACK;
2199 while (here[n].sect) ++n;
2205 static void redo_format(void)
2207 buffer_track = -1;
2208 setup_format_params(format_req.track << STRETCH(_floppy));
2209 floppy_start();
2210 #ifdef DEBUGT
2211 debugt("queue format request");
2212 #endif
2215 static struct cont_t format_cont={
2216 format_interrupt,
2217 redo_format,
2218 bad_flp_intr,
2219 generic_done };
2221 static int do_format(kdev_t device, struct format_descr *tmp_format_req)
2223 int ret;
2224 int drive=DRIVE(device);
2226 LOCK_FDC(drive,1);
2227 set_floppy(device);
2228 if (!_floppy ||
2229 _floppy->track > DP->tracks ||
2230 tmp_format_req->track >= _floppy->track ||
2231 tmp_format_req->head >= _floppy->head ||
2232 (_floppy->sect << 2) % (1 << FD_SIZECODE(_floppy)) ||
2233 !_floppy->fmt_gap) {
2234 process_fd_request();
2235 return -EINVAL;
2237 format_req = *tmp_format_req;
2238 format_errors = 0;
2239 cont = &format_cont;
2240 errors = &format_errors;
2241 IWAIT(redo_format);
2242 process_fd_request();
2243 return ret;
2247 * Buffer read/write and support
2248 * =============================
2251 /* new request_done. Can handle physical sectors which are smaller than a
2252 * logical buffer */
2253 static void request_done(int uptodate)
2255 int block;
2257 probing = 0;
2258 reschedule_timeout(MAXTIMEOUT, "request done %d", uptodate);
2260 if (!CURRENT){
2261 DPRINT("request list destroyed in floppy request done\n");
2262 return;
2265 if (uptodate){
2266 /* maintain values for invalidation on geometry
2267 * change */
2268 block = current_count_sectors + CURRENT->sector;
2269 INFBOUND(DRS->maxblock, block);
2270 if (block > _floppy->sect)
2271 DRS->maxtrack = 1;
2273 /* unlock chained buffers */
2274 while (current_count_sectors && CURRENT &&
2275 current_count_sectors >= CURRENT->current_nr_sectors){
2276 current_count_sectors -= CURRENT->current_nr_sectors;
2277 CURRENT->nr_sectors -= CURRENT->current_nr_sectors;
2278 CURRENT->sector += CURRENT->current_nr_sectors;
2279 end_request(1);
2281 if (current_count_sectors && CURRENT){
2282 /* "unlock" last subsector */
2283 CURRENT->buffer += current_count_sectors <<9;
2284 CURRENT->current_nr_sectors -= current_count_sectors;
2285 CURRENT->nr_sectors -= current_count_sectors;
2286 CURRENT->sector += current_count_sectors;
2287 return;
2290 if (current_count_sectors && !CURRENT)
2291 DPRINT("request list destroyed in floppy request done\n");
2293 } else {
2294 if (CURRENT->cmd == WRITE) {
2295 /* record write error information */
2296 DRWE->write_errors++;
2297 if (DRWE->write_errors == 1) {
2298 DRWE->first_error_sector = CURRENT->sector;
2299 DRWE->first_error_generation = DRS->generation;
2301 DRWE->last_error_sector = CURRENT->sector;
2302 DRWE->last_error_generation = DRS->generation;
2304 end_request(0);
2308 /* Interrupt handler evaluating the result of the r/w operation */
2309 static void rw_interrupt(void)
2311 int nr_sectors, ssize, eoc, heads;
2313 if (!DRS->first_read_date)
2314 DRS->first_read_date = jiffies;
2316 nr_sectors = 0;
2317 CODE2SIZE;
2319 if(ST1 & ST1_EOC)
2320 eoc = 1;
2321 else
2322 eoc = 0;
2324 if(COMMAND & 0x80)
2325 heads = 2;
2326 else
2327 heads = 1;
2329 nr_sectors = (((R_TRACK-TRACK) * heads +
2330 R_HEAD-HEAD) * SECT_PER_TRACK +
2331 R_SECTOR-SECTOR + eoc) << SIZECODE >> 2;
2333 #ifdef FLOPPY_SANITY_CHECK
2334 if (nr_sectors / ssize >
2335 (in_sector_offset + current_count_sectors + ssize - 1)/ssize) {
2336 DPRINT("long rw: %x instead of %lx\n",
2337 nr_sectors, current_count_sectors);
2338 printk("rs=%d s=%d\n", R_SECTOR, SECTOR);
2339 printk("rh=%d h=%d\n", R_HEAD, HEAD);
2340 printk("rt=%d t=%d\n", R_TRACK, TRACK);
2341 printk("heads=%d eoc=%d\n", heads, eoc);
2342 printk("spt=%d st=%d ss=%d\n", SECT_PER_TRACK,
2343 sector_t, ssize);
2344 printk("in_sector_offset=%d\n", in_sector_offset);
2346 #endif
2348 nr_sectors -= in_sector_offset;
2349 INFBOUND(nr_sectors,0);
2350 SUPBOUND(current_count_sectors, nr_sectors);
2352 switch (interpret_errors()){
2353 case 2:
2354 cont->redo();
2355 return;
2356 case 1:
2357 if (!current_count_sectors){
2358 cont->error();
2359 cont->redo();
2360 return;
2362 break;
2363 case 0:
2364 if (!current_count_sectors){
2365 cont->redo();
2366 return;
2368 current_type[current_drive] = _floppy;
2369 floppy_sizes[TOMINOR(current_drive) ]=
2370 (_floppy->size+1)>>1;
2371 break;
2374 if (probing) {
2375 if (DP->flags & FTD_MSG)
2376 DPRINT("Auto-detected floppy type %s in fd%d\n",
2377 _floppy->name,current_drive);
2378 current_type[current_drive] = _floppy;
2379 floppy_sizes[TOMINOR(current_drive)] = (_floppy->size+1) >> 1;
2380 probing = 0;
2383 if (CT(COMMAND) != FD_READ ||
2384 raw_cmd->kernel_data == CURRENT->buffer){
2385 /* transfer directly from buffer */
2386 cont->done(1);
2387 } else if (CT(COMMAND) == FD_READ){
2388 buffer_track = raw_cmd->track;
2389 buffer_drive = current_drive;
2390 INFBOUND(buffer_max, nr_sectors + sector_t);
2392 cont->redo();
2395 /* Compute maximal contiguous buffer size. */
2396 static int buffer_chain_size(void)
2398 struct buffer_head *bh;
2399 int size;
2400 char *base;
2402 base = CURRENT->buffer;
2403 size = CURRENT->current_nr_sectors << 9;
2404 bh = CURRENT->bh;
2406 if (bh){
2407 bh = bh->b_reqnext;
2408 while (bh && bh->b_data == base + size){
2409 size += bh->b_size;
2410 bh = bh->b_reqnext;
2413 return size >> 9;
2416 /* Compute the maximal transfer size */
2417 static int transfer_size(int ssize, int max_sector, int max_size)
2419 SUPBOUND(max_sector, sector_t + max_size);
2421 /* alignment */
2422 max_sector -= (max_sector % _floppy->sect) % ssize;
2424 /* transfer size, beginning not aligned */
2425 current_count_sectors = max_sector - sector_t ;
2427 return max_sector;
2431 * Move data from/to the track buffer to/from the buffer cache.
2433 static void copy_buffer(int ssize, int max_sector, int max_sector_2)
2435 int remaining; /* number of transferred 512-byte sectors */
2436 struct buffer_head *bh;
2437 char *buffer, *dma_buffer;
2438 int size;
2440 max_sector = transfer_size(ssize,
2441 minimum(max_sector, max_sector_2),
2442 CURRENT->nr_sectors);
2444 if (current_count_sectors <= 0 && CT(COMMAND) == FD_WRITE &&
2445 buffer_max > sector_t + CURRENT->nr_sectors)
2446 current_count_sectors = minimum(buffer_max - sector_t,
2447 CURRENT->nr_sectors);
2449 remaining = current_count_sectors << 9;
2450 #ifdef FLOPPY_SANITY_CHECK
2451 if ((remaining >> 9) > CURRENT->nr_sectors &&
2452 CT(COMMAND) == FD_WRITE){
2453 DPRINT("in copy buffer\n");
2454 printk("current_count_sectors=%ld\n", current_count_sectors);
2455 printk("remaining=%d\n", remaining >> 9);
2456 printk("CURRENT->nr_sectors=%ld\n",CURRENT->nr_sectors);
2457 printk("CURRENT->current_nr_sectors=%ld\n",
2458 CURRENT->current_nr_sectors);
2459 printk("max_sector=%d\n", max_sector);
2460 printk("ssize=%d\n", ssize);
2462 #endif
2464 buffer_max = maximum(max_sector, buffer_max);
2466 dma_buffer = floppy_track_buffer + ((sector_t - buffer_min) << 9);
2468 bh = CURRENT->bh;
2469 size = CURRENT->current_nr_sectors << 9;
2470 buffer = CURRENT->buffer;
2472 while (remaining > 0){
2473 SUPBOUND(size, remaining);
2474 #ifdef FLOPPY_SANITY_CHECK
2475 if (dma_buffer + size >
2476 floppy_track_buffer + (max_buffer_sectors << 10) ||
2477 dma_buffer < floppy_track_buffer){
2478 DPRINT("buffer overrun in copy buffer %d\n",
2479 (int) ((floppy_track_buffer - dma_buffer) >>9));
2480 printk("sector_t=%d buffer_min=%d\n",
2481 sector_t, buffer_min);
2482 printk("current_count_sectors=%ld\n",
2483 current_count_sectors);
2484 if (CT(COMMAND) == FD_READ)
2485 printk("read\n");
2486 if (CT(COMMAND) == FD_READ)
2487 printk("write\n");
2488 break;
2490 if (((unsigned long)buffer) % 512)
2491 DPRINT("%p buffer not aligned\n", buffer);
2492 #endif
2493 if (CT(COMMAND) == FD_READ)
2494 memcpy(buffer, dma_buffer, size);
2495 else
2496 memcpy(dma_buffer, buffer, size);
2497 remaining -= size;
2498 if (!remaining)
2499 break;
2501 dma_buffer += size;
2502 bh = bh->b_reqnext;
2503 #ifdef FLOPPY_SANITY_CHECK
2504 if (!bh){
2505 DPRINT("bh=null in copy buffer after copy\n");
2506 break;
2508 #endif
2509 size = bh->b_size;
2510 buffer = bh->b_data;
2512 #ifdef FLOPPY_SANITY_CHECK
2513 if (remaining){
2514 if (remaining > 0)
2515 max_sector -= remaining >> 9;
2516 DPRINT("weirdness: remaining %d\n", remaining>>9);
2518 #endif
2521 #if 0
2522 static inline int check_dma_crossing(char *start,
2523 unsigned long length, char *message)
2525 if (CROSS_64KB(start, length)) {
2526 printk("DMA xfer crosses 64KB boundary in %s %p-%p\n",
2527 message, start, start+length);
2528 return 1;
2529 } else
2530 return 0;
2532 #endif
2534 /* work around a bug in pseudo DMA
2535 * (on some FDCs) pseudo DMA does not stop when the CPU stops
2536 * sending data. Hence we need a different way to signal the
2537 * transfer length: We use SECT_PER_TRACK. Unfortunately, this
2538 * does not work with MT, hence we can only transfer one head at
2539 * a time
2541 static void virtualdmabug_workaround(void) {
2542 int hard_sectors, end_sector;
2543 if(CT(COMMAND) == FD_WRITE) {
2544 COMMAND &= ~0x80; /* switch off multiple track mode */
2546 hard_sectors = raw_cmd->length >> (7 + SIZECODE);
2547 end_sector = SECTOR + hard_sectors - 1;
2548 #ifdef FLOPPY_SANITY_CHECK
2549 if(end_sector > SECT_PER_TRACK) {
2550 printk("too many sectors %d > %d\n",
2551 end_sector, SECT_PER_TRACK);
2552 return;
2554 #endif
2555 SECT_PER_TRACK = end_sector; /* make sure SECT_PER_TRACK points
2556 * to end of transfer */
2561 * Formulate a read/write request.
2562 * this routine decides where to load the data (directly to buffer, or to
2563 * tmp floppy area), how much data to load (the size of the buffer, the whole
2564 * track, or a single sector)
2565 * All floppy_track_buffer handling goes in here. If we ever add track buffer
2566 * allocation on the fly, it should be done here. No other part should need
2567 * modification.
2570 static int make_raw_rw_request(void)
2572 int aligned_sector_t;
2573 int max_sector, max_size, tracksize, ssize;
2575 set_fdc(DRIVE(CURRENT->rq_dev));
2577 raw_cmd = &default_raw_cmd;
2578 raw_cmd->flags = FD_RAW_SPIN | FD_RAW_NEED_DISK | FD_RAW_NEED_DISK |
2579 FD_RAW_NEED_SEEK;
2580 raw_cmd->cmd_count = NR_RW;
2581 if (CURRENT->cmd == READ){
2582 raw_cmd->flags |= FD_RAW_READ;
2583 COMMAND = FM_MODE(_floppy,FD_READ);
2584 } else if (CURRENT->cmd == WRITE){
2585 raw_cmd->flags |= FD_RAW_WRITE;
2586 COMMAND = FM_MODE(_floppy,FD_WRITE);
2587 } else {
2588 DPRINT("make_raw_rw_request: unknown command\n");
2589 return 0;
2592 max_sector = _floppy->sect * _floppy->head;
2594 TRACK = CURRENT->sector / max_sector;
2595 sector_t = CURRENT->sector % max_sector;
2596 if (_floppy->track && TRACK >= _floppy->track) {
2597 if(CURRENT->current_nr_sectors & 1) {
2598 current_count_sectors = 1;
2599 return 1;
2600 } else
2601 return 0;
2603 HEAD = sector_t / _floppy->sect;
2605 if (((_floppy->stretch & FD_SWAPSIDES) || TESTF(FD_NEED_TWADDLE)) &&
2606 sector_t < _floppy->sect)
2607 max_sector = _floppy->sect;
2609 /* 2M disks have phantom sectors on the first track */
2610 if ((_floppy->rate & FD_2M) && (!TRACK) && (!HEAD)){
2611 max_sector = 2 * _floppy->sect / 3;
2612 if (sector_t >= max_sector){
2613 current_count_sectors = minimum(_floppy->sect - sector_t,
2614 CURRENT->nr_sectors);
2615 return 1;
2617 SIZECODE = 2;
2618 } else
2619 SIZECODE = FD_SIZECODE(_floppy);
2620 raw_cmd->rate = _floppy->rate & 0x43;
2621 if ((_floppy->rate & FD_2M) &&
2622 (TRACK || HEAD) &&
2623 raw_cmd->rate == 2)
2624 raw_cmd->rate = 1;
2626 if (SIZECODE)
2627 SIZECODE2 = 0xff;
2628 else
2629 SIZECODE2 = 0x80;
2630 raw_cmd->track = TRACK << STRETCH(_floppy);
2631 DR_SELECT = UNIT(current_drive) + PH_HEAD(_floppy,HEAD);
2632 GAP = _floppy->gap;
2633 CODE2SIZE;
2634 SECT_PER_TRACK = _floppy->sect << 2 >> SIZECODE;
2635 SECTOR = ((sector_t % _floppy->sect) << 2 >> SIZECODE) + 1;
2637 /* tracksize describes the size which can be filled up with sectors
2638 * of size ssize.
2640 tracksize = _floppy->sect - _floppy->sect % ssize;
2641 if (tracksize < _floppy->sect){
2642 SECT_PER_TRACK ++;
2643 if (tracksize <= sector_t % _floppy->sect)
2644 SECTOR--;
2646 /* if we are beyond tracksize, fill up using smaller sectors */
2647 while (tracksize <= sector_t % _floppy->sect){
2648 while(tracksize + ssize > _floppy->sect){
2649 SIZECODE--;
2650 ssize >>= 1;
2652 SECTOR++; SECT_PER_TRACK ++;
2653 tracksize += ssize;
2655 max_sector = HEAD * _floppy->sect + tracksize;
2656 } else if (!TRACK && !HEAD && !(_floppy->rate & FD_2M) && probing) {
2657 max_sector = _floppy->sect;
2658 } else if (!HEAD && CT(COMMAND) == FD_WRITE) {
2659 /* for virtual DMA bug workaround */
2660 max_sector = _floppy->sect;
2663 in_sector_offset = (sector_t % _floppy->sect) % ssize;
2664 aligned_sector_t = sector_t - in_sector_offset;
2665 max_size = CURRENT->nr_sectors;
2666 if ((raw_cmd->track == buffer_track) &&
2667 (current_drive == buffer_drive) &&
2668 (sector_t >= buffer_min) && (sector_t < buffer_max)) {
2669 /* data already in track buffer */
2670 if (CT(COMMAND) == FD_READ) {
2671 copy_buffer(1, max_sector, buffer_max);
2672 return 1;
2674 } else if (in_sector_offset || CURRENT->nr_sectors < ssize){
2675 if (CT(COMMAND) == FD_WRITE){
2676 if (sector_t + CURRENT->nr_sectors > ssize &&
2677 sector_t + CURRENT->nr_sectors < ssize + ssize)
2678 max_size = ssize + ssize;
2679 else
2680 max_size = ssize;
2682 raw_cmd->flags &= ~FD_RAW_WRITE;
2683 raw_cmd->flags |= FD_RAW_READ;
2684 COMMAND = FM_MODE(_floppy,FD_READ);
2685 } else if ((unsigned long)CURRENT->buffer < MAX_DMA_ADDRESS) {
2686 unsigned long dma_limit;
2687 int direct, indirect;
2689 indirect= transfer_size(ssize,max_sector,max_buffer_sectors*2) -
2690 sector_t;
2693 * Do NOT use minimum() here---MAX_DMA_ADDRESS is 64 bits wide
2694 * on a 64 bit machine!
2696 max_size = buffer_chain_size();
2697 dma_limit = (MAX_DMA_ADDRESS - ((unsigned long) CURRENT->buffer)) >> 9;
2698 if ((unsigned long) max_size > dma_limit) {
2699 max_size = dma_limit;
2701 /* 64 kb boundaries */
2702 if (CROSS_64KB(CURRENT->buffer, max_size << 9))
2703 max_size = (K_64 -
2704 ((unsigned long)CURRENT->buffer) % K_64)>>9;
2705 direct = transfer_size(ssize,max_sector,max_size) - sector_t;
2707 * We try to read tracks, but if we get too many errors, we
2708 * go back to reading just one sector at a time.
2710 * This means we should be able to read a sector even if there
2711 * are other bad sectors on this track.
2713 if (!direct ||
2714 (indirect * 2 > direct * 3 &&
2715 *errors < DP->max_errors.read_track &&
2716 /*!TESTF(FD_NEED_TWADDLE) &&*/
2717 ((!probing || (DP->read_track&(1<<DRS->probed_format)))))){
2718 max_size = CURRENT->nr_sectors;
2719 } else {
2720 raw_cmd->kernel_data = CURRENT->buffer;
2721 raw_cmd->length = current_count_sectors << 9;
2722 if (raw_cmd->length == 0){
2723 DPRINT("zero dma transfer attempted from make_raw_request\n");
2724 DPRINT("indirect=%d direct=%d sector_t=%d",
2725 indirect, direct, sector_t);
2726 return 0;
2728 /* check_dma_crossing(raw_cmd->kernel_data,
2729 raw_cmd->length,
2730 "end of make_raw_request [1]");*/
2732 virtualdmabug_workaround();
2733 return 2;
2737 if (CT(COMMAND) == FD_READ)
2738 max_size = max_sector; /* unbounded */
2740 /* claim buffer track if needed */
2741 if (buffer_track != raw_cmd->track || /* bad track */
2742 buffer_drive !=current_drive || /* bad drive */
2743 sector_t > buffer_max ||
2744 sector_t < buffer_min ||
2745 ((CT(COMMAND) == FD_READ ||
2746 (!in_sector_offset && CURRENT->nr_sectors >= ssize))&&
2747 max_sector > 2 * max_buffer_sectors + buffer_min &&
2748 max_size + sector_t > 2 * max_buffer_sectors + buffer_min)
2749 /* not enough space */){
2750 buffer_track = -1;
2751 buffer_drive = current_drive;
2752 buffer_max = buffer_min = aligned_sector_t;
2754 raw_cmd->kernel_data = floppy_track_buffer +
2755 ((aligned_sector_t-buffer_min)<<9);
2757 if (CT(COMMAND) == FD_WRITE){
2758 /* copy write buffer to track buffer.
2759 * if we get here, we know that the write
2760 * is either aligned or the data already in the buffer
2761 * (buffer will be overwritten) */
2762 #ifdef FLOPPY_SANITY_CHECK
2763 if (in_sector_offset && buffer_track == -1)
2764 DPRINT("internal error offset !=0 on write\n");
2765 #endif
2766 buffer_track = raw_cmd->track;
2767 buffer_drive = current_drive;
2768 copy_buffer(ssize, max_sector, 2*max_buffer_sectors+buffer_min);
2769 } else
2770 transfer_size(ssize, max_sector,
2771 2*max_buffer_sectors+buffer_min-aligned_sector_t);
2773 /* round up current_count_sectors to get dma xfer size */
2774 raw_cmd->length = in_sector_offset+current_count_sectors;
2775 raw_cmd->length = ((raw_cmd->length -1)|(ssize-1))+1;
2776 raw_cmd->length <<= 9;
2777 #ifdef FLOPPY_SANITY_CHECK
2778 /*check_dma_crossing(raw_cmd->kernel_data, raw_cmd->length,
2779 "end of make_raw_request");*/
2780 if ((raw_cmd->length < current_count_sectors << 9) ||
2781 (raw_cmd->kernel_data != CURRENT->buffer &&
2782 CT(COMMAND) == FD_WRITE &&
2783 (aligned_sector_t + (raw_cmd->length >> 9) > buffer_max ||
2784 aligned_sector_t < buffer_min)) ||
2785 raw_cmd->length % (128 << SIZECODE) ||
2786 raw_cmd->length <= 0 || current_count_sectors <= 0){
2787 DPRINT("fractionary current count b=%lx s=%lx\n",
2788 raw_cmd->length, current_count_sectors);
2789 if (raw_cmd->kernel_data != CURRENT->buffer)
2790 printk("addr=%d, length=%ld\n",
2791 (int) ((raw_cmd->kernel_data -
2792 floppy_track_buffer) >> 9),
2793 current_count_sectors);
2794 printk("st=%d ast=%d mse=%d msi=%d\n",
2795 sector_t, aligned_sector_t, max_sector, max_size);
2796 printk("ssize=%x SIZECODE=%d\n", ssize, SIZECODE);
2797 printk("command=%x SECTOR=%d HEAD=%d, TRACK=%d\n",
2798 COMMAND, SECTOR, HEAD, TRACK);
2799 printk("buffer drive=%d\n", buffer_drive);
2800 printk("buffer track=%d\n", buffer_track);
2801 printk("buffer_min=%d\n", buffer_min);
2802 printk("buffer_max=%d\n", buffer_max);
2803 return 0;
2806 if (raw_cmd->kernel_data != CURRENT->buffer){
2807 if (raw_cmd->kernel_data < floppy_track_buffer ||
2808 current_count_sectors < 0 ||
2809 raw_cmd->length < 0 ||
2810 raw_cmd->kernel_data + raw_cmd->length >
2811 floppy_track_buffer + (max_buffer_sectors << 10)){
2812 DPRINT("buffer overrun in schedule dma\n");
2813 printk("sector_t=%d buffer_min=%d current_count=%ld\n",
2814 sector_t, buffer_min,
2815 raw_cmd->length >> 9);
2816 printk("current_count_sectors=%ld\n",
2817 current_count_sectors);
2818 if (CT(COMMAND) == FD_READ)
2819 printk("read\n");
2820 if (CT(COMMAND) == FD_READ)
2821 printk("write\n");
2822 return 0;
2824 } else if (raw_cmd->length > CURRENT->nr_sectors << 9 ||
2825 current_count_sectors > CURRENT->nr_sectors){
2826 DPRINT("buffer overrun in direct transfer\n");
2827 return 0;
2828 } else if (raw_cmd->length < current_count_sectors << 9){
2829 DPRINT("more sectors than bytes\n");
2830 printk("bytes=%ld\n", raw_cmd->length >> 9);
2831 printk("sectors=%ld\n", current_count_sectors);
2833 if (raw_cmd->length == 0){
2834 DPRINT("zero dma transfer attempted from make_raw_request\n");
2835 return 0;
2837 #endif
2839 virtualdmabug_workaround();
2840 return 2;
2843 static void redo_fd_request(void)
2845 #define REPEAT {request_done(0); continue; }
2846 kdev_t device;
2847 int tmp;
2849 lastredo = jiffies;
2850 if (current_drive < N_DRIVE)
2851 floppy_off(current_drive);
2853 if (CURRENT && CURRENT->rq_status == RQ_INACTIVE){
2854 CLEAR_INTR;
2855 unlock_fdc();
2856 return;
2859 while(1){
2860 if (!CURRENT) {
2861 CLEAR_INTR;
2862 unlock_fdc();
2863 return;
2865 if (MAJOR(CURRENT->rq_dev) != MAJOR_NR)
2866 panic(DEVICE_NAME ": request list destroyed");
2867 if (CURRENT->bh && !buffer_locked(CURRENT->bh))
2868 panic(DEVICE_NAME ": block not locked");
2870 device = CURRENT->rq_dev;
2871 set_fdc(DRIVE(device));
2872 reschedule_timeout(CURRENTD, "redo fd request", 0);
2874 set_floppy(device);
2875 raw_cmd = & default_raw_cmd;
2876 raw_cmd->flags = 0;
2877 if (start_motor(redo_fd_request)) return;
2878 disk_change(current_drive);
2879 if (test_bit(current_drive, &fake_change) ||
2880 TESTF(FD_DISK_CHANGED)){
2881 DPRINT("disk absent or changed during operation\n");
2882 REPEAT;
2884 if (!_floppy) { /* Autodetection */
2885 if (!probing){
2886 DRS->probed_format = 0;
2887 if (next_valid_format()){
2888 DPRINT("no autodetectable formats\n");
2889 _floppy = NULL;
2890 REPEAT;
2893 probing = 1;
2894 _floppy = floppy_type+DP->autodetect[DRS->probed_format];
2895 } else
2896 probing = 0;
2897 errors = & (CURRENT->errors);
2898 tmp = make_raw_rw_request();
2899 if (tmp < 2){
2900 request_done(tmp);
2901 continue;
2904 if (TESTF(FD_NEED_TWADDLE))
2905 twaddle();
2906 schedule_bh( (void *)(void *) floppy_start);
2907 #ifdef DEBUGT
2908 debugt("queue fd request");
2909 #endif
2910 return;
2912 #undef REPEAT
2915 static struct cont_t rw_cont={
2916 rw_interrupt,
2917 redo_fd_request,
2918 bad_flp_intr,
2919 request_done };
2921 static void process_fd_request(void)
2923 cont = &rw_cont;
2924 schedule_bh( (void *)(void *) redo_fd_request);
2927 static void do_fd_request(void)
2929 if(usage_count == 0) {
2930 printk("warning: usage count=0, CURRENT=%p exiting\n", CURRENT);
2931 printk("sect=%ld cmd=%d\n", CURRENT->sector, CURRENT->cmd);
2932 return;
2934 if (fdc_busy){
2935 /* fdc busy, this new request will be treated when the
2936 current one is done */
2937 is_alive("do fd request, old request running");
2938 return;
2940 lock_fdc(MAXTIMEOUT,0);
2941 process_fd_request();
2942 is_alive("do fd request");
2945 static struct cont_t poll_cont={
2946 success_and_wakeup,
2947 floppy_ready,
2948 generic_failure,
2949 generic_done };
2951 static int poll_drive(int interruptible, int flag)
2953 int ret;
2954 /* no auto-sense, just clear dcl */
2955 raw_cmd = &default_raw_cmd;
2956 raw_cmd->flags= flag;
2957 raw_cmd->track=0;
2958 raw_cmd->cmd_count=0;
2959 cont = &poll_cont;
2960 #ifdef DCL_DEBUG
2961 if (DP->flags & FD_DEBUG){
2962 DPRINT("setting NEWCHANGE in poll_drive\n");
2964 #endif
2965 SETF(FD_DISK_NEWCHANGE);
2966 WAIT(floppy_ready);
2967 return ret;
2971 * User triggered reset
2972 * ====================
2975 static void reset_intr(void)
2977 printk("weird, reset interrupt called\n");
2980 static struct cont_t reset_cont={
2981 reset_intr,
2982 success_and_wakeup,
2983 generic_failure,
2984 generic_done };
2986 static int user_reset_fdc(int drive, int arg, int interruptible)
2988 int ret;
2990 ret=0;
2991 LOCK_FDC(drive,interruptible);
2992 if (arg == FD_RESET_ALWAYS)
2993 FDCS->reset=1;
2994 if (FDCS->reset){
2995 cont = &reset_cont;
2996 WAIT(reset_fdc);
2998 process_fd_request();
2999 return ret;
3003 * Misc Ioctl's and support
3004 * ========================
3006 static inline int fd_copyout(void *param, const void *address, int size)
3008 return copy_to_user(param,address, size) ? -EFAULT : 0;
3011 static inline int fd_copyin(void *param, void *address, int size)
3013 return copy_from_user(address, param, size) ? -EFAULT : 0;
3016 #define _COPYOUT(x) (copy_to_user((void *)param, &(x), sizeof(x)) ? -EFAULT : 0)
3017 #define _COPYIN(x) (copy_from_user(&(x), (void *)param, sizeof(x)) ? -EFAULT : 0)
3019 #define COPYOUT(x) ECALL(_COPYOUT(x))
3020 #define COPYIN(x) ECALL(_COPYIN(x))
3022 static inline const char *drive_name(int type, int drive)
3024 struct floppy_struct *floppy;
3026 if (type)
3027 floppy = floppy_type + type;
3028 else {
3029 if (UDP->native_format)
3030 floppy = floppy_type + UDP->native_format;
3031 else
3032 return "(null)";
3034 if (floppy->name)
3035 return floppy->name;
3036 else
3037 return "(null)";
3041 /* raw commands */
3042 static void raw_cmd_done(int flag)
3044 int i;
3046 if (!flag) {
3047 raw_cmd->flags |= FD_RAW_FAILURE;
3048 raw_cmd->flags |= FD_RAW_HARDFAILURE;
3049 } else {
3050 raw_cmd->reply_count = inr;
3051 if(raw_cmd->reply_count > MAX_REPLIES)
3052 raw_cmd->reply_count=0;
3053 for (i=0; i< raw_cmd->reply_count; i++)
3054 raw_cmd->reply[i] = reply_buffer[i];
3056 if (raw_cmd->flags & (FD_RAW_READ | FD_RAW_WRITE))
3058 unsigned long flags;
3059 flags=claim_dma_lock();
3060 raw_cmd->length = fd_get_dma_residue();
3061 release_dma_lock(flags);
3064 if ((raw_cmd->flags & FD_RAW_SOFTFAILURE) &&
3065 (!raw_cmd->reply_count || (raw_cmd->reply[0] & 0xc0)))
3066 raw_cmd->flags |= FD_RAW_FAILURE;
3068 if (disk_change(current_drive))
3069 raw_cmd->flags |= FD_RAW_DISK_CHANGE;
3070 else
3071 raw_cmd->flags &= ~FD_RAW_DISK_CHANGE;
3072 if (raw_cmd->flags & FD_RAW_NO_MOTOR_AFTER)
3073 motor_off_callback(current_drive);
3075 if (raw_cmd->next &&
3076 (!(raw_cmd->flags & FD_RAW_FAILURE) ||
3077 !(raw_cmd->flags & FD_RAW_STOP_IF_FAILURE)) &&
3078 ((raw_cmd->flags & FD_RAW_FAILURE) ||
3079 !(raw_cmd->flags &FD_RAW_STOP_IF_SUCCESS))) {
3080 raw_cmd = raw_cmd->next;
3081 return;
3084 generic_done(flag);
3088 static struct cont_t raw_cmd_cont={
3089 success_and_wakeup,
3090 floppy_start,
3091 generic_failure,
3092 raw_cmd_done
3095 static inline int raw_cmd_copyout(int cmd, char *param,
3096 struct floppy_raw_cmd *ptr)
3098 int ret;
3100 while(ptr) {
3101 COPYOUT(*ptr);
3102 param += sizeof(struct floppy_raw_cmd);
3103 if ((ptr->flags & FD_RAW_READ) && ptr->buffer_length){
3104 if (ptr->length>=0 && ptr->length<=ptr->buffer_length)
3105 ECALL(fd_copyout(ptr->data,
3106 ptr->kernel_data,
3107 ptr->buffer_length -
3108 ptr->length));
3110 ptr = ptr->next;
3112 return 0;
3116 static void raw_cmd_free(struct floppy_raw_cmd **ptr)
3118 struct floppy_raw_cmd *next,*this;
3120 this = *ptr;
3121 *ptr = 0;
3122 while(this) {
3123 if (this->buffer_length) {
3124 fd_dma_mem_free((unsigned long)this->kernel_data,
3125 this->buffer_length);
3126 this->buffer_length = 0;
3128 next = this->next;
3129 kfree(this);
3130 this = next;
3135 static inline int raw_cmd_copyin(int cmd, char *param,
3136 struct floppy_raw_cmd **rcmd)
3138 struct floppy_raw_cmd *ptr;
3139 int ret;
3140 int i;
3142 *rcmd = 0;
3143 while(1) {
3144 ptr = (struct floppy_raw_cmd *)
3145 kmalloc(sizeof(struct floppy_raw_cmd), GFP_USER);
3146 if (!ptr)
3147 return -ENOMEM;
3148 *rcmd = ptr;
3149 COPYIN(*ptr);
3150 ptr->next = 0;
3151 ptr->buffer_length = 0;
3152 param += sizeof(struct floppy_raw_cmd);
3153 if (ptr->cmd_count > 33)
3154 /* the command may now also take up the space
3155 * initially intended for the reply & the
3156 * reply count. Needed for long 82078 commands
3157 * such as RESTORE, which takes ... 17 command
3158 * bytes. Murphy's law #137: When you reserve
3159 * 16 bytes for a structure, you'll one day
3160 * discover that you really need 17...
3162 return -EINVAL;
3164 for (i=0; i< 16; i++)
3165 ptr->reply[i] = 0;
3166 ptr->resultcode = 0;
3167 ptr->kernel_data = 0;
3169 if (ptr->flags & (FD_RAW_READ | FD_RAW_WRITE)) {
3170 if (ptr->length <= 0)
3171 return -EINVAL;
3172 ptr->kernel_data =(char*)fd_dma_mem_alloc(ptr->length);
3173 fallback_on_nodma_alloc(&ptr->kernel_data,
3174 ptr->length);
3175 if (!ptr->kernel_data)
3176 return -ENOMEM;
3177 ptr->buffer_length = ptr->length;
3179 if (ptr->flags & FD_RAW_WRITE)
3180 ECALL(fd_copyin(ptr->data, ptr->kernel_data,
3181 ptr->length));
3182 rcmd = & (ptr->next);
3183 if (!(ptr->flags & FD_RAW_MORE))
3184 return 0;
3185 ptr->rate &= 0x43;
3190 static int raw_cmd_ioctl(int cmd, void *param)
3192 int drive, ret, ret2;
3193 struct floppy_raw_cmd *my_raw_cmd;
3195 if (FDCS->rawcmd <= 1)
3196 FDCS->rawcmd = 1;
3197 for (drive= 0; drive < N_DRIVE; drive++){
3198 if (FDC(drive) != fdc)
3199 continue;
3200 if (drive == current_drive){
3201 if (UDRS->fd_ref > 1){
3202 FDCS->rawcmd = 2;
3203 break;
3205 } else if (UDRS->fd_ref){
3206 FDCS->rawcmd = 2;
3207 break;
3211 if (FDCS->reset)
3212 return -EIO;
3214 ret = raw_cmd_copyin(cmd, param, &my_raw_cmd);
3215 if (ret) {
3216 raw_cmd_free(&my_raw_cmd);
3217 return ret;
3220 raw_cmd = my_raw_cmd;
3221 cont = &raw_cmd_cont;
3222 ret=wait_til_done(floppy_start,1);
3223 #ifdef DCL_DEBUG
3224 if (DP->flags & FD_DEBUG){
3225 DPRINT("calling disk change from raw_cmd ioctl\n");
3227 #endif
3229 if (ret != -EINTR && FDCS->reset)
3230 ret = -EIO;
3232 DRS->track = NO_TRACK;
3234 ret2 = raw_cmd_copyout(cmd, param, my_raw_cmd);
3235 if (!ret)
3236 ret = ret2;
3237 raw_cmd_free(&my_raw_cmd);
3238 return ret;
3241 static int invalidate_drive(kdev_t rdev)
3243 /* invalidate the buffer track to force a reread */
3244 set_bit(DRIVE(rdev), &fake_change);
3245 process_fd_request();
3246 check_disk_change(rdev);
3247 return 0;
3251 static inline void clear_write_error(int drive)
3253 CLEARSTRUCT(UDRWE);
3256 static inline int set_geometry(unsigned int cmd, struct floppy_struct *g,
3257 int drive, int type, kdev_t device)
3259 int cnt;
3261 /* sanity checking for parameters.*/
3262 if (g->sect <= 0 ||
3263 g->head <= 0 ||
3264 g->track <= 0 ||
3265 g->track > UDP->tracks>>STRETCH(g) ||
3266 /* check if reserved bits are set */
3267 (g->stretch&~(FD_STRETCH|FD_SWAPSIDES)) != 0)
3268 return -EINVAL;
3269 if (type){
3270 if (!capable(CAP_SYS_ADMIN))
3271 return -EPERM;
3272 LOCK_FDC(drive,1);
3273 for (cnt = 0; cnt < N_DRIVE; cnt++){
3274 if (ITYPE(drive_state[cnt].fd_device) == type &&
3275 drive_state[cnt].fd_ref)
3276 set_bit(drive, &fake_change);
3278 floppy_type[type] = *g;
3279 floppy_type[type].name="user format";
3280 for (cnt = type << 2; cnt < (type << 2) + 4; cnt++)
3281 floppy_sizes[cnt]= floppy_sizes[cnt+0x80]=
3282 (floppy_type[type].size+1)>>1;
3283 process_fd_request();
3284 for (cnt = 0; cnt < N_DRIVE; cnt++){
3285 if (ITYPE(drive_state[cnt].fd_device) == type &&
3286 drive_state[cnt].fd_ref)
3287 check_disk_change(
3288 MKDEV(FLOPPY_MAJOR,
3289 drive_state[cnt].fd_device));
3291 } else {
3292 LOCK_FDC(drive,1);
3293 if (cmd != FDDEFPRM)
3294 /* notice a disk change immediately, else
3295 * we lose our settings immediately*/
3296 CALL(poll_drive(1, FD_RAW_NEED_DISK));
3297 user_params[drive] = *g;
3298 if (buffer_drive == drive)
3299 SUPBOUND(buffer_max, user_params[drive].sect);
3300 current_type[drive] = &user_params[drive];
3301 floppy_sizes[drive] = (user_params[drive].size+1) >> 1;
3302 if (cmd == FDDEFPRM)
3303 DRS->keep_data = -1;
3304 else
3305 DRS->keep_data = 1;
3306 /* invalidation. Invalidate only when needed, i.e.
3307 * when there are already sectors in the buffer cache
3308 * whose number will change. This is useful, because
3309 * mtools often changes the geometry of the disk after
3310 * looking at the boot block */
3311 if (DRS->maxblock > user_params[drive].sect || DRS->maxtrack)
3312 invalidate_drive(device);
3313 else
3314 process_fd_request();
3316 return 0;
3319 /* handle obsolete ioctl's */
3320 static int ioctl_table[]= {
3321 FDCLRPRM,
3322 FDSETPRM,
3323 FDDEFPRM,
3324 FDGETPRM,
3325 FDMSGON,
3326 FDMSGOFF,
3327 FDFMTBEG,
3328 FDFMTTRK,
3329 FDFMTEND,
3330 FDSETEMSGTRESH,
3331 FDFLUSH,
3332 FDSETMAXERRS,
3333 FDGETMAXERRS,
3334 FDGETDRVTYP,
3335 FDSETDRVPRM,
3336 FDGETDRVPRM,
3337 FDGETDRVSTAT,
3338 FDPOLLDRVSTAT,
3339 FDRESET,
3340 FDGETFDCSTAT,
3341 FDWERRORCLR,
3342 FDWERRORGET,
3343 FDRAWCMD,
3344 FDEJECT,
3345 FDTWADDLE
3348 static inline int normalize_ioctl(int *cmd, int *size)
3350 int i;
3352 for (i=0; i < ARRAY_SIZE(ioctl_table); i++) {
3353 if ((*cmd & 0xffff) == (ioctl_table[i] & 0xffff)){
3354 *size = _IOC_SIZE(*cmd);
3355 *cmd = ioctl_table[i];
3356 if (*size > _IOC_SIZE(*cmd)) {
3357 printk("ioctl not yet supported\n");
3358 return -EFAULT;
3360 return 0;
3363 return -EINVAL;
3366 static int get_floppy_geometry(int drive, int type, struct floppy_struct **g)
3368 if (type)
3369 *g = &floppy_type[type];
3370 else {
3371 LOCK_FDC(drive,0);
3372 CALL(poll_drive(0,0));
3373 process_fd_request();
3374 *g = current_type[drive];
3376 if(!*g)
3377 return -ENODEV;
3378 return 0;
3381 static int fd_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
3382 unsigned long param)
3384 #define IOCTL_MODE_BIT 8
3385 #define OPEN_WRITE_BIT 16
3386 #define IOCTL_ALLOWED (filp && (filp->f_mode & IOCTL_MODE_BIT))
3387 #define OUT(c,x) case c: outparam = (const char *) (x); break
3388 #define IN(c,x,tag) case c: *(x) = inparam. tag ; return 0
3390 int i,drive,type;
3391 kdev_t device;
3392 int ret;
3393 int size;
3394 union inparam {
3395 struct floppy_struct g; /* geometry */
3396 struct format_descr f;
3397 struct floppy_max_errors max_errors;
3398 struct floppy_drive_params dp;
3399 } inparam; /* parameters coming from user space */
3400 const char *outparam; /* parameters passed back to user space */
3402 device = inode->i_rdev;
3403 switch (cmd) {
3404 case BLKROSET:
3405 case BLKROGET:
3406 case BLKRASET:
3407 case BLKRAGET:
3408 case BLKFLSBUF:
3409 return blk_ioctl(device, cmd, param);
3411 type = TYPE(device);
3412 drive = DRIVE(device);
3414 /* convert compatibility eject ioctls into floppy eject ioctl.
3415 * We do this in order to provide a means to eject floppy disks before
3416 * installing the new fdutils package */
3417 if(cmd == CDROMEJECT || /* CD-ROM eject */
3418 cmd == 0x6470 /* SunOS floppy eject */) {
3419 DPRINT("obsolete eject ioctl\n");
3420 DPRINT("please use floppycontrol --eject\n");
3421 cmd = FDEJECT;
3424 /* generic block device ioctls */
3425 switch(cmd) {
3426 /* the following have been inspired by the corresponding
3427 * code for other block devices. */
3428 struct floppy_struct *g;
3429 case HDIO_GETGEO:
3431 struct hd_geometry loc;
3432 ECALL(get_floppy_geometry(drive, type, &g));
3433 loc.heads = g->head;
3434 loc.sectors = g->sect;
3435 loc.cylinders = g->track;
3436 loc.start = 0;
3437 return _COPYOUT(loc);
3440 case BLKGETSIZE:
3441 ECALL(get_floppy_geometry(drive, type, &g));
3442 return put_user(g->size, (long *) param);
3443 /* BLKRRPART is not defined as floppies don't have
3444 * partition tables */
3447 /* convert the old style command into a new style command */
3448 if ((cmd & 0xff00) == 0x0200) {
3449 ECALL(normalize_ioctl(&cmd, &size));
3450 } else
3451 return -EINVAL;
3453 /* permission checks */
3454 if (((cmd & 0x40) && !IOCTL_ALLOWED) ||
3455 ((cmd & 0x80) && !suser()))
3456 return -EPERM;
3458 /* copyin */
3459 CLEARSTRUCT(&inparam);
3460 if (_IOC_DIR(cmd) & _IOC_WRITE)
3461 ECALL(fd_copyin((void *)param, &inparam, size))
3463 switch (cmd) {
3464 case FDEJECT:
3465 if(UDRS->fd_ref != 1)
3466 /* somebody else has this drive open */
3467 return -EBUSY;
3468 LOCK_FDC(drive,1);
3470 /* do the actual eject. Fails on
3471 * non-Sparc architectures */
3472 ret=fd_eject(UNIT(drive));
3474 USETF(FD_DISK_CHANGED);
3475 USETF(FD_VERIFY);
3476 process_fd_request();
3477 return ret;
3478 case FDCLRPRM:
3479 LOCK_FDC(drive,1);
3480 current_type[drive] = NULL;
3481 floppy_sizes[drive] = MAX_DISK_SIZE;
3482 UDRS->keep_data = 0;
3483 return invalidate_drive(device);
3484 case FDSETPRM:
3485 case FDDEFPRM:
3486 return set_geometry(cmd, & inparam.g,
3487 drive, type, device);
3488 case FDGETPRM:
3489 ECALL(get_floppy_geometry(drive, type,
3490 (struct floppy_struct**)
3491 &outparam));
3492 break;
3494 case FDMSGON:
3495 UDP->flags |= FTD_MSG;
3496 return 0;
3497 case FDMSGOFF:
3498 UDP->flags &= ~FTD_MSG;
3499 return 0;
3501 case FDFMTBEG:
3502 LOCK_FDC(drive,1);
3503 CALL(poll_drive(1, FD_RAW_NEED_DISK));
3504 ret = UDRS->flags;
3505 process_fd_request();
3506 if(ret & FD_VERIFY)
3507 return -ENODEV;
3508 if(!(ret & FD_DISK_WRITABLE))
3509 return -EROFS;
3510 return 0;
3511 case FDFMTTRK:
3512 if (UDRS->fd_ref != 1)
3513 return -EBUSY;
3514 return do_format(device, &inparam.f);
3515 case FDFMTEND:
3516 case FDFLUSH:
3517 LOCK_FDC(drive,1);
3518 return invalidate_drive(device);
3520 case FDSETEMSGTRESH:
3521 UDP->max_errors.reporting =
3522 (unsigned short) (param & 0x0f);
3523 return 0;
3524 OUT(FDGETMAXERRS, &UDP->max_errors);
3525 IN(FDSETMAXERRS, &UDP->max_errors, max_errors);
3527 case FDGETDRVTYP:
3528 outparam = drive_name(type,drive);
3529 SUPBOUND(size,strlen(outparam)+1);
3530 break;
3532 IN(FDSETDRVPRM, UDP, dp);
3533 OUT(FDGETDRVPRM, UDP);
3535 case FDPOLLDRVSTAT:
3536 LOCK_FDC(drive,1);
3537 CALL(poll_drive(1, FD_RAW_NEED_DISK));
3538 process_fd_request();
3539 /* fall through */
3540 OUT(FDGETDRVSTAT, UDRS);
3542 case FDRESET:
3543 return user_reset_fdc(drive, (int)param, 1);
3545 OUT(FDGETFDCSTAT,UFDCS);
3547 case FDWERRORCLR:
3548 CLEARSTRUCT(UDRWE);
3549 return 0;
3550 OUT(FDWERRORGET,UDRWE);
3552 case FDRAWCMD:
3553 if (type)
3554 return -EINVAL;
3555 LOCK_FDC(drive,1);
3556 set_floppy(device);
3557 CALL(i = raw_cmd_ioctl(cmd,(void *) param));
3558 process_fd_request();
3559 return i;
3561 case FDTWADDLE:
3562 LOCK_FDC(drive,1);
3563 twaddle();
3564 process_fd_request();
3565 return 0;
3567 default:
3568 return -EINVAL;
3571 if (_IOC_DIR(cmd) & _IOC_READ)
3572 return fd_copyout((void *)param, outparam, size);
3573 else
3574 return 0;
3575 #undef IOCTL_ALLOWED
3576 #undef OUT
3577 #undef IN
3580 static void config_types(void)
3582 int first=1;
3583 int drive;
3585 /* read drive info out of physical CMOS */
3586 drive=0;
3587 if (!UDP->cmos)
3588 UDP->cmos = FLOPPY0_TYPE;
3589 drive=1;
3590 if (!UDP->cmos && FLOPPY1_TYPE)
3591 UDP->cmos = FLOPPY1_TYPE;
3593 /* XXX */
3594 /* additional physical CMOS drive detection should go here */
3596 for (drive=0; drive < N_DRIVE; drive++){
3597 unsigned int type = UDP->cmos;
3598 struct floppy_drive_params *params;
3599 const char *name = NULL;
3600 static char temparea[32];
3602 if (type < NUMBER(default_drive_params)) {
3603 params = &default_drive_params[type].params;
3604 if (type) {
3605 name = default_drive_params[type].name;
3606 allowed_drive_mask |= 1 << drive;
3608 } else {
3609 params = &default_drive_params[0].params;
3610 sprintf(temparea, "unknown type %d (usb?)", type);
3611 name = temparea;
3613 if (name) {
3614 const char * prepend = ",";
3615 if (first) {
3616 prepend = KERN_INFO "Floppy drive(s):";
3617 first = 0;
3619 printk("%s fd%d is %s", prepend, drive, name);
3621 *UDP = *params;
3623 if (!first)
3624 printk("\n");
3627 static ssize_t floppy_read(struct file * filp, char *buf,
3628 size_t count, loff_t *ppos)
3630 struct inode *inode = filp->f_dentry->d_inode;
3631 int drive = DRIVE(inode->i_rdev);
3633 check_disk_change(inode->i_rdev);
3634 if (UTESTF(FD_DISK_CHANGED))
3635 return -ENXIO;
3636 return block_read(filp, buf, count, ppos);
3639 static ssize_t floppy_write(struct file * filp, const char * buf,
3640 size_t count, loff_t *ppos)
3642 struct inode * inode = filp->f_dentry->d_inode;
3643 int block;
3644 int ret;
3645 int drive = DRIVE(inode->i_rdev);
3647 if (!UDRS->maxblock)
3648 UDRS->maxblock=1;/* make change detectable */
3649 check_disk_change(inode->i_rdev);
3650 if (UTESTF(FD_DISK_CHANGED))
3651 return -ENXIO;
3652 if (!UTESTF(FD_DISK_WRITABLE))
3653 return -EROFS;
3654 block = (*ppos + count) >> 9;
3655 INFBOUND(UDRS->maxblock, block);
3656 ret= block_write(filp, buf, count, ppos);
3657 return ret;
3660 static int floppy_release(struct inode * inode, struct file * filp)
3662 int drive;
3664 drive = DRIVE(inode->i_rdev);
3667 * If filp is NULL, we're being called from blkdev_release
3668 * or after a failed mount attempt. In the former case the
3669 * device has already been sync'ed, and in the latter no
3670 * sync is required. Otherwise, sync if filp is writable.
3672 if (filp && (filp->f_mode & (2 | OPEN_WRITE_BIT)))
3673 block_fsync(filp, filp->f_dentry);
3675 if (UDRS->fd_ref < 0)
3676 UDRS->fd_ref=0;
3677 else if (!UDRS->fd_ref--) {
3678 DPRINT("floppy_release with fd_ref == 0");
3679 UDRS->fd_ref = 0;
3681 floppy_release_irq_and_dma();
3682 return 0;
3686 * floppy_open check for aliasing (/dev/fd0 can be the same as
3687 * /dev/PS0 etc), and disallows simultaneous access to the same
3688 * drive with different device numbers.
3690 #define RETERR(x) do{floppy_release(inode,filp); return -(x);}while(0)
3692 static int floppy_open(struct inode * inode, struct file * filp)
3694 int drive;
3695 int old_dev;
3696 int try;
3697 char *tmp;
3699 if (!filp) {
3700 DPRINT("Weird, open called with filp=0\n");
3701 return -EIO;
3704 drive = DRIVE(inode->i_rdev);
3705 if (drive >= N_DRIVE ||
3706 !(allowed_drive_mask & (1 << drive)) ||
3707 fdc_state[FDC(drive)].version == FDC_NONE)
3708 return -ENXIO;
3710 if (TYPE(inode->i_rdev) >= NUMBER(floppy_type))
3711 return -ENXIO;
3712 old_dev = UDRS->fd_device;
3713 if (UDRS->fd_ref && old_dev != MINOR(inode->i_rdev))
3714 return -EBUSY;
3716 if (!UDRS->fd_ref && (UDP->flags & FD_BROKEN_DCL)){
3717 USETF(FD_DISK_CHANGED);
3718 USETF(FD_VERIFY);
3721 if (UDRS->fd_ref == -1 ||
3722 (UDRS->fd_ref && (filp->f_flags & O_EXCL)))
3723 return -EBUSY;
3725 if (floppy_grab_irq_and_dma())
3726 return -EBUSY;
3728 if (filp->f_flags & O_EXCL)
3729 UDRS->fd_ref = -1;
3730 else
3731 UDRS->fd_ref++;
3733 if (!floppy_track_buffer){
3734 /* if opening an ED drive, reserve a big buffer,
3735 * else reserve a small one */
3736 if ((UDP->cmos == 6) || (UDP->cmos == 5))
3737 try = 64; /* Only 48 actually useful */
3738 else
3739 try = 32; /* Only 24 actually useful */
3741 tmp=(char *)fd_dma_mem_alloc(1024 * try);
3742 if (!tmp && !floppy_track_buffer) {
3743 try >>= 1; /* buffer only one side */
3744 INFBOUND(try, 16);
3745 tmp= (char *)fd_dma_mem_alloc(1024*try);
3747 if(!tmp && !floppy_track_buffer) {
3748 fallback_on_nodma_alloc(&tmp, 2048 * try);
3750 if (!tmp && !floppy_track_buffer) {
3751 DPRINT("Unable to allocate DMA memory\n");
3752 RETERR(ENXIO);
3754 if (floppy_track_buffer) {
3755 if(tmp)
3756 fd_dma_mem_free((unsigned long)tmp,try*1024);
3757 } else {
3758 buffer_min = buffer_max = -1;
3759 floppy_track_buffer = tmp;
3760 max_buffer_sectors = try;
3764 UDRS->fd_device = MINOR(inode->i_rdev);
3765 if (old_dev != -1 && old_dev != MINOR(inode->i_rdev)) {
3766 if (buffer_drive == drive)
3767 buffer_track = -1;
3768 invalidate_buffers(MKDEV(FLOPPY_MAJOR,old_dev));
3771 /* Allow ioctls if we have write-permissions even if read-only open */
3772 if ((filp->f_mode & 2) || (permission(inode,2) == 0))
3773 filp->f_mode |= IOCTL_MODE_BIT;
3774 if (filp->f_mode & 2)
3775 filp->f_mode |= OPEN_WRITE_BIT;
3777 if (UFDCS->rawcmd == 1)
3778 UFDCS->rawcmd = 2;
3780 if (filp->f_flags & O_NDELAY)
3781 return 0;
3782 if (filp->f_mode & 3) {
3783 UDRS->last_checked = 0;
3784 check_disk_change(inode->i_rdev);
3785 if (UTESTF(FD_DISK_CHANGED))
3786 RETERR(ENXIO);
3788 if ((filp->f_mode & 2) && !(UTESTF(FD_DISK_WRITABLE)))
3789 RETERR(EROFS);
3790 return 0;
3791 #undef RETERR
3795 * Check if the disk has been changed or if a change has been faked.
3797 static int check_floppy_change(kdev_t dev)
3799 int drive = DRIVE(dev);
3801 if (MAJOR(dev) != MAJOR_NR) {
3802 DPRINT("check_floppy_change: not a floppy\n");
3803 return 0;
3806 if (UTESTF(FD_DISK_CHANGED) || UTESTF(FD_VERIFY))
3807 return 1;
3809 if (UDP->checkfreq < jiffies - UDRS->last_checked){
3810 lock_fdc(drive,0);
3811 poll_drive(0,0);
3812 process_fd_request();
3815 if (UTESTF(FD_DISK_CHANGED) ||
3816 UTESTF(FD_VERIFY) ||
3817 test_bit(drive, &fake_change) ||
3818 (!TYPE(dev) && !current_type[drive]))
3819 return 1;
3820 return 0;
3823 /* revalidate the floppy disk, i.e. trigger format autodetection by reading
3824 * the bootblock (block 0). "Autodetection" is also needed to check whether
3825 * there is a disk in the drive at all... Thus we also do it for fixed
3826 * geometry formats */
3827 static int floppy_revalidate(kdev_t dev)
3829 #define NO_GEOM (!current_type[drive] && !TYPE(dev))
3830 struct buffer_head * bh;
3831 int drive=DRIVE(dev);
3832 int cf;
3834 if (UTESTF(FD_DISK_CHANGED) ||
3835 UTESTF(FD_VERIFY) ||
3836 test_bit(drive, &fake_change) ||
3837 NO_GEOM){
3838 lock_fdc(drive,0);
3839 cf = UTESTF(FD_DISK_CHANGED) || UTESTF(FD_VERIFY);
3840 if (!(cf || test_bit(drive, &fake_change) || NO_GEOM)){
3841 process_fd_request(); /*already done by another thread*/
3842 return 0;
3844 UDRS->maxblock = 0;
3845 UDRS->maxtrack = 0;
3846 if (buffer_drive == drive)
3847 buffer_track = -1;
3848 clear_bit(drive, &fake_change);
3849 UCLEARF(FD_DISK_CHANGED);
3850 if (cf)
3851 UDRS->generation++;
3852 if (NO_GEOM){
3853 /* auto-sensing */
3854 int size = floppy_blocksizes[MINOR(dev)];
3855 if (!size)
3856 size = 1024;
3857 if (!(bh = getblk(dev,0,size))){
3858 process_fd_request();
3859 return 1;
3861 if (bh && !buffer_uptodate(bh))
3862 ll_rw_block(READ, 1, &bh);
3863 process_fd_request();
3864 wait_on_buffer(bh);
3865 brelse(bh);
3866 return 0;
3868 if (cf)
3869 poll_drive(0, FD_RAW_NEED_DISK);
3870 process_fd_request();
3872 return 0;
3875 static struct file_operations floppy_fops = {
3876 NULL, /* lseek - default */
3877 floppy_read, /* read - general block-dev read */
3878 floppy_write, /* write - general block-dev write */
3879 NULL, /* readdir - bad */
3880 NULL, /* poll */
3881 fd_ioctl, /* ioctl */
3882 NULL, /* mmap */
3883 floppy_open, /* open */
3884 NULL, /* flush */
3885 floppy_release, /* release */
3886 block_fsync, /* fsync */
3887 NULL, /* fasync */
3888 check_floppy_change, /* media_change */
3889 floppy_revalidate, /* revalidate */
3893 * Floppy Driver initialization
3894 * =============================
3897 /* Determine the floppy disk controller type */
3898 /* This routine was written by David C. Niemi */
3899 __initfunc(static char get_fdc_version(void))
3901 int r;
3903 output_byte(FD_DUMPREGS); /* 82072 and better know DUMPREGS */
3904 if (FDCS->reset)
3905 return FDC_NONE;
3906 if ((r = result()) <= 0x00)
3907 return FDC_NONE; /* No FDC present ??? */
3908 if ((r==1) && (reply_buffer[0] == 0x80)){
3909 printk(KERN_INFO "FDC %d is an 8272A\n",fdc);
3910 return FDC_8272A; /* 8272a/765 don't know DUMPREGS */
3912 if (r != 10) {
3913 printk("FDC %d init: DUMPREGS: unexpected return of %d bytes.\n",
3914 fdc, r);
3915 return FDC_UNKNOWN;
3918 if(!fdc_configure()) {
3919 printk(KERN_INFO "FDC %d is an 82072\n",fdc);
3920 return FDC_82072; /* 82072 doesn't know CONFIGURE */
3923 output_byte(FD_PERPENDICULAR);
3924 if(need_more_output() == MORE_OUTPUT) {
3925 output_byte(0);
3926 } else {
3927 printk(KERN_INFO "FDC %d is an 82072A\n", fdc);
3928 return FDC_82072A; /* 82072A as found on Sparcs. */
3931 output_byte(FD_UNLOCK);
3932 r = result();
3933 if ((r == 1) && (reply_buffer[0] == 0x80)){
3934 printk(KERN_INFO "FDC %d is a pre-1991 82077\n", fdc);
3935 return FDC_82077_ORIG; /* Pre-1991 82077, doesn't know
3936 * LOCK/UNLOCK */
3938 if ((r != 1) || (reply_buffer[0] != 0x00)) {
3939 printk("FDC %d init: UNLOCK: unexpected return of %d bytes.\n",
3940 fdc, r);
3941 return FDC_UNKNOWN;
3943 output_byte(FD_PARTID);
3944 r = result();
3945 if (r != 1) {
3946 printk("FDC %d init: PARTID: unexpected return of %d bytes.\n",
3947 fdc, r);
3948 return FDC_UNKNOWN;
3950 if (reply_buffer[0] == 0x80) {
3951 printk(KERN_INFO "FDC %d is a post-1991 82077\n",fdc);
3952 return FDC_82077; /* Revised 82077AA passes all the tests */
3954 switch (reply_buffer[0] >> 5) {
3955 case 0x0:
3956 /* Either a 82078-1 or a 82078SL running at 5Volt */
3957 printk(KERN_INFO "FDC %d is an 82078.\n",fdc);
3958 return FDC_82078;
3959 case 0x1:
3960 printk(KERN_INFO "FDC %d is a 44pin 82078\n",fdc);
3961 return FDC_82078;
3962 case 0x2:
3963 printk(KERN_INFO "FDC %d is a S82078B\n", fdc);
3964 return FDC_S82078B;
3965 case 0x3:
3966 printk(KERN_INFO "FDC %d is a National Semiconductor PC87306\n", fdc);
3967 return FDC_87306;
3968 default:
3969 printk(KERN_INFO "FDC %d init: 82078 variant with unknown PARTID=%d.\n",
3970 fdc, reply_buffer[0] >> 5);
3971 return FDC_82078_UNKN;
3973 } /* get_fdc_version */
3975 /* lilo configuration */
3977 __initfunc(static void floppy_set_flags(int *ints,int param, int param2))
3979 int i;
3981 for (i=0; i < ARRAY_SIZE(default_drive_params); i++){
3982 if (param)
3983 default_drive_params[i].params.flags |= param2;
3984 else
3985 default_drive_params[i].params.flags &= ~param2;
3987 DPRINT("%s flag 0x%x\n", param2 ? "Setting" : "Clearing", param);
3990 __initfunc(static void daring(int *ints,int param, int param2))
3992 int i;
3994 for (i=0; i < ARRAY_SIZE(default_drive_params); i++){
3995 if (param){
3996 default_drive_params[i].params.select_delay = 0;
3997 default_drive_params[i].params.flags |= FD_SILENT_DCL_CLEAR;
3998 } else {
3999 default_drive_params[i].params.select_delay = 2*HZ/100;
4000 default_drive_params[i].params.flags &= ~FD_SILENT_DCL_CLEAR;
4003 DPRINT("Assuming %s floppy hardware\n", param ? "standard" : "broken");
4006 __initfunc(static void set_cmos(int *ints, int dummy, int dummy2))
4008 int current_drive=0;
4010 if (ints[0] != 2){
4011 DPRINT("wrong number of parameters for CMOS\n");
4012 return;
4014 current_drive = ints[1];
4015 if (current_drive < 0 || current_drive >= 8){
4016 DPRINT("bad drive for set_cmos\n");
4017 return;
4019 if (current_drive >= 4 && !FDC2)
4020 FDC2 = 0x370;
4021 DP->cmos = ints[2];
4022 DPRINT("setting CMOS code to %d\n", ints[2]);
4025 static struct param_table {
4026 const char *name;
4027 void (*fn)(int *ints, int param, int param2);
4028 int *var;
4029 int def_param;
4030 int param2;
4031 } config_params[]={
4032 { "allowed_drive_mask", 0, &allowed_drive_mask, 0xff, 0}, /* obsolete */
4033 { "all_drives", 0, &allowed_drive_mask, 0xff, 0 }, /* obsolete */
4034 { "asus_pci", 0, &allowed_drive_mask, 0x33, 0},
4036 { "irq", 0, &FLOPPY_IRQ, 6, 0 },
4037 { "dma", 0, &FLOPPY_DMA, 2, 0 },
4039 { "daring", daring, 0, 1, 0},
4041 { "two_fdc", 0, &FDC2, 0x370, 0 },
4042 { "one_fdc", 0, &FDC2, 0, 0 },
4044 { "thinkpad", floppy_set_flags, 0, 1, FD_INVERTED_DCL },
4045 { "broken_dcl", floppy_set_flags, 0, 1, FD_BROKEN_DCL },
4046 { "messages", floppy_set_flags, 0, 1, FTD_MSG },
4047 { "silent_dcl_clear", floppy_set_flags, 0, 1, FD_SILENT_DCL_CLEAR },
4048 { "debug", floppy_set_flags, 0, 1, FD_DEBUG },
4050 { "nodma", 0, &can_use_virtual_dma, 1, 0 },
4051 { "omnibook", 0, &can_use_virtual_dma, 1, 0 },
4052 { "yesdma", 0, &can_use_virtual_dma, 0, 0 },
4054 { "fifo_depth", 0, &fifo_depth, 0xa, 0 },
4055 { "nofifo", 0, &no_fifo, 0x20, 0 },
4056 { "usefifo", 0, &no_fifo, 0, 0 },
4058 { "cmos", set_cmos, 0, 0, 0 },
4059 { "slow", 0, &slow_floppy, 1, 0 },
4061 { "unexpected_interrupts", 0, &print_unex, 1, 0 },
4062 { "no_unexpected_interrupts", 0, &print_unex, 0, 0 },
4063 { "L40SX", 0, &print_unex, 0, 0 } };
4065 #define FLOPPY_SETUP
4066 __initfunc(void floppy_setup(char *str, int *ints))
4068 int i;
4069 int param;
4070 if (str) {
4071 for (i=0; i< ARRAY_SIZE(config_params); i++){
4072 if (strcmp(str,config_params[i].name) == 0){
4073 if (ints[0])
4074 param = ints[1];
4075 else
4076 param = config_params[i].def_param;
4077 if(config_params[i].fn)
4078 config_params[i].
4079 fn(ints,param,
4080 config_params[i].param2);
4081 if(config_params[i].var) {
4082 DPRINT("%s=%d\n", str, param);
4083 *config_params[i].var = param;
4085 return;
4089 if (str) {
4090 DPRINT("unknown floppy option [%s]\n", str);
4092 DPRINT("allowed options are:");
4093 for (i=0; i< ARRAY_SIZE(config_params); i++)
4094 printk(" %s",config_params[i].name);
4095 printk("\n");
4096 } else
4097 DPRINT("botched floppy option\n");
4098 DPRINT("Read linux/drivers/block/README.fd\n");
4101 static int have_no_fdc= -EIO;
4104 __initfunc(int floppy_init(void))
4106 int i,unit,drive;
4109 raw_cmd = 0;
4111 if (register_blkdev(MAJOR_NR,"fd",&floppy_fops)) {
4112 printk("Unable to get major %d for floppy\n",MAJOR_NR);
4113 return -EBUSY;
4116 for (i=0; i<256; i++)
4117 if (ITYPE(i))
4118 floppy_sizes[i] = (floppy_type[ITYPE(i)].size+1) >> 1;
4119 else
4120 floppy_sizes[i] = MAX_DISK_SIZE;
4122 blk_size[MAJOR_NR] = floppy_sizes;
4123 blksize_size[MAJOR_NR] = floppy_blocksizes;
4124 blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
4125 reschedule_timeout(MAXTIMEOUT, "floppy init", MAXTIMEOUT);
4126 config_types();
4128 for (i = 0; i < N_FDC; i++) {
4129 fdc = i;
4130 CLEARSTRUCT(FDCS);
4131 FDCS->dtr = -1;
4132 FDCS->dor = 0x4;
4133 #ifdef __sparc__
4134 /*sparcs don't have a DOR reset which we can fall back on to*/
4135 FDCS->version = FDC_82072A;
4136 #endif
4139 use_virtual_dma = can_use_virtual_dma & 1;
4140 fdc_state[0].address = FDC1;
4141 if (fdc_state[0].address == -1) {
4142 unregister_blkdev(MAJOR_NR,"fd");
4143 del_timer(&fd_timeout);
4144 return -ENODEV;
4146 #if N_FDC > 1
4147 fdc_state[1].address = FDC2;
4148 #endif
4150 fdc = 0; /* reset fdc in case of unexpected interrupt */
4151 if (floppy_grab_irq_and_dma()){
4152 del_timer(&fd_timeout);
4153 blk_dev[MAJOR_NR].request_fn = NULL;
4154 unregister_blkdev(MAJOR_NR,"fd");
4155 del_timer(&fd_timeout);
4156 return -EBUSY;
4159 /* initialise drive state */
4160 for (drive = 0; drive < N_DRIVE; drive++) {
4161 CLEARSTRUCT(UDRS);
4162 CLEARSTRUCT(UDRWE);
4163 USETF(FD_DISK_NEWCHANGE);
4164 USETF(FD_DISK_CHANGED);
4165 USETF(FD_VERIFY);
4166 UDRS->fd_device = -1;
4167 floppy_track_buffer = NULL;
4168 max_buffer_sectors = 0;
4171 for (i = 0; i < N_FDC; i++) {
4172 fdc = i;
4173 FDCS->driver_version = FD_DRIVER_VERSION;
4174 for (unit=0; unit<4; unit++)
4175 FDCS->track[unit] = 0;
4176 if (FDCS->address == -1)
4177 continue;
4178 FDCS->rawcmd = 2;
4179 if (user_reset_fdc(-1,FD_RESET_ALWAYS,0)){
4180 /* free ioports reserved by floppy_grab_irq_and_dma() */
4181 release_region(FDCS->address, 6);
4182 release_region(FDCS->address+7, 1);
4183 FDCS->address = -1;
4184 FDCS->version = FDC_NONE;
4185 continue;
4187 /* Try to determine the floppy controller type */
4188 FDCS->version = get_fdc_version();
4189 if (FDCS->version == FDC_NONE){
4190 /* free ioports reserved by floppy_grab_irq_and_dma() */
4191 release_region(FDCS->address, 6);
4192 release_region(FDCS->address+7, 1);
4193 FDCS->address = -1;
4194 continue;
4196 if(can_use_virtual_dma == 2 && FDCS->version < FDC_82072A)
4197 can_use_virtual_dma = 0;
4199 have_no_fdc = 0;
4200 /* Not all FDCs seem to be able to handle the version command
4201 * properly, so force a reset for the standard FDC clones,
4202 * to avoid interrupt garbage.
4204 user_reset_fdc(-1,FD_RESET_ALWAYS,0);
4206 fdc=0;
4207 del_timer(&fd_timeout);
4208 current_drive = 0;
4209 floppy_release_irq_and_dma();
4210 initialising=0;
4211 if (have_no_fdc)
4213 DPRINT("no floppy controllers found\n");
4214 floppy_tq.routine = (void *)(void *) empty;
4215 mark_bh(IMMEDIATE_BH);
4216 schedule();
4217 if (usage_count)
4218 floppy_release_irq_and_dma();
4219 blk_dev[MAJOR_NR].request_fn = NULL;
4220 unregister_blkdev(MAJOR_NR,"fd");
4222 return have_no_fdc;
4225 static int floppy_grab_irq_and_dma(void)
4227 unsigned long flags;
4229 INT_OFF;
4230 if (usage_count++){
4231 INT_ON;
4232 return 0;
4234 INT_ON;
4235 MOD_INC_USE_COUNT;
4236 if (fd_request_irq()) {
4237 DPRINT("Unable to grab IRQ%d for the floppy driver\n",
4238 FLOPPY_IRQ);
4239 MOD_DEC_USE_COUNT;
4240 usage_count--;
4241 return -1;
4243 if (fd_request_dma()) {
4244 DPRINT("Unable to grab DMA%d for the floppy driver\n",
4245 FLOPPY_DMA);
4246 fd_free_irq();
4247 MOD_DEC_USE_COUNT;
4248 usage_count--;
4249 return -1;
4252 for (fdc=0; fdc< N_FDC; fdc++){
4253 if (FDCS->address != -1){
4254 if (check_region(FDCS->address, 6) < 0 ||
4255 check_region(FDCS->address+7, 1) < 0) {
4256 DPRINT("Floppy io-port 0x%04lx in use\n", FDCS->address);
4257 fd_free_irq();
4258 fd_free_dma();
4259 while(--fdc >= 0) {
4260 release_region(FDCS->address, 6);
4261 release_region(FDCS->address+7, 1);
4263 MOD_DEC_USE_COUNT;
4264 usage_count--;
4265 return -1;
4267 request_region(FDCS->address, 6, "floppy");
4268 request_region(FDCS->address+7, 1, "floppy DIR");
4269 /* address + 6 is reserved, and may be taken by IDE.
4270 * Unfortunately, Adaptec doesn't know this :-(, */
4273 for (fdc=0; fdc< N_FDC; fdc++){
4274 if (FDCS->address != -1){
4275 reset_fdc_info(1);
4276 fd_outb(FDCS->dor, FD_DOR);
4279 fdc = 0;
4280 set_dor(0, ~0, 8); /* avoid immediate interrupt */
4282 for (fdc = 0; fdc < N_FDC; fdc++)
4283 if (FDCS->address != -1)
4284 fd_outb(FDCS->dor, FD_DOR);
4286 * The driver will try and free resources and relies on us
4287 * to know if they were allocated or not.
4289 fdc = 0;
4290 irqdma_allocated = 1;
4291 return 0;
4294 static void floppy_release_irq_and_dma(void)
4296 int old_fdc;
4297 #ifdef FLOPPY_SANITY_CHECK
4298 #ifndef __sparc__
4299 int drive;
4300 #endif
4301 #endif
4302 long tmpsize;
4303 unsigned long tmpaddr;
4304 unsigned long flags;
4306 INT_OFF;
4307 if (--usage_count){
4308 INT_ON;
4309 return;
4311 INT_ON;
4312 if(irqdma_allocated)
4314 fd_disable_dma();
4315 fd_free_dma();
4316 fd_free_irq();
4317 irqdma_allocated=0;
4319 set_dor(0, ~0, 8);
4320 #if N_FDC > 1
4321 set_dor(1, ~8, 0);
4322 #endif
4323 floppy_enable_hlt();
4325 if (floppy_track_buffer && max_buffer_sectors) {
4326 tmpsize = max_buffer_sectors*1024;
4327 tmpaddr = (unsigned long)floppy_track_buffer;
4328 floppy_track_buffer = 0;
4329 max_buffer_sectors = 0;
4330 buffer_min = buffer_max = -1;
4331 fd_dma_mem_free(tmpaddr, tmpsize);
4334 #ifdef FLOPPY_SANITY_CHECK
4335 #ifndef __sparc__
4336 for (drive=0; drive < N_FDC * 4; drive++)
4337 if (motor_off_timer[drive].next)
4338 printk("motor off timer %d still active\n", drive);
4339 #endif
4341 if (fd_timeout.next)
4342 printk("floppy timer still active:%s\n", timeout_message);
4343 if (fd_timer.next)
4344 printk("auxiliary floppy timer still active\n");
4345 if (floppy_tq.sync)
4346 printk("task queue still active\n");
4347 #endif
4348 old_fdc = fdc;
4349 for (fdc = 0; fdc < N_FDC; fdc++)
4350 if (FDCS->address != -1) {
4351 release_region(FDCS->address, 6);
4352 release_region(FDCS->address+7, 1);
4354 fdc = old_fdc;
4355 MOD_DEC_USE_COUNT;
4359 #ifdef MODULE
4361 extern char *get_options(char *str, int *ints);
4363 char *floppy=NULL;
4365 __initfunc(static void parse_floppy_cfg_string(char *cfg))
4367 char *ptr;
4368 int ints[11];
4370 while(*cfg) {
4371 for(ptr = cfg;*cfg && *cfg != ' ' && *cfg != '\t'; cfg++);
4372 if(*cfg) {
4373 *cfg = '\0';
4374 cfg++;
4376 if(*ptr)
4377 floppy_setup(get_options(ptr,ints),ints);
4381 __initfunc(static void mod_setup(char *pattern, void (*setup)(char *, int *)))
4383 unsigned long i;
4384 char c;
4385 int j;
4386 int match;
4387 char buffer[100];
4388 int ints[11];
4389 int length = strlen(pattern)+1;
4391 match=0;
4392 j=1;
4394 for (i=current->mm->env_start; i< current->mm->env_end; i ++){
4395 get_user(c, (char *)i);
4396 if (match){
4397 if (j==99)
4398 c='\0';
4399 buffer[j] = c;
4400 if (!c || c == ' ' || c == '\t'){
4401 if (j){
4402 buffer[j] = '\0';
4403 setup(get_options(buffer,ints),ints);
4405 j=0;
4406 } else
4407 j++;
4408 if (!c)
4409 break;
4410 continue;
4412 if ((!j && !c) || (j && c == pattern[j-1]))
4413 j++;
4414 else
4415 j=0;
4416 if (j==length){
4417 match=1;
4418 j=0;
4424 #ifdef __cplusplus
4425 extern "C" {
4426 #endif
4427 int init_module(void)
4429 printk(KERN_INFO "inserting floppy driver for " UTS_RELEASE "\n");
4431 if(floppy)
4432 parse_floppy_cfg_string(floppy);
4433 else
4434 mod_setup("floppy=", floppy_setup);
4436 return floppy_init();
4439 void cleanup_module(void)
4441 int dummy;
4443 unregister_blkdev(MAJOR_NR, "fd");
4445 blk_dev[MAJOR_NR].request_fn = 0;
4446 /* eject disk, if any */
4447 dummy = fd_eject(0);
4450 MODULE_PARM(floppy,"s");
4451 MODULE_PARM(FLOPPY_IRQ,"i");
4452 MODULE_PARM(FLOPPY_DMA,"i");
4453 MODULE_AUTHOR("Alain L. Knaff");
4454 MODULE_SUPPORTED_DEVICE("fd");
4456 #ifdef __cplusplus
4458 #endif
4460 #else
4461 /* eject the boot floppy (if we need the drive for a different root floppy) */
4462 /* This should only be called at boot time when we're sure that there's no
4463 * resource contention. */
4464 void floppy_eject(void)
4466 int dummy;
4467 if(have_no_fdc)
4468 return;
4469 if(floppy_grab_irq_and_dma()==0)
4471 lock_fdc(MAXTIMEOUT,0);
4472 dummy=fd_eject(0);
4473 process_fd_request();
4474 floppy_release_irq_and_dma();
4477 #endif