1 /* This file contains the device dependent part of the driver for the Floppy
2 * Disk Controller (FDC) using the NEC PD765 chip.
4 * The file contains two entry points:
6 * floppy_task: main entry when system is brought up
9 * Sep 11, 2005 code cleanup (Andy Tanenbaum)
10 * Dec 01, 2004 floppy driver moved to user-space (Jorrit N. Herder)
11 * Sep 15, 2004 sync alarms/ local timer management (Jorrit N. Herder)
12 * Aug 12, 2003 null seek no interrupt fix (Mike Haertel)
13 * May 14, 2000 d-d/i rewrite (Kees J. Bot)
14 * Apr 04, 1992 device dependent/independent split (Kees J. Bot)
15 * Mar 27, 1992 last details on density checking (Kees J. Bot)
16 * Feb 14, 1992 check drive density on opens only (Andy Tanenbaum)
17 * 1991 len[] / motors / reset / step rate / ... (Bruce Evans)
18 * May 13, 1991 renovated the errors loop (Don Chapman)
19 * 1989 I/O vector to keep up with 1-1 interleave (Bruce Evans)
20 * Jan 06, 1988 allow 1.44 MB diskettes (Al Crew)
21 * Nov 28, 1986 better resetting for 386 (Peter Kay)
22 * Oct 27, 1986 fdc_results fixed for 8 MHz (Jakob Schripsema)
27 #include <ibm/diskparm.h>
28 #include <minix/sysutil.h>
29 #include <minix/syslib.h>
31 /* I/O Ports used by floppy disk task. */
32 #define DOR 0x3F2 /* motor drive control bits */
33 #define FDC_STATUS 0x3F4 /* floppy disk controller status register */
34 #define FDC_DATA 0x3F5 /* floppy disk controller data register */
35 #define FDC_RATE 0x3F7 /* transfer rate register */
36 #define DMA_ADDR 0x004 /* port for low 16 bits of DMA address */
37 #define DMA_TOP 0x081 /* port for top 8 bits of 24-bit DMA addr */
38 #define DMA_COUNT 0x005 /* port for DMA count (count = bytes - 1) */
39 #define DMA_FLIPFLOP 0x00C /* DMA byte pointer flip-flop */
40 #define DMA_MODE 0x00B /* DMA mode port */
41 #define DMA_INIT 0x00A /* DMA init port */
42 #define DMA_RESET_VAL 0x006
44 #define DMA_ADDR_MASK 0xFFFFFF /* mask to verify DMA address is 24-bit */
46 /* Status registers returned as result of operation. */
47 #define ST0 0x00 /* status register 0 */
48 #define ST1 0x01 /* status register 1 */
49 #define ST2 0x02 /* status register 2 */
50 #define ST3 0x00 /* status register 3 (return by DRIVE_SENSE) */
51 #define ST_CYL 0x03 /* slot where controller reports cylinder */
52 #define ST_HEAD 0x04 /* slot where controller reports head */
53 #define ST_SEC 0x05 /* slot where controller reports sector */
54 #define ST_PCN 0x01 /* slot where controller reports present cyl */
56 /* Fields within the I/O ports. */
57 /* Main status register. */
58 #define CTL_BUSY 0x10 /* bit is set when read or write in progress */
59 #define DIRECTION 0x40 /* bit is set when reading data reg is valid */
60 #define MASTER 0x80 /* bit is set when data reg can be accessed */
62 /* Digital output port (DOR). */
63 #define MOTOR_SHIFT 4 /* high 4 bits control the motors in DOR */
64 #define ENABLE_INT 0x0C /* used for setting DOR port */
67 #define ST0_BITS_TRANS 0xD8 /* check 4 bits of status */
68 #define TRANS_ST0 0x00 /* 4 bits of ST0 for READ/WRITE */
69 #define ST0_BITS_SEEK 0xF8 /* check top 5 bits of seek status */
70 #define SEEK_ST0 0x20 /* top 5 bits of ST0 for SEEK */
73 #define BAD_SECTOR 0x05 /* if these bits are set in ST1, recalibrate */
74 #define WRITE_PROTECT 0x02 /* bit is set if diskette is write protected */
77 #define BAD_CYL 0x1F /* if any of these bits are set, recalibrate */
80 #define ST3_FAULT 0x80 /* if this bit is set, drive is sick */
81 #define ST3_WR_PROTECT 0x40 /* set when diskette is write protected */
82 #define ST3_READY 0x20 /* set when drive is ready */
84 /* Floppy disk controller command bytes. */
85 #define FDC_SEEK 0x0F /* command the drive to seek */
86 #define FDC_READ 0xE6 /* command the drive to read */
87 #define FDC_WRITE 0xC5 /* command the drive to write */
88 #define FDC_SENSE 0x08 /* command the controller to tell its status */
89 #define FDC_RECALIBRATE 0x07 /* command the drive to go to cyl 0 */
90 #define FDC_SPECIFY 0x03 /* command the drive to accept params */
91 #define FDC_READ_ID 0x4A /* command the drive to read sector identity */
92 #define FDC_FORMAT 0x4D /* command the drive to format a track */
94 /* DMA channel commands. */
95 #define DMA_READ 0x46 /* DMA read opcode */
96 #define DMA_WRITE 0x4A /* DMA write opcode */
98 /* Parameters for the disk drive. */
99 #define HC_SIZE 2880 /* # sectors on largest legal disk (1.44MB) */
100 #define NR_HEADS 0x02 /* two heads (i.e., two tracks/cylinder) */
101 #define MAX_SECTORS 18 /* largest # sectors per track */
102 #define DTL 0xFF /* determines data length (sector size) */
103 #define SPEC2 0x02 /* second parameter to SPECIFY */
104 #define MOTOR_OFF (3*HZ) /* how long to wait before stopping motor */
105 #define WAKEUP (2*HZ) /* timeout on I/O, FDC won't quit. */
108 #define ERR_SEEK (-1) /* bad seek */
109 #define ERR_TRANSFER (-2) /* bad transfer */
110 #define ERR_STATUS (-3) /* something wrong when getting status */
111 #define ERR_READ_ID (-4) /* bad read id */
112 #define ERR_RECALIBRATE (-5) /* recalibrate didn't work properly */
113 #define ERR_DRIVE (-6) /* something wrong with a drive */
114 #define ERR_WR_PROTECT (-7) /* diskette is write protected */
115 #define ERR_TIMEOUT (-8) /* interrupt timeout */
117 /* No retries on some errors. */
118 #define err_no_retry(err) ((err) <= ERR_WR_PROTECT)
120 /* Encoding of drive type in minor device number. */
121 #define DEV_TYPE_BITS 0x7C /* drive type + 1, if nonzero */
122 #define DEV_TYPE_SHIFT 2 /* right shift to normalize type bits */
123 #define FORMAT_DEV_BIT 0x80 /* bit in minor to turn write into format */
126 #define MAX_ERRORS 6 /* how often to try rd/wt before quitting */
127 #define MAX_RESULTS 7 /* max number of bytes controller returns */
128 #define NR_DRIVES 2 /* maximum number of drives */
129 #define DIVISOR 128 /* used for sector size encoding */
130 #define SECTOR_SIZE_CODE 2 /* code to say "512" to the controller */
131 #define TIMEOUT_MICROS 500000L /* microseconds waiting for FDC */
132 #define TIMEOUT_TICKS 30 /* ticks waiting for FDC */
133 #define NT 7 /* number of diskette/drive combinations */
134 #define UNCALIBRATED 0 /* drive needs to be calibrated at next use */
135 #define CALIBRATED 1 /* no calibration needed */
136 #define BASE_SECTOR 1 /* sectors are numbered starting at 1 */
137 #define NO_SECTOR (-1) /* current sector unknown */
138 #define NO_CYL (-1) /* current cylinder unknown, must seek */
139 #define NO_DENS 100 /* current media unknown */
140 #define BSY_IDLE 0 /* busy doing nothing */
141 #define BSY_IO 1 /* busy doing I/O */
142 #define BSY_WAKEN 2 /* got a wakeup call */
144 /* Seven combinations of diskette/drive are supported.
146 * # Diskette Drive Sectors Tracks Rotation Data-rate Comment
147 * 0 360K 360K 9 40 300 RPM 250 kbps Standard PC DSDD
148 * 1 1.2M 1.2M 15 80 360 RPM 500 kbps AT disk in AT drive
149 * 2 360K 720K 9 40 300 RPM 250 kbps Quad density PC
150 * 3 720K 720K 9 80 300 RPM 250 kbps Toshiba, et al.
151 * 4 360K 1.2M 9 40 360 RPM 300 kbps PC disk in AT drive
152 * 5 720K 1.2M 9 80 360 RPM 300 kbps Toshiba in AT drive
153 * 6 1.44M 1.44M 18 80 300 RPM 500 kbps PS/2, et al.
155 * In addition, 720K diskettes can be read in 1.44MB drives, but that does
156 * not need a different set of parameters. This combination uses
158 * 3 720K 1.44M 9 80 300 RPM 250 kbps PS/2, et al.
160 PRIVATE
struct density
{
161 u8_t secpt
; /* sectors per track */
162 u8_t cyls
; /* tracks per side */
163 u8_t steps
; /* steps per cylinder (2 = double step) */
164 u8_t test
; /* sector to try for density test */
165 u8_t rate
; /* data rate (2=250, 1=300, 0=500 kbps) */
166 clock_t start
; /* motor start (clock ticks) */
167 u8_t gap
; /* gap size */
168 u8_t spec1
; /* first specify byte (SRT/HUT) */
170 { 9, 40, 1, 4*9, 2, 4*HZ
/8, 0x2A, 0xDF }, /* 360K / 360K */
171 { 15, 80, 1, 14, 0, 4*HZ
/8, 0x1B, 0xDF }, /* 1.2M / 1.2M */
172 { 9, 40, 2, 2*9, 2, 4*HZ
/8, 0x2A, 0xDF }, /* 360K / 720K */
173 { 9, 80, 1, 4*9, 2, 6*HZ
/8, 0x2A, 0xDF }, /* 720K / 720K */
174 { 9, 40, 2, 2*9, 1, 4*HZ
/8, 0x23, 0xDF }, /* 360K / 1.2M */
175 { 9, 80, 1, 4*9, 1, 4*HZ
/8, 0x23, 0xDF }, /* 720K / 1.2M */
176 { 18, 80, 1, 17, 0, 6*HZ
/8, 0x1B, 0xCF }, /* 1.44M / 1.44M */
179 /* The following table is used with the test_sector array to recognize a
180 * drive/floppy combination. The sector to test has been determined by
181 * looking at the differences in gap size, sectors/track, and double stepping.
182 * This means that types 0 and 3 can't be told apart, only the motor start
183 * time differs. If a read test succeeds then the drive is limited to the
184 * set of densities it can support to avoid unnecessary tests in the future.
187 #define b(d) (1 << (d)) /* bit for density d. */
189 PRIVATE
struct test_order
{
190 u8_t t_density
; /* floppy/drive type */
191 u8_t t_class
; /* limit drive to this class of densities */
192 } test_order
[NT
-1] = {
193 { 6, b(3) | b(6) }, /* 1.44M {720K, 1.44M} */
194 { 1, b(1) | b(4) | b(5) }, /* 1.2M {1.2M, 360K, 720K} */
195 { 3, b(2) | b(3) | b(6) }, /* 720K {360K, 720K, 1.44M} */
196 { 4, b(1) | b(4) | b(5) }, /* 360K {1.2M, 360K, 720K} */
197 { 5, b(1) | b(4) | b(5) }, /* 720K {1.2M, 360K, 720K} */
198 { 2, b(2) | b(3) }, /* 360K {360K, 720K} */
199 /* Note that type 0 is missing, type 3 can read/write it too, which is
200 * why the type 3 parameters have been pessimized to be like type 0.
205 PRIVATE
struct floppy
{ /* main drive struct, one entry per drive */
206 unsigned fl_curcyl
; /* current cylinder */
207 unsigned fl_hardcyl
; /* hardware cylinder, as opposed to: */
208 unsigned fl_cylinder
; /* cylinder number addressed */
209 unsigned fl_sector
; /* sector addressed */
210 unsigned fl_head
; /* head number addressed */
211 char fl_calibration
; /* CALIBRATED or UNCALIBRATED */
212 u8_t fl_density
; /* NO_DENS = ?, 0 = 360K; 1 = 360K/1.2M; etc.*/
213 u8_t fl_class
; /* bitmap for possible densities */
214 timer_t fl_tmr_stop
; /* timer to stop motor */
215 struct device fl_geom
; /* Geometry of the drive */
216 struct device fl_part
[NR_PARTITIONS
]; /* partition's base & size */
219 PRIVATE
int irq_hook_id
; /* id of irq hook at the kernel */
220 PRIVATE
int motor_status
; /* bitmap of current motor status */
221 PRIVATE
int need_reset
; /* set to 1 when controller must be reset */
222 PRIVATE
unsigned f_drive
; /* selected drive */
223 PRIVATE
unsigned f_device
; /* selected minor device */
224 PRIVATE
struct floppy
*f_fp
; /* current drive */
225 PRIVATE
struct density
*f_dp
; /* current density parameters */
226 PRIVATE
struct density
*prev_dp
;/* previous density parameters */
227 PRIVATE
unsigned f_sectors
; /* equal to f_dp->secpt (needed a lot) */
228 PRIVATE u16_t f_busy
; /* BSY_IDLE, BSY_IO, BSY_WAKEN */
229 PRIVATE
struct device
*f_dv
; /* device's base and size */
230 PRIVATE
struct disk_parameter_s fmt_param
; /* parameters for format */
231 PRIVATE u8_t f_results
[MAX_RESULTS
];/* the controller can give lots of output */
233 /* The floppy uses various timers. These are managed by the floppy driver
234 * itself, because only a single synchronous alarm is available per process.
235 * Besides the 'f_tmr_timeout' timer below, the floppy structure for each
236 * floppy disk drive contains a 'fl_tmr_stop' timer.
238 PRIVATE timer_t f_tmr_timeout
; /* timer for various timeouts */
239 PRIVATE timer_t
*f_timers
; /* queue of floppy timers */
240 PRIVATE
clock_t f_next_timeout
; /* the next timeout time */
241 FORWARD
_PROTOTYPE( void f_expire_tmrs
, (struct driver
*dp
, message
*m_ptr
) );
242 FORWARD
_PROTOTYPE( void f_set_timer
, (timer_t
*tp
, clock_t delta
,
243 tmr_func_t watchdog
) );
244 FORWARD
_PROTOTYPE( void stop_motor
, (timer_t
*tp
) );
245 FORWARD
_PROTOTYPE( void f_timeout
, (timer_t
*tp
) );
247 FORWARD
_PROTOTYPE( struct device
*f_prepare
, (int device
) );
248 FORWARD
_PROTOTYPE( char *f_name
, (void) );
249 FORWARD
_PROTOTYPE( void f_cleanup
, (void) );
250 FORWARD
_PROTOTYPE( int f_transfer
, (int proc_nr
, int opcode
, u64_t position
,
251 iovec_t
*iov
, unsigned nr_req
, int) );
252 FORWARD
_PROTOTYPE( int dma_setup
, (int opcode
) );
253 FORWARD
_PROTOTYPE( void start_motor
, (void) );
254 FORWARD
_PROTOTYPE( int seek
, (void) );
255 FORWARD
_PROTOTYPE( int fdc_transfer
, (int opcode
) );
256 FORWARD
_PROTOTYPE( int fdc_results
, (void) );
257 FORWARD
_PROTOTYPE( int fdc_command
, (u8_t
*cmd
, int len
) );
258 FORWARD
_PROTOTYPE( void fdc_out
, (int val
) );
259 FORWARD
_PROTOTYPE( int recalibrate
, (void) );
260 FORWARD
_PROTOTYPE( void f_reset
, (void) );
261 FORWARD
_PROTOTYPE( int f_intr_wait
, (void) );
262 FORWARD
_PROTOTYPE( int read_id
, (void) );
263 FORWARD
_PROTOTYPE( int f_do_open
, (struct driver
*dp
, message
*m_ptr
) );
264 FORWARD
_PROTOTYPE( void floppy_stop
, (struct driver
*dp
, message
*m_ptr
));
265 FORWARD
_PROTOTYPE( int test_read
, (int density
) );
266 FORWARD
_PROTOTYPE( void f_geometry
, (struct partition
*entry
) );
268 /* Entry points to this driver. */
269 PRIVATE
struct driver f_dtab
= {
270 f_name
, /* current device's name */
271 f_do_open
, /* open or mount request, sense type of diskette */
272 do_nop
, /* nothing on a close */
273 do_diocntl
, /* get or set a partitions geometry */
274 f_prepare
, /* prepare for I/O on a given minor device */
275 f_transfer
, /* do the I/O */
276 f_cleanup
, /* cleanup before sending reply to user process */
277 f_geometry
, /* tell the geometry of the diskette */
278 floppy_stop
, /* floppy cleanup on shutdown */
279 f_expire_tmrs
,/* expire all alarm timers */
286 /*===========================================================================*
288 *===========================================================================*/
291 /* Initialize the floppy structure and the timers. */
296 f_next_timeout
= TMR_NEVER
;
297 tmr_inittimer(&f_tmr_timeout
);
299 for (fp
= &floppy
[0]; fp
< &floppy
[NR_DRIVES
]; fp
++) {
300 fp
->fl_curcyl
= NO_CYL
;
301 fp
->fl_density
= NO_DENS
;
303 tmr_inittimer(&fp
->fl_tmr_stop
);
306 /* Set IRQ policy, only request notifications, do not automatically
307 * reenable interrupts. ID return on interrupt is the IRQ line number.
309 irq_hook_id
= FLOPPY_IRQ
;
310 if ((s
=sys_irqsetpolicy(FLOPPY_IRQ
, 0, &irq_hook_id
)) != OK
)
311 panic("FLOPPY", "Couldn't set IRQ policy", s
);
312 if ((s
=sys_irqenable(&irq_hook_id
)) != OK
)
313 panic("FLOPPY", "Couldn't enable IRQs", s
);
316 signal(SIGHUP
, SIG_IGN
);
318 driver_task(&f_dtab
);
321 /*===========================================================================*
323 *===========================================================================*/
324 PRIVATE
void f_expire_tmrs(struct driver
*dp
, message
*m_ptr
)
326 /* A synchronous alarm message was received. Check if there are any expired
327 * timers. Possibly reschedule the next alarm.
329 clock_t now
; /* current time */
333 /* Get the current time to compare the timers against. */
334 if ((s
=getuptime(&now
)) != OK
)
335 panic("FLOPPY","Couldn't get uptime from clock.", s
);
337 /* Scan the timers queue for expired timers. Dispatch the watchdog function
338 * for each expired timers. FLOPPY watchdog functions are f_tmr_timeout()
339 * and stop_motor(). Possibly a new alarm call must be scheduled.
341 tmrs_exptimers(&f_timers
, now
, NULL
);
342 if (f_timers
== NULL
) {
343 f_next_timeout
= TMR_NEVER
;
344 } else { /* set new sync alarm */
345 f_next_timeout
= f_timers
->tmr_exp_time
;
346 if ((s
=sys_setalarm(f_next_timeout
, 1)) != OK
)
347 panic("FLOPPY","Couldn't set synchronous alarm.", s
);
351 /*===========================================================================*
353 *===========================================================================*/
354 PRIVATE
void f_set_timer(tp
, delta
, watchdog
)
355 timer_t
*tp
; /* timer to be set */
356 clock_t delta
; /* in how many ticks */
357 tmr_func_t watchdog
; /* watchdog function to be called */
359 clock_t now
; /* current time */
362 /* Get the current time. */
363 if ((s
=getuptime(&now
)) != OK
)
364 panic("FLOPPY","Couldn't get uptime from clock.", s
);
366 /* Add the timer to the local timer queue. */
367 tmrs_settimer(&f_timers
, tp
, now
+ delta
, watchdog
, NULL
);
369 /* Possibly reschedule an alarm call. This happens when the front of the
370 * timers queue was reinserted at another position, i.e., when a timer was
371 * reset, or when a new timer was added in front.
373 if (f_timers
->tmr_exp_time
!= f_next_timeout
) {
374 f_next_timeout
= f_timers
->tmr_exp_time
;
375 if ((s
=sys_setalarm(f_next_timeout
, 1)) != OK
)
376 panic("FLOPPY","Couldn't set synchronous alarm.", s
);
380 /*===========================================================================*
382 *===========================================================================*/
383 PRIVATE
struct device
*f_prepare(device
)
386 /* Prepare for I/O on a device. */
389 f_drive
= device
& ~(DEV_TYPE_BITS
| FORMAT_DEV_BIT
);
390 if (f_drive
< 0 || f_drive
>= NR_DRIVES
) return(NIL_DEV
);
392 f_fp
= &floppy
[f_drive
];
393 f_dv
= &f_fp
->fl_geom
;
394 if (f_fp
->fl_density
< NT
) {
395 f_dp
= &fdensity
[f_fp
->fl_density
];
396 f_sectors
= f_dp
->secpt
;
397 f_fp
->fl_geom
.dv_size
= mul64u((long) (NR_HEADS
* f_sectors
398 * f_dp
->cyls
), SECTOR_SIZE
);
402 if ((device
&= DEV_TYPE_BITS
) >= MINOR_fd0p0
)
403 f_dv
= &f_fp
->fl_part
[(device
- MINOR_fd0p0
) >> DEV_TYPE_SHIFT
];
408 /*===========================================================================*
410 *===========================================================================*/
411 PRIVATE
char *f_name()
413 /* Return a name for the current device. */
414 static char name
[] = "fd0";
416 name
[2] = '0' + f_drive
;
420 /*===========================================================================*
422 *===========================================================================*/
423 PRIVATE
void f_cleanup()
425 /* Start a timer to turn the motor off in a few seconds. */
426 tmr_arg(&f_fp
->fl_tmr_stop
)->ta_int
= f_drive
;
427 f_set_timer(&f_fp
->fl_tmr_stop
, MOTOR_OFF
, stop_motor
);
429 /* Exiting the floppy driver, so forget where we are. */
430 f_fp
->fl_sector
= NO_SECTOR
;
433 /*===========================================================================*
435 *===========================================================================*/
436 PRIVATE
int f_transfer(proc_nr
, opcode
, pos64
, iov
, nr_req
, safe
)
437 int proc_nr
; /* process doing the request */
438 int opcode
; /* DEV_GATHER_S or DEV_SCATTER_S */
439 u64_t pos64
; /* offset on device to read or write */
440 iovec_t
*iov
; /* pointer to read or write request vector */
441 unsigned nr_req
; /* length of request vector */
445 struct floppy
*fp
= f_fp
;
446 iovec_t
*iop
, *iov_end
= iov
+ nr_req
;
447 int s
, r
, errors
, nr
;
448 unsigned block
; /* Seen any 32M floppies lately? */
449 unsigned nbytes
, count
, chunk
, sector
;
450 unsigned long dv_size
= cv64ul(f_dv
->dv_size
);
451 vir_bytes user_offset
, iov_offset
= 0, iop_offset
;
453 signed long uoffsets
[MAX_SECTORS
], *up
;
454 cp_grant_id_t ugrants
[MAX_SECTORS
], *ug
;
457 if (ex64hi(pos64
) != 0)
458 return OK
; /* Way beyond EOF */
459 position
= cv64ul(pos64
);
461 /* Check disk address. */
462 if ((position
& SECTOR_MASK
) != 0) return(EINVAL
);
464 #if 0 /* XXX hack to create a disk driver that crashes */
465 { static int count
= 0; if (++count
> 10) {
466 printf("floppy: time to die\n"); *(int *)-1= 42;
472 /* How many bytes to transfer? */
474 for (iop
= iov
; iop
< iov_end
; iop
++) nbytes
+= iop
->iov_size
;
476 /* Which block on disk and how close to EOF? */
477 if (position
>= dv_size
) return(OK
); /* At EOF */
478 if (position
+ nbytes
> dv_size
) nbytes
= dv_size
- position
;
479 block
= div64u(add64ul(f_dv
->dv_base
, position
), SECTOR_SIZE
);
481 if ((nbytes
& SECTOR_MASK
) != 0) return(EINVAL
);
483 /* Using a formatting device? */
484 if (f_device
& FORMAT_DEV_BIT
) {
485 if (opcode
!= DEV_SCATTER_S
) return(EIO
);
486 if (iov
->iov_size
< SECTOR_SIZE
+ sizeof(fmt_param
))
490 s
=sys_safecopyfrom(proc_nr
, iov
->iov_addr
,
491 SECTOR_SIZE
+ iov_offset
, (vir_bytes
) &fmt_param
,
492 (phys_bytes
) sizeof(fmt_param
), D
);
494 s
=sys_datacopy(proc_nr
, iov
->iov_addr
+
495 SECTOR_SIZE
+ iov_offset
,
496 SELF
, (vir_bytes
) &fmt_param
,
497 (phys_bytes
) sizeof(fmt_param
));
501 panic("FLOPPY", "Sys_*copy failed", s
);
503 /* Check that the number of sectors in the data is reasonable,
504 * to avoid division by 0. Leave checking of other data to
507 if (fmt_param
.sectors_per_cylinder
== 0) return(EIO
);
509 /* Only the first sector of the parameters now needed. */
510 iov
->iov_size
= nbytes
= SECTOR_SIZE
;
513 /* Only try one sector if there were errors. */
514 if (errors
> 0) nbytes
= SECTOR_SIZE
;
516 /* Compute cylinder and head of the track to access. */
517 fp
->fl_cylinder
= block
/ (NR_HEADS
* f_sectors
);
518 fp
->fl_hardcyl
= fp
->fl_cylinder
* f_dp
->steps
;
519 fp
->fl_head
= (block
% (NR_HEADS
* f_sectors
)) / f_sectors
;
521 /* For each sector on this track compute the user address it is to
522 * go or to come from.
524 for (up
= uoffsets
; up
< uoffsets
+ MAX_SECTORS
; up
++) *up
= NO_OFFSET
;
527 sector
= block
% f_sectors
;
529 iop_offset
= iov_offset
;
532 user_offset
= iop_offset
;
533 chunk
= iop
->iov_size
;
534 if ((chunk
& SECTOR_MASK
) != 0) return(EINVAL
);
537 ugrants
[sector
] = iop
->iov_addr
;
538 uoffsets
[sector
++] = user_offset
;
539 chunk
-= SECTOR_SIZE
;
540 user_offset
+= SECTOR_SIZE
;
541 count
+= SECTOR_SIZE
;
542 if (sector
== f_sectors
|| count
== nbytes
)
550 /* First check to see if a reset is needed. */
551 if (need_reset
) f_reset();
553 /* See if motor is running; if not, turn it on and wait. */
556 /* Set the stepping rate and data rate */
557 if (f_dp
!= prev_dp
) {
558 cmd
[0] = FDC_SPECIFY
;
559 cmd
[1] = f_dp
->spec1
;
561 (void) fdc_command(cmd
, 3);
562 if ((s
=sys_outb(FDC_RATE
, f_dp
->rate
)) != OK
)
563 panic("FLOPPY","Sys_outb failed", s
);
567 /* If we are going to a new cylinder, perform a seek. */
570 /* Avoid read_id() if we don't plan to read much. */
571 if (fp
->fl_sector
== NO_SECTOR
&& count
< (6 * SECTOR_SIZE
))
574 for (nbytes
= 0; nbytes
< count
; nbytes
+= SECTOR_SIZE
) {
575 if (fp
->fl_sector
== NO_SECTOR
) {
576 /* Find out what the current sector is. This often
577 * fails right after a seek, so try it twice.
579 if (r
== OK
&& read_id() != OK
) r
= read_id();
582 /* Look for the next job in uoffsets[] */
585 if (fp
->fl_sector
>= f_sectors
)
588 up
= &uoffsets
[fp
->fl_sector
];
589 ug
= &ugrants
[fp
->fl_sector
];
590 if (*up
!= NO_OFFSET
) break;
595 if (r
== OK
&& opcode
== DEV_SCATTER_S
) {
596 /* Copy the user bytes to the DMA buffer. */
598 s
=sys_safecopyfrom(proc_nr
, *ug
, *up
,
600 (phys_bytes
) SECTOR_SIZE
, D
);
602 s
=sys_datacopy(proc_nr
, *ug
+ *up
, SELF
,
604 (phys_bytes
) SECTOR_SIZE
);
607 panic("FLOPPY", "Sys_vircopy failed", s
);
610 /* Set up the DMA chip and perform the transfer. */
612 if (dma_setup(opcode
) != OK
) {
613 /* This can only fail for addresses above 16MB
614 * that cannot be handled by the controller,
615 * because it uses 24-bit addressing.
619 r
= fdc_transfer(opcode
);
622 if (r
== OK
&& opcode
== DEV_GATHER_S
) {
623 /* Copy the DMA buffer to user space. */
625 s
=sys_safecopyto(proc_nr
, *ug
, *up
,
627 (phys_bytes
) SECTOR_SIZE
, D
);
629 s
=sys_datacopy(SELF
, (vir_bytes
) tmp_buf
,
631 (phys_bytes
) SECTOR_SIZE
);
634 panic("FLOPPY", "Sys_vircopy failed", s
);
638 /* Don't retry if write protected or too many errors. */
639 if (err_no_retry(r
) || ++errors
== MAX_ERRORS
) {
643 /* Recalibrate if halfway. */
644 if (errors
== MAX_ERRORS
/ 2)
645 fp
->fl_calibration
= UNCALIBRATED
;
652 /* Book the bytes successfully transferred. */
655 if (nbytes
< iov
->iov_size
) {
656 /* Not done with this one yet. */
657 iov_offset
+= nbytes
;
658 iov
->iov_size
-= nbytes
;
662 nbytes
-= iov
->iov_size
;
665 /* The rest is optional, so we return to give FS a
666 * chance to think it over.
677 /*===========================================================================*
679 *===========================================================================*/
680 PRIVATE
int dma_setup(opcode
)
681 int opcode
; /* DEV_GATHER_S or DEV_SCATTER_S */
683 /* The IBM PC can perform DMA operations by using the DMA chip. To use it,
684 * the DMA (Direct Memory Access) chip is loaded with the 20-bit memory address
685 * to be read from or written to, the byte count minus 1, and a read or write
686 * opcode. This routine sets up the DMA chip. Note that the chip is not
687 * capable of doing a DMA across a 64K boundary (e.g., you can't read a
688 * 512-byte block starting at physical address 65520).
690 * Warning! Also note that it's not possible to do DMA above 16 MB because
691 * the ISA bus uses 24-bit addresses. Addresses above 16 MB therefore will
692 * be interpreted modulo 16 MB, dangerously overwriting arbitrary memory.
693 * A check here denies the I/O if the address is out of range.
695 pvb_pair_t byte_out
[9];
698 /* First check the DMA memory address not to exceed maximum. */
699 if (tmp_phys
!= (tmp_phys
& DMA_ADDR_MASK
)) {
700 report("FLOPPY", "DMA denied because address out of range", NO_NUM
);
704 /* Set up the DMA registers. (The comment on the reset is a bit strong,
705 * it probably only resets the floppy channel.)
707 pv_set(byte_out
[0], DMA_INIT
, DMA_RESET_VAL
); /* reset the dma controller */
708 pv_set(byte_out
[1], DMA_FLIPFLOP
, 0); /* write anything to reset it */
709 pv_set(byte_out
[2], DMA_MODE
, opcode
== DEV_SCATTER_S
? DMA_WRITE
: DMA_READ
);
710 pv_set(byte_out
[3], DMA_ADDR
, (unsigned) (tmp_phys
>> 0) & 0xff);
711 pv_set(byte_out
[4], DMA_ADDR
, (unsigned) (tmp_phys
>> 8) & 0xff);
712 pv_set(byte_out
[5], DMA_TOP
, (unsigned) (tmp_phys
>> 16) & 0xff);
713 pv_set(byte_out
[6], DMA_COUNT
, (((SECTOR_SIZE
- 1) >> 0)) & 0xff);
714 pv_set(byte_out
[7], DMA_COUNT
, (SECTOR_SIZE
- 1) >> 8);
715 pv_set(byte_out
[8], DMA_INIT
, 2); /* some sort of enable */
717 if ((s
=sys_voutb(byte_out
, 9)) != OK
)
718 panic("FLOPPY","Sys_voutb in dma_setup() failed", s
);
722 /*===========================================================================*
724 *===========================================================================*/
725 PRIVATE
void start_motor()
727 /* Control of the floppy disk motors is a big pain. If a motor is off, you
728 * have to turn it on first, which takes 1/2 second. You can't leave it on
729 * all the time, since that would wear out the diskette. However, if you turn
730 * the motor off after each operation, the system performance will be awful.
731 * The compromise used here is to leave it on for a few seconds after each
732 * operation. If a new operation is started in that interval, it need not be
733 * turned on again. If no new operation is started, a timer goes off and the
734 * motor is turned off. I/O port DOR has bits to control each of 4 drives.
737 int s
, motor_bit
, running
;
740 motor_bit
= 1 << f_drive
; /* bit mask for this drive */
741 running
= motor_status
& motor_bit
; /* nonzero if this motor is running */
742 motor_status
|= motor_bit
; /* want this drive running too */
745 (motor_status
<< MOTOR_SHIFT
) | ENABLE_INT
| f_drive
)) != OK
)
746 panic("FLOPPY","Sys_outb in start_motor() failed", s
);
748 /* If the motor was already running, we don't have to wait for it. */
749 if (running
) return; /* motor was already running */
751 /* Set an alarm timer to force a timeout if the hardware does not interrupt
752 * in time. Expect HARD_INT message, but check for SYN_ALARM timeout.
754 f_set_timer(&f_tmr_timeout
, f_dp
->start
, f_timeout
);
758 if (mess
.m_type
== SYN_ALARM
) {
759 f_expire_tmrs(NULL
, NULL
);
760 } else if(mess
.m_type
== DEV_PING
) {
761 notify(mess
.m_source
);
765 } while (f_busy
== BSY_IO
);
766 f_fp
->fl_sector
= NO_SECTOR
;
769 /*===========================================================================*
771 *===========================================================================*/
772 PRIVATE
void stop_motor(tp
)
775 /* This routine is called from an alarm timer after several seconds have
776 * elapsed with no floppy disk activity. It turns the drive motor off.
779 motor_status
&= ~(1 << tmr_arg(tp
)->ta_int
);
780 if ((s
=sys_outb(DOR
, (motor_status
<< MOTOR_SHIFT
) | ENABLE_INT
)) != OK
)
781 panic("FLOPPY","Sys_outb in stop_motor() failed", s
);
784 /*===========================================================================*
786 *===========================================================================*/
787 PRIVATE
void floppy_stop(struct driver
*dp
, message
*m_ptr
)
789 /* Stop all activity and cleanly exit with the system. */
791 sigset_t sigset
= m_ptr
->NOTIFY_ARG
;
792 if (sigismember(&sigset
, SIGTERM
) || sigismember(&sigset
, SIGKSTOP
)) {
793 if ((s
=sys_outb(DOR
, ENABLE_INT
)) != OK
)
794 panic("FLOPPY","Sys_outb in floppy_stop() failed", s
);
799 /*===========================================================================*
801 *===========================================================================*/
804 /* Issue a SEEK command on the indicated drive unless the arm is already
805 * positioned on the correct cylinder.
808 struct floppy
*fp
= f_fp
;
813 /* Are we already on the correct cylinder? */
814 if (fp
->fl_calibration
== UNCALIBRATED
)
815 if (recalibrate() != OK
) return(ERR_SEEK
);
816 if (fp
->fl_curcyl
== fp
->fl_hardcyl
) return(OK
);
818 /* No. Wrong cylinder. Issue a SEEK and wait for interrupt. */
820 cmd
[1] = (fp
->fl_head
<< 2) | f_drive
;
821 cmd
[2] = fp
->fl_hardcyl
;
822 if (fdc_command(cmd
, 3) != OK
) return(ERR_SEEK
);
823 if (f_intr_wait() != OK
) return(ERR_TIMEOUT
);
825 /* Interrupt has been received. Check drive status. */
826 fdc_out(FDC_SENSE
); /* probe FDC to make it return status */
827 r
= fdc_results(); /* get controller status bytes */
828 if (r
!= OK
|| (f_results
[ST0
] & ST0_BITS_SEEK
) != SEEK_ST0
829 || f_results
[ST1
] != fp
->fl_hardcyl
) {
830 /* seek failed, may need a recalibrate */
833 /* Give head time to settle on a format, no retrying here! */
834 if (f_device
& FORMAT_DEV_BIT
) {
835 /* Set a synchronous alarm to force a timeout if the hardware does
836 * not interrupt. Expect HARD_INT, but check for SYN_ALARM timeout.
838 f_set_timer(&f_tmr_timeout
, HZ
/30, f_timeout
);
842 if (mess
.m_type
== SYN_ALARM
) {
843 f_expire_tmrs(NULL
, NULL
);
844 } else if(mess
.m_type
== DEV_PING
) {
845 notify(mess
.m_source
);
849 } while (f_busy
== BSY_IO
);
851 fp
->fl_curcyl
= fp
->fl_hardcyl
;
852 fp
->fl_sector
= NO_SECTOR
;
856 /*===========================================================================*
858 *===========================================================================*/
859 PRIVATE
int fdc_transfer(opcode
)
860 int opcode
; /* DEV_GATHER_S or DEV_SCATTER_S */
862 /* The drive is now on the proper cylinder. Read, write or format 1 block. */
864 struct floppy
*fp
= f_fp
;
868 /* Never attempt a transfer if the drive is uncalibrated or motor is off. */
869 if (fp
->fl_calibration
== UNCALIBRATED
) return(ERR_TRANSFER
);
870 if ((motor_status
& (1 << f_drive
)) == 0) return(ERR_TRANSFER
);
872 /* The command is issued by outputting several bytes to the controller chip.
874 if (f_device
& FORMAT_DEV_BIT
) {
876 cmd
[1] = (fp
->fl_head
<< 2) | f_drive
;
877 cmd
[2] = fmt_param
.sector_size_code
;
878 cmd
[3] = fmt_param
.sectors_per_cylinder
;
879 cmd
[4] = fmt_param
.gap_length_for_format
;
880 cmd
[5] = fmt_param
.fill_byte_for_format
;
881 if (fdc_command(cmd
, 6) != OK
) return(ERR_TRANSFER
);
883 cmd
[0] = opcode
== DEV_SCATTER_S
? FDC_WRITE
: FDC_READ
;
884 cmd
[1] = (fp
->fl_head
<< 2) | f_drive
;
885 cmd
[2] = fp
->fl_cylinder
;
886 cmd
[3] = fp
->fl_head
;
887 cmd
[4] = BASE_SECTOR
+ fp
->fl_sector
;
888 cmd
[5] = SECTOR_SIZE_CODE
;
890 cmd
[7] = f_dp
->gap
; /* sector gap */
891 cmd
[8] = DTL
; /* data length */
892 if (fdc_command(cmd
, 9) != OK
) return(ERR_TRANSFER
);
895 /* Block, waiting for disk interrupt. */
896 if (f_intr_wait() != OK
) {
897 printf("%s: disk interrupt timed out.\n", f_name());
901 /* Get controller status and check for errors. */
903 if (r
!= OK
) return(r
);
905 if (f_results
[ST1
] & WRITE_PROTECT
) {
906 printf("%s: diskette is write protected.\n", f_name());
907 return(ERR_WR_PROTECT
);
910 if ((f_results
[ST0
] & ST0_BITS_TRANS
) != TRANS_ST0
) return(ERR_TRANSFER
);
911 if (f_results
[ST1
] | f_results
[ST2
]) return(ERR_TRANSFER
);
913 if (f_device
& FORMAT_DEV_BIT
) return(OK
);
915 /* Compare actual numbers of sectors transferred with expected number. */
916 s
= (f_results
[ST_CYL
] - fp
->fl_cylinder
) * NR_HEADS
* f_sectors
;
917 s
+= (f_results
[ST_HEAD
] - fp
->fl_head
) * f_sectors
;
918 s
+= (f_results
[ST_SEC
] - BASE_SECTOR
- fp
->fl_sector
);
919 if (s
!= 1) return(ERR_TRANSFER
);
921 /* This sector is next for I/O: */
922 fp
->fl_sector
= f_results
[ST_SEC
] - BASE_SECTOR
;
924 if (processor
< 386) fp
->fl_sector
++; /* Old CPU can't keep up. */
929 /*===========================================================================*
931 *===========================================================================*/
932 PRIVATE
int fdc_results()
934 /* Extract results from the controller after an operation, then allow floppy
939 unsigned long status
;
942 /* Extract bytes from FDC until it says it has no more. The loop is
943 * really an outer loop on result_nr and an inner loop on status.
944 * A timeout flag alarm is set.
949 /* Reading one byte is almost a mirror of fdc_out() - the DIRECTION
950 * bit must be set instead of clear, but the CTL_BUSY bit destroys
951 * the perfection of the mirror.
953 if ((s
=sys_inb(FDC_STATUS
, &status
)) != OK
)
954 panic("FLOPPY","Sys_inb in fdc_results() failed", s
);
955 status
&= (MASTER
| DIRECTION
| CTL_BUSY
);
956 if (status
== (MASTER
| DIRECTION
| CTL_BUSY
)) {
958 if (result_nr
>= MAX_RESULTS
) break; /* too many results */
959 if ((s
=sys_inb(FDC_DATA
, &tmp_r
)) != OK
)
960 panic("FLOPPY","Sys_inb in fdc_results() failed", s
);
961 f_results
[result_nr
] = tmp_r
;
965 if (status
== MASTER
) { /* all read */
966 if ((s
=sys_irqenable(&irq_hook_id
)) != OK
)
967 panic("FLOPPY", "Couldn't enable IRQs", s
);
969 return(OK
); /* only good exit */
971 } while ( (s
=getuptime(&t1
))==OK
&& (t1
-t0
) < TIMEOUT_TICKS
);
972 if (OK
!=s
) printf("FLOPPY: warning, getuptime failed: %d\n", s
);
973 need_reset
= TRUE
; /* controller chip must be reset */
975 if ((s
=sys_irqenable(&irq_hook_id
)) != OK
)
976 panic("FLOPPY", "Couldn't enable IRQs", s
);
980 /*===========================================================================*
982 *===========================================================================*/
983 PRIVATE
int fdc_command(cmd
, len
)
984 u8_t
*cmd
; /* command bytes */
985 int len
; /* command length */
987 /* Output a command to the controller. */
989 /* Set a synchronous alarm to force a timeout if the hardware does
990 * not interrupt. Expect HARD_INT, but check for SYN_ALARM timeout.
991 * Note that the actual check is done by the code that issued the
992 * fdc_command() call.
994 f_set_timer(&f_tmr_timeout
, WAKEUP
, f_timeout
);
1001 return(need_reset
? ERR_DRIVE
: OK
);
1004 /*===========================================================================*
1006 *===========================================================================*/
1007 PRIVATE
void fdc_out(val
)
1008 int val
; /* write this byte to floppy disk controller */
1010 /* Output a byte to the controller. This is not entirely trivial, since you
1011 * can only write to it when it is listening, and it decides when to listen.
1012 * If the controller refuses to listen, the FDC chip is given a hard reset.
1016 unsigned long status
;
1018 if (need_reset
) return; /* if controller is not listening, return */
1020 /* It may take several tries to get the FDC to accept a command. */
1023 if ( (s
=getuptime(&t1
))==OK
&& (t1
-t0
) > TIMEOUT_TICKS
) {
1024 if (OK
!=s
) printf("FLOPPY: warning, getuptime failed: %d\n", s
);
1025 need_reset
= TRUE
; /* hit it over the head */
1028 if ((s
=sys_inb(FDC_STATUS
, &status
)) != OK
)
1029 panic("FLOPPY","Sys_inb in fdc_out() failed", s
);
1031 while ((status
& (MASTER
| DIRECTION
)) != (MASTER
| 0));
1033 if ((s
=sys_outb(FDC_DATA
, val
)) != OK
)
1034 panic("FLOPPY","Sys_outb in fdc_out() failed", s
);
1037 /*===========================================================================*
1039 *===========================================================================*/
1040 PRIVATE
int recalibrate()
1042 /* The floppy disk controller has no way of determining its absolute arm
1043 * position (cylinder). Instead, it steps the arm a cylinder at a time and
1044 * keeps track of where it thinks it is (in software). However, after a
1045 * SEEK, the hardware reads information from the diskette telling where the
1046 * arm actually is. If the arm is in the wrong place, a recalibration is done,
1047 * which forces the arm to cylinder 0. This way the controller can get back
1048 * into sync with reality.
1051 struct floppy
*fp
= f_fp
;
1055 /* Issue the RECALIBRATE command and wait for the interrupt. */
1056 cmd
[0] = FDC_RECALIBRATE
; /* tell drive to recalibrate itself */
1057 cmd
[1] = f_drive
; /* specify drive */
1058 if (fdc_command(cmd
, 2) != OK
) return(ERR_SEEK
);
1059 if (f_intr_wait() != OK
) return(ERR_TIMEOUT
);
1061 /* Determine if the recalibration succeeded. */
1062 fdc_out(FDC_SENSE
); /* issue SENSE command to request results */
1063 r
= fdc_results(); /* get results of the FDC_RECALIBRATE command*/
1064 fp
->fl_curcyl
= NO_CYL
; /* force a SEEK next time */
1065 fp
->fl_sector
= NO_SECTOR
;
1066 if (r
!= OK
|| /* controller would not respond */
1067 (f_results
[ST0
] & ST0_BITS_SEEK
) != SEEK_ST0
|| f_results
[ST_PCN
] != 0) {
1068 /* Recalibration failed. FDC must be reset. */
1070 return(ERR_RECALIBRATE
);
1072 /* Recalibration succeeded. */
1073 fp
->fl_calibration
= CALIBRATED
;
1074 fp
->fl_curcyl
= f_results
[ST_PCN
];
1079 /*===========================================================================*
1081 *===========================================================================*/
1082 PRIVATE
void f_reset()
1084 /* Issue a reset to the controller. This is done after any catastrophe,
1085 * like the controller refusing to respond.
1087 pvb_pair_t byte_out
[2];
1091 /* Disable interrupts and strobe reset bit low. */
1094 /* It is not clear why the next lock is needed. Writing 0 to DOR causes
1095 * interrupt, while the PC documentation says turning bit 8 off disables
1096 * interrupts. Without the lock:
1097 * 1) the interrupt handler sets the floppy mask bit in the 8259.
1098 * 2) writing ENABLE_INT to DOR causes the FDC to assert the interrupt
1099 * line again, but the mask stops the cpu being interrupted.
1100 * 3) the sense interrupt clears the interrupt (not clear which one).
1101 * and for some reason the reset does not work.
1103 (void) fdc_command((u8_t
*) 0, 0); /* need only the timer */
1105 pv_set(byte_out
[0], DOR
, 0); /* strobe reset bit low */
1106 pv_set(byte_out
[1], DOR
, ENABLE_INT
); /* strobe it high again */
1107 if ((s
=sys_voutb(byte_out
, 2)) != OK
)
1108 panic("FLOPPY", "Sys_voutb in f_reset() failed", s
);
1110 /* A synchronous alarm timer was set in fdc_command. Expect a HARD_INT
1111 * message to collect the reset interrupt, but be prepared to handle the
1112 * SYN_ALARM message on a timeout.
1115 receive(ANY
, &mess
);
1116 if (mess
.m_type
== SYN_ALARM
) {
1117 f_expire_tmrs(NULL
, NULL
);
1118 } else if(mess
.m_type
== DEV_PING
) {
1119 notify(mess
.m_source
);
1120 } else { /* expect HARD_INT */
1123 } while (f_busy
== BSY_IO
);
1125 /* The controller supports 4 drives and returns a result for each of them.
1126 * Collect all the results now. The old version only collected the first
1127 * result. This happens to work for 2 drives, but it doesn't work for 3
1128 * or more drives, at least with only drives 0 and 2 actually connected
1129 * (the controller generates an extra interrupt for the middle drive when
1130 * drive 2 is accessed and the driver panics).
1132 * It would be better to keep collecting results until there are no more.
1133 * For this, fdc_results needs to return the number of results (instead
1134 * of OK) when it succeeds.
1136 for (i
= 0; i
< 4; i
++) {
1137 fdc_out(FDC_SENSE
); /* probe FDC to make it return status */
1138 (void) fdc_results(); /* flush controller */
1140 for (i
= 0; i
< NR_DRIVES
; i
++) /* clear each drive */
1141 floppy
[i
].fl_calibration
= UNCALIBRATED
;
1143 /* The current timing parameters must be specified again. */
1147 /*===========================================================================*
1149 *===========================================================================*/
1150 PRIVATE
int f_intr_wait()
1152 /* Wait for an interrupt, but not forever. The FDC may have all the time of
1153 * the world, but we humans do not.
1157 /* We expect a HARD_INT message from the interrupt handler, but if there is
1158 * a timeout, a SYN_ALARM notification is received instead. If a timeout
1159 * occurs, report an error.
1162 receive(ANY
, &mess
);
1163 if (mess
.m_type
== SYN_ALARM
) {
1164 f_expire_tmrs(NULL
, NULL
);
1165 } else if(mess
.m_type
== DEV_PING
) {
1166 notify(mess
.m_source
);
1170 } while (f_busy
== BSY_IO
);
1172 if (f_busy
== BSY_WAKEN
) {
1174 /* No interrupt from the FDC, this means that there is probably no
1175 * floppy in the drive. Get the FDC down to earth and return error.
1178 return(ERR_TIMEOUT
);
1183 /*===========================================================================*
1185 *===========================================================================*/
1186 PRIVATE
void f_timeout(tp
)
1189 /* This routine is called when a timer expires. Usually to tell that a
1190 * motor has spun up, but also to forge an interrupt when it takes too long
1191 * for the FDC to interrupt (no floppy in the drive). It sets a flag to tell
1192 * what has happened.
1194 if (f_busy
== BSY_IO
) {
1199 /*===========================================================================*
1201 *===========================================================================*/
1202 PRIVATE
int read_id()
1204 /* Determine current cylinder and sector. */
1206 struct floppy
*fp
= f_fp
;
1210 /* Never attempt a read id if the drive is uncalibrated or motor is off. */
1211 if (fp
->fl_calibration
== UNCALIBRATED
) return(ERR_READ_ID
);
1212 if ((motor_status
& (1 << f_drive
)) == 0) return(ERR_READ_ID
);
1214 /* The command is issued by outputting 2 bytes to the controller chip. */
1215 cmd
[0] = FDC_READ_ID
; /* issue the read id command */
1216 cmd
[1] = (fp
->fl_head
<< 2) | f_drive
;
1217 if (fdc_command(cmd
, 2) != OK
) return(ERR_READ_ID
);
1218 if (f_intr_wait() != OK
) return(ERR_TIMEOUT
);
1220 /* Get controller status and check for errors. */
1221 result
= fdc_results();
1222 if (result
!= OK
) return(result
);
1224 if ((f_results
[ST0
] & ST0_BITS_TRANS
) != TRANS_ST0
) return(ERR_READ_ID
);
1225 if (f_results
[ST1
] | f_results
[ST2
]) return(ERR_READ_ID
);
1227 /* The next sector is next for I/O: */
1228 fp
->fl_sector
= f_results
[ST_SEC
] - BASE_SECTOR
+ 1;
1232 /*===========================================================================*
1234 *===========================================================================*/
1235 PRIVATE
int f_do_open(dp
, m_ptr
)
1237 message
*m_ptr
; /* pointer to open message */
1239 /* Handle an open on a floppy. Determine diskette type if need be. */
1242 struct test_order
*top
;
1244 /* Decode the message parameters. */
1245 if (f_prepare(m_ptr
->DEVICE
) == NIL_DEV
) return(ENXIO
);
1247 dtype
= f_device
& DEV_TYPE_BITS
; /* get density from minor dev */
1248 if (dtype
>= MINOR_fd0p0
) dtype
= 0;
1251 /* All types except 0 indicate a specific drive/medium combination.*/
1252 dtype
= (dtype
>> DEV_TYPE_SHIFT
) - 1;
1253 if (dtype
>= NT
) return(ENXIO
);
1254 f_fp
->fl_density
= dtype
;
1255 (void) f_prepare(f_device
); /* Recompute parameters. */
1258 if (f_device
& FORMAT_DEV_BIT
) return(EIO
); /* Can't format /dev/fdN */
1260 /* The device opened is /dev/fdN. Experimentally determine drive/medium.
1261 * First check fl_density. If it is not NO_DENS, the drive has been used
1262 * before and the value of fl_density tells what was found last time. Try
1263 * that first. If the motor is still running then assume nothing changed.
1265 if (f_fp
->fl_density
!= NO_DENS
) {
1266 if (motor_status
& (1 << f_drive
)) return(OK
);
1267 if (test_read(f_fp
->fl_density
) == OK
) return(OK
);
1270 /* Either drive type is unknown or a different diskette is now present.
1271 * Use test_order to try them one by one.
1273 for (top
= &test_order
[0]; top
< &test_order
[NT
-1]; top
++) {
1274 dtype
= top
->t_density
;
1276 /* Skip densities that have been proven to be impossible */
1277 if (!(f_fp
->fl_class
& (1 << dtype
))) continue;
1279 if (test_read(dtype
) == OK
) {
1280 /* The test succeeded, use this knowledge to limit the
1281 * drive class to match the density just read.
1283 f_fp
->fl_class
&= top
->t_class
;
1286 /* Test failed, wrong density or did it time out? */
1287 if (f_busy
== BSY_WAKEN
) break;
1289 f_fp
->fl_density
= NO_DENS
;
1290 return(EIO
); /* nothing worked */
1293 /*===========================================================================*
1295 *===========================================================================*/
1296 PRIVATE
int test_read(density
)
1299 /* Try to read the highest numbered sector on cylinder 2. Not all floppy
1300 * types have as many sectors per track, and trying cylinder 2 finds the
1301 * ones that need double stepping.
1308 f_fp
->fl_density
= density
;
1309 device
= ((density
+ 1) << DEV_TYPE_SHIFT
) + f_drive
;
1311 (void) f_prepare(device
);
1312 position
= (off_t
) f_dp
->test
<< SECTOR_SHIFT
;
1313 iovec1
.iov_addr
= (vir_bytes
) tmp_buf
;
1314 iovec1
.iov_size
= SECTOR_SIZE
;
1315 result
= f_transfer(SELF
, DEV_GATHER_S
, cvul64(position
), &iovec1
, 1, 0);
1317 if (iovec1
.iov_size
!= 0) return(EIO
);
1319 partition(&f_dtab
, f_drive
, P_FLOPPY
, 0);
1323 /*===========================================================================*
1325 *===========================================================================*/
1326 PRIVATE
void f_geometry(entry
)
1327 struct partition
*entry
;
1329 entry
->cylinders
= f_dp
->cyls
;
1330 entry
->heads
= NR_HEADS
;
1331 entry
->sectors
= f_sectors
;