blob: 90c4038702da3606e796bc3dea012ee37eb7bcc4 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Joe Perches891eda82010-03-10 15:21:05 -0800153#define DPRINT(format, args...) \
154 pr_info("floppy%d: " format, current_drive, ##args)
155
156#define DCL_DEBUG /* debug disk change line */
Joe Perches87f530d2010-03-10 15:20:54 -0800157#ifdef DCL_DEBUG
158#define debug_dcl(test, fmt, args...) \
159 do { if ((test) & FD_DEBUG) DPRINT(fmt, ##args); } while (0)
160#else
161#define debug_dcl(test, fmt, args...) \
162 do { if (0) DPRINT(fmt, ##args); } while (0)
163#endif
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165/* do print messages for unexpected interrupts */
166static int print_unex = 1;
167#include <linux/module.h>
168#include <linux/sched.h>
169#include <linux/fs.h>
170#include <linux/kernel.h>
171#include <linux/timer.h>
172#include <linux/workqueue.h>
173#define FDPATCHES
174#include <linux/fdreg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175#include <linux/fd.h>
176#include <linux/hdreg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177#include <linux/errno.h>
178#include <linux/slab.h>
179#include <linux/mm.h>
180#include <linux/bio.h>
181#include <linux/string.h>
Marcelo Feitoza Parisi50297cb2006-03-28 01:56:44 -0800182#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183#include <linux/fcntl.h>
184#include <linux/delay.h>
185#include <linux/mc146818rtc.h> /* CMOS defines */
186#include <linux/ioport.h>
187#include <linux/interrupt.h>
188#include <linux/init.h>
Russell Kingd052d1b2005-10-29 19:07:23 +0100189#include <linux/platform_device.h>
Scott James Remnant83f9ef42009-04-02 16:56:47 -0700190#include <linux/mod_devicetable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191#include <linux/buffer_head.h> /* for invalidate_buffers() */
Jes Sorensenb1c82b52006-03-23 03:00:26 -0800192#include <linux/mutex.h>
Joe Perchesd4937542010-03-10 15:20:44 -0800193#include <linux/io.h>
194#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196/*
197 * PS/2 floppies have much slower step rates than regular floppies.
198 * It's been recommended that take about 1/4 of the default speed
199 * in some more extreme cases.
200 */
201static int slow_floppy;
202
203#include <asm/dma.h>
204#include <asm/irq.h>
205#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207static int FLOPPY_IRQ = 6;
208static int FLOPPY_DMA = 2;
209static int can_use_virtual_dma = 2;
210/* =======
211 * can use virtual DMA:
212 * 0 = use of virtual DMA disallowed by config
213 * 1 = use of virtual DMA prescribed by config
214 * 2 = no virtual DMA preference configured. By default try hard DMA,
215 * but fall back on virtual DMA when not enough memory available
216 */
217
218static int use_virtual_dma;
219/* =======
220 * use virtual DMA
221 * 0 using hard DMA
222 * 1 using virtual DMA
223 * This variable is set to virtual when a DMA mem problem arises, and
224 * reset back in floppy_grab_irq_and_dma.
225 * It is not safe to reset it in other circumstances, because the floppy
226 * driver may have several buffers in use at once, and we do currently not
227 * record each buffers capabilities
228 */
229
230static DEFINE_SPINLOCK(floppy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232static unsigned short virtual_dma_port = 0x3f0;
David Howells7d12e782006-10-05 14:55:46 +0100233irqreturn_t floppy_interrupt(int irq, void *dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234static int set_dor(int fdc, char mask, char data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236#define K_64 0x10000 /* 64KB */
237
238/* the following is the mask of allowed drives. By default units 2 and
239 * 3 of both floppy controllers are disabled, because switching on the
240 * motor of these drives causes system hangs on some PCI computers. drive
241 * 0 is the low bit (0x1), and drive 7 is the high bit (0x80). Bits are on if
242 * a drive is allowed.
243 *
244 * NOTE: This must come before we include the arch floppy header because
245 * some ports reference this variable from there. -DaveM
246 */
247
248static int allowed_drive_mask = 0x33;
249
250#include <asm/floppy.h>
251
252static int irqdma_allocated;
253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254#include <linux/blkdev.h>
255#include <linux/blkpg.h>
256#include <linux/cdrom.h> /* for the compatibility eject ioctl */
257#include <linux/completion.h>
258
259static struct request *current_req;
260static struct request_queue *floppy_queue;
Joe Perches48c8cee2010-03-10 15:20:45 -0800261static void do_fd_request(struct request_queue *q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263#ifndef fd_get_dma_residue
264#define fd_get_dma_residue() get_dma_residue(FLOPPY_DMA)
265#endif
266
267/* Dma Memory related stuff */
268
269#ifndef fd_dma_mem_free
270#define fd_dma_mem_free(addr, size) free_pages(addr, get_order(size))
271#endif
272
273#ifndef fd_dma_mem_alloc
Joe Perches48c8cee2010-03-10 15:20:45 -0800274#define fd_dma_mem_alloc(size) __get_dma_pages(GFP_KERNEL, get_order(size))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275#endif
276
277static inline void fallback_on_nodma_alloc(char **addr, size_t l)
278{
279#ifdef FLOPPY_CAN_FALLBACK_ON_NODMA
280 if (*addr)
281 return; /* we have the memory */
282 if (can_use_virtual_dma != 2)
283 return; /* no fallback allowed */
Joe Perchesb46df352010-03-10 15:20:46 -0800284 pr_info("DMA memory shortage. Temporarily falling back on virtual DMA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 *addr = (char *)nodma_mem_alloc(l);
286#else
287 return;
288#endif
289}
290
291/* End dma memory related stuff */
292
293static unsigned long fake_change;
Joe Perches29f1c782010-03-10 15:21:00 -0800294static bool initialized;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Joe Perches48c8cee2010-03-10 15:20:45 -0800296#define ITYPE(x) (((x) >> 2) & 0x1f)
297#define TOMINOR(x) ((x & 3) | ((x & 4) << 5))
298#define UNIT(x) ((x) & 0x03) /* drive on fdc */
299#define FDC(x) (((x) & 0x04) >> 2) /* fdc of drive */
Jesper Juhl06f748c2007-10-16 23:30:57 -0700300 /* reverse mapping from unit and fdc to drive */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301#define REVDRIVE(fdc, unit) ((unit) + ((fdc) << 2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Joe Perches48c8cee2010-03-10 15:20:45 -0800303#define DP (&drive_params[current_drive])
304#define DRS (&drive_state[current_drive])
305#define DRWE (&write_errors[current_drive])
306#define FDCS (&fdc_state[fdc])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Joe Perches48c8cee2010-03-10 15:20:45 -0800308#define UDP (&drive_params[drive])
309#define UDRS (&drive_state[drive])
310#define UDRWE (&write_errors[drive])
311#define UFDCS (&fdc_state[FDC(drive)])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Joe Perches48c8cee2010-03-10 15:20:45 -0800313#define PH_HEAD(floppy, head) (((((floppy)->stretch & 2) >> 1) ^ head) << 2)
314#define STRETCH(floppy) ((floppy)->stretch & FD_STRETCH)
315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316/* read/write */
Joe Perches48c8cee2010-03-10 15:20:45 -0800317#define COMMAND (raw_cmd->cmd[0])
318#define DR_SELECT (raw_cmd->cmd[1])
319#define TRACK (raw_cmd->cmd[2])
320#define HEAD (raw_cmd->cmd[3])
321#define SECTOR (raw_cmd->cmd[4])
322#define SIZECODE (raw_cmd->cmd[5])
323#define SECT_PER_TRACK (raw_cmd->cmd[6])
324#define GAP (raw_cmd->cmd[7])
325#define SIZECODE2 (raw_cmd->cmd[8])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326#define NR_RW 9
327
328/* format */
Joe Perches48c8cee2010-03-10 15:20:45 -0800329#define F_SIZECODE (raw_cmd->cmd[2])
330#define F_SECT_PER_TRACK (raw_cmd->cmd[3])
331#define F_GAP (raw_cmd->cmd[4])
332#define F_FILL (raw_cmd->cmd[5])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333#define NR_F 6
334
335/*
Joe Perches48c8cee2010-03-10 15:20:45 -0800336 * Maximum disk size (in kilobytes).
337 * This default is used whenever the current disk size is unknown.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 * [Now it is rather a minimum]
339 */
340#define MAX_DISK_SIZE 4 /* 3984 */
341
342/*
343 * globals used by 'result()'
344 */
345#define MAX_REPLIES 16
346static unsigned char reply_buffer[MAX_REPLIES];
Joe Perches891eda82010-03-10 15:21:05 -0800347static int inr; /* size of reply buffer, when called from interrupt */
Joe Perches48c8cee2010-03-10 15:20:45 -0800348#define ST0 (reply_buffer[0])
349#define ST1 (reply_buffer[1])
350#define ST2 (reply_buffer[2])
351#define ST3 (reply_buffer[0]) /* result of GETSTATUS */
352#define R_TRACK (reply_buffer[3])
353#define R_HEAD (reply_buffer[4])
354#define R_SECTOR (reply_buffer[5])
355#define R_SIZECODE (reply_buffer[6])
356
357#define SEL_DLY (2 * HZ / 100)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359/*
360 * this struct defines the different floppy drive types.
361 */
362static struct {
363 struct floppy_drive_params params;
364 const char *name; /* name printed while booting */
365} default_drive_params[] = {
366/* NOTE: the time values in jiffies should be in msec!
367 CMOS drive type
368 | Maximum data rate supported by drive type
369 | | Head load time, msec
370 | | | Head unload time, msec (not used)
371 | | | | Step rate interval, usec
372 | | | | | Time needed for spinup time (jiffies)
373 | | | | | | Timeout for spinning down (jiffies)
374 | | | | | | | Spindown offset (where disk stops)
375 | | | | | | | | Select delay
376 | | | | | | | | | RPS
377 | | | | | | | | | | Max number of tracks
378 | | | | | | | | | | | Interrupt timeout
379 | | | | | | | | | | | | Max nonintlv. sectors
380 | | | | | | | | | | | | | -Max Errors- flags */
381{{0, 500, 16, 16, 8000, 1*HZ, 3*HZ, 0, SEL_DLY, 5, 80, 3*HZ, 20, {3,1,2,0,2}, 0,
382 0, { 7, 4, 8, 2, 1, 5, 3,10}, 3*HZ/2, 0 }, "unknown" },
383
384{{1, 300, 16, 16, 8000, 1*HZ, 3*HZ, 0, SEL_DLY, 5, 40, 3*HZ, 17, {3,1,2,0,2}, 0,
385 0, { 1, 0, 0, 0, 0, 0, 0, 0}, 3*HZ/2, 1 }, "360K PC" }, /*5 1/4 360 KB PC*/
386
387{{2, 500, 16, 16, 6000, 4*HZ/10, 3*HZ, 14, SEL_DLY, 6, 83, 3*HZ, 17, {3,1,2,0,2}, 0,
388 0, { 2, 5, 6,23,10,20,12, 0}, 3*HZ/2, 2 }, "1.2M" }, /*5 1/4 HD AT*/
389
390{{3, 250, 16, 16, 3000, 1*HZ, 3*HZ, 0, SEL_DLY, 5, 83, 3*HZ, 20, {3,1,2,0,2}, 0,
391 0, { 4,22,21,30, 3, 0, 0, 0}, 3*HZ/2, 4 }, "720k" }, /*3 1/2 DD*/
392
393{{4, 500, 16, 16, 4000, 4*HZ/10, 3*HZ, 10, SEL_DLY, 5, 83, 3*HZ, 20, {3,1,2,0,2}, 0,
394 0, { 7, 4,25,22,31,21,29,11}, 3*HZ/2, 7 }, "1.44M" }, /*3 1/2 HD*/
395
396{{5, 1000, 15, 8, 3000, 4*HZ/10, 3*HZ, 10, SEL_DLY, 5, 83, 3*HZ, 40, {3,1,2,0,2}, 0,
397 0, { 7, 8, 4,25,28,22,31,21}, 3*HZ/2, 8 }, "2.88M AMI BIOS" }, /*3 1/2 ED*/
398
399{{6, 1000, 15, 8, 3000, 4*HZ/10, 3*HZ, 10, SEL_DLY, 5, 83, 3*HZ, 40, {3,1,2,0,2}, 0,
400 0, { 7, 8, 4,25,28,22,31,21}, 3*HZ/2, 8 }, "2.88M" } /*3 1/2 ED*/
401/* | --autodetected formats--- | | |
402 * read_track | | Name printed when booting
403 * | Native format
404 * Frequency of disk change checks */
405};
406
407static struct floppy_drive_params drive_params[N_DRIVE];
408static struct floppy_drive_struct drive_state[N_DRIVE];
409static struct floppy_write_errors write_errors[N_DRIVE];
410static struct timer_list motor_off_timer[N_DRIVE];
411static struct gendisk *disks[N_DRIVE];
412static struct block_device *opened_bdev[N_DRIVE];
Jes Sorensenb1c82b52006-03-23 03:00:26 -0800413static DEFINE_MUTEX(open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414static struct floppy_raw_cmd *raw_cmd, default_raw_cmd;
415
416/*
417 * This struct defines the different floppy types.
418 *
419 * Bit 0 of 'stretch' tells if the tracks need to be doubled for some
420 * types (e.g. 360kB diskette in 1.2MB drive, etc.). Bit 1 of 'stretch'
421 * tells if the disk is in Commodore 1581 format, which means side 0 sectors
422 * are located on side 1 of the disk but with a side 0 ID, and vice-versa.
423 * This is the same as the Sharp MZ-80 5.25" CP/M disk format, except that the
424 * 1581's logical side 0 is on physical side 1, whereas the Sharp's logical
425 * side 0 is on physical side 0 (but with the misnamed sector IDs).
426 * 'stretch' should probably be renamed to something more general, like
Keith Wansbrough9e491842008-09-22 14:57:17 -0700427 * 'options'.
428 *
429 * Bits 2 through 9 of 'stretch' tell the number of the first sector.
430 * The LSB (bit 2) is flipped. For most disks, the first sector
431 * is 1 (represented by 0x00<<2). For some CP/M and music sampler
432 * disks (such as Ensoniq EPS 16plus) it is 0 (represented as 0x01<<2).
433 * For Amstrad CPC disks it is 0xC1 (represented as 0xC0<<2).
434 *
435 * Other parameters should be self-explanatory (see also setfdprm(8)).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 */
437/*
438 Size
439 | Sectors per track
440 | | Head
441 | | | Tracks
442 | | | | Stretch
443 | | | | | Gap 1 size
444 | | | | | | Data rate, | 0x40 for perp
445 | | | | | | | Spec1 (stepping rate, head unload
446 | | | | | | | | /fmt gap (gap2) */
447static struct floppy_struct floppy_type[32] = {
448 { 0, 0,0, 0,0,0x00,0x00,0x00,0x00,NULL }, /* 0 no testing */
449 { 720, 9,2,40,0,0x2A,0x02,0xDF,0x50,"d360" }, /* 1 360KB PC */
450 { 2400,15,2,80,0,0x1B,0x00,0xDF,0x54,"h1200" }, /* 2 1.2MB AT */
451 { 720, 9,1,80,0,0x2A,0x02,0xDF,0x50,"D360" }, /* 3 360KB SS 3.5" */
452 { 1440, 9,2,80,0,0x2A,0x02,0xDF,0x50,"D720" }, /* 4 720KB 3.5" */
453 { 720, 9,2,40,1,0x23,0x01,0xDF,0x50,"h360" }, /* 5 360KB AT */
454 { 1440, 9,2,80,0,0x23,0x01,0xDF,0x50,"h720" }, /* 6 720KB AT */
455 { 2880,18,2,80,0,0x1B,0x00,0xCF,0x6C,"H1440" }, /* 7 1.44MB 3.5" */
456 { 5760,36,2,80,0,0x1B,0x43,0xAF,0x54,"E2880" }, /* 8 2.88MB 3.5" */
457 { 6240,39,2,80,0,0x1B,0x43,0xAF,0x28,"E3120" }, /* 9 3.12MB 3.5" */
458
459 { 2880,18,2,80,0,0x25,0x00,0xDF,0x02,"h1440" }, /* 10 1.44MB 5.25" */
460 { 3360,21,2,80,0,0x1C,0x00,0xCF,0x0C,"H1680" }, /* 11 1.68MB 3.5" */
461 { 820,10,2,41,1,0x25,0x01,0xDF,0x2E,"h410" }, /* 12 410KB 5.25" */
462 { 1640,10,2,82,0,0x25,0x02,0xDF,0x2E,"H820" }, /* 13 820KB 3.5" */
463 { 2952,18,2,82,0,0x25,0x00,0xDF,0x02,"h1476" }, /* 14 1.48MB 5.25" */
464 { 3444,21,2,82,0,0x25,0x00,0xDF,0x0C,"H1722" }, /* 15 1.72MB 3.5" */
465 { 840,10,2,42,1,0x25,0x01,0xDF,0x2E,"h420" }, /* 16 420KB 5.25" */
466 { 1660,10,2,83,0,0x25,0x02,0xDF,0x2E,"H830" }, /* 17 830KB 3.5" */
467 { 2988,18,2,83,0,0x25,0x00,0xDF,0x02,"h1494" }, /* 18 1.49MB 5.25" */
468 { 3486,21,2,83,0,0x25,0x00,0xDF,0x0C,"H1743" }, /* 19 1.74 MB 3.5" */
469
470 { 1760,11,2,80,0,0x1C,0x09,0xCF,0x00,"h880" }, /* 20 880KB 5.25" */
471 { 2080,13,2,80,0,0x1C,0x01,0xCF,0x00,"D1040" }, /* 21 1.04MB 3.5" */
472 { 2240,14,2,80,0,0x1C,0x19,0xCF,0x00,"D1120" }, /* 22 1.12MB 3.5" */
473 { 3200,20,2,80,0,0x1C,0x20,0xCF,0x2C,"h1600" }, /* 23 1.6MB 5.25" */
474 { 3520,22,2,80,0,0x1C,0x08,0xCF,0x2e,"H1760" }, /* 24 1.76MB 3.5" */
475 { 3840,24,2,80,0,0x1C,0x20,0xCF,0x00,"H1920" }, /* 25 1.92MB 3.5" */
476 { 6400,40,2,80,0,0x25,0x5B,0xCF,0x00,"E3200" }, /* 26 3.20MB 3.5" */
477 { 7040,44,2,80,0,0x25,0x5B,0xCF,0x00,"E3520" }, /* 27 3.52MB 3.5" */
478 { 7680,48,2,80,0,0x25,0x63,0xCF,0x00,"E3840" }, /* 28 3.84MB 3.5" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 { 3680,23,2,80,0,0x1C,0x10,0xCF,0x00,"H1840" }, /* 29 1.84MB 3.5" */
Jesper Juhl06f748c2007-10-16 23:30:57 -0700480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 { 1600,10,2,80,0,0x25,0x02,0xDF,0x2E,"D800" }, /* 30 800KB 3.5" */
482 { 3200,20,2,80,0,0x1C,0x00,0xCF,0x2C,"H1600" }, /* 31 1.6MB 3.5" */
483};
484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485#define SECTSIZE (_FD_SECTSIZE(*floppy))
486
487/* Auto-detection: Disk type used until the next media change occurs. */
488static struct floppy_struct *current_type[N_DRIVE];
489
490/*
491 * User-provided type information. current_type points to
492 * the respective entry of this array.
493 */
494static struct floppy_struct user_params[N_DRIVE];
495
496static sector_t floppy_sizes[256];
497
Hannes Reinecke94fd0db2005-07-15 10:09:25 +0200498static char floppy_device_name[] = "floppy";
499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500/*
501 * The driver is trying to determine the correct media format
502 * while probing is set. rw_interrupt() clears it after a
503 * successful access.
504 */
505static int probing;
506
507/* Synchronization of FDC access. */
Joe Perches48c8cee2010-03-10 15:20:45 -0800508#define FD_COMMAND_NONE -1
509#define FD_COMMAND_ERROR 2
510#define FD_COMMAND_OKAY 3
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
512static volatile int command_status = FD_COMMAND_NONE;
513static unsigned long fdc_busy;
514static DECLARE_WAIT_QUEUE_HEAD(fdc_wait);
515static DECLARE_WAIT_QUEUE_HEAD(command_done);
516
517#define NO_SIGNAL (!interruptible || !signal_pending(current))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519/* Errors during formatting are counted here. */
520static int format_errors;
521
522/* Format request descriptor. */
523static struct format_descr format_req;
524
525/*
526 * Rate is 0 for 500kb/s, 1 for 300kbps, 2 for 250kbps
527 * Spec1 is 0xSH, where S is stepping rate (F=1ms, E=2ms, D=3ms etc),
528 * H is head unload time (1=16ms, 2=32ms, etc)
529 */
530
531/*
532 * Track buffer
533 * Because these are written to by the DMA controller, they must
534 * not contain a 64k byte boundary crossing, or data will be
535 * corrupted/lost.
536 */
537static char *floppy_track_buffer;
538static int max_buffer_sectors;
539
540static int *errors;
Jesper Juhl06f748c2007-10-16 23:30:57 -0700541typedef void (*done_f)(int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542static struct cont_t {
Joe Perches48c8cee2010-03-10 15:20:45 -0800543 void (*interrupt)(void);
544 /* this is called after the interrupt of the
545 * main command */
Jesper Juhl06f748c2007-10-16 23:30:57 -0700546 void (*redo)(void); /* this is called to retry the operation */
547 void (*error)(void); /* this is called to tally an error */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 done_f done; /* this is called to say if the operation has
549 * succeeded/failed */
550} *cont;
551
552static void floppy_ready(void);
553static void floppy_start(void);
554static void process_fd_request(void);
555static void recalibrate_floppy(void);
556static void floppy_shutdown(unsigned long);
557
Philippe De Muyter5a74db02009-02-18 14:48:36 -0800558static int floppy_request_regions(int);
559static void floppy_release_regions(int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560static int floppy_grab_irq_and_dma(void);
561static void floppy_release_irq_and_dma(void);
562
563/*
564 * The "reset" variable should be tested whenever an interrupt is scheduled,
565 * after the commands have been sent. This is to ensure that the driver doesn't
566 * get wedged when the interrupt doesn't come because of a failed command.
567 * reset doesn't need to be tested before sending commands, because
568 * output_byte is automatically disabled when reset is set.
569 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570static void reset_fdc(void);
571
572/*
573 * These are global variables, as that's the easiest way to give
574 * information to interrupts. They are the data used for the current
575 * request.
576 */
Joe Perches48c8cee2010-03-10 15:20:45 -0800577#define NO_TRACK -1
578#define NEED_1_RECAL -2
579#define NEED_2_RECAL -3
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581static int usage_count;
582
583/* buffer related variables */
584static int buffer_track = -1;
585static int buffer_drive = -1;
586static int buffer_min = -1;
587static int buffer_max = -1;
588
589/* fdc related variables, should end up in a struct */
590static struct floppy_fdc_state fdc_state[N_FDC];
591static int fdc; /* current fdc */
592
593static struct floppy_struct *_floppy = floppy_type;
594static unsigned char current_drive;
595static long current_count_sectors;
596static unsigned char fsector_t; /* sector in track */
597static unsigned char in_sector_offset; /* offset within physical sector,
598 * expressed in units of 512 bytes */
599
600#ifndef fd_eject
601static inline int fd_eject(int drive)
602{
603 return -EINVAL;
604}
605#endif
606
607/*
608 * Debugging
609 * =========
610 */
611#ifdef DEBUGT
612static long unsigned debugtimer;
613
614static inline void set_debugt(void)
615{
616 debugtimer = jiffies;
617}
618
Joe Perchesded28632010-03-10 15:21:09 -0800619static inline void debugt(const char *func, const char *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
621 if (DP->flags & DEBUGT)
Joe Perchesded28632010-03-10 15:21:09 -0800622 pr_info("%s:%s dtime=%lu\n", func, msg, jiffies - debugtimer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623}
624#else
625static inline void set_debugt(void) { }
Joe Perchesded28632010-03-10 15:21:09 -0800626static inline void debugt(const char *func, const char *msg) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627#endif /* DEBUGT */
628
Joe Perchesa0a52d62010-03-10 15:20:52 -0800629typedef void (*timeout_fn)(unsigned long);
Ingo Molnar8d06afa2005-09-09 13:10:40 -0700630static DEFINE_TIMER(fd_timeout, floppy_shutdown, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632static const char *timeout_message;
633
Joe Perches275176b2010-03-10 15:21:06 -0800634static void is_alive(const char *func, const char *message)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
636 /* this routine checks whether the floppy driver is "alive" */
Joe Perchesc5297302010-03-10 15:20:58 -0800637 if (test_bit(0, &fdc_busy) && command_status < 2 &&
638 !timer_pending(&fd_timeout)) {
Joe Perches275176b2010-03-10 15:21:06 -0800639 DPRINT("%s: timeout handler died. %s\n", func, message);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 }
641}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Joe Perches48c8cee2010-03-10 15:20:45 -0800643static void (*do_floppy)(void) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645#define OLOGSIZE 20
646
Joe Perches48c8cee2010-03-10 15:20:45 -0800647static void (*lasthandler)(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648static unsigned long interruptjiffies;
649static unsigned long resultjiffies;
650static int resultsize;
651static unsigned long lastredo;
652
653static struct output_log {
654 unsigned char data;
655 unsigned char status;
656 unsigned long jiffies;
657} output_log[OLOGSIZE];
658
659static int output_log_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661#define current_reqD -1
662#define MAXTIMEOUT -2
663
Joe Perches73507e62010-03-10 15:21:03 -0800664static void __reschedule_timeout(int drive, const char *message)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
666 if (drive == current_reqD)
667 drive = current_drive;
668 del_timer(&fd_timeout);
Eric Sesterhenn / Snakebyte4acb3e22007-05-23 13:58:15 -0700669 if (drive < 0 || drive >= N_DRIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 fd_timeout.expires = jiffies + 20UL * HZ;
671 drive = 0;
672 } else
673 fd_timeout.expires = jiffies + UDP->timeout;
674 add_timer(&fd_timeout);
Joe Perchesa81ee542010-03-10 15:20:46 -0800675 if (UDP->flags & FD_DEBUG)
Joe Perches73507e62010-03-10 15:21:03 -0800676 DPRINT("reschedule timeout %s\n", message);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 timeout_message = message;
678}
679
Joe Perches73507e62010-03-10 15:21:03 -0800680static void reschedule_timeout(int drive, const char *message)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
682 unsigned long flags;
683
684 spin_lock_irqsave(&floppy_lock, flags);
Joe Perches73507e62010-03-10 15:21:03 -0800685 __reschedule_timeout(drive, message);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 spin_unlock_irqrestore(&floppy_lock, flags);
687}
688
Joe Perches48c8cee2010-03-10 15:20:45 -0800689#define INFBOUND(a, b) (a) = max_t(int, a, b)
690#define SUPBOUND(a, b) (a) = min_t(int, a, b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692/*
693 * Bottom half floppy driver.
694 * ==========================
695 *
696 * This part of the file contains the code talking directly to the hardware,
697 * and also the main service loop (seek-configure-spinup-command)
698 */
699
700/*
701 * disk change.
702 * This routine is responsible for maintaining the FD_DISK_CHANGE flag,
703 * and the last_checked date.
704 *
705 * last_checked is the date of the last check which showed 'no disk change'
706 * FD_DISK_CHANGE is set under two conditions:
707 * 1. The floppy has been changed after some i/o to that floppy already
708 * took place.
709 * 2. No floppy disk is in the drive. This is done in order to ensure that
710 * requests are quickly flushed in case there is no disk in the drive. It
711 * follows that FD_DISK_CHANGE can only be cleared if there is a disk in
712 * the drive.
713 *
714 * For 1., maxblock is observed. Maxblock is 0 if no i/o has taken place yet.
715 * For 2., FD_DISK_NEWCHANGE is watched. FD_DISK_NEWCHANGE is cleared on
716 * each seek. If a disk is present, the disk change line should also be
717 * cleared on each seek. Thus, if FD_DISK_NEWCHANGE is clear, but the disk
718 * change line is set, this means either that no disk is in the drive, or
719 * that it has been removed since the last seek.
720 *
721 * This means that we really have a third possibility too:
722 * The floppy has been changed after the last seek.
723 */
724
725static int disk_change(int drive)
726{
727 int fdc = FDC(drive);
Jesper Juhl06f748c2007-10-16 23:30:57 -0700728
Marcelo Feitoza Parisi50297cb2006-03-28 01:56:44 -0800729 if (time_before(jiffies, UDRS->select_date + UDP->select_delay))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 DPRINT("WARNING disk change called early\n");
731 if (!(FDCS->dor & (0x10 << UNIT(drive))) ||
732 (FDCS->dor & 3) != UNIT(drive) || fdc != FDC(drive)) {
733 DPRINT("probing disk change on unselected drive\n");
734 DPRINT("drive=%d fdc=%d dor=%x\n", drive, FDC(drive),
735 (unsigned int)FDCS->dor);
736 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Joe Perches87f530d2010-03-10 15:20:54 -0800738 debug_dcl(UDP->flags,
739 "checking disk change line for drive %d\n", drive);
740 debug_dcl(UDP->flags, "jiffies=%lu\n", jiffies);
741 debug_dcl(UDP->flags, "disk change line=%x\n", fd_inb(FD_DIR) & 0x80);
742 debug_dcl(UDP->flags, "flags=%lx\n", UDRS->flags);
743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 if (UDP->flags & FD_BROKEN_DCL)
Joe Perchese0298532010-03-10 15:20:55 -0800745 return test_bit(FD_DISK_CHANGED_BIT, &UDRS->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 if ((fd_inb(FD_DIR) ^ UDP->flags) & 0x80) {
Joe Perchese0298532010-03-10 15:20:55 -0800747 set_bit(FD_VERIFY_BIT, &UDRS->flags);
748 /* verify write protection */
749
750 if (UDRS->maxblock) /* mark it changed */
751 set_bit(FD_DISK_CHANGED_BIT, &UDRS->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753 /* invalidate its geometry */
754 if (UDRS->keep_data >= 0) {
755 if ((UDP->flags & FTD_MSG) &&
756 current_type[drive] != NULL)
Joe Perches891eda82010-03-10 15:21:05 -0800757 DPRINT("Disk type is undefined after disk change\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 current_type[drive] = NULL;
759 floppy_sizes[TOMINOR(drive)] = MAX_DISK_SIZE << 1;
760 }
761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 return 1;
763 } else {
764 UDRS->last_checked = jiffies;
Joe Perchese0298532010-03-10 15:20:55 -0800765 clear_bit(FD_DISK_NEWCHANGE_BIT, &UDRS->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 }
767 return 0;
768}
769
770static inline int is_selected(int dor, int unit)
771{
772 return ((dor & (0x10 << unit)) && (dor & 3) == unit);
773}
774
Joe Perches57584c52010-03-10 15:21:00 -0800775static bool is_ready_state(int status)
776{
777 int state = status & (STATUS_READY | STATUS_DIR | STATUS_DMA);
778 return state == STATUS_READY;
779}
780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781static int set_dor(int fdc, char mask, char data)
782{
Jesper Juhlfdc1ca82007-10-16 23:30:58 -0700783 unsigned char unit;
784 unsigned char drive;
785 unsigned char newdor;
786 unsigned char olddor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
788 if (FDCS->address == -1)
789 return -1;
790
791 olddor = FDCS->dor;
792 newdor = (olddor & mask) | data;
793 if (newdor != olddor) {
794 unit = olddor & 0x3;
795 if (is_selected(olddor, unit) && !is_selected(newdor, unit)) {
796 drive = REVDRIVE(fdc, unit);
Joe Perches87f530d2010-03-10 15:20:54 -0800797 debug_dcl(UDP->flags,
798 "calling disk change from set_dor\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 disk_change(drive);
800 }
801 FDCS->dor = newdor;
802 fd_outb(newdor, FD_DOR);
803
804 unit = newdor & 0x3;
805 if (!is_selected(olddor, unit) && is_selected(newdor, unit)) {
806 drive = REVDRIVE(fdc, unit);
807 UDRS->select_date = jiffies;
808 }
809 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 return olddor;
811}
812
813static void twaddle(void)
814{
815 if (DP->select_delay)
816 return;
817 fd_outb(FDCS->dor & ~(0x10 << UNIT(current_drive)), FD_DOR);
818 fd_outb(FDCS->dor, FD_DOR);
819 DRS->select_date = jiffies;
820}
821
Joe Perches57584c52010-03-10 15:21:00 -0800822/*
823 * Reset all driver information about the current fdc.
824 * This is needed after a reset, and after a raw command.
825 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826static void reset_fdc_info(int mode)
827{
828 int drive;
829
830 FDCS->spec1 = FDCS->spec2 = -1;
831 FDCS->need_configure = 1;
832 FDCS->perp_mode = 1;
833 FDCS->rawcmd = 0;
834 for (drive = 0; drive < N_DRIVE; drive++)
835 if (FDC(drive) == fdc && (mode || UDRS->track != NEED_1_RECAL))
836 UDRS->track = NEED_2_RECAL;
837}
838
839/* selects the fdc and drive, and enables the fdc's input/dma. */
840static void set_fdc(int drive)
841{
842 if (drive >= 0 && drive < N_DRIVE) {
843 fdc = FDC(drive);
844 current_drive = drive;
845 }
846 if (fdc != 1 && fdc != 0) {
Joe Perchesb46df352010-03-10 15:20:46 -0800847 pr_info("bad fdc value\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 return;
849 }
850 set_dor(fdc, ~0, 8);
851#if N_FDC > 1
852 set_dor(1 - fdc, ~8, 0);
853#endif
854 if (FDCS->rawcmd == 2)
855 reset_fdc_info(1);
856 if (fd_inb(FD_STATUS) != STATUS_READY)
857 FDCS->reset = 1;
858}
859
860/* locks the driver */
Joe Perches74f63f42010-03-10 15:20:58 -0800861static int _lock_fdc(int drive, bool interruptible, int line)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
863 if (!usage_count) {
Joe Perchesb46df352010-03-10 15:20:46 -0800864 pr_err("Trying to lock fdc while usage count=0 at line %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 line);
866 return -1;
867 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
869 if (test_and_set_bit(0, &fdc_busy)) {
870 DECLARE_WAITQUEUE(wait, current);
871 add_wait_queue(&fdc_wait, &wait);
872
873 for (;;) {
874 set_current_state(TASK_INTERRUPTIBLE);
875
876 if (!test_and_set_bit(0, &fdc_busy))
877 break;
878
879 schedule();
880
881 if (!NO_SIGNAL) {
882 remove_wait_queue(&fdc_wait, &wait);
883 return -EINTR;
884 }
885 }
886
887 set_current_state(TASK_RUNNING);
888 remove_wait_queue(&fdc_wait, &wait);
Ingo Molnar3e541a42006-07-03 00:24:23 -0700889 flush_scheduled_work();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 }
891 command_status = FD_COMMAND_NONE;
892
Joe Perches73507e62010-03-10 15:21:03 -0800893 __reschedule_timeout(drive, "lock fdc");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 set_fdc(drive);
895 return 0;
896}
897
Joe Perches48c8cee2010-03-10 15:20:45 -0800898#define lock_fdc(drive, interruptible) \
899 _lock_fdc(drive, interruptible, __LINE__)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901/* unlocks the driver */
902static inline void unlock_fdc(void)
903{
904 unsigned long flags;
905
906 raw_cmd = NULL;
907 if (!test_bit(0, &fdc_busy))
908 DPRINT("FDC access conflict!\n");
909
910 if (do_floppy)
Joe Perches1ebddd82010-03-10 15:21:07 -0800911 DPRINT("device interrupt still active at FDC release: %pf!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 do_floppy);
913 command_status = FD_COMMAND_NONE;
914 spin_lock_irqsave(&floppy_lock, flags);
915 del_timer(&fd_timeout);
916 cont = NULL;
917 clear_bit(0, &fdc_busy);
Tejun Heo9934c8c2009-05-08 11:54:16 +0900918 if (current_req || blk_peek_request(floppy_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 do_fd_request(floppy_queue);
920 spin_unlock_irqrestore(&floppy_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 wake_up(&fdc_wait);
922}
923
924/* switches the motor off after a given timeout */
925static void motor_off_callback(unsigned long nr)
926{
927 unsigned char mask = ~(0x10 << UNIT(nr));
928
929 set_dor(FDC(nr), mask, 0);
930}
931
932/* schedules motor off */
933static void floppy_off(unsigned int drive)
934{
935 unsigned long volatile delta;
Jesper Juhlfdc1ca82007-10-16 23:30:58 -0700936 int fdc = FDC(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
938 if (!(FDCS->dor & (0x10 << UNIT(drive))))
939 return;
940
941 del_timer(motor_off_timer + drive);
942
943 /* make spindle stop in a position which minimizes spinup time
944 * next time */
945 if (UDP->rps) {
946 delta = jiffies - UDRS->first_read_date + HZ -
947 UDP->spindown_offset;
948 delta = ((delta * UDP->rps) % HZ) / UDP->rps;
949 motor_off_timer[drive].expires =
950 jiffies + UDP->spindown - delta;
951 }
952 add_timer(motor_off_timer + drive);
953}
954
955/*
956 * cycle through all N_DRIVE floppy drives, for disk change testing.
957 * stopping at current drive. This is done before any long operation, to
958 * be sure to have up to date disk change information.
959 */
960static void scandrives(void)
961{
Jesper Juhl06f748c2007-10-16 23:30:57 -0700962 int i;
963 int drive;
964 int saved_drive;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
966 if (DP->select_delay)
967 return;
968
969 saved_drive = current_drive;
970 for (i = 0; i < N_DRIVE; i++) {
971 drive = (saved_drive + i + 1) % N_DRIVE;
972 if (UDRS->fd_ref == 0 || UDP->select_delay != 0)
973 continue; /* skip closed drives */
974 set_fdc(drive);
975 if (!(set_dor(fdc, ~3, UNIT(drive) | (0x10 << UNIT(drive))) &
976 (0x10 << UNIT(drive))))
977 /* switch the motor off again, if it was off to
978 * begin with */
979 set_dor(fdc, ~(0x10 << UNIT(drive)), 0);
980 }
981 set_fdc(saved_drive);
982}
983
984static void empty(void)
985{
986}
987
David Howells65f27f32006-11-22 14:55:48 +0000988static DECLARE_WORK(floppy_work, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
Joe Perches48c8cee2010-03-10 15:20:45 -0800990static void schedule_bh(void (*handler)(void))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991{
David Howells65f27f32006-11-22 14:55:48 +0000992 PREPARE_WORK(&floppy_work, (work_func_t)handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 schedule_work(&floppy_work);
994}
995
Ingo Molnar8d06afa2005-09-09 13:10:40 -0700996static DEFINE_TIMER(fd_timer, NULL, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
998static void cancel_activity(void)
999{
1000 unsigned long flags;
1001
1002 spin_lock_irqsave(&floppy_lock, flags);
1003 do_floppy = NULL;
David Howells65f27f32006-11-22 14:55:48 +00001004 PREPARE_WORK(&floppy_work, (work_func_t)empty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 del_timer(&fd_timer);
1006 spin_unlock_irqrestore(&floppy_lock, flags);
1007}
1008
1009/* this function makes sure that the disk stays in the drive during the
1010 * transfer */
1011static void fd_watchdog(void)
1012{
Joe Perches87f530d2010-03-10 15:20:54 -08001013 debug_dcl(DP->flags, "calling disk change from watchdog\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
1015 if (disk_change(current_drive)) {
1016 DPRINT("disk removed during i/o\n");
1017 cancel_activity();
1018 cont->done(0);
1019 reset_fdc();
1020 } else {
1021 del_timer(&fd_timer);
Joe Perchesa0a52d62010-03-10 15:20:52 -08001022 fd_timer.function = (timeout_fn)fd_watchdog;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 fd_timer.expires = jiffies + HZ / 10;
1024 add_timer(&fd_timer);
1025 }
1026}
1027
1028static void main_command_interrupt(void)
1029{
1030 del_timer(&fd_timer);
1031 cont->interrupt();
1032}
1033
1034/* waits for a delay (spinup or select) to pass */
1035static int fd_wait_for_completion(unsigned long delay, timeout_fn function)
1036{
1037 if (FDCS->reset) {
1038 reset_fdc(); /* do the reset during sleep to win time
1039 * if we don't need to sleep, it's a good
1040 * occasion anyways */
1041 return 1;
1042 }
1043
Marcelo Feitoza Parisi50297cb2006-03-28 01:56:44 -08001044 if (time_before(jiffies, delay)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 del_timer(&fd_timer);
1046 fd_timer.function = function;
1047 fd_timer.expires = delay;
1048 add_timer(&fd_timer);
1049 return 1;
1050 }
1051 return 0;
1052}
1053
1054static DEFINE_SPINLOCK(floppy_hlt_lock);
1055static int hlt_disabled;
1056static void floppy_disable_hlt(void)
1057{
1058 unsigned long flags;
1059
1060 spin_lock_irqsave(&floppy_hlt_lock, flags);
1061 if (!hlt_disabled) {
1062 hlt_disabled = 1;
1063#ifdef HAVE_DISABLE_HLT
1064 disable_hlt();
1065#endif
1066 }
1067 spin_unlock_irqrestore(&floppy_hlt_lock, flags);
1068}
1069
1070static void floppy_enable_hlt(void)
1071{
1072 unsigned long flags;
1073
1074 spin_lock_irqsave(&floppy_hlt_lock, flags);
1075 if (hlt_disabled) {
1076 hlt_disabled = 0;
1077#ifdef HAVE_DISABLE_HLT
1078 enable_hlt();
1079#endif
1080 }
1081 spin_unlock_irqrestore(&floppy_hlt_lock, flags);
1082}
1083
1084static void setup_DMA(void)
1085{
1086 unsigned long f;
1087
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 if (raw_cmd->length == 0) {
1089 int i;
1090
Joe Perchesb46df352010-03-10 15:20:46 -08001091 pr_info("zero dma transfer size:");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 for (i = 0; i < raw_cmd->cmd_count; i++)
Joe Perchesb46df352010-03-10 15:20:46 -08001093 pr_cont("%x,", raw_cmd->cmd[i]);
1094 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 cont->done(0);
1096 FDCS->reset = 1;
1097 return;
1098 }
1099 if (((unsigned long)raw_cmd->kernel_data) % 512) {
Joe Perchesb46df352010-03-10 15:20:46 -08001100 pr_info("non aligned address: %p\n", raw_cmd->kernel_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 cont->done(0);
1102 FDCS->reset = 1;
1103 return;
1104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 f = claim_dma_lock();
1106 fd_disable_dma();
1107#ifdef fd_dma_setup
1108 if (fd_dma_setup(raw_cmd->kernel_data, raw_cmd->length,
1109 (raw_cmd->flags & FD_RAW_READ) ?
1110 DMA_MODE_READ : DMA_MODE_WRITE, FDCS->address) < 0) {
1111 release_dma_lock(f);
1112 cont->done(0);
1113 FDCS->reset = 1;
1114 return;
1115 }
1116 release_dma_lock(f);
1117#else
1118 fd_clear_dma_ff();
1119 fd_cacheflush(raw_cmd->kernel_data, raw_cmd->length);
1120 fd_set_dma_mode((raw_cmd->flags & FD_RAW_READ) ?
1121 DMA_MODE_READ : DMA_MODE_WRITE);
1122 fd_set_dma_addr(raw_cmd->kernel_data);
1123 fd_set_dma_count(raw_cmd->length);
1124 virtual_dma_port = FDCS->address;
1125 fd_enable_dma();
1126 release_dma_lock(f);
1127#endif
1128 floppy_disable_hlt();
1129}
1130
1131static void show_floppy(void);
1132
1133/* waits until the fdc becomes ready */
1134static int wait_til_ready(void)
1135{
Jesper Juhl06f748c2007-10-16 23:30:57 -07001136 int status;
1137 int counter;
1138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 if (FDCS->reset)
1140 return -1;
1141 for (counter = 0; counter < 10000; counter++) {
1142 status = fd_inb(FD_STATUS);
1143 if (status & STATUS_READY)
1144 return status;
1145 }
Joe Perches29f1c782010-03-10 15:21:00 -08001146 if (initialized) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 DPRINT("Getstatus times out (%x) on fdc %d\n", status, fdc);
1148 show_floppy();
1149 }
1150 FDCS->reset = 1;
1151 return -1;
1152}
1153
1154/* sends a command byte to the fdc */
1155static int output_byte(char byte)
1156{
Joe Perchesd7b2b2e2010-03-10 15:20:48 -08001157 int status = wait_til_ready();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Joe Perchesd7b2b2e2010-03-10 15:20:48 -08001159 if (status < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 return -1;
Joe Perches57584c52010-03-10 15:21:00 -08001161
1162 if (is_ready_state(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 fd_outb(byte, FD_DATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 output_log[output_log_pos].data = byte;
1165 output_log[output_log_pos].status = status;
1166 output_log[output_log_pos].jiffies = jiffies;
1167 output_log_pos = (output_log_pos + 1) % OLOGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 return 0;
1169 }
1170 FDCS->reset = 1;
Joe Perches29f1c782010-03-10 15:21:00 -08001171 if (initialized) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 DPRINT("Unable to send byte %x to FDC. Fdc=%x Status=%x\n",
1173 byte, fdc, status);
1174 show_floppy();
1175 }
1176 return -1;
1177}
1178
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179/* gets the response from the fdc */
1180static int result(void)
1181{
Jesper Juhl06f748c2007-10-16 23:30:57 -07001182 int i;
1183 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
1185 for (i = 0; i < MAX_REPLIES; i++) {
Joe Perchesd7b2b2e2010-03-10 15:20:48 -08001186 status = wait_til_ready();
1187 if (status < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 break;
1189 status &= STATUS_DIR | STATUS_READY | STATUS_BUSY | STATUS_DMA;
1190 if ((status & ~STATUS_BUSY) == STATUS_READY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 resultjiffies = jiffies;
1192 resultsize = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 return i;
1194 }
1195 if (status == (STATUS_DIR | STATUS_READY | STATUS_BUSY))
1196 reply_buffer[i] = fd_inb(FD_DATA);
1197 else
1198 break;
1199 }
Joe Perches29f1c782010-03-10 15:21:00 -08001200 if (initialized) {
1201 DPRINT("get result error. Fdc=%d Last status=%x Read bytes=%d\n",
1202 fdc, status, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 show_floppy();
1204 }
1205 FDCS->reset = 1;
1206 return -1;
1207}
1208
1209#define MORE_OUTPUT -2
1210/* does the fdc need more output? */
1211static int need_more_output(void)
1212{
Joe Perchesd7b2b2e2010-03-10 15:20:48 -08001213 int status = wait_til_ready();
Jesper Juhl06f748c2007-10-16 23:30:57 -07001214
Joe Perchesd7b2b2e2010-03-10 15:20:48 -08001215 if (status < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 return -1;
Joe Perches57584c52010-03-10 15:21:00 -08001217
1218 if (is_ready_state(status))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 return MORE_OUTPUT;
Joe Perches57584c52010-03-10 15:21:00 -08001220
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 return result();
1222}
1223
1224/* Set perpendicular mode as required, based on data rate, if supported.
1225 * 82077 Now tested. 1Mbps data rate only possible with 82077-1.
1226 */
1227static inline void perpendicular_mode(void)
1228{
1229 unsigned char perp_mode;
1230
1231 if (raw_cmd->rate & 0x40) {
1232 switch (raw_cmd->rate & 3) {
1233 case 0:
1234 perp_mode = 2;
1235 break;
1236 case 3:
1237 perp_mode = 3;
1238 break;
1239 default:
1240 DPRINT("Invalid data rate for perpendicular mode!\n");
1241 cont->done(0);
Joe Perchesbb57f0c62010-03-10 15:20:50 -08001242 FDCS->reset = 1;
1243 /*
1244 * convenient way to return to
1245 * redo without too much hassle
1246 * (deep stack et al.)
1247 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 return;
1249 }
1250 } else
1251 perp_mode = 0;
1252
1253 if (FDCS->perp_mode == perp_mode)
1254 return;
1255 if (FDCS->version >= FDC_82077_ORIG) {
1256 output_byte(FD_PERPENDICULAR);
1257 output_byte(perp_mode);
1258 FDCS->perp_mode = perp_mode;
1259 } else if (perp_mode) {
1260 DPRINT("perpendicular mode not supported by this FDC.\n");
1261 }
1262} /* perpendicular_mode */
1263
1264static int fifo_depth = 0xa;
1265static int no_fifo;
1266
1267static int fdc_configure(void)
1268{
1269 /* Turn on FIFO */
1270 output_byte(FD_CONFIGURE);
1271 if (need_more_output() != MORE_OUTPUT)
1272 return 0;
1273 output_byte(0);
1274 output_byte(0x10 | (no_fifo & 0x20) | (fifo_depth & 0xf));
1275 output_byte(0); /* pre-compensation from track
1276 0 upwards */
1277 return 1;
1278}
1279
1280#define NOMINAL_DTR 500
1281
1282/* Issue a "SPECIFY" command to set the step rate time, head unload time,
1283 * head load time, and DMA disable flag to values needed by floppy.
1284 *
1285 * The value "dtr" is the data transfer rate in Kbps. It is needed
1286 * to account for the data rate-based scaling done by the 82072 and 82077
1287 * FDC types. This parameter is ignored for other types of FDCs (i.e.
1288 * 8272a).
1289 *
1290 * Note that changing the data transfer rate has a (probably deleterious)
1291 * effect on the parameters subject to scaling for 82072/82077 FDCs, so
1292 * fdc_specify is called again after each data transfer rate
1293 * change.
1294 *
1295 * srt: 1000 to 16000 in microseconds
1296 * hut: 16 to 240 milliseconds
1297 * hlt: 2 to 254 milliseconds
1298 *
1299 * These values are rounded up to the next highest available delay time.
1300 */
1301static void fdc_specify(void)
1302{
Jesper Juhl06f748c2007-10-16 23:30:57 -07001303 unsigned char spec1;
1304 unsigned char spec2;
1305 unsigned long srt;
1306 unsigned long hlt;
1307 unsigned long hut;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 unsigned long dtr = NOMINAL_DTR;
1309 unsigned long scale_dtr = NOMINAL_DTR;
1310 int hlt_max_code = 0x7f;
1311 int hut_max_code = 0xf;
1312
1313 if (FDCS->need_configure && FDCS->version >= FDC_82072A) {
1314 fdc_configure();
1315 FDCS->need_configure = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 }
1317
1318 switch (raw_cmd->rate & 0x03) {
1319 case 3:
1320 dtr = 1000;
1321 break;
1322 case 1:
1323 dtr = 300;
1324 if (FDCS->version >= FDC_82078) {
1325 /* chose the default rate table, not the one
1326 * where 1 = 2 Mbps */
1327 output_byte(FD_DRIVESPEC);
1328 if (need_more_output() == MORE_OUTPUT) {
1329 output_byte(UNIT(current_drive));
1330 output_byte(0xc0);
1331 }
1332 }
1333 break;
1334 case 2:
1335 dtr = 250;
1336 break;
1337 }
1338
1339 if (FDCS->version >= FDC_82072) {
1340 scale_dtr = dtr;
1341 hlt_max_code = 0x00; /* 0==256msec*dtr0/dtr (not linear!) */
1342 hut_max_code = 0x0; /* 0==256msec*dtr0/dtr (not linear!) */
1343 }
1344
1345 /* Convert step rate from microseconds to milliseconds and 4 bits */
Julia Lawall061837b2008-09-22 14:57:16 -07001346 srt = 16 - DIV_ROUND_UP(DP->srt * scale_dtr / 1000, NOMINAL_DTR);
Joe Perchesa81ee542010-03-10 15:20:46 -08001347 if (slow_floppy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 srt = srt / 4;
Joe Perchesa81ee542010-03-10 15:20:46 -08001349
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 SUPBOUND(srt, 0xf);
1351 INFBOUND(srt, 0);
1352
Julia Lawall061837b2008-09-22 14:57:16 -07001353 hlt = DIV_ROUND_UP(DP->hlt * scale_dtr / 2, NOMINAL_DTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 if (hlt < 0x01)
1355 hlt = 0x01;
1356 else if (hlt > 0x7f)
1357 hlt = hlt_max_code;
1358
Julia Lawall061837b2008-09-22 14:57:16 -07001359 hut = DIV_ROUND_UP(DP->hut * scale_dtr / 16, NOMINAL_DTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 if (hut < 0x1)
1361 hut = 0x1;
1362 else if (hut > 0xf)
1363 hut = hut_max_code;
1364
1365 spec1 = (srt << 4) | hut;
1366 spec2 = (hlt << 1) | (use_virtual_dma & 1);
1367
1368 /* If these parameters did not change, just return with success */
1369 if (FDCS->spec1 != spec1 || FDCS->spec2 != spec2) {
1370 /* Go ahead and set spec1 and spec2 */
1371 output_byte(FD_SPECIFY);
1372 output_byte(FDCS->spec1 = spec1);
1373 output_byte(FDCS->spec2 = spec2);
1374 }
1375} /* fdc_specify */
1376
1377/* Set the FDC's data transfer rate on behalf of the specified drive.
1378 * NOTE: with 82072/82077 FDCs, changing the data rate requires a reissue
1379 * of the specify command (i.e. using the fdc_specify function).
1380 */
1381static int fdc_dtr(void)
1382{
1383 /* If data rate not already set to desired value, set it. */
1384 if ((raw_cmd->rate & 3) == FDCS->dtr)
1385 return 0;
1386
1387 /* Set dtr */
1388 fd_outb(raw_cmd->rate & 3, FD_DCR);
1389
1390 /* TODO: some FDC/drive combinations (C&T 82C711 with TEAC 1.2MB)
1391 * need a stabilization period of several milliseconds to be
1392 * enforced after data rate changes before R/W operations.
1393 * Pause 5 msec to avoid trouble. (Needs to be 2 jiffies)
1394 */
1395 FDCS->dtr = raw_cmd->rate & 3;
Joe Perchesd7b2b2e2010-03-10 15:20:48 -08001396 return fd_wait_for_completion(jiffies + 2UL * HZ / 100,
1397 (timeout_fn)floppy_ready);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398} /* fdc_dtr */
1399
1400static void tell_sector(void)
1401{
Joe Perchesb46df352010-03-10 15:20:46 -08001402 pr_cont(": track %d, head %d, sector %d, size %d",
1403 R_TRACK, R_HEAD, R_SECTOR, R_SIZECODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404} /* tell_sector */
1405
Joe Perchesb46df352010-03-10 15:20:46 -08001406static void print_errors(void)
1407{
1408 DPRINT("");
1409 if (ST0 & ST0_ECE) {
1410 pr_cont("Recalibrate failed!");
1411 } else if (ST2 & ST2_CRC) {
1412 pr_cont("data CRC error");
1413 tell_sector();
1414 } else if (ST1 & ST1_CRC) {
1415 pr_cont("CRC error");
1416 tell_sector();
1417 } else if ((ST1 & (ST1_MAM | ST1_ND)) ||
1418 (ST2 & ST2_MAM)) {
1419 if (!probing) {
1420 pr_cont("sector not found");
1421 tell_sector();
1422 } else
1423 pr_cont("probe failed...");
1424 } else if (ST2 & ST2_WC) { /* seek error */
1425 pr_cont("wrong cylinder");
1426 } else if (ST2 & ST2_BC) { /* cylinder marked as bad */
1427 pr_cont("bad cylinder");
1428 } else {
1429 pr_cont("unknown error. ST[0..2] are: 0x%x 0x%x 0x%x",
1430 ST0, ST1, ST2);
1431 tell_sector();
1432 }
1433 pr_cont("\n");
1434}
1435
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436/*
1437 * OK, this error interpreting routine is called after a
1438 * DMA read/write has succeeded
1439 * or failed, so we check the results, and copy any buffers.
1440 * hhb: Added better error reporting.
1441 * ak: Made this into a separate routine.
1442 */
1443static int interpret_errors(void)
1444{
1445 char bad;
1446
1447 if (inr != 7) {
Joe Perches891eda82010-03-10 15:21:05 -08001448 DPRINT("-- FDC reply error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 FDCS->reset = 1;
1450 return 1;
1451 }
1452
1453 /* check IC to find cause of interrupt */
1454 switch (ST0 & ST0_INTR) {
1455 case 0x40: /* error occurred during command execution */
1456 if (ST1 & ST1_EOC)
1457 return 0; /* occurs with pseudo-DMA */
1458 bad = 1;
1459 if (ST1 & ST1_WP) {
1460 DPRINT("Drive is write protected\n");
Joe Perchese0298532010-03-10 15:20:55 -08001461 clear_bit(FD_DISK_WRITABLE_BIT, &DRS->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 cont->done(0);
1463 bad = 2;
1464 } else if (ST1 & ST1_ND) {
Joe Perchese0298532010-03-10 15:20:55 -08001465 set_bit(FD_NEED_TWADDLE_BIT, &DRS->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 } else if (ST1 & ST1_OR) {
1467 if (DP->flags & FTD_MSG)
1468 DPRINT("Over/Underrun - retrying\n");
1469 bad = 0;
1470 } else if (*errors >= DP->max_errors.reporting) {
Joe Perchesb46df352010-03-10 15:20:46 -08001471 print_errors();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 }
1473 if (ST2 & ST2_WC || ST2 & ST2_BC)
1474 /* wrong cylinder => recal */
1475 DRS->track = NEED_2_RECAL;
1476 return bad;
1477 case 0x80: /* invalid command given */
1478 DPRINT("Invalid FDC command given!\n");
1479 cont->done(0);
1480 return 2;
1481 case 0xc0:
1482 DPRINT("Abnormal termination caused by polling\n");
1483 cont->error();
1484 return 2;
1485 default: /* (0) Normal command termination */
1486 return 0;
1487 }
1488}
1489
1490/*
1491 * This routine is called when everything should be correctly set up
1492 * for the transfer (i.e. floppy motor is on, the correct floppy is
1493 * selected, and the head is sitting on the right track).
1494 */
1495static void setup_rw_floppy(void)
1496{
Jesper Juhl06f748c2007-10-16 23:30:57 -07001497 int i;
1498 int r;
1499 int flags;
1500 int dflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 unsigned long ready_date;
1502 timeout_fn function;
1503
1504 flags = raw_cmd->flags;
1505 if (flags & (FD_RAW_READ | FD_RAW_WRITE))
1506 flags |= FD_RAW_INTR;
1507
1508 if ((flags & FD_RAW_SPIN) && !(flags & FD_RAW_NO_MOTOR)) {
1509 ready_date = DRS->spinup_date + DP->spinup;
1510 /* If spinup will take a long time, rerun scandrives
1511 * again just before spinup completion. Beware that
1512 * after scandrives, we must again wait for selection.
1513 */
Marcelo Feitoza Parisi50297cb2006-03-28 01:56:44 -08001514 if (time_after(ready_date, jiffies + DP->select_delay)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 ready_date -= DP->select_delay;
Joe Perchesa0a52d62010-03-10 15:20:52 -08001516 function = (timeout_fn)floppy_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 } else
Joe Perchesa0a52d62010-03-10 15:20:52 -08001518 function = (timeout_fn)setup_rw_floppy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
1520 /* wait until the floppy is spinning fast enough */
1521 if (fd_wait_for_completion(ready_date, function))
1522 return;
1523 }
1524 dflags = DRS->flags;
1525
1526 if ((flags & FD_RAW_READ) || (flags & FD_RAW_WRITE))
1527 setup_DMA();
1528
1529 if (flags & FD_RAW_INTR)
1530 do_floppy = main_command_interrupt;
1531
1532 r = 0;
1533 for (i = 0; i < raw_cmd->cmd_count; i++)
1534 r |= output_byte(raw_cmd->cmd[i]);
1535
Joe Perchesded28632010-03-10 15:21:09 -08001536 debugt(__func__, "rw_command");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
1538 if (r) {
1539 cont->error();
1540 reset_fdc();
1541 return;
1542 }
1543
1544 if (!(flags & FD_RAW_INTR)) {
1545 inr = result();
1546 cont->interrupt();
1547 } else if (flags & FD_RAW_NEED_DISK)
1548 fd_watchdog();
1549}
1550
1551static int blind_seek;
1552
1553/*
1554 * This is the routine called after every seek (or recalibrate) interrupt
1555 * from the floppy controller.
1556 */
1557static void seek_interrupt(void)
1558{
Joe Perchesded28632010-03-10 15:21:09 -08001559 debugt(__func__, "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 if (inr != 2 || (ST0 & 0xF8) != 0x20) {
1561 DPRINT("seek failed\n");
1562 DRS->track = NEED_2_RECAL;
1563 cont->error();
1564 cont->redo();
1565 return;
1566 }
1567 if (DRS->track >= 0 && DRS->track != ST1 && !blind_seek) {
Joe Perches87f530d2010-03-10 15:20:54 -08001568 debug_dcl(DP->flags,
1569 "clearing NEWCHANGE flag because of effective seek\n");
1570 debug_dcl(DP->flags, "jiffies=%lu\n", jiffies);
Joe Perchese0298532010-03-10 15:20:55 -08001571 clear_bit(FD_DISK_NEWCHANGE_BIT, &DRS->flags);
1572 /* effective seek */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 DRS->select_date = jiffies;
1574 }
1575 DRS->track = ST1;
1576 floppy_ready();
1577}
1578
1579static void check_wp(void)
1580{
Joe Perchese0298532010-03-10 15:20:55 -08001581 if (test_bit(FD_VERIFY_BIT, &DRS->flags)) {
1582 /* check write protection */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 output_byte(FD_GETSTATUS);
1584 output_byte(UNIT(current_drive));
1585 if (result() != 1) {
1586 FDCS->reset = 1;
1587 return;
1588 }
Joe Perchese0298532010-03-10 15:20:55 -08001589 clear_bit(FD_VERIFY_BIT, &DRS->flags);
1590 clear_bit(FD_NEED_TWADDLE_BIT, &DRS->flags);
Joe Perches87f530d2010-03-10 15:20:54 -08001591 debug_dcl(DP->flags,
1592 "checking whether disk is write protected\n");
1593 debug_dcl(DP->flags, "wp=%x\n", ST3 & 0x40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 if (!(ST3 & 0x40))
Joe Perchese0298532010-03-10 15:20:55 -08001595 set_bit(FD_DISK_WRITABLE_BIT, &DRS->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 else
Joe Perchese0298532010-03-10 15:20:55 -08001597 clear_bit(FD_DISK_WRITABLE_BIT, &DRS->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 }
1599}
1600
1601static void seek_floppy(void)
1602{
1603 int track;
1604
1605 blind_seek = 0;
1606
Joe Perchesded28632010-03-10 15:21:09 -08001607 debug_dcl(DP->flags, "calling disk change from %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
Joe Perchese0298532010-03-10 15:20:55 -08001609 if (!test_bit(FD_DISK_NEWCHANGE_BIT, &DRS->flags) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 disk_change(current_drive) && (raw_cmd->flags & FD_RAW_NEED_DISK)) {
1611 /* the media changed flag should be cleared after the seek.
1612 * If it isn't, this means that there is really no disk in
1613 * the drive.
1614 */
Joe Perchese0298532010-03-10 15:20:55 -08001615 set_bit(FD_DISK_CHANGED_BIT, &DRS->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 cont->done(0);
1617 cont->redo();
1618 return;
1619 }
1620 if (DRS->track <= NEED_1_RECAL) {
1621 recalibrate_floppy();
1622 return;
Joe Perchese0298532010-03-10 15:20:55 -08001623 } else if (test_bit(FD_DISK_NEWCHANGE_BIT, &DRS->flags) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 (raw_cmd->flags & FD_RAW_NEED_DISK) &&
1625 (DRS->track <= NO_TRACK || DRS->track == raw_cmd->track)) {
1626 /* we seek to clear the media-changed condition. Does anybody
1627 * know a more elegant way, which works on all drives? */
1628 if (raw_cmd->track)
1629 track = raw_cmd->track - 1;
1630 else {
1631 if (DP->flags & FD_SILENT_DCL_CLEAR) {
1632 set_dor(fdc, ~(0x10 << UNIT(current_drive)), 0);
1633 blind_seek = 1;
1634 raw_cmd->flags |= FD_RAW_NEED_SEEK;
1635 }
1636 track = 1;
1637 }
1638 } else {
1639 check_wp();
1640 if (raw_cmd->track != DRS->track &&
1641 (raw_cmd->flags & FD_RAW_NEED_SEEK))
1642 track = raw_cmd->track;
1643 else {
1644 setup_rw_floppy();
1645 return;
1646 }
1647 }
1648
1649 do_floppy = seek_interrupt;
1650 output_byte(FD_SEEK);
1651 output_byte(UNIT(current_drive));
Joe Perches2300f902010-03-10 15:20:49 -08001652 if (output_byte(track) < 0) {
1653 reset_fdc();
1654 return;
1655 }
Joe Perchesded28632010-03-10 15:21:09 -08001656 debugt(__func__, "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657}
1658
1659static void recal_interrupt(void)
1660{
Joe Perchesded28632010-03-10 15:21:09 -08001661 debugt(__func__, "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 if (inr != 2)
1663 FDCS->reset = 1;
1664 else if (ST0 & ST0_ECE) {
1665 switch (DRS->track) {
1666 case NEED_1_RECAL:
Joe Perchesded28632010-03-10 15:21:09 -08001667 debugt(__func__, "need 1 recal");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 /* after a second recalibrate, we still haven't
1669 * reached track 0. Probably no drive. Raise an
1670 * error, as failing immediately might upset
1671 * computers possessed by the Devil :-) */
1672 cont->error();
1673 cont->redo();
1674 return;
1675 case NEED_2_RECAL:
Joe Perchesded28632010-03-10 15:21:09 -08001676 debugt(__func__, "need 2 recal");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 /* If we already did a recalibrate,
1678 * and we are not at track 0, this
1679 * means we have moved. (The only way
1680 * not to move at recalibration is to
1681 * be already at track 0.) Clear the
1682 * new change flag */
Joe Perches87f530d2010-03-10 15:20:54 -08001683 debug_dcl(DP->flags,
1684 "clearing NEWCHANGE flag because of second recalibrate\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
Joe Perchese0298532010-03-10 15:20:55 -08001686 clear_bit(FD_DISK_NEWCHANGE_BIT, &DRS->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 DRS->select_date = jiffies;
1688 /* fall through */
1689 default:
Joe Perchesded28632010-03-10 15:21:09 -08001690 debugt(__func__, "default");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 /* Recalibrate moves the head by at
1692 * most 80 steps. If after one
1693 * recalibrate we don't have reached
1694 * track 0, this might mean that we
1695 * started beyond track 80. Try
1696 * again. */
1697 DRS->track = NEED_1_RECAL;
1698 break;
1699 }
1700 } else
1701 DRS->track = ST1;
1702 floppy_ready();
1703}
1704
1705static void print_result(char *message, int inr)
1706{
1707 int i;
1708
1709 DPRINT("%s ", message);
1710 if (inr >= 0)
1711 for (i = 0; i < inr; i++)
Joe Perchesb46df352010-03-10 15:20:46 -08001712 pr_cont("repl[%d]=%x ", i, reply_buffer[i]);
1713 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714}
1715
1716/* interrupt handler. Note that this can be called externally on the Sparc */
David Howells7d12e782006-10-05 14:55:46 +01001717irqreturn_t floppy_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 int do_print;
1720 unsigned long f;
Jesper Juhl06f748c2007-10-16 23:30:57 -07001721 void (*handler)(void) = do_floppy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
1723 lasthandler = handler;
1724 interruptjiffies = jiffies;
1725
1726 f = claim_dma_lock();
1727 fd_disable_dma();
1728 release_dma_lock(f);
1729
1730 floppy_enable_hlt();
1731 do_floppy = NULL;
1732 if (fdc >= N_FDC || FDCS->address == -1) {
1733 /* we don't even know which FDC is the culprit */
Joe Perchesb46df352010-03-10 15:20:46 -08001734 pr_info("DOR0=%x\n", fdc_state[0].dor);
1735 pr_info("floppy interrupt on bizarre fdc %d\n", fdc);
Joe Perches1ebddd82010-03-10 15:21:07 -08001736 pr_info("handler=%pf\n", handler);
Joe Perches275176b2010-03-10 15:21:06 -08001737 is_alive(__func__, "bizarre fdc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 return IRQ_NONE;
1739 }
1740
1741 FDCS->reset = 0;
1742 /* We have to clear the reset flag here, because apparently on boxes
1743 * with level triggered interrupts (PS/2, Sparc, ...), it is needed to
1744 * emit SENSEI's to clear the interrupt line. And FDCS->reset blocks the
1745 * emission of the SENSEI's.
1746 * It is OK to emit floppy commands because we are in an interrupt
1747 * handler here, and thus we have to fear no interference of other
1748 * activity.
1749 */
1750
Joe Perches29f1c782010-03-10 15:21:00 -08001751 do_print = !handler && print_unex && initialized;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
1753 inr = result();
1754 if (do_print)
1755 print_result("unexpected interrupt", inr);
1756 if (inr == 0) {
1757 int max_sensei = 4;
1758 do {
1759 output_byte(FD_SENSEI);
1760 inr = result();
1761 if (do_print)
1762 print_result("sensei", inr);
1763 max_sensei--;
Joe Perchesc5297302010-03-10 15:20:58 -08001764 } while ((ST0 & 0x83) != UNIT(current_drive) &&
1765 inr == 2 && max_sensei);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 }
1767 if (!handler) {
1768 FDCS->reset = 1;
1769 return IRQ_NONE;
1770 }
1771 schedule_bh(handler);
Joe Perches275176b2010-03-10 15:21:06 -08001772 is_alive(__func__, "normal interrupt end");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
1774 /* FIXME! Was it really for us? */
1775 return IRQ_HANDLED;
1776}
1777
1778static void recalibrate_floppy(void)
1779{
Joe Perchesded28632010-03-10 15:21:09 -08001780 debugt(__func__, "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 do_floppy = recal_interrupt;
1782 output_byte(FD_RECALIBRATE);
Joe Perches15b26302010-03-10 15:21:01 -08001783 if (output_byte(UNIT(current_drive)) < 0)
Joe Perches2300f902010-03-10 15:20:49 -08001784 reset_fdc();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785}
1786
1787/*
1788 * Must do 4 FD_SENSEIs after reset because of ``drive polling''.
1789 */
1790static void reset_interrupt(void)
1791{
Joe Perchesded28632010-03-10 15:21:09 -08001792 debugt(__func__, "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 result(); /* get the status ready for set_fdc */
1794 if (FDCS->reset) {
Joe Perches1ebddd82010-03-10 15:21:07 -08001795 pr_info("reset set in interrupt, calling %pf\n", cont->error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 cont->error(); /* a reset just after a reset. BAD! */
1797 }
1798 cont->redo();
1799}
1800
1801/*
1802 * reset is done by pulling bit 2 of DOR low for a while (old FDCs),
1803 * or by setting the self clearing bit 7 of STATUS (newer FDCs)
1804 */
1805static void reset_fdc(void)
1806{
1807 unsigned long flags;
1808
1809 do_floppy = reset_interrupt;
1810 FDCS->reset = 0;
1811 reset_fdc_info(0);
1812
1813 /* Pseudo-DMA may intercept 'reset finished' interrupt. */
1814 /* Irrelevant for systems with true DMA (i386). */
1815
1816 flags = claim_dma_lock();
1817 fd_disable_dma();
1818 release_dma_lock(flags);
1819
1820 if (FDCS->version >= FDC_82072A)
1821 fd_outb(0x80 | (FDCS->dtr & 3), FD_STATUS);
1822 else {
1823 fd_outb(FDCS->dor & ~0x04, FD_DOR);
1824 udelay(FD_RESET_DELAY);
1825 fd_outb(FDCS->dor, FD_DOR);
1826 }
1827}
1828
1829static void show_floppy(void)
1830{
1831 int i;
1832
Joe Perchesb46df352010-03-10 15:20:46 -08001833 pr_info("\n");
1834 pr_info("floppy driver state\n");
1835 pr_info("-------------------\n");
Joe Perches1ebddd82010-03-10 15:21:07 -08001836 pr_info("now=%lu last interrupt=%lu diff=%lu last called handler=%pf\n",
Joe Perchesb46df352010-03-10 15:20:46 -08001837 jiffies, interruptjiffies, jiffies - interruptjiffies,
1838 lasthandler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
Joe Perchesb46df352010-03-10 15:20:46 -08001840 pr_info("timeout_message=%s\n", timeout_message);
1841 pr_info("last output bytes:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 for (i = 0; i < OLOGSIZE; i++)
Joe Perchesb46df352010-03-10 15:20:46 -08001843 pr_info("%2x %2x %lu\n",
1844 output_log[(i + output_log_pos) % OLOGSIZE].data,
1845 output_log[(i + output_log_pos) % OLOGSIZE].status,
1846 output_log[(i + output_log_pos) % OLOGSIZE].jiffies);
1847 pr_info("last result at %lu\n", resultjiffies);
1848 pr_info("last redo_fd_request at %lu\n", lastredo);
1849 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_NONE, 16, 1,
1850 reply_buffer, resultsize, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
Joe Perchesb46df352010-03-10 15:20:46 -08001852 pr_info("status=%x\n", fd_inb(FD_STATUS));
1853 pr_info("fdc_busy=%lu\n", fdc_busy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 if (do_floppy)
Joe Perches1ebddd82010-03-10 15:21:07 -08001855 pr_info("do_floppy=%pf\n", do_floppy);
David Howells365970a2006-11-22 14:54:49 +00001856 if (work_pending(&floppy_work))
Joe Perches1ebddd82010-03-10 15:21:07 -08001857 pr_info("floppy_work.func=%pf\n", floppy_work.func);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 if (timer_pending(&fd_timer))
Joe Perches1ebddd82010-03-10 15:21:07 -08001859 pr_info("fd_timer.function=%pf\n", fd_timer.function);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 if (timer_pending(&fd_timeout)) {
Joe Perches1ebddd82010-03-10 15:21:07 -08001861 pr_info("timer_function=%pf\n", fd_timeout.function);
Joe Perchesb46df352010-03-10 15:20:46 -08001862 pr_info("expires=%lu\n", fd_timeout.expires - jiffies);
1863 pr_info("now=%lu\n", jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 }
Joe Perchesb46df352010-03-10 15:20:46 -08001865 pr_info("cont=%p\n", cont);
1866 pr_info("current_req=%p\n", current_req);
1867 pr_info("command_status=%d\n", command_status);
1868 pr_info("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869}
1870
1871static void floppy_shutdown(unsigned long data)
1872{
1873 unsigned long flags;
1874
Joe Perches29f1c782010-03-10 15:21:00 -08001875 if (initialized)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 show_floppy();
1877 cancel_activity();
1878
1879 floppy_enable_hlt();
1880
1881 flags = claim_dma_lock();
1882 fd_disable_dma();
1883 release_dma_lock(flags);
1884
1885 /* avoid dma going to a random drive after shutdown */
1886
Joe Perches29f1c782010-03-10 15:21:00 -08001887 if (initialized)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 DPRINT("floppy timeout called\n");
1889 FDCS->reset = 1;
1890 if (cont) {
1891 cont->done(0);
1892 cont->redo(); /* this will recall reset when needed */
1893 } else {
Joe Perchesb46df352010-03-10 15:20:46 -08001894 pr_info("no cont in shutdown!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 process_fd_request();
1896 }
Joe Perches275176b2010-03-10 15:21:06 -08001897 is_alive(__func__, "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898}
1899
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900/* start motor, check media-changed condition and write protection */
Jesper Juhl06f748c2007-10-16 23:30:57 -07001901static int start_motor(void (*function)(void))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902{
Jesper Juhl06f748c2007-10-16 23:30:57 -07001903 int mask;
1904 int data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
1906 mask = 0xfc;
1907 data = UNIT(current_drive);
1908 if (!(raw_cmd->flags & FD_RAW_NO_MOTOR)) {
1909 if (!(FDCS->dor & (0x10 << UNIT(current_drive)))) {
1910 set_debugt();
1911 /* no read since this drive is running */
1912 DRS->first_read_date = 0;
1913 /* note motor start time if motor is not yet running */
1914 DRS->spinup_date = jiffies;
1915 data |= (0x10 << UNIT(current_drive));
1916 }
1917 } else if (FDCS->dor & (0x10 << UNIT(current_drive)))
1918 mask &= ~(0x10 << UNIT(current_drive));
1919
1920 /* starts motor and selects floppy */
1921 del_timer(motor_off_timer + current_drive);
1922 set_dor(fdc, mask, data);
1923
1924 /* wait_for_completion also schedules reset if needed. */
Joe Perchesd7b2b2e2010-03-10 15:20:48 -08001925 return fd_wait_for_completion(DRS->select_date + DP->select_delay,
1926 (timeout_fn)function);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927}
1928
1929static void floppy_ready(void)
1930{
Joe Perches045f9832010-03-10 15:20:47 -08001931 if (FDCS->reset) {
1932 reset_fdc();
1933 return;
1934 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 if (start_motor(floppy_ready))
1936 return;
1937 if (fdc_dtr())
1938 return;
1939
Joe Perches87f530d2010-03-10 15:20:54 -08001940 debug_dcl(DP->flags, "calling disk change from floppy_ready\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 if (!(raw_cmd->flags & FD_RAW_NO_MOTOR) &&
1942 disk_change(current_drive) && !DP->select_delay)
Joe Perchesbb57f0c62010-03-10 15:20:50 -08001943 twaddle(); /* this clears the dcl on certain
1944 * drive/controller combinations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
1946#ifdef fd_chose_dma_mode
1947 if ((raw_cmd->flags & FD_RAW_READ) || (raw_cmd->flags & FD_RAW_WRITE)) {
1948 unsigned long flags = claim_dma_lock();
1949 fd_chose_dma_mode(raw_cmd->kernel_data, raw_cmd->length);
1950 release_dma_lock(flags);
1951 }
1952#endif
1953
1954 if (raw_cmd->flags & (FD_RAW_NEED_SEEK | FD_RAW_NEED_DISK)) {
1955 perpendicular_mode();
1956 fdc_specify(); /* must be done here because of hut, hlt ... */
1957 seek_floppy();
1958 } else {
1959 if ((raw_cmd->flags & FD_RAW_READ) ||
1960 (raw_cmd->flags & FD_RAW_WRITE))
1961 fdc_specify();
1962 setup_rw_floppy();
1963 }
1964}
1965
1966static void floppy_start(void)
1967{
Joe Perches73507e62010-03-10 15:21:03 -08001968 reschedule_timeout(current_reqD, "floppy start");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
1970 scandrives();
Joe Perches87f530d2010-03-10 15:20:54 -08001971 debug_dcl(DP->flags, "setting NEWCHANGE in floppy_start\n");
Joe Perchese0298532010-03-10 15:20:55 -08001972 set_bit(FD_DISK_NEWCHANGE_BIT, &DRS->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 floppy_ready();
1974}
1975
1976/*
1977 * ========================================================================
1978 * here ends the bottom half. Exported routines are:
1979 * floppy_start, floppy_off, floppy_ready, lock_fdc, unlock_fdc, set_fdc,
1980 * start_motor, reset_fdc, reset_fdc_info, interpret_errors.
1981 * Initialization also uses output_byte, result, set_dor, floppy_interrupt
1982 * and set_dor.
1983 * ========================================================================
1984 */
1985/*
1986 * General purpose continuations.
1987 * ==============================
1988 */
1989
1990static void do_wakeup(void)
1991{
Joe Perches73507e62010-03-10 15:21:03 -08001992 reschedule_timeout(MAXTIMEOUT, "do wakeup");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 cont = NULL;
1994 command_status += 2;
1995 wake_up(&command_done);
1996}
1997
1998static struct cont_t wakeup_cont = {
1999 .interrupt = empty,
2000 .redo = do_wakeup,
2001 .error = empty,
Jesper Juhl06f748c2007-10-16 23:30:57 -07002002 .done = (done_f)empty
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003};
2004
2005static struct cont_t intr_cont = {
2006 .interrupt = empty,
2007 .redo = process_fd_request,
2008 .error = empty,
Jesper Juhl06f748c2007-10-16 23:30:57 -07002009 .done = (done_f)empty
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010};
2011
Joe Perches74f63f42010-03-10 15:20:58 -08002012static int wait_til_done(void (*handler)(void), bool interruptible)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013{
2014 int ret;
2015
2016 schedule_bh(handler);
2017
2018 if (command_status < 2 && NO_SIGNAL) {
2019 DECLARE_WAITQUEUE(wait, current);
2020
2021 add_wait_queue(&command_done, &wait);
2022 for (;;) {
2023 set_current_state(interruptible ?
2024 TASK_INTERRUPTIBLE :
2025 TASK_UNINTERRUPTIBLE);
2026
2027 if (command_status >= 2 || !NO_SIGNAL)
2028 break;
2029
Joe Perches275176b2010-03-10 15:21:06 -08002030 is_alive(__func__, "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 schedule();
2032 }
2033
2034 set_current_state(TASK_RUNNING);
2035 remove_wait_queue(&command_done, &wait);
2036 }
2037
2038 if (command_status < 2) {
2039 cancel_activity();
2040 cont = &intr_cont;
2041 reset_fdc();
2042 return -EINTR;
2043 }
2044
2045 if (FDCS->reset)
2046 command_status = FD_COMMAND_ERROR;
2047 if (command_status == FD_COMMAND_OKAY)
2048 ret = 0;
2049 else
2050 ret = -EIO;
2051 command_status = FD_COMMAND_NONE;
2052 return ret;
2053}
2054
2055static void generic_done(int result)
2056{
2057 command_status = result;
2058 cont = &wakeup_cont;
2059}
2060
2061static void generic_success(void)
2062{
2063 cont->done(1);
2064}
2065
2066static void generic_failure(void)
2067{
2068 cont->done(0);
2069}
2070
2071static void success_and_wakeup(void)
2072{
2073 generic_success();
2074 cont->redo();
2075}
2076
2077/*
2078 * formatting and rw support.
2079 * ==========================
2080 */
2081
2082static int next_valid_format(void)
2083{
2084 int probed_format;
2085
2086 probed_format = DRS->probed_format;
2087 while (1) {
2088 if (probed_format >= 8 || !DP->autodetect[probed_format]) {
2089 DRS->probed_format = 0;
2090 return 1;
2091 }
2092 if (floppy_type[DP->autodetect[probed_format]].sect) {
2093 DRS->probed_format = probed_format;
2094 return 0;
2095 }
2096 probed_format++;
2097 }
2098}
2099
2100static void bad_flp_intr(void)
2101{
2102 int err_count;
2103
2104 if (probing) {
2105 DRS->probed_format++;
2106 if (!next_valid_format())
2107 return;
2108 }
2109 err_count = ++(*errors);
2110 INFBOUND(DRWE->badness, err_count);
2111 if (err_count > DP->max_errors.abort)
2112 cont->done(0);
2113 if (err_count > DP->max_errors.reset)
2114 FDCS->reset = 1;
2115 else if (err_count > DP->max_errors.recal)
2116 DRS->track = NEED_2_RECAL;
2117}
2118
2119static void set_floppy(int drive)
2120{
2121 int type = ITYPE(UDRS->fd_device);
Jesper Juhl06f748c2007-10-16 23:30:57 -07002122
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 if (type)
2124 _floppy = floppy_type + type;
2125 else
2126 _floppy = current_type[drive];
2127}
2128
2129/*
2130 * formatting support.
2131 * ===================
2132 */
2133static void format_interrupt(void)
2134{
2135 switch (interpret_errors()) {
2136 case 1:
2137 cont->error();
2138 case 2:
2139 break;
2140 case 0:
2141 cont->done(1);
2142 }
2143 cont->redo();
2144}
2145
Joe Perches48c8cee2010-03-10 15:20:45 -08002146#define FM_MODE(x, y) ((y) & ~(((x)->rate & 0x80) >> 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147#define CT(x) ((x) | 0xc0)
Joe Perches48c8cee2010-03-10 15:20:45 -08002148
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149static void setup_format_params(int track)
2150{
Jesper Juhl06f748c2007-10-16 23:30:57 -07002151 int n;
2152 int il;
2153 int count;
2154 int head_shift;
2155 int track_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 struct fparm {
2157 unsigned char track, head, sect, size;
2158 } *here = (struct fparm *)floppy_track_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
2160 raw_cmd = &default_raw_cmd;
2161 raw_cmd->track = track;
2162
Joe Perches48c8cee2010-03-10 15:20:45 -08002163 raw_cmd->flags = (FD_RAW_WRITE | FD_RAW_INTR | FD_RAW_SPIN |
2164 FD_RAW_NEED_DISK | FD_RAW_NEED_SEEK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 raw_cmd->rate = _floppy->rate & 0x43;
2166 raw_cmd->cmd_count = NR_F;
2167 COMMAND = FM_MODE(_floppy, FD_FORMAT);
2168 DR_SELECT = UNIT(current_drive) + PH_HEAD(_floppy, format_req.head);
2169 F_SIZECODE = FD_SIZECODE(_floppy);
2170 F_SECT_PER_TRACK = _floppy->sect << 2 >> F_SIZECODE;
2171 F_GAP = _floppy->fmt_gap;
2172 F_FILL = FD_FILL_BYTE;
2173
2174 raw_cmd->kernel_data = floppy_track_buffer;
2175 raw_cmd->length = 4 * F_SECT_PER_TRACK;
2176
2177 /* allow for about 30ms for data transport per track */
2178 head_shift = (F_SECT_PER_TRACK + 5) / 6;
2179
2180 /* a ``cylinder'' is two tracks plus a little stepping time */
2181 track_shift = 2 * head_shift + 3;
2182
2183 /* position of logical sector 1 on this track */
2184 n = (track_shift * format_req.track + head_shift * format_req.head)
2185 % F_SECT_PER_TRACK;
2186
2187 /* determine interleave */
2188 il = 1;
2189 if (_floppy->fmt_gap < 0x22)
2190 il++;
2191
2192 /* initialize field */
2193 for (count = 0; count < F_SECT_PER_TRACK; ++count) {
2194 here[count].track = format_req.track;
2195 here[count].head = format_req.head;
2196 here[count].sect = 0;
2197 here[count].size = F_SIZECODE;
2198 }
2199 /* place logical sectors */
2200 for (count = 1; count <= F_SECT_PER_TRACK; ++count) {
2201 here[n].sect = count;
2202 n = (n + il) % F_SECT_PER_TRACK;
2203 if (here[n].sect) { /* sector busy, find next free sector */
2204 ++n;
2205 if (n >= F_SECT_PER_TRACK) {
2206 n -= F_SECT_PER_TRACK;
2207 while (here[n].sect)
2208 ++n;
2209 }
2210 }
2211 }
Keith Wansbrough9e491842008-09-22 14:57:17 -07002212 if (_floppy->stretch & FD_SECTBASEMASK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 for (count = 0; count < F_SECT_PER_TRACK; count++)
Keith Wansbrough9e491842008-09-22 14:57:17 -07002214 here[count].sect += FD_SECTBASE(_floppy) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 }
2216}
2217
2218static void redo_format(void)
2219{
2220 buffer_track = -1;
2221 setup_format_params(format_req.track << STRETCH(_floppy));
2222 floppy_start();
Joe Perchesded28632010-03-10 15:21:09 -08002223 debugt(__func__, "queue format request");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224}
2225
2226static struct cont_t format_cont = {
2227 .interrupt = format_interrupt,
2228 .redo = redo_format,
2229 .error = bad_flp_intr,
2230 .done = generic_done
2231};
2232
2233static int do_format(int drive, struct format_descr *tmp_format_req)
2234{
2235 int ret;
2236
Joe Perches74f63f42010-03-10 15:20:58 -08002237 if (lock_fdc(drive, true))
Joe Perches52a0d612010-03-10 15:20:53 -08002238 return -EINTR;
2239
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 set_floppy(drive);
2241 if (!_floppy ||
2242 _floppy->track > DP->tracks ||
2243 tmp_format_req->track >= _floppy->track ||
2244 tmp_format_req->head >= _floppy->head ||
2245 (_floppy->sect << 2) % (1 << FD_SIZECODE(_floppy)) ||
2246 !_floppy->fmt_gap) {
2247 process_fd_request();
2248 return -EINVAL;
2249 }
2250 format_req = *tmp_format_req;
2251 format_errors = 0;
2252 cont = &format_cont;
2253 errors = &format_errors;
Joe Perches74f63f42010-03-10 15:20:58 -08002254 ret = wait_til_done(redo_format, true);
Joe Perches55eee802010-03-10 15:20:57 -08002255 if (ret == -EINTR)
2256 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 process_fd_request();
2258 return ret;
2259}
2260
2261/*
2262 * Buffer read/write and support
2263 * =============================
2264 */
2265
Kiyoshi Ueda1c5093b2008-01-28 10:36:21 +01002266static void floppy_end_request(struct request *req, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267{
2268 unsigned int nr_sectors = current_count_sectors;
Kiyoshi Ueda1c5093b2008-01-28 10:36:21 +01002269 unsigned int drive = (unsigned long)req->rq_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270
2271 /* current_count_sectors can be zero if transfer failed */
Kiyoshi Ueda1c5093b2008-01-28 10:36:21 +01002272 if (error)
Tejun Heo83096eb2009-05-07 22:24:39 +09002273 nr_sectors = blk_rq_cur_sectors(req);
Kiyoshi Ueda1c5093b2008-01-28 10:36:21 +01002274 if (__blk_end_request(req, error, nr_sectors << 9))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276
2277 /* We're done with the request */
Kiyoshi Ueda1c5093b2008-01-28 10:36:21 +01002278 floppy_off(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 current_req = NULL;
2280}
2281
2282/* new request_done. Can handle physical sectors which are smaller than a
2283 * logical buffer */
2284static void request_done(int uptodate)
2285{
2286 struct request_queue *q = floppy_queue;
2287 struct request *req = current_req;
2288 unsigned long flags;
2289 int block;
Joe Perches73507e62010-03-10 15:21:03 -08002290 char msg[sizeof("request done ") + sizeof(int) * 3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291
2292 probing = 0;
Joe Perches73507e62010-03-10 15:21:03 -08002293 snprintf(msg, sizeof(msg), "request done %d", uptodate);
2294 reschedule_timeout(MAXTIMEOUT, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295
2296 if (!req) {
Joe Perchesb46df352010-03-10 15:20:46 -08002297 pr_info("floppy.c: no request in request_done\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 return;
2299 }
2300
2301 if (uptodate) {
2302 /* maintain values for invalidation on geometry
2303 * change */
Tejun Heo83096eb2009-05-07 22:24:39 +09002304 block = current_count_sectors + blk_rq_pos(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 INFBOUND(DRS->maxblock, block);
2306 if (block > _floppy->sect)
2307 DRS->maxtrack = 1;
2308
2309 /* unlock chained buffers */
2310 spin_lock_irqsave(q->queue_lock, flags);
Kiyoshi Ueda1c5093b2008-01-28 10:36:21 +01002311 floppy_end_request(req, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 spin_unlock_irqrestore(q->queue_lock, flags);
2313 } else {
2314 if (rq_data_dir(req) == WRITE) {
2315 /* record write error information */
2316 DRWE->write_errors++;
2317 if (DRWE->write_errors == 1) {
Tejun Heo83096eb2009-05-07 22:24:39 +09002318 DRWE->first_error_sector = blk_rq_pos(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 DRWE->first_error_generation = DRS->generation;
2320 }
Tejun Heo83096eb2009-05-07 22:24:39 +09002321 DRWE->last_error_sector = blk_rq_pos(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 DRWE->last_error_generation = DRS->generation;
2323 }
2324 spin_lock_irqsave(q->queue_lock, flags);
Kiyoshi Ueda1c5093b2008-01-28 10:36:21 +01002325 floppy_end_request(req, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 spin_unlock_irqrestore(q->queue_lock, flags);
2327 }
2328}
2329
2330/* Interrupt handler evaluating the result of the r/w operation */
2331static void rw_interrupt(void)
2332{
Jesper Juhl06f748c2007-10-16 23:30:57 -07002333 int eoc;
2334 int ssize;
2335 int heads;
2336 int nr_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337
2338 if (R_HEAD >= 2) {
2339 /* some Toshiba floppy controllers occasionnally seem to
2340 * return bogus interrupts after read/write operations, which
2341 * can be recognized by a bad head number (>= 2) */
2342 return;
2343 }
2344
2345 if (!DRS->first_read_date)
2346 DRS->first_read_date = jiffies;
2347
2348 nr_sectors = 0;
Joe Perches712e1de2010-03-10 15:21:10 -08002349 ssize = DIV_ROUND_UP(1 << SIZECODE, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350
2351 if (ST1 & ST1_EOC)
2352 eoc = 1;
2353 else
2354 eoc = 0;
2355
2356 if (COMMAND & 0x80)
2357 heads = 2;
2358 else
2359 heads = 1;
2360
2361 nr_sectors = (((R_TRACK - TRACK) * heads +
2362 R_HEAD - HEAD) * SECT_PER_TRACK +
2363 R_SECTOR - SECTOR + eoc) << SIZECODE >> 2;
2364
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 if (nr_sectors / ssize >
Julia Lawall061837b2008-09-22 14:57:16 -07002366 DIV_ROUND_UP(in_sector_offset + current_count_sectors, ssize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 DPRINT("long rw: %x instead of %lx\n",
2368 nr_sectors, current_count_sectors);
Joe Perchesb46df352010-03-10 15:20:46 -08002369 pr_info("rs=%d s=%d\n", R_SECTOR, SECTOR);
2370 pr_info("rh=%d h=%d\n", R_HEAD, HEAD);
2371 pr_info("rt=%d t=%d\n", R_TRACK, TRACK);
2372 pr_info("heads=%d eoc=%d\n", heads, eoc);
2373 pr_info("spt=%d st=%d ss=%d\n",
2374 SECT_PER_TRACK, fsector_t, ssize);
2375 pr_info("in_sector_offset=%d\n", in_sector_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377
2378 nr_sectors -= in_sector_offset;
2379 INFBOUND(nr_sectors, 0);
2380 SUPBOUND(current_count_sectors, nr_sectors);
2381
2382 switch (interpret_errors()) {
2383 case 2:
2384 cont->redo();
2385 return;
2386 case 1:
2387 if (!current_count_sectors) {
2388 cont->error();
2389 cont->redo();
2390 return;
2391 }
2392 break;
2393 case 0:
2394 if (!current_count_sectors) {
2395 cont->redo();
2396 return;
2397 }
2398 current_type[current_drive] = _floppy;
2399 floppy_sizes[TOMINOR(current_drive)] = _floppy->size;
2400 break;
2401 }
2402
2403 if (probing) {
2404 if (DP->flags & FTD_MSG)
2405 DPRINT("Auto-detected floppy type %s in fd%d\n",
2406 _floppy->name, current_drive);
2407 current_type[current_drive] = _floppy;
2408 floppy_sizes[TOMINOR(current_drive)] = _floppy->size;
2409 probing = 0;
2410 }
2411
2412 if (CT(COMMAND) != FD_READ ||
2413 raw_cmd->kernel_data == current_req->buffer) {
2414 /* transfer directly from buffer */
2415 cont->done(1);
2416 } else if (CT(COMMAND) == FD_READ) {
2417 buffer_track = raw_cmd->track;
2418 buffer_drive = current_drive;
2419 INFBOUND(buffer_max, nr_sectors + fsector_t);
2420 }
2421 cont->redo();
2422}
2423
2424/* Compute maximal contiguous buffer size. */
2425static int buffer_chain_size(void)
2426{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 struct bio_vec *bv;
NeilBrown5705f702007-09-25 12:35:59 +02002428 int size;
2429 struct req_iterator iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 char *base;
2431
2432 base = bio_data(current_req->bio);
2433 size = 0;
2434
NeilBrown5705f702007-09-25 12:35:59 +02002435 rq_for_each_segment(bv, current_req, iter) {
2436 if (page_address(bv->bv_page) + bv->bv_offset != base + size)
2437 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438
NeilBrown5705f702007-09-25 12:35:59 +02002439 size += bv->bv_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 }
2441
2442 return size >> 9;
2443}
2444
2445/* Compute the maximal transfer size */
2446static int transfer_size(int ssize, int max_sector, int max_size)
2447{
2448 SUPBOUND(max_sector, fsector_t + max_size);
2449
2450 /* alignment */
2451 max_sector -= (max_sector % _floppy->sect) % ssize;
2452
2453 /* transfer size, beginning not aligned */
2454 current_count_sectors = max_sector - fsector_t;
2455
2456 return max_sector;
2457}
2458
2459/*
2460 * Move data from/to the track buffer to/from the buffer cache.
2461 */
2462static void copy_buffer(int ssize, int max_sector, int max_sector_2)
2463{
2464 int remaining; /* number of transferred 512-byte sectors */
2465 struct bio_vec *bv;
Jesper Juhl06f748c2007-10-16 23:30:57 -07002466 char *buffer;
2467 char *dma_buffer;
NeilBrown5705f702007-09-25 12:35:59 +02002468 int size;
2469 struct req_iterator iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470
2471 max_sector = transfer_size(ssize,
2472 min(max_sector, max_sector_2),
Tejun Heo83096eb2009-05-07 22:24:39 +09002473 blk_rq_sectors(current_req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474
2475 if (current_count_sectors <= 0 && CT(COMMAND) == FD_WRITE &&
Tejun Heo83096eb2009-05-07 22:24:39 +09002476 buffer_max > fsector_t + blk_rq_sectors(current_req))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 current_count_sectors = min_t(int, buffer_max - fsector_t,
Tejun Heo83096eb2009-05-07 22:24:39 +09002478 blk_rq_sectors(current_req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479
2480 remaining = current_count_sectors << 9;
Tejun Heo1011c1b2009-05-07 22:24:45 +09002481 if (remaining > blk_rq_bytes(current_req) && CT(COMMAND) == FD_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 DPRINT("in copy buffer\n");
Joe Perchesb46df352010-03-10 15:20:46 -08002483 pr_info("current_count_sectors=%ld\n", current_count_sectors);
2484 pr_info("remaining=%d\n", remaining >> 9);
2485 pr_info("current_req->nr_sectors=%u\n",
2486 blk_rq_sectors(current_req));
2487 pr_info("current_req->current_nr_sectors=%u\n",
2488 blk_rq_cur_sectors(current_req));
2489 pr_info("max_sector=%d\n", max_sector);
2490 pr_info("ssize=%d\n", ssize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
2493 buffer_max = max(max_sector, buffer_max);
2494
2495 dma_buffer = floppy_track_buffer + ((fsector_t - buffer_min) << 9);
2496
Tejun Heo1011c1b2009-05-07 22:24:45 +09002497 size = blk_rq_cur_bytes(current_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498
NeilBrown5705f702007-09-25 12:35:59 +02002499 rq_for_each_segment(bv, current_req, iter) {
2500 if (!remaining)
2501 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502
NeilBrown5705f702007-09-25 12:35:59 +02002503 size = bv->bv_len;
2504 SUPBOUND(size, remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505
NeilBrown5705f702007-09-25 12:35:59 +02002506 buffer = page_address(bv->bv_page) + bv->bv_offset;
NeilBrown5705f702007-09-25 12:35:59 +02002507 if (dma_buffer + size >
2508 floppy_track_buffer + (max_buffer_sectors << 10) ||
2509 dma_buffer < floppy_track_buffer) {
2510 DPRINT("buffer overrun in copy buffer %d\n",
Joe Perchesb46df352010-03-10 15:20:46 -08002511 (int)((floppy_track_buffer - dma_buffer) >> 9));
2512 pr_info("fsector_t=%d buffer_min=%d\n",
2513 fsector_t, buffer_min);
2514 pr_info("current_count_sectors=%ld\n",
2515 current_count_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 if (CT(COMMAND) == FD_READ)
Joe Perchesb46df352010-03-10 15:20:46 -08002517 pr_info("read\n");
NeilBrown5705f702007-09-25 12:35:59 +02002518 if (CT(COMMAND) == FD_WRITE)
Joe Perchesb46df352010-03-10 15:20:46 -08002519 pr_info("write\n");
NeilBrown5705f702007-09-25 12:35:59 +02002520 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 }
NeilBrown5705f702007-09-25 12:35:59 +02002522 if (((unsigned long)buffer) % 512)
2523 DPRINT("%p buffer not aligned\n", buffer);
Joe Perches1a23d132010-03-10 15:21:04 -08002524
NeilBrown5705f702007-09-25 12:35:59 +02002525 if (CT(COMMAND) == FD_READ)
2526 memcpy(buffer, dma_buffer, size);
2527 else
2528 memcpy(dma_buffer, buffer, size);
2529
2530 remaining -= size;
2531 dma_buffer += size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 if (remaining) {
2534 if (remaining > 0)
2535 max_sector -= remaining >> 9;
2536 DPRINT("weirdness: remaining %d\n", remaining >> 9);
2537 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538}
2539
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540/* work around a bug in pseudo DMA
2541 * (on some FDCs) pseudo DMA does not stop when the CPU stops
2542 * sending data. Hence we need a different way to signal the
2543 * transfer length: We use SECT_PER_TRACK. Unfortunately, this
2544 * does not work with MT, hence we can only transfer one head at
2545 * a time
2546 */
2547static void virtualdmabug_workaround(void)
2548{
Jesper Juhl06f748c2007-10-16 23:30:57 -07002549 int hard_sectors;
2550 int end_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551
2552 if (CT(COMMAND) == FD_WRITE) {
2553 COMMAND &= ~0x80; /* switch off multiple track mode */
2554
2555 hard_sectors = raw_cmd->length >> (7 + SIZECODE);
2556 end_sector = SECTOR + hard_sectors - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 if (end_sector > SECT_PER_TRACK) {
Joe Perchesb46df352010-03-10 15:20:46 -08002558 pr_info("too many sectors %d > %d\n",
2559 end_sector, SECT_PER_TRACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 return;
2561 }
Joe Perches48c8cee2010-03-10 15:20:45 -08002562 SECT_PER_TRACK = end_sector;
2563 /* make sure SECT_PER_TRACK
2564 * points to end of transfer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 }
2566}
2567
2568/*
2569 * Formulate a read/write request.
2570 * this routine decides where to load the data (directly to buffer, or to
2571 * tmp floppy area), how much data to load (the size of the buffer, the whole
2572 * track, or a single sector)
2573 * All floppy_track_buffer handling goes in here. If we ever add track buffer
2574 * allocation on the fly, it should be done here. No other part should need
2575 * modification.
2576 */
2577
2578static int make_raw_rw_request(void)
2579{
2580 int aligned_sector_t;
Jesper Juhl06f748c2007-10-16 23:30:57 -07002581 int max_sector;
2582 int max_size;
2583 int tracksize;
2584 int ssize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585
2586 if (max_buffer_sectors == 0) {
Joe Perchesb46df352010-03-10 15:20:46 -08002587 pr_info("VFS: Block I/O scheduled on unopened device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 return 0;
2589 }
2590
2591 set_fdc((long)current_req->rq_disk->private_data);
2592
2593 raw_cmd = &default_raw_cmd;
2594 raw_cmd->flags = FD_RAW_SPIN | FD_RAW_NEED_DISK | FD_RAW_NEED_DISK |
2595 FD_RAW_NEED_SEEK;
2596 raw_cmd->cmd_count = NR_RW;
2597 if (rq_data_dir(current_req) == READ) {
2598 raw_cmd->flags |= FD_RAW_READ;
2599 COMMAND = FM_MODE(_floppy, FD_READ);
2600 } else if (rq_data_dir(current_req) == WRITE) {
2601 raw_cmd->flags |= FD_RAW_WRITE;
2602 COMMAND = FM_MODE(_floppy, FD_WRITE);
2603 } else {
Joe Perches275176b2010-03-10 15:21:06 -08002604 DPRINT("%s: unknown command\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 return 0;
2606 }
2607
2608 max_sector = _floppy->sect * _floppy->head;
2609
Tejun Heo83096eb2009-05-07 22:24:39 +09002610 TRACK = (int)blk_rq_pos(current_req) / max_sector;
2611 fsector_t = (int)blk_rq_pos(current_req) % max_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 if (_floppy->track && TRACK >= _floppy->track) {
Tejun Heo83096eb2009-05-07 22:24:39 +09002613 if (blk_rq_cur_sectors(current_req) & 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 current_count_sectors = 1;
2615 return 1;
2616 } else
2617 return 0;
2618 }
2619 HEAD = fsector_t / _floppy->sect;
2620
Keith Wansbrough9e491842008-09-22 14:57:17 -07002621 if (((_floppy->stretch & (FD_SWAPSIDES | FD_SECTBASEMASK)) ||
Joe Perchese0298532010-03-10 15:20:55 -08002622 test_bit(FD_NEED_TWADDLE_BIT, &DRS->flags)) &&
2623 fsector_t < _floppy->sect)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 max_sector = _floppy->sect;
2625
2626 /* 2M disks have phantom sectors on the first track */
2627 if ((_floppy->rate & FD_2M) && (!TRACK) && (!HEAD)) {
2628 max_sector = 2 * _floppy->sect / 3;
2629 if (fsector_t >= max_sector) {
2630 current_count_sectors =
2631 min_t(int, _floppy->sect - fsector_t,
Tejun Heo83096eb2009-05-07 22:24:39 +09002632 blk_rq_sectors(current_req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 return 1;
2634 }
2635 SIZECODE = 2;
2636 } else
2637 SIZECODE = FD_SIZECODE(_floppy);
2638 raw_cmd->rate = _floppy->rate & 0x43;
2639 if ((_floppy->rate & FD_2M) && (TRACK || HEAD) && raw_cmd->rate == 2)
2640 raw_cmd->rate = 1;
2641
2642 if (SIZECODE)
2643 SIZECODE2 = 0xff;
2644 else
2645 SIZECODE2 = 0x80;
2646 raw_cmd->track = TRACK << STRETCH(_floppy);
2647 DR_SELECT = UNIT(current_drive) + PH_HEAD(_floppy, HEAD);
2648 GAP = _floppy->gap;
Joe Perches712e1de2010-03-10 15:21:10 -08002649 ssize = DIV_ROUND_UP(1 << SIZECODE, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 SECT_PER_TRACK = _floppy->sect << 2 >> SIZECODE;
2651 SECTOR = ((fsector_t % _floppy->sect) << 2 >> SIZECODE) +
Keith Wansbrough9e491842008-09-22 14:57:17 -07002652 FD_SECTBASE(_floppy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653
2654 /* tracksize describes the size which can be filled up with sectors
2655 * of size ssize.
2656 */
2657 tracksize = _floppy->sect - _floppy->sect % ssize;
2658 if (tracksize < _floppy->sect) {
2659 SECT_PER_TRACK++;
2660 if (tracksize <= fsector_t % _floppy->sect)
2661 SECTOR--;
2662
2663 /* if we are beyond tracksize, fill up using smaller sectors */
2664 while (tracksize <= fsector_t % _floppy->sect) {
2665 while (tracksize + ssize > _floppy->sect) {
2666 SIZECODE--;
2667 ssize >>= 1;
2668 }
2669 SECTOR++;
2670 SECT_PER_TRACK++;
2671 tracksize += ssize;
2672 }
2673 max_sector = HEAD * _floppy->sect + tracksize;
2674 } else if (!TRACK && !HEAD && !(_floppy->rate & FD_2M) && probing) {
2675 max_sector = _floppy->sect;
2676 } else if (!HEAD && CT(COMMAND) == FD_WRITE) {
2677 /* for virtual DMA bug workaround */
2678 max_sector = _floppy->sect;
2679 }
2680
2681 in_sector_offset = (fsector_t % _floppy->sect) % ssize;
2682 aligned_sector_t = fsector_t - in_sector_offset;
Tejun Heo83096eb2009-05-07 22:24:39 +09002683 max_size = blk_rq_sectors(current_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 if ((raw_cmd->track == buffer_track) &&
2685 (current_drive == buffer_drive) &&
2686 (fsector_t >= buffer_min) && (fsector_t < buffer_max)) {
2687 /* data already in track buffer */
2688 if (CT(COMMAND) == FD_READ) {
2689 copy_buffer(1, max_sector, buffer_max);
2690 return 1;
2691 }
Tejun Heo83096eb2009-05-07 22:24:39 +09002692 } else if (in_sector_offset || blk_rq_sectors(current_req) < ssize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 if (CT(COMMAND) == FD_WRITE) {
Joe Perchesd7b2b2e2010-03-10 15:20:48 -08002694 unsigned int sectors;
2695
2696 sectors = fsector_t + blk_rq_sectors(current_req);
2697 if (sectors > ssize && sectors < ssize + ssize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 max_size = ssize + ssize;
2699 else
2700 max_size = ssize;
2701 }
2702 raw_cmd->flags &= ~FD_RAW_WRITE;
2703 raw_cmd->flags |= FD_RAW_READ;
2704 COMMAND = FM_MODE(_floppy, FD_READ);
2705 } else if ((unsigned long)current_req->buffer < MAX_DMA_ADDRESS) {
2706 unsigned long dma_limit;
2707 int direct, indirect;
2708
2709 indirect =
2710 transfer_size(ssize, max_sector,
2711 max_buffer_sectors * 2) - fsector_t;
2712
2713 /*
2714 * Do NOT use minimum() here---MAX_DMA_ADDRESS is 64 bits wide
2715 * on a 64 bit machine!
2716 */
2717 max_size = buffer_chain_size();
Joe Perchesd7b2b2e2010-03-10 15:20:48 -08002718 dma_limit = (MAX_DMA_ADDRESS -
2719 ((unsigned long)current_req->buffer)) >> 9;
Joe Perchesa81ee542010-03-10 15:20:46 -08002720 if ((unsigned long)max_size > dma_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 max_size = dma_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 /* 64 kb boundaries */
2723 if (CROSS_64KB(current_req->buffer, max_size << 9))
2724 max_size = (K_64 -
2725 ((unsigned long)current_req->buffer) %
2726 K_64) >> 9;
2727 direct = transfer_size(ssize, max_sector, max_size) - fsector_t;
2728 /*
2729 * We try to read tracks, but if we get too many errors, we
2730 * go back to reading just one sector at a time.
2731 *
2732 * This means we should be able to read a sector even if there
2733 * are other bad sectors on this track.
2734 */
2735 if (!direct ||
2736 (indirect * 2 > direct * 3 &&
Joe Perchesd7b2b2e2010-03-10 15:20:48 -08002737 *errors < DP->max_errors.read_track &&
2738 ((!probing ||
2739 (DP->read_track & (1 << DRS->probed_format)))))) {
Tejun Heo83096eb2009-05-07 22:24:39 +09002740 max_size = blk_rq_sectors(current_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 } else {
2742 raw_cmd->kernel_data = current_req->buffer;
2743 raw_cmd->length = current_count_sectors << 9;
2744 if (raw_cmd->length == 0) {
Joe Perches275176b2010-03-10 15:21:06 -08002745 DPRINT("%s: zero dma transfer attempted\n", __func__);
Joe Perchesd7b2b2e2010-03-10 15:20:48 -08002746 DPRINT("indirect=%d direct=%d fsector_t=%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 indirect, direct, fsector_t);
2748 return 0;
2749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 virtualdmabug_workaround();
2751 return 2;
2752 }
2753 }
2754
2755 if (CT(COMMAND) == FD_READ)
2756 max_size = max_sector; /* unbounded */
2757
2758 /* claim buffer track if needed */
2759 if (buffer_track != raw_cmd->track || /* bad track */
2760 buffer_drive != current_drive || /* bad drive */
2761 fsector_t > buffer_max ||
2762 fsector_t < buffer_min ||
2763 ((CT(COMMAND) == FD_READ ||
Tejun Heo83096eb2009-05-07 22:24:39 +09002764 (!in_sector_offset && blk_rq_sectors(current_req) >= ssize)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 max_sector > 2 * max_buffer_sectors + buffer_min &&
Joe Perchesbb57f0c62010-03-10 15:20:50 -08002766 max_size + fsector_t > 2 * max_buffer_sectors + buffer_min)) {
2767 /* not enough space */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 buffer_track = -1;
2769 buffer_drive = current_drive;
2770 buffer_max = buffer_min = aligned_sector_t;
2771 }
2772 raw_cmd->kernel_data = floppy_track_buffer +
Joe Perchesbb57f0c62010-03-10 15:20:50 -08002773 ((aligned_sector_t - buffer_min) << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774
2775 if (CT(COMMAND) == FD_WRITE) {
2776 /* copy write buffer to track buffer.
2777 * if we get here, we know that the write
2778 * is either aligned or the data already in the buffer
2779 * (buffer will be overwritten) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 if (in_sector_offset && buffer_track == -1)
2781 DPRINT("internal error offset !=0 on write\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 buffer_track = raw_cmd->track;
2783 buffer_drive = current_drive;
2784 copy_buffer(ssize, max_sector,
2785 2 * max_buffer_sectors + buffer_min);
2786 } else
2787 transfer_size(ssize, max_sector,
2788 2 * max_buffer_sectors + buffer_min -
2789 aligned_sector_t);
2790
2791 /* round up current_count_sectors to get dma xfer size */
2792 raw_cmd->length = in_sector_offset + current_count_sectors;
2793 raw_cmd->length = ((raw_cmd->length - 1) | (ssize - 1)) + 1;
2794 raw_cmd->length <<= 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 if ((raw_cmd->length < current_count_sectors << 9) ||
2796 (raw_cmd->kernel_data != current_req->buffer &&
2797 CT(COMMAND) == FD_WRITE &&
2798 (aligned_sector_t + (raw_cmd->length >> 9) > buffer_max ||
2799 aligned_sector_t < buffer_min)) ||
2800 raw_cmd->length % (128 << SIZECODE) ||
2801 raw_cmd->length <= 0 || current_count_sectors <= 0) {
2802 DPRINT("fractionary current count b=%lx s=%lx\n",
2803 raw_cmd->length, current_count_sectors);
2804 if (raw_cmd->kernel_data != current_req->buffer)
Joe Perchesb46df352010-03-10 15:20:46 -08002805 pr_info("addr=%d, length=%ld\n",
2806 (int)((raw_cmd->kernel_data -
2807 floppy_track_buffer) >> 9),
2808 current_count_sectors);
2809 pr_info("st=%d ast=%d mse=%d msi=%d\n",
2810 fsector_t, aligned_sector_t, max_sector, max_size);
2811 pr_info("ssize=%x SIZECODE=%d\n", ssize, SIZECODE);
2812 pr_info("command=%x SECTOR=%d HEAD=%d, TRACK=%d\n",
2813 COMMAND, SECTOR, HEAD, TRACK);
2814 pr_info("buffer drive=%d\n", buffer_drive);
2815 pr_info("buffer track=%d\n", buffer_track);
2816 pr_info("buffer_min=%d\n", buffer_min);
2817 pr_info("buffer_max=%d\n", buffer_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 return 0;
2819 }
2820
2821 if (raw_cmd->kernel_data != current_req->buffer) {
2822 if (raw_cmd->kernel_data < floppy_track_buffer ||
2823 current_count_sectors < 0 ||
2824 raw_cmd->length < 0 ||
2825 raw_cmd->kernel_data + raw_cmd->length >
2826 floppy_track_buffer + (max_buffer_sectors << 10)) {
2827 DPRINT("buffer overrun in schedule dma\n");
Joe Perchesb46df352010-03-10 15:20:46 -08002828 pr_info("fsector_t=%d buffer_min=%d current_count=%ld\n",
2829 fsector_t, buffer_min, raw_cmd->length >> 9);
2830 pr_info("current_count_sectors=%ld\n",
2831 current_count_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 if (CT(COMMAND) == FD_READ)
Joe Perchesb46df352010-03-10 15:20:46 -08002833 pr_info("read\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 if (CT(COMMAND) == FD_WRITE)
Joe Perchesb46df352010-03-10 15:20:46 -08002835 pr_info("write\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 return 0;
2837 }
Tejun Heo1011c1b2009-05-07 22:24:45 +09002838 } else if (raw_cmd->length > blk_rq_bytes(current_req) ||
Tejun Heo83096eb2009-05-07 22:24:39 +09002839 current_count_sectors > blk_rq_sectors(current_req)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 DPRINT("buffer overrun in direct transfer\n");
2841 return 0;
2842 } else if (raw_cmd->length < current_count_sectors << 9) {
2843 DPRINT("more sectors than bytes\n");
Joe Perchesb46df352010-03-10 15:20:46 -08002844 pr_info("bytes=%ld\n", raw_cmd->length >> 9);
2845 pr_info("sectors=%ld\n", current_count_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 }
2847 if (raw_cmd->length == 0) {
2848 DPRINT("zero dma transfer attempted from make_raw_request\n");
2849 return 0;
2850 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851
2852 virtualdmabug_workaround();
2853 return 2;
2854}
2855
2856static void redo_fd_request(void)
2857{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 int drive;
2859 int tmp;
2860
2861 lastredo = jiffies;
2862 if (current_drive < N_DRIVE)
2863 floppy_off(current_drive);
2864
Joe Perches0da31322010-03-10 15:21:03 -08002865do_request:
2866 if (!current_req) {
2867 struct request *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868
Joe Perches0da31322010-03-10 15:21:03 -08002869 spin_lock_irq(floppy_queue->queue_lock);
2870 req = blk_fetch_request(floppy_queue);
2871 spin_unlock_irq(floppy_queue->queue_lock);
2872 if (!req) {
2873 do_floppy = NULL;
2874 unlock_fdc();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 }
Joe Perches0da31322010-03-10 15:21:03 -08002877 current_req = req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 }
Joe Perches0da31322010-03-10 15:21:03 -08002879 drive = (long)current_req->rq_disk->private_data;
2880 set_fdc(drive);
Joe Perches73507e62010-03-10 15:21:03 -08002881 reschedule_timeout(current_reqD, "redo fd request");
Joe Perches0da31322010-03-10 15:21:03 -08002882
2883 set_floppy(drive);
2884 raw_cmd = &default_raw_cmd;
2885 raw_cmd->flags = 0;
2886 if (start_motor(redo_fd_request))
2887 return;
2888
2889 disk_change(current_drive);
2890 if (test_bit(current_drive, &fake_change) ||
2891 test_bit(FD_DISK_CHANGED_BIT, &DRS->flags)) {
2892 DPRINT("disk absent or changed during operation\n");
2893 request_done(0);
2894 goto do_request;
2895 }
2896 if (!_floppy) { /* Autodetection */
2897 if (!probing) {
2898 DRS->probed_format = 0;
2899 if (next_valid_format()) {
2900 DPRINT("no autodetectable formats\n");
2901 _floppy = NULL;
2902 request_done(0);
2903 goto do_request;
2904 }
2905 }
2906 probing = 1;
2907 _floppy = floppy_type + DP->autodetect[DRS->probed_format];
2908 } else
2909 probing = 0;
2910 errors = &(current_req->errors);
2911 tmp = make_raw_rw_request();
2912 if (tmp < 2) {
2913 request_done(tmp);
2914 goto do_request;
2915 }
2916
2917 if (test_bit(FD_NEED_TWADDLE_BIT, &DRS->flags))
2918 twaddle();
2919 schedule_bh(floppy_start);
Joe Perchesded28632010-03-10 15:21:09 -08002920 debugt(__func__, "queue fd request");
Joe Perches0da31322010-03-10 15:21:03 -08002921 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922}
2923
2924static struct cont_t rw_cont = {
2925 .interrupt = rw_interrupt,
2926 .redo = redo_fd_request,
2927 .error = bad_flp_intr,
2928 .done = request_done
2929};
2930
2931static void process_fd_request(void)
2932{
2933 cont = &rw_cont;
2934 schedule_bh(redo_fd_request);
2935}
2936
Joe Perchesd7b2b2e2010-03-10 15:20:48 -08002937static void do_fd_request(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938{
2939 if (max_buffer_sectors == 0) {
Joe Perches275176b2010-03-10 15:21:06 -08002940 pr_info("VFS: %s called on non-open device\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 return;
2942 }
2943
2944 if (usage_count == 0) {
Joe Perchesb46df352010-03-10 15:20:46 -08002945 pr_info("warning: usage count=0, current_req=%p exiting\n",
2946 current_req);
2947 pr_info("sect=%ld type=%x flags=%x\n",
2948 (long)blk_rq_pos(current_req), current_req->cmd_type,
2949 current_req->cmd_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 return;
2951 }
2952 if (test_bit(0, &fdc_busy)) {
2953 /* fdc busy, this new request will be treated when the
2954 current one is done */
Joe Perches275176b2010-03-10 15:21:06 -08002955 is_alive(__func__, "old request running");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 return;
2957 }
Joe Perches74f63f42010-03-10 15:20:58 -08002958 lock_fdc(MAXTIMEOUT, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 process_fd_request();
Joe Perches275176b2010-03-10 15:21:06 -08002960 is_alive(__func__, "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961}
2962
2963static struct cont_t poll_cont = {
2964 .interrupt = success_and_wakeup,
2965 .redo = floppy_ready,
2966 .error = generic_failure,
2967 .done = generic_done
2968};
2969
Joe Perches74f63f42010-03-10 15:20:58 -08002970static int poll_drive(bool interruptible, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 /* no auto-sense, just clear dcl */
2973 raw_cmd = &default_raw_cmd;
2974 raw_cmd->flags = flag;
2975 raw_cmd->track = 0;
2976 raw_cmd->cmd_count = 0;
2977 cont = &poll_cont;
Joe Perches87f530d2010-03-10 15:20:54 -08002978 debug_dcl(DP->flags, "setting NEWCHANGE in poll_drive\n");
Joe Perchese0298532010-03-10 15:20:55 -08002979 set_bit(FD_DISK_NEWCHANGE_BIT, &DRS->flags);
Joe Perches55eee802010-03-10 15:20:57 -08002980
2981 return wait_til_done(floppy_ready, interruptible);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982}
2983
2984/*
2985 * User triggered reset
2986 * ====================
2987 */
2988
2989static void reset_intr(void)
2990{
Joe Perchesb46df352010-03-10 15:20:46 -08002991 pr_info("weird, reset interrupt called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992}
2993
2994static struct cont_t reset_cont = {
2995 .interrupt = reset_intr,
2996 .redo = success_and_wakeup,
2997 .error = generic_failure,
2998 .done = generic_done
2999};
3000
Joe Perches74f63f42010-03-10 15:20:58 -08003001static int user_reset_fdc(int drive, int arg, bool interruptible)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002{
3003 int ret;
3004
Joe Perches52a0d612010-03-10 15:20:53 -08003005 if (lock_fdc(drive, interruptible))
3006 return -EINTR;
3007
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 if (arg == FD_RESET_ALWAYS)
3009 FDCS->reset = 1;
3010 if (FDCS->reset) {
3011 cont = &reset_cont;
Joe Perches55eee802010-03-10 15:20:57 -08003012 ret = wait_til_done(reset_fdc, interruptible);
3013 if (ret == -EINTR)
3014 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 }
3016 process_fd_request();
Joe Perches52a0d612010-03-10 15:20:53 -08003017 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018}
3019
3020/*
3021 * Misc Ioctl's and support
3022 * ========================
3023 */
3024static inline int fd_copyout(void __user *param, const void *address,
3025 unsigned long size)
3026{
3027 return copy_to_user(param, address, size) ? -EFAULT : 0;
3028}
3029
Joe Perches48c8cee2010-03-10 15:20:45 -08003030static inline int fd_copyin(void __user *param, void *address,
3031 unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032{
3033 return copy_from_user(address, param, size) ? -EFAULT : 0;
3034}
3035
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036static inline const char *drive_name(int type, int drive)
3037{
3038 struct floppy_struct *floppy;
3039
3040 if (type)
3041 floppy = floppy_type + type;
3042 else {
3043 if (UDP->native_format)
3044 floppy = floppy_type + UDP->native_format;
3045 else
3046 return "(null)";
3047 }
3048 if (floppy->name)
3049 return floppy->name;
3050 else
3051 return "(null)";
3052}
3053
3054/* raw commands */
3055static void raw_cmd_done(int flag)
3056{
3057 int i;
3058
3059 if (!flag) {
3060 raw_cmd->flags |= FD_RAW_FAILURE;
3061 raw_cmd->flags |= FD_RAW_HARDFAILURE;
3062 } else {
3063 raw_cmd->reply_count = inr;
3064 if (raw_cmd->reply_count > MAX_REPLIES)
3065 raw_cmd->reply_count = 0;
3066 for (i = 0; i < raw_cmd->reply_count; i++)
3067 raw_cmd->reply[i] = reply_buffer[i];
3068
3069 if (raw_cmd->flags & (FD_RAW_READ | FD_RAW_WRITE)) {
3070 unsigned long flags;
3071 flags = claim_dma_lock();
3072 raw_cmd->length = fd_get_dma_residue();
3073 release_dma_lock(flags);
3074 }
3075
3076 if ((raw_cmd->flags & FD_RAW_SOFTFAILURE) &&
3077 (!raw_cmd->reply_count || (raw_cmd->reply[0] & 0xc0)))
3078 raw_cmd->flags |= FD_RAW_FAILURE;
3079
3080 if (disk_change(current_drive))
3081 raw_cmd->flags |= FD_RAW_DISK_CHANGE;
3082 else
3083 raw_cmd->flags &= ~FD_RAW_DISK_CHANGE;
3084 if (raw_cmd->flags & FD_RAW_NO_MOTOR_AFTER)
3085 motor_off_callback(current_drive);
3086
3087 if (raw_cmd->next &&
3088 (!(raw_cmd->flags & FD_RAW_FAILURE) ||
3089 !(raw_cmd->flags & FD_RAW_STOP_IF_FAILURE)) &&
3090 ((raw_cmd->flags & FD_RAW_FAILURE) ||
3091 !(raw_cmd->flags & FD_RAW_STOP_IF_SUCCESS))) {
3092 raw_cmd = raw_cmd->next;
3093 return;
3094 }
3095 }
3096 generic_done(flag);
3097}
3098
3099static struct cont_t raw_cmd_cont = {
3100 .interrupt = success_and_wakeup,
3101 .redo = floppy_start,
3102 .error = generic_failure,
3103 .done = raw_cmd_done
3104};
3105
Joe Perchesce2f11f2010-03-10 15:21:08 -08003106static inline int raw_cmd_copyout(int cmd, void __user *param,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 struct floppy_raw_cmd *ptr)
3108{
3109 int ret;
3110
3111 while (ptr) {
Joe Perchesce2f11f2010-03-10 15:21:08 -08003112 ret = copy_to_user(param, ptr, sizeof(*ptr));
Joe Perches86b12b42010-03-10 15:20:56 -08003113 if (ret)
3114 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 param += sizeof(struct floppy_raw_cmd);
3116 if ((ptr->flags & FD_RAW_READ) && ptr->buffer_length) {
Joe Perchesbb57f0c62010-03-10 15:20:50 -08003117 if (ptr->length >= 0 &&
3118 ptr->length <= ptr->buffer_length) {
3119 long length = ptr->buffer_length - ptr->length;
Joe Perches4575b552010-03-10 15:20:55 -08003120 ret = fd_copyout(ptr->data, ptr->kernel_data,
3121 length);
3122 if (ret)
3123 return ret;
Joe Perchesbb57f0c62010-03-10 15:20:50 -08003124 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 }
3126 ptr = ptr->next;
3127 }
Joe Perches7f252712010-03-10 15:21:08 -08003128
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 return 0;
3130}
3131
3132static void raw_cmd_free(struct floppy_raw_cmd **ptr)
3133{
Jesper Juhl06f748c2007-10-16 23:30:57 -07003134 struct floppy_raw_cmd *next;
3135 struct floppy_raw_cmd *this;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136
3137 this = *ptr;
3138 *ptr = NULL;
3139 while (this) {
3140 if (this->buffer_length) {
3141 fd_dma_mem_free((unsigned long)this->kernel_data,
3142 this->buffer_length);
3143 this->buffer_length = 0;
3144 }
3145 next = this->next;
3146 kfree(this);
3147 this = next;
3148 }
3149}
3150
Joe Perchesce2f11f2010-03-10 15:21:08 -08003151static inline int raw_cmd_copyin(int cmd, void __user *param,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 struct floppy_raw_cmd **rcmd)
3153{
3154 struct floppy_raw_cmd *ptr;
3155 int ret;
3156 int i;
3157
3158 *rcmd = NULL;
Joe Perches7f252712010-03-10 15:21:08 -08003159
3160loop:
3161 ptr = kmalloc(sizeof(struct floppy_raw_cmd), GFP_USER);
3162 if (!ptr)
3163 return -ENOMEM;
3164 *rcmd = ptr;
3165 ret = copy_from_user(ptr, param, sizeof(*ptr));
3166 if (ret)
3167 return -EFAULT;
3168 ptr->next = NULL;
3169 ptr->buffer_length = 0;
3170 param += sizeof(struct floppy_raw_cmd);
3171 if (ptr->cmd_count > 33)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 /* the command may now also take up the space
3173 * initially intended for the reply & the
3174 * reply count. Needed for long 82078 commands
3175 * such as RESTORE, which takes ... 17 command
3176 * bytes. Murphy's law #137: When you reserve
3177 * 16 bytes for a structure, you'll one day
3178 * discover that you really need 17...
3179 */
Joe Perches7f252712010-03-10 15:21:08 -08003180 return -EINVAL;
3181
3182 for (i = 0; i < 16; i++)
3183 ptr->reply[i] = 0;
3184 ptr->resultcode = 0;
3185 ptr->kernel_data = NULL;
3186
3187 if (ptr->flags & (FD_RAW_READ | FD_RAW_WRITE)) {
3188 if (ptr->length <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 return -EINVAL;
Joe Perches7f252712010-03-10 15:21:08 -08003190 ptr->kernel_data = (char *)fd_dma_mem_alloc(ptr->length);
3191 fallback_on_nodma_alloc(&ptr->kernel_data, ptr->length);
3192 if (!ptr->kernel_data)
3193 return -ENOMEM;
3194 ptr->buffer_length = ptr->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 }
Joe Perches7f252712010-03-10 15:21:08 -08003196 if (ptr->flags & FD_RAW_WRITE) {
3197 ret = fd_copyin(ptr->data, ptr->kernel_data, ptr->length);
3198 if (ret)
3199 return ret;
3200 }
3201
3202 if (ptr->flags & FD_RAW_MORE) {
3203 rcmd = &(ptr->next);
3204 ptr->rate &= 0x43;
3205 goto loop;
3206 }
3207
3208 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209}
3210
3211static int raw_cmd_ioctl(int cmd, void __user *param)
3212{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 struct floppy_raw_cmd *my_raw_cmd;
Jesper Juhl06f748c2007-10-16 23:30:57 -07003214 int drive;
3215 int ret2;
3216 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217
3218 if (FDCS->rawcmd <= 1)
3219 FDCS->rawcmd = 1;
3220 for (drive = 0; drive < N_DRIVE; drive++) {
3221 if (FDC(drive) != fdc)
3222 continue;
3223 if (drive == current_drive) {
3224 if (UDRS->fd_ref > 1) {
3225 FDCS->rawcmd = 2;
3226 break;
3227 }
3228 } else if (UDRS->fd_ref) {
3229 FDCS->rawcmd = 2;
3230 break;
3231 }
3232 }
3233
3234 if (FDCS->reset)
3235 return -EIO;
3236
3237 ret = raw_cmd_copyin(cmd, param, &my_raw_cmd);
3238 if (ret) {
3239 raw_cmd_free(&my_raw_cmd);
3240 return ret;
3241 }
3242
3243 raw_cmd = my_raw_cmd;
3244 cont = &raw_cmd_cont;
Joe Perches74f63f42010-03-10 15:20:58 -08003245 ret = wait_til_done(floppy_start, true);
Joe Perches87f530d2010-03-10 15:20:54 -08003246 debug_dcl(DP->flags, "calling disk change from raw_cmd ioctl\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247
3248 if (ret != -EINTR && FDCS->reset)
3249 ret = -EIO;
3250
3251 DRS->track = NO_TRACK;
3252
3253 ret2 = raw_cmd_copyout(cmd, param, my_raw_cmd);
3254 if (!ret)
3255 ret = ret2;
3256 raw_cmd_free(&my_raw_cmd);
3257 return ret;
3258}
3259
3260static int invalidate_drive(struct block_device *bdev)
3261{
3262 /* invalidate the buffer track to force a reread */
3263 set_bit((long)bdev->bd_disk->private_data, &fake_change);
3264 process_fd_request();
3265 check_disk_change(bdev);
3266 return 0;
3267}
3268
3269static inline int set_geometry(unsigned int cmd, struct floppy_struct *g,
3270 int drive, int type, struct block_device *bdev)
3271{
3272 int cnt;
3273
3274 /* sanity checking for parameters. */
3275 if (g->sect <= 0 ||
3276 g->head <= 0 ||
3277 g->track <= 0 || g->track > UDP->tracks >> STRETCH(g) ||
3278 /* check if reserved bits are set */
Keith Wansbrough9e491842008-09-22 14:57:17 -07003279 (g->stretch & ~(FD_STRETCH | FD_SWAPSIDES | FD_SECTBASEMASK)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 return -EINVAL;
3281 if (type) {
3282 if (!capable(CAP_SYS_ADMIN))
3283 return -EPERM;
Jes Sorensenb1c82b52006-03-23 03:00:26 -08003284 mutex_lock(&open_lock);
Joe Perches74f63f42010-03-10 15:20:58 -08003285 if (lock_fdc(drive, true)) {
Jiri Slaby8516a502009-06-30 11:41:44 -07003286 mutex_unlock(&open_lock);
3287 return -EINTR;
3288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 floppy_type[type] = *g;
3290 floppy_type[type].name = "user format";
3291 for (cnt = type << 2; cnt < (type << 2) + 4; cnt++)
3292 floppy_sizes[cnt] = floppy_sizes[cnt + 0x80] =
3293 floppy_type[type].size + 1;
3294 process_fd_request();
3295 for (cnt = 0; cnt < N_DRIVE; cnt++) {
3296 struct block_device *bdev = opened_bdev[cnt];
3297 if (!bdev || ITYPE(drive_state[cnt].fd_device) != type)
3298 continue;
Christoph Hellwig2ef41632005-05-05 16:15:59 -07003299 __invalidate_device(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 }
Jes Sorensenb1c82b52006-03-23 03:00:26 -08003301 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 } else {
3303 int oldStretch;
Joe Perches52a0d612010-03-10 15:20:53 -08003304
Joe Perches74f63f42010-03-10 15:20:58 -08003305 if (lock_fdc(drive, true))
Joe Perches52a0d612010-03-10 15:20:53 -08003306 return -EINTR;
Joe Perches4575b552010-03-10 15:20:55 -08003307 if (cmd != FDDEFPRM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 /* notice a disk change immediately, else
3309 * we lose our settings immediately*/
Joe Perches74f63f42010-03-10 15:20:58 -08003310 if (poll_drive(true, FD_RAW_NEED_DISK) == -EINTR)
Joe Perches4575b552010-03-10 15:20:55 -08003311 return -EINTR;
3312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 oldStretch = g->stretch;
3314 user_params[drive] = *g;
3315 if (buffer_drive == drive)
3316 SUPBOUND(buffer_max, user_params[drive].sect);
3317 current_type[drive] = &user_params[drive];
3318 floppy_sizes[drive] = user_params[drive].size;
3319 if (cmd == FDDEFPRM)
3320 DRS->keep_data = -1;
3321 else
3322 DRS->keep_data = 1;
3323 /* invalidation. Invalidate only when needed, i.e.
3324 * when there are already sectors in the buffer cache
3325 * whose number will change. This is useful, because
3326 * mtools often changes the geometry of the disk after
3327 * looking at the boot block */
3328 if (DRS->maxblock > user_params[drive].sect ||
3329 DRS->maxtrack ||
3330 ((user_params[drive].sect ^ oldStretch) &
Keith Wansbrough9e491842008-09-22 14:57:17 -07003331 (FD_SWAPSIDES | FD_SECTBASEMASK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 invalidate_drive(bdev);
3333 else
3334 process_fd_request();
3335 }
3336 return 0;
3337}
3338
3339/* handle obsolete ioctl's */
3340static int ioctl_table[] = {
3341 FDCLRPRM,
3342 FDSETPRM,
3343 FDDEFPRM,
3344 FDGETPRM,
3345 FDMSGON,
3346 FDMSGOFF,
3347 FDFMTBEG,
3348 FDFMTTRK,
3349 FDFMTEND,
3350 FDSETEMSGTRESH,
3351 FDFLUSH,
3352 FDSETMAXERRS,
3353 FDGETMAXERRS,
3354 FDGETDRVTYP,
3355 FDSETDRVPRM,
3356 FDGETDRVPRM,
3357 FDGETDRVSTAT,
3358 FDPOLLDRVSTAT,
3359 FDRESET,
3360 FDGETFDCSTAT,
3361 FDWERRORCLR,
3362 FDWERRORGET,
3363 FDRAWCMD,
3364 FDEJECT,
3365 FDTWADDLE
3366};
3367
3368static inline int normalize_ioctl(int *cmd, int *size)
3369{
3370 int i;
3371
3372 for (i = 0; i < ARRAY_SIZE(ioctl_table); i++) {
3373 if ((*cmd & 0xffff) == (ioctl_table[i] & 0xffff)) {
3374 *size = _IOC_SIZE(*cmd);
3375 *cmd = ioctl_table[i];
3376 if (*size > _IOC_SIZE(*cmd)) {
Joe Perchesb46df352010-03-10 15:20:46 -08003377 pr_info("ioctl not yet supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 return -EFAULT;
3379 }
3380 return 0;
3381 }
3382 }
3383 return -EINVAL;
3384}
3385
3386static int get_floppy_geometry(int drive, int type, struct floppy_struct **g)
3387{
3388 if (type)
3389 *g = &floppy_type[type];
3390 else {
Joe Perches74f63f42010-03-10 15:20:58 -08003391 if (lock_fdc(drive, false))
Joe Perches52a0d612010-03-10 15:20:53 -08003392 return -EINTR;
Joe Perches74f63f42010-03-10 15:20:58 -08003393 if (poll_drive(false, 0) == -EINTR)
Joe Perches4575b552010-03-10 15:20:55 -08003394 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 process_fd_request();
3396 *g = current_type[drive];
3397 }
3398 if (!*g)
3399 return -ENODEV;
3400 return 0;
3401}
3402
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003403static int fd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
3404{
3405 int drive = (long)bdev->bd_disk->private_data;
3406 int type = ITYPE(drive_state[drive].fd_device);
3407 struct floppy_struct *g;
3408 int ret;
3409
3410 ret = get_floppy_geometry(drive, type, &g);
3411 if (ret)
3412 return ret;
3413
3414 geo->heads = g->head;
3415 geo->sectors = g->sect;
3416 geo->cylinders = g->track;
3417 return 0;
3418}
3419
Al Viroa4af9b42008-03-02 09:27:55 -05003420static int fd_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 unsigned long param)
3422{
Al Viroa4af9b42008-03-02 09:27:55 -05003423 int drive = (long)bdev->bd_disk->private_data;
Jesper Juhl06f748c2007-10-16 23:30:57 -07003424 int type = ITYPE(UDRS->fd_device);
3425 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 int ret;
3427 int size;
3428 union inparam {
3429 struct floppy_struct g; /* geometry */
3430 struct format_descr f;
3431 struct floppy_max_errors max_errors;
3432 struct floppy_drive_params dp;
3433 } inparam; /* parameters coming from user space */
Joe Perches724ee622010-03-10 15:21:11 -08003434 const void *outparam; /* parameters passed back to user space */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435
3436 /* convert compatibility eject ioctls into floppy eject ioctl.
3437 * We do this in order to provide a means to eject floppy disks before
3438 * installing the new fdutils package */
3439 if (cmd == CDROMEJECT || /* CD-ROM eject */
Joe Perchesa81ee542010-03-10 15:20:46 -08003440 cmd == 0x6470) { /* SunOS floppy eject */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 DPRINT("obsolete eject ioctl\n");
3442 DPRINT("please use floppycontrol --eject\n");
3443 cmd = FDEJECT;
3444 }
3445
Joe Perchesa81ee542010-03-10 15:20:46 -08003446 if (!((cmd & 0xff00) == 0x0200))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 return -EINVAL;
3448
Joe Perchesa81ee542010-03-10 15:20:46 -08003449 /* convert the old style command into a new style command */
Joe Perches4575b552010-03-10 15:20:55 -08003450 ret = normalize_ioctl(&cmd, &size);
3451 if (ret)
3452 return ret;
Joe Perchesa81ee542010-03-10 15:20:46 -08003453
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 /* permission checks */
Joe Perches0aad92c2010-03-10 15:21:10 -08003455 if (((cmd & 0x40) && !(mode & (FMODE_WRITE | FMODE_WRITE_IOCTL))) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 ((cmd & 0x80) && !capable(CAP_SYS_ADMIN)))
3457 return -EPERM;
3458
Arjan van de Ven2886a8b2009-12-14 18:00:11 -08003459 if (WARN_ON(size < 0 || size > sizeof(inparam)))
3460 return -EINVAL;
3461
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 /* copyin */
Joe Perchesb87c9e02010-03-10 15:20:50 -08003463 memset(&inparam, 0, sizeof(inparam));
Joe Perches4575b552010-03-10 15:20:55 -08003464 if (_IOC_DIR(cmd) & _IOC_WRITE) {
3465 ret = fd_copyin((void __user *)param, &inparam, size);
3466 if (ret)
3467 return ret;
3468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469
Joe Perchesda273652010-03-10 15:20:52 -08003470 switch (cmd) {
3471 case FDEJECT:
3472 if (UDRS->fd_ref != 1)
3473 /* somebody else has this drive open */
3474 return -EBUSY;
Joe Perches74f63f42010-03-10 15:20:58 -08003475 if (lock_fdc(drive, true))
Joe Perches52a0d612010-03-10 15:20:53 -08003476 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477
Joe Perchesda273652010-03-10 15:20:52 -08003478 /* do the actual eject. Fails on
3479 * non-Sparc architectures */
3480 ret = fd_eject(UNIT(drive));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481
Joe Perchese0298532010-03-10 15:20:55 -08003482 set_bit(FD_DISK_CHANGED_BIT, &UDRS->flags);
3483 set_bit(FD_VERIFY_BIT, &UDRS->flags);
Joe Perchesda273652010-03-10 15:20:52 -08003484 process_fd_request();
3485 return ret;
3486 case FDCLRPRM:
Joe Perches74f63f42010-03-10 15:20:58 -08003487 if (lock_fdc(drive, true))
Joe Perches52a0d612010-03-10 15:20:53 -08003488 return -EINTR;
Joe Perchesda273652010-03-10 15:20:52 -08003489 current_type[drive] = NULL;
3490 floppy_sizes[drive] = MAX_DISK_SIZE << 1;
3491 UDRS->keep_data = 0;
3492 return invalidate_drive(bdev);
3493 case FDSETPRM:
3494 case FDDEFPRM:
3495 return set_geometry(cmd, &inparam.g, drive, type, bdev);
3496 case FDGETPRM:
Joe Perches4575b552010-03-10 15:20:55 -08003497 ret = get_floppy_geometry(drive, type,
Joe Perches724ee622010-03-10 15:21:11 -08003498 (struct floppy_struct **)&outparam);
Joe Perches4575b552010-03-10 15:20:55 -08003499 if (ret)
3500 return ret;
Joe Perchesda273652010-03-10 15:20:52 -08003501 break;
3502 case FDMSGON:
3503 UDP->flags |= FTD_MSG;
3504 return 0;
3505 case FDMSGOFF:
3506 UDP->flags &= ~FTD_MSG;
3507 return 0;
3508 case FDFMTBEG:
Joe Perches74f63f42010-03-10 15:20:58 -08003509 if (lock_fdc(drive, true))
Joe Perches52a0d612010-03-10 15:20:53 -08003510 return -EINTR;
Joe Perches74f63f42010-03-10 15:20:58 -08003511 if (poll_drive(true, FD_RAW_NEED_DISK) == -EINTR)
Joe Perches4575b552010-03-10 15:20:55 -08003512 return -EINTR;
Joe Perchesda273652010-03-10 15:20:52 -08003513 ret = UDRS->flags;
3514 process_fd_request();
3515 if (ret & FD_VERIFY)
3516 return -ENODEV;
3517 if (!(ret & FD_DISK_WRITABLE))
3518 return -EROFS;
3519 return 0;
3520 case FDFMTTRK:
3521 if (UDRS->fd_ref != 1)
3522 return -EBUSY;
3523 return do_format(drive, &inparam.f);
3524 case FDFMTEND:
3525 case FDFLUSH:
Joe Perches74f63f42010-03-10 15:20:58 -08003526 if (lock_fdc(drive, true))
Joe Perches52a0d612010-03-10 15:20:53 -08003527 return -EINTR;
Joe Perchesda273652010-03-10 15:20:52 -08003528 return invalidate_drive(bdev);
3529 case FDSETEMSGTRESH:
3530 UDP->max_errors.reporting = (unsigned short)(param & 0x0f);
3531 return 0;
3532 case FDGETMAXERRS:
Joe Perches724ee622010-03-10 15:21:11 -08003533 outparam = &UDP->max_errors;
Joe Perchesda273652010-03-10 15:20:52 -08003534 break;
3535 case FDSETMAXERRS:
3536 UDP->max_errors = inparam.max_errors;
3537 break;
3538 case FDGETDRVTYP:
3539 outparam = drive_name(type, drive);
Joe Perches724ee622010-03-10 15:21:11 -08003540 SUPBOUND(size, strlen((const char *)outparam) + 1);
Joe Perchesda273652010-03-10 15:20:52 -08003541 break;
3542 case FDSETDRVPRM:
3543 *UDP = inparam.dp;
3544 break;
3545 case FDGETDRVPRM:
Joe Perches724ee622010-03-10 15:21:11 -08003546 outparam = UDP;
Joe Perchesda273652010-03-10 15:20:52 -08003547 break;
3548 case FDPOLLDRVSTAT:
Joe Perches74f63f42010-03-10 15:20:58 -08003549 if (lock_fdc(drive, true))
Joe Perches52a0d612010-03-10 15:20:53 -08003550 return -EINTR;
Joe Perches74f63f42010-03-10 15:20:58 -08003551 if (poll_drive(true, FD_RAW_NEED_DISK) == -EINTR)
Joe Perches4575b552010-03-10 15:20:55 -08003552 return -EINTR;
Joe Perchesda273652010-03-10 15:20:52 -08003553 process_fd_request();
3554 /* fall through */
3555 case FDGETDRVSTAT:
Joe Perches724ee622010-03-10 15:21:11 -08003556 outparam = UDRS;
Joe Perchesda273652010-03-10 15:20:52 -08003557 break;
3558 case FDRESET:
Joe Perches74f63f42010-03-10 15:20:58 -08003559 return user_reset_fdc(drive, (int)param, true);
Joe Perchesda273652010-03-10 15:20:52 -08003560 case FDGETFDCSTAT:
Joe Perches724ee622010-03-10 15:21:11 -08003561 outparam = UFDCS;
Joe Perchesda273652010-03-10 15:20:52 -08003562 break;
3563 case FDWERRORCLR:
3564 memset(UDRWE, 0, sizeof(*UDRWE));
3565 return 0;
3566 case FDWERRORGET:
Joe Perches724ee622010-03-10 15:21:11 -08003567 outparam = UDRWE;
Joe Perchesda273652010-03-10 15:20:52 -08003568 break;
3569 case FDRAWCMD:
3570 if (type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571 return -EINVAL;
Joe Perches74f63f42010-03-10 15:20:58 -08003572 if (lock_fdc(drive, true))
Joe Perches52a0d612010-03-10 15:20:53 -08003573 return -EINTR;
Joe Perchesda273652010-03-10 15:20:52 -08003574 set_floppy(drive);
Joe Perches4575b552010-03-10 15:20:55 -08003575 i = raw_cmd_ioctl(cmd, (void __user *)param);
3576 if (i == -EINTR)
3577 return -EINTR;
Joe Perchesda273652010-03-10 15:20:52 -08003578 process_fd_request();
3579 return i;
3580 case FDTWADDLE:
Joe Perches74f63f42010-03-10 15:20:58 -08003581 if (lock_fdc(drive, true))
Joe Perches52a0d612010-03-10 15:20:53 -08003582 return -EINTR;
Joe Perchesda273652010-03-10 15:20:52 -08003583 twaddle();
3584 process_fd_request();
3585 return 0;
3586 default:
3587 return -EINVAL;
3588 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589
3590 if (_IOC_DIR(cmd) & _IOC_READ)
3591 return fd_copyout((void __user *)param, outparam, size);
Joe Perchesda273652010-03-10 15:20:52 -08003592
3593 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594}
3595
3596static void __init config_types(void)
3597{
Joe Perchesb46df352010-03-10 15:20:46 -08003598 bool has_drive = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599 int drive;
3600
3601 /* read drive info out of physical CMOS */
3602 drive = 0;
3603 if (!UDP->cmos)
3604 UDP->cmos = FLOPPY0_TYPE;
3605 drive = 1;
3606 if (!UDP->cmos && FLOPPY1_TYPE)
3607 UDP->cmos = FLOPPY1_TYPE;
3608
Jesper Juhl06f748c2007-10-16 23:30:57 -07003609 /* FIXME: additional physical CMOS drive detection should go here */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610
3611 for (drive = 0; drive < N_DRIVE; drive++) {
3612 unsigned int type = UDP->cmos;
3613 struct floppy_drive_params *params;
3614 const char *name = NULL;
3615 static char temparea[32];
3616
Tobias Klauser945f3902006-01-08 01:05:11 -08003617 if (type < ARRAY_SIZE(default_drive_params)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 params = &default_drive_params[type].params;
3619 if (type) {
3620 name = default_drive_params[type].name;
3621 allowed_drive_mask |= 1 << drive;
3622 } else
3623 allowed_drive_mask &= ~(1 << drive);
3624 } else {
3625 params = &default_drive_params[0].params;
3626 sprintf(temparea, "unknown type %d (usb?)", type);
3627 name = temparea;
3628 }
3629 if (name) {
Joe Perchesb46df352010-03-10 15:20:46 -08003630 const char *prepend;
3631 if (!has_drive) {
3632 prepend = "";
3633 has_drive = true;
3634 pr_info("Floppy drive(s):");
3635 } else {
3636 prepend = ",";
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637 }
Joe Perchesb46df352010-03-10 15:20:46 -08003638
3639 pr_cont("%s fd%d is %s", prepend, drive, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 }
3641 *UDP = *params;
3642 }
Joe Perchesb46df352010-03-10 15:20:46 -08003643
3644 if (has_drive)
3645 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646}
3647
Al Viroa4af9b42008-03-02 09:27:55 -05003648static int floppy_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649{
Al Viroa4af9b42008-03-02 09:27:55 -05003650 int drive = (long)disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651
Jes Sorensenb1c82b52006-03-23 03:00:26 -08003652 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653 if (UDRS->fd_ref < 0)
3654 UDRS->fd_ref = 0;
3655 else if (!UDRS->fd_ref--) {
3656 DPRINT("floppy_release with fd_ref == 0");
3657 UDRS->fd_ref = 0;
3658 }
3659 if (!UDRS->fd_ref)
3660 opened_bdev[drive] = NULL;
Jes Sorensenb1c82b52006-03-23 03:00:26 -08003661 mutex_unlock(&open_lock);
Ingo Molnar3e541a42006-07-03 00:24:23 -07003662
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 return 0;
3664}
3665
3666/*
3667 * floppy_open check for aliasing (/dev/fd0 can be the same as
3668 * /dev/PS0 etc), and disallows simultaneous access to the same
3669 * drive with different device numbers.
3670 */
Al Viroa4af9b42008-03-02 09:27:55 -05003671static int floppy_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672{
Al Viroa4af9b42008-03-02 09:27:55 -05003673 int drive = (long)bdev->bd_disk->private_data;
3674 int old_dev, new_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 int try;
3676 int res = -EBUSY;
3677 char *tmp;
3678
Jes Sorensenb1c82b52006-03-23 03:00:26 -08003679 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 old_dev = UDRS->fd_device;
Al Viroa4af9b42008-03-02 09:27:55 -05003681 if (opened_bdev[drive] && opened_bdev[drive] != bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 goto out2;
3683
3684 if (!UDRS->fd_ref && (UDP->flags & FD_BROKEN_DCL)) {
Joe Perchese0298532010-03-10 15:20:55 -08003685 set_bit(FD_DISK_CHANGED_BIT, &UDRS->flags);
3686 set_bit(FD_VERIFY_BIT, &UDRS->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 }
3688
Al Viroa4af9b42008-03-02 09:27:55 -05003689 if (UDRS->fd_ref == -1 || (UDRS->fd_ref && (mode & FMODE_EXCL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690 goto out2;
3691
Al Viroa4af9b42008-03-02 09:27:55 -05003692 if (mode & FMODE_EXCL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693 UDRS->fd_ref = -1;
3694 else
3695 UDRS->fd_ref++;
3696
Al Viroa4af9b42008-03-02 09:27:55 -05003697 opened_bdev[drive] = bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698
3699 res = -ENXIO;
3700
3701 if (!floppy_track_buffer) {
3702 /* if opening an ED drive, reserve a big buffer,
3703 * else reserve a small one */
3704 if ((UDP->cmos == 6) || (UDP->cmos == 5))
3705 try = 64; /* Only 48 actually useful */
3706 else
3707 try = 32; /* Only 24 actually useful */
3708
3709 tmp = (char *)fd_dma_mem_alloc(1024 * try);
3710 if (!tmp && !floppy_track_buffer) {
3711 try >>= 1; /* buffer only one side */
3712 INFBOUND(try, 16);
3713 tmp = (char *)fd_dma_mem_alloc(1024 * try);
3714 }
Joe Perchesa81ee542010-03-10 15:20:46 -08003715 if (!tmp && !floppy_track_buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 fallback_on_nodma_alloc(&tmp, 2048 * try);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717 if (!tmp && !floppy_track_buffer) {
3718 DPRINT("Unable to allocate DMA memory\n");
3719 goto out;
3720 }
3721 if (floppy_track_buffer) {
3722 if (tmp)
3723 fd_dma_mem_free((unsigned long)tmp, try * 1024);
3724 } else {
3725 buffer_min = buffer_max = -1;
3726 floppy_track_buffer = tmp;
3727 max_buffer_sectors = try;
3728 }
3729 }
3730
Al Viroa4af9b42008-03-02 09:27:55 -05003731 new_dev = MINOR(bdev->bd_dev);
3732 UDRS->fd_device = new_dev;
3733 set_capacity(disks[drive], floppy_sizes[new_dev]);
3734 if (old_dev != -1 && old_dev != new_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735 if (buffer_drive == drive)
3736 buffer_track = -1;
3737 }
3738
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739 if (UFDCS->rawcmd == 1)
3740 UFDCS->rawcmd = 2;
3741
Al Viroa4af9b42008-03-02 09:27:55 -05003742 if (!(mode & FMODE_NDELAY)) {
3743 if (mode & (FMODE_READ|FMODE_WRITE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744 UDRS->last_checked = 0;
Al Viroa4af9b42008-03-02 09:27:55 -05003745 check_disk_change(bdev);
Joe Perchese0298532010-03-10 15:20:55 -08003746 if (test_bit(FD_DISK_CHANGED_BIT, &UDRS->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747 goto out;
3748 }
3749 res = -EROFS;
Joe Perchese0298532010-03-10 15:20:55 -08003750 if ((mode & FMODE_WRITE) &&
3751 !test_bit(FD_DISK_WRITABLE_BIT, &UDRS->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 goto out;
3753 }
Jes Sorensenb1c82b52006-03-23 03:00:26 -08003754 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755 return 0;
3756out:
3757 if (UDRS->fd_ref < 0)
3758 UDRS->fd_ref = 0;
3759 else
3760 UDRS->fd_ref--;
3761 if (!UDRS->fd_ref)
3762 opened_bdev[drive] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763out2:
Jes Sorensenb1c82b52006-03-23 03:00:26 -08003764 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 return res;
3766}
3767
3768/*
3769 * Check if the disk has been changed or if a change has been faked.
3770 */
3771static int check_floppy_change(struct gendisk *disk)
3772{
3773 int drive = (long)disk->private_data;
3774
Joe Perchese0298532010-03-10 15:20:55 -08003775 if (test_bit(FD_DISK_CHANGED_BIT, &UDRS->flags) ||
3776 test_bit(FD_VERIFY_BIT, &UDRS->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 return 1;
3778
Marcelo Feitoza Parisi50297cb2006-03-28 01:56:44 -08003779 if (time_after(jiffies, UDRS->last_checked + UDP->checkfreq)) {
Joe Perches74f63f42010-03-10 15:20:58 -08003780 lock_fdc(drive, false);
3781 poll_drive(false, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 process_fd_request();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783 }
3784
Joe Perchese0298532010-03-10 15:20:55 -08003785 if (test_bit(FD_DISK_CHANGED_BIT, &UDRS->flags) ||
3786 test_bit(FD_VERIFY_BIT, &UDRS->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 test_bit(drive, &fake_change) ||
3788 (!ITYPE(UDRS->fd_device) && !current_type[drive]))
3789 return 1;
3790 return 0;
3791}
3792
3793/*
3794 * This implements "read block 0" for floppy_revalidate().
3795 * Needed for format autodetection, checking whether there is
3796 * a disk in the drive, and whether that disk is writable.
3797 */
3798
Joe Perchesbb57f0c62010-03-10 15:20:50 -08003799static void floppy_rb0_complete(struct bio *bio, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801 complete((struct completion *)bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802}
3803
3804static int __floppy_read_block_0(struct block_device *bdev)
3805{
3806 struct bio bio;
3807 struct bio_vec bio_vec;
3808 struct completion complete;
3809 struct page *page;
3810 size_t size;
3811
3812 page = alloc_page(GFP_NOIO);
3813 if (!page) {
3814 process_fd_request();
3815 return -ENOMEM;
3816 }
3817
3818 size = bdev->bd_block_size;
3819 if (!size)
3820 size = 1024;
3821
3822 bio_init(&bio);
3823 bio.bi_io_vec = &bio_vec;
3824 bio_vec.bv_page = page;
3825 bio_vec.bv_len = size;
3826 bio_vec.bv_offset = 0;
3827 bio.bi_vcnt = 1;
3828 bio.bi_idx = 0;
3829 bio.bi_size = size;
3830 bio.bi_bdev = bdev;
3831 bio.bi_sector = 0;
3832 init_completion(&complete);
3833 bio.bi_private = &complete;
3834 bio.bi_end_io = floppy_rb0_complete;
3835
3836 submit_bio(READ, &bio);
3837 generic_unplug_device(bdev_get_queue(bdev));
3838 process_fd_request();
3839 wait_for_completion(&complete);
3840
3841 __free_page(page);
3842
3843 return 0;
3844}
3845
3846/* revalidate the floppy disk, i.e. trigger format autodetection by reading
3847 * the bootblock (block 0). "Autodetection" is also needed to check whether
3848 * there is a disk in the drive at all... Thus we also do it for fixed
3849 * geometry formats */
3850static int floppy_revalidate(struct gendisk *disk)
3851{
3852 int drive = (long)disk->private_data;
3853#define NO_GEOM (!current_type[drive] && !ITYPE(UDRS->fd_device))
3854 int cf;
3855 int res = 0;
3856
Joe Perchese0298532010-03-10 15:20:55 -08003857 if (test_bit(FD_DISK_CHANGED_BIT, &UDRS->flags) ||
3858 test_bit(FD_VERIFY_BIT, &UDRS->flags) ||
3859 test_bit(drive, &fake_change) || NO_GEOM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860 if (usage_count == 0) {
Joe Perchesb46df352010-03-10 15:20:46 -08003861 pr_info("VFS: revalidate called on non-open device.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862 return -EFAULT;
3863 }
Joe Perches74f63f42010-03-10 15:20:58 -08003864 lock_fdc(drive, false);
Joe Perchese0298532010-03-10 15:20:55 -08003865 cf = (test_bit(FD_DISK_CHANGED_BIT, &UDRS->flags) ||
3866 test_bit(FD_VERIFY_BIT, &UDRS->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 if (!(cf || test_bit(drive, &fake_change) || NO_GEOM)) {
3868 process_fd_request(); /*already done by another thread */
3869 return 0;
3870 }
3871 UDRS->maxblock = 0;
3872 UDRS->maxtrack = 0;
3873 if (buffer_drive == drive)
3874 buffer_track = -1;
3875 clear_bit(drive, &fake_change);
Joe Perchese0298532010-03-10 15:20:55 -08003876 clear_bit(FD_DISK_CHANGED_BIT, &UDRS->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877 if (cf)
3878 UDRS->generation++;
3879 if (NO_GEOM) {
3880 /* auto-sensing */
3881 res = __floppy_read_block_0(opened_bdev[drive]);
3882 } else {
3883 if (cf)
Joe Perches74f63f42010-03-10 15:20:58 -08003884 poll_drive(false, FD_RAW_NEED_DISK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885 process_fd_request();
3886 }
3887 }
3888 set_capacity(disk, floppy_sizes[UDRS->fd_device]);
3889 return res;
3890}
3891
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07003892static const struct block_device_operations floppy_fops = {
Jesper Juhl06f748c2007-10-16 23:30:57 -07003893 .owner = THIS_MODULE,
Al Viroa4af9b42008-03-02 09:27:55 -05003894 .open = floppy_open,
3895 .release = floppy_release,
3896 .locked_ioctl = fd_ioctl,
Jesper Juhl06f748c2007-10-16 23:30:57 -07003897 .getgeo = fd_getgeo,
3898 .media_changed = check_floppy_change,
3899 .revalidate_disk = floppy_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902/*
3903 * Floppy Driver initialization
3904 * =============================
3905 */
3906
3907/* Determine the floppy disk controller type */
3908/* This routine was written by David C. Niemi */
3909static char __init get_fdc_version(void)
3910{
3911 int r;
3912
3913 output_byte(FD_DUMPREGS); /* 82072 and better know DUMPREGS */
3914 if (FDCS->reset)
3915 return FDC_NONE;
Joe Perchesd7b2b2e2010-03-10 15:20:48 -08003916 r = result();
3917 if (r <= 0x00)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918 return FDC_NONE; /* No FDC present ??? */
3919 if ((r == 1) && (reply_buffer[0] == 0x80)) {
Joe Perchesb46df352010-03-10 15:20:46 -08003920 pr_info("FDC %d is an 8272A\n", fdc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921 return FDC_8272A; /* 8272a/765 don't know DUMPREGS */
3922 }
3923 if (r != 10) {
Joe Perchesb46df352010-03-10 15:20:46 -08003924 pr_info("FDC %d init: DUMPREGS: unexpected return of %d bytes.\n",
3925 fdc, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926 return FDC_UNKNOWN;
3927 }
3928
3929 if (!fdc_configure()) {
Joe Perchesb46df352010-03-10 15:20:46 -08003930 pr_info("FDC %d is an 82072\n", fdc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 return FDC_82072; /* 82072 doesn't know CONFIGURE */
3932 }
3933
3934 output_byte(FD_PERPENDICULAR);
3935 if (need_more_output() == MORE_OUTPUT) {
3936 output_byte(0);
3937 } else {
Joe Perchesb46df352010-03-10 15:20:46 -08003938 pr_info("FDC %d is an 82072A\n", fdc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939 return FDC_82072A; /* 82072A as found on Sparcs. */
3940 }
3941
3942 output_byte(FD_UNLOCK);
3943 r = result();
3944 if ((r == 1) && (reply_buffer[0] == 0x80)) {
Joe Perchesb46df352010-03-10 15:20:46 -08003945 pr_info("FDC %d is a pre-1991 82077\n", fdc);
Joe Perchesd7b2b2e2010-03-10 15:20:48 -08003946 return FDC_82077_ORIG; /* Pre-1991 82077, doesn't know
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 * LOCK/UNLOCK */
3948 }
3949 if ((r != 1) || (reply_buffer[0] != 0x00)) {
Joe Perchesb46df352010-03-10 15:20:46 -08003950 pr_info("FDC %d init: UNLOCK: unexpected return of %d bytes.\n",
3951 fdc, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952 return FDC_UNKNOWN;
3953 }
3954 output_byte(FD_PARTID);
3955 r = result();
3956 if (r != 1) {
Joe Perchesb46df352010-03-10 15:20:46 -08003957 pr_info("FDC %d init: PARTID: unexpected return of %d bytes.\n",
3958 fdc, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 return FDC_UNKNOWN;
3960 }
3961 if (reply_buffer[0] == 0x80) {
Joe Perchesb46df352010-03-10 15:20:46 -08003962 pr_info("FDC %d is a post-1991 82077\n", fdc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 return FDC_82077; /* Revised 82077AA passes all the tests */
3964 }
3965 switch (reply_buffer[0] >> 5) {
3966 case 0x0:
3967 /* Either a 82078-1 or a 82078SL running at 5Volt */
Joe Perchesb46df352010-03-10 15:20:46 -08003968 pr_info("FDC %d is an 82078.\n", fdc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 return FDC_82078;
3970 case 0x1:
Joe Perchesb46df352010-03-10 15:20:46 -08003971 pr_info("FDC %d is a 44pin 82078\n", fdc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 return FDC_82078;
3973 case 0x2:
Joe Perchesb46df352010-03-10 15:20:46 -08003974 pr_info("FDC %d is a S82078B\n", fdc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 return FDC_S82078B;
3976 case 0x3:
Joe Perchesb46df352010-03-10 15:20:46 -08003977 pr_info("FDC %d is a National Semiconductor PC87306\n", fdc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978 return FDC_87306;
3979 default:
Joe Perchesb46df352010-03-10 15:20:46 -08003980 pr_info("FDC %d init: 82078 variant with unknown PARTID=%d.\n",
3981 fdc, reply_buffer[0] >> 5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 return FDC_82078_UNKN;
3983 }
3984} /* get_fdc_version */
3985
3986/* lilo configuration */
3987
3988static void __init floppy_set_flags(int *ints, int param, int param2)
3989{
3990 int i;
3991
3992 for (i = 0; i < ARRAY_SIZE(default_drive_params); i++) {
3993 if (param)
3994 default_drive_params[i].params.flags |= param2;
3995 else
3996 default_drive_params[i].params.flags &= ~param2;
3997 }
3998 DPRINT("%s flag 0x%x\n", param2 ? "Setting" : "Clearing", param);
3999}
4000
4001static void __init daring(int *ints, int param, int param2)
4002{
4003 int i;
4004
4005 for (i = 0; i < ARRAY_SIZE(default_drive_params); i++) {
4006 if (param) {
4007 default_drive_params[i].params.select_delay = 0;
4008 default_drive_params[i].params.flags |=
4009 FD_SILENT_DCL_CLEAR;
4010 } else {
4011 default_drive_params[i].params.select_delay =
4012 2 * HZ / 100;
4013 default_drive_params[i].params.flags &=
4014 ~FD_SILENT_DCL_CLEAR;
4015 }
4016 }
4017 DPRINT("Assuming %s floppy hardware\n", param ? "standard" : "broken");
4018}
4019
4020static void __init set_cmos(int *ints, int dummy, int dummy2)
4021{
4022 int current_drive = 0;
4023
4024 if (ints[0] != 2) {
4025 DPRINT("wrong number of parameters for CMOS\n");
4026 return;
4027 }
4028 current_drive = ints[1];
4029 if (current_drive < 0 || current_drive >= 8) {
4030 DPRINT("bad drive for set_cmos\n");
4031 return;
4032 }
4033#if N_FDC > 1
4034 if (current_drive >= 4 && !FDC2)
4035 FDC2 = 0x370;
4036#endif
4037 DP->cmos = ints[2];
4038 DPRINT("setting CMOS code to %d\n", ints[2]);
4039}
4040
4041static struct param_table {
4042 const char *name;
4043 void (*fn) (int *ints, int param, int param2);
4044 int *var;
4045 int def_param;
4046 int param2;
4047} config_params[] __initdata = {
4048 {"allowed_drive_mask", NULL, &allowed_drive_mask, 0xff, 0}, /* obsolete */
4049 {"all_drives", NULL, &allowed_drive_mask, 0xff, 0}, /* obsolete */
4050 {"asus_pci", NULL, &allowed_drive_mask, 0x33, 0},
4051 {"irq", NULL, &FLOPPY_IRQ, 6, 0},
4052 {"dma", NULL, &FLOPPY_DMA, 2, 0},
4053 {"daring", daring, NULL, 1, 0},
4054#if N_FDC > 1
4055 {"two_fdc", NULL, &FDC2, 0x370, 0},
4056 {"one_fdc", NULL, &FDC2, 0, 0},
4057#endif
4058 {"thinkpad", floppy_set_flags, NULL, 1, FD_INVERTED_DCL},
4059 {"broken_dcl", floppy_set_flags, NULL, 1, FD_BROKEN_DCL},
4060 {"messages", floppy_set_flags, NULL, 1, FTD_MSG},
4061 {"silent_dcl_clear", floppy_set_flags, NULL, 1, FD_SILENT_DCL_CLEAR},
4062 {"debug", floppy_set_flags, NULL, 1, FD_DEBUG},
4063 {"nodma", NULL, &can_use_virtual_dma, 1, 0},
4064 {"omnibook", NULL, &can_use_virtual_dma, 1, 0},
4065 {"yesdma", NULL, &can_use_virtual_dma, 0, 0},
4066 {"fifo_depth", NULL, &fifo_depth, 0xa, 0},
4067 {"nofifo", NULL, &no_fifo, 0x20, 0},
4068 {"usefifo", NULL, &no_fifo, 0, 0},
4069 {"cmos", set_cmos, NULL, 0, 0},
4070 {"slow", NULL, &slow_floppy, 1, 0},
4071 {"unexpected_interrupts", NULL, &print_unex, 1, 0},
4072 {"no_unexpected_interrupts", NULL, &print_unex, 0, 0},
4073 {"L40SX", NULL, &print_unex, 0, 0}
4074
4075 EXTRA_FLOPPY_PARAMS
4076};
4077
4078static int __init floppy_setup(char *str)
4079{
4080 int i;
4081 int param;
4082 int ints[11];
4083
4084 str = get_options(str, ARRAY_SIZE(ints), ints);
4085 if (str) {
4086 for (i = 0; i < ARRAY_SIZE(config_params); i++) {
4087 if (strcmp(str, config_params[i].name) == 0) {
4088 if (ints[0])
4089 param = ints[1];
4090 else
4091 param = config_params[i].def_param;
4092 if (config_params[i].fn)
Joe Perchesbb57f0c62010-03-10 15:20:50 -08004093 config_params[i].fn(ints, param,
4094 config_params[i].
4095 param2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 if (config_params[i].var) {
4097 DPRINT("%s=%d\n", str, param);
4098 *config_params[i].var = param;
4099 }
4100 return 1;
4101 }
4102 }
4103 }
4104 if (str) {
4105 DPRINT("unknown floppy option [%s]\n", str);
4106
4107 DPRINT("allowed options are:");
4108 for (i = 0; i < ARRAY_SIZE(config_params); i++)
Joe Perchesb46df352010-03-10 15:20:46 -08004109 pr_cont(" %s", config_params[i].name);
4110 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 } else
4112 DPRINT("botched floppy option\n");
Randy Dunlap31c00fc2008-11-13 21:33:24 +00004113 DPRINT("Read Documentation/blockdev/floppy.txt\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 return 0;
4115}
4116
4117static int have_no_fdc = -ENODEV;
4118
Andrew Morton9a8af6b2005-07-27 17:37:34 -07004119static ssize_t floppy_cmos_show(struct device *dev,
4120 struct device_attribute *attr, char *buf)
Hannes Reinecke94fd0db2005-07-15 10:09:25 +02004121{
Eric Miao71b3e0c2009-01-31 22:47:44 +08004122 struct platform_device *p = to_platform_device(dev);
Andrew Morton9a8af6b2005-07-27 17:37:34 -07004123 int drive;
Hannes Reinecke94fd0db2005-07-15 10:09:25 +02004124
Andrew Morton9a8af6b2005-07-27 17:37:34 -07004125 drive = p->id;
4126 return sprintf(buf, "%X\n", UDP->cmos);
Hannes Reinecke94fd0db2005-07-15 10:09:25 +02004127}
Joe Perches48c8cee2010-03-10 15:20:45 -08004128
4129DEVICE_ATTR(cmos, S_IRUGO, floppy_cmos_show, NULL);
Hannes Reinecke94fd0db2005-07-15 10:09:25 +02004130
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131static void floppy_device_release(struct device *dev)
4132{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133}
4134
Frans Popc90cd332009-07-25 22:24:54 +02004135static int floppy_resume(struct device *dev)
Ondrej Zary5e50b9e2009-06-10 12:57:09 -07004136{
4137 int fdc;
4138
4139 for (fdc = 0; fdc < N_FDC; fdc++)
4140 if (FDCS->address != -1)
Joe Perches74f63f42010-03-10 15:20:58 -08004141 user_reset_fdc(-1, FD_RESET_ALWAYS, false);
Ondrej Zary5e50b9e2009-06-10 12:57:09 -07004142
4143 return 0;
4144}
4145
Alexey Dobriyan47145212009-12-14 18:00:08 -08004146static const struct dev_pm_ops floppy_pm_ops = {
Ondrej Zary5e50b9e2009-06-10 12:57:09 -07004147 .resume = floppy_resume,
Frans Popc90cd332009-07-25 22:24:54 +02004148 .restore = floppy_resume,
4149};
4150
4151static struct platform_driver floppy_driver = {
Ondrej Zary5e50b9e2009-06-10 12:57:09 -07004152 .driver = {
Joe Perchesbb57f0c62010-03-10 15:20:50 -08004153 .name = "floppy",
4154 .pm = &floppy_pm_ops,
Ondrej Zary5e50b9e2009-06-10 12:57:09 -07004155 },
4156};
4157
Hannes Reinecke94fd0db2005-07-15 10:09:25 +02004158static struct platform_device floppy_device[N_DRIVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159
4160static struct kobject *floppy_find(dev_t dev, int *part, void *data)
4161{
4162 int drive = (*part & 3) | ((*part & 0x80) >> 5);
4163 if (drive >= N_DRIVE ||
4164 !(allowed_drive_mask & (1 << drive)) ||
4165 fdc_state[FDC(drive)].version == FDC_NONE)
4166 return NULL;
Tobias Klauser945f3902006-01-08 01:05:11 -08004167 if (((*part >> 2) & 0x1f) >= ARRAY_SIZE(floppy_type))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168 return NULL;
4169 *part = 0;
4170 return get_disk(disks[drive]);
4171}
4172
4173static int __init floppy_init(void)
4174{
4175 int i, unit, drive;
4176 int err, dr;
4177
Kumar Gala68e1ee62008-09-22 14:41:31 -07004178#if defined(CONFIG_PPC)
Olaf Heringef16b512006-08-31 21:27:41 -07004179 if (check_legacy_ioport(FDC1))
4180 return -ENODEV;
4181#endif
4182
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 raw_cmd = NULL;
4184
4185 for (dr = 0; dr < N_DRIVE; dr++) {
4186 disks[dr] = alloc_disk(1);
4187 if (!disks[dr]) {
4188 err = -ENOMEM;
4189 goto out_put_disk;
4190 }
4191
4192 disks[dr]->major = FLOPPY_MAJOR;
4193 disks[dr]->first_minor = TOMINOR(dr);
4194 disks[dr]->fops = &floppy_fops;
4195 sprintf(disks[dr]->disk_name, "fd%d", dr);
4196
4197 init_timer(&motor_off_timer[dr]);
4198 motor_off_timer[dr].data = dr;
4199 motor_off_timer[dr].function = motor_off_callback;
4200 }
4201
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202 err = register_blkdev(FLOPPY_MAJOR, "fd");
4203 if (err)
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07004204 goto out_put_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205
Ondrej Zary5e50b9e2009-06-10 12:57:09 -07004206 err = platform_driver_register(&floppy_driver);
4207 if (err)
4208 goto out_unreg_blkdev;
4209
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210 floppy_queue = blk_init_queue(do_fd_request, &floppy_lock);
4211 if (!floppy_queue) {
4212 err = -ENOMEM;
Ondrej Zary5e50b9e2009-06-10 12:57:09 -07004213 goto out_unreg_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214 }
Martin K. Petersen086fa5f2010-02-26 00:20:38 -05004215 blk_queue_max_hw_sectors(floppy_queue, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216
4217 blk_register_region(MKDEV(FLOPPY_MAJOR, 0), 256, THIS_MODULE,
4218 floppy_find, NULL, NULL);
4219
4220 for (i = 0; i < 256; i++)
4221 if (ITYPE(i))
4222 floppy_sizes[i] = floppy_type[ITYPE(i)].size;
4223 else
4224 floppy_sizes[i] = MAX_DISK_SIZE << 1;
4225
Joe Perches73507e62010-03-10 15:21:03 -08004226 reschedule_timeout(MAXTIMEOUT, "floppy init");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 config_types();
4228
4229 for (i = 0; i < N_FDC; i++) {
4230 fdc = i;
Joe Perchesb87c9e02010-03-10 15:20:50 -08004231 memset(FDCS, 0, sizeof(*FDCS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 FDCS->dtr = -1;
4233 FDCS->dor = 0x4;
4234#if defined(__sparc__) || defined(__mc68000__)
Joe Perches96534f12010-03-10 15:20:51 -08004235 /*sparcs/sun3x don't have a DOR reset which we can fall back on to */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236#ifdef __mc68000__
4237 if (MACH_IS_SUN3X)
4238#endif
4239 FDCS->version = FDC_82072A;
4240#endif
4241 }
4242
4243 use_virtual_dma = can_use_virtual_dma & 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244 fdc_state[0].address = FDC1;
4245 if (fdc_state[0].address == -1) {
4246 del_timer(&fd_timeout);
4247 err = -ENODEV;
4248 goto out_unreg_region;
4249 }
4250#if N_FDC > 1
4251 fdc_state[1].address = FDC2;
4252#endif
4253
4254 fdc = 0; /* reset fdc in case of unexpected interrupt */
4255 err = floppy_grab_irq_and_dma();
4256 if (err) {
4257 del_timer(&fd_timeout);
4258 err = -EBUSY;
4259 goto out_unreg_region;
4260 }
4261
4262 /* initialise drive state */
4263 for (drive = 0; drive < N_DRIVE; drive++) {
Joe Perchesb87c9e02010-03-10 15:20:50 -08004264 memset(UDRS, 0, sizeof(*UDRS));
4265 memset(UDRWE, 0, sizeof(*UDRWE));
Joe Perchese0298532010-03-10 15:20:55 -08004266 set_bit(FD_DISK_NEWCHANGE_BIT, &UDRS->flags);
4267 set_bit(FD_DISK_CHANGED_BIT, &UDRS->flags);
4268 set_bit(FD_VERIFY_BIT, &UDRS->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 UDRS->fd_device = -1;
4270 floppy_track_buffer = NULL;
4271 max_buffer_sectors = 0;
4272 }
4273 /*
4274 * Small 10 msec delay to let through any interrupt that
4275 * initialization might have triggered, to not
4276 * confuse detection:
4277 */
4278 msleep(10);
4279
4280 for (i = 0; i < N_FDC; i++) {
4281 fdc = i;
4282 FDCS->driver_version = FD_DRIVER_VERSION;
4283 for (unit = 0; unit < 4; unit++)
4284 FDCS->track[unit] = 0;
4285 if (FDCS->address == -1)
4286 continue;
4287 FDCS->rawcmd = 2;
Joe Perches74f63f42010-03-10 15:20:58 -08004288 if (user_reset_fdc(-1, FD_RESET_ALWAYS, false)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289 /* free ioports reserved by floppy_grab_irq_and_dma() */
Philippe De Muyter5a74db02009-02-18 14:48:36 -08004290 floppy_release_regions(fdc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 FDCS->address = -1;
4292 FDCS->version = FDC_NONE;
4293 continue;
4294 }
4295 /* Try to determine the floppy controller type */
4296 FDCS->version = get_fdc_version();
4297 if (FDCS->version == FDC_NONE) {
4298 /* free ioports reserved by floppy_grab_irq_and_dma() */
Philippe De Muyter5a74db02009-02-18 14:48:36 -08004299 floppy_release_regions(fdc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 FDCS->address = -1;
4301 continue;
4302 }
4303 if (can_use_virtual_dma == 2 && FDCS->version < FDC_82072A)
4304 can_use_virtual_dma = 0;
4305
4306 have_no_fdc = 0;
4307 /* Not all FDCs seem to be able to handle the version command
4308 * properly, so force a reset for the standard FDC clones,
4309 * to avoid interrupt garbage.
4310 */
Joe Perches74f63f42010-03-10 15:20:58 -08004311 user_reset_fdc(-1, FD_RESET_ALWAYS, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312 }
4313 fdc = 0;
4314 del_timer(&fd_timeout);
4315 current_drive = 0;
Joe Perches29f1c782010-03-10 15:21:00 -08004316 initialized = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 if (have_no_fdc) {
4318 DPRINT("no floppy controllers found\n");
4319 err = have_no_fdc;
4320 goto out_flush_work;
4321 }
4322
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 for (drive = 0; drive < N_DRIVE; drive++) {
4324 if (!(allowed_drive_mask & (1 << drive)))
4325 continue;
4326 if (fdc_state[FDC(drive)].version == FDC_NONE)
4327 continue;
Hannes Reinecke94fd0db2005-07-15 10:09:25 +02004328
4329 floppy_device[drive].name = floppy_device_name;
4330 floppy_device[drive].id = drive;
4331 floppy_device[drive].dev.release = floppy_device_release;
4332
4333 err = platform_device_register(&floppy_device[drive]);
4334 if (err)
4335 goto out_flush_work;
4336
Joe Perchesd7b2b2e2010-03-10 15:20:48 -08004337 err = device_create_file(&floppy_device[drive].dev,
4338 &dev_attr_cmos);
Dmitriy Monakhov4ea1b0f2007-05-08 00:25:58 -07004339 if (err)
4340 goto out_unreg_platform_dev;
4341
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 /* to be cleaned up... */
4343 disks[drive]->private_data = (void *)(long)drive;
4344 disks[drive]->queue = floppy_queue;
4345 disks[drive]->flags |= GENHD_FL_REMOVABLE;
Hannes Reinecke94fd0db2005-07-15 10:09:25 +02004346 disks[drive]->driverfs_dev = &floppy_device[drive].dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 add_disk(disks[drive]);
4348 }
4349
4350 return 0;
4351
Dmitriy Monakhov4ea1b0f2007-05-08 00:25:58 -07004352out_unreg_platform_dev:
4353 platform_device_unregister(&floppy_device[drive]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354out_flush_work:
4355 flush_scheduled_work();
4356 if (usage_count)
4357 floppy_release_irq_and_dma();
4358out_unreg_region:
4359 blk_unregister_region(MKDEV(FLOPPY_MAJOR, 0), 256);
4360 blk_cleanup_queue(floppy_queue);
Ondrej Zary5e50b9e2009-06-10 12:57:09 -07004361out_unreg_driver:
4362 platform_driver_unregister(&floppy_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363out_unreg_blkdev:
4364 unregister_blkdev(FLOPPY_MAJOR, "fd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365out_put_disk:
4366 while (dr--) {
4367 del_timer(&motor_off_timer[dr]);
4368 put_disk(disks[dr]);
4369 }
4370 return err;
4371}
4372
4373static DEFINE_SPINLOCK(floppy_usage_lock);
4374
Philippe De Muyter5a74db02009-02-18 14:48:36 -08004375static const struct io_region {
4376 int offset;
4377 int size;
4378} io_regions[] = {
4379 { 2, 1 },
4380 /* address + 3 is sometimes reserved by pnp bios for motherboard */
4381 { 4, 2 },
4382 /* address + 6 is reserved, and may be taken by IDE.
4383 * Unfortunately, Adaptec doesn't know this :-(, */
4384 { 7, 1 },
4385};
4386
4387static void floppy_release_allocated_regions(int fdc, const struct io_region *p)
4388{
4389 while (p != io_regions) {
4390 p--;
4391 release_region(FDCS->address + p->offset, p->size);
4392 }
4393}
4394
4395#define ARRAY_END(X) (&((X)[ARRAY_SIZE(X)]))
4396
4397static int floppy_request_regions(int fdc)
4398{
4399 const struct io_region *p;
4400
4401 for (p = io_regions; p < ARRAY_END(io_regions); p++) {
Joe Perchesbb57f0c62010-03-10 15:20:50 -08004402 if (!request_region(FDCS->address + p->offset,
4403 p->size, "floppy")) {
4404 DPRINT("Floppy io-port 0x%04lx in use\n",
4405 FDCS->address + p->offset);
Philippe De Muyter5a74db02009-02-18 14:48:36 -08004406 floppy_release_allocated_regions(fdc, p);
4407 return -EBUSY;
4408 }
4409 }
4410 return 0;
4411}
4412
4413static void floppy_release_regions(int fdc)
4414{
4415 floppy_release_allocated_regions(fdc, ARRAY_END(io_regions));
4416}
4417
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418static int floppy_grab_irq_and_dma(void)
4419{
4420 unsigned long flags;
4421
4422 spin_lock_irqsave(&floppy_usage_lock, flags);
4423 if (usage_count++) {
4424 spin_unlock_irqrestore(&floppy_usage_lock, flags);
4425 return 0;
4426 }
4427 spin_unlock_irqrestore(&floppy_usage_lock, flags);
Ingo Molnar6dc659d2006-03-26 01:36:54 -08004428
4429 /*
4430 * We might have scheduled a free_irq(), wait it to
4431 * drain first:
4432 */
4433 flush_scheduled_work();
4434
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 if (fd_request_irq()) {
4436 DPRINT("Unable to grab IRQ%d for the floppy driver\n",
4437 FLOPPY_IRQ);
4438 spin_lock_irqsave(&floppy_usage_lock, flags);
4439 usage_count--;
4440 spin_unlock_irqrestore(&floppy_usage_lock, flags);
4441 return -1;
4442 }
4443 if (fd_request_dma()) {
4444 DPRINT("Unable to grab DMA%d for the floppy driver\n",
4445 FLOPPY_DMA);
Jan Beulich2e9c47c2007-10-16 23:27:32 -07004446 if (can_use_virtual_dma & 2)
4447 use_virtual_dma = can_use_virtual_dma = 1;
4448 if (!(can_use_virtual_dma & 1)) {
4449 fd_free_irq();
4450 spin_lock_irqsave(&floppy_usage_lock, flags);
4451 usage_count--;
4452 spin_unlock_irqrestore(&floppy_usage_lock, flags);
4453 return -1;
4454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 }
4456
4457 for (fdc = 0; fdc < N_FDC; fdc++) {
4458 if (FDCS->address != -1) {
Philippe De Muyter5a74db02009-02-18 14:48:36 -08004459 if (floppy_request_regions(fdc))
4460 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 }
4462 }
4463 for (fdc = 0; fdc < N_FDC; fdc++) {
4464 if (FDCS->address != -1) {
4465 reset_fdc_info(1);
4466 fd_outb(FDCS->dor, FD_DOR);
4467 }
4468 }
4469 fdc = 0;
4470 set_dor(0, ~0, 8); /* avoid immediate interrupt */
4471
4472 for (fdc = 0; fdc < N_FDC; fdc++)
4473 if (FDCS->address != -1)
4474 fd_outb(FDCS->dor, FD_DOR);
4475 /*
Jesper Juhl06f748c2007-10-16 23:30:57 -07004476 * The driver will try and free resources and relies on us
4477 * to know if they were allocated or not.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 */
4479 fdc = 0;
4480 irqdma_allocated = 1;
4481 return 0;
Philippe De Muyter5a74db02009-02-18 14:48:36 -08004482cleanup:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483 fd_free_irq();
4484 fd_free_dma();
Philippe De Muyter5a74db02009-02-18 14:48:36 -08004485 while (--fdc >= 0)
4486 floppy_release_regions(fdc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487 spin_lock_irqsave(&floppy_usage_lock, flags);
4488 usage_count--;
4489 spin_unlock_irqrestore(&floppy_usage_lock, flags);
4490 return -1;
4491}
4492
4493static void floppy_release_irq_and_dma(void)
4494{
4495 int old_fdc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496#ifndef __sparc__
4497 int drive;
4498#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499 long tmpsize;
4500 unsigned long tmpaddr;
4501 unsigned long flags;
4502
4503 spin_lock_irqsave(&floppy_usage_lock, flags);
4504 if (--usage_count) {
4505 spin_unlock_irqrestore(&floppy_usage_lock, flags);
4506 return;
4507 }
4508 spin_unlock_irqrestore(&floppy_usage_lock, flags);
4509 if (irqdma_allocated) {
4510 fd_disable_dma();
4511 fd_free_dma();
Ingo Molnar3e541a42006-07-03 00:24:23 -07004512 fd_free_irq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513 irqdma_allocated = 0;
4514 }
4515 set_dor(0, ~0, 8);
4516#if N_FDC > 1
4517 set_dor(1, ~8, 0);
4518#endif
4519 floppy_enable_hlt();
4520
4521 if (floppy_track_buffer && max_buffer_sectors) {
4522 tmpsize = max_buffer_sectors * 1024;
4523 tmpaddr = (unsigned long)floppy_track_buffer;
4524 floppy_track_buffer = NULL;
4525 max_buffer_sectors = 0;
4526 buffer_min = buffer_max = -1;
4527 fd_dma_mem_free(tmpaddr, tmpsize);
4528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529#ifndef __sparc__
4530 for (drive = 0; drive < N_FDC * 4; drive++)
4531 if (timer_pending(motor_off_timer + drive))
Joe Perchesb46df352010-03-10 15:20:46 -08004532 pr_info("motor off timer %d still active\n", drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533#endif
4534
4535 if (timer_pending(&fd_timeout))
Joe Perchesb46df352010-03-10 15:20:46 -08004536 pr_info("floppy timer still active:%s\n", timeout_message);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537 if (timer_pending(&fd_timer))
Joe Perchesb46df352010-03-10 15:20:46 -08004538 pr_info("auxiliary floppy timer still active\n");
David Howells365970a2006-11-22 14:54:49 +00004539 if (work_pending(&floppy_work))
Joe Perchesb46df352010-03-10 15:20:46 -08004540 pr_info("work still pending\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541 old_fdc = fdc;
4542 for (fdc = 0; fdc < N_FDC; fdc++)
Philippe De Muyter5a74db02009-02-18 14:48:36 -08004543 if (FDCS->address != -1)
4544 floppy_release_regions(fdc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545 fdc = old_fdc;
4546}
4547
4548#ifdef MODULE
4549
4550static char *floppy;
4551
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552static void __init parse_floppy_cfg_string(char *cfg)
4553{
4554 char *ptr;
4555
4556 while (*cfg) {
Joe Perchesbb57f0c62010-03-10 15:20:50 -08004557 ptr = cfg;
4558 while (*cfg && *cfg != ' ' && *cfg != '\t')
4559 cfg++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560 if (*cfg) {
4561 *cfg = '\0';
4562 cfg++;
4563 }
4564 if (*ptr)
4565 floppy_setup(ptr);
4566 }
4567}
4568
Jon Schindler7afea3b2008-04-29 00:59:21 -07004569static int __init floppy_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570{
4571 if (floppy)
4572 parse_floppy_cfg_string(floppy);
4573 return floppy_init();
4574}
Jon Schindler7afea3b2008-04-29 00:59:21 -07004575module_init(floppy_module_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576
Jon Schindler7afea3b2008-04-29 00:59:21 -07004577static void __exit floppy_module_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578{
4579 int drive;
4580
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581 blk_unregister_region(MKDEV(FLOPPY_MAJOR, 0), 256);
4582 unregister_blkdev(FLOPPY_MAJOR, "fd");
Ondrej Zary5e50b9e2009-06-10 12:57:09 -07004583 platform_driver_unregister(&floppy_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584
4585 for (drive = 0; drive < N_DRIVE; drive++) {
4586 del_timer_sync(&motor_off_timer[drive]);
4587
4588 if ((allowed_drive_mask & (1 << drive)) &&
4589 fdc_state[FDC(drive)].version != FDC_NONE) {
4590 del_gendisk(disks[drive]);
Hannes Reinecke94fd0db2005-07-15 10:09:25 +02004591 device_remove_file(&floppy_device[drive].dev, &dev_attr_cmos);
4592 platform_device_unregister(&floppy_device[drive]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593 }
4594 put_disk(disks[drive]);
4595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596
4597 del_timer_sync(&fd_timeout);
4598 del_timer_sync(&fd_timer);
4599 blk_cleanup_queue(floppy_queue);
4600
4601 if (usage_count)
4602 floppy_release_irq_and_dma();
4603
4604 /* eject disk, if any */
4605 fd_eject(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606}
Joe Perches48c8cee2010-03-10 15:20:45 -08004607
Jon Schindler7afea3b2008-04-29 00:59:21 -07004608module_exit(floppy_module_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609
4610module_param(floppy, charp, 0);
4611module_param(FLOPPY_IRQ, int, 0);
4612module_param(FLOPPY_DMA, int, 0);
4613MODULE_AUTHOR("Alain L. Knaff");
4614MODULE_SUPPORTED_DEVICE("fd");
4615MODULE_LICENSE("GPL");
4616
Scott James Remnant83f9ef42009-04-02 16:56:47 -07004617/* This doesn't actually get used other than for module information */
4618static const struct pnp_device_id floppy_pnpids[] = {
Joe Perches48c8cee2010-03-10 15:20:45 -08004619 {"PNP0700", 0},
4620 {}
Scott James Remnant83f9ef42009-04-02 16:56:47 -07004621};
Joe Perches48c8cee2010-03-10 15:20:45 -08004622
Scott James Remnant83f9ef42009-04-02 16:56:47 -07004623MODULE_DEVICE_TABLE(pnp, floppy_pnpids);
4624
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625#else
4626
4627__setup("floppy=", floppy_setup);
4628module_init(floppy_init)
4629#endif
4630
4631MODULE_ALIAS_BLOCKDEV_MAJOR(FLOPPY_MAJOR);