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