blob: c2d9c59f0e649dc573f19b74876eeb23224789c9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/block/floppy.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 * Copyright (C) 1993, 1994 Alain Knaff
6 * Copyright (C) 1998 Alan Cox
7 */
Jesper Juhl06f748c2007-10-16 23:30:57 -07008
Linus Torvalds1da177e2005-04-16 15:20:36 -07009/*
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.
14 */
15
16/*
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.
22 */
23
24/*
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.
30 */
31
32/*
33 * 28.02.92 - made track-buffering routines, based on the routines written
34 * by entropy@wintermute.wpi.edu (Lawrence Foard). Linus.
35 */
36
37/*
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.
41 */
42
43/*
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.
47 *
48 * 1992/9/17: Added DMA allocation & DMA functions. -- hhb.
49 *
50 * TODO: Errors are still not counted properly.
51 */
52
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.
61 */
62
63/*
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.
67 */
68
69/* 1994/6/24 --bbroad-- added the floppy table entries and made
70 * minor modifications to allow 2.88 floppies to be run.
71 */
72
73/* 1994/7/13 -- Paul Vojta -- modified the probing code to allow three or more
74 * disk types.
75 */
76
77/*
78 * 1994/8/8 -- Alain Knaff -- Switched to fdpatch driver: Support for bigger
79 * format bug fixes, but unfortunately some new bugs too...
80 */
81
82/* 1994/9/17 -- Koen Holtman -- added logging of physical floppy write
83 * errors to allow safe writing by specialized programs.
84 */
85
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").
90 */
91
92/*
93 * 1995/8/26 -- Andreas Busse -- added Mips support.
94 */
95
96/*
97 * 1995/10/18 -- Ralf Baechle -- Portability cleanup; move machine dependent
98 * features to asm/floppy.h.
99 */
100
101/*
James Nelsonb88b0982005-11-08 16:52:12 +0100102 * 1998/1/21 -- Richard Gooch <rgooch@atnf.csiro.au> -- devfs support
103 */
104
105/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 * 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.
109 */
110
111/*
112 * 1998/06/07 -- Alan Cox -- Merged the 2.0.34 fixes for resource allocation
113 * failures.
114 */
115
116/*
117 * 1998/09/20 -- David Weinehall -- Added slow-down code for buggy PS/2-drives.
118 */
119
120/*
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).
124 */
125
126/*
127 * 2000/08/28 -- Arnaldo Carvalho de Melo <acme@conectiva.com.br>
128 * - get rid of check_region
129 * - s/suser/capable/
130 */
131
132/*
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.)
135 */
136
137/*
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.
141 */
142
143/* 2003/07/28 -- Daniele Bellucci <bellucda@tiscali.it>.
144 * Better audit of register_blkdev.
145 */
146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147#undef FLOPPY_SILENT_DCL_CLEAR
148
149#define REALLY_SLOW_IO
150
151#define DEBUGT 2
Joe Perches48c8cee2010-03-10 15:20:45 -0800152#define DCL_DEBUG /* debug disk change line */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Joe Perches87f530d2010-03-10 15:20:54 -0800154#ifdef DCL_DEBUG
155#define debug_dcl(test, fmt, args...) \
156 do { if ((test) & FD_DEBUG) DPRINT(fmt, ##args); } while (0)
157#else
158#define debug_dcl(test, fmt, args...) \
159 do { if (0) DPRINT(fmt, ##args); } while (0)
160#endif
161
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163/* do print messages for unexpected interrupts */
164static int print_unex = 1;
165#include <linux/module.h>
166#include <linux/sched.h>
167#include <linux/fs.h>
168#include <linux/kernel.h>
169#include <linux/timer.h>
170#include <linux/workqueue.h>
171#define FDPATCHES
172#include <linux/fdreg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173#include <linux/fd.h>
174#include <linux/hdreg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175#include <linux/errno.h>
176#include <linux/slab.h>
177#include <linux/mm.h>
178#include <linux/bio.h>
179#include <linux/string.h>
Marcelo Feitoza Parisi50297cb2006-03-28 01:56:44 -0800180#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181#include <linux/fcntl.h>
182#include <linux/delay.h>
183#include <linux/mc146818rtc.h> /* CMOS defines */
184#include <linux/ioport.h>
185#include <linux/interrupt.h>
186#include <linux/init.h>
Russell Kingd052d1b2005-10-29 19:07:23 +0100187#include <linux/platform_device.h>
Scott James Remnant83f9ef42009-04-02 16:56:47 -0700188#include <linux/mod_devicetable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189#include <linux/buffer_head.h> /* for invalidate_buffers() */
Jes Sorensenb1c82b52006-03-23 03:00:26 -0800190#include <linux/mutex.h>
Joe Perchesd4937542010-03-10 15:20:44 -0800191#include <linux/io.h>
192#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194/*
195 * PS/2 floppies have much slower step rates than regular floppies.
196 * It's been recommended that take about 1/4 of the default speed
197 * in some more extreme cases.
198 */
199static int slow_floppy;
200
201#include <asm/dma.h>
202#include <asm/irq.h>
203#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205static int FLOPPY_IRQ = 6;
206static int FLOPPY_DMA = 2;
207static int can_use_virtual_dma = 2;
208/* =======
209 * can use virtual DMA:
210 * 0 = use of virtual DMA disallowed by config
211 * 1 = use of virtual DMA prescribed by config
212 * 2 = no virtual DMA preference configured. By default try hard DMA,
213 * but fall back on virtual DMA when not enough memory available
214 */
215
216static int use_virtual_dma;
217/* =======
218 * use virtual DMA
219 * 0 using hard DMA
220 * 1 using virtual DMA
221 * This variable is set to virtual when a DMA mem problem arises, and
222 * reset back in floppy_grab_irq_and_dma.
223 * It is not safe to reset it in other circumstances, because the floppy
224 * driver may have several buffers in use at once, and we do currently not
225 * record each buffers capabilities
226 */
227
228static DEFINE_SPINLOCK(floppy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230static unsigned short virtual_dma_port = 0x3f0;
David Howells7d12e782006-10-05 14:55:46 +0100231irqreturn_t floppy_interrupt(int irq, void *dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232static int set_dor(int fdc, char mask, char data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234#define K_64 0x10000 /* 64KB */
235
236/* the following is the mask of allowed drives. By default units 2 and
237 * 3 of both floppy controllers are disabled, because switching on the
238 * motor of these drives causes system hangs on some PCI computers. drive
239 * 0 is the low bit (0x1), and drive 7 is the high bit (0x80). Bits are on if
240 * a drive is allowed.
241 *
242 * NOTE: This must come before we include the arch floppy header because
243 * some ports reference this variable from there. -DaveM
244 */
245
246static int allowed_drive_mask = 0x33;
247
248#include <asm/floppy.h>
249
250static int irqdma_allocated;
251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252#include <linux/blkdev.h>
253#include <linux/blkpg.h>
254#include <linux/cdrom.h> /* for the compatibility eject ioctl */
255#include <linux/completion.h>
256
257static struct request *current_req;
258static struct request_queue *floppy_queue;
Joe Perches48c8cee2010-03-10 15:20:45 -0800259static void do_fd_request(struct request_queue *q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261#ifndef fd_get_dma_residue
262#define fd_get_dma_residue() get_dma_residue(FLOPPY_DMA)
263#endif
264
265/* Dma Memory related stuff */
266
267#ifndef fd_dma_mem_free
268#define fd_dma_mem_free(addr, size) free_pages(addr, get_order(size))
269#endif
270
271#ifndef fd_dma_mem_alloc
Joe Perches48c8cee2010-03-10 15:20:45 -0800272#define fd_dma_mem_alloc(size) __get_dma_pages(GFP_KERNEL, get_order(size))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273#endif
274
275static inline void fallback_on_nodma_alloc(char **addr, size_t l)
276{
277#ifdef FLOPPY_CAN_FALLBACK_ON_NODMA
278 if (*addr)
279 return; /* we have the memory */
280 if (can_use_virtual_dma != 2)
281 return; /* no fallback allowed */
Joe Perchesb46df352010-03-10 15:20:46 -0800282 pr_info("DMA memory shortage. Temporarily falling back on virtual DMA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 *addr = (char *)nodma_mem_alloc(l);
284#else
285 return;
286#endif
287}
288
289/* End dma memory related stuff */
290
291static unsigned long fake_change;
Joe Perches29f1c782010-03-10 15:21:00 -0800292static bool initialized;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Joe Perches48c8cee2010-03-10 15:20:45 -0800294#define ITYPE(x) (((x) >> 2) & 0x1f)
295#define TOMINOR(x) ((x & 3) | ((x & 4) << 5))
296#define UNIT(x) ((x) & 0x03) /* drive on fdc */
297#define FDC(x) (((x) & 0x04) >> 2) /* fdc of drive */
Jesper Juhl06f748c2007-10-16 23:30:57 -0700298 /* reverse mapping from unit and fdc to drive */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299#define REVDRIVE(fdc, unit) ((unit) + ((fdc) << 2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Joe Perches48c8cee2010-03-10 15:20:45 -0800301#define DP (&drive_params[current_drive])
302#define DRS (&drive_state[current_drive])
303#define DRWE (&write_errors[current_drive])
304#define FDCS (&fdc_state[fdc])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Joe Perches48c8cee2010-03-10 15:20:45 -0800306#define UDP (&drive_params[drive])
307#define UDRS (&drive_state[drive])
308#define UDRWE (&write_errors[drive])
309#define UFDCS (&fdc_state[FDC(drive)])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Joe Perches48c8cee2010-03-10 15:20:45 -0800311#define DPRINT(format, args...) \
Joe Perches4d18ef02010-03-10 15:20:59 -0800312 pr_info("floppy%d: " format, current_drive, ##args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Joe Perches48c8cee2010-03-10 15:20:45 -0800314#define PH_HEAD(floppy, head) (((((floppy)->stretch & 2) >> 1) ^ head) << 2)
315#define STRETCH(floppy) ((floppy)->stretch & FD_STRETCH)
316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317/* read/write */
Joe Perches48c8cee2010-03-10 15:20:45 -0800318#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])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327#define NR_RW 9
328
329/* format */
Joe Perches48c8cee2010-03-10 15:20:45 -0800330#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])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334#define NR_F 6
335
336/*
Joe Perches48c8cee2010-03-10 15:20:45 -0800337 * Maximum disk size (in kilobytes).
338 * This default is used whenever the current disk size is unknown.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 * [Now it is rather a minimum]
340 */
341#define MAX_DISK_SIZE 4 /* 3984 */
342
343/*
344 * globals used by 'result()'
345 */
346#define MAX_REPLIES 16
347static unsigned char reply_buffer[MAX_REPLIES];
348static int inr; /* size of reply buffer, when called from interrupt */
Joe Perches48c8cee2010-03-10 15:20:45 -0800349#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])
357
358#define SEL_DLY (2 * HZ / 100)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360/*
361 * this struct defines the different floppy drive types.
362 */
363static 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" },
384
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*/
387
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*/
390
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*/
393
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*/
396
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*/
399
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 */
406};
407
408static struct floppy_drive_params drive_params[N_DRIVE];
409static struct floppy_drive_struct drive_state[N_DRIVE];
410static struct floppy_write_errors write_errors[N_DRIVE];
411static struct timer_list motor_off_timer[N_DRIVE];
412static struct gendisk *disks[N_DRIVE];
413static struct block_device *opened_bdev[N_DRIVE];
Jes Sorensenb1c82b52006-03-23 03:00:26 -0800414static DEFINE_MUTEX(open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415static struct floppy_raw_cmd *raw_cmd, default_raw_cmd;
416
417/*
418 * This struct defines the different floppy types.
419 *
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
Keith Wansbrough9e491842008-09-22 14:57:17 -0700428 * 'options'.
429 *
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).
435 *
436 * Other parameters should be self-explanatory (see also setfdprm(8)).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 */
438/*
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) */
448static 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" */
459
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" */
470
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" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 { 3680,23,2,80,0,0x1C,0x10,0xCF,0x00,"H1840" }, /* 29 1.84MB 3.5" */
Jesper Juhl06f748c2007-10-16 23:30:57 -0700481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 { 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" */
484};
485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486#define SECTSIZE (_FD_SECTSIZE(*floppy))
487
488/* Auto-detection: Disk type used until the next media change occurs. */
489static struct floppy_struct *current_type[N_DRIVE];
490
491/*
492 * User-provided type information. current_type points to
493 * the respective entry of this array.
494 */
495static struct floppy_struct user_params[N_DRIVE];
496
497static sector_t floppy_sizes[256];
498
Hannes Reinecke94fd0db2005-07-15 10:09:25 +0200499static char floppy_device_name[] = "floppy";
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501/*
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.
505 */
506static int probing;
507
508/* Synchronization of FDC access. */
Joe Perches48c8cee2010-03-10 15:20:45 -0800509#define FD_COMMAND_NONE -1
510#define FD_COMMAND_ERROR 2
511#define FD_COMMAND_OKAY 3
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513static volatile int command_status = FD_COMMAND_NONE;
514static unsigned long fdc_busy;
515static DECLARE_WAIT_QUEUE_HEAD(fdc_wait);
516static DECLARE_WAIT_QUEUE_HEAD(command_done);
517
518#define NO_SIGNAL (!interruptible || !signal_pending(current))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520/* Errors during formatting are counted here. */
521static int format_errors;
522
523/* Format request descriptor. */
524static struct format_descr format_req;
525
526/*
527 * Rate is 0 for 500kb/s, 1 for 300kbps, 2 for 250kbps
528 * Spec1 is 0xSH, where S is stepping rate (F=1ms, E=2ms, D=3ms etc),
529 * H is head unload time (1=16ms, 2=32ms, etc)
530 */
531
532/*
533 * Track buffer
534 * Because these are written to by the DMA controller, they must
535 * not contain a 64k byte boundary crossing, or data will be
536 * corrupted/lost.
537 */
538static char *floppy_track_buffer;
539static int max_buffer_sectors;
540
541static int *errors;
Jesper Juhl06f748c2007-10-16 23:30:57 -0700542typedef void (*done_f)(int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543static struct cont_t {
Joe Perches48c8cee2010-03-10 15:20:45 -0800544 void (*interrupt)(void);
545 /* this is called after the interrupt of the
546 * main command */
Jesper Juhl06f748c2007-10-16 23:30:57 -0700547 void (*redo)(void); /* this is called to retry the operation */
548 void (*error)(void); /* this is called to tally an error */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 done_f done; /* this is called to say if the operation has
550 * succeeded/failed */
551} *cont;
552
553static void floppy_ready(void);
554static void floppy_start(void);
555static void process_fd_request(void);
556static void recalibrate_floppy(void);
557static void floppy_shutdown(unsigned long);
558
Philippe De Muyter5a74db02009-02-18 14:48:36 -0800559static int floppy_request_regions(int);
560static void floppy_release_regions(int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561static int floppy_grab_irq_and_dma(void);
562static void floppy_release_irq_and_dma(void);
563
564/*
565 * The "reset" variable should be tested whenever an interrupt is scheduled,
566 * after the commands have been sent. This is to ensure that the driver doesn't
567 * get wedged when the interrupt doesn't come because of a failed command.
568 * reset doesn't need to be tested before sending commands, because
569 * output_byte is automatically disabled when reset is set.
570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571static void reset_fdc(void);
572
573/*
574 * These are global variables, as that's the easiest way to give
575 * information to interrupts. They are the data used for the current
576 * request.
577 */
Joe Perches48c8cee2010-03-10 15:20:45 -0800578#define NO_TRACK -1
579#define NEED_1_RECAL -2
580#define NEED_2_RECAL -3
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582static int usage_count;
583
584/* buffer related variables */
585static int buffer_track = -1;
586static int buffer_drive = -1;
587static int buffer_min = -1;
588static int buffer_max = -1;
589
590/* fdc related variables, should end up in a struct */
591static struct floppy_fdc_state fdc_state[N_FDC];
592static int fdc; /* current fdc */
593
594static struct floppy_struct *_floppy = floppy_type;
595static unsigned char current_drive;
596static long current_count_sectors;
597static unsigned char fsector_t; /* sector in track */
598static unsigned char in_sector_offset; /* offset within physical sector,
599 * expressed in units of 512 bytes */
600
601#ifndef fd_eject
602static inline int fd_eject(int drive)
603{
604 return -EINVAL;
605}
606#endif
607
608/*
609 * Debugging
610 * =========
611 */
612#ifdef DEBUGT
613static long unsigned debugtimer;
614
615static inline void set_debugt(void)
616{
617 debugtimer = jiffies;
618}
619
620static inline void debugt(const char *message)
621{
622 if (DP->flags & DEBUGT)
Joe Perchesb46df352010-03-10 15:20:46 -0800623 pr_info("%s dtime=%lu\n", message, jiffies - debugtimer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624}
625#else
626static inline void set_debugt(void) { }
627static inline void debugt(const char *message) { }
628#endif /* DEBUGT */
629
Joe Perchesa0a52d62010-03-10 15:20:52 -0800630typedef void (*timeout_fn)(unsigned long);
Ingo Molnar8d06afa2005-09-09 13:10:40 -0700631static DEFINE_TIMER(fd_timeout, floppy_shutdown, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633static const char *timeout_message;
634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635static void is_alive(const char *message)
636{
637 /* this routine checks whether the floppy driver is "alive" */
Joe Perchesc5297302010-03-10 15:20:58 -0800638 if (test_bit(0, &fdc_busy) && command_status < 2 &&
639 !timer_pending(&fd_timeout)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 DPRINT("timeout handler died: %s\n", message);
641 }
642}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Joe Perches48c8cee2010-03-10 15:20:45 -0800644static void (*do_floppy)(void) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646#define OLOGSIZE 20
647
Joe Perches48c8cee2010-03-10 15:20:45 -0800648static void (*lasthandler)(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649static unsigned long interruptjiffies;
650static unsigned long resultjiffies;
651static int resultsize;
652static unsigned long lastredo;
653
654static struct output_log {
655 unsigned char data;
656 unsigned char status;
657 unsigned long jiffies;
658} output_log[OLOGSIZE];
659
660static int output_log_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662#define current_reqD -1
663#define MAXTIMEOUT -2
664
Joe Perches73507e62010-03-10 15:21:03 -0800665static void __reschedule_timeout(int drive, const char *message)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
667 if (drive == current_reqD)
668 drive = current_drive;
669 del_timer(&fd_timeout);
Eric Sesterhenn / Snakebyte4acb3e22007-05-23 13:58:15 -0700670 if (drive < 0 || drive >= N_DRIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 fd_timeout.expires = jiffies + 20UL * HZ;
672 drive = 0;
673 } else
674 fd_timeout.expires = jiffies + UDP->timeout;
675 add_timer(&fd_timeout);
Joe Perchesa81ee542010-03-10 15:20:46 -0800676 if (UDP->flags & FD_DEBUG)
Joe Perches73507e62010-03-10 15:21:03 -0800677 DPRINT("reschedule timeout %s\n", message);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 timeout_message = message;
679}
680
Joe Perches73507e62010-03-10 15:21:03 -0800681static void reschedule_timeout(int drive, const char *message)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
683 unsigned long flags;
684
685 spin_lock_irqsave(&floppy_lock, flags);
Joe Perches73507e62010-03-10 15:21:03 -0800686 __reschedule_timeout(drive, message);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 spin_unlock_irqrestore(&floppy_lock, flags);
688}
689
Joe Perches48c8cee2010-03-10 15:20:45 -0800690#define INFBOUND(a, b) (a) = max_t(int, a, b)
691#define SUPBOUND(a, b) (a) = min_t(int, a, b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693/*
694 * Bottom half floppy driver.
695 * ==========================
696 *
697 * This part of the file contains the code talking directly to the hardware,
698 * and also the main service loop (seek-configure-spinup-command)
699 */
700
701/*
702 * disk change.
703 * This routine is responsible for maintaining the FD_DISK_CHANGE flag,
704 * and the last_checked date.
705 *
706 * last_checked is the date of the last check which showed 'no disk change'
707 * FD_DISK_CHANGE is set under two conditions:
708 * 1. The floppy has been changed after some i/o to that floppy already
709 * took place.
710 * 2. No floppy disk is in the drive. This is done in order to ensure that
711 * requests are quickly flushed in case there is no disk in the drive. It
712 * follows that FD_DISK_CHANGE can only be cleared if there is a disk in
713 * the drive.
714 *
715 * For 1., maxblock is observed. Maxblock is 0 if no i/o has taken place yet.
716 * For 2., FD_DISK_NEWCHANGE is watched. FD_DISK_NEWCHANGE is cleared on
717 * each seek. If a disk is present, the disk change line should also be
718 * cleared on each seek. Thus, if FD_DISK_NEWCHANGE is clear, but the disk
719 * change line is set, this means either that no disk is in the drive, or
720 * that it has been removed since the last seek.
721 *
722 * This means that we really have a third possibility too:
723 * The floppy has been changed after the last seek.
724 */
725
726static int disk_change(int drive)
727{
728 int fdc = FDC(drive);
Jesper Juhl06f748c2007-10-16 23:30:57 -0700729
Marcelo Feitoza Parisi50297cb2006-03-28 01:56:44 -0800730 if (time_before(jiffies, UDRS->select_date + UDP->select_delay))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 DPRINT("WARNING disk change called early\n");
732 if (!(FDCS->dor & (0x10 << UNIT(drive))) ||
733 (FDCS->dor & 3) != UNIT(drive) || fdc != FDC(drive)) {
734 DPRINT("probing disk change on unselected drive\n");
735 DPRINT("drive=%d fdc=%d dor=%x\n", drive, FDC(drive),
736 (unsigned int)FDCS->dor);
737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Joe Perches87f530d2010-03-10 15:20:54 -0800739 debug_dcl(UDP->flags,
740 "checking disk change line for drive %d\n", drive);
741 debug_dcl(UDP->flags, "jiffies=%lu\n", jiffies);
742 debug_dcl(UDP->flags, "disk change line=%x\n", fd_inb(FD_DIR) & 0x80);
743 debug_dcl(UDP->flags, "flags=%lx\n", UDRS->flags);
744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 if (UDP->flags & FD_BROKEN_DCL)
Joe Perchese0298532010-03-10 15:20:55 -0800746 return test_bit(FD_DISK_CHANGED_BIT, &UDRS->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 if ((fd_inb(FD_DIR) ^ UDP->flags) & 0x80) {
Joe Perchese0298532010-03-10 15:20:55 -0800748 set_bit(FD_VERIFY_BIT, &UDRS->flags);
749 /* verify write protection */
750
751 if (UDRS->maxblock) /* mark it changed */
752 set_bit(FD_DISK_CHANGED_BIT, &UDRS->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754 /* invalidate its geometry */
755 if (UDRS->keep_data >= 0) {
756 if ((UDP->flags & FTD_MSG) &&
757 current_type[drive] != NULL)
758 DPRINT("Disk type is undefined after "
759 "disk change\n");
760 current_type[drive] = NULL;
761 floppy_sizes[TOMINOR(drive)] = MAX_DISK_SIZE << 1;
762 }
763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 return 1;
765 } else {
766 UDRS->last_checked = jiffies;
Joe Perchese0298532010-03-10 15:20:55 -0800767 clear_bit(FD_DISK_NEWCHANGE_BIT, &UDRS->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 }
769 return 0;
770}
771
772static inline int is_selected(int dor, int unit)
773{
774 return ((dor & (0x10 << unit)) && (dor & 3) == unit);
775}
776
Joe Perches57584c52010-03-10 15:21:00 -0800777static bool is_ready_state(int status)
778{
779 int state = status & (STATUS_READY | STATUS_DIR | STATUS_DMA);
780 return state == STATUS_READY;
781}
782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783static int set_dor(int fdc, char mask, char data)
784{
Jesper Juhlfdc1ca82007-10-16 23:30:58 -0700785 unsigned char unit;
786 unsigned char drive;
787 unsigned char newdor;
788 unsigned char olddor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790 if (FDCS->address == -1)
791 return -1;
792
793 olddor = FDCS->dor;
794 newdor = (olddor & mask) | data;
795 if (newdor != olddor) {
796 unit = olddor & 0x3;
797 if (is_selected(olddor, unit) && !is_selected(newdor, unit)) {
798 drive = REVDRIVE(fdc, unit);
Joe Perches87f530d2010-03-10 15:20:54 -0800799 debug_dcl(UDP->flags,
800 "calling disk change from set_dor\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 disk_change(drive);
802 }
803 FDCS->dor = newdor;
804 fd_outb(newdor, FD_DOR);
805
806 unit = newdor & 0x3;
807 if (!is_selected(olddor, unit) && is_selected(newdor, unit)) {
808 drive = REVDRIVE(fdc, unit);
809 UDRS->select_date = jiffies;
810 }
811 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 return olddor;
813}
814
815static void twaddle(void)
816{
817 if (DP->select_delay)
818 return;
819 fd_outb(FDCS->dor & ~(0x10 << UNIT(current_drive)), FD_DOR);
820 fd_outb(FDCS->dor, FD_DOR);
821 DRS->select_date = jiffies;
822}
823
Joe Perches57584c52010-03-10 15:21:00 -0800824/*
825 * Reset all driver information about the current fdc.
826 * This is needed after a reset, and after a raw command.
827 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828static void reset_fdc_info(int mode)
829{
830 int drive;
831
832 FDCS->spec1 = FDCS->spec2 = -1;
833 FDCS->need_configure = 1;
834 FDCS->perp_mode = 1;
835 FDCS->rawcmd = 0;
836 for (drive = 0; drive < N_DRIVE; drive++)
837 if (FDC(drive) == fdc && (mode || UDRS->track != NEED_1_RECAL))
838 UDRS->track = NEED_2_RECAL;
839}
840
841/* selects the fdc and drive, and enables the fdc's input/dma. */
842static void set_fdc(int drive)
843{
844 if (drive >= 0 && drive < N_DRIVE) {
845 fdc = FDC(drive);
846 current_drive = drive;
847 }
848 if (fdc != 1 && fdc != 0) {
Joe Perchesb46df352010-03-10 15:20:46 -0800849 pr_info("bad fdc value\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 return;
851 }
852 set_dor(fdc, ~0, 8);
853#if N_FDC > 1
854 set_dor(1 - fdc, ~8, 0);
855#endif
856 if (FDCS->rawcmd == 2)
857 reset_fdc_info(1);
858 if (fd_inb(FD_STATUS) != STATUS_READY)
859 FDCS->reset = 1;
860}
861
862/* locks the driver */
Joe Perches74f63f42010-03-10 15:20:58 -0800863static int _lock_fdc(int drive, bool interruptible, int line)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
865 if (!usage_count) {
Joe Perchesb46df352010-03-10 15:20:46 -0800866 pr_err("Trying to lock fdc while usage count=0 at line %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 line);
868 return -1;
869 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
871 if (test_and_set_bit(0, &fdc_busy)) {
872 DECLARE_WAITQUEUE(wait, current);
873 add_wait_queue(&fdc_wait, &wait);
874
875 for (;;) {
876 set_current_state(TASK_INTERRUPTIBLE);
877
878 if (!test_and_set_bit(0, &fdc_busy))
879 break;
880
881 schedule();
882
883 if (!NO_SIGNAL) {
884 remove_wait_queue(&fdc_wait, &wait);
885 return -EINTR;
886 }
887 }
888
889 set_current_state(TASK_RUNNING);
890 remove_wait_queue(&fdc_wait, &wait);
Ingo Molnar3e541a42006-07-03 00:24:23 -0700891 flush_scheduled_work();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 }
893 command_status = FD_COMMAND_NONE;
894
Joe Perches73507e62010-03-10 15:21:03 -0800895 __reschedule_timeout(drive, "lock fdc");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 set_fdc(drive);
897 return 0;
898}
899
Joe Perches48c8cee2010-03-10 15:20:45 -0800900#define lock_fdc(drive, interruptible) \
901 _lock_fdc(drive, interruptible, __LINE__)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903/* unlocks the driver */
904static inline void unlock_fdc(void)
905{
906 unsigned long flags;
907
908 raw_cmd = NULL;
909 if (!test_bit(0, &fdc_busy))
910 DPRINT("FDC access conflict!\n");
911
912 if (do_floppy)
913 DPRINT("device interrupt still active at FDC release: %p!\n",
914 do_floppy);
915 command_status = FD_COMMAND_NONE;
916 spin_lock_irqsave(&floppy_lock, flags);
917 del_timer(&fd_timeout);
918 cont = NULL;
919 clear_bit(0, &fdc_busy);
Tejun Heo9934c8c2009-05-08 11:54:16 +0900920 if (current_req || blk_peek_request(floppy_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 do_fd_request(floppy_queue);
922 spin_unlock_irqrestore(&floppy_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 wake_up(&fdc_wait);
924}
925
926/* switches the motor off after a given timeout */
927static void motor_off_callback(unsigned long nr)
928{
929 unsigned char mask = ~(0x10 << UNIT(nr));
930
931 set_dor(FDC(nr), mask, 0);
932}
933
934/* schedules motor off */
935static void floppy_off(unsigned int drive)
936{
937 unsigned long volatile delta;
Jesper Juhlfdc1ca82007-10-16 23:30:58 -0700938 int fdc = FDC(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
940 if (!(FDCS->dor & (0x10 << UNIT(drive))))
941 return;
942
943 del_timer(motor_off_timer + drive);
944
945 /* make spindle stop in a position which minimizes spinup time
946 * next time */
947 if (UDP->rps) {
948 delta = jiffies - UDRS->first_read_date + HZ -
949 UDP->spindown_offset;
950 delta = ((delta * UDP->rps) % HZ) / UDP->rps;
951 motor_off_timer[drive].expires =
952 jiffies + UDP->spindown - delta;
953 }
954 add_timer(motor_off_timer + drive);
955}
956
957/*
958 * cycle through all N_DRIVE floppy drives, for disk change testing.
959 * stopping at current drive. This is done before any long operation, to
960 * be sure to have up to date disk change information.
961 */
962static void scandrives(void)
963{
Jesper Juhl06f748c2007-10-16 23:30:57 -0700964 int i;
965 int drive;
966 int saved_drive;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
968 if (DP->select_delay)
969 return;
970
971 saved_drive = current_drive;
972 for (i = 0; i < N_DRIVE; i++) {
973 drive = (saved_drive + i + 1) % N_DRIVE;
974 if (UDRS->fd_ref == 0 || UDP->select_delay != 0)
975 continue; /* skip closed drives */
976 set_fdc(drive);
977 if (!(set_dor(fdc, ~3, UNIT(drive) | (0x10 << UNIT(drive))) &
978 (0x10 << UNIT(drive))))
979 /* switch the motor off again, if it was off to
980 * begin with */
981 set_dor(fdc, ~(0x10 << UNIT(drive)), 0);
982 }
983 set_fdc(saved_drive);
984}
985
986static void empty(void)
987{
988}
989
David Howells65f27f32006-11-22 14:55:48 +0000990static DECLARE_WORK(floppy_work, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Joe Perches48c8cee2010-03-10 15:20:45 -0800992static void schedule_bh(void (*handler)(void))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993{
David Howells65f27f32006-11-22 14:55:48 +0000994 PREPARE_WORK(&floppy_work, (work_func_t)handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 schedule_work(&floppy_work);
996}
997
Ingo Molnar8d06afa2005-09-09 13:10:40 -0700998static DEFINE_TIMER(fd_timer, NULL, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
1000static void cancel_activity(void)
1001{
1002 unsigned long flags;
1003
1004 spin_lock_irqsave(&floppy_lock, flags);
1005 do_floppy = NULL;
David Howells65f27f32006-11-22 14:55:48 +00001006 PREPARE_WORK(&floppy_work, (work_func_t)empty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 del_timer(&fd_timer);
1008 spin_unlock_irqrestore(&floppy_lock, flags);
1009}
1010
1011/* this function makes sure that the disk stays in the drive during the
1012 * transfer */
1013static void fd_watchdog(void)
1014{
Joe Perches87f530d2010-03-10 15:20:54 -08001015 debug_dcl(DP->flags, "calling disk change from watchdog\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
1017 if (disk_change(current_drive)) {
1018 DPRINT("disk removed during i/o\n");
1019 cancel_activity();
1020 cont->done(0);
1021 reset_fdc();
1022 } else {
1023 del_timer(&fd_timer);
Joe Perchesa0a52d62010-03-10 15:20:52 -08001024 fd_timer.function = (timeout_fn)fd_watchdog;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 fd_timer.expires = jiffies + HZ / 10;
1026 add_timer(&fd_timer);
1027 }
1028}
1029
1030static void main_command_interrupt(void)
1031{
1032 del_timer(&fd_timer);
1033 cont->interrupt();
1034}
1035
1036/* waits for a delay (spinup or select) to pass */
1037static int fd_wait_for_completion(unsigned long delay, timeout_fn function)
1038{
1039 if (FDCS->reset) {
1040 reset_fdc(); /* do the reset during sleep to win time
1041 * if we don't need to sleep, it's a good
1042 * occasion anyways */
1043 return 1;
1044 }
1045
Marcelo Feitoza Parisi50297cb2006-03-28 01:56:44 -08001046 if (time_before(jiffies, delay)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 del_timer(&fd_timer);
1048 fd_timer.function = function;
1049 fd_timer.expires = delay;
1050 add_timer(&fd_timer);
1051 return 1;
1052 }
1053 return 0;
1054}
1055
1056static DEFINE_SPINLOCK(floppy_hlt_lock);
1057static int hlt_disabled;
1058static void floppy_disable_hlt(void)
1059{
1060 unsigned long flags;
1061
1062 spin_lock_irqsave(&floppy_hlt_lock, flags);
1063 if (!hlt_disabled) {
1064 hlt_disabled = 1;
1065#ifdef HAVE_DISABLE_HLT
1066 disable_hlt();
1067#endif
1068 }
1069 spin_unlock_irqrestore(&floppy_hlt_lock, flags);
1070}
1071
1072static void floppy_enable_hlt(void)
1073{
1074 unsigned long flags;
1075
1076 spin_lock_irqsave(&floppy_hlt_lock, flags);
1077 if (hlt_disabled) {
1078 hlt_disabled = 0;
1079#ifdef HAVE_DISABLE_HLT
1080 enable_hlt();
1081#endif
1082 }
1083 spin_unlock_irqrestore(&floppy_hlt_lock, flags);
1084}
1085
1086static void setup_DMA(void)
1087{
1088 unsigned long f;
1089
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 if (raw_cmd->length == 0) {
1091 int i;
1092
Joe Perchesb46df352010-03-10 15:20:46 -08001093 pr_info("zero dma transfer size:");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 for (i = 0; i < raw_cmd->cmd_count; i++)
Joe Perchesb46df352010-03-10 15:20:46 -08001095 pr_cont("%x,", raw_cmd->cmd[i]);
1096 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 cont->done(0);
1098 FDCS->reset = 1;
1099 return;
1100 }
1101 if (((unsigned long)raw_cmd->kernel_data) % 512) {
Joe Perchesb46df352010-03-10 15:20:46 -08001102 pr_info("non aligned address: %p\n", raw_cmd->kernel_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 cont->done(0);
1104 FDCS->reset = 1;
1105 return;
1106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 f = claim_dma_lock();
1108 fd_disable_dma();
1109#ifdef fd_dma_setup
1110 if (fd_dma_setup(raw_cmd->kernel_data, raw_cmd->length,
1111 (raw_cmd->flags & FD_RAW_READ) ?
1112 DMA_MODE_READ : DMA_MODE_WRITE, FDCS->address) < 0) {
1113 release_dma_lock(f);
1114 cont->done(0);
1115 FDCS->reset = 1;
1116 return;
1117 }
1118 release_dma_lock(f);
1119#else
1120 fd_clear_dma_ff();
1121 fd_cacheflush(raw_cmd->kernel_data, raw_cmd->length);
1122 fd_set_dma_mode((raw_cmd->flags & FD_RAW_READ) ?
1123 DMA_MODE_READ : DMA_MODE_WRITE);
1124 fd_set_dma_addr(raw_cmd->kernel_data);
1125 fd_set_dma_count(raw_cmd->length);
1126 virtual_dma_port = FDCS->address;
1127 fd_enable_dma();
1128 release_dma_lock(f);
1129#endif
1130 floppy_disable_hlt();
1131}
1132
1133static void show_floppy(void);
1134
1135/* waits until the fdc becomes ready */
1136static int wait_til_ready(void)
1137{
Jesper Juhl06f748c2007-10-16 23:30:57 -07001138 int status;
1139 int counter;
1140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 if (FDCS->reset)
1142 return -1;
1143 for (counter = 0; counter < 10000; counter++) {
1144 status = fd_inb(FD_STATUS);
1145 if (status & STATUS_READY)
1146 return status;
1147 }
Joe Perches29f1c782010-03-10 15:21:00 -08001148 if (initialized) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 DPRINT("Getstatus times out (%x) on fdc %d\n", status, fdc);
1150 show_floppy();
1151 }
1152 FDCS->reset = 1;
1153 return -1;
1154}
1155
1156/* sends a command byte to the fdc */
1157static int output_byte(char byte)
1158{
Joe Perchesd7b2b2e2010-03-10 15:20:48 -08001159 int status = wait_til_ready();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
Joe Perchesd7b2b2e2010-03-10 15:20:48 -08001161 if (status < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 return -1;
Joe Perches57584c52010-03-10 15:21:00 -08001163
1164 if (is_ready_state(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 fd_outb(byte, FD_DATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 output_log[output_log_pos].data = byte;
1167 output_log[output_log_pos].status = status;
1168 output_log[output_log_pos].jiffies = jiffies;
1169 output_log_pos = (output_log_pos + 1) % OLOGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 return 0;
1171 }
1172 FDCS->reset = 1;
Joe Perches29f1c782010-03-10 15:21:00 -08001173 if (initialized) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 DPRINT("Unable to send byte %x to FDC. Fdc=%x Status=%x\n",
1175 byte, fdc, status);
1176 show_floppy();
1177 }
1178 return -1;
1179}
1180
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181/* gets the response from the fdc */
1182static int result(void)
1183{
Jesper Juhl06f748c2007-10-16 23:30:57 -07001184 int i;
1185 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
1187 for (i = 0; i < MAX_REPLIES; i++) {
Joe Perchesd7b2b2e2010-03-10 15:20:48 -08001188 status = wait_til_ready();
1189 if (status < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 break;
1191 status &= STATUS_DIR | STATUS_READY | STATUS_BUSY | STATUS_DMA;
1192 if ((status & ~STATUS_BUSY) == STATUS_READY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 resultjiffies = jiffies;
1194 resultsize = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 return i;
1196 }
1197 if (status == (STATUS_DIR | STATUS_READY | STATUS_BUSY))
1198 reply_buffer[i] = fd_inb(FD_DATA);
1199 else
1200 break;
1201 }
Joe Perches29f1c782010-03-10 15:21:00 -08001202 if (initialized) {
1203 DPRINT("get result error. Fdc=%d Last status=%x Read bytes=%d\n",
1204 fdc, status, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 show_floppy();
1206 }
1207 FDCS->reset = 1;
1208 return -1;
1209}
1210
1211#define MORE_OUTPUT -2
1212/* does the fdc need more output? */
1213static int need_more_output(void)
1214{
Joe Perchesd7b2b2e2010-03-10 15:20:48 -08001215 int status = wait_til_ready();
Jesper Juhl06f748c2007-10-16 23:30:57 -07001216
Joe Perchesd7b2b2e2010-03-10 15:20:48 -08001217 if (status < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 return -1;
Joe Perches57584c52010-03-10 15:21:00 -08001219
1220 if (is_ready_state(status))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 return MORE_OUTPUT;
Joe Perches57584c52010-03-10 15:21:00 -08001222
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 return result();
1224}
1225
1226/* Set perpendicular mode as required, based on data rate, if supported.
1227 * 82077 Now tested. 1Mbps data rate only possible with 82077-1.
1228 */
1229static inline void perpendicular_mode(void)
1230{
1231 unsigned char perp_mode;
1232
1233 if (raw_cmd->rate & 0x40) {
1234 switch (raw_cmd->rate & 3) {
1235 case 0:
1236 perp_mode = 2;
1237 break;
1238 case 3:
1239 perp_mode = 3;
1240 break;
1241 default:
1242 DPRINT("Invalid data rate for perpendicular mode!\n");
1243 cont->done(0);
Joe Perchesbb57f0c62010-03-10 15:20:50 -08001244 FDCS->reset = 1;
1245 /*
1246 * convenient way to return to
1247 * redo without too much hassle
1248 * (deep stack et al.)
1249 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 return;
1251 }
1252 } else
1253 perp_mode = 0;
1254
1255 if (FDCS->perp_mode == perp_mode)
1256 return;
1257 if (FDCS->version >= FDC_82077_ORIG) {
1258 output_byte(FD_PERPENDICULAR);
1259 output_byte(perp_mode);
1260 FDCS->perp_mode = perp_mode;
1261 } else if (perp_mode) {
1262 DPRINT("perpendicular mode not supported by this FDC.\n");
1263 }
1264} /* perpendicular_mode */
1265
1266static int fifo_depth = 0xa;
1267static int no_fifo;
1268
1269static int fdc_configure(void)
1270{
1271 /* Turn on FIFO */
1272 output_byte(FD_CONFIGURE);
1273 if (need_more_output() != MORE_OUTPUT)
1274 return 0;
1275 output_byte(0);
1276 output_byte(0x10 | (no_fifo & 0x20) | (fifo_depth & 0xf));
1277 output_byte(0); /* pre-compensation from track
1278 0 upwards */
1279 return 1;
1280}
1281
1282#define NOMINAL_DTR 500
1283
1284/* Issue a "SPECIFY" command to set the step rate time, head unload time,
1285 * head load time, and DMA disable flag to values needed by floppy.
1286 *
1287 * The value "dtr" is the data transfer rate in Kbps. It is needed
1288 * to account for the data rate-based scaling done by the 82072 and 82077
1289 * FDC types. This parameter is ignored for other types of FDCs (i.e.
1290 * 8272a).
1291 *
1292 * Note that changing the data transfer rate has a (probably deleterious)
1293 * effect on the parameters subject to scaling for 82072/82077 FDCs, so
1294 * fdc_specify is called again after each data transfer rate
1295 * change.
1296 *
1297 * srt: 1000 to 16000 in microseconds
1298 * hut: 16 to 240 milliseconds
1299 * hlt: 2 to 254 milliseconds
1300 *
1301 * These values are rounded up to the next highest available delay time.
1302 */
1303static void fdc_specify(void)
1304{
Jesper Juhl06f748c2007-10-16 23:30:57 -07001305 unsigned char spec1;
1306 unsigned char spec2;
1307 unsigned long srt;
1308 unsigned long hlt;
1309 unsigned long hut;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 unsigned long dtr = NOMINAL_DTR;
1311 unsigned long scale_dtr = NOMINAL_DTR;
1312 int hlt_max_code = 0x7f;
1313 int hut_max_code = 0xf;
1314
1315 if (FDCS->need_configure && FDCS->version >= FDC_82072A) {
1316 fdc_configure();
1317 FDCS->need_configure = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 }
1319
1320 switch (raw_cmd->rate & 0x03) {
1321 case 3:
1322 dtr = 1000;
1323 break;
1324 case 1:
1325 dtr = 300;
1326 if (FDCS->version >= FDC_82078) {
1327 /* chose the default rate table, not the one
1328 * where 1 = 2 Mbps */
1329 output_byte(FD_DRIVESPEC);
1330 if (need_more_output() == MORE_OUTPUT) {
1331 output_byte(UNIT(current_drive));
1332 output_byte(0xc0);
1333 }
1334 }
1335 break;
1336 case 2:
1337 dtr = 250;
1338 break;
1339 }
1340
1341 if (FDCS->version >= FDC_82072) {
1342 scale_dtr = dtr;
1343 hlt_max_code = 0x00; /* 0==256msec*dtr0/dtr (not linear!) */
1344 hut_max_code = 0x0; /* 0==256msec*dtr0/dtr (not linear!) */
1345 }
1346
1347 /* Convert step rate from microseconds to milliseconds and 4 bits */
Julia Lawall061837b2008-09-22 14:57:16 -07001348 srt = 16 - DIV_ROUND_UP(DP->srt * scale_dtr / 1000, NOMINAL_DTR);
Joe Perchesa81ee542010-03-10 15:20:46 -08001349 if (slow_floppy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 srt = srt / 4;
Joe Perchesa81ee542010-03-10 15:20:46 -08001351
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 SUPBOUND(srt, 0xf);
1353 INFBOUND(srt, 0);
1354
Julia Lawall061837b2008-09-22 14:57:16 -07001355 hlt = DIV_ROUND_UP(DP->hlt * scale_dtr / 2, NOMINAL_DTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 if (hlt < 0x01)
1357 hlt = 0x01;
1358 else if (hlt > 0x7f)
1359 hlt = hlt_max_code;
1360
Julia Lawall061837b2008-09-22 14:57:16 -07001361 hut = DIV_ROUND_UP(DP->hut * scale_dtr / 16, NOMINAL_DTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 if (hut < 0x1)
1363 hut = 0x1;
1364 else if (hut > 0xf)
1365 hut = hut_max_code;
1366
1367 spec1 = (srt << 4) | hut;
1368 spec2 = (hlt << 1) | (use_virtual_dma & 1);
1369
1370 /* If these parameters did not change, just return with success */
1371 if (FDCS->spec1 != spec1 || FDCS->spec2 != spec2) {
1372 /* Go ahead and set spec1 and spec2 */
1373 output_byte(FD_SPECIFY);
1374 output_byte(FDCS->spec1 = spec1);
1375 output_byte(FDCS->spec2 = spec2);
1376 }
1377} /* fdc_specify */
1378
1379/* Set the FDC's data transfer rate on behalf of the specified drive.
1380 * NOTE: with 82072/82077 FDCs, changing the data rate requires a reissue
1381 * of the specify command (i.e. using the fdc_specify function).
1382 */
1383static int fdc_dtr(void)
1384{
1385 /* If data rate not already set to desired value, set it. */
1386 if ((raw_cmd->rate & 3) == FDCS->dtr)
1387 return 0;
1388
1389 /* Set dtr */
1390 fd_outb(raw_cmd->rate & 3, FD_DCR);
1391
1392 /* TODO: some FDC/drive combinations (C&T 82C711 with TEAC 1.2MB)
1393 * need a stabilization period of several milliseconds to be
1394 * enforced after data rate changes before R/W operations.
1395 * Pause 5 msec to avoid trouble. (Needs to be 2 jiffies)
1396 */
1397 FDCS->dtr = raw_cmd->rate & 3;
Joe Perchesd7b2b2e2010-03-10 15:20:48 -08001398 return fd_wait_for_completion(jiffies + 2UL * HZ / 100,
1399 (timeout_fn)floppy_ready);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400} /* fdc_dtr */
1401
1402static void tell_sector(void)
1403{
Joe Perchesb46df352010-03-10 15:20:46 -08001404 pr_cont(": track %d, head %d, sector %d, size %d",
1405 R_TRACK, R_HEAD, R_SECTOR, R_SIZECODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406} /* tell_sector */
1407
Joe Perchesb46df352010-03-10 15:20:46 -08001408static void print_errors(void)
1409{
1410 DPRINT("");
1411 if (ST0 & ST0_ECE) {
1412 pr_cont("Recalibrate failed!");
1413 } else if (ST2 & ST2_CRC) {
1414 pr_cont("data CRC error");
1415 tell_sector();
1416 } else if (ST1 & ST1_CRC) {
1417 pr_cont("CRC error");
1418 tell_sector();
1419 } else if ((ST1 & (ST1_MAM | ST1_ND)) ||
1420 (ST2 & ST2_MAM)) {
1421 if (!probing) {
1422 pr_cont("sector not found");
1423 tell_sector();
1424 } else
1425 pr_cont("probe failed...");
1426 } else if (ST2 & ST2_WC) { /* seek error */
1427 pr_cont("wrong cylinder");
1428 } else if (ST2 & ST2_BC) { /* cylinder marked as bad */
1429 pr_cont("bad cylinder");
1430 } else {
1431 pr_cont("unknown error. ST[0..2] are: 0x%x 0x%x 0x%x",
1432 ST0, ST1, ST2);
1433 tell_sector();
1434 }
1435 pr_cont("\n");
1436}
1437
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438/*
1439 * OK, this error interpreting routine is called after a
1440 * DMA read/write has succeeded
1441 * or failed, so we check the results, and copy any buffers.
1442 * hhb: Added better error reporting.
1443 * ak: Made this into a separate routine.
1444 */
1445static int interpret_errors(void)
1446{
1447 char bad;
1448
1449 if (inr != 7) {
1450 DPRINT("-- FDC reply error");
1451 FDCS->reset = 1;
1452 return 1;
1453 }
1454
1455 /* check IC to find cause of interrupt */
1456 switch (ST0 & ST0_INTR) {
1457 case 0x40: /* error occurred during command execution */
1458 if (ST1 & ST1_EOC)
1459 return 0; /* occurs with pseudo-DMA */
1460 bad = 1;
1461 if (ST1 & ST1_WP) {
1462 DPRINT("Drive is write protected\n");
Joe Perchese0298532010-03-10 15:20:55 -08001463 clear_bit(FD_DISK_WRITABLE_BIT, &DRS->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 cont->done(0);
1465 bad = 2;
1466 } else if (ST1 & ST1_ND) {
Joe Perchese0298532010-03-10 15:20:55 -08001467 set_bit(FD_NEED_TWADDLE_BIT, &DRS->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 } else if (ST1 & ST1_OR) {
1469 if (DP->flags & FTD_MSG)
1470 DPRINT("Over/Underrun - retrying\n");
1471 bad = 0;
1472 } else if (*errors >= DP->max_errors.reporting) {
Joe Perchesb46df352010-03-10 15:20:46 -08001473 print_errors();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 }
1475 if (ST2 & ST2_WC || ST2 & ST2_BC)
1476 /* wrong cylinder => recal */
1477 DRS->track = NEED_2_RECAL;
1478 return bad;
1479 case 0x80: /* invalid command given */
1480 DPRINT("Invalid FDC command given!\n");
1481 cont->done(0);
1482 return 2;
1483 case 0xc0:
1484 DPRINT("Abnormal termination caused by polling\n");
1485 cont->error();
1486 return 2;
1487 default: /* (0) Normal command termination */
1488 return 0;
1489 }
1490}
1491
1492/*
1493 * This routine is called when everything should be correctly set up
1494 * for the transfer (i.e. floppy motor is on, the correct floppy is
1495 * selected, and the head is sitting on the right track).
1496 */
1497static void setup_rw_floppy(void)
1498{
Jesper Juhl06f748c2007-10-16 23:30:57 -07001499 int i;
1500 int r;
1501 int flags;
1502 int dflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 unsigned long ready_date;
1504 timeout_fn function;
1505
1506 flags = raw_cmd->flags;
1507 if (flags & (FD_RAW_READ | FD_RAW_WRITE))
1508 flags |= FD_RAW_INTR;
1509
1510 if ((flags & FD_RAW_SPIN) && !(flags & FD_RAW_NO_MOTOR)) {
1511 ready_date = DRS->spinup_date + DP->spinup;
1512 /* If spinup will take a long time, rerun scandrives
1513 * again just before spinup completion. Beware that
1514 * after scandrives, we must again wait for selection.
1515 */
Marcelo Feitoza Parisi50297cb2006-03-28 01:56:44 -08001516 if (time_after(ready_date, jiffies + DP->select_delay)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 ready_date -= DP->select_delay;
Joe Perchesa0a52d62010-03-10 15:20:52 -08001518 function = (timeout_fn)floppy_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 } else
Joe Perchesa0a52d62010-03-10 15:20:52 -08001520 function = (timeout_fn)setup_rw_floppy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
1522 /* wait until the floppy is spinning fast enough */
1523 if (fd_wait_for_completion(ready_date, function))
1524 return;
1525 }
1526 dflags = DRS->flags;
1527
1528 if ((flags & FD_RAW_READ) || (flags & FD_RAW_WRITE))
1529 setup_DMA();
1530
1531 if (flags & FD_RAW_INTR)
1532 do_floppy = main_command_interrupt;
1533
1534 r = 0;
1535 for (i = 0; i < raw_cmd->cmd_count; i++)
1536 r |= output_byte(raw_cmd->cmd[i]);
1537
1538 debugt("rw_command: ");
1539
1540 if (r) {
1541 cont->error();
1542 reset_fdc();
1543 return;
1544 }
1545
1546 if (!(flags & FD_RAW_INTR)) {
1547 inr = result();
1548 cont->interrupt();
1549 } else if (flags & FD_RAW_NEED_DISK)
1550 fd_watchdog();
1551}
1552
1553static int blind_seek;
1554
1555/*
1556 * This is the routine called after every seek (or recalibrate) interrupt
1557 * from the floppy controller.
1558 */
1559static void seek_interrupt(void)
1560{
1561 debugt("seek interrupt:");
1562 if (inr != 2 || (ST0 & 0xF8) != 0x20) {
1563 DPRINT("seek failed\n");
1564 DRS->track = NEED_2_RECAL;
1565 cont->error();
1566 cont->redo();
1567 return;
1568 }
1569 if (DRS->track >= 0 && DRS->track != ST1 && !blind_seek) {
Joe Perches87f530d2010-03-10 15:20:54 -08001570 debug_dcl(DP->flags,
1571 "clearing NEWCHANGE flag because of effective seek\n");
1572 debug_dcl(DP->flags, "jiffies=%lu\n", jiffies);
Joe Perchese0298532010-03-10 15:20:55 -08001573 clear_bit(FD_DISK_NEWCHANGE_BIT, &DRS->flags);
1574 /* effective seek */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 DRS->select_date = jiffies;
1576 }
1577 DRS->track = ST1;
1578 floppy_ready();
1579}
1580
1581static void check_wp(void)
1582{
Joe Perchese0298532010-03-10 15:20:55 -08001583 if (test_bit(FD_VERIFY_BIT, &DRS->flags)) {
1584 /* check write protection */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 output_byte(FD_GETSTATUS);
1586 output_byte(UNIT(current_drive));
1587 if (result() != 1) {
1588 FDCS->reset = 1;
1589 return;
1590 }
Joe Perchese0298532010-03-10 15:20:55 -08001591 clear_bit(FD_VERIFY_BIT, &DRS->flags);
1592 clear_bit(FD_NEED_TWADDLE_BIT, &DRS->flags);
Joe Perches87f530d2010-03-10 15:20:54 -08001593 debug_dcl(DP->flags,
1594 "checking whether disk is write protected\n");
1595 debug_dcl(DP->flags, "wp=%x\n", ST3 & 0x40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 if (!(ST3 & 0x40))
Joe Perchese0298532010-03-10 15:20:55 -08001597 set_bit(FD_DISK_WRITABLE_BIT, &DRS->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 else
Joe Perchese0298532010-03-10 15:20:55 -08001599 clear_bit(FD_DISK_WRITABLE_BIT, &DRS->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 }
1601}
1602
1603static void seek_floppy(void)
1604{
1605 int track;
1606
1607 blind_seek = 0;
1608
Joe Perches87f530d2010-03-10 15:20:54 -08001609 debug_dcl(DP->flags, "calling disk change from seek\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
Joe Perchese0298532010-03-10 15:20:55 -08001611 if (!test_bit(FD_DISK_NEWCHANGE_BIT, &DRS->flags) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 disk_change(current_drive) && (raw_cmd->flags & FD_RAW_NEED_DISK)) {
1613 /* the media changed flag should be cleared after the seek.
1614 * If it isn't, this means that there is really no disk in
1615 * the drive.
1616 */
Joe Perchese0298532010-03-10 15:20:55 -08001617 set_bit(FD_DISK_CHANGED_BIT, &DRS->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 cont->done(0);
1619 cont->redo();
1620 return;
1621 }
1622 if (DRS->track <= NEED_1_RECAL) {
1623 recalibrate_floppy();
1624 return;
Joe Perchese0298532010-03-10 15:20:55 -08001625 } else if (test_bit(FD_DISK_NEWCHANGE_BIT, &DRS->flags) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 (raw_cmd->flags & FD_RAW_NEED_DISK) &&
1627 (DRS->track <= NO_TRACK || DRS->track == raw_cmd->track)) {
1628 /* we seek to clear the media-changed condition. Does anybody
1629 * know a more elegant way, which works on all drives? */
1630 if (raw_cmd->track)
1631 track = raw_cmd->track - 1;
1632 else {
1633 if (DP->flags & FD_SILENT_DCL_CLEAR) {
1634 set_dor(fdc, ~(0x10 << UNIT(current_drive)), 0);
1635 blind_seek = 1;
1636 raw_cmd->flags |= FD_RAW_NEED_SEEK;
1637 }
1638 track = 1;
1639 }
1640 } else {
1641 check_wp();
1642 if (raw_cmd->track != DRS->track &&
1643 (raw_cmd->flags & FD_RAW_NEED_SEEK))
1644 track = raw_cmd->track;
1645 else {
1646 setup_rw_floppy();
1647 return;
1648 }
1649 }
1650
1651 do_floppy = seek_interrupt;
1652 output_byte(FD_SEEK);
1653 output_byte(UNIT(current_drive));
Joe Perches2300f902010-03-10 15:20:49 -08001654 if (output_byte(track) < 0) {
1655 reset_fdc();
1656 return;
1657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 debugt("seek command:");
1659}
1660
1661static void recal_interrupt(void)
1662{
1663 debugt("recal interrupt:");
1664 if (inr != 2)
1665 FDCS->reset = 1;
1666 else if (ST0 & ST0_ECE) {
1667 switch (DRS->track) {
1668 case NEED_1_RECAL:
1669 debugt("recal interrupt need 1 recal:");
1670 /* after a second recalibrate, we still haven't
1671 * reached track 0. Probably no drive. Raise an
1672 * error, as failing immediately might upset
1673 * computers possessed by the Devil :-) */
1674 cont->error();
1675 cont->redo();
1676 return;
1677 case NEED_2_RECAL:
1678 debugt("recal interrupt need 2 recal:");
1679 /* If we already did a recalibrate,
1680 * and we are not at track 0, this
1681 * means we have moved. (The only way
1682 * not to move at recalibration is to
1683 * be already at track 0.) Clear the
1684 * new change flag */
Joe Perches87f530d2010-03-10 15:20:54 -08001685 debug_dcl(DP->flags,
1686 "clearing NEWCHANGE flag because of second recalibrate\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
Joe Perchese0298532010-03-10 15:20:55 -08001688 clear_bit(FD_DISK_NEWCHANGE_BIT, &DRS->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 DRS->select_date = jiffies;
1690 /* fall through */
1691 default:
1692 debugt("recal interrupt default:");
1693 /* Recalibrate moves the head by at
1694 * most 80 steps. If after one
1695 * recalibrate we don't have reached
1696 * track 0, this might mean that we
1697 * started beyond track 80. Try
1698 * again. */
1699 DRS->track = NEED_1_RECAL;
1700 break;
1701 }
1702 } else
1703 DRS->track = ST1;
1704 floppy_ready();
1705}
1706
1707static void print_result(char *message, int inr)
1708{
1709 int i;
1710
1711 DPRINT("%s ", message);
1712 if (inr >= 0)
1713 for (i = 0; i < inr; i++)
Joe Perchesb46df352010-03-10 15:20:46 -08001714 pr_cont("repl[%d]=%x ", i, reply_buffer[i]);
1715 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716}
1717
1718/* interrupt handler. Note that this can be called externally on the Sparc */
David Howells7d12e782006-10-05 14:55:46 +01001719irqreturn_t floppy_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 int do_print;
1722 unsigned long f;
Jesper Juhl06f748c2007-10-16 23:30:57 -07001723 void (*handler)(void) = do_floppy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
1725 lasthandler = handler;
1726 interruptjiffies = jiffies;
1727
1728 f = claim_dma_lock();
1729 fd_disable_dma();
1730 release_dma_lock(f);
1731
1732 floppy_enable_hlt();
1733 do_floppy = NULL;
1734 if (fdc >= N_FDC || FDCS->address == -1) {
1735 /* we don't even know which FDC is the culprit */
Joe Perchesb46df352010-03-10 15:20:46 -08001736 pr_info("DOR0=%x\n", fdc_state[0].dor);
1737 pr_info("floppy interrupt on bizarre fdc %d\n", fdc);
1738 pr_info("handler=%p\n", handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 is_alive("bizarre fdc");
1740 return IRQ_NONE;
1741 }
1742
1743 FDCS->reset = 0;
1744 /* We have to clear the reset flag here, because apparently on boxes
1745 * with level triggered interrupts (PS/2, Sparc, ...), it is needed to
1746 * emit SENSEI's to clear the interrupt line. And FDCS->reset blocks the
1747 * emission of the SENSEI's.
1748 * It is OK to emit floppy commands because we are in an interrupt
1749 * handler here, and thus we have to fear no interference of other
1750 * activity.
1751 */
1752
Joe Perches29f1c782010-03-10 15:21:00 -08001753 do_print = !handler && print_unex && initialized;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
1755 inr = result();
1756 if (do_print)
1757 print_result("unexpected interrupt", inr);
1758 if (inr == 0) {
1759 int max_sensei = 4;
1760 do {
1761 output_byte(FD_SENSEI);
1762 inr = result();
1763 if (do_print)
1764 print_result("sensei", inr);
1765 max_sensei--;
Joe Perchesc5297302010-03-10 15:20:58 -08001766 } while ((ST0 & 0x83) != UNIT(current_drive) &&
1767 inr == 2 && max_sensei);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 }
1769 if (!handler) {
1770 FDCS->reset = 1;
1771 return IRQ_NONE;
1772 }
1773 schedule_bh(handler);
1774 is_alive("normal interrupt end");
1775
1776 /* FIXME! Was it really for us? */
1777 return IRQ_HANDLED;
1778}
1779
1780static void recalibrate_floppy(void)
1781{
1782 debugt("recalibrate floppy:");
1783 do_floppy = recal_interrupt;
1784 output_byte(FD_RECALIBRATE);
Joe Perches15b26302010-03-10 15:21:01 -08001785 if (output_byte(UNIT(current_drive)) < 0)
Joe Perches2300f902010-03-10 15:20:49 -08001786 reset_fdc();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787}
1788
1789/*
1790 * Must do 4 FD_SENSEIs after reset because of ``drive polling''.
1791 */
1792static void reset_interrupt(void)
1793{
1794 debugt("reset interrupt:");
1795 result(); /* get the status ready for set_fdc */
1796 if (FDCS->reset) {
Joe Perchesb46df352010-03-10 15:20:46 -08001797 pr_info("reset set in interrupt, calling %p\n", cont->error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 cont->error(); /* a reset just after a reset. BAD! */
1799 }
1800 cont->redo();
1801}
1802
1803/*
1804 * reset is done by pulling bit 2 of DOR low for a while (old FDCs),
1805 * or by setting the self clearing bit 7 of STATUS (newer FDCs)
1806 */
1807static void reset_fdc(void)
1808{
1809 unsigned long flags;
1810
1811 do_floppy = reset_interrupt;
1812 FDCS->reset = 0;
1813 reset_fdc_info(0);
1814
1815 /* Pseudo-DMA may intercept 'reset finished' interrupt. */
1816 /* Irrelevant for systems with true DMA (i386). */
1817
1818 flags = claim_dma_lock();
1819 fd_disable_dma();
1820 release_dma_lock(flags);
1821
1822 if (FDCS->version >= FDC_82072A)
1823 fd_outb(0x80 | (FDCS->dtr & 3), FD_STATUS);
1824 else {
1825 fd_outb(FDCS->dor & ~0x04, FD_DOR);
1826 udelay(FD_RESET_DELAY);
1827 fd_outb(FDCS->dor, FD_DOR);
1828 }
1829}
1830
1831static void show_floppy(void)
1832{
1833 int i;
1834
Joe Perchesb46df352010-03-10 15:20:46 -08001835 pr_info("\n");
1836 pr_info("floppy driver state\n");
1837 pr_info("-------------------\n");
1838 pr_info("now=%lu last interrupt=%lu diff=%lu last called handler=%p\n",
1839 jiffies, interruptjiffies, jiffies - interruptjiffies,
1840 lasthandler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
Joe Perchesb46df352010-03-10 15:20:46 -08001842 pr_info("timeout_message=%s\n", timeout_message);
1843 pr_info("last output bytes:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 for (i = 0; i < OLOGSIZE; i++)
Joe Perchesb46df352010-03-10 15:20:46 -08001845 pr_info("%2x %2x %lu\n",
1846 output_log[(i + output_log_pos) % OLOGSIZE].data,
1847 output_log[(i + output_log_pos) % OLOGSIZE].status,
1848 output_log[(i + output_log_pos) % OLOGSIZE].jiffies);
1849 pr_info("last result at %lu\n", resultjiffies);
1850 pr_info("last redo_fd_request at %lu\n", lastredo);
1851 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_NONE, 16, 1,
1852 reply_buffer, resultsize, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853
Joe Perchesb46df352010-03-10 15:20:46 -08001854 pr_info("status=%x\n", fd_inb(FD_STATUS));
1855 pr_info("fdc_busy=%lu\n", fdc_busy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 if (do_floppy)
Joe Perchesb46df352010-03-10 15:20:46 -08001857 pr_info("do_floppy=%p\n", do_floppy);
David Howells365970a2006-11-22 14:54:49 +00001858 if (work_pending(&floppy_work))
Joe Perchesb46df352010-03-10 15:20:46 -08001859 pr_info("floppy_work.func=%p\n", floppy_work.func);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 if (timer_pending(&fd_timer))
Joe Perchesb46df352010-03-10 15:20:46 -08001861 pr_info("fd_timer.function=%p\n", fd_timer.function);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 if (timer_pending(&fd_timeout)) {
Joe Perchesb46df352010-03-10 15:20:46 -08001863 pr_info("timer_function=%p\n", fd_timeout.function);
1864 pr_info("expires=%lu\n", fd_timeout.expires - jiffies);
1865 pr_info("now=%lu\n", jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 }
Joe Perchesb46df352010-03-10 15:20:46 -08001867 pr_info("cont=%p\n", cont);
1868 pr_info("current_req=%p\n", current_req);
1869 pr_info("command_status=%d\n", command_status);
1870 pr_info("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871}
1872
1873static void floppy_shutdown(unsigned long data)
1874{
1875 unsigned long flags;
1876
Joe Perches29f1c782010-03-10 15:21:00 -08001877 if (initialized)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 show_floppy();
1879 cancel_activity();
1880
1881 floppy_enable_hlt();
1882
1883 flags = claim_dma_lock();
1884 fd_disable_dma();
1885 release_dma_lock(flags);
1886
1887 /* avoid dma going to a random drive after shutdown */
1888
Joe Perches29f1c782010-03-10 15:21:00 -08001889 if (initialized)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 DPRINT("floppy timeout called\n");
1891 FDCS->reset = 1;
1892 if (cont) {
1893 cont->done(0);
1894 cont->redo(); /* this will recall reset when needed */
1895 } else {
Joe Perchesb46df352010-03-10 15:20:46 -08001896 pr_info("no cont in shutdown!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 process_fd_request();
1898 }
1899 is_alive("floppy shutdown");
1900}
1901
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902/* start motor, check media-changed condition and write protection */
Jesper Juhl06f748c2007-10-16 23:30:57 -07001903static int start_motor(void (*function)(void))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904{
Jesper Juhl06f748c2007-10-16 23:30:57 -07001905 int mask;
1906 int data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
1908 mask = 0xfc;
1909 data = UNIT(current_drive);
1910 if (!(raw_cmd->flags & FD_RAW_NO_MOTOR)) {
1911 if (!(FDCS->dor & (0x10 << UNIT(current_drive)))) {
1912 set_debugt();
1913 /* no read since this drive is running */
1914 DRS->first_read_date = 0;
1915 /* note motor start time if motor is not yet running */
1916 DRS->spinup_date = jiffies;
1917 data |= (0x10 << UNIT(current_drive));
1918 }
1919 } else if (FDCS->dor & (0x10 << UNIT(current_drive)))
1920 mask &= ~(0x10 << UNIT(current_drive));
1921
1922 /* starts motor and selects floppy */
1923 del_timer(motor_off_timer + current_drive);
1924 set_dor(fdc, mask, data);
1925
1926 /* wait_for_completion also schedules reset if needed. */
Joe Perchesd7b2b2e2010-03-10 15:20:48 -08001927 return fd_wait_for_completion(DRS->select_date + DP->select_delay,
1928 (timeout_fn)function);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929}
1930
1931static void floppy_ready(void)
1932{
Joe Perches045f9832010-03-10 15:20:47 -08001933 if (FDCS->reset) {
1934 reset_fdc();
1935 return;
1936 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 if (start_motor(floppy_ready))
1938 return;
1939 if (fdc_dtr())
1940 return;
1941
Joe Perches87f530d2010-03-10 15:20:54 -08001942 debug_dcl(DP->flags, "calling disk change from floppy_ready\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 if (!(raw_cmd->flags & FD_RAW_NO_MOTOR) &&
1944 disk_change(current_drive) && !DP->select_delay)
Joe Perchesbb57f0c62010-03-10 15:20:50 -08001945 twaddle(); /* this clears the dcl on certain
1946 * drive/controller combinations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
1948#ifdef fd_chose_dma_mode
1949 if ((raw_cmd->flags & FD_RAW_READ) || (raw_cmd->flags & FD_RAW_WRITE)) {
1950 unsigned long flags = claim_dma_lock();
1951 fd_chose_dma_mode(raw_cmd->kernel_data, raw_cmd->length);
1952 release_dma_lock(flags);
1953 }
1954#endif
1955
1956 if (raw_cmd->flags & (FD_RAW_NEED_SEEK | FD_RAW_NEED_DISK)) {
1957 perpendicular_mode();
1958 fdc_specify(); /* must be done here because of hut, hlt ... */
1959 seek_floppy();
1960 } else {
1961 if ((raw_cmd->flags & FD_RAW_READ) ||
1962 (raw_cmd->flags & FD_RAW_WRITE))
1963 fdc_specify();
1964 setup_rw_floppy();
1965 }
1966}
1967
1968static void floppy_start(void)
1969{
Joe Perches73507e62010-03-10 15:21:03 -08001970 reschedule_timeout(current_reqD, "floppy start");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
1972 scandrives();
Joe Perches87f530d2010-03-10 15:20:54 -08001973 debug_dcl(DP->flags, "setting NEWCHANGE in floppy_start\n");
Joe Perchese0298532010-03-10 15:20:55 -08001974 set_bit(FD_DISK_NEWCHANGE_BIT, &DRS->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 floppy_ready();
1976}
1977
1978/*
1979 * ========================================================================
1980 * here ends the bottom half. Exported routines are:
1981 * floppy_start, floppy_off, floppy_ready, lock_fdc, unlock_fdc, set_fdc,
1982 * start_motor, reset_fdc, reset_fdc_info, interpret_errors.
1983 * Initialization also uses output_byte, result, set_dor, floppy_interrupt
1984 * and set_dor.
1985 * ========================================================================
1986 */
1987/*
1988 * General purpose continuations.
1989 * ==============================
1990 */
1991
1992static void do_wakeup(void)
1993{
Joe Perches73507e62010-03-10 15:21:03 -08001994 reschedule_timeout(MAXTIMEOUT, "do wakeup");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 cont = NULL;
1996 command_status += 2;
1997 wake_up(&command_done);
1998}
1999
2000static struct cont_t wakeup_cont = {
2001 .interrupt = empty,
2002 .redo = do_wakeup,
2003 .error = empty,
Jesper Juhl06f748c2007-10-16 23:30:57 -07002004 .done = (done_f)empty
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005};
2006
2007static struct cont_t intr_cont = {
2008 .interrupt = empty,
2009 .redo = process_fd_request,
2010 .error = empty,
Jesper Juhl06f748c2007-10-16 23:30:57 -07002011 .done = (done_f)empty
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012};
2013
Joe Perches74f63f42010-03-10 15:20:58 -08002014static int wait_til_done(void (*handler)(void), bool interruptible)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015{
2016 int ret;
2017
2018 schedule_bh(handler);
2019
2020 if (command_status < 2 && NO_SIGNAL) {
2021 DECLARE_WAITQUEUE(wait, current);
2022
2023 add_wait_queue(&command_done, &wait);
2024 for (;;) {
2025 set_current_state(interruptible ?
2026 TASK_INTERRUPTIBLE :
2027 TASK_UNINTERRUPTIBLE);
2028
2029 if (command_status >= 2 || !NO_SIGNAL)
2030 break;
2031
2032 is_alive("wait_til_done");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 schedule();
2034 }
2035
2036 set_current_state(TASK_RUNNING);
2037 remove_wait_queue(&command_done, &wait);
2038 }
2039
2040 if (command_status < 2) {
2041 cancel_activity();
2042 cont = &intr_cont;
2043 reset_fdc();
2044 return -EINTR;
2045 }
2046
2047 if (FDCS->reset)
2048 command_status = FD_COMMAND_ERROR;
2049 if (command_status == FD_COMMAND_OKAY)
2050 ret = 0;
2051 else
2052 ret = -EIO;
2053 command_status = FD_COMMAND_NONE;
2054 return ret;
2055}
2056
2057static void generic_done(int result)
2058{
2059 command_status = result;
2060 cont = &wakeup_cont;
2061}
2062
2063static void generic_success(void)
2064{
2065 cont->done(1);
2066}
2067
2068static void generic_failure(void)
2069{
2070 cont->done(0);
2071}
2072
2073static void success_and_wakeup(void)
2074{
2075 generic_success();
2076 cont->redo();
2077}
2078
2079/*
2080 * formatting and rw support.
2081 * ==========================
2082 */
2083
2084static int next_valid_format(void)
2085{
2086 int probed_format;
2087
2088 probed_format = DRS->probed_format;
2089 while (1) {
2090 if (probed_format >= 8 || !DP->autodetect[probed_format]) {
2091 DRS->probed_format = 0;
2092 return 1;
2093 }
2094 if (floppy_type[DP->autodetect[probed_format]].sect) {
2095 DRS->probed_format = probed_format;
2096 return 0;
2097 }
2098 probed_format++;
2099 }
2100}
2101
2102static void bad_flp_intr(void)
2103{
2104 int err_count;
2105
2106 if (probing) {
2107 DRS->probed_format++;
2108 if (!next_valid_format())
2109 return;
2110 }
2111 err_count = ++(*errors);
2112 INFBOUND(DRWE->badness, err_count);
2113 if (err_count > DP->max_errors.abort)
2114 cont->done(0);
2115 if (err_count > DP->max_errors.reset)
2116 FDCS->reset = 1;
2117 else if (err_count > DP->max_errors.recal)
2118 DRS->track = NEED_2_RECAL;
2119}
2120
2121static void set_floppy(int drive)
2122{
2123 int type = ITYPE(UDRS->fd_device);
Jesper Juhl06f748c2007-10-16 23:30:57 -07002124
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 if (type)
2126 _floppy = floppy_type + type;
2127 else
2128 _floppy = current_type[drive];
2129}
2130
2131/*
2132 * formatting support.
2133 * ===================
2134 */
2135static void format_interrupt(void)
2136{
2137 switch (interpret_errors()) {
2138 case 1:
2139 cont->error();
2140 case 2:
2141 break;
2142 case 0:
2143 cont->done(1);
2144 }
2145 cont->redo();
2146}
2147
2148#define CODE2SIZE (ssize = ((1 << SIZECODE) + 3) >> 2)
Joe Perches48c8cee2010-03-10 15:20:45 -08002149#define FM_MODE(x, y) ((y) & ~(((x)->rate & 0x80) >> 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150#define CT(x) ((x) | 0xc0)
Joe Perches48c8cee2010-03-10 15:20:45 -08002151
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152static void setup_format_params(int track)
2153{
Jesper Juhl06f748c2007-10-16 23:30:57 -07002154 int n;
2155 int il;
2156 int count;
2157 int head_shift;
2158 int track_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 struct fparm {
2160 unsigned char track, head, sect, size;
2161 } *here = (struct fparm *)floppy_track_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
2163 raw_cmd = &default_raw_cmd;
2164 raw_cmd->track = track;
2165
Joe Perches48c8cee2010-03-10 15:20:45 -08002166 raw_cmd->flags = (FD_RAW_WRITE | FD_RAW_INTR | FD_RAW_SPIN |
2167 FD_RAW_NEED_DISK | FD_RAW_NEED_SEEK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 raw_cmd->rate = _floppy->rate & 0x43;
2169 raw_cmd->cmd_count = NR_F;
2170 COMMAND = FM_MODE(_floppy, FD_FORMAT);
2171 DR_SELECT = UNIT(current_drive) + PH_HEAD(_floppy, format_req.head);
2172 F_SIZECODE = FD_SIZECODE(_floppy);
2173 F_SECT_PER_TRACK = _floppy->sect << 2 >> F_SIZECODE;
2174 F_GAP = _floppy->fmt_gap;
2175 F_FILL = FD_FILL_BYTE;
2176
2177 raw_cmd->kernel_data = floppy_track_buffer;
2178 raw_cmd->length = 4 * F_SECT_PER_TRACK;
2179
2180 /* allow for about 30ms for data transport per track */
2181 head_shift = (F_SECT_PER_TRACK + 5) / 6;
2182
2183 /* a ``cylinder'' is two tracks plus a little stepping time */
2184 track_shift = 2 * head_shift + 3;
2185
2186 /* position of logical sector 1 on this track */
2187 n = (track_shift * format_req.track + head_shift * format_req.head)
2188 % F_SECT_PER_TRACK;
2189
2190 /* determine interleave */
2191 il = 1;
2192 if (_floppy->fmt_gap < 0x22)
2193 il++;
2194
2195 /* initialize field */
2196 for (count = 0; count < F_SECT_PER_TRACK; ++count) {
2197 here[count].track = format_req.track;
2198 here[count].head = format_req.head;
2199 here[count].sect = 0;
2200 here[count].size = F_SIZECODE;
2201 }
2202 /* place logical sectors */
2203 for (count = 1; count <= F_SECT_PER_TRACK; ++count) {
2204 here[n].sect = count;
2205 n = (n + il) % F_SECT_PER_TRACK;
2206 if (here[n].sect) { /* sector busy, find next free sector */
2207 ++n;
2208 if (n >= F_SECT_PER_TRACK) {
2209 n -= F_SECT_PER_TRACK;
2210 while (here[n].sect)
2211 ++n;
2212 }
2213 }
2214 }
Keith Wansbrough9e491842008-09-22 14:57:17 -07002215 if (_floppy->stretch & FD_SECTBASEMASK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 for (count = 0; count < F_SECT_PER_TRACK; count++)
Keith Wansbrough9e491842008-09-22 14:57:17 -07002217 here[count].sect += FD_SECTBASE(_floppy) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 }
2219}
2220
2221static void redo_format(void)
2222{
2223 buffer_track = -1;
2224 setup_format_params(format_req.track << STRETCH(_floppy));
2225 floppy_start();
2226 debugt("queue format request");
2227}
2228
2229static struct cont_t format_cont = {
2230 .interrupt = format_interrupt,
2231 .redo = redo_format,
2232 .error = bad_flp_intr,
2233 .done = generic_done
2234};
2235
2236static int do_format(int drive, struct format_descr *tmp_format_req)
2237{
2238 int ret;
2239
Joe Perches74f63f42010-03-10 15:20:58 -08002240 if (lock_fdc(drive, true))
Joe Perches52a0d612010-03-10 15:20:53 -08002241 return -EINTR;
2242
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 set_floppy(drive);
2244 if (!_floppy ||
2245 _floppy->track > DP->tracks ||
2246 tmp_format_req->track >= _floppy->track ||
2247 tmp_format_req->head >= _floppy->head ||
2248 (_floppy->sect << 2) % (1 << FD_SIZECODE(_floppy)) ||
2249 !_floppy->fmt_gap) {
2250 process_fd_request();
2251 return -EINVAL;
2252 }
2253 format_req = *tmp_format_req;
2254 format_errors = 0;
2255 cont = &format_cont;
2256 errors = &format_errors;
Joe Perches74f63f42010-03-10 15:20:58 -08002257 ret = wait_til_done(redo_format, true);
Joe Perches55eee802010-03-10 15:20:57 -08002258 if (ret == -EINTR)
2259 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 process_fd_request();
2261 return ret;
2262}
2263
2264/*
2265 * Buffer read/write and support
2266 * =============================
2267 */
2268
Kiyoshi Ueda1c5093b2008-01-28 10:36:21 +01002269static void floppy_end_request(struct request *req, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270{
2271 unsigned int nr_sectors = current_count_sectors;
Kiyoshi Ueda1c5093b2008-01-28 10:36:21 +01002272 unsigned int drive = (unsigned long)req->rq_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273
2274 /* current_count_sectors can be zero if transfer failed */
Kiyoshi Ueda1c5093b2008-01-28 10:36:21 +01002275 if (error)
Tejun Heo83096eb2009-05-07 22:24:39 +09002276 nr_sectors = blk_rq_cur_sectors(req);
Kiyoshi Ueda1c5093b2008-01-28 10:36:21 +01002277 if (__blk_end_request(req, error, nr_sectors << 9))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279
2280 /* We're done with the request */
Kiyoshi Ueda1c5093b2008-01-28 10:36:21 +01002281 floppy_off(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 current_req = NULL;
2283}
2284
2285/* new request_done. Can handle physical sectors which are smaller than a
2286 * logical buffer */
2287static void request_done(int uptodate)
2288{
2289 struct request_queue *q = floppy_queue;
2290 struct request *req = current_req;
2291 unsigned long flags;
2292 int block;
Joe Perches73507e62010-03-10 15:21:03 -08002293 char msg[sizeof("request done ") + sizeof(int) * 3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294
2295 probing = 0;
Joe Perches73507e62010-03-10 15:21:03 -08002296 snprintf(msg, sizeof(msg), "request done %d", uptodate);
2297 reschedule_timeout(MAXTIMEOUT, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298
2299 if (!req) {
Joe Perchesb46df352010-03-10 15:20:46 -08002300 pr_info("floppy.c: no request in request_done\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 return;
2302 }
2303
2304 if (uptodate) {
2305 /* maintain values for invalidation on geometry
2306 * change */
Tejun Heo83096eb2009-05-07 22:24:39 +09002307 block = current_count_sectors + blk_rq_pos(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 INFBOUND(DRS->maxblock, block);
2309 if (block > _floppy->sect)
2310 DRS->maxtrack = 1;
2311
2312 /* unlock chained buffers */
2313 spin_lock_irqsave(q->queue_lock, flags);
Kiyoshi Ueda1c5093b2008-01-28 10:36:21 +01002314 floppy_end_request(req, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 spin_unlock_irqrestore(q->queue_lock, flags);
2316 } else {
2317 if (rq_data_dir(req) == WRITE) {
2318 /* record write error information */
2319 DRWE->write_errors++;
2320 if (DRWE->write_errors == 1) {
Tejun Heo83096eb2009-05-07 22:24:39 +09002321 DRWE->first_error_sector = blk_rq_pos(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 DRWE->first_error_generation = DRS->generation;
2323 }
Tejun Heo83096eb2009-05-07 22:24:39 +09002324 DRWE->last_error_sector = blk_rq_pos(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 DRWE->last_error_generation = DRS->generation;
2326 }
2327 spin_lock_irqsave(q->queue_lock, flags);
Kiyoshi Ueda1c5093b2008-01-28 10:36:21 +01002328 floppy_end_request(req, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 spin_unlock_irqrestore(q->queue_lock, flags);
2330 }
2331}
2332
2333/* Interrupt handler evaluating the result of the r/w operation */
2334static void rw_interrupt(void)
2335{
Jesper Juhl06f748c2007-10-16 23:30:57 -07002336 int eoc;
2337 int ssize;
2338 int heads;
2339 int nr_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
2341 if (R_HEAD >= 2) {
2342 /* some Toshiba floppy controllers occasionnally seem to
2343 * return bogus interrupts after read/write operations, which
2344 * can be recognized by a bad head number (>= 2) */
2345 return;
2346 }
2347
2348 if (!DRS->first_read_date)
2349 DRS->first_read_date = jiffies;
2350
2351 nr_sectors = 0;
2352 CODE2SIZE;
2353
2354 if (ST1 & ST1_EOC)
2355 eoc = 1;
2356 else
2357 eoc = 0;
2358
2359 if (COMMAND & 0x80)
2360 heads = 2;
2361 else
2362 heads = 1;
2363
2364 nr_sectors = (((R_TRACK - TRACK) * heads +
2365 R_HEAD - HEAD) * SECT_PER_TRACK +
2366 R_SECTOR - SECTOR + eoc) << SIZECODE >> 2;
2367
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 if (nr_sectors / ssize >
Julia Lawall061837b2008-09-22 14:57:16 -07002369 DIV_ROUND_UP(in_sector_offset + current_count_sectors, ssize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 DPRINT("long rw: %x instead of %lx\n",
2371 nr_sectors, current_count_sectors);
Joe Perchesb46df352010-03-10 15:20:46 -08002372 pr_info("rs=%d s=%d\n", R_SECTOR, SECTOR);
2373 pr_info("rh=%d h=%d\n", R_HEAD, HEAD);
2374 pr_info("rt=%d t=%d\n", R_TRACK, TRACK);
2375 pr_info("heads=%d eoc=%d\n", heads, eoc);
2376 pr_info("spt=%d st=%d ss=%d\n",
2377 SECT_PER_TRACK, fsector_t, ssize);
2378 pr_info("in_sector_offset=%d\n", in_sector_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380
2381 nr_sectors -= in_sector_offset;
2382 INFBOUND(nr_sectors, 0);
2383 SUPBOUND(current_count_sectors, nr_sectors);
2384
2385 switch (interpret_errors()) {
2386 case 2:
2387 cont->redo();
2388 return;
2389 case 1:
2390 if (!current_count_sectors) {
2391 cont->error();
2392 cont->redo();
2393 return;
2394 }
2395 break;
2396 case 0:
2397 if (!current_count_sectors) {
2398 cont->redo();
2399 return;
2400 }
2401 current_type[current_drive] = _floppy;
2402 floppy_sizes[TOMINOR(current_drive)] = _floppy->size;
2403 break;
2404 }
2405
2406 if (probing) {
2407 if (DP->flags & FTD_MSG)
2408 DPRINT("Auto-detected floppy type %s in fd%d\n",
2409 _floppy->name, current_drive);
2410 current_type[current_drive] = _floppy;
2411 floppy_sizes[TOMINOR(current_drive)] = _floppy->size;
2412 probing = 0;
2413 }
2414
2415 if (CT(COMMAND) != FD_READ ||
2416 raw_cmd->kernel_data == current_req->buffer) {
2417 /* transfer directly from buffer */
2418 cont->done(1);
2419 } else if (CT(COMMAND) == FD_READ) {
2420 buffer_track = raw_cmd->track;
2421 buffer_drive = current_drive;
2422 INFBOUND(buffer_max, nr_sectors + fsector_t);
2423 }
2424 cont->redo();
2425}
2426
2427/* Compute maximal contiguous buffer size. */
2428static int buffer_chain_size(void)
2429{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 struct bio_vec *bv;
NeilBrown5705f702007-09-25 12:35:59 +02002431 int size;
2432 struct req_iterator iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 char *base;
2434
2435 base = bio_data(current_req->bio);
2436 size = 0;
2437
NeilBrown5705f702007-09-25 12:35:59 +02002438 rq_for_each_segment(bv, current_req, iter) {
2439 if (page_address(bv->bv_page) + bv->bv_offset != base + size)
2440 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441
NeilBrown5705f702007-09-25 12:35:59 +02002442 size += bv->bv_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 }
2444
2445 return size >> 9;
2446}
2447
2448/* Compute the maximal transfer size */
2449static int transfer_size(int ssize, int max_sector, int max_size)
2450{
2451 SUPBOUND(max_sector, fsector_t + max_size);
2452
2453 /* alignment */
2454 max_sector -= (max_sector % _floppy->sect) % ssize;
2455
2456 /* transfer size, beginning not aligned */
2457 current_count_sectors = max_sector - fsector_t;
2458
2459 return max_sector;
2460}
2461
2462/*
2463 * Move data from/to the track buffer to/from the buffer cache.
2464 */
2465static void copy_buffer(int ssize, int max_sector, int max_sector_2)
2466{
2467 int remaining; /* number of transferred 512-byte sectors */
2468 struct bio_vec *bv;
Jesper Juhl06f748c2007-10-16 23:30:57 -07002469 char *buffer;
2470 char *dma_buffer;
NeilBrown5705f702007-09-25 12:35:59 +02002471 int size;
2472 struct req_iterator iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473
2474 max_sector = transfer_size(ssize,
2475 min(max_sector, max_sector_2),
Tejun Heo83096eb2009-05-07 22:24:39 +09002476 blk_rq_sectors(current_req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477
2478 if (current_count_sectors <= 0 && CT(COMMAND) == FD_WRITE &&
Tejun Heo83096eb2009-05-07 22:24:39 +09002479 buffer_max > fsector_t + blk_rq_sectors(current_req))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 current_count_sectors = min_t(int, buffer_max - fsector_t,
Tejun Heo83096eb2009-05-07 22:24:39 +09002481 blk_rq_sectors(current_req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482
2483 remaining = current_count_sectors << 9;
Tejun Heo1011c1b2009-05-07 22:24:45 +09002484 if (remaining > blk_rq_bytes(current_req) && CT(COMMAND) == FD_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 DPRINT("in copy buffer\n");
Joe Perchesb46df352010-03-10 15:20:46 -08002486 pr_info("current_count_sectors=%ld\n", current_count_sectors);
2487 pr_info("remaining=%d\n", remaining >> 9);
2488 pr_info("current_req->nr_sectors=%u\n",
2489 blk_rq_sectors(current_req));
2490 pr_info("current_req->current_nr_sectors=%u\n",
2491 blk_rq_cur_sectors(current_req));
2492 pr_info("max_sector=%d\n", max_sector);
2493 pr_info("ssize=%d\n", ssize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495
2496 buffer_max = max(max_sector, buffer_max);
2497
2498 dma_buffer = floppy_track_buffer + ((fsector_t - buffer_min) << 9);
2499
Tejun Heo1011c1b2009-05-07 22:24:45 +09002500 size = blk_rq_cur_bytes(current_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501
NeilBrown5705f702007-09-25 12:35:59 +02002502 rq_for_each_segment(bv, current_req, iter) {
2503 if (!remaining)
2504 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505
NeilBrown5705f702007-09-25 12:35:59 +02002506 size = bv->bv_len;
2507 SUPBOUND(size, remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508
NeilBrown5705f702007-09-25 12:35:59 +02002509 buffer = page_address(bv->bv_page) + bv->bv_offset;
NeilBrown5705f702007-09-25 12:35:59 +02002510 if (dma_buffer + size >
2511 floppy_track_buffer + (max_buffer_sectors << 10) ||
2512 dma_buffer < floppy_track_buffer) {
2513 DPRINT("buffer overrun in copy buffer %d\n",
Joe Perchesb46df352010-03-10 15:20:46 -08002514 (int)((floppy_track_buffer - dma_buffer) >> 9));
2515 pr_info("fsector_t=%d buffer_min=%d\n",
2516 fsector_t, buffer_min);
2517 pr_info("current_count_sectors=%ld\n",
2518 current_count_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 if (CT(COMMAND) == FD_READ)
Joe Perchesb46df352010-03-10 15:20:46 -08002520 pr_info("read\n");
NeilBrown5705f702007-09-25 12:35:59 +02002521 if (CT(COMMAND) == FD_WRITE)
Joe Perchesb46df352010-03-10 15:20:46 -08002522 pr_info("write\n");
NeilBrown5705f702007-09-25 12:35:59 +02002523 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 }
NeilBrown5705f702007-09-25 12:35:59 +02002525 if (((unsigned long)buffer) % 512)
2526 DPRINT("%p buffer not aligned\n", buffer);
Joe Perches1a23d132010-03-10 15:21:04 -08002527
NeilBrown5705f702007-09-25 12:35:59 +02002528 if (CT(COMMAND) == FD_READ)
2529 memcpy(buffer, dma_buffer, size);
2530 else
2531 memcpy(dma_buffer, buffer, size);
2532
2533 remaining -= size;
2534 dma_buffer += size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 if (remaining) {
2537 if (remaining > 0)
2538 max_sector -= remaining >> 9;
2539 DPRINT("weirdness: remaining %d\n", remaining >> 9);
2540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541}
2542
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543/* work around a bug in pseudo DMA
2544 * (on some FDCs) pseudo DMA does not stop when the CPU stops
2545 * sending data. Hence we need a different way to signal the
2546 * transfer length: We use SECT_PER_TRACK. Unfortunately, this
2547 * does not work with MT, hence we can only transfer one head at
2548 * a time
2549 */
2550static void virtualdmabug_workaround(void)
2551{
Jesper Juhl06f748c2007-10-16 23:30:57 -07002552 int hard_sectors;
2553 int end_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554
2555 if (CT(COMMAND) == FD_WRITE) {
2556 COMMAND &= ~0x80; /* switch off multiple track mode */
2557
2558 hard_sectors = raw_cmd->length >> (7 + SIZECODE);
2559 end_sector = SECTOR + hard_sectors - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 if (end_sector > SECT_PER_TRACK) {
Joe Perchesb46df352010-03-10 15:20:46 -08002561 pr_info("too many sectors %d > %d\n",
2562 end_sector, SECT_PER_TRACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 return;
2564 }
Joe Perches48c8cee2010-03-10 15:20:45 -08002565 SECT_PER_TRACK = end_sector;
2566 /* make sure SECT_PER_TRACK
2567 * points to end of transfer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 }
2569}
2570
2571/*
2572 * Formulate a read/write request.
2573 * this routine decides where to load the data (directly to buffer, or to
2574 * tmp floppy area), how much data to load (the size of the buffer, the whole
2575 * track, or a single sector)
2576 * All floppy_track_buffer handling goes in here. If we ever add track buffer
2577 * allocation on the fly, it should be done here. No other part should need
2578 * modification.
2579 */
2580
2581static int make_raw_rw_request(void)
2582{
2583 int aligned_sector_t;
Jesper Juhl06f748c2007-10-16 23:30:57 -07002584 int max_sector;
2585 int max_size;
2586 int tracksize;
2587 int ssize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588
2589 if (max_buffer_sectors == 0) {
Joe Perchesb46df352010-03-10 15:20:46 -08002590 pr_info("VFS: Block I/O scheduled on unopened device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 return 0;
2592 }
2593
2594 set_fdc((long)current_req->rq_disk->private_data);
2595
2596 raw_cmd = &default_raw_cmd;
2597 raw_cmd->flags = FD_RAW_SPIN | FD_RAW_NEED_DISK | FD_RAW_NEED_DISK |
2598 FD_RAW_NEED_SEEK;
2599 raw_cmd->cmd_count = NR_RW;
2600 if (rq_data_dir(current_req) == READ) {
2601 raw_cmd->flags |= FD_RAW_READ;
2602 COMMAND = FM_MODE(_floppy, FD_READ);
2603 } else if (rq_data_dir(current_req) == WRITE) {
2604 raw_cmd->flags |= FD_RAW_WRITE;
2605 COMMAND = FM_MODE(_floppy, FD_WRITE);
2606 } else {
2607 DPRINT("make_raw_rw_request: unknown command\n");
2608 return 0;
2609 }
2610
2611 max_sector = _floppy->sect * _floppy->head;
2612
Tejun Heo83096eb2009-05-07 22:24:39 +09002613 TRACK = (int)blk_rq_pos(current_req) / max_sector;
2614 fsector_t = (int)blk_rq_pos(current_req) % max_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 if (_floppy->track && TRACK >= _floppy->track) {
Tejun Heo83096eb2009-05-07 22:24:39 +09002616 if (blk_rq_cur_sectors(current_req) & 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 current_count_sectors = 1;
2618 return 1;
2619 } else
2620 return 0;
2621 }
2622 HEAD = fsector_t / _floppy->sect;
2623
Keith Wansbrough9e491842008-09-22 14:57:17 -07002624 if (((_floppy->stretch & (FD_SWAPSIDES | FD_SECTBASEMASK)) ||
Joe Perchese0298532010-03-10 15:20:55 -08002625 test_bit(FD_NEED_TWADDLE_BIT, &DRS->flags)) &&
2626 fsector_t < _floppy->sect)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 max_sector = _floppy->sect;
2628
2629 /* 2M disks have phantom sectors on the first track */
2630 if ((_floppy->rate & FD_2M) && (!TRACK) && (!HEAD)) {
2631 max_sector = 2 * _floppy->sect / 3;
2632 if (fsector_t >= max_sector) {
2633 current_count_sectors =
2634 min_t(int, _floppy->sect - fsector_t,
Tejun Heo83096eb2009-05-07 22:24:39 +09002635 blk_rq_sectors(current_req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 return 1;
2637 }
2638 SIZECODE = 2;
2639 } else
2640 SIZECODE = FD_SIZECODE(_floppy);
2641 raw_cmd->rate = _floppy->rate & 0x43;
2642 if ((_floppy->rate & FD_2M) && (TRACK || HEAD) && raw_cmd->rate == 2)
2643 raw_cmd->rate = 1;
2644
2645 if (SIZECODE)
2646 SIZECODE2 = 0xff;
2647 else
2648 SIZECODE2 = 0x80;
2649 raw_cmd->track = TRACK << STRETCH(_floppy);
2650 DR_SELECT = UNIT(current_drive) + PH_HEAD(_floppy, HEAD);
2651 GAP = _floppy->gap;
2652 CODE2SIZE;
2653 SECT_PER_TRACK = _floppy->sect << 2 >> SIZECODE;
2654 SECTOR = ((fsector_t % _floppy->sect) << 2 >> SIZECODE) +
Keith Wansbrough9e491842008-09-22 14:57:17 -07002655 FD_SECTBASE(_floppy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656
2657 /* tracksize describes the size which can be filled up with sectors
2658 * of size ssize.
2659 */
2660 tracksize = _floppy->sect - _floppy->sect % ssize;
2661 if (tracksize < _floppy->sect) {
2662 SECT_PER_TRACK++;
2663 if (tracksize <= fsector_t % _floppy->sect)
2664 SECTOR--;
2665
2666 /* if we are beyond tracksize, fill up using smaller sectors */
2667 while (tracksize <= fsector_t % _floppy->sect) {
2668 while (tracksize + ssize > _floppy->sect) {
2669 SIZECODE--;
2670 ssize >>= 1;
2671 }
2672 SECTOR++;
2673 SECT_PER_TRACK++;
2674 tracksize += ssize;
2675 }
2676 max_sector = HEAD * _floppy->sect + tracksize;
2677 } else if (!TRACK && !HEAD && !(_floppy->rate & FD_2M) && probing) {
2678 max_sector = _floppy->sect;
2679 } else if (!HEAD && CT(COMMAND) == FD_WRITE) {
2680 /* for virtual DMA bug workaround */
2681 max_sector = _floppy->sect;
2682 }
2683
2684 in_sector_offset = (fsector_t % _floppy->sect) % ssize;
2685 aligned_sector_t = fsector_t - in_sector_offset;
Tejun Heo83096eb2009-05-07 22:24:39 +09002686 max_size = blk_rq_sectors(current_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 if ((raw_cmd->track == buffer_track) &&
2688 (current_drive == buffer_drive) &&
2689 (fsector_t >= buffer_min) && (fsector_t < buffer_max)) {
2690 /* data already in track buffer */
2691 if (CT(COMMAND) == FD_READ) {
2692 copy_buffer(1, max_sector, buffer_max);
2693 return 1;
2694 }
Tejun Heo83096eb2009-05-07 22:24:39 +09002695 } else if (in_sector_offset || blk_rq_sectors(current_req) < ssize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 if (CT(COMMAND) == FD_WRITE) {
Joe Perchesd7b2b2e2010-03-10 15:20:48 -08002697 unsigned int sectors;
2698
2699 sectors = fsector_t + blk_rq_sectors(current_req);
2700 if (sectors > ssize && sectors < ssize + ssize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 max_size = ssize + ssize;
2702 else
2703 max_size = ssize;
2704 }
2705 raw_cmd->flags &= ~FD_RAW_WRITE;
2706 raw_cmd->flags |= FD_RAW_READ;
2707 COMMAND = FM_MODE(_floppy, FD_READ);
2708 } else if ((unsigned long)current_req->buffer < MAX_DMA_ADDRESS) {
2709 unsigned long dma_limit;
2710 int direct, indirect;
2711
2712 indirect =
2713 transfer_size(ssize, max_sector,
2714 max_buffer_sectors * 2) - fsector_t;
2715
2716 /*
2717 * Do NOT use minimum() here---MAX_DMA_ADDRESS is 64 bits wide
2718 * on a 64 bit machine!
2719 */
2720 max_size = buffer_chain_size();
Joe Perchesd7b2b2e2010-03-10 15:20:48 -08002721 dma_limit = (MAX_DMA_ADDRESS -
2722 ((unsigned long)current_req->buffer)) >> 9;
Joe Perchesa81ee542010-03-10 15:20:46 -08002723 if ((unsigned long)max_size > dma_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 max_size = dma_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 /* 64 kb boundaries */
2726 if (CROSS_64KB(current_req->buffer, max_size << 9))
2727 max_size = (K_64 -
2728 ((unsigned long)current_req->buffer) %
2729 K_64) >> 9;
2730 direct = transfer_size(ssize, max_sector, max_size) - fsector_t;
2731 /*
2732 * We try to read tracks, but if we get too many errors, we
2733 * go back to reading just one sector at a time.
2734 *
2735 * This means we should be able to read a sector even if there
2736 * are other bad sectors on this track.
2737 */
2738 if (!direct ||
2739 (indirect * 2 > direct * 3 &&
Joe Perchesd7b2b2e2010-03-10 15:20:48 -08002740 *errors < DP->max_errors.read_track &&
2741 ((!probing ||
2742 (DP->read_track & (1 << DRS->probed_format)))))) {
Tejun Heo83096eb2009-05-07 22:24:39 +09002743 max_size = blk_rq_sectors(current_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 } else {
2745 raw_cmd->kernel_data = current_req->buffer;
2746 raw_cmd->length = current_count_sectors << 9;
2747 if (raw_cmd->length == 0) {
Joe Perchesd7b2b2e2010-03-10 15:20:48 -08002748 DPRINT("zero dma transfer attempted from make_raw_request\n");
2749 DPRINT("indirect=%d direct=%d fsector_t=%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 indirect, direct, fsector_t);
2751 return 0;
2752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 virtualdmabug_workaround();
2754 return 2;
2755 }
2756 }
2757
2758 if (CT(COMMAND) == FD_READ)
2759 max_size = max_sector; /* unbounded */
2760
2761 /* claim buffer track if needed */
2762 if (buffer_track != raw_cmd->track || /* bad track */
2763 buffer_drive != current_drive || /* bad drive */
2764 fsector_t > buffer_max ||
2765 fsector_t < buffer_min ||
2766 ((CT(COMMAND) == FD_READ ||
Tejun Heo83096eb2009-05-07 22:24:39 +09002767 (!in_sector_offset && blk_rq_sectors(current_req) >= ssize)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 max_sector > 2 * max_buffer_sectors + buffer_min &&
Joe Perchesbb57f0c62010-03-10 15:20:50 -08002769 max_size + fsector_t > 2 * max_buffer_sectors + buffer_min)) {
2770 /* not enough space */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 buffer_track = -1;
2772 buffer_drive = current_drive;
2773 buffer_max = buffer_min = aligned_sector_t;
2774 }
2775 raw_cmd->kernel_data = floppy_track_buffer +
Joe Perchesbb57f0c62010-03-10 15:20:50 -08002776 ((aligned_sector_t - buffer_min) << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777
2778 if (CT(COMMAND) == FD_WRITE) {
2779 /* copy write buffer to track buffer.
2780 * if we get here, we know that the write
2781 * is either aligned or the data already in the buffer
2782 * (buffer will be overwritten) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 if (in_sector_offset && buffer_track == -1)
2784 DPRINT("internal error offset !=0 on write\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 buffer_track = raw_cmd->track;
2786 buffer_drive = current_drive;
2787 copy_buffer(ssize, max_sector,
2788 2 * max_buffer_sectors + buffer_min);
2789 } else
2790 transfer_size(ssize, max_sector,
2791 2 * max_buffer_sectors + buffer_min -
2792 aligned_sector_t);
2793
2794 /* round up current_count_sectors to get dma xfer size */
2795 raw_cmd->length = in_sector_offset + current_count_sectors;
2796 raw_cmd->length = ((raw_cmd->length - 1) | (ssize - 1)) + 1;
2797 raw_cmd->length <<= 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 if ((raw_cmd->length < current_count_sectors << 9) ||
2799 (raw_cmd->kernel_data != current_req->buffer &&
2800 CT(COMMAND) == FD_WRITE &&
2801 (aligned_sector_t + (raw_cmd->length >> 9) > buffer_max ||
2802 aligned_sector_t < buffer_min)) ||
2803 raw_cmd->length % (128 << SIZECODE) ||
2804 raw_cmd->length <= 0 || current_count_sectors <= 0) {
2805 DPRINT("fractionary current count b=%lx s=%lx\n",
2806 raw_cmd->length, current_count_sectors);
2807 if (raw_cmd->kernel_data != current_req->buffer)
Joe Perchesb46df352010-03-10 15:20:46 -08002808 pr_info("addr=%d, length=%ld\n",
2809 (int)((raw_cmd->kernel_data -
2810 floppy_track_buffer) >> 9),
2811 current_count_sectors);
2812 pr_info("st=%d ast=%d mse=%d msi=%d\n",
2813 fsector_t, aligned_sector_t, max_sector, max_size);
2814 pr_info("ssize=%x SIZECODE=%d\n", ssize, SIZECODE);
2815 pr_info("command=%x SECTOR=%d HEAD=%d, TRACK=%d\n",
2816 COMMAND, SECTOR, HEAD, TRACK);
2817 pr_info("buffer drive=%d\n", buffer_drive);
2818 pr_info("buffer track=%d\n", buffer_track);
2819 pr_info("buffer_min=%d\n", buffer_min);
2820 pr_info("buffer_max=%d\n", buffer_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 return 0;
2822 }
2823
2824 if (raw_cmd->kernel_data != current_req->buffer) {
2825 if (raw_cmd->kernel_data < floppy_track_buffer ||
2826 current_count_sectors < 0 ||
2827 raw_cmd->length < 0 ||
2828 raw_cmd->kernel_data + raw_cmd->length >
2829 floppy_track_buffer + (max_buffer_sectors << 10)) {
2830 DPRINT("buffer overrun in schedule dma\n");
Joe Perchesb46df352010-03-10 15:20:46 -08002831 pr_info("fsector_t=%d buffer_min=%d current_count=%ld\n",
2832 fsector_t, buffer_min, raw_cmd->length >> 9);
2833 pr_info("current_count_sectors=%ld\n",
2834 current_count_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 if (CT(COMMAND) == FD_READ)
Joe Perchesb46df352010-03-10 15:20:46 -08002836 pr_info("read\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 if (CT(COMMAND) == FD_WRITE)
Joe Perchesb46df352010-03-10 15:20:46 -08002838 pr_info("write\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 return 0;
2840 }
Tejun Heo1011c1b2009-05-07 22:24:45 +09002841 } else if (raw_cmd->length > blk_rq_bytes(current_req) ||
Tejun Heo83096eb2009-05-07 22:24:39 +09002842 current_count_sectors > blk_rq_sectors(current_req)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 DPRINT("buffer overrun in direct transfer\n");
2844 return 0;
2845 } else if (raw_cmd->length < current_count_sectors << 9) {
2846 DPRINT("more sectors than bytes\n");
Joe Perchesb46df352010-03-10 15:20:46 -08002847 pr_info("bytes=%ld\n", raw_cmd->length >> 9);
2848 pr_info("sectors=%ld\n", current_count_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 }
2850 if (raw_cmd->length == 0) {
2851 DPRINT("zero dma transfer attempted from make_raw_request\n");
2852 return 0;
2853 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854
2855 virtualdmabug_workaround();
2856 return 2;
2857}
2858
2859static void redo_fd_request(void)
2860{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 int drive;
2862 int tmp;
2863
2864 lastredo = jiffies;
2865 if (current_drive < N_DRIVE)
2866 floppy_off(current_drive);
2867
Joe Perches0da31322010-03-10 15:21:03 -08002868do_request:
2869 if (!current_req) {
2870 struct request *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871
Joe Perches0da31322010-03-10 15:21:03 -08002872 spin_lock_irq(floppy_queue->queue_lock);
2873 req = blk_fetch_request(floppy_queue);
2874 spin_unlock_irq(floppy_queue->queue_lock);
2875 if (!req) {
2876 do_floppy = NULL;
2877 unlock_fdc();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 }
Joe Perches0da31322010-03-10 15:21:03 -08002880 current_req = req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 }
Joe Perches0da31322010-03-10 15:21:03 -08002882 drive = (long)current_req->rq_disk->private_data;
2883 set_fdc(drive);
Joe Perches73507e62010-03-10 15:21:03 -08002884 reschedule_timeout(current_reqD, "redo fd request");
Joe Perches0da31322010-03-10 15:21:03 -08002885
2886 set_floppy(drive);
2887 raw_cmd = &default_raw_cmd;
2888 raw_cmd->flags = 0;
2889 if (start_motor(redo_fd_request))
2890 return;
2891
2892 disk_change(current_drive);
2893 if (test_bit(current_drive, &fake_change) ||
2894 test_bit(FD_DISK_CHANGED_BIT, &DRS->flags)) {
2895 DPRINT("disk absent or changed during operation\n");
2896 request_done(0);
2897 goto do_request;
2898 }
2899 if (!_floppy) { /* Autodetection */
2900 if (!probing) {
2901 DRS->probed_format = 0;
2902 if (next_valid_format()) {
2903 DPRINT("no autodetectable formats\n");
2904 _floppy = NULL;
2905 request_done(0);
2906 goto do_request;
2907 }
2908 }
2909 probing = 1;
2910 _floppy = floppy_type + DP->autodetect[DRS->probed_format];
2911 } else
2912 probing = 0;
2913 errors = &(current_req->errors);
2914 tmp = make_raw_rw_request();
2915 if (tmp < 2) {
2916 request_done(tmp);
2917 goto do_request;
2918 }
2919
2920 if (test_bit(FD_NEED_TWADDLE_BIT, &DRS->flags))
2921 twaddle();
2922 schedule_bh(floppy_start);
2923 debugt("queue fd request");
2924 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925}
2926
2927static struct cont_t rw_cont = {
2928 .interrupt = rw_interrupt,
2929 .redo = redo_fd_request,
2930 .error = bad_flp_intr,
2931 .done = request_done
2932};
2933
2934static void process_fd_request(void)
2935{
2936 cont = &rw_cont;
2937 schedule_bh(redo_fd_request);
2938}
2939
Joe Perchesd7b2b2e2010-03-10 15:20:48 -08002940static void do_fd_request(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941{
2942 if (max_buffer_sectors == 0) {
Joe Perchesb46df352010-03-10 15:20:46 -08002943 pr_info("VFS: do_fd_request called on non-open device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 return;
2945 }
2946
2947 if (usage_count == 0) {
Joe Perchesb46df352010-03-10 15:20:46 -08002948 pr_info("warning: usage count=0, current_req=%p exiting\n",
2949 current_req);
2950 pr_info("sect=%ld type=%x flags=%x\n",
2951 (long)blk_rq_pos(current_req), current_req->cmd_type,
2952 current_req->cmd_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 return;
2954 }
2955 if (test_bit(0, &fdc_busy)) {
2956 /* fdc busy, this new request will be treated when the
2957 current one is done */
2958 is_alive("do fd request, old request running");
2959 return;
2960 }
Joe Perches74f63f42010-03-10 15:20:58 -08002961 lock_fdc(MAXTIMEOUT, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 process_fd_request();
2963 is_alive("do fd request");
2964}
2965
2966static struct cont_t poll_cont = {
2967 .interrupt = success_and_wakeup,
2968 .redo = floppy_ready,
2969 .error = generic_failure,
2970 .done = generic_done
2971};
2972
Joe Perches74f63f42010-03-10 15:20:58 -08002973static int poll_drive(bool interruptible, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 /* no auto-sense, just clear dcl */
2976 raw_cmd = &default_raw_cmd;
2977 raw_cmd->flags = flag;
2978 raw_cmd->track = 0;
2979 raw_cmd->cmd_count = 0;
2980 cont = &poll_cont;
Joe Perches87f530d2010-03-10 15:20:54 -08002981 debug_dcl(DP->flags, "setting NEWCHANGE in poll_drive\n");
Joe Perchese0298532010-03-10 15:20:55 -08002982 set_bit(FD_DISK_NEWCHANGE_BIT, &DRS->flags);
Joe Perches55eee802010-03-10 15:20:57 -08002983
2984 return wait_til_done(floppy_ready, interruptible);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985}
2986
2987/*
2988 * User triggered reset
2989 * ====================
2990 */
2991
2992static void reset_intr(void)
2993{
Joe Perchesb46df352010-03-10 15:20:46 -08002994 pr_info("weird, reset interrupt called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995}
2996
2997static struct cont_t reset_cont = {
2998 .interrupt = reset_intr,
2999 .redo = success_and_wakeup,
3000 .error = generic_failure,
3001 .done = generic_done
3002};
3003
Joe Perches74f63f42010-03-10 15:20:58 -08003004static int user_reset_fdc(int drive, int arg, bool interruptible)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005{
3006 int ret;
3007
Joe Perches52a0d612010-03-10 15:20:53 -08003008 if (lock_fdc(drive, interruptible))
3009 return -EINTR;
3010
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 if (arg == FD_RESET_ALWAYS)
3012 FDCS->reset = 1;
3013 if (FDCS->reset) {
3014 cont = &reset_cont;
Joe Perches55eee802010-03-10 15:20:57 -08003015 ret = wait_til_done(reset_fdc, interruptible);
3016 if (ret == -EINTR)
3017 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 }
3019 process_fd_request();
Joe Perches52a0d612010-03-10 15:20:53 -08003020 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021}
3022
3023/*
3024 * Misc Ioctl's and support
3025 * ========================
3026 */
3027static inline int fd_copyout(void __user *param, const void *address,
3028 unsigned long size)
3029{
3030 return copy_to_user(param, address, size) ? -EFAULT : 0;
3031}
3032
Joe Perches48c8cee2010-03-10 15:20:45 -08003033static inline int fd_copyin(void __user *param, void *address,
3034 unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035{
3036 return copy_from_user(address, param, size) ? -EFAULT : 0;
3037}
3038
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039static inline const char *drive_name(int type, int drive)
3040{
3041 struct floppy_struct *floppy;
3042
3043 if (type)
3044 floppy = floppy_type + type;
3045 else {
3046 if (UDP->native_format)
3047 floppy = floppy_type + UDP->native_format;
3048 else
3049 return "(null)";
3050 }
3051 if (floppy->name)
3052 return floppy->name;
3053 else
3054 return "(null)";
3055}
3056
3057/* raw commands */
3058static void raw_cmd_done(int flag)
3059{
3060 int i;
3061
3062 if (!flag) {
3063 raw_cmd->flags |= FD_RAW_FAILURE;
3064 raw_cmd->flags |= FD_RAW_HARDFAILURE;
3065 } else {
3066 raw_cmd->reply_count = inr;
3067 if (raw_cmd->reply_count > MAX_REPLIES)
3068 raw_cmd->reply_count = 0;
3069 for (i = 0; i < raw_cmd->reply_count; i++)
3070 raw_cmd->reply[i] = reply_buffer[i];
3071
3072 if (raw_cmd->flags & (FD_RAW_READ | FD_RAW_WRITE)) {
3073 unsigned long flags;
3074 flags = claim_dma_lock();
3075 raw_cmd->length = fd_get_dma_residue();
3076 release_dma_lock(flags);
3077 }
3078
3079 if ((raw_cmd->flags & FD_RAW_SOFTFAILURE) &&
3080 (!raw_cmd->reply_count || (raw_cmd->reply[0] & 0xc0)))
3081 raw_cmd->flags |= FD_RAW_FAILURE;
3082
3083 if (disk_change(current_drive))
3084 raw_cmd->flags |= FD_RAW_DISK_CHANGE;
3085 else
3086 raw_cmd->flags &= ~FD_RAW_DISK_CHANGE;
3087 if (raw_cmd->flags & FD_RAW_NO_MOTOR_AFTER)
3088 motor_off_callback(current_drive);
3089
3090 if (raw_cmd->next &&
3091 (!(raw_cmd->flags & FD_RAW_FAILURE) ||
3092 !(raw_cmd->flags & FD_RAW_STOP_IF_FAILURE)) &&
3093 ((raw_cmd->flags & FD_RAW_FAILURE) ||
3094 !(raw_cmd->flags & FD_RAW_STOP_IF_SUCCESS))) {
3095 raw_cmd = raw_cmd->next;
3096 return;
3097 }
3098 }
3099 generic_done(flag);
3100}
3101
3102static struct cont_t raw_cmd_cont = {
3103 .interrupt = success_and_wakeup,
3104 .redo = floppy_start,
3105 .error = generic_failure,
3106 .done = raw_cmd_done
3107};
3108
3109static inline int raw_cmd_copyout(int cmd, char __user *param,
3110 struct floppy_raw_cmd *ptr)
3111{
3112 int ret;
3113
3114 while (ptr) {
Joe Perches86b12b42010-03-10 15:20:56 -08003115 ret = copy_to_user((void __user *)param, ptr, sizeof(*ptr));
3116 if (ret)
3117 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 param += sizeof(struct floppy_raw_cmd);
3119 if ((ptr->flags & FD_RAW_READ) && ptr->buffer_length) {
Joe Perchesbb57f0c62010-03-10 15:20:50 -08003120 if (ptr->length >= 0 &&
3121 ptr->length <= ptr->buffer_length) {
3122 long length = ptr->buffer_length - ptr->length;
Joe Perches4575b552010-03-10 15:20:55 -08003123 ret = fd_copyout(ptr->data, ptr->kernel_data,
3124 length);
3125 if (ret)
3126 return ret;
Joe Perchesbb57f0c62010-03-10 15:20:50 -08003127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 }
3129 ptr = ptr->next;
3130 }
3131 return 0;
3132}
3133
3134static void raw_cmd_free(struct floppy_raw_cmd **ptr)
3135{
Jesper Juhl06f748c2007-10-16 23:30:57 -07003136 struct floppy_raw_cmd *next;
3137 struct floppy_raw_cmd *this;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138
3139 this = *ptr;
3140 *ptr = NULL;
3141 while (this) {
3142 if (this->buffer_length) {
3143 fd_dma_mem_free((unsigned long)this->kernel_data,
3144 this->buffer_length);
3145 this->buffer_length = 0;
3146 }
3147 next = this->next;
3148 kfree(this);
3149 this = next;
3150 }
3151}
3152
3153static inline int raw_cmd_copyin(int cmd, char __user *param,
3154 struct floppy_raw_cmd **rcmd)
3155{
3156 struct floppy_raw_cmd *ptr;
3157 int ret;
3158 int i;
3159
3160 *rcmd = NULL;
3161 while (1) {
3162 ptr = (struct floppy_raw_cmd *)
3163 kmalloc(sizeof(struct floppy_raw_cmd), GFP_USER);
3164 if (!ptr)
3165 return -ENOMEM;
3166 *rcmd = ptr;
Joe Perches86b12b42010-03-10 15:20:56 -08003167 ret = copy_from_user(ptr, (void __user *)param, sizeof(*ptr));
3168 if (ret)
3169 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 ptr->next = NULL;
3171 ptr->buffer_length = 0;
3172 param += sizeof(struct floppy_raw_cmd);
3173 if (ptr->cmd_count > 33)
3174 /* the command may now also take up the space
3175 * initially intended for the reply & the
3176 * reply count. Needed for long 82078 commands
3177 * such as RESTORE, which takes ... 17 command
3178 * bytes. Murphy's law #137: When you reserve
3179 * 16 bytes for a structure, you'll one day
3180 * discover that you really need 17...
3181 */
3182 return -EINVAL;
3183
3184 for (i = 0; i < 16; i++)
3185 ptr->reply[i] = 0;
3186 ptr->resultcode = 0;
3187 ptr->kernel_data = NULL;
3188
3189 if (ptr->flags & (FD_RAW_READ | FD_RAW_WRITE)) {
3190 if (ptr->length <= 0)
3191 return -EINVAL;
3192 ptr->kernel_data =
3193 (char *)fd_dma_mem_alloc(ptr->length);
3194 fallback_on_nodma_alloc(&ptr->kernel_data, ptr->length);
3195 if (!ptr->kernel_data)
3196 return -ENOMEM;
3197 ptr->buffer_length = ptr->length;
3198 }
Joe Perches4575b552010-03-10 15:20:55 -08003199 if (ptr->flags & FD_RAW_WRITE) {
3200 ret = fd_copyin(ptr->data, ptr->kernel_data,
3201 ptr->length);
3202 if (ret)
3203 return ret;
3204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 rcmd = &(ptr->next);
3206 if (!(ptr->flags & FD_RAW_MORE))
3207 return 0;
3208 ptr->rate &= 0x43;
3209 }
3210}
3211
3212static int raw_cmd_ioctl(int cmd, void __user *param)
3213{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 struct floppy_raw_cmd *my_raw_cmd;
Jesper Juhl06f748c2007-10-16 23:30:57 -07003215 int drive;
3216 int ret2;
3217 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218
3219 if (FDCS->rawcmd <= 1)
3220 FDCS->rawcmd = 1;
3221 for (drive = 0; drive < N_DRIVE; drive++) {
3222 if (FDC(drive) != fdc)
3223 continue;
3224 if (drive == current_drive) {
3225 if (UDRS->fd_ref > 1) {
3226 FDCS->rawcmd = 2;
3227 break;
3228 }
3229 } else if (UDRS->fd_ref) {
3230 FDCS->rawcmd = 2;
3231 break;
3232 }
3233 }
3234
3235 if (FDCS->reset)
3236 return -EIO;
3237
3238 ret = raw_cmd_copyin(cmd, param, &my_raw_cmd);
3239 if (ret) {
3240 raw_cmd_free(&my_raw_cmd);
3241 return ret;
3242 }
3243
3244 raw_cmd = my_raw_cmd;
3245 cont = &raw_cmd_cont;
Joe Perches74f63f42010-03-10 15:20:58 -08003246 ret = wait_til_done(floppy_start, true);
Joe Perches87f530d2010-03-10 15:20:54 -08003247 debug_dcl(DP->flags, "calling disk change from raw_cmd ioctl\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248
3249 if (ret != -EINTR && FDCS->reset)
3250 ret = -EIO;
3251
3252 DRS->track = NO_TRACK;
3253
3254 ret2 = raw_cmd_copyout(cmd, param, my_raw_cmd);
3255 if (!ret)
3256 ret = ret2;
3257 raw_cmd_free(&my_raw_cmd);
3258 return ret;
3259}
3260
3261static int invalidate_drive(struct block_device *bdev)
3262{
3263 /* invalidate the buffer track to force a reread */
3264 set_bit((long)bdev->bd_disk->private_data, &fake_change);
3265 process_fd_request();
3266 check_disk_change(bdev);
3267 return 0;
3268}
3269
3270static inline int set_geometry(unsigned int cmd, struct floppy_struct *g,
3271 int drive, int type, struct block_device *bdev)
3272{
3273 int cnt;
3274
3275 /* sanity checking for parameters. */
3276 if (g->sect <= 0 ||
3277 g->head <= 0 ||
3278 g->track <= 0 || g->track > UDP->tracks >> STRETCH(g) ||
3279 /* check if reserved bits are set */
Keith Wansbrough9e491842008-09-22 14:57:17 -07003280 (g->stretch & ~(FD_STRETCH | FD_SWAPSIDES | FD_SECTBASEMASK)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 return -EINVAL;
3282 if (type) {
3283 if (!capable(CAP_SYS_ADMIN))
3284 return -EPERM;
Jes Sorensenb1c82b52006-03-23 03:00:26 -08003285 mutex_lock(&open_lock);
Joe Perches74f63f42010-03-10 15:20:58 -08003286 if (lock_fdc(drive, true)) {
Jiri Slaby8516a502009-06-30 11:41:44 -07003287 mutex_unlock(&open_lock);
3288 return -EINTR;
3289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 floppy_type[type] = *g;
3291 floppy_type[type].name = "user format";
3292 for (cnt = type << 2; cnt < (type << 2) + 4; cnt++)
3293 floppy_sizes[cnt] = floppy_sizes[cnt + 0x80] =
3294 floppy_type[type].size + 1;
3295 process_fd_request();
3296 for (cnt = 0; cnt < N_DRIVE; cnt++) {
3297 struct block_device *bdev = opened_bdev[cnt];
3298 if (!bdev || ITYPE(drive_state[cnt].fd_device) != type)
3299 continue;
Christoph Hellwig2ef41632005-05-05 16:15:59 -07003300 __invalidate_device(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 }
Jes Sorensenb1c82b52006-03-23 03:00:26 -08003302 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 } else {
3304 int oldStretch;
Joe Perches52a0d612010-03-10 15:20:53 -08003305
Joe Perches74f63f42010-03-10 15:20:58 -08003306 if (lock_fdc(drive, true))
Joe Perches52a0d612010-03-10 15:20:53 -08003307 return -EINTR;
Joe Perches4575b552010-03-10 15:20:55 -08003308 if (cmd != FDDEFPRM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 /* notice a disk change immediately, else
3310 * we lose our settings immediately*/
Joe Perches74f63f42010-03-10 15:20:58 -08003311 if (poll_drive(true, FD_RAW_NEED_DISK) == -EINTR)
Joe Perches4575b552010-03-10 15:20:55 -08003312 return -EINTR;
3313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 oldStretch = g->stretch;
3315 user_params[drive] = *g;
3316 if (buffer_drive == drive)
3317 SUPBOUND(buffer_max, user_params[drive].sect);
3318 current_type[drive] = &user_params[drive];
3319 floppy_sizes[drive] = user_params[drive].size;
3320 if (cmd == FDDEFPRM)
3321 DRS->keep_data = -1;
3322 else
3323 DRS->keep_data = 1;
3324 /* invalidation. Invalidate only when needed, i.e.
3325 * when there are already sectors in the buffer cache
3326 * whose number will change. This is useful, because
3327 * mtools often changes the geometry of the disk after
3328 * looking at the boot block */
3329 if (DRS->maxblock > user_params[drive].sect ||
3330 DRS->maxtrack ||
3331 ((user_params[drive].sect ^ oldStretch) &
Keith Wansbrough9e491842008-09-22 14:57:17 -07003332 (FD_SWAPSIDES | FD_SECTBASEMASK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 invalidate_drive(bdev);
3334 else
3335 process_fd_request();
3336 }
3337 return 0;
3338}
3339
3340/* handle obsolete ioctl's */
3341static int ioctl_table[] = {
3342 FDCLRPRM,
3343 FDSETPRM,
3344 FDDEFPRM,
3345 FDGETPRM,
3346 FDMSGON,
3347 FDMSGOFF,
3348 FDFMTBEG,
3349 FDFMTTRK,
3350 FDFMTEND,
3351 FDSETEMSGTRESH,
3352 FDFLUSH,
3353 FDSETMAXERRS,
3354 FDGETMAXERRS,
3355 FDGETDRVTYP,
3356 FDSETDRVPRM,
3357 FDGETDRVPRM,
3358 FDGETDRVSTAT,
3359 FDPOLLDRVSTAT,
3360 FDRESET,
3361 FDGETFDCSTAT,
3362 FDWERRORCLR,
3363 FDWERRORGET,
3364 FDRAWCMD,
3365 FDEJECT,
3366 FDTWADDLE
3367};
3368
3369static inline int normalize_ioctl(int *cmd, int *size)
3370{
3371 int i;
3372
3373 for (i = 0; i < ARRAY_SIZE(ioctl_table); i++) {
3374 if ((*cmd & 0xffff) == (ioctl_table[i] & 0xffff)) {
3375 *size = _IOC_SIZE(*cmd);
3376 *cmd = ioctl_table[i];
3377 if (*size > _IOC_SIZE(*cmd)) {
Joe Perchesb46df352010-03-10 15:20:46 -08003378 pr_info("ioctl not yet supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 return -EFAULT;
3380 }
3381 return 0;
3382 }
3383 }
3384 return -EINVAL;
3385}
3386
3387static int get_floppy_geometry(int drive, int type, struct floppy_struct **g)
3388{
3389 if (type)
3390 *g = &floppy_type[type];
3391 else {
Joe Perches74f63f42010-03-10 15:20:58 -08003392 if (lock_fdc(drive, false))
Joe Perches52a0d612010-03-10 15:20:53 -08003393 return -EINTR;
Joe Perches74f63f42010-03-10 15:20:58 -08003394 if (poll_drive(false, 0) == -EINTR)
Joe Perches4575b552010-03-10 15:20:55 -08003395 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 process_fd_request();
3397 *g = current_type[drive];
3398 }
3399 if (!*g)
3400 return -ENODEV;
3401 return 0;
3402}
3403
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003404static int fd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
3405{
3406 int drive = (long)bdev->bd_disk->private_data;
3407 int type = ITYPE(drive_state[drive].fd_device);
3408 struct floppy_struct *g;
3409 int ret;
3410
3411 ret = get_floppy_geometry(drive, type, &g);
3412 if (ret)
3413 return ret;
3414
3415 geo->heads = g->head;
3416 geo->sectors = g->sect;
3417 geo->cylinders = g->track;
3418 return 0;
3419}
3420
Al Viroa4af9b42008-03-02 09:27:55 -05003421static int fd_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 unsigned long param)
3423{
Al Viroa4af9b42008-03-02 09:27:55 -05003424#define FD_IOCTL_ALLOWED (mode & (FMODE_WRITE|FMODE_WRITE_IOCTL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425
Al Viroa4af9b42008-03-02 09:27:55 -05003426 int drive = (long)bdev->bd_disk->private_data;
Jesper Juhl06f748c2007-10-16 23:30:57 -07003427 int type = ITYPE(UDRS->fd_device);
3428 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 int ret;
3430 int size;
3431 union inparam {
3432 struct floppy_struct g; /* geometry */
3433 struct format_descr f;
3434 struct floppy_max_errors max_errors;
3435 struct floppy_drive_params dp;
3436 } inparam; /* parameters coming from user space */
3437 const char *outparam; /* parameters passed back to user space */
3438
3439 /* convert compatibility eject ioctls into floppy eject ioctl.
3440 * We do this in order to provide a means to eject floppy disks before
3441 * installing the new fdutils package */
3442 if (cmd == CDROMEJECT || /* CD-ROM eject */
Joe Perchesa81ee542010-03-10 15:20:46 -08003443 cmd == 0x6470) { /* SunOS floppy eject */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 DPRINT("obsolete eject ioctl\n");
3445 DPRINT("please use floppycontrol --eject\n");
3446 cmd = FDEJECT;
3447 }
3448
Joe Perchesa81ee542010-03-10 15:20:46 -08003449 if (!((cmd & 0xff00) == 0x0200))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 return -EINVAL;
3451
Joe Perchesa81ee542010-03-10 15:20:46 -08003452 /* convert the old style command into a new style command */
Joe Perches4575b552010-03-10 15:20:55 -08003453 ret = normalize_ioctl(&cmd, &size);
3454 if (ret)
3455 return ret;
Joe Perchesa81ee542010-03-10 15:20:46 -08003456
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 /* permission checks */
3458 if (((cmd & 0x40) && !FD_IOCTL_ALLOWED) ||
3459 ((cmd & 0x80) && !capable(CAP_SYS_ADMIN)))
3460 return -EPERM;
3461
Arjan van de Ven2886a8b2009-12-14 18:00:11 -08003462 if (WARN_ON(size < 0 || size > sizeof(inparam)))
3463 return -EINVAL;
3464
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 /* copyin */
Joe Perchesb87c9e02010-03-10 15:20:50 -08003466 memset(&inparam, 0, sizeof(inparam));
Joe Perches4575b552010-03-10 15:20:55 -08003467 if (_IOC_DIR(cmd) & _IOC_WRITE) {
3468 ret = fd_copyin((void __user *)param, &inparam, size);
3469 if (ret)
3470 return ret;
3471 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472
Joe Perchesda273652010-03-10 15:20:52 -08003473 switch (cmd) {
3474 case FDEJECT:
3475 if (UDRS->fd_ref != 1)
3476 /* somebody else has this drive open */
3477 return -EBUSY;
Joe Perches74f63f42010-03-10 15:20:58 -08003478 if (lock_fdc(drive, true))
Joe Perches52a0d612010-03-10 15:20:53 -08003479 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480
Joe Perchesda273652010-03-10 15:20:52 -08003481 /* do the actual eject. Fails on
3482 * non-Sparc architectures */
3483 ret = fd_eject(UNIT(drive));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484
Joe Perchese0298532010-03-10 15:20:55 -08003485 set_bit(FD_DISK_CHANGED_BIT, &UDRS->flags);
3486 set_bit(FD_VERIFY_BIT, &UDRS->flags);
Joe Perchesda273652010-03-10 15:20:52 -08003487 process_fd_request();
3488 return ret;
3489 case FDCLRPRM:
Joe Perches74f63f42010-03-10 15:20:58 -08003490 if (lock_fdc(drive, true))
Joe Perches52a0d612010-03-10 15:20:53 -08003491 return -EINTR;
Joe Perchesda273652010-03-10 15:20:52 -08003492 current_type[drive] = NULL;
3493 floppy_sizes[drive] = MAX_DISK_SIZE << 1;
3494 UDRS->keep_data = 0;
3495 return invalidate_drive(bdev);
3496 case FDSETPRM:
3497 case FDDEFPRM:
3498 return set_geometry(cmd, &inparam.g, drive, type, bdev);
3499 case FDGETPRM:
Joe Perches4575b552010-03-10 15:20:55 -08003500 ret = get_floppy_geometry(drive, type,
Joe Perchesda273652010-03-10 15:20:52 -08003501 (struct floppy_struct **)
Joe Perches4575b552010-03-10 15:20:55 -08003502 &outparam);
3503 if (ret)
3504 return ret;
Joe Perchesda273652010-03-10 15:20:52 -08003505 break;
3506 case FDMSGON:
3507 UDP->flags |= FTD_MSG;
3508 return 0;
3509 case FDMSGOFF:
3510 UDP->flags &= ~FTD_MSG;
3511 return 0;
3512 case FDFMTBEG:
Joe Perches74f63f42010-03-10 15:20:58 -08003513 if (lock_fdc(drive, true))
Joe Perches52a0d612010-03-10 15:20:53 -08003514 return -EINTR;
Joe Perches74f63f42010-03-10 15:20:58 -08003515 if (poll_drive(true, FD_RAW_NEED_DISK) == -EINTR)
Joe Perches4575b552010-03-10 15:20:55 -08003516 return -EINTR;
Joe Perchesda273652010-03-10 15:20:52 -08003517 ret = UDRS->flags;
3518 process_fd_request();
3519 if (ret & FD_VERIFY)
3520 return -ENODEV;
3521 if (!(ret & FD_DISK_WRITABLE))
3522 return -EROFS;
3523 return 0;
3524 case FDFMTTRK:
3525 if (UDRS->fd_ref != 1)
3526 return -EBUSY;
3527 return do_format(drive, &inparam.f);
3528 case FDFMTEND:
3529 case FDFLUSH:
Joe Perches74f63f42010-03-10 15:20:58 -08003530 if (lock_fdc(drive, true))
Joe Perches52a0d612010-03-10 15:20:53 -08003531 return -EINTR;
Joe Perchesda273652010-03-10 15:20:52 -08003532 return invalidate_drive(bdev);
3533 case FDSETEMSGTRESH:
3534 UDP->max_errors.reporting = (unsigned short)(param & 0x0f);
3535 return 0;
3536 case FDGETMAXERRS:
3537 outparam = (const char *)&UDP->max_errors;
3538 break;
3539 case FDSETMAXERRS:
3540 UDP->max_errors = inparam.max_errors;
3541 break;
3542 case FDGETDRVTYP:
3543 outparam = drive_name(type, drive);
3544 SUPBOUND(size, strlen(outparam) + 1);
3545 break;
3546 case FDSETDRVPRM:
3547 *UDP = inparam.dp;
3548 break;
3549 case FDGETDRVPRM:
3550 outparam = (const char *)UDP;
3551 break;
3552 case FDPOLLDRVSTAT:
Joe Perches74f63f42010-03-10 15:20:58 -08003553 if (lock_fdc(drive, true))
Joe Perches52a0d612010-03-10 15:20:53 -08003554 return -EINTR;
Joe Perches74f63f42010-03-10 15:20:58 -08003555 if (poll_drive(true, FD_RAW_NEED_DISK) == -EINTR)
Joe Perches4575b552010-03-10 15:20:55 -08003556 return -EINTR;
Joe Perchesda273652010-03-10 15:20:52 -08003557 process_fd_request();
3558 /* fall through */
3559 case FDGETDRVSTAT:
3560 outparam = (const char *)UDRS;
3561 break;
3562 case FDRESET:
Joe Perches74f63f42010-03-10 15:20:58 -08003563 return user_reset_fdc(drive, (int)param, true);
Joe Perchesda273652010-03-10 15:20:52 -08003564 case FDGETFDCSTAT:
3565 outparam = (const char *)UFDCS;
3566 break;
3567 case FDWERRORCLR:
3568 memset(UDRWE, 0, sizeof(*UDRWE));
3569 return 0;
3570 case FDWERRORGET:
3571 outparam = (const char *)UDRWE;
3572 break;
3573 case FDRAWCMD:
3574 if (type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575 return -EINVAL;
Joe Perches74f63f42010-03-10 15:20:58 -08003576 if (lock_fdc(drive, true))
Joe Perches52a0d612010-03-10 15:20:53 -08003577 return -EINTR;
Joe Perchesda273652010-03-10 15:20:52 -08003578 set_floppy(drive);
Joe Perches4575b552010-03-10 15:20:55 -08003579 i = raw_cmd_ioctl(cmd, (void __user *)param);
3580 if (i == -EINTR)
3581 return -EINTR;
Joe Perchesda273652010-03-10 15:20:52 -08003582 process_fd_request();
3583 return i;
3584 case FDTWADDLE:
Joe Perches74f63f42010-03-10 15:20:58 -08003585 if (lock_fdc(drive, true))
Joe Perches52a0d612010-03-10 15:20:53 -08003586 return -EINTR;
Joe Perchesda273652010-03-10 15:20:52 -08003587 twaddle();
3588 process_fd_request();
3589 return 0;
3590 default:
3591 return -EINVAL;
3592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593
3594 if (_IOC_DIR(cmd) & _IOC_READ)
3595 return fd_copyout((void __user *)param, outparam, size);
Joe Perchesda273652010-03-10 15:20:52 -08003596
3597 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598}
3599
3600static void __init config_types(void)
3601{
Joe Perchesb46df352010-03-10 15:20:46 -08003602 bool has_drive = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 int drive;
3604
3605 /* read drive info out of physical CMOS */
3606 drive = 0;
3607 if (!UDP->cmos)
3608 UDP->cmos = FLOPPY0_TYPE;
3609 drive = 1;
3610 if (!UDP->cmos && FLOPPY1_TYPE)
3611 UDP->cmos = FLOPPY1_TYPE;
3612
Jesper Juhl06f748c2007-10-16 23:30:57 -07003613 /* FIXME: additional physical CMOS drive detection should go here */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614
3615 for (drive = 0; drive < N_DRIVE; drive++) {
3616 unsigned int type = UDP->cmos;
3617 struct floppy_drive_params *params;
3618 const char *name = NULL;
3619 static char temparea[32];
3620
Tobias Klauser945f3902006-01-08 01:05:11 -08003621 if (type < ARRAY_SIZE(default_drive_params)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 params = &default_drive_params[type].params;
3623 if (type) {
3624 name = default_drive_params[type].name;
3625 allowed_drive_mask |= 1 << drive;
3626 } else
3627 allowed_drive_mask &= ~(1 << drive);
3628 } else {
3629 params = &default_drive_params[0].params;
3630 sprintf(temparea, "unknown type %d (usb?)", type);
3631 name = temparea;
3632 }
3633 if (name) {
Joe Perchesb46df352010-03-10 15:20:46 -08003634 const char *prepend;
3635 if (!has_drive) {
3636 prepend = "";
3637 has_drive = true;
3638 pr_info("Floppy drive(s):");
3639 } else {
3640 prepend = ",";
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641 }
Joe Perchesb46df352010-03-10 15:20:46 -08003642
3643 pr_cont("%s fd%d is %s", prepend, drive, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 }
3645 *UDP = *params;
3646 }
Joe Perchesb46df352010-03-10 15:20:46 -08003647
3648 if (has_drive)
3649 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650}
3651
Al Viroa4af9b42008-03-02 09:27:55 -05003652static int floppy_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653{
Al Viroa4af9b42008-03-02 09:27:55 -05003654 int drive = (long)disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655
Jes Sorensenb1c82b52006-03-23 03:00:26 -08003656 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 if (UDRS->fd_ref < 0)
3658 UDRS->fd_ref = 0;
3659 else if (!UDRS->fd_ref--) {
3660 DPRINT("floppy_release with fd_ref == 0");
3661 UDRS->fd_ref = 0;
3662 }
3663 if (!UDRS->fd_ref)
3664 opened_bdev[drive] = NULL;
Jes Sorensenb1c82b52006-03-23 03:00:26 -08003665 mutex_unlock(&open_lock);
Ingo Molnar3e541a42006-07-03 00:24:23 -07003666
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 return 0;
3668}
3669
3670/*
3671 * floppy_open check for aliasing (/dev/fd0 can be the same as
3672 * /dev/PS0 etc), and disallows simultaneous access to the same
3673 * drive with different device numbers.
3674 */
Al Viroa4af9b42008-03-02 09:27:55 -05003675static int floppy_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676{
Al Viroa4af9b42008-03-02 09:27:55 -05003677 int drive = (long)bdev->bd_disk->private_data;
3678 int old_dev, new_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 int try;
3680 int res = -EBUSY;
3681 char *tmp;
3682
Jes Sorensenb1c82b52006-03-23 03:00:26 -08003683 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 old_dev = UDRS->fd_device;
Al Viroa4af9b42008-03-02 09:27:55 -05003685 if (opened_bdev[drive] && opened_bdev[drive] != bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 goto out2;
3687
3688 if (!UDRS->fd_ref && (UDP->flags & FD_BROKEN_DCL)) {
Joe Perchese0298532010-03-10 15:20:55 -08003689 set_bit(FD_DISK_CHANGED_BIT, &UDRS->flags);
3690 set_bit(FD_VERIFY_BIT, &UDRS->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691 }
3692
Al Viroa4af9b42008-03-02 09:27:55 -05003693 if (UDRS->fd_ref == -1 || (UDRS->fd_ref && (mode & FMODE_EXCL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 goto out2;
3695
Al Viroa4af9b42008-03-02 09:27:55 -05003696 if (mode & FMODE_EXCL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697 UDRS->fd_ref = -1;
3698 else
3699 UDRS->fd_ref++;
3700
Al Viroa4af9b42008-03-02 09:27:55 -05003701 opened_bdev[drive] = bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702
3703 res = -ENXIO;
3704
3705 if (!floppy_track_buffer) {
3706 /* if opening an ED drive, reserve a big buffer,
3707 * else reserve a small one */
3708 if ((UDP->cmos == 6) || (UDP->cmos == 5))
3709 try = 64; /* Only 48 actually useful */
3710 else
3711 try = 32; /* Only 24 actually useful */
3712
3713 tmp = (char *)fd_dma_mem_alloc(1024 * try);
3714 if (!tmp && !floppy_track_buffer) {
3715 try >>= 1; /* buffer only one side */
3716 INFBOUND(try, 16);
3717 tmp = (char *)fd_dma_mem_alloc(1024 * try);
3718 }
Joe Perchesa81ee542010-03-10 15:20:46 -08003719 if (!tmp && !floppy_track_buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 fallback_on_nodma_alloc(&tmp, 2048 * try);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721 if (!tmp && !floppy_track_buffer) {
3722 DPRINT("Unable to allocate DMA memory\n");
3723 goto out;
3724 }
3725 if (floppy_track_buffer) {
3726 if (tmp)
3727 fd_dma_mem_free((unsigned long)tmp, try * 1024);
3728 } else {
3729 buffer_min = buffer_max = -1;
3730 floppy_track_buffer = tmp;
3731 max_buffer_sectors = try;
3732 }
3733 }
3734
Al Viroa4af9b42008-03-02 09:27:55 -05003735 new_dev = MINOR(bdev->bd_dev);
3736 UDRS->fd_device = new_dev;
3737 set_capacity(disks[drive], floppy_sizes[new_dev]);
3738 if (old_dev != -1 && old_dev != new_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739 if (buffer_drive == drive)
3740 buffer_track = -1;
3741 }
3742
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 if (UFDCS->rawcmd == 1)
3744 UFDCS->rawcmd = 2;
3745
Al Viroa4af9b42008-03-02 09:27:55 -05003746 if (!(mode & FMODE_NDELAY)) {
3747 if (mode & (FMODE_READ|FMODE_WRITE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748 UDRS->last_checked = 0;
Al Viroa4af9b42008-03-02 09:27:55 -05003749 check_disk_change(bdev);
Joe Perchese0298532010-03-10 15:20:55 -08003750 if (test_bit(FD_DISK_CHANGED_BIT, &UDRS->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751 goto out;
3752 }
3753 res = -EROFS;
Joe Perchese0298532010-03-10 15:20:55 -08003754 if ((mode & FMODE_WRITE) &&
3755 !test_bit(FD_DISK_WRITABLE_BIT, &UDRS->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 goto out;
3757 }
Jes Sorensenb1c82b52006-03-23 03:00:26 -08003758 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759 return 0;
3760out:
3761 if (UDRS->fd_ref < 0)
3762 UDRS->fd_ref = 0;
3763 else
3764 UDRS->fd_ref--;
3765 if (!UDRS->fd_ref)
3766 opened_bdev[drive] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767out2:
Jes Sorensenb1c82b52006-03-23 03:00:26 -08003768 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769 return res;
3770}
3771
3772/*
3773 * Check if the disk has been changed or if a change has been faked.
3774 */
3775static int check_floppy_change(struct gendisk *disk)
3776{
3777 int drive = (long)disk->private_data;
3778
Joe Perchese0298532010-03-10 15:20:55 -08003779 if (test_bit(FD_DISK_CHANGED_BIT, &UDRS->flags) ||
3780 test_bit(FD_VERIFY_BIT, &UDRS->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 return 1;
3782
Marcelo Feitoza Parisi50297cb2006-03-28 01:56:44 -08003783 if (time_after(jiffies, UDRS->last_checked + UDP->checkfreq)) {
Joe Perches74f63f42010-03-10 15:20:58 -08003784 lock_fdc(drive, false);
3785 poll_drive(false, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 process_fd_request();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 }
3788
Joe Perchese0298532010-03-10 15:20:55 -08003789 if (test_bit(FD_DISK_CHANGED_BIT, &UDRS->flags) ||
3790 test_bit(FD_VERIFY_BIT, &UDRS->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 test_bit(drive, &fake_change) ||
3792 (!ITYPE(UDRS->fd_device) && !current_type[drive]))
3793 return 1;
3794 return 0;
3795}
3796
3797/*
3798 * This implements "read block 0" for floppy_revalidate().
3799 * Needed for format autodetection, checking whether there is
3800 * a disk in the drive, and whether that disk is writable.
3801 */
3802
Joe Perchesbb57f0c62010-03-10 15:20:50 -08003803static void floppy_rb0_complete(struct bio *bio, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 complete((struct completion *)bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806}
3807
3808static int __floppy_read_block_0(struct block_device *bdev)
3809{
3810 struct bio bio;
3811 struct bio_vec bio_vec;
3812 struct completion complete;
3813 struct page *page;
3814 size_t size;
3815
3816 page = alloc_page(GFP_NOIO);
3817 if (!page) {
3818 process_fd_request();
3819 return -ENOMEM;
3820 }
3821
3822 size = bdev->bd_block_size;
3823 if (!size)
3824 size = 1024;
3825
3826 bio_init(&bio);
3827 bio.bi_io_vec = &bio_vec;
3828 bio_vec.bv_page = page;
3829 bio_vec.bv_len = size;
3830 bio_vec.bv_offset = 0;
3831 bio.bi_vcnt = 1;
3832 bio.bi_idx = 0;
3833 bio.bi_size = size;
3834 bio.bi_bdev = bdev;
3835 bio.bi_sector = 0;
3836 init_completion(&complete);
3837 bio.bi_private = &complete;
3838 bio.bi_end_io = floppy_rb0_complete;
3839
3840 submit_bio(READ, &bio);
3841 generic_unplug_device(bdev_get_queue(bdev));
3842 process_fd_request();
3843 wait_for_completion(&complete);
3844
3845 __free_page(page);
3846
3847 return 0;
3848}
3849
3850/* revalidate the floppy disk, i.e. trigger format autodetection by reading
3851 * the bootblock (block 0). "Autodetection" is also needed to check whether
3852 * there is a disk in the drive at all... Thus we also do it for fixed
3853 * geometry formats */
3854static int floppy_revalidate(struct gendisk *disk)
3855{
3856 int drive = (long)disk->private_data;
3857#define NO_GEOM (!current_type[drive] && !ITYPE(UDRS->fd_device))
3858 int cf;
3859 int res = 0;
3860
Joe Perchese0298532010-03-10 15:20:55 -08003861 if (test_bit(FD_DISK_CHANGED_BIT, &UDRS->flags) ||
3862 test_bit(FD_VERIFY_BIT, &UDRS->flags) ||
3863 test_bit(drive, &fake_change) || NO_GEOM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864 if (usage_count == 0) {
Joe Perchesb46df352010-03-10 15:20:46 -08003865 pr_info("VFS: revalidate called on non-open device.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866 return -EFAULT;
3867 }
Joe Perches74f63f42010-03-10 15:20:58 -08003868 lock_fdc(drive, false);
Joe Perchese0298532010-03-10 15:20:55 -08003869 cf = (test_bit(FD_DISK_CHANGED_BIT, &UDRS->flags) ||
3870 test_bit(FD_VERIFY_BIT, &UDRS->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 if (!(cf || test_bit(drive, &fake_change) || NO_GEOM)) {
3872 process_fd_request(); /*already done by another thread */
3873 return 0;
3874 }
3875 UDRS->maxblock = 0;
3876 UDRS->maxtrack = 0;
3877 if (buffer_drive == drive)
3878 buffer_track = -1;
3879 clear_bit(drive, &fake_change);
Joe Perchese0298532010-03-10 15:20:55 -08003880 clear_bit(FD_DISK_CHANGED_BIT, &UDRS->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 if (cf)
3882 UDRS->generation++;
3883 if (NO_GEOM) {
3884 /* auto-sensing */
3885 res = __floppy_read_block_0(opened_bdev[drive]);
3886 } else {
3887 if (cf)
Joe Perches74f63f42010-03-10 15:20:58 -08003888 poll_drive(false, FD_RAW_NEED_DISK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889 process_fd_request();
3890 }
3891 }
3892 set_capacity(disk, floppy_sizes[UDRS->fd_device]);
3893 return res;
3894}
3895
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07003896static const struct block_device_operations floppy_fops = {
Jesper Juhl06f748c2007-10-16 23:30:57 -07003897 .owner = THIS_MODULE,
Al Viroa4af9b42008-03-02 09:27:55 -05003898 .open = floppy_open,
3899 .release = floppy_release,
3900 .locked_ioctl = fd_ioctl,
Jesper Juhl06f748c2007-10-16 23:30:57 -07003901 .getgeo = fd_getgeo,
3902 .media_changed = check_floppy_change,
3903 .revalidate_disk = floppy_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906/*
3907 * Floppy Driver initialization
3908 * =============================
3909 */
3910
3911/* Determine the floppy disk controller type */
3912/* This routine was written by David C. Niemi */
3913static char __init get_fdc_version(void)
3914{
3915 int r;
3916
3917 output_byte(FD_DUMPREGS); /* 82072 and better know DUMPREGS */
3918 if (FDCS->reset)
3919 return FDC_NONE;
Joe Perchesd7b2b2e2010-03-10 15:20:48 -08003920 r = result();
3921 if (r <= 0x00)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922 return FDC_NONE; /* No FDC present ??? */
3923 if ((r == 1) && (reply_buffer[0] == 0x80)) {
Joe Perchesb46df352010-03-10 15:20:46 -08003924 pr_info("FDC %d is an 8272A\n", fdc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 return FDC_8272A; /* 8272a/765 don't know DUMPREGS */
3926 }
3927 if (r != 10) {
Joe Perchesb46df352010-03-10 15:20:46 -08003928 pr_info("FDC %d init: DUMPREGS: unexpected return of %d bytes.\n",
3929 fdc, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 return FDC_UNKNOWN;
3931 }
3932
3933 if (!fdc_configure()) {
Joe Perchesb46df352010-03-10 15:20:46 -08003934 pr_info("FDC %d is an 82072\n", fdc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 return FDC_82072; /* 82072 doesn't know CONFIGURE */
3936 }
3937
3938 output_byte(FD_PERPENDICULAR);
3939 if (need_more_output() == MORE_OUTPUT) {
3940 output_byte(0);
3941 } else {
Joe Perchesb46df352010-03-10 15:20:46 -08003942 pr_info("FDC %d is an 82072A\n", fdc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943 return FDC_82072A; /* 82072A as found on Sparcs. */
3944 }
3945
3946 output_byte(FD_UNLOCK);
3947 r = result();
3948 if ((r == 1) && (reply_buffer[0] == 0x80)) {
Joe Perchesb46df352010-03-10 15:20:46 -08003949 pr_info("FDC %d is a pre-1991 82077\n", fdc);
Joe Perchesd7b2b2e2010-03-10 15:20:48 -08003950 return FDC_82077_ORIG; /* Pre-1991 82077, doesn't know
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 * LOCK/UNLOCK */
3952 }
3953 if ((r != 1) || (reply_buffer[0] != 0x00)) {
Joe Perchesb46df352010-03-10 15:20:46 -08003954 pr_info("FDC %d init: UNLOCK: unexpected return of %d bytes.\n",
3955 fdc, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 return FDC_UNKNOWN;
3957 }
3958 output_byte(FD_PARTID);
3959 r = result();
3960 if (r != 1) {
Joe Perchesb46df352010-03-10 15:20:46 -08003961 pr_info("FDC %d init: PARTID: unexpected return of %d bytes.\n",
3962 fdc, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 return FDC_UNKNOWN;
3964 }
3965 if (reply_buffer[0] == 0x80) {
Joe Perchesb46df352010-03-10 15:20:46 -08003966 pr_info("FDC %d is a post-1991 82077\n", fdc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 return FDC_82077; /* Revised 82077AA passes all the tests */
3968 }
3969 switch (reply_buffer[0] >> 5) {
3970 case 0x0:
3971 /* Either a 82078-1 or a 82078SL running at 5Volt */
Joe Perchesb46df352010-03-10 15:20:46 -08003972 pr_info("FDC %d is an 82078.\n", fdc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 return FDC_82078;
3974 case 0x1:
Joe Perchesb46df352010-03-10 15:20:46 -08003975 pr_info("FDC %d is a 44pin 82078\n", fdc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 return FDC_82078;
3977 case 0x2:
Joe Perchesb46df352010-03-10 15:20:46 -08003978 pr_info("FDC %d is a S82078B\n", fdc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 return FDC_S82078B;
3980 case 0x3:
Joe Perchesb46df352010-03-10 15:20:46 -08003981 pr_info("FDC %d is a National Semiconductor PC87306\n", fdc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 return FDC_87306;
3983 default:
Joe Perchesb46df352010-03-10 15:20:46 -08003984 pr_info("FDC %d init: 82078 variant with unknown PARTID=%d.\n",
3985 fdc, reply_buffer[0] >> 5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 return FDC_82078_UNKN;
3987 }
3988} /* get_fdc_version */
3989
3990/* lilo configuration */
3991
3992static void __init floppy_set_flags(int *ints, int param, int param2)
3993{
3994 int i;
3995
3996 for (i = 0; i < ARRAY_SIZE(default_drive_params); i++) {
3997 if (param)
3998 default_drive_params[i].params.flags |= param2;
3999 else
4000 default_drive_params[i].params.flags &= ~param2;
4001 }
4002 DPRINT("%s flag 0x%x\n", param2 ? "Setting" : "Clearing", param);
4003}
4004
4005static void __init daring(int *ints, int param, int param2)
4006{
4007 int i;
4008
4009 for (i = 0; i < ARRAY_SIZE(default_drive_params); i++) {
4010 if (param) {
4011 default_drive_params[i].params.select_delay = 0;
4012 default_drive_params[i].params.flags |=
4013 FD_SILENT_DCL_CLEAR;
4014 } else {
4015 default_drive_params[i].params.select_delay =
4016 2 * HZ / 100;
4017 default_drive_params[i].params.flags &=
4018 ~FD_SILENT_DCL_CLEAR;
4019 }
4020 }
4021 DPRINT("Assuming %s floppy hardware\n", param ? "standard" : "broken");
4022}
4023
4024static void __init set_cmos(int *ints, int dummy, int dummy2)
4025{
4026 int current_drive = 0;
4027
4028 if (ints[0] != 2) {
4029 DPRINT("wrong number of parameters for CMOS\n");
4030 return;
4031 }
4032 current_drive = ints[1];
4033 if (current_drive < 0 || current_drive >= 8) {
4034 DPRINT("bad drive for set_cmos\n");
4035 return;
4036 }
4037#if N_FDC > 1
4038 if (current_drive >= 4 && !FDC2)
4039 FDC2 = 0x370;
4040#endif
4041 DP->cmos = ints[2];
4042 DPRINT("setting CMOS code to %d\n", ints[2]);
4043}
4044
4045static struct param_table {
4046 const char *name;
4047 void (*fn) (int *ints, int param, int param2);
4048 int *var;
4049 int def_param;
4050 int param2;
4051} config_params[] __initdata = {
4052 {"allowed_drive_mask", NULL, &allowed_drive_mask, 0xff, 0}, /* obsolete */
4053 {"all_drives", NULL, &allowed_drive_mask, 0xff, 0}, /* obsolete */
4054 {"asus_pci", NULL, &allowed_drive_mask, 0x33, 0},
4055 {"irq", NULL, &FLOPPY_IRQ, 6, 0},
4056 {"dma", NULL, &FLOPPY_DMA, 2, 0},
4057 {"daring", daring, NULL, 1, 0},
4058#if N_FDC > 1
4059 {"two_fdc", NULL, &FDC2, 0x370, 0},
4060 {"one_fdc", NULL, &FDC2, 0, 0},
4061#endif
4062 {"thinkpad", floppy_set_flags, NULL, 1, FD_INVERTED_DCL},
4063 {"broken_dcl", floppy_set_flags, NULL, 1, FD_BROKEN_DCL},
4064 {"messages", floppy_set_flags, NULL, 1, FTD_MSG},
4065 {"silent_dcl_clear", floppy_set_flags, NULL, 1, FD_SILENT_DCL_CLEAR},
4066 {"debug", floppy_set_flags, NULL, 1, FD_DEBUG},
4067 {"nodma", NULL, &can_use_virtual_dma, 1, 0},
4068 {"omnibook", NULL, &can_use_virtual_dma, 1, 0},
4069 {"yesdma", NULL, &can_use_virtual_dma, 0, 0},
4070 {"fifo_depth", NULL, &fifo_depth, 0xa, 0},
4071 {"nofifo", NULL, &no_fifo, 0x20, 0},
4072 {"usefifo", NULL, &no_fifo, 0, 0},
4073 {"cmos", set_cmos, NULL, 0, 0},
4074 {"slow", NULL, &slow_floppy, 1, 0},
4075 {"unexpected_interrupts", NULL, &print_unex, 1, 0},
4076 {"no_unexpected_interrupts", NULL, &print_unex, 0, 0},
4077 {"L40SX", NULL, &print_unex, 0, 0}
4078
4079 EXTRA_FLOPPY_PARAMS
4080};
4081
4082static int __init floppy_setup(char *str)
4083{
4084 int i;
4085 int param;
4086 int ints[11];
4087
4088 str = get_options(str, ARRAY_SIZE(ints), ints);
4089 if (str) {
4090 for (i = 0; i < ARRAY_SIZE(config_params); i++) {
4091 if (strcmp(str, config_params[i].name) == 0) {
4092 if (ints[0])
4093 param = ints[1];
4094 else
4095 param = config_params[i].def_param;
4096 if (config_params[i].fn)
Joe Perchesbb57f0c62010-03-10 15:20:50 -08004097 config_params[i].fn(ints, param,
4098 config_params[i].
4099 param2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100 if (config_params[i].var) {
4101 DPRINT("%s=%d\n", str, param);
4102 *config_params[i].var = param;
4103 }
4104 return 1;
4105 }
4106 }
4107 }
4108 if (str) {
4109 DPRINT("unknown floppy option [%s]\n", str);
4110
4111 DPRINT("allowed options are:");
4112 for (i = 0; i < ARRAY_SIZE(config_params); i++)
Joe Perchesb46df352010-03-10 15:20:46 -08004113 pr_cont(" %s", config_params[i].name);
4114 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115 } else
4116 DPRINT("botched floppy option\n");
Randy Dunlap31c00fc2008-11-13 21:33:24 +00004117 DPRINT("Read Documentation/blockdev/floppy.txt\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 return 0;
4119}
4120
4121static int have_no_fdc = -ENODEV;
4122
Andrew Morton9a8af6b2005-07-27 17:37:34 -07004123static ssize_t floppy_cmos_show(struct device *dev,
4124 struct device_attribute *attr, char *buf)
Hannes Reinecke94fd0db2005-07-15 10:09:25 +02004125{
Eric Miao71b3e0c2009-01-31 22:47:44 +08004126 struct platform_device *p = to_platform_device(dev);
Andrew Morton9a8af6b2005-07-27 17:37:34 -07004127 int drive;
Hannes Reinecke94fd0db2005-07-15 10:09:25 +02004128
Andrew Morton9a8af6b2005-07-27 17:37:34 -07004129 drive = p->id;
4130 return sprintf(buf, "%X\n", UDP->cmos);
Hannes Reinecke94fd0db2005-07-15 10:09:25 +02004131}
Joe Perches48c8cee2010-03-10 15:20:45 -08004132
4133DEVICE_ATTR(cmos, S_IRUGO, floppy_cmos_show, NULL);
Hannes Reinecke94fd0db2005-07-15 10:09:25 +02004134
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135static void floppy_device_release(struct device *dev)
4136{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137}
4138
Frans Popc90cd332009-07-25 22:24:54 +02004139static int floppy_resume(struct device *dev)
Ondrej Zary5e50b9e2009-06-10 12:57:09 -07004140{
4141 int fdc;
4142
4143 for (fdc = 0; fdc < N_FDC; fdc++)
4144 if (FDCS->address != -1)
Joe Perches74f63f42010-03-10 15:20:58 -08004145 user_reset_fdc(-1, FD_RESET_ALWAYS, false);
Ondrej Zary5e50b9e2009-06-10 12:57:09 -07004146
4147 return 0;
4148}
4149
Alexey Dobriyan47145212009-12-14 18:00:08 -08004150static const struct dev_pm_ops floppy_pm_ops = {
Ondrej Zary5e50b9e2009-06-10 12:57:09 -07004151 .resume = floppy_resume,
Frans Popc90cd332009-07-25 22:24:54 +02004152 .restore = floppy_resume,
4153};
4154
4155static struct platform_driver floppy_driver = {
Ondrej Zary5e50b9e2009-06-10 12:57:09 -07004156 .driver = {
Joe Perchesbb57f0c62010-03-10 15:20:50 -08004157 .name = "floppy",
4158 .pm = &floppy_pm_ops,
Ondrej Zary5e50b9e2009-06-10 12:57:09 -07004159 },
4160};
4161
Hannes Reinecke94fd0db2005-07-15 10:09:25 +02004162static struct platform_device floppy_device[N_DRIVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163
4164static struct kobject *floppy_find(dev_t dev, int *part, void *data)
4165{
4166 int drive = (*part & 3) | ((*part & 0x80) >> 5);
4167 if (drive >= N_DRIVE ||
4168 !(allowed_drive_mask & (1 << drive)) ||
4169 fdc_state[FDC(drive)].version == FDC_NONE)
4170 return NULL;
Tobias Klauser945f3902006-01-08 01:05:11 -08004171 if (((*part >> 2) & 0x1f) >= ARRAY_SIZE(floppy_type))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172 return NULL;
4173 *part = 0;
4174 return get_disk(disks[drive]);
4175}
4176
4177static int __init floppy_init(void)
4178{
4179 int i, unit, drive;
4180 int err, dr;
4181
Kumar Gala68e1ee62008-09-22 14:41:31 -07004182#if defined(CONFIG_PPC)
Olaf Heringef16b512006-08-31 21:27:41 -07004183 if (check_legacy_ioport(FDC1))
4184 return -ENODEV;
4185#endif
4186
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187 raw_cmd = NULL;
4188
4189 for (dr = 0; dr < N_DRIVE; dr++) {
4190 disks[dr] = alloc_disk(1);
4191 if (!disks[dr]) {
4192 err = -ENOMEM;
4193 goto out_put_disk;
4194 }
4195
4196 disks[dr]->major = FLOPPY_MAJOR;
4197 disks[dr]->first_minor = TOMINOR(dr);
4198 disks[dr]->fops = &floppy_fops;
4199 sprintf(disks[dr]->disk_name, "fd%d", dr);
4200
4201 init_timer(&motor_off_timer[dr]);
4202 motor_off_timer[dr].data = dr;
4203 motor_off_timer[dr].function = motor_off_callback;
4204 }
4205
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 err = register_blkdev(FLOPPY_MAJOR, "fd");
4207 if (err)
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07004208 goto out_put_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209
Ondrej Zary5e50b9e2009-06-10 12:57:09 -07004210 err = platform_driver_register(&floppy_driver);
4211 if (err)
4212 goto out_unreg_blkdev;
4213
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214 floppy_queue = blk_init_queue(do_fd_request, &floppy_lock);
4215 if (!floppy_queue) {
4216 err = -ENOMEM;
Ondrej Zary5e50b9e2009-06-10 12:57:09 -07004217 goto out_unreg_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 }
Martin K. Petersen086fa5f2010-02-26 00:20:38 -05004219 blk_queue_max_hw_sectors(floppy_queue, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220
4221 blk_register_region(MKDEV(FLOPPY_MAJOR, 0), 256, THIS_MODULE,
4222 floppy_find, NULL, NULL);
4223
4224 for (i = 0; i < 256; i++)
4225 if (ITYPE(i))
4226 floppy_sizes[i] = floppy_type[ITYPE(i)].size;
4227 else
4228 floppy_sizes[i] = MAX_DISK_SIZE << 1;
4229
Joe Perches73507e62010-03-10 15:21:03 -08004230 reschedule_timeout(MAXTIMEOUT, "floppy init");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 config_types();
4232
4233 for (i = 0; i < N_FDC; i++) {
4234 fdc = i;
Joe Perchesb87c9e02010-03-10 15:20:50 -08004235 memset(FDCS, 0, sizeof(*FDCS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236 FDCS->dtr = -1;
4237 FDCS->dor = 0x4;
4238#if defined(__sparc__) || defined(__mc68000__)
Joe Perches96534f12010-03-10 15:20:51 -08004239 /*sparcs/sun3x don't have a DOR reset which we can fall back on to */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240#ifdef __mc68000__
4241 if (MACH_IS_SUN3X)
4242#endif
4243 FDCS->version = FDC_82072A;
4244#endif
4245 }
4246
4247 use_virtual_dma = can_use_virtual_dma & 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 fdc_state[0].address = FDC1;
4249 if (fdc_state[0].address == -1) {
4250 del_timer(&fd_timeout);
4251 err = -ENODEV;
4252 goto out_unreg_region;
4253 }
4254#if N_FDC > 1
4255 fdc_state[1].address = FDC2;
4256#endif
4257
4258 fdc = 0; /* reset fdc in case of unexpected interrupt */
4259 err = floppy_grab_irq_and_dma();
4260 if (err) {
4261 del_timer(&fd_timeout);
4262 err = -EBUSY;
4263 goto out_unreg_region;
4264 }
4265
4266 /* initialise drive state */
4267 for (drive = 0; drive < N_DRIVE; drive++) {
Joe Perchesb87c9e02010-03-10 15:20:50 -08004268 memset(UDRS, 0, sizeof(*UDRS));
4269 memset(UDRWE, 0, sizeof(*UDRWE));
Joe Perchese0298532010-03-10 15:20:55 -08004270 set_bit(FD_DISK_NEWCHANGE_BIT, &UDRS->flags);
4271 set_bit(FD_DISK_CHANGED_BIT, &UDRS->flags);
4272 set_bit(FD_VERIFY_BIT, &UDRS->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273 UDRS->fd_device = -1;
4274 floppy_track_buffer = NULL;
4275 max_buffer_sectors = 0;
4276 }
4277 /*
4278 * Small 10 msec delay to let through any interrupt that
4279 * initialization might have triggered, to not
4280 * confuse detection:
4281 */
4282 msleep(10);
4283
4284 for (i = 0; i < N_FDC; i++) {
4285 fdc = i;
4286 FDCS->driver_version = FD_DRIVER_VERSION;
4287 for (unit = 0; unit < 4; unit++)
4288 FDCS->track[unit] = 0;
4289 if (FDCS->address == -1)
4290 continue;
4291 FDCS->rawcmd = 2;
Joe Perches74f63f42010-03-10 15:20:58 -08004292 if (user_reset_fdc(-1, FD_RESET_ALWAYS, false)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 /* free ioports reserved by floppy_grab_irq_and_dma() */
Philippe De Muyter5a74db02009-02-18 14:48:36 -08004294 floppy_release_regions(fdc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 FDCS->address = -1;
4296 FDCS->version = FDC_NONE;
4297 continue;
4298 }
4299 /* Try to determine the floppy controller type */
4300 FDCS->version = get_fdc_version();
4301 if (FDCS->version == FDC_NONE) {
4302 /* free ioports reserved by floppy_grab_irq_and_dma() */
Philippe De Muyter5a74db02009-02-18 14:48:36 -08004303 floppy_release_regions(fdc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 FDCS->address = -1;
4305 continue;
4306 }
4307 if (can_use_virtual_dma == 2 && FDCS->version < FDC_82072A)
4308 can_use_virtual_dma = 0;
4309
4310 have_no_fdc = 0;
4311 /* Not all FDCs seem to be able to handle the version command
4312 * properly, so force a reset for the standard FDC clones,
4313 * to avoid interrupt garbage.
4314 */
Joe Perches74f63f42010-03-10 15:20:58 -08004315 user_reset_fdc(-1, FD_RESET_ALWAYS, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 }
4317 fdc = 0;
4318 del_timer(&fd_timeout);
4319 current_drive = 0;
Joe Perches29f1c782010-03-10 15:21:00 -08004320 initialized = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 if (have_no_fdc) {
4322 DPRINT("no floppy controllers found\n");
4323 err = have_no_fdc;
4324 goto out_flush_work;
4325 }
4326
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 for (drive = 0; drive < N_DRIVE; drive++) {
4328 if (!(allowed_drive_mask & (1 << drive)))
4329 continue;
4330 if (fdc_state[FDC(drive)].version == FDC_NONE)
4331 continue;
Hannes Reinecke94fd0db2005-07-15 10:09:25 +02004332
4333 floppy_device[drive].name = floppy_device_name;
4334 floppy_device[drive].id = drive;
4335 floppy_device[drive].dev.release = floppy_device_release;
4336
4337 err = platform_device_register(&floppy_device[drive]);
4338 if (err)
4339 goto out_flush_work;
4340
Joe Perchesd7b2b2e2010-03-10 15:20:48 -08004341 err = device_create_file(&floppy_device[drive].dev,
4342 &dev_attr_cmos);
Dmitriy Monakhov4ea1b0f2007-05-08 00:25:58 -07004343 if (err)
4344 goto out_unreg_platform_dev;
4345
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346 /* to be cleaned up... */
4347 disks[drive]->private_data = (void *)(long)drive;
4348 disks[drive]->queue = floppy_queue;
4349 disks[drive]->flags |= GENHD_FL_REMOVABLE;
Hannes Reinecke94fd0db2005-07-15 10:09:25 +02004350 disks[drive]->driverfs_dev = &floppy_device[drive].dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 add_disk(disks[drive]);
4352 }
4353
4354 return 0;
4355
Dmitriy Monakhov4ea1b0f2007-05-08 00:25:58 -07004356out_unreg_platform_dev:
4357 platform_device_unregister(&floppy_device[drive]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358out_flush_work:
4359 flush_scheduled_work();
4360 if (usage_count)
4361 floppy_release_irq_and_dma();
4362out_unreg_region:
4363 blk_unregister_region(MKDEV(FLOPPY_MAJOR, 0), 256);
4364 blk_cleanup_queue(floppy_queue);
Ondrej Zary5e50b9e2009-06-10 12:57:09 -07004365out_unreg_driver:
4366 platform_driver_unregister(&floppy_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367out_unreg_blkdev:
4368 unregister_blkdev(FLOPPY_MAJOR, "fd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369out_put_disk:
4370 while (dr--) {
4371 del_timer(&motor_off_timer[dr]);
4372 put_disk(disks[dr]);
4373 }
4374 return err;
4375}
4376
4377static DEFINE_SPINLOCK(floppy_usage_lock);
4378
Philippe De Muyter5a74db02009-02-18 14:48:36 -08004379static const struct io_region {
4380 int offset;
4381 int size;
4382} io_regions[] = {
4383 { 2, 1 },
4384 /* address + 3 is sometimes reserved by pnp bios for motherboard */
4385 { 4, 2 },
4386 /* address + 6 is reserved, and may be taken by IDE.
4387 * Unfortunately, Adaptec doesn't know this :-(, */
4388 { 7, 1 },
4389};
4390
4391static void floppy_release_allocated_regions(int fdc, const struct io_region *p)
4392{
4393 while (p != io_regions) {
4394 p--;
4395 release_region(FDCS->address + p->offset, p->size);
4396 }
4397}
4398
4399#define ARRAY_END(X) (&((X)[ARRAY_SIZE(X)]))
4400
4401static int floppy_request_regions(int fdc)
4402{
4403 const struct io_region *p;
4404
4405 for (p = io_regions; p < ARRAY_END(io_regions); p++) {
Joe Perchesbb57f0c62010-03-10 15:20:50 -08004406 if (!request_region(FDCS->address + p->offset,
4407 p->size, "floppy")) {
4408 DPRINT("Floppy io-port 0x%04lx in use\n",
4409 FDCS->address + p->offset);
Philippe De Muyter5a74db02009-02-18 14:48:36 -08004410 floppy_release_allocated_regions(fdc, p);
4411 return -EBUSY;
4412 }
4413 }
4414 return 0;
4415}
4416
4417static void floppy_release_regions(int fdc)
4418{
4419 floppy_release_allocated_regions(fdc, ARRAY_END(io_regions));
4420}
4421
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422static int floppy_grab_irq_and_dma(void)
4423{
4424 unsigned long flags;
4425
4426 spin_lock_irqsave(&floppy_usage_lock, flags);
4427 if (usage_count++) {
4428 spin_unlock_irqrestore(&floppy_usage_lock, flags);
4429 return 0;
4430 }
4431 spin_unlock_irqrestore(&floppy_usage_lock, flags);
Ingo Molnar6dc659d2006-03-26 01:36:54 -08004432
4433 /*
4434 * We might have scheduled a free_irq(), wait it to
4435 * drain first:
4436 */
4437 flush_scheduled_work();
4438
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 if (fd_request_irq()) {
4440 DPRINT("Unable to grab IRQ%d for the floppy driver\n",
4441 FLOPPY_IRQ);
4442 spin_lock_irqsave(&floppy_usage_lock, flags);
4443 usage_count--;
4444 spin_unlock_irqrestore(&floppy_usage_lock, flags);
4445 return -1;
4446 }
4447 if (fd_request_dma()) {
4448 DPRINT("Unable to grab DMA%d for the floppy driver\n",
4449 FLOPPY_DMA);
Jan Beulich2e9c47c2007-10-16 23:27:32 -07004450 if (can_use_virtual_dma & 2)
4451 use_virtual_dma = can_use_virtual_dma = 1;
4452 if (!(can_use_virtual_dma & 1)) {
4453 fd_free_irq();
4454 spin_lock_irqsave(&floppy_usage_lock, flags);
4455 usage_count--;
4456 spin_unlock_irqrestore(&floppy_usage_lock, flags);
4457 return -1;
4458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 }
4460
4461 for (fdc = 0; fdc < N_FDC; fdc++) {
4462 if (FDCS->address != -1) {
Philippe De Muyter5a74db02009-02-18 14:48:36 -08004463 if (floppy_request_regions(fdc))
4464 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465 }
4466 }
4467 for (fdc = 0; fdc < N_FDC; fdc++) {
4468 if (FDCS->address != -1) {
4469 reset_fdc_info(1);
4470 fd_outb(FDCS->dor, FD_DOR);
4471 }
4472 }
4473 fdc = 0;
4474 set_dor(0, ~0, 8); /* avoid immediate interrupt */
4475
4476 for (fdc = 0; fdc < N_FDC; fdc++)
4477 if (FDCS->address != -1)
4478 fd_outb(FDCS->dor, FD_DOR);
4479 /*
Jesper Juhl06f748c2007-10-16 23:30:57 -07004480 * The driver will try and free resources and relies on us
4481 * to know if they were allocated or not.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 */
4483 fdc = 0;
4484 irqdma_allocated = 1;
4485 return 0;
Philippe De Muyter5a74db02009-02-18 14:48:36 -08004486cleanup:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487 fd_free_irq();
4488 fd_free_dma();
Philippe De Muyter5a74db02009-02-18 14:48:36 -08004489 while (--fdc >= 0)
4490 floppy_release_regions(fdc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 spin_lock_irqsave(&floppy_usage_lock, flags);
4492 usage_count--;
4493 spin_unlock_irqrestore(&floppy_usage_lock, flags);
4494 return -1;
4495}
4496
4497static void floppy_release_irq_and_dma(void)
4498{
4499 int old_fdc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500#ifndef __sparc__
4501 int drive;
4502#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503 long tmpsize;
4504 unsigned long tmpaddr;
4505 unsigned long flags;
4506
4507 spin_lock_irqsave(&floppy_usage_lock, flags);
4508 if (--usage_count) {
4509 spin_unlock_irqrestore(&floppy_usage_lock, flags);
4510 return;
4511 }
4512 spin_unlock_irqrestore(&floppy_usage_lock, flags);
4513 if (irqdma_allocated) {
4514 fd_disable_dma();
4515 fd_free_dma();
Ingo Molnar3e541a42006-07-03 00:24:23 -07004516 fd_free_irq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517 irqdma_allocated = 0;
4518 }
4519 set_dor(0, ~0, 8);
4520#if N_FDC > 1
4521 set_dor(1, ~8, 0);
4522#endif
4523 floppy_enable_hlt();
4524
4525 if (floppy_track_buffer && max_buffer_sectors) {
4526 tmpsize = max_buffer_sectors * 1024;
4527 tmpaddr = (unsigned long)floppy_track_buffer;
4528 floppy_track_buffer = NULL;
4529 max_buffer_sectors = 0;
4530 buffer_min = buffer_max = -1;
4531 fd_dma_mem_free(tmpaddr, tmpsize);
4532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533#ifndef __sparc__
4534 for (drive = 0; drive < N_FDC * 4; drive++)
4535 if (timer_pending(motor_off_timer + drive))
Joe Perchesb46df352010-03-10 15:20:46 -08004536 pr_info("motor off timer %d still active\n", drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537#endif
4538
4539 if (timer_pending(&fd_timeout))
Joe Perchesb46df352010-03-10 15:20:46 -08004540 pr_info("floppy timer still active:%s\n", timeout_message);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541 if (timer_pending(&fd_timer))
Joe Perchesb46df352010-03-10 15:20:46 -08004542 pr_info("auxiliary floppy timer still active\n");
David Howells365970a2006-11-22 14:54:49 +00004543 if (work_pending(&floppy_work))
Joe Perchesb46df352010-03-10 15:20:46 -08004544 pr_info("work still pending\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545 old_fdc = fdc;
4546 for (fdc = 0; fdc < N_FDC; fdc++)
Philippe De Muyter5a74db02009-02-18 14:48:36 -08004547 if (FDCS->address != -1)
4548 floppy_release_regions(fdc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549 fdc = old_fdc;
4550}
4551
4552#ifdef MODULE
4553
4554static char *floppy;
4555
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556static void __init parse_floppy_cfg_string(char *cfg)
4557{
4558 char *ptr;
4559
4560 while (*cfg) {
Joe Perchesbb57f0c62010-03-10 15:20:50 -08004561 ptr = cfg;
4562 while (*cfg && *cfg != ' ' && *cfg != '\t')
4563 cfg++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 if (*cfg) {
4565 *cfg = '\0';
4566 cfg++;
4567 }
4568 if (*ptr)
4569 floppy_setup(ptr);
4570 }
4571}
4572
Jon Schindler7afea3b2008-04-29 00:59:21 -07004573static int __init floppy_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574{
4575 if (floppy)
4576 parse_floppy_cfg_string(floppy);
4577 return floppy_init();
4578}
Jon Schindler7afea3b2008-04-29 00:59:21 -07004579module_init(floppy_module_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580
Jon Schindler7afea3b2008-04-29 00:59:21 -07004581static void __exit floppy_module_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582{
4583 int drive;
4584
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585 blk_unregister_region(MKDEV(FLOPPY_MAJOR, 0), 256);
4586 unregister_blkdev(FLOPPY_MAJOR, "fd");
Ondrej Zary5e50b9e2009-06-10 12:57:09 -07004587 platform_driver_unregister(&floppy_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588
4589 for (drive = 0; drive < N_DRIVE; drive++) {
4590 del_timer_sync(&motor_off_timer[drive]);
4591
4592 if ((allowed_drive_mask & (1 << drive)) &&
4593 fdc_state[FDC(drive)].version != FDC_NONE) {
4594 del_gendisk(disks[drive]);
Hannes Reinecke94fd0db2005-07-15 10:09:25 +02004595 device_remove_file(&floppy_device[drive].dev, &dev_attr_cmos);
4596 platform_device_unregister(&floppy_device[drive]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597 }
4598 put_disk(disks[drive]);
4599 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600
4601 del_timer_sync(&fd_timeout);
4602 del_timer_sync(&fd_timer);
4603 blk_cleanup_queue(floppy_queue);
4604
4605 if (usage_count)
4606 floppy_release_irq_and_dma();
4607
4608 /* eject disk, if any */
4609 fd_eject(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610}
Joe Perches48c8cee2010-03-10 15:20:45 -08004611
Jon Schindler7afea3b2008-04-29 00:59:21 -07004612module_exit(floppy_module_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613
4614module_param(floppy, charp, 0);
4615module_param(FLOPPY_IRQ, int, 0);
4616module_param(FLOPPY_DMA, int, 0);
4617MODULE_AUTHOR("Alain L. Knaff");
4618MODULE_SUPPORTED_DEVICE("fd");
4619MODULE_LICENSE("GPL");
4620
Scott James Remnant83f9ef42009-04-02 16:56:47 -07004621/* This doesn't actually get used other than for module information */
4622static const struct pnp_device_id floppy_pnpids[] = {
Joe Perches48c8cee2010-03-10 15:20:45 -08004623 {"PNP0700", 0},
4624 {}
Scott James Remnant83f9ef42009-04-02 16:56:47 -07004625};
Joe Perches48c8cee2010-03-10 15:20:45 -08004626
Scott James Remnant83f9ef42009-04-02 16:56:47 -07004627MODULE_DEVICE_TABLE(pnp, floppy_pnpids);
4628
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629#else
4630
4631__setup("floppy=", floppy_setup);
4632module_init(floppy_init)
4633#endif
4634
4635MODULE_ALIAS_BLOCKDEV_MAJOR(FLOPPY_MAJOR);