GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / block / floppy.c
blob6d968d69dd6bc3f69cc4915f9edb4266fb8170a1
1 /*
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
7 */
9 /*
10 * 02.12.91 - Changed to static variables to indicate need for reset
11 * and recalibrate. This makes some things easier (output_byte reset
12 * checking etc), and means less interrupt jumping in case of errors,
13 * so the code is hopefully easier to understand.
17 * This file is certainly a mess. I've tried my best to get it working,
18 * but I don't like programming floppies, and I have only one anyway.
19 * Urgel. I should check for more errors, and do more graceful error
20 * recovery. Seems there are problems with several drives. I've tried to
21 * correct them. No promises.
25 * As with hd.c, all routines within this file can (and will) be called
26 * by interrupts, so extreme caution is needed. A hardware interrupt
27 * handler may not sleep, or a kernel panic will happen. Thus I cannot
28 * call "floppy-on" directly, but have to set a special timer interrupt
29 * etc.
33 * 28.02.92 - made track-buffering routines, based on the routines written
34 * by entropy@wintermute.wpi.edu (Lawrence Foard). Linus.
38 * Automatic floppy-detection and formatting written by Werner Almesberger
39 * (almesber@nessie.cs.id.ethz.ch), who also corrected some problems with
40 * the floppy-change signal detection.
44 * 1992/7/22 -- Hennus Bergman: Added better error reporting, fixed
45 * FDC data overrun bug, added some preliminary stuff for vertical
46 * recording support.
48 * 1992/9/17: Added DMA allocation & DMA functions. -- hhb.
50 * TODO: Errors are still not counted properly.
53 /* 1992/9/20
54 * Modifications for ``Sector Shifting'' by Rob Hooft (hooft@chem.ruu.nl)
55 * modeled after the freeware MS-DOS program fdformat/88 V1.8 by
56 * Christoph H. Hochst\"atter.
57 * I have fixed the shift values to the ones I always use. Maybe a new
58 * ioctl() should be created to be able to modify them.
59 * There is a bug in the driver that makes it impossible to format a
60 * floppy as the first thing after bootup.
64 * 1993/4/29 -- Linus -- cleaned up the timer handling in the kernel, and
65 * this helped the floppy driver as well. Much cleaner, and still seems to
66 * work.
69 /* 1994/6/24 --bbroad-- added the floppy table entries and made
70 * minor modifications to allow 2.88 floppies to be run.
73 /* 1994/7/13 -- Paul Vojta -- modified the probing code to allow three or more
74 * disk types.
78 * 1994/8/8 -- Alain Knaff -- Switched to fdpatch driver: Support for bigger
79 * format bug fixes, but unfortunately some new bugs too...
82 /* 1994/9/17 -- Koen Holtman -- added logging of physical floppy write
83 * errors to allow safe writing by specialized programs.
86 /* 1995/4/24 -- Dan Fandrich -- added support for Commodore 1581 3.5" disks
87 * by defining bit 1 of the "stretch" parameter to mean put sectors on the
88 * opposite side of the disk, leaving the sector IDs alone (i.e. Commodore's
89 * drives are "upside-down").
93 * 1995/8/26 -- Andreas Busse -- added Mips support.
97 * 1995/10/18 -- Ralf Baechle -- Portability cleanup; move machine dependent
98 * features to asm/floppy.h.
102 * 1998/1/21 -- Richard Gooch <rgooch@atnf.csiro.au> -- devfs support
106 * 1998/05/07 -- Russell King -- More portability cleanups; moved definition of
107 * interrupt and dma channel to asm/floppy.h. Cleaned up some formatting &
108 * use of '0' for NULL.
112 * 1998/06/07 -- Alan Cox -- Merged the 2.0.34 fixes for resource allocation
113 * failures.
117 * 1998/09/20 -- David Weinehall -- Added slow-down code for buggy PS/2-drives.
121 * 1999/08/13 -- Paul Slootman -- floppy stopped working on Alpha after 24
122 * days, 6 hours, 32 minutes and 32 seconds (i.e. MAXINT jiffies; ints were
123 * being used to store jiffies, which are unsigned longs).
127 * 2000/08/28 -- Arnaldo Carvalho de Melo <acme@conectiva.com.br>
128 * - get rid of check_region
129 * - s/suser/capable/
133 * 2001/08/26 -- Paul Gortmaker - fix insmod oops on machines with no
134 * floppy controller (lingering task on list after module is gone... boom.)
138 * 2002/02/07 -- Anton Altaparmakov - Fix io ports reservation to correct range
139 * (0x3f2-0x3f5, 0x3f7). This fix is a bit of a hack but the proper fix
140 * requires many non-obvious changes in arch dependent code.
143 /* 2003/07/28 -- Daniele Bellucci <bellucda@tiscali.it>.
144 * Better audit of register_blkdev.
147 #undef FLOPPY_SILENT_DCL_CLEAR
149 #define REALLY_SLOW_IO
151 #define DEBUGT 2
153 #define DPRINT(format, args...) \
154 pr_info("floppy%d: " format, current_drive, ##args)
156 #define DCL_DEBUG /* debug disk change line */
157 #ifdef DCL_DEBUG
158 #define debug_dcl(test, fmt, args...) \
159 do { if ((test) & FD_DEBUG) DPRINT(fmt, ##args); } while (0)
160 #else
161 #define debug_dcl(test, fmt, args...) \
162 do { if (0) DPRINT(fmt, ##args); } while (0)
163 #endif
165 /* do print messages for unexpected interrupts */
166 static int print_unex = 1;
167 #include <linux/module.h>
168 #include <linux/sched.h>
169 #include <linux/fs.h>
170 #include <linux/kernel.h>
171 #include <linux/timer.h>
172 #include <linux/workqueue.h>
173 #define FDPATCHES
174 #include <linux/fdreg.h>
175 #include <linux/fd.h>
176 #include <linux/hdreg.h>
177 #include <linux/errno.h>
178 #include <linux/slab.h>
179 #include <linux/mm.h>
180 #include <linux/bio.h>
181 #include <linux/smp_lock.h>
182 #include <linux/string.h>
183 #include <linux/jiffies.h>
184 #include <linux/fcntl.h>
185 #include <linux/delay.h>
186 #include <linux/mc146818rtc.h> /* CMOS defines */
187 #include <linux/ioport.h>
188 #include <linux/interrupt.h>
189 #include <linux/init.h>
190 #include <linux/platform_device.h>
191 #include <linux/mod_devicetable.h>
192 #include <linux/buffer_head.h> /* for invalidate_buffers() */
193 #include <linux/mutex.h>
194 #include <linux/io.h>
195 #include <linux/uaccess.h>
198 * PS/2 floppies have much slower step rates than regular floppies.
199 * It's been recommended that take about 1/4 of the default speed
200 * in some more extreme cases.
202 static int slow_floppy;
204 #include <asm/dma.h>
205 #include <asm/irq.h>
206 #include <asm/system.h>
208 static int FLOPPY_IRQ = 6;
209 static int FLOPPY_DMA = 2;
210 static int can_use_virtual_dma = 2;
211 /* =======
212 * can use virtual DMA:
213 * 0 = use of virtual DMA disallowed by config
214 * 1 = use of virtual DMA prescribed by config
215 * 2 = no virtual DMA preference configured. By default try hard DMA,
216 * but fall back on virtual DMA when not enough memory available
219 static int use_virtual_dma;
220 /* =======
221 * use virtual DMA
222 * 0 using hard DMA
223 * 1 using virtual DMA
224 * This variable is set to virtual when a DMA mem problem arises, and
225 * reset back in floppy_grab_irq_and_dma.
226 * It is not safe to reset it in other circumstances, because the floppy
227 * driver may have several buffers in use at once, and we do currently not
228 * record each buffers capabilities
231 static DEFINE_SPINLOCK(floppy_lock);
233 static unsigned short virtual_dma_port = 0x3f0;
234 irqreturn_t floppy_interrupt(int irq, void *dev_id);
235 static int set_dor(int fdc, char mask, char data);
237 #define K_64 0x10000 /* 64KB */
239 /* the following is the mask of allowed drives. By default units 2 and
240 * 3 of both floppy controllers are disabled, because switching on the
241 * motor of these drives causes system hangs on some PCI computers. drive
242 * 0 is the low bit (0x1), and drive 7 is the high bit (0x80). Bits are on if
243 * a drive is allowed.
245 * NOTE: This must come before we include the arch floppy header because
246 * some ports reference this variable from there. -DaveM
249 static int allowed_drive_mask = 0x33;
251 #include <asm/floppy.h>
253 static int irqdma_allocated;
255 #include <linux/blkdev.h>
256 #include <linux/blkpg.h>
257 #include <linux/cdrom.h> /* for the compatibility eject ioctl */
258 #include <linux/completion.h>
260 static struct request *current_req;
261 static struct request_queue *floppy_queue;
262 static void do_fd_request(struct request_queue *q);
264 #ifndef fd_get_dma_residue
265 #define fd_get_dma_residue() get_dma_residue(FLOPPY_DMA)
266 #endif
268 /* Dma Memory related stuff */
270 #ifndef fd_dma_mem_free
271 #define fd_dma_mem_free(addr, size) free_pages(addr, get_order(size))
272 #endif
274 #ifndef fd_dma_mem_alloc
275 #define fd_dma_mem_alloc(size) __get_dma_pages(GFP_KERNEL, get_order(size))
276 #endif
278 static inline void fallback_on_nodma_alloc(char **addr, size_t l)
280 #ifdef FLOPPY_CAN_FALLBACK_ON_NODMA
281 if (*addr)
282 return; /* we have the memory */
283 if (can_use_virtual_dma != 2)
284 return; /* no fallback allowed */
285 pr_info("DMA memory shortage. Temporarily falling back on virtual DMA\n");
286 *addr = (char *)nodma_mem_alloc(l);
287 #else
288 return;
289 #endif
292 /* End dma memory related stuff */
294 static unsigned long fake_change;
295 static bool initialized;
297 #define ITYPE(x) (((x) >> 2) & 0x1f)
298 #define TOMINOR(x) ((x & 3) | ((x & 4) << 5))
299 #define UNIT(x) ((x) & 0x03) /* drive on fdc */
300 #define FDC(x) (((x) & 0x04) >> 2) /* fdc of drive */
301 /* reverse mapping from unit and fdc to drive */
302 #define REVDRIVE(fdc, unit) ((unit) + ((fdc) << 2))
304 #define DP (&drive_params[current_drive])
305 #define DRS (&drive_state[current_drive])
306 #define DRWE (&write_errors[current_drive])
307 #define FDCS (&fdc_state[fdc])
309 #define UDP (&drive_params[drive])
310 #define UDRS (&drive_state[drive])
311 #define UDRWE (&write_errors[drive])
312 #define UFDCS (&fdc_state[FDC(drive)])
314 #define PH_HEAD(floppy, head) (((((floppy)->stretch & 2) >> 1) ^ head) << 2)
315 #define STRETCH(floppy) ((floppy)->stretch & FD_STRETCH)
317 /* read/write */
318 #define COMMAND (raw_cmd->cmd[0])
319 #define DR_SELECT (raw_cmd->cmd[1])
320 #define TRACK (raw_cmd->cmd[2])
321 #define HEAD (raw_cmd->cmd[3])
322 #define SECTOR (raw_cmd->cmd[4])
323 #define SIZECODE (raw_cmd->cmd[5])
324 #define SECT_PER_TRACK (raw_cmd->cmd[6])
325 #define GAP (raw_cmd->cmd[7])
326 #define SIZECODE2 (raw_cmd->cmd[8])
327 #define NR_RW 9
329 /* format */
330 #define F_SIZECODE (raw_cmd->cmd[2])
331 #define F_SECT_PER_TRACK (raw_cmd->cmd[3])
332 #define F_GAP (raw_cmd->cmd[4])
333 #define F_FILL (raw_cmd->cmd[5])
334 #define NR_F 6
337 * Maximum disk size (in kilobytes).
338 * This default is used whenever the current disk size is unknown.
339 * [Now it is rather a minimum]
341 #define MAX_DISK_SIZE 4 /* 3984 */
344 * globals used by 'result()'
346 #define MAX_REPLIES 16
347 static unsigned char reply_buffer[MAX_REPLIES];
348 static int inr; /* size of reply buffer, when called from interrupt */
349 #define ST0 (reply_buffer[0])
350 #define ST1 (reply_buffer[1])
351 #define ST2 (reply_buffer[2])
352 #define ST3 (reply_buffer[0]) /* result of GETSTATUS */
353 #define R_TRACK (reply_buffer[3])
354 #define R_HEAD (reply_buffer[4])
355 #define R_SECTOR (reply_buffer[5])
356 #define R_SIZECODE (reply_buffer[6])
358 #define SEL_DLY (2 * HZ / 100)
361 * this struct defines the different floppy drive types.
363 static struct {
364 struct floppy_drive_params params;
365 const char *name; /* name printed while booting */
366 } default_drive_params[] = {
367 /* NOTE: the time values in jiffies should be in msec!
368 CMOS drive type
369 | Maximum data rate supported by drive type
370 | | Head load time, msec
371 | | | Head unload time, msec (not used)
372 | | | | Step rate interval, usec
373 | | | | | Time needed for spinup time (jiffies)
374 | | | | | | Timeout for spinning down (jiffies)
375 | | | | | | | Spindown offset (where disk stops)
376 | | | | | | | | Select delay
377 | | | | | | | | | RPS
378 | | | | | | | | | | Max number of tracks
379 | | | | | | | | | | | Interrupt timeout
380 | | | | | | | | | | | | Max nonintlv. sectors
381 | | | | | | | | | | | | | -Max Errors- flags */
382 {{0, 500, 16, 16, 8000, 1*HZ, 3*HZ, 0, SEL_DLY, 5, 80, 3*HZ, 20, {3,1,2,0,2}, 0,
383 0, { 7, 4, 8, 2, 1, 5, 3,10}, 3*HZ/2, 0 }, "unknown" },
385 {{1, 300, 16, 16, 8000, 1*HZ, 3*HZ, 0, SEL_DLY, 5, 40, 3*HZ, 17, {3,1,2,0,2}, 0,
386 0, { 1, 0, 0, 0, 0, 0, 0, 0}, 3*HZ/2, 1 }, "360K PC" }, /*5 1/4 360 KB PC*/
388 {{2, 500, 16, 16, 6000, 4*HZ/10, 3*HZ, 14, SEL_DLY, 6, 83, 3*HZ, 17, {3,1,2,0,2}, 0,
389 0, { 2, 5, 6,23,10,20,12, 0}, 3*HZ/2, 2 }, "1.2M" }, /*5 1/4 HD AT*/
391 {{3, 250, 16, 16, 3000, 1*HZ, 3*HZ, 0, SEL_DLY, 5, 83, 3*HZ, 20, {3,1,2,0,2}, 0,
392 0, { 4,22,21,30, 3, 0, 0, 0}, 3*HZ/2, 4 }, "720k" }, /*3 1/2 DD*/
394 {{4, 500, 16, 16, 4000, 4*HZ/10, 3*HZ, 10, SEL_DLY, 5, 83, 3*HZ, 20, {3,1,2,0,2}, 0,
395 0, { 7, 4,25,22,31,21,29,11}, 3*HZ/2, 7 }, "1.44M" }, /*3 1/2 HD*/
397 {{5, 1000, 15, 8, 3000, 4*HZ/10, 3*HZ, 10, SEL_DLY, 5, 83, 3*HZ, 40, {3,1,2,0,2}, 0,
398 0, { 7, 8, 4,25,28,22,31,21}, 3*HZ/2, 8 }, "2.88M AMI BIOS" }, /*3 1/2 ED*/
400 {{6, 1000, 15, 8, 3000, 4*HZ/10, 3*HZ, 10, SEL_DLY, 5, 83, 3*HZ, 40, {3,1,2,0,2}, 0,
401 0, { 7, 8, 4,25,28,22,31,21}, 3*HZ/2, 8 }, "2.88M" } /*3 1/2 ED*/
402 /* | --autodetected formats--- | | |
403 * read_track | | Name printed when booting
404 * | Native format
405 * Frequency of disk change checks */
408 static struct floppy_drive_params drive_params[N_DRIVE];
409 static struct floppy_drive_struct drive_state[N_DRIVE];
410 static struct floppy_write_errors write_errors[N_DRIVE];
411 static struct timer_list motor_off_timer[N_DRIVE];
412 static struct gendisk *disks[N_DRIVE];
413 static struct block_device *opened_bdev[N_DRIVE];
414 static DEFINE_MUTEX(open_lock);
415 static struct floppy_raw_cmd *raw_cmd, default_raw_cmd;
418 * This struct defines the different floppy types.
420 * Bit 0 of 'stretch' tells if the tracks need to be doubled for some
421 * types (e.g. 360kB diskette in 1.2MB drive, etc.). Bit 1 of 'stretch'
422 * tells if the disk is in Commodore 1581 format, which means side 0 sectors
423 * are located on side 1 of the disk but with a side 0 ID, and vice-versa.
424 * This is the same as the Sharp MZ-80 5.25" CP/M disk format, except that the
425 * 1581's logical side 0 is on physical side 1, whereas the Sharp's logical
426 * side 0 is on physical side 0 (but with the misnamed sector IDs).
427 * 'stretch' should probably be renamed to something more general, like
428 * 'options'.
430 * Bits 2 through 9 of 'stretch' tell the number of the first sector.
431 * The LSB (bit 2) is flipped. For most disks, the first sector
432 * is 1 (represented by 0x00<<2). For some CP/M and music sampler
433 * disks (such as Ensoniq EPS 16plus) it is 0 (represented as 0x01<<2).
434 * For Amstrad CPC disks it is 0xC1 (represented as 0xC0<<2).
436 * Other parameters should be self-explanatory (see also setfdprm(8)).
439 Size
440 | Sectors per track
441 | | Head
442 | | | Tracks
443 | | | | Stretch
444 | | | | | Gap 1 size
445 | | | | | | Data rate, | 0x40 for perp
446 | | | | | | | Spec1 (stepping rate, head unload
447 | | | | | | | | /fmt gap (gap2) */
448 static struct floppy_struct floppy_type[32] = {
449 { 0, 0,0, 0,0,0x00,0x00,0x00,0x00,NULL }, /* 0 no testing */
450 { 720, 9,2,40,0,0x2A,0x02,0xDF,0x50,"d360" }, /* 1 360KB PC */
451 { 2400,15,2,80,0,0x1B,0x00,0xDF,0x54,"h1200" }, /* 2 1.2MB AT */
452 { 720, 9,1,80,0,0x2A,0x02,0xDF,0x50,"D360" }, /* 3 360KB SS 3.5" */
453 { 1440, 9,2,80,0,0x2A,0x02,0xDF,0x50,"D720" }, /* 4 720KB 3.5" */
454 { 720, 9,2,40,1,0x23,0x01,0xDF,0x50,"h360" }, /* 5 360KB AT */
455 { 1440, 9,2,80,0,0x23,0x01,0xDF,0x50,"h720" }, /* 6 720KB AT */
456 { 2880,18,2,80,0,0x1B,0x00,0xCF,0x6C,"H1440" }, /* 7 1.44MB 3.5" */
457 { 5760,36,2,80,0,0x1B,0x43,0xAF,0x54,"E2880" }, /* 8 2.88MB 3.5" */
458 { 6240,39,2,80,0,0x1B,0x43,0xAF,0x28,"E3120" }, /* 9 3.12MB 3.5" */
460 { 2880,18,2,80,0,0x25,0x00,0xDF,0x02,"h1440" }, /* 10 1.44MB 5.25" */
461 { 3360,21,2,80,0,0x1C,0x00,0xCF,0x0C,"H1680" }, /* 11 1.68MB 3.5" */
462 { 820,10,2,41,1,0x25,0x01,0xDF,0x2E,"h410" }, /* 12 410KB 5.25" */
463 { 1640,10,2,82,0,0x25,0x02,0xDF,0x2E,"H820" }, /* 13 820KB 3.5" */
464 { 2952,18,2,82,0,0x25,0x00,0xDF,0x02,"h1476" }, /* 14 1.48MB 5.25" */
465 { 3444,21,2,82,0,0x25,0x00,0xDF,0x0C,"H1722" }, /* 15 1.72MB 3.5" */
466 { 840,10,2,42,1,0x25,0x01,0xDF,0x2E,"h420" }, /* 16 420KB 5.25" */
467 { 1660,10,2,83,0,0x25,0x02,0xDF,0x2E,"H830" }, /* 17 830KB 3.5" */
468 { 2988,18,2,83,0,0x25,0x00,0xDF,0x02,"h1494" }, /* 18 1.49MB 5.25" */
469 { 3486,21,2,83,0,0x25,0x00,0xDF,0x0C,"H1743" }, /* 19 1.74 MB 3.5" */
471 { 1760,11,2,80,0,0x1C,0x09,0xCF,0x00,"h880" }, /* 20 880KB 5.25" */
472 { 2080,13,2,80,0,0x1C,0x01,0xCF,0x00,"D1040" }, /* 21 1.04MB 3.5" */
473 { 2240,14,2,80,0,0x1C,0x19,0xCF,0x00,"D1120" }, /* 22 1.12MB 3.5" */
474 { 3200,20,2,80,0,0x1C,0x20,0xCF,0x2C,"h1600" }, /* 23 1.6MB 5.25" */
475 { 3520,22,2,80,0,0x1C,0x08,0xCF,0x2e,"H1760" }, /* 24 1.76MB 3.5" */
476 { 3840,24,2,80,0,0x1C,0x20,0xCF,0x00,"H1920" }, /* 25 1.92MB 3.5" */
477 { 6400,40,2,80,0,0x25,0x5B,0xCF,0x00,"E3200" }, /* 26 3.20MB 3.5" */
478 { 7040,44,2,80,0,0x25,0x5B,0xCF,0x00,"E3520" }, /* 27 3.52MB 3.5" */
479 { 7680,48,2,80,0,0x25,0x63,0xCF,0x00,"E3840" }, /* 28 3.84MB 3.5" */
480 { 3680,23,2,80,0,0x1C,0x10,0xCF,0x00,"H1840" }, /* 29 1.84MB 3.5" */
482 { 1600,10,2,80,0,0x25,0x02,0xDF,0x2E,"D800" }, /* 30 800KB 3.5" */
483 { 3200,20,2,80,0,0x1C,0x00,0xCF,0x2C,"H1600" }, /* 31 1.6MB 3.5" */
486 #define SECTSIZE (_FD_SECTSIZE(*floppy))
488 /* Auto-detection: Disk type used until the next media change occurs. */
489 static struct floppy_struct *current_type[N_DRIVE];
492 * User-provided type information. current_type points to
493 * the respective entry of this array.
495 static struct floppy_struct user_params[N_DRIVE];
497 static sector_t floppy_sizes[256];
499 static char floppy_device_name[] = "floppy";
502 * The driver is trying to determine the correct media format
503 * while probing is set. rw_interrupt() clears it after a
504 * successful access.
506 static int probing;
508 /* Synchronization of FDC access. */
509 #define FD_COMMAND_NONE -1
510 #define FD_COMMAND_ERROR 2
511 #define FD_COMMAND_OKAY 3
513 static volatile int command_status = FD_COMMAND_NONE;
514 static unsigned long fdc_busy;
515 static DECLARE_WAIT_QUEUE_HEAD(fdc_wait);
516 static DECLARE_WAIT_QUEUE_HEAD(command_done);
518 /* Errors during formatting are counted here. */
519 static int format_errors;
521 /* Format request descriptor. */
522 static struct format_descr format_req;
525 * Rate is 0 for 500kb/s, 1 for 300kbps, 2 for 250kbps
526 * Spec1 is 0xSH, where S is stepping rate (F=1ms, E=2ms, D=3ms etc),
527 * H is head unload time (1=16ms, 2=32ms, etc)
531 * Track buffer
532 * Because these are written to by the DMA controller, they must
533 * not contain a 64k byte boundary crossing, or data will be
534 * corrupted/lost.
536 static char *floppy_track_buffer;
537 static int max_buffer_sectors;
539 static int *errors;
540 typedef void (*done_f)(int);
541 static const struct cont_t {
542 void (*interrupt)(void);
543 /* this is called after the interrupt of the
544 * main command */
545 void (*redo)(void); /* this is called to retry the operation */
546 void (*error)(void); /* this is called to tally an error */
547 done_f done; /* this is called to say if the operation has
548 * succeeded/failed */
549 } *cont;
551 static void floppy_ready(void);
552 static void floppy_start(void);
553 static void process_fd_request(void);
554 static void recalibrate_floppy(void);
555 static void floppy_shutdown(unsigned long);
557 static int floppy_request_regions(int);
558 static void floppy_release_regions(int);
559 static int floppy_grab_irq_and_dma(void);
560 static void floppy_release_irq_and_dma(void);
563 * The "reset" variable should be tested whenever an interrupt is scheduled,
564 * after the commands have been sent. This is to ensure that the driver doesn't
565 * get wedged when the interrupt doesn't come because of a failed command.
566 * reset doesn't need to be tested before sending commands, because
567 * output_byte is automatically disabled when reset is set.
569 static void reset_fdc(void);
572 * These are global variables, as that's the easiest way to give
573 * information to interrupts. They are the data used for the current
574 * request.
576 #define NO_TRACK -1
577 #define NEED_1_RECAL -2
578 #define NEED_2_RECAL -3
580 static atomic_t usage_count = ATOMIC_INIT(0);
582 /* buffer related variables */
583 static int buffer_track = -1;
584 static int buffer_drive = -1;
585 static int buffer_min = -1;
586 static int buffer_max = -1;
588 /* fdc related variables, should end up in a struct */
589 static struct floppy_fdc_state fdc_state[N_FDC];
590 static int fdc; /* current fdc */
592 static struct floppy_struct *_floppy = floppy_type;
593 static unsigned char current_drive;
594 static long current_count_sectors;
595 static unsigned char fsector_t; /* sector in track */
596 static unsigned char in_sector_offset; /* offset within physical sector,
597 * expressed in units of 512 bytes */
599 #ifndef fd_eject
600 static inline int fd_eject(int drive)
602 return -EINVAL;
604 #endif
607 * Debugging
608 * =========
610 #ifdef DEBUGT
611 static long unsigned debugtimer;
613 static inline void set_debugt(void)
615 debugtimer = jiffies;
618 static inline void debugt(const char *func, const char *msg)
620 if (DP->flags & DEBUGT)
621 pr_info("%s:%s dtime=%lu\n", func, msg, jiffies - debugtimer);
623 #else
624 static inline void set_debugt(void) { }
625 static inline void debugt(const char *func, const char *msg) { }
626 #endif /* DEBUGT */
628 typedef void (*timeout_fn)(unsigned long);
629 static DEFINE_TIMER(fd_timeout, floppy_shutdown, 0, 0);
631 static const char *timeout_message;
633 static void is_alive(const char *func, const char *message)
635 /* this routine checks whether the floppy driver is "alive" */
636 if (test_bit(0, &fdc_busy) && command_status < 2 &&
637 !timer_pending(&fd_timeout)) {
638 DPRINT("%s: timeout handler died. %s\n", func, message);
642 static void (*do_floppy)(void) = NULL;
644 #define OLOGSIZE 20
646 static void (*lasthandler)(void);
647 static unsigned long interruptjiffies;
648 static unsigned long resultjiffies;
649 static int resultsize;
650 static unsigned long lastredo;
652 static struct output_log {
653 unsigned char data;
654 unsigned char status;
655 unsigned long jiffies;
656 } output_log[OLOGSIZE];
658 static int output_log_pos;
660 #define current_reqD -1
661 #define MAXTIMEOUT -2
663 static void __reschedule_timeout(int drive, const char *message)
665 if (drive == current_reqD)
666 drive = current_drive;
667 del_timer(&fd_timeout);
668 if (drive < 0 || drive >= N_DRIVE) {
669 fd_timeout.expires = jiffies + 20UL * HZ;
670 drive = 0;
671 } else
672 fd_timeout.expires = jiffies + UDP->timeout;
673 add_timer(&fd_timeout);
674 if (UDP->flags & FD_DEBUG)
675 DPRINT("reschedule timeout %s\n", message);
676 timeout_message = message;
679 static void reschedule_timeout(int drive, const char *message)
681 unsigned long flags;
683 spin_lock_irqsave(&floppy_lock, flags);
684 __reschedule_timeout(drive, message);
685 spin_unlock_irqrestore(&floppy_lock, flags);
688 #define INFBOUND(a, b) (a) = max_t(int, a, b)
689 #define SUPBOUND(a, b) (a) = min_t(int, a, b)
692 * Bottom half floppy driver.
693 * ==========================
695 * This part of the file contains the code talking directly to the hardware,
696 * and also the main service loop (seek-configure-spinup-command)
700 * disk change.
701 * This routine is responsible for maintaining the FD_DISK_CHANGE flag,
702 * and the last_checked date.
704 * last_checked is the date of the last check which showed 'no disk change'
705 * FD_DISK_CHANGE is set under two conditions:
706 * 1. The floppy has been changed after some i/o to that floppy already
707 * took place.
708 * 2. No floppy disk is in the drive. This is done in order to ensure that
709 * requests are quickly flushed in case there is no disk in the drive. It
710 * follows that FD_DISK_CHANGE can only be cleared if there is a disk in
711 * the drive.
713 * For 1., maxblock is observed. Maxblock is 0 if no i/o has taken place yet.
714 * For 2., FD_DISK_NEWCHANGE is watched. FD_DISK_NEWCHANGE is cleared on
715 * each seek. If a disk is present, the disk change line should also be
716 * cleared on each seek. Thus, if FD_DISK_NEWCHANGE is clear, but the disk
717 * change line is set, this means either that no disk is in the drive, or
718 * that it has been removed since the last seek.
720 * This means that we really have a third possibility too:
721 * The floppy has been changed after the last seek.
724 static int disk_change(int drive)
726 int fdc = FDC(drive);
728 if (time_before(jiffies, UDRS->select_date + UDP->select_delay))
729 DPRINT("WARNING disk change called early\n");
730 if (!(FDCS->dor & (0x10 << UNIT(drive))) ||
731 (FDCS->dor & 3) != UNIT(drive) || fdc != FDC(drive)) {
732 DPRINT("probing disk change on unselected drive\n");
733 DPRINT("drive=%d fdc=%d dor=%x\n", drive, FDC(drive),
734 (unsigned int)FDCS->dor);
737 debug_dcl(UDP->flags,
738 "checking disk change line for drive %d\n", drive);
739 debug_dcl(UDP->flags, "jiffies=%lu\n", jiffies);
740 debug_dcl(UDP->flags, "disk change line=%x\n", fd_inb(FD_DIR) & 0x80);
741 debug_dcl(UDP->flags, "flags=%lx\n", UDRS->flags);
743 if (UDP->flags & FD_BROKEN_DCL)
744 return test_bit(FD_DISK_CHANGED_BIT, &UDRS->flags);
745 if ((fd_inb(FD_DIR) ^ UDP->flags) & 0x80) {
746 set_bit(FD_VERIFY_BIT, &UDRS->flags);
747 /* verify write protection */
749 if (UDRS->maxblock) /* mark it changed */
750 set_bit(FD_DISK_CHANGED_BIT, &UDRS->flags);
752 /* invalidate its geometry */
753 if (UDRS->keep_data >= 0) {
754 if ((UDP->flags & FTD_MSG) &&
755 current_type[drive] != NULL)
756 DPRINT("Disk type is undefined after disk change\n");
757 current_type[drive] = NULL;
758 floppy_sizes[TOMINOR(drive)] = MAX_DISK_SIZE << 1;
761 return 1;
762 } else {
763 UDRS->last_checked = jiffies;
764 clear_bit(FD_DISK_NEWCHANGE_BIT, &UDRS->flags);
766 return 0;
769 static inline int is_selected(int dor, int unit)
771 return ((dor & (0x10 << unit)) && (dor & 3) == unit);
774 static bool is_ready_state(int status)
776 int state = status & (STATUS_READY | STATUS_DIR | STATUS_DMA);
777 return state == STATUS_READY;
780 static int set_dor(int fdc, char mask, char data)
782 unsigned char unit;
783 unsigned char drive;
784 unsigned char newdor;
785 unsigned char olddor;
787 if (FDCS->address == -1)
788 return -1;
790 olddor = FDCS->dor;
791 newdor = (olddor & mask) | data;
792 if (newdor != olddor) {
793 unit = olddor & 0x3;
794 if (is_selected(olddor, unit) && !is_selected(newdor, unit)) {
795 drive = REVDRIVE(fdc, unit);
796 debug_dcl(UDP->flags,
797 "calling disk change from set_dor\n");
798 disk_change(drive);
800 FDCS->dor = newdor;
801 fd_outb(newdor, FD_DOR);
803 unit = newdor & 0x3;
804 if (!is_selected(olddor, unit) && is_selected(newdor, unit)) {
805 drive = REVDRIVE(fdc, unit);
806 UDRS->select_date = jiffies;
809 return olddor;
812 static void twaddle(void)
814 if (DP->select_delay)
815 return;
816 fd_outb(FDCS->dor & ~(0x10 << UNIT(current_drive)), FD_DOR);
817 fd_outb(FDCS->dor, FD_DOR);
818 DRS->select_date = jiffies;
822 * Reset all driver information about the current fdc.
823 * This is needed after a reset, and after a raw command.
825 static void reset_fdc_info(int mode)
827 int drive;
829 FDCS->spec1 = FDCS->spec2 = -1;
830 FDCS->need_configure = 1;
831 FDCS->perp_mode = 1;
832 FDCS->rawcmd = 0;
833 for (drive = 0; drive < N_DRIVE; drive++)
834 if (FDC(drive) == fdc && (mode || UDRS->track != NEED_1_RECAL))
835 UDRS->track = NEED_2_RECAL;
838 /* selects the fdc and drive, and enables the fdc's input/dma. */
839 static void set_fdc(int drive)
841 if (drive >= 0 && drive < N_DRIVE) {
842 fdc = FDC(drive);
843 current_drive = drive;
845 if (fdc != 1 && fdc != 0) {
846 pr_info("bad fdc value\n");
847 return;
849 set_dor(fdc, ~0, 8);
850 #if N_FDC > 1
851 set_dor(1 - fdc, ~8, 0);
852 #endif
853 if (FDCS->rawcmd == 2)
854 reset_fdc_info(1);
855 if (fd_inb(FD_STATUS) != STATUS_READY)
856 FDCS->reset = 1;
859 /* locks the driver */
860 static int lock_fdc(int drive, bool interruptible)
862 if (WARN(atomic_read(&usage_count) == 0,
863 "Trying to lock fdc while usage count=0\n"))
864 return -1;
866 if (wait_event_interruptible(fdc_wait, !test_and_set_bit(0, &fdc_busy)))
867 return -EINTR;
869 command_status = FD_COMMAND_NONE;
871 __reschedule_timeout(drive, "lock fdc");
872 set_fdc(drive);
873 return 0;
876 /* unlocks the driver */
877 static void unlock_fdc(void)
879 unsigned long flags;
881 raw_cmd = NULL;
882 if (!test_bit(0, &fdc_busy))
883 DPRINT("FDC access conflict!\n");
885 if (do_floppy)
886 DPRINT("device interrupt still active at FDC release: %pf!\n",
887 do_floppy);
888 command_status = FD_COMMAND_NONE;
889 spin_lock_irqsave(&floppy_lock, flags);
890 del_timer(&fd_timeout);
891 cont = NULL;
892 clear_bit(0, &fdc_busy);
893 if (current_req || blk_peek_request(floppy_queue))
894 do_fd_request(floppy_queue);
895 spin_unlock_irqrestore(&floppy_lock, flags);
896 wake_up(&fdc_wait);
899 /* switches the motor off after a given timeout */
900 static void motor_off_callback(unsigned long nr)
902 unsigned char mask = ~(0x10 << UNIT(nr));
904 set_dor(FDC(nr), mask, 0);
907 /* schedules motor off */
908 static void floppy_off(unsigned int drive)
910 unsigned long volatile delta;
911 int fdc = FDC(drive);
913 if (!(FDCS->dor & (0x10 << UNIT(drive))))
914 return;
916 del_timer(motor_off_timer + drive);
918 /* make spindle stop in a position which minimizes spinup time
919 * next time */
920 if (UDP->rps) {
921 delta = jiffies - UDRS->first_read_date + HZ -
922 UDP->spindown_offset;
923 delta = ((delta * UDP->rps) % HZ) / UDP->rps;
924 motor_off_timer[drive].expires =
925 jiffies + UDP->spindown - delta;
927 add_timer(motor_off_timer + drive);
931 * cycle through all N_DRIVE floppy drives, for disk change testing.
932 * stopping at current drive. This is done before any long operation, to
933 * be sure to have up to date disk change information.
935 static void scandrives(void)
937 int i;
938 int drive;
939 int saved_drive;
941 if (DP->select_delay)
942 return;
944 saved_drive = current_drive;
945 for (i = 0; i < N_DRIVE; i++) {
946 drive = (saved_drive + i + 1) % N_DRIVE;
947 if (UDRS->fd_ref == 0 || UDP->select_delay != 0)
948 continue; /* skip closed drives */
949 set_fdc(drive);
950 if (!(set_dor(fdc, ~3, UNIT(drive) | (0x10 << UNIT(drive))) &
951 (0x10 << UNIT(drive))))
952 /* switch the motor off again, if it was off to
953 * begin with */
954 set_dor(fdc, ~(0x10 << UNIT(drive)), 0);
956 set_fdc(saved_drive);
959 static void empty(void)
963 static DECLARE_WORK(floppy_work, NULL);
965 static void schedule_bh(void (*handler)(void))
967 PREPARE_WORK(&floppy_work, (work_func_t)handler);
968 schedule_work(&floppy_work);
971 static DEFINE_TIMER(fd_timer, NULL, 0, 0);
973 static void cancel_activity(void)
975 unsigned long flags;
977 spin_lock_irqsave(&floppy_lock, flags);
978 do_floppy = NULL;
979 PREPARE_WORK(&floppy_work, (work_func_t)empty);
980 del_timer(&fd_timer);
981 spin_unlock_irqrestore(&floppy_lock, flags);
984 /* this function makes sure that the disk stays in the drive during the
985 * transfer */
986 static void fd_watchdog(void)
988 debug_dcl(DP->flags, "calling disk change from watchdog\n");
990 if (disk_change(current_drive)) {
991 DPRINT("disk removed during i/o\n");
992 cancel_activity();
993 cont->done(0);
994 reset_fdc();
995 } else {
996 del_timer(&fd_timer);
997 fd_timer.function = (timeout_fn)fd_watchdog;
998 fd_timer.expires = jiffies + HZ / 10;
999 add_timer(&fd_timer);
1003 static void main_command_interrupt(void)
1005 del_timer(&fd_timer);
1006 cont->interrupt();
1009 /* waits for a delay (spinup or select) to pass */
1010 static int fd_wait_for_completion(unsigned long delay, timeout_fn function)
1012 if (FDCS->reset) {
1013 reset_fdc(); /* do the reset during sleep to win time
1014 * if we don't need to sleep, it's a good
1015 * occasion anyways */
1016 return 1;
1019 if (time_before(jiffies, delay)) {
1020 del_timer(&fd_timer);
1021 fd_timer.function = function;
1022 fd_timer.expires = delay;
1023 add_timer(&fd_timer);
1024 return 1;
1026 return 0;
1029 static DEFINE_SPINLOCK(floppy_hlt_lock);
1030 static int hlt_disabled;
1031 static void floppy_disable_hlt(void)
1033 unsigned long flags;
1035 spin_lock_irqsave(&floppy_hlt_lock, flags);
1036 if (!hlt_disabled) {
1037 hlt_disabled = 1;
1038 #ifdef HAVE_DISABLE_HLT
1039 disable_hlt();
1040 #endif
1042 spin_unlock_irqrestore(&floppy_hlt_lock, flags);
1045 static void floppy_enable_hlt(void)
1047 unsigned long flags;
1049 spin_lock_irqsave(&floppy_hlt_lock, flags);
1050 if (hlt_disabled) {
1051 hlt_disabled = 0;
1052 #ifdef HAVE_DISABLE_HLT
1053 enable_hlt();
1054 #endif
1056 spin_unlock_irqrestore(&floppy_hlt_lock, flags);
1059 static void setup_DMA(void)
1061 unsigned long f;
1063 if (raw_cmd->length == 0) {
1064 int i;
1066 pr_info("zero dma transfer size:");
1067 for (i = 0; i < raw_cmd->cmd_count; i++)
1068 pr_cont("%x,", raw_cmd->cmd[i]);
1069 pr_cont("\n");
1070 cont->done(0);
1071 FDCS->reset = 1;
1072 return;
1074 if (((unsigned long)raw_cmd->kernel_data) % 512) {
1075 pr_info("non aligned address: %p\n", raw_cmd->kernel_data);
1076 cont->done(0);
1077 FDCS->reset = 1;
1078 return;
1080 f = claim_dma_lock();
1081 fd_disable_dma();
1082 #ifdef fd_dma_setup
1083 if (fd_dma_setup(raw_cmd->kernel_data, raw_cmd->length,
1084 (raw_cmd->flags & FD_RAW_READ) ?
1085 DMA_MODE_READ : DMA_MODE_WRITE, FDCS->address) < 0) {
1086 release_dma_lock(f);
1087 cont->done(0);
1088 FDCS->reset = 1;
1089 return;
1091 release_dma_lock(f);
1092 #else
1093 fd_clear_dma_ff();
1094 fd_cacheflush(raw_cmd->kernel_data, raw_cmd->length);
1095 fd_set_dma_mode((raw_cmd->flags & FD_RAW_READ) ?
1096 DMA_MODE_READ : DMA_MODE_WRITE);
1097 fd_set_dma_addr(raw_cmd->kernel_data);
1098 fd_set_dma_count(raw_cmd->length);
1099 virtual_dma_port = FDCS->address;
1100 fd_enable_dma();
1101 release_dma_lock(f);
1102 #endif
1103 floppy_disable_hlt();
1106 static void show_floppy(void);
1108 /* waits until the fdc becomes ready */
1109 static int wait_til_ready(void)
1111 int status;
1112 int counter;
1114 if (FDCS->reset)
1115 return -1;
1116 for (counter = 0; counter < 10000; counter++) {
1117 status = fd_inb(FD_STATUS);
1118 if (status & STATUS_READY)
1119 return status;
1121 if (initialized) {
1122 DPRINT("Getstatus times out (%x) on fdc %d\n", status, fdc);
1123 show_floppy();
1125 FDCS->reset = 1;
1126 return -1;
1129 /* sends a command byte to the fdc */
1130 static int output_byte(char byte)
1132 int status = wait_til_ready();
1134 if (status < 0)
1135 return -1;
1137 if (is_ready_state(status)) {
1138 fd_outb(byte, FD_DATA);
1139 output_log[output_log_pos].data = byte;
1140 output_log[output_log_pos].status = status;
1141 output_log[output_log_pos].jiffies = jiffies;
1142 output_log_pos = (output_log_pos + 1) % OLOGSIZE;
1143 return 0;
1145 FDCS->reset = 1;
1146 if (initialized) {
1147 DPRINT("Unable to send byte %x to FDC. Fdc=%x Status=%x\n",
1148 byte, fdc, status);
1149 show_floppy();
1151 return -1;
1154 /* gets the response from the fdc */
1155 static int result(void)
1157 int i;
1158 int status = 0;
1160 for (i = 0; i < MAX_REPLIES; i++) {
1161 status = wait_til_ready();
1162 if (status < 0)
1163 break;
1164 status &= STATUS_DIR | STATUS_READY | STATUS_BUSY | STATUS_DMA;
1165 if ((status & ~STATUS_BUSY) == STATUS_READY) {
1166 resultjiffies = jiffies;
1167 resultsize = i;
1168 return i;
1170 if (status == (STATUS_DIR | STATUS_READY | STATUS_BUSY))
1171 reply_buffer[i] = fd_inb(FD_DATA);
1172 else
1173 break;
1175 if (initialized) {
1176 DPRINT("get result error. Fdc=%d Last status=%x Read bytes=%d\n",
1177 fdc, status, i);
1178 show_floppy();
1180 FDCS->reset = 1;
1181 return -1;
1184 #define MORE_OUTPUT -2
1185 /* does the fdc need more output? */
1186 static int need_more_output(void)
1188 int status = wait_til_ready();
1190 if (status < 0)
1191 return -1;
1193 if (is_ready_state(status))
1194 return MORE_OUTPUT;
1196 return result();
1199 /* Set perpendicular mode as required, based on data rate, if supported.
1200 * 82077 Now tested. 1Mbps data rate only possible with 82077-1.
1202 static void perpendicular_mode(void)
1204 unsigned char perp_mode;
1206 if (raw_cmd->rate & 0x40) {
1207 switch (raw_cmd->rate & 3) {
1208 case 0:
1209 perp_mode = 2;
1210 break;
1211 case 3:
1212 perp_mode = 3;
1213 break;
1214 default:
1215 DPRINT("Invalid data rate for perpendicular mode!\n");
1216 cont->done(0);
1217 FDCS->reset = 1;
1219 * convenient way to return to
1220 * redo without too much hassle
1221 * (deep stack et al.)
1223 return;
1225 } else
1226 perp_mode = 0;
1228 if (FDCS->perp_mode == perp_mode)
1229 return;
1230 if (FDCS->version >= FDC_82077_ORIG) {
1231 output_byte(FD_PERPENDICULAR);
1232 output_byte(perp_mode);
1233 FDCS->perp_mode = perp_mode;
1234 } else if (perp_mode) {
1235 DPRINT("perpendicular mode not supported by this FDC.\n");
1237 } /* perpendicular_mode */
1239 static int fifo_depth = 0xa;
1240 static int no_fifo;
1242 static int fdc_configure(void)
1244 /* Turn on FIFO */
1245 output_byte(FD_CONFIGURE);
1246 if (need_more_output() != MORE_OUTPUT)
1247 return 0;
1248 output_byte(0);
1249 output_byte(0x10 | (no_fifo & 0x20) | (fifo_depth & 0xf));
1250 output_byte(0); /* pre-compensation from track
1251 0 upwards */
1252 return 1;
1255 #define NOMINAL_DTR 500
1257 /* Issue a "SPECIFY" command to set the step rate time, head unload time,
1258 * head load time, and DMA disable flag to values needed by floppy.
1260 * The value "dtr" is the data transfer rate in Kbps. It is needed
1261 * to account for the data rate-based scaling done by the 82072 and 82077
1262 * FDC types. This parameter is ignored for other types of FDCs (i.e.
1263 * 8272a).
1265 * Note that changing the data transfer rate has a (probably deleterious)
1266 * effect on the parameters subject to scaling for 82072/82077 FDCs, so
1267 * fdc_specify is called again after each data transfer rate
1268 * change.
1270 * srt: 1000 to 16000 in microseconds
1271 * hut: 16 to 240 milliseconds
1272 * hlt: 2 to 254 milliseconds
1274 * These values are rounded up to the next highest available delay time.
1276 static void fdc_specify(void)
1278 unsigned char spec1;
1279 unsigned char spec2;
1280 unsigned long srt;
1281 unsigned long hlt;
1282 unsigned long hut;
1283 unsigned long dtr = NOMINAL_DTR;
1284 unsigned long scale_dtr = NOMINAL_DTR;
1285 int hlt_max_code = 0x7f;
1286 int hut_max_code = 0xf;
1288 if (FDCS->need_configure && FDCS->version >= FDC_82072A) {
1289 fdc_configure();
1290 FDCS->need_configure = 0;
1293 switch (raw_cmd->rate & 0x03) {
1294 case 3:
1295 dtr = 1000;
1296 break;
1297 case 1:
1298 dtr = 300;
1299 if (FDCS->version >= FDC_82078) {
1300 /* chose the default rate table, not the one
1301 * where 1 = 2 Mbps */
1302 output_byte(FD_DRIVESPEC);
1303 if (need_more_output() == MORE_OUTPUT) {
1304 output_byte(UNIT(current_drive));
1305 output_byte(0xc0);
1308 break;
1309 case 2:
1310 dtr = 250;
1311 break;
1314 if (FDCS->version >= FDC_82072) {
1315 scale_dtr = dtr;
1316 hlt_max_code = 0x00; /* 0==256msec*dtr0/dtr (not linear!) */
1317 hut_max_code = 0x0; /* 0==256msec*dtr0/dtr (not linear!) */
1320 /* Convert step rate from microseconds to milliseconds and 4 bits */
1321 srt = 16 - DIV_ROUND_UP(DP->srt * scale_dtr / 1000, NOMINAL_DTR);
1322 if (slow_floppy)
1323 srt = srt / 4;
1325 SUPBOUND(srt, 0xf);
1326 INFBOUND(srt, 0);
1328 hlt = DIV_ROUND_UP(DP->hlt * scale_dtr / 2, NOMINAL_DTR);
1329 if (hlt < 0x01)
1330 hlt = 0x01;
1331 else if (hlt > 0x7f)
1332 hlt = hlt_max_code;
1334 hut = DIV_ROUND_UP(DP->hut * scale_dtr / 16, NOMINAL_DTR);
1335 if (hut < 0x1)
1336 hut = 0x1;
1337 else if (hut > 0xf)
1338 hut = hut_max_code;
1340 spec1 = (srt << 4) | hut;
1341 spec2 = (hlt << 1) | (use_virtual_dma & 1);
1343 /* If these parameters did not change, just return with success */
1344 if (FDCS->spec1 != spec1 || FDCS->spec2 != spec2) {
1345 /* Go ahead and set spec1 and spec2 */
1346 output_byte(FD_SPECIFY);
1347 output_byte(FDCS->spec1 = spec1);
1348 output_byte(FDCS->spec2 = spec2);
1350 } /* fdc_specify */
1352 /* Set the FDC's data transfer rate on behalf of the specified drive.
1353 * NOTE: with 82072/82077 FDCs, changing the data rate requires a reissue
1354 * of the specify command (i.e. using the fdc_specify function).
1356 static int fdc_dtr(void)
1358 /* If data rate not already set to desired value, set it. */
1359 if ((raw_cmd->rate & 3) == FDCS->dtr)
1360 return 0;
1362 /* Set dtr */
1363 fd_outb(raw_cmd->rate & 3, FD_DCR);
1365 /* TODO: some FDC/drive combinations (C&T 82C711 with TEAC 1.2MB)
1366 * need a stabilization period of several milliseconds to be
1367 * enforced after data rate changes before R/W operations.
1368 * Pause 5 msec to avoid trouble. (Needs to be 2 jiffies)
1370 FDCS->dtr = raw_cmd->rate & 3;
1371 return fd_wait_for_completion(jiffies + 2UL * HZ / 100,
1372 (timeout_fn)floppy_ready);
1373 } /* fdc_dtr */
1375 static void tell_sector(void)
1377 pr_cont(": track %d, head %d, sector %d, size %d",
1378 R_TRACK, R_HEAD, R_SECTOR, R_SIZECODE);
1379 } /* tell_sector */
1381 static void print_errors(void)
1383 DPRINT("");
1384 if (ST0 & ST0_ECE) {
1385 pr_cont("Recalibrate failed!");
1386 } else if (ST2 & ST2_CRC) {
1387 pr_cont("data CRC error");
1388 tell_sector();
1389 } else if (ST1 & ST1_CRC) {
1390 pr_cont("CRC error");
1391 tell_sector();
1392 } else if ((ST1 & (ST1_MAM | ST1_ND)) ||
1393 (ST2 & ST2_MAM)) {
1394 if (!probing) {
1395 pr_cont("sector not found");
1396 tell_sector();
1397 } else
1398 pr_cont("probe failed...");
1399 } else if (ST2 & ST2_WC) { /* seek error */
1400 pr_cont("wrong cylinder");
1401 } else if (ST2 & ST2_BC) { /* cylinder marked as bad */
1402 pr_cont("bad cylinder");
1403 } else {
1404 pr_cont("unknown error. ST[0..2] are: 0x%x 0x%x 0x%x",
1405 ST0, ST1, ST2);
1406 tell_sector();
1408 pr_cont("\n");
1412 * OK, this error interpreting routine is called after a
1413 * DMA read/write has succeeded
1414 * or failed, so we check the results, and copy any buffers.
1415 * hhb: Added better error reporting.
1416 * ak: Made this into a separate routine.
1418 static int interpret_errors(void)
1420 char bad;
1422 if (inr != 7) {
1423 DPRINT("-- FDC reply error\n");
1424 FDCS->reset = 1;
1425 return 1;
1428 /* check IC to find cause of interrupt */
1429 switch (ST0 & ST0_INTR) {
1430 case 0x40: /* error occurred during command execution */
1431 if (ST1 & ST1_EOC)
1432 return 0; /* occurs with pseudo-DMA */
1433 bad = 1;
1434 if (ST1 & ST1_WP) {
1435 DPRINT("Drive is write protected\n");
1436 clear_bit(FD_DISK_WRITABLE_BIT, &DRS->flags);
1437 cont->done(0);
1438 bad = 2;
1439 } else if (ST1 & ST1_ND) {
1440 set_bit(FD_NEED_TWADDLE_BIT, &DRS->flags);
1441 } else if (ST1 & ST1_OR) {
1442 if (DP->flags & FTD_MSG)
1443 DPRINT("Over/Underrun - retrying\n");
1444 bad = 0;
1445 } else if (*errors >= DP->max_errors.reporting) {
1446 print_errors();
1448 if (ST2 & ST2_WC || ST2 & ST2_BC)
1449 /* wrong cylinder => recal */
1450 DRS->track = NEED_2_RECAL;
1451 return bad;
1452 case 0x80: /* invalid command given */
1453 DPRINT("Invalid FDC command given!\n");
1454 cont->done(0);
1455 return 2;
1456 case 0xc0:
1457 DPRINT("Abnormal termination caused by polling\n");
1458 cont->error();
1459 return 2;
1460 default: /* (0) Normal command termination */
1461 return 0;
1466 * This routine is called when everything should be correctly set up
1467 * for the transfer (i.e. floppy motor is on, the correct floppy is
1468 * selected, and the head is sitting on the right track).
1470 static void setup_rw_floppy(void)
1472 int i;
1473 int r;
1474 int flags;
1475 int dflags;
1476 unsigned long ready_date;
1477 timeout_fn function;
1479 flags = raw_cmd->flags;
1480 if (flags & (FD_RAW_READ | FD_RAW_WRITE))
1481 flags |= FD_RAW_INTR;
1483 if ((flags & FD_RAW_SPIN) && !(flags & FD_RAW_NO_MOTOR)) {
1484 ready_date = DRS->spinup_date + DP->spinup;
1485 /* If spinup will take a long time, rerun scandrives
1486 * again just before spinup completion. Beware that
1487 * after scandrives, we must again wait for selection.
1489 if (time_after(ready_date, jiffies + DP->select_delay)) {
1490 ready_date -= DP->select_delay;
1491 function = (timeout_fn)floppy_start;
1492 } else
1493 function = (timeout_fn)setup_rw_floppy;
1495 /* wait until the floppy is spinning fast enough */
1496 if (fd_wait_for_completion(ready_date, function))
1497 return;
1499 dflags = DRS->flags;
1501 if ((flags & FD_RAW_READ) || (flags & FD_RAW_WRITE))
1502 setup_DMA();
1504 if (flags & FD_RAW_INTR)
1505 do_floppy = main_command_interrupt;
1507 r = 0;
1508 for (i = 0; i < raw_cmd->cmd_count; i++)
1509 r |= output_byte(raw_cmd->cmd[i]);
1511 debugt(__func__, "rw_command");
1513 if (r) {
1514 cont->error();
1515 reset_fdc();
1516 return;
1519 if (!(flags & FD_RAW_INTR)) {
1520 inr = result();
1521 cont->interrupt();
1522 } else if (flags & FD_RAW_NEED_DISK)
1523 fd_watchdog();
1526 static int blind_seek;
1529 * This is the routine called after every seek (or recalibrate) interrupt
1530 * from the floppy controller.
1532 static void seek_interrupt(void)
1534 debugt(__func__, "");
1535 if (inr != 2 || (ST0 & 0xF8) != 0x20) {
1536 DPRINT("seek failed\n");
1537 DRS->track = NEED_2_RECAL;
1538 cont->error();
1539 cont->redo();
1540 return;
1542 if (DRS->track >= 0 && DRS->track != ST1 && !blind_seek) {
1543 debug_dcl(DP->flags,
1544 "clearing NEWCHANGE flag because of effective seek\n");
1545 debug_dcl(DP->flags, "jiffies=%lu\n", jiffies);
1546 clear_bit(FD_DISK_NEWCHANGE_BIT, &DRS->flags);
1547 /* effective seek */
1548 DRS->select_date = jiffies;
1550 DRS->track = ST1;
1551 floppy_ready();
1554 static void check_wp(void)
1556 if (test_bit(FD_VERIFY_BIT, &DRS->flags)) {
1557 /* check write protection */
1558 output_byte(FD_GETSTATUS);
1559 output_byte(UNIT(current_drive));
1560 if (result() != 1) {
1561 FDCS->reset = 1;
1562 return;
1564 clear_bit(FD_VERIFY_BIT, &DRS->flags);
1565 clear_bit(FD_NEED_TWADDLE_BIT, &DRS->flags);
1566 debug_dcl(DP->flags,
1567 "checking whether disk is write protected\n");
1568 debug_dcl(DP->flags, "wp=%x\n", ST3 & 0x40);
1569 if (!(ST3 & 0x40))
1570 set_bit(FD_DISK_WRITABLE_BIT, &DRS->flags);
1571 else
1572 clear_bit(FD_DISK_WRITABLE_BIT, &DRS->flags);
1576 static void seek_floppy(void)
1578 int track;
1580 blind_seek = 0;
1582 debug_dcl(DP->flags, "calling disk change from %s\n", __func__);
1584 if (!test_bit(FD_DISK_NEWCHANGE_BIT, &DRS->flags) &&
1585 disk_change(current_drive) && (raw_cmd->flags & FD_RAW_NEED_DISK)) {
1586 /* the media changed flag should be cleared after the seek.
1587 * If it isn't, this means that there is really no disk in
1588 * the drive.
1590 set_bit(FD_DISK_CHANGED_BIT, &DRS->flags);
1591 cont->done(0);
1592 cont->redo();
1593 return;
1595 if (DRS->track <= NEED_1_RECAL) {
1596 recalibrate_floppy();
1597 return;
1598 } else if (test_bit(FD_DISK_NEWCHANGE_BIT, &DRS->flags) &&
1599 (raw_cmd->flags & FD_RAW_NEED_DISK) &&
1600 (DRS->track <= NO_TRACK || DRS->track == raw_cmd->track)) {
1601 /* we seek to clear the media-changed condition. Does anybody
1602 * know a more elegant way, which works on all drives? */
1603 if (raw_cmd->track)
1604 track = raw_cmd->track - 1;
1605 else {
1606 if (DP->flags & FD_SILENT_DCL_CLEAR) {
1607 set_dor(fdc, ~(0x10 << UNIT(current_drive)), 0);
1608 blind_seek = 1;
1609 raw_cmd->flags |= FD_RAW_NEED_SEEK;
1611 track = 1;
1613 } else {
1614 check_wp();
1615 if (raw_cmd->track != DRS->track &&
1616 (raw_cmd->flags & FD_RAW_NEED_SEEK))
1617 track = raw_cmd->track;
1618 else {
1619 setup_rw_floppy();
1620 return;
1624 do_floppy = seek_interrupt;
1625 output_byte(FD_SEEK);
1626 output_byte(UNIT(current_drive));
1627 if (output_byte(track) < 0) {
1628 reset_fdc();
1629 return;
1631 debugt(__func__, "");
1634 static void recal_interrupt(void)
1636 debugt(__func__, "");
1637 if (inr != 2)
1638 FDCS->reset = 1;
1639 else if (ST0 & ST0_ECE) {
1640 switch (DRS->track) {
1641 case NEED_1_RECAL:
1642 debugt(__func__, "need 1 recal");
1643 /* after a second recalibrate, we still haven't
1644 * reached track 0. Probably no drive. Raise an
1645 * error, as failing immediately might upset
1646 * computers possessed by the Devil :-) */
1647 cont->error();
1648 cont->redo();
1649 return;
1650 case NEED_2_RECAL:
1651 debugt(__func__, "need 2 recal");
1652 /* If we already did a recalibrate,
1653 * and we are not at track 0, this
1654 * means we have moved. (The only way
1655 * not to move at recalibration is to
1656 * be already at track 0.) Clear the
1657 * new change flag */
1658 debug_dcl(DP->flags,
1659 "clearing NEWCHANGE flag because of second recalibrate\n");
1661 clear_bit(FD_DISK_NEWCHANGE_BIT, &DRS->flags);
1662 DRS->select_date = jiffies;
1663 /* fall through */
1664 default:
1665 debugt(__func__, "default");
1666 /* Recalibrate moves the head by at
1667 * most 80 steps. If after one
1668 * recalibrate we don't have reached
1669 * track 0, this might mean that we
1670 * started beyond track 80. Try
1671 * again. */
1672 DRS->track = NEED_1_RECAL;
1673 break;
1675 } else
1676 DRS->track = ST1;
1677 floppy_ready();
1680 static void print_result(char *message, int inr)
1682 int i;
1684 DPRINT("%s ", message);
1685 if (inr >= 0)
1686 for (i = 0; i < inr; i++)
1687 pr_cont("repl[%d]=%x ", i, reply_buffer[i]);
1688 pr_cont("\n");
1691 /* interrupt handler. Note that this can be called externally on the Sparc */
1692 irqreturn_t floppy_interrupt(int irq, void *dev_id)
1694 int do_print;
1695 unsigned long f;
1696 void (*handler)(void) = do_floppy;
1698 lasthandler = handler;
1699 interruptjiffies = jiffies;
1701 f = claim_dma_lock();
1702 fd_disable_dma();
1703 release_dma_lock(f);
1705 floppy_enable_hlt();
1706 do_floppy = NULL;
1707 if (fdc >= N_FDC || FDCS->address == -1) {
1708 /* we don't even know which FDC is the culprit */
1709 pr_info("DOR0=%x\n", fdc_state[0].dor);
1710 pr_info("floppy interrupt on bizarre fdc %d\n", fdc);
1711 pr_info("handler=%pf\n", handler);
1712 is_alive(__func__, "bizarre fdc");
1713 return IRQ_NONE;
1716 FDCS->reset = 0;
1717 /* We have to clear the reset flag here, because apparently on boxes
1718 * with level triggered interrupts (PS/2, Sparc, ...), it is needed to
1719 * emit SENSEI's to clear the interrupt line. And FDCS->reset blocks the
1720 * emission of the SENSEI's.
1721 * It is OK to emit floppy commands because we are in an interrupt
1722 * handler here, and thus we have to fear no interference of other
1723 * activity.
1726 do_print = !handler && print_unex && initialized;
1728 inr = result();
1729 if (do_print)
1730 print_result("unexpected interrupt", inr);
1731 if (inr == 0) {
1732 int max_sensei = 4;
1733 do {
1734 output_byte(FD_SENSEI);
1735 inr = result();
1736 if (do_print)
1737 print_result("sensei", inr);
1738 max_sensei--;
1739 } while ((ST0 & 0x83) != UNIT(current_drive) &&
1740 inr == 2 && max_sensei);
1742 if (!handler) {
1743 FDCS->reset = 1;
1744 return IRQ_NONE;
1746 schedule_bh(handler);
1747 is_alive(__func__, "normal interrupt end");
1749 return IRQ_HANDLED;
1752 static void recalibrate_floppy(void)
1754 debugt(__func__, "");
1755 do_floppy = recal_interrupt;
1756 output_byte(FD_RECALIBRATE);
1757 if (output_byte(UNIT(current_drive)) < 0)
1758 reset_fdc();
1762 * Must do 4 FD_SENSEIs after reset because of ``drive polling''.
1764 static void reset_interrupt(void)
1766 debugt(__func__, "");
1767 result(); /* get the status ready for set_fdc */
1768 if (FDCS->reset) {
1769 pr_info("reset set in interrupt, calling %pf\n", cont->error);
1770 cont->error(); /* a reset just after a reset. BAD! */
1772 cont->redo();
1776 * reset is done by pulling bit 2 of DOR low for a while (old FDCs),
1777 * or by setting the self clearing bit 7 of STATUS (newer FDCs)
1779 static void reset_fdc(void)
1781 unsigned long flags;
1783 do_floppy = reset_interrupt;
1784 FDCS->reset = 0;
1785 reset_fdc_info(0);
1787 /* Pseudo-DMA may intercept 'reset finished' interrupt. */
1788 /* Irrelevant for systems with true DMA (i386). */
1790 flags = claim_dma_lock();
1791 fd_disable_dma();
1792 release_dma_lock(flags);
1794 if (FDCS->version >= FDC_82072A)
1795 fd_outb(0x80 | (FDCS->dtr & 3), FD_STATUS);
1796 else {
1797 fd_outb(FDCS->dor & ~0x04, FD_DOR);
1798 udelay(FD_RESET_DELAY);
1799 fd_outb(FDCS->dor, FD_DOR);
1803 static void show_floppy(void)
1805 int i;
1807 pr_info("\n");
1808 pr_info("floppy driver state\n");
1809 pr_info("-------------------\n");
1810 pr_info("now=%lu last interrupt=%lu diff=%lu last called handler=%pf\n",
1811 jiffies, interruptjiffies, jiffies - interruptjiffies,
1812 lasthandler);
1814 pr_info("timeout_message=%s\n", timeout_message);
1815 pr_info("last output bytes:\n");
1816 for (i = 0; i < OLOGSIZE; i++)
1817 pr_info("%2x %2x %lu\n",
1818 output_log[(i + output_log_pos) % OLOGSIZE].data,
1819 output_log[(i + output_log_pos) % OLOGSIZE].status,
1820 output_log[(i + output_log_pos) % OLOGSIZE].jiffies);
1821 pr_info("last result at %lu\n", resultjiffies);
1822 pr_info("last redo_fd_request at %lu\n", lastredo);
1823 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_NONE, 16, 1,
1824 reply_buffer, resultsize, true);
1826 pr_info("status=%x\n", fd_inb(FD_STATUS));
1827 pr_info("fdc_busy=%lu\n", fdc_busy);
1828 if (do_floppy)
1829 pr_info("do_floppy=%pf\n", do_floppy);
1830 if (work_pending(&floppy_work))
1831 pr_info("floppy_work.func=%pf\n", floppy_work.func);
1832 if (timer_pending(&fd_timer))
1833 pr_info("fd_timer.function=%pf\n", fd_timer.function);
1834 if (timer_pending(&fd_timeout)) {
1835 pr_info("timer_function=%pf\n", fd_timeout.function);
1836 pr_info("expires=%lu\n", fd_timeout.expires - jiffies);
1837 pr_info("now=%lu\n", jiffies);
1839 pr_info("cont=%p\n", cont);
1840 pr_info("current_req=%p\n", current_req);
1841 pr_info("command_status=%d\n", command_status);
1842 pr_info("\n");
1845 static void floppy_shutdown(unsigned long data)
1847 unsigned long flags;
1849 if (initialized)
1850 show_floppy();
1851 cancel_activity();
1853 floppy_enable_hlt();
1855 flags = claim_dma_lock();
1856 fd_disable_dma();
1857 release_dma_lock(flags);
1859 /* avoid dma going to a random drive after shutdown */
1861 if (initialized)
1862 DPRINT("floppy timeout called\n");
1863 FDCS->reset = 1;
1864 if (cont) {
1865 cont->done(0);
1866 cont->redo(); /* this will recall reset when needed */
1867 } else {
1868 pr_info("no cont in shutdown!\n");
1869 process_fd_request();
1871 is_alive(__func__, "");
1874 /* start motor, check media-changed condition and write protection */
1875 static int start_motor(void (*function)(void))
1877 int mask;
1878 int data;
1880 mask = 0xfc;
1881 data = UNIT(current_drive);
1882 if (!(raw_cmd->flags & FD_RAW_NO_MOTOR)) {
1883 if (!(FDCS->dor & (0x10 << UNIT(current_drive)))) {
1884 set_debugt();
1885 /* no read since this drive is running */
1886 DRS->first_read_date = 0;
1887 /* note motor start time if motor is not yet running */
1888 DRS->spinup_date = jiffies;
1889 data |= (0x10 << UNIT(current_drive));
1891 } else if (FDCS->dor & (0x10 << UNIT(current_drive)))
1892 mask &= ~(0x10 << UNIT(current_drive));
1894 /* starts motor and selects floppy */
1895 del_timer(motor_off_timer + current_drive);
1896 set_dor(fdc, mask, data);
1898 /* wait_for_completion also schedules reset if needed. */
1899 return fd_wait_for_completion(DRS->select_date + DP->select_delay,
1900 (timeout_fn)function);
1903 static void floppy_ready(void)
1905 if (FDCS->reset) {
1906 reset_fdc();
1907 return;
1909 if (start_motor(floppy_ready))
1910 return;
1911 if (fdc_dtr())
1912 return;
1914 debug_dcl(DP->flags, "calling disk change from floppy_ready\n");
1915 if (!(raw_cmd->flags & FD_RAW_NO_MOTOR) &&
1916 disk_change(current_drive) && !DP->select_delay)
1917 twaddle(); /* this clears the dcl on certain
1918 * drive/controller combinations */
1920 #ifdef fd_chose_dma_mode
1921 if ((raw_cmd->flags & FD_RAW_READ) || (raw_cmd->flags & FD_RAW_WRITE)) {
1922 unsigned long flags = claim_dma_lock();
1923 fd_chose_dma_mode(raw_cmd->kernel_data, raw_cmd->length);
1924 release_dma_lock(flags);
1926 #endif
1928 if (raw_cmd->flags & (FD_RAW_NEED_SEEK | FD_RAW_NEED_DISK)) {
1929 perpendicular_mode();
1930 fdc_specify(); /* must be done here because of hut, hlt ... */
1931 seek_floppy();
1932 } else {
1933 if ((raw_cmd->flags & FD_RAW_READ) ||
1934 (raw_cmd->flags & FD_RAW_WRITE))
1935 fdc_specify();
1936 setup_rw_floppy();
1940 static void floppy_start(void)
1942 reschedule_timeout(current_reqD, "floppy start");
1944 scandrives();
1945 debug_dcl(DP->flags, "setting NEWCHANGE in floppy_start\n");
1946 set_bit(FD_DISK_NEWCHANGE_BIT, &DRS->flags);
1947 floppy_ready();
1951 * ========================================================================
1952 * here ends the bottom half. Exported routines are:
1953 * floppy_start, floppy_off, floppy_ready, lock_fdc, unlock_fdc, set_fdc,
1954 * start_motor, reset_fdc, reset_fdc_info, interpret_errors.
1955 * Initialization also uses output_byte, result, set_dor, floppy_interrupt
1956 * and set_dor.
1957 * ========================================================================
1960 * General purpose continuations.
1961 * ==============================
1964 static void do_wakeup(void)
1966 reschedule_timeout(MAXTIMEOUT, "do wakeup");
1967 cont = NULL;
1968 command_status += 2;
1969 wake_up(&command_done);
1972 static const struct cont_t wakeup_cont = {
1973 .interrupt = empty,
1974 .redo = do_wakeup,
1975 .error = empty,
1976 .done = (done_f)empty
1979 static const struct cont_t intr_cont = {
1980 .interrupt = empty,
1981 .redo = process_fd_request,
1982 .error = empty,
1983 .done = (done_f)empty
1986 static int wait_til_done(void (*handler)(void), bool interruptible)
1988 int ret;
1990 schedule_bh(handler);
1992 if (interruptible)
1993 wait_event_interruptible(command_done, command_status >= 2);
1994 else
1995 wait_event(command_done, command_status >= 2);
1997 if (command_status < 2) {
1998 cancel_activity();
1999 cont = &intr_cont;
2000 reset_fdc();
2001 return -EINTR;
2004 if (FDCS->reset)
2005 command_status = FD_COMMAND_ERROR;
2006 if (command_status == FD_COMMAND_OKAY)
2007 ret = 0;
2008 else
2009 ret = -EIO;
2010 command_status = FD_COMMAND_NONE;
2011 return ret;
2014 static void generic_done(int result)
2016 command_status = result;
2017 cont = &wakeup_cont;
2020 static void generic_success(void)
2022 cont->done(1);
2025 static void generic_failure(void)
2027 cont->done(0);
2030 static void success_and_wakeup(void)
2032 generic_success();
2033 cont->redo();
2037 * formatting and rw support.
2038 * ==========================
2041 static int next_valid_format(void)
2043 int probed_format;
2045 probed_format = DRS->probed_format;
2046 while (1) {
2047 if (probed_format >= 8 || !DP->autodetect[probed_format]) {
2048 DRS->probed_format = 0;
2049 return 1;
2051 if (floppy_type[DP->autodetect[probed_format]].sect) {
2052 DRS->probed_format = probed_format;
2053 return 0;
2055 probed_format++;
2059 static void bad_flp_intr(void)
2061 int err_count;
2063 if (probing) {
2064 DRS->probed_format++;
2065 if (!next_valid_format())
2066 return;
2068 err_count = ++(*errors);
2069 INFBOUND(DRWE->badness, err_count);
2070 if (err_count > DP->max_errors.abort)
2071 cont->done(0);
2072 if (err_count > DP->max_errors.reset)
2073 FDCS->reset = 1;
2074 else if (err_count > DP->max_errors.recal)
2075 DRS->track = NEED_2_RECAL;
2078 static void set_floppy(int drive)
2080 int type = ITYPE(UDRS->fd_device);
2082 if (type)
2083 _floppy = floppy_type + type;
2084 else
2085 _floppy = current_type[drive];
2089 * formatting support.
2090 * ===================
2092 static void format_interrupt(void)
2094 switch (interpret_errors()) {
2095 case 1:
2096 cont->error();
2097 case 2:
2098 break;
2099 case 0:
2100 cont->done(1);
2102 cont->redo();
2105 #define FM_MODE(x, y) ((y) & ~(((x)->rate & 0x80) >> 1))
2106 #define CT(x) ((x) | 0xc0)
2108 static void setup_format_params(int track)
2110 int n;
2111 int il;
2112 int count;
2113 int head_shift;
2114 int track_shift;
2115 struct fparm {
2116 unsigned char track, head, sect, size;
2117 } *here = (struct fparm *)floppy_track_buffer;
2119 raw_cmd = &default_raw_cmd;
2120 raw_cmd->track = track;
2122 raw_cmd->flags = (FD_RAW_WRITE | FD_RAW_INTR | FD_RAW_SPIN |
2123 FD_RAW_NEED_DISK | FD_RAW_NEED_SEEK);
2124 raw_cmd->rate = _floppy->rate & 0x43;
2125 raw_cmd->cmd_count = NR_F;
2126 COMMAND = FM_MODE(_floppy, FD_FORMAT);
2127 DR_SELECT = UNIT(current_drive) + PH_HEAD(_floppy, format_req.head);
2128 F_SIZECODE = FD_SIZECODE(_floppy);
2129 F_SECT_PER_TRACK = _floppy->sect << 2 >> F_SIZECODE;
2130 F_GAP = _floppy->fmt_gap;
2131 F_FILL = FD_FILL_BYTE;
2133 raw_cmd->kernel_data = floppy_track_buffer;
2134 raw_cmd->length = 4 * F_SECT_PER_TRACK;
2136 /* allow for about 30ms for data transport per track */
2137 head_shift = (F_SECT_PER_TRACK + 5) / 6;
2139 /* a ``cylinder'' is two tracks plus a little stepping time */
2140 track_shift = 2 * head_shift + 3;
2142 /* position of logical sector 1 on this track */
2143 n = (track_shift * format_req.track + head_shift * format_req.head)
2144 % F_SECT_PER_TRACK;
2146 /* determine interleave */
2147 il = 1;
2148 if (_floppy->fmt_gap < 0x22)
2149 il++;
2151 /* initialize field */
2152 for (count = 0; count < F_SECT_PER_TRACK; ++count) {
2153 here[count].track = format_req.track;
2154 here[count].head = format_req.head;
2155 here[count].sect = 0;
2156 here[count].size = F_SIZECODE;
2158 /* place logical sectors */
2159 for (count = 1; count <= F_SECT_PER_TRACK; ++count) {
2160 here[n].sect = count;
2161 n = (n + il) % F_SECT_PER_TRACK;
2162 if (here[n].sect) { /* sector busy, find next free sector */
2163 ++n;
2164 if (n >= F_SECT_PER_TRACK) {
2165 n -= F_SECT_PER_TRACK;
2166 while (here[n].sect)
2167 ++n;
2171 if (_floppy->stretch & FD_SECTBASEMASK) {
2172 for (count = 0; count < F_SECT_PER_TRACK; count++)
2173 here[count].sect += FD_SECTBASE(_floppy) - 1;
2177 static void redo_format(void)
2179 buffer_track = -1;
2180 setup_format_params(format_req.track << STRETCH(_floppy));
2181 floppy_start();
2182 debugt(__func__, "queue format request");
2185 static const struct cont_t format_cont = {
2186 .interrupt = format_interrupt,
2187 .redo = redo_format,
2188 .error = bad_flp_intr,
2189 .done = generic_done
2192 static int do_format(int drive, struct format_descr *tmp_format_req)
2194 int ret;
2196 if (lock_fdc(drive, true))
2197 return -EINTR;
2199 set_floppy(drive);
2200 if (!_floppy ||
2201 _floppy->track > DP->tracks ||
2202 tmp_format_req->track >= _floppy->track ||
2203 tmp_format_req->head >= _floppy->head ||
2204 (_floppy->sect << 2) % (1 << FD_SIZECODE(_floppy)) ||
2205 !_floppy->fmt_gap) {
2206 process_fd_request();
2207 return -EINVAL;
2209 format_req = *tmp_format_req;
2210 format_errors = 0;
2211 cont = &format_cont;
2212 errors = &format_errors;
2213 ret = wait_til_done(redo_format, true);
2214 if (ret == -EINTR)
2215 return -EINTR;
2216 process_fd_request();
2217 return ret;
2221 * Buffer read/write and support
2222 * =============================
2225 static void floppy_end_request(struct request *req, int error)
2227 unsigned int nr_sectors = current_count_sectors;
2228 unsigned int drive = (unsigned long)req->rq_disk->private_data;
2230 /* current_count_sectors can be zero if transfer failed */
2231 if (error)
2232 nr_sectors = blk_rq_cur_sectors(req);
2233 if (__blk_end_request(req, error, nr_sectors << 9))
2234 return;
2236 /* We're done with the request */
2237 floppy_off(drive);
2238 current_req = NULL;
2241 /* new request_done. Can handle physical sectors which are smaller than a
2242 * logical buffer */
2243 static void request_done(int uptodate)
2245 struct request_queue *q = floppy_queue;
2246 struct request *req = current_req;
2247 unsigned long flags;
2248 int block;
2249 char msg[sizeof("request done ") + sizeof(int) * 3];
2251 probing = 0;
2252 snprintf(msg, sizeof(msg), "request done %d", uptodate);
2253 reschedule_timeout(MAXTIMEOUT, msg);
2255 if (!req) {
2256 pr_info("floppy.c: no request in request_done\n");
2257 return;
2260 if (uptodate) {
2261 /* maintain values for invalidation on geometry
2262 * change */
2263 block = current_count_sectors + blk_rq_pos(req);
2264 INFBOUND(DRS->maxblock, block);
2265 if (block > _floppy->sect)
2266 DRS->maxtrack = 1;
2268 /* unlock chained buffers */
2269 spin_lock_irqsave(q->queue_lock, flags);
2270 floppy_end_request(req, 0);
2271 spin_unlock_irqrestore(q->queue_lock, flags);
2272 } else {
2273 if (rq_data_dir(req) == WRITE) {
2274 /* record write error information */
2275 DRWE->write_errors++;
2276 if (DRWE->write_errors == 1) {
2277 DRWE->first_error_sector = blk_rq_pos(req);
2278 DRWE->first_error_generation = DRS->generation;
2280 DRWE->last_error_sector = blk_rq_pos(req);
2281 DRWE->last_error_generation = DRS->generation;
2283 spin_lock_irqsave(q->queue_lock, flags);
2284 floppy_end_request(req, -EIO);
2285 spin_unlock_irqrestore(q->queue_lock, flags);
2289 /* Interrupt handler evaluating the result of the r/w operation */
2290 static void rw_interrupt(void)
2292 int eoc;
2293 int ssize;
2294 int heads;
2295 int nr_sectors;
2297 if (R_HEAD >= 2) {
2298 /* some Toshiba floppy controllers occasionnally seem to
2299 * return bogus interrupts after read/write operations, which
2300 * can be recognized by a bad head number (>= 2) */
2301 return;
2304 if (!DRS->first_read_date)
2305 DRS->first_read_date = jiffies;
2307 nr_sectors = 0;
2308 ssize = DIV_ROUND_UP(1 << SIZECODE, 4);
2310 if (ST1 & ST1_EOC)
2311 eoc = 1;
2312 else
2313 eoc = 0;
2315 if (COMMAND & 0x80)
2316 heads = 2;
2317 else
2318 heads = 1;
2320 nr_sectors = (((R_TRACK - TRACK) * heads +
2321 R_HEAD - HEAD) * SECT_PER_TRACK +
2322 R_SECTOR - SECTOR + eoc) << SIZECODE >> 2;
2324 if (nr_sectors / ssize >
2325 DIV_ROUND_UP(in_sector_offset + current_count_sectors, ssize)) {
2326 DPRINT("long rw: %x instead of %lx\n",
2327 nr_sectors, current_count_sectors);
2328 pr_info("rs=%d s=%d\n", R_SECTOR, SECTOR);
2329 pr_info("rh=%d h=%d\n", R_HEAD, HEAD);
2330 pr_info("rt=%d t=%d\n", R_TRACK, TRACK);
2331 pr_info("heads=%d eoc=%d\n", heads, eoc);
2332 pr_info("spt=%d st=%d ss=%d\n",
2333 SECT_PER_TRACK, fsector_t, ssize);
2334 pr_info("in_sector_offset=%d\n", in_sector_offset);
2337 nr_sectors -= in_sector_offset;
2338 INFBOUND(nr_sectors, 0);
2339 SUPBOUND(current_count_sectors, nr_sectors);
2341 switch (interpret_errors()) {
2342 case 2:
2343 cont->redo();
2344 return;
2345 case 1:
2346 if (!current_count_sectors) {
2347 cont->error();
2348 cont->redo();
2349 return;
2351 break;
2352 case 0:
2353 if (!current_count_sectors) {
2354 cont->redo();
2355 return;
2357 current_type[current_drive] = _floppy;
2358 floppy_sizes[TOMINOR(current_drive)] = _floppy->size;
2359 break;
2362 if (probing) {
2363 if (DP->flags & FTD_MSG)
2364 DPRINT("Auto-detected floppy type %s in fd%d\n",
2365 _floppy->name, current_drive);
2366 current_type[current_drive] = _floppy;
2367 floppy_sizes[TOMINOR(current_drive)] = _floppy->size;
2368 probing = 0;
2371 if (CT(COMMAND) != FD_READ ||
2372 raw_cmd->kernel_data == current_req->buffer) {
2373 /* transfer directly from buffer */
2374 cont->done(1);
2375 } else if (CT(COMMAND) == FD_READ) {
2376 buffer_track = raw_cmd->track;
2377 buffer_drive = current_drive;
2378 INFBOUND(buffer_max, nr_sectors + fsector_t);
2380 cont->redo();
2383 /* Compute maximal contiguous buffer size. */
2384 static int buffer_chain_size(void)
2386 struct bio_vec *bv;
2387 int size;
2388 struct req_iterator iter;
2389 char *base;
2391 base = bio_data(current_req->bio);
2392 size = 0;
2394 rq_for_each_segment(bv, current_req, iter) {
2395 if (page_address(bv->bv_page) + bv->bv_offset != base + size)
2396 break;
2398 size += bv->bv_len;
2401 return size >> 9;
2404 /* Compute the maximal transfer size */
2405 static int transfer_size(int ssize, int max_sector, int max_size)
2407 SUPBOUND(max_sector, fsector_t + max_size);
2409 /* alignment */
2410 max_sector -= (max_sector % _floppy->sect) % ssize;
2412 /* transfer size, beginning not aligned */
2413 current_count_sectors = max_sector - fsector_t;
2415 return max_sector;
2419 * Move data from/to the track buffer to/from the buffer cache.
2421 static void copy_buffer(int ssize, int max_sector, int max_sector_2)
2423 int remaining; /* number of transferred 512-byte sectors */
2424 struct bio_vec *bv;
2425 char *buffer;
2426 char *dma_buffer;
2427 int size;
2428 struct req_iterator iter;
2430 max_sector = transfer_size(ssize,
2431 min(max_sector, max_sector_2),
2432 blk_rq_sectors(current_req));
2434 if (current_count_sectors <= 0 && CT(COMMAND) == FD_WRITE &&
2435 buffer_max > fsector_t + blk_rq_sectors(current_req))
2436 current_count_sectors = min_t(int, buffer_max - fsector_t,
2437 blk_rq_sectors(current_req));
2439 remaining = current_count_sectors << 9;
2440 if (remaining > blk_rq_bytes(current_req) && CT(COMMAND) == FD_WRITE) {
2441 DPRINT("in copy buffer\n");
2442 pr_info("current_count_sectors=%ld\n", current_count_sectors);
2443 pr_info("remaining=%d\n", remaining >> 9);
2444 pr_info("current_req->nr_sectors=%u\n",
2445 blk_rq_sectors(current_req));
2446 pr_info("current_req->current_nr_sectors=%u\n",
2447 blk_rq_cur_sectors(current_req));
2448 pr_info("max_sector=%d\n", max_sector);
2449 pr_info("ssize=%d\n", ssize);
2452 buffer_max = max(max_sector, buffer_max);
2454 dma_buffer = floppy_track_buffer + ((fsector_t - buffer_min) << 9);
2456 size = blk_rq_cur_bytes(current_req);
2458 rq_for_each_segment(bv, current_req, iter) {
2459 if (!remaining)
2460 break;
2462 size = bv->bv_len;
2463 SUPBOUND(size, remaining);
2465 buffer = page_address(bv->bv_page) + bv->bv_offset;
2466 if (dma_buffer + size >
2467 floppy_track_buffer + (max_buffer_sectors << 10) ||
2468 dma_buffer < floppy_track_buffer) {
2469 DPRINT("buffer overrun in copy buffer %d\n",
2470 (int)((floppy_track_buffer - dma_buffer) >> 9));
2471 pr_info("fsector_t=%d buffer_min=%d\n",
2472 fsector_t, buffer_min);
2473 pr_info("current_count_sectors=%ld\n",
2474 current_count_sectors);
2475 if (CT(COMMAND) == FD_READ)
2476 pr_info("read\n");
2477 if (CT(COMMAND) == FD_WRITE)
2478 pr_info("write\n");
2479 break;
2481 if (((unsigned long)buffer) % 512)
2482 DPRINT("%p buffer not aligned\n", buffer);
2484 if (CT(COMMAND) == FD_READ)
2485 memcpy(buffer, dma_buffer, size);
2486 else
2487 memcpy(dma_buffer, buffer, size);
2489 remaining -= size;
2490 dma_buffer += size;
2492 if (remaining) {
2493 if (remaining > 0)
2494 max_sector -= remaining >> 9;
2495 DPRINT("weirdness: remaining %d\n", remaining >> 9);
2499 static void virtualdmabug_workaround(void)
2501 int hard_sectors;
2502 int end_sector;
2504 if (CT(COMMAND) == FD_WRITE) {
2505 COMMAND &= ~0x80; /* switch off multiple track mode */
2507 hard_sectors = raw_cmd->length >> (7 + SIZECODE);
2508 end_sector = SECTOR + hard_sectors - 1;
2509 if (end_sector > SECT_PER_TRACK) {
2510 pr_info("too many sectors %d > %d\n",
2511 end_sector, SECT_PER_TRACK);
2512 return;
2514 SECT_PER_TRACK = end_sector;
2515 /* make sure SECT_PER_TRACK
2516 * points to end of transfer */
2521 * Formulate a read/write request.
2522 * this routine decides where to load the data (directly to buffer, or to
2523 * tmp floppy area), how much data to load (the size of the buffer, the whole
2524 * track, or a single sector)
2525 * All floppy_track_buffer handling goes in here. If we ever add track buffer
2526 * allocation on the fly, it should be done here. No other part should need
2527 * modification.
2530 static int make_raw_rw_request(void)
2532 int aligned_sector_t;
2533 int max_sector;
2534 int max_size;
2535 int tracksize;
2536 int ssize;
2538 if (WARN(max_buffer_sectors == 0, "VFS: Block I/O scheduled on unopened device\n"))
2539 return 0;
2541 set_fdc((long)current_req->rq_disk->private_data);
2543 raw_cmd = &default_raw_cmd;
2544 raw_cmd->flags = FD_RAW_SPIN | FD_RAW_NEED_DISK | FD_RAW_NEED_DISK |
2545 FD_RAW_NEED_SEEK;
2546 raw_cmd->cmd_count = NR_RW;
2547 if (rq_data_dir(current_req) == READ) {
2548 raw_cmd->flags |= FD_RAW_READ;
2549 COMMAND = FM_MODE(_floppy, FD_READ);
2550 } else if (rq_data_dir(current_req) == WRITE) {
2551 raw_cmd->flags |= FD_RAW_WRITE;
2552 COMMAND = FM_MODE(_floppy, FD_WRITE);
2553 } else {
2554 DPRINT("%s: unknown command\n", __func__);
2555 return 0;
2558 max_sector = _floppy->sect * _floppy->head;
2560 TRACK = (int)blk_rq_pos(current_req) / max_sector;
2561 fsector_t = (int)blk_rq_pos(current_req) % max_sector;
2562 if (_floppy->track && TRACK >= _floppy->track) {
2563 if (blk_rq_cur_sectors(current_req) & 1) {
2564 current_count_sectors = 1;
2565 return 1;
2566 } else
2567 return 0;
2569 HEAD = fsector_t / _floppy->sect;
2571 if (((_floppy->stretch & (FD_SWAPSIDES | FD_SECTBASEMASK)) ||
2572 test_bit(FD_NEED_TWADDLE_BIT, &DRS->flags)) &&
2573 fsector_t < _floppy->sect)
2574 max_sector = _floppy->sect;
2576 /* 2M disks have phantom sectors on the first track */
2577 if ((_floppy->rate & FD_2M) && (!TRACK) && (!HEAD)) {
2578 max_sector = 2 * _floppy->sect / 3;
2579 if (fsector_t >= max_sector) {
2580 current_count_sectors =
2581 min_t(int, _floppy->sect - fsector_t,
2582 blk_rq_sectors(current_req));
2583 return 1;
2585 SIZECODE = 2;
2586 } else
2587 SIZECODE = FD_SIZECODE(_floppy);
2588 raw_cmd->rate = _floppy->rate & 0x43;
2589 if ((_floppy->rate & FD_2M) && (TRACK || HEAD) && raw_cmd->rate == 2)
2590 raw_cmd->rate = 1;
2592 if (SIZECODE)
2593 SIZECODE2 = 0xff;
2594 else
2595 SIZECODE2 = 0x80;
2596 raw_cmd->track = TRACK << STRETCH(_floppy);
2597 DR_SELECT = UNIT(current_drive) + PH_HEAD(_floppy, HEAD);
2598 GAP = _floppy->gap;
2599 ssize = DIV_ROUND_UP(1 << SIZECODE, 4);
2600 SECT_PER_TRACK = _floppy->sect << 2 >> SIZECODE;
2601 SECTOR = ((fsector_t % _floppy->sect) << 2 >> SIZECODE) +
2602 FD_SECTBASE(_floppy);
2604 /* tracksize describes the size which can be filled up with sectors
2605 * of size ssize.
2607 tracksize = _floppy->sect - _floppy->sect % ssize;
2608 if (tracksize < _floppy->sect) {
2609 SECT_PER_TRACK++;
2610 if (tracksize <= fsector_t % _floppy->sect)
2611 SECTOR--;
2613 /* if we are beyond tracksize, fill up using smaller sectors */
2614 while (tracksize <= fsector_t % _floppy->sect) {
2615 while (tracksize + ssize > _floppy->sect) {
2616 SIZECODE--;
2617 ssize >>= 1;
2619 SECTOR++;
2620 SECT_PER_TRACK++;
2621 tracksize += ssize;
2623 max_sector = HEAD * _floppy->sect + tracksize;
2624 } else if (!TRACK && !HEAD && !(_floppy->rate & FD_2M) && probing) {
2625 max_sector = _floppy->sect;
2626 } else if (!HEAD && CT(COMMAND) == FD_WRITE) {
2627 max_sector = _floppy->sect;
2630 in_sector_offset = (fsector_t % _floppy->sect) % ssize;
2631 aligned_sector_t = fsector_t - in_sector_offset;
2632 max_size = blk_rq_sectors(current_req);
2633 if ((raw_cmd->track == buffer_track) &&
2634 (current_drive == buffer_drive) &&
2635 (fsector_t >= buffer_min) && (fsector_t < buffer_max)) {
2636 /* data already in track buffer */
2637 if (CT(COMMAND) == FD_READ) {
2638 copy_buffer(1, max_sector, buffer_max);
2639 return 1;
2641 } else if (in_sector_offset || blk_rq_sectors(current_req) < ssize) {
2642 if (CT(COMMAND) == FD_WRITE) {
2643 unsigned int sectors;
2645 sectors = fsector_t + blk_rq_sectors(current_req);
2646 if (sectors > ssize && sectors < ssize + ssize)
2647 max_size = ssize + ssize;
2648 else
2649 max_size = ssize;
2651 raw_cmd->flags &= ~FD_RAW_WRITE;
2652 raw_cmd->flags |= FD_RAW_READ;
2653 COMMAND = FM_MODE(_floppy, FD_READ);
2654 } else if ((unsigned long)current_req->buffer < MAX_DMA_ADDRESS) {
2655 unsigned long dma_limit;
2656 int direct, indirect;
2658 indirect =
2659 transfer_size(ssize, max_sector,
2660 max_buffer_sectors * 2) - fsector_t;
2663 * Do NOT use minimum() here---MAX_DMA_ADDRESS is 64 bits wide
2664 * on a 64 bit machine!
2666 max_size = buffer_chain_size();
2667 dma_limit = (MAX_DMA_ADDRESS -
2668 ((unsigned long)current_req->buffer)) >> 9;
2669 if ((unsigned long)max_size > dma_limit)
2670 max_size = dma_limit;
2671 /* 64 kb boundaries */
2672 if (CROSS_64KB(current_req->buffer, max_size << 9))
2673 max_size = (K_64 -
2674 ((unsigned long)current_req->buffer) %
2675 K_64) >> 9;
2676 direct = transfer_size(ssize, max_sector, max_size) - fsector_t;
2678 * We try to read tracks, but if we get too many errors, we
2679 * go back to reading just one sector at a time.
2681 * This means we should be able to read a sector even if there
2682 * are other bad sectors on this track.
2684 if (!direct ||
2685 (indirect * 2 > direct * 3 &&
2686 *errors < DP->max_errors.read_track &&
2687 ((!probing ||
2688 (DP->read_track & (1 << DRS->probed_format)))))) {
2689 max_size = blk_rq_sectors(current_req);
2690 } else {
2691 raw_cmd->kernel_data = current_req->buffer;
2692 raw_cmd->length = current_count_sectors << 9;
2693 if (raw_cmd->length == 0) {
2694 DPRINT("%s: zero dma transfer attempted\n", __func__);
2695 DPRINT("indirect=%d direct=%d fsector_t=%d\n",
2696 indirect, direct, fsector_t);
2697 return 0;
2699 virtualdmabug_workaround();
2700 return 2;
2704 if (CT(COMMAND) == FD_READ)
2705 max_size = max_sector; /* unbounded */
2707 /* claim buffer track if needed */
2708 if (buffer_track != raw_cmd->track || /* bad track */
2709 buffer_drive != current_drive || /* bad drive */
2710 fsector_t > buffer_max ||
2711 fsector_t < buffer_min ||
2712 ((CT(COMMAND) == FD_READ ||
2713 (!in_sector_offset && blk_rq_sectors(current_req) >= ssize)) &&
2714 max_sector > 2 * max_buffer_sectors + buffer_min &&
2715 max_size + fsector_t > 2 * max_buffer_sectors + buffer_min)) {
2716 /* not enough space */
2717 buffer_track = -1;
2718 buffer_drive = current_drive;
2719 buffer_max = buffer_min = aligned_sector_t;
2721 raw_cmd->kernel_data = floppy_track_buffer +
2722 ((aligned_sector_t - buffer_min) << 9);
2724 if (CT(COMMAND) == FD_WRITE) {
2725 /* copy write buffer to track buffer.
2726 * if we get here, we know that the write
2727 * is either aligned or the data already in the buffer
2728 * (buffer will be overwritten) */
2729 if (in_sector_offset && buffer_track == -1)
2730 DPRINT("internal error offset !=0 on write\n");
2731 buffer_track = raw_cmd->track;
2732 buffer_drive = current_drive;
2733 copy_buffer(ssize, max_sector,
2734 2 * max_buffer_sectors + buffer_min);
2735 } else
2736 transfer_size(ssize, max_sector,
2737 2 * max_buffer_sectors + buffer_min -
2738 aligned_sector_t);
2740 /* round up current_count_sectors to get dma xfer size */
2741 raw_cmd->length = in_sector_offset + current_count_sectors;
2742 raw_cmd->length = ((raw_cmd->length - 1) | (ssize - 1)) + 1;
2743 raw_cmd->length <<= 9;
2744 if ((raw_cmd->length < current_count_sectors << 9) ||
2745 (raw_cmd->kernel_data != current_req->buffer &&
2746 CT(COMMAND) == FD_WRITE &&
2747 (aligned_sector_t + (raw_cmd->length >> 9) > buffer_max ||
2748 aligned_sector_t < buffer_min)) ||
2749 raw_cmd->length % (128 << SIZECODE) ||
2750 raw_cmd->length <= 0 || current_count_sectors <= 0) {
2751 DPRINT("fractionary current count b=%lx s=%lx\n",
2752 raw_cmd->length, current_count_sectors);
2753 if (raw_cmd->kernel_data != current_req->buffer)
2754 pr_info("addr=%d, length=%ld\n",
2755 (int)((raw_cmd->kernel_data -
2756 floppy_track_buffer) >> 9),
2757 current_count_sectors);
2758 pr_info("st=%d ast=%d mse=%d msi=%d\n",
2759 fsector_t, aligned_sector_t, max_sector, max_size);
2760 pr_info("ssize=%x SIZECODE=%d\n", ssize, SIZECODE);
2761 pr_info("command=%x SECTOR=%d HEAD=%d, TRACK=%d\n",
2762 COMMAND, SECTOR, HEAD, TRACK);
2763 pr_info("buffer drive=%d\n", buffer_drive);
2764 pr_info("buffer track=%d\n", buffer_track);
2765 pr_info("buffer_min=%d\n", buffer_min);
2766 pr_info("buffer_max=%d\n", buffer_max);
2767 return 0;
2770 if (raw_cmd->kernel_data != current_req->buffer) {
2771 if (raw_cmd->kernel_data < floppy_track_buffer ||
2772 current_count_sectors < 0 ||
2773 raw_cmd->length < 0 ||
2774 raw_cmd->kernel_data + raw_cmd->length >
2775 floppy_track_buffer + (max_buffer_sectors << 10)) {
2776 DPRINT("buffer overrun in schedule dma\n");
2777 pr_info("fsector_t=%d buffer_min=%d current_count=%ld\n",
2778 fsector_t, buffer_min, raw_cmd->length >> 9);
2779 pr_info("current_count_sectors=%ld\n",
2780 current_count_sectors);
2781 if (CT(COMMAND) == FD_READ)
2782 pr_info("read\n");
2783 if (CT(COMMAND) == FD_WRITE)
2784 pr_info("write\n");
2785 return 0;
2787 } else if (raw_cmd->length > blk_rq_bytes(current_req) ||
2788 current_count_sectors > blk_rq_sectors(current_req)) {
2789 DPRINT("buffer overrun in direct transfer\n");
2790 return 0;
2791 } else if (raw_cmd->length < current_count_sectors << 9) {
2792 DPRINT("more sectors than bytes\n");
2793 pr_info("bytes=%ld\n", raw_cmd->length >> 9);
2794 pr_info("sectors=%ld\n", current_count_sectors);
2796 if (raw_cmd->length == 0) {
2797 DPRINT("zero dma transfer attempted from make_raw_request\n");
2798 return 0;
2801 virtualdmabug_workaround();
2802 return 2;
2805 static void redo_fd_request(void)
2807 int drive;
2808 int tmp;
2810 lastredo = jiffies;
2811 if (current_drive < N_DRIVE)
2812 floppy_off(current_drive);
2814 do_request:
2815 if (!current_req) {
2816 struct request *req;
2818 spin_lock_irq(floppy_queue->queue_lock);
2819 req = blk_fetch_request(floppy_queue);
2820 spin_unlock_irq(floppy_queue->queue_lock);
2821 if (!req) {
2822 do_floppy = NULL;
2823 unlock_fdc();
2824 return;
2826 current_req = req;
2828 drive = (long)current_req->rq_disk->private_data;
2829 set_fdc(drive);
2830 reschedule_timeout(current_reqD, "redo fd request");
2832 set_floppy(drive);
2833 raw_cmd = &default_raw_cmd;
2834 raw_cmd->flags = 0;
2835 if (start_motor(redo_fd_request))
2836 return;
2838 disk_change(current_drive);
2839 if (test_bit(current_drive, &fake_change) ||
2840 test_bit(FD_DISK_CHANGED_BIT, &DRS->flags)) {
2841 DPRINT("disk absent or changed during operation\n");
2842 request_done(0);
2843 goto do_request;
2845 if (!_floppy) { /* Autodetection */
2846 if (!probing) {
2847 DRS->probed_format = 0;
2848 if (next_valid_format()) {
2849 DPRINT("no autodetectable formats\n");
2850 _floppy = NULL;
2851 request_done(0);
2852 goto do_request;
2855 probing = 1;
2856 _floppy = floppy_type + DP->autodetect[DRS->probed_format];
2857 } else
2858 probing = 0;
2859 errors = &(current_req->errors);
2860 tmp = make_raw_rw_request();
2861 if (tmp < 2) {
2862 request_done(tmp);
2863 goto do_request;
2866 if (test_bit(FD_NEED_TWADDLE_BIT, &DRS->flags))
2867 twaddle();
2868 schedule_bh(floppy_start);
2869 debugt(__func__, "queue fd request");
2870 return;
2873 static const struct cont_t rw_cont = {
2874 .interrupt = rw_interrupt,
2875 .redo = redo_fd_request,
2876 .error = bad_flp_intr,
2877 .done = request_done
2880 static void process_fd_request(void)
2882 cont = &rw_cont;
2883 schedule_bh(redo_fd_request);
2886 static void do_fd_request(struct request_queue *q)
2888 if (WARN(max_buffer_sectors == 0,
2889 "VFS: %s called on non-open device\n", __func__))
2890 return;
2892 if (WARN(atomic_read(&usage_count) == 0,
2893 "warning: usage count=0, current_req=%p sect=%ld type=%x flags=%x\n",
2894 current_req, (long)blk_rq_pos(current_req), current_req->cmd_type,
2895 current_req->cmd_flags))
2896 return;
2898 if (test_bit(0, &fdc_busy)) {
2899 /* fdc busy, this new request will be treated when the
2900 current one is done */
2901 is_alive(__func__, "old request running");
2902 return;
2904 lock_fdc(MAXTIMEOUT, false);
2905 process_fd_request();
2906 is_alive(__func__, "");
2909 static const struct cont_t poll_cont = {
2910 .interrupt = success_and_wakeup,
2911 .redo = floppy_ready,
2912 .error = generic_failure,
2913 .done = generic_done
2916 static int poll_drive(bool interruptible, int flag)
2918 /* no auto-sense, just clear dcl */
2919 raw_cmd = &default_raw_cmd;
2920 raw_cmd->flags = flag;
2921 raw_cmd->track = 0;
2922 raw_cmd->cmd_count = 0;
2923 cont = &poll_cont;
2924 debug_dcl(DP->flags, "setting NEWCHANGE in poll_drive\n");
2925 set_bit(FD_DISK_NEWCHANGE_BIT, &DRS->flags);
2927 return wait_til_done(floppy_ready, interruptible);
2931 * User triggered reset
2932 * ====================
2935 static void reset_intr(void)
2937 pr_info("weird, reset interrupt called\n");
2940 static const struct cont_t reset_cont = {
2941 .interrupt = reset_intr,
2942 .redo = success_and_wakeup,
2943 .error = generic_failure,
2944 .done = generic_done
2947 static int user_reset_fdc(int drive, int arg, bool interruptible)
2949 int ret;
2951 if (lock_fdc(drive, interruptible))
2952 return -EINTR;
2954 if (arg == FD_RESET_ALWAYS)
2955 FDCS->reset = 1;
2956 if (FDCS->reset) {
2957 cont = &reset_cont;
2958 ret = wait_til_done(reset_fdc, interruptible);
2959 if (ret == -EINTR)
2960 return -EINTR;
2962 process_fd_request();
2963 return 0;
2967 * Misc Ioctl's and support
2968 * ========================
2970 static inline int fd_copyout(void __user *param, const void *address,
2971 unsigned long size)
2973 return copy_to_user(param, address, size) ? -EFAULT : 0;
2976 static inline int fd_copyin(void __user *param, void *address,
2977 unsigned long size)
2979 return copy_from_user(address, param, size) ? -EFAULT : 0;
2982 static const char *drive_name(int type, int drive)
2984 struct floppy_struct *floppy;
2986 if (type)
2987 floppy = floppy_type + type;
2988 else {
2989 if (UDP->native_format)
2990 floppy = floppy_type + UDP->native_format;
2991 else
2992 return "(null)";
2994 if (floppy->name)
2995 return floppy->name;
2996 else
2997 return "(null)";
3000 /* raw commands */
3001 static void raw_cmd_done(int flag)
3003 int i;
3005 if (!flag) {
3006 raw_cmd->flags |= FD_RAW_FAILURE;
3007 raw_cmd->flags |= FD_RAW_HARDFAILURE;
3008 } else {
3009 raw_cmd->reply_count = inr;
3010 if (raw_cmd->reply_count > MAX_REPLIES)
3011 raw_cmd->reply_count = 0;
3012 for (i = 0; i < raw_cmd->reply_count; i++)
3013 raw_cmd->reply[i] = reply_buffer[i];
3015 if (raw_cmd->flags & (FD_RAW_READ | FD_RAW_WRITE)) {
3016 unsigned long flags;
3017 flags = claim_dma_lock();
3018 raw_cmd->length = fd_get_dma_residue();
3019 release_dma_lock(flags);
3022 if ((raw_cmd->flags & FD_RAW_SOFTFAILURE) &&
3023 (!raw_cmd->reply_count || (raw_cmd->reply[0] & 0xc0)))
3024 raw_cmd->flags |= FD_RAW_FAILURE;
3026 if (disk_change(current_drive))
3027 raw_cmd->flags |= FD_RAW_DISK_CHANGE;
3028 else
3029 raw_cmd->flags &= ~FD_RAW_DISK_CHANGE;
3030 if (raw_cmd->flags & FD_RAW_NO_MOTOR_AFTER)
3031 motor_off_callback(current_drive);
3033 if (raw_cmd->next &&
3034 (!(raw_cmd->flags & FD_RAW_FAILURE) ||
3035 !(raw_cmd->flags & FD_RAW_STOP_IF_FAILURE)) &&
3036 ((raw_cmd->flags & FD_RAW_FAILURE) ||
3037 !(raw_cmd->flags & FD_RAW_STOP_IF_SUCCESS))) {
3038 raw_cmd = raw_cmd->next;
3039 return;
3042 generic_done(flag);
3045 static const struct cont_t raw_cmd_cont = {
3046 .interrupt = success_and_wakeup,
3047 .redo = floppy_start,
3048 .error = generic_failure,
3049 .done = raw_cmd_done
3052 static int raw_cmd_copyout(int cmd, void __user *param,
3053 struct floppy_raw_cmd *ptr)
3055 int ret;
3057 while (ptr) {
3058 ret = copy_to_user(param, ptr, sizeof(*ptr));
3059 if (ret)
3060 return -EFAULT;
3061 param += sizeof(struct floppy_raw_cmd);
3062 if ((ptr->flags & FD_RAW_READ) && ptr->buffer_length) {
3063 if (ptr->length >= 0 &&
3064 ptr->length <= ptr->buffer_length) {
3065 long length = ptr->buffer_length - ptr->length;
3066 ret = fd_copyout(ptr->data, ptr->kernel_data,
3067 length);
3068 if (ret)
3069 return ret;
3072 ptr = ptr->next;
3075 return 0;
3078 static void raw_cmd_free(struct floppy_raw_cmd **ptr)
3080 struct floppy_raw_cmd *next;
3081 struct floppy_raw_cmd *this;
3083 this = *ptr;
3084 *ptr = NULL;
3085 while (this) {
3086 if (this->buffer_length) {
3087 fd_dma_mem_free((unsigned long)this->kernel_data,
3088 this->buffer_length);
3089 this->buffer_length = 0;
3091 next = this->next;
3092 kfree(this);
3093 this = next;
3097 static int raw_cmd_copyin(int cmd, void __user *param,
3098 struct floppy_raw_cmd **rcmd)
3100 struct floppy_raw_cmd *ptr;
3101 int ret;
3102 int i;
3104 *rcmd = NULL;
3106 loop:
3107 ptr = kmalloc(sizeof(struct floppy_raw_cmd), GFP_USER);
3108 if (!ptr)
3109 return -ENOMEM;
3110 *rcmd = ptr;
3111 ret = copy_from_user(ptr, param, sizeof(*ptr));
3112 if (ret)
3113 return -EFAULT;
3114 ptr->next = NULL;
3115 ptr->buffer_length = 0;
3116 param += sizeof(struct floppy_raw_cmd);
3117 if (ptr->cmd_count > 33)
3118 /* the command may now also take up the space
3119 * initially intended for the reply & the
3120 * reply count. Needed for long 82078 commands
3121 * such as RESTORE, which takes ... 17 command
3122 * bytes. Murphy's law #137: When you reserve
3123 * 16 bytes for a structure, you'll one day
3124 * discover that you really need 17...
3126 return -EINVAL;
3128 for (i = 0; i < 16; i++)
3129 ptr->reply[i] = 0;
3130 ptr->resultcode = 0;
3131 ptr->kernel_data = NULL;
3133 if (ptr->flags & (FD_RAW_READ | FD_RAW_WRITE)) {
3134 if (ptr->length <= 0)
3135 return -EINVAL;
3136 ptr->kernel_data = (char *)fd_dma_mem_alloc(ptr->length);
3137 fallback_on_nodma_alloc(&ptr->kernel_data, ptr->length);
3138 if (!ptr->kernel_data)
3139 return -ENOMEM;
3140 ptr->buffer_length = ptr->length;
3142 if (ptr->flags & FD_RAW_WRITE) {
3143 ret = fd_copyin(ptr->data, ptr->kernel_data, ptr->length);
3144 if (ret)
3145 return ret;
3148 if (ptr->flags & FD_RAW_MORE) {
3149 rcmd = &(ptr->next);
3150 ptr->rate &= 0x43;
3151 goto loop;
3154 return 0;
3157 static int raw_cmd_ioctl(int cmd, void __user *param)
3159 struct floppy_raw_cmd *my_raw_cmd;
3160 int drive;
3161 int ret2;
3162 int ret;
3164 if (FDCS->rawcmd <= 1)
3165 FDCS->rawcmd = 1;
3166 for (drive = 0; drive < N_DRIVE; drive++) {
3167 if (FDC(drive) != fdc)
3168 continue;
3169 if (drive == current_drive) {
3170 if (UDRS->fd_ref > 1) {
3171 FDCS->rawcmd = 2;
3172 break;
3174 } else if (UDRS->fd_ref) {
3175 FDCS->rawcmd = 2;
3176 break;
3180 if (FDCS->reset)
3181 return -EIO;
3183 ret = raw_cmd_copyin(cmd, param, &my_raw_cmd);
3184 if (ret) {
3185 raw_cmd_free(&my_raw_cmd);
3186 return ret;
3189 raw_cmd = my_raw_cmd;
3190 cont = &raw_cmd_cont;
3191 ret = wait_til_done(floppy_start, true);
3192 debug_dcl(DP->flags, "calling disk change from raw_cmd ioctl\n");
3194 if (ret != -EINTR && FDCS->reset)
3195 ret = -EIO;
3197 DRS->track = NO_TRACK;
3199 ret2 = raw_cmd_copyout(cmd, param, my_raw_cmd);
3200 if (!ret)
3201 ret = ret2;
3202 raw_cmd_free(&my_raw_cmd);
3203 return ret;
3206 static int invalidate_drive(struct block_device *bdev)
3208 /* invalidate the buffer track to force a reread */
3209 set_bit((long)bdev->bd_disk->private_data, &fake_change);
3210 process_fd_request();
3211 check_disk_change(bdev);
3212 return 0;
3215 static int set_geometry(unsigned int cmd, struct floppy_struct *g,
3216 int drive, int type, struct block_device *bdev)
3218 int cnt;
3220 /* sanity checking for parameters. */
3221 if (g->sect <= 0 ||
3222 g->head <= 0 ||
3223 g->track <= 0 || g->track > UDP->tracks >> STRETCH(g) ||
3224 /* check if reserved bits are set */
3225 (g->stretch & ~(FD_STRETCH | FD_SWAPSIDES | FD_SECTBASEMASK)) != 0)
3226 return -EINVAL;
3227 if (type) {
3228 if (!capable(CAP_SYS_ADMIN))
3229 return -EPERM;
3230 mutex_lock(&open_lock);
3231 if (lock_fdc(drive, true)) {
3232 mutex_unlock(&open_lock);
3233 return -EINTR;
3235 floppy_type[type] = *g;
3236 floppy_type[type].name = "user format";
3237 for (cnt = type << 2; cnt < (type << 2) + 4; cnt++)
3238 floppy_sizes[cnt] = floppy_sizes[cnt + 0x80] =
3239 floppy_type[type].size + 1;
3240 process_fd_request();
3241 for (cnt = 0; cnt < N_DRIVE; cnt++) {
3242 struct block_device *bdev = opened_bdev[cnt];
3243 if (!bdev || ITYPE(drive_state[cnt].fd_device) != type)
3244 continue;
3245 __invalidate_device(bdev);
3247 mutex_unlock(&open_lock);
3248 } else {
3249 int oldStretch;
3251 if (lock_fdc(drive, true))
3252 return -EINTR;
3253 if (cmd != FDDEFPRM) {
3254 /* notice a disk change immediately, else
3255 * we lose our settings immediately*/
3256 if (poll_drive(true, FD_RAW_NEED_DISK) == -EINTR)
3257 return -EINTR;
3259 oldStretch = g->stretch;
3260 user_params[drive] = *g;
3261 if (buffer_drive == drive)
3262 SUPBOUND(buffer_max, user_params[drive].sect);
3263 current_type[drive] = &user_params[drive];
3264 floppy_sizes[drive] = user_params[drive].size;
3265 if (cmd == FDDEFPRM)
3266 DRS->keep_data = -1;
3267 else
3268 DRS->keep_data = 1;
3269 /* invalidation. Invalidate only when needed, i.e.
3270 * when there are already sectors in the buffer cache
3271 * whose number will change. This is useful, because
3272 * mtools often changes the geometry of the disk after
3273 * looking at the boot block */
3274 if (DRS->maxblock > user_params[drive].sect ||
3275 DRS->maxtrack ||
3276 ((user_params[drive].sect ^ oldStretch) &
3277 (FD_SWAPSIDES | FD_SECTBASEMASK)))
3278 invalidate_drive(bdev);
3279 else
3280 process_fd_request();
3282 return 0;
3285 /* handle obsolete ioctl's */
3286 static unsigned int ioctl_table[] = {
3287 FDCLRPRM,
3288 FDSETPRM,
3289 FDDEFPRM,
3290 FDGETPRM,
3291 FDMSGON,
3292 FDMSGOFF,
3293 FDFMTBEG,
3294 FDFMTTRK,
3295 FDFMTEND,
3296 FDSETEMSGTRESH,
3297 FDFLUSH,
3298 FDSETMAXERRS,
3299 FDGETMAXERRS,
3300 FDGETDRVTYP,
3301 FDSETDRVPRM,
3302 FDGETDRVPRM,
3303 FDGETDRVSTAT,
3304 FDPOLLDRVSTAT,
3305 FDRESET,
3306 FDGETFDCSTAT,
3307 FDWERRORCLR,
3308 FDWERRORGET,
3309 FDRAWCMD,
3310 FDEJECT,
3311 FDTWADDLE
3314 static int normalize_ioctl(unsigned int *cmd, int *size)
3316 int i;
3318 for (i = 0; i < ARRAY_SIZE(ioctl_table); i++) {
3319 if ((*cmd & 0xffff) == (ioctl_table[i] & 0xffff)) {
3320 *size = _IOC_SIZE(*cmd);
3321 *cmd = ioctl_table[i];
3322 if (*size > _IOC_SIZE(*cmd)) {
3323 pr_info("ioctl not yet supported\n");
3324 return -EFAULT;
3326 return 0;
3329 return -EINVAL;
3332 static int get_floppy_geometry(int drive, int type, struct floppy_struct **g)
3334 if (type)
3335 *g = &floppy_type[type];
3336 else {
3337 if (lock_fdc(drive, false))
3338 return -EINTR;
3339 if (poll_drive(false, 0) == -EINTR)
3340 return -EINTR;
3341 process_fd_request();
3342 *g = current_type[drive];
3344 if (!*g)
3345 return -ENODEV;
3346 return 0;
3349 static int fd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
3351 int drive = (long)bdev->bd_disk->private_data;
3352 int type = ITYPE(drive_state[drive].fd_device);
3353 struct floppy_struct *g;
3354 int ret;
3356 ret = get_floppy_geometry(drive, type, &g);
3357 if (ret)
3358 return ret;
3360 geo->heads = g->head;
3361 geo->sectors = g->sect;
3362 geo->cylinders = g->track;
3363 return 0;
3366 static int fd_locked_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd,
3367 unsigned long param)
3369 int drive = (long)bdev->bd_disk->private_data;
3370 int type = ITYPE(UDRS->fd_device);
3371 int i;
3372 int ret;
3373 int size;
3374 union inparam {
3375 struct floppy_struct g; /* geometry */
3376 struct format_descr f;
3377 struct floppy_max_errors max_errors;
3378 struct floppy_drive_params dp;
3379 } inparam; /* parameters coming from user space */
3380 const void *outparam; /* parameters passed back to user space */
3382 /* convert compatibility eject ioctls into floppy eject ioctl.
3383 * We do this in order to provide a means to eject floppy disks before
3384 * installing the new fdutils package */
3385 if (cmd == CDROMEJECT || /* CD-ROM eject */
3386 cmd == 0x6470) { /* SunOS floppy eject */
3387 DPRINT("obsolete eject ioctl\n");
3388 DPRINT("please use floppycontrol --eject\n");
3389 cmd = FDEJECT;
3392 if (!((cmd & 0xff00) == 0x0200))
3393 return -EINVAL;
3395 /* convert the old style command into a new style command */
3396 ret = normalize_ioctl(&cmd, &size);
3397 if (ret)
3398 return ret;
3400 /* permission checks */
3401 if (((cmd & 0x40) && !(mode & (FMODE_WRITE | FMODE_WRITE_IOCTL))) ||
3402 ((cmd & 0x80) && !capable(CAP_SYS_ADMIN)))
3403 return -EPERM;
3405 if (WARN_ON(size < 0 || size > sizeof(inparam)))
3406 return -EINVAL;
3408 /* copyin */
3409 memset(&inparam, 0, sizeof(inparam));
3410 if (_IOC_DIR(cmd) & _IOC_WRITE) {
3411 ret = fd_copyin((void __user *)param, &inparam, size);
3412 if (ret)
3413 return ret;
3416 switch (cmd) {
3417 case FDEJECT:
3418 if (UDRS->fd_ref != 1)
3419 /* somebody else has this drive open */
3420 return -EBUSY;
3421 if (lock_fdc(drive, true))
3422 return -EINTR;
3424 /* do the actual eject. Fails on
3425 * non-Sparc architectures */
3426 ret = fd_eject(UNIT(drive));
3428 set_bit(FD_DISK_CHANGED_BIT, &UDRS->flags);
3429 set_bit(FD_VERIFY_BIT, &UDRS->flags);
3430 process_fd_request();
3431 return ret;
3432 case FDCLRPRM:
3433 if (lock_fdc(drive, true))
3434 return -EINTR;
3435 current_type[drive] = NULL;
3436 floppy_sizes[drive] = MAX_DISK_SIZE << 1;
3437 UDRS->keep_data = 0;
3438 return invalidate_drive(bdev);
3439 case FDSETPRM:
3440 case FDDEFPRM:
3441 return set_geometry(cmd, &inparam.g, drive, type, bdev);
3442 case FDGETPRM:
3443 ret = get_floppy_geometry(drive, type,
3444 (struct floppy_struct **)&outparam);
3445 if (ret)
3446 return ret;
3447 break;
3448 case FDMSGON:
3449 UDP->flags |= FTD_MSG;
3450 return 0;
3451 case FDMSGOFF:
3452 UDP->flags &= ~FTD_MSG;
3453 return 0;
3454 case FDFMTBEG:
3455 if (lock_fdc(drive, true))
3456 return -EINTR;
3457 if (poll_drive(true, FD_RAW_NEED_DISK) == -EINTR)
3458 return -EINTR;
3459 ret = UDRS->flags;
3460 process_fd_request();
3461 if (ret & FD_VERIFY)
3462 return -ENODEV;
3463 if (!(ret & FD_DISK_WRITABLE))
3464 return -EROFS;
3465 return 0;
3466 case FDFMTTRK:
3467 if (UDRS->fd_ref != 1)
3468 return -EBUSY;
3469 return do_format(drive, &inparam.f);
3470 case FDFMTEND:
3471 case FDFLUSH:
3472 if (lock_fdc(drive, true))
3473 return -EINTR;
3474 return invalidate_drive(bdev);
3475 case FDSETEMSGTRESH:
3476 UDP->max_errors.reporting = (unsigned short)(param & 0x0f);
3477 return 0;
3478 case FDGETMAXERRS:
3479 outparam = &UDP->max_errors;
3480 break;
3481 case FDSETMAXERRS:
3482 UDP->max_errors = inparam.max_errors;
3483 break;
3484 case FDGETDRVTYP:
3485 outparam = drive_name(type, drive);
3486 SUPBOUND(size, strlen((const char *)outparam) + 1);
3487 break;
3488 case FDSETDRVPRM:
3489 *UDP = inparam.dp;
3490 break;
3491 case FDGETDRVPRM:
3492 outparam = UDP;
3493 break;
3494 case FDPOLLDRVSTAT:
3495 if (lock_fdc(drive, true))
3496 return -EINTR;
3497 if (poll_drive(true, FD_RAW_NEED_DISK) == -EINTR)
3498 return -EINTR;
3499 process_fd_request();
3500 /* fall through */
3501 case FDGETDRVSTAT:
3502 outparam = UDRS;
3503 break;
3504 case FDRESET:
3505 return user_reset_fdc(drive, (int)param, true);
3506 case FDGETFDCSTAT:
3507 outparam = UFDCS;
3508 break;
3509 case FDWERRORCLR:
3510 memset(UDRWE, 0, sizeof(*UDRWE));
3511 return 0;
3512 case FDWERRORGET:
3513 outparam = UDRWE;
3514 break;
3515 case FDRAWCMD:
3516 if (type)
3517 return -EINVAL;
3518 if (lock_fdc(drive, true))
3519 return -EINTR;
3520 set_floppy(drive);
3521 i = raw_cmd_ioctl(cmd, (void __user *)param);
3522 if (i == -EINTR)
3523 return -EINTR;
3524 process_fd_request();
3525 return i;
3526 case FDTWADDLE:
3527 if (lock_fdc(drive, true))
3528 return -EINTR;
3529 twaddle();
3530 process_fd_request();
3531 return 0;
3532 default:
3533 return -EINVAL;
3536 if (_IOC_DIR(cmd) & _IOC_READ)
3537 return fd_copyout((void __user *)param, outparam, size);
3539 return 0;
3542 static int fd_ioctl(struct block_device *bdev, fmode_t mode,
3543 unsigned int cmd, unsigned long param)
3545 int ret;
3547 lock_kernel();
3548 ret = fd_locked_ioctl(bdev, mode, cmd, param);
3549 unlock_kernel();
3551 return ret;
3554 static void __init config_types(void)
3556 bool has_drive = false;
3557 int drive;
3559 /* read drive info out of physical CMOS */
3560 drive = 0;
3561 if (!UDP->cmos)
3562 UDP->cmos = FLOPPY0_TYPE;
3563 drive = 1;
3564 if (!UDP->cmos && FLOPPY1_TYPE)
3565 UDP->cmos = FLOPPY1_TYPE;
3568 for (drive = 0; drive < N_DRIVE; drive++) {
3569 unsigned int type = UDP->cmos;
3570 struct floppy_drive_params *params;
3571 const char *name = NULL;
3572 static char temparea[32];
3574 if (type < ARRAY_SIZE(default_drive_params)) {
3575 params = &default_drive_params[type].params;
3576 if (type) {
3577 name = default_drive_params[type].name;
3578 allowed_drive_mask |= 1 << drive;
3579 } else
3580 allowed_drive_mask &= ~(1 << drive);
3581 } else {
3582 params = &default_drive_params[0].params;
3583 sprintf(temparea, "unknown type %d (usb?)", type);
3584 name = temparea;
3586 if (name) {
3587 const char *prepend;
3588 if (!has_drive) {
3589 prepend = "";
3590 has_drive = true;
3591 pr_info("Floppy drive(s):");
3592 } else {
3593 prepend = ",";
3596 pr_cont("%s fd%d is %s", prepend, drive, name);
3598 *UDP = *params;
3601 if (has_drive)
3602 pr_cont("\n");
3605 static int floppy_release(struct gendisk *disk, fmode_t mode)
3607 int drive = (long)disk->private_data;
3609 lock_kernel();
3610 mutex_lock(&open_lock);
3611 if (UDRS->fd_ref < 0)
3612 UDRS->fd_ref = 0;
3613 else if (!UDRS->fd_ref--) {
3614 DPRINT("floppy_release with fd_ref == 0");
3615 UDRS->fd_ref = 0;
3617 if (!UDRS->fd_ref)
3618 opened_bdev[drive] = NULL;
3619 mutex_unlock(&open_lock);
3620 unlock_kernel();
3622 return 0;
3626 * floppy_open check for aliasing (/dev/fd0 can be the same as
3627 * /dev/PS0 etc), and disallows simultaneous access to the same
3628 * drive with different device numbers.
3630 static int floppy_open(struct block_device *bdev, fmode_t mode)
3632 int drive = (long)bdev->bd_disk->private_data;
3633 int old_dev, new_dev;
3634 int try;
3635 int res = -EBUSY;
3636 char *tmp;
3638 lock_kernel();
3639 mutex_lock(&open_lock);
3640 old_dev = UDRS->fd_device;
3641 if (opened_bdev[drive] && opened_bdev[drive] != bdev)
3642 goto out2;
3644 if (!UDRS->fd_ref && (UDP->flags & FD_BROKEN_DCL)) {
3645 set_bit(FD_DISK_CHANGED_BIT, &UDRS->flags);
3646 set_bit(FD_VERIFY_BIT, &UDRS->flags);
3649 if (UDRS->fd_ref == -1 || (UDRS->fd_ref && (mode & FMODE_EXCL)))
3650 goto out2;
3652 if (mode & FMODE_EXCL)
3653 UDRS->fd_ref = -1;
3654 else
3655 UDRS->fd_ref++;
3657 opened_bdev[drive] = bdev;
3659 res = -ENXIO;
3661 if (!floppy_track_buffer) {
3662 /* if opening an ED drive, reserve a big buffer,
3663 * else reserve a small one */
3664 if ((UDP->cmos == 6) || (UDP->cmos == 5))
3665 try = 64; /* Only 48 actually useful */
3666 else
3667 try = 32; /* Only 24 actually useful */
3669 tmp = (char *)fd_dma_mem_alloc(1024 * try);
3670 if (!tmp && !floppy_track_buffer) {
3671 try >>= 1; /* buffer only one side */
3672 INFBOUND(try, 16);
3673 tmp = (char *)fd_dma_mem_alloc(1024 * try);
3675 if (!tmp && !floppy_track_buffer)
3676 fallback_on_nodma_alloc(&tmp, 2048 * try);
3677 if (!tmp && !floppy_track_buffer) {
3678 DPRINT("Unable to allocate DMA memory\n");
3679 goto out;
3681 if (floppy_track_buffer) {
3682 if (tmp)
3683 fd_dma_mem_free((unsigned long)tmp, try * 1024);
3684 } else {
3685 buffer_min = buffer_max = -1;
3686 floppy_track_buffer = tmp;
3687 max_buffer_sectors = try;
3691 new_dev = MINOR(bdev->bd_dev);
3692 UDRS->fd_device = new_dev;
3693 set_capacity(disks[drive], floppy_sizes[new_dev]);
3694 if (old_dev != -1 && old_dev != new_dev) {
3695 if (buffer_drive == drive)
3696 buffer_track = -1;
3699 if (UFDCS->rawcmd == 1)
3700 UFDCS->rawcmd = 2;
3702 if (!(mode & FMODE_NDELAY)) {
3703 if (mode & (FMODE_READ|FMODE_WRITE)) {
3704 UDRS->last_checked = 0;
3705 check_disk_change(bdev);
3706 if (test_bit(FD_DISK_CHANGED_BIT, &UDRS->flags))
3707 goto out;
3709 res = -EROFS;
3710 if ((mode & FMODE_WRITE) &&
3711 !test_bit(FD_DISK_WRITABLE_BIT, &UDRS->flags))
3712 goto out;
3714 mutex_unlock(&open_lock);
3715 unlock_kernel();
3716 return 0;
3717 out:
3718 if (UDRS->fd_ref < 0)
3719 UDRS->fd_ref = 0;
3720 else
3721 UDRS->fd_ref--;
3722 if (!UDRS->fd_ref)
3723 opened_bdev[drive] = NULL;
3724 out2:
3725 mutex_unlock(&open_lock);
3726 unlock_kernel();
3727 return res;
3731 * Check if the disk has been changed or if a change has been faked.
3733 static int check_floppy_change(struct gendisk *disk)
3735 int drive = (long)disk->private_data;
3737 if (test_bit(FD_DISK_CHANGED_BIT, &UDRS->flags) ||
3738 test_bit(FD_VERIFY_BIT, &UDRS->flags))
3739 return 1;
3741 if (time_after(jiffies, UDRS->last_checked + UDP->checkfreq)) {
3742 lock_fdc(drive, false);
3743 poll_drive(false, 0);
3744 process_fd_request();
3747 if (test_bit(FD_DISK_CHANGED_BIT, &UDRS->flags) ||
3748 test_bit(FD_VERIFY_BIT, &UDRS->flags) ||
3749 test_bit(drive, &fake_change) ||
3750 (!ITYPE(UDRS->fd_device) && !current_type[drive]))
3751 return 1;
3752 return 0;
3756 * This implements "read block 0" for floppy_revalidate().
3757 * Needed for format autodetection, checking whether there is
3758 * a disk in the drive, and whether that disk is writable.
3761 static void floppy_rb0_complete(struct bio *bio, int err)
3763 complete((struct completion *)bio->bi_private);
3766 static int __floppy_read_block_0(struct block_device *bdev)
3768 struct bio bio;
3769 struct bio_vec bio_vec;
3770 struct completion complete;
3771 struct page *page;
3772 size_t size;
3774 page = alloc_page(GFP_NOIO);
3775 if (!page) {
3776 process_fd_request();
3777 return -ENOMEM;
3780 size = bdev->bd_block_size;
3781 if (!size)
3782 size = 1024;
3784 bio_init(&bio);
3785 bio.bi_io_vec = &bio_vec;
3786 bio_vec.bv_page = page;
3787 bio_vec.bv_len = size;
3788 bio_vec.bv_offset = 0;
3789 bio.bi_vcnt = 1;
3790 bio.bi_idx = 0;
3791 bio.bi_size = size;
3792 bio.bi_bdev = bdev;
3793 bio.bi_sector = 0;
3794 bio.bi_flags = BIO_QUIET;
3795 init_completion(&complete);
3796 bio.bi_private = &complete;
3797 bio.bi_end_io = floppy_rb0_complete;
3799 submit_bio(READ, &bio);
3800 generic_unplug_device(bdev_get_queue(bdev));
3801 process_fd_request();
3802 wait_for_completion(&complete);
3804 __free_page(page);
3806 return 0;
3809 /* revalidate the floppy disk, i.e. trigger format autodetection by reading
3810 * the bootblock (block 0). "Autodetection" is also needed to check whether
3811 * there is a disk in the drive at all... Thus we also do it for fixed
3812 * geometry formats */
3813 static int floppy_revalidate(struct gendisk *disk)
3815 int drive = (long)disk->private_data;
3816 #define NO_GEOM (!current_type[drive] && !ITYPE(UDRS->fd_device))
3817 int cf;
3818 int res = 0;
3820 if (test_bit(FD_DISK_CHANGED_BIT, &UDRS->flags) ||
3821 test_bit(FD_VERIFY_BIT, &UDRS->flags) ||
3822 test_bit(drive, &fake_change) || NO_GEOM) {
3823 if (WARN(atomic_read(&usage_count) == 0,
3824 "VFS: revalidate called on non-open device.\n"))
3825 return -EFAULT;
3827 lock_fdc(drive, false);
3828 cf = (test_bit(FD_DISK_CHANGED_BIT, &UDRS->flags) ||
3829 test_bit(FD_VERIFY_BIT, &UDRS->flags));
3830 if (!(cf || test_bit(drive, &fake_change) || NO_GEOM)) {
3831 process_fd_request(); /*already done by another thread */
3832 return 0;
3834 UDRS->maxblock = 0;
3835 UDRS->maxtrack = 0;
3836 if (buffer_drive == drive)
3837 buffer_track = -1;
3838 clear_bit(drive, &fake_change);
3839 clear_bit(FD_DISK_CHANGED_BIT, &UDRS->flags);
3840 if (cf)
3841 UDRS->generation++;
3842 if (NO_GEOM) {
3843 /* auto-sensing */
3844 res = __floppy_read_block_0(opened_bdev[drive]);
3845 } else {
3846 if (cf)
3847 poll_drive(false, FD_RAW_NEED_DISK);
3848 process_fd_request();
3851 set_capacity(disk, floppy_sizes[UDRS->fd_device]);
3852 return res;
3855 static const struct block_device_operations floppy_fops = {
3856 .owner = THIS_MODULE,
3857 .open = floppy_open,
3858 .release = floppy_release,
3859 .ioctl = fd_ioctl,
3860 .getgeo = fd_getgeo,
3861 .media_changed = check_floppy_change,
3862 .revalidate_disk = floppy_revalidate,
3866 * Floppy Driver initialization
3867 * =============================
3870 /* Determine the floppy disk controller type */
3871 /* This routine was written by David C. Niemi */
3872 static char __init get_fdc_version(void)
3874 int r;
3876 output_byte(FD_DUMPREGS); /* 82072 and better know DUMPREGS */
3877 if (FDCS->reset)
3878 return FDC_NONE;
3879 r = result();
3880 if (r <= 0x00)
3881 return FDC_NONE; /* No FDC present ??? */
3882 if ((r == 1) && (reply_buffer[0] == 0x80)) {
3883 pr_info("FDC %d is an 8272A\n", fdc);
3884 return FDC_8272A; /* 8272a/765 don't know DUMPREGS */
3886 if (r != 10) {
3887 pr_info("FDC %d init: DUMPREGS: unexpected return of %d bytes.\n",
3888 fdc, r);
3889 return FDC_UNKNOWN;
3892 if (!fdc_configure()) {
3893 pr_info("FDC %d is an 82072\n", fdc);
3894 return FDC_82072; /* 82072 doesn't know CONFIGURE */
3897 output_byte(FD_PERPENDICULAR);
3898 if (need_more_output() == MORE_OUTPUT) {
3899 output_byte(0);
3900 } else {
3901 pr_info("FDC %d is an 82072A\n", fdc);
3902 return FDC_82072A; /* 82072A as found on Sparcs. */
3905 output_byte(FD_UNLOCK);
3906 r = result();
3907 if ((r == 1) && (reply_buffer[0] == 0x80)) {
3908 pr_info("FDC %d is a pre-1991 82077\n", fdc);
3909 return FDC_82077_ORIG; /* Pre-1991 82077, doesn't know
3910 * LOCK/UNLOCK */
3912 if ((r != 1) || (reply_buffer[0] != 0x00)) {
3913 pr_info("FDC %d init: UNLOCK: unexpected return of %d bytes.\n",
3914 fdc, r);
3915 return FDC_UNKNOWN;
3917 output_byte(FD_PARTID);
3918 r = result();
3919 if (r != 1) {
3920 pr_info("FDC %d init: PARTID: unexpected return of %d bytes.\n",
3921 fdc, r);
3922 return FDC_UNKNOWN;
3924 if (reply_buffer[0] == 0x80) {
3925 pr_info("FDC %d is a post-1991 82077\n", fdc);
3926 return FDC_82077; /* Revised 82077AA passes all the tests */
3928 switch (reply_buffer[0] >> 5) {
3929 case 0x0:
3930 /* Either a 82078-1 or a 82078SL running at 5Volt */
3931 pr_info("FDC %d is an 82078.\n", fdc);
3932 return FDC_82078;
3933 case 0x1:
3934 pr_info("FDC %d is a 44pin 82078\n", fdc);
3935 return FDC_82078;
3936 case 0x2:
3937 pr_info("FDC %d is a S82078B\n", fdc);
3938 return FDC_S82078B;
3939 case 0x3:
3940 pr_info("FDC %d is a National Semiconductor PC87306\n", fdc);
3941 return FDC_87306;
3942 default:
3943 pr_info("FDC %d init: 82078 variant with unknown PARTID=%d.\n",
3944 fdc, reply_buffer[0] >> 5);
3945 return FDC_82078_UNKN;
3947 } /* get_fdc_version */
3949 /* lilo configuration */
3951 static void __init floppy_set_flags(int *ints, int param, int param2)
3953 int i;
3955 for (i = 0; i < ARRAY_SIZE(default_drive_params); i++) {
3956 if (param)
3957 default_drive_params[i].params.flags |= param2;
3958 else
3959 default_drive_params[i].params.flags &= ~param2;
3961 DPRINT("%s flag 0x%x\n", param2 ? "Setting" : "Clearing", param);
3964 static void __init daring(int *ints, int param, int param2)
3966 int i;
3968 for (i = 0; i < ARRAY_SIZE(default_drive_params); i++) {
3969 if (param) {
3970 default_drive_params[i].params.select_delay = 0;
3971 default_drive_params[i].params.flags |=
3972 FD_SILENT_DCL_CLEAR;
3973 } else {
3974 default_drive_params[i].params.select_delay =
3975 2 * HZ / 100;
3976 default_drive_params[i].params.flags &=
3977 ~FD_SILENT_DCL_CLEAR;
3980 DPRINT("Assuming %s floppy hardware\n", param ? "standard" : "broken");
3983 static void __init set_cmos(int *ints, int dummy, int dummy2)
3985 int current_drive = 0;
3987 if (ints[0] != 2) {
3988 DPRINT("wrong number of parameters for CMOS\n");
3989 return;
3991 current_drive = ints[1];
3992 if (current_drive < 0 || current_drive >= 8) {
3993 DPRINT("bad drive for set_cmos\n");
3994 return;
3996 #if N_FDC > 1
3997 if (current_drive >= 4 && !FDC2)
3998 FDC2 = 0x370;
3999 #endif
4000 DP->cmos = ints[2];
4001 DPRINT("setting CMOS code to %d\n", ints[2]);
4004 static struct param_table {
4005 const char *name;
4006 void (*fn) (int *ints, int param, int param2);
4007 int *var;
4008 int def_param;
4009 int param2;
4010 } config_params[] __initdata = {
4011 {"allowed_drive_mask", NULL, &allowed_drive_mask, 0xff, 0}, /* obsolete */
4012 {"all_drives", NULL, &allowed_drive_mask, 0xff, 0}, /* obsolete */
4013 {"asus_pci", NULL, &allowed_drive_mask, 0x33, 0},
4014 {"irq", NULL, &FLOPPY_IRQ, 6, 0},
4015 {"dma", NULL, &FLOPPY_DMA, 2, 0},
4016 {"daring", daring, NULL, 1, 0},
4017 #if N_FDC > 1
4018 {"two_fdc", NULL, &FDC2, 0x370, 0},
4019 {"one_fdc", NULL, &FDC2, 0, 0},
4020 #endif
4021 {"thinkpad", floppy_set_flags, NULL, 1, FD_INVERTED_DCL},
4022 {"broken_dcl", floppy_set_flags, NULL, 1, FD_BROKEN_DCL},
4023 {"messages", floppy_set_flags, NULL, 1, FTD_MSG},
4024 {"silent_dcl_clear", floppy_set_flags, NULL, 1, FD_SILENT_DCL_CLEAR},
4025 {"debug", floppy_set_flags, NULL, 1, FD_DEBUG},
4026 {"nodma", NULL, &can_use_virtual_dma, 1, 0},
4027 {"omnibook", NULL, &can_use_virtual_dma, 1, 0},
4028 {"yesdma", NULL, &can_use_virtual_dma, 0, 0},
4029 {"fifo_depth", NULL, &fifo_depth, 0xa, 0},
4030 {"nofifo", NULL, &no_fifo, 0x20, 0},
4031 {"usefifo", NULL, &no_fifo, 0, 0},
4032 {"cmos", set_cmos, NULL, 0, 0},
4033 {"slow", NULL, &slow_floppy, 1, 0},
4034 {"unexpected_interrupts", NULL, &print_unex, 1, 0},
4035 {"no_unexpected_interrupts", NULL, &print_unex, 0, 0},
4036 {"L40SX", NULL, &print_unex, 0, 0}
4038 EXTRA_FLOPPY_PARAMS
4041 static int __init floppy_setup(char *str)
4043 int i;
4044 int param;
4045 int ints[11];
4047 str = get_options(str, ARRAY_SIZE(ints), ints);
4048 if (str) {
4049 for (i = 0; i < ARRAY_SIZE(config_params); i++) {
4050 if (strcmp(str, config_params[i].name) == 0) {
4051 if (ints[0])
4052 param = ints[1];
4053 else
4054 param = config_params[i].def_param;
4055 if (config_params[i].fn)
4056 config_params[i].fn(ints, param,
4057 config_params[i].
4058 param2);
4059 if (config_params[i].var) {
4060 DPRINT("%s=%d\n", str, param);
4061 *config_params[i].var = param;
4063 return 1;
4067 if (str) {
4068 DPRINT("unknown floppy option [%s]\n", str);
4070 DPRINT("allowed options are:");
4071 for (i = 0; i < ARRAY_SIZE(config_params); i++)
4072 pr_cont(" %s", config_params[i].name);
4073 pr_cont("\n");
4074 } else
4075 DPRINT("botched floppy option\n");
4076 DPRINT("Read Documentation/blockdev/floppy.txt\n");
4077 return 0;
4080 static int have_no_fdc = -ENODEV;
4082 static ssize_t floppy_cmos_show(struct device *dev,
4083 struct device_attribute *attr, char *buf)
4085 struct platform_device *p = to_platform_device(dev);
4086 int drive;
4088 drive = p->id;
4089 return sprintf(buf, "%X\n", UDP->cmos);
4092 static DEVICE_ATTR(cmos, S_IRUGO, floppy_cmos_show, NULL);
4094 static void floppy_device_release(struct device *dev)
4098 static int floppy_resume(struct device *dev)
4100 int fdc;
4102 for (fdc = 0; fdc < N_FDC; fdc++)
4103 if (FDCS->address != -1)
4104 user_reset_fdc(-1, FD_RESET_ALWAYS, false);
4106 return 0;
4109 static const struct dev_pm_ops floppy_pm_ops = {
4110 .resume = floppy_resume,
4111 .restore = floppy_resume,
4114 static struct platform_driver floppy_driver = {
4115 .driver = {
4116 .name = "floppy",
4117 .pm = &floppy_pm_ops,
4121 static struct platform_device floppy_device[N_DRIVE];
4123 static struct kobject *floppy_find(dev_t dev, int *part, void *data)
4125 int drive = (*part & 3) | ((*part & 0x80) >> 5);
4126 if (drive >= N_DRIVE ||
4127 !(allowed_drive_mask & (1 << drive)) ||
4128 fdc_state[FDC(drive)].version == FDC_NONE)
4129 return NULL;
4130 if (((*part >> 2) & 0x1f) >= ARRAY_SIZE(floppy_type))
4131 return NULL;
4132 *part = 0;
4133 return get_disk(disks[drive]);
4136 static int __init floppy_init(void)
4138 int i, unit, drive;
4139 int err, dr;
4141 set_debugt();
4142 interruptjiffies = resultjiffies = jiffies;
4144 #if defined(CONFIG_PPC)
4145 if (check_legacy_ioport(FDC1))
4146 return -ENODEV;
4147 #endif
4149 raw_cmd = NULL;
4151 for (dr = 0; dr < N_DRIVE; dr++) {
4152 disks[dr] = alloc_disk(1);
4153 if (!disks[dr]) {
4154 err = -ENOMEM;
4155 goto out_put_disk;
4158 disks[dr]->major = FLOPPY_MAJOR;
4159 disks[dr]->first_minor = TOMINOR(dr);
4160 disks[dr]->fops = &floppy_fops;
4161 sprintf(disks[dr]->disk_name, "fd%d", dr);
4163 init_timer(&motor_off_timer[dr]);
4164 motor_off_timer[dr].data = dr;
4165 motor_off_timer[dr].function = motor_off_callback;
4168 err = register_blkdev(FLOPPY_MAJOR, "fd");
4169 if (err)
4170 goto out_put_disk;
4172 err = platform_driver_register(&floppy_driver);
4173 if (err)
4174 goto out_unreg_blkdev;
4176 floppy_queue = blk_init_queue(do_fd_request, &floppy_lock);
4177 if (!floppy_queue) {
4178 err = -ENOMEM;
4179 goto out_unreg_driver;
4181 blk_queue_max_hw_sectors(floppy_queue, 64);
4183 blk_register_region(MKDEV(FLOPPY_MAJOR, 0), 256, THIS_MODULE,
4184 floppy_find, NULL, NULL);
4186 for (i = 0; i < 256; i++)
4187 if (ITYPE(i))
4188 floppy_sizes[i] = floppy_type[ITYPE(i)].size;
4189 else
4190 floppy_sizes[i] = MAX_DISK_SIZE << 1;
4192 reschedule_timeout(MAXTIMEOUT, "floppy init");
4193 config_types();
4195 for (i = 0; i < N_FDC; i++) {
4196 fdc = i;
4197 memset(FDCS, 0, sizeof(*FDCS));
4198 FDCS->dtr = -1;
4199 FDCS->dor = 0x4;
4200 #if defined(__sparc__) || defined(__mc68000__)
4201 /*sparcs/sun3x don't have a DOR reset which we can fall back on to */
4202 #ifdef __mc68000__
4203 if (MACH_IS_SUN3X)
4204 #endif
4205 FDCS->version = FDC_82072A;
4206 #endif
4209 use_virtual_dma = can_use_virtual_dma & 1;
4210 fdc_state[0].address = FDC1;
4211 if (fdc_state[0].address == -1) {
4212 del_timer(&fd_timeout);
4213 err = -ENODEV;
4214 goto out_unreg_region;
4216 #if N_FDC > 1
4217 fdc_state[1].address = FDC2;
4218 #endif
4220 fdc = 0; /* reset fdc in case of unexpected interrupt */
4221 err = floppy_grab_irq_and_dma();
4222 if (err) {
4223 del_timer(&fd_timeout);
4224 err = -EBUSY;
4225 goto out_unreg_region;
4228 /* initialise drive state */
4229 for (drive = 0; drive < N_DRIVE; drive++) {
4230 memset(UDRS, 0, sizeof(*UDRS));
4231 memset(UDRWE, 0, sizeof(*UDRWE));
4232 set_bit(FD_DISK_NEWCHANGE_BIT, &UDRS->flags);
4233 set_bit(FD_DISK_CHANGED_BIT, &UDRS->flags);
4234 set_bit(FD_VERIFY_BIT, &UDRS->flags);
4235 UDRS->fd_device = -1;
4236 floppy_track_buffer = NULL;
4237 max_buffer_sectors = 0;
4240 * Small 10 msec delay to let through any interrupt that
4241 * initialization might have triggered, to not
4242 * confuse detection:
4244 msleep(10);
4246 for (i = 0; i < N_FDC; i++) {
4247 fdc = i;
4248 FDCS->driver_version = FD_DRIVER_VERSION;
4249 for (unit = 0; unit < 4; unit++)
4250 FDCS->track[unit] = 0;
4251 if (FDCS->address == -1)
4252 continue;
4253 FDCS->rawcmd = 2;
4254 if (user_reset_fdc(-1, FD_RESET_ALWAYS, false)) {
4255 /* free ioports reserved by floppy_grab_irq_and_dma() */
4256 floppy_release_regions(fdc);
4257 FDCS->address = -1;
4258 FDCS->version = FDC_NONE;
4259 continue;
4261 /* Try to determine the floppy controller type */
4262 FDCS->version = get_fdc_version();
4263 if (FDCS->version == FDC_NONE) {
4264 /* free ioports reserved by floppy_grab_irq_and_dma() */
4265 floppy_release_regions(fdc);
4266 FDCS->address = -1;
4267 continue;
4269 if (can_use_virtual_dma == 2 && FDCS->version < FDC_82072A)
4270 can_use_virtual_dma = 0;
4272 have_no_fdc = 0;
4273 /* Not all FDCs seem to be able to handle the version command
4274 * properly, so force a reset for the standard FDC clones,
4275 * to avoid interrupt garbage.
4277 user_reset_fdc(-1, FD_RESET_ALWAYS, false);
4279 fdc = 0;
4280 del_timer(&fd_timeout);
4281 current_drive = 0;
4282 initialized = true;
4283 if (have_no_fdc) {
4284 DPRINT("no floppy controllers found\n");
4285 err = have_no_fdc;
4286 goto out_flush_work;
4289 for (drive = 0; drive < N_DRIVE; drive++) {
4290 if (!(allowed_drive_mask & (1 << drive)))
4291 continue;
4292 if (fdc_state[FDC(drive)].version == FDC_NONE)
4293 continue;
4295 floppy_device[drive].name = floppy_device_name;
4296 floppy_device[drive].id = drive;
4297 floppy_device[drive].dev.release = floppy_device_release;
4299 err = platform_device_register(&floppy_device[drive]);
4300 if (err)
4301 goto out_flush_work;
4303 err = device_create_file(&floppy_device[drive].dev,
4304 &dev_attr_cmos);
4305 if (err)
4306 goto out_unreg_platform_dev;
4308 /* to be cleaned up... */
4309 disks[drive]->private_data = (void *)(long)drive;
4310 disks[drive]->queue = floppy_queue;
4311 disks[drive]->flags |= GENHD_FL_REMOVABLE;
4312 disks[drive]->driverfs_dev = &floppy_device[drive].dev;
4313 add_disk(disks[drive]);
4316 return 0;
4318 out_unreg_platform_dev:
4319 platform_device_unregister(&floppy_device[drive]);
4320 out_flush_work:
4321 flush_scheduled_work();
4322 if (atomic_read(&usage_count))
4323 floppy_release_irq_and_dma();
4324 out_unreg_region:
4325 blk_unregister_region(MKDEV(FLOPPY_MAJOR, 0), 256);
4326 blk_cleanup_queue(floppy_queue);
4327 out_unreg_driver:
4328 platform_driver_unregister(&floppy_driver);
4329 out_unreg_blkdev:
4330 unregister_blkdev(FLOPPY_MAJOR, "fd");
4331 out_put_disk:
4332 while (dr--) {
4333 del_timer(&motor_off_timer[dr]);
4334 put_disk(disks[dr]);
4336 return err;
4339 static const struct io_region {
4340 int offset;
4341 int size;
4342 } io_regions[] = {
4343 { 2, 1 },
4344 /* address + 3 is sometimes reserved by pnp bios for motherboard */
4345 { 4, 2 },
4346 /* address + 6 is reserved, and may be taken by IDE.
4347 * Unfortunately, Adaptec doesn't know this :-(, */
4348 { 7, 1 },
4351 static void floppy_release_allocated_regions(int fdc, const struct io_region *p)
4353 while (p != io_regions) {
4354 p--;
4355 release_region(FDCS->address + p->offset, p->size);
4359 #define ARRAY_END(X) (&((X)[ARRAY_SIZE(X)]))
4361 static int floppy_request_regions(int fdc)
4363 const struct io_region *p;
4365 for (p = io_regions; p < ARRAY_END(io_regions); p++) {
4366 if (!request_region(FDCS->address + p->offset,
4367 p->size, "floppy")) {
4368 DPRINT("Floppy io-port 0x%04lx in use\n",
4369 FDCS->address + p->offset);
4370 floppy_release_allocated_regions(fdc, p);
4371 return -EBUSY;
4374 return 0;
4377 static void floppy_release_regions(int fdc)
4379 floppy_release_allocated_regions(fdc, ARRAY_END(io_regions));
4382 static int floppy_grab_irq_and_dma(void)
4384 if (atomic_inc_return(&usage_count) > 1)
4385 return 0;
4388 * We might have scheduled a free_irq(), wait it to
4389 * drain first:
4391 flush_scheduled_work();
4393 if (fd_request_irq()) {
4394 DPRINT("Unable to grab IRQ%d for the floppy driver\n",
4395 FLOPPY_IRQ);
4396 atomic_dec(&usage_count);
4397 return -1;
4399 if (fd_request_dma()) {
4400 DPRINT("Unable to grab DMA%d for the floppy driver\n",
4401 FLOPPY_DMA);
4402 if (can_use_virtual_dma & 2)
4403 use_virtual_dma = can_use_virtual_dma = 1;
4404 if (!(can_use_virtual_dma & 1)) {
4405 fd_free_irq();
4406 atomic_dec(&usage_count);
4407 return -1;
4411 for (fdc = 0; fdc < N_FDC; fdc++) {
4412 if (FDCS->address != -1) {
4413 if (floppy_request_regions(fdc))
4414 goto cleanup;
4417 for (fdc = 0; fdc < N_FDC; fdc++) {
4418 if (FDCS->address != -1) {
4419 reset_fdc_info(1);
4420 fd_outb(FDCS->dor, FD_DOR);
4423 fdc = 0;
4424 set_dor(0, ~0, 8); /* avoid immediate interrupt */
4426 for (fdc = 0; fdc < N_FDC; fdc++)
4427 if (FDCS->address != -1)
4428 fd_outb(FDCS->dor, FD_DOR);
4430 * The driver will try and free resources and relies on us
4431 * to know if they were allocated or not.
4433 fdc = 0;
4434 irqdma_allocated = 1;
4435 return 0;
4436 cleanup:
4437 fd_free_irq();
4438 fd_free_dma();
4439 while (--fdc >= 0)
4440 floppy_release_regions(fdc);
4441 atomic_dec(&usage_count);
4442 return -1;
4445 static void floppy_release_irq_and_dma(void)
4447 int old_fdc;
4448 #ifndef __sparc__
4449 int drive;
4450 #endif
4451 long tmpsize;
4452 unsigned long tmpaddr;
4454 if (!atomic_dec_and_test(&usage_count))
4455 return;
4457 if (irqdma_allocated) {
4458 fd_disable_dma();
4459 fd_free_dma();
4460 fd_free_irq();
4461 irqdma_allocated = 0;
4463 set_dor(0, ~0, 8);
4464 #if N_FDC > 1
4465 set_dor(1, ~8, 0);
4466 #endif
4467 floppy_enable_hlt();
4469 if (floppy_track_buffer && max_buffer_sectors) {
4470 tmpsize = max_buffer_sectors * 1024;
4471 tmpaddr = (unsigned long)floppy_track_buffer;
4472 floppy_track_buffer = NULL;
4473 max_buffer_sectors = 0;
4474 buffer_min = buffer_max = -1;
4475 fd_dma_mem_free(tmpaddr, tmpsize);
4477 #ifndef __sparc__
4478 for (drive = 0; drive < N_FDC * 4; drive++)
4479 if (timer_pending(motor_off_timer + drive))
4480 pr_info("motor off timer %d still active\n", drive);
4481 #endif
4483 if (timer_pending(&fd_timeout))
4484 pr_info("floppy timer still active:%s\n", timeout_message);
4485 if (timer_pending(&fd_timer))
4486 pr_info("auxiliary floppy timer still active\n");
4487 if (work_pending(&floppy_work))
4488 pr_info("work still pending\n");
4489 old_fdc = fdc;
4490 for (fdc = 0; fdc < N_FDC; fdc++)
4491 if (FDCS->address != -1)
4492 floppy_release_regions(fdc);
4493 fdc = old_fdc;
4496 #ifdef MODULE
4498 static char *floppy;
4500 static void __init parse_floppy_cfg_string(char *cfg)
4502 char *ptr;
4504 while (*cfg) {
4505 ptr = cfg;
4506 while (*cfg && *cfg != ' ' && *cfg != '\t')
4507 cfg++;
4508 if (*cfg) {
4509 *cfg = '\0';
4510 cfg++;
4512 if (*ptr)
4513 floppy_setup(ptr);
4517 static int __init floppy_module_init(void)
4519 if (floppy)
4520 parse_floppy_cfg_string(floppy);
4521 return floppy_init();
4523 module_init(floppy_module_init);
4525 static void __exit floppy_module_exit(void)
4527 int drive;
4529 blk_unregister_region(MKDEV(FLOPPY_MAJOR, 0), 256);
4530 unregister_blkdev(FLOPPY_MAJOR, "fd");
4531 platform_driver_unregister(&floppy_driver);
4533 for (drive = 0; drive < N_DRIVE; drive++) {
4534 del_timer_sync(&motor_off_timer[drive]);
4536 if ((allowed_drive_mask & (1 << drive)) &&
4537 fdc_state[FDC(drive)].version != FDC_NONE) {
4538 del_gendisk(disks[drive]);
4539 device_remove_file(&floppy_device[drive].dev, &dev_attr_cmos);
4540 platform_device_unregister(&floppy_device[drive]);
4542 put_disk(disks[drive]);
4545 del_timer_sync(&fd_timeout);
4546 del_timer_sync(&fd_timer);
4547 blk_cleanup_queue(floppy_queue);
4549 if (atomic_read(&usage_count))
4550 floppy_release_irq_and_dma();
4552 /* eject disk, if any */
4553 fd_eject(0);
4556 module_exit(floppy_module_exit);
4558 module_param(floppy, charp, 0);
4559 module_param(FLOPPY_IRQ, int, 0);
4560 module_param(FLOPPY_DMA, int, 0);
4561 MODULE_AUTHOR("Alain L. Knaff");
4562 MODULE_SUPPORTED_DEVICE("fd");
4563 MODULE_LICENSE("GPL");
4565 /* This doesn't actually get used other than for module information */
4566 static const struct pnp_device_id floppy_pnpids[] = {
4567 {"PNP0700", 0},
4571 MODULE_DEVICE_TABLE(pnp, floppy_pnpids);
4573 #else
4575 __setup("floppy=", floppy_setup);
4576 module_init(floppy_init)
4577 #endif
4579 MODULE_ALIAS_BLOCKDEV_MAJOR(FLOPPY_MAJOR);