1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/drivers/block/floppy.c
5 * Copyright (C) 1991, 1992 Linus Torvalds
6 * Copyright (C) 1993, 1994 Alain Knaff
7 * Copyright (C) 1998 Alan Cox
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.
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.
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
34 * 28.02.92 - made track-buffering routines, based on the routines written
35 * by entropy@wintermute.wpi.edu (Lawrence Foard). Linus.
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.
45 * 1992/7/22 -- Hennus Bergman: Added better error reporting, fixed
46 * FDC data overrun bug, added some preliminary stuff for vertical
49 * 1992/9/17: Added DMA allocation & DMA functions. -- hhb.
51 * TODO: Errors are still not counted properly.
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.
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
70 /* 1994/6/24 --bbroad-- added the floppy table entries and made
71 * minor modifications to allow 2.88 floppies to be run.
74 /* 1994/7/13 -- Paul Vojta -- modified the probing code to allow three or more
79 * 1994/8/8 -- Alain Knaff -- Switched to fdpatch driver: Support for bigger
80 * format bug fixes, but unfortunately some new bugs too...
83 /* 1994/9/17 -- Koen Holtman -- added logging of physical floppy write
84 * errors to allow safe writing by specialized programs.
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").
94 * 1995/8/26 -- Andreas Busse -- added Mips support.
98 * 1995/10/18 -- Ralf Baechle -- Portability cleanup; move machine dependent
99 * features to asm/floppy.h.
103 * 1998/1/21 -- Richard Gooch <rgooch@atnf.csiro.au> -- devfs support
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.
113 * 1998/06/07 -- Alan Cox -- Merged the 2.0.34 fixes for resource allocation
118 * 1998/09/20 -- David Weinehall -- Added slow-down code for buggy PS/2-drives.
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).
128 * 2000/08/28 -- Arnaldo Carvalho de Melo <acme@conectiva.com.br>
129 * - get rid of check_region
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.)
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.
144 /* 2003/07/28 -- Daniele Bellucci <bellucda@tiscali.it>.
145 * Better audit of register_blkdev.
148 #define REALLY_SLOW_IO
152 #define DPRINT(format, args...) \
153 pr_info("floppy%d: " format, current_drive, ##args)
155 #define DCL_DEBUG /* debug disk change line */
157 #define debug_dcl(test, fmt, args...) \
158 do { if ((test) & FD_DEBUG) DPRINT(fmt, ##args); } while (0)
160 #define debug_dcl(test, fmt, args...) \
161 do { if (0) DPRINT(fmt, ##args); } while (0)
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>
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.
201 static DEFINE_MUTEX(floppy_mutex);
202 static int slow_floppy;
207 static int FLOPPY_IRQ = 6;
208 static int FLOPPY_DMA = 2;
209 static int can_use_virtual_dma = 2;
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
218 static int use_virtual_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
230 static DEFINE_SPINLOCK(floppy_lock);
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);
236 #define K_64 0x10000 /* 64KB */
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.
244 * NOTE: This must come before we include the arch floppy header because
245 * some ports reference this variable from there. -DaveM
248 static int allowed_drive_mask = 0x33;
250 #include <asm/floppy.h>
252 static int irqdma_allocated;
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>
259 static LIST_HEAD(floppy_reqs);
260 static struct request *current_req;
261 static int set_next_request(void);
263 #ifndef fd_get_dma_residue
264 #define fd_get_dma_residue() get_dma_residue(FLOPPY_DMA)
267 /* Dma Memory related stuff */
269 #ifndef fd_dma_mem_free
270 #define fd_dma_mem_free(addr, size) free_pages(addr, get_order(size))
273 #ifndef fd_dma_mem_alloc
274 #define fd_dma_mem_alloc(size) __get_dma_pages(GFP_KERNEL, get_order(size))
277 #ifndef fd_cacheflush
278 #define fd_cacheflush(addr, size) /* nothing... */
281 static inline void fallback_on_nodma_alloc(char **addr, size_t l)
283 #ifdef FLOPPY_CAN_FALLBACK_ON_NODMA
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);
295 /* End dma memory related stuff */
297 static unsigned long fake_change;
298 static bool initialized;
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))
307 #define PH_HEAD(floppy, head) (((((floppy)->stretch & 2) >> 1) ^ head) << 2)
308 #define STRETCH(floppy) ((floppy)->stretch & FD_STRETCH)
310 /* read/write commands */
317 #define SECT_PER_TRACK 6
322 /* format commands */
324 #define F_SECT_PER_TRACK 3
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]
334 #define MAX_DISK_SIZE 4 /* 3984 */
337 * globals used by 'result()'
339 static unsigned char reply_buffer[FD_RAW_REPLY_SIZE];
340 static int inr; /* size of reply buffer, when called from interrupt */
344 #define ST3 0 /* result of GETSTATUS */
350 #define SEL_DLY (2 * HZ / 100)
353 * this struct defines the different floppy drive types.
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!
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" },
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*/
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*/
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*/
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*/
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*/
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
397 * Frequency of disk change checks */
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;
410 * This struct defines the different floppy types.
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
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).
428 * Other parameters should be self-explanatory (see also setfdprm(8)).
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" */
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" */
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" */
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" */
478 static struct gendisk *disks[N_DRIVE][ARRAY_SIZE(floppy_type)];
480 #define SECTSIZE (_FD_SECTSIZE(*floppy))
482 /* Auto-detection: Disk type used until the next media change occurs. */
483 static struct floppy_struct *current_type[N_DRIVE];
486 * User-provided type information. current_type points to
487 * the respective entry of this array.
489 static struct floppy_struct user_params[N_DRIVE];
491 static sector_t floppy_sizes[256];
493 static char floppy_device_name[] = "floppy";
496 * The driver is trying to determine the correct media format
497 * while probing is set. rw_interrupt() clears it after a
502 /* Synchronization of FDC access. */
503 #define FD_COMMAND_NONE -1
504 #define FD_COMMAND_ERROR 2
505 #define FD_COMMAND_OKAY 3
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);
512 /* Errors during formatting are counted here. */
513 static int format_errors;
515 /* Format request descriptor. */
516 static struct format_descr format_req;
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)
526 * Because these are written to by the DMA controller, they must
527 * not contain a 64k byte boundary crossing, or data will be
530 static char *floppy_track_buffer;
531 static int max_buffer_sectors;
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
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 */
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 *);
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);
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.
563 static void reset_fdc(void);
564 static int floppy_revalidate(struct gendisk *disk);
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
572 #define NEED_1_RECAL -2
573 #define NEED_2_RECAL -3
575 static atomic_t usage_count = ATOMIC_INIT(0);
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;
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 */
587 static struct workqueue_struct *floppy_wq;
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 */
596 static inline unsigned char fdc_inb(int fdc, int reg)
598 return fd_inb(fdc_state[fdc].address, reg);
601 static inline void fdc_outb(unsigned char value, int fdc, int reg)
603 fd_outb(value, fdc_state[fdc].address, reg);
606 static inline bool drive_no_geom(int drive)
608 return !current_type[drive] && !ITYPE(drive_state[drive].fd_device);
612 static inline int fd_eject(int drive)
623 static long unsigned debugtimer;
625 static inline void set_debugt(void)
627 debugtimer = jiffies;
630 static inline void debugt(const char *func, const char *msg)
632 if (drive_params[current_drive].flags & DEBUGT)
633 pr_info("%s:%s dtime=%lu\n", func, msg, jiffies - debugtimer);
636 static inline void set_debugt(void) { }
637 static inline void debugt(const char *func, const char *msg) { }
641 static DECLARE_DELAYED_WORK(fd_timeout, floppy_shutdown);
642 static const char *timeout_message;
644 static void is_alive(const char *func, const char *message)
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);
653 static void (*do_floppy)(void) = NULL;
657 static void (*lasthandler)(void);
658 static unsigned long interruptjiffies;
659 static unsigned long resultjiffies;
660 static int resultsize;
661 static unsigned long lastredo;
663 static struct output_log {
665 unsigned char status;
666 unsigned long jiffies;
667 } output_log[OLOGSIZE];
669 static int output_log_pos;
671 #define MAXTIMEOUT -2
673 static void __reschedule_timeout(int drive, const char *message)
677 if (drive < 0 || drive >= N_DRIVE) {
681 delay = drive_params[drive].timeout;
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;
689 static void reschedule_timeout(int drive, const char *message)
693 spin_lock_irqsave(&floppy_lock, flags);
694 __reschedule_timeout(drive, message);
695 spin_unlock_irqrestore(&floppy_lock, flags);
698 #define INFBOUND(a, b) (a) = max_t(int, a, b)
699 #define SUPBOUND(a, b) (a) = min_t(int, a, b)
702 * Bottom half floppy driver.
703 * ==========================
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)
711 * This routine is responsible for maintaining the FD_DISK_CHANGE flag,
712 * and the last_checked date.
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
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
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.
730 * This means that we really have a third possibility too:
731 * The floppy has been changed after the last seek.
734 static int disk_change(int drive)
736 int fdc = FDC(drive);
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);
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);
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 */
762 if (drive_state[drive].maxblock) /* mark it changed */
763 set_bit(FD_DISK_CHANGED_BIT,
764 &drive_state[drive].flags);
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;
777 drive_state[drive].last_checked = jiffies;
778 clear_bit(FD_DISK_NEWCHANGE_BIT, &drive_state[drive].flags);
783 static inline int is_selected(int dor, int unit)
785 return ((dor & (0x10 << unit)) && (dor & 3) == unit);
788 static bool is_ready_state(int status)
790 int state = status & (STATUS_READY | STATUS_DIR | STATUS_DMA);
791 return state == STATUS_READY;
794 static int set_dor(int fdc, char mask, char data)
798 unsigned char newdor;
799 unsigned char olddor;
801 if (fdc_state[fdc].address == -1)
804 olddor = fdc_state[fdc].dor;
805 newdor = (olddor & mask) | data;
806 if (newdor != olddor) {
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");
814 fdc_state[fdc].dor = newdor;
815 fdc_outb(newdor, fdc, FD_DOR);
818 if (!is_selected(olddor, unit) && is_selected(newdor, unit)) {
819 drive = REVDRIVE(fdc, unit);
820 drive_state[drive].select_date = jiffies;
826 static void twaddle(int fdc, int drive)
828 if (drive_params[drive].select_delay)
830 fdc_outb(fdc_state[fdc].dor & ~(0x10 << UNIT(drive)),
832 fdc_outb(fdc_state[fdc].dor, fdc, FD_DOR);
833 drive_state[drive].select_date = jiffies;
837 * Reset all driver information about the specified fdc.
838 * This is needed after a reset, and after a raw command.
840 static void reset_fdc_info(int fdc, int mode)
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;
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.
858 static void set_fdc(int drive)
862 if (drive < 0 || drive >= N_DRIVE) {
863 pr_info("bad drive value %d\n", drive);
869 pr_info("bad fdc value\n");
875 set_dor(1 - fdc, ~8, 0);
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;
882 current_drive = drive;
888 * Both current_drive and current_fdc are changed to match the new drive.
890 static int lock_fdc(int drive)
892 if (WARN(atomic_read(&usage_count) == 0,
893 "Trying to lock fdc while usage count=0\n"))
896 if (wait_event_interruptible(fdc_wait, !test_and_set_bit(0, &fdc_busy)))
899 command_status = FD_COMMAND_NONE;
901 reschedule_timeout(drive, "lock fdc");
906 /* unlocks the driver */
907 static void unlock_fdc(void)
909 if (!test_bit(0, &fdc_busy))
910 DPRINT("FDC access conflict!\n");
913 command_status = FD_COMMAND_NONE;
914 cancel_delayed_work(&fd_timeout);
917 clear_bit(0, &fdc_busy);
921 /* switches the motor off after a given timeout */
922 static void motor_off_callback(struct timer_list *t)
924 unsigned long nr = t - motor_off_timer;
925 unsigned char mask = ~(0x10 << UNIT(nr));
927 if (WARN_ON_ONCE(nr >= N_DRIVE))
930 set_dor(FDC(nr), mask, 0);
933 /* schedules motor off */
934 static void floppy_off(unsigned int drive)
936 unsigned long volatile delta;
937 int fdc = FDC(drive);
939 if (!(fdc_state[fdc].dor & (0x10 << UNIT(drive))))
942 del_timer(motor_off_timer + drive);
944 /* make spindle stop in a position which minimizes spinup 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;
953 add_timer(motor_off_timer + drive);
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.
961 static void scandrives(void)
967 if (drive_params[current_drive].select_delay)
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 */
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
980 set_dor(current_fdc, ~(0x10 << UNIT(drive)), 0);
982 set_fdc(saved_drive);
985 static void empty(void)
989 static void (*floppy_work_fn)(void);
991 static void floppy_work_workfn(struct work_struct *work)
996 static DECLARE_WORK(floppy_work, floppy_work_workfn);
998 static void schedule_bh(void (*handler)(void))
1000 WARN_ON(work_pending(&floppy_work));
1002 floppy_work_fn = handler;
1003 queue_work(floppy_wq, &floppy_work);
1006 static void (*fd_timer_fn)(void) = NULL;
1008 static void fd_timer_workfn(struct work_struct *work)
1013 static DECLARE_DELAYED_WORK(fd_timer, fd_timer_workfn);
1015 static void cancel_activity(void)
1018 cancel_delayed_work(&fd_timer);
1019 cancel_work_sync(&floppy_work);
1022 /* this function makes sure that the disk stays in the drive during the
1024 static void fd_watchdog(void)
1026 debug_dcl(drive_params[current_drive].flags,
1027 "calling disk change from watchdog\n");
1029 if (disk_change(current_drive)) {
1030 DPRINT("disk removed during i/o\n");
1035 cancel_delayed_work(&fd_timer);
1036 fd_timer_fn = fd_watchdog;
1037 queue_delayed_work(floppy_wq, &fd_timer, HZ / 10);
1041 static void main_command_interrupt(void)
1043 cancel_delayed_work(&fd_timer);
1047 /* waits for a delay (spinup or select) to pass */
1048 static int fd_wait_for_completion(unsigned long expires,
1049 void (*function)(void))
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 */
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);
1067 static void setup_DMA(void)
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);
1076 fdc_state[current_fdc].reset = 1;
1079 if (((unsigned long)raw_cmd->kernel_data) % 512) {
1080 pr_info("non aligned address: %p\n", raw_cmd->kernel_data);
1082 fdc_state[current_fdc].reset = 1;
1085 f = claim_dma_lock();
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);
1094 fdc_state[current_fdc].reset = 1;
1097 release_dma_lock(f);
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;
1107 release_dma_lock(f);
1111 static void show_floppy(int fdc);
1113 /* waits until the fdc becomes ready */
1114 static int wait_til_ready(int fdc)
1119 if (fdc_state[fdc].reset)
1121 for (counter = 0; counter < 10000; counter++) {
1122 status = fdc_inb(fdc, FD_STATUS);
1123 if (status & STATUS_READY)
1127 DPRINT("Getstatus times out (%x) on fdc %d\n", status, fdc);
1130 fdc_state[fdc].reset = 1;
1134 /* sends a command byte to the fdc */
1135 static int output_byte(int fdc, char byte)
1137 int status = wait_til_ready(fdc);
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;
1150 fdc_state[fdc].reset = 1;
1152 DPRINT("Unable to send byte %x to FDC. Fdc=%x Status=%x\n",
1159 /* gets the response from the fdc */
1160 static int result(int fdc)
1165 for (i = 0; i < FD_RAW_REPLY_SIZE; i++) {
1166 status = wait_til_ready(fdc);
1169 status &= STATUS_DIR | STATUS_READY | STATUS_BUSY | STATUS_DMA;
1170 if ((status & ~STATUS_BUSY) == STATUS_READY) {
1171 resultjiffies = jiffies;
1175 if (status == (STATUS_DIR | STATUS_READY | STATUS_BUSY))
1176 reply_buffer[i] = fdc_inb(fdc, FD_DATA);
1181 DPRINT("get result error. Fdc=%d Last status=%x Read bytes=%d\n",
1185 fdc_state[fdc].reset = 1;
1189 #define MORE_OUTPUT -2
1190 /* does the fdc need more output? */
1191 static int need_more_output(int fdc)
1193 int status = wait_til_ready(fdc);
1198 if (is_ready_state(status))
1204 /* Set perpendicular mode as required, based on data rate, if supported.
1205 * 82077 Now tested. 1Mbps data rate only possible with 82077-1.
1207 static void perpendicular_mode(int fdc)
1209 unsigned char perp_mode;
1211 if (raw_cmd->rate & 0x40) {
1212 switch (raw_cmd->rate & 3) {
1220 DPRINT("Invalid data rate for perpendicular mode!\n");
1222 fdc_state[fdc].reset = 1;
1224 * convenient way to return to
1225 * redo without too much hassle
1226 * (deep stack et al.)
1233 if (fdc_state[fdc].perp_mode == perp_mode)
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");
1242 } /* perpendicular_mode */
1244 static int fifo_depth = 0xa;
1247 static int fdc_configure(int fdc)
1250 output_byte(fdc, FD_CONFIGURE);
1251 if (need_more_output(fdc) != MORE_OUTPUT)
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 */
1259 #define NOMINAL_DTR 500
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.
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.
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
1274 * srt: 1000 to 16000 in microseconds
1275 * hut: 16 to 240 milliseconds
1276 * hlt: 2 to 254 milliseconds
1278 * These values are rounded up to the next highest available delay time.
1280 static void fdc_specify(int fdc, int drive)
1282 unsigned char spec1;
1283 unsigned char spec2;
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;
1292 if (fdc_state[fdc].need_configure &&
1293 fdc_state[fdc].version >= FDC_82072A) {
1295 fdc_state[fdc].need_configure = 0;
1298 switch (raw_cmd->rate & 0x03) {
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);
1319 if (fdc_state[fdc].version >= FDC_82072) {
1321 hlt_max_code = 0x00; /* 0==256msec*dtr0/dtr (not linear!) */
1322 hut_max_code = 0x0; /* 0==256msec*dtr0/dtr (not linear!) */
1325 /* Convert step rate from microseconds to milliseconds and 4 bits */
1326 srt = 16 - DIV_ROUND_UP(drive_params[drive].srt * scale_dtr / 1000,
1334 hlt = DIV_ROUND_UP(drive_params[drive].hlt * scale_dtr / 2,
1338 else if (hlt > 0x7f)
1341 hut = DIV_ROUND_UP(drive_params[drive].hut * scale_dtr / 16,
1348 spec1 = (srt << 4) | hut;
1349 spec2 = (hlt << 1) | (use_virtual_dma & 1);
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);
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).
1365 static int fdc_dtr(void)
1367 /* If data rate not already set to desired value, set it. */
1368 if ((raw_cmd->rate & 3) == fdc_state[current_fdc].dtr)
1372 fdc_outb(raw_cmd->rate & 3, current_fdc, FD_DCR);
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)
1379 fdc_state[current_fdc].dtr = raw_cmd->rate & 3;
1380 return fd_wait_for_completion(jiffies + 2UL * HZ / 100, floppy_ready);
1383 static void tell_sector(void)
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]);
1391 static void print_errors(void)
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");
1399 } else if (reply_buffer[ST1] & ST1_CRC) {
1400 pr_cont("CRC error");
1402 } else if ((reply_buffer[ST1] & (ST1_MAM | ST1_ND)) ||
1403 (reply_buffer[ST2] & ST2_MAM)) {
1405 pr_cont("sector not found");
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");
1414 pr_cont("unknown error. ST[0..2] are: 0x%x 0x%x 0x%x",
1415 reply_buffer[ST0], reply_buffer[ST1],
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.
1429 static int interpret_errors(void)
1434 DPRINT("-- FDC reply error\n");
1435 fdc_state[current_fdc].reset = 1;
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 */
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);
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");
1458 } else if (*errors >= drive_params[current_drive].max_errors.reporting) {
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;
1465 case 0x80: /* invalid command given */
1466 DPRINT("Invalid FDC command given!\n");
1470 DPRINT("Abnormal termination caused by polling\n");
1473 default: /* (0) Normal command termination */
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).
1483 static void setup_rw_floppy(void)
1488 unsigned long ready_date;
1489 void (*function)(void);
1491 flags = raw_cmd->flags;
1492 if (flags & (FD_RAW_READ | FD_RAW_WRITE))
1493 flags |= FD_RAW_INTR;
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.
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;
1505 function = setup_rw_floppy;
1507 /* wait until the floppy is spinning fast enough */
1508 if (fd_wait_for_completion(ready_date, function))
1511 if ((flags & FD_RAW_READ) || (flags & FD_RAW_WRITE))
1514 if (flags & FD_RAW_INTR)
1515 do_floppy = main_command_interrupt;
1518 for (i = 0; i < raw_cmd->cmd_count; i++)
1519 r |= output_byte(current_fdc, raw_cmd->fullcmd[i]);
1521 debugt(__func__, "rw_command");
1529 if (!(flags & FD_RAW_INTR)) {
1530 inr = result(current_fdc);
1532 } else if (flags & FD_RAW_NEED_DISK)
1536 static int blind_seek;
1539 * This is the routine called after every seek (or recalibrate) interrupt
1540 * from the floppy controller.
1542 static void seek_interrupt(void)
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;
1552 if (drive_state[current_drive].track >= 0 &&
1553 drive_state[current_drive].track != reply_buffer[ST1] &&
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",
1559 clear_bit(FD_DISK_NEWCHANGE_BIT,
1560 &drive_state[current_drive].flags);
1561 /* effective seek */
1562 drive_state[current_drive].select_date = jiffies;
1564 drive_state[current_drive].track = reply_buffer[ST1];
1568 static void check_wp(int fdc, int drive)
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;
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);
1589 clear_bit(FD_DISK_WRITABLE_BIT,
1590 &drive_state[drive].flags);
1594 static void seek_floppy(void)
1600 debug_dcl(drive_params[current_drive].flags,
1601 "calling disk change from %s\n", __func__);
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
1609 set_bit(FD_DISK_CHANGED_BIT,
1610 &drive_state[current_drive].flags);
1615 if (drive_state[current_drive].track <= NEED_1_RECAL) {
1616 recalibrate_floppy();
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? */
1624 track = raw_cmd->track - 1;
1626 if (drive_params[current_drive].flags & FD_SILENT_DCL_CLEAR) {
1627 set_dor(current_fdc, ~(0x10 << UNIT(current_drive)), 0);
1629 raw_cmd->flags |= FD_RAW_NEED_SEEK;
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;
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) {
1651 debugt(__func__, "");
1654 static void recal_interrupt(void)
1656 debugt(__func__, "");
1658 fdc_state[current_fdc].reset = 1;
1659 else if (reply_buffer[ST0] & ST0_ECE) {
1660 switch (drive_state[current_drive].track) {
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 :-) */
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");
1681 clear_bit(FD_DISK_NEWCHANGE_BIT,
1682 &drive_state[current_drive].flags);
1683 drive_state[current_drive].select_date = jiffies;
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
1693 drive_state[current_drive].track = NEED_1_RECAL;
1697 drive_state[current_drive].track = reply_buffer[ST1];
1701 static void print_result(char *message, int inr)
1705 DPRINT("%s ", message);
1707 for (i = 0; i < inr; i++)
1708 pr_cont("repl[%d]=%x ", i, reply_buffer[i]);
1712 /* interrupt handler. Note that this can be called externally on the Sparc */
1713 irqreturn_t floppy_interrupt(int irq, void *dev_id)
1717 void (*handler)(void) = do_floppy;
1719 lasthandler = handler;
1720 interruptjiffies = jiffies;
1722 f = claim_dma_lock();
1724 release_dma_lock(f);
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");
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
1746 do_print = !handler && print_unex && initialized;
1748 inr = result(current_fdc);
1750 print_result("unexpected interrupt", inr);
1754 output_byte(current_fdc, FD_SENSEI);
1755 inr = result(current_fdc);
1757 print_result("sensei", inr);
1759 } while ((reply_buffer[ST0] & 0x83) != UNIT(current_drive) &&
1760 inr == 2 && max_sensei);
1763 fdc_state[current_fdc].reset = 1;
1766 schedule_bh(handler);
1767 is_alive(__func__, "normal interrupt end");
1769 /* FIXME! Was it really for us? */
1773 static void recalibrate_floppy(void)
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)
1783 * Must do 4 FD_SENSEIs after reset because of ``drive polling''.
1785 static void reset_interrupt(void)
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! */
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().
1802 static void reset_fdc(void)
1804 unsigned long flags;
1806 do_floppy = reset_interrupt;
1807 fdc_state[current_fdc].reset = 0;
1808 reset_fdc_info(current_fdc, 0);
1810 /* Pseudo-DMA may intercept 'reset finished' interrupt. */
1811 /* Irrelevant for systems with true DMA (i386). */
1813 flags = claim_dma_lock();
1815 release_dma_lock(flags);
1817 if (fdc_state[current_fdc].version >= FDC_82072A)
1818 fdc_outb(0x80 | (fdc_state[current_fdc].dtr & 3),
1819 current_fdc, FD_STATUS);
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);
1827 static void show_floppy(int fdc)
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,
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);
1850 pr_info("status=%x\n", fdc_inb(fdc, FD_STATUS));
1851 pr_info("fdc_busy=%lu\n", fdc_busy);
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",
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);
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);
1871 static void floppy_shutdown(struct work_struct *arg)
1873 unsigned long flags;
1876 show_floppy(current_fdc);
1879 flags = claim_dma_lock();
1881 release_dma_lock(flags);
1883 /* avoid dma going to a random drive after shutdown */
1886 DPRINT("floppy timeout called\n");
1887 fdc_state[current_fdc].reset = 1;
1890 cont->redo(); /* this will recall reset when needed */
1892 pr_info("no cont in shutdown!\n");
1893 process_fd_request();
1895 is_alive(__func__, "");
1898 /* start motor, check media-changed condition and write protection */
1899 static int start_motor(void (*function)(void))
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)))) {
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));
1915 } else if (fdc_state[current_fdc].dor & (0x10 << UNIT(current_drive)))
1916 mask &= ~(0x10 << UNIT(current_drive));
1918 /* starts motor and selects floppy */
1919 del_timer(motor_off_timer + current_drive);
1920 set_dor(current_fdc, mask, data);
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,
1927 static void floppy_ready(void)
1929 if (fdc_state[current_fdc].reset) {
1933 if (start_motor(floppy_ready))
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 */
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);
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 ... */
1958 if ((raw_cmd->flags & FD_RAW_READ) ||
1959 (raw_cmd->flags & FD_RAW_WRITE))
1960 fdc_specify(current_fdc, current_drive);
1965 static void floppy_start(void)
1967 reschedule_timeout(current_drive, "floppy start");
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);
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
1983 * ========================================================================
1986 * General purpose continuations.
1987 * ==============================
1990 static void do_wakeup(void)
1992 reschedule_timeout(MAXTIMEOUT, "do wakeup");
1994 command_status += 2;
1995 wake_up(&command_done);
1998 static const struct cont_t wakeup_cont = {
2002 .done = (done_f)empty
2005 static const struct cont_t intr_cont = {
2007 .redo = process_fd_request,
2009 .done = (done_f)empty
2012 /* schedules handler, waiting for completion. May be interrupted, will then
2013 * return -EINTR, in which case the driver will automatically be unlocked.
2015 static int wait_til_done(void (*handler)(void), bool interruptible)
2019 schedule_bh(handler);
2022 wait_event_interruptible(command_done, command_status >= 2);
2024 wait_event(command_done, command_status >= 2);
2026 if (command_status < 2) {
2033 if (fdc_state[current_fdc].reset)
2034 command_status = FD_COMMAND_ERROR;
2035 if (command_status == FD_COMMAND_OKAY)
2039 command_status = FD_COMMAND_NONE;
2043 static void generic_done(int result)
2045 command_status = result;
2046 cont = &wakeup_cont;
2049 static void generic_success(void)
2054 static void generic_failure(void)
2059 static void success_and_wakeup(void)
2066 * formatting and rw support.
2067 * ==========================
2070 static int next_valid_format(int drive)
2074 probed_format = drive_state[drive].probed_format;
2076 if (probed_format >= FD_AUTODETECT_SIZE ||
2077 !drive_params[drive].autodetect[probed_format]) {
2078 drive_state[drive].probed_format = 0;
2081 if (floppy_type[drive_params[drive].autodetect[probed_format]].sect) {
2082 drive_state[drive].probed_format = probed_format;
2089 static void bad_flp_intr(void)
2094 drive_state[current_drive].probed_format++;
2095 if (!next_valid_format(current_drive))
2098 err_count = ++(*errors);
2099 INFBOUND(write_errors[current_drive].badness, err_count);
2100 if (err_count > drive_params[current_drive].max_errors.abort)
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;
2108 static void set_floppy(int drive)
2110 int type = ITYPE(drive_state[drive].fd_device);
2113 _floppy = floppy_type + type;
2115 _floppy = current_type[drive];
2119 * formatting support.
2120 * ===================
2122 static void format_interrupt(void)
2124 switch (interpret_errors()) {
2136 #define FM_MODE(x, y) ((y) & ~(((x)->rate & 0x80) >> 1))
2137 #define CT(x) ((x) | 0xc0)
2139 static void setup_format_params(int track)
2147 unsigned char track, head, sect, size;
2148 } *here = (struct fparm *)floppy_track_buffer;
2150 raw_cmd = &default_raw_cmd;
2151 raw_cmd->track = track;
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;
2164 raw_cmd->kernel_data = floppy_track_buffer;
2165 raw_cmd->length = 4 * raw_cmd->cmd[F_SECT_PER_TRACK];
2167 if (!raw_cmd->cmd[F_SECT_PER_TRACK])
2170 /* allow for about 30ms for data transport per track */
2171 head_shift = (raw_cmd->cmd[F_SECT_PER_TRACK] + 5) / 6;
2173 /* a ``cylinder'' is two tracks plus a little stepping time */
2174 track_shift = 2 * head_shift + 3;
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];
2180 /* determine interleave */
2182 if (_floppy->fmt_gap < 0x22)
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];
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 */
2198 if (n >= raw_cmd->cmd[F_SECT_PER_TRACK]) {
2199 n -= raw_cmd->cmd[F_SECT_PER_TRACK];
2200 while (here[n].sect)
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;
2211 static void redo_format(void)
2214 setup_format_params(format_req.track << STRETCH(_floppy));
2216 debugt(__func__, "queue format request");
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
2226 static int do_format(int drive, struct format_descr *tmp_format_req)
2230 if (lock_fdc(drive))
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();
2243 format_req = *tmp_format_req;
2245 cont = &format_cont;
2246 errors = &format_errors;
2247 ret = wait_til_done(redo_format, true);
2250 process_fd_request();
2255 * Buffer read/write and support
2256 * =============================
2259 static void floppy_end_request(struct request *req, blk_status_t error)
2261 unsigned int nr_sectors = current_count_sectors;
2262 unsigned int drive = (unsigned long)req->q->disk->private_data;
2264 /* current_count_sectors can be zero if transfer failed */
2266 nr_sectors = blk_rq_cur_sectors(req);
2267 if (blk_update_request(req, error, nr_sectors << 9))
2269 __blk_mq_end_request(req, error);
2271 /* We're done with the request */
2276 /* new request_done. Can handle physical sectors which are smaller than a
2278 static void request_done(int uptodate)
2280 struct request *req = current_req;
2282 char msg[sizeof("request done ") + sizeof(int) * 3];
2285 snprintf(msg, sizeof(msg), "request done %d", uptodate);
2286 reschedule_timeout(MAXTIMEOUT, msg);
2289 pr_info("floppy.c: no request in request_done\n");
2294 /* maintain values for invalidation on geometry
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;
2301 floppy_end_request(req, 0);
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;
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;
2313 floppy_end_request(req, BLK_STS_IOERR);
2317 /* Interrupt handler evaluating the result of the r/w operation */
2318 static void rw_interrupt(void)
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) */
2332 if (!drive_state[current_drive].first_read_date)
2333 drive_state[current_drive].first_read_date = jiffies;
2335 ssize = DIV_ROUND_UP(1 << raw_cmd->cmd[SIZECODE], 4);
2337 if (reply_buffer[ST1] & ST1_EOC)
2342 if (raw_cmd->cmd[COMMAND] & 0x80)
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;
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);
2367 nr_sectors -= in_sector_offset;
2368 INFBOUND(nr_sectors, 0);
2369 SUPBOUND(current_count_sectors, nr_sectors);
2371 switch (interpret_errors()) {
2376 if (!current_count_sectors) {
2383 if (!current_count_sectors) {
2387 current_type[current_drive] = _floppy;
2388 floppy_sizes[TOMINOR(current_drive)] = _floppy->size;
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;
2401 if (CT(raw_cmd->cmd[COMMAND]) != FD_READ) {
2402 /* transfer directly from buffer */
2405 buffer_track = raw_cmd->track;
2406 buffer_drive = current_drive;
2407 INFBOUND(buffer_max, nr_sectors + fsector_t);
2412 /* Compute the maximal transfer size */
2413 static int transfer_size(int ssize, int max_sector, int max_size)
2415 SUPBOUND(max_sector, fsector_t + max_size);
2418 max_sector -= (max_sector % _floppy->sect) % ssize;
2420 /* transfer size, beginning not aligned */
2421 current_count_sectors = max_sector - fsector_t;
2427 * Move data from/to the track buffer to/from the buffer cache.
2429 static void copy_buffer(int ssize, int max_sector, int max_sector_2)
2431 int remaining; /* number of transferred 512-byte sectors */
2435 struct req_iterator iter;
2437 max_sector = transfer_size(ssize,
2438 min(max_sector, max_sector_2),
2439 blk_rq_sectors(current_req));
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));
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);
2459 buffer_max = max(max_sector, buffer_max);
2461 dma_buffer = floppy_track_buffer + ((fsector_t - buffer_min) << 9);
2463 size = blk_rq_cur_bytes(current_req);
2465 rq_for_each_segment(bv, current_req, iter) {
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)
2482 if (CT(raw_cmd->cmd[COMMAND]) == FD_WRITE)
2487 if (CT(raw_cmd->cmd[COMMAND]) == FD_READ)
2488 memcpy_to_bvec(&bv, dma_buffer);
2490 memcpy_from_bvec(dma_buffer, &bv);
2497 max_sector -= remaining >> 9;
2498 DPRINT("weirdness: remaining %d\n", remaining >> 9);
2502 /* work around a bug in pseudo DMA
2503 * (on some FDCs) pseudo DMA does not stop when the CPU stops
2504 * sending data. Hence we need a different way to signal the
2505 * transfer length: We use raw_cmd->cmd[SECT_PER_TRACK]. Unfortunately, this
2506 * does not work with MT, hence we can only transfer one head at
2509 static void virtualdmabug_workaround(void)
2514 if (CT(raw_cmd->cmd[COMMAND]) == FD_WRITE) {
2515 raw_cmd->cmd[COMMAND] &= ~0x80; /* switch off multiple track mode */
2517 hard_sectors = raw_cmd->length >> (7 + raw_cmd->cmd[SIZECODE]);
2518 end_sector = raw_cmd->cmd[SECTOR] + hard_sectors - 1;
2519 if (end_sector > raw_cmd->cmd[SECT_PER_TRACK]) {
2520 pr_info("too many sectors %d > %d\n",
2521 end_sector, raw_cmd->cmd[SECT_PER_TRACK]);
2524 raw_cmd->cmd[SECT_PER_TRACK] = end_sector;
2525 /* make sure raw_cmd->cmd[SECT_PER_TRACK]
2526 * points to end of transfer */
2531 * Formulate a read/write request.
2532 * this routine decides where to load the data (directly to buffer, or to
2533 * tmp floppy area), how much data to load (the size of the buffer, the whole
2534 * track, or a single sector)
2535 * All floppy_track_buffer handling goes in here. If we ever add track buffer
2536 * allocation on the fly, it should be done here. No other part should need
2540 static int make_raw_rw_request(void)
2542 int aligned_sector_t;
2548 if (WARN(max_buffer_sectors == 0, "VFS: Block I/O scheduled on unopened device\n"))
2551 set_fdc((long)current_req->q->disk->private_data);
2553 raw_cmd = &default_raw_cmd;
2554 raw_cmd->flags = FD_RAW_SPIN | FD_RAW_NEED_DISK | FD_RAW_NEED_SEEK;
2555 raw_cmd->cmd_count = NR_RW;
2556 if (rq_data_dir(current_req) == READ) {
2557 raw_cmd->flags |= FD_RAW_READ;
2558 raw_cmd->cmd[COMMAND] = FM_MODE(_floppy, FD_READ);
2559 } else if (rq_data_dir(current_req) == WRITE) {
2560 raw_cmd->flags |= FD_RAW_WRITE;
2561 raw_cmd->cmd[COMMAND] = FM_MODE(_floppy, FD_WRITE);
2563 DPRINT("%s: unknown command\n", __func__);
2567 max_sector = _floppy->sect * _floppy->head;
2569 raw_cmd->cmd[TRACK] = (int)blk_rq_pos(current_req) / max_sector;
2570 fsector_t = (int)blk_rq_pos(current_req) % max_sector;
2571 if (_floppy->track && raw_cmd->cmd[TRACK] >= _floppy->track) {
2572 if (blk_rq_cur_sectors(current_req) & 1) {
2573 current_count_sectors = 1;
2578 raw_cmd->cmd[HEAD] = fsector_t / _floppy->sect;
2580 if (((_floppy->stretch & (FD_SWAPSIDES | FD_SECTBASEMASK)) ||
2581 test_bit(FD_NEED_TWADDLE_BIT, &drive_state[current_drive].flags)) &&
2582 fsector_t < _floppy->sect)
2583 max_sector = _floppy->sect;
2585 /* 2M disks have phantom sectors on the first track */
2586 if ((_floppy->rate & FD_2M) && (!raw_cmd->cmd[TRACK]) && (!raw_cmd->cmd[HEAD])) {
2587 max_sector = 2 * _floppy->sect / 3;
2588 if (fsector_t >= max_sector) {
2589 current_count_sectors =
2590 min_t(int, _floppy->sect - fsector_t,
2591 blk_rq_sectors(current_req));
2594 raw_cmd->cmd[SIZECODE] = 2;
2596 raw_cmd->cmd[SIZECODE] = FD_SIZECODE(_floppy);
2597 raw_cmd->rate = _floppy->rate & 0x43;
2598 if ((_floppy->rate & FD_2M) &&
2599 (raw_cmd->cmd[TRACK] || raw_cmd->cmd[HEAD]) && raw_cmd->rate == 2)
2602 if (raw_cmd->cmd[SIZECODE])
2603 raw_cmd->cmd[SIZECODE2] = 0xff;
2605 raw_cmd->cmd[SIZECODE2] = 0x80;
2606 raw_cmd->track = raw_cmd->cmd[TRACK] << STRETCH(_floppy);
2607 raw_cmd->cmd[DR_SELECT] = UNIT(current_drive) + PH_HEAD(_floppy, raw_cmd->cmd[HEAD]);
2608 raw_cmd->cmd[GAP] = _floppy->gap;
2609 ssize = DIV_ROUND_UP(1 << raw_cmd->cmd[SIZECODE], 4);
2610 raw_cmd->cmd[SECT_PER_TRACK] = _floppy->sect << 2 >> raw_cmd->cmd[SIZECODE];
2611 raw_cmd->cmd[SECTOR] = ((fsector_t % _floppy->sect) << 2 >> raw_cmd->cmd[SIZECODE]) +
2612 FD_SECTBASE(_floppy);
2614 /* tracksize describes the size which can be filled up with sectors
2617 tracksize = _floppy->sect - _floppy->sect % ssize;
2618 if (tracksize < _floppy->sect) {
2619 raw_cmd->cmd[SECT_PER_TRACK]++;
2620 if (tracksize <= fsector_t % _floppy->sect)
2621 raw_cmd->cmd[SECTOR]--;
2623 /* if we are beyond tracksize, fill up using smaller sectors */
2624 while (tracksize <= fsector_t % _floppy->sect) {
2625 while (tracksize + ssize > _floppy->sect) {
2626 raw_cmd->cmd[SIZECODE]--;
2629 raw_cmd->cmd[SECTOR]++;
2630 raw_cmd->cmd[SECT_PER_TRACK]++;
2633 max_sector = raw_cmd->cmd[HEAD] * _floppy->sect + tracksize;
2634 } else if (!raw_cmd->cmd[TRACK] && !raw_cmd->cmd[HEAD] && !(_floppy->rate & FD_2M) && probing) {
2635 max_sector = _floppy->sect;
2636 } else if (!raw_cmd->cmd[HEAD] && CT(raw_cmd->cmd[COMMAND]) == FD_WRITE) {
2637 /* for virtual DMA bug workaround */
2638 max_sector = _floppy->sect;
2641 in_sector_offset = (fsector_t % _floppy->sect) % ssize;
2642 aligned_sector_t = fsector_t - in_sector_offset;
2643 max_size = blk_rq_sectors(current_req);
2644 if ((raw_cmd->track == buffer_track) &&
2645 (current_drive == buffer_drive) &&
2646 (fsector_t >= buffer_min) && (fsector_t < buffer_max)) {
2647 /* data already in track buffer */
2648 if (CT(raw_cmd->cmd[COMMAND]) == FD_READ) {
2649 copy_buffer(1, max_sector, buffer_max);
2652 } else if (in_sector_offset || blk_rq_sectors(current_req) < ssize) {
2653 if (CT(raw_cmd->cmd[COMMAND]) == FD_WRITE) {
2654 unsigned int sectors;
2656 sectors = fsector_t + blk_rq_sectors(current_req);
2657 if (sectors > ssize && sectors < ssize + ssize)
2658 max_size = ssize + ssize;
2662 raw_cmd->flags &= ~FD_RAW_WRITE;
2663 raw_cmd->flags |= FD_RAW_READ;
2664 raw_cmd->cmd[COMMAND] = FM_MODE(_floppy, FD_READ);
2667 if (CT(raw_cmd->cmd[COMMAND]) == FD_READ)
2668 max_size = max_sector; /* unbounded */
2670 /* claim buffer track if needed */
2671 if (buffer_track != raw_cmd->track || /* bad track */
2672 buffer_drive != current_drive || /* bad drive */
2673 fsector_t > buffer_max ||
2674 fsector_t < buffer_min ||
2675 ((CT(raw_cmd->cmd[COMMAND]) == FD_READ ||
2676 (!in_sector_offset && blk_rq_sectors(current_req) >= ssize)) &&
2677 max_sector > 2 * max_buffer_sectors + buffer_min &&
2678 max_size + fsector_t > 2 * max_buffer_sectors + buffer_min)) {
2679 /* not enough space */
2681 buffer_drive = current_drive;
2682 buffer_max = buffer_min = aligned_sector_t;
2684 raw_cmd->kernel_data = floppy_track_buffer +
2685 ((aligned_sector_t - buffer_min) << 9);
2687 if (CT(raw_cmd->cmd[COMMAND]) == FD_WRITE) {
2688 /* copy write buffer to track buffer.
2689 * if we get here, we know that the write
2690 * is either aligned or the data already in the buffer
2691 * (buffer will be overwritten) */
2692 if (in_sector_offset && buffer_track == -1)
2693 DPRINT("internal error offset !=0 on write\n");
2694 buffer_track = raw_cmd->track;
2695 buffer_drive = current_drive;
2696 copy_buffer(ssize, max_sector,
2697 2 * max_buffer_sectors + buffer_min);
2699 transfer_size(ssize, max_sector,
2700 2 * max_buffer_sectors + buffer_min -
2703 /* round up current_count_sectors to get dma xfer size */
2704 raw_cmd->length = in_sector_offset + current_count_sectors;
2705 raw_cmd->length = ((raw_cmd->length - 1) | (ssize - 1)) + 1;
2706 raw_cmd->length <<= 9;
2707 if ((raw_cmd->length < current_count_sectors << 9) ||
2708 (CT(raw_cmd->cmd[COMMAND]) == FD_WRITE &&
2709 (aligned_sector_t + (raw_cmd->length >> 9) > buffer_max ||
2710 aligned_sector_t < buffer_min)) ||
2711 raw_cmd->length % (128 << raw_cmd->cmd[SIZECODE]) ||
2712 raw_cmd->length <= 0 || current_count_sectors <= 0) {
2713 DPRINT("fractionary current count b=%lx s=%lx\n",
2714 raw_cmd->length, current_count_sectors);
2715 pr_info("addr=%d, length=%ld\n",
2716 (int)((raw_cmd->kernel_data -
2717 floppy_track_buffer) >> 9),
2718 current_count_sectors);
2719 pr_info("st=%d ast=%d mse=%d msi=%d\n",
2720 fsector_t, aligned_sector_t, max_sector, max_size);
2721 pr_info("ssize=%x SIZECODE=%d\n", ssize, raw_cmd->cmd[SIZECODE]);
2722 pr_info("command=%x SECTOR=%d HEAD=%d, TRACK=%d\n",
2723 raw_cmd->cmd[COMMAND], raw_cmd->cmd[SECTOR],
2724 raw_cmd->cmd[HEAD], raw_cmd->cmd[TRACK]);
2725 pr_info("buffer drive=%d\n", buffer_drive);
2726 pr_info("buffer track=%d\n", buffer_track);
2727 pr_info("buffer_min=%d\n", buffer_min);
2728 pr_info("buffer_max=%d\n", buffer_max);
2732 if (raw_cmd->kernel_data < floppy_track_buffer ||
2733 current_count_sectors < 0 ||
2734 raw_cmd->length < 0 ||
2735 raw_cmd->kernel_data + raw_cmd->length >
2736 floppy_track_buffer + (max_buffer_sectors << 10)) {
2737 DPRINT("buffer overrun in schedule dma\n");
2738 pr_info("fsector_t=%d buffer_min=%d current_count=%ld\n",
2739 fsector_t, buffer_min, raw_cmd->length >> 9);
2740 pr_info("current_count_sectors=%ld\n",
2741 current_count_sectors);
2742 if (CT(raw_cmd->cmd[COMMAND]) == FD_READ)
2744 if (CT(raw_cmd->cmd[COMMAND]) == FD_WRITE)
2748 if (raw_cmd->length == 0) {
2749 DPRINT("zero dma transfer attempted from make_raw_request\n");
2753 virtualdmabug_workaround();
2757 static int set_next_request(void)
2759 current_req = list_first_entry_or_null(&floppy_reqs, struct request,
2762 current_req->error_count = 0;
2763 list_del_init(¤t_req->queuelist);
2765 return current_req != NULL;
2768 /* Starts or continues processing request. Will automatically unlock the
2769 * driver at end of request.
2771 static void redo_fd_request(void)
2777 if (current_drive < N_DRIVE)
2778 floppy_off(current_drive);
2784 spin_lock_irq(&floppy_lock);
2785 pending = set_next_request();
2786 spin_unlock_irq(&floppy_lock);
2793 drive = (long)current_req->q->disk->private_data;
2795 reschedule_timeout(current_drive, "redo fd request");
2798 raw_cmd = &default_raw_cmd;
2800 if (start_motor(redo_fd_request))
2803 disk_change(current_drive);
2804 if (test_bit(current_drive, &fake_change) ||
2805 test_bit(FD_DISK_CHANGED_BIT, &drive_state[current_drive].flags)) {
2806 DPRINT("disk absent or changed during operation\n");
2810 if (!_floppy) { /* Autodetection */
2812 drive_state[current_drive].probed_format = 0;
2813 if (next_valid_format(current_drive)) {
2814 DPRINT("no autodetectable formats\n");
2821 _floppy = floppy_type + drive_params[current_drive].autodetect[drive_state[current_drive].probed_format];
2824 errors = &(current_req->error_count);
2825 tmp = make_raw_rw_request();
2831 if (test_bit(FD_NEED_TWADDLE_BIT, &drive_state[current_drive].flags))
2832 twaddle(current_fdc, current_drive);
2833 schedule_bh(floppy_start);
2834 debugt(__func__, "queue fd request");
2838 static const struct cont_t rw_cont = {
2839 .interrupt = rw_interrupt,
2840 .redo = redo_fd_request,
2841 .error = bad_flp_intr,
2842 .done = request_done
2845 /* schedule the request and automatically unlock the driver on completion */
2846 static void process_fd_request(void)
2849 schedule_bh(redo_fd_request);
2852 static blk_status_t floppy_queue_rq(struct blk_mq_hw_ctx *hctx,
2853 const struct blk_mq_queue_data *bd)
2855 blk_mq_start_request(bd->rq);
2857 if (WARN(max_buffer_sectors == 0,
2858 "VFS: %s called on non-open device\n", __func__))
2859 return BLK_STS_IOERR;
2861 if (WARN(atomic_read(&usage_count) == 0,
2862 "warning: usage count=0, current_req=%p sect=%ld flags=%llx\n",
2863 current_req, (long)blk_rq_pos(current_req),
2864 (unsigned long long) current_req->cmd_flags))
2865 return BLK_STS_IOERR;
2867 if (test_and_set_bit(0, &fdc_busy)) {
2868 /* fdc busy, this new request will be treated when the
2869 current one is done */
2870 is_alive(__func__, "old request running");
2871 return BLK_STS_RESOURCE;
2874 spin_lock_irq(&floppy_lock);
2875 list_add_tail(&bd->rq->queuelist, &floppy_reqs);
2876 spin_unlock_irq(&floppy_lock);
2878 command_status = FD_COMMAND_NONE;
2879 __reschedule_timeout(MAXTIMEOUT, "fd_request");
2881 process_fd_request();
2882 is_alive(__func__, "");
2886 static const struct cont_t poll_cont = {
2887 .interrupt = success_and_wakeup,
2888 .redo = floppy_ready,
2889 .error = generic_failure,
2890 .done = generic_done
2893 static int poll_drive(bool interruptible, int flag)
2895 /* no auto-sense, just clear dcl */
2896 raw_cmd = &default_raw_cmd;
2897 raw_cmd->flags = flag;
2899 raw_cmd->cmd_count = 0;
2901 debug_dcl(drive_params[current_drive].flags,
2902 "setting NEWCHANGE in poll_drive\n");
2903 set_bit(FD_DISK_NEWCHANGE_BIT, &drive_state[current_drive].flags);
2905 return wait_til_done(floppy_ready, interruptible);
2909 * User triggered reset
2910 * ====================
2913 static void reset_intr(void)
2915 pr_info("weird, reset interrupt called\n");
2918 static const struct cont_t reset_cont = {
2919 .interrupt = reset_intr,
2920 .redo = success_and_wakeup,
2921 .error = generic_failure,
2922 .done = generic_done
2926 * Resets the FDC connected to drive <drive>.
2927 * Both current_drive and current_fdc are changed to match the new drive.
2929 static int user_reset_fdc(int drive, int arg, bool interruptible)
2933 if (lock_fdc(drive))
2936 if (arg == FD_RESET_ALWAYS)
2937 fdc_state[current_fdc].reset = 1;
2938 if (fdc_state[current_fdc].reset) {
2939 /* note: reset_fdc will take care of unlocking the driver
2943 ret = wait_til_done(reset_fdc, interruptible);
2947 process_fd_request();
2952 * Misc Ioctl's and support
2953 * ========================
2955 static inline int fd_copyout(void __user *param, const void *address,
2958 return copy_to_user(param, address, size) ? -EFAULT : 0;
2961 static inline int fd_copyin(void __user *param, void *address,
2964 return copy_from_user(address, param, size) ? -EFAULT : 0;
2967 static const char *drive_name(int type, int drive)
2969 struct floppy_struct *floppy;
2972 floppy = floppy_type + type;
2974 if (drive_params[drive].native_format)
2975 floppy = floppy_type + drive_params[drive].native_format;
2980 return floppy->name;
2985 #ifdef CONFIG_BLK_DEV_FD_RAWCMD
2988 static void raw_cmd_done(int flag)
2991 raw_cmd->flags |= FD_RAW_FAILURE;
2992 raw_cmd->flags |= FD_RAW_HARDFAILURE;
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);
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);
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;
3010 if (disk_change(current_drive))
3011 raw_cmd->flags |= FD_RAW_DISK_CHANGE;
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]);
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;
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
3036 static int raw_cmd_copyout(int cmd, void __user *param,
3037 struct floppy_raw_cmd *ptr)
3042 struct floppy_raw_cmd cmd = *ptr;
3044 cmd.kernel_data = NULL;
3045 ret = copy_to_user(param, &cmd, sizeof(cmd));
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,
3065 static void raw_cmd_free(struct floppy_raw_cmd **ptr)
3067 struct floppy_raw_cmd *next;
3068 struct floppy_raw_cmd *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;
3084 #define MAX_LEN (1UL << MAX_ORDER << PAGE_SHIFT)
3086 static int raw_cmd_copyin(int cmd, void __user *param,
3087 struct floppy_raw_cmd **rcmd)
3089 struct floppy_raw_cmd *ptr;
3095 ptr = kmalloc(sizeof(struct floppy_raw_cmd), GFP_KERNEL);
3099 ret = copy_from_user(ptr, param, sizeof(*ptr));
3101 ptr->buffer_length = 0;
3102 ptr->kernel_data = NULL;
3105 param += sizeof(struct floppy_raw_cmd);
3106 if (ptr->cmd_count > FD_RAW_CMD_FULLSIZE)
3109 memset(ptr->reply, 0, FD_RAW_REPLY_SIZE);
3110 ptr->resultcode = 0;
3112 if (ptr->flags & (FD_RAW_READ | FD_RAW_WRITE)) {
3113 if (ptr->length <= 0 || ptr->length >= MAX_LEN)
3115 ptr->kernel_data = (char *)fd_dma_mem_alloc(ptr->length);
3116 fallback_on_nodma_alloc(&ptr->kernel_data, ptr->length);
3117 if (!ptr->kernel_data)
3119 ptr->buffer_length = ptr->length;
3121 if (ptr->flags & FD_RAW_WRITE) {
3122 ret = fd_copyin(ptr->data, ptr->kernel_data, ptr->length);
3127 if (ptr->flags & FD_RAW_MORE) {
3128 rcmd = &(ptr->next);
3136 static int raw_cmd_ioctl(int cmd, void __user *param)
3138 struct floppy_raw_cmd *my_raw_cmd;
3143 if (fdc_state[current_fdc].rawcmd <= 1)
3144 fdc_state[current_fdc].rawcmd = 1;
3145 for (drive = 0; drive < N_DRIVE; drive++) {
3146 if (FDC(drive) != current_fdc)
3148 if (drive == current_drive) {
3149 if (drive_state[drive].fd_ref > 1) {
3150 fdc_state[current_fdc].rawcmd = 2;
3153 } else if (drive_state[drive].fd_ref) {
3154 fdc_state[current_fdc].rawcmd = 2;
3159 if (fdc_state[current_fdc].reset)
3162 ret = raw_cmd_copyin(cmd, param, &my_raw_cmd);
3164 raw_cmd_free(&my_raw_cmd);
3168 raw_cmd = my_raw_cmd;
3169 cont = &raw_cmd_cont;
3170 ret = wait_til_done(floppy_start, true);
3171 debug_dcl(drive_params[current_drive].flags,
3172 "calling disk change from raw_cmd ioctl\n");
3174 if (ret != -EINTR && fdc_state[current_fdc].reset)
3177 drive_state[current_drive].track = NO_TRACK;
3179 ret2 = raw_cmd_copyout(cmd, param, my_raw_cmd);
3182 raw_cmd_free(&my_raw_cmd);
3186 static int floppy_raw_cmd_ioctl(int type, int drive, int cmd,
3191 pr_warn_once("Note: FDRAWCMD is deprecated and will be removed from the kernel in the near future.\n");
3195 if (lock_fdc(drive))
3198 ret = raw_cmd_ioctl(cmd, param);
3201 process_fd_request();
3205 #else /* CONFIG_BLK_DEV_FD_RAWCMD */
3207 static int floppy_raw_cmd_ioctl(int type, int drive, int cmd,
3215 static int invalidate_drive(struct block_device *bdev)
3217 /* invalidate the buffer track to force a reread */
3218 set_bit((long)bdev->bd_disk->private_data, &fake_change);
3219 process_fd_request();
3220 if (bdev_check_media_change(bdev))
3221 floppy_revalidate(bdev->bd_disk);
3225 static int set_geometry(unsigned int cmd, struct floppy_struct *g,
3226 int drive, int type, struct block_device *bdev)
3230 /* sanity checking for parameters. */
3231 if ((int)g->sect <= 0 ||
3232 (int)g->head <= 0 ||
3233 /* check for overflow in max_sector */
3234 (int)(g->sect * g->head) <= 0 ||
3235 /* check for zero in raw_cmd->cmd[F_SECT_PER_TRACK] */
3236 (unsigned char)((g->sect << 2) >> FD_SIZECODE(g)) == 0 ||
3237 g->track <= 0 || g->track > drive_params[drive].tracks >> STRETCH(g) ||
3238 /* check if reserved bits are set */
3239 (g->stretch & ~(FD_STRETCH | FD_SWAPSIDES | FD_SECTBASEMASK)) != 0)
3242 if (!capable(CAP_SYS_ADMIN))
3244 mutex_lock(&open_lock);
3245 if (lock_fdc(drive)) {
3246 mutex_unlock(&open_lock);
3249 floppy_type[type] = *g;
3250 floppy_type[type].name = "user format";
3251 for (cnt = type << 2; cnt < (type << 2) + 4; cnt++)
3252 floppy_sizes[cnt] = floppy_sizes[cnt + 0x80] =
3253 floppy_type[type].size + 1;
3254 process_fd_request();
3255 for (cnt = 0; cnt < N_DRIVE; cnt++) {
3256 struct block_device *bdev = opened_bdev[cnt];
3257 if (!bdev || ITYPE(drive_state[cnt].fd_device) != type)
3259 __invalidate_device(bdev, true);
3261 mutex_unlock(&open_lock);
3265 if (lock_fdc(drive))
3267 if (cmd != FDDEFPRM) {
3268 /* notice a disk change immediately, else
3269 * we lose our settings immediately*/
3270 if (poll_drive(true, FD_RAW_NEED_DISK) == -EINTR)
3273 oldStretch = g->stretch;
3274 user_params[drive] = *g;
3275 if (buffer_drive == drive)
3276 SUPBOUND(buffer_max, user_params[drive].sect);
3277 current_type[drive] = &user_params[drive];
3278 floppy_sizes[drive] = user_params[drive].size;
3279 if (cmd == FDDEFPRM)
3280 drive_state[current_drive].keep_data = -1;
3282 drive_state[current_drive].keep_data = 1;
3283 /* invalidation. Invalidate only when needed, i.e.
3284 * when there are already sectors in the buffer cache
3285 * whose number will change. This is useful, because
3286 * mtools often changes the geometry of the disk after
3287 * looking at the boot block */
3288 if (drive_state[current_drive].maxblock > user_params[drive].sect ||
3289 drive_state[current_drive].maxtrack ||
3290 ((user_params[drive].sect ^ oldStretch) &
3291 (FD_SWAPSIDES | FD_SECTBASEMASK)))
3292 invalidate_drive(bdev);
3294 process_fd_request();
3299 /* handle obsolete ioctl's */
3300 static unsigned int ioctl_table[] = {
3328 static int normalize_ioctl(unsigned int *cmd, int *size)
3332 for (i = 0; i < ARRAY_SIZE(ioctl_table); i++) {
3333 if ((*cmd & 0xffff) == (ioctl_table[i] & 0xffff)) {
3334 *size = _IOC_SIZE(*cmd);
3335 *cmd = ioctl_table[i];
3336 if (*size > _IOC_SIZE(*cmd)) {
3337 pr_info("ioctl not yet supported\n");
3346 static int get_floppy_geometry(int drive, int type, struct floppy_struct **g)
3349 *g = &floppy_type[type];
3351 if (lock_fdc(drive))
3353 if (poll_drive(false, 0) == -EINTR)
3355 process_fd_request();
3356 *g = current_type[drive];
3363 static int fd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
3365 int drive = (long)bdev->bd_disk->private_data;
3366 int type = ITYPE(drive_state[drive].fd_device);
3367 struct floppy_struct *g;
3370 ret = get_floppy_geometry(drive, type, &g);
3374 geo->heads = g->head;
3375 geo->sectors = g->sect;
3376 geo->cylinders = g->track;
3380 static bool valid_floppy_drive_params(const short autodetect[FD_AUTODETECT_SIZE],
3383 size_t floppy_type_size = ARRAY_SIZE(floppy_type);
3386 for (i = 0; i < FD_AUTODETECT_SIZE; ++i) {
3387 if (autodetect[i] < 0 ||
3388 autodetect[i] >= floppy_type_size)
3392 if (native_format < 0 || native_format >= floppy_type_size)
3398 static int fd_locked_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd,
3399 unsigned long param)
3401 int drive = (long)bdev->bd_disk->private_data;
3402 int type = ITYPE(drive_state[drive].fd_device);
3406 struct floppy_struct g; /* geometry */
3407 struct format_descr f;
3408 struct floppy_max_errors max_errors;
3409 struct floppy_drive_params dp;
3410 } inparam; /* parameters coming from user space */
3411 const void *outparam; /* parameters passed back to user space */
3413 /* convert compatibility eject ioctls into floppy eject ioctl.
3414 * We do this in order to provide a means to eject floppy disks before
3415 * installing the new fdutils package */
3416 if (cmd == CDROMEJECT || /* CD-ROM eject */
3417 cmd == 0x6470) { /* SunOS floppy eject */
3418 DPRINT("obsolete eject ioctl\n");
3419 DPRINT("please use floppycontrol --eject\n");
3423 if (!((cmd & 0xff00) == 0x0200))
3426 /* convert the old style command into a new style command */
3427 ret = normalize_ioctl(&cmd, &size);
3431 /* permission checks */
3432 if (((cmd & 0x40) && !(mode & (FMODE_WRITE | FMODE_WRITE_IOCTL))) ||
3433 ((cmd & 0x80) && !capable(CAP_SYS_ADMIN)))
3436 if (WARN_ON(size < 0 || size > sizeof(inparam)))
3440 memset(&inparam, 0, sizeof(inparam));
3441 if (_IOC_DIR(cmd) & _IOC_WRITE) {
3442 ret = fd_copyin((void __user *)param, &inparam, size);
3449 if (drive_state[drive].fd_ref != 1)
3450 /* somebody else has this drive open */
3452 if (lock_fdc(drive))
3455 /* do the actual eject. Fails on
3456 * non-Sparc architectures */
3457 ret = fd_eject(UNIT(drive));
3459 set_bit(FD_DISK_CHANGED_BIT, &drive_state[drive].flags);
3460 set_bit(FD_VERIFY_BIT, &drive_state[drive].flags);
3461 process_fd_request();
3464 if (lock_fdc(drive))
3466 current_type[drive] = NULL;
3467 floppy_sizes[drive] = MAX_DISK_SIZE << 1;
3468 drive_state[drive].keep_data = 0;
3469 return invalidate_drive(bdev);
3472 return set_geometry(cmd, &inparam.g, drive, type, bdev);
3474 ret = get_floppy_geometry(drive, type,
3475 (struct floppy_struct **)&outparam);
3478 memcpy(&inparam.g, outparam,
3479 offsetof(struct floppy_struct, name));
3480 outparam = &inparam.g;
3483 drive_params[drive].flags |= FTD_MSG;
3486 drive_params[drive].flags &= ~FTD_MSG;
3489 if (lock_fdc(drive))
3491 if (poll_drive(true, FD_RAW_NEED_DISK) == -EINTR)
3493 ret = drive_state[drive].flags;
3494 process_fd_request();
3495 if (ret & FD_VERIFY)
3497 if (!(ret & FD_DISK_WRITABLE))
3501 if (drive_state[drive].fd_ref != 1)
3503 return do_format(drive, &inparam.f);
3506 if (lock_fdc(drive))
3508 return invalidate_drive(bdev);
3509 case FDSETEMSGTRESH:
3510 drive_params[drive].max_errors.reporting = (unsigned short)(param & 0x0f);
3513 outparam = &drive_params[drive].max_errors;
3516 drive_params[drive].max_errors = inparam.max_errors;
3519 outparam = drive_name(type, drive);
3520 SUPBOUND(size, strlen((const char *)outparam) + 1);
3523 if (!valid_floppy_drive_params(inparam.dp.autodetect,
3524 inparam.dp.native_format))
3526 drive_params[drive] = inparam.dp;
3529 outparam = &drive_params[drive];
3532 if (lock_fdc(drive))
3534 if (poll_drive(true, FD_RAW_NEED_DISK) == -EINTR)
3536 process_fd_request();
3539 outparam = &drive_state[drive];
3542 return user_reset_fdc(drive, (int)param, true);
3544 outparam = &fdc_state[FDC(drive)];
3547 memset(&write_errors[drive], 0, sizeof(write_errors[drive]));
3550 outparam = &write_errors[drive];
3553 return floppy_raw_cmd_ioctl(type, drive, cmd, (void __user *)param);
3555 if (lock_fdc(drive))
3557 twaddle(current_fdc, current_drive);
3558 process_fd_request();
3564 if (_IOC_DIR(cmd) & _IOC_READ)
3565 return fd_copyout((void __user *)param, outparam, size);
3570 static int fd_ioctl(struct block_device *bdev, fmode_t mode,
3571 unsigned int cmd, unsigned long param)
3575 mutex_lock(&floppy_mutex);
3576 ret = fd_locked_ioctl(bdev, mode, cmd, param);
3577 mutex_unlock(&floppy_mutex);
3582 #ifdef CONFIG_COMPAT
3584 struct compat_floppy_drive_params {
3586 compat_ulong_t max_dtr;
3590 compat_ulong_t spinup;
3591 compat_ulong_t spindown;
3592 unsigned char spindown_offset;
3593 unsigned char select_delay;
3595 unsigned char tracks;
3596 compat_ulong_t timeout;
3597 unsigned char interleave_sect;
3598 struct floppy_max_errors max_errors;
3601 short autodetect[FD_AUTODETECT_SIZE];
3602 compat_int_t checkfreq;
3603 compat_int_t native_format;
3606 struct compat_floppy_drive_struct {
3608 compat_ulong_t spinup_date;
3609 compat_ulong_t select_date;
3610 compat_ulong_t first_read_date;
3611 short probed_format;
3615 compat_int_t generation;
3616 compat_int_t keep_data;
3617 compat_int_t fd_ref;
3618 compat_int_t fd_device;
3619 compat_int_t last_checked;
3620 compat_caddr_t dmabuf;
3621 compat_int_t bufblocks;
3624 struct compat_floppy_fdc_state {
3628 unsigned char version;
3630 compat_ulong_t address;
3631 unsigned int rawcmd:2;
3632 unsigned int reset:1;
3633 unsigned int need_configure:1;
3634 unsigned int perp_mode:2;
3635 unsigned int has_fifo:1;
3636 unsigned int driver_version;
3637 unsigned char track[4];
3640 struct compat_floppy_write_errors {
3641 unsigned int write_errors;
3642 compat_ulong_t first_error_sector;
3643 compat_int_t first_error_generation;
3644 compat_ulong_t last_error_sector;
3645 compat_int_t last_error_generation;
3646 compat_uint_t badness;
3649 #define FDSETPRM32 _IOW(2, 0x42, struct compat_floppy_struct)
3650 #define FDDEFPRM32 _IOW(2, 0x43, struct compat_floppy_struct)
3651 #define FDSETDRVPRM32 _IOW(2, 0x90, struct compat_floppy_drive_params)
3652 #define FDGETDRVPRM32 _IOR(2, 0x11, struct compat_floppy_drive_params)
3653 #define FDGETDRVSTAT32 _IOR(2, 0x12, struct compat_floppy_drive_struct)
3654 #define FDPOLLDRVSTAT32 _IOR(2, 0x13, struct compat_floppy_drive_struct)
3655 #define FDGETFDCSTAT32 _IOR(2, 0x15, struct compat_floppy_fdc_state)
3656 #define FDWERRORGET32 _IOR(2, 0x17, struct compat_floppy_write_errors)
3658 static int compat_set_geometry(struct block_device *bdev, fmode_t mode, unsigned int cmd,
3659 struct compat_floppy_struct __user *arg)
3661 struct floppy_struct v;
3665 BUILD_BUG_ON(offsetof(struct floppy_struct, name) !=
3666 offsetof(struct compat_floppy_struct, name));
3668 if (!(mode & (FMODE_WRITE | FMODE_WRITE_IOCTL)))
3671 memset(&v, 0, sizeof(struct floppy_struct));
3672 if (copy_from_user(&v, arg, offsetof(struct floppy_struct, name)))
3675 mutex_lock(&floppy_mutex);
3676 drive = (long)bdev->bd_disk->private_data;
3677 type = ITYPE(drive_state[drive].fd_device);
3678 err = set_geometry(cmd == FDSETPRM32 ? FDSETPRM : FDDEFPRM,
3679 &v, drive, type, bdev);
3680 mutex_unlock(&floppy_mutex);
3684 static int compat_get_prm(int drive,
3685 struct compat_floppy_struct __user *arg)
3687 struct compat_floppy_struct v;
3688 struct floppy_struct *p;
3691 memset(&v, 0, sizeof(v));
3692 mutex_lock(&floppy_mutex);
3693 err = get_floppy_geometry(drive, ITYPE(drive_state[drive].fd_device),
3696 mutex_unlock(&floppy_mutex);
3699 memcpy(&v, p, offsetof(struct floppy_struct, name));
3700 mutex_unlock(&floppy_mutex);
3701 if (copy_to_user(arg, &v, sizeof(struct compat_floppy_struct)))
3706 static int compat_setdrvprm(int drive,
3707 struct compat_floppy_drive_params __user *arg)
3709 struct compat_floppy_drive_params v;
3711 if (!capable(CAP_SYS_ADMIN))
3713 if (copy_from_user(&v, arg, sizeof(struct compat_floppy_drive_params)))
3715 if (!valid_floppy_drive_params(v.autodetect, v.native_format))
3717 mutex_lock(&floppy_mutex);
3718 drive_params[drive].cmos = v.cmos;
3719 drive_params[drive].max_dtr = v.max_dtr;
3720 drive_params[drive].hlt = v.hlt;
3721 drive_params[drive].hut = v.hut;
3722 drive_params[drive].srt = v.srt;
3723 drive_params[drive].spinup = v.spinup;
3724 drive_params[drive].spindown = v.spindown;
3725 drive_params[drive].spindown_offset = v.spindown_offset;
3726 drive_params[drive].select_delay = v.select_delay;
3727 drive_params[drive].rps = v.rps;
3728 drive_params[drive].tracks = v.tracks;
3729 drive_params[drive].timeout = v.timeout;
3730 drive_params[drive].interleave_sect = v.interleave_sect;
3731 drive_params[drive].max_errors = v.max_errors;
3732 drive_params[drive].flags = v.flags;
3733 drive_params[drive].read_track = v.read_track;
3734 memcpy(drive_params[drive].autodetect, v.autodetect,
3735 sizeof(v.autodetect));
3736 drive_params[drive].checkfreq = v.checkfreq;
3737 drive_params[drive].native_format = v.native_format;
3738 mutex_unlock(&floppy_mutex);
3742 static int compat_getdrvprm(int drive,
3743 struct compat_floppy_drive_params __user *arg)
3745 struct compat_floppy_drive_params v;
3747 memset(&v, 0, sizeof(struct compat_floppy_drive_params));
3748 mutex_lock(&floppy_mutex);
3749 v.cmos = drive_params[drive].cmos;
3750 v.max_dtr = drive_params[drive].max_dtr;
3751 v.hlt = drive_params[drive].hlt;
3752 v.hut = drive_params[drive].hut;
3753 v.srt = drive_params[drive].srt;
3754 v.spinup = drive_params[drive].spinup;
3755 v.spindown = drive_params[drive].spindown;
3756 v.spindown_offset = drive_params[drive].spindown_offset;
3757 v.select_delay = drive_params[drive].select_delay;
3758 v.rps = drive_params[drive].rps;
3759 v.tracks = drive_params[drive].tracks;
3760 v.timeout = drive_params[drive].timeout;
3761 v.interleave_sect = drive_params[drive].interleave_sect;
3762 v.max_errors = drive_params[drive].max_errors;
3763 v.flags = drive_params[drive].flags;
3764 v.read_track = drive_params[drive].read_track;
3765 memcpy(v.autodetect, drive_params[drive].autodetect,
3766 sizeof(v.autodetect));
3767 v.checkfreq = drive_params[drive].checkfreq;
3768 v.native_format = drive_params[drive].native_format;
3769 mutex_unlock(&floppy_mutex);
3771 if (copy_to_user(arg, &v, sizeof(struct compat_floppy_drive_params)))
3776 static int compat_getdrvstat(int drive, bool poll,
3777 struct compat_floppy_drive_struct __user *arg)
3779 struct compat_floppy_drive_struct v;
3781 memset(&v, 0, sizeof(struct compat_floppy_drive_struct));
3782 mutex_lock(&floppy_mutex);
3785 if (lock_fdc(drive))
3787 if (poll_drive(true, FD_RAW_NEED_DISK) == -EINTR)
3789 process_fd_request();
3791 v.spinup_date = drive_state[drive].spinup_date;
3792 v.select_date = drive_state[drive].select_date;
3793 v.first_read_date = drive_state[drive].first_read_date;
3794 v.probed_format = drive_state[drive].probed_format;
3795 v.track = drive_state[drive].track;
3796 v.maxblock = drive_state[drive].maxblock;
3797 v.maxtrack = drive_state[drive].maxtrack;
3798 v.generation = drive_state[drive].generation;
3799 v.keep_data = drive_state[drive].keep_data;
3800 v.fd_ref = drive_state[drive].fd_ref;
3801 v.fd_device = drive_state[drive].fd_device;
3802 v.last_checked = drive_state[drive].last_checked;
3803 v.dmabuf = (uintptr_t) drive_state[drive].dmabuf;
3804 v.bufblocks = drive_state[drive].bufblocks;
3805 mutex_unlock(&floppy_mutex);
3807 if (copy_to_user(arg, &v, sizeof(struct compat_floppy_drive_struct)))
3811 mutex_unlock(&floppy_mutex);
3815 static int compat_getfdcstat(int drive,
3816 struct compat_floppy_fdc_state __user *arg)
3818 struct compat_floppy_fdc_state v32;
3819 struct floppy_fdc_state v;
3821 mutex_lock(&floppy_mutex);
3822 v = fdc_state[FDC(drive)];
3823 mutex_unlock(&floppy_mutex);
3825 memset(&v32, 0, sizeof(struct compat_floppy_fdc_state));
3826 v32.spec1 = v.spec1;
3827 v32.spec2 = v.spec2;
3829 v32.version = v.version;
3831 v32.address = v.address;
3832 v32.rawcmd = v.rawcmd;
3833 v32.reset = v.reset;
3834 v32.need_configure = v.need_configure;
3835 v32.perp_mode = v.perp_mode;
3836 v32.has_fifo = v.has_fifo;
3837 v32.driver_version = v.driver_version;
3838 memcpy(v32.track, v.track, 4);
3839 if (copy_to_user(arg, &v32, sizeof(struct compat_floppy_fdc_state)))
3844 static int compat_werrorget(int drive,
3845 struct compat_floppy_write_errors __user *arg)
3847 struct compat_floppy_write_errors v32;
3848 struct floppy_write_errors v;
3850 memset(&v32, 0, sizeof(struct compat_floppy_write_errors));
3851 mutex_lock(&floppy_mutex);
3852 v = write_errors[drive];
3853 mutex_unlock(&floppy_mutex);
3854 v32.write_errors = v.write_errors;
3855 v32.first_error_sector = v.first_error_sector;
3856 v32.first_error_generation = v.first_error_generation;
3857 v32.last_error_sector = v.last_error_sector;
3858 v32.last_error_generation = v.last_error_generation;
3859 v32.badness = v.badness;
3860 if (copy_to_user(arg, &v32, sizeof(struct compat_floppy_write_errors)))
3865 static int fd_compat_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd,
3866 unsigned long param)
3868 int drive = (long)bdev->bd_disk->private_data;
3870 case CDROMEJECT: /* CD-ROM eject */
3871 case 0x6470: /* SunOS floppy eject */
3875 case FDSETEMSGTRESH:
3883 return fd_ioctl(bdev, mode, cmd, param);
3890 return fd_ioctl(bdev, mode, cmd,
3891 (unsigned long)compat_ptr(param));
3894 return compat_set_geometry(bdev, mode, cmd, compat_ptr(param));
3896 return compat_get_prm(drive, compat_ptr(param));
3898 return compat_setdrvprm(drive, compat_ptr(param));
3900 return compat_getdrvprm(drive, compat_ptr(param));
3901 case FDPOLLDRVSTAT32:
3902 return compat_getdrvstat(drive, true, compat_ptr(param));
3903 case FDGETDRVSTAT32:
3904 return compat_getdrvstat(drive, false, compat_ptr(param));
3905 case FDGETFDCSTAT32:
3906 return compat_getfdcstat(drive, compat_ptr(param));
3908 return compat_werrorget(drive, compat_ptr(param));
3914 static void __init config_types(void)
3916 bool has_drive = false;
3919 /* read drive info out of physical CMOS */
3921 if (!drive_params[drive].cmos)
3922 drive_params[drive].cmos = FLOPPY0_TYPE;
3924 if (!drive_params[drive].cmos)
3925 drive_params[drive].cmos = FLOPPY1_TYPE;
3927 /* FIXME: additional physical CMOS drive detection should go here */
3929 for (drive = 0; drive < N_DRIVE; drive++) {
3930 unsigned int type = drive_params[drive].cmos;
3931 struct floppy_drive_params *params;
3932 const char *name = NULL;
3935 if (type < ARRAY_SIZE(default_drive_params)) {
3936 params = &default_drive_params[type].params;
3938 name = default_drive_params[type].name;
3939 allowed_drive_mask |= 1 << drive;
3941 allowed_drive_mask &= ~(1 << drive);
3943 params = &default_drive_params[0].params;
3944 snprintf(temparea, sizeof(temparea),
3945 "unknown type %d (usb?)", type);
3949 const char *prepend;
3953 pr_info("Floppy drive(s):");
3958 pr_cont("%s fd%d is %s", prepend, drive, name);
3960 drive_params[drive] = *params;
3967 static void floppy_release(struct gendisk *disk, fmode_t mode)
3969 int drive = (long)disk->private_data;
3971 mutex_lock(&floppy_mutex);
3972 mutex_lock(&open_lock);
3973 if (!drive_state[drive].fd_ref--) {
3974 DPRINT("floppy_release with fd_ref == 0");
3975 drive_state[drive].fd_ref = 0;
3977 if (!drive_state[drive].fd_ref)
3978 opened_bdev[drive] = NULL;
3979 mutex_unlock(&open_lock);
3980 mutex_unlock(&floppy_mutex);
3984 * floppy_open check for aliasing (/dev/fd0 can be the same as
3985 * /dev/PS0 etc), and disallows simultaneous access to the same
3986 * drive with different device numbers.
3988 static int floppy_open(struct block_device *bdev, fmode_t mode)
3990 int drive = (long)bdev->bd_disk->private_data;
3991 int old_dev, new_dev;
3996 mutex_lock(&floppy_mutex);
3997 mutex_lock(&open_lock);
3998 old_dev = drive_state[drive].fd_device;
3999 if (opened_bdev[drive] && opened_bdev[drive] != bdev)
4002 if (!drive_state[drive].fd_ref && (drive_params[drive].flags & FD_BROKEN_DCL)) {
4003 set_bit(FD_DISK_CHANGED_BIT, &drive_state[drive].flags);
4004 set_bit(FD_VERIFY_BIT, &drive_state[drive].flags);
4007 drive_state[drive].fd_ref++;
4009 opened_bdev[drive] = bdev;
4013 if (!floppy_track_buffer) {
4014 /* if opening an ED drive, reserve a big buffer,
4015 * else reserve a small one */
4016 if ((drive_params[drive].cmos == 6) || (drive_params[drive].cmos == 5))
4017 try = 64; /* Only 48 actually useful */
4019 try = 32; /* Only 24 actually useful */
4021 tmp = (char *)fd_dma_mem_alloc(1024 * try);
4022 if (!tmp && !floppy_track_buffer) {
4023 try >>= 1; /* buffer only one side */
4025 tmp = (char *)fd_dma_mem_alloc(1024 * try);
4027 if (!tmp && !floppy_track_buffer)
4028 fallback_on_nodma_alloc(&tmp, 2048 * try);
4029 if (!tmp && !floppy_track_buffer) {
4030 DPRINT("Unable to allocate DMA memory\n");
4033 if (floppy_track_buffer) {
4035 fd_dma_mem_free((unsigned long)tmp, try * 1024);
4037 buffer_min = buffer_max = -1;
4038 floppy_track_buffer = tmp;
4039 max_buffer_sectors = try;
4043 new_dev = MINOR(bdev->bd_dev);
4044 drive_state[drive].fd_device = new_dev;
4045 set_capacity(disks[drive][ITYPE(new_dev)], floppy_sizes[new_dev]);
4046 if (old_dev != -1 && old_dev != new_dev) {
4047 if (buffer_drive == drive)
4051 if (fdc_state[FDC(drive)].rawcmd == 1)
4052 fdc_state[FDC(drive)].rawcmd = 2;
4054 if (!(mode & FMODE_NDELAY)) {
4055 if (mode & (FMODE_READ|FMODE_WRITE)) {
4056 drive_state[drive].last_checked = 0;
4057 clear_bit(FD_OPEN_SHOULD_FAIL_BIT,
4058 &drive_state[drive].flags);
4059 if (bdev_check_media_change(bdev))
4060 floppy_revalidate(bdev->bd_disk);
4061 if (test_bit(FD_DISK_CHANGED_BIT, &drive_state[drive].flags))
4063 if (test_bit(FD_OPEN_SHOULD_FAIL_BIT, &drive_state[drive].flags))
4067 if ((mode & FMODE_WRITE) &&
4068 !test_bit(FD_DISK_WRITABLE_BIT, &drive_state[drive].flags))
4071 mutex_unlock(&open_lock);
4072 mutex_unlock(&floppy_mutex);
4075 drive_state[drive].fd_ref--;
4077 if (!drive_state[drive].fd_ref)
4078 opened_bdev[drive] = NULL;
4080 mutex_unlock(&open_lock);
4081 mutex_unlock(&floppy_mutex);
4086 * Check if the disk has been changed or if a change has been faked.
4088 static unsigned int floppy_check_events(struct gendisk *disk,
4089 unsigned int clearing)
4091 int drive = (long)disk->private_data;
4093 if (test_bit(FD_DISK_CHANGED_BIT, &drive_state[drive].flags) ||
4094 test_bit(FD_VERIFY_BIT, &drive_state[drive].flags))
4095 return DISK_EVENT_MEDIA_CHANGE;
4097 if (time_after(jiffies, drive_state[drive].last_checked + drive_params[drive].checkfreq)) {
4098 if (lock_fdc(drive))
4100 poll_drive(false, 0);
4101 process_fd_request();
4104 if (test_bit(FD_DISK_CHANGED_BIT, &drive_state[drive].flags) ||
4105 test_bit(FD_VERIFY_BIT, &drive_state[drive].flags) ||
4106 test_bit(drive, &fake_change) ||
4107 drive_no_geom(drive))
4108 return DISK_EVENT_MEDIA_CHANGE;
4113 * This implements "read block 0" for floppy_revalidate().
4114 * Needed for format autodetection, checking whether there is
4115 * a disk in the drive, and whether that disk is writable.
4120 struct completion complete;
4123 static void floppy_rb0_cb(struct bio *bio)
4125 struct rb0_cbdata *cbdata = (struct rb0_cbdata *)bio->bi_private;
4126 int drive = cbdata->drive;
4128 if (bio->bi_status) {
4129 pr_info("floppy: error %d while reading block 0\n",
4131 set_bit(FD_OPEN_SHOULD_FAIL_BIT, &drive_state[drive].flags);
4133 complete(&cbdata->complete);
4136 static int __floppy_read_block_0(struct block_device *bdev, int drive)
4139 struct bio_vec bio_vec;
4141 struct rb0_cbdata cbdata;
4143 page = alloc_page(GFP_NOIO);
4145 process_fd_request();
4149 cbdata.drive = drive;
4151 bio_init(&bio, bdev, &bio_vec, 1, REQ_OP_READ);
4152 bio_add_page(&bio, page, block_size(bdev), 0);
4154 bio.bi_iter.bi_sector = 0;
4155 bio.bi_flags |= (1 << BIO_QUIET);
4156 bio.bi_private = &cbdata;
4157 bio.bi_end_io = floppy_rb0_cb;
4159 init_completion(&cbdata.complete);
4162 process_fd_request();
4164 wait_for_completion(&cbdata.complete);
4171 /* revalidate the floppy disk, i.e. trigger format autodetection by reading
4172 * the bootblock (block 0). "Autodetection" is also needed to check whether
4173 * there is a disk in the drive at all... Thus we also do it for fixed
4174 * geometry formats */
4175 static int floppy_revalidate(struct gendisk *disk)
4177 int drive = (long)disk->private_data;
4181 if (test_bit(FD_DISK_CHANGED_BIT, &drive_state[drive].flags) ||
4182 test_bit(FD_VERIFY_BIT, &drive_state[drive].flags) ||
4183 test_bit(drive, &fake_change) ||
4184 drive_no_geom(drive)) {
4185 if (WARN(atomic_read(&usage_count) == 0,
4186 "VFS: revalidate called on non-open device.\n"))
4189 res = lock_fdc(drive);
4192 cf = (test_bit(FD_DISK_CHANGED_BIT, &drive_state[drive].flags) ||
4193 test_bit(FD_VERIFY_BIT, &drive_state[drive].flags));
4194 if (!(cf || test_bit(drive, &fake_change) || drive_no_geom(drive))) {
4195 process_fd_request(); /*already done by another thread */
4198 drive_state[drive].maxblock = 0;
4199 drive_state[drive].maxtrack = 0;
4200 if (buffer_drive == drive)
4202 clear_bit(drive, &fake_change);
4203 clear_bit(FD_DISK_CHANGED_BIT, &drive_state[drive].flags);
4205 drive_state[drive].generation++;
4206 if (drive_no_geom(drive)) {
4208 res = __floppy_read_block_0(opened_bdev[drive], drive);
4211 poll_drive(false, FD_RAW_NEED_DISK);
4212 process_fd_request();
4215 set_capacity(disk, floppy_sizes[drive_state[drive].fd_device]);
4219 static const struct block_device_operations floppy_fops = {
4220 .owner = THIS_MODULE,
4221 .open = floppy_open,
4222 .release = floppy_release,
4224 .getgeo = fd_getgeo,
4225 .check_events = floppy_check_events,
4226 #ifdef CONFIG_COMPAT
4227 .compat_ioctl = fd_compat_ioctl,
4232 * Floppy Driver initialization
4233 * =============================
4236 /* Determine the floppy disk controller type */
4237 /* This routine was written by David C. Niemi */
4238 static char __init get_fdc_version(int fdc)
4242 output_byte(fdc, FD_DUMPREGS); /* 82072 and better know DUMPREGS */
4243 if (fdc_state[fdc].reset)
4247 return FDC_NONE; /* No FDC present ??? */
4248 if ((r == 1) && (reply_buffer[ST0] == 0x80)) {
4249 pr_info("FDC %d is an 8272A\n", fdc);
4250 return FDC_8272A; /* 8272a/765 don't know DUMPREGS */
4253 pr_info("FDC %d init: DUMPREGS: unexpected return of %d bytes.\n",
4258 if (!fdc_configure(fdc)) {
4259 pr_info("FDC %d is an 82072\n", fdc);
4260 return FDC_82072; /* 82072 doesn't know CONFIGURE */
4263 output_byte(fdc, FD_PERPENDICULAR);
4264 if (need_more_output(fdc) == MORE_OUTPUT) {
4265 output_byte(fdc, 0);
4267 pr_info("FDC %d is an 82072A\n", fdc);
4268 return FDC_82072A; /* 82072A as found on Sparcs. */
4271 output_byte(fdc, FD_UNLOCK);
4273 if ((r == 1) && (reply_buffer[ST0] == 0x80)) {
4274 pr_info("FDC %d is a pre-1991 82077\n", fdc);
4275 return FDC_82077_ORIG; /* Pre-1991 82077, doesn't know
4278 if ((r != 1) || (reply_buffer[ST0] != 0x00)) {
4279 pr_info("FDC %d init: UNLOCK: unexpected return of %d bytes.\n",
4283 output_byte(fdc, FD_PARTID);
4286 pr_info("FDC %d init: PARTID: unexpected return of %d bytes.\n",
4290 if (reply_buffer[ST0] == 0x80) {
4291 pr_info("FDC %d is a post-1991 82077\n", fdc);
4292 return FDC_82077; /* Revised 82077AA passes all the tests */
4294 switch (reply_buffer[ST0] >> 5) {
4296 /* Either a 82078-1 or a 82078SL running at 5Volt */
4297 pr_info("FDC %d is an 82078.\n", fdc);
4300 pr_info("FDC %d is a 44pin 82078\n", fdc);
4303 pr_info("FDC %d is a S82078B\n", fdc);
4306 pr_info("FDC %d is a National Semiconductor PC87306\n", fdc);
4309 pr_info("FDC %d init: 82078 variant with unknown PARTID=%d.\n",
4310 fdc, reply_buffer[ST0] >> 5);
4311 return FDC_82078_UNKN;
4313 } /* get_fdc_version */
4315 /* lilo configuration */
4317 static void __init floppy_set_flags(int *ints, int param, int param2)
4321 for (i = 0; i < ARRAY_SIZE(default_drive_params); i++) {
4323 default_drive_params[i].params.flags |= param2;
4325 default_drive_params[i].params.flags &= ~param2;
4327 DPRINT("%s flag 0x%x\n", param2 ? "Setting" : "Clearing", param);
4330 static void __init daring(int *ints, int param, int param2)
4334 for (i = 0; i < ARRAY_SIZE(default_drive_params); i++) {
4336 default_drive_params[i].params.select_delay = 0;
4337 default_drive_params[i].params.flags |=
4338 FD_SILENT_DCL_CLEAR;
4340 default_drive_params[i].params.select_delay =
4342 default_drive_params[i].params.flags &=
4343 ~FD_SILENT_DCL_CLEAR;
4346 DPRINT("Assuming %s floppy hardware\n", param ? "standard" : "broken");
4349 static void __init set_cmos(int *ints, int dummy, int dummy2)
4351 int current_drive = 0;
4354 DPRINT("wrong number of parameters for CMOS\n");
4357 current_drive = ints[1];
4358 if (current_drive < 0 || current_drive >= 8) {
4359 DPRINT("bad drive for set_cmos\n");
4363 if (current_drive >= 4 && !FDC2)
4366 drive_params[current_drive].cmos = ints[2];
4367 DPRINT("setting CMOS code to %d\n", ints[2]);
4370 static struct param_table {
4372 void (*fn) (int *ints, int param, int param2);
4376 } config_params[] __initdata = {
4377 {"allowed_drive_mask", NULL, &allowed_drive_mask, 0xff, 0}, /* obsolete */
4378 {"all_drives", NULL, &allowed_drive_mask, 0xff, 0}, /* obsolete */
4379 {"asus_pci", NULL, &allowed_drive_mask, 0x33, 0},
4380 {"irq", NULL, &FLOPPY_IRQ, 6, 0},
4381 {"dma", NULL, &FLOPPY_DMA, 2, 0},
4382 {"daring", daring, NULL, 1, 0},
4384 {"two_fdc", NULL, &FDC2, 0x370, 0},
4385 {"one_fdc", NULL, &FDC2, 0, 0},
4387 {"thinkpad", floppy_set_flags, NULL, 1, FD_INVERTED_DCL},
4388 {"broken_dcl", floppy_set_flags, NULL, 1, FD_BROKEN_DCL},
4389 {"messages", floppy_set_flags, NULL, 1, FTD_MSG},
4390 {"silent_dcl_clear", floppy_set_flags, NULL, 1, FD_SILENT_DCL_CLEAR},
4391 {"debug", floppy_set_flags, NULL, 1, FD_DEBUG},
4392 {"nodma", NULL, &can_use_virtual_dma, 1, 0},
4393 {"omnibook", NULL, &can_use_virtual_dma, 1, 0},
4394 {"yesdma", NULL, &can_use_virtual_dma, 0, 0},
4395 {"fifo_depth", NULL, &fifo_depth, 0xa, 0},
4396 {"nofifo", NULL, &no_fifo, 0x20, 0},
4397 {"usefifo", NULL, &no_fifo, 0, 0},
4398 {"cmos", set_cmos, NULL, 0, 0},
4399 {"slow", NULL, &slow_floppy, 1, 0},
4400 {"unexpected_interrupts", NULL, &print_unex, 1, 0},
4401 {"no_unexpected_interrupts", NULL, &print_unex, 0, 0},
4402 {"L40SX", NULL, &print_unex, 0, 0}
4407 static int __init floppy_setup(char *str)
4413 str = get_options(str, ARRAY_SIZE(ints), ints);
4415 for (i = 0; i < ARRAY_SIZE(config_params); i++) {
4416 if (strcmp(str, config_params[i].name) == 0) {
4420 param = config_params[i].def_param;
4421 if (config_params[i].fn)
4422 config_params[i].fn(ints, param,
4425 if (config_params[i].var) {
4426 DPRINT("%s=%d\n", str, param);
4427 *config_params[i].var = param;
4434 DPRINT("unknown floppy option [%s]\n", str);
4436 DPRINT("allowed options are:");
4437 for (i = 0; i < ARRAY_SIZE(config_params); i++)
4438 pr_cont(" %s", config_params[i].name);
4441 DPRINT("botched floppy option\n");
4442 DPRINT("Read Documentation/admin-guide/blockdev/floppy.rst\n");
4446 static int have_no_fdc = -ENODEV;
4448 static ssize_t floppy_cmos_show(struct device *dev,
4449 struct device_attribute *attr, char *buf)
4451 struct platform_device *p = to_platform_device(dev);
4455 return sprintf(buf, "%X\n", drive_params[drive].cmos);
4458 static DEVICE_ATTR(cmos, 0444, floppy_cmos_show, NULL);
4460 static struct attribute *floppy_dev_attrs[] = {
4461 &dev_attr_cmos.attr,
4465 ATTRIBUTE_GROUPS(floppy_dev);
4467 static void floppy_device_release(struct device *dev)
4471 static int floppy_resume(struct device *dev)
4476 saved_drive = current_drive;
4477 for (fdc = 0; fdc < N_FDC; fdc++)
4478 if (fdc_state[fdc].address != -1)
4479 user_reset_fdc(REVDRIVE(fdc, 0), FD_RESET_ALWAYS, false);
4480 set_fdc(saved_drive);
4484 static const struct dev_pm_ops floppy_pm_ops = {
4485 .resume = floppy_resume,
4486 .restore = floppy_resume,
4489 static struct platform_driver floppy_driver = {
4492 .pm = &floppy_pm_ops,
4496 static const struct blk_mq_ops floppy_mq_ops = {
4497 .queue_rq = floppy_queue_rq,
4500 static struct platform_device floppy_device[N_DRIVE];
4501 static bool registered[N_DRIVE];
4503 static bool floppy_available(int drive)
4505 if (!(allowed_drive_mask & (1 << drive)))
4507 if (fdc_state[FDC(drive)].version == FDC_NONE)
4512 static int floppy_alloc_disk(unsigned int drive, unsigned int type)
4514 struct gendisk *disk;
4516 disk = blk_mq_alloc_disk(&tag_sets[drive], NULL);
4518 return PTR_ERR(disk);
4520 blk_queue_max_hw_sectors(disk->queue, 64);
4521 disk->major = FLOPPY_MAJOR;
4522 disk->first_minor = TOMINOR(drive) | (type << 2);
4524 disk->fops = &floppy_fops;
4525 disk->flags |= GENHD_FL_NO_PART;
4526 disk->events = DISK_EVENT_MEDIA_CHANGE;
4528 sprintf(disk->disk_name, "fd%d_type%d", drive, type);
4530 sprintf(disk->disk_name, "fd%d", drive);
4531 /* to be cleaned up... */
4532 disk->private_data = (void *)(long)drive;
4533 disk->flags |= GENHD_FL_REMOVABLE;
4535 disks[drive][type] = disk;
4539 static DEFINE_MUTEX(floppy_probe_lock);
4541 static void floppy_probe(dev_t dev)
4543 unsigned int drive = (MINOR(dev) & 3) | ((MINOR(dev) & 0x80) >> 5);
4544 unsigned int type = (MINOR(dev) >> 2) & 0x1f;
4546 if (drive >= N_DRIVE || !floppy_available(drive) ||
4547 type >= ARRAY_SIZE(floppy_type))
4550 mutex_lock(&floppy_probe_lock);
4551 if (disks[drive][type])
4553 if (floppy_alloc_disk(drive, type))
4555 if (add_disk(disks[drive][type]))
4558 mutex_unlock(&floppy_probe_lock);
4562 blk_cleanup_disk(disks[drive][type]);
4563 disks[drive][type] = NULL;
4564 mutex_unlock(&floppy_probe_lock);
4567 static int __init do_floppy_init(void)
4569 int i, unit, drive, err;
4572 interruptjiffies = resultjiffies = jiffies;
4574 #if defined(CONFIG_PPC)
4575 if (check_legacy_ioport(FDC1))
4581 floppy_wq = alloc_ordered_workqueue("floppy", 0);
4585 for (drive = 0; drive < N_DRIVE; drive++) {
4586 memset(&tag_sets[drive], 0, sizeof(tag_sets[drive]));
4587 tag_sets[drive].ops = &floppy_mq_ops;
4588 tag_sets[drive].nr_hw_queues = 1;
4589 tag_sets[drive].nr_maps = 1;
4590 tag_sets[drive].queue_depth = 2;
4591 tag_sets[drive].numa_node = NUMA_NO_NODE;
4592 tag_sets[drive].flags = BLK_MQ_F_SHOULD_MERGE;
4593 err = blk_mq_alloc_tag_set(&tag_sets[drive]);
4597 err = floppy_alloc_disk(drive, 0);
4601 timer_setup(&motor_off_timer[drive], motor_off_callback, 0);
4604 err = __register_blkdev(FLOPPY_MAJOR, "fd", floppy_probe);
4608 err = platform_driver_register(&floppy_driver);
4610 goto out_unreg_blkdev;
4612 for (i = 0; i < 256; i++)
4614 floppy_sizes[i] = floppy_type[ITYPE(i)].size;
4616 floppy_sizes[i] = MAX_DISK_SIZE << 1;
4618 reschedule_timeout(MAXTIMEOUT, "floppy init");
4621 for (i = 0; i < N_FDC; i++) {
4622 memset(&fdc_state[i], 0, sizeof(*fdc_state));
4623 fdc_state[i].dtr = -1;
4624 fdc_state[i].dor = 0x4;
4625 #if defined(__sparc__) || defined(__mc68000__)
4626 /*sparcs/sun3x don't have a DOR reset which we can fall back on to */
4630 fdc_state[i].version = FDC_82072A;
4634 use_virtual_dma = can_use_virtual_dma & 1;
4635 fdc_state[0].address = FDC1;
4636 if (fdc_state[0].address == -1) {
4637 cancel_delayed_work(&fd_timeout);
4639 goto out_unreg_driver;
4642 fdc_state[1].address = FDC2;
4645 current_fdc = 0; /* reset fdc in case of unexpected interrupt */
4646 err = floppy_grab_irq_and_dma();
4648 cancel_delayed_work(&fd_timeout);
4650 goto out_unreg_driver;
4653 /* initialise drive state */
4654 for (drive = 0; drive < N_DRIVE; drive++) {
4655 memset(&drive_state[drive], 0, sizeof(drive_state[drive]));
4656 memset(&write_errors[drive], 0, sizeof(write_errors[drive]));
4657 set_bit(FD_DISK_NEWCHANGE_BIT, &drive_state[drive].flags);
4658 set_bit(FD_DISK_CHANGED_BIT, &drive_state[drive].flags);
4659 set_bit(FD_VERIFY_BIT, &drive_state[drive].flags);
4660 drive_state[drive].fd_device = -1;
4661 floppy_track_buffer = NULL;
4662 max_buffer_sectors = 0;
4665 * Small 10 msec delay to let through any interrupt that
4666 * initialization might have triggered, to not
4667 * confuse detection:
4671 for (i = 0; i < N_FDC; i++) {
4672 fdc_state[i].driver_version = FD_DRIVER_VERSION;
4673 for (unit = 0; unit < 4; unit++)
4674 fdc_state[i].track[unit] = 0;
4675 if (fdc_state[i].address == -1)
4677 fdc_state[i].rawcmd = 2;
4678 if (user_reset_fdc(REVDRIVE(i, 0), FD_RESET_ALWAYS, false)) {
4679 /* free ioports reserved by floppy_grab_irq_and_dma() */
4680 floppy_release_regions(i);
4681 fdc_state[i].address = -1;
4682 fdc_state[i].version = FDC_NONE;
4685 /* Try to determine the floppy controller type */
4686 fdc_state[i].version = get_fdc_version(i);
4687 if (fdc_state[i].version == FDC_NONE) {
4688 /* free ioports reserved by floppy_grab_irq_and_dma() */
4689 floppy_release_regions(i);
4690 fdc_state[i].address = -1;
4693 if (can_use_virtual_dma == 2 &&
4694 fdc_state[i].version < FDC_82072A)
4695 can_use_virtual_dma = 0;
4698 /* Not all FDCs seem to be able to handle the version command
4699 * properly, so force a reset for the standard FDC clones,
4700 * to avoid interrupt garbage.
4702 user_reset_fdc(REVDRIVE(i, 0), FD_RESET_ALWAYS, false);
4705 cancel_delayed_work(&fd_timeout);
4709 DPRINT("no floppy controllers found\n");
4711 goto out_release_dma;
4714 for (drive = 0; drive < N_DRIVE; drive++) {
4715 if (!floppy_available(drive))
4718 floppy_device[drive].name = floppy_device_name;
4719 floppy_device[drive].id = drive;
4720 floppy_device[drive].dev.release = floppy_device_release;
4721 floppy_device[drive].dev.groups = floppy_dev_groups;
4723 err = platform_device_register(&floppy_device[drive]);
4725 goto out_remove_drives;
4727 registered[drive] = true;
4729 err = device_add_disk(&floppy_device[drive].dev,
4730 disks[drive][0], NULL);
4732 goto out_remove_drives;
4739 if (floppy_available(drive)) {
4740 del_gendisk(disks[drive][0]);
4741 if (registered[drive])
4742 platform_device_unregister(&floppy_device[drive]);
4746 if (atomic_read(&usage_count))
4747 floppy_release_irq_and_dma();
4749 platform_driver_unregister(&floppy_driver);
4751 unregister_blkdev(FLOPPY_MAJOR, "fd");
4753 destroy_workqueue(floppy_wq);
4754 for (drive = 0; drive < N_DRIVE; drive++) {
4755 if (!disks[drive][0])
4757 del_timer_sync(&motor_off_timer[drive]);
4758 blk_cleanup_disk(disks[drive][0]);
4759 blk_mq_free_tag_set(&tag_sets[drive]);
4765 static __init void floppy_async_init(void *data, async_cookie_t cookie)
4771 static int __init floppy_init(void)
4774 return do_floppy_init();
4776 /* Don't hold up the bootup by the floppy initialization */
4777 async_schedule(floppy_async_init, NULL);
4782 static const struct io_region {
4787 /* address + 3 is sometimes reserved by pnp bios for motherboard */
4789 /* address + 6 is reserved, and may be taken by IDE.
4790 * Unfortunately, Adaptec doesn't know this :-(, */
4794 static void floppy_release_allocated_regions(int fdc, const struct io_region *p)
4796 while (p != io_regions) {
4798 release_region(fdc_state[fdc].address + p->offset, p->size);
4802 #define ARRAY_END(X) (&((X)[ARRAY_SIZE(X)]))
4804 static int floppy_request_regions(int fdc)
4806 const struct io_region *p;
4808 for (p = io_regions; p < ARRAY_END(io_regions); p++) {
4809 if (!request_region(fdc_state[fdc].address + p->offset,
4810 p->size, "floppy")) {
4811 DPRINT("Floppy io-port 0x%04lx in use\n",
4812 fdc_state[fdc].address + p->offset);
4813 floppy_release_allocated_regions(fdc, p);
4820 static void floppy_release_regions(int fdc)
4822 floppy_release_allocated_regions(fdc, ARRAY_END(io_regions));
4825 static int floppy_grab_irq_and_dma(void)
4829 if (atomic_inc_return(&usage_count) > 1)
4833 * We might have scheduled a free_irq(), wait it to
4836 flush_workqueue(floppy_wq);
4838 if (fd_request_irq()) {
4839 DPRINT("Unable to grab IRQ%d for the floppy driver\n",
4841 atomic_dec(&usage_count);
4844 if (fd_request_dma()) {
4845 DPRINT("Unable to grab DMA%d for the floppy driver\n",
4847 if (can_use_virtual_dma & 2)
4848 use_virtual_dma = can_use_virtual_dma = 1;
4849 if (!(can_use_virtual_dma & 1)) {
4851 atomic_dec(&usage_count);
4856 for (fdc = 0; fdc < N_FDC; fdc++) {
4857 if (fdc_state[fdc].address != -1) {
4858 if (floppy_request_regions(fdc))
4862 for (fdc = 0; fdc < N_FDC; fdc++) {
4863 if (fdc_state[fdc].address != -1) {
4864 reset_fdc_info(fdc, 1);
4865 fdc_outb(fdc_state[fdc].dor, fdc, FD_DOR);
4869 set_dor(0, ~0, 8); /* avoid immediate interrupt */
4871 for (fdc = 0; fdc < N_FDC; fdc++)
4872 if (fdc_state[fdc].address != -1)
4873 fdc_outb(fdc_state[fdc].dor, fdc, FD_DOR);
4875 * The driver will try and free resources and relies on us
4876 * to know if they were allocated or not.
4879 irqdma_allocated = 1;
4885 floppy_release_regions(fdc);
4887 atomic_dec(&usage_count);
4891 static void floppy_release_irq_and_dma(void)
4898 unsigned long tmpaddr;
4900 if (!atomic_dec_and_test(&usage_count))
4903 if (irqdma_allocated) {
4907 irqdma_allocated = 0;
4914 if (floppy_track_buffer && max_buffer_sectors) {
4915 tmpsize = max_buffer_sectors * 1024;
4916 tmpaddr = (unsigned long)floppy_track_buffer;
4917 floppy_track_buffer = NULL;
4918 max_buffer_sectors = 0;
4919 buffer_min = buffer_max = -1;
4920 fd_dma_mem_free(tmpaddr, tmpsize);
4923 for (drive = 0; drive < N_FDC * 4; drive++)
4924 if (timer_pending(motor_off_timer + drive))
4925 pr_info("motor off timer %d still active\n", drive);
4928 if (delayed_work_pending(&fd_timeout))
4929 pr_info("floppy timer still active:%s\n", timeout_message);
4930 if (delayed_work_pending(&fd_timer))
4931 pr_info("auxiliary floppy timer still active\n");
4932 if (work_pending(&floppy_work))
4933 pr_info("work still pending\n");
4934 for (fdc = 0; fdc < N_FDC; fdc++)
4935 if (fdc_state[fdc].address != -1)
4936 floppy_release_regions(fdc);
4941 static char *floppy;
4943 static void __init parse_floppy_cfg_string(char *cfg)
4949 while (*cfg && *cfg != ' ' && *cfg != '\t')
4960 static int __init floppy_module_init(void)
4963 parse_floppy_cfg_string(floppy);
4964 return floppy_init();
4966 module_init(floppy_module_init);
4968 static void __exit floppy_module_exit(void)
4972 unregister_blkdev(FLOPPY_MAJOR, "fd");
4973 platform_driver_unregister(&floppy_driver);
4975 destroy_workqueue(floppy_wq);
4977 for (drive = 0; drive < N_DRIVE; drive++) {
4978 del_timer_sync(&motor_off_timer[drive]);
4980 if (floppy_available(drive)) {
4981 for (i = 0; i < ARRAY_SIZE(floppy_type); i++) {
4982 if (disks[drive][i])
4983 del_gendisk(disks[drive][i]);
4985 if (registered[drive])
4986 platform_device_unregister(&floppy_device[drive]);
4988 for (i = 0; i < ARRAY_SIZE(floppy_type); i++) {
4989 if (disks[drive][i])
4990 blk_cleanup_disk(disks[drive][i]);
4992 blk_mq_free_tag_set(&tag_sets[drive]);
4995 cancel_delayed_work_sync(&fd_timeout);
4996 cancel_delayed_work_sync(&fd_timer);
4998 if (atomic_read(&usage_count))
4999 floppy_release_irq_and_dma();
5001 /* eject disk, if any */
5005 module_exit(floppy_module_exit);
5007 module_param(floppy, charp, 0);
5008 module_param(FLOPPY_IRQ, int, 0);
5009 module_param(FLOPPY_DMA, int, 0);
5010 MODULE_AUTHOR("Alain L. Knaff");
5011 MODULE_LICENSE("GPL");
5013 /* This doesn't actually get used other than for module information */
5014 static const struct pnp_device_id floppy_pnpids[] = {
5019 MODULE_DEVICE_TABLE(pnp, floppy_pnpids);
5023 __setup("floppy=", floppy_setup);
5024 module_init(floppy_init)
5027 MODULE_ALIAS_BLOCKDEV_MAJOR(FLOPPY_MAJOR);