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