OpenCores
URL https://opencores.org/ocsvn/or1k_old/or1k_old/trunk

Subversion Repositories or1k_old

[/] [or1k_old/] [trunk/] [rc203soc/] [sw/] [uClinux/] [arch/] [armnommu/] [drivers/] [block/] [floppy.c] - Blame information for rev 1765

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1622 jcastillo
/*
2
 *  linux/arch/arm/drivers/block/floppy.c
3
 *  [ was linux/drivers/block/floppy.c ]
4
 *
5
 *  Copyright (C) 1991, 1992  Linus Torvalds
6
 *  Copyright (C) 1993, 1994  Alain Knaff
7
 *  Modifications Copyright (C) 1995 Russell King
8
 */
9
/*
10
 * 02.12.91 - Changed to static variables to indicate need for reset
11
 * and recalibrate. This makes some things easier (output_byte reset
12
 * checking etc), and means less interrupt jumping in case of errors,
13
 * so the code is hopefully easier to understand.
14
 */
15
 
16
/*
17
 * This file is certainly a mess. I've tried my best to get it working,
18
 * but I don't like programming floppies, and I have only one anyway.
19
 * Urgel. I should check for more errors, and do more graceful error
20
 * recovery. Seems there are problems with several drives. I've tried to
21
 * correct them. No promises.
22
 */
23
 
24
/*
25
 * As with hd.c, all routines within this file can (and will) be called
26
 * by interrupts, so extreme caution is needed. A hardware interrupt
27
 * handler may not sleep, or a kernel panic will happen. Thus I cannot
28
 * call "floppy-on" directly, but have to set a special timer interrupt
29
 * etc.
30
 */
31
 
32
/*
33
 * 28.02.92 - made track-buffering routines, based on the routines written
34
 * by entropy@wintermute.wpi.edu (Lawrence Foard). Linus.
35
 */
36
 
37
/*
38
 * Automatic floppy-detection and formatting written by Werner Almesberger
39
 * (almesber@nessie.cs.id.ethz.ch), who also corrected some problems with
40
 * the floppy-change signal detection.
41
 */
42
 
43
/*
44
 * 1992/7/22 -- Hennus Bergman: Added better error reporting, fixed
45
 * FDC data overrun bug, added some preliminary stuff for vertical
46
 * recording support.
47
 *
48
 * 1992/9/17: Added DMA allocation & DMA functions. -- hhb.
49
 *
50
 * TODO: Errors are still not counted properly.
51
 */
52
 
53
/* 1992/9/20
54
 * Modifications for ``Sector Shifting'' by Rob Hooft (hooft@chem.ruu.nl)
55
 * modeled after the freeware MS-DOS program fdformat/88 V1.8 by
56
 * Christoph H. Hochst\"atter.
57
 * I have fixed the shift values to the ones I always use. Maybe a new
58
 * ioctl() should be created to be able to modify them.
59
 * There is a bug in the driver that makes it impossible to format a
60
 * floppy as the first thing after bootup.
61
 */
62
 
63
/*
64
 * 1993/4/29 -- Linus -- cleaned up the timer handling in the kernel, and
65
 * this helped the floppy driver as well. Much cleaner, and still seems to
66
 * work.
67
 */
68
 
69
/* 1994/6/24 --bbroad-- added the floppy table entries and made
70
 * minor modifications to allow 2.88 floppies to be run.
71
 */
72
 
73
/* 1994/7/13 -- Paul Vojta -- modified the probing code to allow three or more
74
 * disk types.
75
 */
76
 
77
/*
78
 * 1994/8/8 -- Alain Knaff -- Switched to fdpatch driver: Support for bigger
79
 * format bug fixes, but unfortunately some new bugs too...
80
 */
81
 
82
/* 1994/9/17 -- Koen Holtman -- added logging of physical floppy write
83
 * errors to allow safe writing by specialized programs.
84
 */
85
 
86
/* 1995/4/24 -- Dan Fandrich -- added support for Commodore 1581 3.5" disks
87
 * by defining bit 1 of the "stretch" parameter to mean put sectors on the
88
 * opposite side of the disk, leaving the sector IDs alone (i.e. Commodore's
89
 * drives are "upside-down").
90
 */
91
 
92
/*
93
 * 1995/8/26 -- Andreas Busse -- added Mips support.
94
 */
95
 
96
/*
97
 * 1995/8/16 -- Russell King -- added ARM support.
98
 */
99
 
100
/*
101
 * 1995/10/18 -- Ralf Baechle -- Portability cleanup; move machine dependent
102
 * features to asm/floppy.h.
103
 */
104
 
105
 
106
#define FLOPPY_SANITY_CHECK
107
#undef  FLOPPY_SILENT_DCL_CLEAR
108
 
109
#define REALLY_SLOW_IO
110
 
111
#define DEBUGT 2
112
#define DCL_DEBUG /* debug disk change line */
113
 
114
/* do print messages for unexpected interrupts */
115
static int print_unex=1;
116
#include <linux/utsname.h>
117
#include <linux/module.h>
118
#include <linux/sched.h>
119
#include <linux/fs.h>
120
#include <linux/kernel.h>
121
#include <linux/timer.h>
122
#include <linux/tqueue.h>
123
#define FDPATCHES
124
#include <linux/fdreg.h>
125
#include <linux/fd.h>
126
 
127
#define OLDFDRAWCMD 0x020d /* send a raw command to the FDC */
128
 
129
struct old_floppy_raw_cmd {
130
  void *data;
131
  long length;
132
 
133
  unsigned char rate;
134
  unsigned char flags;
135
  unsigned char cmd_count;
136
  unsigned char cmd[9];
137
  unsigned char reply_count;
138
  unsigned char reply[7];
139
  int track;
140
};
141
 
142
#include <linux/errno.h>
143
#include <linux/malloc.h>
144
#include <linux/mm.h>
145
#include <linux/string.h>
146
#include <linux/fcntl.h>
147
#include <linux/delay.h>
148
#ifndef CONFIG_ARM
149
#include <linux/mc146818rtc.h> /* CMOS defines */
150
#endif
151
#include <linux/ioport.h>
152
#include <linux/interrupt.h>
153
 
154
#include <asm/dma.h>
155
#include <asm/irq.h>
156
#include <asm/system.h>
157
#include <asm/io.h>
158
#include <asm/segment.h>
159
 
160
/* the following is the mask of allowed drives. By default units 2 and
161
 * 3 of both floppy controllers are disabled, because switching on the
162
 * motor of these drives causes system hangs on some PCI computers. drive
163
 * 0 is the low bit (0x1), and drive 7 is the high bit (0x80). Bits are on if
164
 * a drive is allowed. */
165
static int FLOPPY_IRQ=IRQ_FLOPPYDISK;
166
static int FLOPPY_DMA=DMA_FLOPPY;
167
static int allowed_drive_mask = 0x33;
168
 
169
static int use_virtual_dma=0; /* virtual DMA for Intel */
170
static unsigned short virtual_dma_port=0x3f0;
171
void floppy_interrupt(int irq, void *dev_id, struct pt_regs * regs);
172
static int set_dor(int fdc, char mask, char data);
173
static inline int __get_order(unsigned long size);
174
#include <asm/floppy.h>
175
 
176
 
177
#define MAJOR_NR FLOPPY_MAJOR
178
 
179
#include <linux/blk.h>
180
#include <linux/cdrom.h> /* for the compatibility eject ioctl */
181
 
182
 
183
#ifndef FLOPPY_MOTOR_MASK
184
#define FLOPPY_MOTOR_MASK 0xf0
185
#endif
186
 
187
#ifndef fd_get_dma_residue
188
#define fd_get_dma_residue() get_dma_residue(FLOPPY_DMA)
189
#endif
190
 
191
/* Dma Memory related stuff */
192
 
193
/* Pure 2^n version of get_order */
194
static inline int __get_order(unsigned long size)
195
{
196
        int order;
197
 
198
        size = (size-1) >> (PAGE_SHIFT-1);
199
        order = -1;
200
        do {
201
                size >>= 1;
202
                order++;
203
        } while (size);
204
        return order;
205
}
206
 
207
#ifndef fd_dma_mem_free
208
#define fd_dma_mem_free(addr, size) free_pages(addr, __get_order(size))
209
#endif
210
 
211
#ifndef fd_dma_mem_alloc
212
#define fd_dma_mem_alloc(size) __get_dma_pages(GFP_KERNEL,__get_order(size))
213
#endif
214
 
215
/* End dma memory related stuff */
216
 
217
static unsigned int fake_change = 0;
218
static int initialising=1;
219
 
220
static inline int TYPE(kdev_t x) {
221
        return  (MINOR(x)>>2) & 0x1f;
222
}
223
static inline int DRIVE(kdev_t x) {
224
        return (MINOR(x)&0x03) | ((MINOR(x)&0x80) >> 5);
225
}
226
#define ITYPE(x) (((x)>>2) & 0x1f)
227
#define TOMINOR(x) ((x & 3) | ((x & 4) << 5))
228
#define UNIT(x) ((x) & 0x03)            /* drive on fdc */
229
#define FDC(x) (((x) & 0x04) >> 2)  /* fdc of drive */
230
#define REVDRIVE(fdc, unit) ((unit) + ((fdc) << 2))
231
                                /* reverse mapping from unit and fdc to drive */
232
#define DP (&drive_params[current_drive])
233
#define DRS (&drive_state[current_drive])
234
#define DRWE (&write_errors[current_drive])
235
#define FDCS (&fdc_state[fdc])
236
#define CLEARF(x) (clear_bit(x##_BIT, &DRS->flags))
237
#define SETF(x) (set_bit(x##_BIT, &DRS->flags))
238
#define TESTF(x) (test_bit(x##_BIT, &DRS->flags))
239
 
240
#define UDP (&drive_params[drive])
241
#define UDRS (&drive_state[drive])
242
#define UDRWE (&write_errors[drive])
243
#define UFDCS (&fdc_state[FDC(drive)])
244
#define UCLEARF(x) (clear_bit(x##_BIT, &UDRS->flags))
245
#define USETF(x) (set_bit(x##_BIT, &UDRS->flags))
246
#define UTESTF(x) (test_bit(x##_BIT, &UDRS->flags))
247
 
248
#define DPRINT(format, args...) printk(DEVICE_NAME "%d: " format, current_drive , ## args)
249
 
250
#define PH_HEAD(floppy,head) (((((floppy)->stretch & 2) >>1) ^ head) << 2)
251
#define STRETCH(floppy) ((floppy)->stretch & FD_STRETCH)
252
 
253
#define CLEARSTRUCT(x) memset((x), 0, sizeof(*(x)))
254
 
255
#define INT_OFF save_flags(flags); cli()
256
#define INT_ON  restore_flags(flags)
257
 
258
/* read/write */
259
#define COMMAND raw_cmd->cmd[0]
260
#define DR_SELECT raw_cmd->cmd[1]
261
#define TRACK raw_cmd->cmd[2]
262
#define HEAD raw_cmd->cmd[3]
263
#define SECTOR raw_cmd->cmd[4]
264
#define SIZECODE raw_cmd->cmd[5]
265
#define SECT_PER_TRACK raw_cmd->cmd[6]
266
#define GAP raw_cmd->cmd[7]
267
#define SIZECODE2 raw_cmd->cmd[8]
268
#define NR_RW 9
269
 
270
/* format */
271
#define F_SIZECODE raw_cmd->cmd[2]
272
#define F_SECT_PER_TRACK raw_cmd->cmd[3]
273
#define F_GAP raw_cmd->cmd[4]
274
#define F_FILL raw_cmd->cmd[5]
275
#define NR_F 6
276
 
277
/*
278
 * Maximum disk size (in kilobytes). This default is used whenever the
279
 * current disk size is unknown.
280
 * [Now it is rather a minimum]
281
 */
282
#define MAX_DISK_SIZE 4 /* 3984*/
283
 
284
#define K_64    0x10000         /* 64KB */
285
 
286
/*
287
 * globals used by 'result()'
288
 */
289
#define MAX_REPLIES 16
290
static unsigned char reply_buffer[MAX_REPLIES];
291
static int inr; /* size of reply buffer, when called from interrupt */
292
#define ST0 (reply_buffer[0])
293
#define ST1 (reply_buffer[1])
294
#define ST2 (reply_buffer[2])
295
#define ST3 (reply_buffer[0]) /* result of GETSTATUS */
296
#define R_TRACK (reply_buffer[3])
297
#define R_HEAD (reply_buffer[4])
298
#define R_SECTOR (reply_buffer[5])
299
#define R_SIZECODE (reply_buffer[6])
300
 
301
#define SEL_DLY (2*HZ/100)
302
 
303
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
304
/*
305
 * this struct defines the different floppy drive types.
306
 */
307
static struct {
308
        struct floppy_drive_params params;
309
        const char *name; /* name printed while booting */
310
} default_drive_params[]= {
311
/* NOTE: the time values in jiffies should be in msec!
312
 CMOS drive type
313
  |     Maximum data rate supported by drive type
314
  |     |   Head load time, msec
315
  |     |   |   Head unload time, msec (not used)
316
  |     |   |   |     Step rate interval, usec
317
  |     |   |   |     |       Time needed for spinup time (jiffies)
318
  |     |   |   |     |       |      Timeout for spinning down (jiffies)
319
  |     |   |   |     |       |      |   Spindown offset (where disk stops)
320
  |     |   |   |     |       |      |   |     Select delay
321
  |     |   |   |     |       |      |   |     |     RPS
322
  |     |   |   |     |       |      |   |     |     |    Max number of tracks
323
  |     |   |   |     |       |      |   |     |     |    |     Interrupt timeout
324
  |     |   |   |     |       |      |   |     |     |    |     |   Max nonintlv. sectors
325
  |     |   |   |     |       |      |   |     |     |    |     |   | -Max Errors- flags */
326
{{0,  500, 16, 16, 8000,    1*HZ, 3*HZ,  0, SEL_DLY, 5,  80, 3*HZ, 20, {3,1,2,0,2}, 0,
327
      0, { 7, 4, 8, 2, 1, 5, 3,10}, 3*HZ/2, 0 }, "unknown" },
328
 
329
{{1,  300, 16, 16, 8000,    1*HZ, 3*HZ,  0, SEL_DLY, 5,  40, 3*HZ, 17, {3,1,2,0,2}, 0,
330
      0, { 1, 0, 0, 0, 0, 0, 0, 0}, 3*HZ/2, 1 }, "360K PC" }, /*5 1/4 360 KB PC*/
331
 
332
{{2,  500, 16, 16, 6000, 4*HZ/10, 3*HZ, 14, SEL_DLY, 6,  83, 3*HZ, 17, {3,1,2,0,2}, 0,
333
      0, { 2, 5, 6,23,10,20,11, 0}, 3*HZ/2, 2 }, "1.2M" }, /*5 1/4 HD AT*/
334
 
335
{{3,  250, 16, 16, 3000,    1*HZ, 3*HZ,  0, SEL_DLY, 5,  83, 3*HZ, 20, {3,1,2,0,2}, 0,
336
      0, { 4,22,21,30, 3, 0, 0, 0}, 3*HZ/2, 4 }, "720k" }, /*3 1/2 DD*/
337
 
338
{{4,  500, 16, 16, 4000, 4*HZ/10, 3*HZ, 10, SEL_DLY, 5,  83, 3*HZ, 20, {3,1,2,0,2}, 0,
339
      0, { 7, 4,25,22,31,21,29,11}, 3*HZ/2, 7 }, "1.44M" }, /*3 1/2 HD*/
340
 
341
{{5, 1000, 15,  8, 3000, 4*HZ/10, 3*HZ, 10, SEL_DLY, 5,  83, 3*HZ, 40, {3,1,2,0,2}, 0,
342
      0, { 7, 8, 4,25,28,22,31,21}, 3*HZ/2, 8 }, "2.88M AMI BIOS" }, /*3 1/2 ED*/
343
 
344
{{6, 1000, 15,  8, 3000, 4*HZ/10, 3*HZ, 10, SEL_DLY, 5,  83, 3*HZ, 40, {3,1,2,0,2}, 0,
345
      0, { 7, 8, 4,25,28,22,31,21}, 3*HZ/2, 8 }, "2.88M" } /*3 1/2 ED*/
346
/*    |  --autodetected formats---    |      |      |
347
 *    read_track                      |      |    Name printed when booting
348
 *                                    |     Native format
349
 *                  Frequency of disk change checks */
350
};
351
 
352
static struct floppy_drive_params drive_params[N_DRIVE];
353
static struct floppy_drive_struct drive_state[N_DRIVE];
354
static struct floppy_write_errors write_errors[N_DRIVE];
355
static struct floppy_raw_cmd *raw_cmd, default_raw_cmd;
356
 
357
/*
358
 * This struct defines the different floppy types.
359
 *
360
 * Bit 0 of 'stretch' tells if the tracks need to be doubled for some
361
 * types (e.g. 360kB diskette in 1.2MB drive, etc.).  Bit 1 of 'stretch'
362
 * tells if the disk is in Commodore 1581 format, which means side 0 sectors
363
 * are located on side 1 of the disk but with a side 0 ID, and vice-versa.
364
 * This is the same as the Sharp MZ-80 5.25" CP/M disk format, except that the
365
 * 1581's logical side 0 is on physical side 1, whereas the Sharp's logical
366
 * side 0 is on physical side 0 (but with the misnamed sector IDs).
367
 * 'stretch' should probably be renamed to something more general, like
368
 * 'options'.  Other parameters should be self-explanatory (see also
369
 * setfdprm(8)).
370
 */
371
/*
372
            Size
373
             |  Sectors per track
374
             |  | Head
375
             |  | |  Tracks
376
             |  | |  | Stretch
377
             |  | |  | |  Gap 1 size
378
             |  | |  | |    |  Data rate, | 0x40 for perp
379
             |  | |  | |    |    |  Spec1 (stepping rate, head unload
380
             |  | |  | |    |    |    |    /fmt gap (gap2) */
381
static struct floppy_struct floppy_type[32] = {
382
        {    0, 0,0, 0,0,0x00,0x00,0x00,0x00,NULL    },      /*  0 no testing    */
383
        {  720, 9,2,40,0,0x2A,0x02,0xDF,0x50,"d360"  }, /*  1 360KB PC      */
384
        { 2400,15,2,80,0,0x1B,0x00,0xDF,0x54,"h1200" },  /*  2 1.2MB AT      */
385
        {  720, 9,1,80,0,0x2A,0x02,0xDF,0x50,"D360"  },  /*  3 360KB SS 3.5" */
386
        { 1440, 9,2,80,0,0x2A,0x02,0xDF,0x50,"D720"  },  /*  4 720KB 3.5"    */
387
        {  720, 9,2,40,1,0x23,0x01,0xDF,0x50,"h360"  }, /*  5 360KB AT      */
388
        { 1440, 9,2,80,0,0x23,0x01,0xDF,0x50,"h720"  },  /*  6 720KB AT      */
389
        { 2880,18,2,80,0,0x1B,0x00,0xCF,0x6C,"H1440" },  /*  7 1.44MB 3.5"   */
390
        { 5760,36,2,80,0,0x1B,0x43,0xAF,0x54,"E2880" },  /*  8 2.88MB 3.5"   */
391
        { 6240,39,2,80,0,0x1B,0x43,0xAF,0x28,"E3120" },  /*  9 3.12MB 3.5"   */
392
 
393
        { 2880,18,2,80,0,0x25,0x00,0xDF,0x02,"h1440" }, /* 10 1.44MB 5.25"  */
394
        { 3360,21,2,80,0,0x1C,0x00,0xCF,0x0C,"H1680" }, /* 11 1.68MB 3.5"   */
395
        {  820,10,2,41,1,0x25,0x01,0xDF,0x2E,"h410"  }, /* 12 410KB 5.25"   */
396
        { 1640,10,2,82,0,0x25,0x02,0xDF,0x2E,"H820"  },  /* 13 820KB 3.5"    */
397
        { 2952,18,2,82,0,0x25,0x00,0xDF,0x02,"h1476" },  /* 14 1.48MB 5.25"  */
398
        { 3444,21,2,82,0,0x25,0x00,0xDF,0x0C,"H1722" },  /* 15 1.72MB 3.5"   */
399
        {  840,10,2,42,1,0x25,0x01,0xDF,0x2E,"h420"  }, /* 16 420KB 5.25"   */
400
        { 1660,10,2,83,0,0x25,0x02,0xDF,0x2E,"H830"  },  /* 17 830KB 3.5"    */
401
        { 2988,18,2,83,0,0x25,0x00,0xDF,0x02,"h1494" },  /* 18 1.49MB 5.25"  */
402
        { 3486,21,2,83,0,0x25,0x00,0xDF,0x0C,"H1743" }, /* 19 1.74 MB 3.5"  */
403
 
404
        { 1760,11,2,80,0,0x1C,0x09,0xCF,0x00,"h880"  }, /* 20 880KB 5.25"   */
405
        { 2080,13,2,80,0,0x1C,0x01,0xCF,0x00,"D1040" }, /* 21 1.04MB 3.5"   */
406
        { 2240,14,2,80,0,0x1C,0x19,0xCF,0x00,"D1120" }, /* 22 1.12MB 3.5"   */
407
        { 3200,20,2,80,0,0x1C,0x20,0xCF,0x2C,"h1600" }, /* 23 1.6MB 5.25"   */
408
        { 3520,22,2,80,0,0x1C,0x08,0xCF,0x2e,"H1760" }, /* 24 1.76MB 3.5"   */
409
        { 3840,24,2,80,0,0x1C,0x20,0xCF,0x00,"H1920" }, /* 25 1.92MB 3.5"   */
410
        { 6400,40,2,80,0,0x25,0x5B,0xCF,0x00,"E3200" }, /* 26 3.20MB 3.5"   */
411
        { 7040,44,2,80,0,0x25,0x5B,0xCF,0x00,"E3520" }, /* 27 3.52MB 3.5"   */
412
        { 7680,48,2,80,0,0x25,0x63,0xCF,0x00,"E3840" }, /* 28 3.84MB 3.5"   */
413
 
414
        { 3680,23,2,80,0,0x1C,0x10,0xCF,0x00,"H1840" }, /* 29 1.84MB 3.5"   */
415
        { 1600,10,2,80,0,0x25,0x02,0xDF,0x2E,"D800"  },  /* 30 800KB 3.5"    */
416
        { 3200,20,2,80,0,0x1C,0x00,0xCF,0x2C,"H1600" }, /* 31 1.6MB 3.5"    */
417
};
418
 
419
#define NUMBER(x)       (sizeof(x) / sizeof(*(x)))
420
#define SECTSIZE (_FD_SECTSIZE(*floppy))
421
 
422
/* Auto-detection: Disk type used until the next media change occurs. */
423
static struct floppy_struct *current_type[N_DRIVE] = {
424
        NULL, NULL, NULL, NULL,
425
        NULL, NULL, NULL, NULL
426
};
427
 
428
/*
429
 * User-provided type information. current_type points to
430
 * the respective entry of this array.
431
 */
432
static struct floppy_struct user_params[N_DRIVE];
433
 
434
static int floppy_sizes[256];
435
static int floppy_blocksizes[256] = { 0, };
436
 
437
/*
438
 * The driver is trying to determine the correct media format
439
 * while probing is set. rw_interrupt() clears it after a
440
 * successful access.
441
 */
442
static int probing = 0;
443
 
444
/* Synchronization of FDC access. */
445
#define FD_COMMAND_NONE -1
446
#define FD_COMMAND_ERROR 2
447
#define FD_COMMAND_OKAY 3
448
 
449
static volatile int command_status = FD_COMMAND_NONE, fdc_busy = 0;
450
static struct wait_queue *fdc_wait = NULL, *command_done = NULL;
451
#define NO_SIGNAL (!(current->signal & ~current->blocked) || !interruptible)
452
#define CALL(x) if ((x) == -EINTR) return -EINTR
453
#define ECALL(x) if ((ret = (x))) return ret;
454
#define _WAIT(x,i) CALL(ret=wait_til_done((x),i))
455
#define WAIT(x) _WAIT((x),interruptible)
456
#define IWAIT(x) _WAIT((x),1)
457
 
458
/* Errors during formatting are counted here. */
459
static int format_errors;
460
 
461
/* Format request descriptor. */
462
static struct format_descr format_req;
463
 
464
/*
465
 * Rate is 0 for 500kb/s, 1 for 300kbps, 2 for 250kbps
466
 * Spec1 is 0xSH, where S is stepping rate (F=1ms, E=2ms, D=3ms etc),
467
 * H is head unload time (1=16ms, 2=32ms, etc)
468
 */
469
 
470
/*
471
 * Track buffer
472
 * Because these are written to by the DMA controller, they must
473
 * not contain a 64k byte boundary crossing, or data will be
474
 * corrupted/lost.
475
 */
476
static char *floppy_track_buffer=NULL;
477
static int max_buffer_sectors=0;
478
 
479
static int *errors;
480
typedef void (*done_f)(int);
481
static struct cont_t {
482
        void (*interrupt)(void); /* this is called after the interrupt of the
483
                                  * main command */
484
        void (*redo)(void); /* this is called to retry the operation */
485
        void (*error)(void); /* this is called to tally an error */
486
        done_f done; /* this is called to say if the operation has
487
                      * succeeded/failed */
488
} *cont=NULL;
489
 
490
static void floppy_ready(void);
491
static void floppy_start(void);
492
static void process_fd_request(void);
493
static void recalibrate_floppy(void);
494
static void floppy_shutdown(void);
495
 
496
static int floppy_grab_irq_and_dma(void);
497
static void floppy_release_irq_and_dma(void);
498
 
499
/*
500
 * The "reset" variable should be tested whenever an interrupt is scheduled,
501
 * after the commands have been sent. This is to ensure that the driver doesn't
502
 * get wedged when the interrupt doesn't come because of a failed command.
503
 * reset doesn't need to be tested before sending commands, because
504
 * output_byte is automatically disabled when reset is set.
505
 */
506
#define CHECK_RESET { if (FDCS->reset){ reset_fdc(); return; } }
507
static void reset_fdc(void);
508
 
509
/*
510
 * These are global variables, as that's the easiest way to give
511
 * information to interrupts. They are the data used for the current
512
 * request.
513
 */
514
#define NO_TRACK -1
515
#define NEED_1_RECAL -2
516
#define NEED_2_RECAL -3
517
 
518
/* */
519
static int usage_count = 0;
520
 
521
 
522
/* buffer related variables */
523
static int buffer_track = -1;
524
static int buffer_drive = -1;
525
static int buffer_min = -1;
526
static int buffer_max = -1;
527
 
528
/* fdc related variables, should end up in a struct */
529
static struct floppy_fdc_state fdc_state[N_FDC];
530
static int fdc; /* current fdc */
531
 
532
static struct floppy_struct *_floppy = floppy_type;
533
static unsigned char current_drive = 0;
534
static long current_count_sectors = 0;
535
static unsigned char sector_t; /* sector in track */
536
 
537
#ifndef fd_eject
538
#define fd_eject(x) -EINVAL
539
#endif
540
 
541
#ifdef DEBUGT
542
static long unsigned debugtimer;
543
#endif
544
 
545
/*
546
 * Debugging
547
 * =========
548
 */
549
static inline void set_debugt(void)
550
{
551
#ifdef DEBUGT
552
        debugtimer = jiffies;
553
#endif
554
}
555
 
556
static inline void debugt(const char *message)
557
{
558
#ifdef DEBUGT
559
        if (DP->flags & DEBUGT)
560
                printk("%s dtime=%lu\n", message, jiffies-debugtimer);
561
#endif
562
}
563
 
564
typedef void (*timeout_fn)(unsigned long);
565
static struct timer_list fd_timeout ={ NULL, NULL, 0, 0,
566
                                       (timeout_fn) floppy_shutdown };
567
 
568
static const char *timeout_message;
569
 
570
#ifdef FLOPPY_SANITY_CHECK
571
static void is_alive(const char *message)
572
{
573
        /* this routine checks whether the floppy driver is "alive" */
574
        if (fdc_busy && command_status < 2 && !fd_timeout.prev){
575
                DPRINT("timeout handler died: %s\n",message);
576
        }
577
}
578
#endif
579
 
580
#ifdef FLOPPY_SANITY_CHECK
581
 
582
#define OLOGSIZE 20
583
 
584
static void (*lasthandler)(void) = NULL;
585
static int interruptjiffies=0;
586
static int resultjiffies=0;
587
static int resultsize=0;
588
static int lastredo=0;
589
 
590
static struct output_log {
591
        unsigned char data;
592
        unsigned char status;
593
        unsigned long jiffies;
594
} output_log[OLOGSIZE];
595
 
596
static int output_log_pos=0;
597
#endif
598
 
599
#define CURRENTD -1
600
#define MAXTIMEOUT -2
601
 
602
static void reschedule_timeout(int drive, const char *message, int marg)
603
{
604
        if (drive == CURRENTD)
605
                drive = current_drive;
606
        del_timer(&fd_timeout);
607
        if (drive < 0 || drive > N_DRIVE) {
608
                fd_timeout.expires = jiffies + 20*HZ;
609
                drive=0;
610
        } else
611
                fd_timeout.expires = jiffies + UDP->timeout;
612
        add_timer(&fd_timeout);
613
        if (UDP->flags & FD_DEBUG){
614
                DPRINT("reschedule timeout ");
615
                printk(message, marg);
616
                printk("\n");
617
        }
618
        timeout_message = message;
619
}
620
 
621
static int maximum(int a, int b)
622
{
623
        if(a > b)
624
                return a;
625
        else
626
                return b;
627
}
628
#define INFBOUND(a,b) (a)=maximum((a),(b));
629
 
630
static int minimum(int a, int b)
631
{
632
        if(a < b)
633
                return a;
634
        else
635
                return b;
636
}
637
#define SUPBOUND(a,b) (a)=minimum((a),(b));
638
 
639
 
640
/*
641
 * Bottom half floppy driver.
642
 * ==========================
643
 *
644
 * This part of the file contains the code talking directly to the hardware,
645
 * and also the main service loop (seek-configure-spinup-command)
646
 */
647
 
648
/*
649
 * disk change.
650
 * This routine is responsible for maintaining the FD_DISK_CHANGE flag,
651
 * and the last_checked date.
652
 *
653
 * last_checked is the date of the last check which showed 'no disk change'
654
 * FD_DISK_CHANGE is set under two conditions:
655
 * 1. The floppy has been changed after some i/o to that floppy already
656
 *    took place.
657
 * 2. No floppy disk is in the drive. This is done in order to ensure that
658
 *    requests are quickly flushed in case there is no disk in the drive. It
659
 *    follows that FD_DISK_CHANGE can only be cleared if there is a disk in
660
 *    the drive.
661
 *
662
 * For 1., maxblock is observed. Maxblock is 0 if no i/o has taken place yet.
663
 * For 2., FD_DISK_NEWCHANGE is watched. FD_DISK_NEWCHANGE is cleared on
664
 *  each seek. If a disk is present, the disk change line should also be
665
 *  cleared on each seek. Thus, if FD_DISK_NEWCHANGE is clear, but the disk
666
 *  change line is set, this means either that no disk is in the drive, or
667
 *  that it has been removed since the last seek.
668
 *
669
 * This means that we really have a third possibility too:
670
 *  The floppy has been changed after the last seek.
671
 */
672
 
673
static int disk_change(int drive)
674
{
675
        int fdc=FDC(drive);
676
#ifdef FLOPPY_SANITY_CHECK
677
        if (jiffies - UDRS->select_date < UDP->select_delay)
678
                DPRINT("WARNING disk change called early\n");
679
        if (!(FDCS->dor & (0x10 << UNIT(drive))) ||
680
           (FDCS->dor & 3) != UNIT(drive) ||
681
           fdc != FDC(drive)){
682
                DPRINT("probing disk change on unselected drive\n");
683
                DPRINT("drive=%d fdc=%d dor=%x\n",drive, FDC(drive),
684
                        (unsigned int)FDCS->dor);
685
        }
686
#endif
687
 
688
#ifdef DCL_DEBUG
689
        if (UDP->flags & FD_DEBUG){
690
                DPRINT("checking disk change line for drive %d\n",drive);
691
                DPRINT("jiffies=%ld\n", jiffies);
692
                DPRINT("disk change line=%x\n",fd_inb(FD_DIR)&0x80);
693
                DPRINT("flags=%x\n",UDRS->flags);
694
        }
695
#endif
696
        if (UDP->flags & FD_BROKEN_DCL)
697
                return UTESTF(FD_DISK_CHANGED);
698
        if ((fd_inb(FD_DIR) ^ UDP->flags) & 0x80){
699
                USETF(FD_VERIFY); /* verify write protection */
700
                if (UDRS->maxblock){
701
                        /* mark it changed */
702
                        USETF(FD_DISK_CHANGED);
703
                }
704
 
705
                /* invalidate its geometry */
706
                if (UDRS->keep_data >= 0) {
707
                        if ((UDP->flags & FTD_MSG) &&
708
                            current_type[drive] != NULL)
709
                                DPRINT("Disk type is undefined after "
710
                                       "disk change\n");
711
                        current_type[drive] = NULL;
712
                        floppy_sizes[TOMINOR(drive)] = MAX_DISK_SIZE;
713
                }
714
 
715
                /*USETF(FD_DISK_NEWCHANGE);*/
716
                return 1;
717
        } else {
718
                UDRS->last_checked=jiffies;
719
                UCLEARF(FD_DISK_NEWCHANGE);
720
        }
721
        return 0;
722
}
723
 
724
static inline int is_selected(int dor, int unit)
725
{
726
        return ((dor  & (0x10 << unit)) && (dor &3) == unit);
727
}
728
 
729
static int set_dor(int fdc, char mask, char data)
730
{
731
        register unsigned char drive, unit, newdor,olddor;
732
 
733
        if (FDCS->address == -1)
734
                return -1;
735
 
736
        olddor = FDCS->dor;
737
        newdor =  (olddor & mask) | data;
738
        if (newdor != olddor){
739
                unit = olddor & 0x3;
740
                if (is_selected(olddor, unit) && !is_selected(newdor,unit)){
741
                        drive = REVDRIVE(fdc,unit);
742
#ifdef DCL_DEBUG
743
                        if (UDP->flags & FD_DEBUG){
744
                                DPRINT("calling disk change from set_dor\n");
745
                        }
746
#endif
747
                        disk_change(drive);
748
                }
749
                FDCS->dor = newdor;
750
                fd_setdor(newdor);
751
 
752
                unit = newdor & 0x3;
753
                if (!is_selected(olddor, unit) && is_selected(newdor,unit)){
754
                        drive = REVDRIVE(fdc,unit);
755
                        UDRS->select_date = jiffies;
756
                }
757
        }
758
        if (newdor & FLOPPY_MOTOR_MASK)
759
                floppy_grab_irq_and_dma();
760
        if (olddor & FLOPPY_MOTOR_MASK)
761
                floppy_release_irq_and_dma();
762
        return olddor;
763
}
764
 
765
static void twaddle(void)
766
{
767
        if (DP->select_delay)
768
                return;
769
        fd_setdor(FDCS->dor & ~(0x10<<UNIT(current_drive)));
770
        fd_setdor(FDCS->dor);
771
        DRS->select_date = jiffies;
772
}
773
 
774
/* reset all driver information about the current fdc. This is needed after
775
 * a reset, and after a raw command. */
776
static void reset_fdc_info(int mode)
777
{
778
        int drive;
779
 
780
        FDCS->spec1 = FDCS->spec2 = -1;
781
        FDCS->need_configure = 1;
782
        FDCS->perp_mode = 1;
783
        FDCS->rawcmd = 0;
784
        for (drive = 0; drive < N_DRIVE; drive++)
785
                if (FDC(drive) == fdc &&
786
                    (mode || UDRS->track != NEED_1_RECAL))
787
                        UDRS->track = NEED_2_RECAL;
788
}
789
 
790
/* selects the fdc and drive, and enables the fdc's input/dma. */
791
static void set_fdc(int drive)
792
{
793
        if (drive >= 0 && drive < N_DRIVE){
794
                fdc = FDC(drive);
795
                current_drive = drive;
796
        }
797
        if (fdc != 1 && fdc != 0) {
798
                printk("bad fdc value\n");
799
                return;
800
        }
801
        set_dor(fdc,~0,8);
802
#if N_FDC > 1
803
        set_dor(1-fdc, ~8, 0);
804
#endif
805
        if (FDCS->rawcmd == 2)
806
                reset_fdc_info(1);
807
        if (fd_inb(FD_STATUS) != STATUS_READY)
808
                FDCS->reset = 1;
809
}
810
 
811
/* locks the driver */
812
static int lock_fdc(int drive, int interruptible)
813
{
814
        unsigned long flags;
815
 
816
        if (!usage_count){
817
                printk("trying to lock fdc while usage count=0\n");
818
                return -1;
819
        }
820
        floppy_grab_irq_and_dma();
821
        INT_OFF;
822
        while (fdc_busy && NO_SIGNAL)
823
                interruptible_sleep_on(&fdc_wait);
824
        if (fdc_busy){
825
                INT_ON;
826
                return -EINTR;
827
        }
828
        fdc_busy = 1;
829
        INT_ON;
830
        command_status = FD_COMMAND_NONE;
831
        reschedule_timeout(drive, "lock fdc", 0);
832
        set_fdc(drive);
833
        return 0;
834
}
835
 
836
#define LOCK_FDC(drive,interruptible) \
837
if (lock_fdc(drive,interruptible)) return -EINTR;
838
 
839
 
840
/* unlocks the driver */
841
static inline void unlock_fdc(void)
842
{
843
        raw_cmd = 0;
844
        if (!fdc_busy)
845
                DPRINT("FDC access conflict!\n");
846
 
847
        if (DEVICE_INTR)
848
                DPRINT("device interrupt still active at FDC release: %p!\n",
849
                        DEVICE_INTR);
850
        command_status = FD_COMMAND_NONE;
851
        del_timer(&fd_timeout);
852
        cont = NULL;
853
        fdc_busy = 0;
854
        floppy_release_irq_and_dma();
855
        wake_up(&fdc_wait);
856
}
857
 
858
/* switches the motor off after a given timeout */
859
static void motor_off_callback(unsigned long nr)
860
{
861
        unsigned char mask = ~(0x10 << UNIT(nr));
862
 
863
        set_dor(FDC(nr), mask, 0);
864
}
865
 
866
static struct timer_list motor_off_timer[N_DRIVE] = {
867
        { NULL, NULL, 0, 0, motor_off_callback },
868
        { NULL, NULL, 0, 1, motor_off_callback },
869
        { NULL, NULL, 0, 2, motor_off_callback },
870
        { NULL, NULL, 0, 3, motor_off_callback },
871
        { NULL, NULL, 0, 4, motor_off_callback },
872
        { NULL, NULL, 0, 5, motor_off_callback },
873
        { NULL, NULL, 0, 6, motor_off_callback },
874
        { NULL, NULL, 0, 7, motor_off_callback }
875
};
876
 
877
/* schedules motor off */
878
static void floppy_off(unsigned int drive)
879
{
880
        unsigned long volatile delta;
881
        register int fdc=FDC(drive);
882
 
883
        if (!(FDCS->dor & (0x10 << UNIT(drive))))
884
                return;
885
 
886
        del_timer(motor_off_timer+drive);
887
 
888
        /* make spindle stop in a position which minimizes spinup time
889
         * next time */
890
        if (UDP->rps){
891
                delta = jiffies - UDRS->first_read_date + HZ -
892
                        UDP->spindown_offset;
893
                delta = ((delta * UDP->rps) % HZ) / UDP->rps;
894
                motor_off_timer[drive].expires = jiffies + UDP->spindown - delta;
895
        }
896
        add_timer(motor_off_timer+drive);
897
}
898
 
899
/*
900
 * cycle through all N_DRIVE floppy drives, for disk change testing.
901
 * stopping at current drive. This is done before any long operation, to
902
 * be sure to have up to date disk change information.
903
 */
904
static void scandrives(void)
905
{
906
        int i, drive, saved_drive;
907
 
908
        if (DP->select_delay)
909
                return;
910
 
911
        saved_drive = current_drive;
912
        for (i=0; i < N_DRIVE; i++){
913
                drive = (saved_drive + i + 1) % N_DRIVE;
914
                if (UDRS->fd_ref == 0 || UDP->select_delay != 0)
915
                        continue; /* skip closed drives */
916
                set_fdc(drive);
917
                if (!(set_dor(fdc, ~3, UNIT(drive) | (0x10 << UNIT(drive))) &
918
                      (0x10 << UNIT(drive))))
919
                        /* switch the motor off again, if it was off to
920
                         * begin with */
921
                        set_dor(fdc, ~(0x10 << UNIT(drive)), 0);
922
        }
923
        set_fdc(saved_drive);
924
}
925
 
926
static void empty(void)
927
{
928
}
929
 
930
static struct tq_struct floppy_tq =
931
{ 0, 0, 0, 0 };
932
 
933
static struct timer_list fd_timer ={ NULL, NULL, 0, 0, 0 };
934
 
935
static void cancel_activity(void)
936
{
937
        CLEAR_INTR;
938
        floppy_tq.routine = (void *)(void *) empty;
939
        del_timer(&fd_timer);
940
}
941
 
942
/* this function makes sure that the disk stays in the drive during the
943
 * transfer */
944
static void fd_watchdog(void)
945
{
946
#ifdef DCL_DEBUG
947
        if (DP->flags & FD_DEBUG){
948
                DPRINT("calling disk change from watchdog\n");
949
        }
950
#endif
951
 
952
        if (disk_change(current_drive)){
953
                DPRINT("disk removed during i/o\n");
954
                cancel_activity();
955
                cont->done(0);
956
                reset_fdc();
957
        } else {
958
                del_timer(&fd_timer);
959
                fd_timer.function = (timeout_fn) fd_watchdog;
960
                fd_timer.expires = jiffies + HZ / 10;
961
                add_timer(&fd_timer);
962
        }
963
}
964
 
965
static void main_command_interrupt(void)
966
{
967
        del_timer(&fd_timer);
968
        cont->interrupt();
969
}
970
 
971
/* waits for a delay (spinup or select) to pass */
972
static int wait_for_completion(int delay, timeout_fn function)
973
{
974
        if (FDCS->reset){
975
                reset_fdc(); /* do the reset during sleep to win time
976
                              * if we don't need to sleep, it's a good
977
                              * occasion anyways */
978
                return 1;
979
        }
980
 
981
        if ((signed) (jiffies - delay) < 0){
982
                del_timer(&fd_timer);
983
                fd_timer.function = function;
984
                fd_timer.expires = delay;
985
                add_timer(&fd_timer);
986
                return 1;
987
        }
988
        return 0;
989
}
990
 
991
static int hlt_disabled=0;
992
static void floppy_disable_hlt(void)
993
{
994
        unsigned long flags;
995
 
996
        INT_OFF;
997
        if (!hlt_disabled){
998
                hlt_disabled=1;
999
#ifdef HAVE_DISABLE_HLT
1000
                disable_hlt();
1001
#endif
1002
        }
1003
        INT_ON;
1004
}
1005
 
1006
static void floppy_enable_hlt(void)
1007
{
1008
        unsigned long flags;
1009
 
1010
        INT_OFF;
1011
        if (hlt_disabled){
1012
                hlt_disabled=0;
1013
#ifdef HAVE_DISABLE_HLT
1014
                enable_hlt();
1015
#endif
1016
        }
1017
        INT_ON;
1018
}
1019
 
1020
 
1021
static void setup_DMA(void)
1022
{
1023
        unsigned long flags;
1024
 
1025
#ifdef FLOPPY_SANITY_CHECK
1026
        if (raw_cmd->length == 0){
1027
                int i;
1028
 
1029
                printk("zero dma transfer size:");
1030
                for (i=0; i < raw_cmd->cmd_count; i++)
1031
                        printk("%x,", raw_cmd->cmd[i]);
1032
                printk("\n");
1033
                cont->done(0);
1034
                FDCS->reset = 1;
1035
                return;
1036
        }
1037
#if 0
1038
        if ((long) raw_cmd->kernel_data % 512){
1039
                printk("non aligned address: %p\n", raw_cmd->kernel_data);
1040
                cont->done(0);
1041
                FDCS->reset=1;
1042
                return;
1043
        }
1044
        if (CROSS_64KB(raw_cmd->kernel_data, raw_cmd->length)) {
1045
                printk("DMA crossing 64-K boundary %p-%p\n",
1046
                       raw_cmd->kernel_data,
1047
                       raw_cmd->kernel_data + raw_cmd->length);
1048
                cont->done(0);
1049
                FDCS->reset=1;
1050
                return;
1051
        }
1052
#endif
1053
#endif
1054
        INT_OFF;
1055
        fd_disable_dma();
1056
        fd_clear_dma_ff();
1057
        fd_cacheflush(raw_cmd->kernel_data, raw_cmd->length);
1058
        fd_set_dma_mode((raw_cmd->flags & FD_RAW_READ)?
1059
                        DMA_MODE_READ : DMA_MODE_WRITE);
1060
        fd_set_dma_addr(virt_to_bus(raw_cmd->kernel_data));
1061
        fd_set_dma_count(raw_cmd->length);
1062
        virtual_dma_port = FDCS->address;
1063
        fd_enable_dma();
1064
        INT_ON;
1065
        floppy_disable_hlt();
1066
}
1067
 
1068
void show_floppy(void);
1069
 
1070
/* waits until the fdc becomes ready */
1071
static int wait_til_ready(void)
1072
{
1073
        int counter, status;
1074
        if(FDCS->reset)
1075
                return -1;
1076
        for (counter = 0; counter < 10000; counter++) {
1077
                status = fd_inb(FD_STATUS);
1078
                if (status & STATUS_READY)
1079
                        return status;
1080
        }
1081
        if (!initialising) {
1082
                DPRINT("Getstatus times out (%x) on fdc %d\n",
1083
                        status, fdc);
1084
                show_floppy();
1085
        }
1086
        FDCS->reset = 1;
1087
        return -1;
1088
}
1089
 
1090
/* sends a command byte to the fdc */
1091
static int output_byte(char byte)
1092
{
1093
        int status;
1094
 
1095
        if ((status = wait_til_ready()) < 0)
1096
                return -1;
1097
        if ((status & (STATUS_READY|STATUS_DIR|STATUS_DMA)) == STATUS_READY){
1098
                fd_outb(byte,FD_DATA);
1099
#ifdef FLOPPY_SANITY_CHECK
1100
                output_log[output_log_pos].data = byte;
1101
                output_log[output_log_pos].status = status;
1102
                output_log[output_log_pos].jiffies = jiffies;
1103
                output_log_pos = (output_log_pos + 1) % OLOGSIZE;
1104
#endif
1105
                return 0;
1106
        }
1107
        FDCS->reset = 1;
1108
        if (!initialising) {
1109
                DPRINT("Unable to send byte %x to FDC. Fdc=%x Status=%x\n",
1110
                       byte, fdc, status);
1111
                show_floppy();
1112
        }
1113
        return -1;
1114
}
1115
#define LAST_OUT(x) if (output_byte(x)<0){ reset_fdc();return;}
1116
 
1117
/* gets the response from the fdc */
1118
static int result(void)
1119
{
1120
        int i, status;
1121
 
1122
        for(i=0; i < MAX_REPLIES; i++) {
1123
                if ((status = wait_til_ready()) < 0)
1124
                        break;
1125
                status &= STATUS_DIR|STATUS_READY|STATUS_BUSY|STATUS_DMA;
1126
                if ((status & ~STATUS_BUSY) == STATUS_READY){
1127
#ifdef FLOPPY_SANITY_CHECK
1128
                        resultjiffies = jiffies;
1129
                        resultsize = i;
1130
#endif
1131
                        return i;
1132
                }
1133
                if (status == (STATUS_DIR|STATUS_READY|STATUS_BUSY))
1134
                        reply_buffer[i] = fd_inb(FD_DATA);
1135
                else
1136
                        break;
1137
        }
1138
        if(!initialising) {
1139
                DPRINT("get result error. Fdc=%d Last status=%x Read bytes=%d\n",
1140
                       fdc, status, i);
1141
                show_floppy();
1142
        }
1143
        FDCS->reset = 1;
1144
        return -1;
1145
}
1146
 
1147
#define MORE_OUTPUT -2
1148
/* does the fdc need more output? */
1149
static int need_more_output(void)
1150
{
1151
        int status;
1152
        if( (status = wait_til_ready()) < 0)
1153
                return -1;
1154
        if ((status & (STATUS_READY|STATUS_DIR|STATUS_DMA)) == STATUS_READY)
1155
                return MORE_OUTPUT;
1156
        return result();
1157
}
1158
 
1159
/* Set perpendicular mode as required, based on data rate, if supported.
1160
 * 82077 Now tested. 1Mbps data rate only possible with 82077-1.
1161
 */
1162
static inline void perpendicular_mode(void)
1163
{
1164
        unsigned char perp_mode;
1165
 
1166
        if (raw_cmd->rate & 0x40){
1167
                switch(raw_cmd->rate & 3){
1168
                        case 0:
1169
                                perp_mode=2;
1170
                                break;
1171
                        case 3:
1172
                                perp_mode=3;
1173
                                break;
1174
                        default:
1175
                                DPRINT("Invalid data rate for perpendicular mode!\n");
1176
                                cont->done(0);
1177
                                FDCS->reset = 1; /* convenient way to return to
1178
                                                  * redo without to much hassle (deep
1179
                                                  * stack et al. */
1180
                                return;
1181
                }
1182
        } else
1183
                perp_mode = 0;
1184
 
1185
        if (FDCS->perp_mode == perp_mode)
1186
                return;
1187
        if (FDCS->version >= FDC_82077_ORIG) {
1188
                output_byte(FD_PERPENDICULAR);
1189
                output_byte(perp_mode);
1190
                FDCS->perp_mode = perp_mode;
1191
        } else if (perp_mode) {
1192
                DPRINT("perpendicular mode not supported by this FDC.\n");
1193
        }
1194
} /* perpendicular_mode */
1195
 
1196
static int fifo_depth = 0xa;
1197
static int no_fifo = 0;
1198
 
1199
static int fdc_configure(void)
1200
{
1201
        /* Turn on FIFO */
1202
        output_byte(FD_CONFIGURE);
1203
        if(need_more_output() != MORE_OUTPUT)
1204
                return 0;
1205
        output_byte(0);
1206
        output_byte(0x10 | (no_fifo & 0x20) | (fifo_depth & 0xf));
1207
        output_byte(0);  /* pre-compensation from track
1208
 
1209
        return 1;
1210
}
1211
 
1212
#define NOMINAL_DTR 500
1213
 
1214
/* Issue a "SPECIFY" command to set the step rate time, head unload time,
1215
 * head load time, and DMA disable flag to values needed by floppy.
1216
 *
1217
 * The value "dtr" is the data transfer rate in Kbps.  It is needed
1218
 * to account for the data rate-based scaling done by the 82072 and 82077
1219
 * FDC types.  This parameter is ignored for other types of FDCs (i.e.
1220
 * 8272a).
1221
 *
1222
 * Note that changing the data transfer rate has a (probably deleterious)
1223
 * effect on the parameters subject to scaling for 82072/82077 FDCs, so
1224
 * fdc_specify is called again after each data transfer rate
1225
 * change.
1226
 *
1227
 * srt: 1000 to 16000 in microseconds
1228
 * hut: 16 to 240 milliseconds
1229
 * hlt: 2 to 254 milliseconds
1230
 *
1231
 * These values are rounded up to the next highest available delay time.
1232
 */
1233
static void fdc_specify(void)
1234
{
1235
        unsigned char spec1, spec2;
1236
        int srt, hlt, hut;
1237
        unsigned long dtr = NOMINAL_DTR;
1238
        unsigned long scale_dtr = NOMINAL_DTR;
1239
        int hlt_max_code = 0x7f;
1240
        int hut_max_code = 0xf;
1241
 
1242
        if (FDCS->need_configure && FDCS->version >= FDC_82072A) {
1243
                fdc_configure();
1244
                FDCS->need_configure = 0;
1245
                /*DPRINT("FIFO enabled\n");*/
1246
        }
1247
 
1248
        switch (raw_cmd->rate & 0x03) {
1249
                case 3:
1250
                        dtr = 1000;
1251
                        break;
1252
                case 1:
1253
                        dtr = 300;
1254
                        if (FDCS->version >= FDC_82078) {
1255
                                /* chose the default rate table, not the one
1256
                                 * where 1 = 2 Mbps */
1257
                                output_byte(FD_DRIVESPEC);
1258
                                if(need_more_output() == MORE_OUTPUT) {
1259
                                        output_byte(UNIT(current_drive));
1260
                                        output_byte(0xc0);
1261
                                }
1262
                        }
1263
                        break;
1264
                case 2:
1265
                        dtr = 250;
1266
                        break;
1267
        }
1268
 
1269
        if (FDCS->version >= FDC_82072) {
1270
                scale_dtr = dtr;
1271
                hlt_max_code = 0x00; /* 0==256msec*dtr0/dtr (not linear!) */
1272
                hut_max_code = 0x0; /* 0==256msec*dtr0/dtr (not linear!) */
1273
        }
1274
 
1275
        /* Convert step rate from microseconds to milliseconds and 4 bits */
1276
        srt = 16 - (DP->srt*scale_dtr/1000 + NOMINAL_DTR - 1)/NOMINAL_DTR;
1277
        SUPBOUND(srt, 0xf);
1278
        INFBOUND(srt, 0);
1279
 
1280
        hlt = (DP->hlt*scale_dtr/2 + NOMINAL_DTR - 1)/NOMINAL_DTR;
1281
        if (hlt < 0x01)
1282
                hlt = 0x01;
1283
        else if (hlt > 0x7f)
1284
                hlt = hlt_max_code;
1285
 
1286
        hut = (DP->hut*scale_dtr/16 + NOMINAL_DTR - 1)/NOMINAL_DTR;
1287
        if (hut < 0x1)
1288
                hut = 0x1;
1289
        else if (hut > 0xf)
1290
                hut = hut_max_code;
1291
 
1292
        spec1 = (srt << 4) | hut;
1293
        spec2 = (hlt << 1) | (use_virtual_dma & 1);
1294
 
1295
        /* If these parameters did not change, just return with success */
1296
        if (FDCS->spec1 != spec1 || FDCS->spec2 != spec2) {
1297
                /* Go ahead and set spec1 and spec2 */
1298
                output_byte(FD_SPECIFY);
1299
                output_byte(FDCS->spec1 = spec1);
1300
                output_byte(FDCS->spec2 = spec2);
1301
        }
1302
} /* fdc_specify */
1303
 
1304
/* Set the FDC's data transfer rate on behalf of the specified drive.
1305
 * NOTE: with 82072/82077 FDCs, changing the data rate requires a reissue
1306
 * of the specify command (i.e. using the fdc_specify function).
1307
 */
1308
static int fdc_dtr(void)
1309
{
1310
        /* If data rate not already set to desired value, set it. */
1311
        if ((raw_cmd->rate & 3) == FDCS->dtr)
1312
                return 0;
1313
 
1314
        /* Set dtr */
1315
        fd_outb(raw_cmd->rate & 3, FD_DCR);
1316
 
1317
        /* TODO: some FDC/drive combinations (C&T 82C711 with TEAC 1.2MB)
1318
         * need a stabilization period of several milliseconds to be
1319
         * enforced after data rate changes before R/W operations.
1320
         * Pause 5 msec to avoid trouble. (Needs to be 2 jiffies)
1321
         */
1322
        FDCS->dtr = raw_cmd->rate & 3;
1323
        return(wait_for_completion(jiffies+2*HZ/100,
1324
                                   (timeout_fn) floppy_ready));
1325
} /* fdc_dtr */
1326
 
1327
static void tell_sector(void)
1328
{
1329
        printk(": track %d, head %d, sector %d, size %d",
1330
               R_TRACK, R_HEAD, R_SECTOR, R_SIZECODE);
1331
} /* tell_sector */
1332
 
1333
 
1334
/*
1335
 * OK, this error interpreting routine is called after a
1336
 * DMA read/write has succeeded
1337
 * or failed, so we check the results, and copy any buffers.
1338
 * hhb: Added better error reporting.
1339
 * ak: Made this into a separate routine.
1340
 */
1341
static int interpret_errors(void)
1342
{
1343
        char bad;
1344
int res = get_dma_residue(FLOPPY_DMA);
1345
if(res) {printk("\n-- DMA residue (%d)",res); tell_sector(); printk("\n");}
1346
 
1347
        if (inr!=7) {
1348
                DPRINT("-- FDC reply error");
1349
                FDCS->reset = 1;
1350
                return 1;
1351
        }
1352
 
1353
        /* check IC to find cause of interrupt */
1354
        switch (ST0 & ST0_INTR) {
1355
                case 0x40:      /* error occurred during command execution */
1356
                        if (ST1 & ST1_EOC)
1357
                                return 0; /* occurs with pseudo-DMA */
1358
                        bad = 1;
1359
                        if (ST1 & ST1_WP) {
1360
                                DPRINT("Drive is write protected\n");
1361
                                CLEARF(FD_DISK_WRITABLE);
1362
                                cont->done(0);
1363
                                bad = 2;
1364
                        } else if (ST1 & ST1_ND) {
1365
                                SETF(FD_NEED_TWADDLE);
1366
                        } else if (ST1 & ST1_OR) {
1367
                                if (DP->flags & FTD_MSG)
1368
                                        DPRINT("Over/Underrun - retrying\n");
1369
                                bad = 0;
1370
                        }else if (*errors >= DP->max_errors.reporting){
1371
                                DPRINT("");
1372
                                if (ST0 & ST0_ECE) {
1373
                                        printk("Recalibrate failed!");
1374
                                } else if (ST2 & ST2_CRC) {
1375
                                        printk("data CRC error");
1376
                                        tell_sector();
1377
                                } else if (ST1 & ST1_CRC) {
1378
                                        printk("CRC error");
1379
                                        tell_sector();
1380
                                } else if ((ST1 & (ST1_MAM|ST1_ND)) || (ST2 & ST2_MAM)) {
1381
                                        if (!probing) {
1382
                                                printk("sector not found");
1383
                                                tell_sector();
1384
                                        } else
1385
                                                printk("probe failed...");
1386
                                } else if (ST2 & ST2_WC) {      /* seek error */
1387
                                        printk("wrong cylinder");
1388
                                } else if (ST2 & ST2_BC) {      /* cylinder marked as bad */
1389
                                        printk("bad cylinder");
1390
                                } else {
1391
                                        printk("unknown error. ST[0..2] are: 0x%x 0x%x 0x%x", ST0, ST1, ST2);
1392
                                        tell_sector();
1393
                                }
1394
                                printk("\n");
1395
 
1396
                        }
1397
                        if (ST2 & ST2_WC || ST2 & ST2_BC)
1398
                                /* wrong cylinder => recal */
1399
                                DRS->track = NEED_2_RECAL;
1400
                        return bad;
1401
                case 0x80: /* invalid command given */
1402
                        DPRINT("Invalid FDC command given!\n");
1403
                        cont->done(0);
1404
                        return 2;
1405
                case 0xc0:
1406
                        DPRINT("Abnormal termination caused by polling\n");
1407
                        cont->error();
1408
                        return 2;
1409
                default: /* (0) Normal command termination */
1410
                        return 0;
1411
        }
1412
}
1413
 
1414
/*
1415
 * This routine is called when everything should be correctly set up
1416
 * for the transfer (i.e. floppy motor is on, the correct floppy is
1417
 * selected, and the head is sitting on the right track).
1418
 */
1419
static void setup_rw_floppy(void)
1420
{
1421
        int i,ready_date,r, flags,dflags;
1422
        timeout_fn function;
1423
 
1424
        flags = raw_cmd->flags;
1425
        if (flags & (FD_RAW_READ | FD_RAW_WRITE))
1426
                flags |= FD_RAW_INTR;
1427
 
1428
        if ((flags & FD_RAW_SPIN) && !(flags & FD_RAW_NO_MOTOR)){
1429
                ready_date = DRS->spinup_date + DP->spinup;
1430
                /* If spinup will take a long time, rerun scandrives
1431
                 * again just before spinup completion. Beware that
1432
                 * after scandrives, we must again wait for selection.
1433
                 */
1434
                if ((signed) (ready_date - jiffies) > DP->select_delay){
1435
                        ready_date -= DP->select_delay;
1436
                        function = (timeout_fn) floppy_start;
1437
                } else
1438
                        function = (timeout_fn) setup_rw_floppy;
1439
 
1440
                /* wait until the floppy is spinning fast enough */
1441
                if (wait_for_completion(ready_date,function))
1442
                        return;
1443
        }
1444
        dflags = DRS->flags;
1445
 
1446
        if ((flags & FD_RAW_READ) || (flags & FD_RAW_WRITE))
1447
                setup_DMA();
1448
 
1449
        if (flags & FD_RAW_INTR)
1450
                SET_INTR(main_command_interrupt);
1451
 
1452
        r=0;
1453
        for (i=0; i< raw_cmd->cmd_count; i++)
1454
                r|=output_byte(raw_cmd->cmd[i]);
1455
 
1456
#ifdef DEBUGT
1457
        debugt("rw_command: ");
1458
#endif
1459
        if (r){
1460
                cont->error();
1461
                reset_fdc();
1462
                return;
1463
        }
1464
 
1465
        if (!(flags & FD_RAW_INTR)){
1466
                inr = result();
1467
                cont->interrupt();
1468
        } else if (flags & FD_RAW_NEED_DISK)
1469
                fd_watchdog();
1470
}
1471
 
1472
static int blind_seek;
1473
 
1474
/*
1475
 * This is the routine called after every seek (or recalibrate) interrupt
1476
 * from the floppy controller.
1477
 */
1478
static void seek_interrupt(void)
1479
{
1480
#ifdef DEBUGT
1481
        debugt("seek interrupt:");
1482
#endif
1483
        if (inr != 2 || (ST0 & 0xF8) != 0x20) {
1484
                DPRINT("seek failed\n");
1485
                DRS->track = NEED_2_RECAL;
1486
                cont->error();
1487
                cont->redo();
1488
                return;
1489
        }
1490
        if (DRS->track >= 0 && DRS->track != ST1 && !blind_seek){
1491
#ifdef DCL_DEBUG
1492
                if (DP->flags & FD_DEBUG){
1493
                        DPRINT("clearing NEWCHANGE flag because of effective seek\n");
1494
                        DPRINT("jiffies=%ld\n", jiffies);
1495
                }
1496
#endif
1497
                CLEARF(FD_DISK_NEWCHANGE); /* effective seek */
1498
                DRS->select_date = jiffies;
1499
        }
1500
        DRS->track = ST1;
1501
        floppy_ready();
1502
}
1503
 
1504
static void check_wp(void)
1505
{
1506
        if (TESTF(FD_VERIFY)) {
1507
                /* check write protection */
1508
                output_byte(FD_GETSTATUS);
1509
                output_byte(UNIT(current_drive));
1510
                if (result() != 1){
1511
                        FDCS->reset = 1;
1512
                        return;
1513
                }
1514
                CLEARF(FD_VERIFY);
1515
                CLEARF(FD_NEED_TWADDLE);
1516
#ifdef DCL_DEBUG
1517
                if (DP->flags & FD_DEBUG){
1518
                        DPRINT("checking whether disk is write protected\n");
1519
                        DPRINT("wp=%x\n",ST3 & 0x40);
1520
                }
1521
#endif
1522
                if (!(ST3  & 0x40))
1523
                        SETF(FD_DISK_WRITABLE);
1524
                else
1525
                        CLEARF(FD_DISK_WRITABLE);
1526
        }
1527
}
1528
 
1529
static void seek_floppy(void)
1530
{
1531
        int track;
1532
 
1533
        blind_seek=0;
1534
 
1535
#ifdef DCL_DEBUG
1536
        if (DP->flags & FD_DEBUG){
1537
                DPRINT("calling disk change from seek\n");
1538
        }
1539
#endif
1540
 
1541
        if (!TESTF(FD_DISK_NEWCHANGE) &&
1542
            disk_change(current_drive) &&
1543
            (raw_cmd->flags & FD_RAW_NEED_DISK)){
1544
                /* the media changed flag should be cleared after the seek.
1545
                 * If it isn't, this means that there is really no disk in
1546
                 * the drive.
1547
                 */
1548
                SETF(FD_DISK_CHANGED);
1549
                cont->done(0);
1550
                cont->redo();
1551
                return;
1552
        }
1553
        if (DRS->track <= NEED_1_RECAL){
1554
                recalibrate_floppy();
1555
                return;
1556
        } else if (TESTF(FD_DISK_NEWCHANGE) &&
1557
                   (raw_cmd->flags & FD_RAW_NEED_DISK) &&
1558
                   (DRS->track <= NO_TRACK || DRS->track == raw_cmd->track)) {
1559
                /* we seek to clear the media-changed condition. Does anybody
1560
                 * know a more elegant way, which works on all drives? */
1561
                if (raw_cmd->track)
1562
                        track = raw_cmd->track - 1;
1563
                else {
1564
                        if (DP->flags & FD_SILENT_DCL_CLEAR){
1565
                                set_dor(fdc, ~(0x10 << UNIT(current_drive)), 0);
1566
                                blind_seek = 1;
1567
                                raw_cmd->flags |= FD_RAW_NEED_SEEK;
1568
                        }
1569
                        track = 1;
1570
                }
1571
        } else {
1572
                check_wp();
1573
                if (raw_cmd->track != DRS->track &&
1574
                    (raw_cmd->flags & FD_RAW_NEED_SEEK))
1575
                        track = raw_cmd->track;
1576
                else {
1577
                        setup_rw_floppy();
1578
                        return;
1579
                }
1580
        }
1581
 
1582
        SET_INTR(seek_interrupt);
1583
        output_byte(FD_SEEK);
1584
        output_byte(UNIT(current_drive));
1585
        LAST_OUT(track);
1586
#ifdef DEBUGT
1587
        debugt("seek command:");
1588
#endif
1589
}
1590
 
1591
static void recal_interrupt(void)
1592
{
1593
#ifdef DEBUGT
1594
        debugt("recal interrupt:");
1595
#endif
1596
        if (inr !=2)
1597
                FDCS->reset = 1;
1598
        else if (ST0 & ST0_ECE) {
1599
                switch(DRS->track){
1600
                        case NEED_1_RECAL:
1601
#ifdef DEBUGT
1602
                                debugt("recal interrupt need 1 recal:");
1603
#endif
1604
                                /* after a second recalibrate, we still haven't
1605
                                 * reached track 0. Probably no drive. Raise an
1606
                                 * error, as failing immediately might upset
1607
                                 * computers possessed by the Devil :-) */
1608
                                cont->error();
1609
                                cont->redo();
1610
                                return;
1611
                        case NEED_2_RECAL:
1612
#ifdef DEBUGT
1613
                                debugt("recal interrupt need 2 recal:");
1614
#endif
1615
                                /* If we already did a recalibrate,
1616
                                 * and we are not at track 0, this
1617
                                 * means we have moved. (The only way
1618
                                 * not to move at recalibration is to
1619
                                 * be already at track 0.) Clear the
1620
                                 * new change flag */
1621
#ifdef DCL_DEBUG
1622
                                if (DP->flags & FD_DEBUG){
1623
                                        DPRINT("clearing NEWCHANGE flag because of second recalibrate\n");
1624
                                }
1625
#endif
1626
 
1627
                                CLEARF(FD_DISK_NEWCHANGE);
1628
                                DRS->select_date = jiffies;
1629
                                /* fall through */
1630
                        default:
1631
#ifdef DEBUGT
1632
                                debugt("recal interrupt default:");
1633
#endif
1634
                                /* Recalibrate moves the head by at
1635
                                 * most 80 steps. If after one
1636
                                 * recalibrate we don't have reached
1637
                                 * track 0, this might mean that we
1638
                                 * started beyond track 80.  Try
1639
                                 * again.  */
1640
                                DRS->track = NEED_1_RECAL;
1641
                                break;
1642
                }
1643
        } else
1644
                DRS->track = ST1;
1645
        floppy_ready();
1646
}
1647
 
1648
static void print_result(char *message, int inr)
1649
{
1650
        int i;
1651
 
1652
        DPRINT("%s ", message);
1653
        if (inr >= 0)
1654
                for (i=0; i<inr; i++)
1655
                        printk("repl[%d]=%x ", i, reply_buffer[i]);
1656
        printk("\n");
1657
}
1658
 
1659
/* interrupt handler */
1660
void floppy_interrupt(int irq, void *dev_id, struct pt_regs * regs)
1661
{
1662
        void (*handler)(void) = DEVICE_INTR;
1663
        int do_print;
1664
 
1665
        lasthandler = handler;
1666
        interruptjiffies = jiffies;
1667
 
1668
        fd_disable_dma();
1669
        floppy_enable_hlt();
1670
        CLEAR_INTR;
1671
        if (fdc >= N_FDC || FDCS->address == -1){
1672
                /* we don't even know which FDC is the culprit */
1673
                printk("DOR0=%x\n", (unsigned int)fdc_state[0].dor);
1674
                printk("floppy interrupt on bizarre fdc %d\n",fdc);
1675
                printk("handler=%p\n", handler);
1676
                is_alive("bizarre fdc");
1677
                return;
1678
        }
1679
 
1680
        FDCS->reset = 0;
1681
        /* We have to clear the reset flag here, because apparently on boxes
1682
         * with level triggered interrupts (PS/2, Sparc, ...), it is needed to
1683
         * emit SENSEI's to clear the interrupt line. And FDCS->reset blocks the
1684
         * emission of the SENSEI's.
1685
         * It is OK to emit floppy commands because we are in an interrupt
1686
         * handler here, and thus we have to fear no interference of other
1687
         * activity.
1688
         */
1689
 
1690
        do_print = !handler && print_unex && !initialising;
1691
 
1692
        inr = result();
1693
        if(do_print)
1694
                print_result("unexpected interrupt", inr);
1695
        if (inr == 0){
1696
                do {
1697
                        output_byte(FD_SENSEI);
1698
                        inr = result();
1699
                        if(do_print)
1700
                                print_result("sensei", inr);
1701
                } while ((ST0 & 0x83) != UNIT(current_drive) && inr == 2);
1702
        }
1703
        if (handler) {
1704
                if(intr_count >= 2) {
1705
                        /* expected interrupt */
1706
                        floppy_tq.routine = (void *)(void *) handler;
1707
                        queue_task_irq(&floppy_tq, &tq_immediate);
1708
                        mark_bh(IMMEDIATE_BH);
1709
                } else
1710
                        handler();
1711
        } else
1712
                FDCS->reset = 1;
1713
        is_alive("normal interrupt end");
1714
}
1715
 
1716
static void recalibrate_floppy(void)
1717
{
1718
#ifdef DEBUGT
1719
        debugt("recalibrate floppy:");
1720
#endif
1721
        SET_INTR(recal_interrupt);
1722
        output_byte(FD_RECALIBRATE);
1723
        LAST_OUT(UNIT(current_drive));
1724
}
1725
 
1726
/*
1727
 * Must do 4 FD_SENSEIs after reset because of ``drive polling''.
1728
 */
1729
static void reset_interrupt(void)
1730
{
1731
#ifdef DEBUGT
1732
        debugt("reset interrupt:");
1733
#endif
1734
        result();               /* get the status ready for set_fdc */
1735
        if (FDCS->reset) {
1736
                printk("reset set in interrupt, calling %p\n", cont->error);
1737
                cont->error(); /* a reset just after a reset. BAD! */
1738
        }
1739
        cont->redo();
1740
}
1741
 
1742
/*
1743
 * reset is done by pulling bit 2 of DOR low for a while (old FDCs),
1744
 * or by setting the self clearing bit 7 of STATUS (newer FDCs)
1745
 */
1746
static void reset_fdc(void)
1747
{
1748
        SET_INTR(reset_interrupt);
1749
        FDCS->reset = 0;
1750
        reset_fdc_info(0);
1751
 
1752
        /* Pseudo-DMA may intercept 'reset finished' interrupt.  */
1753
        /* Irrelevant for systems with true DMA (i386).          */
1754
        fd_disable_dma();
1755
 
1756
        if (FDCS->version >= FDC_82072A)
1757
                fd_outb(0x80 | (FDCS->dtr &3), FD_STATUS);
1758
        else {
1759
                fd_setdor(FDCS->dor & ~0x04);
1760
                udelay(FD_RESET_DELAY);
1761
                fd_setdor(FDCS->dor);
1762
        }
1763
}
1764
 
1765
void show_floppy(void)
1766
{
1767
        int i;
1768
 
1769
        printk("\n");
1770
        printk("floppy driver state\n");
1771
        printk("-------------------\n");
1772
        printk("now=%ld last interrupt=%d last called handler=%p\n",
1773
               jiffies, interruptjiffies, lasthandler);
1774
 
1775
 
1776
#ifdef FLOPPY_SANITY_CHECK
1777
        printk("timeout_message=%s\n", timeout_message);
1778
        printk("last output bytes:\n");
1779
        for (i=0; i < OLOGSIZE; i++)
1780
                printk("%2x %2x %ld\n",
1781
                       output_log[(i+output_log_pos) % OLOGSIZE].data,
1782
                       output_log[(i+output_log_pos) % OLOGSIZE].status,
1783
                       output_log[(i+output_log_pos) % OLOGSIZE].jiffies);
1784
        printk("last result at %d\n", resultjiffies);
1785
        printk("last redo_fd_request at %d\n", lastredo);
1786
        for (i=0; i<resultsize; i++){
1787
                printk("%2x ", reply_buffer[i]);
1788
        }
1789
        printk("\n");
1790
#endif
1791
 
1792
        printk("status=%x\n", fd_inb(FD_STATUS));
1793
        printk("fdc_busy=%d\n", fdc_busy);
1794
        if (DEVICE_INTR)
1795
                printk("DEVICE_INTR=%p\n", DEVICE_INTR);
1796
        if (floppy_tq.sync)
1797
                printk("floppy_tq.routine=%p\n", floppy_tq.routine);
1798
        if (fd_timer.prev)
1799
                printk("fd_timer.function=%p\n", fd_timer.function);
1800
        if (fd_timeout.prev){
1801
                printk("timer_table=%p\n",fd_timeout.function);
1802
                printk("expires=%ld\n",fd_timeout.expires-jiffies);
1803
                printk("now=%ld\n",jiffies);
1804
        }
1805
        printk("cont=%p\n", cont);
1806
        printk("CURRENT=%p\n", CURRENT);
1807
        printk("command_status=%d\n", command_status);
1808
        printk("\n");
1809
}
1810
 
1811
static void floppy_shutdown(void)
1812
{
1813
        if (!initialising)
1814
                show_floppy();
1815
        cancel_activity();
1816
        sti();
1817
 
1818
        floppy_enable_hlt();
1819
        fd_disable_dma();
1820
        /* avoid dma going to a random drive after shutdown */
1821
 
1822
        if (!initialising)
1823
                DPRINT("floppy timeout called\n");
1824
        FDCS->reset = 1;
1825
        if (cont){
1826
                cont->done(0);
1827
                cont->redo(); /* this will recall reset when needed */
1828
        } else {
1829
                printk("no cont in shutdown!\n");
1830
                process_fd_request();
1831
        }
1832
        is_alive("floppy shutdown");
1833
}
1834
/*typedef void (*timeout_fn)(unsigned long);*/
1835
 
1836
/* start motor, check media-changed condition and write protection */
1837
static int start_motor(void (*function)(void) )
1838
{
1839
        int mask, data;
1840
 
1841
        mask = 0xfc;
1842
        data = UNIT(current_drive);
1843
        if (!(raw_cmd->flags & FD_RAW_NO_MOTOR)){
1844
                if (!(FDCS->dor & (0x10 << UNIT(current_drive)))){
1845
                        set_debugt();
1846
                        /* no read since this drive is running */
1847
                        DRS->first_read_date = 0;
1848
                        /* note motor start time if motor is not yet running */
1849
                        DRS->spinup_date = jiffies;
1850
                        data |= (0x10 << UNIT(current_drive));
1851
                }
1852
        } else
1853
                if (FDCS->dor & (0x10 << UNIT(current_drive)))
1854
                        mask &= ~(0x10 << UNIT(current_drive));
1855
 
1856
        /* starts motor and selects floppy */
1857
        del_timer(motor_off_timer + current_drive);
1858
        set_dor(fdc, mask, data);
1859
 
1860
        /* wait_for_completion also schedules reset if needed. */
1861
        return(wait_for_completion(DRS->select_date+DP->select_delay,
1862
                                   (timeout_fn) function));
1863
}
1864
 
1865
static void floppy_ready(void)
1866
{
1867
        CHECK_RESET;
1868
        if (start_motor(floppy_ready)) return;
1869
        if (fdc_dtr()) return;
1870
 
1871
#ifdef DCL_DEBUG
1872
        if (DP->flags & FD_DEBUG){
1873
                DPRINT("calling disk change from floppy_ready\n");
1874
        }
1875
#endif
1876
 
1877
        if (!(raw_cmd->flags & FD_RAW_NO_MOTOR) &&
1878
           disk_change(current_drive) &&
1879
           !DP->select_delay)
1880
                twaddle(); /* this clears the dcl on certain drive/controller
1881
                            * combinations */
1882
 
1883
        if (raw_cmd->flags & (FD_RAW_NEED_SEEK | FD_RAW_NEED_DISK)){
1884
                perpendicular_mode();
1885
                fdc_specify(); /* must be done here because of hut, hlt ... */
1886
                seek_floppy();
1887
        } else
1888
                setup_rw_floppy();
1889
}
1890
 
1891
static void floppy_start(void)
1892
{
1893
        reschedule_timeout(CURRENTD, "floppy start", 0);
1894
 
1895
        scandrives();
1896
#ifdef DCL_DEBUG
1897
        if (DP->flags & FD_DEBUG){
1898
                DPRINT("setting NEWCHANGE in floppy_start\n");
1899
        }
1900
#endif
1901
        SETF(FD_DISK_NEWCHANGE);
1902
        floppy_ready();
1903
}
1904
 
1905
/*
1906
 * ========================================================================
1907
 * here ends the bottom half. Exported routines are:
1908
 * floppy_start, floppy_off, floppy_ready, lock_fdc, unlock_fdc, set_fdc,
1909
 * start_motor, reset_fdc, reset_fdc_info, interpret_errors.
1910
 * Initialization also uses output_byte, result, set_dor, floppy_interrupt
1911
 * and set_dor.
1912
 * ========================================================================
1913
 */
1914
/*
1915
 * General purpose continuations.
1916
 * ==============================
1917
 */
1918
 
1919
static void do_wakeup(void)
1920
{
1921
        reschedule_timeout(MAXTIMEOUT, "do wakeup", 0);
1922
        cont = 0;
1923
        command_status += 2;
1924
        wake_up(&command_done);
1925
}
1926
 
1927
static struct cont_t wakeup_cont={
1928
        empty,
1929
        do_wakeup,
1930
        empty,
1931
        (done_f)empty
1932
};
1933
 
1934
 
1935
static struct cont_t intr_cont={
1936
        empty,
1937
        process_fd_request,
1938
        empty,
1939
        (done_f) empty
1940
};
1941
 
1942
static int wait_til_done(void (*handler)(void), int interruptible)
1943
{
1944
        int ret;
1945
        unsigned long flags;
1946
 
1947
        floppy_tq.routine = (void *)(void *) handler;
1948
        queue_task(&floppy_tq, &tq_immediate);
1949
        mark_bh(IMMEDIATE_BH);
1950
        INT_OFF;
1951
        while(command_status < 2 && NO_SIGNAL){
1952
                is_alive("wait_til_done");
1953
                if (interruptible)
1954
                        interruptible_sleep_on(&command_done);
1955
                else
1956
                        sleep_on(&command_done);
1957
        }
1958
        if (command_status < 2){
1959
                cancel_activity();
1960
                cont = &intr_cont;
1961
                reset_fdc();
1962
                INT_ON;
1963
                return -EINTR;
1964
        }
1965
        INT_ON;
1966
 
1967
        if (FDCS->reset)
1968
                command_status = FD_COMMAND_ERROR;
1969
        if (command_status == FD_COMMAND_OKAY)
1970
                ret=0;
1971
        else
1972
                ret=-EIO;
1973
        command_status = FD_COMMAND_NONE;
1974
        return ret;
1975
}
1976
 
1977
static void generic_done(int result)
1978
{
1979
        command_status = result;
1980
        cont = &wakeup_cont;
1981
}
1982
 
1983
static void generic_success(void)
1984
{
1985
        cont->done(1);
1986
}
1987
 
1988
static void generic_failure(void)
1989
{
1990
        cont->done(0);
1991
}
1992
 
1993
static void success_and_wakeup(void)
1994
{
1995
        generic_success();
1996
        cont->redo();
1997
}
1998
 
1999
 
2000
/*
2001
 * formatting and rw support.
2002
 * ==========================
2003
 */
2004
 
2005
static int next_valid_format(void)
2006
{
2007
        int probed_format;
2008
 
2009
        probed_format = DRS->probed_format;
2010
        while(1){
2011
                if (probed_format >= 8 ||
2012
                     !DP->autodetect[probed_format]){
2013
                        DRS->probed_format = 0;
2014
                        return 1;
2015
                }
2016
                if (floppy_type[DP->autodetect[probed_format]].sect){
2017
                        DRS->probed_format = probed_format;
2018
                        return 0;
2019
                }
2020
                probed_format++;
2021
        }
2022
}
2023
 
2024
static void bad_flp_intr(void)
2025
{
2026
        if (probing){
2027
                DRS->probed_format++;
2028
                if (!next_valid_format())
2029
                        return;
2030
        }
2031
        (*errors)++;
2032
        INFBOUND(DRWE->badness, *errors);
2033
        if (*errors > DP->max_errors.abort)
2034
                cont->done(0);
2035
        if (*errors > DP->max_errors.reset)
2036
                FDCS->reset = 1;
2037
        else if (*errors > DP->max_errors.recal)
2038
                DRS->track = NEED_2_RECAL;
2039
}
2040
 
2041
static void set_floppy(kdev_t device)
2042
{
2043
        if (TYPE(device))
2044
                _floppy = TYPE(device) + floppy_type;
2045
        else
2046
                _floppy = current_type[ DRIVE(device) ];
2047
}
2048
 
2049
/*
2050
 * formatting support.
2051
 * ===================
2052
 */
2053
static void format_interrupt(void)
2054
{
2055
        switch (interpret_errors()){
2056
                case 1:
2057
                        cont->error();
2058
                case 2:
2059
                        break;
2060
                case 0:
2061
                        cont->done(1);
2062
        }
2063
        cont->redo();
2064
}
2065
 
2066
#define CODE2SIZE (ssize = ((1 << SIZECODE) + 3) >> 2)
2067
#define FM_MODE(x,y) ((y) & ~(((x)->rate & 0x80) >>1))
2068
#define CT(x) ((x) | 0x40)
2069
static void setup_format_params(int track)
2070
{
2071
        struct fparm {
2072
                unsigned char track,head,sect,size;
2073
        } *here = (struct fparm *)floppy_track_buffer;
2074
        int il,n;
2075
        int count,head_shift,track_shift;
2076
 
2077
        raw_cmd = &default_raw_cmd;
2078
        raw_cmd->track = track;
2079
 
2080
        raw_cmd->flags = FD_RAW_WRITE | FD_RAW_INTR | FD_RAW_SPIN |
2081
                FD_RAW_NEED_DISK | FD_RAW_NEED_SEEK;
2082
        raw_cmd->rate = _floppy->rate & 0x43;
2083
        raw_cmd->cmd_count = NR_F;
2084
        COMMAND = FM_MODE(_floppy,FD_FORMAT);
2085
        DR_SELECT = UNIT(current_drive) + PH_HEAD(_floppy,format_req.head);
2086
        F_SIZECODE = FD_SIZECODE(_floppy);
2087
        F_SECT_PER_TRACK = _floppy->sect << 2 >> F_SIZECODE;
2088
        F_GAP = _floppy->fmt_gap;
2089
        F_FILL = FD_FILL_BYTE;
2090
 
2091
        raw_cmd->kernel_data = floppy_track_buffer;
2092
        raw_cmd->length = 4 * F_SECT_PER_TRACK;
2093
 
2094
        /* allow for about 30ms for data transport per track */
2095
        head_shift  = (F_SECT_PER_TRACK + 5) / 6;
2096
 
2097
        /* a ``cylinder'' is two tracks plus a little stepping time */
2098
        track_shift = 2 * head_shift + 3;
2099
 
2100
        /* position of logical sector 1 on this track */
2101
        n = (track_shift * format_req.track + head_shift * format_req.head)
2102
                % F_SECT_PER_TRACK;
2103
 
2104
        /* determine interleave */
2105
        il = 1;
2106
        if (_floppy->fmt_gap < 0x22)
2107
                il++;
2108
 
2109
        /* initialize field */
2110
        for (count = 0; count < F_SECT_PER_TRACK; ++count) {
2111
                here[count].track = format_req.track;
2112
                here[count].head = format_req.head;
2113
                here[count].sect = 0;
2114
                here[count].size = F_SIZECODE;
2115
        }
2116
        /* place logical sectors */
2117
        for (count = 1; count <= F_SECT_PER_TRACK; ++count) {
2118
                here[n].sect = count;
2119
                n = (n+il) % F_SECT_PER_TRACK;
2120
                if (here[n].sect) { /* sector busy, find next free sector */
2121
                        ++n;
2122
                        if (n>= F_SECT_PER_TRACK) {
2123
                                n-=F_SECT_PER_TRACK;
2124
                                while (here[n].sect) ++n;
2125
                        }
2126
                }
2127
        }
2128
}
2129
 
2130
static void redo_format(void)
2131
{
2132
        buffer_track = -1;
2133
        setup_format_params(format_req.track << STRETCH(_floppy));
2134
        floppy_start();
2135
#ifdef DEBUGT
2136
        debugt("queue format request");
2137
#endif
2138
}
2139
 
2140
static struct cont_t format_cont={
2141
        format_interrupt,
2142
        redo_format,
2143
        bad_flp_intr,
2144
        generic_done };
2145
 
2146
static int do_format(kdev_t device, struct format_descr *tmp_format_req)
2147
{
2148
        int ret;
2149
        int drive=DRIVE(device);
2150
 
2151
        LOCK_FDC(drive,1);
2152
        set_floppy(device);
2153
        if (!_floppy ||
2154
            _floppy->track > DP->tracks ||
2155
            tmp_format_req->track >= _floppy->track ||
2156
            tmp_format_req->head >= _floppy->head ||
2157
            (_floppy->sect << 2) % (1 <<  FD_SIZECODE(_floppy)) ||
2158
            !_floppy->fmt_gap) {
2159
                process_fd_request();
2160
                return -EINVAL;
2161
        }
2162
        format_req = *tmp_format_req;
2163
        format_errors = 0;
2164
        cont = &format_cont;
2165
        errors = &format_errors;
2166
        IWAIT(redo_format);
2167
        process_fd_request();
2168
        return ret;
2169
}
2170
 
2171
/*
2172
 * Buffer read/write and support
2173
 * =============================
2174
 */
2175
 
2176
/* new request_done. Can handle physical sectors which are smaller than a
2177
 * logical buffer */
2178
static void request_done(int uptodate)
2179
{
2180
        int block;
2181
 
2182
        probing = 0;
2183
        reschedule_timeout(MAXTIMEOUT, "request done %d", uptodate);
2184
 
2185
        if (!CURRENT){
2186
                DPRINT("request list destroyed in floppy request done\n");
2187
                return;
2188
        }
2189
 
2190
        if (uptodate){
2191
                /* maintain values for invalidation on geometry
2192
                 * change */
2193
                block = current_count_sectors + CURRENT->sector;
2194
                INFBOUND(DRS->maxblock, block);
2195
                if (block > _floppy->sect)
2196
                        DRS->maxtrack = 1;
2197
 
2198
                /* unlock chained buffers */
2199
                while (current_count_sectors && CURRENT &&
2200
                       current_count_sectors >= CURRENT->current_nr_sectors){
2201
                        current_count_sectors -= CURRENT->current_nr_sectors;
2202
                        CURRENT->nr_sectors -= CURRENT->current_nr_sectors;
2203
                        CURRENT->sector += CURRENT->current_nr_sectors;
2204
                        end_request(1);
2205
                }
2206
                if (current_count_sectors && CURRENT){
2207
                        /* "unlock" last subsector */
2208
                        CURRENT->buffer += current_count_sectors <<9;
2209
                        CURRENT->current_nr_sectors -= current_count_sectors;
2210
                        CURRENT->nr_sectors -= current_count_sectors;
2211
                        CURRENT->sector += current_count_sectors;
2212
                        return;
2213
                }
2214
 
2215
                if (current_count_sectors && !CURRENT)
2216
                        DPRINT("request list destroyed in floppy request done\n");
2217
 
2218
        } else {
2219
                if (CURRENT->cmd == WRITE) {
2220
                        /* record write error information */
2221
                        DRWE->write_errors++;
2222
                        if (DRWE->write_errors == 1) {
2223
                                DRWE->first_error_sector = CURRENT->sector;
2224
                                DRWE->first_error_generation = DRS->generation;
2225
                        }
2226
                        DRWE->last_error_sector = CURRENT->sector;
2227
                        DRWE->last_error_generation = DRS->generation;
2228
                }
2229
                end_request(0);
2230
        }
2231
}
2232
 
2233
/* Interrupt handler evaluating the result of the r/w operation */
2234
static void rw_interrupt(void)
2235
{
2236
        int nr_sectors, ssize, eoc;
2237
 
2238
        if (!DRS->first_read_date)
2239
                DRS->first_read_date = jiffies;
2240
 
2241
        nr_sectors = 0;
2242
        CODE2SIZE;
2243
 
2244
        if(ST1 & ST1_EOC)
2245
                eoc = 1;
2246
        else
2247
                eoc = 0;
2248
        nr_sectors = ((R_TRACK-TRACK)*_floppy->head+R_HEAD-HEAD) *
2249
                _floppy->sect + ((R_SECTOR-SECTOR+eoc) <<  SIZECODE >> 2) -
2250
                (sector_t % _floppy->sect) % ssize;
2251
 
2252
#ifdef FLOPPY_SANITY_CHECK
2253
        if (nr_sectors > current_count_sectors + ssize -
2254
             (current_count_sectors + sector_t) % ssize +
2255
             sector_t % ssize){
2256
                DPRINT("long rw: %x instead of %lx\n",
2257
                        nr_sectors, current_count_sectors);
2258
                printk("rs=%d s=%d\n", R_SECTOR, SECTOR);
2259
                printk("rh=%d h=%d\n", R_HEAD, HEAD);
2260
                printk("rt=%d t=%d\n", R_TRACK, TRACK);
2261
                printk("spt=%d st=%d ss=%d\n", SECT_PER_TRACK,
2262
                       sector_t, ssize);
2263
        }
2264
#endif
2265
        INFBOUND(nr_sectors,0);
2266
        SUPBOUND(current_count_sectors, nr_sectors);
2267
 
2268
        switch (interpret_errors()){
2269
                case 2:
2270
                        cont->redo();
2271
                        return;
2272
                case 1:
2273
                        if (!current_count_sectors){
2274
                                cont->error();
2275
                                cont->redo();
2276
                                return;
2277
                        }
2278
                        break;
2279
                case 0:
2280
                        if (!current_count_sectors){
2281
                                cont->redo();
2282
                                return;
2283
                        }
2284
                        current_type[current_drive] = _floppy;
2285
                        floppy_sizes[TOMINOR(current_drive) ]= _floppy->size>>1;
2286
                        break;
2287
        }
2288
 
2289
        if (probing) {
2290
                if (DP->flags & FTD_MSG)
2291
                        DPRINT("Auto-detected floppy type %s in fd%d\n",
2292
                                _floppy->name,current_drive);
2293
                current_type[current_drive] = _floppy;
2294
                floppy_sizes[TOMINOR(current_drive)] = _floppy->size >> 1;
2295
                probing = 0;
2296
        }
2297
 
2298
        if (CT(COMMAND) != FD_READ ||
2299
             raw_cmd->kernel_data == CURRENT->buffer){
2300
                /* transfer directly from buffer */
2301
                cont->done(1);
2302
        } else if (CT(COMMAND) == FD_READ){
2303
                buffer_track = raw_cmd->track;
2304
                buffer_drive = current_drive;
2305
                INFBOUND(buffer_max, nr_sectors + sector_t);
2306
        }
2307
        cont->redo();
2308
}
2309
 
2310
/* Compute maximal contiguous buffer size. */
2311
static int buffer_chain_size(void)
2312
{
2313
        struct buffer_head *bh;
2314
        int size;
2315
        char *base;
2316
 
2317
        base = CURRENT->buffer;
2318
        size = CURRENT->current_nr_sectors << 9;
2319
        bh = CURRENT->bh;
2320
 
2321
        if (bh){
2322
                bh = bh->b_reqnext;
2323
                while (bh && bh->b_data == base + size){
2324
                        size += bh->b_size;
2325
                        bh = bh->b_reqnext;
2326
                }
2327
        }
2328
        return size >> 9;
2329
}
2330
 
2331
/* Compute the maximal transfer size */
2332
static int transfer_size(int ssize, int max_sector, int max_size)
2333
{
2334
        SUPBOUND(max_sector, sector_t + max_size);
2335
 
2336
        /* alignment */
2337
        max_sector -= (max_sector % _floppy->sect) % ssize;
2338
 
2339
        /* transfer size, beginning not aligned */
2340
        current_count_sectors = max_sector - sector_t ;
2341
 
2342
        return max_sector;
2343
}
2344
 
2345
/*
2346
 * Move data from/to the track buffer to/from the buffer cache.
2347
 */
2348
static void copy_buffer(int ssize, int max_sector, int max_sector_2)
2349
{
2350
        int remaining; /* number of transferred 512-byte sectors */
2351
        struct buffer_head *bh;
2352
        char *buffer, *dma_buffer;
2353
        int size;
2354
 
2355
        max_sector = transfer_size(ssize,
2356
                                   minimum(max_sector, max_sector_2),
2357
                                   CURRENT->nr_sectors);
2358
 
2359
        if (current_count_sectors <= 0 && CT(COMMAND) == FD_WRITE &&
2360
            buffer_max > sector_t + CURRENT->nr_sectors)
2361
                current_count_sectors = minimum(buffer_max - sector_t,
2362
                                                CURRENT->nr_sectors);
2363
 
2364
        remaining = current_count_sectors << 9;
2365
#ifdef FLOPPY_SANITY_CHECK
2366
        if ((remaining >> 9) > CURRENT->nr_sectors  &&
2367
            CT(COMMAND) == FD_WRITE){
2368
                DPRINT("in copy buffer\n");
2369
                printk("current_count_sectors=%ld\n", current_count_sectors);
2370
                printk("remaining=%d\n", remaining >> 9);
2371
                printk("CURRENT->nr_sectors=%ld\n",CURRENT->nr_sectors);
2372
                printk("CURRENT->current_nr_sectors=%ld\n",
2373
                       CURRENT->current_nr_sectors);
2374
                printk("max_sector=%d\n", max_sector);
2375
                printk("ssize=%d\n", ssize);
2376
        }
2377
#endif
2378
 
2379
        buffer_max = maximum(max_sector, buffer_max);
2380
 
2381
        dma_buffer = floppy_track_buffer + ((sector_t - buffer_min) << 9);
2382
 
2383
        bh = CURRENT->bh;
2384
        size = CURRENT->current_nr_sectors << 9;
2385
        buffer = CURRENT->buffer;
2386
 
2387
        while (remaining > 0){
2388
                SUPBOUND(size, remaining);
2389
#ifdef FLOPPY_SANITY_CHECK
2390
                if (dma_buffer + size >
2391
                    floppy_track_buffer + (max_buffer_sectors << 10) ||
2392
                    dma_buffer < floppy_track_buffer){
2393
                        DPRINT("buffer overrun in copy buffer %d\n",
2394
                                (int) ((floppy_track_buffer - dma_buffer) >>9));
2395
                        printk("sector_t=%d buffer_min=%d\n",
2396
                               sector_t, buffer_min);
2397
                        printk("current_count_sectors=%ld\n",
2398
                               current_count_sectors);
2399
                        if (CT(COMMAND) == FD_READ)
2400
                                printk("read\n");
2401
                        if (CT(COMMAND) == FD_READ)
2402
                                printk("write\n");
2403
                        break;
2404
                }
2405
                if (((unsigned long)buffer) % 512)
2406
                        DPRINT("%p buffer not aligned\n", buffer);
2407
#endif
2408
                if (CT(COMMAND) == FD_READ)
2409
                        memcpy(buffer, dma_buffer, size);
2410
                else
2411
                        memcpy(dma_buffer, buffer, size);
2412
                remaining -= size;
2413
                if (!remaining)
2414
                        break;
2415
 
2416
                dma_buffer += size;
2417
                bh = bh->b_reqnext;
2418
#ifdef FLOPPY_SANITY_CHECK
2419
                if (!bh){
2420
                        DPRINT("bh=null in copy buffer after copy\n");
2421
                        break;
2422
                }
2423
#endif
2424
                size = bh->b_size;
2425
                buffer = bh->b_data;
2426
        }
2427
#ifdef FLOPPY_SANITY_CHECK
2428
        if (remaining){
2429
                if (remaining > 0)
2430
                        max_sector -= remaining >> 9;
2431
                DPRINT("weirdness: remaining %d\n", remaining>>9);
2432
        }
2433
#endif
2434
}
2435
 
2436
/*
2437
 * Formulate a read/write request.
2438
 * this routine decides where to load the data (directly to buffer, or to
2439
 * tmp floppy area), how much data to load (the size of the buffer, the whole
2440
 * track, or a single sector)
2441
 * All floppy_track_buffer handling goes in here. If we ever add track buffer
2442
 * allocation on the fly, it should be done here. No other part should need
2443
 * modification.
2444
 */
2445
 
2446
static int make_raw_rw_request(void)
2447
{
2448
        int aligned_sector_t;
2449
        int max_sector, max_size, tracksize, ssize;
2450
 
2451
        set_fdc(DRIVE(CURRENT->rq_dev));
2452
 
2453
        raw_cmd = &default_raw_cmd;
2454
        raw_cmd->flags = FD_RAW_SPIN | FD_RAW_NEED_DISK | FD_RAW_NEED_DISK |
2455
                FD_RAW_NEED_SEEK;
2456
        raw_cmd->cmd_count = NR_RW;
2457
        if (CURRENT->cmd == READ){
2458
                raw_cmd->flags |= FD_RAW_READ;
2459
                COMMAND = FM_MODE(_floppy,FD_READ);
2460
        } else if (CURRENT->cmd == WRITE){
2461
                raw_cmd->flags |= FD_RAW_WRITE;
2462
                COMMAND = FM_MODE(_floppy,FD_WRITE);
2463
        } else {
2464
                DPRINT("make_raw_rw_request: unknown command\n");
2465
                return 0;
2466
        }
2467
 
2468
        max_sector = _floppy->sect * _floppy->head;
2469
 
2470
        TRACK = CURRENT->sector / max_sector;
2471
        sector_t = CURRENT->sector % max_sector;
2472
        if (_floppy->track && TRACK >= _floppy->track)
2473
                return 0;
2474
        HEAD = sector_t / _floppy->sect;
2475
 
2476
        if (((_floppy->stretch & FD_SWAPSIDES) || TESTF(FD_NEED_TWADDLE)) &&
2477
            sector_t < _floppy->sect)
2478
                max_sector = _floppy->sect;
2479
 
2480
        /* 2M disks have phantom sectors on the first track */
2481
        if ((_floppy->rate & FD_2M) && (!TRACK) && (!HEAD)){
2482
                max_sector = 2 * _floppy->sect / 3;
2483
                if (sector_t >= max_sector){
2484
                        current_count_sectors = minimum(_floppy->sect - sector_t,
2485
                                                        CURRENT->nr_sectors);
2486
                        return 1;
2487
                }
2488
                SIZECODE = 2;
2489
        } else
2490
                SIZECODE = FD_SIZECODE(_floppy);
2491
        raw_cmd->rate = _floppy->rate & 0x43;
2492
        if ((_floppy->rate & FD_2M) &&
2493
            (TRACK || HEAD) &&
2494
            raw_cmd->rate == 2)
2495
                raw_cmd->rate = 1;
2496
 
2497
        if (SIZECODE)
2498
                SIZECODE2 = 0xff;
2499
        else
2500
                SIZECODE2 = 0x80;
2501
        raw_cmd->track = TRACK << STRETCH(_floppy);
2502
        DR_SELECT = UNIT(current_drive) + PH_HEAD(_floppy,HEAD);
2503
        GAP = _floppy->gap;
2504
        CODE2SIZE;
2505
        SECT_PER_TRACK = _floppy->sect << 2 >> SIZECODE;
2506
        SECTOR = ((sector_t % _floppy->sect) << 2 >> SIZECODE) + 1;
2507
        tracksize = _floppy->sect - _floppy->sect % ssize;
2508
        if (tracksize < _floppy->sect){
2509
                SECT_PER_TRACK ++;
2510
                if (tracksize <= sector_t % _floppy->sect)
2511
                        SECTOR--;
2512
                while (tracksize <= sector_t % _floppy->sect){
2513
                        while(tracksize + ssize > _floppy->sect){
2514
                                SIZECODE--;
2515
                                ssize >>= 1;
2516
                        }
2517
                        SECTOR++; SECT_PER_TRACK ++;
2518
                        tracksize += ssize;
2519
                }
2520
                max_sector = HEAD * _floppy->sect + tracksize;
2521
        } else if (!TRACK && !HEAD && !(_floppy->rate & FD_2M) && probing)
2522
                max_sector = _floppy->sect;
2523
 
2524
        aligned_sector_t = sector_t - (sector_t % _floppy->sect) % ssize;
2525
        max_size = CURRENT->nr_sectors;
2526
        if ((raw_cmd->track == buffer_track) &&
2527
            (current_drive == buffer_drive) &&
2528
            (sector_t >= buffer_min) && (sector_t < buffer_max)) {
2529
                /* data already in track buffer */
2530
                if (CT(COMMAND) == FD_READ) {
2531
                        copy_buffer(1, max_sector, buffer_max);
2532
                        return 1;
2533
                }
2534
        } else if (aligned_sector_t != sector_t || CURRENT->nr_sectors < ssize){
2535
                if (CT(COMMAND) == FD_WRITE){
2536
                        if (sector_t + CURRENT->nr_sectors > ssize &&
2537
                            sector_t + CURRENT->nr_sectors < ssize + ssize)
2538
                                max_size = ssize + ssize;
2539
                        else
2540
                                max_size = ssize;
2541
                }
2542
                raw_cmd->flags &= ~FD_RAW_WRITE;
2543
                raw_cmd->flags |= FD_RAW_READ;
2544
                COMMAND = FM_MODE(_floppy,FD_READ);
2545
        } else if ((unsigned long)CURRENT->buffer < MAX_DMA_ADDRESS) {
2546
                unsigned long dma_limit;
2547
                int direct, indirect;
2548
 
2549
                indirect= transfer_size(ssize,max_sector,max_buffer_sectors*2) -
2550
                        sector_t;
2551
 
2552
                /*
2553
                 * Do NOT use minimum() here---MAX_DMA_ADDRESS is 64 bits wide
2554
                 * on a 64 bit machine!
2555
                 */
2556
                max_size = buffer_chain_size();
2557
#ifndef CONFIG_ARM
2558
                dma_limit = (MAX_DMA_ADDRESS - ((unsigned long) CURRENT->buffer)) >> 9;
2559
                if ((unsigned long) max_size > dma_limit) {
2560
                        max_size = dma_limit;
2561
                }
2562
                /* 64 kb boundaries */
2563
                if (CROSS_64KB(CURRENT->buffer, max_size << 9))
2564
                        max_size = (K_64 - ((long) CURRENT->buffer) % K_64)>>9;
2565
#endif
2566
                direct = transfer_size(ssize,max_sector,max_size) - sector_t;
2567
                /*
2568
                 * We try to read tracks, but if we get too many errors, we
2569
                 * go back to reading just one sector at a time.
2570
                 *
2571
                 * This means we should be able to read a sector even if there
2572
                 * are other bad sectors on this track.
2573
                 */
2574
                if (!direct ||
2575
                    (indirect * 2 > direct * 3 &&
2576
                     *errors < DP->max_errors.read_track &&
2577
                     /*!TESTF(FD_NEED_TWADDLE) &&*/
2578
                     ((!probing || (DP->read_track&(1<<DRS->probed_format)))))){
2579
                        max_size = CURRENT->nr_sectors;
2580
                } else {
2581
                        raw_cmd->kernel_data = CURRENT->buffer;
2582
                        raw_cmd->length = current_count_sectors << 9;
2583
                        if (raw_cmd->length == 0){
2584
                                DPRINT("zero dma transfer attempted from make_raw_request\n");
2585
                                DPRINT("indirect=%d direct=%d sector_t=%d",
2586
                                        indirect, direct, sector_t);
2587
                                return 0;
2588
                        }
2589
                        return 2;
2590
                }
2591
        }
2592
 
2593
        if (CT(COMMAND) == FD_READ)
2594
                max_size = max_sector; /* unbounded */
2595
 
2596
        /* claim buffer track if needed */
2597
        if (buffer_track != raw_cmd->track ||  /* bad track */
2598
            buffer_drive !=current_drive || /* bad drive */
2599
            sector_t > buffer_max ||
2600
            sector_t < buffer_min ||
2601
            ((CT(COMMAND) == FD_READ ||
2602
              (aligned_sector_t == sector_t && CURRENT->nr_sectors >= ssize))&&
2603
             max_sector > 2 * max_buffer_sectors + buffer_min &&
2604
             max_size + sector_t > 2 * max_buffer_sectors + buffer_min)
2605
            /* not enough space */){
2606
                buffer_track = -1;
2607
                buffer_drive = current_drive;
2608
                buffer_max = buffer_min = aligned_sector_t;
2609
        }
2610
        raw_cmd->kernel_data = floppy_track_buffer +
2611
                ((aligned_sector_t-buffer_min)<<9);
2612
 
2613
        if (CT(COMMAND) == FD_WRITE){
2614
                /* copy write buffer to track buffer.
2615
                 * if we get here, we know that the write
2616
                 * is either aligned or the data already in the buffer
2617
                 * (buffer will be overwritten) */
2618
#ifdef FLOPPY_SANITY_CHECK
2619
                if (sector_t != aligned_sector_t && buffer_track == -1)
2620
                        DPRINT("internal error offset !=0 on write\n");
2621
#endif
2622
                buffer_track = raw_cmd->track;
2623
                buffer_drive = current_drive;
2624
                copy_buffer(ssize, max_sector, 2*max_buffer_sectors+buffer_min);
2625
        } else
2626
                transfer_size(ssize, max_sector,
2627
                              2*max_buffer_sectors+buffer_min-aligned_sector_t);
2628
 
2629
        /* round up current_count_sectors to get dma xfer size */
2630
        raw_cmd->length = sector_t+current_count_sectors-aligned_sector_t;
2631
        raw_cmd->length = ((raw_cmd->length -1)|(ssize-1))+1;
2632
        raw_cmd->length <<= 9;
2633
#ifdef FLOPPY_SANITY_CHECK
2634
        if ((raw_cmd->length < current_count_sectors << 9) ||
2635
            (raw_cmd->kernel_data != CURRENT->buffer &&
2636
             CT(COMMAND) == FD_WRITE &&
2637
             (aligned_sector_t + (raw_cmd->length >> 9) > buffer_max ||
2638
              aligned_sector_t < buffer_min)) ||
2639
            raw_cmd->length % (128 << SIZECODE) ||
2640
            raw_cmd->length <= 0 || current_count_sectors <= 0){
2641
                DPRINT("fractionary current count b=%lx s=%lx\n",
2642
                        raw_cmd->length, current_count_sectors);
2643
                if (raw_cmd->kernel_data != CURRENT->buffer)
2644
                        printk("addr=%d, length=%ld\n",
2645
                               (int) ((raw_cmd->kernel_data -
2646
                                       floppy_track_buffer) >> 9),
2647
                               current_count_sectors);
2648
                printk("st=%d ast=%d mse=%d msi=%d\n",
2649
                       sector_t, aligned_sector_t, max_sector, max_size);
2650
                printk("ssize=%x SIZECODE=%d\n", ssize, SIZECODE);
2651
                printk("command=%x SECTOR=%d HEAD=%d, TRACK=%d\n",
2652
                       COMMAND, SECTOR, HEAD, TRACK);
2653
                printk("buffer drive=%d\n", buffer_drive);
2654
                printk("buffer track=%d\n", buffer_track);
2655
                printk("buffer_min=%d\n", buffer_min);
2656
                printk("buffer_max=%d\n", buffer_max);
2657
                return 0;
2658
        }
2659
 
2660
        if (raw_cmd->kernel_data != CURRENT->buffer){
2661
                if (raw_cmd->kernel_data < floppy_track_buffer ||
2662
                    current_count_sectors < 0 ||
2663
                    raw_cmd->length < 0 ||
2664
                    raw_cmd->kernel_data + raw_cmd->length >
2665
                    floppy_track_buffer + (max_buffer_sectors  << 10)){
2666
                        DPRINT("buffer overrun in schedule dma\n");
2667
                        printk("sector_t=%d buffer_min=%d current_count=%ld\n",
2668
                               sector_t, buffer_min,
2669
                               raw_cmd->length >> 9);
2670
                        printk("current_count_sectors=%ld\n",
2671
                               current_count_sectors);
2672
                        if (CT(COMMAND) == FD_READ)
2673
                                printk("read\n");
2674
                        if (CT(COMMAND) == FD_READ)
2675
                                printk("write\n");
2676
                        return 0;
2677
                }
2678
        } else if (raw_cmd->length > CURRENT->nr_sectors << 9 ||
2679
                   current_count_sectors > CURRENT->nr_sectors){
2680
                DPRINT("buffer overrun in direct transfer\n");
2681
                return 0;
2682
        } else if (raw_cmd->length < current_count_sectors << 9){
2683
                DPRINT("more sectors than bytes\n");
2684
                printk("bytes=%ld\n", raw_cmd->length >> 9);
2685
                printk("sectors=%ld\n", current_count_sectors);
2686
        }
2687
        if (raw_cmd->length == 0){
2688
                DPRINT("zero dma transfer attempted from make_raw_request\n");
2689
                return 0;
2690
        }
2691
#endif
2692
        return 2;
2693
}
2694
 
2695
static void redo_fd_request(void)
2696
{
2697
#define REPEAT {request_done(0); continue; }
2698
        kdev_t device;
2699
        int tmp;
2700
 
2701
        lastredo = jiffies;
2702
        if (current_drive < N_DRIVE)
2703
                floppy_off(current_drive);
2704
 
2705
        if (CURRENT && CURRENT->rq_status == RQ_INACTIVE){
2706
                CLEAR_INTR;
2707
                unlock_fdc();
2708
                return;
2709
        }
2710
 
2711
        while(1){
2712
                if (!CURRENT) {
2713
                        CLEAR_INTR;
2714
                        unlock_fdc();
2715
                        return;
2716
                }
2717
                if (MAJOR(CURRENT->rq_dev) != MAJOR_NR)
2718
                        panic(DEVICE_NAME ": request list destroyed");
2719
                if (CURRENT->bh && !buffer_locked(CURRENT->bh))
2720
                        panic(DEVICE_NAME ": block not locked");
2721
 
2722
                device = CURRENT->rq_dev;
2723
                set_fdc(DRIVE(device));
2724
                reschedule_timeout(CURRENTD, "redo fd request", 0);
2725
 
2726
                set_floppy(device);
2727
                raw_cmd = & default_raw_cmd;
2728
                raw_cmd->flags = 0;
2729
                if (start_motor(redo_fd_request)) return;
2730
                disk_change(current_drive);
2731
                if (test_bit(current_drive, &fake_change) ||
2732
                   TESTF(FD_DISK_CHANGED)){
2733
                        DPRINT("disk absent or changed during operation\n");
2734
                        REPEAT;
2735
                }
2736
                if (!_floppy) { /* Autodetection */
2737
                        if (!probing){
2738
                                DRS->probed_format = 0;
2739
                                if (next_valid_format()){
2740
                                        DPRINT("no autodetectable formats\n");
2741
                                        _floppy = NULL;
2742
                                        REPEAT;
2743
                                }
2744
                        }
2745
                        probing = 1;
2746
                        _floppy = floppy_type+DP->autodetect[DRS->probed_format];
2747
                } else
2748
                        probing = 0;
2749
                errors = & (CURRENT->errors);
2750
                tmp = make_raw_rw_request();
2751
                if (tmp < 2){
2752
                        request_done(tmp);
2753
                        continue;
2754
                }
2755
 
2756
                if (TESTF(FD_NEED_TWADDLE))
2757
                        twaddle();
2758
                floppy_tq.routine = (void *)(void *) floppy_start;
2759
                queue_task(&floppy_tq, &tq_immediate);
2760
                mark_bh(IMMEDIATE_BH);
2761
#ifdef DEBUGT
2762
                debugt("queue fd request");
2763
#endif
2764
                return;
2765
        }
2766
#undef REPEAT
2767
}
2768
 
2769
static struct cont_t rw_cont={
2770
        rw_interrupt,
2771
        redo_fd_request,
2772
        bad_flp_intr,
2773
        request_done };
2774
 
2775
static struct tq_struct request_tq =
2776
{ 0, 0, (void *) (void *) redo_fd_request, 0 };
2777
 
2778
static void process_fd_request(void)
2779
{
2780
        cont = &rw_cont;
2781
        queue_task(&request_tq, &tq_immediate);
2782
        mark_bh(IMMEDIATE_BH);
2783
}
2784
 
2785
static void do_fd_request(void)
2786
{
2787
        sti();
2788
        if (fdc_busy){
2789
                /* fdc busy, this new request will be treated when the
2790
                   current one is done */
2791
                is_alive("do fd request, old request running");
2792
                return;
2793
        }
2794
        lock_fdc(MAXTIMEOUT,0);
2795
        process_fd_request();
2796
        is_alive("do fd request");
2797
}
2798
 
2799
static struct cont_t poll_cont={
2800
        success_and_wakeup,
2801
        floppy_ready,
2802
        generic_failure,
2803
        generic_done };
2804
 
2805
static int poll_drive(int interruptible, int flag)
2806
{
2807
        int ret;
2808
        /* no auto-sense, just clear dcl */
2809
        raw_cmd = &default_raw_cmd;
2810
        raw_cmd->flags= flag;
2811
        raw_cmd->track=0;
2812
        raw_cmd->cmd_count=0;
2813
        cont = &poll_cont;
2814
#ifdef DCL_DEBUG
2815
        if (DP->flags & FD_DEBUG){
2816
                DPRINT("setting NEWCHANGE in poll_drive\n");
2817
        }
2818
#endif
2819
        SETF(FD_DISK_NEWCHANGE);
2820
        WAIT(floppy_ready);
2821
        return ret;
2822
}
2823
 
2824
/*
2825
 * User triggered reset
2826
 * ====================
2827
 */
2828
 
2829
static void reset_intr(void)
2830
{
2831
        printk("weird, reset interrupt called\n");
2832
}
2833
 
2834
static struct cont_t reset_cont={
2835
        reset_intr,
2836
        success_and_wakeup,
2837
        generic_failure,
2838
        generic_done };
2839
 
2840
static int user_reset_fdc(int drive, int arg, int interruptible)
2841
{
2842
        int ret;
2843
 
2844
        ret=0;
2845
        LOCK_FDC(drive,interruptible);
2846
        if (arg == FD_RESET_ALWAYS)
2847
                FDCS->reset=1;
2848
        if (FDCS->reset){
2849
                cont = &reset_cont;
2850
                WAIT(reset_fdc);
2851
        }
2852
        process_fd_request();
2853
        return ret;
2854
}
2855
 
2856
/*
2857
 * Misc Ioctl's and support
2858
 * ========================
2859
 */
2860
static int fd_copyout(void *param, const void *address, int size)
2861
{
2862
        int ret;
2863
 
2864
        ECALL(verify_area(VERIFY_WRITE,param,size));
2865
        memcpy_tofs(param,(void *) address, size);
2866
        return 0;
2867
}
2868
 
2869
static int fd_copyin(void *param, void *address, int size)
2870
{
2871
        int ret;
2872
 
2873
        ECALL(verify_area(VERIFY_READ,param,size));
2874
        memcpy_fromfs((void *) address, param, size);
2875
        return 0;
2876
}
2877
 
2878
#define COPYOUT(x) ECALL(fd_copyout((void *)param, &(x), sizeof(x)))
2879
#define COPYIN(x) ECALL(fd_copyin((void *)param, &(x), sizeof(x)))
2880
 
2881
static inline const char *drive_name(int type, int drive)
2882
{
2883
        struct floppy_struct *floppy;
2884
 
2885
        if (type)
2886
                floppy = floppy_type + type;
2887
        else {
2888
                if (UDP->native_format)
2889
                        floppy = floppy_type + UDP->native_format;
2890
                else
2891
                        return "(null)";
2892
        }
2893
        if (floppy->name)
2894
                return floppy->name;
2895
        else
2896
                return "(null)";
2897
}
2898
 
2899
 
2900
/* raw commands */
2901
static void raw_cmd_done(int flag)
2902
{
2903
        int i;
2904
 
2905
        if (!flag) {
2906
                raw_cmd->flags |= FD_RAW_FAILURE;
2907
                raw_cmd->flags |= FD_RAW_HARDFAILURE;
2908
        } else {
2909
                raw_cmd->reply_count = inr;
2910
                for (i=0; i< raw_cmd->reply_count; i++)
2911
                        raw_cmd->reply[i] = reply_buffer[i];
2912
 
2913
                if (raw_cmd->flags & (FD_RAW_READ | FD_RAW_WRITE))
2914
                        raw_cmd->length = fd_get_dma_residue();
2915
 
2916
                if ((raw_cmd->flags & FD_RAW_SOFTFAILURE) &&
2917
                    (!raw_cmd->reply_count || (raw_cmd->reply[0] & 0xc0)))
2918
                        raw_cmd->flags |= FD_RAW_FAILURE;
2919
 
2920
                if (disk_change(current_drive))
2921
                        raw_cmd->flags |= FD_RAW_DISK_CHANGE;
2922
                else
2923
                        raw_cmd->flags &= ~FD_RAW_DISK_CHANGE;
2924
                if (raw_cmd->flags & FD_RAW_NO_MOTOR_AFTER)
2925
                        motor_off_callback(current_drive);
2926
 
2927
                if (raw_cmd->next &&
2928
                   (!(raw_cmd->flags & FD_RAW_FAILURE) ||
2929
                    !(raw_cmd->flags & FD_RAW_STOP_IF_FAILURE)) &&
2930
                   ((raw_cmd->flags & FD_RAW_FAILURE) ||
2931
                    !(raw_cmd->flags &FD_RAW_STOP_IF_SUCCESS))) {
2932
                        raw_cmd = raw_cmd->next;
2933
                        return;
2934
                }
2935
        }
2936
        generic_done(flag);
2937
}
2938
 
2939
 
2940
static struct cont_t raw_cmd_cont={
2941
        success_and_wakeup,
2942
        floppy_start,
2943
        generic_failure,
2944
        raw_cmd_done
2945
};
2946
 
2947
static inline int raw_cmd_copyout(int cmd, char *param,
2948
                                  struct floppy_raw_cmd *ptr)
2949
{
2950
        struct old_floppy_raw_cmd old_raw_cmd;
2951
        int ret;
2952
 
2953
        while(ptr) {
2954
                if (cmd == OLDFDRAWCMD) {
2955
                        old_raw_cmd.flags = ptr->flags;
2956
                        old_raw_cmd.data = ptr->data;
2957
                        old_raw_cmd.length = ptr->length;
2958
                        old_raw_cmd.rate = ptr->rate;
2959
                        old_raw_cmd.reply_count = ptr->reply_count;
2960
                        memcpy(old_raw_cmd.reply, ptr->reply, 7);
2961
                        COPYOUT(old_raw_cmd);
2962
                        param += sizeof(old_raw_cmd);
2963
                } else {
2964
                        COPYOUT(*ptr);
2965
                        param += sizeof(struct floppy_raw_cmd);
2966
                }
2967
 
2968
                if ((ptr->flags & FD_RAW_READ) && ptr->buffer_length){
2969
                        if (ptr->length>=0 && ptr->length<=ptr->buffer_length)
2970
                                ECALL(fd_copyout(ptr->data,
2971
                                                 ptr->kernel_data,
2972
                                                 ptr->buffer_length -
2973
                                                 ptr->length));
2974
                }
2975
                ptr = ptr->next;
2976
        }
2977
        return 0;
2978
}
2979
 
2980
 
2981
static void raw_cmd_free(struct floppy_raw_cmd **ptr)
2982
{
2983
        struct floppy_raw_cmd *next,*this;
2984
 
2985
        this = *ptr;
2986
        *ptr = 0;
2987
        while(this) {
2988
                if (this->buffer_length) {
2989
                        fd_dma_mem_free((unsigned long)this->kernel_data,
2990
                                        this->buffer_length);
2991
                        this->buffer_length = 0;
2992
                }
2993
                next = this->next;
2994
                kfree(this);
2995
                this = next;
2996
        }
2997
}
2998
 
2999
 
3000
static inline int raw_cmd_copyin(int cmd, char *param,
3001
                                 struct floppy_raw_cmd **rcmd)
3002
{
3003
        struct floppy_raw_cmd *ptr;
3004
        struct old_floppy_raw_cmd old_raw_cmd;
3005
        int ret;
3006
        int i;
3007
 
3008
        *rcmd = 0;
3009
        while(1) {
3010
                ptr = (struct floppy_raw_cmd *)
3011
                        kmalloc(sizeof(struct floppy_raw_cmd), GFP_USER);
3012
                if (!ptr)
3013
                        return -ENOMEM;
3014
                *rcmd = ptr;
3015
                if (cmd == OLDFDRAWCMD){
3016
                        COPYIN(old_raw_cmd);
3017
                        ptr->flags = old_raw_cmd.flags;
3018
                        ptr->data = old_raw_cmd.data;
3019
                        ptr->length = old_raw_cmd.length;
3020
                        ptr->rate = old_raw_cmd.rate;
3021
                        ptr->cmd_count = old_raw_cmd.cmd_count;
3022
                        ptr->track = old_raw_cmd.track;
3023
                        ptr->phys_length = 0;
3024
                        ptr->next = 0;
3025
                        ptr->buffer_length = 0;
3026
                        memcpy(ptr->cmd, old_raw_cmd.cmd, 9);
3027
                        param += sizeof(struct old_floppy_raw_cmd);
3028
                        if (ptr->cmd_count > 9)
3029
                                return -EINVAL;
3030
                } else {
3031
                        COPYIN(*ptr);
3032
                        ptr->next = 0;
3033
                        ptr->buffer_length = 0;
3034
                        param += sizeof(struct floppy_raw_cmd);
3035
                        if (ptr->cmd_count > 33)
3036
                                /* the command may now also take up the space
3037
                                 * initially intended for the reply & the
3038
                                 * reply count. Needed for long 82078 commands
3039
                                 * such as RESTORE, which takes ... 17 command
3040
                                 * bytes. Murphy's law #137: When you reserve
3041
                                 * 16 bytes for a structure, you'll one day
3042
                                 * discover that you really need 17...
3043
                                 */
3044
                                return -EINVAL;
3045
                }
3046
 
3047
                for (i=0; i< 16; i++)
3048
                        ptr->reply[i] = 0;
3049
                ptr->resultcode = 0;
3050
                ptr->kernel_data = 0;
3051
 
3052
                if (ptr->flags & (FD_RAW_READ | FD_RAW_WRITE)) {
3053
                        if (ptr->length <= 0)
3054
                                return -EINVAL;
3055
                        ptr->kernel_data =(char*)fd_dma_mem_alloc(ptr->length);
3056
                        if (!ptr->kernel_data)
3057
                                return -ENOMEM;
3058
                        ptr->buffer_length = ptr->length;
3059
                }
3060
                if ( ptr->flags & FD_RAW_READ )
3061
                    ECALL( verify_area( VERIFY_WRITE, ptr->data,
3062
                                        ptr->length ));
3063
                if (ptr->flags & FD_RAW_WRITE)
3064
                        ECALL(fd_copyin(ptr->data, ptr->kernel_data,
3065
                                        ptr->length));
3066
                rcmd = & (ptr->next);
3067
                if (!(ptr->flags & FD_RAW_MORE))
3068
                        return 0;
3069
                ptr->rate &= 0x43;
3070
        }
3071
}
3072
 
3073
 
3074
static int raw_cmd_ioctl(int cmd, void *param)
3075
{
3076
        int drive, ret, ret2;
3077
        struct floppy_raw_cmd *my_raw_cmd;
3078
 
3079
        if (FDCS->rawcmd <= 1)
3080
                FDCS->rawcmd = 1;
3081
        for (drive= 0; drive < N_DRIVE; drive++){
3082
                if (FDC(drive) != fdc)
3083
                        continue;
3084
                if (drive == current_drive){
3085
                        if (UDRS->fd_ref > 1){
3086
                                FDCS->rawcmd = 2;
3087
                                break;
3088
                        }
3089
                } else if (UDRS->fd_ref){
3090
                        FDCS->rawcmd = 2;
3091
                        break;
3092
                }
3093
        }
3094
 
3095
        if (FDCS->reset)
3096
                return -EIO;
3097
 
3098
        ret = raw_cmd_copyin(cmd, param, &my_raw_cmd);
3099
        if (ret) {
3100
                raw_cmd_free(&my_raw_cmd);
3101
                return ret;
3102
        }
3103
 
3104
        raw_cmd = my_raw_cmd;
3105
        cont = &raw_cmd_cont;
3106
        ret=wait_til_done(floppy_start,1);
3107
#ifdef DCL_DEBUG
3108
        if (DP->flags & FD_DEBUG){
3109
                DPRINT("calling disk change from raw_cmd ioctl\n");
3110
        }
3111
#endif
3112
 
3113
        if (ret != -EINTR && FDCS->reset)
3114
                ret = -EIO;
3115
 
3116
        DRS->track = NO_TRACK;
3117
 
3118
        ret2 = raw_cmd_copyout(cmd, param, my_raw_cmd);
3119
        if (!ret)
3120
                ret = ret2;
3121
        raw_cmd_free(&my_raw_cmd);
3122
        return ret;
3123
}
3124
 
3125
static int invalidate_drive(kdev_t rdev)
3126
{
3127
        /* invalidate the buffer track to force a reread */
3128
        set_bit(DRIVE(rdev), &fake_change);
3129
        process_fd_request();
3130
        check_disk_change(rdev);
3131
        return 0;
3132
}
3133
 
3134
 
3135
static inline void clear_write_error(int drive)
3136
{
3137
        CLEARSTRUCT(UDRWE);
3138
}
3139
 
3140
static inline int set_geometry(unsigned int cmd, struct floppy_struct *g,
3141
                               int drive, int type, kdev_t device)
3142
{
3143
        int cnt;
3144
 
3145
        /* sanity checking for parameters.*/
3146
        if (g->sect <= 0 ||
3147
            g->head <= 0 ||
3148
            g->track <= 0 ||
3149
            g->track > UDP->tracks>>STRETCH(g) ||
3150
            /* check if reserved bits are set */
3151
            (g->stretch&~(FD_STRETCH|FD_SWAPSIDES)) != 0)
3152
                return -EINVAL;
3153
        if (type){
3154
                if (!suser())
3155
                        return -EPERM;
3156
                LOCK_FDC(drive,1);
3157
                for (cnt = 0; cnt < N_DRIVE; cnt++){
3158
                        if (ITYPE(drive_state[cnt].fd_device) == type &&
3159
                            drive_state[cnt].fd_ref)
3160
                                set_bit(drive, &fake_change);
3161
                }
3162
                floppy_type[type] = *g;
3163
                floppy_type[type].name="user format";
3164
                for (cnt = type << 2; cnt < (type << 2) + 4; cnt++)
3165
                        floppy_sizes[cnt]= floppy_sizes[cnt+0x80]=
3166
                                floppy_type[type].size>>1;
3167
                process_fd_request();
3168
                for (cnt = 0; cnt < N_DRIVE; cnt++){
3169
                        if (ITYPE(drive_state[cnt].fd_device) == type &&
3170
                            drive_state[cnt].fd_ref)
3171
                                check_disk_change(
3172
                                        MKDEV(FLOPPY_MAJOR,
3173
                                              drive_state[cnt].fd_device));
3174
                }
3175
        } else {
3176
                LOCK_FDC(drive,1);
3177
                if (cmd != FDDEFPRM)
3178
                        /* notice a disk change immediately, else
3179
                         * we loose our settings immediately*/
3180
                        CALL(poll_drive(1, FD_RAW_NEED_DISK));
3181
                user_params[drive] = *g;
3182
                if (buffer_drive == drive)
3183
                        SUPBOUND(buffer_max, user_params[drive].sect);
3184
                current_type[drive] = &user_params[drive];
3185
                floppy_sizes[drive] = user_params[drive].size >> 1;
3186
                if (cmd == FDDEFPRM)
3187
                        DRS->keep_data = -1;
3188
                else
3189
                        DRS->keep_data = 1;
3190
                /* invalidation. Invalidate only when needed, i.e.
3191
                 * when there are already sectors in the buffer cache
3192
                 * whose number will change. This is useful, because
3193
                 * mtools often changes the geometry of the disk after
3194
                 * looking at the boot block */
3195
                if (DRS->maxblock > user_params[drive].sect || DRS->maxtrack)
3196
                        invalidate_drive(device);
3197
                else
3198
                        process_fd_request();
3199
        }
3200
        return 0;
3201
}
3202
 
3203
/* handle obsolete ioctl's */
3204
static struct translation_entry {
3205
    int newcmd;
3206
    int oldcmd;
3207
    int oldsize; /* size of 0x00xx-style ioctl. Reflects old structures, thus
3208
                  * use numeric values. NO SIZEOFS */
3209
} translation_table[]= {
3210
    {FDCLRPRM,           0,  0},
3211
    {FDSETPRM,           1, 28},
3212
    {FDDEFPRM,           2, 28},
3213
    {FDGETPRM,           3, 28},
3214
    {FDMSGON,            4,  0},
3215
    {FDMSGOFF,           5,  0},
3216
    {FDFMTBEG,           6,  0},
3217
    {FDFMTTRK,           7, 12},
3218
    {FDFMTEND,           8,  0},
3219
    {FDSETEMSGTRESH,    10,  0},
3220
    {FDFLUSH,           11,  0},
3221
    {FDSETMAXERRS,      12, 20},
3222
    {OLDFDRAWCMD,       30,  0},
3223
    {FDGETMAXERRS,      14, 20},
3224
    {FDGETDRVTYP,       16, 16},
3225
    {FDSETDRVPRM,       20, 88},
3226
    {FDGETDRVPRM,       21, 88},
3227
    {FDGETDRVSTAT,      22, 52},
3228
    {FDPOLLDRVSTAT,     23, 52},
3229
    {FDRESET,           24,  0},
3230
    {FDGETFDCSTAT,      25, 40},
3231
    {FDWERRORCLR,       27,  0},
3232
    {FDWERRORGET,       28, 24},
3233
    {FDRAWCMD,           0,  0},
3234
    {FDEJECT,            0,  0},
3235
    {FDTWADDLE,         40,  0} };
3236
 
3237
static inline int normalize_0x02xx_ioctl(int *cmd, int *size)
3238
{
3239
        int i;
3240
 
3241
        for (i=0; i < ARRAY_SIZE(translation_table); i++) {
3242
                if ((*cmd & 0xffff) == (translation_table[i].newcmd & 0xffff)){
3243
                        *size = _IOC_SIZE(*cmd);
3244
                        *cmd = translation_table[i].newcmd;
3245
                        if (*size > _IOC_SIZE(*cmd)) {
3246
                                printk("ioctl not yet supported\n");
3247
                                return -EFAULT;
3248
                        }
3249
                        return 0;
3250
                }
3251
        }
3252
        return -EINVAL;
3253
}
3254
 
3255
static inline int xlate_0x00xx_ioctl(int *cmd, int *size)
3256
{
3257
        int i;
3258
        /* old ioctls' for kernels <= 1.3.33 */
3259
        /* When the next even release will come around, we'll start
3260
         * warning against these.
3261
         * When the next odd release will come around, we'll fail with
3262
         * -EINVAL */
3263
        if(strcmp(system_utsname.version, "1.4.0") >= 0)
3264
                printk("obsolete floppy ioctl %x\n", *cmd);
3265
        if((system_utsname.version[0] == '1' &&
3266
            strcmp(system_utsname.version, "1.5.0") >= 0) ||
3267
           (system_utsname.version[0] >= '2' &&
3268
            strcmp(system_utsname.version, "2.1.0") >= 0))
3269
                return -EINVAL;
3270
        for (i=0; i < ARRAY_SIZE(translation_table); i++) {
3271
                if (*cmd == translation_table[i].oldcmd) {
3272
                        *size = translation_table[i].oldsize;
3273
                        *cmd = translation_table[i].newcmd;
3274
                        return 0;
3275
                }
3276
        }
3277
        return -EINVAL;
3278
}
3279
 
3280
static int fd_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
3281
                    unsigned long param)
3282
{
3283
#define IOCTL_MODE_BIT 8
3284
#define OPEN_WRITE_BIT 16
3285
#define IOCTL_ALLOWED (filp && (filp->f_mode & IOCTL_MODE_BIT))
3286
#define OUT(c,x) case c: outparam = (const char *) (x); break
3287
#define IN(c,x,tag) case c: *(x) = inparam. tag ; return 0
3288
 
3289
        int i,drive,type;
3290
        kdev_t device;
3291
        int ret;
3292
        int size;
3293
        union inparam {
3294
                struct floppy_struct g; /* geometry */
3295
                struct format_descr f;
3296
                struct floppy_max_errors max_errors;
3297
                struct floppy_drive_params dp;
3298
        } inparam; /* parameters coming from user space */
3299
        const char *outparam; /* parameters passed back to user space */
3300
 
3301
        device = inode->i_rdev;
3302
        switch (cmd) {
3303
                RO_IOCTLS(device,param);
3304
        }
3305
        type = TYPE(device);
3306
        drive = DRIVE(device);
3307
 
3308
        /* convert compatibility eject ioctls into floppy eject ioctl.
3309
         * We do this in order to provide a means to eject floppy disks before
3310
         * installing the new fdutils package */
3311
        if(cmd == CDROMEJECT || /* CD-ROM eject */
3312
           cmd == 0x6470 /* SunOS floppy eject */) {
3313
                DPRINT("obsolete eject ioctl\n");
3314
                DPRINT("please use floppycontrol --eject\n");
3315
                cmd = FDEJECT;
3316
        }
3317
 
3318
        /* convert the old style command into a new style command */
3319
        if ((cmd & 0xff00) == 0x0200) {
3320
                ECALL(normalize_0x02xx_ioctl(&cmd, &size));
3321
        } else if ((cmd & 0xff00) == 0x0000) {
3322
                ECALL(xlate_0x00xx_ioctl(&cmd, &size));
3323
        } else
3324
                return -EINVAL;
3325
 
3326
        /* permission checks */
3327
        if (((cmd & 0x80) && !suser()) ||
3328
             ((cmd & 0x40) && !IOCTL_ALLOWED))
3329
                return -EPERM;
3330
 
3331
        /* verify writability of result, and fail early */
3332
        if (_IOC_DIR(cmd) & _IOC_READ)
3333
                ECALL(verify_area(VERIFY_WRITE,(void *) param, size));
3334
 
3335
        /* copyin */
3336
        CLEARSTRUCT(&inparam);
3337
        if (_IOC_DIR(cmd) & _IOC_WRITE)
3338
                ECALL(fd_copyin((void *)param, &inparam, size))
3339
 
3340
        switch (cmd) {
3341
                case FDEJECT:
3342
                        if(UDRS->fd_ref != 1)
3343
                                /* somebody else has this drive open */
3344
                                return -EBUSY;
3345
                        LOCK_FDC(drive,1);
3346
 
3347
                        /* do the actual eject. Fails on
3348
                         * non-Sparc architectures */
3349
                        ret=fd_eject(UNIT(drive));
3350
 
3351
                        USETF(FD_DISK_CHANGED);
3352
                        USETF(FD_VERIFY);
3353
                        process_fd_request();
3354
                        return ret;
3355
                case FDCLRPRM:
3356
                        LOCK_FDC(drive,1);
3357
                        current_type[drive] = NULL;
3358
                        floppy_sizes[drive] = MAX_DISK_SIZE;
3359
                        UDRS->keep_data = 0;
3360
                        return invalidate_drive(device);
3361
                case FDSETPRM:
3362
                case FDDEFPRM:
3363
                        return set_geometry(cmd, & inparam.g,
3364
                                            drive, type, device);
3365
                case FDGETPRM:
3366
                        LOCK_FDC(drive,1);
3367
                        CALL(poll_drive(1,0));
3368
                        process_fd_request();
3369
                        if (type)
3370
                                outparam = (char *) &floppy_type[type];
3371
                        else
3372
                                outparam = (char *) current_type[drive];
3373
                        if(!outparam)
3374
                                return -ENODEV;
3375
                        break;
3376
 
3377
                case FDMSGON:
3378
                        UDP->flags |= FTD_MSG;
3379
                        return 0;
3380
                case FDMSGOFF:
3381
                        UDP->flags &= ~FTD_MSG;
3382
                        return 0;
3383
 
3384
                case FDFMTBEG:
3385
                        LOCK_FDC(drive,1);
3386
                        CALL(poll_drive(1, FD_RAW_NEED_DISK));
3387
                        ret = UDRS->flags;
3388
                        process_fd_request();
3389
                        if(ret & FD_VERIFY)
3390
                                return -ENODEV;
3391
                        if(!(ret & FD_DISK_WRITABLE))
3392
                                return -EROFS;
3393
                        return 0;
3394
                case FDFMTTRK:
3395
                        if (UDRS->fd_ref != 1)
3396
                                return -EBUSY;
3397
                        return do_format(device, &inparam.f);
3398
                case FDFMTEND:
3399
                case FDFLUSH:
3400
                        LOCK_FDC(drive,1);
3401
                        return invalidate_drive(device);
3402
 
3403
                case FDSETEMSGTRESH:
3404
                        UDP->max_errors.reporting =
3405
                                (unsigned short) (param & 0x0f);
3406
                        return 0;
3407
                OUT(FDGETMAXERRS, &UDP->max_errors);
3408
                IN(FDSETMAXERRS, &UDP->max_errors, max_errors);
3409
 
3410
                case FDGETDRVTYP:
3411
                        outparam = drive_name(type,drive);
3412
                        SUPBOUND(size,strlen(outparam)+1);
3413
                        break;
3414
 
3415
                IN(FDSETDRVPRM, UDP, dp);
3416
                OUT(FDGETDRVPRM, UDP);
3417
 
3418
                case FDPOLLDRVSTAT:
3419
                        LOCK_FDC(drive,1);
3420
                        CALL(poll_drive(1, FD_RAW_NEED_DISK));
3421
                        process_fd_request();
3422
                        /* fall through */
3423
                OUT(FDGETDRVSTAT, UDRS);
3424
 
3425
                case FDRESET:
3426
                        return user_reset_fdc(drive, (int)param, 1);
3427
 
3428
                OUT(FDGETFDCSTAT,UFDCS);
3429
 
3430
                case FDWERRORCLR:
3431
                        CLEARSTRUCT(UDRWE);
3432
                        return 0;
3433
                OUT(FDWERRORGET,UDRWE);
3434
 
3435
                case OLDFDRAWCMD:
3436
                case FDRAWCMD:
3437
                        if (type)
3438
                                return -EINVAL;
3439
                        LOCK_FDC(drive,1);
3440
                        set_floppy(device);
3441
                        CALL(i = raw_cmd_ioctl(cmd,(void *) param));
3442
                        process_fd_request();
3443
                        return i;
3444
 
3445
                case FDTWADDLE:
3446
                        LOCK_FDC(drive,1);
3447
                        twaddle();
3448
                        process_fd_request();
3449
                        return 0;
3450
 
3451
                default:
3452
                        return -EINVAL;
3453
        }
3454
 
3455
        if (_IOC_DIR(cmd) & _IOC_READ)
3456
                return fd_copyout((void *)param, outparam, size);
3457
        else
3458
                return 0;
3459
#undef IOCTL_ALLOWED
3460
#undef OUT
3461
#undef IN
3462
}
3463
 
3464
static void config_types(void)
3465
{
3466
        int first=1;
3467
        int drive;
3468
 
3469
        /* read drive info out of physical CMOS */
3470
        drive=0;
3471
        if (!UDP->cmos)
3472
                UDP->cmos= FLOPPY0_TYPE;
3473
        drive=1;
3474
        if (!UDP->cmos && FLOPPY1_TYPE)
3475
                UDP->cmos = FLOPPY1_TYPE;
3476
 
3477
        /* XXX */
3478
        /* additional physical CMOS drive detection should go here */
3479
 
3480
        for (drive=0; drive < N_DRIVE; drive++){
3481
                if (UDP->cmos >= 16)
3482
                        UDP->cmos = 0;
3483
                if (UDP->cmos >= 0 && UDP->cmos <= NUMBER(default_drive_params))
3484
                        memcpy((char *) UDP,
3485
                               (char *) (&default_drive_params[(int)UDP->cmos].params),
3486
                               sizeof(struct floppy_drive_params));
3487
                if (UDP->cmos){
3488
                        if (first)
3489
                                printk(KERN_INFO "Floppy drive(s): ");
3490
                        else
3491
                                printk(", ");
3492
                        first=0;
3493
                        if (UDP->cmos > 0){
3494
                                allowed_drive_mask |= 1 << drive;
3495
                                printk("fd%d is %s", drive,
3496
                                       default_drive_params[(int)UDP->cmos].name);
3497
                        } else
3498
                                printk("fd%d is unknown type %d",drive,
3499
                                       (unsigned int)UDP->cmos);
3500
                }
3501
        }
3502
        if (!first)
3503
                printk("\n");
3504
}
3505
 
3506
static int floppy_read(struct inode * inode, struct file * filp,
3507
                       char * buf, int count)
3508
{
3509
        int drive = DRIVE(inode->i_rdev);
3510
 
3511
        check_disk_change(inode->i_rdev);
3512
        if (UTESTF(FD_DISK_CHANGED))
3513
                return -ENXIO;
3514
        return block_read(inode, filp, buf, count);
3515
}
3516
 
3517
static int floppy_write(struct inode * inode, struct file * filp,
3518
                        const char * buf, int count)
3519
{
3520
        int block;
3521
        int ret;
3522
        int drive = DRIVE(inode->i_rdev);
3523
 
3524
        if (!UDRS->maxblock)
3525
                UDRS->maxblock=1;/* make change detectable */
3526
        check_disk_change(inode->i_rdev);
3527
        if (UTESTF(FD_DISK_CHANGED))
3528
                return -ENXIO;
3529
        if (!UTESTF(FD_DISK_WRITABLE))
3530
                return -EROFS;
3531
        block = (filp->f_pos + count) >> 9;
3532
        INFBOUND(UDRS->maxblock, block);
3533
        ret= block_write(inode, filp, buf, count);
3534
        return ret;
3535
}
3536
 
3537
static void floppy_release(struct inode * inode, struct file * filp)
3538
{
3539
        int drive;
3540
 
3541
        drive = DRIVE(inode->i_rdev);
3542
 
3543
        if (!filp || (filp->f_mode & (2 | OPEN_WRITE_BIT)))
3544
                /* if the file is mounted OR (writable now AND writable at
3545
                 * open time) Linus: Does this cover all cases? */
3546
                block_fsync(inode,filp);
3547
 
3548
        if (UDRS->fd_ref < 0)
3549
                UDRS->fd_ref=0;
3550
        else if (!UDRS->fd_ref--) {
3551
                DPRINT("floppy_release with fd_ref == 0");
3552
                UDRS->fd_ref = 0;
3553
        }
3554
        floppy_release_irq_and_dma();
3555
}
3556
 
3557
/*
3558
 * floppy_open check for aliasing (/dev/fd0 can be the same as
3559
 * /dev/PS0 etc), and disallows simultaneous access to the same
3560
 * drive with different device numbers.
3561
 */
3562
#define RETERR(x) do{floppy_release(inode,filp); return -(x);}while(0)
3563
 
3564
static int floppy_open(struct inode * inode, struct file * filp)
3565
{
3566
        int drive;
3567
        int old_dev;
3568
        int try;
3569
        char *tmp;
3570
 
3571
        if (!filp) {
3572
                DPRINT("Weird, open called with filp=0\n");
3573
                return -EIO;
3574
        }
3575
 
3576
        drive = DRIVE(inode->i_rdev);
3577
        if (drive >= N_DRIVE ||
3578
            !(allowed_drive_mask & (1 << drive)) ||
3579
            fdc_state[FDC(drive)].version == FDC_NONE)
3580
                return -ENXIO;
3581
 
3582
        if (TYPE(inode->i_rdev) >= NUMBER(floppy_type))
3583
                return -ENXIO;
3584
        old_dev = UDRS->fd_device;
3585
        if (UDRS->fd_ref && old_dev != MINOR(inode->i_rdev))
3586
                return -EBUSY;
3587
 
3588
        if (!UDRS->fd_ref && (UDP->flags & FD_BROKEN_DCL)){
3589
                USETF(FD_DISK_CHANGED);
3590
                USETF(FD_VERIFY);
3591
        }
3592
 
3593
        if (UDRS->fd_ref == -1 ||
3594
           (UDRS->fd_ref && (filp->f_flags & O_EXCL)))
3595
                return -EBUSY;
3596
 
3597
        if (floppy_grab_irq_and_dma())
3598
                return -EBUSY;
3599
 
3600
        if (filp->f_flags & O_EXCL)
3601
                UDRS->fd_ref = -1;
3602
        else
3603
                UDRS->fd_ref++;
3604
 
3605
        if (!floppy_track_buffer){
3606
                /* if opening an ED drive, reserve a big buffer,
3607
                 * else reserve a small one */
3608
                if ((UDP->cmos == 6) || (UDP->cmos == 5))
3609
                        try = 64; /* Only 48 actually useful */
3610
                else
3611
                        try = 32; /* Only 24 actually useful */
3612
 
3613
                tmp=(char *)fd_dma_mem_alloc(1024 * try);
3614
                if (!tmp) {
3615
                        try >>= 1; /* buffer only one side */
3616
                        INFBOUND(try, 16);
3617
                        tmp= (char *)fd_dma_mem_alloc(1024*try);
3618
                }
3619
                if (!tmp) {
3620
                        DPRINT("Unable to allocate DMA memory\n");
3621
                        RETERR(ENXIO);
3622
                }
3623
                if (floppy_track_buffer)
3624
                        fd_dma_mem_free((unsigned long)tmp,try*1024);
3625
                else {
3626
                        buffer_min = buffer_max = -1;
3627
                        floppy_track_buffer = tmp;
3628
                        max_buffer_sectors = try;
3629
                }
3630
        }
3631
 
3632
        UDRS->fd_device = MINOR(inode->i_rdev);
3633
        if (old_dev != -1 && old_dev != MINOR(inode->i_rdev)) {
3634
                if (buffer_drive == drive)
3635
                        buffer_track = -1;
3636
                invalidate_buffers(MKDEV(FLOPPY_MAJOR,old_dev));
3637
        }
3638
 
3639
        /* Allow ioctls if we have write-permissions even if read-only open */
3640
        if ((filp->f_mode & 2) || (permission(inode,2) == 0))
3641
                filp->f_mode |= IOCTL_MODE_BIT;
3642
        if (filp->f_mode & 2)
3643
                filp->f_mode |= OPEN_WRITE_BIT;
3644
 
3645
        if (UFDCS->rawcmd == 1)
3646
                UFDCS->rawcmd = 2;
3647
 
3648
        if (filp->f_flags & O_NDELAY)
3649
                return 0;
3650
        if (filp->f_mode & 3) {
3651
                UDRS->last_checked = 0;
3652
                check_disk_change(inode->i_rdev);
3653
                if (UTESTF(FD_DISK_CHANGED))
3654
                        RETERR(ENXIO);
3655
        }
3656
        if ((filp->f_mode & 2) && !(UTESTF(FD_DISK_WRITABLE)))
3657
                RETERR(EROFS);
3658
        return 0;
3659
#undef RETERR
3660
}
3661
 
3662
/*
3663
 * Check if the disk has been changed or if a change has been faked.
3664
 */
3665
static int check_floppy_change(kdev_t dev)
3666
{
3667
        int drive = DRIVE(dev);
3668
 
3669
        if (MAJOR(dev) != MAJOR_NR) {
3670
                DPRINT("check_floppy_change: not a floppy\n");
3671
                return 0;
3672
        }
3673
 
3674
        if (UTESTF(FD_DISK_CHANGED) || UTESTF(FD_VERIFY))
3675
                return 1;
3676
 
3677
        if (UDP->checkfreq < jiffies - UDRS->last_checked){
3678
                lock_fdc(drive,0);
3679
                poll_drive(0,0);
3680
                process_fd_request();
3681
        }
3682
 
3683
        if (UTESTF(FD_DISK_CHANGED) ||
3684
           UTESTF(FD_VERIFY) ||
3685
           test_bit(drive, &fake_change) ||
3686
           (!TYPE(dev) && !current_type[drive]))
3687
                return 1;
3688
        return 0;
3689
}
3690
 
3691
/* revalidate the floppy disk, i.e. trigger format autodetection by reading
3692
 * the bootblock (block 0). "Autodetection" is also needed to check whether
3693
 * there is a disk in the drive at all... Thus we also do it for fixed
3694
 * geometry formats */
3695
static int floppy_revalidate(kdev_t dev)
3696
{
3697
#define NO_GEOM (!current_type[drive] && !TYPE(dev))
3698
        struct buffer_head * bh;
3699
        int drive=DRIVE(dev);
3700
        int cf;
3701
 
3702
        if (UTESTF(FD_DISK_CHANGED) ||
3703
           UTESTF(FD_VERIFY) ||
3704
           test_bit(drive, &fake_change) ||
3705
           NO_GEOM){
3706
                lock_fdc(drive,0);
3707
                cf = UTESTF(FD_DISK_CHANGED) || UTESTF(FD_VERIFY);
3708
                if (!(cf || test_bit(drive, &fake_change) || NO_GEOM)){
3709
                        process_fd_request(); /*already done by another thread*/
3710
                        return 0;
3711
                }
3712
                UDRS->maxblock = 0;
3713
                UDRS->maxtrack = 0;
3714
                if (buffer_drive == drive)
3715
                        buffer_track = -1;
3716
                clear_bit(drive, &fake_change);
3717
                UCLEARF(FD_DISK_CHANGED);
3718
                if (cf)
3719
                        UDRS->generation++;
3720
                if (NO_GEOM){
3721
                        /* auto-sensing */
3722
                        int size = floppy_blocksizes[MINOR(dev)];
3723
                        if (!size)
3724
                                size = 1024;
3725
                        if (!(bh = getblk(dev,0,size))){
3726
                                process_fd_request();
3727
                                return 1;
3728
                        }
3729
                        if (bh && !buffer_uptodate(bh))
3730
                                ll_rw_block(READ, 1, &bh);
3731
                        process_fd_request();
3732
                        wait_on_buffer(bh);
3733
                        brelse(bh);
3734
                        return 0;
3735
                }
3736
                if (cf)
3737
                        poll_drive(0, FD_RAW_NEED_DISK);
3738
                process_fd_request();
3739
        }
3740
        return 0;
3741
}
3742
 
3743
static struct file_operations floppy_fops = {
3744
        NULL,                   /* lseek - default */
3745
        floppy_read,            /* read - general block-dev read */
3746
        floppy_write,           /* write - general block-dev write */
3747
        NULL,                   /* readdir - bad */
3748
        NULL,                   /* select */
3749
        fd_ioctl,               /* ioctl */
3750
        NULL,                   /* mmap */
3751
        floppy_open,            /* open */
3752
        floppy_release,         /* release */
3753
        block_fsync,            /* fsync */
3754
        NULL,                   /* fasync */
3755
        check_floppy_change,    /* media_change */
3756
        floppy_revalidate,      /* revalidate */
3757
};
3758
 
3759
/*
3760
 * Floppy Driver initialization
3761
 * =============================
3762
 */
3763
 
3764
/* Determine the floppy disk controller type */
3765
/* This routine was written by David C. Niemi */
3766
static char get_fdc_version(void)
3767
{
3768
        int r;
3769
 
3770
        output_byte(FD_DUMPREGS);       /* 82072 and better know DUMPREGS */
3771
        if (FDCS->reset)
3772
                return FDC_NONE;
3773
        if ((r = result()) <= 0x00)
3774
                return FDC_NONE;        /* No FDC present ??? */
3775
        if ((r==1) && (reply_buffer[0] == 0x80)){
3776
                printk(KERN_INFO "FDC %d is an 8272A\n",fdc);
3777
                return FDC_8272A;       /* 8272a/765 don't know DUMPREGS */
3778
        }
3779
        if (r != 10) {
3780
                printk("FDC %d init: DUMPREGS: unexpected return of %d bytes.\n",
3781
                       fdc, r);
3782
                return FDC_UNKNOWN;
3783
        }
3784
 
3785
        if(!fdc_configure()) {
3786
                printk(KERN_INFO "FDC %d is an 82072\n",fdc);
3787
                return FDC_82072;       /* 82072 doesn't know CONFIGURE */
3788
        }
3789
 
3790
        output_byte(FD_PERPENDICULAR);
3791
        if(need_more_output() == MORE_OUTPUT) {
3792
                output_byte(0);
3793
        } else {
3794
                printk(KERN_INFO "FDC %d is an 82072A\n", fdc);
3795
                return FDC_82072A;      /* 82072A as found on Sparcs. */
3796
        }
3797
 
3798
        output_byte(FD_UNLOCK);
3799
        r = result();
3800
        if ((r == 1) && (reply_buffer[0] == 0x80)){
3801
                printk(KERN_INFO "FDC %d is a pre-1991 82077\n", fdc);
3802
                return FDC_82077_ORIG;  /* Pre-1991 82077, doesn't know
3803
                                         * LOCK/UNLOCK */
3804
        }
3805
        if ((r != 1) || (reply_buffer[0] != 0x00)) {
3806
                printk("FDC %d init: UNLOCK: unexpected return of %d bytes.\n",
3807
                       fdc, r);
3808
                return FDC_UNKNOWN;
3809
        }
3810
        output_byte(FD_PARTID);
3811
        r = result();
3812
        if (r != 1) {
3813
                printk("FDC %d init: PARTID: unexpected return of %d bytes.\n",
3814
                       fdc, r);
3815
                return FDC_UNKNOWN;
3816
        }
3817
        if (reply_buffer[0] == 0x80) {
3818
                printk(KERN_INFO "FDC %d is a post-1991 82077\n",fdc);
3819
                return FDC_82077;       /* Revised 82077AA passes all the tests */
3820
        }
3821
        switch (reply_buffer[0] >> 5) {
3822
                case 0x0:
3823
                        /* Either a 82078-1 or a 82078SL running at 5Volt */
3824
                        printk(KERN_INFO "FDC %d is an 82078.\n",fdc);
3825
                        return FDC_82078;
3826
                case 0x1:
3827
                        printk(KERN_INFO "FDC %d is a 44pin 82078\n",fdc);
3828
                        return FDC_82078;
3829
                case 0x2:
3830
                        printk(KERN_INFO "FDC %d is a S82078B\n", fdc);
3831
                        return FDC_S82078B;
3832
                case 0x3:
3833
                        printk(KERN_INFO "FDC %d is a National Semiconductor PC87306\n", fdc);
3834
                        return FDC_87306;
3835
                default:
3836
                        printk(KERN_INFO "FDC %d init: 82078 variant with unknown PARTID=%d.\n",
3837
                               fdc, reply_buffer[0] >> 5);
3838
                        return FDC_82078_UNKN;
3839
        }
3840
} /* get_fdc_version */
3841
 
3842
/* lilo configuration */
3843
 
3844
/* we make the invert_dcl function global. One day, somebody might
3845
 * want to centralize all thinkpad related options into one lilo option,
3846
 * there are just so many thinkpad related quirks! */
3847
void floppy_invert_dcl(int *ints,int param)
3848
{
3849
        int i;
3850
 
3851
        for (i=0; i < ARRAY_SIZE(default_drive_params); i++){
3852
                if (param)
3853
                        default_drive_params[i].params.flags |= 0x80;
3854
                else
3855
                        default_drive_params[i].params.flags &= ~0x80;
3856
        }
3857
        DPRINT("Configuring drives for inverted dcl\n");
3858
}
3859
 
3860
static void daring(int *ints,int param)
3861
{
3862
        int i;
3863
 
3864
        for (i=0; i < ARRAY_SIZE(default_drive_params); i++){
3865
                if (param){
3866
                        default_drive_params[i].params.select_delay = 0;
3867
                        default_drive_params[i].params.flags |= FD_SILENT_DCL_CLEAR;
3868
                } else {
3869
                        default_drive_params[i].params.select_delay = 2*HZ/100;
3870
                        default_drive_params[i].params.flags &= ~FD_SILENT_DCL_CLEAR;
3871
                }
3872
        }
3873
        DPRINT("Assuming %s floppy hardware\n", param ? "standard" : "broken");
3874
}
3875
 
3876
static void set_cmos(int *ints, int dummy)
3877
{
3878
        int current_drive=0;
3879
 
3880
        if (ints[0] != 2){
3881
                DPRINT("wrong number of parameter for cmos\n");
3882
                return;
3883
        }
3884
        current_drive = ints[1];
3885
        if (current_drive < 0 || current_drive >= 8){
3886
                DPRINT("bad drive for set_cmos\n");
3887
                return;
3888
        }
3889
        if (current_drive >= 4 && !FDC2)
3890
                FDC2 = 0x370;
3891
        if (ints[2] <= 0 ||
3892
            (ints[2] >= NUMBER(default_drive_params) && ints[2] != 16)){
3893
                DPRINT("bad cmos code %d\n", ints[2]);
3894
                return;
3895
        }
3896
        DP->cmos = ints[2];
3897
        DPRINT("setting cmos code to %d\n", ints[2]);
3898
}
3899
 
3900
static struct param_table {
3901
        const char *name;
3902
        void (*fn)(int *ints, int param);
3903
        int *var;
3904
        int def_param;
3905
} config_params[]={
3906
        { "allowed_drive_mask", 0, &allowed_drive_mask, 0xff },
3907
        { "all_drives", 0, &allowed_drive_mask, 0xff },
3908
        { "asus_pci", 0, &allowed_drive_mask, 0x33 },
3909
 
3910
        { "daring", daring, 0, 1},
3911
 
3912
        { "two_fdc",  0, &FDC2, 0x370 },
3913
        { "one_fdc", 0, &FDC2, 0 },
3914
 
3915
        { "thinkpad", floppy_invert_dcl, 0, 1 },
3916
 
3917
        { "nodma", 0, &use_virtual_dma, 1 },
3918
        { "omnibook", 0, &use_virtual_dma, 1 },
3919
        { "dma", 0, &use_virtual_dma, 0 },
3920
 
3921
        { "fifo_depth", 0, &fifo_depth, 0xa },
3922
        { "nofifo", 0, &no_fifo, 0x20 },
3923
        { "usefifo", 0, &no_fifo, 0 },
3924
 
3925
        { "cmos", set_cmos, 0, 0 },
3926
 
3927
        { "unexpected_interrupts", 0, &print_unex, 1 },
3928
        { "no_unexpected_interrupts", 0, &print_unex, 0 },
3929
        { "L40SX", 0, &print_unex, 0 } };
3930
 
3931
#define FLOPPY_SETUP
3932
void floppy_setup(char *str, int *ints)
3933
{
3934
        int i;
3935
        int param;
3936
        if (str)
3937
                for (i=0; i< ARRAY_SIZE(config_params); i++){
3938
                        if (strcmp(str,config_params[i].name) == 0){
3939
                                if (ints[0])
3940
                                        param = ints[1];
3941
                                else
3942
                                        param = config_params[i].def_param;
3943
                                if(config_params[i].fn)
3944
                                        config_params[i].fn(ints,param);
3945
                                if(config_params[i].var) {
3946
                                        DPRINT("%s=%d\n", str, param);
3947
                                        *config_params[i].var = param;
3948
                                }
3949
                                return;
3950
                        }
3951
                }
3952
        if (str) {
3953
                DPRINT("unknown floppy option [%s]\n", str);
3954
 
3955
                DPRINT("allowed options are:");
3956
                for (i=0; i< ARRAY_SIZE(config_params); i++)
3957
                        printk(" %s",config_params[i].name);
3958
                printk("\n");
3959
        } else
3960
                DPRINT("botched floppy option\n");
3961
        DPRINT("Read linux/arch/arm/drivers/block/README.fd\n");
3962
}
3963
 
3964
int floppy_init(void)
3965
{
3966
        int i,unit,drive;
3967
        int have_no_fdc= -EIO;
3968
 
3969
        raw_cmd = NULL;
3970
 
3971
        if (register_blkdev(MAJOR_NR,"fd",&floppy_fops)) {
3972
                printk("Unable to get major %d for floppy\n",MAJOR_NR);
3973
                return -EBUSY;
3974
        }
3975
 
3976
        for (i=0; i<256; i++)
3977
                if (ITYPE(i))
3978
                        floppy_sizes[i] = floppy_type[ITYPE(i)].size >> 1;
3979
                else
3980
                        floppy_sizes[i] = MAX_DISK_SIZE;
3981
 
3982
        blk_size[MAJOR_NR] = floppy_sizes;
3983
        blksize_size[MAJOR_NR] = floppy_blocksizes;
3984
        blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
3985
        reschedule_timeout(MAXTIMEOUT, "floppy init", MAXTIMEOUT);
3986
        config_types();
3987
 
3988
        for (i = 0; i < N_FDC; i++) {
3989
                fdc = i;
3990
                CLEARSTRUCT(FDCS);
3991
                FDCS->dtr = -1;
3992
                FDCS->dor = 0x4;
3993
#ifdef __sparc__
3994
                /*sparcs don't have a DOR reset which we can fall back on to*/
3995
                FDCS->version = FDC_82072A;
3996
#endif
3997
        }
3998
 
3999
        fdc_state[0].address = FDC1;
4000
#if N_FDC > 1
4001
        fdc_state[1].address = FDC2;
4002
#endif
4003
 
4004
        if (floppy_grab_irq_and_dma()){
4005
                unregister_blkdev(MAJOR_NR,"fd");
4006
                return -EBUSY;
4007
        }
4008
 
4009
        /* initialise drive state */
4010
        for (drive = 0; drive < N_DRIVE; drive++) {
4011
                CLEARSTRUCT(UDRS);
4012
                CLEARSTRUCT(UDRWE);
4013
                UDRS->flags = FD_VERIFY | FD_DISK_NEWCHANGE | FD_DISK_CHANGED;
4014
                UDRS->fd_device = -1;
4015
                floppy_track_buffer = NULL;
4016
                max_buffer_sectors = 0;
4017
        }
4018
 
4019
        for (i = 0; i < N_FDC; i++) {
4020
                fdc = i;
4021
                FDCS->driver_version = FD_DRIVER_VERSION;
4022
                for (unit=0; unit<4; unit++)
4023
                        FDCS->track[unit] = 0;
4024
                if (FDCS->address == -1)
4025
                        continue;
4026
                FDCS->rawcmd = 2;
4027
                if (user_reset_fdc(-1,FD_RESET_ALWAYS,0)){
4028
                        FDCS->address = -1;
4029
                        FDCS->version = FDC_NONE;
4030
                        continue;
4031
                }
4032
                /* Try to determine the floppy controller type */
4033
                FDCS->version = get_fdc_version();
4034
                if (FDCS->version == FDC_NONE){
4035
                        FDCS->address = -1;
4036
                        continue;
4037
                }
4038
 
4039
                request_region(FDCS->address, 6, "floppy");
4040
                request_region(FDCS->address+7, 1, "floppy DIR");
4041
                /* address + 6 is reserved, and may be taken by IDE.
4042
                 * Unfortunately, Adaptec doesn't know this :-(, */
4043
 
4044
                have_no_fdc = 0;
4045
                /* Not all FDCs seem to be able to handle the version command
4046
                 * properly, so force a reset for the standard FDC clones,
4047
                 * to avoid interrupt garbage.
4048
                 */
4049
                user_reset_fdc(-1,FD_RESET_ALWAYS,0);
4050
#ifdef CONFIG_ARM
4051
                fd_scandrives();
4052
#endif
4053
        }
4054
        fdc=0;
4055
        del_timer(&fd_timeout);
4056
        current_drive = 0;
4057
        floppy_release_irq_and_dma();
4058
        initialising=0;
4059
        if (have_no_fdc) {
4060
                DPRINT("no floppy controllers found\n");
4061
                unregister_blkdev(MAJOR_NR,"fd");
4062
        }
4063
        return have_no_fdc;
4064
}
4065
 
4066
static int floppy_grab_irq_and_dma(void)
4067
{
4068
        int i;
4069
        unsigned long flags;
4070
 
4071
        INT_OFF;
4072
        if (usage_count++){
4073
                INT_ON;
4074
                return 0;
4075
        }
4076
        INT_ON;
4077
        MOD_INC_USE_COUNT;
4078
        for (i=0; i< N_FDC; i++){
4079
                if (fdc_state[i].address != -1){
4080
                        fdc = i;
4081
                        reset_fdc_info(1);
4082
                        fd_setdor(FDCS->dor);
4083
                }
4084
        }
4085
        fdc = 0;
4086
        set_dor(0, ~0, 8);  /* avoid immediate interrupt */
4087
 
4088
        if (fd_request_irq()) {
4089
                DPRINT("Unable to grab IRQ%d for the floppy driver\n",
4090
                        FLOPPY_IRQ);
4091
                MOD_DEC_USE_COUNT;
4092
                usage_count--;
4093
                return -1;
4094
        }
4095
        if (fd_request_dma()) {
4096
                DPRINT("Unable to grab DMA%d for the floppy driver\n",
4097
                        FLOPPY_DMA);
4098
                fd_free_irq();
4099
                MOD_DEC_USE_COUNT;
4100
                usage_count--;
4101
                return -1;
4102
        }
4103
        for (fdc = 0; fdc < N_FDC; fdc++)
4104
                if (FDCS->address != -1)
4105
                        fd_setdor(FDCS->dor);
4106
        fdc = 0;
4107
        fd_enable_irq();
4108
        return 0;
4109
}
4110
 
4111
static void floppy_release_irq_and_dma(void)
4112
{
4113
#ifdef FLOPPY_SANITY_CHECK
4114
        int drive;
4115
#endif
4116
        long tmpsize;
4117
        unsigned long tmpaddr;
4118
        unsigned long flags;
4119
 
4120
        INT_OFF;
4121
        if (--usage_count){
4122
                INT_ON;
4123
                return;
4124
        }
4125
        INT_ON;
4126
        fd_disable_dma();
4127
        fd_free_dma();
4128
        fd_disable_irq();
4129
        fd_free_irq();
4130
 
4131
        set_dor(0, ~0, 8);
4132
#if N_FDC > 1
4133
        set_dor(1, ~8, 0);
4134
#endif
4135
        floppy_enable_hlt();
4136
 
4137
        if (floppy_track_buffer && max_buffer_sectors) {
4138
                tmpsize = max_buffer_sectors*1024;
4139
                tmpaddr = (unsigned long)floppy_track_buffer;
4140
                floppy_track_buffer = NULL;
4141
                max_buffer_sectors = 0;
4142
                buffer_min = buffer_max = -1;
4143
                fd_dma_mem_free(tmpaddr, tmpsize);
4144
        }
4145
 
4146
#ifdef FLOPPY_SANITY_CHECK
4147
        for (drive=0; drive < N_FDC * 4; drive++)
4148
                if (motor_off_timer[drive].next)
4149
                        printk("motor off timer %d still active\n", drive);
4150
 
4151
        if (fd_timeout.next)
4152
                printk("floppy timer still active:%s\n", timeout_message);
4153
        if (fd_timer.next)
4154
                printk("auxiliary floppy timer still active\n");
4155
        if (floppy_tq.sync)
4156
                printk("task queue still active\n");
4157
#endif
4158
        MOD_DEC_USE_COUNT;
4159
}
4160
 
4161
 
4162
#ifdef MODULE
4163
 
4164
extern char *get_options(char *str, int *ints);
4165
 
4166
char *floppy=NULL;
4167
 
4168
static void parse_floppy_cfg_string(char *cfg)
4169
{
4170
        char *ptr;
4171
        int ints[11];
4172
 
4173
        while(*cfg) {
4174
                for(ptr = cfg;*cfg && *cfg != ' ' && *cfg != '\t'; cfg++);
4175
                if(*cfg) {
4176
                        *cfg = '\0';
4177
                        cfg++;
4178
                }
4179
                if(*ptr)
4180
                        floppy_setup(get_options(ptr,ints),ints);
4181
        }
4182
}
4183
 
4184
static void mod_setup(char *pattern, void (*setup)(char *, int *))
4185
{
4186
        unsigned long i;
4187
        char c;
4188
        int j;
4189
        int match;
4190
        char buffer[100];
4191
        int ints[11];
4192
        int length = strlen(pattern)+1;
4193
 
4194
        match=0;
4195
        j=1;
4196
 
4197
        for (i=current->mm->env_start; i< current->mm->env_end; i ++){
4198
                c= get_fs_byte(i);
4199
                if (match){
4200
                        if (j==99)
4201
                                c='\0';
4202
                        buffer[j] = c;
4203
                        if (!c || c == ' ' || c == '\t'){
4204
                                if (j){
4205
                                        buffer[j] = '\0';
4206
                                        setup(get_options(buffer,ints),ints);
4207
                                }
4208
                                j=0;
4209
                        } else
4210
                                j++;
4211
                        if (!c)
4212
                                break;
4213
                        continue;
4214
                }
4215
                if ((!j && !c) || (j && c == pattern[j-1]))
4216
                        j++;
4217
                else
4218
                        j=0;
4219
                if (j==length){
4220
                        match=1;
4221
                        j=0;
4222
                }
4223
        }
4224
}
4225
 
4226
 
4227
#ifdef __cplusplus
4228
extern "C" {
4229
#endif
4230
int init_module(void)
4231
{
4232
        printk(KERN_INFO "inserting floppy driver for %s\n", kernel_version);
4233
 
4234
        if(floppy)
4235
                parse_floppy_cfg_string(floppy);
4236
        else
4237
                mod_setup("floppy=", floppy_setup);
4238
 
4239
        return floppy_init();
4240
}
4241
 
4242
void cleanup_module(void)
4243
{
4244
        int fdc, dummy;
4245
 
4246
        for (fdc=0; fdc<2; fdc++)
4247
                if (FDCS->address != -1){
4248
                        release_region(FDCS->address, 6);
4249
                        release_region(FDCS->address+7, 1);
4250
                }
4251
 
4252
        unregister_blkdev(MAJOR_NR, "fd");
4253
 
4254
        blk_dev[MAJOR_NR].request_fn = 0;
4255
        /* eject disk, if any */
4256
        dummy = fd_eject(0);
4257
}
4258
 
4259
#ifdef __cplusplus
4260
}
4261
#endif
4262
 
4263
#else
4264
/* eject the boot floppy (if we need the drive for a different root floppy) */
4265
/* This should only be called at boot time when we're sure that there's no
4266
 * resource contention. */
4267
void floppy_eject(void)
4268
{
4269
        int dummy;
4270
        floppy_grab_irq_and_dma();
4271
        lock_fdc(MAXTIMEOUT,0);
4272
        dummy=fd_eject(0);
4273
        process_fd_request();
4274
        floppy_release_irq_and_dma();
4275
}
4276
#endif

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.