2 * linux/drivers/block/floppy.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 * Copyright (C) 1993, 1994 Alain Knaff
6 * Copyright (C) 1998 Alan Cox
9 * 02.12.91 - Changed to static variables to indicate need for reset
10 * and recalibrate. This makes some things easier (output_byte reset
11 * checking etc), and means less interrupt jumping in case of errors,
12 * so the code is hopefully easier to understand.
16 * This file is certainly a mess. I've tried my best to get it working,
17 * but I don't like programming floppies, and I have only one anyway.
18 * Urgel. I should check for more errors, and do more graceful error
19 * recovery. Seems there are problems with several drives. I've tried to
20 * correct them. No promises.
24 * As with hd.c, all routines within this file can (and will) be called
25 * by interrupts, so extreme caution is needed. A hardware interrupt
26 * handler may not sleep, or a kernel panic will happen. Thus I cannot
27 * call "floppy-on" directly, but have to set a special timer interrupt
32 * 28.02.92 - made track-buffering routines, based on the routines written
33 * by entropy@wintermute.wpi.edu (Lawrence Foard). Linus.
37 * Automatic floppy-detection and formatting written by Werner Almesberger
38 * (almesber@nessie.cs.id.ethz.ch), who also corrected some problems with
39 * the floppy-change signal detection.
43 * 1992/7/22 -- Hennus Bergman: Added better error reporting, fixed
44 * FDC data overrun bug, added some preliminary stuff for vertical
47 * 1992/9/17: Added DMA allocation & DMA functions. -- hhb.
49 * TODO: Errors are still not counted properly.
53 * Modifications for ``Sector Shifting'' by Rob Hooft (hooft@chem.ruu.nl)
54 * modeled after the freeware MS-DOS program fdformat/88 V1.8 by
55 * Christoph H. Hochst\"atter.
56 * I have fixed the shift values to the ones I always use. Maybe a new
57 * ioctl() should be created to be able to modify them.
58 * There is a bug in the driver that makes it impossible to format a
59 * floppy as the first thing after bootup.
63 * 1993/4/29 -- Linus -- cleaned up the timer handling in the kernel, and
64 * this helped the floppy driver as well. Much cleaner, and still seems to
68 /* 1994/6/24 --bbroad-- added the floppy table entries and made
69 * minor modifications to allow 2.88 floppies to be run.
72 /* 1994/7/13 -- Paul Vojta -- modified the probing code to allow three or more
77 * 1994/8/8 -- Alain Knaff -- Switched to fdpatch driver: Support for bigger
78 * format bug fixes, but unfortunately some new bugs too...
81 /* 1994/9/17 -- Koen Holtman -- added logging of physical floppy write
82 * errors to allow safe writing by specialized programs.
85 /* 1995/4/24 -- Dan Fandrich -- added support for Commodore 1581 3.5" disks
86 * by defining bit 1 of the "stretch" parameter to mean put sectors on the
87 * opposite side of the disk, leaving the sector IDs alone (i.e. Commodore's
88 * drives are "upside-down").
92 * 1995/8/26 -- Andreas Busse -- added Mips support.
96 * 1995/10/18 -- Ralf Baechle -- Portability cleanup; move machine dependent
97 * features to asm/floppy.h.
101 * 1998/1/21 -- Richard Gooch <rgooch@atnf.csiro.au> -- devfs support
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.
111 * 1998/06/07 -- Alan Cox -- Merged the 2.0.34 fixes for resource allocation
116 * 1998/09/20 -- David Weinehall -- Added slow-down code for buggy PS/2-drives.
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).
126 * 2000/08/28 -- Arnaldo Carvalho de Melo <acme@conectiva.com.br>
127 * - get rid of check_region
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.)
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.
142 /* 2003/07/28 -- Daniele Bellucci <bellucda@tiscali.it>.
143 * Better audit of register_blkdev.
146 #define FLOPPY_SANITY_CHECK
147 #undef FLOPPY_SILENT_DCL_CLEAR
149 #define REALLY_SLOW_IO
152 #define DCL_DEBUG /* debug disk change line */
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>
163 #include <linux/fdreg.h>
165 #include <linux/fd.h>
166 #include <linux/hdreg.h>
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>
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.
190 static int slow_floppy;
194 #include <asm/system.h>
196 #include <asm/uaccess.h>
198 static int FLOPPY_IRQ = 6;
199 static int FLOPPY_DMA = 2;
200 static int can_use_virtual_dma = 2;
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
209 static int use_virtual_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
221 static DEFINE_SPINLOCK(floppy_lock);
222 static struct completion device_release;
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;
229 #define K_64 0x10000 /* 64KB */
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.
237 * NOTE: This must come before we include the arch floppy header because
238 * some ports reference this variable from there. -DaveM
241 static int allowed_drive_mask = 0x33;
243 #include <asm/floppy.h>
245 static int irqdma_allocated;
247 #define DEVICE_NAME "floppy"
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>
255 * Interrupt freeing also means /proc VFS work - dont do it
256 * from interrupt context. We push this work into keventd:
258 static void fd_free_irq_fn(void *data)
263 static DECLARE_WORK(fd_free_irq_work, fd_free_irq_fn, NULL);
266 static struct request *current_req;
267 static struct request_queue *floppy_queue;
268 static void do_fd_request(request_queue_t * q);
270 #ifndef fd_get_dma_residue
271 #define fd_get_dma_residue() get_dma_residue(FLOPPY_DMA)
274 /* Dma Memory related stuff */
276 #ifndef fd_dma_mem_free
277 #define fd_dma_mem_free(addr, size) free_pages(addr, get_order(size))
280 #ifndef fd_dma_mem_alloc
281 #define fd_dma_mem_alloc(size) __get_dma_pages(GFP_KERNEL,get_order(size))
284 static inline void fallback_on_nodma_alloc(char **addr, size_t l)
286 #ifdef FLOPPY_CAN_FALLBACK_ON_NODMA
288 return; /* we have the memory */
289 if (can_use_virtual_dma != 2)
290 return; /* no fallback allowed */
292 ("DMA memory shortage. Temporarily falling back on virtual DMA\n");
293 *addr = (char *)nodma_mem_alloc(l);
299 /* End dma memory related stuff */
301 static unsigned long fake_change;
302 static int initialising = 1;
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))
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))
326 #define DPRINT(format, args...) printk(DEVICE_NAME "%d: " format, current_drive , ## args)
328 #define PH_HEAD(floppy,head) (((((floppy)->stretch & 2) >>1) ^ head) << 2)
329 #define STRETCH(floppy) ((floppy)->stretch & FD_STRETCH)
331 #define CLEARSTRUCT(x) memset((x), 0, sizeof(*(x)))
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]
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]
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]
357 #define MAX_DISK_SIZE 4 /* 3984 */
360 * globals used by 'result()'
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])
374 #define SEL_DLY (2*HZ/100)
377 * this struct defines the different floppy drive types.
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!
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" },
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*/
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*/
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*/
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*/
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*/
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
421 * Frequency of disk change checks */
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;
434 * This struct defines the different floppy types.
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
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" */
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" */
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" */
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" */
495 #define SECTSIZE (_FD_SECTSIZE(*floppy))
497 /* Auto-detection: Disk type used until the next media change occurs. */
498 static struct floppy_struct *current_type[N_DRIVE];
501 * User-provided type information. current_type points to
502 * the respective entry of this array.
504 static struct floppy_struct user_params[N_DRIVE];
506 static sector_t floppy_sizes[256];
508 static char floppy_device_name[] = "floppy";
511 * The driver is trying to determine the correct media format
512 * while probing is set. rw_interrupt() clears it after a
517 /* Synchronization of FDC access. */
518 #define FD_COMMAND_NONE -1
519 #define FD_COMMAND_ERROR 2
520 #define FD_COMMAND_OKAY 3
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);
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)
534 /* Errors during formatting are counted here. */
535 static int format_errors;
537 /* Format request descriptor. */
538 static struct format_descr format_req;
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)
548 * Because these are written to by the DMA controller, they must
549 * not contain a 64k byte boundary crossing, or data will be
552 static char *floppy_track_buffer;
553 static int max_buffer_sectors;
556 typedef void (*done_f) (int);
557 static struct cont_t {
558 void (*interrupt) (void); /* this is called after the interrupt of the
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 */
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);
572 static int floppy_grab_irq_and_dma(void);
573 static void floppy_release_irq_and_dma(void);
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.
582 #define CHECK_RESET { if (FDCS->reset){ reset_fdc(); return; } }
583 static void reset_fdc(void);
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
591 #define NEED_1_RECAL -2
592 #define NEED_2_RECAL -3
594 static int usage_count;
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;
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 */
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 */
614 static inline int fd_eject(int drive)
625 static long unsigned debugtimer;
627 static inline void set_debugt(void)
629 debugtimer = jiffies;
632 static inline void debugt(const char *message)
634 if (DP->flags & DEBUGT)
635 printk("%s dtime=%lu\n", message, jiffies - debugtimer);
638 static inline void set_debugt(void) { }
639 static inline void debugt(const char *message) { }
642 typedef void (*timeout_fn) (unsigned long);
643 static DEFINE_TIMER(fd_timeout, floppy_shutdown, 0, 0);
645 static const char *timeout_message;
647 #ifdef FLOPPY_SANITY_CHECK
648 static void is_alive(const char *message)
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);
658 static void (*do_floppy) (void) = NULL;
660 #ifdef FLOPPY_SANITY_CHECK
664 static void (*lasthandler) (void);
665 static unsigned long interruptjiffies;
666 static unsigned long resultjiffies;
667 static int resultsize;
668 static unsigned long lastredo;
670 static struct output_log {
672 unsigned char status;
673 unsigned long jiffies;
674 } output_log[OLOGSIZE];
676 static int output_log_pos;
679 #define current_reqD -1
680 #define MAXTIMEOUT -2
682 static void __reschedule_timeout(int drive, const char *message, int marg)
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;
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);
698 timeout_message = message;
701 static void reschedule_timeout(int drive, const char *message, int marg)
705 spin_lock_irqsave(&floppy_lock, flags);
706 __reschedule_timeout(drive, message, marg);
707 spin_unlock_irqrestore(&floppy_lock, flags);
710 #define INFBOUND(a,b) (a)=max_t(int, a, b)
712 #define SUPBOUND(a,b) (a)=min_t(int, a, b)
715 * Bottom half floppy driver.
716 * ==========================
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)
724 * This routine is responsible for maintaining the FD_DISK_CHANGE flag,
725 * and the last_checked date.
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
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
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.
743 * This means that we really have a third possibility too:
744 * The floppy has been changed after the last seek.
747 static int disk_change(int drive)
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);
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);
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);
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 "
784 current_type[drive] = NULL;
785 floppy_sizes[TOMINOR(drive)] = MAX_DISK_SIZE << 1;
788 /*USETF(FD_DISK_NEWCHANGE); */
791 UDRS->last_checked = jiffies;
792 UCLEARF(FD_DISK_NEWCHANGE);
797 static inline int is_selected(int dor, int unit)
799 return ((dor & (0x10 << unit)) && (dor & 3) == unit);
802 static int set_dor(int fdc, char mask, char data)
804 register unsigned char drive, unit, newdor, olddor;
806 if (FDCS->address == -1)
810 newdor = (olddor & mask) | data;
811 if (newdor != olddor) {
813 if (is_selected(olddor, unit) && !is_selected(newdor, unit)) {
814 drive = REVDRIVE(fdc, unit);
816 if (UDP->flags & FD_DEBUG) {
817 DPRINT("calling disk change from set_dor\n");
823 fd_outb(newdor, FD_DOR);
826 if (!is_selected(olddor, unit) && is_selected(newdor, unit)) {
827 drive = REVDRIVE(fdc, unit);
828 UDRS->select_date = jiffies;
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.
836 if (newdor & FLOPPY_MOTOR_MASK)
837 floppy_grab_irq_and_dma();
838 if (olddor & FLOPPY_MOTOR_MASK)
839 floppy_release_irq_and_dma();
843 static void twaddle(void)
845 if (DP->select_delay)
847 fd_outb(FDCS->dor & ~(0x10 << UNIT(current_drive)), FD_DOR);
848 fd_outb(FDCS->dor, FD_DOR);
849 DRS->select_date = jiffies;
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)
858 FDCS->spec1 = FDCS->spec2 = -1;
859 FDCS->need_configure = 1;
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;
867 /* selects the fdc and drive, and enables the fdc's input/dma. */
868 static void set_fdc(int drive)
870 if (drive >= 0 && drive < N_DRIVE) {
872 current_drive = drive;
874 if (fdc != 1 && fdc != 0) {
875 printk("bad fdc value\n");
880 set_dor(1 - fdc, ~8, 0);
882 if (FDCS->rawcmd == 2)
884 if (fd_inb(FD_STATUS) != STATUS_READY)
888 /* locks the driver */
889 static int _lock_fdc(int drive, int interruptible, int line)
893 "Trying to lock fdc while usage count=0 at line %d\n",
897 if (floppy_grab_irq_and_dma() == -1)
900 if (test_and_set_bit(0, &fdc_busy)) {
901 DECLARE_WAITQUEUE(wait, current);
902 add_wait_queue(&fdc_wait, &wait);
905 set_current_state(TASK_INTERRUPTIBLE);
907 if (!test_and_set_bit(0, &fdc_busy))
913 remove_wait_queue(&fdc_wait, &wait);
918 set_current_state(TASK_RUNNING);
919 remove_wait_queue(&fdc_wait, &wait);
921 command_status = FD_COMMAND_NONE;
923 __reschedule_timeout(drive, "lock fdc", 0);
928 #define lock_fdc(drive,interruptible) _lock_fdc(drive,interruptible, __LINE__)
930 #define LOCK_FDC(drive,interruptible) \
931 if (lock_fdc(drive,interruptible)) return -EINTR;
933 /* unlocks the driver */
934 static inline void unlock_fdc(void)
939 if (!test_bit(0, &fdc_busy))
940 DPRINT("FDC access conflict!\n");
943 DPRINT("device interrupt still active at FDC release: %p!\n",
945 command_status = FD_COMMAND_NONE;
946 spin_lock_irqsave(&floppy_lock, flags);
947 del_timer(&fd_timeout);
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();
957 /* switches the motor off after a given timeout */
958 static void motor_off_callback(unsigned long nr)
960 unsigned char mask = ~(0x10 << UNIT(nr));
962 set_dor(FDC(nr), mask, 0);
965 /* schedules motor off */
966 static void floppy_off(unsigned int drive)
968 unsigned long volatile delta;
969 register int fdc = FDC(drive);
971 if (!(FDCS->dor & (0x10 << UNIT(drive))))
974 del_timer(motor_off_timer + drive);
976 /* make spindle stop in a position which minimizes spinup time
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;
985 add_timer(motor_off_timer + drive);
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.
993 static void scandrives(void)
995 int i, drive, saved_drive;
997 if (DP->select_delay)
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 */
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
1010 set_dor(fdc, ~(0x10 << UNIT(drive)), 0);
1012 set_fdc(saved_drive);
1015 static void empty(void)
1019 static DECLARE_WORK(floppy_work, NULL, NULL);
1021 static void schedule_bh(void (*handler) (void))
1023 PREPARE_WORK(&floppy_work, (void (*)(void *))handler, NULL);
1024 schedule_work(&floppy_work);
1027 static DEFINE_TIMER(fd_timer, NULL, 0, 0);
1029 static void cancel_activity(void)
1031 unsigned long flags;
1033 spin_lock_irqsave(&floppy_lock, flags);
1035 PREPARE_WORK(&floppy_work, (void *)empty, NULL);
1036 del_timer(&fd_timer);
1037 spin_unlock_irqrestore(&floppy_lock, flags);
1040 /* this function makes sure that the disk stays in the drive during the
1042 static void fd_watchdog(void)
1045 if (DP->flags & FD_DEBUG) {
1046 DPRINT("calling disk change from watchdog\n");
1050 if (disk_change(current_drive)) {
1051 DPRINT("disk removed during i/o\n");
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);
1063 static void main_command_interrupt(void)
1065 del_timer(&fd_timer);
1069 /* waits for a delay (spinup or select) to pass */
1070 static int fd_wait_for_completion(unsigned long delay, timeout_fn function)
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 */
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);
1089 static DEFINE_SPINLOCK(floppy_hlt_lock);
1090 static int hlt_disabled;
1091 static void floppy_disable_hlt(void)
1093 unsigned long flags;
1095 spin_lock_irqsave(&floppy_hlt_lock, flags);
1096 if (!hlt_disabled) {
1098 #ifdef HAVE_DISABLE_HLT
1102 spin_unlock_irqrestore(&floppy_hlt_lock, flags);
1105 static void floppy_enable_hlt(void)
1107 unsigned long flags;
1109 spin_lock_irqsave(&floppy_hlt_lock, flags);
1112 #ifdef HAVE_DISABLE_HLT
1116 spin_unlock_irqrestore(&floppy_hlt_lock, flags);
1119 static void setup_DMA(void)
1123 #ifdef FLOPPY_SANITY_CHECK
1124 if (raw_cmd->length == 0) {
1127 printk("zero dma transfer size:");
1128 for (i = 0; i < raw_cmd->cmd_count; i++)
1129 printk("%x,", raw_cmd->cmd[i]);
1135 if (((unsigned long)raw_cmd->kernel_data) % 512) {
1136 printk("non aligned address: %p\n", raw_cmd->kernel_data);
1142 f = claim_dma_lock();
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);
1153 release_dma_lock(f);
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;
1163 release_dma_lock(f);
1165 floppy_disable_hlt();
1168 static void show_floppy(void);
1170 /* waits until the fdc becomes ready */
1171 static int wait_til_ready(void)
1173 int counter, status;
1176 for (counter = 0; counter < 10000; counter++) {
1177 status = fd_inb(FD_STATUS);
1178 if (status & STATUS_READY)
1181 if (!initialising) {
1182 DPRINT("Getstatus times out (%x) on fdc %d\n", status, fdc);
1189 /* sends a command byte to the fdc */
1190 static int output_byte(char byte)
1194 if ((status = wait_til_ready()) < 0)
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;
1207 if (!initialising) {
1208 DPRINT("Unable to send byte %x to FDC. Fdc=%x Status=%x\n",
1215 #define LAST_OUT(x) if (output_byte(x)<0){ reset_fdc();return;}
1217 /* gets the response from the fdc */
1218 static int result(void)
1222 for (i = 0; i < MAX_REPLIES; i++) {
1223 if ((status = wait_til_ready()) < 0)
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;
1233 if (status == (STATUS_DIR | STATUS_READY | STATUS_BUSY))
1234 reply_buffer[i] = fd_inb(FD_DATA);
1238 if (!initialising) {
1240 ("get result error. Fdc=%d Last status=%x Read bytes=%d\n",
1248 #define MORE_OUTPUT -2
1249 /* does the fdc need more output? */
1250 static int need_more_output(void)
1253 if ((status = wait_til_ready()) < 0)
1255 if ((status & (STATUS_READY | STATUS_DIR | STATUS_DMA)) == STATUS_READY)
1260 /* Set perpendicular mode as required, based on data rate, if supported.
1261 * 82077 Now tested. 1Mbps data rate only possible with 82077-1.
1263 static inline void perpendicular_mode(void)
1265 unsigned char perp_mode;
1267 if (raw_cmd->rate & 0x40) {
1268 switch (raw_cmd->rate & 3) {
1276 DPRINT("Invalid data rate for perpendicular mode!\n");
1278 FDCS->reset = 1; /* convenient way to return to
1279 * redo without to much hassle (deep
1286 if (FDCS->perp_mode == perp_mode)
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");
1295 } /* perpendicular_mode */
1297 static int fifo_depth = 0xa;
1300 static int fdc_configure(void)
1303 output_byte(FD_CONFIGURE);
1304 if (need_more_output() != MORE_OUTPUT)
1307 output_byte(0x10 | (no_fifo & 0x20) | (fifo_depth & 0xf));
1308 output_byte(0); /* pre-compensation from track
1313 #define NOMINAL_DTR 500
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.
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.
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
1328 * srt: 1000 to 16000 in microseconds
1329 * hut: 16 to 240 milliseconds
1330 * hlt: 2 to 254 milliseconds
1332 * These values are rounded up to the next highest available delay time.
1334 static void fdc_specify(void)
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;
1343 if (FDCS->need_configure && FDCS->version >= FDC_82072A) {
1345 FDCS->need_configure = 0;
1346 /*DPRINT("FIFO enabled\n"); */
1349 switch (raw_cmd->rate & 0x03) {
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));
1370 if (FDCS->version >= FDC_82072) {
1372 hlt_max_code = 0x00; /* 0==256msec*dtr0/dtr (not linear!) */
1373 hut_max_code = 0x0; /* 0==256msec*dtr0/dtr (not linear!) */
1376 /* Convert step rate from microseconds to milliseconds and 4 bits */
1377 srt = 16 - (DP->srt * scale_dtr / 1000 + NOMINAL_DTR - 1) / NOMINAL_DTR;
1384 hlt = (DP->hlt * scale_dtr / 2 + NOMINAL_DTR - 1) / NOMINAL_DTR;
1387 else if (hlt > 0x7f)
1390 hut = (DP->hut * scale_dtr / 16 + NOMINAL_DTR - 1) / NOMINAL_DTR;
1396 spec1 = (srt << 4) | hut;
1397 spec2 = (hlt << 1) | (use_virtual_dma & 1);
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);
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).
1412 static int fdc_dtr(void)
1414 /* If data rate not already set to desired value, set it. */
1415 if ((raw_cmd->rate & 3) == FDCS->dtr)
1419 fd_outb(raw_cmd->rate & 3, FD_DCR);
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)
1426 FDCS->dtr = raw_cmd->rate & 3;
1427 return (fd_wait_for_completion(jiffies + 2UL * HZ / 100,
1428 (timeout_fn) floppy_ready));
1431 static void tell_sector(void)
1433 printk(": track %d, head %d, sector %d, size %d",
1434 R_TRACK, R_HEAD, R_SECTOR, R_SIZECODE);
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.
1444 static int interpret_errors(void)
1449 DPRINT("-- FDC reply error");
1454 /* check IC to find cause of interrupt */
1455 switch (ST0 & ST0_INTR) {
1456 case 0x40: /* error occurred during command execution */
1458 return 0; /* occurs with pseudo-DMA */
1461 DPRINT("Drive is write protected\n");
1462 CLEARF(FD_DISK_WRITABLE);
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");
1471 } else if (*errors >= DP->max_errors.reporting) {
1473 if (ST0 & ST0_ECE) {
1474 printk("Recalibrate failed!");
1475 } else if (ST2 & ST2_CRC) {
1476 printk("data CRC error");
1478 } else if (ST1 & ST1_CRC) {
1479 printk("CRC error");
1481 } else if ((ST1 & (ST1_MAM | ST1_ND))
1482 || (ST2 & ST2_MAM)) {
1484 printk("sector not found");
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");
1494 ("unknown error. ST[0..2] are: 0x%x 0x%x 0x%x",
1501 if (ST2 & ST2_WC || ST2 & ST2_BC)
1502 /* wrong cylinder => recal */
1503 DRS->track = NEED_2_RECAL;
1505 case 0x80: /* invalid command given */
1506 DPRINT("Invalid FDC command given!\n");
1510 DPRINT("Abnormal termination caused by polling\n");
1513 default: /* (0) Normal command termination */
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).
1523 static void setup_rw_floppy(void)
1525 int i, r, flags, dflags;
1526 unsigned long ready_date;
1527 timeout_fn function;
1529 flags = raw_cmd->flags;
1530 if (flags & (FD_RAW_READ | FD_RAW_WRITE))
1531 flags |= FD_RAW_INTR;
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.
1539 if (time_after(ready_date, jiffies + DP->select_delay)) {
1540 ready_date -= DP->select_delay;
1541 function = (timeout_fn) floppy_start;
1543 function = (timeout_fn) setup_rw_floppy;
1545 /* wait until the floppy is spinning fast enough */
1546 if (fd_wait_for_completion(ready_date, function))
1549 dflags = DRS->flags;
1551 if ((flags & FD_RAW_READ) || (flags & FD_RAW_WRITE))
1554 if (flags & FD_RAW_INTR)
1555 do_floppy = main_command_interrupt;
1558 for (i = 0; i < raw_cmd->cmd_count; i++)
1559 r |= output_byte(raw_cmd->cmd[i]);
1561 debugt("rw_command: ");
1569 if (!(flags & FD_RAW_INTR)) {
1572 } else if (flags & FD_RAW_NEED_DISK)
1576 static int blind_seek;
1579 * This is the routine called after every seek (or recalibrate) interrupt
1580 * from the floppy controller.
1582 static void seek_interrupt(void)
1584 debugt("seek interrupt:");
1585 if (inr != 2 || (ST0 & 0xF8) != 0x20) {
1586 DPRINT("seek failed\n");
1587 DRS->track = NEED_2_RECAL;
1592 if (DRS->track >= 0 && DRS->track != ST1 && !blind_seek) {
1594 if (DP->flags & FD_DEBUG) {
1596 ("clearing NEWCHANGE flag because of effective seek\n");
1597 DPRINT("jiffies=%lu\n", jiffies);
1600 CLEARF(FD_DISK_NEWCHANGE); /* effective seek */
1601 DRS->select_date = jiffies;
1607 static void check_wp(void)
1609 if (TESTF(FD_VERIFY)) {
1610 /* check write protection */
1611 output_byte(FD_GETSTATUS);
1612 output_byte(UNIT(current_drive));
1613 if (result() != 1) {
1618 CLEARF(FD_NEED_TWADDLE);
1620 if (DP->flags & FD_DEBUG) {
1621 DPRINT("checking whether disk is write protected\n");
1622 DPRINT("wp=%x\n", ST3 & 0x40);
1626 SETF(FD_DISK_WRITABLE);
1628 CLEARF(FD_DISK_WRITABLE);
1632 static void seek_floppy(void)
1639 if (DP->flags & FD_DEBUG) {
1640 DPRINT("calling disk change from seek\n");
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
1650 SETF(FD_DISK_CHANGED);
1655 if (DRS->track <= NEED_1_RECAL) {
1656 recalibrate_floppy();
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? */
1664 track = raw_cmd->track - 1;
1666 if (DP->flags & FD_SILENT_DCL_CLEAR) {
1667 set_dor(fdc, ~(0x10 << UNIT(current_drive)), 0);
1669 raw_cmd->flags |= FD_RAW_NEED_SEEK;
1675 if (raw_cmd->track != DRS->track &&
1676 (raw_cmd->flags & FD_RAW_NEED_SEEK))
1677 track = raw_cmd->track;
1684 do_floppy = seek_interrupt;
1685 output_byte(FD_SEEK);
1686 output_byte(UNIT(current_drive));
1688 debugt("seek command:");
1691 static void recal_interrupt(void)
1693 debugt("recal interrupt:");
1696 else if (ST0 & ST0_ECE) {
1697 switch (DRS->track) {
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 :-) */
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 */
1716 if (DP->flags & FD_DEBUG) {
1718 ("clearing NEWCHANGE flag because of second recalibrate\n");
1722 CLEARF(FD_DISK_NEWCHANGE);
1723 DRS->select_date = jiffies;
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
1733 DRS->track = NEED_1_RECAL;
1741 static void print_result(char *message, int inr)
1745 DPRINT("%s ", message);
1747 for (i = 0; i < inr; i++)
1748 printk("repl[%d]=%x ", i, reply_buffer[i]);
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)
1755 void (*handler) (void) = do_floppy;
1759 lasthandler = handler;
1760 interruptjiffies = jiffies;
1762 f = claim_dma_lock();
1764 release_dma_lock(f);
1766 floppy_enable_hlt();
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");
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
1787 do_print = !handler && print_unex && !initialising;
1791 print_result("unexpected interrupt", inr);
1795 output_byte(FD_SENSEI);
1798 print_result("sensei", inr);
1800 } while ((ST0 & 0x83) != UNIT(current_drive) && inr == 2
1807 schedule_bh(handler);
1808 is_alive("normal interrupt end");
1810 /* FIXME! Was it really for us? */
1814 static void recalibrate_floppy(void)
1816 debugt("recalibrate floppy:");
1817 do_floppy = recal_interrupt;
1818 output_byte(FD_RECALIBRATE);
1819 LAST_OUT(UNIT(current_drive));
1823 * Must do 4 FD_SENSEIs after reset because of ``drive polling''.
1825 static void reset_interrupt(void)
1827 debugt("reset interrupt:");
1828 result(); /* get the status ready for set_fdc */
1830 printk("reset set in interrupt, calling %p\n", cont->error);
1831 cont->error(); /* a reset just after a reset. BAD! */
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)
1840 static void reset_fdc(void)
1842 unsigned long flags;
1844 do_floppy = reset_interrupt;
1848 /* Pseudo-DMA may intercept 'reset finished' interrupt. */
1849 /* Irrelevant for systems with true DMA (i386). */
1851 flags = claim_dma_lock();
1853 release_dma_lock(flags);
1855 if (FDCS->version >= FDC_82072A)
1856 fd_outb(0x80 | (FDCS->dtr & 3), FD_STATUS);
1858 fd_outb(FDCS->dor & ~0x04, FD_DOR);
1859 udelay(FD_RESET_DELAY);
1860 fd_outb(FDCS->dor, FD_DOR);
1864 static void show_floppy(void)
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,
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]);
1891 printk("status=%x\n", fd_inb(FD_STATUS));
1892 printk("fdc_busy=%lu\n", fdc_busy);
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);
1904 printk("cont=%p\n", cont);
1905 printk("current_req=%p\n", current_req);
1906 printk("command_status=%d\n", command_status);
1910 static void floppy_shutdown(unsigned long data)
1912 unsigned long flags;
1918 floppy_enable_hlt();
1920 flags = claim_dma_lock();
1922 release_dma_lock(flags);
1924 /* avoid dma going to a random drive after shutdown */
1927 DPRINT("floppy timeout called\n");
1931 cont->redo(); /* this will recall reset when needed */
1933 printk("no cont in shutdown!\n");
1934 process_fd_request();
1936 is_alive("floppy shutdown");
1939 /*typedef void (*timeout_fn)(unsigned long);*/
1941 /* start motor, check media-changed condition and write protection */
1942 static int start_motor(void (*function) (void))
1947 data = UNIT(current_drive);
1948 if (!(raw_cmd->flags & FD_RAW_NO_MOTOR)) {
1949 if (!(FDCS->dor & (0x10 << UNIT(current_drive)))) {
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));
1957 } else if (FDCS->dor & (0x10 << UNIT(current_drive)))
1958 mask &= ~(0x10 << UNIT(current_drive));
1960 /* starts motor and selects floppy */
1961 del_timer(motor_off_timer + current_drive);
1962 set_dor(fdc, mask, data);
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));
1969 static void floppy_ready(void)
1972 if (start_motor(floppy_ready))
1978 if (DP->flags & FD_DEBUG) {
1979 DPRINT("calling disk change from floppy_ready\n");
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
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);
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 ... */
2000 if ((raw_cmd->flags & FD_RAW_READ) ||
2001 (raw_cmd->flags & FD_RAW_WRITE))
2007 static void floppy_start(void)
2009 reschedule_timeout(current_reqD, "floppy start", 0);
2013 if (DP->flags & FD_DEBUG) {
2014 DPRINT("setting NEWCHANGE in floppy_start\n");
2017 SETF(FD_DISK_NEWCHANGE);
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
2028 * ========================================================================
2031 * General purpose continuations.
2032 * ==============================
2035 static void do_wakeup(void)
2037 reschedule_timeout(MAXTIMEOUT, "do wakeup", 0);
2039 command_status += 2;
2040 wake_up(&command_done);
2043 static struct cont_t wakeup_cont = {
2047 .done = (done_f) empty
2050 static struct cont_t intr_cont = {
2052 .redo = process_fd_request,
2054 .done = (done_f) empty
2057 static int wait_til_done(void (*handler) (void), int interruptible)
2061 schedule_bh(handler);
2063 if (command_status < 2 && NO_SIGNAL) {
2064 DECLARE_WAITQUEUE(wait, current);
2066 add_wait_queue(&command_done, &wait);
2068 set_current_state(interruptible ?
2069 TASK_INTERRUPTIBLE :
2070 TASK_UNINTERRUPTIBLE);
2072 if (command_status >= 2 || !NO_SIGNAL)
2075 is_alive("wait_til_done");
2080 set_current_state(TASK_RUNNING);
2081 remove_wait_queue(&command_done, &wait);
2084 if (command_status < 2) {
2092 command_status = FD_COMMAND_ERROR;
2093 if (command_status == FD_COMMAND_OKAY)
2097 command_status = FD_COMMAND_NONE;
2101 static void generic_done(int result)
2103 command_status = result;
2104 cont = &wakeup_cont;
2107 static void generic_success(void)
2112 static void generic_failure(void)
2117 static void success_and_wakeup(void)
2124 * formatting and rw support.
2125 * ==========================
2128 static int next_valid_format(void)
2132 probed_format = DRS->probed_format;
2134 if (probed_format >= 8 || !DP->autodetect[probed_format]) {
2135 DRS->probed_format = 0;
2138 if (floppy_type[DP->autodetect[probed_format]].sect) {
2139 DRS->probed_format = probed_format;
2146 static void bad_flp_intr(void)
2151 DRS->probed_format++;
2152 if (!next_valid_format())
2155 err_count = ++(*errors);
2156 INFBOUND(DRWE->badness, err_count);
2157 if (err_count > DP->max_errors.abort)
2159 if (err_count > DP->max_errors.reset)
2161 else if (err_count > DP->max_errors.recal)
2162 DRS->track = NEED_2_RECAL;
2165 static void set_floppy(int drive)
2167 int type = ITYPE(UDRS->fd_device);
2169 _floppy = floppy_type + type;
2171 _floppy = current_type[drive];
2175 * formatting support.
2176 * ===================
2178 static void format_interrupt(void)
2180 switch (interpret_errors()) {
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)
2197 unsigned char track, head, sect, size;
2198 } *here = (struct fparm *)floppy_track_buffer;
2200 int count, head_shift, track_shift;
2202 raw_cmd = &default_raw_cmd;
2203 raw_cmd->track = track;
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;
2216 raw_cmd->kernel_data = floppy_track_buffer;
2217 raw_cmd->length = 4 * F_SECT_PER_TRACK;
2219 /* allow for about 30ms for data transport per track */
2220 head_shift = (F_SECT_PER_TRACK + 5) / 6;
2222 /* a ``cylinder'' is two tracks plus a little stepping time */
2223 track_shift = 2 * head_shift + 3;
2225 /* position of logical sector 1 on this track */
2226 n = (track_shift * format_req.track + head_shift * format_req.head)
2229 /* determine interleave */
2231 if (_floppy->fmt_gap < 0x22)
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;
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 */
2247 if (n >= F_SECT_PER_TRACK) {
2248 n -= F_SECT_PER_TRACK;
2249 while (here[n].sect)
2254 if (_floppy->stretch & FD_ZEROBASED) {
2255 for (count = 0; count < F_SECT_PER_TRACK; count++)
2260 static void redo_format(void)
2263 setup_format_params(format_req.track << STRETCH(_floppy));
2265 debugt("queue format request");
2268 static struct cont_t format_cont = {
2269 .interrupt = format_interrupt,
2270 .redo = redo_format,
2271 .error = bad_flp_intr,
2272 .done = generic_done
2275 static int do_format(int drive, struct format_descr *tmp_format_req)
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();
2290 format_req = *tmp_format_req;
2292 cont = &format_cont;
2293 errors = &format_errors;
2295 process_fd_request();
2300 * Buffer read/write and support
2301 * =============================
2304 static void floppy_end_request(struct request *req, int uptodate)
2306 unsigned int nr_sectors = current_count_sectors;
2308 /* current_count_sectors can be zero if transfer failed */
2310 nr_sectors = req->current_nr_sectors;
2311 if (end_that_request_first(req, uptodate, nr_sectors))
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);
2318 /* We're done with the request */
2322 /* new request_done. Can handle physical sectors which are smaller than a
2324 static void request_done(int uptodate)
2326 struct request_queue *q = floppy_queue;
2327 struct request *req = current_req;
2328 unsigned long flags;
2332 reschedule_timeout(MAXTIMEOUT, "request done %d", uptodate);
2335 printk("floppy.c: no request in request_done\n");
2340 /* maintain values for invalidation on geometry
2342 block = current_count_sectors + req->sector;
2343 INFBOUND(DRS->maxblock, block);
2344 if (block > _floppy->sect)
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);
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;
2359 DRWE->last_error_sector = req->sector;
2360 DRWE->last_error_generation = DRS->generation;
2362 spin_lock_irqsave(q->queue_lock, flags);
2363 floppy_end_request(req, 0);
2364 spin_unlock_irqrestore(q->queue_lock, flags);
2368 /* Interrupt handler evaluating the result of the r/w operation */
2369 static void rw_interrupt(void)
2371 int nr_sectors, ssize, eoc, heads;
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) */
2380 if (!DRS->first_read_date)
2381 DRS->first_read_date = jiffies;
2396 nr_sectors = (((R_TRACK - TRACK) * heads +
2397 R_HEAD - HEAD) * SECT_PER_TRACK +
2398 R_SECTOR - SECTOR + eoc) << SIZECODE >> 2;
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,
2411 printk("in_sector_offset=%d\n", in_sector_offset);
2415 nr_sectors -= in_sector_offset;
2416 INFBOUND(nr_sectors, 0);
2417 SUPBOUND(current_count_sectors, nr_sectors);
2419 switch (interpret_errors()) {
2424 if (!current_count_sectors) {
2431 if (!current_count_sectors) {
2435 current_type[current_drive] = _floppy;
2436 floppy_sizes[TOMINOR(current_drive)] = _floppy->size;
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;
2449 if (CT(COMMAND) != FD_READ ||
2450 raw_cmd->kernel_data == current_req->buffer) {
2451 /* transfer directly from buffer */
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);
2461 /* Compute maximal contiguous buffer size. */
2462 static int buffer_chain_size(void)
2469 base = bio_data(current_req->bio);
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 !=
2485 /* Compute the maximal transfer size */
2486 static int transfer_size(int ssize, int max_sector, int max_size)
2488 SUPBOUND(max_sector, fsector_t + max_size);
2491 max_sector -= (max_sector % _floppy->sect) % ssize;
2493 /* transfer size, beginning not aligned */
2494 current_count_sectors = max_sector - fsector_t;
2500 * Move data from/to the track buffer to/from the buffer cache.
2502 static void copy_buffer(int ssize, int max_sector, int max_sector_2)
2504 int remaining; /* number of transferred 512-byte sectors */
2507 char *buffer, *dma_buffer;
2510 max_sector = transfer_size(ssize,
2511 min(max_sector, max_sector_2),
2512 current_req->nr_sectors);
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);
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);
2535 buffer_max = max(max_sector, buffer_max);
2537 dma_buffer = floppy_track_buffer + ((fsector_t - buffer_min) << 9);
2539 size = current_req->current_nr_sectors << 9;
2541 rq_for_each_bio(bio, current_req) {
2542 bio_for_each_segment(bv, bio, i) {
2547 SUPBOUND(size, remaining);
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 -
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)
2563 if (CT(COMMAND) == FD_WRITE)
2567 if (((unsigned long)buffer) % 512)
2568 DPRINT("%p buffer not aligned\n", buffer);
2570 if (CT(COMMAND) == FD_READ)
2571 memcpy(buffer, dma_buffer, size);
2573 memcpy(dma_buffer, buffer, size);
2579 #ifdef FLOPPY_SANITY_CHECK
2582 max_sector -= remaining >> 9;
2583 DPRINT("weirdness: remaining %d\n", remaining >> 9);
2589 static inline int check_dma_crossing(char *start,
2590 unsigned long length, char *message)
2592 if (CROSS_64KB(start, length)) {
2593 printk("DMA xfer crosses 64KB boundary in %s %p-%p\n",
2594 message, start, start + length);
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
2608 static void virtualdmabug_workaround(void)
2610 int hard_sectors, end_sector;
2612 if (CT(COMMAND) == FD_WRITE) {
2613 COMMAND &= ~0x80; /* switch off multiple track mode */
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);
2624 SECT_PER_TRACK = end_sector; /* make sure SECT_PER_TRACK points
2625 * to end of transfer */
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
2639 static int make_raw_rw_request(void)
2641 int aligned_sector_t;
2642 int max_sector, max_size, tracksize, ssize;
2644 if (max_buffer_sectors == 0) {
2645 printk("VFS: Block I/O scheduled on unopened device\n");
2649 set_fdc((long)current_req->rq_disk->private_data);
2651 raw_cmd = &default_raw_cmd;
2652 raw_cmd->flags = FD_RAW_SPIN | FD_RAW_NEED_DISK | FD_RAW_NEED_DISK |
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);
2662 DPRINT("make_raw_rw_request: unknown command\n");
2666 max_sector = _floppy->sect * _floppy->head;
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;
2677 HEAD = fsector_t / _floppy->sect;
2679 if (((_floppy->stretch & (FD_SWAPSIDES | FD_ZEROBASED)) ||
2680 TESTF(FD_NEED_TWADDLE)) && fsector_t < _floppy->sect)
2681 max_sector = _floppy->sect;
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);
2694 SIZECODE = FD_SIZECODE(_floppy);
2695 raw_cmd->rate = _floppy->rate & 0x43;
2696 if ((_floppy->rate & FD_2M) && (TRACK || HEAD) && raw_cmd->rate == 2)
2703 raw_cmd->track = TRACK << STRETCH(_floppy);
2704 DR_SELECT = UNIT(current_drive) + PH_HEAD(_floppy, HEAD);
2707 SECT_PER_TRACK = _floppy->sect << 2 >> SIZECODE;
2708 SECTOR = ((fsector_t % _floppy->sect) << 2 >> SIZECODE) +
2709 ((_floppy->stretch & FD_ZEROBASED) ? 0 : 1);
2711 /* tracksize describes the size which can be filled up with sectors
2714 tracksize = _floppy->sect - _floppy->sect % ssize;
2715 if (tracksize < _floppy->sect) {
2717 if (tracksize <= fsector_t % _floppy->sect)
2720 /* if we are beyond tracksize, fill up using smaller sectors */
2721 while (tracksize <= fsector_t % _floppy->sect) {
2722 while (tracksize + ssize > _floppy->sect) {
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;
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);
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;
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;
2765 transfer_size(ssize, max_sector,
2766 max_buffer_sectors * 2) - fsector_t;
2769 * Do NOT use minimum() here---MAX_DMA_ADDRESS is 64 bits wide
2770 * on a 64 bit machine!
2772 max_size = buffer_chain_size();
2775 ((unsigned long)current_req->buffer)) >> 9;
2776 if ((unsigned long)max_size > dma_limit) {
2777 max_size = dma_limit;
2779 /* 64 kb boundaries */
2780 if (CROSS_64KB(current_req->buffer, max_size << 9))
2782 ((unsigned long)current_req->buffer) %
2784 direct = transfer_size(ssize, max_sector, max_size) - fsector_t;
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.
2789 * This means we should be able to read a sector even if there
2790 * are other bad sectors on this track.
2793 (indirect * 2 > direct * 3 &&
2794 *errors < DP->max_errors.read_track &&
2795 /*!TESTF(FD_NEED_TWADDLE) && */
2797 || (DP->read_track & (1 << DRS->probed_format)))))) {
2798 max_size = current_req->nr_sectors;
2800 raw_cmd->kernel_data = current_req->buffer;
2801 raw_cmd->length = current_count_sectors << 9;
2802 if (raw_cmd->length == 0) {
2804 ("zero dma transfer attempted from make_raw_request\n");
2805 DPRINT("indirect=%d direct=%d fsector_t=%d",
2806 indirect, direct, fsector_t);
2809 /* check_dma_crossing(raw_cmd->kernel_data,
2811 "end of make_raw_request [1]");*/
2813 virtualdmabug_workaround();
2818 if (CT(COMMAND) == FD_READ)
2819 max_size = max_sector; /* unbounded */
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 */
2833 buffer_drive = current_drive;
2834 buffer_max = buffer_min = aligned_sector_t;
2836 raw_cmd->kernel_data = floppy_track_buffer +
2837 ((aligned_sector_t - buffer_min) << 9);
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");
2848 buffer_track = raw_cmd->track;
2849 buffer_drive = current_drive;
2850 copy_buffer(ssize, max_sector,
2851 2 * max_buffer_sectors + buffer_min);
2853 transfer_size(ssize, max_sector,
2854 2 * max_buffer_sectors + buffer_min -
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);
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)
2903 if (CT(COMMAND) == FD_WRITE)
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");
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);
2916 if (raw_cmd->length == 0) {
2917 DPRINT("zero dma transfer attempted from make_raw_request\n");
2922 virtualdmabug_workaround();
2926 static void redo_fd_request(void)
2928 #define REPEAT {request_done(0); continue; }
2933 if (current_drive < N_DRIVE)
2934 floppy_off(current_drive);
2938 struct request *req;
2940 spin_lock_irq(floppy_queue->queue_lock);
2941 req = elv_next_request(floppy_queue);
2942 spin_unlock_irq(floppy_queue->queue_lock);
2950 drive = (long)current_req->rq_disk->private_data;
2952 reschedule_timeout(current_reqD, "redo fd request", 0);
2955 raw_cmd = &default_raw_cmd;
2957 if (start_motor(redo_fd_request))
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");
2965 if (!_floppy) { /* Autodetection */
2967 DRS->probed_format = 0;
2968 if (next_valid_format()) {
2969 DPRINT("no autodetectable formats\n");
2976 floppy_type + DP->autodetect[DRS->probed_format];
2979 errors = &(current_req->errors);
2980 tmp = make_raw_rw_request();
2986 if (TESTF(FD_NEED_TWADDLE))
2988 schedule_bh(floppy_start);
2989 debugt("queue fd request");
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
3002 static void process_fd_request(void)
3005 schedule_bh(redo_fd_request);
3008 static void do_fd_request(request_queue_t * q)
3010 if (max_buffer_sectors == 0) {
3011 printk("VFS: do_fd_request called on non-open device\n");
3015 if (usage_count == 0) {
3016 printk("warning: usage count=0, current_req=%p exiting\n",
3018 printk("sect=%ld flags=%lx\n", (long)current_req->sector,
3019 current_req->flags);
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");
3028 lock_fdc(MAXTIMEOUT, 0);
3029 process_fd_request();
3030 is_alive("do fd request");
3033 static struct cont_t poll_cont = {
3034 .interrupt = success_and_wakeup,
3035 .redo = floppy_ready,
3036 .error = generic_failure,
3037 .done = generic_done
3040 static int poll_drive(int interruptible, int flag)
3043 /* no auto-sense, just clear dcl */
3044 raw_cmd = &default_raw_cmd;
3045 raw_cmd->flags = flag;
3047 raw_cmd->cmd_count = 0;
3050 if (DP->flags & FD_DEBUG) {
3051 DPRINT("setting NEWCHANGE in poll_drive\n");
3054 SETF(FD_DISK_NEWCHANGE);
3060 * User triggered reset
3061 * ====================
3064 static void reset_intr(void)
3066 printk("weird, reset interrupt called\n");
3069 static struct cont_t reset_cont = {
3070 .interrupt = reset_intr,
3071 .redo = success_and_wakeup,
3072 .error = generic_failure,
3073 .done = generic_done
3076 static int user_reset_fdc(int drive, int arg, int interruptible)
3081 LOCK_FDC(drive, interruptible);
3082 if (arg == FD_RESET_ALWAYS)
3088 process_fd_request();
3093 * Misc Ioctl's and support
3094 * ========================
3096 static inline int fd_copyout(void __user *param, const void *address,
3099 return copy_to_user(param, address, size) ? -EFAULT : 0;
3102 static inline int fd_copyin(void __user *param, void *address, unsigned long size)
3104 return copy_from_user(address, param, size) ? -EFAULT : 0;
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)
3110 #define COPYOUT(x) ECALL(_COPYOUT(x))
3111 #define COPYIN(x) ECALL(_COPYIN(x))
3113 static inline const char *drive_name(int type, int drive)
3115 struct floppy_struct *floppy;
3118 floppy = floppy_type + type;
3120 if (UDP->native_format)
3121 floppy = floppy_type + UDP->native_format;
3126 return floppy->name;
3132 static void raw_cmd_done(int flag)
3137 raw_cmd->flags |= FD_RAW_FAILURE;
3138 raw_cmd->flags |= FD_RAW_HARDFAILURE;
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];
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);
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;
3157 if (disk_change(current_drive))
3158 raw_cmd->flags |= FD_RAW_DISK_CHANGE;
3160 raw_cmd->flags &= ~FD_RAW_DISK_CHANGE;
3161 if (raw_cmd->flags & FD_RAW_NO_MOTOR_AFTER)
3162 motor_off_callback(current_drive);
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;
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
3183 static inline int raw_cmd_copyout(int cmd, char __user *param,
3184 struct floppy_raw_cmd *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)
3195 (ptr->data, ptr->kernel_data,
3196 ptr->buffer_length - ptr->length));
3203 static void raw_cmd_free(struct floppy_raw_cmd **ptr)
3205 struct floppy_raw_cmd *next, *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;
3221 static inline int raw_cmd_copyin(int cmd, char __user *param,
3222 struct floppy_raw_cmd **rcmd)
3224 struct floppy_raw_cmd *ptr;
3230 ptr = (struct floppy_raw_cmd *)
3231 kmalloc(sizeof(struct floppy_raw_cmd), GFP_USER);
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...
3250 for (i = 0; i < 16; i++)
3252 ptr->resultcode = 0;
3253 ptr->kernel_data = NULL;
3255 if (ptr->flags & (FD_RAW_READ | FD_RAW_WRITE)) {
3256 if (ptr->length <= 0)
3259 (char *)fd_dma_mem_alloc(ptr->length);
3260 fallback_on_nodma_alloc(&ptr->kernel_data, ptr->length);
3261 if (!ptr->kernel_data)
3263 ptr->buffer_length = ptr->length;
3265 if (ptr->flags & FD_RAW_WRITE)
3266 ECALL(fd_copyin(ptr->data, ptr->kernel_data,
3268 rcmd = &(ptr->next);
3269 if (!(ptr->flags & FD_RAW_MORE))
3275 static int raw_cmd_ioctl(int cmd, void __user *param)
3277 int drive, ret, ret2;
3278 struct floppy_raw_cmd *my_raw_cmd;
3280 if (FDCS->rawcmd <= 1)
3282 for (drive = 0; drive < N_DRIVE; drive++) {
3283 if (FDC(drive) != fdc)
3285 if (drive == current_drive) {
3286 if (UDRS->fd_ref > 1) {
3290 } else if (UDRS->fd_ref) {
3299 ret = raw_cmd_copyin(cmd, param, &my_raw_cmd);
3301 raw_cmd_free(&my_raw_cmd);
3305 raw_cmd = my_raw_cmd;
3306 cont = &raw_cmd_cont;
3307 ret = wait_til_done(floppy_start, 1);
3309 if (DP->flags & FD_DEBUG) {
3310 DPRINT("calling disk change from raw_cmd ioctl\n");
3314 if (ret != -EINTR && FDCS->reset)
3317 DRS->track = NO_TRACK;
3319 ret2 = raw_cmd_copyout(cmd, param, my_raw_cmd);
3322 raw_cmd_free(&my_raw_cmd);
3326 static int invalidate_drive(struct block_device *bdev)
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);
3335 static inline int set_geometry(unsigned int cmd, struct floppy_struct *g,
3336 int drive, int type, struct block_device *bdev)
3340 /* sanity checking for parameters. */
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)
3348 if (!capable(CAP_SYS_ADMIN))
3350 mutex_lock(&open_lock);
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)
3362 __invalidate_device(bdev);
3364 mutex_unlock(&open_lock);
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;
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 ||
3389 ((user_params[drive].sect ^ oldStretch) &
3390 (FD_SWAPSIDES | FD_ZEROBASED)))
3391 invalidate_drive(bdev);
3393 process_fd_request();
3398 /* handle obsolete ioctl's */
3399 static int ioctl_table[] = {
3427 static inline int normalize_ioctl(int *cmd, int *size)
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");
3445 static int get_floppy_geometry(int drive, int type, struct floppy_struct **g)
3448 *g = &floppy_type[type];
3451 CALL(poll_drive(0, 0));
3452 process_fd_request();
3453 *g = current_type[drive];
3460 static int fd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
3462 int drive = (long)bdev->bd_disk->private_data;
3463 int type = ITYPE(drive_state[drive].fd_device);
3464 struct floppy_struct *g;
3467 ret = get_floppy_geometry(drive, type, &g);
3471 geo->heads = g->head;
3472 geo->sectors = g->sect;
3473 geo->cylinders = g->track;
3477 static int fd_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
3478 unsigned long param)
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
3484 int drive = (long)inode->i_bdev->bd_disk->private_data;
3485 int i, type = ITYPE(UDRS->fd_device);
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 */
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");
3506 /* convert the old style command into a new style command */
3507 if ((cmd & 0xff00) == 0x0200) {
3508 ECALL(normalize_ioctl(&cmd, &size));
3512 /* permission checks */
3513 if (((cmd & 0x40) && !FD_IOCTL_ALLOWED) ||
3514 ((cmd & 0x80) && !capable(CAP_SYS_ADMIN)))
3518 CLEARSTRUCT(&inparam);
3519 if (_IOC_DIR(cmd) & _IOC_WRITE)
3520 ECALL(fd_copyin((void __user *)param, &inparam, size))
3524 if (UDRS->fd_ref != 1)
3525 /* somebody else has this drive open */
3529 /* do the actual eject. Fails on
3530 * non-Sparc architectures */
3531 ret = fd_eject(UNIT(drive));
3533 USETF(FD_DISK_CHANGED);
3535 process_fd_request();
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);
3545 return set_geometry(cmd, &inparam.g,
3546 drive, type, inode->i_bdev);
3548 ECALL(get_floppy_geometry(drive, type,
3549 (struct floppy_struct **)
3554 UDP->flags |= FTD_MSG;
3557 UDP->flags &= ~FTD_MSG;
3562 CALL(poll_drive(1, FD_RAW_NEED_DISK));
3564 process_fd_request();
3565 if (ret & FD_VERIFY)
3567 if (!(ret & FD_DISK_WRITABLE))
3571 if (UDRS->fd_ref != 1)
3573 return do_format(drive, &inparam.f);
3577 return invalidate_drive(inode->i_bdev);
3579 case FDSETEMSGTRESH:
3580 UDP->max_errors.reporting =
3581 (unsigned short)(param & 0x0f);
3583 OUT(FDGETMAXERRS, &UDP->max_errors);
3584 IN(FDSETMAXERRS, &UDP->max_errors, max_errors);
3587 outparam = drive_name(type, drive);
3588 SUPBOUND(size, strlen(outparam) + 1);
3591 IN(FDSETDRVPRM, UDP, dp);
3592 OUT(FDGETDRVPRM, UDP);
3596 CALL(poll_drive(1, FD_RAW_NEED_DISK));
3597 process_fd_request();
3599 OUT(FDGETDRVSTAT, UDRS);
3602 return user_reset_fdc(drive, (int)param, 1);
3604 OUT(FDGETFDCSTAT, UFDCS);
3609 OUT(FDWERRORGET, UDRWE);
3616 CALL(i = raw_cmd_ioctl(cmd, (void __user *)param));
3617 process_fd_request();
3623 process_fd_request();
3630 if (_IOC_DIR(cmd) & _IOC_READ)
3631 return fd_copyout((void __user *)param, outparam, size);
3638 static void __init config_types(void)
3643 /* read drive info out of physical CMOS */
3646 UDP->cmos = FLOPPY0_TYPE;
3648 if (!UDP->cmos && FLOPPY1_TYPE)
3649 UDP->cmos = FLOPPY1_TYPE;
3652 /* additional physical CMOS drive detection should go here */
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];
3660 if (type < ARRAY_SIZE(default_drive_params)) {
3661 params = &default_drive_params[type].params;
3663 name = default_drive_params[type].name;
3664 allowed_drive_mask |= 1 << drive;
3666 allowed_drive_mask &= ~(1 << drive);
3668 params = &default_drive_params[0].params;
3669 sprintf(temparea, "unknown type %d (usb?)", type);
3673 const char *prepend = ",";
3675 prepend = KERN_INFO "Floppy drive(s):";
3678 printk("%s fd%d is %s", prepend, drive, name);
3679 register_devfs_entries(drive);
3687 static int floppy_release(struct inode *inode, struct file *filp)
3689 int drive = (long)inode->i_bdev->bd_disk->private_data;
3691 mutex_lock(&open_lock);
3692 if (UDRS->fd_ref < 0)
3694 else if (!UDRS->fd_ref--) {
3695 DPRINT("floppy_release with fd_ref == 0");
3699 opened_bdev[drive] = NULL;
3700 floppy_release_irq_and_dma();
3701 mutex_unlock(&open_lock);
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.
3710 static int floppy_open(struct inode *inode, struct file *filp)
3712 int drive = (long)inode->i_bdev->bd_disk->private_data;
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)
3724 if (!UDRS->fd_ref && (UDP->flags & FD_BROKEN_DCL)) {
3725 USETF(FD_DISK_CHANGED);
3729 if (UDRS->fd_ref == -1 || (UDRS->fd_ref && (filp->f_flags & O_EXCL)))
3732 if (floppy_grab_irq_and_dma())
3735 if (filp->f_flags & O_EXCL)
3740 opened_bdev[drive] = inode->i_bdev;
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 */
3750 try = 32; /* Only 24 actually useful */
3752 tmp = (char *)fd_dma_mem_alloc(1024 * try);
3753 if (!tmp && !floppy_track_buffer) {
3754 try >>= 1; /* buffer only one side */
3756 tmp = (char *)fd_dma_mem_alloc(1024 * try);
3758 if (!tmp && !floppy_track_buffer) {
3759 fallback_on_nodma_alloc(&tmp, 2048 * try);
3761 if (!tmp && !floppy_track_buffer) {
3762 DPRINT("Unable to allocate DMA memory\n");
3765 if (floppy_track_buffer) {
3767 fd_dma_mem_free((unsigned long)tmp, try * 1024);
3769 buffer_min = buffer_max = -1;
3770 floppy_track_buffer = tmp;
3771 max_buffer_sectors = try;
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)
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;
3788 if (UFDCS->rawcmd == 1)
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))
3799 if ((filp->f_mode & 2) && !(UTESTF(FD_DISK_WRITABLE)))
3802 mutex_unlock(&open_lock);
3805 if (UDRS->fd_ref < 0)
3810 opened_bdev[drive] = NULL;
3811 floppy_release_irq_and_dma();
3813 mutex_unlock(&open_lock);
3818 * Check if the disk has been changed or if a change has been faked.
3820 static int check_floppy_change(struct gendisk *disk)
3822 int drive = (long)disk->private_data;
3824 if (UTESTF(FD_DISK_CHANGED) || UTESTF(FD_VERIFY))
3827 if (time_after(jiffies, UDRS->last_checked + UDP->checkfreq)) {
3828 if (floppy_grab_irq_and_dma()) {
3834 process_fd_request();
3835 floppy_release_irq_and_dma();
3838 if (UTESTF(FD_DISK_CHANGED) ||
3839 UTESTF(FD_VERIFY) ||
3840 test_bit(drive, &fake_change) ||
3841 (!ITYPE(UDRS->fd_device) && !current_type[drive]))
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.
3852 static int floppy_rb0_complete(struct bio *bio, unsigned int bytes_done,
3858 complete((struct completion *)bio->bi_private);
3862 static int __floppy_read_block_0(struct block_device *bdev)
3865 struct bio_vec bio_vec;
3866 struct completion complete;
3870 page = alloc_page(GFP_NOIO);
3872 process_fd_request();
3876 size = bdev->bd_block_size;
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;
3890 init_completion(&complete);
3891 bio.bi_private = &complete;
3892 bio.bi_end_io = floppy_rb0_complete;
3894 submit_bio(READ, &bio);
3895 generic_unplug_device(bdev_get_queue(bdev));
3896 process_fd_request();
3897 wait_for_completion(&complete);
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)
3910 int drive = (long)disk->private_data;
3911 #define NO_GEOM (!current_type[drive] && !ITYPE(UDRS->fd_device))
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");
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 */
3929 if (buffer_drive == drive)
3931 clear_bit(drive, &fake_change);
3932 UCLEARF(FD_DISK_CHANGED);
3937 res = __floppy_read_block_0(opened_bdev[drive]);
3940 poll_drive(0, FD_RAW_NEED_DISK);
3941 process_fd_request();
3944 set_capacity(disk, floppy_sizes[UDRS->fd_device]);
3948 static struct block_device_operations floppy_fops = {
3949 .owner = THIS_MODULE,
3950 .open = floppy_open,
3951 .release = floppy_release,
3953 .getgeo = fd_getgeo,
3954 .media_changed = check_floppy_change,
3955 .revalidate_disk = floppy_revalidate,
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",
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 };
3972 static void __init register_devfs_entries(int drive)
3974 int base_minor = (drive < 4) ? drive : (124 + drive);
3976 if (UDP->cmos < ARRAY_SIZE(default_drive_params)) {
3979 int minor = base_minor + (table_sup[UDP->cmos][i] << 2);
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++]);
3990 * Floppy Driver initialization
3991 * =============================
3994 /* Determine the floppy disk controller type */
3995 /* This routine was written by David C. Niemi */
3996 static char __init get_fdc_version(void)
4000 output_byte(FD_DUMPREGS); /* 82072 and better know DUMPREGS */
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 */
4011 ("FDC %d init: DUMPREGS: unexpected return of %d bytes.\n",
4016 if (!fdc_configure()) {
4017 printk(KERN_INFO "FDC %d is an 82072\n", fdc);
4018 return FDC_82072; /* 82072 doesn't know CONFIGURE */
4021 output_byte(FD_PERPENDICULAR);
4022 if (need_more_output() == MORE_OUTPUT) {
4025 printk(KERN_INFO "FDC %d is an 82072A\n", fdc);
4026 return FDC_82072A; /* 82072A as found on Sparcs. */
4029 output_byte(FD_UNLOCK);
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
4036 if ((r != 1) || (reply_buffer[0] != 0x00)) {
4037 printk("FDC %d init: UNLOCK: unexpected return of %d bytes.\n",
4041 output_byte(FD_PARTID);
4044 printk("FDC %d init: PARTID: unexpected return of %d bytes.\n",
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 */
4052 switch (reply_buffer[0] >> 5) {
4054 /* Either a 82078-1 or a 82078SL running at 5Volt */
4055 printk(KERN_INFO "FDC %d is an 82078.\n", fdc);
4058 printk(KERN_INFO "FDC %d is a 44pin 82078\n", fdc);
4061 printk(KERN_INFO "FDC %d is a S82078B\n", fdc);
4064 printk(KERN_INFO "FDC %d is a National Semiconductor PC87306\n",
4069 "FDC %d init: 82078 variant with unknown PARTID=%d.\n",
4070 fdc, reply_buffer[0] >> 5);
4071 return FDC_82078_UNKN;
4073 } /* get_fdc_version */
4075 /* lilo configuration */
4077 static void __init floppy_set_flags(int *ints, int param, int param2)
4081 for (i = 0; i < ARRAY_SIZE(default_drive_params); i++) {
4083 default_drive_params[i].params.flags |= param2;
4085 default_drive_params[i].params.flags &= ~param2;
4087 DPRINT("%s flag 0x%x\n", param2 ? "Setting" : "Clearing", param);
4090 static void __init daring(int *ints, int param, int param2)
4094 for (i = 0; i < ARRAY_SIZE(default_drive_params); i++) {
4096 default_drive_params[i].params.select_delay = 0;
4097 default_drive_params[i].params.flags |=
4098 FD_SILENT_DCL_CLEAR;
4100 default_drive_params[i].params.select_delay =
4102 default_drive_params[i].params.flags &=
4103 ~FD_SILENT_DCL_CLEAR;
4106 DPRINT("Assuming %s floppy hardware\n", param ? "standard" : "broken");
4109 static void __init set_cmos(int *ints, int dummy, int dummy2)
4111 int current_drive = 0;
4114 DPRINT("wrong number of parameters for CMOS\n");
4117 current_drive = ints[1];
4118 if (current_drive < 0 || current_drive >= 8) {
4119 DPRINT("bad drive for set_cmos\n");
4123 if (current_drive >= 4 && !FDC2)
4127 DPRINT("setting CMOS code to %d\n", ints[2]);
4130 static struct param_table {
4132 void (*fn) (int *ints, int param, 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},
4144 {"two_fdc", NULL, &FDC2, 0x370, 0},
4145 {"one_fdc", NULL, &FDC2, 0, 0},
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}
4167 static int __init floppy_setup(char *str)
4173 str = get_options(str, ARRAY_SIZE(ints), ints);
4175 for (i = 0; i < ARRAY_SIZE(config_params); i++) {
4176 if (strcmp(str, config_params[i].name) == 0) {
4180 param = config_params[i].def_param;
4181 if (config_params[i].fn)
4184 config_params[i].param2);
4185 if (config_params[i].var) {
4186 DPRINT("%s=%d\n", str, param);
4187 *config_params[i].var = param;
4194 DPRINT("unknown floppy option [%s]\n", str);
4196 DPRINT("allowed options are:");
4197 for (i = 0; i < ARRAY_SIZE(config_params); i++)
4198 printk(" %s", config_params[i].name);
4201 DPRINT("botched floppy option\n");
4202 DPRINT("Read Documentation/floppy.txt\n");
4206 static int have_no_fdc = -ENODEV;
4208 static ssize_t floppy_cmos_show(struct device *dev,
4209 struct device_attribute *attr, char *buf)
4211 struct platform_device *p;
4214 p = container_of(dev, struct platform_device,dev);
4216 return sprintf(buf, "%X\n", UDP->cmos);
4218 DEVICE_ATTR(cmos,S_IRUGO,floppy_cmos_show,NULL);
4220 static void floppy_device_release(struct device *dev)
4222 complete(&device_release);
4225 static struct platform_device floppy_device[N_DRIVE];
4227 static struct kobject *floppy_find(dev_t dev, int *part, void *data)
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)
4234 if (((*part >> 2) & 0x1f) >= ARRAY_SIZE(floppy_type))
4237 return get_disk(disks[drive]);
4240 static int __init floppy_init(void)
4247 for (dr = 0; dr < N_DRIVE; dr++) {
4248 disks[dr] = alloc_disk(1);
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);
4259 init_timer(&motor_off_timer[dr]);
4260 motor_off_timer[dr].data = dr;
4261 motor_off_timer[dr].function = motor_off_callback;
4264 devfs_mk_dir("floppy");
4266 err = register_blkdev(FLOPPY_MAJOR, "fd");
4268 goto out_devfs_remove;
4270 floppy_queue = blk_init_queue(do_fd_request, &floppy_lock);
4271 if (!floppy_queue) {
4273 goto out_unreg_blkdev;
4275 blk_queue_max_sectors(floppy_queue, 64);
4277 blk_register_region(MKDEV(FLOPPY_MAJOR, 0), 256, THIS_MODULE,
4278 floppy_find, NULL, NULL);
4280 for (i = 0; i < 256; i++)
4282 floppy_sizes[i] = floppy_type[ITYPE(i)].size;
4284 floppy_sizes[i] = MAX_DISK_SIZE << 1;
4286 reschedule_timeout(MAXTIMEOUT, "floppy init", MAXTIMEOUT);
4289 for (i = 0; i < N_FDC; i++) {
4294 #if defined(__sparc__) || defined(__mc68000__)
4295 /*sparcs/sun3x don't have a DOR reset which we can fall back on to */
4299 FDCS->version = FDC_82072A;
4303 use_virtual_dma = can_use_virtual_dma & 1;
4304 #if defined(CONFIG_PPC64)
4305 if (check_legacy_ioport(FDC1)) {
4306 del_timer(&fd_timeout);
4308 goto out_unreg_region;
4311 fdc_state[0].address = FDC1;
4312 if (fdc_state[0].address == -1) {
4313 del_timer(&fd_timeout);
4315 goto out_unreg_region;
4318 fdc_state[1].address = FDC2;
4321 fdc = 0; /* reset fdc in case of unexpected interrupt */
4322 err = floppy_grab_irq_and_dma();
4324 del_timer(&fd_timeout);
4326 goto out_unreg_region;
4329 /* initialise drive state */
4330 for (drive = 0; drive < N_DRIVE; drive++) {
4333 USETF(FD_DISK_NEWCHANGE);
4334 USETF(FD_DISK_CHANGED);
4336 UDRS->fd_device = -1;
4337 floppy_track_buffer = NULL;
4338 max_buffer_sectors = 0;
4341 * Small 10 msec delay to let through any interrupt that
4342 * initialization might have triggered, to not
4343 * confuse detection:
4347 for (i = 0; i < N_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)
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);
4360 FDCS->version = FDC_NONE;
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);
4372 if (can_use_virtual_dma == 2 && FDCS->version < FDC_82072A)
4373 can_use_virtual_dma = 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.
4380 user_reset_fdc(-1, FD_RESET_ALWAYS, 0);
4383 del_timer(&fd_timeout);
4385 floppy_release_irq_and_dma();
4388 DPRINT("no floppy controllers found\n");
4390 goto out_flush_work;
4393 for (drive = 0; drive < N_DRIVE; drive++) {
4394 if (!(allowed_drive_mask & (1 << drive)))
4396 if (fdc_state[FDC(drive)].version == FDC_NONE)
4399 floppy_device[drive].name = floppy_device_name;
4400 floppy_device[drive].id = drive;
4401 floppy_device[drive].dev.release = floppy_device_release;
4403 err = platform_device_register(&floppy_device[drive]);
4405 goto out_flush_work;
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]);
4419 flush_scheduled_work();
4421 floppy_release_irq_and_dma();
4423 blk_unregister_region(MKDEV(FLOPPY_MAJOR, 0), 256);
4424 blk_cleanup_queue(floppy_queue);
4426 unregister_blkdev(FLOPPY_MAJOR, "fd");
4428 devfs_remove("floppy");
4431 del_timer(&motor_off_timer[dr]);
4432 put_disk(disks[dr]);
4437 static DEFINE_SPINLOCK(floppy_usage_lock);
4439 static int floppy_grab_irq_and_dma(void)
4441 unsigned long flags;
4443 spin_lock_irqsave(&floppy_usage_lock, flags);
4444 if (usage_count++) {
4445 spin_unlock_irqrestore(&floppy_usage_lock, flags);
4448 spin_unlock_irqrestore(&floppy_usage_lock, flags);
4451 * We might have scheduled a free_irq(), wait it to
4454 flush_scheduled_work();
4456 if (fd_request_irq()) {
4457 DPRINT("Unable to grab IRQ%d for the floppy driver\n",
4459 spin_lock_irqsave(&floppy_usage_lock, flags);
4461 spin_unlock_irqrestore(&floppy_usage_lock, flags);
4464 if (fd_request_dma()) {
4465 DPRINT("Unable to grab DMA%d for the floppy driver\n",
4468 spin_lock_irqsave(&floppy_usage_lock, flags);
4470 spin_unlock_irqrestore(&floppy_usage_lock, flags);
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",
4481 if (!request_region(FDCS->address + 7, 1, "floppy DIR")) {
4482 DPRINT("Floppy io-port 0x%04lx in use\n",
4486 /* address + 6 is reserved, and may be taken by IDE.
4487 * Unfortunately, Adaptec doesn't know this :-(, */
4490 for (fdc = 0; fdc < N_FDC; fdc++) {
4491 if (FDCS->address != -1) {
4493 fd_outb(FDCS->dor, FD_DOR);
4497 set_dor(0, ~0, 8); /* avoid immediate interrupt */
4499 for (fdc = 0; fdc < N_FDC; fdc++)
4500 if (FDCS->address != -1)
4501 fd_outb(FDCS->dor, FD_DOR);
4503 * The driver will try and free resources and relies on us
4504 * to know if they were allocated or not.
4507 irqdma_allocated = 1;
4510 release_region(FDCS->address + 2, 4);
4514 while (--fdc >= 0) {
4515 release_region(FDCS->address + 2, 4);
4516 release_region(FDCS->address + 7, 1);
4518 spin_lock_irqsave(&floppy_usage_lock, flags);
4520 spin_unlock_irqrestore(&floppy_usage_lock, flags);
4524 static void floppy_release_irq_and_dma(void)
4527 #ifdef FLOPPY_SANITY_CHECK
4533 unsigned long tmpaddr;
4534 unsigned long flags;
4536 spin_lock_irqsave(&floppy_usage_lock, flags);
4537 if (--usage_count) {
4538 spin_unlock_irqrestore(&floppy_usage_lock, flags);
4541 spin_unlock_irqrestore(&floppy_usage_lock, flags);
4542 if (irqdma_allocated) {
4545 schedule_work(&fd_free_irq_work);
4546 irqdma_allocated = 0;
4552 floppy_enable_hlt();
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);
4562 #ifdef FLOPPY_SANITY_CHECK
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);
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");
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);
4587 static char *floppy;
4589 static void unregister_devfs_entries(int drive)
4593 if (UDP->cmos < ARRAY_SIZE(default_drive_params)) {
4596 devfs_remove("floppy/%d%s", drive,
4597 table[table_sup[UDP->cmos][i]]);
4598 } while (table_sup[UDP->cmos][i++]);
4602 static void __init parse_floppy_cfg_string(char *cfg)
4607 for (ptr = cfg; *cfg && *cfg != ' ' && *cfg != '\t'; cfg++) ;
4617 int __init init_module(void)
4620 parse_floppy_cfg_string(floppy);
4621 return floppy_init();
4624 void cleanup_module(void)
4628 init_completion(&device_release);
4629 blk_unregister_region(MKDEV(FLOPPY_MAJOR, 0), 256);
4630 unregister_blkdev(FLOPPY_MAJOR, "fd");
4632 for (drive = 0; drive < N_DRIVE; drive++) {
4633 del_timer_sync(&motor_off_timer[drive]);
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]);
4642 put_disk(disks[drive]);
4644 devfs_remove("floppy");
4646 del_timer_sync(&fd_timeout);
4647 del_timer_sync(&fd_timer);
4648 blk_cleanup_queue(floppy_queue);
4651 floppy_release_irq_and_dma();
4653 /* eject disk, if any */
4656 flush_scheduled_work(); /* fd_free_irq() might be pending */
4658 wait_for_completion(&device_release);
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");
4670 __setup("floppy=", floppy_setup);
4671 module_init(floppy_init)
4674 MODULE_ALIAS_BLOCKDEV_MAJOR(FLOPPY_MAJOR);