1 /*
2 * linux/drivers/block/floppy.c
3 *
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.
13 */
14
15 /*
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.
21 */
22
23 /*
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.
29 */
30
31 /*
32 * 28.02.92 - made track-buffering routines, based on the routines written
33 * by entropy@wintermute.wpi.edu (Lawrence Foard). Linus.
34 */
35
36 /*
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.
40 */
41
42 /*
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.
46 *
47 * 1992/9/17: Added DMA allocation & DMA functions. -- hhb.
48 *
49 * TODO: Errors are still not counted properly.
50 */
51
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.
60 */
61
62 /*
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.
66 */
67
68 /* 1994/6/24 --bbroad-- added the floppy table entries and made
69 * minor modifications to allow 2.88 floppies to be run.
70 */
71
72 /* 1994/7/13 -- Paul Vojta -- modified the probing code to allow three or more
73 * disk types.
74 */
75
76 /*
77 * 1994/8/8 -- Alain Knaff -- Switched to fdpatch driver: Support for bigger
78 * format bug fixes, but unfortunately some new bugs too...
79 */
80
81 /* 1994/9/17 -- Koen Holtman -- added logging of physical floppy write
82 * errors to allow safe writing by specialized programs.
83 */
84
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").
89 */
90
91 /*
92 * 1995/8/26 -- Andreas Busse -- added Mips support.
93 */
94
95 /*
96 * 1995/10/18 -- Ralf Baechle -- Portability cleanup; move machine dependent
97 * features to asm/floppy.h.
98 */
99
100 /*
101 * 1998/05/07 -- Russell King -- More portability cleanups; moved definition of
102 * interrupt and dma channel to asm/floppy.h. Cleaned up some formatting &
103 * use of '' for NULL.
104 */
105
106 /*
107 * 1998/06/07 -- Alan Cox -- Merged the 2.0.34 fixes for resource allocation
108 * failures.
109 */
110
111 /*
112 * 1998/09/20 -- David Weinehall -- Added slow-down code for buggy PS/2-drives.
113 */
114
115 /*
116 * 1999/08/13 -- Paul Slootman -- floppy stopped working on Alpha after 24
117 * days, 6 hours, 32 minutes and 32 seconds (i.e. MAXINT jiffies; ints were
118 * being used to store jiffies, which are unsigned longs).
119 */
120
121 /*
122 * 2000/08/28 -- Arnaldo Carvalho de Melo <acme@conectiva.com.br>
123 * - get rid of check_region
124 * - s/suser/capable/
125 */
126
127 #define FLOPPY_SANITY_CHECK
128 #undef FLOPPY_SILENT_DCL_CLEAR
129
130 #define REALLY_SLOW_IO
131
132 #define DEBUGT 2
133 #define DCL_DEBUG /* debug disk change line */
134
135 /* do print messages for unexpected interrupts */
136 static int print_unex=1;
137 #include <linux/module.h>
138 #include <linux/sched.h>
139 #include <linux/fs.h>
140 #include <linux/kernel.h>
141 #include <linux/timer.h>
142 #include <linux/tqueue.h>
143 #define FDPATCHES
144 #include <linux/fdreg.h>
145
146 /*
147 * 1998/1/21 -- Richard Gooch <rgooch@atnf.csiro.au> -- devfs support
148 */
149
150
151 #include <linux/fd.h>
152 #include <linux/hdreg.h>
153
154 #include <linux/errno.h>
155 #include <linux/malloc.h>
156 #include <linux/mm.h>
157 #include <linux/string.h>
158 #include <linux/fcntl.h>
159 #include <linux/delay.h>
160 #include <linux/mc146818rtc.h> /* CMOS defines */
161 #include <linux/ioport.h>
162 #include <linux/interrupt.h>
163 #include <linux/init.h>
164 #include <linux/devfs_fs_kernel.h>
165
166 /*
167 * PS/2 floppies have much slower step rates than regular floppies.
168 * It's been recommended that take about 1/4 of the default speed
169 * in some more extreme cases.
170 */
171 static int slow_floppy;
172
173 #include <asm/dma.h>
174 #include <asm/irq.h>
175 #include <asm/system.h>
176 #include <asm/io.h>
177 #include <asm/uaccess.h>
178
179 static int FLOPPY_IRQ=6;
180 static int FLOPPY_DMA=2;
181 static int can_use_virtual_dma=2;
182 /* =======
183 * can use virtual DMA:
184 * 0 = use of virtual DMA disallowed by config
185 * 1 = use of virtual DMA prescribed by config
186 * 2 = no virtual DMA preference configured. By default try hard DMA,
187 * but fall back on virtual DMA when not enough memory available
188 */
189
190 static int use_virtual_dma;
191 /* =======
192 * use virtual DMA
193 * 0 using hard DMA
194 * 1 using virtual DMA
195 * This variable is set to virtual when a DMA mem problem arises, and
196 * reset back in floppy_grab_irq_and_dma.
197 * It is not safe to reset it in other circumstances, because the floppy
198 * driver may have several buffers in use at once, and we do currently not
199 * record each buffers capabilities
200 */
201
202 static unsigned short virtual_dma_port=0x3f0;
203 void floppy_interrupt(int irq, void *dev_id, struct pt_regs * regs);
204 static int set_dor(int fdc, char mask, char data);
205 static void register_devfs_entries (int drive) __init;
206 static devfs_handle_t devfs_handle;
207
208 #define K_64 0x10000 /* 64KB */
209
210 /* the following is the mask of allowed drives. By default units 2 and
211 * 3 of both floppy controllers are disabled, because switching on the
212 * motor of these drives causes system hangs on some PCI computers. drive
213 * 0 is the low bit (0x1), and drive 7 is the high bit (0x80). Bits are on if
214 * a drive is allowed.
215 *
216 * NOTE: This must come before we include the arch floppy header because
217 * some ports reference this variable from there. -DaveM
218 */
219
220 static int allowed_drive_mask = 0x33;
221
222 #include <asm/floppy.h>
223
224 static int irqdma_allocated;
225
226 #define MAJOR_NR FLOPPY_MAJOR
227
228 #include <linux/blk.h>
229 #include <linux/blkpg.h>
230 #include <linux/cdrom.h> /* for the compatibility eject ioctl */
231
232 #ifndef fd_get_dma_residue
233 #define fd_get_dma_residue() get_dma_residue(FLOPPY_DMA)
234 #endif
235
236 /* Dma Memory related stuff */
237
238 #ifndef fd_dma_mem_free
239 #define fd_dma_mem_free(addr, size) free_pages(addr, get_order(size))
240 #endif
241
242 #ifndef fd_dma_mem_alloc
243 #define fd_dma_mem_alloc(size) __get_dma_pages(GFP_KERNEL,get_order(size))
244 #endif
245
246 static inline void fallback_on_nodma_alloc(char **addr, size_t l)
247 {
248 #ifdef FLOPPY_CAN_FALLBACK_ON_NODMA
249 if (*addr)
250 return; /* we have the memory */
251 if (can_use_virtual_dma != 2)
252 return; /* no fallback allowed */
253 printk("DMA memory shortage. Temporarily falling back on virtual DMA\n");
254 *addr = (char *) nodma_mem_alloc(l);
255 #else
256 return;
257 #endif
258 }
259
260 /* End dma memory related stuff */
261
262 static unsigned long fake_change;
263 static int initialising=1;
264
265 static inline int TYPE(kdev_t x) {
266 return (MINOR(x)>>2) & 0x1f;
267 }
268 static inline int DRIVE(kdev_t x) {
269 return (MINOR(x)&0x03) | ((MINOR(x)&0x80) >> 5);
270 }
271 #define ITYPE(x) (((x)>>2) & 0x1f)
272 #define TOMINOR(x) ((x & 3) | ((x & 4) << 5))
273 #define UNIT(x) ((x) & 0x03) /* drive on fdc */
274 #define FDC(x) (((x) & 0x04) >> 2) /* fdc of drive */
275 #define REVDRIVE(fdc, unit) ((unit) + ((fdc) << 2))
276 /* reverse mapping from unit and fdc to drive */
277 #define DP (&drive_params[current_drive])
278 #define DRS (&drive_state[current_drive])
279 #define DRWE (&write_errors[current_drive])
280 #define FDCS (&fdc_state[fdc])
281 #define CLEARF(x) (clear_bit(x##_BIT, &DRS->flags))
282 #define SETF(x) (set_bit(x##_BIT, &DRS->flags))
283 #define TESTF(x) (test_bit(x##_BIT, &DRS->flags))
284
285 #define UDP (&drive_params[drive])
286 #define UDRS (&drive_state[drive])
287 #define UDRWE (&write_errors[drive])
288 #define UFDCS (&fdc_state[FDC(drive)])
289 #define UCLEARF(x) (clear_bit(x##_BIT, &UDRS->flags))
290 #define USETF(x) (set_bit(x##_BIT, &UDRS->flags))
291 #define UTESTF(x) (test_bit(x##_BIT, &UDRS->flags))
292
293 #define DPRINT(format, args...) printk(DEVICE_NAME "%d: " format, current_drive , ## args)
294
295 #define PH_HEAD(floppy,head) (((((floppy)->stretch & 2) >>1) ^ head) << 2)
296 #define STRETCH(floppy) ((floppy)->stretch & FD_STRETCH)
297
298 #define CLEARSTRUCT(x) memset((x), 0, sizeof(*(x)))
299
300 /* read/write */
301 #define COMMAND raw_cmd->cmd[0]
302 #define DR_SELECT raw_cmd->cmd[1]
303 #define TRACK raw_cmd->cmd[2]
304 #define HEAD raw_cmd->cmd[3]
305 #define SECTOR raw_cmd->cmd[4]
306 #define SIZECODE raw_cmd->cmd[5]
307 #define SECT_PER_TRACK raw_cmd->cmd[6]
308 #define GAP raw_cmd->cmd[7]
309 #define SIZECODE2 raw_cmd->cmd[8]
310 #define NR_RW 9
311
312 /* format */
313 #define F_SIZECODE raw_cmd->cmd[2]
314 #define F_SECT_PER_TRACK raw_cmd->cmd[3]
315 #define F_GAP raw_cmd->cmd[4]
316 #define F_FILL raw_cmd->cmd[5]
317 #define NR_F 6
318
319 /*
320 * Maximum disk size (in kilobytes). This default is used whenever the
321 * current disk size is unknown.
322 * [Now it is rather a minimum]
323 */
324 #define MAX_DISK_SIZE 4 /* 3984*/
325
326
327 /*
328 * globals used by 'result()'
329 */
330 #define MAX_REPLIES 16
331 static unsigned char reply_buffer[MAX_REPLIES];
332 static int inr; /* size of reply buffer, when called from interrupt */
333 #define ST0 (reply_buffer[0])
334 #define ST1 (reply_buffer[1])
335 #define ST2 (reply_buffer[2])
336 #define ST3 (reply_buffer[0]) /* result of GETSTATUS */
337 #define R_TRACK (reply_buffer[3])
338 #define R_HEAD (reply_buffer[4])
339 #define R_SECTOR (reply_buffer[5])
340 #define R_SIZECODE (reply_buffer[6])
341
342 #define SEL_DLY (2*HZ/100)
343
344 /*
345 * this struct defines the different floppy drive types.
346 */
347 static struct {
348 struct floppy_drive_params params;
349 const char *name; /* name printed while booting */
350 } default_drive_params[]= {
351 /* NOTE: the time values in jiffies should be in msec!
352 CMOS drive type
353 | Maximum data rate supported by drive type
354 | | Head load time, msec
355 | | | Head unload time, msec (not used)
356 | | | | Step rate interval, usec
357 | | | | | Time needed for spinup time (jiffies)
358 | | | | | | Timeout for spinning down (jiffies)
359 | | | | | | | Spindown offset (where disk stops)
360 | | | | | | | | Select delay
361 | | | | | | | | | RPS
362 | | | | | | | | | | Max number of tracks
363 | | | | | | | | | | | Interrupt timeout
364 | | | | | | | | | | | | Max nonintlv. sectors
365 | | | | | | | | | | | | | -Max Errors- flags */
366 {{0, 500, 16, 16, 8000, 1*HZ, 3*HZ, 0, SEL_DLY, 5, 80, 3*HZ, 20, {3,1,2,0,2}, 0,
367 0, { 7, 4, 8, 2, 1, 5, 3,10}, 3*HZ/2, 0 }, "unknown" },
368
369 {{1, 300, 16, 16, 8000, 1*HZ, 3*HZ, 0, SEL_DLY, 5, 40, 3*HZ, 17, {3,1,2,0,2}, 0,
370 0, { 1, 0, 0, 0, 0, 0, 0, 0}, 3*HZ/2, 1 }, "360K PC" }, /*5 1/4 360 KB PC*/
371
372 {{2, 500, 16, 16, 6000, 4*HZ/10, 3*HZ, 14, SEL_DLY, 6, 83, 3*HZ, 17, {3,1,2,0,2}, 0,
373 0, { 2, 5, 6,23,10,20,12, 0}, 3*HZ/2, 2 }, "1.2M" }, /*5 1/4 HD AT*/
374
375 {{3, 250, 16, 16, 3000, 1*HZ, 3*HZ, 0, SEL_DLY, 5, 83, 3*HZ, 20, {3,1,2,0,2}, 0,
376 0, { 4,22,21,30, 3, 0, 0, 0}, 3*HZ/2, 4 }, "720k" }, /*3 1/2 DD*/
377
378 {{4, 500, 16, 16, 4000, 4*HZ/10, 3*HZ, 10, SEL_DLY, 5, 83, 3*HZ, 20, {3,1,2,0,2}, 0,
379 0, { 7, 4,25,22,31,21,29,11}, 3*HZ/2, 7 }, "1.44M" }, /*3 1/2 HD*/
380
381 {{5, 1000, 15, 8, 3000, 4*HZ/10, 3*HZ, 10, SEL_DLY, 5, 83, 3*HZ, 40, {3,1,2,0,2}, 0,
382 0, { 7, 8, 4,25,28,22,31,21}, 3*HZ/2, 8 }, "2.88M AMI BIOS" }, /*3 1/2 ED*/
383
384 {{6, 1000, 15, 8, 3000, 4*HZ/10, 3*HZ, 10, SEL_DLY, 5, 83, 3*HZ, 40, {3,1,2,0,2}, 0,
385 0, { 7, 8, 4,25,28,22,31,21}, 3*HZ/2, 8 }, "2.88M" } /*3 1/2 ED*/
386 /* | --autodetected formats--- | | |
387 * read_track | | Name printed when booting
388 * | Native format
389 * Frequency of disk change checks */
390 };
391
392 static struct floppy_drive_params drive_params[N_DRIVE];
393 static struct floppy_drive_struct drive_state[N_DRIVE];
394 static struct floppy_write_errors write_errors[N_DRIVE];
395 static struct floppy_raw_cmd *raw_cmd, default_raw_cmd;
396
397 /*
398 * This struct defines the different floppy types.
399 *
400 * Bit 0 of 'stretch' tells if the tracks need to be doubled for some
401 * types (e.g. 360kB diskette in 1.2MB drive, etc.). Bit 1 of 'stretch'
402 * tells if the disk is in Commodore 1581 format, which means side 0 sectors
403 * are located on side 1 of the disk but with a side 0 ID, and vice-versa.
404 * This is the same as the Sharp MZ-80 5.25" CP/M disk format, except that the
405 * 1581's logical side 0 is on physical side 1, whereas the Sharp's logical
406 * side 0 is on physical side 0 (but with the misnamed sector IDs).
407 * 'stretch' should probably be renamed to something more general, like
408 * 'options'. Other parameters should be self-explanatory (see also
409 * setfdprm(8)).
410 */
411 /*
412 Size
413 | Sectors per track
414 | | Head
415 | | | Tracks
416 | | | | Stretch
417 | | | | | Gap 1 size
418 | | | | | | Data rate, | 0x40 for perp
419 | | | | | | | Spec1 (stepping rate, head unload
420 | | | | | | | | /fmt gap (gap2) */
421 static struct floppy_struct floppy_type[32] = {
422 { 0, 0,0, 0,0,0x00,0x00,0x00,0x00,NULL }, /* 0 no testing */
423 { 720, 9,2,40,0,0x2A,0x02,0xDF,0x50,"d360" }, /* 1 360KB PC */
424 { 2400,15,2,80,0,0x1B,0x00,0xDF,0x54,"h1200" }, /* 2 1.2MB AT */
425 { 720, 9,1,80,0,0x2A,0x02,0xDF,0x50,"D360" }, /* 3 360KB SS 3.5" */
426 { 1440, 9,2,80,0,0x2A,0x02,0xDF,0x50,"D720" }, /* 4 720KB 3.5" */
427 { 720, 9,2,40,1,0x23,0x01,0xDF,0x50,"h360" }, /* 5 360KB AT */
428 { 1440, 9,2,80,0,0x23,0x01,0xDF,0x50,"h720" }, /* 6 720KB AT */
429 { 2880,18,2,80,0,0x1B,0x00,0xCF,0x6C,"H1440" }, /* 7 1.44MB 3.5" */
430 { 5760,36,2,80,0,0x1B,0x43,0xAF,0x54,"E2880" }, /* 8 2.88MB 3.5" */
431 { 6240,39,2,80,0,0x1B,0x43,0xAF,0x28,"E3120" }, /* 9 3.12MB 3.5" */
432
433 { 2880,18,2,80,0,0x25,0x00,0xDF,0x02,"h1440" }, /* 10 1.44MB 5.25" */
434 { 3360,21,2,80,0,0x1C,0x00,0xCF,0x0C,"H1680" }, /* 11 1.68MB 3.5" */
435 { 820,10,2,41,1,0x25,0x01,0xDF,0x2E,"h410" }, /* 12 410KB 5.25" */
436 { 1640,10,2,82,0,0x25,0x02,0xDF,0x2E,"H820" }, /* 13 820KB 3.5" */
437 { 2952,18,2,82,0,0x25,0x00,0xDF,0x02,"h1476" }, /* 14 1.48MB 5.25" */
438 { 3444,21,2,82,0,0x25,0x00,0xDF,0x0C,"H1722" }, /* 15 1.72MB 3.5" */
439 { 840,10,2,42,1,0x25,0x01,0xDF,0x2E,"h420" }, /* 16 420KB 5.25" */
440 { 1660,10,2,83,0,0x25,0x02,0xDF,0x2E,"H830" }, /* 17 830KB 3.5" */
441 { 2988,18,2,83,0,0x25,0x00,0xDF,0x02,"h1494" }, /* 18 1.49MB 5.25" */
442 { 3486,21,2,83,0,0x25,0x00,0xDF,0x0C,"H1743" }, /* 19 1.74 MB 3.5" */
443
444 { 1760,11,2,80,0,0x1C,0x09,0xCF,0x00,"h880" }, /* 20 880KB 5.25" */
445 { 2080,13,2,80,0,0x1C,0x01,0xCF,0x00,"D1040" }, /* 21 1.04MB 3.5" */
446 { 2240,14,2,80,0,0x1C,0x19,0xCF,0x00,"D1120" }, /* 22 1.12MB 3.5" */
447 { 3200,20,2,80,0,0x1C,0x20,0xCF,0x2C,"h1600" }, /* 23 1.6MB 5.25" */
448 { 3520,22,2,80,0,0x1C,0x08,0xCF,0x2e,"H1760" }, /* 24 1.76MB 3.5" */
449 { 3840,24,2,80,0,0x1C,0x20,0xCF,0x00,"H1920" }, /* 25 1.92MB 3.5" */
450 { 6400,40,2,80,0,0x25,0x5B,0xCF,0x00,"E3200" }, /* 26 3.20MB 3.5" */
451 { 7040,44,2,80,0,0x25,0x5B,0xCF,0x00,"E3520" }, /* 27 3.52MB 3.5" */
452 { 7680,48,2,80,0,0x25,0x63,0xCF,0x00,"E3840" }, /* 28 3.84MB 3.5" */
453
454 { 3680,23,2,80,0,0x1C,0x10,0xCF,0x00,"H1840" }, /* 29 1.84MB 3.5" */
455 { 1600,10,2,80,0,0x25,0x02,0xDF,0x2E,"D800" }, /* 30 800KB 3.5" */
456 { 3200,20,2,80,0,0x1C,0x00,0xCF,0x2C,"H1600" }, /* 31 1.6MB 3.5" */
457 };
458
459 #define NUMBER(x) (sizeof(x) / sizeof(*(x)))
460 #define SECTSIZE (_FD_SECTSIZE(*floppy))
461
462 /* Auto-detection: Disk type used until the next media change occurs. */
463 static struct floppy_struct *current_type[N_DRIVE];
464
465 /*
466 * User-provided type information. current_type points to
467 * the respective entry of this array.
468 */
469 static struct floppy_struct user_params[N_DRIVE];
470
471 static int floppy_sizes[256];
472 static int floppy_blocksizes[256];
473
474 /*
475 * The driver is trying to determine the correct media format
476 * while probing is set. rw_interrupt() clears it after a
477 * successful access.
478 */
479 static int probing;
480
481 /* Synchronization of FDC access. */
482 #define FD_COMMAND_NONE -1
483 #define FD_COMMAND_ERROR 2
484 #define FD_COMMAND_OKAY 3
485
486 static volatile int command_status = FD_COMMAND_NONE;
487 static unsigned long fdc_busy;
488 static DECLARE_WAIT_QUEUE_HEAD(fdc_wait);
489 static DECLARE_WAIT_QUEUE_HEAD(command_done);
490
491 #define NO_SIGNAL (!interruptible || !signal_pending(current))
492 #define CALL(x) if ((x) == -EINTR) return -EINTR
493 #define ECALL(x) if ((ret = (x))) return ret;
494 #define _WAIT(x,i) CALL(ret=wait_til_done((x),i))
495 #define WAIT(x) _WAIT((x),interruptible)
496 #define IWAIT(x) _WAIT((x),1)
497
498 /* Errors during formatting are counted here. */
499 static int format_errors;
500
501 /* Format request descriptor. */
502 static struct format_descr format_req;
503
504 /*
505 * Rate is 0 for 500kb/s, 1 for 300kbps, 2 for 250kbps
506 * Spec1 is 0xSH, where S is stepping rate (F=1ms, E=2ms, D=3ms etc),
507 * H is head unload time (1=16ms, 2=32ms, etc)
508 */
509
510 /*
511 * Track buffer
512 * Because these are written to by the DMA controller, they must
513 * not contain a 64k byte boundary crossing, or data will be
514 * corrupted/lost.
515 */
516 static char *floppy_track_buffer;
517 static int max_buffer_sectors;
518
519 static int *errors;
520 typedef void (*done_f)(int);
521 static struct cont_t {
522 void (*interrupt)(void); /* this is called after the interrupt of the
523 * main command */
524 void (*redo)(void); /* this is called to retry the operation */
525 void (*error)(void); /* this is called to tally an error */
526 done_f done; /* this is called to say if the operation has
527 * succeeded/failed */
528 } *cont;
529
530 static void floppy_ready(void);
531 static void floppy_start(void);
532 static void process_fd_request(void);
533 static void recalibrate_floppy(void);
534 static void floppy_shutdown(void);
535
536 static int floppy_grab_irq_and_dma(void);
537 static void floppy_release_irq_and_dma(void);
538
539 /*
540 * The "reset" variable should be tested whenever an interrupt is scheduled,
541 * after the commands have been sent. This is to ensure that the driver doesn't
542 * get wedged when the interrupt doesn't come because of a failed command.
543 * reset doesn't need to be tested before sending commands, because
544 * output_byte is automatically disabled when reset is set.
545 */
546 #define CHECK_RESET { if (FDCS->reset){ reset_fdc(); return; } }
547 static void reset_fdc(void);
548
549 /*
550 * These are global variables, as that's the easiest way to give
551 * information to interrupts. They are the data used for the current
552 * request.
553 */
554 #define NO_TRACK -1
555 #define NEED_1_RECAL -2
556 #define NEED_2_RECAL -3
557
558 static int usage_count;
559
560 /* buffer related variables */
561 static int buffer_track = -1;
562 static int buffer_drive = -1;
563 static int buffer_min = -1;
564 static int buffer_max = -1;
565
566 /* fdc related variables, should end up in a struct */
567 static struct floppy_fdc_state fdc_state[N_FDC];
568 static int fdc; /* current fdc */
569
570 static struct floppy_struct *_floppy = floppy_type;
571 static unsigned char current_drive;
572 static long current_count_sectors;
573 static unsigned char sector_t; /* sector in track */
574 static unsigned char in_sector_offset; /* offset within physical sector,
575 * expressed in units of 512 bytes */
576
577 #ifndef fd_eject
578 #define fd_eject(x) -EINVAL
579 #endif
580
581 #ifdef DEBUGT
582 static long unsigned debugtimer;
583 #endif
584
585 /*
586 * Debugging
587 * =========
588 */
589 static inline void set_debugt(void)
590 {
591 #ifdef DEBUGT
592 debugtimer = jiffies;
593 #endif
594 }
595
596 static inline void debugt(const char *message)
597 {
598 #ifdef DEBUGT
599 if (DP->flags & DEBUGT)
600 printk("%s dtime=%lu\n", message, jiffies-debugtimer);
601 #endif
602 }
603
604 typedef void (*timeout_fn)(unsigned long);
605 static struct timer_list fd_timeout ={ function: (timeout_fn) floppy_shutdown };
606
607 static const char *timeout_message;
608
609 #ifdef FLOPPY_SANITY_CHECK
610 static void is_alive(const char *message)
611 {
612 /* this routine checks whether the floppy driver is "alive" */
613 if (fdc_busy && command_status < 2 && !timer_pending(&fd_timeout)){
614 DPRINT("timeout handler died: %s\n",message);
615 }
616 }
617 #endif
618
619 #ifdef FLOPPY_SANITY_CHECK
620
621 #define OLOGSIZE 20
622
623 static void (*lasthandler)(void);
624 static unsigned long interruptjiffies;
625 static unsigned long resultjiffies;
626 static int resultsize;
627 static unsigned long lastredo;
628
629 static struct output_log {
630 unsigned char data;
631 unsigned char status;
632 unsigned long jiffies;
633 } output_log[OLOGSIZE];
634
635 static int output_log_pos;
636 #endif
637
638 #define CURRENTD -1
639 #define MAXTIMEOUT -2
640
641 static void reschedule_timeout(int drive, const char *message, int marg)
642 {
643 if (drive == CURRENTD)
644 drive = current_drive;
645 del_timer(&fd_timeout);
646 if (drive < 0 || drive > N_DRIVE) {
647 fd_timeout.expires = jiffies + 20UL*HZ;
648 drive=0;
649 } else
650 fd_timeout.expires = jiffies + UDP->timeout;
651 add_timer(&fd_timeout);
652 if (UDP->flags & FD_DEBUG){
653 DPRINT("reschedule timeout ");
654 printk(message, marg);
655 printk("\n");
656 }
657 timeout_message = message;
658 }
659
660 static int maximum(int a, int b)
661 {
662 if (a > b)
663 return a;
664 else
665 return b;
666 }
667 #define INFBOUND(a,b) (a)=maximum((a),(b));
668
669 static int minimum(int a, int b)
670 {
671 if (a < b)
672 return a;
673 else
674 return b;
675 }
676 #define SUPBOUND(a,b) (a)=minimum((a),(b));
677
678
679 /*
680 * Bottom half floppy driver.
681 * ==========================
682 *
683 * This part of the file contains the code talking directly to the hardware,
684 * and also the main service loop (seek-configure-spinup-command)
685 */
686
687 /*
688 * disk change.
689 * This routine is responsible for maintaining the FD_DISK_CHANGE flag,
690 * and the last_checked date.
691 *
692 * last_checked is the date of the last check which showed 'no disk change'
693 * FD_DISK_CHANGE is set under two conditions:
694 * 1. The floppy has been changed after some i/o to that floppy already
695 * took place.
696 * 2. No floppy disk is in the drive. This is done in order to ensure that
697 * requests are quickly flushed in case there is no disk in the drive. It
698 * follows that FD_DISK_CHANGE can only be cleared if there is a disk in
699 * the drive.
700 *
701 * For 1., maxblock is observed. Maxblock is 0 if no i/o has taken place yet.
702 * For 2., FD_DISK_NEWCHANGE is watched. FD_DISK_NEWCHANGE is cleared on
703 * each seek. If a disk is present, the disk change line should also be
704 * cleared on each seek. Thus, if FD_DISK_NEWCHANGE is clear, but the disk
705 * change line is set, this means either that no disk is in the drive, or
706 * that it has been removed since the last seek.
707 *
708 * This means that we really have a third possibility too:
709 * The floppy has been changed after the last seek.
710 */
711
712 static int disk_change(int drive)
713 {
714 int fdc=FDC(drive);
715 #ifdef FLOPPY_SANITY_CHECK
716 if (jiffies - UDRS->select_date < UDP->select_delay)
717 DPRINT("WARNING disk change called early\n");
718 if (!(FDCS->dor & (0x10 << UNIT(drive))) ||
719 (FDCS->dor & 3) != UNIT(drive) ||
720 fdc != FDC(drive)){
721 DPRINT("probing disk change on unselected drive\n");
722 DPRINT("drive=%d fdc=%d dor=%x\n",drive, FDC(drive),
723 (unsigned int)FDCS->dor);
724 }
725 #endif
726
727 #ifdef DCL_DEBUG
728 if (UDP->flags & FD_DEBUG){
729 DPRINT("checking disk change line for drive %d\n",drive);
730 DPRINT("jiffies=%lu\n", jiffies);
731 DPRINT("disk change line=%x\n",fd_inb(FD_DIR)&0x80);
732 DPRINT("flags=%lx\n",UDRS->flags);
733 }
734 #endif
735 if (UDP->flags & FD_BROKEN_DCL)
736 return UTESTF(FD_DISK_CHANGED);
737 if ((fd_inb(FD_DIR) ^ UDP->flags) & 0x80){
738 USETF(FD_VERIFY); /* verify write protection */
739 if (UDRS->maxblock){
740 /* mark it changed */
741 USETF(FD_DISK_CHANGED);
742 }
743
744 /* invalidate its geometry */
745 if (UDRS->keep_data >= 0) {
746 if ((UDP->flags & FTD_MSG) &&
747 current_type[drive] != NULL)
748 DPRINT("Disk type is undefined after "
749 "disk change\n");
750 current_type[drive] = NULL;
751 floppy_sizes[TOMINOR(drive)] = MAX_DISK_SIZE;
752 }
753
754 /*USETF(FD_DISK_NEWCHANGE);*/
755 return 1;
756 } else {
757 UDRS->last_checked=jiffies;
758 UCLEARF(FD_DISK_NEWCHANGE);
759 }
760 return 0;
761 }
762
763 static inline int is_selected(int dor, int unit)
764 {
765 return ((dor & (0x10 << unit)) && (dor &3) == unit);
766 }
767
768 static int set_dor(int fdc, char mask, char data)
769 {
770 register unsigned char drive, unit, newdor,olddor;
771
772 if (FDCS->address == -1)
773 return -1;
774
775 olddor = FDCS->dor;
776 newdor = (olddor & mask) | data;
777 if (newdor != olddor){
778 unit = olddor & 0x3;
779 if (is_selected(olddor, unit) && !is_selected(newdor,unit)){
780 drive = REVDRIVE(fdc,unit);
781 #ifdef DCL_DEBUG
782 if (UDP->flags & FD_DEBUG){
783 DPRINT("calling disk change from set_dor\n");
784 }
785 #endif
786 disk_change(drive);
787 }
788 FDCS->dor = newdor;
789 fd_outb(newdor, FD_DOR);
790
791 unit = newdor & 0x3;
792 if (!is_selected(olddor, unit) && is_selected(newdor,unit)){
793 drive = REVDRIVE(fdc,unit);
794 UDRS->select_date = jiffies;
795 }
796 }
797 /*
798 * We should propogate failures to grab the resources back
799 * nicely from here. Actually we ought to rewrite the fd
800 * driver some day too.
801 */
802 if (newdor & FLOPPY_MOTOR_MASK)
803 floppy_grab_irq_and_dma();
804 if (olddor & FLOPPY_MOTOR_MASK)
805 floppy_release_irq_and_dma();
806 return olddor;
807 }
808
809 static void twaddle(void)
810 {
811 if (DP->select_delay)
812 return;
813 fd_outb(FDCS->dor & ~(0x10<<UNIT(current_drive)), FD_DOR);
814 fd_outb(FDCS->dor, FD_DOR);
815 DRS->select_date = jiffies;
816 }
817
818 /* reset all driver information about the current fdc. This is needed after
819 * a reset, and after a raw command. */
820 static void reset_fdc_info(int mode)
821 {
822 int drive;
823
824 FDCS->spec1 = FDCS->spec2 = -1;
825 FDCS->need_configure = 1;
826 FDCS->perp_mode = 1;
827 FDCS->rawcmd = 0;
828 for (drive = 0; drive < N_DRIVE; drive++)
829 if (FDC(drive) == fdc &&
830 (mode || UDRS->track != NEED_1_RECAL))
831 UDRS->track = NEED_2_RECAL;
832 }
833
834 /* selects the fdc and drive, and enables the fdc's input/dma. */
835 static void set_fdc(int drive)
836 {
837 if (drive >= 0 && drive < N_DRIVE){
838 fdc = FDC(drive);
839 current_drive = drive;
840 }
841 if (fdc != 1 && fdc != 0) {
842 printk("bad fdc value\n");
843 return;
844 }
845 set_dor(fdc,~0,8);
846 #if N_FDC > 1
847 set_dor(1-fdc, ~8, 0);
848 #endif
849 if (FDCS->rawcmd == 2)
850 reset_fdc_info(1);
851 if (fd_inb(FD_STATUS) != STATUS_READY)
852 FDCS->reset = 1;
853 }
854
855 /* locks the driver */
856 static int _lock_fdc(int drive, int interruptible, int line)
857 {
858 if (!usage_count){
859 printk(KERN_ERR "Trying to lock fdc while usage count=0 at line %d\n", line);
860 return -1;
861 }
862 if(floppy_grab_irq_and_dma()==-1)
863 return -EBUSY;
864
865 if (test_and_set_bit(0, &fdc_busy)) {
866 DECLARE_WAITQUEUE(wait, current);
867 add_wait_queue(&fdc_wait, &wait);
868
869 for (;;) {
870 set_current_state(TASK_INTERRUPTIBLE);
871
872 if (!test_and_set_bit(0, &fdc_busy))
873 break;
874
875 schedule();
876
877 if (!NO_SIGNAL) {
878 remove_wait_queue(&fdc_wait, &wait);
879 return -EINTR;
880 }
881 }
882
883 set_current_state(TASK_RUNNING);
884 remove_wait_queue(&fdc_wait, &wait);
885 }
886 command_status = FD_COMMAND_NONE;
887
888 reschedule_timeout(drive, "lock fdc", 0);
889 set_fdc(drive);
890 return 0;
891 }
892
893 #define lock_fdc(drive,interruptible) _lock_fdc(drive,interruptible, __LINE__)
894
895 #define LOCK_FDC(drive,interruptible) \
896 if (lock_fdc(drive,interruptible)) return -EINTR;
897
898
899 /* unlocks the driver */
900 static inline void unlock_fdc(void)
901 {
902 raw_cmd = 0;
903 if (!fdc_busy)
904 DPRINT("FDC access conflict!\n");
905
906 if (DEVICE_INTR)
907 DPRINT("device interrupt still active at FDC release: %p!\n",
908 DEVICE_INTR);
909 command_status = FD_COMMAND_NONE;
910 del_timer(&fd_timeout);
911 cont = NULL;
912 clear_bit(0, &fdc_busy);
913 floppy_release_irq_and_dma();
914 wake_up(&fdc_wait);
915 }
916
917 /* switches the motor off after a given timeout */
918 static void motor_off_callback(unsigned long nr)
919 {
920 unsigned char mask = ~(0x10 << UNIT(nr));
921
922 set_dor(FDC(nr), mask, 0);
923 }
924
925 static struct timer_list motor_off_timer[N_DRIVE] = {
926 { data: 0, function: motor_off_callback },
927 { data: 1, function: motor_off_callback },
928 { data: 2, function: motor_off_callback },
929 { data: 3, function: motor_off_callback },
930 { data: 4, function: motor_off_callback },
931 { data: 5, function: motor_off_callback },
932 { data: 6, function: motor_off_callback },
933 { data: 7, function: motor_off_callback }
934 };
935
936 /* schedules motor off */
937 static void floppy_off(unsigned int drive)
938 {
939 unsigned long volatile delta;
940 register int fdc=FDC(drive);
941
942 if (!(FDCS->dor & (0x10 << UNIT(drive))))
943 return;
944
945 del_timer(motor_off_timer+drive);
946
947 /* make spindle stop in a position which minimizes spinup time
948 * next time */
949 if (UDP->rps){
950 delta = jiffies - UDRS->first_read_date + HZ -
951 UDP->spindown_offset;
952 delta = ((delta * UDP->rps) % HZ) / UDP->rps;
953 motor_off_timer[drive].expires = jiffies + UDP->spindown - delta;
954 }
955 add_timer(motor_off_timer+drive);
956 }
957
958 /*
959 * cycle through all N_DRIVE floppy drives, for disk change testing.
960 * stopping at current drive. This is done before any long operation, to
961 * be sure to have up to date disk change information.
962 */
963 static void scandrives(void)
964 {
965 int i, drive, saved_drive;
966
967 if (DP->select_delay)
968 return;
969
970 saved_drive = current_drive;
971 for (i=0; i < N_DRIVE; i++){
972 drive = (saved_drive + i + 1) % N_DRIVE;
973 if (UDRS->fd_ref == 0 || UDP->select_delay != 0)
974 continue; /* skip closed drives */
975 set_fdc(drive);
976 if (!(set_dor(fdc, ~3, UNIT(drive) | (0x10 << UNIT(drive))) &
977 (0x10 << UNIT(drive))))
978 /* switch the motor off again, if it was off to
979 * begin with */
980 set_dor(fdc, ~(0x10 << UNIT(drive)), 0);
981 }
982 set_fdc(saved_drive);
983 }
984
985 static void empty(void)
986 {
987 }
988
989 static struct tq_struct floppy_tq;
990
991 static void schedule_bh( void (*handler)(void*) )
992 {
993 floppy_tq.routine = (void *)(void *) handler;
994 queue_task(&floppy_tq, &tq_immediate);
995 mark_bh(IMMEDIATE_BH);
996 }
997
998 static struct timer_list fd_timer;
999
1000 static void cancel_activity(void)
1001 {
1002 CLEAR_INTR;
1003 floppy_tq.routine = (void *)(void *) empty;
1004 del_timer(&fd_timer);
1005 }
1006
1007 /* this function makes sure that the disk stays in the drive during the
1008 * transfer */
1009 static void fd_watchdog(void)
1010 {
1011 #ifdef DCL_DEBUG
1012 if (DP->flags & FD_DEBUG){
1013 DPRINT("calling disk change from watchdog\n");
1014 }
1015 #endif
1016
1017 if (disk_change(current_drive)){
1018 DPRINT("disk removed during i/o\n");
1019 cancel_activity();
1020 cont->done(0);
1021 reset_fdc();
1022 } else {
1023 del_timer(&fd_timer);
1024 fd_timer.function = (timeout_fn) fd_watchdog;
1025 fd_timer.expires = jiffies + HZ / 10;
1026 add_timer(&fd_timer);
1027 }
1028 }
1029
1030 static void main_command_interrupt(void)
1031 {
1032 del_timer(&fd_timer);
1033 cont->interrupt();
1034 }
1035
1036 /* waits for a delay (spinup or select) to pass */
1037 static int wait_for_completion(unsigned long delay, timeout_fn function)
1038 {
1039 if (FDCS->reset){
1040 reset_fdc(); /* do the reset during sleep to win time
1041 * if we don't need to sleep, it's a good
1042 * occasion anyways */
1043 return 1;
1044 }
1045
1046 if ((signed) (jiffies - delay) < 0){
1047 del_timer(&fd_timer);
1048 fd_timer.function = function;
1049 fd_timer.expires = delay;
1050 add_timer(&fd_timer);
1051 return 1;
1052 }
1053 return 0;
1054 }
1055
1056 static spinlock_t floppy_hlt_lock = SPIN_LOCK_UNLOCKED;
1057 static int hlt_disabled;
1058 static void floppy_disable_hlt(void)
1059 {
1060 unsigned long flags;
1061
1062 spin_lock_irqsave(&floppy_hlt_lock, flags);
1063 if (!hlt_disabled) {
1064 hlt_disabled=1;
1065 #ifdef HAVE_DISABLE_HLT
1066 disable_hlt();
1067 #endif
1068 }
1069 spin_unlock_irqrestore(&floppy_hlt_lock, flags);
1070 }
1071
1072 static void floppy_enable_hlt(void)
1073 {
1074 unsigned long flags;
1075
1076 spin_lock_irqsave(&floppy_hlt_lock, flags);
1077 if (hlt_disabled){
1078 hlt_disabled=0;
1079 #ifdef HAVE_DISABLE_HLT
1080 enable_hlt();
1081 #endif
1082 }
1083 spin_unlock_irqrestore(&floppy_hlt_lock, flags);
1084 }
1085
1086
1087 static void setup_DMA(void)
1088 {
1089 unsigned long f;
1090
1091 #ifdef FLOPPY_SANITY_CHECK
1092 if (raw_cmd->length == 0){
1093 int i;
1094
1095 printk("zero dma transfer size:");
1096 for (i=0; i < raw_cmd->cmd_count; i++)
1097 printk("%x,", raw_cmd->cmd[i]);
1098 printk("\n");
1099 cont->done(0);
1100 FDCS->reset = 1;
1101 return;
1102 }
1103 if (((unsigned long) raw_cmd->kernel_data) % 512){
1104 printk("non aligned address: %p\n", raw_cmd->kernel_data);
1105 cont->done(0);
1106 FDCS->reset=1;
1107 return;
1108 }
1109 #endif
1110 f=claim_dma_lock();
1111 fd_disable_dma();
1112 #ifdef fd_dma_setup
1113 if (fd_dma_setup(raw_cmd->kernel_data, raw_cmd->length,
1114 (raw_cmd->flags & FD_RAW_READ)?
1115 DMA_MODE_READ : DMA_MODE_WRITE,
1116 FDCS->address) < 0) {
1117 release_dma_lock(f);
1118 cont->done(0);
1119 FDCS->reset=1;
1120 return;
1121 }
1122 release_dma_lock(f);
1123 #else
1124 fd_clear_dma_ff();
1125 fd_cacheflush(raw_cmd->kernel_data, raw_cmd->length);
1126 fd_set_dma_mode((raw_cmd->flags & FD_RAW_READ)?
1127 DMA_MODE_READ : DMA_MODE_WRITE);
1128 fd_set_dma_addr(raw_cmd->kernel_data);
1129 fd_set_dma_count(raw_cmd->length);
1130 virtual_dma_port = FDCS->address;
1131 fd_enable_dma();
1132 release_dma_lock(f);
1133 #endif
1134 floppy_disable_hlt();
1135 }
1136
1137 static void show_floppy(void);
1138
1139 /* waits until the fdc becomes ready */
1140 static int wait_til_ready(void)
1141 {
1142 int counter, status;
1143 if (FDCS->reset)
1144 return -1;
1145 for (counter = 0; counter < 10000; counter++) {
1146 status = fd_inb(FD_STATUS);
1147 if (status & STATUS_READY)
1148 return status;
1149 }
1150 if (!initialising) {
1151 DPRINT("Getstatus times out (%x) on fdc %d\n",
1152 status, fdc);
1153 show_floppy();
1154 }
1155 FDCS->reset = 1;
1156 return -1;
1157 }
1158
1159 /* sends a command byte to the fdc */
1160 static int output_byte(char byte)
1161 {
1162 int status;
1163
1164 if ((status = wait_til_ready()) < 0)
1165 return -1;
1166 if ((status & (STATUS_READY|STATUS_DIR|STATUS_DMA)) == STATUS_READY){
1167 fd_outb(byte,FD_DATA);
1168 #ifdef FLOPPY_SANITY_CHECK
1169 output_log[output_log_pos].data = byte;
1170 output_log[output_log_pos].status = status;
1171 output_log[output_log_pos].jiffies = jiffies;
1172 output_log_pos = (output_log_pos + 1) % OLOGSIZE;
1173 #endif
1174 return 0;
1175 }
1176 FDCS->reset = 1;
1177 if (!initialising) {
1178 DPRINT("Unable to send byte %x to FDC. Fdc=%x Status=%x\n",
1179 byte, fdc, status);
1180 show_floppy();
1181 }
1182 return -1;
1183 }
1184 #define LAST_OUT(x) if (output_byte(x)<0){ reset_fdc();return;}
1185
1186 /* gets the response from the fdc */
1187 static int result(void)
1188 {
1189 int i, status=0;
1190
1191 for(i=0; i < MAX_REPLIES; i++) {
1192 if ((status = wait_til_ready()) < 0)
1193 break;
1194 status &= STATUS_DIR|STATUS_READY|STATUS_BUSY|STATUS_DMA;
1195 if ((status & ~STATUS_BUSY) == STATUS_READY){
1196 #ifdef FLOPPY_SANITY_CHECK
1197 resultjiffies = jiffies;
1198 resultsize = i;
1199 #endif
1200 return i;
1201 }
1202 if (status == (STATUS_DIR|STATUS_READY|STATUS_BUSY))
1203 reply_buffer[i] = fd_inb(FD_DATA);
1204 else
1205 break;
1206 }
1207 if (!initialising) {
1208 DPRINT("get result error. Fdc=%d Last status=%x Read bytes=%d\n",
1209 fdc, status, i);
1210 show_floppy();
1211 }
1212 FDCS->reset = 1;
1213 return -1;
1214 }
1215
1216 #define MORE_OUTPUT -2
1217 /* does the fdc need more output? */
1218 static int need_more_output(void)
1219 {
1220 int status;
1221 if ((status = wait_til_ready()) < 0)
1222 return -1;
1223 if ((status & (STATUS_READY|STATUS_DIR|STATUS_DMA)) == STATUS_READY)
1224 return MORE_OUTPUT;
1225 return result();
1226 }
1227
1228 /* Set perpendicular mode as required, based on data rate, if supported.
1229 * 82077 Now tested. 1Mbps data rate only possible with 82077-1.
1230 */
1231 static inline void perpendicular_mode(void)
1232 {
1233 unsigned char perp_mode;
1234
1235 if (raw_cmd->rate & 0x40){
1236 switch(raw_cmd->rate & 3){
1237 case 0:
1238 perp_mode=2;
1239 break;
1240 case 3:
1241 perp_mode=3;
1242 break;
1243 default:
1244 DPRINT("Invalid data rate for perpendicular mode!\n");
1245 cont->done(0);
1246 FDCS->reset = 1; /* convenient way to return to
1247 * redo without to much hassle (deep
1248 * stack et al. */
1249 return;
1250 }
1251 } else
1252 perp_mode = 0;
1253
1254 if (FDCS->perp_mode == perp_mode)
1255 return;
1256 if (FDCS->version >= FDC_82077_ORIG) {
1257 output_byte(FD_PERPENDICULAR);
1258 output_byte(perp_mode);
1259 FDCS->perp_mode = perp_mode;
1260 } else if (perp_mode) {
1261 DPRINT("perpendicular mode not supported by this FDC.\n");
1262 }
1263 } /* perpendicular_mode */
1264
1265 static int fifo_depth = 0xa;
1266 static int no_fifo;
1267
1268 static int fdc_configure(void)
1269 {
1270 /* Turn on FIFO */
1271 output_byte(FD_CONFIGURE);
1272 if (need_more_output() != MORE_OUTPUT)
1273 return 0;
1274 output_byte(0);
1275 output_byte(0x10 | (no_fifo & 0x20) | (fifo_depth & 0xf));
1276 output_byte(0); /* pre-compensation from track
1277 0 upwards */
1278 return 1;
1279 }
1280
1281 #define NOMINAL_DTR 500
1282
1283 /* Issue a "SPECIFY" command to set the step rate time, head unload time,
1284 * head load time, and DMA disable flag to values needed by floppy.
1285 *
1286 * The value "dtr" is the data transfer rate in Kbps. It is needed
1287 * to account for the data rate-based scaling done by the 82072 and 82077
1288 * FDC types. This parameter is ignored for other types of FDCs (i.e.
1289 * 8272a).
1290 *
1291 * Note that changing the data transfer rate has a (probably deleterious)
1292 * effect on the parameters subject to scaling for 82072/82077 FDCs, so
1293 * fdc_specify is called again after each data transfer rate
1294 * change.
1295 *
1296 * srt: 1000 to 16000 in microseconds
1297 * hut: 16 to 240 milliseconds
1298 * hlt: 2 to 254 milliseconds
1299 *
1300 * These values are rounded up to the next highest available delay time.
1301 */
1302 static void fdc_specify(void)
1303 {
1304 unsigned char spec1, spec2;
1305 unsigned long srt, hlt, hut;
1306 unsigned long dtr = NOMINAL_DTR;
1307 unsigned long scale_dtr = NOMINAL_DTR;
1308 int hlt_max_code = 0x7f;
1309 int hut_max_code = 0xf;
1310
1311 if (FDCS->need_configure && FDCS->version >= FDC_82072A) {
1312 fdc_configure();
1313 FDCS->need_configure = 0;
1314 /*DPRINT("FIFO enabled\n");*/
1315 }
1316
1317 switch (raw_cmd->rate & 0x03) {
1318 case 3:
1319 dtr = 1000;
1320 break;
1321 case 1:
1322 dtr = 300;
1323 if (FDCS->version >= FDC_82078) {
1324 /* chose the default rate table, not the one
1325 * where 1 = 2 Mbps */
1326 output_byte(FD_DRIVESPEC);
1327 if (need_more_output() == MORE_OUTPUT) {
1328 output_byte(UNIT(current_drive));
1329 output_byte(0xc0);
1330 }
1331 }
1332 break;
1333 case 2:
1334 dtr = 250;
1335 break;
1336 }
1337
1338 if (FDCS->version >= FDC_82072) {
1339 scale_dtr = dtr;
1340 hlt_max_code = 0x00; /* 0==256msec*dtr0/dtr (not linear!) */
1341 hut_max_code = 0x0; /* 0==256msec*dtr0/dtr (not linear!) */
1342 }
1343
1344 /* Convert step rate from microseconds to milliseconds and 4 bits */
1345 srt = 16 - (DP->srt*scale_dtr/1000 + NOMINAL_DTR - 1)/NOMINAL_DTR;
1346 if( slow_floppy ) {
1347 srt = srt / 4;
1348 }
1349 SUPBOUND(srt, 0xf);
1350 INFBOUND(srt, 0);
1351
1352 hlt = (DP->hlt*scale_dtr/2 + NOMINAL_DTR - 1)/NOMINAL_DTR;
1353 if (hlt < 0x01)
1354 hlt = 0x01;
1355 else if (hlt > 0x7f)
1356 hlt = hlt_max_code;
1357
1358 hut = (DP->hut*scale_dtr/16 + NOMINAL_DTR - 1)/NOMINAL_DTR;
1359 if (hut < 0x1)
1360 hut = 0x1;
1361 else if (hut > 0xf)
1362 hut = hut_max_code;
1363
1364 spec1 = (srt << 4) | hut;
1365 spec2 = (hlt << 1) | (use_virtual_dma & 1);
1366
1367 /* If these parameters did not change, just return with success */
1368 if (FDCS->spec1 != spec1 || FDCS->spec2 != spec2) {
1369 /* Go ahead and set spec1 and spec2 */
1370 output_byte(FD_SPECIFY);
1371 output_byte(FDCS->spec1 = spec1);
1372 output_byte(FDCS->spec2 = spec2);
1373 }
1374 } /* fdc_specify */
1375
1376 /* Set the FDC's data transfer rate on behalf of the specified drive.
1377 * NOTE: with 82072/82077 FDCs, changing the data rate requires a reissue
1378 * of the specify command (i.e. using the fdc_specify function).
1379 */
1380 static int fdc_dtr(void)
1381 {
1382 /* If data rate not already set to desired value, set it. */
1383 if ((raw_cmd->rate & 3) == FDCS->dtr)
1384 return 0;
1385
1386 /* Set dtr */
1387 fd_outb(raw_cmd->rate & 3, FD_DCR);
1388
1389 /* TODO: some FDC/drive combinations (C&T 82C711 with TEAC 1.2MB)
1390 * need a stabilization period of several milliseconds to be
1391 * enforced after data rate changes before R/W operations.
1392 * Pause 5 msec to avoid trouble. (Needs to be 2 jiffies)
1393 */
1394 FDCS->dtr = raw_cmd->rate & 3;
1395 return(wait_for_completion(jiffies+2UL*HZ/100,
1396 (timeout_fn) floppy_ready));
1397 } /* fdc_dtr */
1398
1399 static void tell_sector(void)
1400 {
1401 printk(": track %d, head %d, sector %d, size %d",
1402 R_TRACK, R_HEAD, R_SECTOR, R_SIZECODE);
1403 } /* tell_sector */
1404
1405
1406 /*
1407 * OK, this error interpreting routine is called after a
1408 * DMA read/write has succeeded
1409 * or failed, so we check the results, and copy any buffers.
1410 * hhb: Added better error reporting.
1411 * ak: Made this into a separate routine.
1412 */
1413 static int interpret_errors(void)
1414 {
1415 char bad;
1416
1417 if (inr!=7) {
1418 DPRINT("-- FDC reply error");
1419 FDCS->reset = 1;
1420 return 1;
1421 }
1422
1423 /* check IC to find cause of interrupt */
1424 switch (ST0 & ST0_INTR) {
1425 case 0x40: /* error occurred during command execution */
1426 if (ST1 & ST1_EOC)
1427 return 0; /* occurs with pseudo-DMA */
1428 bad = 1;
1429 if (ST1 & ST1_WP) {
1430 DPRINT("Drive is write protected\n");
1431 CLEARF(FD_DISK_WRITABLE);
1432 cont->done(0);
1433 bad = 2;
1434 } else if (ST1 & ST1_ND) {
1435 SETF(FD_NEED_TWADDLE);
1436 } else if (ST1 & ST1_OR) {
1437 if (DP->flags & FTD_MSG)
1438 DPRINT("Over/Underrun - retrying\n");
1439 bad = 0;
1440 }else if (*errors >= DP->max_errors.reporting){
1441 DPRINT("");
1442 if (ST0 & ST0_ECE) {
1443 printk("Recalibrate failed!");
1444 } else if (ST2 & ST2_CRC) {
1445 printk("data CRC error");
1446 tell_sector();
1447 } else if (ST1 & ST1_CRC) {
1448 printk("CRC error");
1449 tell_sector();
1450 } else if ((ST1 & (ST1_MAM|ST1_ND)) || (ST2 & ST2_MAM)) {
1451 if (!probing) {
1452 printk("sector not found");
1453 tell_sector();
1454 } else
1455 printk("probe failed...");
1456 } else if (ST2 & ST2_WC) { /* seek error */
1457 printk("wrong cylinder");
1458 } else if (ST2 & ST2_BC) { /* cylinder marked as bad */
1459 printk("bad cylinder");
1460 } else {
1461 printk("unknown error. ST[0..2] are: 0x%x 0x%x 0x%x", ST0, ST1, ST2);
1462 tell_sector();
1463 }
1464 printk("\n");
1465
1466 }
1467 if (ST2 & ST2_WC || ST2 & ST2_BC)
1468 /* wrong cylinder => recal */
1469 DRS->track = NEED_2_RECAL;
1470 return bad;
1471 case 0x80: /* invalid command given */
1472 DPRINT("Invalid FDC command given!\n");
1473 cont->done(0);
1474 return 2;
1475 case 0xc0:
1476 DPRINT("Abnormal termination caused by polling\n");
1477 cont->error();
1478 return 2;
1479 default: /* (0) Normal command termination */
1480 return 0;
1481 }
1482 }
1483
1484 /*
1485 * This routine is called when everything should be correctly set up
1486 * for the transfer (i.e. floppy motor is on, the correct floppy is
1487 * selected, and the head is sitting on the right track).
1488 */
1489 static void setup_rw_floppy(void)
1490 {
1491 int i,r, flags,dflags;
1492 unsigned long ready_date;
1493 timeout_fn function;
1494
1495 flags = raw_cmd->flags;
1496 if (flags & (FD_RAW_READ | FD_RAW_WRITE))
1497 flags |= FD_RAW_INTR;
1498
1499 if ((flags & FD_RAW_SPIN) && !(flags & FD_RAW_NO_MOTOR)){
1500 ready_date = DRS->spinup_date + DP->spinup;
1501 /* If spinup will take a long time, rerun scandrives
1502 * again just before spinup completion. Beware that
1503 * after scandrives, we must again wait for selection.
1504 */
1505 if ((signed) (ready_date - jiffies) > DP->select_delay){
1506 ready_date -= DP->select_delay;
1507 function = (timeout_fn) floppy_start;
1508 } else
1509 function = (timeout_fn) setup_rw_floppy;
1510
1511 /* wait until the floppy is spinning fast enough */
1512 if (wait_for_completion(ready_date,function))
1513 return;
1514 }
1515 dflags = DRS->flags;
1516
1517 if ((flags & FD_RAW_READ) || (flags & FD_RAW_WRITE))
1518 setup_DMA();
1519
1520 if (flags & FD_RAW_INTR)
1521 SET_INTR(main_command_interrupt);
1522
1523 r=0;
1524 for (i=0; i< raw_cmd->cmd_count; i++)
1525 r|=output_byte(raw_cmd->cmd[i]);
1526
1527 #ifdef DEBUGT
1528 debugt("rw_command: ");
1529 #endif
1530 if (r){
1531 cont->error();
1532 reset_fdc();
1533 return;
1534 }
1535
1536 if (!(flags & FD_RAW_INTR)){
1537 inr = result();
1538 cont->interrupt();
1539 } else if (flags & FD_RAW_NEED_DISK)
1540 fd_watchdog();
1541 }
1542
1543 static int blind_seek;
1544
1545 /*
1546 * This is the routine called after every seek (or recalibrate) interrupt
1547 * from the floppy controller.
1548 */
1549 static void seek_interrupt(void)
1550 {
1551 #ifdef DEBUGT
1552 debugt("seek interrupt:");
1553 #endif
1554 if (inr != 2 || (ST0 & 0xF8) != 0x20) {
1555 DPRINT("seek failed\n");
1556 DRS->track = NEED_2_RECAL;
1557 cont->error();
1558 cont->redo();
1559 return;
1560 }
1561 if (DRS->track >= 0 && DRS->track != ST1 && !blind_seek){
1562 #ifdef DCL_DEBUG
1563 if (DP->flags & FD_DEBUG){
1564 DPRINT("clearing NEWCHANGE flag because of effective seek\n");
1565 DPRINT("jiffies=%lu\n", jiffies);
1566 }
1567 #endif
1568 CLEARF(FD_DISK_NEWCHANGE); /* effective seek */
1569 DRS->select_date = jiffies;
1570 }
1571 DRS->track = ST1;
1572 floppy_ready();
1573 }
1574
1575 static void check_wp(void)
1576 {
1577 if (TESTF(FD_VERIFY)) {
1578 /* check write protection */
1579 output_byte(FD_GETSTATUS);
1580 output_byte(UNIT(current_drive));
1581 if (result() != 1){
1582 FDCS->reset = 1;
1583 return;
1584 }
1585 CLEARF(FD_VERIFY);
1586 CLEARF(FD_NEED_TWADDLE);
1587 #ifdef DCL_DEBUG
1588 if (DP->flags & FD_DEBUG){
1589 DPRINT("checking whether disk is write protected\n");
1590 DPRINT("wp=%x\n",ST3 & 0x40);
1591 }
1592 #endif
1593 if (!(ST3 & 0x40))
1594 SETF(FD_DISK_WRITABLE);
1595 else
1596 CLEARF(FD_DISK_WRITABLE);
1597 }
1598 }
1599
1600 static void seek_floppy(void)
1601 {
1602 int track;
1603
1604 blind_seek=0;
1605
1606 #ifdef DCL_DEBUG
1607 if (DP->flags & FD_DEBUG){
1608 DPRINT("calling disk change from seek\n");
1609 }
1610 #endif
1611
1612 if (!TESTF(FD_DISK_NEWCHANGE) &&
1613 disk_change(current_drive) &&
1614 (raw_cmd->flags & FD_RAW_NEED_DISK)){
1615 /* the media changed flag should be cleared after the seek.
1616 * If it isn't, this means that there is really no disk in
1617 * the drive.
1618 */
1619 SETF(FD_DISK_CHANGED);
1620 cont->done(0);
1621 cont->redo();
1622 return;
1623 }
1624 if (DRS->track <= NEED_1_RECAL){
1625 recalibrate_floppy();
1626 return;
1627 } else if (TESTF(FD_DISK_NEWCHANGE) &&
1628 (raw_cmd->flags & FD_RAW_NEED_DISK) &&
1629 (DRS->track <= NO_TRACK || DRS->track == raw_cmd->track)) {
1630 /* we seek to clear the media-changed condition. Does anybody
1631 * know a more elegant way, which works on all drives? */
1632 if (raw_cmd->track)
1633 track = raw_cmd->track - 1;
1634 else {
1635 if (DP->flags & FD_SILENT_DCL_CLEAR){
1636 set_dor(fdc, ~(0x10 << UNIT(current_drive)), 0);
1637 blind_seek = 1;
1638 raw_cmd->flags |= FD_RAW_NEED_SEEK;
1639 }
1640 track = 1;
1641 }
1642 } else {
1643 check_wp();
1644 if (raw_cmd->track != DRS->track &&
1645 (raw_cmd->flags & FD_RAW_NEED_SEEK))
1646 track = raw_cmd->track;
1647 else {
1648 setup_rw_floppy();
1649 return;
1650 }
1651 }
1652
1653 SET_INTR(seek_interrupt);
1654 output_byte(FD_SEEK);
1655 output_byte(UNIT(current_drive));
1656 LAST_OUT(track);
1657 #ifdef DEBUGT
1658 debugt("seek command:");
1659 #endif
1660 }
1661
1662 static void recal_interrupt(void)
1663 {
1664 #ifdef DEBUGT
1665 debugt("recal interrupt:");
1666 #endif
1667 if (inr !=2)
1668 FDCS->reset = 1;
1669 else if (ST0 & ST0_ECE) {
1670 switch(DRS->track){
1671 case NEED_1_RECAL:
1672 #ifdef DEBUGT
1673 debugt("recal interrupt need 1 recal:");
1674 #endif
1675 /* after a second recalibrate, we still haven't
1676 * reached track 0. Probably no drive. Raise an
1677 * error, as failing immediately might upset
1678 * computers possessed by the Devil :-) */
1679 cont->error();
1680 cont->redo();
1681 return;
1682 case NEED_2_RECAL:
1683 #ifdef DEBUGT
1684 debugt("recal interrupt need 2 recal:");
1685 #endif
1686 /* If we already did a recalibrate,
1687 * and we are not at track 0, this
1688 * means we have moved. (The only way
1689 * not to move at recalibration is to
1690 * be already at track 0.) Clear the
1691 * new change flag */
1692 #ifdef DCL_DEBUG
1693 if (DP->flags & FD_DEBUG){
1694 DPRINT("clearing NEWCHANGE flag because of second recalibrate\n");
1695 }
1696 #endif
1697
1698 CLEARF(FD_DISK_NEWCHANGE);
1699 DRS->select_date = jiffies;
1700 /* fall through */
1701 default:
1702 #ifdef DEBUGT
1703 debugt("recal interrupt default:");
1704 #endif
1705 /* Recalibrate moves the head by at
1706 * most 80 steps. If after one
1707 * recalibrate we don't have reached
1708 * track 0, this might mean that we
1709 * started beyond track 80. Try
1710 * again. */
1711 DRS->track = NEED_1_RECAL;
1712 break;
1713 }
1714 } else
1715 DRS->track = ST1;
1716 floppy_ready();
1717 }
1718
1719 static void print_result(char *message, int inr)
1720 {
1721 int i;
1722
1723 DPRINT("%s ", message);
1724 if (inr >= 0)
1725 for (i=0; i<inr; i++)
1726 printk("repl[%d]=%x ", i, reply_buffer[i]);
1727 printk("\n");
1728 }
1729
1730 /* interrupt handler. Note that this can be called externally on the Sparc */
1731 void floppy_interrupt(int irq, void *dev_id, struct pt_regs * regs)
1732 {
1733 void (*handler)(void) = DEVICE_INTR;
1734 int do_print;
1735 unsigned long f;
1736
1737 lasthandler = handler;
1738 interruptjiffies = jiffies;
1739
1740 f=claim_dma_lock();
1741 fd_disable_dma();
1742 release_dma_lock(f);
1743
1744 floppy_enable_hlt();
1745 CLEAR_INTR;
1746 if (fdc >= N_FDC || FDCS->address == -1){
1747 /* we don't even know which FDC is the culprit */
1748 printk("DOR0=%x\n", fdc_state[0].dor);
1749 printk("floppy interrupt on bizarre fdc %d\n",fdc);
1750 printk("handler=%p\n", handler);
1751 is_alive("bizarre fdc");
1752 return;
1753 }
1754
1755 FDCS->reset = 0;
1756 /* We have to clear the reset flag here, because apparently on boxes
1757 * with level triggered interrupts (PS/2, Sparc, ...), it is needed to
1758 * emit SENSEI's to clear the interrupt line. And FDCS->reset blocks the
1759 * emission of the SENSEI's.
1760 * It is OK to emit floppy commands because we are in an interrupt
1761 * handler here, and thus we have to fear no interference of other
1762 * activity.
1763 */
1764
1765 do_print = !handler && print_unex && !initialising;
1766
1767 inr = result();
1768 if (do_print)
1769 print_result("unexpected interrupt", inr);
1770 if (inr == 0){
1771 int max_sensei = 4;
1772 do {
1773 output_byte(FD_SENSEI);
1774 inr = result();
1775 if (do_print)
1776 print_result("sensei", inr);
1777 max_sensei--;
1778 } while ((ST0 & 0x83) != UNIT(current_drive) && inr == 2 && max_sensei);
1779 }
1780 if (handler) {
1781 schedule_bh( (void *)(void *) handler);
1782 } else
1783 FDCS->reset = 1;
1784 is_alive("normal interrupt end");
1785 }
1786
1787 static void recalibrate_floppy(void)
1788 {
1789 #ifdef DEBUGT
1790 debugt("recalibrate floppy:");
1791 #endif
1792 SET_INTR(recal_interrupt);
1793 output_byte(FD_RECALIBRATE);
1794 LAST_OUT(UNIT(current_drive));
1795 }
1796
1797 /*
1798 * Must do 4 FD_SENSEIs after reset because of ``drive polling''.
1799 */
1800 static void reset_interrupt(void)
1801 {
1802 #ifdef DEBUGT
1803 debugt("reset interrupt:");
1804 #endif
1805 result(); /* get the status ready for set_fdc */
1806 if (FDCS->reset) {
1807 printk("reset set in interrupt, calling %p\n", cont->error);
1808 cont->error(); /* a reset just after a reset. BAD! */
1809 }
1810 cont->redo();
1811 }
1812
1813 /*
1814 * reset is done by pulling bit 2 of DOR low for a while (old FDCs),
1815 * or by setting the self clearing bit 7 of STATUS (newer FDCs)
1816 */
1817 static void reset_fdc(void)
1818 {
1819 unsigned long flags;
1820
1821 SET_INTR(reset_interrupt);
1822 FDCS->reset = 0;
1823 reset_fdc_info(0);
1824
1825 /* Pseudo-DMA may intercept 'reset finished' interrupt. */
1826 /* Irrelevant for systems with true DMA (i386). */
1827
1828 flags=claim_dma_lock();
1829 fd_disable_dma();
1830 release_dma_lock(flags);
1831
1832 if (FDCS->version >= FDC_82072A)
1833 fd_outb(0x80 | (FDCS->dtr &3), FD_STATUS);
1834 else {
1835 fd_outb(FDCS->dor & ~0x04, FD_DOR);
1836 udelay(FD_RESET_DELAY);
1837 fd_outb(FDCS->dor, FD_DOR);
1838 }
1839 }
1840
1841 static void show_floppy(void)
1842 {
1843 int i;
1844
1845 printk("\n");
1846 printk("floppy driver state\n");
1847 printk("-------------------\n");
1848 printk("now=%lu last interrupt=%lu diff=%lu last called handler=%p\n",
1849 jiffies, interruptjiffies, jiffies-interruptjiffies, lasthandler);
1850
1851
1852 #ifdef FLOPPY_SANITY_CHECK
1853 printk("timeout_message=%s\n", timeout_message);
1854 printk("last output bytes:\n");
1855 for (i=0; i < OLOGSIZE; i++)
1856 printk("%2x %2x %lu\n",
1857 output_log[(i+output_log_pos) % OLOGSIZE].data,
1858 output_log[(i+output_log_pos) % OLOGSIZE].status,
1859 output_log[(i+output_log_pos) % OLOGSIZE].jiffies);
1860 printk("last result at %lu\n", resultjiffies);
1861 printk("last redo_fd_request at %lu\n", lastredo);
1862 for (i=0; i<resultsize; i++){
1863 printk("%2x ", reply_buffer[i]);
1864 }
1865 printk("\n");
1866 #endif
1867
1868 printk("status=%x\n", fd_inb(FD_STATUS));
1869 printk("fdc_busy=%lu\n", fdc_busy);
1870 if (DEVICE_INTR)
1871 printk("DEVICE_INTR=%p\n", DEVICE_INTR);
1872 if (floppy_tq.sync)
1873 printk("floppy_tq.routine=%p\n", floppy_tq.routine);
1874 if (timer_pending(&fd_timer))
1875 printk("fd_timer.function=%p\n", fd_timer.function);
1876 if (timer_pending(&fd_timeout)){
1877 printk("timer_function=%p\n",fd_timeout.function);
1878 printk("expires=%lu\n",fd_timeout.expires-jiffies);
1879 printk("now=%lu\n",jiffies);
1880 }
1881 printk("cont=%p\n", cont);
1882 printk("CURRENT=%p\n", CURRENT);
1883 printk("command_status=%d\n", command_status);
1884 printk("\n");
1885 }
1886
1887 static void floppy_shutdown(void)
1888 {
1889 unsigned long flags;
1890
1891 if (!initialising)
1892 show_floppy();
1893 cancel_activity();
1894
1895 floppy_enable_hlt();
1896
1897 flags=claim_dma_lock();
1898 fd_disable_dma();
1899 release_dma_lock(flags);
1900
1901 /* avoid dma going to a random drive after shutdown */
1902
1903 if (!initialising)
1904 DPRINT("floppy timeout called\n");
1905 FDCS->reset = 1;
1906 if (cont){
1907 cont->done(0);
1908 cont->redo(); /* this will recall reset when needed */
1909 } else {
1910 printk("no cont in shutdown!\n");
1911 process_fd_request();
1912 }
1913 is_alive("floppy shutdown");
1914 }
1915 /*typedef void (*timeout_fn)(unsigned long);*/
1916
1917 /* start motor, check media-changed condition and write protection */
1918 static int start_motor(void (*function)(void) )
1919 {
1920 int mask, data;
1921
1922 mask = 0xfc;
1923 data = UNIT(current_drive);
1924 if (!(raw_cmd->flags & FD_RAW_NO_MOTOR)){
1925 if (!(FDCS->dor & (0x10 << UNIT(current_drive)))){
1926 set_debugt();
1927 /* no read since this drive is running */
1928 DRS->first_read_date = 0;
1929 /* note motor start time if motor is not yet running */
1930 DRS->spinup_date = jiffies;
1931 data |= (0x10 << UNIT(current_drive));
1932 }
1933 } else
1934 if (FDCS->dor & (0x10 << UNIT(current_drive)))
1935 mask &= ~(0x10 << UNIT(current_drive));
1936
1937 /* starts motor and selects floppy */
1938 del_timer(motor_off_timer + current_drive);
1939 set_dor(fdc, mask, data);
1940
1941 /* wait_for_completion also schedules reset if needed. */
1942 return(wait_for_completion(DRS->select_date+DP->select_delay,
1943 (timeout_fn) function));
1944 }
1945
1946 static void floppy_ready(void)
1947 {
1948 CHECK_RESET;
1949 if (start_motor(floppy_ready)) return;
1950 if (fdc_dtr()) return;
1951
1952 #ifdef DCL_DEBUG
1953 if (DP->flags & FD_DEBUG){
1954 DPRINT("calling disk change from floppy_ready\n");
1955 }
1956 #endif
1957 if (!(raw_cmd->flags & FD_RAW_NO_MOTOR) &&
1958 disk_change(current_drive) &&
1959 !DP->select_delay)
1960 twaddle(); /* this clears the dcl on certain drive/controller
1961 * combinations */
1962
1963 #ifdef fd_chose_dma_mode
1964 if ((raw_cmd->flags & FD_RAW_READ) ||
1965 (raw_cmd->flags & FD_RAW_WRITE))
1966 {
1967 unsigned long flags = claim_dma_lock();
1968 fd_chose_dma_mode(raw_cmd->kernel_data,
1969 raw_cmd->length);
1970 release_dma_lock(flags);
1971 }
1972 #endif
1973
1974 if (raw_cmd->flags & (FD_RAW_NEED_SEEK | FD_RAW_NEED_DISK)){
1975 perpendicular_mode();
1976 fdc_specify(); /* must be done here because of hut, hlt ... */
1977 seek_floppy();
1978 } else {
1979 if ((raw_cmd->flags & FD_RAW_READ) ||
1980 (raw_cmd->flags & FD_RAW_WRITE))
1981 fdc_specify();
1982 setup_rw_floppy();
1983 }
1984 }
1985
1986 static void floppy_start(void)
1987 {
1988 reschedule_timeout(CURRENTD, "floppy start", 0);
1989
1990 scandrives();
1991 #ifdef DCL_DEBUG
1992 if (DP->flags & FD_DEBUG){
1993 DPRINT("setting NEWCHANGE in floppy_start\n");
1994 }
1995 #endif
1996 SETF(FD_DISK_NEWCHANGE);
1997 floppy_ready();
1998 }
1999
2000 /*
2001 * ========================================================================
2002 * here ends the bottom half. Exported routines are:
2003 * floppy_start, floppy_off, floppy_ready, lock_fdc, unlock_fdc, set_fdc,
2004 * start_motor, reset_fdc, reset_fdc_info, interpret_errors.
2005 * Initialization also uses output_byte, result, set_dor, floppy_interrupt
2006 * and set_dor.
2007 * ========================================================================
2008 */
2009 /*
2010 * General purpose continuations.
2011 * ==============================
2012 */
2013
2014 static void do_wakeup(void)
2015 {
2016 reschedule_timeout(MAXTIMEOUT, "do wakeup", 0);
2017 cont = 0;
2018 command_status += 2;
2019 wake_up(&command_done);
2020 }
2021
2022 static struct cont_t wakeup_cont={
2023 empty,
2024 do_wakeup,
2025 empty,
2026 (done_f)empty
2027 };
2028
2029
2030 static struct cont_t intr_cont={
2031 empty,
2032 process_fd_request,
2033 empty,
2034 (done_f) empty
2035 };
2036
2037 static int wait_til_done(void (*handler)(void), int interruptible)
2038 {
2039 int ret;
2040
2041 schedule_bh((void *)(void *)handler);
2042
2043 if (command_status < 2 && NO_SIGNAL) {
2044 DECLARE_WAITQUEUE(wait, current);
2045
2046 add_wait_queue(&command_done, &wait);
2047 for (;;) {
2048 set_current_state(interruptible?
2049 TASK_INTERRUPTIBLE:
2050 TASK_UNINTERRUPTIBLE);
2051
2052 if (command_status >= 2 || !NO_SIGNAL)
2053 break;
2054
2055 is_alive("wait_til_done");
2056
2057 schedule();
2058 }
2059
2060 set_current_state(TASK_RUNNING);
2061 remove_wait_queue(&command_done, &wait);
2062 }
2063
2064 if (command_status < 2){
2065 cancel_activity();
2066 cont = &intr_cont;
2067 reset_fdc();
2068 return -EINTR;
2069 }
2070
2071 if (FDCS->reset)
2072 command_status = FD_COMMAND_ERROR;
2073 if (command_status == FD_COMMAND_OKAY)
2074 ret=0;
2075 else
2076 ret=-EIO;
2077 command_status = FD_COMMAND_NONE;
2078 return ret;
2079 }
2080
2081 static void generic_done(int result)
2082 {
2083 command_status = result;
2084 cont = &wakeup_cont;
2085 }
2086
2087 static void generic_success(void)
2088 {
2089 cont->done(1);
2090 }
2091
2092 static void generic_failure(void)
2093 {
2094 cont->done(0);
2095 }
2096
2097 static void success_and_wakeup(void)
2098 {
2099 generic_success();
2100 cont->redo();
2101 }
2102
2103
2104 /*
2105 * formatting and rw support.
2106 * ==========================
2107 */
2108
2109 static int next_valid_format(void)
2110 {
2111 int probed_format;
2112
2113 probed_format = DRS->probed_format;
2114 while(1){
2115 if (probed_format >= 8 ||
2116 !DP->autodetect[probed_format]){
2117 DRS->probed_format = 0;
2118 return 1;
2119 }
2120 if (floppy_type[DP->autodetect[probed_format]].sect){
2121 DRS->probed_format = probed_format;
2122 return 0;
2123 }
2124 probed_format++;
2125 }
2126 }
2127
2128 static void bad_flp_intr(void)
2129 {
2130 if (probing){
2131 DRS->probed_format++;
2132 if (!next_valid_format())
2133 return;
2134 }
2135 (*errors)++;
2136 INFBOUND(DRWE->badness, *errors);
2137 if (*errors > DP->max_errors.abort)
2138 cont->done(0);
2139 if (*errors > DP->max_errors.reset)
2140 FDCS->reset = 1;
2141 else if (*errors > DP->max_errors.recal)
2142 DRS->track = NEED_2_RECAL;
2143 }
2144
2145 static void set_floppy(kdev_t device)
2146 {
2147 if (TYPE(device))
2148 _floppy = TYPE(device) + floppy_type;
2149 else
2150 _floppy = current_type[ DRIVE(device) ];
2151 }
2152
2153 /*
2154 * formatting support.
2155 * ===================
2156 */
2157 static void format_interrupt(void)
2158 {
2159 switch (interpret_errors()){
2160 case 1:
2161 cont->error();
2162 case 2:
2163 break;
2164 case 0:
2165 cont->done(1);
2166 }
2167 cont->redo();
2168 }
2169
2170 #define CODE2SIZE (ssize = ((1 << SIZECODE) + 3) >> 2)
2171 #define FM_MODE(x,y) ((y) & ~(((x)->rate & 0x80) >>1))
2172 #define CT(x) ((x) | 0xc0)
2173 static void setup_format_params(int track)
2174 {
2175 struct fparm {
2176 unsigned char track,head,sect,size;
2177 } *here = (struct fparm *)floppy_track_buffer;
2178 int il,n;
2179 int count,head_shift,track_shift;
2180
2181 raw_cmd = &default_raw_cmd;
2182 raw_cmd->track = track;
2183
2184 raw_cmd->flags = FD_RAW_WRITE | FD_RAW_INTR | FD_RAW_SPIN |
2185 FD_RAW_NEED_DISK | FD_RAW_NEED_SEEK;
2186 raw_cmd->rate = _floppy->rate & 0x43;
2187 raw_cmd->cmd_count = NR_F;
2188 COMMAND = FM_MODE(_floppy,FD_FORMAT);
2189 DR_SELECT = UNIT(current_drive) + PH_HEAD(_floppy,format_req.head);
2190 F_SIZECODE = FD_SIZECODE(_floppy);
2191 F_SECT_PER_TRACK = _floppy->sect << 2 >> F_SIZECODE;
2192 F_GAP = _floppy->fmt_gap;
2193 F_FILL = FD_FILL_BYTE;
2194
2195 raw_cmd->kernel_data = floppy_track_buffer;
2196 raw_cmd->length = 4 * F_SECT_PER_TRACK;
2197
2198 /* allow for about 30ms for data transport per track */
2199 head_shift = (F_SECT_PER_TRACK + 5) / 6;
2200
2201 /* a ``cylinder'' is two tracks plus a little stepping time */
2202 track_shift = 2 * head_shift + 3;
2203
2204 /* position of logical sector 1 on this track */
2205 n = (track_shift * format_req.track + head_shift * format_req.head)
2206 % F_SECT_PER_TRACK;
2207
2208 /* determine interleave */
2209 il = 1;
2210 if (_floppy->fmt_gap < 0x22)
2211 il++;
2212
2213 /* initialize field */
2214 for (count = 0; count < F_SECT_PER_TRACK; ++count) {
2215 here[count].track = format_req.track;
2216 here[count].head = format_req.head;
2217 here[count].sect = 0;
2218 here[count].size = F_SIZECODE;
2219 }
2220 /* place logical sectors */
2221 for (count = 1; count <= F_SECT_PER_TRACK; ++count) {
2222 here[n].sect = count;
2223 n = (n+il) % F_SECT_PER_TRACK;
2224 if (here[n].sect) { /* sector busy, find next free sector */
2225 ++n;
2226 if (n>= F_SECT_PER_TRACK) {
2227 n-=F_SECT_PER_TRACK;
2228 while (here[n].sect) ++n;
2229 }
2230 }
2231 }
2232 }
2233
2234 static void redo_format(void)
2235 {
2236 buffer_track = -1;
2237 setup_format_params(format_req.track << STRETCH(_floppy));
2238 floppy_start();
2239 #ifdef DEBUGT
2240 debugt("queue format request");
2241 #endif
2242 }
2243
2244 static struct cont_t format_cont={
2245 format_interrupt,
2246 redo_format,
2247 bad_flp_intr,
2248 generic_done };
2249
2250 static int do_format(kdev_t device, struct format_descr *tmp_format_req)
2251 {
2252 int ret;
2253 int drive=DRIVE(device);
2254
2255 LOCK_FDC(drive,1);
2256 set_floppy(device);
2257 if (!_floppy ||
2258 _floppy->track > DP->tracks ||
2259 tmp_format_req->track >= _floppy->track ||
2260 tmp_format_req->head >= _floppy->head ||
2261 (_floppy->sect << 2) % (1 << FD_SIZECODE(_floppy)) ||
2262 !_floppy->fmt_gap) {
2263 process_fd_request();
2264 return -EINVAL;
2265 }
2266 format_req = *tmp_format_req;
2267 format_errors = 0;
2268 cont = &format_cont;
2269 errors = &format_errors;
2270 IWAIT(redo_format);
2271 process_fd_request();
2272 return ret;
2273 }
2274
2275 /*
2276 * Buffer read/write and support
2277 * =============================
2278 */
2279
2280 /* new request_done. Can handle physical sectors which are smaller than a
2281 * logical buffer */
2282 static void request_done(int uptodate)
2283 {
2284 int block;
2285 unsigned long flags;
2286
2287 probing = 0;
2288 reschedule_timeout(MAXTIMEOUT, "request done %d", uptodate);
2289
2290 if (QUEUE_EMPTY){
2291 DPRINT("request list destroyed in floppy request done\n");
2292 return;
2293 }
2294
2295 if (uptodate){
2296 /* maintain values for invalidation on geometry
2297 * change */
2298 block = current_count_sectors + CURRENT->sector;
2299 INFBOUND(DRS->maxblock, block);
2300 if (block > _floppy->sect)
2301 DRS->maxtrack = 1;
2302
2303 /* unlock chained buffers */
2304 spin_lock_irqsave(&io_request_lock, flags);
2305 while (current_count_sectors && !QUEUE_EMPTY &&
2306 current_count_sectors >= CURRENT->current_nr_sectors){
2307 current_count_sectors -= CURRENT->current_nr_sectors;
2308 CURRENT->nr_sectors -= CURRENT->current_nr_sectors;
2309 CURRENT->sector += CURRENT->current_nr_sectors;
2310 end_request(1);
2311 }
2312 spin_unlock_irqrestore(&io_request_lock, flags);
2313
2314 if (current_count_sectors && !QUEUE_EMPTY){
2315 /* "unlock" last subsector */
2316 CURRENT->buffer += current_count_sectors <<9;
2317 CURRENT->current_nr_sectors -= current_count_sectors;
2318 CURRENT->nr_sectors -= current_count_sectors;
2319 CURRENT->sector += current_count_sectors;
2320 return;
2321 }
2322
2323 if (current_count_sectors && QUEUE_EMPTY)
2324 DPRINT("request list destroyed in floppy request done\n");
2325
2326 } else {
2327 if (CURRENT->cmd == WRITE) {
2328 /* record write error information */
2329 DRWE->write_errors++;
2330 if (DRWE->write_errors == 1) {
2331 DRWE->first_error_sector = CURRENT->sector;
2332 DRWE->first_error_generation = DRS->generation;
2333 }
2334 DRWE->last_error_sector = CURRENT->sector;
2335 DRWE->last_error_generation = DRS->generation;
2336 }
2337 spin_lock_irqsave(&io_request_lock, flags);
2338 end_request(0);
2339 spin_unlock_irqrestore(&io_request_lock, flags);
2340 }
2341 }
2342
2343 /* Interrupt handler evaluating the result of the r/w operation */
2344 static void rw_interrupt(void)
2345 {
2346 int nr_sectors, ssize, eoc, heads;
2347
2348 if (R_HEAD >= 2) {
2349 /* some Toshiba floppy controllers occasionnally seem to
2350 * return bogus interrupts after read/write operations, which
2351 * can be recognized by a bad head number (>= 2) */
2352 return;
2353 }
2354
2355 if (!DRS->first_read_date)
2356 DRS->first_read_date = jiffies;
2357
2358 nr_sectors = 0;
2359 CODE2SIZE;
2360
2361 if (ST1 & ST1_EOC)
2362 eoc = 1;
2363 else
2364 eoc = 0;
2365
2366 if (COMMAND & 0x80)
2367 heads = 2;
2368 else
2369 heads = 1;
2370
2371 nr_sectors = (((R_TRACK-TRACK) * heads +
2372 R_HEAD-HEAD) * SECT_PER_TRACK +
2373 R_SECTOR-SECTOR + eoc) << SIZECODE >> 2;
2374
2375 #ifdef FLOPPY_SANITY_CHECK
2376 if (nr_sectors / ssize >
2377 (in_sector_offset + current_count_sectors + ssize - 1) / ssize) {
2378 DPRINT("long rw: %x instead of %lx\n",
2379 nr_sectors, current_count_sectors);
2380 printk("rs=%d s=%d\n", R_SECTOR, SECTOR);
2381 printk("rh=%d h=%d\n", R_HEAD, HEAD);
2382 printk("rt=%d t=%d\n", R_TRACK, TRACK);
2383 printk("heads=%d eoc=%d\n", heads, eoc);
2384 printk("spt=%d st=%d ss=%d\n", SECT_PER_TRACK,
2385 sector_t, ssize);
2386 printk("in_sector_offset=%d\n", in_sector_offset);
2387 }
2388 #endif
2389
2390 nr_sectors -= in_sector_offset;
2391 INFBOUND(nr_sectors,0);
2392 SUPBOUND(current_count_sectors, nr_sectors);
2393
2394 switch (interpret_errors()){
2395 case 2:
2396 cont->redo();
2397 return;
2398 case 1:
2399 if (!current_count_sectors){
2400 cont->error();
2401 cont->redo();
2402 return;
2403 }
2404 break;
2405 case 0:
2406 if (!current_count_sectors){
2407 cont->redo();
2408 return;
2409 }
2410 current_type[current_drive] = _floppy;
2411 floppy_sizes[TOMINOR(current_drive) ]=
2412 (_floppy->size+1)>>1;
2413 break;
2414 }
2415
2416 if (probing) {
2417 if (DP->flags & FTD_MSG)
2418 DPRINT("Auto-detected floppy type %s in fd%d\n",
2419 _floppy->name,current_drive);
2420 current_type[current_drive] = _floppy;
2421 floppy_sizes[TOMINOR(current_drive)] = (_floppy->size+1) >> 1;
2422 probing = 0;
2423 }
2424
2425 if (CT(COMMAND) != FD_READ ||
2426 raw_cmd->kernel_data == CURRENT->buffer){
2427 /* transfer directly from buffer */
2428 cont->done(1);
2429 } else if (CT(COMMAND) == FD_READ){
2430 buffer_track = raw_cmd->track;
2431 buffer_drive = current_drive;
2432 INFBOUND(buffer_max, nr_sectors + sector_t);
2433 }
2434 cont->redo();
2435 }
2436
2437 /* Compute maximal contiguous buffer size. */
2438 static int buffer_chain_size(void)
2439 {
2440 struct buffer_head *bh;
2441 int size;
2442 char *base;
2443
2444 base = CURRENT->buffer;
2445 size = CURRENT->current_nr_sectors << 9;
2446 bh = CURRENT->bh;
2447
2448 if (bh){
2449 bh = bh->b_reqnext;
2450 while (bh && bh->b_data == base + size){
2451 size += bh->b_size;
2452 bh = bh->b_reqnext;
2453 }
2454 }
2455 return size >> 9;
2456 }
2457
2458 /* Compute the maximal transfer size */
2459 static int transfer_size(int ssize, int max_sector, int max_size)
2460 {
2461 SUPBOUND(max_sector, sector_t + max_size);
2462
2463 /* alignment */
2464 max_sector -= (max_sector % _floppy->sect) % ssize;
2465
2466 /* transfer size, beginning not aligned */
2467 current_count_sectors = max_sector - sector_t ;
2468
2469 return max_sector;
2470 }
2471
2472 /*
2473 * Move data from/to the track buffer to/from the buffer cache.
2474 */
2475 static void copy_buffer(int ssize, int max_sector, int max_sector_2)
2476 {
2477 int remaining; /* number of transferred 512-byte sectors */
2478 struct buffer_head *bh;
2479 char *buffer, *dma_buffer;
2480 int size;
2481
2482 max_sector = transfer_size(ssize,
2483 minimum(max_sector, max_sector_2),
2484 CURRENT->nr_sectors);
2485
2486 if (current_count_sectors <= 0 && CT(COMMAND) == FD_WRITE &&
2487 buffer_max > sector_t + CURRENT->nr_sectors)
2488 current_count_sectors = minimum(buffer_max - sector_t,
2489 CURRENT->nr_sectors);
2490
2491 remaining = current_count_sectors << 9;
2492 #ifdef FLOPPY_SANITY_CHECK
2493 if ((remaining >> 9) > CURRENT->nr_sectors &&
2494 CT(COMMAND) == FD_WRITE){
2495 DPRINT("in copy buffer\n");
2496 printk("current_count_sectors=%ld\n", current_count_sectors);
2497 printk("remaining=%d\n", remaining >> 9);
2498 printk("CURRENT->nr_sectors=%ld\n",CURRENT->nr_sectors);
2499 printk("CURRENT->current_nr_sectors=%ld\n",
2500 CURRENT->current_nr_sectors);
2501 printk("max_sector=%d\n", max_sector);
2502 printk("ssize=%d\n", ssize);
2503 }
2504 #endif
2505
2506 buffer_max = maximum(max_sector, buffer_max);
2507
2508 dma_buffer = floppy_track_buffer + ((sector_t - buffer_min) << 9);
2509
2510 bh = CURRENT->bh;
2511 size = CURRENT->current_nr_sectors << 9;
2512 buffer = CURRENT->buffer;
2513
2514 while (remaining > 0){
2515 SUPBOUND(size, remaining);
2516 #ifdef FLOPPY_SANITY_CHECK
2517 if (dma_buffer + size >
2518 floppy_track_buffer + (max_buffer_sectors << 10) ||
2519 dma_buffer < floppy_track_buffer){
2520 DPRINT("buffer overrun in copy buffer %d\n",
2521 (int) ((floppy_track_buffer - dma_buffer) >>9));
2522 printk("sector_t=%d buffer_min=%d\n",
2523 sector_t, buffer_min);
2524 printk("current_count_sectors=%ld\n",
2525 current_count_sectors);
2526 if (CT(COMMAND) == FD_READ)
2527 printk("read\n");
2528 if (CT(COMMAND) == FD_READ)
2529 printk("write\n");
2530 break;
2531 }
2532 if (((unsigned long)buffer) % 512)
2533 DPRINT("%p buffer not aligned\n", buffer);
2534 #endif
2535 if (CT(COMMAND) == FD_READ)
2536 memcpy(buffer, dma_buffer, size);
2537 else
2538 memcpy(dma_buffer, buffer, size);
2539 remaining -= size;
2540 if (!remaining)
2541 break;
2542
2543 dma_buffer += size;
2544 bh = bh->b_reqnext;
2545 #ifdef FLOPPY_SANITY_CHECK
2546 if (!bh){
2547 DPRINT("bh=null in copy buffer after copy\n");
2548 break;
2549 }
2550 #endif
2551 size = bh->b_size;
2552 buffer = bh->b_data;
2553 }
2554 #ifdef FLOPPY_SANITY_CHECK
2555 if (remaining){
2556 if (remaining > 0)
2557 max_sector -= remaining >> 9;
2558 DPRINT("weirdness: remaining %d\n", remaining>>9);
2559 }
2560 #endif
2561 }
2562
2563 #if 0
2564 static inline int check_dma_crossing(char *start,
2565 unsigned long length, char *message)
2566 {
2567 if (CROSS_64KB(start, length)) {
2568 printk("DMA xfer crosses 64KB boundary in %s %p-%p\n",
2569 message, start, start+length);
2570 return 1;
2571 } else
2572 return 0;
2573 }
2574 #endif
2575
2576 /* work around a bug in pseudo DMA
2577 * (on some FDCs) pseudo DMA does not stop when the CPU stops
2578 * sending data. Hence we need a different way to signal the
2579 * transfer length: We use SECT_PER_TRACK. Unfortunately, this
2580 * does not work with MT, hence we can only transfer one head at
2581 * a time
2582 */
2583 static void virtualdmabug_workaround(void)
2584 {
2585 int hard_sectors, end_sector;
2586
2587 if(CT(COMMAND) == FD_WRITE) {
2588 COMMAND &= ~0x80; /* switch off multiple track mode */
2589
2590 hard_sectors = raw_cmd->length >> (7 + SIZECODE);
2591 end_sector = SECTOR + hard_sectors - 1;
2592 #ifdef FLOPPY_SANITY_CHECK
2593 if(end_sector > SECT_PER_TRACK) {
2594 printk("too many sectors %d > %d\n",
2595 end_sector, SECT_PER_TRACK);
2596 return;
2597 }
2598 #endif
2599 SECT_PER_TRACK = end_sector; /* make sure SECT_PER_TRACK points
2600 * to end of transfer */
2601 }
2602 }
2603
2604 /*
2605 * Formulate a read/write request.
2606 * this routine decides where to load the data (directly to buffer, or to
2607 * tmp floppy area), how much data to load (the size of the buffer, the whole
2608 * track, or a single sector)
2609 * All floppy_track_buffer handling goes in here. If we ever add track buffer
2610 * allocation on the fly, it should be done here. No other part should need
2611 * modification.
2612 */
2613
2614 static int make_raw_rw_request(void)
2615 {
2616 int aligned_sector_t;
2617 int max_sector, max_size, tracksize, ssize;
2618
2619 if(max_buffer_sectors == 0) {
2620 printk("VFS: Block I/O scheduled on unopened device\n");
2621 return 0;
2622 }
2623
2624 set_fdc(DRIVE(CURRENT->rq_dev));
2625
2626 raw_cmd = &default_raw_cmd;
2627 raw_cmd->flags = FD_RAW_SPIN | FD_RAW_NEED_DISK | FD_RAW_NEED_DISK |
2628 FD_RAW_NEED_SEEK;
2629 raw_cmd->cmd_count = NR_RW;
2630 if (CURRENT->cmd == READ){
2631 raw_cmd->flags |= FD_RAW_READ;
2632 COMMAND = FM_MODE(_floppy,FD_READ);
2633 } else if (CURRENT->cmd == WRITE){
2634 raw_cmd->flags |= FD_RAW_WRITE;
2635 COMMAND = FM_MODE(_floppy,FD_WRITE);
2636 } else {
2637 DPRINT("make_raw_rw_request: unknown command\n");
2638 return 0;
2639 }
2640
2641 max_sector = _floppy->sect * _floppy->head;
2642
2643 TRACK = CURRENT->sector / max_sector;
2644 sector_t = CURRENT->sector % max_sector;
2645 if (_floppy->track && TRACK >= _floppy->track) {
2646 if (CURRENT->current_nr_sectors & 1) {
2647 current_count_sectors = 1;
2648 return 1;
2649 } else
2650 return 0;
2651 }
2652 HEAD = sector_t / _floppy->sect;
2653
2654 if (((_floppy->stretch & FD_SWAPSIDES) || TESTF(FD_NEED_TWADDLE)) &&
2655 sector_t < _floppy->sect)
2656 max_sector = _floppy->sect;
2657
2658 /* 2M disks have phantom sectors on the first track */
2659 if ((_floppy->rate & FD_2M) && (!TRACK) && (!HEAD)){
2660 max_sector = 2 * _floppy->sect / 3;
2661 if (sector_t >= max_sector){
2662 current_count_sectors = minimum(_floppy->sect - sector_t,
2663 CURRENT->nr_sectors);
2664 return 1;
2665 }
2666 SIZECODE = 2;
2667 } else
2668 SIZECODE = FD_SIZECODE(_floppy);
2669 raw_cmd->rate = _floppy->rate & 0x43;
2670 if ((_floppy->rate & FD_2M) &&
2671 (TRACK || HEAD) &&
2672 raw_cmd->rate == 2)
2673 raw_cmd->rate = 1;
2674
2675 if (SIZECODE)
2676 SIZECODE2 = 0xff;
2677 else
2678 SIZECODE2 = 0x80;
2679 raw_cmd->track = TRACK << STRETCH(_floppy);
2680 DR_SELECT = UNIT(current_drive) + PH_HEAD(_floppy,HEAD);
2681 GAP = _floppy->gap;
2682 CODE2SIZE;
2683 SECT_PER_TRACK = _floppy->sect << 2 >> SIZECODE;
2684 SECTOR = ((sector_t % _floppy->sect) << 2 >> SIZECODE) + 1;
2685
2686 /* tracksize describes the size which can be filled up with sectors
2687 * of size ssize.
2688 */
2689 tracksize = _floppy->sect - _floppy->sect % ssize;
2690 if (tracksize < _floppy->sect){
2691 SECT_PER_TRACK ++;
2692 if (tracksize <= sector_t % _floppy->sect)
2693 SECTOR--;
2694
2695 /* if we are beyond tracksize, fill up using smaller sectors */
2696 while (tracksize <= sector_t % _floppy->sect){
2697 while(tracksize + ssize > _floppy->sect){
2698 SIZECODE--;
2699 ssize >>= 1;
2700 }
2701 SECTOR++; SECT_PER_TRACK ++;
2702 tracksize += ssize;
2703 }
2704 max_sector = HEAD * _floppy->sect + tracksize;
2705 } else if (!TRACK && !HEAD && !(_floppy->rate & FD_2M) && probing) {
2706 max_sector = _floppy->sect;
2707 } else if (!HEAD && CT(COMMAND) == FD_WRITE) {
2708 /* for virtual DMA bug workaround */
2709 max_sector = _floppy->sect;
2710 }
2711
2712 in_sector_offset = (sector_t % _floppy->sect) % ssize;
2713 aligned_sector_t = sector_t - in_sector_offset;
2714 max_size = CURRENT->nr_sectors;
2715 if ((raw_cmd->track == buffer_track) &&
2716 (current_drive == buffer_drive) &&
2717 (sector_t >= buffer_min) && (sector_t < buffer_max)) {
2718 /* data already in track buffer */
2719 if (CT(COMMAND) == FD_READ) {
2720 copy_buffer(1, max_sector, buffer_max);
2721 return 1;
2722 }
2723 } else if (in_sector_offset || CURRENT->nr_sectors < ssize){
2724 if (CT(COMMAND) == FD_WRITE){
2725 if (sector_t + CURRENT->nr_sectors > ssize &&
2726 sector_t + CURRENT->nr_sectors < ssize + ssize)
2727 max_size = ssize + ssize;
2728 else
2729 max_size = ssize;
2730 }
2731 raw_cmd->flags &= ~FD_RAW_WRITE;
2732 raw_cmd->flags |= FD_RAW_READ;
2733 COMMAND = FM_MODE(_floppy,FD_READ);
2734 } else if ((unsigned long)CURRENT->buffer < MAX_DMA_ADDRESS) {
2735 unsigned long dma_limit;
2736 int direct, indirect;
2737
2738 indirect= transfer_size(ssize,max_sector,max_buffer_sectors*2) -
2739 sector_t;
2740
2741 /*
2742 * Do NOT use minimum() here---MAX_DMA_ADDRESS is 64 bits wide
2743 * on a 64 bit machine!
2744 */
2745 max_size = buffer_chain_size();
2746 dma_limit = (MAX_DMA_ADDRESS - ((unsigned long) CURRENT->buffer)) >> 9;
2747 if ((unsigned long) max_size > dma_limit) {
2748 max_size = dma_limit;
2749 }
2750 /* 64 kb boundaries */
2751 if (CROSS_64KB(CURRENT->buffer, max_size << 9))
2752 max_size = (K_64 -
2753 ((unsigned long)CURRENT->buffer) % K_64)>>9;
2754 direct = transfer_size(ssize,max_sector,max_size) - sector_t;
2755 /*
2756 * We try to read tracks, but if we get too many errors, we
2757 * go back to reading just one sector at a time.
2758 *
2759 * This means we should be able to read a sector even if there
2760 * are other bad sectors on this track.
2761 */
2762 if (!direct ||
2763 (indirect * 2 > direct * 3 &&
2764 *errors < DP->max_errors.read_track &&
2765 /*!TESTF(FD_NEED_TWADDLE) &&*/
2766 ((!probing || (DP->read_track&(1<<DRS->probed_format)))))){
2767 max_size = CURRENT->nr_sectors;
2768 } else {
2769 raw_cmd->kernel_data = CURRENT->buffer;
2770 raw_cmd->length = current_count_sectors << 9;
2771 if (raw_cmd->length == 0){
2772 DPRINT("zero dma transfer attempted from make_raw_request\n");
2773 DPRINT("indirect=%d direct=%d sector_t=%d",
2774 indirect, direct, sector_t);
2775 return 0;
2776 }
2777 /* check_dma_crossing(raw_cmd->kernel_data,
2778 raw_cmd->length,
2779 "end of make_raw_request [1]");*/
2780
2781 virtualdmabug_workaround();
2782 return 2;
2783 }
2784 }
2785
2786 if (CT(COMMAND) == FD_READ)
2787 max_size = max_sector; /* unbounded */
2788
2789 /* claim buffer track if needed */
2790 if (buffer_track != raw_cmd->track || /* bad track */
2791 buffer_drive !=current_drive || /* bad drive */
2792 sector_t > buffer_max ||
2793 sector_t < buffer_min ||
2794 ((CT(COMMAND) == FD_READ ||
2795 (!in_sector_offset && CURRENT->nr_sectors >= ssize))&&
2796 max_sector > 2 * max_buffer_sectors + buffer_min &&
2797 max_size + sector_t > 2 * max_buffer_sectors + buffer_min)
2798 /* not enough space */){
2799 buffer_track = -1;
2800 buffer_drive = current_drive;
2801 buffer_max = buffer_min = aligned_sector_t;
2802 }
2803 raw_cmd->kernel_data = floppy_track_buffer +
2804 ((aligned_sector_t-buffer_min)<<9);
2805
2806 if (CT(COMMAND) == FD_WRITE){
2807 /* copy write buffer to track buffer.
2808 * if we get here, we know that the write
2809 * is either aligned or the data already in the buffer
2810 * (buffer will be overwritten) */
2811 #ifdef FLOPPY_SANITY_CHECK
2812 if (in_sector_offset && buffer_track == -1)
2813 DPRINT("internal error offset !=0 on write\n");
2814 #endif
2815 buffer_track = raw_cmd->track;
2816 buffer_drive = current_drive;
2817 copy_buffer(ssize, max_sector, 2*max_buffer_sectors+buffer_min);
2818 } else
2819 transfer_size(ssize, max_sector,
2820 2*max_buffer_sectors+buffer_min-aligned_sector_t);
2821
2822 /* round up current_count_sectors to get dma xfer size */
2823 raw_cmd->length = in_sector_offset+current_count_sectors;
2824 raw_cmd->length = ((raw_cmd->length -1)|(ssize-1))+1;
2825 raw_cmd->length <<= 9;
2826 #ifdef FLOPPY_SANITY_CHECK
2827 /*check_dma_crossing(raw_cmd->kernel_data, raw_cmd->length,
2828 "end of make_raw_request");*/
2829 if ((raw_cmd->length < current_count_sectors << 9) ||
2830 (raw_cmd->kernel_data != CURRENT->buffer &&
2831 CT(COMMAND) == FD_WRITE &&
2832 (aligned_sector_t + (raw_cmd->length >> 9) > buffer_max ||
2833 aligned_sector_t < buffer_min)) ||
2834 raw_cmd->length % (128 << SIZECODE) ||
2835 raw_cmd->length <= 0 || current_count_sectors <= 0){
2836 DPRINT("fractionary current count b=%lx s=%lx\n",
2837 raw_cmd->length, current_count_sectors);
2838 if (raw_cmd->kernel_data != CURRENT->buffer)
2839 printk("addr=%d, length=%ld\n",
2840 (int) ((raw_cmd->kernel_data -
2841 floppy_track_buffer) >> 9),
2842 current_count_sectors);
2843 printk("st=%d ast=%d mse=%d msi=%d\n",
2844 sector_t, aligned_sector_t, max_sector, max_size);
2845 printk("ssize=%x SIZECODE=%d\n", ssize, SIZECODE);
2846 printk("command=%x SECTOR=%d HEAD=%d, TRACK=%d\n",
2847 COMMAND, SECTOR, HEAD, TRACK);
2848 printk("buffer drive=%d\n", buffer_drive);
2849 printk("buffer track=%d\n", buffer_track);
2850 printk("buffer_min=%d\n", buffer_min);
2851 printk("buffer_max=%d\n", buffer_max);
2852 return 0;
2853 }
2854
2855 if (raw_cmd->kernel_data != CURRENT->buffer){
2856 if (raw_cmd->kernel_data < floppy_track_buffer ||
2857 current_count_sectors < 0 ||
2858 raw_cmd->length < 0 ||
2859 raw_cmd->kernel_data + raw_cmd->length >
2860 floppy_track_buffer + (max_buffer_sectors << 10)){
2861 DPRINT("buffer overrun in schedule dma\n");
2862 printk("sector_t=%d buffer_min=%d current_count=%ld\n",
2863 sector_t, buffer_min,
2864 raw_cmd->length >> 9);
2865 printk("current_count_sectors=%ld\n",
2866 current_count_sectors);
2867 if (CT(COMMAND) == FD_READ)
2868 printk("read\n");
2869 if (CT(COMMAND) == FD_READ)
2870 printk("write\n");
2871 return 0;
2872 }
2873 } else if (raw_cmd->length > CURRENT->nr_sectors << 9 ||
2874 current_count_sectors > CURRENT->nr_sectors){
2875 DPRINT("buffer overrun in direct transfer\n");
2876 return 0;
2877 } else if (raw_cmd->length < current_count_sectors << 9){
2878 DPRINT("more sectors than bytes\n");
2879 printk("bytes=%ld\n", raw_cmd->length >> 9);
2880 printk("sectors=%ld\n", current_count_sectors);
2881 }
2882 if (raw_cmd->length == 0){
2883 DPRINT("zero dma transfer attempted from make_raw_request\n");
2884 return 0;
2885 }
2886 #endif
2887
2888 virtualdmabug_workaround();
2889 return 2;
2890 }
2891
2892 static void redo_fd_request(void)
2893 {
2894 #define REPEAT {request_done(0); continue; }
2895 kdev_t device;
2896 int tmp;
2897
2898 lastredo = jiffies;
2899 if (current_drive < N_DRIVE)
2900 floppy_off(current_drive);
2901
2902 if (!QUEUE_EMPTY && CURRENT->rq_status == RQ_INACTIVE){
2903 CLEAR_INTR;
2904 unlock_fdc();
2905 return;
2906 }
2907
2908 while(1){
2909 if (QUEUE_EMPTY) {
2910 CLEAR_INTR;
2911 unlock_fdc();
2912 return;
2913 }
2914 if (MAJOR(CURRENT->rq_dev) != MAJOR_NR)
2915 panic(DEVICE_NAME ": request list destroyed");
2916 if (CURRENT->bh && !buffer_locked(CURRENT->bh))
2917 panic(DEVICE_NAME ": block not locked");
2918
2919 device = CURRENT->rq_dev;
2920 set_fdc(DRIVE(device));
2921 reschedule_timeout(CURRENTD, "redo fd request", 0);
2922
2923 set_floppy(device);
2924 raw_cmd = & default_raw_cmd;
2925 raw_cmd->flags = 0;
2926 if (start_motor(redo_fd_request)) return;
2927 disk_change(current_drive);
2928 if (test_bit(current_drive, &fake_change) ||
2929 TESTF(FD_DISK_CHANGED)){
2930 DPRINT("disk absent or changed during operation\n");
2931 REPEAT;
2932 }
2933 if (!_floppy) { /* Autodetection */
2934 if (!probing){
2935 DRS->probed_format = 0;
2936 if (next_valid_format()){
2937 DPRINT("no autodetectable formats\n");
2938 _floppy = NULL;
2939 REPEAT;
2940 }
2941 }
2942 probing = 1;
2943 _floppy = floppy_type+DP->autodetect[DRS->probed_format];
2944 } else
2945 probing = 0;
2946 errors = & (CURRENT->errors);
2947 tmp = make_raw_rw_request();
2948 if (tmp < 2){
2949 request_done(tmp);
2950 continue;
2951 }
2952
2953 if (TESTF(FD_NEED_TWADDLE))
2954 twaddle();
2955 schedule_bh( (void *)(void *) floppy_start);
2956 #ifdef DEBUGT
2957 debugt("queue fd request");
2958 #endif
2959 return;
2960 }
2961 #undef REPEAT
2962 }
2963
2964 static struct cont_t rw_cont={
2965 rw_interrupt,
2966 redo_fd_request,
2967 bad_flp_intr,
2968 request_done };
2969
2970 static void process_fd_request(void)
2971 {
2972 cont = &rw_cont;
2973 schedule_bh( (void *)(void *) redo_fd_request);
2974 }
2975
2976 static void do_fd_request(request_queue_t * q)
2977 {
2978 if(max_buffer_sectors == 0) {
2979 printk("VFS: do_fd_request called on non-open device\n");
2980 return;
2981 }
2982
2983 if (usage_count == 0) {
2984 printk("warning: usage count=0, CURRENT=%p exiting\n", CURRENT);
2985 printk("sect=%ld cmd=%d\n", CURRENT->sector, CURRENT->cmd);
2986 return;
2987 }
2988 if (fdc_busy){
2989 /* fdc busy, this new request will be treated when the
2990 current one is done */
2991 is_alive("do fd request, old request running");
2992 return;
2993 }
2994 lock_fdc(MAXTIMEOUT,0);
2995 process_fd_request();
2996 is_alive("do fd request");
2997 }
2998
2999 static struct cont_t poll_cont={
3000 success_and_wakeup,
3001 floppy_ready,
3002 generic_failure,
3003 generic_done };
3004
3005 static int poll_drive(int interruptible, int flag)
3006 {
3007 int ret;
3008 /* no auto-sense, just clear dcl */
3009 raw_cmd = &default_raw_cmd;
3010 raw_cmd->flags= flag;
3011 raw_cmd->track=0;
3012 raw_cmd->cmd_count=0;
3013 cont = &poll_cont;
3014 #ifdef DCL_DEBUG
3015 if (DP->flags & FD_DEBUG){
3016 DPRINT("setting NEWCHANGE in poll_drive\n");
3017 }
3018 #endif
3019 SETF(FD_DISK_NEWCHANGE);
3020 WAIT(floppy_ready);
3021 return ret;
3022 }
3023
3024 /*
3025 * User triggered reset
3026 * ====================
3027 */
3028
3029 static void reset_intr(void)
3030 {
3031 printk("weird, reset interrupt called\n");
3032 }
3033
3034 static struct cont_t reset_cont={
3035 reset_intr,
3036 success_and_wakeup,
3037 generic_failure,
3038 generic_done };
3039
3040 static int user_reset_fdc(int drive, int arg, int interruptible)
3041 {
3042 int ret;
3043
3044 ret=0;
3045 LOCK_FDC(drive,interruptible);
3046 if (arg == FD_RESET_ALWAYS)
3047 FDCS->reset=1;
3048 if (FDCS->reset){
3049 cont = &reset_cont;
3050 WAIT(reset_fdc);
3051 }
3052 process_fd_request();
3053 return ret;
3054 }
3055
3056 /*
3057 * Misc Ioctl's and support
3058 * ========================
3059 */
3060 static inline int fd_copyout(void *param, const void *address, unsigned long size)
3061 {
3062 return copy_to_user(param,address, size) ? -EFAULT : 0;
3063 }
3064
3065 static inline int fd_copyin(void *param, void *address, unsigned long size)
3066 {
3067 return copy_from_user(address, param, size) ? -EFAULT : 0;
3068 }
3069
3070 #define _COPYOUT(x) (copy_to_user((void *)param, &(x), sizeof(x)) ? -EFAULT : 0)
3071 #define _COPYIN(x) (copy_from_user(&(x), (void *)param, sizeof(x)) ? -EFAULT : 0)
3072
3073 #define COPYOUT(x) ECALL(_COPYOUT(x))
3074 #define COPYIN(x) ECALL(_COPYIN(x))
3075
3076 static inline const char *drive_name(int type, int drive)
3077 {
3078 struct floppy_struct *floppy;
3079
3080 if (type)
3081 floppy = floppy_type + type;
3082 else {
3083 if (UDP->native_format)
3084 floppy = floppy_type + UDP->native_format;
3085 else
3086 return "(null)";
3087 }
3088 if (floppy->name)
3089 return floppy->name;
3090 else
3091 return "(null)";
3092 }
3093
3094
3095 /* raw commands */
3096 static void raw_cmd_done(int flag)
3097 {
3098 int i;
3099
3100 if (!flag) {
3101 raw_cmd->flags |= FD_RAW_FAILURE;
3102 raw_cmd->flags |= FD_RAW_HARDFAILURE;
3103 } else {
3104 raw_cmd->reply_count = inr;
3105 if (raw_cmd->reply_count > MAX_REPLIES)
3106 raw_cmd->reply_count=0;
3107 for (i=0; i< raw_cmd->reply_count; i++)
3108 raw_cmd->reply[i] = reply_buffer[i];
3109
3110 if (raw_cmd->flags & (FD_RAW_READ | FD_RAW_WRITE))
3111 {
3112 unsigned long flags;
3113 flags=claim_dma_lock();
3114 raw_cmd->length = fd_get_dma_residue();
3115 release_dma_lock(flags);
3116 }
3117
3118 if ((raw_cmd->flags & FD_RAW_SOFTFAILURE) &&
3119 (!raw_cmd->reply_count || (raw_cmd->reply[0] & 0xc0)))
3120 raw_cmd->flags |= FD_RAW_FAILURE;
3121
3122 if (disk_change(current_drive))
3123 raw_cmd->flags |= FD_RAW_DISK_CHANGE;
3124 else
3125 raw_cmd->flags &= ~FD_RAW_DISK_CHANGE;
3126 if (raw_cmd->flags & FD_RAW_NO_MOTOR_AFTER)
3127 motor_off_callback(current_drive);
3128
3129 if (raw_cmd->next &&
3130 (!(raw_cmd->flags & FD_RAW_FAILURE) ||
3131 !(raw_cmd->flags & FD_RAW_STOP_IF_FAILURE)) &&
3132 ((raw_cmd->flags & FD_RAW_FAILURE) ||
3133 !(raw_cmd->flags &FD_RAW_STOP_IF_SUCCESS))) {
3134 raw_cmd = raw_cmd->next;
3135 return;
3136 }
3137 }
3138 generic_done(flag);
3139 }
3140
3141
3142 static struct cont_t raw_cmd_cont={
3143 success_and_wakeup,
3144 floppy_start,
3145 generic_failure,
3146 raw_cmd_done
3147 };
3148
3149 static inline int raw_cmd_copyout(int cmd, char *param,
3150 struct floppy_raw_cmd *ptr)
3151 {
3152 int ret;
3153
3154 while(ptr) {
3155 COPYOUT(*ptr);
3156 param += sizeof(struct floppy_raw_cmd);
3157 if ((ptr->flags & FD_RAW_READ) && ptr->buffer_length){
3158 if (ptr->length>=0 && ptr->length<=ptr->buffer_length)
3159 ECALL(fd_copyout(ptr->data,
3160 ptr->kernel_data,
3161 ptr->buffer_length -
3162 ptr->length));
3163 }
3164 ptr = ptr->next;
3165 }
3166 return 0;
3167 }
3168
3169
3170 static void raw_cmd_free(struct floppy_raw_cmd **ptr)
3171 {
3172 struct floppy_raw_cmd *next,*this;
3173
3174 this = *ptr;
3175 *ptr = 0;
3176 while(this) {
3177 if (this->buffer_length) {
3178 fd_dma_mem_free((unsigned long)this->kernel_data,
3179 this->buffer_length);
3180 this->buffer_length = 0;
3181 }
3182 next = this->next;
3183 kfree(this);
3184 this = next;
3185 }
3186 }
3187
3188
3189 static inline int raw_cmd_copyin(int cmd, char *param,
3190 struct floppy_raw_cmd **rcmd)
3191 {
3192 struct floppy_raw_cmd *ptr;
3193 int ret;
3194 int i;
3195
3196 *rcmd = 0;
3197 while(1) {
3198 ptr = (struct floppy_raw_cmd *)
3199 kmalloc(sizeof(struct floppy_raw_cmd), GFP_USER);
3200 if (!ptr)
3201 return -ENOMEM;
3202 *rcmd = ptr;
3203 COPYIN(*ptr);
3204 ptr->next = 0;
3205 ptr->buffer_length = 0;
3206 param += sizeof(struct floppy_raw_cmd);
3207 if (ptr->cmd_count > 33)
3208 /* the command may now also take up the space
3209 * initially intended for the reply & the
3210 * reply count. Needed for long 82078 commands
3211 * such as RESTORE, which takes ... 17 command
3212 * bytes. Murphy's law #137: When you reserve
3213 * 16 bytes for a structure, you'll one day
3214 * discover that you really need 17...
3215 */
3216 return -EINVAL;
3217
3218 for (i=0; i< 16; i++)
3219 ptr->reply[i] = 0;
3220 ptr->resultcode = 0;
3221 ptr->kernel_data = 0;
3222
3223 if (ptr->flags & (FD_RAW_READ | FD_RAW_WRITE)) {
3224 if (ptr->length <= 0)
3225 return -EINVAL;
3226 ptr->kernel_data =(char*)fd_dma_mem_alloc(ptr->length);
3227 fallback_on_nodma_alloc(&ptr->kernel_data,
3228 ptr->length);
3229 if (!ptr->kernel_data)
3230 return -ENOMEM;
3231 ptr->buffer_length = ptr->length;
3232 }
3233 if (ptr->flags & FD_RAW_WRITE)
3234 ECALL(fd_copyin(ptr->data, ptr->kernel_data,
3235 ptr->length));
3236 rcmd = & (ptr->next);
3237 if (!(ptr->flags & FD_RAW_MORE))
3238 return 0;
3239 ptr->rate &= 0x43;
3240 }
3241 }
3242
3243
3244 static int raw_cmd_ioctl(int cmd, void *param)
3245 {
3246 int drive, ret, ret2;
3247 struct floppy_raw_cmd *my_raw_cmd;
3248
3249 if (FDCS->rawcmd <= 1)
3250 FDCS->rawcmd = 1;
3251 for (drive= 0; drive < N_DRIVE; drive++){
3252 if (FDC(drive) != fdc)
3253 continue;
3254 if (drive == current_drive){
3255 if (UDRS->fd_ref > 1){
3256 FDCS->rawcmd = 2;
3257 break;
3258 }
3259 } else if (UDRS->fd_ref){
3260 FDCS->rawcmd = 2;
3261 break;
3262 }
3263 }
3264
3265 if (FDCS->reset)
3266 return -EIO;
3267
3268 ret = raw_cmd_copyin(cmd, param, &my_raw_cmd);
3269 if (ret) {
3270 raw_cmd_free(&my_raw_cmd);
3271 return ret;
3272 }
3273
3274 raw_cmd = my_raw_cmd;
3275 cont = &raw_cmd_cont;
3276 ret=wait_til_done(floppy_start,1);
3277 #ifdef DCL_DEBUG
3278 if (DP->flags & FD_DEBUG){
3279 DPRINT("calling disk change from raw_cmd ioctl\n");
3280 }
3281 #endif
3282
3283 if (ret != -EINTR && FDCS->reset)
3284 ret = -EIO;
3285
3286 DRS->track = NO_TRACK;
3287
3288 ret2 = raw_cmd_copyout(cmd, param, my_raw_cmd);
3289 if (!ret)
3290 ret = ret2;
3291 raw_cmd_free(&my_raw_cmd);
3292 return ret;
3293 }
3294
3295 static int invalidate_drive(kdev_t rdev)
3296 {
3297 /* invalidate the buffer track to force a reread */
3298 set_bit(DRIVE(rdev), &fake_change);
3299 process_fd_request();
3300 check_disk_change(rdev);
3301 return 0;
3302 }
3303
3304
3305 static inline void clear_write_error(int drive)
3306 {
3307 CLEARSTRUCT(UDRWE);
3308 }
3309
3310 static inline int set_geometry(unsigned int cmd, struct floppy_struct *g,
3311 int drive, int type, kdev_t device)
3312 {
3313 int cnt;
3314
3315 /* sanity checking for parameters.*/
3316 if (g->sect <= 0 ||
3317 g->head <= 0 ||
3318 g->track <= 0 ||
3319 g->track > UDP->tracks>>STRETCH(g) ||
3320 /* check if reserved bits are set */
3321 (g->stretch&~(FD_STRETCH|FD_SWAPSIDES)) != 0)
3322 return -EINVAL;
3323 if (type){
3324 if (!capable(CAP_SYS_ADMIN))
3325 return -EPERM;
3326 LOCK_FDC(drive,1);
3327 for (cnt = 0; cnt < N_DRIVE; cnt++){
3328 if (ITYPE(drive_state[cnt].fd_device) == type &&
3329 drive_state[cnt].fd_ref)
3330 set_bit(drive, &fake_change);
3331 }
3332 floppy_type[type] = *g;
3333 floppy_type[type].name="user format";
3334 for (cnt = type << 2; cnt < (type << 2) + 4; cnt++)
3335 floppy_sizes[cnt]= floppy_sizes[cnt+0x80]=
3336 (floppy_type[type].size+1)>>1;
3337 process_fd_request();
3338 for (cnt = 0; cnt < N_DRIVE; cnt++){
3339 if (ITYPE(drive_state[cnt].fd_device) == type &&
3340 drive_state[cnt].fd_ref)
3341 check_disk_change(
3342 MKDEV(FLOPPY_MAJOR,
3343 drive_state[cnt].fd_device));
3344 }
3345 } else {
3346 LOCK_FDC(drive,1);
3347 if (cmd != FDDEFPRM)
3348 /* notice a disk change immediately, else
3349 * we lose our settings immediately*/
3350 CALL(poll_drive(1, FD_RAW_NEED_DISK));
3351 user_params[drive] = *g;
3352 if (buffer_drive == drive)
3353 SUPBOUND(buffer_max, user_params[drive].sect);
3354 current_type[drive] = &user_params[drive];
3355 floppy_sizes[drive] = (user_params[drive].size+1) >> 1;
3356 if (cmd == FDDEFPRM)
3357 DRS->keep_data = -1;
3358 else
3359 DRS->keep_data = 1;
3360 /* invalidation. Invalidate only when needed, i.e.
3361 * when there are already sectors in the buffer cache
3362 * whose number will change. This is useful, because
3363 * mtools often changes the geometry of the disk after
3364 * looking at the boot block */
3365 if (DRS->maxblock > user_params[drive].sect || DRS->maxtrack)
3366 invalidate_drive(device);
3367 else
3368 process_fd_request();
3369 }
3370 return 0;
3371 }
3372
3373 /* handle obsolete ioctl's */
3374 static int ioctl_table[]= {
3375 FDCLRPRM,
3376 FDSETPRM,
3377 FDDEFPRM,
3378 FDGETPRM,
3379 FDMSGON,
3380 FDMSGOFF,
3381 FDFMTBEG,
3382 FDFMTTRK,
3383 FDFMTEND,
3384 FDSETEMSGTRESH,
3385 FDFLUSH,
3386 FDSETMAXERRS,
3387 FDGETMAXERRS,
3388 FDGETDRVTYP,
3389 FDSETDRVPRM,
3390 FDGETDRVPRM,
3391 FDGETDRVSTAT,
3392 FDPOLLDRVSTAT,
3393 FDRESET,
3394 FDGETFDCSTAT,
3395 FDWERRORCLR,
3396 FDWERRORGET,
3397 FDRAWCMD,
3398 FDEJECT,
3399 FDTWADDLE
3400 };
3401
3402 static inline int normalize_ioctl(int *cmd, int *size)
3403 {
3404 int i;
3405
3406 for (i=0; i < ARRAY_SIZE(ioctl_table); i++) {
3407 if ((*cmd & 0xffff) == (ioctl_table[i] & 0xffff)){
3408 *size = _IOC_SIZE(*cmd);
3409 *cmd = ioctl_table[i];
3410 if (*size > _IOC_SIZE(*cmd)) {
3411 printk("ioctl not yet supported\n");
3412 return -EFAULT;
3413 }
3414 return 0;
3415 }
3416 }
3417 return -EINVAL;
3418 }
3419
3420 static int get_floppy_geometry(int drive, int type, struct floppy_struct **g)
3421 {
3422 if (type)
3423 *g = &floppy_type[type];
3424 else {
3425 LOCK_FDC(drive,0);
3426 CALL(poll_drive(0,0));
3427 process_fd_request();
3428 *g = current_type[drive];
3429 }
3430 if (!*g)
3431 return -ENODEV;
3432 return 0;
3433 }
3434
3435 static int fd_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
3436 unsigned long param)
3437 {
3438 #define FD_IOCTL_ALLOWED ((filp) && (filp)->private_data)
3439 #define OUT(c,x) case c: outparam = (const char *) (x); break
3440 #define IN(c,x,tag) case c: *(x) = inparam. tag ; return 0
3441
3442 int i,drive,type;
3443 kdev_t device;
3444 int ret;
3445 int size;
3446 union inparam {
3447 struct floppy_struct g; /* geometry */
3448 struct format_descr f;
3449 struct floppy_max_errors max_errors;
3450 struct floppy_drive_params dp;
3451 } inparam; /* parameters coming from user space */
3452 const char *outparam; /* parameters passed back to user space */
3453
3454 device = inode->i_rdev;
3455 switch (cmd) {
3456 case BLKROSET:
3457 case BLKROGET:
3458 case BLKRASET:
3459 case BLKRAGET:
3460 case BLKFLSBUF:
3461 return blk_ioctl(device, cmd, param);
3462 }
3463 type = TYPE(device);
3464 drive = DRIVE(device);
3465
3466 /* convert compatibility eject ioctls into floppy eject ioctl.
3467 * We do this in order to provide a means to eject floppy disks before
3468 * installing the new fdutils package */
3469 if (cmd == CDROMEJECT || /* CD-ROM eject */
3470 cmd == 0x6470 /* SunOS floppy eject */) {
3471 DPRINT("obsolete eject ioctl\n");
3472 DPRINT("please use floppycontrol --eject\n");
3473 cmd = FDEJECT;
3474 }
3475
3476 /* generic block device ioctls */
3477 switch(cmd) {
3478 /* the following have been inspired by the corresponding
3479 * code for other block devices. */
3480 struct floppy_struct *g;
3481 case HDIO_GETGEO:
3482 {
3483 struct hd_geometry loc;
3484 ECALL(get_floppy_geometry(drive, type, &g));
3485 loc.heads = g->head;
3486 loc.sectors = g->sect;
3487 loc.cylinders = g->track;
3488 loc.start = 0;
3489 return _COPYOUT(loc);
3490 }
3491
3492 case BLKGETSIZE:
3493 ECALL(get_floppy_geometry(drive, type, &g));
3494 return put_user(g->size, (long *) param);
3495 /* BLKRRPART is not defined as floppies don't have
3496 * partition tables */
3497 }
3498
3499 /* convert the old style command into a new style command */
3500 if ((cmd & 0xff00) == 0x0200) {
3501 ECALL(normalize_ioctl(&cmd, &size));
3502 } else
3503 return -EINVAL;
3504
3505 /* permission checks */
3506 if (((cmd & 0x40) && !FD_IOCTL_ALLOWED) ||
3507 ((cmd & 0x80) && !capable(CAP_SYS_ADMIN)))
3508 return -EPERM;
3509
3510 /* copyin */
3511 CLEARSTRUCT(&inparam);
3512 if (_IOC_DIR(cmd) & _IOC_WRITE)
3513 ECALL(fd_copyin((void *)param, &inparam, size))
3514
3515 switch (cmd) {
3516 case FDEJECT:
3517 if (UDRS->fd_ref != 1)
3518 /* somebody else has this drive open */
3519 return -EBUSY;
3520 LOCK_FDC(drive,1);
3521
3522 /* do the actual eject. Fails on
3523 * non-Sparc architectures */
3524 ret=fd_eject(UNIT(drive));
3525
3526 USETF(FD_DISK_CHANGED);
3527 USETF(FD_VERIFY);
3528 process_fd_request();
3529 return ret;
3530 case FDCLRPRM:
3531 LOCK_FDC(drive,1);
3532 current_type[drive] = NULL;
3533 floppy_sizes[drive] = MAX_DISK_SIZE;
3534 UDRS->keep_data = 0;
3535 return invalidate_drive(device);
3536 case FDSETPRM:
3537 case FDDEFPRM:
3538 return set_geometry(cmd, & inparam.g,
3539 drive, type, device);
3540 case FDGETPRM:
3541 ECALL(get_floppy_geometry(drive, type,
3542 (struct floppy_struct**)
3543 &outparam));
3544 break;
3545
3546 case FDMSGON:
3547 UDP->flags |= FTD_MSG;
3548 return 0;
3549 case FDMSGOFF:
3550 UDP->flags &= ~FTD_MSG;
3551 return 0;
3552
3553 case FDFMTBEG:
3554 LOCK_FDC(drive,1);
3555 CALL(poll_drive(1, FD_RAW_NEED_DISK));
3556 ret = UDRS->flags;
3557 process_fd_request();
3558 if (ret & FD_VERIFY)
3559 return -ENODEV;
3560 if (!(ret & FD_DISK_WRITABLE))
3561 return -EROFS;
3562 return 0;
3563 case FDFMTTRK:
3564 if (UDRS->fd_ref != 1)
3565 return -EBUSY;
3566 return do_format(device, &inparam.f);
3567 case FDFMTEND:
3568 case FDFLUSH:
3569 LOCK_FDC(drive,1);
3570 return invalidate_drive(device);
3571
3572 case FDSETEMSGTRESH:
3573 UDP->max_errors.reporting =
3574 (unsigned short) (param & 0x0f);
3575 return 0;
3576 OUT(FDGETMAXERRS, &UDP->max_errors);
3577 IN(FDSETMAXERRS, &UDP->max_errors, max_errors);
3578
3579 case FDGETDRVTYP:
3580 outparam = drive_name(type,drive);
3581 SUPBOUND(size,strlen(outparam)+1);
3582 break;
3583
3584 IN(FDSETDRVPRM, UDP, dp);
3585 OUT(FDGETDRVPRM, UDP);
3586
3587 case FDPOLLDRVSTAT:
3588 LOCK_FDC(drive,1);
3589 CALL(poll_drive(1, FD_RAW_NEED_DISK));
3590 process_fd_request();
3591 /* fall through */
3592 OUT(FDGETDRVSTAT, UDRS);
3593
3594 case FDRESET:
3595 return user_reset_fdc(drive, (int)param, 1);
3596
3597 OUT(FDGETFDCSTAT,UFDCS);
3598
3599 case FDWERRORCLR:
3600 CLEARSTRUCT(UDRWE);
3601 return 0;
3602 OUT(FDWERRORGET,UDRWE);
3603
3604 case FDRAWCMD:
3605 if (type)
3606 return -EINVAL;
3607 LOCK_FDC(drive,1);
3608 set_floppy(device);
3609 CALL(i = raw_cmd_ioctl(cmd,(void *) param));
3610 process_fd_request();
3611 return i;
3612
3613 case FDTWADDLE:
3614 LOCK_FDC(drive,1);
3615 twaddle();
3616 process_fd_request();
3617 return 0;
3618
3619 default:
3620 return -EINVAL;
3621 }
3622
3623 if (_IOC_DIR(cmd) & _IOC_READ)
3624 return fd_copyout((void *)param, outparam, size);
3625 else
3626 return 0;
3627 #undef OUT
3628 #undef IN
3629 }
3630
3631 static void __init config_types(void)
3632 {
3633 int first=1;
3634 int drive;
3635
3636 /* read drive info out of physical CMOS */
3637 drive=0;
3638 if (!UDP->cmos)
3639 UDP->cmos = FLOPPY0_TYPE;
3640 drive=1;
3641 if (!UDP->cmos && FLOPPY1_TYPE)
3642 UDP->cmos = FLOPPY1_TYPE;
3643
3644 /* XXX */
3645 /* additional physical CMOS drive detection should go here */
3646
3647 for (drive=0; drive < N_DRIVE; drive++){
3648 unsigned int type = UDP->cmos;
3649 struct floppy_drive_params *params;
3650 const char *name = NULL;
3651 static char temparea[32];
3652
3653 if (type < NUMBER(default_drive_params)) {
3654 params = &default_drive_params[type].params;
3655 if (type) {
3656 name = default_drive_params[type].name;
3657 allowed_drive_mask |= 1 << drive;
3658 }
3659 } else {
3660 params = &default_drive_params[0].params;
3661 sprintf(temparea, "unknown type %d (usb?)", type);
3662 name = temparea;
3663 }
3664 if (name) {
3665 const char * prepend = ",";
3666 if (first) {
3667 prepend = KERN_INFO "Floppy drive(s):";
3668 first = 0;
3669 }
3670 printk("%s fd%d is %s", prepend, drive, name);
3671 register_devfs_entries (drive);
3672 }
3673 *UDP = *params;
3674 }
3675 if (!first)
3676 printk("\n");
3677 }
3678
3679 static int floppy_release(struct inode * inode, struct file * filp)
3680 {
3681 int drive = DRIVE(inode->i_rdev);
3682
3683 if (UDRS->fd_ref < 0)
3684 UDRS->fd_ref=0;
3685 else if (!UDRS->fd_ref--) {
3686 DPRINT("floppy_release with fd_ref == 0");
3687 UDRS->fd_ref = 0;
3688 }
3689 floppy_release_irq_and_dma();
3690 return 0;
3691 }
3692
3693 /*
3694 * floppy_open check for aliasing (/dev/fd0 can be the same as
3695 * /dev/PS0 etc), and disallows simultaneous access to the same
3696 * drive with different device numbers.
3697 */
3698 #define RETERR(x) do{floppy_release(inode,filp); return -(x);}while(0)
3699
3700 static int floppy_open(struct inode * inode, struct file * filp)
3701 {
3702 int drive;
3703 int old_dev;
3704 int try;
3705 char *tmp;
3706
3707 if (!filp) {
3708 DPRINT("Weird, open called with filp=0\n");
3709 return -EIO;
3710 }
3711
3712 filp->private_data = (void*) 0;
3713
3714 drive = DRIVE(inode->i_rdev);
3715 if (drive >= N_DRIVE ||
3716 !(allowed_drive_mask & (1 << drive)) ||
3717 fdc_state[FDC(drive)].version == FDC_NONE)
3718 return -ENXIO;
3719
3720 if (TYPE(inode->i_rdev) >= NUMBER(floppy_type))
3721 return -ENXIO;
3722 old_dev = UDRS->fd_device;
3723 if (UDRS->fd_ref && old_dev != MINOR(inode->i_rdev))
3724 return -EBUSY;
3725
3726 if (!UDRS->fd_ref && (UDP->flags & FD_BROKEN_DCL)){
3727 USETF(FD_DISK_CHANGED);
3728 USETF(FD_VERIFY);
3729 }
3730
3731 if (UDRS->fd_ref == -1 ||
3732 (UDRS->fd_ref && (filp->f_flags & O_EXCL)))
3733 return -EBUSY;
3734
3735 if (floppy_grab_irq_and_dma())
3736 return -EBUSY;
3737
3738 if (filp->f_flags & O_EXCL)
3739 UDRS->fd_ref = -1;
3740 else
3741 UDRS->fd_ref++;
3742
3743 if (!floppy_track_buffer){
3744 /* if opening an ED drive, reserve a big buffer,
3745 * else reserve a small one */
3746 if ((UDP->cmos == 6) || (UDP->cmos == 5))
3747 try = 64; /* Only 48 actually useful */
3748 else
3749 try = 32; /* Only 24 actually useful */
3750
3751 tmp=(char *)fd_dma_mem_alloc(1024 * try);
3752 if (!tmp && !floppy_track_buffer) {
3753 try >>= 1; /* buffer only one side */
3754 INFBOUND(try, 16);
3755 tmp= (char *)fd_dma_mem_alloc(1024*try);
3756 }
3757 if (!tmp && !floppy_track_buffer) {
3758 fallback_on_nodma_alloc(&tmp, 2048 * try);
3759 }
3760 if (!tmp && !floppy_track_buffer) {
3761 DPRINT("Unable to allocate DMA memory\n");
3762 RETERR(ENXIO);
3763 }
3764 if (floppy_track_buffer) {
3765 if (tmp)
3766 fd_dma_mem_free((unsigned long)tmp,try*1024);
3767 } else {
3768 buffer_min = buffer_max = -1;
3769 floppy_track_buffer = tmp;
3770 max_buffer_sectors = try;
3771 }
3772 }
3773
3774 UDRS->fd_device = MINOR(inode->i_rdev);
3775 if (old_dev != -1 && old_dev != MINOR(inode->i_rdev)) {
3776 if (buffer_drive == drive)
3777 buffer_track = -1;
3778 invalidate_buffers(MKDEV(FLOPPY_MAJOR,old_dev));
3779 }
3780
3781 /* Allow ioctls if we have write-permissions even if read-only open.
3782 * Needed so that programs such as fdrawcmd still can work on write
3783 * protected disks */
3784 if ((filp->f_mode & 2) ||
3785 (inode->i_sb && (permission(inode,2) == 0)))
3786 filp->private_data = (void*) 8;
3787
3788 if (UFDCS->rawcmd == 1)
3789 UFDCS->rawcmd = 2;
3790
3791 if (filp->f_flags & O_NDELAY)
3792 return 0;
3793 if (filp->f_mode & 3) {
3794 UDRS->last_checked = 0;
3795 check_disk_change(inode->i_rdev);
3796 if (UTESTF(FD_DISK_CHANGED))
3797 RETERR(ENXIO);
3798 }
3799 if ((filp->f_mode & 2) && !(UTESTF(FD_DISK_WRITABLE)))
3800 RETERR(EROFS);
3801 return 0;
3802 #undef RETERR
3803 }
3804
3805 /*
3806 * Check if the disk has been changed or if a change has been faked.
3807 */
3808 static int check_floppy_change(kdev_t dev)
3809 {
3810 int drive = DRIVE(dev);
3811
3812 if (MAJOR(dev) != MAJOR_NR) {
3813 DPRINT("check_floppy_change: not a floppy\n");
3814 return 0;
3815 }
3816
3817 if (UTESTF(FD_DISK_CHANGED) || UTESTF(FD_VERIFY))
3818 return 1;
3819
3820 if (UDP->checkfreq < (int)(jiffies - UDRS->last_checked)) {
3821 if(floppy_grab_irq_and_dma()) {
3822 return 1;
3823 }
3824
3825 lock_fdc(drive,0);
3826 poll_drive(0,0);
3827 process_fd_request();
3828 floppy_release_irq_and_dma();
3829 }
3830
3831 if (UTESTF(FD_DISK_CHANGED) ||
3832 UTESTF(FD_VERIFY) ||
3833 test_bit(drive, &fake_change) ||
3834 (!TYPE(dev) && !current_type[drive]))
3835 return 1;
3836 return 0;
3837 }
3838
3839 /* revalidate the floppy disk, i.e. trigger format autodetection by reading
3840 * the bootblock (block 0). "Autodetection" is also needed to check whether
3841 * there is a disk in the drive at all... Thus we also do it for fixed
3842 * geometry formats */
3843 static int floppy_revalidate(kdev_t dev)
3844 {
3845 #define NO_GEOM (!current_type[drive] && !TYPE(dev))
3846 struct buffer_head * bh;
3847 int drive=DRIVE(dev);
3848 int cf;
3849
3850 if (UTESTF(FD_DISK_CHANGED) ||
3851 UTESTF(FD_VERIFY) ||
3852 test_bit(drive, &fake_change) ||
3853 NO_GEOM){
3854 if(usage_count == 0) {
3855 printk("VFS: revalidate called on non-open device.\n");
3856 return -EFAULT;
3857 }
3858 lock_fdc(drive,0);
3859 cf = UTESTF(FD_DISK_CHANGED) || UTESTF(FD_VERIFY);
3860 if (!(cf || test_bit(drive, &fake_change) || NO_GEOM)){
3861 process_fd_request(); /*already done by another thread*/
3862 return 0;
3863 }
3864 UDRS->maxblock = 0;
3865 UDRS->maxtrack = 0;
3866 if (buffer_drive == drive)
3867 buffer_track = -1;
3868 clear_bit(drive, &fake_change);
3869 UCLEARF(FD_DISK_CHANGED);
3870 if (cf)
3871 UDRS->generation++;
3872 if (NO_GEOM){
3873 /* auto-sensing */
3874 int size = floppy_blocksizes[MINOR(dev)];
3875 if (!size)
3876 size = 1024;
3877 if (!(bh = getblk(dev,0,size))){
3878 process_fd_request();
3879 return -ENXIO;
3880 }
3881 if (bh && !buffer_uptodate(bh))
3882 ll_rw_block(READ, 1, &bh);
3883 process_fd_request();
3884 wait_on_buffer(bh);
3885 brelse(bh);
3886 return 0;
3887 }
3888 if (cf)
3889 poll_drive(0, FD_RAW_NEED_DISK);
3890 process_fd_request();
3891 }
3892 return 0;
3893 }
3894
3895 static struct block_device_operations floppy_fops = {
3896 open: floppy_open,
3897 release: floppy_release,
3898 ioctl: fd_ioctl,
3899 check_media_change: check_floppy_change,
3900 revalidate: floppy_revalidate,
3901 };
3902
3903 static void __init register_devfs_entries (int drive)
3904 {
3905 int base_minor, i;
3906 static char *table[] =
3907 {"", "d360", "h1200", "u360", "u720", "h360", "h720",
3908 "u1440", "u2880", "CompaQ", "h1440", "u1680", "h410",
3909 "u820", "h1476", "u1722", "h420", "u830", "h1494", "u1743",
3910 "h880", "u1040", "u1120", "h1600", "u1760", "u1920",
3911 "u3200", "u3520", "u3840", "u1840", "u800", "u1600",
3912 NULL
3913 };
3914 static int t360[] = {1,0}, t1200[] = {2,5,6,10,12,14,16,18,20,23,0},
3915 t3in[] = {8,9,26,27,28, 7,11,15,19,24,25,29,31, 3,4,13,17,21,22,30,0};
3916 static int *table_sup[] =
3917 {NULL, t360, t1200, t3in+5+8, t3in+5, t3in, t3in};
3918
3919 base_minor = (drive < 4) ? drive : (124 + drive);
3920 if (UDP->cmos <= NUMBER(default_drive_params)) {
3921 i = 0;
3922 do {
3923 char name[16];
3924
3925 sprintf (name, "%d%s", drive, table[table_sup[UDP->cmos][i]]);
3926 devfs_register (devfs_handle, name, DEVFS_FL_DEFAULT, MAJOR_NR,
3927 base_minor + (table_sup[UDP->cmos][i] << 2),
3928 S_IFBLK | S_IRUSR | S_IWUSR | S_IRGRP |S_IWGRP,
3929 &floppy_fops, NULL);
3930 } while (table_sup[UDP->cmos][i++]);
3931 }
3932 }
3933
3934 /*
3935 * Floppy Driver initialization
3936 * =============================
3937 */
3938
3939 /* Determine the floppy disk controller type */
3940 /* This routine was written by David C. Niemi */
3941 static char __init get_fdc_version(void)
3942 {
3943 int r;
3944
3945 output_byte(FD_DUMPREGS); /* 82072 and better know DUMPREGS */
3946 if (FDCS->reset)
3947 return FDC_NONE;
3948 if ((r = result()) <= 0x00)
3949 return FDC_NONE; /* No FDC present ??? */
3950 if ((r==1) && (reply_buffer[0] == 0x80)){
3951 printk(KERN_INFO "FDC %d is an 8272A\n",fdc);
3952 return FDC_8272A; /* 8272a/765 don't know DUMPREGS */
3953 }
3954 if (r != 10) {
3955 printk("FDC %d init: DUMPREGS: unexpected return of %d bytes.\n",
3956 fdc, r);
3957 return FDC_UNKNOWN;
3958 }
3959
3960 if (!fdc_configure()) {
3961 printk(KERN_INFO "FDC %d is an 82072\n",fdc);
3962 return FDC_82072; /* 82072 doesn't know CONFIGURE */
3963 }
3964
3965 output_byte(FD_PERPENDICULAR);
3966 if (need_more_output() == MORE_OUTPUT) {
3967 output_byte(0);
3968 } else {
3969 printk(KERN_INFO "FDC %d is an 82072A\n", fdc);
3970 return FDC_82072A; /* 82072A as found on Sparcs. */
3971 }
3972
3973 output_byte(FD_UNLOCK);
3974 r = result();
3975 if ((r == 1) && (reply_buffer[0] == 0x80)){
3976 printk(KERN_INFO "FDC %d is a pre-1991 82077\n", fdc);
3977 return FDC_82077_ORIG; /* Pre-1991 82077, doesn't know
3978 * LOCK/UNLOCK */
3979 }
3980 if ((r != 1) || (reply_buffer[0] != 0x00)) {
3981 printk("FDC %d init: UNLOCK: unexpected return of %d bytes.\n",
3982 fdc, r);
3983 return FDC_UNKNOWN;
3984 }
3985 output_byte(FD_PARTID);
3986 r = result();
3987 if (r != 1) {
3988 printk("FDC %d init: PARTID: unexpected return of %d bytes.\n",
3989 fdc, r);
3990 return FDC_UNKNOWN;
3991 }
3992 if (reply_buffer[0] == 0x80) {
3993 printk(KERN_INFO "FDC %d is a post-1991 82077\n",fdc);
3994 return FDC_82077; /* Revised 82077AA passes all the tests */
3995 }
3996 switch (reply_buffer[0] >> 5) {
3997 case 0x0:
3998 /* Either a 82078-1 or a 82078SL running at 5Volt */
3999 printk(KERN_INFO "FDC %d is an 82078.\n",fdc);
4000 return FDC_82078;
4001 case 0x1:
4002 printk(KERN_INFO "FDC %d is a 44pin 82078\n",fdc);
4003 return FDC_82078;
4004 case 0x2:
4005 printk(KERN_INFO "FDC %d is a S82078B\n", fdc);
4006 return FDC_S82078B;
4007 case 0x3:
4008 printk(KERN_INFO "FDC %d is a National Semiconductor PC87306\n", fdc);
4009 return FDC_87306;
4010 default:
4011 printk(KERN_INFO "FDC %d init: 82078 variant with unknown PARTID=%d.\n",
4012 fdc, reply_buffer[0] >> 5);
4013 return FDC_82078_UNKN;
4014 }
4015 } /* get_fdc_version */
4016
4017 /* lilo configuration */
4018
4019 static void __init floppy_set_flags(int *ints,int param, int param2)
4020 {
4021 int i;
4022
4023 for (i=0; i < ARRAY_SIZE(default_drive_params); i++){
4024 if (param)
4025 default_drive_params[i].params.flags |= param2;
4026 else
4027 default_drive_params[i].params.flags &= ~param2;
4028 }
4029 DPRINT("%s flag 0x%x\n", param2 ? "Setting" : "Clearing", param);
4030 }
4031
4032 static void __init daring(int *ints,int param, int param2)
4033 {
4034 int i;
4035
4036 for (i=0; i < ARRAY_SIZE(default_drive_params); i++){
4037 if (param){
4038 default_drive_params[i].params.select_delay = 0;
4039 default_drive_params[i].params.flags |= FD_SILENT_DCL_CLEAR;
4040 } else {
4041 default_drive_params[i].params.select_delay = 2*HZ/100;
4042 default_drive_params[i].params.flags &= ~FD_SILENT_DCL_CLEAR;
4043 }
4044 }
4045 DPRINT("Assuming %s floppy hardware\n", param ? "standard" : "broken");
4046 }
4047
4048 static void __init set_cmos(int *ints, int dummy, int dummy2)
4049 {
4050 int current_drive=0;
4051
4052 if (ints[0] != 2){
4053 DPRINT("wrong number of parameters for CMOS\n");
4054 return;
4055 }
4056 current_drive = ints[1];
4057 if (current_drive < 0 || current_drive >= 8){
4058 DPRINT("bad drive for set_cmos\n");
4059 return;
4060 }
4061 if (current_drive >= 4 && !FDC2)
4062 FDC2 = 0x370;
4063 DP->cmos = ints[2];
4064 DPRINT("setting CMOS code to %d\n", ints[2]);
4065 }
4066
4067 static struct param_table {
4068 const char *name;
4069 void (*fn)(int *ints, int param, int param2);
4070 int *var;
4071 int def_param;
4072 int param2;
4073 } config_params[]={
4074 { "allowed_drive_mask", 0, &allowed_drive_mask, 0xff, 0}, /* obsolete */
4075 { "all_drives", 0, &allowed_drive_mask, 0xff, 0 }, /* obsolete */
4076 { "asus_pci", 0, &allowed_drive_mask, 0x33, 0},
4077
4078 { "irq", 0, &FLOPPY_IRQ, 6, 0 },
4079 { "dma", 0, &FLOPPY_DMA, 2, 0 },
4080
4081 { "daring", daring, 0, 1, 0},
4082
4083 { "two_fdc", 0, &FDC2, 0x370, 0 },
4084 { "one_fdc", 0, &FDC2, 0, 0 },
4085
4086 { "thinkpad", floppy_set_flags, 0, 1, FD_INVERTED_DCL },
4087 { "broken_dcl", floppy_set_flags, 0, 1, FD_BROKEN_DCL },
4088 { "messages", floppy_set_flags, 0, 1, FTD_MSG },
4089 { "silent_dcl_clear", floppy_set_flags, 0, 1, FD_SILENT_DCL_CLEAR },
4090 { "debug", floppy_set_flags, 0, 1, FD_DEBUG },
4091
4092 { "nodma", 0, &can_use_virtual_dma, 1, 0 },
4093 { "omnibook", 0, &can_use_virtual_dma, 1, 0 },
4094 { "yesdma", 0, &can_use_virtual_dma, 0, 0 },
4095
4096 { "fifo_depth", 0, &fifo_depth, 0xa, 0 },
4097 { "nofifo", 0, &no_fifo, 0x20, 0 },
4098 { "usefifo", 0, &no_fifo, 0, 0 },
4099
4100 { "cmos", set_cmos, 0, 0, 0 },
4101 { "slow", 0, &slow_floppy, 1, 0 },
4102
4103 { "unexpected_interrupts", 0, &print_unex, 1, 0 },
4104 { "no_unexpected_interrupts", 0, &print_unex, 0, 0 },
4105 { "L40SX", 0, &print_unex, 0, 0 }
4106 };
4107
4108 static int __init floppy_setup(char *str)
4109 {
4110 int i;
4111 int param;
4112 int ints[11];
4113
4114 str = get_options(str,ARRAY_SIZE(ints),ints);
4115 if (str) {
4116 for (i=0; i< ARRAY_SIZE(config_params); i++){
4117 if (strcmp(str,config_params[i].name) == 0){
4118 if (ints[0])
4119 param = ints[1];
4120 else
4121 param = config_params[i].def_param;
4122 if (config_params[i].fn)
4123 config_params[i].
4124 fn(ints,param,
4125 config_params[i].param2);
4126 if (config_params[i].var) {
4127 DPRINT("%s=%d\n", str, param);
4128 *config_params[i].var = param;
4129 }
4130 return 1;
4131 }
4132 }
4133 }
4134 if (str) {
4135 DPRINT("unknown floppy option [%s]\n", str);
4136
4137 DPRINT("allowed options are:");
4138 for (i=0; i< ARRAY_SIZE(config_params); i++)
4139 printk(" %s",config_params[i].name);
4140 printk("\n");
4141 } else
4142 DPRINT("botched floppy option\n");
4143 DPRINT("Read linux/Documentation/floppy.txt\n");
4144 return 0;
4145 }
4146
4147 static int have_no_fdc= -EIO;
4148
4149
4150 int __init floppy_init(void)
4151 {
4152 int i,unit,drive;
4153
4154
4155 raw_cmd = NULL;
4156
4157 devfs_handle = devfs_mk_dir (NULL, "floppy", NULL);
4158 if (devfs_register_blkdev(MAJOR_NR,"fd",&floppy_fops)) {
4159 printk("Unable to get major %d for floppy\n",MAJOR_NR);
4160 return -EBUSY;
4161 }
4162
4163 for (i=0; i<256; i++)
4164 if (ITYPE(i))
4165 floppy_sizes[i] = (floppy_type[ITYPE(i)].size+1) >> 1;
4166 else
4167 floppy_sizes[i] = MAX_DISK_SIZE;
4168
4169 blk_size[MAJOR_NR] = floppy_sizes;
4170 blksize_size[MAJOR_NR] = floppy_blocksizes;
4171 blk_init_queue(BLK_DEFAULT_QUEUE(MAJOR_NR), DEVICE_REQUEST);
4172 reschedule_timeout(MAXTIMEOUT, "floppy init", MAXTIMEOUT);
4173 config_types();
4174
4175 for (i = 0; i < N_FDC; i++) {
4176 fdc = i;
4177 CLEARSTRUCT(FDCS);
4178 FDCS->dtr = -1;
4179 FDCS->dor = 0x4;
4180 #ifdef __sparc__
4181 /*sparcs don't have a DOR reset which we can fall back on to*/
4182 FDCS->version = FDC_82072A;
4183 #endif
4184 }
4185
4186 use_virtual_dma = can_use_virtual_dma & 1;
4187 fdc_state[0].address = FDC1;
4188 if (fdc_state[0].address == -1) {
4189 devfs_unregister_blkdev(MAJOR_NR,"fd");
4190 del_timer(&fd_timeout);
4191 blk_cleanup_queue(BLK_DEFAULT_QUEUE(MAJOR_NR));
4192 return -ENODEV;
4193 }
4194 #if N_FDC > 1
4195 fdc_state[1].address = FDC2;
4196 #endif
4197
4198 fdc = 0; /* reset fdc in case of unexpected interrupt */
4199 if (floppy_grab_irq_and_dma()){
4200 del_timer(&fd_timeout);
4201 blk_cleanup_queue(BLK_DEFAULT_QUEUE(MAJOR_NR));
4202 devfs_unregister_blkdev(MAJOR_NR,"fd");
4203 del_timer(&fd_timeout);
4204 return -EBUSY;
4205 }
4206
4207 /* initialise drive state */
4208 for (drive = 0; drive < N_DRIVE; drive++) {
4209 CLEARSTRUCT(UDRS);
4210 CLEARSTRUCT(UDRWE);
4211 USETF(FD_DISK_NEWCHANGE);
4212 USETF(FD_DISK_CHANGED);
4213 USETF(FD_VERIFY);
4214 UDRS->fd_device = -1;
4215 floppy_track_buffer = NULL;
4216 max_buffer_sectors = 0;
4217 }
4218
4219 for (i = 0; i < N_FDC; i++) {
4220 fdc = i;
4221 FDCS->driver_version = FD_DRIVER_VERSION;
4222 for (unit=0; unit<4; unit++)
4223 FDCS->track[unit] = 0;
4224 if (FDCS->address == -1)
4225 continue;
4226 FDCS->rawcmd = 2;
4227 if (user_reset_fdc(-1,FD_RESET_ALWAYS,0)){
4228 /* free ioports reserved by floppy_grab_irq_and_dma() */
4229 release_region(FDCS->address, 6);
4230 release_region(FDCS->address+7, 1);
4231 FDCS->address = -1;
4232 FDCS->version = FDC_NONE;
4233 continue;
4234 }
4235 /* Try to determine the floppy controller type */
4236 FDCS->version = get_fdc_version();
4237 if (FDCS->version == FDC_NONE){
4238 /* free ioports reserved by floppy_grab_irq_and_dma() */
4239 release_region(FDCS->address, 6);
4240 release_region(FDCS->address+7, 1);
4241 FDCS->address = -1;
4242 continue;
4243 }
4244 if (can_use_virtual_dma == 2 && FDCS->version < FDC_82072A)
4245 can_use_virtual_dma = 0;
4246
4247 have_no_fdc = 0;
4248 /* Not all FDCs seem to be able to handle the version command
4249 * properly, so force a reset for the standard FDC clones,
4250 * to avoid interrupt garbage.
4251 */
4252 user_reset_fdc(-1,FD_RESET_ALWAYS,0);
4253 }
4254 fdc=0;
4255 del_timer(&fd_timeout);
4256 current_drive = 0;
4257 floppy_release_irq_and_dma();
4258 initialising=0;
4259 if (have_no_fdc)
4260 {
4261 DPRINT("no floppy controllers found\n");
4262 floppy_tq.routine = (void *)(void *) empty;
4263 mark_bh(IMMEDIATE_BH);
4264 schedule();
4265 if (usage_count)
4266 floppy_release_irq_and_dma();
4267 blk_cleanup_queue(BLK_DEFAULT_QUEUE(MAJOR_NR));
4268 devfs_unregister_blkdev(MAJOR_NR,"fd");
4269 }
4270
4271 for (drive = 0; drive < N_DRIVE; drive++) {
4272 if (!(allowed_drive_mask & (1 << drive)))
4273 continue;
4274 if (fdc_state[FDC(drive)].version == FDC_NONE)
4275 continue;
4276 for (i = 0; i<NUMBER(floppy_type); i++)
4277 register_disk(NULL, MKDEV(MAJOR_NR,TOMINOR(drive)+i*4),
4278 1, &floppy_fops, 0);
4279 }
4280 return have_no_fdc;
4281 }
4282
4283 static spinlock_t floppy_usage_lock = SPIN_LOCK_UNLOCKED;
4284
4285 static int floppy_grab_irq_and_dma(void)
4286 {
4287 unsigned long flags;
4288
4289 spin_lock_irqsave(&floppy_usage_lock, flags);
4290 if (usage_count++){
4291 spin_unlock_irqrestore(&floppy_usage_lock, flags);
4292 return 0;
4293 }
4294 spin_unlock_irqrestore(&floppy_usage_lock, flags);
4295 MOD_INC_USE_COUNT;
4296 if (fd_request_irq()) {
4297 DPRINT("Unable to grab IRQ%d for the floppy driver\n",
4298 FLOPPY_IRQ);
4299 MOD_DEC_USE_COUNT;
4300 spin_lock_irqsave(&floppy_usage_lock, flags);
4301 usage_count--;
4302 spin_unlock_irqrestore(&floppy_usage_lock, flags);
4303 return -1;
4304 }
4305 if (fd_request_dma()) {
4306 DPRINT("Unable to grab DMA%d for the floppy driver\n",
4307 FLOPPY_DMA);
4308 fd_free_irq();
4309 MOD_DEC_USE_COUNT;
4310 spin_lock_irqsave(&floppy_usage_lock, flags);
4311 usage_count--;
4312 spin_unlock_irqrestore(&floppy_usage_lock, flags);
4313 return -1;
4314 }
4315
4316 for (fdc=0; fdc< N_FDC; fdc++){
4317 if (FDCS->address != -1){
4318 if (!request_region(FDCS->address, 6, "floppy")) {
4319 DPRINT("Floppy io-port 0x%04lx in use\n", FDCS->address);
4320 goto cleanup1;
4321 }
4322 if (!request_region(FDCS->address + 7, 1, "floppy DIR")) {
4323 DPRINT("Floppy io-port 0x%04lx in use\n", FDCS->address + 7);
4324 goto cleanup2;
4325 }
4326 /* address + 6 is reserved, and may be taken by IDE.
4327 * Unfortunately, Adaptec doesn't know this :-(, */
4328 }
4329 }
4330 for (fdc=0; fdc< N_FDC; fdc++){
4331 if (FDCS->address != -1){
4332 reset_fdc_info(1);
4333 fd_outb(FDCS->dor, FD_DOR);
4334 }
4335 }
4336 fdc = 0;
4337 set_dor(0, ~0, 8); /* avoid immediate interrupt */
4338
4339 for (fdc = 0; fdc < N_FDC; fdc++)
4340 if (FDCS->address != -1)
4341 fd_outb(FDCS->dor, FD_DOR);
4342 /*
4343 * The driver will try and free resources and relies on us
4344 * to know if they were allocated or not.
4345 */
4346 fdc = 0;
4347 irqdma_allocated = 1;
4348 return 0;
4349 cleanup2:
4350 release_region(FDCS->address, 6);
4351 cleanup1:
4352 fd_free_irq();
4353 fd_free_dma();
4354 while(--fdc >= 0) {
4355 release_region(FDCS->address, 6);
4356 release_region(FDCS->address + 7, 1);
4357 }
4358 MOD_DEC_USE_COUNT;
4359 spin_lock_irqsave(&floppy_usage_lock, flags);
4360 usage_count--;
4361 spin_unlock_irqrestore(&floppy_usage_lock, flags);
4362 return -1;
4363 }
4364
4365 static void floppy_release_irq_and_dma(void)
4366 {
4367 int old_fdc;
4368 #ifdef FLOPPY_SANITY_CHECK
4369 #ifndef __sparc__
4370 int drive;
4371 #endif
4372 #endif
4373 long tmpsize;
4374 unsigned long tmpaddr;
4375 unsigned long flags;
4376
4377 spin_lock_irqsave(&floppy_usage_lock, flags);
4378 if (--usage_count){
4379 spin_unlock_irqrestore(&floppy_usage_lock, flags);
4380 return;
4381 }
4382 spin_unlock_irqrestore(&floppy_usage_lock, flags);
4383 if(irqdma_allocated)
4384 {
4385 fd_disable_dma();
4386 fd_free_dma();
4387 fd_free_irq();
4388 irqdma_allocated=0;
4389 }
4390 set_dor(0, ~0, 8);
4391 #if N_FDC > 1
4392 set_dor(1, ~8, 0);
4393 #endif
4394 floppy_enable_hlt();
4395
4396 if (floppy_track_buffer && max_buffer_sectors) {
4397 tmpsize = max_buffer_sectors*1024;
4398 tmpaddr = (unsigned long)floppy_track_buffer;
4399 floppy_track_buffer = NULL;
4400 max_buffer_sectors = 0;
4401 buffer_min = buffer_max = -1;
4402 fd_dma_mem_free(tmpaddr, tmpsize);
4403 }
4404
4405 #ifdef FLOPPY_SANITY_CHECK
4406 #ifndef __sparc__
4407 for (drive=0; drive < N_FDC * 4; drive++)
4408 if (timer_pending(motor_off_timer + drive))
4409 printk("motor off timer %d still active\n", drive);
4410 #endif
4411
4412 if (timer_pending(&fd_timeout))
4413 printk("floppy timer still active:%s\n", timeout_message);
4414 if (timer_pending(&fd_timer))
4415 printk("auxiliary floppy timer still active\n");
4416 if (floppy_tq.sync)
4417 printk("task queue still active\n");
4418 #endif
4419 old_fdc = fdc;
4420 for (fdc = 0; fdc < N_FDC; fdc++)
4421 if (FDCS->address != -1) {
4422 release_region(FDCS->address, 6);
4423 release_region(FDCS->address+7, 1);
4424 }
4425 fdc = old_fdc;
4426 MOD_DEC_USE_COUNT;
4427 }
4428
4429
4430 #ifdef MODULE
4431
4432 char *floppy;
4433
4434 static void __init parse_floppy_cfg_string(char *cfg)
4435 {
4436 char *ptr;
4437
4438 while(*cfg) {
4439 for(ptr = cfg;*cfg && *cfg != ' ' && *cfg != '\t'; cfg++);
4440 if (*cfg) {
4441 *cfg = '\0';
4442 cfg++;
4443 }
4444 if (*ptr)
4445 floppy_setup(ptr);
4446 }
4447 }
4448
4449 int init_module(void)
4450 {
4451 printk(KERN_INFO "inserting floppy driver for " UTS_RELEASE "\n");
4452
4453 if (floppy)
4454 parse_floppy_cfg_string(floppy);
4455 return floppy_init();
4456 }
4457
4458 void cleanup_module(void)
4459 {
4460 int dummy;
4461
4462 devfs_unregister (devfs_handle);
4463 devfs_unregister_blkdev(MAJOR_NR, "fd");
4464
4465 blk_cleanup_queue(BLK_DEFAULT_QUEUE(MAJOR_NR));
4466 /* eject disk, if any */
4467 dummy = fd_eject(0);
4468 }
4469
4470 MODULE_PARM(floppy,"s");
4471 MODULE_PARM(FLOPPY_IRQ,"i");
4472 MODULE_PARM(FLOPPY_DMA,"i");
4473 MODULE_AUTHOR("Alain L. Knaff");
4474 MODULE_SUPPORTED_DEVICE("fd");
4475
4476 #else
4477
4478 __setup ("floppy=", floppy_setup);
4479
4480 /* eject the boot floppy (if we need the drive for a different root floppy) */
4481 /* This should only be called at boot time when we're sure that there's no
4482 * resource contention. */
4483 void floppy_eject(void)
4484 {
4485 int dummy;
4486 if (have_no_fdc)
4487 return;
4488 if(floppy_grab_irq_and_dma()==0)
4489 {
4490 lock_fdc(MAXTIMEOUT,0);
4491 dummy=fd_eject(0);
4492 process_fd_request();
4493 floppy_release_irq_and_dma();
4494 }
4495 }
4496 #endif
4497
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.