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