📄 floppy.c.txt
字号:
1 /*
2 * linux/kernel/floppy.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
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 entropy@wintermute.wpi.edu (Lawrence Foard). Linus.
33 */
34
35 /*
36 * Automatic floppy-detection and formatting written by Werner Almesberger
37 * (almesber@nessie.cs.id.ethz.ch), 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 (hooft@chem.ruu.nl)
53 * modelled 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 #define REALLY_SLOW_IO
68 #define FLOPPY_IRQ 6
69 #define FLOPPY_DMA 2
70
71 #include <linux/sched.h>
72 #include <linux/fs.h>
73 #include <linux/kernel.h>
74 #include <linux/timer.h>
75 #include <linux/fdreg.h>
76 #include <linux/fd.h>
77 #include <linux/errno.h>
78
79 #include <asm/dma.h>
80 #include <asm/system.h>
81 #include <asm/io.h>
82 #include <asm/segment.h>
83
84 #define MAJOR_NR FLOPPY_MAJOR
85 #include "blk.h"
86
87 static unsigned int changed_floppies = 0, fake_change = 0;
88
89 static int initial_reset_flag = 0;
90 static int need_configure = 1; /* for 82077 */
91 static int recalibrate = 0;
92 static int reset = 0;
93 static int recover = 0; /* recalibrate immediately after resetting */
94 static int seek = 0;
95
96 static unsigned char current_DOR = 0x0C;
97 static unsigned char running = 0;
98
99 #define TYPE(x) ((x)>>2)
100 #define DRIVE(x) ((x)&0x03)
101
102 /*
103 * Note that MAX_ERRORS=X doesn't imply that we retry every bad read
104 * max X times - some types of errors increase the errorcount by 2 or
105 * even 3, so we might actually retry only X/2 times before giving up.
106 */
107 #define MAX_ERRORS 12
108
109 /*
110 * Maximum disk size (in kilobytes). This default is used whenever the
111 * current disk size is unknown.
112 */
113 #define MAX_DISK_SIZE 1440
114
115 /*
116 * Maximum number of sectors in a track buffer. Track buffering is disabled
117 * if tracks are bigger.
118 */
119 #define MAX_BUFFER_SECTORS 18
120
121 /*
122 * The DMA channel used by the floppy controller cannot access data at
123 * addresses >= 16MB
124 *
125 * Went back to the 1MB limit, as some people had problems with the floppy
126 * driver otherwise. It doesn't matter much for performance anyway, as most
127 * floppy accesses go through the track buffer.
128 */
129 #define LAST_DMA_ADDR (0x100000 - BLOCK_SIZE)
130
131 /*
132 * globals used by 'result()'
133 */
134 #define MAX_REPLIES 7
135 static unsigned char reply_buffer[MAX_REPLIES];
136 #define ST0 (reply_buffer[0])
137 #define ST1 (reply_buffer[1])
138 #define ST2 (reply_buffer[2])
139 #define ST3 (reply_buffer[3])
140
141 /*
142 * This struct defines the different floppy types.
143 *
144 * The 'stretch' tells if the tracks need to be doubled for some
145 * types (ie 360kB diskette in 1.2MB drive etc). Others should
146 * be self-explanatory.
147 */
148 static struct floppy_struct floppy_type[] = {
149 { 0, 0,0, 0,0,0x00,0x00,0x00,0x00,NULL }, /* no testing */
150 { 720, 9,2,40,0,0x2A,0x02,0xDF,0x50,NULL }, /* 360kB PC diskettes */
151 { 2400,15,2,80,0,0x1B,0x00,0xDF,0x54,NULL }, /* 1.2 MB AT-diskettes */
152 { 720, 9,2,40,1,0x2A,0x02,0xDF,0x50,NULL }, /* 360kB in 720kB drive */
153 { 1440, 9,2,80,0,0x2A,0x02,0xDF,0x50,NULL }, /* 3.5" 720kB diskette */
154 { 720, 9,2,40,1,0x23,0x01,0xDF,0x50,NULL }, /* 360kB in 1.2MB drive */
155 { 1440, 9,2,80,0,0x23,0x01,0xDF,0x50,NULL }, /* 720kB in 1.2MB drive */
156 { 2880,18,2,80,0,0x1B,0x00,0xCF,0x6C,NULL }, /* 1.44MB diskette */
157 };
158
159 /*
160 * Auto-detection. Each drive type has a pair of formats which are
161 * used in succession to try to read the disk. If the FDC cannot lock onto
162 * the disk, the next format is tried. This uses the variable 'probing'.
163 */
164 static struct floppy_struct floppy_types[] = {
165 { 720, 9,2,40,0,0x2A,0x02,0xDF,0x50,"360k/PC" }, /* 360kB PC diskettes */
166 { 720, 9,2,40,0,0x2A,0x02,0xDF,0x50,"360k/PC" }, /* 360kB PC diskettes */
167 { 2400,15,2,80,0,0x1B,0x00,0xDF,0x54,"1.2M" }, /* 1.2 MB AT-diskettes */
168 { 720, 9,2,40,1,0x23,0x01,0xDF,0x50,"360k/AT" }, /* 360kB in 1.2MB drive */
169 { 1440, 9,2,80,0,0x2A,0x02,0xDF,0x50,"720k" }, /* 3.5" 720kB diskette */
170 { 1440, 9,2,80,0,0x2A,0x02,0xDF,0x50,"720k" }, /* 3.5" 720kB diskette */
171 { 2880,18,2,80,0,0x1B,0x00,0xCF,0x6C,"1.44M" }, /* 1.44MB diskette */
172 { 1440, 9,2,80,0,0x2A,0x02,0xDF,0x50,"720k/AT" }, /* 3.5" 720kB diskette */
173 };
174
175 /* Auto-detection: Disk type used until the next media change occurs. */
176 struct floppy_struct *current_type[4] = { NULL, NULL, NULL, NULL };
177
178 /* This type is tried first. */
179 struct floppy_struct *base_type[4];
180
181 /*
182 * User-provided type information. current_type points to
183 * the respective entry of this array.
184 */
185 struct floppy_struct user_params[4];
186
187 static int floppy_sizes[] ={
188 MAX_DISK_SIZE, MAX_DISK_SIZE, MAX_DISK_SIZE, MAX_DISK_SIZE,
189 360, 360 ,360, 360,
190 1200,1200,1200,1200,
191 360, 360, 360, 360,
192 720, 720, 720, 720,
193 360, 360, 360, 360,
194 720, 720, 720, 720,
195 1440,1440,1440,1440
196 };
197
198 /*
199 * The driver is trying to determine the correct media format
200 * while probing is set. rw_interrupt() clears it after a
201 * successful access.
202 */
203 static int probing = 0;
204
205 /*
206 * (User-provided) media information is _not_ discarded after a media change
207 * if the corresponding keep_data flag is non-zero. Positive values are
208 * decremented after each probe.
209 */
210 static int keep_data[4] = { 0,0,0,0 };
211
212 /*
213 * Announce successful media type detection and media information loss after
214 * disk changes.
215 * Also used to enable/disable printing of overrun warnings.
216 */
217 static ftd_msg[4] = { 0,0,0,0 };
218
219 /* Prevent "aliased" accesses. */
220
221 static fd_ref[4] = { 0,0,0,0 };
222 static fd_device[4] = { 0,0,0,0 };
223
224 /* Synchronization of FDC access. */
225 static volatile int format_status = FORMAT_NONE, fdc_busy = 0;
226 static struct wait_queue *fdc_wait = NULL, *format_done = NULL;
227
228 /* Errors during formatting are counted here. */
229 static int format_errors;
230
231 /* Format request descriptor. */
232 static struct format_descr format_req;
233
234 /*
235 * Current device number. Taken either from the block header or from the
236 * format request descriptor.
237 */
238 #define CURRENT_DEVICE (format_status == FORMAT_BUSY ? format_req.device : \
239 (CURRENT->dev))
240
241 /* Current error count. */
242 #define CURRENT_ERRORS (format_status == FORMAT_BUSY ? format_errors : \
243 (CURRENT->errors))
244
245 /*
246 * Threshold for reporting FDC errors to the console.
247 * Setting this to zero may flood your screen when using
248 * ultra cheap floppies ;-)
249 */
250 static unsigned short min_report_error_cnt[4] = {2, 2, 2, 2};
251
252 /*
253 * Rate is 0 for 500kb/s, 1 for 300kbps, 2 for 250kbps
254 * Spec1 is 0xSH, where S is stepping rate (F=1ms, E=2ms, D=3ms etc),
255 * H is head unload time (1=16ms, 2=32ms, etc)
256 *
257 * Spec2 is (HLD<<1 | ND), where HLD is head load time (1=2ms, 2=4 ms etc)
258 * and ND is set means no DMA. Hardcoded to 6 (HLD=6ms, use DMA).
259 */
260
261 /*
262 * Track buffer and block buffer (in case track buffering doesn't work).
263 * Because these are written to by the DMA controller, they must
264 * not contain a 64k byte boundary crossing, or data will be
265 * corrupted/lost. Alignment of these is enforced in boot/head.s.
266 * Note that you must not change the sizes below without updating head.s.
267 */
268 extern char tmp_floppy_area[BLOCK_SIZE];
269 extern char floppy_track_buffer[512*2*MAX_BUFFER_SECTORS];
270
271 static void redo_fd_request(void);
272
273 /*
274 * These are global variables, as that's the easiest way to give
275 * information to interrupts. They are the data used for the current
276 * request.
277 */
278 #define NO_TRACK 255
279
280 static int read_track = 0; /* flag to indicate if we want to read entire track */
281 static int buffer_track = -1;
282 static int buffer_drive = -1;
283 static int cur_spec1 = -1;
284 static int cur_rate = -1;
285 static struct floppy_struct * floppy = floppy_type;
286 static unsigned char current_drive = 255;
287 static unsigned char sector = 0;
288 static unsigned char head = 0;
289 static unsigned char track = 0;
290 static unsigned char seek_track = 0;
291 static unsigned char current_track = NO_TRACK;
292 static unsigned char command = 0;
293 static unsigned char fdc_version = FDC_TYPE_STD; /* FDC version code */
294
295 static void floppy_ready(void);
296
297 static void select_callback(unsigned long unused)
298 {
299 floppy_ready();
300 }
301
302 static void floppy_select(unsigned int nr)
303 {
304 static struct timer_list select = { NULL, NULL, 0, 0, select_callback };
305
306 if (current_drive == (current_DOR & 3)) {
307 floppy_ready();
308 return;
309 }
310 seek = 1;
311 current_track = NO_TRACK;
312 current_DOR &= 0xFC;
313 current_DOR |= current_drive;
314 outb(current_DOR,FD_DOR);
315 del_timer(&select);
316 select.expires = 2;
317 add_timer(&select);
318 }
319
320 static void motor_on_callback(unsigned long nr)
321 {
322 running |= 0x10 << nr;
323 floppy_select(nr);
324 }
325
326 static struct timer_list motor_on_timer[4] = {
327 { NULL, NULL, 0, 0, motor_on_callback },
328 { NULL, NULL, 0, 1, motor_on_callback },
329 { NULL, NULL, 0, 2, motor_on_callback },
330 { NULL, NULL, 0, 3, motor_on_callback }
331 };
332
333 static void motor_off_callback(unsigned long nr)
334 {
335 unsigned char mask = ~(0x10 << nr);
336 cli();
337 running &= mask;
338 current_DOR &= mask;
339 outb(current_DOR,FD_DOR);
340 sti();
341 }
342
343 static struct timer_list motor_off_timer[4] = {
344 { NULL, NULL, 0, 0, motor_off_callback },
345 { NULL, NULL, 0, 1, motor_off_callback },
346 { NULL, NULL, 0, 2, motor_off_callback },
347 { NULL, NULL, 0, 3, motor_off_callback }
348 };
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -