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