Blame view

drivers/block/floppy.c 117 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
  /*
   *  linux/drivers/block/floppy.c
   *
   *  Copyright (C) 1991, 1992  Linus Torvalds
   *  Copyright (C) 1993, 1994  Alain Knaff
   *  Copyright (C) 1998 Alan Cox
   */
06f748c47   Jesper Juhl   floppy: do a very...
8

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
  /*
   * 02.12.91 - Changed to static variables to indicate need for reset
   * and recalibrate. This makes some things easier (output_byte reset
   * checking etc), and means less interrupt jumping in case of errors,
   * so the code is hopefully easier to understand.
   */
  
  /*
   * This file is certainly a mess. I've tried my best to get it working,
   * but I don't like programming floppies, and I have only one anyway.
   * Urgel. I should check for more errors, and do more graceful error
   * recovery. Seems there are problems with several drives. I've tried to
   * correct them. No promises.
   */
  
  /*
   * As with hd.c, all routines within this file can (and will) be called
   * by interrupts, so extreme caution is needed. A hardware interrupt
   * handler may not sleep, or a kernel panic will happen. Thus I cannot
   * call "floppy-on" directly, but have to set a special timer interrupt
   * etc.
   */
  
  /*
   * 28.02.92 - made track-buffering routines, based on the routines written
   * by entropy@wintermute.wpi.edu (Lawrence Foard). Linus.
   */
  
  /*
   * Automatic floppy-detection and formatting written by Werner Almesberger
   * (almesber@nessie.cs.id.ethz.ch), who also corrected some problems with
   * the floppy-change signal detection.
   */
  
  /*
   * 1992/7/22 -- Hennus Bergman: Added better error reporting, fixed
   * FDC data overrun bug, added some preliminary stuff for vertical
   * recording support.
   *
   * 1992/9/17: Added DMA allocation & DMA functions. -- hhb.
   *
   * TODO: Errors are still not counted properly.
   */
  
  /* 1992/9/20
   * Modifications for ``Sector Shifting'' by Rob Hooft (hooft@chem.ruu.nl)
   * modeled after the freeware MS-DOS program fdformat/88 V1.8 by
   * Christoph H. Hochst\"atter.
   * I have fixed the shift values to the ones I always use. Maybe a new
   * ioctl() should be created to be able to modify them.
   * There is a bug in the driver that makes it impossible to format a
   * floppy as the first thing after bootup.
   */
  
  /*
   * 1993/4/29 -- Linus -- cleaned up the timer handling in the kernel, and
   * this helped the floppy driver as well. Much cleaner, and still seems to
   * work.
   */
  
  /* 1994/6/24 --bbroad-- added the floppy table entries and made
   * minor modifications to allow 2.88 floppies to be run.
   */
  
  /* 1994/7/13 -- Paul Vojta -- modified the probing code to allow three or more
   * disk types.
   */
  
  /*
   * 1994/8/8 -- Alain Knaff -- Switched to fdpatch driver: Support for bigger
   * format bug fixes, but unfortunately some new bugs too...
   */
  
  /* 1994/9/17 -- Koen Holtman -- added logging of physical floppy write
   * errors to allow safe writing by specialized programs.
   */
  
  /* 1995/4/24 -- Dan Fandrich -- added support for Commodore 1581 3.5" disks
   * by defining bit 1 of the "stretch" parameter to mean put sectors on the
   * opposite side of the disk, leaving the sector IDs alone (i.e. Commodore's
   * drives are "upside-down").
   */
  
  /*
   * 1995/8/26 -- Andreas Busse -- added Mips support.
   */
  
  /*
   * 1995/10/18 -- Ralf Baechle -- Portability cleanup; move machine dependent
   * features to asm/floppy.h.
   */
  
  /*
b88b09851   James Nelson   floppy: relocate ...
102
103
104
105
   * 1998/1/21 -- Richard Gooch <rgooch@atnf.csiro.au> -- devfs support
   */
  
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
   * 1998/05/07 -- Russell King -- More portability cleanups; moved definition of
   * interrupt and dma channel to asm/floppy.h. Cleaned up some formatting &
   * use of '0' for NULL.
   */
  
  /*
   * 1998/06/07 -- Alan Cox -- Merged the 2.0.34 fixes for resource allocation
   * failures.
   */
  
  /*
   * 1998/09/20 -- David Weinehall -- Added slow-down code for buggy PS/2-drives.
   */
  
  /*
   * 1999/08/13 -- Paul Slootman -- floppy stopped working on Alpha after 24
   * days, 6 hours, 32 minutes and 32 seconds (i.e. MAXINT jiffies; ints were
   * being used to store jiffies, which are unsigned longs).
   */
  
  /*
   * 2000/08/28 -- Arnaldo Carvalho de Melo <acme@conectiva.com.br>
   * - get rid of check_region
   * - s/suser/capable/
   */
  
  /*
   * 2001/08/26 -- Paul Gortmaker - fix insmod oops on machines with no
   * floppy controller (lingering task on list after module is gone... boom.)
   */
  
  /*
   * 2002/02/07 -- Anton Altaparmakov - Fix io ports reservation to correct range
   * (0x3f2-0x3f5, 0x3f7). This fix is a bit of a hack but the proper fix
   * requires many non-obvious changes in arch dependent code.
   */
  
  /* 2003/07/28 -- Daniele Bellucci <bellucda@tiscali.it>.
   * Better audit of register_blkdev.
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
146
147
148
149
150
  #undef  FLOPPY_SILENT_DCL_CLEAR
  
  #define REALLY_SLOW_IO
  
  #define DEBUGT 2
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
151

891eda80a   Joe Perches   drivers/block/flo...
152
153
154
155
  #define DPRINT(format, args...) \
  	pr_info("floppy%d: " format, current_drive, ##args)
  
  #define DCL_DEBUG		/* debug disk change line */
87f530d8f   Joe Perches   drivers/block/flo...
156
157
158
159
160
161
162
  #ifdef DCL_DEBUG
  #define debug_dcl(test, fmt, args...) \
  	do { if ((test) & FD_DEBUG) DPRINT(fmt, ##args); } while (0)
  #else
  #define debug_dcl(test, fmt, args...) \
  	do { if (0) DPRINT(fmt, ##args); } while (0)
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
163
164
165
166
167
168
169
170
171
172
  /* do print messages for unexpected interrupts */
  static int print_unex = 1;
  #include <linux/module.h>
  #include <linux/sched.h>
  #include <linux/fs.h>
  #include <linux/kernel.h>
  #include <linux/timer.h>
  #include <linux/workqueue.h>
  #define FDPATCHES
  #include <linux/fdreg.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
173
174
  #include <linux/fd.h>
  #include <linux/hdreg.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
175
176
177
178
179
  #include <linux/errno.h>
  #include <linux/slab.h>
  #include <linux/mm.h>
  #include <linux/bio.h>
  #include <linux/string.h>
50297cbf0   Marcelo Feitoza Parisi   [PATCH] drivers/b...
180
  #include <linux/jiffies.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
181
182
183
184
185
186
  #include <linux/fcntl.h>
  #include <linux/delay.h>
  #include <linux/mc146818rtc.h>	/* CMOS defines */
  #include <linux/ioport.h>
  #include <linux/interrupt.h>
  #include <linux/init.h>
d052d1bef   Russell King   Create platform_d...
187
  #include <linux/platform_device.h>
83f9ef463   Scott James Remnant   floppy: provide a...
188
  #include <linux/mod_devicetable.h>
b1c82b5c5   Jes Sorensen   [PATCH] sem2mutex...
189
  #include <linux/mutex.h>
d49375434   Joe Perches   drivers/block/flo...
190
191
  #include <linux/io.h>
  #include <linux/uaccess.h>
0cc15d03b   Andi Kleen   floppy: Run flopp...
192
  #include <linux/async.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
193
194
195
196
197
198
  
  /*
   * PS/2 floppies have much slower step rates than regular floppies.
   * It's been recommended that take about 1/4 of the default speed
   * in some more extreme cases.
   */
2a48fc0ab   Arnd Bergmann   block: autoconver...
199
  static DEFINE_MUTEX(floppy_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
200
201
202
203
  static int slow_floppy;
  
  #include <asm/dma.h>
  #include <asm/irq.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
  
  static int FLOPPY_IRQ = 6;
  static int FLOPPY_DMA = 2;
  static int can_use_virtual_dma = 2;
  /* =======
   * can use virtual DMA:
   * 0 = use of virtual DMA disallowed by config
   * 1 = use of virtual DMA prescribed by config
   * 2 = no virtual DMA preference configured.  By default try hard DMA,
   * but fall back on virtual DMA when not enough memory available
   */
  
  static int use_virtual_dma;
  /* =======
   * use virtual DMA
   * 0 using hard DMA
   * 1 using virtual DMA
   * This variable is set to virtual when a DMA mem problem arises, and
   * reset back in floppy_grab_irq_and_dma.
   * It is not safe to reset it in other circumstances, because the floppy
   * driver may have several buffers in use at once, and we do currently not
   * record each buffers capabilities
   */
  
  static DEFINE_SPINLOCK(floppy_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
229
230
  
  static unsigned short virtual_dma_port = 0x3f0;
7d12e780e   David Howells   IRQ: Maintain reg...
231
  irqreturn_t floppy_interrupt(int irq, void *dev_id);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
232
  static int set_dor(int fdc, char mask, char data);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
  
  #define K_64	0x10000		/* 64KB */
  
  /* the following is the mask of allowed drives. By default units 2 and
   * 3 of both floppy controllers are disabled, because switching on the
   * motor of these drives causes system hangs on some PCI computers. drive
   * 0 is the low bit (0x1), and drive 7 is the high bit (0x80). Bits are on if
   * a drive is allowed.
   *
   * NOTE: This must come before we include the arch floppy header because
   *       some ports reference this variable from there. -DaveM
   */
  
  static int allowed_drive_mask = 0x33;
  
  #include <asm/floppy.h>
  
  static int irqdma_allocated;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
251
252
253
254
255
256
  #include <linux/blkdev.h>
  #include <linux/blkpg.h>
  #include <linux/cdrom.h>	/* for the compatibility eject ioctl */
  #include <linux/completion.h>
  
  static struct request *current_req;
48c8cee61   Joe Perches   drivers/block/flo...
257
  static void do_fd_request(struct request_queue *q);
488211844   Jens Axboe   floppy: switch to...
258
  static int set_next_request(void);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
259
260
261
262
263
264
265
266
267
268
269
270
  
  #ifndef fd_get_dma_residue
  #define fd_get_dma_residue() get_dma_residue(FLOPPY_DMA)
  #endif
  
  /* Dma Memory related stuff */
  
  #ifndef fd_dma_mem_free
  #define fd_dma_mem_free(addr, size) free_pages(addr, get_order(size))
  #endif
  
  #ifndef fd_dma_mem_alloc
48c8cee61   Joe Perches   drivers/block/flo...
271
  #define fd_dma_mem_alloc(size) __get_dma_pages(GFP_KERNEL, get_order(size))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
272
273
274
275
276
277
278
279
280
  #endif
  
  static inline void fallback_on_nodma_alloc(char **addr, size_t l)
  {
  #ifdef FLOPPY_CAN_FALLBACK_ON_NODMA
  	if (*addr)
  		return;		/* we have the memory */
  	if (can_use_virtual_dma != 2)
  		return;		/* no fallback allowed */
b46df356d   Joe Perches   drivers/block/flo...
281
282
  	pr_info("DMA memory shortage. Temporarily falling back on virtual DMA
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
283
284
285
286
287
288
289
290
291
  	*addr = (char *)nodma_mem_alloc(l);
  #else
  	return;
  #endif
  }
  
  /* End dma memory related stuff */
  
  static unsigned long fake_change;
29f1c7848   Joe Perches   drivers/block/flo...
292
  static bool initialized;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
293

48c8cee61   Joe Perches   drivers/block/flo...
294
295
296
297
  #define ITYPE(x)	(((x) >> 2) & 0x1f)
  #define TOMINOR(x)	((x & 3) | ((x & 4) << 5))
  #define UNIT(x)		((x) & 0x03)		/* drive on fdc */
  #define FDC(x)		(((x) & 0x04) >> 2)	/* fdc of drive */
06f748c47   Jesper Juhl   floppy: do a very...
298
  	/* reverse mapping from unit and fdc to drive */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
299
  #define REVDRIVE(fdc, unit) ((unit) + ((fdc) << 2))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
300

48c8cee61   Joe Perches   drivers/block/flo...
301
302
303
304
  #define DP	(&drive_params[current_drive])
  #define DRS	(&drive_state[current_drive])
  #define DRWE	(&write_errors[current_drive])
  #define FDCS	(&fdc_state[fdc])
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
305

48c8cee61   Joe Perches   drivers/block/flo...
306
307
308
309
  #define UDP	(&drive_params[drive])
  #define UDRS	(&drive_state[drive])
  #define UDRWE	(&write_errors[drive])
  #define UFDCS	(&fdc_state[FDC(drive)])
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
310

48c8cee61   Joe Perches   drivers/block/flo...
311
312
  #define PH_HEAD(floppy, head) (((((floppy)->stretch & 2) >> 1) ^ head) << 2)
  #define STRETCH(floppy)	((floppy)->stretch & FD_STRETCH)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
313
  /* read/write */
48c8cee61   Joe Perches   drivers/block/flo...
314
315
316
317
318
319
320
321
322
  #define COMMAND		(raw_cmd->cmd[0])
  #define DR_SELECT	(raw_cmd->cmd[1])
  #define TRACK		(raw_cmd->cmd[2])
  #define HEAD		(raw_cmd->cmd[3])
  #define SECTOR		(raw_cmd->cmd[4])
  #define SIZECODE	(raw_cmd->cmd[5])
  #define SECT_PER_TRACK	(raw_cmd->cmd[6])
  #define GAP		(raw_cmd->cmd[7])
  #define SIZECODE2	(raw_cmd->cmd[8])
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
323
324
325
  #define NR_RW 9
  
  /* format */
48c8cee61   Joe Perches   drivers/block/flo...
326
327
328
329
  #define F_SIZECODE	(raw_cmd->cmd[2])
  #define F_SECT_PER_TRACK (raw_cmd->cmd[3])
  #define F_GAP		(raw_cmd->cmd[4])
  #define F_FILL		(raw_cmd->cmd[5])
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
330
331
332
  #define NR_F 6
  
  /*
48c8cee61   Joe Perches   drivers/block/flo...
333
334
   * Maximum disk size (in kilobytes).
   * This default is used whenever the current disk size is unknown.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
335
336
337
338
339
340
341
342
343
   * [Now it is rather a minimum]
   */
  #define MAX_DISK_SIZE 4		/* 3984 */
  
  /*
   * globals used by 'result()'
   */
  #define MAX_REPLIES 16
  static unsigned char reply_buffer[MAX_REPLIES];
891eda80a   Joe Perches   drivers/block/flo...
344
  static int inr;		/* size of reply buffer, when called from interrupt */
48c8cee61   Joe Perches   drivers/block/flo...
345
346
347
348
349
350
351
352
353
354
  #define ST0		(reply_buffer[0])
  #define ST1		(reply_buffer[1])
  #define ST2		(reply_buffer[2])
  #define ST3		(reply_buffer[0])	/* result of GETSTATUS */
  #define R_TRACK		(reply_buffer[3])
  #define R_HEAD		(reply_buffer[4])
  #define R_SECTOR	(reply_buffer[5])
  #define R_SIZECODE	(reply_buffer[6])
  
  #define SEL_DLY		(2 * HZ / 100)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
  
  /*
   * this struct defines the different floppy drive types.
   */
  static struct {
  	struct floppy_drive_params params;
  	const char *name;	/* name printed while booting */
  } default_drive_params[] = {
  /* NOTE: the time values in jiffies should be in msec!
   CMOS drive type
    |     Maximum data rate supported by drive type
    |     |   Head load time, msec
    |     |   |   Head unload time, msec (not used)
    |     |   |   |     Step rate interval, usec
    |     |   |   |     |       Time needed for spinup time (jiffies)
    |     |   |   |     |       |      Timeout for spinning down (jiffies)
    |     |   |   |     |       |      |   Spindown offset (where disk stops)
    |     |   |   |     |       |      |   |     Select delay
    |     |   |   |     |       |      |   |     |     RPS
    |     |   |   |     |       |      |   |     |     |    Max number of tracks
    |     |   |   |     |       |      |   |     |     |    |     Interrupt timeout
    |     |   |   |     |       |      |   |     |     |    |     |   Max nonintlv. sectors
    |     |   |   |     |       |      |   |     |     |    |     |   | -Max Errors- flags */
  {{0,  500, 16, 16, 8000,    1*HZ, 3*HZ,  0, SEL_DLY, 5,  80, 3*HZ, 20, {3,1,2,0,2}, 0,
        0, { 7, 4, 8, 2, 1, 5, 3,10}, 3*HZ/2, 0 }, "unknown" },
  
  {{1,  300, 16, 16, 8000,    1*HZ, 3*HZ,  0, SEL_DLY, 5,  40, 3*HZ, 17, {3,1,2,0,2}, 0,
        0, { 1, 0, 0, 0, 0, 0, 0, 0}, 3*HZ/2, 1 }, "360K PC" }, /*5 1/4 360 KB PC*/
  
  {{2,  500, 16, 16, 6000, 4*HZ/10, 3*HZ, 14, SEL_DLY, 6,  83, 3*HZ, 17, {3,1,2,0,2}, 0,
        0, { 2, 5, 6,23,10,20,12, 0}, 3*HZ/2, 2 }, "1.2M" }, /*5 1/4 HD AT*/
  
  {{3,  250, 16, 16, 3000,    1*HZ, 3*HZ,  0, SEL_DLY, 5,  83, 3*HZ, 20, {3,1,2,0,2}, 0,
        0, { 4,22,21,30, 3, 0, 0, 0}, 3*HZ/2, 4 }, "720k" }, /*3 1/2 DD*/
  
  {{4,  500, 16, 16, 4000, 4*HZ/10, 3*HZ, 10, SEL_DLY, 5,  83, 3*HZ, 20, {3,1,2,0,2}, 0,
        0, { 7, 4,25,22,31,21,29,11}, 3*HZ/2, 7 }, "1.44M" }, /*3 1/2 HD*/
  
  {{5, 1000, 15,  8, 3000, 4*HZ/10, 3*HZ, 10, SEL_DLY, 5,  83, 3*HZ, 40, {3,1,2,0,2}, 0,
        0, { 7, 8, 4,25,28,22,31,21}, 3*HZ/2, 8 }, "2.88M AMI BIOS" }, /*3 1/2 ED*/
  
  {{6, 1000, 15,  8, 3000, 4*HZ/10, 3*HZ, 10, SEL_DLY, 5,  83, 3*HZ, 40, {3,1,2,0,2}, 0,
        0, { 7, 8, 4,25,28,22,31,21}, 3*HZ/2, 8 }, "2.88M" } /*3 1/2 ED*/
  /*    |  --autodetected formats---    |      |      |
   *    read_track                      |      |    Name printed when booting
   *				      |     Native format
   *	            Frequency of disk change checks */
  };
  
  static struct floppy_drive_params drive_params[N_DRIVE];
  static struct floppy_drive_struct drive_state[N_DRIVE];
  static struct floppy_write_errors write_errors[N_DRIVE];
  static struct timer_list motor_off_timer[N_DRIVE];
  static struct gendisk *disks[N_DRIVE];
  static struct block_device *opened_bdev[N_DRIVE];
b1c82b5c5   Jes Sorensen   [PATCH] sem2mutex...
410
  static DEFINE_MUTEX(open_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
411
  static struct floppy_raw_cmd *raw_cmd, default_raw_cmd;
488211844   Jens Axboe   floppy: switch to...
412
  static int fdc_queue;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
413
414
415
416
417
418
419
420
421
422
423
424
  
  /*
   * This struct defines the different floppy types.
   *
   * Bit 0 of 'stretch' tells if the tracks need to be doubled for some
   * types (e.g. 360kB diskette in 1.2MB drive, etc.).  Bit 1 of 'stretch'
   * tells if the disk is in Commodore 1581 format, which means side 0 sectors
   * are located on side 1 of the disk but with a side 0 ID, and vice-versa.
   * This is the same as the Sharp MZ-80 5.25" CP/M disk format, except that the
   * 1581's logical side 0 is on physical side 1, whereas the Sharp's logical
   * side 0 is on physical side 0 (but with the misnamed sector IDs).
   * 'stretch' should probably be renamed to something more general, like
9e49184c8   Keith Wansbrough   floppy: support a...
425
426
427
428
429
430
431
432
433
   * 'options'.
   *
   * Bits 2 through 9 of 'stretch' tell the number of the first sector.
   * The LSB (bit 2) is flipped. For most disks, the first sector
   * is 1 (represented by 0x00<<2).  For some CP/M and music sampler
   * disks (such as Ensoniq EPS 16plus) it is 0 (represented as 0x01<<2).
   * For Amstrad CPC disks it is 0xC1 (represented as 0xC0<<2).
   *
   * Other parameters should be self-explanatory (see also setfdprm(8)).
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
   */
  /*
  	    Size
  	     |  Sectors per track
  	     |  | Head
  	     |  | |  Tracks
  	     |  | |  | Stretch
  	     |  | |  | |  Gap 1 size
  	     |  | |  | |    |  Data rate, | 0x40 for perp
  	     |  | |  | |    |    |  Spec1 (stepping rate, head unload
  	     |  | |  | |    |    |    |    /fmt gap (gap2) */
  static struct floppy_struct floppy_type[32] = {
  	{    0, 0,0, 0,0,0x00,0x00,0x00,0x00,NULL    },	/*  0 no testing    */
  	{  720, 9,2,40,0,0x2A,0x02,0xDF,0x50,"d360"  }, /*  1 360KB PC      */
  	{ 2400,15,2,80,0,0x1B,0x00,0xDF,0x54,"h1200" },	/*  2 1.2MB AT      */
  	{  720, 9,1,80,0,0x2A,0x02,0xDF,0x50,"D360"  },	/*  3 360KB SS 3.5" */
  	{ 1440, 9,2,80,0,0x2A,0x02,0xDF,0x50,"D720"  },	/*  4 720KB 3.5"    */
  	{  720, 9,2,40,1,0x23,0x01,0xDF,0x50,"h360"  },	/*  5 360KB AT      */
  	{ 1440, 9,2,80,0,0x23,0x01,0xDF,0x50,"h720"  },	/*  6 720KB AT      */
  	{ 2880,18,2,80,0,0x1B,0x00,0xCF,0x6C,"H1440" },	/*  7 1.44MB 3.5"   */
  	{ 5760,36,2,80,0,0x1B,0x43,0xAF,0x54,"E2880" },	/*  8 2.88MB 3.5"   */
  	{ 6240,39,2,80,0,0x1B,0x43,0xAF,0x28,"E3120" },	/*  9 3.12MB 3.5"   */
  
  	{ 2880,18,2,80,0,0x25,0x00,0xDF,0x02,"h1440" }, /* 10 1.44MB 5.25"  */
  	{ 3360,21,2,80,0,0x1C,0x00,0xCF,0x0C,"H1680" }, /* 11 1.68MB 3.5"   */
  	{  820,10,2,41,1,0x25,0x01,0xDF,0x2E,"h410"  },	/* 12 410KB 5.25"   */
  	{ 1640,10,2,82,0,0x25,0x02,0xDF,0x2E,"H820"  },	/* 13 820KB 3.5"    */
  	{ 2952,18,2,82,0,0x25,0x00,0xDF,0x02,"h1476" },	/* 14 1.48MB 5.25"  */
  	{ 3444,21,2,82,0,0x25,0x00,0xDF,0x0C,"H1722" },	/* 15 1.72MB 3.5"   */
  	{  840,10,2,42,1,0x25,0x01,0xDF,0x2E,"h420"  },	/* 16 420KB 5.25"   */
  	{ 1660,10,2,83,0,0x25,0x02,0xDF,0x2E,"H830"  },	/* 17 830KB 3.5"    */
  	{ 2988,18,2,83,0,0x25,0x00,0xDF,0x02,"h1494" },	/* 18 1.49MB 5.25"  */
  	{ 3486,21,2,83,0,0x25,0x00,0xDF,0x0C,"H1743" }, /* 19 1.74 MB 3.5"  */
  
  	{ 1760,11,2,80,0,0x1C,0x09,0xCF,0x00,"h880"  }, /* 20 880KB 5.25"   */
  	{ 2080,13,2,80,0,0x1C,0x01,0xCF,0x00,"D1040" }, /* 21 1.04MB 3.5"   */
  	{ 2240,14,2,80,0,0x1C,0x19,0xCF,0x00,"D1120" }, /* 22 1.12MB 3.5"   */
  	{ 3200,20,2,80,0,0x1C,0x20,0xCF,0x2C,"h1600" }, /* 23 1.6MB 5.25"   */
  	{ 3520,22,2,80,0,0x1C,0x08,0xCF,0x2e,"H1760" }, /* 24 1.76MB 3.5"   */
  	{ 3840,24,2,80,0,0x1C,0x20,0xCF,0x00,"H1920" }, /* 25 1.92MB 3.5"   */
  	{ 6400,40,2,80,0,0x25,0x5B,0xCF,0x00,"E3200" }, /* 26 3.20MB 3.5"   */
  	{ 7040,44,2,80,0,0x25,0x5B,0xCF,0x00,"E3520" }, /* 27 3.52MB 3.5"   */
  	{ 7680,48,2,80,0,0x25,0x63,0xCF,0x00,"E3840" }, /* 28 3.84MB 3.5"   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
477
  	{ 3680,23,2,80,0,0x1C,0x10,0xCF,0x00,"H1840" }, /* 29 1.84MB 3.5"   */
06f748c47   Jesper Juhl   floppy: do a very...
478

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
479
480
481
  	{ 1600,10,2,80,0,0x25,0x02,0xDF,0x2E,"D800"  },	/* 30 800KB 3.5"    */
  	{ 3200,20,2,80,0,0x1C,0x00,0xCF,0x2C,"H1600" }, /* 31 1.6MB 3.5"    */
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
482
483
484
485
486
487
488
489
490
491
492
493
  #define SECTSIZE (_FD_SECTSIZE(*floppy))
  
  /* Auto-detection: Disk type used until the next media change occurs. */
  static struct floppy_struct *current_type[N_DRIVE];
  
  /*
   * User-provided type information. current_type points to
   * the respective entry of this array.
   */
  static struct floppy_struct user_params[N_DRIVE];
  
  static sector_t floppy_sizes[256];
94fd0db7b   Hannes Reinecke   [PATCH] Floppy: A...
494
  static char floppy_device_name[] = "floppy";
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
495
496
497
498
499
500
501
502
  /*
   * The driver is trying to determine the correct media format
   * while probing is set. rw_interrupt() clears it after a
   * successful access.
   */
  static int probing;
  
  /* Synchronization of FDC access. */
48c8cee61   Joe Perches   drivers/block/flo...
503
504
505
  #define FD_COMMAND_NONE		-1
  #define FD_COMMAND_ERROR	2
  #define FD_COMMAND_OKAY		3
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
506
507
508
509
510
  
  static volatile int command_status = FD_COMMAND_NONE;
  static unsigned long fdc_busy;
  static DECLARE_WAIT_QUEUE_HEAD(fdc_wait);
  static DECLARE_WAIT_QUEUE_HEAD(command_done);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
  /* Errors during formatting are counted here. */
  static int format_errors;
  
  /* Format request descriptor. */
  static struct format_descr format_req;
  
  /*
   * Rate is 0 for 500kb/s, 1 for 300kbps, 2 for 250kbps
   * Spec1 is 0xSH, where S is stepping rate (F=1ms, E=2ms, D=3ms etc),
   * H is head unload time (1=16ms, 2=32ms, etc)
   */
  
  /*
   * Track buffer
   * Because these are written to by the DMA controller, they must
   * not contain a 64k byte boundary crossing, or data will be
   * corrupted/lost.
   */
  static char *floppy_track_buffer;
  static int max_buffer_sectors;
  
  static int *errors;
06f748c47   Jesper Juhl   floppy: do a very...
533
  typedef void (*done_f)(int);
3b06c21e8   Stephen Hemminger   floppy: make cont...
534
  static const struct cont_t {
48c8cee61   Joe Perches   drivers/block/flo...
535
536
537
  	void (*interrupt)(void);
  				/* this is called after the interrupt of the
  				 * main command */
06f748c47   Jesper Juhl   floppy: do a very...
538
539
  	void (*redo)(void);	/* this is called to retry the operation */
  	void (*error)(void);	/* this is called to tally an error */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
540
541
542
543
544
545
546
547
  	done_f done;		/* this is called to say if the operation has
  				 * succeeded/failed */
  } *cont;
  
  static void floppy_ready(void);
  static void floppy_start(void);
  static void process_fd_request(void);
  static void recalibrate_floppy(void);
070ad7e79   Jiri Kosina   floppy: convert t...
548
  static void floppy_shutdown(struct work_struct *);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
549

5a74db06c   Philippe De Muyter   floppy: request a...
550
551
  static int floppy_request_regions(int);
  static void floppy_release_regions(int);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
552
553
554
555
556
557
558
559
560
561
  static int floppy_grab_irq_and_dma(void);
  static void floppy_release_irq_and_dma(void);
  
  /*
   * The "reset" variable should be tested whenever an interrupt is scheduled,
   * after the commands have been sent. This is to ensure that the driver doesn't
   * get wedged when the interrupt doesn't come because of a failed command.
   * reset doesn't need to be tested before sending commands, because
   * output_byte is automatically disabled when reset is set.
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
562
563
564
565
566
567
568
  static void reset_fdc(void);
  
  /*
   * These are global variables, as that's the easiest way to give
   * information to interrupts. They are the data used for the current
   * request.
   */
48c8cee61   Joe Perches   drivers/block/flo...
569
570
571
  #define NO_TRACK	-1
  #define NEED_1_RECAL	-2
  #define NEED_2_RECAL	-3
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
572

575cfc673   Stephen Hemminger   floppy: use atomi...
573
  static atomic_t usage_count = ATOMIC_INIT(0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
574
575
576
577
578
579
580
581
582
583
  
  /* buffer related variables */
  static int buffer_track = -1;
  static int buffer_drive = -1;
  static int buffer_min = -1;
  static int buffer_max = -1;
  
  /* fdc related variables, should end up in a struct */
  static struct floppy_fdc_state fdc_state[N_FDC];
  static int fdc;			/* current fdc */
070ad7e79   Jiri Kosina   floppy: convert t...
584
  static struct workqueue_struct *floppy_wq;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
585
586
587
588
589
590
  static struct floppy_struct *_floppy = floppy_type;
  static unsigned char current_drive;
  static long current_count_sectors;
  static unsigned char fsector_t;	/* sector in track */
  static unsigned char in_sector_offset;	/* offset within physical sector,
  					 * expressed in units of 512 bytes */
2b51dca79   Pekka Enberg   floppy: replace N...
591
592
593
594
  static inline bool drive_no_geom(int drive)
  {
  	return !current_type[drive] && !ITYPE(UDRS->fd_device);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
  #ifndef fd_eject
  static inline int fd_eject(int drive)
  {
  	return -EINVAL;
  }
  #endif
  
  /*
   * Debugging
   * =========
   */
  #ifdef DEBUGT
  static long unsigned debugtimer;
  
  static inline void set_debugt(void)
  {
  	debugtimer = jiffies;
  }
ded2863d0   Joe Perches   drivers/block/flo...
613
  static inline void debugt(const char *func, const char *msg)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
614
615
  {
  	if (DP->flags & DEBUGT)
ded2863d0   Joe Perches   drivers/block/flo...
616
617
  		pr_info("%s:%s dtime=%lu
  ", func, msg, jiffies - debugtimer);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
618
619
620
  }
  #else
  static inline void set_debugt(void) { }
ded2863d0   Joe Perches   drivers/block/flo...
621
  static inline void debugt(const char *func, const char *msg) { }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
622
  #endif /* DEBUGT */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
623

070ad7e79   Jiri Kosina   floppy: convert t...
624
  static DECLARE_DELAYED_WORK(fd_timeout, floppy_shutdown);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
625
  static const char *timeout_message;
275176bc2   Joe Perches   drivers/block/flo...
626
  static void is_alive(const char *func, const char *message)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
627
628
  {
  	/* this routine checks whether the floppy driver is "alive" */
c529730a9   Joe Perches   drivers/block/flo...
629
  	if (test_bit(0, &fdc_busy) && command_status < 2 &&
070ad7e79   Jiri Kosina   floppy: convert t...
630
  	    !delayed_work_pending(&fd_timeout)) {
275176bc2   Joe Perches   drivers/block/flo...
631
632
  		DPRINT("%s: timeout handler died.  %s
  ", func, message);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
633
634
  	}
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
635

48c8cee61   Joe Perches   drivers/block/flo...
636
  static void (*do_floppy)(void) = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
637

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
638
  #define OLOGSIZE 20
48c8cee61   Joe Perches   drivers/block/flo...
639
  static void (*lasthandler)(void);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
640
641
642
643
644
645
646
647
648
649
650
651
  static unsigned long interruptjiffies;
  static unsigned long resultjiffies;
  static int resultsize;
  static unsigned long lastredo;
  
  static struct output_log {
  	unsigned char data;
  	unsigned char status;
  	unsigned long jiffies;
  } output_log[OLOGSIZE];
  
  static int output_log_pos;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
652
653
654
  
  #define current_reqD -1
  #define MAXTIMEOUT -2
73507e6cd   Joe Perches   drivers/block/flo...
655
  static void __reschedule_timeout(int drive, const char *message)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
656
  {
070ad7e79   Jiri Kosina   floppy: convert t...
657
  	unsigned long delay;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
658
659
  	if (drive == current_reqD)
  		drive = current_drive;
070ad7e79   Jiri Kosina   floppy: convert t...
660

4acb3e2f9   Eric Sesterhenn / Snakebyte   Off by one in flo...
661
  	if (drive < 0 || drive >= N_DRIVE) {
070ad7e79   Jiri Kosina   floppy: convert t...
662
  		delay = 20UL * HZ;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
663
664
  		drive = 0;
  	} else
070ad7e79   Jiri Kosina   floppy: convert t...
665
  		delay = UDP->timeout;
e7c2f9674   Tejun Heo   workqueue: use mo...
666
  	mod_delayed_work(floppy_wq, &fd_timeout, delay);
a81ee5447   Joe Perches   drivers/block/flo...
667
  	if (UDP->flags & FD_DEBUG)
73507e6cd   Joe Perches   drivers/block/flo...
668
669
  		DPRINT("reschedule timeout %s
  ", message);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
670
671
  	timeout_message = message;
  }
73507e6cd   Joe Perches   drivers/block/flo...
672
  static void reschedule_timeout(int drive, const char *message)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
673
674
675
676
  {
  	unsigned long flags;
  
  	spin_lock_irqsave(&floppy_lock, flags);
73507e6cd   Joe Perches   drivers/block/flo...
677
  	__reschedule_timeout(drive, message);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
678
679
  	spin_unlock_irqrestore(&floppy_lock, flags);
  }
48c8cee61   Joe Perches   drivers/block/flo...
680
681
  #define INFBOUND(a, b) (a) = max_t(int, a, b)
  #define SUPBOUND(a, b) (a) = min_t(int, a, b)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
  
  /*
   * Bottom half floppy driver.
   * ==========================
   *
   * This part of the file contains the code talking directly to the hardware,
   * and also the main service loop (seek-configure-spinup-command)
   */
  
  /*
   * disk change.
   * This routine is responsible for maintaining the FD_DISK_CHANGE flag,
   * and the last_checked date.
   *
   * last_checked is the date of the last check which showed 'no disk change'
   * FD_DISK_CHANGE is set under two conditions:
   * 1. The floppy has been changed after some i/o to that floppy already
   *    took place.
   * 2. No floppy disk is in the drive. This is done in order to ensure that
   *    requests are quickly flushed in case there is no disk in the drive. It
   *    follows that FD_DISK_CHANGE can only be cleared if there is a disk in
   *    the drive.
   *
   * For 1., maxblock is observed. Maxblock is 0 if no i/o has taken place yet.
   * For 2., FD_DISK_NEWCHANGE is watched. FD_DISK_NEWCHANGE is cleared on
   *  each seek. If a disk is present, the disk change line should also be
   *  cleared on each seek. Thus, if FD_DISK_NEWCHANGE is clear, but the disk
   *  change line is set, this means either that no disk is in the drive, or
   *  that it has been removed since the last seek.
   *
   * This means that we really have a third possibility too:
   *  The floppy has been changed after the last seek.
   */
  
  static int disk_change(int drive)
  {
  	int fdc = FDC(drive);
06f748c47   Jesper Juhl   floppy: do a very...
719

50297cbf0   Marcelo Feitoza Parisi   [PATCH] drivers/b...
720
  	if (time_before(jiffies, UDRS->select_date + UDP->select_delay))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
721
722
723
724
725
726
727
728
729
730
  		DPRINT("WARNING disk change called early
  ");
  	if (!(FDCS->dor & (0x10 << UNIT(drive))) ||
  	    (FDCS->dor & 3) != UNIT(drive) || fdc != FDC(drive)) {
  		DPRINT("probing disk change on unselected drive
  ");
  		DPRINT("drive=%d fdc=%d dor=%x
  ", drive, FDC(drive),
  		       (unsigned int)FDCS->dor);
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
731

87f530d8f   Joe Perches   drivers/block/flo...
732
733
734
735
736
737
738
739
740
  	debug_dcl(UDP->flags,
  		  "checking disk change line for drive %d
  ", drive);
  	debug_dcl(UDP->flags, "jiffies=%lu
  ", jiffies);
  	debug_dcl(UDP->flags, "disk change line=%x
  ", fd_inb(FD_DIR) & 0x80);
  	debug_dcl(UDP->flags, "flags=%lx
  ", UDRS->flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
741
  	if (UDP->flags & FD_BROKEN_DCL)
e02985361   Joe Perches   drivers/block/flo...
742
  		return test_bit(FD_DISK_CHANGED_BIT, &UDRS->flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
743
  	if ((fd_inb(FD_DIR) ^ UDP->flags) & 0x80) {
e02985361   Joe Perches   drivers/block/flo...
744
745
746
747
748
  		set_bit(FD_VERIFY_BIT, &UDRS->flags);
  					/* verify write protection */
  
  		if (UDRS->maxblock)	/* mark it changed */
  			set_bit(FD_DISK_CHANGED_BIT, &UDRS->flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
749
750
751
752
753
  
  		/* invalidate its geometry */
  		if (UDRS->keep_data >= 0) {
  			if ((UDP->flags & FTD_MSG) &&
  			    current_type[drive] != NULL)
891eda80a   Joe Perches   drivers/block/flo...
754
755
  				DPRINT("Disk type is undefined after disk change
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
756
757
758
  			current_type[drive] = NULL;
  			floppy_sizes[TOMINOR(drive)] = MAX_DISK_SIZE << 1;
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
759
760
761
  		return 1;
  	} else {
  		UDRS->last_checked = jiffies;
e02985361   Joe Perches   drivers/block/flo...
762
  		clear_bit(FD_DISK_NEWCHANGE_BIT, &UDRS->flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
763
764
765
766
767
768
769
770
  	}
  	return 0;
  }
  
  static inline int is_selected(int dor, int unit)
  {
  	return ((dor & (0x10 << unit)) && (dor & 3) == unit);
  }
57584c5a3   Joe Perches   drivers/block/flo...
771
772
773
774
775
  static bool is_ready_state(int status)
  {
  	int state = status & (STATUS_READY | STATUS_DIR | STATUS_DMA);
  	return state == STATUS_READY;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
776
777
  static int set_dor(int fdc, char mask, char data)
  {
fdc1ca8ab   Jesper Juhl   floppy: remove re...
778
779
780
781
  	unsigned char unit;
  	unsigned char drive;
  	unsigned char newdor;
  	unsigned char olddor;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
782
783
784
785
786
787
788
789
790
791
  
  	if (FDCS->address == -1)
  		return -1;
  
  	olddor = FDCS->dor;
  	newdor = (olddor & mask) | data;
  	if (newdor != olddor) {
  		unit = olddor & 0x3;
  		if (is_selected(olddor, unit) && !is_selected(newdor, unit)) {
  			drive = REVDRIVE(fdc, unit);
87f530d8f   Joe Perches   drivers/block/flo...
792
793
794
  			debug_dcl(UDP->flags,
  				  "calling disk change from set_dor
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
795
796
797
798
799
800
801
802
803
804
805
  			disk_change(drive);
  		}
  		FDCS->dor = newdor;
  		fd_outb(newdor, FD_DOR);
  
  		unit = newdor & 0x3;
  		if (!is_selected(olddor, unit) && is_selected(newdor, unit)) {
  			drive = REVDRIVE(fdc, unit);
  			UDRS->select_date = jiffies;
  		}
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
806
807
808
809
810
811
812
813
814
815
816
  	return olddor;
  }
  
  static void twaddle(void)
  {
  	if (DP->select_delay)
  		return;
  	fd_outb(FDCS->dor & ~(0x10 << UNIT(current_drive)), FD_DOR);
  	fd_outb(FDCS->dor, FD_DOR);
  	DRS->select_date = jiffies;
  }
57584c5a3   Joe Perches   drivers/block/flo...
817
818
819
820
  /*
   * Reset all driver information about the current fdc.
   * This is needed after a reset, and after a raw command.
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
  static void reset_fdc_info(int mode)
  {
  	int drive;
  
  	FDCS->spec1 = FDCS->spec2 = -1;
  	FDCS->need_configure = 1;
  	FDCS->perp_mode = 1;
  	FDCS->rawcmd = 0;
  	for (drive = 0; drive < N_DRIVE; drive++)
  		if (FDC(drive) == fdc && (mode || UDRS->track != NEED_1_RECAL))
  			UDRS->track = NEED_2_RECAL;
  }
  
  /* selects the fdc and drive, and enables the fdc's input/dma. */
  static void set_fdc(int drive)
  {
  	if (drive >= 0 && drive < N_DRIVE) {
  		fdc = FDC(drive);
  		current_drive = drive;
  	}
  	if (fdc != 1 && fdc != 0) {
b46df356d   Joe Perches   drivers/block/flo...
842
843
  		pr_info("bad fdc value
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
844
845
846
847
848
849
850
851
852
853
854
855
856
  		return;
  	}
  	set_dor(fdc, ~0, 8);
  #if N_FDC > 1
  	set_dor(1 - fdc, ~8, 0);
  #endif
  	if (FDCS->rawcmd == 2)
  		reset_fdc_info(1);
  	if (fd_inb(FD_STATUS) != STATUS_READY)
  		FDCS->reset = 1;
  }
  
  /* locks the driver */
a0c80efe5   Jiri Kosina   floppy: fix lock_...
857
  static int lock_fdc(int drive)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
858
  {
b862f26fe   Stephen Hemminger   floppy: use wait_...
859
860
861
  	if (WARN(atomic_read(&usage_count) == 0,
  		 "Trying to lock fdc while usage count=0
  "))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
862
  		return -1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
863

b862f26fe   Stephen Hemminger   floppy: use wait_...
864
865
  	if (wait_event_interruptible(fdc_wait, !test_and_set_bit(0, &fdc_busy)))
  		return -EINTR;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
866

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
867
  	command_status = FD_COMMAND_NONE;
070ad7e79   Jiri Kosina   floppy: convert t...
868
  	reschedule_timeout(drive, "lock fdc");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
869
870
871
  	set_fdc(drive);
  	return 0;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
872
  /* unlocks the driver */
be7a12bb1   Stephen Hemminger   floppy: remove un...
873
  static void unlock_fdc(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
874
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
875
876
877
  	if (!test_bit(0, &fdc_busy))
  		DPRINT("FDC access conflict!
  ");
070ad7e79   Jiri Kosina   floppy: convert t...
878
  	raw_cmd = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
879
  	command_status = FD_COMMAND_NONE;
136b5721d   Tejun Heo   workqueue: deprec...
880
  	cancel_delayed_work(&fd_timeout);
070ad7e79   Jiri Kosina   floppy: convert t...
881
  	do_floppy = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
882
883
  	cont = NULL;
  	clear_bit(0, &fdc_busy);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
  	wake_up(&fdc_wait);
  }
  
  /* switches the motor off after a given timeout */
  static void motor_off_callback(unsigned long nr)
  {
  	unsigned char mask = ~(0x10 << UNIT(nr));
  
  	set_dor(FDC(nr), mask, 0);
  }
  
  /* schedules motor off */
  static void floppy_off(unsigned int drive)
  {
  	unsigned long volatile delta;
fdc1ca8ab   Jesper Juhl   floppy: remove re...
899
  	int fdc = FDC(drive);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
  
  	if (!(FDCS->dor & (0x10 << UNIT(drive))))
  		return;
  
  	del_timer(motor_off_timer + drive);
  
  	/* make spindle stop in a position which minimizes spinup time
  	 * next time */
  	if (UDP->rps) {
  		delta = jiffies - UDRS->first_read_date + HZ -
  		    UDP->spindown_offset;
  		delta = ((delta * UDP->rps) % HZ) / UDP->rps;
  		motor_off_timer[drive].expires =
  		    jiffies + UDP->spindown - delta;
  	}
  	add_timer(motor_off_timer + drive);
  }
  
  /*
   * cycle through all N_DRIVE floppy drives, for disk change testing.
   * stopping at current drive. This is done before any long operation, to
   * be sure to have up to date disk change information.
   */
  static void scandrives(void)
  {
06f748c47   Jesper Juhl   floppy: do a very...
925
926
927
  	int i;
  	int drive;
  	int saved_drive;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
  
  	if (DP->select_delay)
  		return;
  
  	saved_drive = current_drive;
  	for (i = 0; i < N_DRIVE; i++) {
  		drive = (saved_drive + i + 1) % N_DRIVE;
  		if (UDRS->fd_ref == 0 || UDP->select_delay != 0)
  			continue;	/* skip closed drives */
  		set_fdc(drive);
  		if (!(set_dor(fdc, ~3, UNIT(drive) | (0x10 << UNIT(drive))) &
  		      (0x10 << UNIT(drive))))
  			/* switch the motor off again, if it was off to
  			 * begin with */
  			set_dor(fdc, ~(0x10 << UNIT(drive)), 0);
  	}
  	set_fdc(saved_drive);
  }
  
  static void empty(void)
  {
  }
75ddb38f0   Tejun Heo   floppy: don't use...
950
951
952
953
954
955
956
957
  static void (*floppy_work_fn)(void);
  
  static void floppy_work_workfn(struct work_struct *work)
  {
  	floppy_work_fn();
  }
  
  static DECLARE_WORK(floppy_work, floppy_work_workfn);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
958

48c8cee61   Joe Perches   drivers/block/flo...
959
  static void schedule_bh(void (*handler)(void))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
960
  {
070ad7e79   Jiri Kosina   floppy: convert t...
961
  	WARN_ON(work_pending(&floppy_work));
75ddb38f0   Tejun Heo   floppy: don't use...
962
  	floppy_work_fn = handler;
070ad7e79   Jiri Kosina   floppy: convert t...
963
  	queue_work(floppy_wq, &floppy_work);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
964
  }
75ddb38f0   Tejun Heo   floppy: don't use...
965
966
967
968
969
970
971
972
  static void (*fd_timer_fn)(void) = NULL;
  
  static void fd_timer_workfn(struct work_struct *work)
  {
  	fd_timer_fn();
  }
  
  static DECLARE_DELAYED_WORK(fd_timer, fd_timer_workfn);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
973
974
975
  
  static void cancel_activity(void)
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
976
  	do_floppy = NULL;
070ad7e79   Jiri Kosina   floppy: convert t...
977
978
  	cancel_delayed_work_sync(&fd_timer);
  	cancel_work_sync(&floppy_work);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
979
980
981
982
  }
  
  /* this function makes sure that the disk stays in the drive during the
   * transfer */
75ddb38f0   Tejun Heo   floppy: don't use...
983
  static void fd_watchdog(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
984
  {
87f530d8f   Joe Perches   drivers/block/flo...
985
986
  	debug_dcl(DP->flags, "calling disk change from watchdog
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
987
988
989
990
991
992
993
994
  
  	if (disk_change(current_drive)) {
  		DPRINT("disk removed during i/o
  ");
  		cancel_activity();
  		cont->done(0);
  		reset_fdc();
  	} else {
070ad7e79   Jiri Kosina   floppy: convert t...
995
  		cancel_delayed_work(&fd_timer);
75ddb38f0   Tejun Heo   floppy: don't use...
996
  		fd_timer_fn = fd_watchdog;
070ad7e79   Jiri Kosina   floppy: convert t...
997
  		queue_delayed_work(floppy_wq, &fd_timer, HZ / 10);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
998
999
1000
1001
1002
  	}
  }
  
  static void main_command_interrupt(void)
  {
070ad7e79   Jiri Kosina   floppy: convert t...
1003
  	cancel_delayed_work(&fd_timer);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1004
1005
1006
1007
  	cont->interrupt();
  }
  
  /* waits for a delay (spinup or select) to pass */
75ddb38f0   Tejun Heo   floppy: don't use...
1008
1009
  static int fd_wait_for_completion(unsigned long expires,
  				  void (*function)(void))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1010
1011
1012
1013
1014
1015
1016
  {
  	if (FDCS->reset) {
  		reset_fdc();	/* do the reset during sleep to win time
  				 * if we don't need to sleep, it's a good
  				 * occasion anyways */
  		return 1;
  	}
070ad7e79   Jiri Kosina   floppy: convert t...
1017
1018
  	if (time_before(jiffies, expires)) {
  		cancel_delayed_work(&fd_timer);
75ddb38f0   Tejun Heo   floppy: don't use...
1019
  		fd_timer_fn = function;
070ad7e79   Jiri Kosina   floppy: convert t...
1020
  		queue_delayed_work(floppy_wq, &fd_timer, expires - jiffies);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1021
1022
1023
1024
  		return 1;
  	}
  	return 0;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1025
1026
1027
  static void setup_DMA(void)
  {
  	unsigned long f;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1028
1029
  	if (raw_cmd->length == 0) {
  		int i;
b46df356d   Joe Perches   drivers/block/flo...
1030
  		pr_info("zero dma transfer size:");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1031
  		for (i = 0; i < raw_cmd->cmd_count; i++)
b46df356d   Joe Perches   drivers/block/flo...
1032
1033
1034
  			pr_cont("%x,", raw_cmd->cmd[i]);
  		pr_cont("
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1035
1036
1037
1038
1039
  		cont->done(0);
  		FDCS->reset = 1;
  		return;
  	}
  	if (((unsigned long)raw_cmd->kernel_data) % 512) {
b46df356d   Joe Perches   drivers/block/flo...
1040
1041
  		pr_info("non aligned address: %p
  ", raw_cmd->kernel_data);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1042
1043
1044
1045
  		cont->done(0);
  		FDCS->reset = 1;
  		return;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
  	f = claim_dma_lock();
  	fd_disable_dma();
  #ifdef fd_dma_setup
  	if (fd_dma_setup(raw_cmd->kernel_data, raw_cmd->length,
  			 (raw_cmd->flags & FD_RAW_READ) ?
  			 DMA_MODE_READ : DMA_MODE_WRITE, FDCS->address) < 0) {
  		release_dma_lock(f);
  		cont->done(0);
  		FDCS->reset = 1;
  		return;
  	}
  	release_dma_lock(f);
  #else
  	fd_clear_dma_ff();
  	fd_cacheflush(raw_cmd->kernel_data, raw_cmd->length);
  	fd_set_dma_mode((raw_cmd->flags & FD_RAW_READ) ?
  			DMA_MODE_READ : DMA_MODE_WRITE);
  	fd_set_dma_addr(raw_cmd->kernel_data);
  	fd_set_dma_count(raw_cmd->length);
  	virtual_dma_port = FDCS->address;
  	fd_enable_dma();
  	release_dma_lock(f);
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1069
1070
1071
1072
1073
1074
1075
  }
  
  static void show_floppy(void);
  
  /* waits until the fdc becomes ready */
  static int wait_til_ready(void)
  {
06f748c47   Jesper Juhl   floppy: do a very...
1076
1077
  	int status;
  	int counter;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1078
1079
1080
1081
1082
1083
1084
  	if (FDCS->reset)
  		return -1;
  	for (counter = 0; counter < 10000; counter++) {
  		status = fd_inb(FD_STATUS);
  		if (status & STATUS_READY)
  			return status;
  	}
29f1c7848   Joe Perches   drivers/block/flo...
1085
  	if (initialized) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
  		DPRINT("Getstatus times out (%x) on fdc %d
  ", status, fdc);
  		show_floppy();
  	}
  	FDCS->reset = 1;
  	return -1;
  }
  
  /* sends a command byte to the fdc */
  static int output_byte(char byte)
  {
d7b2b2ecd   Joe Perches   drivers/block/flo...
1097
  	int status = wait_til_ready();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1098

d7b2b2ecd   Joe Perches   drivers/block/flo...
1099
  	if (status < 0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1100
  		return -1;
57584c5a3   Joe Perches   drivers/block/flo...
1101
1102
  
  	if (is_ready_state(status)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1103
  		fd_outb(byte, FD_DATA);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1104
1105
1106
1107
  		output_log[output_log_pos].data = byte;
  		output_log[output_log_pos].status = status;
  		output_log[output_log_pos].jiffies = jiffies;
  		output_log_pos = (output_log_pos + 1) % OLOGSIZE;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1108
1109
1110
  		return 0;
  	}
  	FDCS->reset = 1;
29f1c7848   Joe Perches   drivers/block/flo...
1111
  	if (initialized) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1112
1113
1114
1115
1116
1117
1118
  		DPRINT("Unable to send byte %x to FDC. Fdc=%x Status=%x
  ",
  		       byte, fdc, status);
  		show_floppy();
  	}
  	return -1;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1119
1120
1121
  /* gets the response from the fdc */
  static int result(void)
  {
06f748c47   Jesper Juhl   floppy: do a very...
1122
1123
  	int i;
  	int status = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1124
1125
  
  	for (i = 0; i < MAX_REPLIES; i++) {
d7b2b2ecd   Joe Perches   drivers/block/flo...
1126
1127
  		status = wait_til_ready();
  		if (status < 0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1128
1129
1130
  			break;
  		status &= STATUS_DIR | STATUS_READY | STATUS_BUSY | STATUS_DMA;
  		if ((status & ~STATUS_BUSY) == STATUS_READY) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1131
1132
  			resultjiffies = jiffies;
  			resultsize = i;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1133
1134
1135
1136
1137
1138
1139
  			return i;
  		}
  		if (status == (STATUS_DIR | STATUS_READY | STATUS_BUSY))
  			reply_buffer[i] = fd_inb(FD_DATA);
  		else
  			break;
  	}
29f1c7848   Joe Perches   drivers/block/flo...
1140
1141
1142
1143
  	if (initialized) {
  		DPRINT("get result error. Fdc=%d Last status=%x Read bytes=%d
  ",
  		       fdc, status, i);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
  		show_floppy();
  	}
  	FDCS->reset = 1;
  	return -1;
  }
  
  #define MORE_OUTPUT -2
  /* does the fdc need more output? */
  static int need_more_output(void)
  {
d7b2b2ecd   Joe Perches   drivers/block/flo...
1154
  	int status = wait_til_ready();
06f748c47   Jesper Juhl   floppy: do a very...
1155

d7b2b2ecd   Joe Perches   drivers/block/flo...
1156
  	if (status < 0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1157
  		return -1;
57584c5a3   Joe Perches   drivers/block/flo...
1158
1159
  
  	if (is_ready_state(status))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1160
  		return MORE_OUTPUT;
57584c5a3   Joe Perches   drivers/block/flo...
1161

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1162
1163
1164
1165
1166
1167
  	return result();
  }
  
  /* Set perpendicular mode as required, based on data rate, if supported.
   * 82077 Now tested. 1Mbps data rate only possible with 82077-1.
   */
be7a12bb1   Stephen Hemminger   floppy: remove un...
1168
  static void perpendicular_mode(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
  {
  	unsigned char perp_mode;
  
  	if (raw_cmd->rate & 0x40) {
  		switch (raw_cmd->rate & 3) {
  		case 0:
  			perp_mode = 2;
  			break;
  		case 3:
  			perp_mode = 3;
  			break;
  		default:
  			DPRINT("Invalid data rate for perpendicular mode!
  ");
  			cont->done(0);
bb57f0c66   Joe Perches   drivers/block/flo...
1184
1185
1186
1187
1188
1189
  			FDCS->reset = 1;
  					/*
  					 * convenient way to return to
  					 * redo without too much hassle
  					 * (deep stack et al.)
  					 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
  			return;
  		}
  	} else
  		perp_mode = 0;
  
  	if (FDCS->perp_mode == perp_mode)
  		return;
  	if (FDCS->version >= FDC_82077_ORIG) {
  		output_byte(FD_PERPENDICULAR);
  		output_byte(perp_mode);
  		FDCS->perp_mode = perp_mode;
  	} else if (perp_mode) {
  		DPRINT("perpendicular mode not supported by this FDC.
  ");
  	}
  }				/* perpendicular_mode */
  
  static int fifo_depth = 0xa;
  static int no_fifo;
  
  static int fdc_configure(void)
  {
  	/* Turn on FIFO */
  	output_byte(FD_CONFIGURE);
  	if (need_more_output() != MORE_OUTPUT)
  		return 0;
  	output_byte(0);
  	output_byte(0x10 | (no_fifo & 0x20) | (fifo_depth & 0xf));
  	output_byte(0);		/* pre-compensation from track
  				   0 upwards */
  	return 1;
  }
  
  #define NOMINAL_DTR 500
  
  /* Issue a "SPECIFY" command to set the step rate time, head unload time,
   * head load time, and DMA disable flag to values needed by floppy.
   *
   * The value "dtr" is the data transfer rate in Kbps.  It is needed
   * to account for the data rate-based scaling done by the 82072 and 82077
   * FDC types.  This parameter is ignored for other types of FDCs (i.e.
   * 8272a).
   *
   * Note that changing the data transfer rate has a (probably deleterious)
   * effect on the parameters subject to scaling for 82072/82077 FDCs, so
   * fdc_specify is called again after each data transfer rate
   * change.
   *
   * srt: 1000 to 16000 in microseconds
   * hut: 16 to 240 milliseconds
   * hlt: 2 to 254 milliseconds
   *
   * These values are rounded up to the next highest available delay time.
   */
  static void fdc_specify(void)
  {
06f748c47   Jesper Juhl   floppy: do a very...
1246
1247
1248
1249
1250
  	unsigned char spec1;
  	unsigned char spec2;
  	unsigned long srt;
  	unsigned long hlt;
  	unsigned long hut;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1251
1252
1253
1254
1255
1256
1257
1258
  	unsigned long dtr = NOMINAL_DTR;
  	unsigned long scale_dtr = NOMINAL_DTR;
  	int hlt_max_code = 0x7f;
  	int hut_max_code = 0xf;
  
  	if (FDCS->need_configure && FDCS->version >= FDC_82072A) {
  		fdc_configure();
  		FDCS->need_configure = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
  	}
  
  	switch (raw_cmd->rate & 0x03) {
  	case 3:
  		dtr = 1000;
  		break;
  	case 1:
  		dtr = 300;
  		if (FDCS->version >= FDC_82078) {
  			/* chose the default rate table, not the one
  			 * where 1 = 2 Mbps */
  			output_byte(FD_DRIVESPEC);
  			if (need_more_output() == MORE_OUTPUT) {
  				output_byte(UNIT(current_drive));
  				output_byte(0xc0);
  			}
  		}
  		break;
  	case 2:
  		dtr = 250;
  		break;
  	}
  
  	if (FDCS->version >= FDC_82072) {
  		scale_dtr = dtr;
  		hlt_max_code = 0x00;	/* 0==256msec*dtr0/dtr (not linear!) */
  		hut_max_code = 0x0;	/* 0==256msec*dtr0/dtr (not linear!) */
  	}
  
  	/* Convert step rate from microseconds to milliseconds and 4 bits */
061837bc8   Julia Lawall   drivers/block: Us...
1289
  	srt = 16 - DIV_ROUND_UP(DP->srt * scale_dtr / 1000, NOMINAL_DTR);
a81ee5447   Joe Perches   drivers/block/flo...
1290
  	if (slow_floppy)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1291
  		srt = srt / 4;
a81ee5447   Joe Perches   drivers/block/flo...
1292

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1293
1294
  	SUPBOUND(srt, 0xf);
  	INFBOUND(srt, 0);
061837bc8   Julia Lawall   drivers/block: Us...
1295
  	hlt = DIV_ROUND_UP(DP->hlt * scale_dtr / 2, NOMINAL_DTR);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1296
1297
1298
1299
  	if (hlt < 0x01)
  		hlt = 0x01;
  	else if (hlt > 0x7f)
  		hlt = hlt_max_code;
061837bc8   Julia Lawall   drivers/block: Us...
1300
  	hut = DIV_ROUND_UP(DP->hut * scale_dtr / 16, NOMINAL_DTR);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
  	if (hut < 0x1)
  		hut = 0x1;
  	else if (hut > 0xf)
  		hut = hut_max_code;
  
  	spec1 = (srt << 4) | hut;
  	spec2 = (hlt << 1) | (use_virtual_dma & 1);
  
  	/* If these parameters did not change, just return with success */
  	if (FDCS->spec1 != spec1 || FDCS->spec2 != spec2) {
  		/* Go ahead and set spec1 and spec2 */
  		output_byte(FD_SPECIFY);
  		output_byte(FDCS->spec1 = spec1);
  		output_byte(FDCS->spec2 = spec2);
  	}
  }				/* fdc_specify */
  
  /* Set the FDC's data transfer rate on behalf of the specified drive.
   * NOTE: with 82072/82077 FDCs, changing the data rate requires a reissue
   * of the specify command (i.e. using the fdc_specify function).
   */
  static int fdc_dtr(void)
  {
  	/* If data rate not already set to desired value, set it. */
  	if ((raw_cmd->rate & 3) == FDCS->dtr)
  		return 0;
  
  	/* Set dtr */
  	fd_outb(raw_cmd->rate & 3, FD_DCR);
  
  	/* TODO: some FDC/drive combinations (C&T 82C711 with TEAC 1.2MB)
  	 * need a stabilization period of several milliseconds to be
  	 * enforced after data rate changes before R/W operations.
  	 * Pause 5 msec to avoid trouble. (Needs to be 2 jiffies)
  	 */
  	FDCS->dtr = raw_cmd->rate & 3;
75ddb38f0   Tejun Heo   floppy: don't use...
1337
  	return fd_wait_for_completion(jiffies + 2UL * HZ / 100, floppy_ready);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1338
1339
1340
1341
  }				/* fdc_dtr */
  
  static void tell_sector(void)
  {
b46df356d   Joe Perches   drivers/block/flo...
1342
1343
  	pr_cont(": track %d, head %d, sector %d, size %d",
  		R_TRACK, R_HEAD, R_SECTOR, R_SIZECODE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1344
  }				/* tell_sector */
b46df356d   Joe Perches   drivers/block/flo...
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
  static void print_errors(void)
  {
  	DPRINT("");
  	if (ST0 & ST0_ECE) {
  		pr_cont("Recalibrate failed!");
  	} else if (ST2 & ST2_CRC) {
  		pr_cont("data CRC error");
  		tell_sector();
  	} else if (ST1 & ST1_CRC) {
  		pr_cont("CRC error");
  		tell_sector();
  	} else if ((ST1 & (ST1_MAM | ST1_ND)) ||
  		   (ST2 & ST2_MAM)) {
  		if (!probing) {
  			pr_cont("sector not found");
  			tell_sector();
  		} else
  			pr_cont("probe failed...");
  	} else if (ST2 & ST2_WC) {	/* seek error */
  		pr_cont("wrong cylinder");
  	} else if (ST2 & ST2_BC) {	/* cylinder marked as bad */
  		pr_cont("bad cylinder");
  	} else {
  		pr_cont("unknown error. ST[0..2] are: 0x%x 0x%x 0x%x",
  			ST0, ST1, ST2);
  		tell_sector();
  	}
  	pr_cont("
  ");
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
  /*
   * OK, this error interpreting routine is called after a
   * DMA read/write has succeeded
   * or failed, so we check the results, and copy any buffers.
   * hhb: Added better error reporting.
   * ak: Made this into a separate routine.
   */
  static int interpret_errors(void)
  {
  	char bad;
  
  	if (inr != 7) {
891eda80a   Joe Perches   drivers/block/flo...
1387
1388
  		DPRINT("-- FDC reply error
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
  		FDCS->reset = 1;
  		return 1;
  	}
  
  	/* check IC to find cause of interrupt */
  	switch (ST0 & ST0_INTR) {
  	case 0x40:		/* error occurred during command execution */
  		if (ST1 & ST1_EOC)
  			return 0;	/* occurs with pseudo-DMA */
  		bad = 1;
  		if (ST1 & ST1_WP) {
  			DPRINT("Drive is write protected
  ");
e02985361   Joe Perches   drivers/block/flo...
1402
  			clear_bit(FD_DISK_WRITABLE_BIT, &DRS->flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1403
1404
1405
  			cont->done(0);
  			bad = 2;
  		} else if (ST1 & ST1_ND) {
e02985361   Joe Perches   drivers/block/flo...
1406
  			set_bit(FD_NEED_TWADDLE_BIT, &DRS->flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1407
1408
1409
1410
1411
1412
  		} else if (ST1 & ST1_OR) {
  			if (DP->flags & FTD_MSG)
  				DPRINT("Over/Underrun - retrying
  ");
  			bad = 0;
  		} else if (*errors >= DP->max_errors.reporting) {
b46df356d   Joe Perches   drivers/block/flo...
1413
  			print_errors();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
  		}
  		if (ST2 & ST2_WC || ST2 & ST2_BC)
  			/* wrong cylinder => recal */
  			DRS->track = NEED_2_RECAL;
  		return bad;
  	case 0x80:		/* invalid command given */
  		DPRINT("Invalid FDC command given!
  ");
  		cont->done(0);
  		return 2;
  	case 0xc0:
  		DPRINT("Abnormal termination caused by polling
  ");
  		cont->error();
  		return 2;
  	default:		/* (0) Normal command termination */
  		return 0;
  	}
  }
  
  /*
   * This routine is called when everything should be correctly set up
   * for the transfer (i.e. floppy motor is on, the correct floppy is
   * selected, and the head is sitting on the right track).
   */
  static void setup_rw_floppy(void)
  {
06f748c47   Jesper Juhl   floppy: do a very...
1441
1442
1443
1444
  	int i;
  	int r;
  	int flags;
  	int dflags;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1445
  	unsigned long ready_date;
75ddb38f0   Tejun Heo   floppy: don't use...
1446
  	void (*function)(void);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
  
  	flags = raw_cmd->flags;
  	if (flags & (FD_RAW_READ | FD_RAW_WRITE))
  		flags |= FD_RAW_INTR;
  
  	if ((flags & FD_RAW_SPIN) && !(flags & FD_RAW_NO_MOTOR)) {
  		ready_date = DRS->spinup_date + DP->spinup;
  		/* If spinup will take a long time, rerun scandrives
  		 * again just before spinup completion. Beware that
  		 * after scandrives, we must again wait for selection.
  		 */
50297cbf0   Marcelo Feitoza Parisi   [PATCH] drivers/b...
1458
  		if (time_after(ready_date, jiffies + DP->select_delay)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1459
  			ready_date -= DP->select_delay;
75ddb38f0   Tejun Heo   floppy: don't use...
1460
  			function = floppy_start;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1461
  		} else
75ddb38f0   Tejun Heo   floppy: don't use...
1462
  			function = setup_rw_floppy;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
  
  		/* wait until the floppy is spinning fast enough */
  		if (fd_wait_for_completion(ready_date, function))
  			return;
  	}
  	dflags = DRS->flags;
  
  	if ((flags & FD_RAW_READ) || (flags & FD_RAW_WRITE))
  		setup_DMA();
  
  	if (flags & FD_RAW_INTR)
  		do_floppy = main_command_interrupt;
  
  	r = 0;
  	for (i = 0; i < raw_cmd->cmd_count; i++)
  		r |= output_byte(raw_cmd->cmd[i]);
ded2863d0   Joe Perches   drivers/block/flo...
1479
  	debugt(__func__, "rw_command");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
  
  	if (r) {
  		cont->error();
  		reset_fdc();
  		return;
  	}
  
  	if (!(flags & FD_RAW_INTR)) {
  		inr = result();
  		cont->interrupt();
  	} else if (flags & FD_RAW_NEED_DISK)
75ddb38f0   Tejun Heo   floppy: don't use...
1491
  		fd_watchdog();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
  }
  
  static int blind_seek;
  
  /*
   * This is the routine called after every seek (or recalibrate) interrupt
   * from the floppy controller.
   */
  static void seek_interrupt(void)
  {
ded2863d0   Joe Perches   drivers/block/flo...
1502
  	debugt(__func__, "");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1503
1504
1505
1506
1507
1508
1509
1510
1511
  	if (inr != 2 || (ST0 & 0xF8) != 0x20) {
  		DPRINT("seek failed
  ");
  		DRS->track = NEED_2_RECAL;
  		cont->error();
  		cont->redo();
  		return;
  	}
  	if (DRS->track >= 0 && DRS->track != ST1 && !blind_seek) {
87f530d8f   Joe Perches   drivers/block/flo...
1512
1513
1514
1515
1516
  		debug_dcl(DP->flags,
  			  "clearing NEWCHANGE flag because of effective seek
  ");
  		debug_dcl(DP->flags, "jiffies=%lu
  ", jiffies);
e02985361   Joe Perches   drivers/block/flo...
1517
1518
  		clear_bit(FD_DISK_NEWCHANGE_BIT, &DRS->flags);
  					/* effective seek */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1519
1520
1521
1522
1523
1524
1525
1526
  		DRS->select_date = jiffies;
  	}
  	DRS->track = ST1;
  	floppy_ready();
  }
  
  static void check_wp(void)
  {
e02985361   Joe Perches   drivers/block/flo...
1527
1528
  	if (test_bit(FD_VERIFY_BIT, &DRS->flags)) {
  					/* check write protection */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1529
1530
1531
1532
1533
1534
  		output_byte(FD_GETSTATUS);
  		output_byte(UNIT(current_drive));
  		if (result() != 1) {
  			FDCS->reset = 1;
  			return;
  		}
e02985361   Joe Perches   drivers/block/flo...
1535
1536
  		clear_bit(FD_VERIFY_BIT, &DRS->flags);
  		clear_bit(FD_NEED_TWADDLE_BIT, &DRS->flags);
87f530d8f   Joe Perches   drivers/block/flo...
1537
1538
1539
1540
1541
  		debug_dcl(DP->flags,
  			  "checking whether disk is write protected
  ");
  		debug_dcl(DP->flags, "wp=%x
  ", ST3 & 0x40);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1542
  		if (!(ST3 & 0x40))
e02985361   Joe Perches   drivers/block/flo...
1543
  			set_bit(FD_DISK_WRITABLE_BIT, &DRS->flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1544
  		else
e02985361   Joe Perches   drivers/block/flo...
1545
  			clear_bit(FD_DISK_WRITABLE_BIT, &DRS->flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1546
1547
1548
1549
1550
1551
1552
1553
  	}
  }
  
  static void seek_floppy(void)
  {
  	int track;
  
  	blind_seek = 0;
ded2863d0   Joe Perches   drivers/block/flo...
1554
1555
  	debug_dcl(DP->flags, "calling disk change from %s
  ", __func__);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1556

e02985361   Joe Perches   drivers/block/flo...
1557
  	if (!test_bit(FD_DISK_NEWCHANGE_BIT, &DRS->flags) &&
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1558
1559
1560
1561
1562
  	    disk_change(current_drive) && (raw_cmd->flags & FD_RAW_NEED_DISK)) {
  		/* the media changed flag should be cleared after the seek.
  		 * If it isn't, this means that there is really no disk in
  		 * the drive.
  		 */
e02985361   Joe Perches   drivers/block/flo...
1563
  		set_bit(FD_DISK_CHANGED_BIT, &DRS->flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1564
1565
1566
1567
1568
1569
1570
  		cont->done(0);
  		cont->redo();
  		return;
  	}
  	if (DRS->track <= NEED_1_RECAL) {
  		recalibrate_floppy();
  		return;
e02985361   Joe Perches   drivers/block/flo...
1571
  	} else if (test_bit(FD_DISK_NEWCHANGE_BIT, &DRS->flags) &&
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
  		   (raw_cmd->flags & FD_RAW_NEED_DISK) &&
  		   (DRS->track <= NO_TRACK || DRS->track == raw_cmd->track)) {
  		/* we seek to clear the media-changed condition. Does anybody
  		 * know a more elegant way, which works on all drives? */
  		if (raw_cmd->track)
  			track = raw_cmd->track - 1;
  		else {
  			if (DP->flags & FD_SILENT_DCL_CLEAR) {
  				set_dor(fdc, ~(0x10 << UNIT(current_drive)), 0);
  				blind_seek = 1;
  				raw_cmd->flags |= FD_RAW_NEED_SEEK;
  			}
  			track = 1;
  		}
  	} else {
  		check_wp();
  		if (raw_cmd->track != DRS->track &&
  		    (raw_cmd->flags & FD_RAW_NEED_SEEK))
  			track = raw_cmd->track;
  		else {
  			setup_rw_floppy();
  			return;
  		}
  	}
  
  	do_floppy = seek_interrupt;
  	output_byte(FD_SEEK);
  	output_byte(UNIT(current_drive));
2300f90e3   Joe Perches   drivers/block/flo...
1600
1601
1602
1603
  	if (output_byte(track) < 0) {
  		reset_fdc();
  		return;
  	}
ded2863d0   Joe Perches   drivers/block/flo...
1604
  	debugt(__func__, "");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1605
1606
1607
1608
  }
  
  static void recal_interrupt(void)
  {
ded2863d0   Joe Perches   drivers/block/flo...
1609
  	debugt(__func__, "");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1610
1611
1612
1613
1614
  	if (inr != 2)
  		FDCS->reset = 1;
  	else if (ST0 & ST0_ECE) {
  		switch (DRS->track) {
  		case NEED_1_RECAL:
ded2863d0   Joe Perches   drivers/block/flo...
1615
  			debugt(__func__, "need 1 recal");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1616
1617
1618
1619
1620
1621
1622
1623
  			/* after a second recalibrate, we still haven't
  			 * reached track 0. Probably no drive. Raise an
  			 * error, as failing immediately might upset
  			 * computers possessed by the Devil :-) */
  			cont->error();
  			cont->redo();
  			return;
  		case NEED_2_RECAL:
ded2863d0   Joe Perches   drivers/block/flo...
1624
  			debugt(__func__, "need 2 recal");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1625
1626
1627
1628
1629
1630
  			/* If we already did a recalibrate,
  			 * and we are not at track 0, this
  			 * means we have moved. (The only way
  			 * not to move at recalibration is to
  			 * be already at track 0.) Clear the
  			 * new change flag */
87f530d8f   Joe Perches   drivers/block/flo...
1631
1632
1633
  			debug_dcl(DP->flags,
  				  "clearing NEWCHANGE flag because of second recalibrate
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1634

e02985361   Joe Perches   drivers/block/flo...
1635
  			clear_bit(FD_DISK_NEWCHANGE_BIT, &DRS->flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1636
1637
1638
  			DRS->select_date = jiffies;
  			/* fall through */
  		default:
ded2863d0   Joe Perches   drivers/block/flo...
1639
  			debugt(__func__, "default");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
  			/* Recalibrate moves the head by at
  			 * most 80 steps. If after one
  			 * recalibrate we don't have reached
  			 * track 0, this might mean that we
  			 * started beyond track 80.  Try
  			 * again.  */
  			DRS->track = NEED_1_RECAL;
  			break;
  		}
  	} else
  		DRS->track = ST1;
  	floppy_ready();
  }
  
  static void print_result(char *message, int inr)
  {
  	int i;
  
  	DPRINT("%s ", message);
  	if (inr >= 0)
  		for (i = 0; i < inr; i++)
b46df356d   Joe Perches   drivers/block/flo...
1661
1662
1663
  			pr_cont("repl[%d]=%x ", i, reply_buffer[i]);
  	pr_cont("
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1664
1665
1666
  }
  
  /* interrupt handler. Note that this can be called externally on the Sparc */
7d12e780e   David Howells   IRQ: Maintain reg...
1667
  irqreturn_t floppy_interrupt(int irq, void *dev_id)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1668
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1669
1670
  	int do_print;
  	unsigned long f;
06f748c47   Jesper Juhl   floppy: do a very...
1671
  	void (*handler)(void) = do_floppy;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1672
1673
1674
1675
1676
1677
1678
  
  	lasthandler = handler;
  	interruptjiffies = jiffies;
  
  	f = claim_dma_lock();
  	fd_disable_dma();
  	release_dma_lock(f);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1679
1680
1681
  	do_floppy = NULL;
  	if (fdc >= N_FDC || FDCS->address == -1) {
  		/* we don't even know which FDC is the culprit */
b46df356d   Joe Perches   drivers/block/flo...
1682
1683
1684
1685
  		pr_info("DOR0=%x
  ", fdc_state[0].dor);
  		pr_info("floppy interrupt on bizarre fdc %d
  ", fdc);
1ebddd85a   Joe Perches   drivers/block/flo...
1686
1687
  		pr_info("handler=%pf
  ", handler);
275176bc2   Joe Perches   drivers/block/flo...
1688
  		is_alive(__func__, "bizarre fdc");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
  		return IRQ_NONE;
  	}
  
  	FDCS->reset = 0;
  	/* We have to clear the reset flag here, because apparently on boxes
  	 * with level triggered interrupts (PS/2, Sparc, ...), it is needed to
  	 * emit SENSEI's to clear the interrupt line. And FDCS->reset blocks the
  	 * emission of the SENSEI's.
  	 * It is OK to emit floppy commands because we are in an interrupt
  	 * handler here, and thus we have to fear no interference of other
  	 * activity.
  	 */
29f1c7848   Joe Perches   drivers/block/flo...
1701
  	do_print = !handler && print_unex && initialized;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
  
  	inr = result();
  	if (do_print)
  		print_result("unexpected interrupt", inr);
  	if (inr == 0) {
  		int max_sensei = 4;
  		do {
  			output_byte(FD_SENSEI);
  			inr = result();
  			if (do_print)
  				print_result("sensei", inr);
  			max_sensei--;
c529730a9   Joe Perches   drivers/block/flo...
1714
1715
  		} while ((ST0 & 0x83) != UNIT(current_drive) &&
  			 inr == 2 && max_sensei);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1716
1717
1718
1719
1720
1721
  	}
  	if (!handler) {
  		FDCS->reset = 1;
  		return IRQ_NONE;
  	}
  	schedule_bh(handler);
275176bc2   Joe Perches   drivers/block/flo...
1722
  	is_alive(__func__, "normal interrupt end");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1723
1724
1725
1726
1727
1728
1729
  
  	/* FIXME! Was it really for us? */
  	return IRQ_HANDLED;
  }
  
  static void recalibrate_floppy(void)
  {
ded2863d0   Joe Perches   drivers/block/flo...
1730
  	debugt(__func__, "");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1731
1732
  	do_floppy = recal_interrupt;
  	output_byte(FD_RECALIBRATE);
15b2630c5   Joe Perches   drivers/block/flo...
1733
  	if (output_byte(UNIT(current_drive)) < 0)
2300f90e3   Joe Perches   drivers/block/flo...
1734
  		reset_fdc();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1735
1736
1737
1738
1739
1740
1741
  }
  
  /*
   * Must do 4 FD_SENSEIs after reset because of ``drive polling''.
   */
  static void reset_interrupt(void)
  {
ded2863d0   Joe Perches   drivers/block/flo...
1742
  	debugt(__func__, "");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1743
1744
  	result();		/* get the status ready for set_fdc */
  	if (FDCS->reset) {
1ebddd85a   Joe Perches   drivers/block/flo...
1745
1746
  		pr_info("reset set in interrupt, calling %pf
  ", cont->error);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
  		cont->error();	/* a reset just after a reset. BAD! */
  	}
  	cont->redo();
  }
  
  /*
   * reset is done by pulling bit 2 of DOR low for a while (old FDCs),
   * or by setting the self clearing bit 7 of STATUS (newer FDCs)
   */
  static void reset_fdc(void)
  {
  	unsigned long flags;
  
  	do_floppy = reset_interrupt;
  	FDCS->reset = 0;
  	reset_fdc_info(0);
  
  	/* Pseudo-DMA may intercept 'reset finished' interrupt.  */
  	/* Irrelevant for systems with true DMA (i386).          */
  
  	flags = claim_dma_lock();
  	fd_disable_dma();
  	release_dma_lock(flags);
  
  	if (FDCS->version >= FDC_82072A)
  		fd_outb(0x80 | (FDCS->dtr & 3), FD_STATUS);
  	else {
  		fd_outb(FDCS->dor & ~0x04, FD_DOR);
  		udelay(FD_RESET_DELAY);
  		fd_outb(FDCS->dor, FD_DOR);
  	}
  }
  
  static void show_floppy(void)
  {
  	int i;
b46df356d   Joe Perches   drivers/block/flo...
1783
1784
1785
1786
1787
1788
  	pr_info("
  ");
  	pr_info("floppy driver state
  ");
  	pr_info("-------------------
  ");
1ebddd85a   Joe Perches   drivers/block/flo...
1789
1790
  	pr_info("now=%lu last interrupt=%lu diff=%lu last called handler=%pf
  ",
b46df356d   Joe Perches   drivers/block/flo...
1791
1792
  		jiffies, interruptjiffies, jiffies - interruptjiffies,
  		lasthandler);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1793

b46df356d   Joe Perches   drivers/block/flo...
1794
1795
1796
1797
  	pr_info("timeout_message=%s
  ", timeout_message);
  	pr_info("last output bytes:
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1798
  	for (i = 0; i < OLOGSIZE; i++)
b46df356d   Joe Perches   drivers/block/flo...
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
  		pr_info("%2x %2x %lu
  ",
  			output_log[(i + output_log_pos) % OLOGSIZE].data,
  			output_log[(i + output_log_pos) % OLOGSIZE].status,
  			output_log[(i + output_log_pos) % OLOGSIZE].jiffies);
  	pr_info("last result at %lu
  ", resultjiffies);
  	pr_info("last redo_fd_request at %lu
  ", lastredo);
  	print_hex_dump(KERN_INFO, "", DUMP_PREFIX_NONE, 16, 1,
  		       reply_buffer, resultsize, true);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1810

b46df356d   Joe Perches   drivers/block/flo...
1811
1812
1813
1814
  	pr_info("status=%x
  ", fd_inb(FD_STATUS));
  	pr_info("fdc_busy=%lu
  ", fdc_busy);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1815
  	if (do_floppy)
1ebddd85a   Joe Perches   drivers/block/flo...
1816
1817
  		pr_info("do_floppy=%pf
  ", do_floppy);
365970a1e   David Howells   WorkStruct: Merge...
1818
  	if (work_pending(&floppy_work))
1ebddd85a   Joe Perches   drivers/block/flo...
1819
1820
  		pr_info("floppy_work.func=%pf
  ", floppy_work.func);
070ad7e79   Jiri Kosina   floppy: convert t...
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
  	if (delayed_work_pending(&fd_timer))
  		pr_info("delayed work.function=%p expires=%ld
  ",
  		       fd_timer.work.func,
  		       fd_timer.timer.expires - jiffies);
  	if (delayed_work_pending(&fd_timeout))
  		pr_info("timer_function=%p expires=%ld
  ",
  		       fd_timeout.work.func,
  		       fd_timeout.timer.expires - jiffies);
b46df356d   Joe Perches   drivers/block/flo...
1831
1832
1833
1834
1835
1836
1837
1838
  	pr_info("cont=%p
  ", cont);
  	pr_info("current_req=%p
  ", current_req);
  	pr_info("command_status=%d
  ", command_status);
  	pr_info("
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1839
  }
070ad7e79   Jiri Kosina   floppy: convert t...
1840
  static void floppy_shutdown(struct work_struct *arg)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1841
1842
  {
  	unsigned long flags;
29f1c7848   Joe Perches   drivers/block/flo...
1843
  	if (initialized)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1844
1845
  		show_floppy();
  	cancel_activity();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1846
1847
1848
1849
1850
  	flags = claim_dma_lock();
  	fd_disable_dma();
  	release_dma_lock(flags);
  
  	/* avoid dma going to a random drive after shutdown */
29f1c7848   Joe Perches   drivers/block/flo...
1851
  	if (initialized)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1852
1853
1854
1855
1856
1857
1858
  		DPRINT("floppy timeout called
  ");
  	FDCS->reset = 1;
  	if (cont) {
  		cont->done(0);
  		cont->redo();	/* this will recall reset when needed */
  	} else {
b46df356d   Joe Perches   drivers/block/flo...
1859
1860
  		pr_info("no cont in shutdown!
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1861
1862
  		process_fd_request();
  	}
275176bc2   Joe Perches   drivers/block/flo...
1863
  	is_alive(__func__, "");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1864
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1865
  /* start motor, check media-changed condition and write protection */
06f748c47   Jesper Juhl   floppy: do a very...
1866
  static int start_motor(void (*function)(void))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1867
  {
06f748c47   Jesper Juhl   floppy: do a very...
1868
1869
  	int mask;
  	int data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
  
  	mask = 0xfc;
  	data = UNIT(current_drive);
  	if (!(raw_cmd->flags & FD_RAW_NO_MOTOR)) {
  		if (!(FDCS->dor & (0x10 << UNIT(current_drive)))) {
  			set_debugt();
  			/* no read since this drive is running */
  			DRS->first_read_date = 0;
  			/* note motor start time if motor is not yet running */
  			DRS->spinup_date = jiffies;
  			data |= (0x10 << UNIT(current_drive));
  		}
  	} else if (FDCS->dor & (0x10 << UNIT(current_drive)))
  		mask &= ~(0x10 << UNIT(current_drive));
  
  	/* starts motor and selects floppy */
  	del_timer(motor_off_timer + current_drive);
  	set_dor(fdc, mask, data);
  
  	/* wait_for_completion also schedules reset if needed. */
d7b2b2ecd   Joe Perches   drivers/block/flo...
1890
  	return fd_wait_for_completion(DRS->select_date + DP->select_delay,
75ddb38f0   Tejun Heo   floppy: don't use...
1891
  				      function);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1892
1893
1894
1895
  }
  
  static void floppy_ready(void)
  {
045f98363   Joe Perches   drivers/block/flo...
1896
1897
1898
1899
  	if (FDCS->reset) {
  		reset_fdc();
  		return;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1900
1901
1902
1903
  	if (start_motor(floppy_ready))
  		return;
  	if (fdc_dtr())
  		return;
87f530d8f   Joe Perches   drivers/block/flo...
1904
1905
  	debug_dcl(DP->flags, "calling disk change from floppy_ready
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1906
1907
  	if (!(raw_cmd->flags & FD_RAW_NO_MOTOR) &&
  	    disk_change(current_drive) && !DP->select_delay)
bb57f0c66   Joe Perches   drivers/block/flo...
1908
1909
  		twaddle();	/* this clears the dcl on certain
  				 * drive/controller combinations */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
  
  #ifdef fd_chose_dma_mode
  	if ((raw_cmd->flags & FD_RAW_READ) || (raw_cmd->flags & FD_RAW_WRITE)) {
  		unsigned long flags = claim_dma_lock();
  		fd_chose_dma_mode(raw_cmd->kernel_data, raw_cmd->length);
  		release_dma_lock(flags);
  	}
  #endif
  
  	if (raw_cmd->flags & (FD_RAW_NEED_SEEK | FD_RAW_NEED_DISK)) {
  		perpendicular_mode();
  		fdc_specify();	/* must be done here because of hut, hlt ... */
  		seek_floppy();
  	} else {
  		if ((raw_cmd->flags & FD_RAW_READ) ||
  		    (raw_cmd->flags & FD_RAW_WRITE))
  			fdc_specify();
  		setup_rw_floppy();
  	}
  }
  
  static void floppy_start(void)
  {
73507e6cd   Joe Perches   drivers/block/flo...
1933
  	reschedule_timeout(current_reqD, "floppy start");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1934
1935
  
  	scandrives();
87f530d8f   Joe Perches   drivers/block/flo...
1936
1937
  	debug_dcl(DP->flags, "setting NEWCHANGE in floppy_start
  ");
e02985361   Joe Perches   drivers/block/flo...
1938
  	set_bit(FD_DISK_NEWCHANGE_BIT, &DRS->flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
  	floppy_ready();
  }
  
  /*
   * ========================================================================
   * here ends the bottom half. Exported routines are:
   * floppy_start, floppy_off, floppy_ready, lock_fdc, unlock_fdc, set_fdc,
   * start_motor, reset_fdc, reset_fdc_info, interpret_errors.
   * Initialization also uses output_byte, result, set_dor, floppy_interrupt
   * and set_dor.
   * ========================================================================
   */
  /*
   * General purpose continuations.
   * ==============================
   */
  
  static void do_wakeup(void)
  {
73507e6cd   Joe Perches   drivers/block/flo...
1958
  	reschedule_timeout(MAXTIMEOUT, "do wakeup");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1959
1960
1961
1962
  	cont = NULL;
  	command_status += 2;
  	wake_up(&command_done);
  }
3b06c21e8   Stephen Hemminger   floppy: make cont...
1963
  static const struct cont_t wakeup_cont = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1964
1965
1966
  	.interrupt	= empty,
  	.redo		= do_wakeup,
  	.error		= empty,
06f748c47   Jesper Juhl   floppy: do a very...
1967
  	.done		= (done_f)empty
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1968
  };
3b06c21e8   Stephen Hemminger   floppy: make cont...
1969
  static const struct cont_t intr_cont = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1970
1971
1972
  	.interrupt	= empty,
  	.redo		= process_fd_request,
  	.error		= empty,
06f748c47   Jesper Juhl   floppy: do a very...
1973
  	.done		= (done_f)empty
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1974
  };
74f63f469   Joe Perches   drivers/block/flo...
1975
  static int wait_til_done(void (*handler)(void), bool interruptible)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1976
1977
1978
1979
  {
  	int ret;
  
  	schedule_bh(handler);
b862f26fe   Stephen Hemminger   floppy: use wait_...
1980
1981
1982
1983
  	if (interruptible)
  		wait_event_interruptible(command_done, command_status >= 2);
  	else
  		wait_event(command_done, command_status >= 2);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
  
  	if (command_status < 2) {
  		cancel_activity();
  		cont = &intr_cont;
  		reset_fdc();
  		return -EINTR;
  	}
  
  	if (FDCS->reset)
  		command_status = FD_COMMAND_ERROR;
  	if (command_status == FD_COMMAND_OKAY)
  		ret = 0;
  	else
  		ret = -EIO;
  	command_status = FD_COMMAND_NONE;
  	return ret;
  }
  
  static void generic_done(int result)
  {
  	command_status = result;
  	cont = &wakeup_cont;
  }
  
  static void generic_success(void)
  {
  	cont->done(1);
  }
  
  static void generic_failure(void)
  {
  	cont->done(0);
  }
  
  static void success_and_wakeup(void)
  {
  	generic_success();
  	cont->redo();
  }
  
  /*
   * formatting and rw support.
   * ==========================
   */
  
  static int next_valid_format(void)
  {
  	int probed_format;
  
  	probed_format = DRS->probed_format;
  	while (1) {
  		if (probed_format >= 8 || !DP->autodetect[probed_format]) {
  			DRS->probed_format = 0;
  			return 1;
  		}
  		if (floppy_type[DP->autodetect[probed_format]].sect) {
  			DRS->probed_format = probed_format;
  			return 0;
  		}
  		probed_format++;
  	}
  }
  
  static void bad_flp_intr(void)
  {
  	int err_count;
  
  	if (probing) {
  		DRS->probed_format++;
  		if (!next_valid_format())
  			return;
  	}
  	err_count = ++(*errors);
  	INFBOUND(DRWE->badness, err_count);
  	if (err_count > DP->max_errors.abort)
  		cont->done(0);
  	if (err_count > DP->max_errors.reset)
  		FDCS->reset = 1;
  	else if (err_count > DP->max_errors.recal)
  		DRS->track = NEED_2_RECAL;
  }
  
  static void set_floppy(int drive)
  {
  	int type = ITYPE(UDRS->fd_device);
06f748c47   Jesper Juhl   floppy: do a very...
2069

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
  	if (type)
  		_floppy = floppy_type + type;
  	else
  		_floppy = current_type[drive];
  }
  
  /*
   * formatting support.
   * ===================
   */
  static void format_interrupt(void)
  {
  	switch (interpret_errors()) {
  	case 1:
  		cont->error();
  	case 2:
  		break;
  	case 0:
  		cont->done(1);
  	}
  	cont->redo();
  }
48c8cee61   Joe Perches   drivers/block/flo...
2092
  #define FM_MODE(x, y) ((y) & ~(((x)->rate & 0x80) >> 1))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2093
  #define CT(x) ((x) | 0xc0)
48c8cee61   Joe Perches   drivers/block/flo...
2094

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2095
2096
  static void setup_format_params(int track)
  {
06f748c47   Jesper Juhl   floppy: do a very...
2097
2098
2099
2100
2101
  	int n;
  	int il;
  	int count;
  	int head_shift;
  	int track_shift;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2102
2103
2104
  	struct fparm {
  		unsigned char track, head, sect, size;
  	} *here = (struct fparm *)floppy_track_buffer;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2105
2106
2107
  
  	raw_cmd = &default_raw_cmd;
  	raw_cmd->track = track;
48c8cee61   Joe Perches   drivers/block/flo...
2108
2109
  	raw_cmd->flags = (FD_RAW_WRITE | FD_RAW_INTR | FD_RAW_SPIN |
  			  FD_RAW_NEED_DISK | FD_RAW_NEED_SEEK);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
  	raw_cmd->rate = _floppy->rate & 0x43;
  	raw_cmd->cmd_count = NR_F;
  	COMMAND = FM_MODE(_floppy, FD_FORMAT);
  	DR_SELECT = UNIT(current_drive) + PH_HEAD(_floppy, format_req.head);
  	F_SIZECODE = FD_SIZECODE(_floppy);
  	F_SECT_PER_TRACK = _floppy->sect << 2 >> F_SIZECODE;
  	F_GAP = _floppy->fmt_gap;
  	F_FILL = FD_FILL_BYTE;
  
  	raw_cmd->kernel_data = floppy_track_buffer;
  	raw_cmd->length = 4 * F_SECT_PER_TRACK;
  
  	/* allow for about 30ms for data transport per track */
  	head_shift = (F_SECT_PER_TRACK + 5) / 6;
  
  	/* a ``cylinder'' is two tracks plus a little stepping time */
  	track_shift = 2 * head_shift + 3;
  
  	/* position of logical sector 1 on this track */
  	n = (track_shift * format_req.track + head_shift * format_req.head)
  	    % F_SECT_PER_TRACK;
  
  	/* determine interleave */
  	il = 1;
  	if (_floppy->fmt_gap < 0x22)
  		il++;
  
  	/* initialize field */
  	for (count = 0; count < F_SECT_PER_TRACK; ++count) {
  		here[count].track = format_req.track;
  		here[count].head = format_req.head;
  		here[count].sect = 0;
  		here[count].size = F_SIZECODE;
  	}
  	/* place logical sectors */
  	for (count = 1; count <= F_SECT_PER_TRACK; ++count) {
  		here[n].sect = count;
  		n = (n + il) % F_SECT_PER_TRACK;
  		if (here[n].sect) {	/* sector busy, find next free sector */
  			++n;
  			if (n >= F_SECT_PER_TRACK) {
  				n -= F_SECT_PER_TRACK;
  				while (here[n].sect)
  					++n;
  			}
  		}
  	}
9e49184c8   Keith Wansbrough   floppy: support a...
2157
  	if (_floppy->stretch & FD_SECTBASEMASK) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2158
  		for (count = 0; count < F_SECT_PER_TRACK; count++)
9e49184c8   Keith Wansbrough   floppy: support a...
2159
  			here[count].sect += FD_SECTBASE(_floppy) - 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2160
2161
2162
2163
2164
2165
2166
2167
  	}
  }
  
  static void redo_format(void)
  {
  	buffer_track = -1;
  	setup_format_params(format_req.track << STRETCH(_floppy));
  	floppy_start();
ded2863d0   Joe Perches   drivers/block/flo...
2168
  	debugt(__func__, "queue format request");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2169
  }
3b06c21e8   Stephen Hemminger   floppy: make cont...
2170
  static const struct cont_t format_cont = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2171
2172
2173
2174
2175
2176
2177
2178
2179
  	.interrupt	= format_interrupt,
  	.redo		= redo_format,
  	.error		= bad_flp_intr,
  	.done		= generic_done
  };
  
  static int do_format(int drive, struct format_descr *tmp_format_req)
  {
  	int ret;
a0c80efe5   Jiri Kosina   floppy: fix lock_...
2180
  	if (lock_fdc(drive))
52a0d61f6   Joe Perches   drivers/block/flo...
2181
  		return -EINTR;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
  	set_floppy(drive);
  	if (!_floppy ||
  	    _floppy->track > DP->tracks ||
  	    tmp_format_req->track >= _floppy->track ||
  	    tmp_format_req->head >= _floppy->head ||
  	    (_floppy->sect << 2) % (1 << FD_SIZECODE(_floppy)) ||
  	    !_floppy->fmt_gap) {
  		process_fd_request();
  		return -EINVAL;
  	}
  	format_req = *tmp_format_req;
  	format_errors = 0;
  	cont = &format_cont;
  	errors = &format_errors;
74f63f469   Joe Perches   drivers/block/flo...
2196
  	ret = wait_til_done(redo_format, true);
55eee80c6   Joe Perches   drivers/block/flo...
2197
2198
  	if (ret == -EINTR)
  		return -EINTR;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2199
2200
2201
2202
2203
2204
2205
2206
  	process_fd_request();
  	return ret;
  }
  
  /*
   * Buffer read/write and support
   * =============================
   */
1c5093ba0   Kiyoshi Ueda   blk_end_request: ...
2207
  static void floppy_end_request(struct request *req, int error)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2208
2209
  {
  	unsigned int nr_sectors = current_count_sectors;
1c5093ba0   Kiyoshi Ueda   blk_end_request: ...
2210
  	unsigned int drive = (unsigned long)req->rq_disk->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2211
2212
  
  	/* current_count_sectors can be zero if transfer failed */
1c5093ba0   Kiyoshi Ueda   blk_end_request: ...
2213
  	if (error)
83096ebf1   Tejun Heo   block: convert to...
2214
  		nr_sectors = blk_rq_cur_sectors(req);
1c5093ba0   Kiyoshi Ueda   blk_end_request: ...
2215
  	if (__blk_end_request(req, error, nr_sectors << 9))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2216
  		return;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2217
2218
  
  	/* We're done with the request */
1c5093ba0   Kiyoshi Ueda   blk_end_request: ...
2219
  	floppy_off(drive);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2220
2221
2222
2223
2224
2225
2226
  	current_req = NULL;
  }
  
  /* new request_done. Can handle physical sectors which are smaller than a
   * logical buffer */
  static void request_done(int uptodate)
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2227
  	struct request *req = current_req;
488211844   Jens Axboe   floppy: switch to...
2228
  	struct request_queue *q;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2229
2230
  	unsigned long flags;
  	int block;
73507e6cd   Joe Perches   drivers/block/flo...
2231
  	char msg[sizeof("request done ") + sizeof(int) * 3];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2232
2233
  
  	probing = 0;
73507e6cd   Joe Perches   drivers/block/flo...
2234
2235
  	snprintf(msg, sizeof(msg), "request done %d", uptodate);
  	reschedule_timeout(MAXTIMEOUT, msg);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2236
2237
  
  	if (!req) {
b46df356d   Joe Perches   drivers/block/flo...
2238
2239
  		pr_info("floppy.c: no request in request_done
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2240
2241
  		return;
  	}
488211844   Jens Axboe   floppy: switch to...
2242
  	q = req->q;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2243
2244
2245
  	if (uptodate) {
  		/* maintain values for invalidation on geometry
  		 * change */
83096ebf1   Tejun Heo   block: convert to...
2246
  		block = current_count_sectors + blk_rq_pos(req);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2247
2248
2249
2250
2251
2252
  		INFBOUND(DRS->maxblock, block);
  		if (block > _floppy->sect)
  			DRS->maxtrack = 1;
  
  		/* unlock chained buffers */
  		spin_lock_irqsave(q->queue_lock, flags);
1c5093ba0   Kiyoshi Ueda   blk_end_request: ...
2253
  		floppy_end_request(req, 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2254
2255
2256
2257
2258
2259
  		spin_unlock_irqrestore(q->queue_lock, flags);
  	} else {
  		if (rq_data_dir(req) == WRITE) {
  			/* record write error information */
  			DRWE->write_errors++;
  			if (DRWE->write_errors == 1) {
83096ebf1   Tejun Heo   block: convert to...
2260
  				DRWE->first_error_sector = blk_rq_pos(req);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2261
2262
  				DRWE->first_error_generation = DRS->generation;
  			}
83096ebf1   Tejun Heo   block: convert to...
2263
  			DRWE->last_error_sector = blk_rq_pos(req);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2264
2265
2266
  			DRWE->last_error_generation = DRS->generation;
  		}
  		spin_lock_irqsave(q->queue_lock, flags);
1c5093ba0   Kiyoshi Ueda   blk_end_request: ...
2267
  		floppy_end_request(req, -EIO);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2268
2269
2270
2271
2272
2273
2274
  		spin_unlock_irqrestore(q->queue_lock, flags);
  	}
  }
  
  /* Interrupt handler evaluating the result of the r/w operation */
  static void rw_interrupt(void)
  {
06f748c47   Jesper Juhl   floppy: do a very...
2275
2276
2277
2278
  	int eoc;
  	int ssize;
  	int heads;
  	int nr_sectors;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
  
  	if (R_HEAD >= 2) {
  		/* some Toshiba floppy controllers occasionnally seem to
  		 * return bogus interrupts after read/write operations, which
  		 * can be recognized by a bad head number (>= 2) */
  		return;
  	}
  
  	if (!DRS->first_read_date)
  		DRS->first_read_date = jiffies;
  
  	nr_sectors = 0;
712e1de43   Joe Perches   drivers/block/flo...
2291
  	ssize = DIV_ROUND_UP(1 << SIZECODE, 4);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
  
  	if (ST1 & ST1_EOC)
  		eoc = 1;
  	else
  		eoc = 0;
  
  	if (COMMAND & 0x80)
  		heads = 2;
  	else
  		heads = 1;
  
  	nr_sectors = (((R_TRACK - TRACK) * heads +
  		       R_HEAD - HEAD) * SECT_PER_TRACK +
  		      R_SECTOR - SECTOR + eoc) << SIZECODE >> 2;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2306
  	if (nr_sectors / ssize >
061837bc8   Julia Lawall   drivers/block: Us...
2307
  	    DIV_ROUND_UP(in_sector_offset + current_count_sectors, ssize)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2308
2309
2310
  		DPRINT("long rw: %x instead of %lx
  ",
  		       nr_sectors, current_count_sectors);
b46df356d   Joe Perches   drivers/block/flo...
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
  		pr_info("rs=%d s=%d
  ", R_SECTOR, SECTOR);
  		pr_info("rh=%d h=%d
  ", R_HEAD, HEAD);
  		pr_info("rt=%d t=%d
  ", R_TRACK, TRACK);
  		pr_info("heads=%d eoc=%d
  ", heads, eoc);
  		pr_info("spt=%d st=%d ss=%d
  ",
  			SECT_PER_TRACK, fsector_t, ssize);
  		pr_info("in_sector_offset=%d
  ", in_sector_offset);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2324
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
  
  	nr_sectors -= in_sector_offset;
  	INFBOUND(nr_sectors, 0);
  	SUPBOUND(current_count_sectors, nr_sectors);
  
  	switch (interpret_errors()) {
  	case 2:
  		cont->redo();
  		return;
  	case 1:
  		if (!current_count_sectors) {
  			cont->error();
  			cont->redo();
  			return;
  		}
  		break;
  	case 0:
  		if (!current_count_sectors) {
  			cont->redo();
  			return;
  		}
  		current_type[current_drive] = _floppy;
  		floppy_sizes[TOMINOR(current_drive)] = _floppy->size;
  		break;
  	}
  
  	if (probing) {
  		if (DP->flags & FTD_MSG)
  			DPRINT("Auto-detected floppy type %s in fd%d
  ",
  			       _floppy->name, current_drive);
  		current_type[current_drive] = _floppy;
  		floppy_sizes[TOMINOR(current_drive)] = _floppy->size;
  		probing = 0;
  	}
  
  	if (CT(COMMAND) != FD_READ ||
b4f42e283   Jens Axboe   block: remove str...
2362
  	    raw_cmd->kernel_data == bio_data(current_req->bio)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
  		/* transfer directly from buffer */
  		cont->done(1);
  	} else if (CT(COMMAND) == FD_READ) {
  		buffer_track = raw_cmd->track;
  		buffer_drive = current_drive;
  		INFBOUND(buffer_max, nr_sectors + fsector_t);
  	}
  	cont->redo();
  }
  
  /* Compute maximal contiguous buffer size. */
  static int buffer_chain_size(void)
  {
7988613b0   Kent Overstreet   block: Convert bi...
2376
  	struct bio_vec bv;
5705f7021   NeilBrown   Introduce rq_for_...
2377
2378
  	int size;
  	struct req_iterator iter;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2379
2380
2381
2382
  	char *base;
  
  	base = bio_data(current_req->bio);
  	size = 0;
5705f7021   NeilBrown   Introduce rq_for_...
2383
  	rq_for_each_segment(bv, current_req, iter) {
7988613b0   Kent Overstreet   block: Convert bi...
2384
  		if (page_address(bv.bv_page) + bv.bv_offset != base + size)
5705f7021   NeilBrown   Introduce rq_for_...
2385
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2386

7988613b0   Kent Overstreet   block: Convert bi...
2387
  		size += bv.bv_len;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
  	}
  
  	return size >> 9;
  }
  
  /* Compute the maximal transfer size */
  static int transfer_size(int ssize, int max_sector, int max_size)
  {
  	SUPBOUND(max_sector, fsector_t + max_size);
  
  	/* alignment */
  	max_sector -= (max_sector % _floppy->sect) % ssize;
  
  	/* transfer size, beginning not aligned */
  	current_count_sectors = max_sector - fsector_t;
  
  	return max_sector;
  }
  
  /*
   * Move data from/to the track buffer to/from the buffer cache.
   */
  static void copy_buffer(int ssize, int max_sector, int max_sector_2)
  {
  	int remaining;		/* number of transferred 512-byte sectors */
7988613b0   Kent Overstreet   block: Convert bi...
2413
  	struct bio_vec bv;
06f748c47   Jesper Juhl   floppy: do a very...
2414
2415
  	char *buffer;
  	char *dma_buffer;
5705f7021   NeilBrown   Introduce rq_for_...
2416
2417
  	int size;
  	struct req_iterator iter;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2418
2419
2420
  
  	max_sector = transfer_size(ssize,
  				   min(max_sector, max_sector_2),
83096ebf1   Tejun Heo   block: convert to...
2421
  				   blk_rq_sectors(current_req));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2422
2423
  
  	if (current_count_sectors <= 0 && CT(COMMAND) == FD_WRITE &&
83096ebf1   Tejun Heo   block: convert to...
2424
  	    buffer_max > fsector_t + blk_rq_sectors(current_req))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2425
  		current_count_sectors = min_t(int, buffer_max - fsector_t,
83096ebf1   Tejun Heo   block: convert to...
2426
  					      blk_rq_sectors(current_req));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2427
2428
  
  	remaining = current_count_sectors << 9;
1011c1b9f   Tejun Heo   block: blk_rq_[cu...
2429
  	if (remaining > blk_rq_bytes(current_req) && CT(COMMAND) == FD_WRITE) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2430
2431
  		DPRINT("in copy buffer
  ");
b46df356d   Joe Perches   drivers/block/flo...
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
  		pr_info("current_count_sectors=%ld
  ", current_count_sectors);
  		pr_info("remaining=%d
  ", remaining >> 9);
  		pr_info("current_req->nr_sectors=%u
  ",
  			blk_rq_sectors(current_req));
  		pr_info("current_req->current_nr_sectors=%u
  ",
  			blk_rq_cur_sectors(current_req));
  		pr_info("max_sector=%d
  ", max_sector);
  		pr_info("ssize=%d
  ", ssize);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2446
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2447
2448
2449
2450
  
  	buffer_max = max(max_sector, buffer_max);
  
  	dma_buffer = floppy_track_buffer + ((fsector_t - buffer_min) << 9);
1011c1b9f   Tejun Heo   block: blk_rq_[cu...
2451
  	size = blk_rq_cur_bytes(current_req);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2452

5705f7021   NeilBrown   Introduce rq_for_...
2453
2454
2455
  	rq_for_each_segment(bv, current_req, iter) {
  		if (!remaining)
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2456

7988613b0   Kent Overstreet   block: Convert bi...
2457
  		size = bv.bv_len;
5705f7021   NeilBrown   Introduce rq_for_...
2458
  		SUPBOUND(size, remaining);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2459

7988613b0   Kent Overstreet   block: Convert bi...
2460
  		buffer = page_address(bv.bv_page) + bv.bv_offset;
5705f7021   NeilBrown   Introduce rq_for_...
2461
2462
2463
2464
2465
  		if (dma_buffer + size >
  		    floppy_track_buffer + (max_buffer_sectors << 10) ||
  		    dma_buffer < floppy_track_buffer) {
  			DPRINT("buffer overrun in copy buffer %d
  ",
b46df356d   Joe Perches   drivers/block/flo...
2466
2467
2468
2469
2470
2471
2472
  			       (int)((floppy_track_buffer - dma_buffer) >> 9));
  			pr_info("fsector_t=%d buffer_min=%d
  ",
  				fsector_t, buffer_min);
  			pr_info("current_count_sectors=%ld
  ",
  				current_count_sectors);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2473
  			if (CT(COMMAND) == FD_READ)
b46df356d   Joe Perches   drivers/block/flo...
2474
2475
  				pr_info("read
  ");
5705f7021   NeilBrown   Introduce rq_for_...
2476
  			if (CT(COMMAND) == FD_WRITE)
b46df356d   Joe Perches   drivers/block/flo...
2477
2478
  				pr_info("write
  ");
5705f7021   NeilBrown   Introduce rq_for_...
2479
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2480
  		}
5705f7021   NeilBrown   Introduce rq_for_...
2481
2482
2483
  		if (((unsigned long)buffer) % 512)
  			DPRINT("%p buffer not aligned
  ", buffer);
1a23d1333   Joe Perches   drivers/block/flo...
2484

5705f7021   NeilBrown   Introduce rq_for_...
2485
2486
2487
2488
2489
2490
2491
  		if (CT(COMMAND) == FD_READ)
  			memcpy(buffer, dma_buffer, size);
  		else
  			memcpy(dma_buffer, buffer, size);
  
  		remaining -= size;
  		dma_buffer += size;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2492
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2493
2494
2495
2496
2497
2498
  	if (remaining) {
  		if (remaining > 0)
  			max_sector -= remaining >> 9;
  		DPRINT("weirdness: remaining %d
  ", remaining >> 9);
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2499
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2500
2501
2502
2503
2504
2505
2506
2507
2508
  /* work around a bug in pseudo DMA
   * (on some FDCs) pseudo DMA does not stop when the CPU stops
   * sending data.  Hence we need a different way to signal the
   * transfer length:  We use SECT_PER_TRACK.  Unfortunately, this
   * does not work with MT, hence we can only transfer one head at
   * a time
   */
  static void virtualdmabug_workaround(void)
  {
06f748c47   Jesper Juhl   floppy: do a very...
2509
2510
  	int hard_sectors;
  	int end_sector;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2511
2512
2513
2514
2515
2516
  
  	if (CT(COMMAND) == FD_WRITE) {
  		COMMAND &= ~0x80;	/* switch off multiple track mode */
  
  		hard_sectors = raw_cmd->length >> (7 + SIZECODE);
  		end_sector = SECTOR + hard_sectors - 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2517
  		if (end_sector > SECT_PER_TRACK) {
b46df356d   Joe Perches   drivers/block/flo...
2518
2519
2520
  			pr_info("too many sectors %d > %d
  ",
  				end_sector, SECT_PER_TRACK);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2521
2522
  			return;
  		}
48c8cee61   Joe Perches   drivers/block/flo...
2523
2524
2525
  		SECT_PER_TRACK = end_sector;
  					/* make sure SECT_PER_TRACK
  					 * points to end of transfer */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
  	}
  }
  
  /*
   * Formulate a read/write request.
   * this routine decides where to load the data (directly to buffer, or to
   * tmp floppy area), how much data to load (the size of the buffer, the whole
   * track, or a single sector)
   * All floppy_track_buffer handling goes in here. If we ever add track buffer
   * allocation on the fly, it should be done here. No other part should need
   * modification.
   */
  
  static int make_raw_rw_request(void)
  {
  	int aligned_sector_t;
06f748c47   Jesper Juhl   floppy: do a very...
2542
2543
2544
2545
  	int max_sector;
  	int max_size;
  	int tracksize;
  	int ssize;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2546

01b6b67ed   Stephen Hemminger   floppy: use warni...
2547
2548
  	if (WARN(max_buffer_sectors == 0, "VFS: Block I/O scheduled on unopened device
  "))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2549
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2550
2551
2552
2553
  
  	set_fdc((long)current_req->rq_disk->private_data);
  
  	raw_cmd = &default_raw_cmd;
2fb2ca6f5   Fengguang Wu   floppy: remove du...
2554
  	raw_cmd->flags = FD_RAW_SPIN | FD_RAW_NEED_DISK | FD_RAW_NEED_SEEK;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2555
2556
2557
2558
2559
2560
2561
2562
  	raw_cmd->cmd_count = NR_RW;
  	if (rq_data_dir(current_req) == READ) {
  		raw_cmd->flags |= FD_RAW_READ;
  		COMMAND = FM_MODE(_floppy, FD_READ);
  	} else if (rq_data_dir(current_req) == WRITE) {
  		raw_cmd->flags |= FD_RAW_WRITE;
  		COMMAND = FM_MODE(_floppy, FD_WRITE);
  	} else {
275176bc2   Joe Perches   drivers/block/flo...
2563
2564
  		DPRINT("%s: unknown command
  ", __func__);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2565
2566
2567
2568
  		return 0;
  	}
  
  	max_sector = _floppy->sect * _floppy->head;
83096ebf1   Tejun Heo   block: convert to...
2569
2570
  	TRACK = (int)blk_rq_pos(current_req) / max_sector;
  	fsector_t = (int)blk_rq_pos(current_req) % max_sector;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2571
  	if (_floppy->track && TRACK >= _floppy->track) {
83096ebf1   Tejun Heo   block: convert to...
2572
  		if (blk_rq_cur_sectors(current_req) & 1) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2573
2574
2575
2576
2577
2578
  			current_count_sectors = 1;
  			return 1;
  		} else
  			return 0;
  	}
  	HEAD = fsector_t / _floppy->sect;
9e49184c8   Keith Wansbrough   floppy: support a...
2579
  	if (((_floppy->stretch & (FD_SWAPSIDES | FD_SECTBASEMASK)) ||
e02985361   Joe Perches   drivers/block/flo...
2580
2581
  	     test_bit(FD_NEED_TWADDLE_BIT, &DRS->flags)) &&
  	    fsector_t < _floppy->sect)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2582
2583
2584
2585
2586
2587
2588
2589
  		max_sector = _floppy->sect;
  
  	/* 2M disks have phantom sectors on the first track */
  	if ((_floppy->rate & FD_2M) && (!TRACK) && (!HEAD)) {
  		max_sector = 2 * _floppy->sect / 3;
  		if (fsector_t >= max_sector) {
  			current_count_sectors =
  			    min_t(int, _floppy->sect - fsector_t,
83096ebf1   Tejun Heo   block: convert to...
2590
  				  blk_rq_sectors(current_req));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
  			return 1;
  		}
  		SIZECODE = 2;
  	} else
  		SIZECODE = FD_SIZECODE(_floppy);
  	raw_cmd->rate = _floppy->rate & 0x43;
  	if ((_floppy->rate & FD_2M) && (TRACK || HEAD) && raw_cmd->rate == 2)
  		raw_cmd->rate = 1;
  
  	if (SIZECODE)
  		SIZECODE2 = 0xff;
  	else
  		SIZECODE2 = 0x80;
  	raw_cmd->track = TRACK << STRETCH(_floppy);
  	DR_SELECT = UNIT(current_drive) + PH_HEAD(_floppy, HEAD);
  	GAP = _floppy->gap;
712e1de43   Joe Perches   drivers/block/flo...
2607
  	ssize = DIV_ROUND_UP(1 << SIZECODE, 4);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2608
2609
  	SECT_PER_TRACK = _floppy->sect << 2 >> SIZECODE;
  	SECTOR = ((fsector_t % _floppy->sect) << 2 >> SIZECODE) +
9e49184c8   Keith Wansbrough   floppy: support a...
2610
  	    FD_SECTBASE(_floppy);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
  
  	/* tracksize describes the size which can be filled up with sectors
  	 * of size ssize.
  	 */
  	tracksize = _floppy->sect - _floppy->sect % ssize;
  	if (tracksize < _floppy->sect) {
  		SECT_PER_TRACK++;
  		if (tracksize <= fsector_t % _floppy->sect)
  			SECTOR--;
  
  		/* if we are beyond tracksize, fill up using smaller sectors */
  		while (tracksize <= fsector_t % _floppy->sect) {
  			while (tracksize + ssize > _floppy->sect) {
  				SIZECODE--;
  				ssize >>= 1;
  			}
  			SECTOR++;
  			SECT_PER_TRACK++;
  			tracksize += ssize;
  		}
  		max_sector = HEAD * _floppy->sect + tracksize;
  	} else if (!TRACK && !HEAD && !(_floppy->rate & FD_2M) && probing) {
  		max_sector = _floppy->sect;
  	} else if (!HEAD && CT(COMMAND) == FD_WRITE) {
  		/* for virtual DMA bug workaround */
  		max_sector = _floppy->sect;
  	}
  
  	in_sector_offset = (fsector_t % _floppy->sect) % ssize;
  	aligned_sector_t = fsector_t - in_sector_offset;
83096ebf1   Tejun Heo   block: convert to...
2641
  	max_size = blk_rq_sectors(current_req);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2642
2643
2644
2645
2646
2647
2648
2649
  	if ((raw_cmd->track == buffer_track) &&
  	    (current_drive == buffer_drive) &&
  	    (fsector_t >= buffer_min) && (fsector_t < buffer_max)) {
  		/* data already in track buffer */
  		if (CT(COMMAND) == FD_READ) {
  			copy_buffer(1, max_sector, buffer_max);
  			return 1;
  		}
83096ebf1   Tejun Heo   block: convert to...
2650
  	} else if (in_sector_offset || blk_rq_sectors(current_req) < ssize) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2651
  		if (CT(COMMAND) == FD_WRITE) {
d7b2b2ecd   Joe Perches   drivers/block/flo...
2652
2653
2654
2655
  			unsigned int sectors;
  
  			sectors = fsector_t + blk_rq_sectors(current_req);
  			if (sectors > ssize && sectors < ssize + ssize)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2656
2657
2658
2659
2660
2661
2662
  				max_size = ssize + ssize;
  			else
  				max_size = ssize;
  		}
  		raw_cmd->flags &= ~FD_RAW_WRITE;
  		raw_cmd->flags |= FD_RAW_READ;
  		COMMAND = FM_MODE(_floppy, FD_READ);
b4f42e283   Jens Axboe   block: remove str...
2663
  	} else if ((unsigned long)bio_data(current_req->bio) < MAX_DMA_ADDRESS) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
  		unsigned long dma_limit;
  		int direct, indirect;
  
  		indirect =
  		    transfer_size(ssize, max_sector,
  				  max_buffer_sectors * 2) - fsector_t;
  
  		/*
  		 * Do NOT use minimum() here---MAX_DMA_ADDRESS is 64 bits wide
  		 * on a 64 bit machine!
  		 */
  		max_size = buffer_chain_size();
d7b2b2ecd   Joe Perches   drivers/block/flo...
2676
  		dma_limit = (MAX_DMA_ADDRESS -
b4f42e283   Jens Axboe   block: remove str...
2677
  			     ((unsigned long)bio_data(current_req->bio))) >> 9;
a81ee5447   Joe Perches   drivers/block/flo...
2678
  		if ((unsigned long)max_size > dma_limit)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2679
  			max_size = dma_limit;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2680
  		/* 64 kb boundaries */
b4f42e283   Jens Axboe   block: remove str...
2681
  		if (CROSS_64KB(bio_data(current_req->bio), max_size << 9))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2682
  			max_size = (K_64 -
b4f42e283   Jens Axboe   block: remove str...
2683
  				    ((unsigned long)bio_data(current_req->bio)) %
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
  				    K_64) >> 9;
  		direct = transfer_size(ssize, max_sector, max_size) - fsector_t;
  		/*
  		 * We try to read tracks, but if we get too many errors, we
  		 * go back to reading just one sector at a time.
  		 *
  		 * This means we should be able to read a sector even if there
  		 * are other bad sectors on this track.
  		 */
  		if (!direct ||
  		    (indirect * 2 > direct * 3 &&
d7b2b2ecd   Joe Perches   drivers/block/flo...
2695
2696
2697
  		     *errors < DP->max_errors.read_track &&
  		     ((!probing ||
  		       (DP->read_track & (1 << DRS->probed_format)))))) {
83096ebf1   Tejun Heo   block: convert to...
2698
  			max_size = blk_rq_sectors(current_req);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2699
  		} else {
b4f42e283   Jens Axboe   block: remove str...
2700
  			raw_cmd->kernel_data = bio_data(current_req->bio);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2701
2702
  			raw_cmd->length = current_count_sectors << 9;
  			if (raw_cmd->length == 0) {
275176bc2   Joe Perches   drivers/block/flo...
2703
2704
  				DPRINT("%s: zero dma transfer attempted
  ", __func__);
d7b2b2ecd   Joe Perches   drivers/block/flo...
2705
2706
  				DPRINT("indirect=%d direct=%d fsector_t=%d
  ",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2707
2708
2709
  				       indirect, direct, fsector_t);
  				return 0;
  			}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
  			virtualdmabug_workaround();
  			return 2;
  		}
  	}
  
  	if (CT(COMMAND) == FD_READ)
  		max_size = max_sector;	/* unbounded */
  
  	/* claim buffer track if needed */
  	if (buffer_track != raw_cmd->track ||	/* bad track */
  	    buffer_drive != current_drive ||	/* bad drive */
  	    fsector_t > buffer_max ||
  	    fsector_t < buffer_min ||
  	    ((CT(COMMAND) == FD_READ ||
83096ebf1   Tejun Heo   block: convert to...
2724
  	      (!in_sector_offset && blk_rq_sectors(current_req) >= ssize)) &&
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2725
  	     max_sector > 2 * max_buffer_sectors + buffer_min &&
bb57f0c66   Joe Perches   drivers/block/flo...
2726
2727
  	     max_size + fsector_t > 2 * max_buffer_sectors + buffer_min)) {
  		/* not enough space */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2728
2729
2730
2731
2732
  		buffer_track = -1;
  		buffer_drive = current_drive;
  		buffer_max = buffer_min = aligned_sector_t;
  	}
  	raw_cmd->kernel_data = floppy_track_buffer +
bb57f0c66   Joe Perches   drivers/block/flo...
2733
  		((aligned_sector_t - buffer_min) << 9);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2734
2735
2736
2737
2738
2739
  
  	if (CT(COMMAND) == FD_WRITE) {
  		/* copy write buffer to track buffer.
  		 * if we get here, we know that the write
  		 * is either aligned or the data already in the buffer
  		 * (buffer will be overwritten) */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2740
2741
2742
  		if (in_sector_offset && buffer_track == -1)
  			DPRINT("internal error offset !=0 on write
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
  		buffer_track = raw_cmd->track;
  		buffer_drive = current_drive;
  		copy_buffer(ssize, max_sector,
  			    2 * max_buffer_sectors + buffer_min);
  	} else
  		transfer_size(ssize, max_sector,
  			      2 * max_buffer_sectors + buffer_min -
  			      aligned_sector_t);
  
  	/* round up current_count_sectors to get dma xfer size */
  	raw_cmd->length = in_sector_offset + current_count_sectors;
  	raw_cmd->length = ((raw_cmd->length - 1) | (ssize - 1)) + 1;
  	raw_cmd->length <<= 9;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2756
  	if ((raw_cmd->length < current_count_sectors << 9) ||
b4f42e283   Jens Axboe   block: remove str...
2757
  	    (raw_cmd->kernel_data != bio_data(current_req->bio) &&
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2758
2759
2760
2761
2762
2763
2764
2765
  	     CT(COMMAND) == FD_WRITE &&
  	     (aligned_sector_t + (raw_cmd->length >> 9) > buffer_max ||
  	      aligned_sector_t < buffer_min)) ||
  	    raw_cmd->length % (128 << SIZECODE) ||
  	    raw_cmd->length <= 0 || current_count_sectors <= 0) {
  		DPRINT("fractionary current count b=%lx s=%lx
  ",
  		       raw_cmd->length, current_count_sectors);
b4f42e283   Jens Axboe   block: remove str...
2766
  		if (raw_cmd->kernel_data != bio_data(current_req->bio))
b46df356d   Joe Perches   drivers/block/flo...
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
  			pr_info("addr=%d, length=%ld
  ",
  				(int)((raw_cmd->kernel_data -
  				       floppy_track_buffer) >> 9),
  				current_count_sectors);
  		pr_info("st=%d ast=%d mse=%d msi=%d
  ",
  			fsector_t, aligned_sector_t, max_sector, max_size);
  		pr_info("ssize=%x SIZECODE=%d
  ", ssize, SIZECODE);
  		pr_info("command=%x SECTOR=%d HEAD=%d, TRACK=%d
  ",
  			COMMAND, SECTOR, HEAD, TRACK);
  		pr_info("buffer drive=%d
  ", buffer_drive);
  		pr_info("buffer track=%d
  ", buffer_track);
  		pr_info("buffer_min=%d
  ", buffer_min);
  		pr_info("buffer_max=%d
  ", buffer_max);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2788
2789
  		return 0;
  	}
b4f42e283   Jens Axboe   block: remove str...
2790
  	if (raw_cmd->kernel_data != bio_data(current_req->bio)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2791
2792
2793
2794
2795
2796
2797
  		if (raw_cmd->kernel_data < floppy_track_buffer ||
  		    current_count_sectors < 0 ||
  		    raw_cmd->length < 0 ||
  		    raw_cmd->kernel_data + raw_cmd->length >
  		    floppy_track_buffer + (max_buffer_sectors << 10)) {
  			DPRINT("buffer overrun in schedule dma
  ");
b46df356d   Joe Perches   drivers/block/flo...
2798
2799
2800
2801
2802
2803
  			pr_info("fsector_t=%d buffer_min=%d current_count=%ld
  ",
  				fsector_t, buffer_min, raw_cmd->length >> 9);
  			pr_info("current_count_sectors=%ld
  ",
  				current_count_sectors);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2804
  			if (CT(COMMAND) == FD_READ)
b46df356d   Joe Perches   drivers/block/flo...
2805
2806
  				pr_info("read
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2807
  			if (CT(COMMAND) == FD_WRITE)
b46df356d   Joe Perches   drivers/block/flo...
2808
2809
  				pr_info("write
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2810
2811
  			return 0;
  		}
1011c1b9f   Tejun Heo   block: blk_rq_[cu...
2812
  	} else if (raw_cmd->length > blk_rq_bytes(current_req) ||
83096ebf1   Tejun Heo   block: convert to...
2813
  		   current_count_sectors > blk_rq_sectors(current_req)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2814
2815
2816
2817
2818
2819
  		DPRINT("buffer overrun in direct transfer
  ");
  		return 0;
  	} else if (raw_cmd->length < current_count_sectors << 9) {
  		DPRINT("more sectors than bytes
  ");
b46df356d   Joe Perches   drivers/block/flo...
2820
2821
2822
2823
  		pr_info("bytes=%ld
  ", raw_cmd->length >> 9);
  		pr_info("sectors=%ld
  ", current_count_sectors);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2824
2825
2826
2827
2828
2829
  	}
  	if (raw_cmd->length == 0) {
  		DPRINT("zero dma transfer attempted from make_raw_request
  ");
  		return 0;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2830
2831
2832
2833
  
  	virtualdmabug_workaround();
  	return 2;
  }
488211844   Jens Axboe   floppy: switch to...
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
  /*
   * Round-robin between our available drives, doing one request from each
   */
  static int set_next_request(void)
  {
  	struct request_queue *q;
  	int old_pos = fdc_queue;
  
  	do {
  		q = disks[fdc_queue]->queue;
  		if (++fdc_queue == N_DRIVE)
  			fdc_queue = 0;
  		if (q) {
  			current_req = blk_fetch_request(q);
459087959   Christoph Hellwig   floppy: switch fr...
2848
2849
  			if (current_req) {
  				current_req->error_count = 0;
488211844   Jens Axboe   floppy: switch to...
2850
  				break;
459087959   Christoph Hellwig   floppy: switch fr...
2851
  			}
488211844   Jens Axboe   floppy: switch to...
2852
2853
2854
2855
2856
  		}
  	} while (fdc_queue != old_pos);
  
  	return current_req != NULL;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2857
2858
  static void redo_fd_request(void)
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2859
2860
2861
2862
2863
2864
  	int drive;
  	int tmp;
  
  	lastredo = jiffies;
  	if (current_drive < N_DRIVE)
  		floppy_off(current_drive);
0da3132f9   Joe Perches   drivers/block/flo...
2865
2866
  do_request:
  	if (!current_req) {
488211844   Jens Axboe   floppy: switch to...
2867
2868
2869
2870
2871
  		int pending;
  
  		spin_lock_irq(&floppy_lock);
  		pending = set_next_request();
  		spin_unlock_irq(&floppy_lock);
488211844   Jens Axboe   floppy: switch to...
2872
  		if (!pending) {
0da3132f9   Joe Perches   drivers/block/flo...
2873
2874
  			do_floppy = NULL;
  			unlock_fdc();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2875
  			return;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2876
  		}
0da3132f9   Joe Perches   drivers/block/flo...
2877
2878
2879
  	}
  	drive = (long)current_req->rq_disk->private_data;
  	set_fdc(drive);
73507e6cd   Joe Perches   drivers/block/flo...
2880
  	reschedule_timeout(current_reqD, "redo fd request");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2881

0da3132f9   Joe Perches   drivers/block/flo...
2882
2883
2884
2885
  	set_floppy(drive);
  	raw_cmd = &default_raw_cmd;
  	raw_cmd->flags = 0;
  	if (start_motor(redo_fd_request))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2886
  		return;
0da3132f9   Joe Perches   drivers/block/flo...
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
  
  	disk_change(current_drive);
  	if (test_bit(current_drive, &fake_change) ||
  	    test_bit(FD_DISK_CHANGED_BIT, &DRS->flags)) {
  		DPRINT("disk absent or changed during operation
  ");
  		request_done(0);
  		goto do_request;
  	}
  	if (!_floppy) {	/* Autodetection */
  		if (!probing) {
  			DRS->probed_format = 0;
  			if (next_valid_format()) {
  				DPRINT("no autodetectable formats
  ");
  				_floppy = NULL;
  				request_done(0);
  				goto do_request;
  			}
  		}
  		probing = 1;
  		_floppy = floppy_type + DP->autodetect[DRS->probed_format];
  	} else
  		probing = 0;
459087959   Christoph Hellwig   floppy: switch fr...
2911
  	errors = &(current_req->error_count);
0da3132f9   Joe Perches   drivers/block/flo...
2912
2913
2914
2915
  	tmp = make_raw_rw_request();
  	if (tmp < 2) {
  		request_done(tmp);
  		goto do_request;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2916
  	}
0da3132f9   Joe Perches   drivers/block/flo...
2917
2918
2919
2920
  
  	if (test_bit(FD_NEED_TWADDLE_BIT, &DRS->flags))
  		twaddle();
  	schedule_bh(floppy_start);
ded2863d0   Joe Perches   drivers/block/flo...
2921
  	debugt(__func__, "queue fd request");
0da3132f9   Joe Perches   drivers/block/flo...
2922
  	return;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2923
  }
3b06c21e8   Stephen Hemminger   floppy: make cont...
2924
  static const struct cont_t rw_cont = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
  	.interrupt	= rw_interrupt,
  	.redo		= redo_fd_request,
  	.error		= bad_flp_intr,
  	.done		= request_done
  };
  
  static void process_fd_request(void)
  {
  	cont = &rw_cont;
  	schedule_bh(redo_fd_request);
  }
d7b2b2ecd   Joe Perches   drivers/block/flo...
2936
  static void do_fd_request(struct request_queue *q)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2937
  {
01b6b67ed   Stephen Hemminger   floppy: use warni...
2938
2939
2940
  	if (WARN(max_buffer_sectors == 0,
  		 "VFS: %s called on non-open device
  ", __func__))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2941
  		return;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2942

01b6b67ed   Stephen Hemminger   floppy: use warni...
2943
  	if (WARN(atomic_read(&usage_count) == 0,
aebf526b5   Christoph Hellwig   block: fold cmd_t...
2944
2945
2946
  		 "warning: usage count=0, current_req=%p sect=%ld flags=%llx
  ",
  		 current_req, (long)blk_rq_pos(current_req),
5953316db   Jens Axboe   block: make rq->c...
2947
  		 (unsigned long long) current_req->cmd_flags))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2948
  		return;
01b6b67ed   Stephen Hemminger   floppy: use warni...
2949

070ad7e79   Jiri Kosina   floppy: convert t...
2950
  	if (test_and_set_bit(0, &fdc_busy)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2951
2952
  		/* fdc busy, this new request will be treated when the
  		   current one is done */
275176bc2   Joe Perches   drivers/block/flo...
2953
  		is_alive(__func__, "old request running");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2954
2955
  		return;
  	}
070ad7e79   Jiri Kosina   floppy: convert t...
2956
2957
2958
  	command_status = FD_COMMAND_NONE;
  	__reschedule_timeout(MAXTIMEOUT, "fd_request");
  	set_fdc(0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2959
  	process_fd_request();
275176bc2   Joe Perches   drivers/block/flo...
2960
  	is_alive(__func__, "");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2961
  }
3b06c21e8   Stephen Hemminger   floppy: make cont...
2962
  static const struct cont_t poll_cont = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2963
2964
2965
2966
2967
  	.interrupt	= success_and_wakeup,
  	.redo		= floppy_ready,
  	.error		= generic_failure,
  	.done		= generic_done
  };
74f63f469   Joe Perches   drivers/block/flo...
2968
  static int poll_drive(bool interruptible, int flag)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2969
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2970
2971
2972
2973
2974
2975
  	/* no auto-sense, just clear dcl */
  	raw_cmd = &default_raw_cmd;
  	raw_cmd->flags = flag;
  	raw_cmd->track = 0;
  	raw_cmd->cmd_count = 0;
  	cont = &poll_cont;
87f530d8f   Joe Perches   drivers/block/flo...
2976
2977
  	debug_dcl(DP->flags, "setting NEWCHANGE in poll_drive
  ");
e02985361   Joe Perches   drivers/block/flo...
2978
  	set_bit(FD_DISK_NEWCHANGE_BIT, &DRS->flags);
55eee80c6   Joe Perches   drivers/block/flo...
2979
2980
  
  	return wait_til_done(floppy_ready, interruptible);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2981
2982
2983
2984
2985
2986
2987
2988
2989
  }
  
  /*
   * User triggered reset
   * ====================
   */
  
  static void reset_intr(void)
  {
b46df356d   Joe Perches   drivers/block/flo...
2990
2991
  	pr_info("weird, reset interrupt called
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2992
  }
3b06c21e8   Stephen Hemminger   floppy: make cont...
2993
  static const struct cont_t reset_cont = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2994
2995
2996
2997
2998
  	.interrupt	= reset_intr,
  	.redo		= success_and_wakeup,
  	.error		= generic_failure,
  	.done		= generic_done
  };
74f63f469   Joe Perches   drivers/block/flo...
2999
  static int user_reset_fdc(int drive, int arg, bool interruptible)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3000
3001
  {
  	int ret;
a0c80efe5   Jiri Kosina   floppy: fix lock_...
3002
  	if (lock_fdc(drive))
52a0d61f6   Joe Perches   drivers/block/flo...
3003
  		return -EINTR;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3004
3005
3006
3007
  	if (arg == FD_RESET_ALWAYS)
  		FDCS->reset = 1;
  	if (FDCS->reset) {
  		cont = &reset_cont;
55eee80c6   Joe Perches   drivers/block/flo...
3008
3009
3010
  		ret = wait_til_done(reset_fdc, interruptible);
  		if (ret == -EINTR)
  			return -EINTR;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3011
3012
  	}
  	process_fd_request();
52a0d61f6   Joe Perches   drivers/block/flo...
3013
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
  }
  
  /*
   * Misc Ioctl's and support
   * ========================
   */
  static inline int fd_copyout(void __user *param, const void *address,
  			     unsigned long size)
  {
  	return copy_to_user(param, address, size) ? -EFAULT : 0;
  }
48c8cee61   Joe Perches   drivers/block/flo...
3025
3026
  static inline int fd_copyin(void __user *param, void *address,
  			    unsigned long size)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3027
3028
3029
  {
  	return copy_from_user(address, param, size) ? -EFAULT : 0;
  }
be7a12bb1   Stephen Hemminger   floppy: remove un...
3030
  static const char *drive_name(int type, int drive)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
  {
  	struct floppy_struct *floppy;
  
  	if (type)
  		floppy = floppy_type + type;
  	else {
  		if (UDP->native_format)
  			floppy = floppy_type + UDP->native_format;
  		else
  			return "(null)";
  	}
  	if (floppy->name)
  		return floppy->name;
  	else
  		return "(null)";
  }
  
  /* raw commands */
  static void raw_cmd_done(int flag)
  {
  	int i;
  
  	if (!flag) {
  		raw_cmd->flags |= FD_RAW_FAILURE;
  		raw_cmd->flags |= FD_RAW_HARDFAILURE;
  	} else {
  		raw_cmd->reply_count = inr;
  		if (raw_cmd->reply_count > MAX_REPLIES)
  			raw_cmd->reply_count = 0;
  		for (i = 0; i < raw_cmd->reply_count; i++)
  			raw_cmd->reply[i] = reply_buffer[i];
  
  		if (raw_cmd->flags & (FD_RAW_READ | FD_RAW_WRITE)) {
  			unsigned long flags;
  			flags = claim_dma_lock();
  			raw_cmd->length = fd_get_dma_residue();
  			release_dma_lock(flags);
  		}
  
  		if ((raw_cmd->flags & FD_RAW_SOFTFAILURE) &&
  		    (!raw_cmd->reply_count || (raw_cmd->reply[0] & 0xc0)))
  			raw_cmd->flags |= FD_RAW_FAILURE;
  
  		if (disk_change(current_drive))
  			raw_cmd->flags |= FD_RAW_DISK_CHANGE;
  		else
  			raw_cmd->flags &= ~FD_RAW_DISK_CHANGE;
  		if (raw_cmd->flags & FD_RAW_NO_MOTOR_AFTER)
  			motor_off_callback(current_drive);
  
  		if (raw_cmd->next &&
  		    (!(raw_cmd->flags & FD_RAW_FAILURE) ||
  		     !(raw_cmd->flags & FD_RAW_STOP_IF_FAILURE)) &&
  		    ((raw_cmd->flags & FD_RAW_FAILURE) ||
  		     !(raw_cmd->flags & FD_RAW_STOP_IF_SUCCESS))) {
  			raw_cmd = raw_cmd->next;
  			return;
  		}
  	}
  	generic_done(flag);
  }
3b06c21e8   Stephen Hemminger   floppy: make cont...
3092
  static const struct cont_t raw_cmd_cont = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3093
3094
3095
3096
3097
  	.interrupt	= success_and_wakeup,
  	.redo		= floppy_start,
  	.error		= generic_failure,
  	.done		= raw_cmd_done
  };
be7a12bb1   Stephen Hemminger   floppy: remove un...
3098
  static int raw_cmd_copyout(int cmd, void __user *param,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3099
3100
3101
3102
3103
  				  struct floppy_raw_cmd *ptr)
  {
  	int ret;
  
  	while (ptr) {
2145e15e0   Matthew Daley   floppy: don't wri...
3104
3105
3106
3107
  		struct floppy_raw_cmd cmd = *ptr;
  		cmd.next = NULL;
  		cmd.kernel_data = NULL;
  		ret = copy_to_user(param, &cmd, sizeof(cmd));
86b12b48a   Joe Perches   drivers/block/flo...
3108
3109
  		if (ret)
  			return -EFAULT;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3110
3111
  		param += sizeof(struct floppy_raw_cmd);
  		if ((ptr->flags & FD_RAW_READ) && ptr->buffer_length) {
bb57f0c66   Joe Perches   drivers/block/flo...
3112
3113
3114
  			if (ptr->length >= 0 &&
  			    ptr->length <= ptr->buffer_length) {
  				long length = ptr->buffer_length - ptr->length;
4575b5528   Joe Perches   drivers/block/flo...
3115
3116
3117
3118
  				ret = fd_copyout(ptr->data, ptr->kernel_data,
  						 length);
  				if (ret)
  					return ret;
bb57f0c66   Joe Perches   drivers/block/flo...
3119
  			}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3120
3121
3122
  		}
  		ptr = ptr->next;
  	}
7f2527174   Joe Perches   drivers/block/flo...
3123

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3124
3125
3126
3127
3128
  	return 0;
  }
  
  static void raw_cmd_free(struct floppy_raw_cmd **ptr)
  {
06f748c47   Jesper Juhl   floppy: do a very...
3129
3130
  	struct floppy_raw_cmd *next;
  	struct floppy_raw_cmd *this;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
  
  	this = *ptr;
  	*ptr = NULL;
  	while (this) {
  		if (this->buffer_length) {
  			fd_dma_mem_free((unsigned long)this->kernel_data,
  					this->buffer_length);
  			this->buffer_length = 0;
  		}
  		next = this->next;
  		kfree(this);
  		this = next;
  	}
  }
be7a12bb1   Stephen Hemminger   floppy: remove un...
3145
  static int raw_cmd_copyin(int cmd, void __user *param,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3146
3147
3148
3149
3150
3151
3152
  				 struct floppy_raw_cmd **rcmd)
  {
  	struct floppy_raw_cmd *ptr;
  	int ret;
  	int i;
  
  	*rcmd = NULL;
7f2527174   Joe Perches   drivers/block/flo...
3153
3154
  
  loop:
1661f2e21   Vlastimil Babka   floppy: replace w...
3155
  	ptr = kmalloc(sizeof(struct floppy_raw_cmd), GFP_KERNEL);
7f2527174   Joe Perches   drivers/block/flo...
3156
3157
3158
3159
  	if (!ptr)
  		return -ENOMEM;
  	*rcmd = ptr;
  	ret = copy_from_user(ptr, param, sizeof(*ptr));
7f2527174   Joe Perches   drivers/block/flo...
3160
3161
  	ptr->next = NULL;
  	ptr->buffer_length = 0;
ef87dbe76   Matthew Daley   floppy: ignore ke...
3162
3163
3164
  	ptr->kernel_data = NULL;
  	if (ret)
  		return -EFAULT;
7f2527174   Joe Perches   drivers/block/flo...
3165
3166
  	param += sizeof(struct floppy_raw_cmd);
  	if (ptr->cmd_count > 33)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3167
3168
3169
3170
3171
3172
3173
3174
  			/* the command may now also take up the space
  			 * initially intended for the reply & the
  			 * reply count. Needed for long 82078 commands
  			 * such as RESTORE, which takes ... 17 command
  			 * bytes. Murphy's law #137: When you reserve
  			 * 16 bytes for a structure, you'll one day
  			 * discover that you really need 17...
  			 */
7f2527174   Joe Perches   drivers/block/flo...
3175
3176
3177
3178
3179
  		return -EINVAL;
  
  	for (i = 0; i < 16; i++)
  		ptr->reply[i] = 0;
  	ptr->resultcode = 0;
7f2527174   Joe Perches   drivers/block/flo...
3180
3181
3182
  
  	if (ptr->flags & (FD_RAW_READ | FD_RAW_WRITE)) {
  		if (ptr->length <= 0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3183
  			return -EINVAL;
7f2527174   Joe Perches   drivers/block/flo...
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
  		ptr->kernel_data = (char *)fd_dma_mem_alloc(ptr->length);
  		fallback_on_nodma_alloc(&ptr->kernel_data, ptr->length);
  		if (!ptr->kernel_data)
  			return -ENOMEM;
  		ptr->buffer_length = ptr->length;
  	}
  	if (ptr->flags & FD_RAW_WRITE) {
  		ret = fd_copyin(ptr->data, ptr->kernel_data, ptr->length);
  		if (ret)
  			return ret;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3195

7f2527174   Joe Perches   drivers/block/flo...
3196
  	if (ptr->flags & FD_RAW_MORE) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3197
  		rcmd = &(ptr->next);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3198
  		ptr->rate &= 0x43;
7f2527174   Joe Perches   drivers/block/flo...
3199
  		goto loop;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3200
  	}
7f2527174   Joe Perches   drivers/block/flo...
3201
3202
  
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3203
3204
3205
3206
  }
  
  static int raw_cmd_ioctl(int cmd, void __user *param)
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3207
  	struct floppy_raw_cmd *my_raw_cmd;
06f748c47   Jesper Juhl   floppy: do a very...
3208
3209
3210
  	int drive;
  	int ret2;
  	int ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
  
  	if (FDCS->rawcmd <= 1)
  		FDCS->rawcmd = 1;
  	for (drive = 0; drive < N_DRIVE; drive++) {
  		if (FDC(drive) != fdc)
  			continue;
  		if (drive == current_drive) {
  			if (UDRS->fd_ref > 1) {
  				FDCS->rawcmd = 2;
  				break;
  			}
  		} else if (UDRS->fd_ref) {
  			FDCS->rawcmd = 2;
  			break;
  		}
  	}
  
  	if (FDCS->reset)
  		return -EIO;
  
  	ret = raw_cmd_copyin(cmd, param, &my_raw_cmd);
  	if (ret) {
  		raw_cmd_free(&my_raw_cmd);
  		return ret;
  	}
  
  	raw_cmd = my_raw_cmd;
  	cont = &raw_cmd_cont;
74f63f469   Joe Perches   drivers/block/flo...
3239
  	ret = wait_til_done(floppy_start, true);
87f530d8f   Joe Perches   drivers/block/flo...
3240
3241
  	debug_dcl(DP->flags, "calling disk change from raw_cmd ioctl
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
  
  	if (ret != -EINTR && FDCS->reset)
  		ret = -EIO;
  
  	DRS->track = NO_TRACK;
  
  	ret2 = raw_cmd_copyout(cmd, param, my_raw_cmd);
  	if (!ret)
  		ret = ret2;
  	raw_cmd_free(&my_raw_cmd);
  	return ret;
  }
  
  static int invalidate_drive(struct block_device *bdev)
  {
  	/* invalidate the buffer track to force a reread */
  	set_bit((long)bdev->bd_disk->private_data, &fake_change);
  	process_fd_request();
  	check_disk_change(bdev);
  	return 0;
  }
be7a12bb1   Stephen Hemminger   floppy: remove un...
3263
  static int set_geometry(unsigned int cmd, struct floppy_struct *g,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3264
3265
3266
3267
3268
3269
3270
3271
3272
  			       int drive, int type, struct block_device *bdev)
  {
  	int cnt;
  
  	/* sanity checking for parameters. */
  	if (g->sect <= 0 ||
  	    g->head <= 0 ||
  	    g->track <= 0 || g->track > UDP->tracks >> STRETCH(g) ||
  	    /* check if reserved bits are set */
9e49184c8   Keith Wansbrough   floppy: support a...
3273
  	    (g->stretch & ~(FD_STRETCH | FD_SWAPSIDES | FD_SECTBASEMASK)) != 0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3274
3275
3276
3277
  		return -EINVAL;
  	if (type) {
  		if (!capable(CAP_SYS_ADMIN))
  			return -EPERM;
b1c82b5c5   Jes Sorensen   [PATCH] sem2mutex...
3278
  		mutex_lock(&open_lock);
a0c80efe5   Jiri Kosina   floppy: fix lock_...
3279
  		if (lock_fdc(drive)) {
8516a5000   Jiri Slaby   floppy: fix lock ...
3280
3281
3282
  			mutex_unlock(&open_lock);
  			return -EINTR;
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
  		floppy_type[type] = *g;
  		floppy_type[type].name = "user format";
  		for (cnt = type << 2; cnt < (type << 2) + 4; cnt++)
  			floppy_sizes[cnt] = floppy_sizes[cnt + 0x80] =
  			    floppy_type[type].size + 1;
  		process_fd_request();
  		for (cnt = 0; cnt < N_DRIVE; cnt++) {
  			struct block_device *bdev = opened_bdev[cnt];
  			if (!bdev || ITYPE(drive_state[cnt].fd_device) != type)
  				continue;
93b270f76   NeilBrown   Fix over-zealous ...
3293
  			__invalidate_device(bdev, true);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3294
  		}
b1c82b5c5   Jes Sorensen   [PATCH] sem2mutex...
3295
  		mutex_unlock(&open_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3296
3297
  	} else {
  		int oldStretch;
52a0d61f6   Joe Perches   drivers/block/flo...
3298

a0c80efe5   Jiri Kosina   floppy: fix lock_...
3299
  		if (lock_fdc(drive))
52a0d61f6   Joe Perches   drivers/block/flo...
3300
  			return -EINTR;
4575b5528   Joe Perches   drivers/block/flo...
3301
  		if (cmd != FDDEFPRM) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3302
3303
  			/* notice a disk change immediately, else
  			 * we lose our settings immediately*/
74f63f469   Joe Perches   drivers/block/flo...
3304
  			if (poll_drive(true, FD_RAW_NEED_DISK) == -EINTR)
4575b5528   Joe Perches   drivers/block/flo...
3305
3306
  				return -EINTR;
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
  		oldStretch = g->stretch;
  		user_params[drive] = *g;
  		if (buffer_drive == drive)
  			SUPBOUND(buffer_max, user_params[drive].sect);
  		current_type[drive] = &user_params[drive];
  		floppy_sizes[drive] = user_params[drive].size;
  		if (cmd == FDDEFPRM)
  			DRS->keep_data = -1;
  		else
  			DRS->keep_data = 1;
  		/* invalidation. Invalidate only when needed, i.e.
  		 * when there are already sectors in the buffer cache
  		 * whose number will change. This is useful, because
  		 * mtools often changes the geometry of the disk after
  		 * looking at the boot block */
  		if (DRS->maxblock > user_params[drive].sect ||
  		    DRS->maxtrack ||
  		    ((user_params[drive].sect ^ oldStretch) &
9e49184c8   Keith Wansbrough   floppy: support a...
3325
  		     (FD_SWAPSIDES | FD_SECTBASEMASK)))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3326
3327
3328
3329
3330
3331
3332
3333
  			invalidate_drive(bdev);
  		else
  			process_fd_request();
  	}
  	return 0;
  }
  
  /* handle obsolete ioctl's */
21af54480   Stephen Hemminger   floppy: fix signe...
3334
  static unsigned int ioctl_table[] = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
  	FDCLRPRM,
  	FDSETPRM,
  	FDDEFPRM,
  	FDGETPRM,
  	FDMSGON,
  	FDMSGOFF,
  	FDFMTBEG,
  	FDFMTTRK,
  	FDFMTEND,
  	FDSETEMSGTRESH,
  	FDFLUSH,
  	FDSETMAXERRS,
  	FDGETMAXERRS,
  	FDGETDRVTYP,
  	FDSETDRVPRM,
  	FDGETDRVPRM,
  	FDGETDRVSTAT,
  	FDPOLLDRVSTAT,
  	FDRESET,
  	FDGETFDCSTAT,
  	FDWERRORCLR,
  	FDWERRORGET,
  	FDRAWCMD,
  	FDEJECT,
  	FDTWADDLE
  };
21af54480   Stephen Hemminger   floppy: fix signe...
3361
  static int normalize_ioctl(unsigned int *cmd, int *size)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3362
3363
3364
3365
3366
3367
3368
3369
  {
  	int i;
  
  	for (i = 0; i < ARRAY_SIZE(ioctl_table); i++) {
  		if ((*cmd & 0xffff) == (ioctl_table[i] & 0xffff)) {
  			*size = _IOC_SIZE(*cmd);
  			*cmd = ioctl_table[i];
  			if (*size > _IOC_SIZE(*cmd)) {
b46df356d   Joe Perches   drivers/block/flo...
3370
3371
  				pr_info("ioctl not yet supported
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
  				return -EFAULT;
  			}
  			return 0;
  		}
  	}
  	return -EINVAL;
  }
  
  static int get_floppy_geometry(int drive, int type, struct floppy_struct **g)
  {
  	if (type)
  		*g = &floppy_type[type];
  	else {
a0c80efe5   Jiri Kosina   floppy: fix lock_...
3385
  		if (lock_fdc(drive))
52a0d61f6   Joe Perches   drivers/block/flo...
3386
  			return -EINTR;
74f63f469   Joe Perches   drivers/block/flo...
3387
  		if (poll_drive(false, 0) == -EINTR)
4575b5528   Joe Perches   drivers/block/flo...
3388
  			return -EINTR;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3389
3390
3391
3392
3393
3394
3395
  		process_fd_request();
  		*g = current_type[drive];
  	}
  	if (!*g)
  		return -ENODEV;
  	return 0;
  }
a885c8c43   Christoph Hellwig   [PATCH] Add block...
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
  static int fd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  {
  	int drive = (long)bdev->bd_disk->private_data;
  	int type = ITYPE(drive_state[drive].fd_device);
  	struct floppy_struct *g;
  	int ret;
  
  	ret = get_floppy_geometry(drive, type, &g);
  	if (ret)
  		return ret;
  
  	geo->heads = g->head;
  	geo->sectors = g->sect;
  	geo->cylinders = g->track;
  	return 0;
  }
8a6cfeb6d   Arnd Bergmann   block: push down ...
3412
  static int fd_locked_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3413
3414
  		    unsigned long param)
  {
a4af9b48c   Al Viro   [PATCH] switch fl...
3415
  	int drive = (long)bdev->bd_disk->private_data;
06f748c47   Jesper Juhl   floppy: do a very...
3416
3417
  	int type = ITYPE(UDRS->fd_device);
  	int i;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3418
3419
3420
3421
3422
3423
3424
3425
  	int ret;
  	int size;
  	union inparam {
  		struct floppy_struct g;	/* geometry */
  		struct format_descr f;
  		struct floppy_max_errors max_errors;
  		struct floppy_drive_params dp;
  	} inparam;		/* parameters coming from user space */
724ee626f   Joe Perches   drivers/block/flo...
3426
  	const void *outparam;	/* parameters passed back to user space */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3427
3428
3429
3430
3431
  
  	/* convert compatibility eject ioctls into floppy eject ioctl.
  	 * We do this in order to provide a means to eject floppy disks before
  	 * installing the new fdutils package */
  	if (cmd == CDROMEJECT ||	/* CD-ROM eject */
a81ee5447   Joe Perches   drivers/block/flo...
3432
  	    cmd == 0x6470) {		/* SunOS floppy eject */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3433
3434
3435
3436
3437
3438
  		DPRINT("obsolete eject ioctl
  ");
  		DPRINT("please use floppycontrol --eject
  ");
  		cmd = FDEJECT;
  	}
a81ee5447   Joe Perches   drivers/block/flo...
3439
  	if (!((cmd & 0xff00) == 0x0200))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3440
  		return -EINVAL;
a81ee5447   Joe Perches   drivers/block/flo...
3441
  	/* convert the old style command into a new style command */
4575b5528   Joe Perches   drivers/block/flo...
3442
3443
3444
  	ret = normalize_ioctl(&cmd, &size);
  	if (ret)
  		return ret;
a81ee5447   Joe Perches   drivers/block/flo...
3445

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3446
  	/* permission checks */
0aad92cfe   Joe Perches   drivers/block/flo...
3447
  	if (((cmd & 0x40) && !(mode & (FMODE_WRITE | FMODE_WRITE_IOCTL))) ||
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3448
3449
  	    ((cmd & 0x80) && !capable(CAP_SYS_ADMIN)))
  		return -EPERM;
2886a8bdf   Arjan van de Ven   floppy: Add an ex...
3450
3451
  	if (WARN_ON(size < 0 || size > sizeof(inparam)))
  		return -EINVAL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3452
  	/* copyin */
b87c9e0a8   Joe Perches   drivers/block/flo...
3453
  	memset(&inparam, 0, sizeof(inparam));
4575b5528   Joe Perches   drivers/block/flo...
3454
3455
3456
3457
3458
  	if (_IOC_DIR(cmd) & _IOC_WRITE) {
  		ret = fd_copyin((void __user *)param, &inparam, size);
  		if (ret)
  			return ret;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3459

da2736534   Joe Perches   drivers/block/flo...
3460
3461
3462
3463
3464
  	switch (cmd) {
  	case FDEJECT:
  		if (UDRS->fd_ref != 1)
  			/* somebody else has this drive open */
  			return -EBUSY;
a0c80efe5   Jiri Kosina   floppy: fix lock_...
3465
  		if (lock_fdc(drive))
52a0d61f6   Joe Perches   drivers/block/flo...
3466
  			return -EINTR;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3467

da2736534   Joe Perches   drivers/block/flo...
3468
3469
3470
  		/* do the actual eject. Fails on
  		 * non-Sparc architectures */
  		ret = fd_eject(UNIT(drive));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3471

e02985361   Joe Perches   drivers/block/flo...
3472
3473
  		set_bit(FD_DISK_CHANGED_BIT, &UDRS->flags);
  		set_bit(FD_VERIFY_BIT, &UDRS->flags);
da2736534   Joe Perches   drivers/block/flo...
3474
3475
3476
  		process_fd_request();
  		return ret;
  	case FDCLRPRM:
a0c80efe5   Jiri Kosina   floppy: fix lock_...
3477
  		if (lock_fdc(drive))
52a0d61f6   Joe Perches   drivers/block/flo...
3478
  			return -EINTR;
da2736534   Joe Perches   drivers/block/flo...
3479
3480
3481
3482
3483
3484
3485
3486
  		current_type[drive] = NULL;
  		floppy_sizes[drive] = MAX_DISK_SIZE << 1;
  		UDRS->keep_data = 0;
  		return invalidate_drive(bdev);
  	case FDSETPRM:
  	case FDDEFPRM:
  		return set_geometry(cmd, &inparam.g, drive, type, bdev);
  	case FDGETPRM:
4575b5528   Joe Perches   drivers/block/flo...
3487
  		ret = get_floppy_geometry(drive, type,
724ee626f   Joe Perches   drivers/block/flo...
3488
  					  (struct floppy_struct **)&outparam);
4575b5528   Joe Perches   drivers/block/flo...
3489
3490
  		if (ret)
  			return ret;
da2736534   Joe Perches   drivers/block/flo...
3491
3492
3493
3494
3495
3496
3497
3498
  		break;
  	case FDMSGON:
  		UDP->flags |= FTD_MSG;
  		return 0;
  	case FDMSGOFF:
  		UDP->flags &= ~FTD_MSG;
  		return 0;
  	case FDFMTBEG:
a0c80efe5   Jiri Kosina   floppy: fix lock_...
3499
  		if (lock_fdc(drive))
52a0d61f6   Joe Perches   drivers/block/flo...
3500
  			return -EINTR;
74f63f469   Joe Perches   drivers/block/flo...
3501
  		if (poll_drive(true, FD_RAW_NEED_DISK) == -EINTR)
4575b5528   Joe Perches   drivers/block/flo...
3502
  			return -EINTR;
da2736534   Joe Perches   drivers/block/flo...
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
  		ret = UDRS->flags;
  		process_fd_request();
  		if (ret & FD_VERIFY)
  			return -ENODEV;
  		if (!(ret & FD_DISK_WRITABLE))
  			return -EROFS;
  		return 0;
  	case FDFMTTRK:
  		if (UDRS->fd_ref != 1)
  			return -EBUSY;
  		return do_format(drive, &inparam.f);
  	case FDFMTEND:
  	case FDFLUSH:
a0c80efe5   Jiri Kosina   floppy: fix lock_...
3516
  		if (lock_fdc(drive))
52a0d61f6   Joe Perches   drivers/block/flo...
3517
  			return -EINTR;
da2736534   Joe Perches   drivers/block/flo...
3518
3519
3520
3521
3522
  		return invalidate_drive(bdev);
  	case FDSETEMSGTRESH:
  		UDP->max_errors.reporting = (unsigned short)(param & 0x0f);
  		return 0;
  	case FDGETMAXERRS:
724ee626f   Joe Perches   drivers/block/flo...
3523
  		outparam = &UDP->max_errors;
da2736534   Joe Perches   drivers/block/flo...
3524
3525
3526
3527
3528
3529
  		break;
  	case FDSETMAXERRS:
  		UDP->max_errors = inparam.max_errors;
  		break;
  	case FDGETDRVTYP:
  		outparam = drive_name(type, drive);
724ee626f   Joe Perches   drivers/block/flo...
3530
  		SUPBOUND(size, strlen((const char *)outparam) + 1);
da2736534   Joe Perches   drivers/block/flo...
3531
3532
3533
3534
3535
  		break;
  	case FDSETDRVPRM:
  		*UDP = inparam.dp;
  		break;
  	case FDGETDRVPRM:
724ee626f   Joe Perches   drivers/block/flo...
3536
  		outparam = UDP;
da2736534   Joe Perches   drivers/block/flo...
3537
3538
  		break;
  	case FDPOLLDRVSTAT:
a0c80efe5   Jiri Kosina   floppy: fix lock_...
3539
  		if (lock_fdc(drive))
52a0d61f6   Joe Perches   drivers/block/flo...
3540
  			return -EINTR;
74f63f469   Joe Perches   drivers/block/flo...
3541
  		if (poll_drive(true, FD_RAW_NEED_DISK) == -EINTR)
4575b5528   Joe Perches   drivers/block/flo...
3542
  			return -EINTR;
da2736534   Joe Perches   drivers/block/flo...
3543
3544
3545
  		process_fd_request();
  		/* fall through */
  	case FDGETDRVSTAT:
724ee626f   Joe Perches   drivers/block/flo...
3546
  		outparam = UDRS;
da2736534   Joe Perches   drivers/block/flo...
3547
3548
  		break;
  	case FDRESET:
74f63f469   Joe Perches   drivers/block/flo...
3549
  		return user_reset_fdc(drive, (int)param, true);
da2736534   Joe Perches   drivers/block/flo...
3550
  	case FDGETFDCSTAT:
724ee626f   Joe Perches   drivers/block/flo...
3551
  		outparam = UFDCS;
da2736534   Joe Perches   drivers/block/flo...
3552
3553
3554
3555
3556
  		break;
  	case FDWERRORCLR:
  		memset(UDRWE, 0, sizeof(*UDRWE));
  		return 0;
  	case FDWERRORGET:
724ee626f   Joe Perches   drivers/block/flo...
3557
  		outparam = UDRWE;
da2736534   Joe Perches   drivers/block/flo...
3558
3559
3560
  		break;
  	case FDRAWCMD:
  		if (type)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3561
  			return -EINVAL;
a0c80efe5   Jiri Kosina   floppy: fix lock_...
3562
  		if (lock_fdc(drive))
52a0d61f6   Joe Perches   drivers/block/flo...
3563
  			return -EINTR;
da2736534   Joe Perches   drivers/block/flo...
3564
  		set_floppy(drive);
4575b5528   Joe Perches   drivers/block/flo...
3565
3566
3567
  		i = raw_cmd_ioctl(cmd, (void __user *)param);
  		if (i == -EINTR)
  			return -EINTR;
da2736534   Joe Perches   drivers/block/flo...
3568
3569
3570
  		process_fd_request();
  		return i;
  	case FDTWADDLE:
a0c80efe5   Jiri Kosina   floppy: fix lock_...
3571
  		if (lock_fdc(drive))
52a0d61f6   Joe Perches   drivers/block/flo...
3572
  			return -EINTR;
da2736534   Joe Perches   drivers/block/flo...
3573
3574
3575
3576
3577
3578
  		twaddle();
  		process_fd_request();
  		return 0;
  	default:
  		return -EINVAL;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3579
3580
3581
  
  	if (_IOC_DIR(cmd) & _IOC_READ)
  		return fd_copyout((void __user *)param, outparam, size);
da2736534   Joe Perches   drivers/block/flo...
3582
3583
  
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3584
  }
8a6cfeb6d   Arnd Bergmann   block: push down ...
3585
3586
3587
3588
  static int fd_ioctl(struct block_device *bdev, fmode_t mode,
  			     unsigned int cmd, unsigned long param)
  {
  	int ret;
2a48fc0ab   Arnd Bergmann   block: autoconver...
3589
  	mutex_lock(&floppy_mutex);
8a6cfeb6d   Arnd Bergmann   block: push down ...
3590
  	ret = fd_locked_ioctl(bdev, mode, cmd, param);
2a48fc0ab   Arnd Bergmann   block: autoconver...
3591
  	mutex_unlock(&floppy_mutex);
8a6cfeb6d   Arnd Bergmann   block: push down ...
3592
3593
3594
  
  	return ret;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3595
3596
  static void __init config_types(void)
  {
b46df356d   Joe Perches   drivers/block/flo...
3597
  	bool has_drive = false;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3598
3599
3600
3601
3602
3603
3604
3605
3606
  	int drive;
  
  	/* read drive info out of physical CMOS */
  	drive = 0;
  	if (!UDP->cmos)
  		UDP->cmos = FLOPPY0_TYPE;
  	drive = 1;
  	if (!UDP->cmos && FLOPPY1_TYPE)
  		UDP->cmos = FLOPPY1_TYPE;
06f748c47   Jesper Juhl   floppy: do a very...
3607
  	/* FIXME: additional physical CMOS drive detection should go here */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3608
3609
3610
3611
3612
  
  	for (drive = 0; drive < N_DRIVE; drive++) {
  		unsigned int type = UDP->cmos;
  		struct floppy_drive_params *params;
  		const char *name = NULL;
bcf4299e6   Rasmus Villemoes   floppy: make loca...
3613
  		char temparea[32];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3614

945f390f0   Tobias Klauser   [PATCH] drivers/b...
3615
  		if (type < ARRAY_SIZE(default_drive_params)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3616
3617
3618
3619
3620
3621
3622
3623
  			params = &default_drive_params[type].params;
  			if (type) {
  				name = default_drive_params[type].name;
  				allowed_drive_mask |= 1 << drive;
  			} else
  				allowed_drive_mask &= ~(1 << drive);
  		} else {
  			params = &default_drive_params[0].params;
bcf4299e6   Rasmus Villemoes   floppy: make loca...
3624
3625
  			snprintf(temparea, sizeof(temparea),
  				 "unknown type %d (usb?)", type);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3626
3627
3628
  			name = temparea;
  		}
  		if (name) {
b46df356d   Joe Perches   drivers/block/flo...
3629
3630
3631
3632
3633
3634
3635
  			const char *prepend;
  			if (!has_drive) {
  				prepend = "";
  				has_drive = true;
  				pr_info("Floppy drive(s):");
  			} else {
  				prepend = ",";
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3636
  			}
b46df356d   Joe Perches   drivers/block/flo...
3637
3638
  
  			pr_cont("%s fd%d is %s", prepend, drive, name);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3639
3640
3641
  		}
  		*UDP = *params;
  	}
b46df356d   Joe Perches   drivers/block/flo...
3642
3643
3644
3645
  
  	if (has_drive)
  		pr_cont("
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3646
  }
db2a144be   Al Viro   block_device_oper...
3647
  static void floppy_release(struct gendisk *disk, fmode_t mode)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3648
  {
a4af9b48c   Al Viro   [PATCH] switch fl...
3649
  	int drive = (long)disk->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3650

2a48fc0ab   Arnd Bergmann   block: autoconver...
3651
  	mutex_lock(&floppy_mutex);
b1c82b5c5   Jes Sorensen   [PATCH] sem2mutex...
3652
  	mutex_lock(&open_lock);
bfa10b8c9   Jiri Kosina   floppy: remove fl...
3653
  	if (!UDRS->fd_ref--) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3654
3655
3656
3657
3658
  		DPRINT("floppy_release with fd_ref == 0");
  		UDRS->fd_ref = 0;
  	}
  	if (!UDRS->fd_ref)
  		opened_bdev[drive] = NULL;
b1c82b5c5   Jes Sorensen   [PATCH] sem2mutex...
3659
  	mutex_unlock(&open_lock);
2a48fc0ab   Arnd Bergmann   block: autoconver...
3660
  	mutex_unlock(&floppy_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3661
3662
3663
3664
3665
3666
3667
  }
  
  /*
   * floppy_open check for aliasing (/dev/fd0 can be the same as
   * /dev/PS0 etc), and disallows simultaneous access to the same
   * drive with different device numbers.
   */
a4af9b48c   Al Viro   [PATCH] switch fl...
3668
  static int floppy_open(struct block_device *bdev, fmode_t mode)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3669
  {
a4af9b48c   Al Viro   [PATCH] switch fl...
3670
3671
  	int drive = (long)bdev->bd_disk->private_data;
  	int old_dev, new_dev;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3672
3673
3674
  	int try;
  	int res = -EBUSY;
  	char *tmp;
2a48fc0ab   Arnd Bergmann   block: autoconver...
3675
  	mutex_lock(&floppy_mutex);
b1c82b5c5   Jes Sorensen   [PATCH] sem2mutex...
3676
  	mutex_lock(&open_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3677
  	old_dev = UDRS->fd_device;
a4af9b48c   Al Viro   [PATCH] switch fl...
3678
  	if (opened_bdev[drive] && opened_bdev[drive] != bdev)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3679
3680
3681
  		goto out2;
  
  	if (!UDRS->fd_ref && (UDP->flags & FD_BROKEN_DCL)) {
e02985361   Joe Perches   drivers/block/flo...
3682
3683
  		set_bit(FD_DISK_CHANGED_BIT, &UDRS->flags);
  		set_bit(FD_VERIFY_BIT, &UDRS->flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3684
  	}
bfa10b8c9   Jiri Kosina   floppy: remove fl...
3685
  	UDRS->fd_ref++;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3686

a4af9b48c   Al Viro   [PATCH] switch fl...
3687
  	opened_bdev[drive] = bdev;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
  
  	res = -ENXIO;
  
  	if (!floppy_track_buffer) {
  		/* if opening an ED drive, reserve a big buffer,
  		 * else reserve a small one */
  		if ((UDP->cmos == 6) || (UDP->cmos == 5))
  			try = 64;	/* Only 48 actually useful */
  		else
  			try = 32;	/* Only 24 actually useful */
  
  		tmp = (char *)fd_dma_mem_alloc(1024 * try);
  		if (!tmp && !floppy_track_buffer) {
  			try >>= 1;	/* buffer only one side */
  			INFBOUND(try, 16);
  			tmp = (char *)fd_dma_mem_alloc(1024 * try);
  		}
a81ee5447   Joe Perches   drivers/block/flo...
3705
  		if (!tmp && !floppy_track_buffer)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3706
  			fallback_on_nodma_alloc(&tmp, 2048 * try);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
  		if (!tmp && !floppy_track_buffer) {
  			DPRINT("Unable to allocate DMA memory
  ");
  			goto out;
  		}
  		if (floppy_track_buffer) {
  			if (tmp)
  				fd_dma_mem_free((unsigned long)tmp, try * 1024);
  		} else {
  			buffer_min = buffer_max = -1;
  			floppy_track_buffer = tmp;
  			max_buffer_sectors = try;
  		}
  	}
a4af9b48c   Al Viro   [PATCH] switch fl...
3721
3722
3723
3724
  	new_dev = MINOR(bdev->bd_dev);
  	UDRS->fd_device = new_dev;
  	set_capacity(disks[drive], floppy_sizes[new_dev]);
  	if (old_dev != -1 && old_dev != new_dev) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3725
3726
3727
  		if (buffer_drive == drive)
  			buffer_track = -1;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3728
3729
  	if (UFDCS->rawcmd == 1)
  		UFDCS->rawcmd = 2;
f2791e7ea   Jens Axboe   Revert "floppy: r...
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
  	if (!(mode & FMODE_NDELAY)) {
  		if (mode & (FMODE_READ|FMODE_WRITE)) {
  			UDRS->last_checked = 0;
  			clear_bit(FD_OPEN_SHOULD_FAIL_BIT, &UDRS->flags);
  			check_disk_change(bdev);
  			if (test_bit(FD_DISK_CHANGED_BIT, &UDRS->flags))
  				goto out;
  			if (test_bit(FD_OPEN_SHOULD_FAIL_BIT, &UDRS->flags))
  				goto out;
  		}
  		res = -EROFS;
  		if ((mode & FMODE_WRITE) &&
  		    !test_bit(FD_DISK_WRITABLE_BIT, &UDRS->flags))
  			goto out;
  	}
b1c82b5c5   Jes Sorensen   [PATCH] sem2mutex...
3745
  	mutex_unlock(&open_lock);
2a48fc0ab   Arnd Bergmann   block: autoconver...
3746
  	mutex_unlock(&floppy_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3747
3748
  	return 0;
  out:
bfa10b8c9   Jiri Kosina   floppy: remove fl...
3749
  	UDRS->fd_ref--;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3750
3751
  	if (!UDRS->fd_ref)
  		opened_bdev[drive] = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3752
  out2:
b1c82b5c5   Jes Sorensen   [PATCH] sem2mutex...
3753
  	mutex_unlock(&open_lock);
2a48fc0ab   Arnd Bergmann   block: autoconver...
3754
  	mutex_unlock(&floppy_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3755
3756
3757
3758
3759
3760
  	return res;
  }
  
  /*
   * Check if the disk has been changed or if a change has been faked.
   */
1a8a74f03   Tejun Heo   floppy,{ami|ata}f...
3761
3762
  static unsigned int floppy_check_events(struct gendisk *disk,
  					unsigned int clearing)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3763
3764
  {
  	int drive = (long)disk->private_data;
e02985361   Joe Perches   drivers/block/flo...
3765
3766
  	if (test_bit(FD_DISK_CHANGED_BIT, &UDRS->flags) ||
  	    test_bit(FD_VERIFY_BIT, &UDRS->flags))
1a8a74f03   Tejun Heo   floppy,{ami|ata}f...
3767
  		return DISK_EVENT_MEDIA_CHANGE;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3768

50297cbf0   Marcelo Feitoza Parisi   [PATCH] drivers/b...
3769
  	if (time_after(jiffies, UDRS->last_checked + UDP->checkfreq)) {
a0c80efe5   Jiri Kosina   floppy: fix lock_...
3770
3771
  		if (lock_fdc(drive))
  			return -EINTR;
74f63f469   Joe Perches   drivers/block/flo...
3772
  		poll_drive(false, 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3773
  		process_fd_request();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3774
  	}
e02985361   Joe Perches   drivers/block/flo...
3775
3776
  	if (test_bit(FD_DISK_CHANGED_BIT, &UDRS->flags) ||
  	    test_bit(FD_VERIFY_BIT, &UDRS->flags) ||
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3777
  	    test_bit(drive, &fake_change) ||
2b51dca79   Pekka Enberg   floppy: replace N...
3778
  	    drive_no_geom(drive))
1a8a74f03   Tejun Heo   floppy,{ami|ata}f...
3779
  		return DISK_EVENT_MEDIA_CHANGE;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3780
3781
3782
3783
3784
3785
3786
3787
  	return 0;
  }
  
  /*
   * This implements "read block 0" for floppy_revalidate().
   * Needed for format autodetection, checking whether there is
   * a disk in the drive, and whether that disk is writable.
   */
7b7b68bba   Jiri Kosina   floppy: bail out ...
3788
3789
3790
3791
  struct rb0_cbdata {
  	int drive;
  	struct completion complete;
  };
4246a0b63   Christoph Hellwig   block: add a bi_e...
3792
  static void floppy_rb0_cb(struct bio *bio)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3793
  {
7b7b68bba   Jiri Kosina   floppy: bail out ...
3794
3795
  	struct rb0_cbdata *cbdata = (struct rb0_cbdata *)bio->bi_private;
  	int drive = cbdata->drive;
4246a0b63   Christoph Hellwig   block: add a bi_e...
3796
3797
3798
3799
  	if (bio->bi_error) {
  		pr_info("floppy: error %d while reading block 0
  ",
  			bio->bi_error);
7b7b68bba   Jiri Kosina   floppy: bail out ...
3800
3801
3802
  		set_bit(FD_OPEN_SHOULD_FAIL_BIT, &UDRS->flags);
  	}
  	complete(&cbdata->complete);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3803
  }
7b7b68bba   Jiri Kosina   floppy: bail out ...
3804
  static int __floppy_read_block_0(struct block_device *bdev, int drive)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3805
3806
3807
  {
  	struct bio bio;
  	struct bio_vec bio_vec;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3808
  	struct page *page;
7b7b68bba   Jiri Kosina   floppy: bail out ...
3809
  	struct rb0_cbdata cbdata;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
  	size_t size;
  
  	page = alloc_page(GFP_NOIO);
  	if (!page) {
  		process_fd_request();
  		return -ENOMEM;
  	}
  
  	size = bdev->bd_block_size;
  	if (!size)
  		size = 1024;
7b7b68bba   Jiri Kosina   floppy: bail out ...
3821
  	cbdata.drive = drive;
3a83f4677   Ming Lei   block: bio: pass ...
3822
  	bio_init(&bio, &bio_vec, 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3823
  	bio.bi_bdev = bdev;
2c73a603c   Ming Lei   block: floppy: us...
3824
  	bio_add_page(&bio, page, size, 0);
4f024f379   Kent Overstreet   block: Abstract o...
3825
  	bio.bi_iter.bi_sector = 0;
6314a108e   Jiri Kosina   floppy: do not co...
3826
  	bio.bi_flags |= (1 << BIO_QUIET);
7b7b68bba   Jiri Kosina   floppy: bail out ...
3827
3828
  	bio.bi_private = &cbdata;
  	bio.bi_end_io = floppy_rb0_cb;
95fe6c1a2   Mike Christie   block, fs, mm, dr...
3829
  	bio_set_op_attrs(&bio, REQ_OP_READ, 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3830

4e49ea4a3   Mike Christie   block/fs/drivers:...
3831
  	submit_bio(&bio);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3832
  	process_fd_request();
7b7b68bba   Jiri Kosina   floppy: bail out ...
3833
3834
3835
  
  	init_completion(&cbdata.complete);
  	wait_for_completion(&cbdata.complete);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
  
  	__free_page(page);
  
  	return 0;
  }
  
  /* revalidate the floppy disk, i.e. trigger format autodetection by reading
   * the bootblock (block 0). "Autodetection" is also needed to check whether
   * there is a disk in the drive at all... Thus we also do it for fixed
   * geometry formats */
  static int floppy_revalidate(struct gendisk *disk)
  {
  	int drive = (long)disk->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3849
3850
  	int cf;
  	int res = 0;
e02985361   Joe Perches   drivers/block/flo...
3851
3852
  	if (test_bit(FD_DISK_CHANGED_BIT, &UDRS->flags) ||
  	    test_bit(FD_VERIFY_BIT, &UDRS->flags) ||
2b51dca79   Pekka Enberg   floppy: replace N...
3853
3854
  	    test_bit(drive, &fake_change) ||
  	    drive_no_geom(drive)) {
01b6b67ed   Stephen Hemminger   floppy: use warni...
3855
3856
3857
  		if (WARN(atomic_read(&usage_count) == 0,
  			 "VFS: revalidate called on non-open device.
  "))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3858
  			return -EFAULT;
01b6b67ed   Stephen Hemminger   floppy: use warni...
3859

a0c80efe5   Jiri Kosina   floppy: fix lock_...
3860
3861
3862
  		res = lock_fdc(drive);
  		if (res)
  			return res;
e02985361   Joe Perches   drivers/block/flo...
3863
3864
  		cf = (test_bit(FD_DISK_CHANGED_BIT, &UDRS->flags) ||
  		      test_bit(FD_VERIFY_BIT, &UDRS->flags));
2b51dca79   Pekka Enberg   floppy: replace N...
3865
  		if (!(cf || test_bit(drive, &fake_change) || drive_no_geom(drive))) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3866
3867
3868
3869
3870
3871
3872
3873
  			process_fd_request();	/*already done by another thread */
  			return 0;
  		}
  		UDRS->maxblock = 0;
  		UDRS->maxtrack = 0;
  		if (buffer_drive == drive)
  			buffer_track = -1;
  		clear_bit(drive, &fake_change);
e02985361   Joe Perches   drivers/block/flo...
3874
  		clear_bit(FD_DISK_CHANGED_BIT, &UDRS->flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3875
3876
  		if (cf)
  			UDRS->generation++;
2b51dca79   Pekka Enberg   floppy: replace N...
3877
  		if (drive_no_geom(drive)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3878
  			/* auto-sensing */
7b7b68bba   Jiri Kosina   floppy: bail out ...
3879
  			res = __floppy_read_block_0(opened_bdev[drive], drive);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3880
3881
  		} else {
  			if (cf)
74f63f469   Joe Perches   drivers/block/flo...
3882
  				poll_drive(false, FD_RAW_NEED_DISK);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3883
3884
3885
3886
3887
3888
  			process_fd_request();
  		}
  	}
  	set_capacity(disk, floppy_sizes[UDRS->fd_device]);
  	return res;
  }
83d5cde47   Alexey Dobriyan   const: make block...
3889
  static const struct block_device_operations floppy_fops = {
06f748c47   Jesper Juhl   floppy: do a very...
3890
  	.owner			= THIS_MODULE,
a4af9b48c   Al Viro   [PATCH] switch fl...
3891
3892
  	.open			= floppy_open,
  	.release		= floppy_release,
8a6cfeb6d   Arnd Bergmann   block: push down ...
3893
  	.ioctl			= fd_ioctl,
06f748c47   Jesper Juhl   floppy: do a very...
3894
  	.getgeo			= fd_getgeo,
1a8a74f03   Tejun Heo   floppy,{ami|ata}f...
3895
  	.check_events		= floppy_check_events,
06f748c47   Jesper Juhl   floppy: do a very...
3896
  	.revalidate_disk	= floppy_revalidate,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3897
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3898

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
  /*
   * Floppy Driver initialization
   * =============================
   */
  
  /* Determine the floppy disk controller type */
  /* This routine was written by David C. Niemi */
  static char __init get_fdc_version(void)
  {
  	int r;
  
  	output_byte(FD_DUMPREGS);	/* 82072 and better know DUMPREGS */
  	if (FDCS->reset)
  		return FDC_NONE;
d7b2b2ecd   Joe Perches   drivers/block/flo...
3913
3914
  	r = result();
  	if (r <= 0x00)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3915
3916
  		return FDC_NONE;	/* No FDC present ??? */
  	if ((r == 1) && (reply_buffer[0] == 0x80)) {
b46df356d   Joe Perches   drivers/block/flo...
3917
3918
  		pr_info("FDC %d is an 8272A
  ", fdc);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3919
3920
3921
  		return FDC_8272A;	/* 8272a/765 don't know DUMPREGS */
  	}
  	if (r != 10) {
b46df356d   Joe Perches   drivers/block/flo...
3922
3923
3924
  		pr_info("FDC %d init: DUMPREGS: unexpected return of %d bytes.
  ",
  			fdc, r);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3925
3926
3927
3928
  		return FDC_UNKNOWN;
  	}
  
  	if (!fdc_configure()) {
b46df356d   Joe Perches   drivers/block/flo...
3929
3930
  		pr_info("FDC %d is an 82072
  ", fdc);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3931
3932
3933
3934
3935
3936
3937
  		return FDC_82072;	/* 82072 doesn't know CONFIGURE */
  	}
  
  	output_byte(FD_PERPENDICULAR);
  	if (need_more_output() == MORE_OUTPUT) {
  		output_byte(0);
  	} else {
b46df356d   Joe Perches   drivers/block/flo...
3938
3939
  		pr_info("FDC %d is an 82072A
  ", fdc);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3940
3941
3942
3943
3944
3945
  		return FDC_82072A;	/* 82072A as found on Sparcs. */
  	}
  
  	output_byte(FD_UNLOCK);
  	r = result();
  	if ((r == 1) && (reply_buffer[0] == 0x80)) {
b46df356d   Joe Perches   drivers/block/flo...
3946
3947
  		pr_info("FDC %d is a pre-1991 82077
  ", fdc);
d7b2b2ecd   Joe Perches   drivers/block/flo...
3948
  		return FDC_82077_ORIG;	/* Pre-1991 82077, doesn't know
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3949
3950
3951
  					 * LOCK/UNLOCK */
  	}
  	if ((r != 1) || (reply_buffer[0] != 0x00)) {
b46df356d   Joe Perches   drivers/block/flo...
3952
3953
3954
  		pr_info("FDC %d init: UNLOCK: unexpected return of %d bytes.
  ",
  			fdc, r);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3955
3956
3957
3958
3959
  		return FDC_UNKNOWN;
  	}
  	output_byte(FD_PARTID);
  	r = result();
  	if (r != 1) {
b46df356d   Joe Perches   drivers/block/flo...
3960
3961
3962
  		pr_info("FDC %d init: PARTID: unexpected return of %d bytes.
  ",
  			fdc, r);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3963
3964
3965
  		return FDC_UNKNOWN;
  	}
  	if (reply_buffer[0] == 0x80) {
b46df356d   Joe Perches   drivers/block/flo...
3966
3967
  		pr_info("FDC %d is a post-1991 82077
  ", fdc);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3968
3969
3970
3971
3972
  		return FDC_82077;	/* Revised 82077AA passes all the tests */
  	}
  	switch (reply_buffer[0] >> 5) {
  	case 0x0:
  		/* Either a 82078-1 or a 82078SL running at 5Volt */
b46df356d   Joe Perches   drivers/block/flo...
3973
3974
  		pr_info("FDC %d is an 82078.
  ", fdc);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3975
3976
  		return FDC_82078;
  	case 0x1:
b46df356d   Joe Perches   drivers/block/flo...
3977
3978
  		pr_info("FDC %d is a 44pin 82078
  ", fdc);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3979
3980
  		return FDC_82078;
  	case 0x2:
b46df356d   Joe Perches   drivers/block/flo...
3981
3982
  		pr_info("FDC %d is a S82078B
  ", fdc);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3983
3984
  		return FDC_S82078B;
  	case 0x3:
b46df356d   Joe Perches   drivers/block/flo...
3985
3986
  		pr_info("FDC %d is a National Semiconductor PC87306
  ", fdc);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3987
3988
  		return FDC_87306;
  	default:
b46df356d   Joe Perches   drivers/block/flo...
3989
3990
3991
  		pr_info("FDC %d init: 82078 variant with unknown PARTID=%d.
  ",
  			fdc, reply_buffer[0] >> 5);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
  		return FDC_82078_UNKN;
  	}
  }				/* get_fdc_version */
  
  /* lilo configuration */
  
  static void __init floppy_set_flags(int *ints, int param, int param2)
  {
  	int i;
  
  	for (i = 0; i < ARRAY_SIZE(default_drive_params); i++) {
  		if (param)
  			default_drive_params[i].params.flags |= param2;
  		else
  			default_drive_params[i].params.flags &= ~param2;
  	}
  	DPRINT("%s flag 0x%x
  ", param2 ? "Setting" : "Clearing", param);
  }
  
  static void __init daring(int *ints, int param, int param2)
  {
  	int i;
  
  	for (i = 0; i < ARRAY_SIZE(default_drive_params); i++) {
  		if (param) {
  			default_drive_params[i].params.select_delay = 0;
  			default_drive_params[i].params.flags |=
  			    FD_SILENT_DCL_CLEAR;
  		} else {
  			default_drive_params[i].params.select_delay =
  			    2 * HZ / 100;
  			default_drive_params[i].params.flags &=
  			    ~FD_SILENT_DCL_CLEAR;
  		}
  	}
  	DPRINT("Assuming %s floppy hardware
  ", param ? "standard" : "broken");
  }
  
  static void __init set_cmos(int *ints, int dummy, int dummy2)
  {
  	int current_drive = 0;
  
  	if (ints[0] != 2) {
  		DPRINT("wrong number of parameters for CMOS
  ");
  		return;
  	}
  	current_drive = ints[1];
  	if (current_drive < 0 || current_drive >= 8) {
  		DPRINT("bad drive for set_cmos
  ");
  		return;
  	}
  #if N_FDC > 1
  	if (current_drive >= 4 && !FDC2)
  		FDC2 = 0x370;
  #endif
  	DP->cmos = ints[2];
  	DPRINT("setting CMOS code to %d
  ", ints[2]);
  }
  
  static struct param_table {
  	const char *name;
  	void (*fn) (int *ints, int param, int param2);
  	int *var;
  	int def_param;
  	int param2;
  } config_params[] __initdata = {
  	{"allowed_drive_mask", NULL, &allowed_drive_mask, 0xff, 0}, /* obsolete */
  	{"all_drives", NULL, &allowed_drive_mask, 0xff, 0},	/* obsolete */
  	{"asus_pci", NULL, &allowed_drive_mask, 0x33, 0},
  	{"irq", NULL, &FLOPPY_IRQ, 6, 0},
  	{"dma", NULL, &FLOPPY_DMA, 2, 0},
  	{"daring", daring, NULL, 1, 0},
  #if N_FDC > 1
  	{"two_fdc", NULL, &FDC2, 0x370, 0},
  	{"one_fdc", NULL, &FDC2, 0, 0},
  #endif
  	{"thinkpad", floppy_set_flags, NULL, 1, FD_INVERTED_DCL},
  	{"broken_dcl", floppy_set_flags, NULL, 1, FD_BROKEN_DCL},
  	{"messages", floppy_set_flags, NULL, 1, FTD_MSG},
  	{"silent_dcl_clear", floppy_set_flags, NULL, 1, FD_SILENT_DCL_CLEAR},
  	{"debug", floppy_set_flags, NULL, 1, FD_DEBUG},
  	{"nodma", NULL, &can_use_virtual_dma, 1, 0},
  	{"omnibook", NULL, &can_use_virtual_dma, 1, 0},
  	{"yesdma", NULL, &can_use_virtual_dma, 0, 0},
  	{"fifo_depth", NULL, &fifo_depth, 0xa, 0},
  	{"nofifo", NULL, &no_fifo, 0x20, 0},
  	{"usefifo", NULL, &no_fifo, 0, 0},
  	{"cmos", set_cmos, NULL, 0, 0},
  	{"slow", NULL, &slow_floppy, 1, 0},
  	{"unexpected_interrupts", NULL, &print_unex, 1, 0},
  	{"no_unexpected_interrupts", NULL, &print_unex, 0, 0},
  	{"L40SX", NULL, &print_unex, 0, 0}
  
  	EXTRA_FLOPPY_PARAMS
  };
  
  static int __init floppy_setup(char *str)
  {
  	int i;
  	int param;
  	int ints[11];
  
  	str = get_options(str, ARRAY_SIZE(ints), ints);
  	if (str) {
  		for (i = 0; i < ARRAY_SIZE(config_params); i++) {
  			if (strcmp(str, config_params[i].name) == 0) {
  				if (ints[0])
  					param = ints[1];
  				else
  					param = config_params[i].def_param;
  				if (config_params[i].fn)
bb57f0c66   Joe Perches   drivers/block/flo...
4108
4109
4110
  					config_params[i].fn(ints, param,
  							    config_params[i].
  							    param2);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
  				if (config_params[i].var) {
  					DPRINT("%s=%d
  ", str, param);
  					*config_params[i].var = param;
  				}
  				return 1;
  			}
  		}
  	}
  	if (str) {
  		DPRINT("unknown floppy option [%s]
  ", str);
  
  		DPRINT("allowed options are:");
  		for (i = 0; i < ARRAY_SIZE(config_params); i++)
b46df356d   Joe Perches   drivers/block/flo...
4126
4127
4128
  			pr_cont(" %s", config_params[i].name);
  		pr_cont("
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4129
4130
4131
  	} else
  		DPRINT("botched floppy option
  ");
31c00fc15   Randy Dunlap   Create/use more d...
4132
4133
  	DPRINT("Read Documentation/blockdev/floppy.txt
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4134
4135
4136
4137
  	return 0;
  }
  
  static int have_no_fdc = -ENODEV;
9a8af6b3b   Andrew Morton   [PATCH] Floppy: a...
4138
4139
  static ssize_t floppy_cmos_show(struct device *dev,
  				struct device_attribute *attr, char *buf)
94fd0db7b   Hannes Reinecke   [PATCH] Floppy: A...
4140
  {
71b3e0c1a   Eric Miao   platform: make be...
4141
  	struct platform_device *p = to_platform_device(dev);
9a8af6b3b   Andrew Morton   [PATCH] Floppy: a...
4142
  	int drive;
94fd0db7b   Hannes Reinecke   [PATCH] Floppy: A...
4143

9a8af6b3b   Andrew Morton   [PATCH] Floppy: a...
4144
4145
4146
  	drive = p->id;
  	return sprintf(buf, "%X
  ", UDP->cmos);
94fd0db7b   Hannes Reinecke   [PATCH] Floppy: A...
4147
  }
48c8cee61   Joe Perches   drivers/block/flo...
4148

be1c0fbfb   Stephen Hemminger   floppy: cmos attr...
4149
  static DEVICE_ATTR(cmos, S_IRUGO, floppy_cmos_show, NULL);
94fd0db7b   Hannes Reinecke   [PATCH] Floppy: A...
4150

b7f120b21   Takashi Iwai   floppy: Avoid man...
4151
4152
4153
4154
4155
4156
  static struct attribute *floppy_dev_attrs[] = {
  	&dev_attr_cmos.attr,
  	NULL
  };
  
  ATTRIBUTE_GROUPS(floppy_dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4157
4158
  static void floppy_device_release(struct device *dev)
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4159
  }
c90cd332d   Frans Pop   floppy: Switch dr...
4160
  static int floppy_resume(struct device *dev)
5e50b9ef9   Ondrej Zary   floppy: fix hiber...
4161
4162
4163
4164
4165
  {
  	int fdc;
  
  	for (fdc = 0; fdc < N_FDC; fdc++)
  		if (FDCS->address != -1)
74f63f469   Joe Perches   drivers/block/flo...
4166
  			user_reset_fdc(-1, FD_RESET_ALWAYS, false);
5e50b9ef9   Ondrej Zary   floppy: fix hiber...
4167
4168
4169
  
  	return 0;
  }
471452104   Alexey Dobriyan   const: constify r...
4170
  static const struct dev_pm_ops floppy_pm_ops = {
5e50b9ef9   Ondrej Zary   floppy: fix hiber...
4171
  	.resume = floppy_resume,
c90cd332d   Frans Pop   floppy: Switch dr...
4172
4173
4174
4175
  	.restore = floppy_resume,
  };
  
  static struct platform_driver floppy_driver = {
5e50b9ef9   Ondrej Zary   floppy: fix hiber...
4176
  	.driver = {
bb57f0c66   Joe Perches   drivers/block/flo...
4177
4178
  		   .name = "floppy",
  		   .pm = &floppy_pm_ops,
5e50b9ef9   Ondrej Zary   floppy: fix hiber...
4179
4180
  	},
  };
94fd0db7b   Hannes Reinecke   [PATCH] Floppy: A...
4181
  static struct platform_device floppy_device[N_DRIVE];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4182

8d3ab4ebf   Herton Ronaldo Krzesinski   floppy: use commo...
4183
4184
4185
4186
4187
4188
4189
4190
  static bool floppy_available(int drive)
  {
  	if (!(allowed_drive_mask & (1 << drive)))
  		return false;
  	if (fdc_state[FDC(drive)].version == FDC_NONE)
  		return false;
  	return true;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4191
4192
4193
  static struct kobject *floppy_find(dev_t dev, int *part, void *data)
  {
  	int drive = (*part & 3) | ((*part & 0x80) >> 5);
8d3ab4ebf   Herton Ronaldo Krzesinski   floppy: use commo...
4194
  	if (drive >= N_DRIVE || !floppy_available(drive))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4195
  		return NULL;
945f390f0   Tobias Klauser   [PATCH] drivers/b...
4196
  	if (((*part >> 2) & 0x1f) >= ARRAY_SIZE(floppy_type))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4197
4198
4199
4200
  		return NULL;
  	*part = 0;
  	return get_disk(disks[drive]);
  }
0cc15d03b   Andi Kleen   floppy: Run flopp...
4201
  static int __init do_floppy_init(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4202
  {
1a4ae43e4   Herton Ronaldo Krzesinski   floppy: remove dr...
4203
  	int i, unit, drive, err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4204

285203c8f   Stephen Hemminger   floppy: initializ...
4205
4206
  	set_debugt();
  	interruptjiffies = resultjiffies = jiffies;
68e1ee62f   Kumar Gala   powerpc: convert ...
4207
  #if defined(CONFIG_PPC)
ef16b5194   Olaf Hering   [PATCH] exit earl...
4208
4209
4210
  	if (check_legacy_ioport(FDC1))
  		return -ENODEV;
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4211
  	raw_cmd = NULL;
b54e1f888   Herton Ronaldo Krzesinski   floppy: don't cal...
4212
4213
4214
  	floppy_wq = alloc_ordered_workqueue("floppy", 0);
  	if (!floppy_wq)
  		return -ENOMEM;
1a4ae43e4   Herton Ronaldo Krzesinski   floppy: remove dr...
4215
4216
4217
  	for (drive = 0; drive < N_DRIVE; drive++) {
  		disks[drive] = alloc_disk(1);
  		if (!disks[drive]) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4218
4219
4220
  			err = -ENOMEM;
  			goto out_put_disk;
  		}
1a4ae43e4   Herton Ronaldo Krzesinski   floppy: remove dr...
4221
4222
  		disks[drive]->queue = blk_init_queue(do_fd_request, &floppy_lock);
  		if (!disks[drive]->queue) {
488211844   Jens Axboe   floppy: switch to...
4223
  			err = -ENOMEM;
b54e1f888   Herton Ronaldo Krzesinski   floppy: don't cal...
4224
  			goto out_put_disk;
488211844   Jens Axboe   floppy: switch to...
4225
  		}
1a4ae43e4   Herton Ronaldo Krzesinski   floppy: remove dr...
4226
4227
4228
4229
4230
  		blk_queue_max_hw_sectors(disks[drive]->queue, 64);
  		disks[drive]->major = FLOPPY_MAJOR;
  		disks[drive]->first_minor = TOMINOR(drive);
  		disks[drive]->fops = &floppy_fops;
  		sprintf(disks[drive]->disk_name, "fd%d", drive);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4231

68219bdf5   Geliang Tang   block: floppy: us...
4232
  		setup_timer(&motor_off_timer[drive], motor_off_callback, drive);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4233
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4234
4235
  	err = register_blkdev(FLOPPY_MAJOR, "fd");
  	if (err)
8ab5e4c15   Greg Kroah-Hartman   [PATCH] devfs: Re...
4236
  		goto out_put_disk;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4237

5e50b9ef9   Ondrej Zary   floppy: fix hiber...
4238
4239
4240
  	err = platform_driver_register(&floppy_driver);
  	if (err)
  		goto out_unreg_blkdev;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4241
4242
4243
4244
4245
4246
4247
4248
  	blk_register_region(MKDEV(FLOPPY_MAJOR, 0), 256, THIS_MODULE,
  			    floppy_find, NULL, NULL);
  
  	for (i = 0; i < 256; i++)
  		if (ITYPE(i))
  			floppy_sizes[i] = floppy_type[ITYPE(i)].size;
  		else
  			floppy_sizes[i] = MAX_DISK_SIZE << 1;
73507e6cd   Joe Perches   drivers/block/flo...
4249
  	reschedule_timeout(MAXTIMEOUT, "floppy init");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4250
4251
4252
4253
  	config_types();
  
  	for (i = 0; i < N_FDC; i++) {
  		fdc = i;
b87c9e0a8   Joe Perches   drivers/block/flo...
4254
  		memset(FDCS, 0, sizeof(*FDCS));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4255
4256
4257
  		FDCS->dtr = -1;
  		FDCS->dor = 0x4;
  #if defined(__sparc__) || defined(__mc68000__)
96534f1dd   Joe Perches   drivers/block/flo...
4258
  	/*sparcs/sun3x don't have a DOR reset which we can fall back on to */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4259
4260
4261
4262
4263
4264
4265
4266
  #ifdef __mc68000__
  		if (MACH_IS_SUN3X)
  #endif
  			FDCS->version = FDC_82072A;
  #endif
  	}
  
  	use_virtual_dma = can_use_virtual_dma & 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4267
4268
  	fdc_state[0].address = FDC1;
  	if (fdc_state[0].address == -1) {
070ad7e79   Jiri Kosina   floppy: convert t...
4269
  		cancel_delayed_work(&fd_timeout);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
  		err = -ENODEV;
  		goto out_unreg_region;
  	}
  #if N_FDC > 1
  	fdc_state[1].address = FDC2;
  #endif
  
  	fdc = 0;		/* reset fdc in case of unexpected interrupt */
  	err = floppy_grab_irq_and_dma();
  	if (err) {
070ad7e79   Jiri Kosina   floppy: convert t...
4280
  		cancel_delayed_work(&fd_timeout);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4281
4282
4283
4284
4285
4286
  		err = -EBUSY;
  		goto out_unreg_region;
  	}
  
  	/* initialise drive state */
  	for (drive = 0; drive < N_DRIVE; drive++) {
b87c9e0a8   Joe Perches   drivers/block/flo...
4287
4288
  		memset(UDRS, 0, sizeof(*UDRS));
  		memset(UDRWE, 0, sizeof(*UDRWE));
e02985361   Joe Perches   drivers/block/flo...
4289
4290
4291
  		set_bit(FD_DISK_NEWCHANGE_BIT, &UDRS->flags);
  		set_bit(FD_DISK_CHANGED_BIT, &UDRS->flags);
  		set_bit(FD_VERIFY_BIT, &UDRS->flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
  		UDRS->fd_device = -1;
  		floppy_track_buffer = NULL;
  		max_buffer_sectors = 0;
  	}
  	/*
  	 * Small 10 msec delay to let through any interrupt that
  	 * initialization might have triggered, to not
  	 * confuse detection:
  	 */
  	msleep(10);
  
  	for (i = 0; i < N_FDC; i++) {
  		fdc = i;
  		FDCS->driver_version = FD_DRIVER_VERSION;
  		for (unit = 0; unit < 4; unit++)
  			FDCS->track[unit] = 0;
  		if (FDCS->address == -1)
  			continue;
  		FDCS->rawcmd = 2;
74f63f469   Joe Perches   drivers/block/flo...
4311
  		if (user_reset_fdc(-1, FD_RESET_ALWAYS, false)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4312
  			/* free ioports reserved by floppy_grab_irq_and_dma() */
5a74db06c   Philippe De Muyter   floppy: request a...
4313
  			floppy_release_regions(fdc);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4314
4315
4316
4317
4318
4319
4320
4321
  			FDCS->address = -1;
  			FDCS->version = FDC_NONE;
  			continue;
  		}
  		/* Try to determine the floppy controller type */
  		FDCS->version = get_fdc_version();
  		if (FDCS->version == FDC_NONE) {
  			/* free ioports reserved by floppy_grab_irq_and_dma() */
5a74db06c   Philippe De Muyter   floppy: request a...
4322
  			floppy_release_regions(fdc);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
  			FDCS->address = -1;
  			continue;
  		}
  		if (can_use_virtual_dma == 2 && FDCS->version < FDC_82072A)
  			can_use_virtual_dma = 0;
  
  		have_no_fdc = 0;
  		/* Not all FDCs seem to be able to handle the version command
  		 * properly, so force a reset for the standard FDC clones,
  		 * to avoid interrupt garbage.
  		 */
74f63f469   Joe Perches   drivers/block/flo...
4334
  		user_reset_fdc(-1, FD_RESET_ALWAYS, false);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4335
4336
  	}
  	fdc = 0;
070ad7e79   Jiri Kosina   floppy: convert t...
4337
  	cancel_delayed_work(&fd_timeout);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4338
  	current_drive = 0;
29f1c7848   Joe Perches   drivers/block/flo...
4339
  	initialized = true;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4340
4341
4342
4343
  	if (have_no_fdc) {
  		DPRINT("no floppy controllers found
  ");
  		err = have_no_fdc;
070ad7e79   Jiri Kosina   floppy: convert t...
4344
  		goto out_release_dma;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4345
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4346
  	for (drive = 0; drive < N_DRIVE; drive++) {
8d3ab4ebf   Herton Ronaldo Krzesinski   floppy: use commo...
4347
  		if (!floppy_available(drive))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4348
  			continue;
94fd0db7b   Hannes Reinecke   [PATCH] Floppy: A...
4349
4350
4351
4352
  
  		floppy_device[drive].name = floppy_device_name;
  		floppy_device[drive].id = drive;
  		floppy_device[drive].dev.release = floppy_device_release;
b7f120b21   Takashi Iwai   floppy: Avoid man...
4353
  		floppy_device[drive].dev.groups = floppy_dev_groups;
94fd0db7b   Hannes Reinecke   [PATCH] Floppy: A...
4354
4355
4356
  
  		err = platform_device_register(&floppy_device[drive]);
  		if (err)
d60e7ec18   Herton Ronaldo Krzesinski   floppy: properly ...
4357
  			goto out_remove_drives;
94fd0db7b   Hannes Reinecke   [PATCH] Floppy: A...
4358

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4359
4360
  		/* to be cleaned up... */
  		disks[drive]->private_data = (void *)(long)drive;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4361
  		disks[drive]->flags |= GENHD_FL_REMOVABLE;
0d52c756a   Dan Williams   block: convert to...
4362
  		device_add_disk(&floppy_device[drive].dev, disks[drive]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4363
4364
4365
  	}
  
  	return 0;
d60e7ec18   Herton Ronaldo Krzesinski   floppy: properly ...
4366
4367
  out_remove_drives:
  	while (drive--) {
8d3ab4ebf   Herton Ronaldo Krzesinski   floppy: use commo...
4368
  		if (floppy_available(drive)) {
d60e7ec18   Herton Ronaldo Krzesinski   floppy: properly ...
4369
  			del_gendisk(disks[drive]);
d60e7ec18   Herton Ronaldo Krzesinski   floppy: properly ...
4370
4371
4372
  			platform_device_unregister(&floppy_device[drive]);
  		}
  	}
070ad7e79   Jiri Kosina   floppy: convert t...
4373
  out_release_dma:
575cfc673   Stephen Hemminger   floppy: use atomi...
4374
  	if (atomic_read(&usage_count))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4375
4376
4377
  		floppy_release_irq_and_dma();
  out_unreg_region:
  	blk_unregister_region(MKDEV(FLOPPY_MAJOR, 0), 256);
5e50b9ef9   Ondrej Zary   floppy: fix hiber...
4378
  	platform_driver_unregister(&floppy_driver);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4379
4380
  out_unreg_blkdev:
  	unregister_blkdev(FLOPPY_MAJOR, "fd");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4381
  out_put_disk:
eac7cc52c   Jiri Kosina   floppy: destroy f...
4382
  	destroy_workqueue(floppy_wq);
1a4ae43e4   Herton Ronaldo Krzesinski   floppy: remove dr...
4383
4384
4385
4386
4387
4388
4389
  	for (drive = 0; drive < N_DRIVE; drive++) {
  		if (!disks[drive])
  			break;
  		if (disks[drive]->queue) {
  			del_timer_sync(&motor_off_timer[drive]);
  			blk_cleanup_queue(disks[drive]->queue);
  			disks[drive]->queue = NULL;
3f9a5aabd   Vivek Goyal   floppy: Cleanup d...
4390
  		}
1a4ae43e4   Herton Ronaldo Krzesinski   floppy: remove dr...
4391
  		put_disk(disks[drive]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4392
4393
4394
  	}
  	return err;
  }
0cc15d03b   Andi Kleen   floppy: Run flopp...
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
  #ifndef MODULE
  static __init void floppy_async_init(void *data, async_cookie_t cookie)
  {
  	do_floppy_init();
  }
  #endif
  
  static int __init floppy_init(void)
  {
  #ifdef MODULE
  	return do_floppy_init();
  #else
  	/* Don't hold up the bootup by the floppy initialization */
  	async_schedule(floppy_async_init, NULL);
  	return 0;
  #endif
  }
5a74db06c   Philippe De Muyter   floppy: request a...
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
  static const struct io_region {
  	int offset;
  	int size;
  } io_regions[] = {
  	{ 2, 1 },
  	/* address + 3 is sometimes reserved by pnp bios for motherboard */
  	{ 4, 2 },
  	/* address + 6 is reserved, and may be taken by IDE.
  	 * Unfortunately, Adaptec doesn't know this :-(, */
  	{ 7, 1 },
  };
  
  static void floppy_release_allocated_regions(int fdc, const struct io_region *p)
  {
  	while (p != io_regions) {
  		p--;
  		release_region(FDCS->address + p->offset, p->size);
  	}
  }
  
  #define ARRAY_END(X) (&((X)[ARRAY_SIZE(X)]))
  
  static int floppy_request_regions(int fdc)
  {
  	const struct io_region *p;
  
  	for (p = io_regions; p < ARRAY_END(io_regions); p++) {
bb57f0c66   Joe Perches   drivers/block/flo...
4439
4440
4441
4442
4443
  		if (!request_region(FDCS->address + p->offset,
  				    p->size, "floppy")) {
  			DPRINT("Floppy io-port 0x%04lx in use
  ",
  			       FDCS->address + p->offset);
5a74db06c   Philippe De Muyter   floppy: request a...
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
  			floppy_release_allocated_regions(fdc, p);
  			return -EBUSY;
  		}
  	}
  	return 0;
  }
  
  static void floppy_release_regions(int fdc)
  {
  	floppy_release_allocated_regions(fdc, ARRAY_END(io_regions));
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4455
4456
  static int floppy_grab_irq_and_dma(void)
  {
575cfc673   Stephen Hemminger   floppy: use atomi...
4457
  	if (atomic_inc_return(&usage_count) > 1)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4458
  		return 0;
6dc659d81   Ingo Molnar   [PATCH] drivers/b...
4459
4460
4461
4462
4463
  
  	/*
  	 * We might have scheduled a free_irq(), wait it to
  	 * drain first:
  	 */
070ad7e79   Jiri Kosina   floppy: convert t...
4464
  	flush_workqueue(floppy_wq);
6dc659d81   Ingo Molnar   [PATCH] drivers/b...
4465

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4466
4467
4468
4469
  	if (fd_request_irq()) {
  		DPRINT("Unable to grab IRQ%d for the floppy driver
  ",
  		       FLOPPY_IRQ);
575cfc673   Stephen Hemminger   floppy: use atomi...
4470
  		atomic_dec(&usage_count);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4471
4472
4473
4474
4475
4476
  		return -1;
  	}
  	if (fd_request_dma()) {
  		DPRINT("Unable to grab DMA%d for the floppy driver
  ",
  		       FLOPPY_DMA);
2e9c47cd4   Jan Beulich   floppy: tolerate ...
4477
4478
4479
4480
  		if (can_use_virtual_dma & 2)
  			use_virtual_dma = can_use_virtual_dma = 1;
  		if (!(can_use_virtual_dma & 1)) {
  			fd_free_irq();
575cfc673   Stephen Hemminger   floppy: use atomi...
4481
  			atomic_dec(&usage_count);
2e9c47cd4   Jan Beulich   floppy: tolerate ...
4482
4483
  			return -1;
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4484
4485
4486
4487
  	}
  
  	for (fdc = 0; fdc < N_FDC; fdc++) {
  		if (FDCS->address != -1) {
5a74db06c   Philippe De Muyter   floppy: request a...
4488
4489
  			if (floppy_request_regions(fdc))
  				goto cleanup;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
  		}
  	}
  	for (fdc = 0; fdc < N_FDC; fdc++) {
  		if (FDCS->address != -1) {
  			reset_fdc_info(1);
  			fd_outb(FDCS->dor, FD_DOR);
  		}
  	}
  	fdc = 0;
  	set_dor(0, ~0, 8);	/* avoid immediate interrupt */
  
  	for (fdc = 0; fdc < N_FDC; fdc++)
  		if (FDCS->address != -1)
  			fd_outb(FDCS->dor, FD_DOR);
  	/*
06f748c47   Jesper Juhl   floppy: do a very...
4505
4506
  	 * The driver will try and free resources and relies on us
  	 * to know if they were allocated or not.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4507
4508
4509
4510
  	 */
  	fdc = 0;
  	irqdma_allocated = 1;
  	return 0;
5a74db06c   Philippe De Muyter   floppy: request a...
4511
  cleanup:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4512
4513
  	fd_free_irq();
  	fd_free_dma();
5a74db06c   Philippe De Muyter   floppy: request a...
4514
4515
  	while (--fdc >= 0)
  		floppy_release_regions(fdc);
575cfc673   Stephen Hemminger   floppy: use atomi...
4516
  	atomic_dec(&usage_count);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4517
4518
4519
4520
4521
4522
  	return -1;
  }
  
  static void floppy_release_irq_and_dma(void)
  {
  	int old_fdc;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4523
4524
4525
  #ifndef __sparc__
  	int drive;
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4526
4527
  	long tmpsize;
  	unsigned long tmpaddr;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4528

575cfc673   Stephen Hemminger   floppy: use atomi...
4529
  	if (!atomic_dec_and_test(&usage_count))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4530
  		return;
575cfc673   Stephen Hemminger   floppy: use atomi...
4531

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4532
4533
4534
  	if (irqdma_allocated) {
  		fd_disable_dma();
  		fd_free_dma();
3e541a4ae   Ingo Molnar   [PATCH] lockdep: ...
4535
  		fd_free_irq();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4536
4537
4538
4539
4540
4541
  		irqdma_allocated = 0;
  	}
  	set_dor(0, ~0, 8);
  #if N_FDC > 1
  	set_dor(1, ~8, 0);
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4542
4543
4544
4545
4546
4547
4548
4549
4550
  
  	if (floppy_track_buffer && max_buffer_sectors) {
  		tmpsize = max_buffer_sectors * 1024;
  		tmpaddr = (unsigned long)floppy_track_buffer;
  		floppy_track_buffer = NULL;
  		max_buffer_sectors = 0;
  		buffer_min = buffer_max = -1;
  		fd_dma_mem_free(tmpaddr, tmpsize);
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4551
4552
4553
  #ifndef __sparc__
  	for (drive = 0; drive < N_FDC * 4; drive++)
  		if (timer_pending(motor_off_timer + drive))
b46df356d   Joe Perches   drivers/block/flo...
4554
4555
  			pr_info("motor off timer %d still active
  ", drive);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4556
  #endif
070ad7e79   Jiri Kosina   floppy: convert t...
4557
  	if (delayed_work_pending(&fd_timeout))
b46df356d   Joe Perches   drivers/block/flo...
4558
4559
  		pr_info("floppy timer still active:%s
  ", timeout_message);
070ad7e79   Jiri Kosina   floppy: convert t...
4560
  	if (delayed_work_pending(&fd_timer))
b46df356d   Joe Perches   drivers/block/flo...
4561
4562
  		pr_info("auxiliary floppy timer still active
  ");
365970a1e   David Howells   WorkStruct: Merge...
4563
  	if (work_pending(&floppy_work))
b46df356d   Joe Perches   drivers/block/flo...
4564
4565
  		pr_info("work still pending
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4566
4567
  	old_fdc = fdc;
  	for (fdc = 0; fdc < N_FDC; fdc++)
5a74db06c   Philippe De Muyter   floppy: request a...
4568
4569
  		if (FDCS->address != -1)
  			floppy_release_regions(fdc);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4570
4571
4572
4573
4574
4575
  	fdc = old_fdc;
  }
  
  #ifdef MODULE
  
  static char *floppy;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4576
4577
4578
4579
4580
  static void __init parse_floppy_cfg_string(char *cfg)
  {
  	char *ptr;
  
  	while (*cfg) {
bb57f0c66   Joe Perches   drivers/block/flo...
4581
4582
4583
  		ptr = cfg;
  		while (*cfg && *cfg != ' ' && *cfg != '\t')
  			cfg++;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4584
4585
4586
4587
4588
4589
4590
4591
  		if (*cfg) {
  			*cfg = '\0';
  			cfg++;
  		}
  		if (*ptr)
  			floppy_setup(ptr);
  	}
  }
7afea3bcb   Jon Schindler   drivers/block/flo...
4592
  static int __init floppy_module_init(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4593
4594
4595
4596
4597
  {
  	if (floppy)
  		parse_floppy_cfg_string(floppy);
  	return floppy_init();
  }
7afea3bcb   Jon Schindler   drivers/block/flo...
4598
  module_init(floppy_module_init);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4599

7afea3bcb   Jon Schindler   drivers/block/flo...
4600
  static void __exit floppy_module_exit(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4601
4602
  {
  	int drive;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4603
4604
  	blk_unregister_region(MKDEV(FLOPPY_MAJOR, 0), 256);
  	unregister_blkdev(FLOPPY_MAJOR, "fd");
5e50b9ef9   Ondrej Zary   floppy: fix hiber...
4605
  	platform_driver_unregister(&floppy_driver);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4606

eac7cc52c   Jiri Kosina   floppy: destroy f...
4607
  	destroy_workqueue(floppy_wq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4608
4609
  	for (drive = 0; drive < N_DRIVE; drive++) {
  		del_timer_sync(&motor_off_timer[drive]);
8d3ab4ebf   Herton Ronaldo Krzesinski   floppy: use commo...
4610
  		if (floppy_available(drive)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4611
  			del_gendisk(disks[drive]);
94fd0db7b   Hannes Reinecke   [PATCH] Floppy: A...
4612
  			platform_device_unregister(&floppy_device[drive]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4613
  		}
488211844   Jens Axboe   floppy: switch to...
4614
  		blk_cleanup_queue(disks[drive]->queue);
4609dff6b   Vivek Goyal   floppy: Fix a cra...
4615
4616
4617
4618
4619
4620
4621
4622
  
  		/*
  		 * These disks have not called add_disk().  Don't put down
  		 * queue reference in put_disk().
  		 */
  		if (!(allowed_drive_mask & (1 << drive)) ||
  		    fdc_state[FDC(drive)].version == FDC_NONE)
  			disks[drive]->queue = NULL;
d017bf6b4   Vivek Goyal   floppy: fix anoth...
4623
  		put_disk(disks[drive]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4624
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4625

070ad7e79   Jiri Kosina   floppy: convert t...
4626
4627
  	cancel_delayed_work_sync(&fd_timeout);
  	cancel_delayed_work_sync(&fd_timer);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4628

575cfc673   Stephen Hemminger   floppy: use atomi...
4629
  	if (atomic_read(&usage_count))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4630
4631
4632
4633
  		floppy_release_irq_and_dma();
  
  	/* eject disk, if any */
  	fd_eject(0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4634
  }
48c8cee61   Joe Perches   drivers/block/flo...
4635

7afea3bcb   Jon Schindler   drivers/block/flo...
4636
  module_exit(floppy_module_exit);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4637
4638
4639
4640
4641
4642
4643
  
  module_param(floppy, charp, 0);
  module_param(FLOPPY_IRQ, int, 0);
  module_param(FLOPPY_DMA, int, 0);
  MODULE_AUTHOR("Alain L. Knaff");
  MODULE_SUPPORTED_DEVICE("fd");
  MODULE_LICENSE("GPL");
83f9ef463   Scott James Remnant   floppy: provide a...
4644
4645
  /* This doesn't actually get used other than for module information */
  static const struct pnp_device_id floppy_pnpids[] = {
48c8cee61   Joe Perches   drivers/block/flo...
4646
4647
  	{"PNP0700", 0},
  	{}
83f9ef463   Scott James Remnant   floppy: provide a...
4648
  };
48c8cee61   Joe Perches   drivers/block/flo...
4649

83f9ef463   Scott James Remnant   floppy: provide a...
4650
  MODULE_DEVICE_TABLE(pnp, floppy_pnpids);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4651
4652
4653
4654
4655
4656
4657
  #else
  
  __setup("floppy=", floppy_setup);
  module_init(floppy_init)
  #endif
  
  MODULE_ALIAS_BLOCKDEV_MAJOR(FLOPPY_MAJOR);