Deleted Added
sdiff udiff text old ( 53328 ) new ( 54073 )
full compact
1/*
2 * Copyright (c) 1990 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Don Ahn.
7 *
8 * Libretto PCMCIA floppy support by David Horwitt (dhorwitt@ucsd.edu)
9 * aided by the Linux floppy driver modifications from David Bateman
10 * (dbateman@eng.uts.edu.au).
11 *
12 * Copyright (c) 1993, 1994 by
13 * jc@irbs.UUCP (John Capo)
14 * vak@zebub.msk.su (Serge Vakulenko)
15 * ache@astral.msk.su (Andrew A. Chernov)
16 *
17 * Copyright (c) 1993, 1994, 1995 by
18 * joerg_wunsch@uriah.sax.de (Joerg Wunsch)
19 * dufault@hda.com (Peter Dufault)
20 *
21 * Redistribution and use in source and binary forms, with or without
22 * modification, are permitted provided that the following conditions
23 * are met:
24 * 1. Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * 2. Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in the
28 * documentation and/or other materials provided with the distribution.
29 * 3. All advertising materials mentioning features or use of this software
30 * must display the following acknowledgement:
31 * This product includes software developed by the University of
32 * California, Berkeley and its contributors.
33 * 4. Neither the name of the University nor the names of its contributors
34 * may be used to endorse or promote products derived from this software
35 * without specific prior written permission.
36 *
37 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
38 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
40 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
41 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
42 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
43 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
45 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
46 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 * SUCH DAMAGE.
48 *
49 * from: @(#)fd.c 7.4 (Berkeley) 5/25/91
50 * $FreeBSD: head/sys/dev/fdc/fdc.c 54073 1999-12-03 08:41:24Z mdodd $
51 *
52 */
53
54#include "opt_fdc.h"
55
56#include <sys/param.h>
57#include <sys/systm.h>
58#include <sys/kernel.h>
59#include <sys/buf.h>
60#include <sys/bus.h>
61#include <sys/conf.h>
62#include <sys/disklabel.h>
63#include <sys/devicestat.h>
64#include <sys/fcntl.h>
65#include <sys/malloc.h>
66#include <sys/module.h>
67#include <sys/proc.h>
68#include <sys/syslog.h>
69
70#include <sys/bus.h>
71#include <machine/bus.h>
72#include <sys/rman.h>
73
74#include <machine/clock.h>
75#include <machine/ioctl_fd.h>
76#include <machine/resource.h>
77#include <machine/stdarg.h>
78
79#include <isa/isavar.h>
80#include <isa/isareg.h>
81#include <isa/fdreg.h>
82#include <isa/fdc.h>
83#include <isa/rtc.h>
84
85#ifdef FDC_YE
86#undef FDC_YE
87#warning "fix FDC_YE! - newbus casualty"
88#endif
89
90/* misuse a flag to identify format operation */
91#define B_FORMAT B_XXX
92
93/* configuration flags */
94#define FDC_PRETEND_D0 (1 << 0) /* pretend drive 0 to be there */
95#ifdef FDC_YE
96#define FDC_IS_PCMCIA (1 << 1) /* if successful probe, then it's
97 a PCMCIA device */
98#endif
99
100/* internally used only, not really from CMOS: */
101#define RTCFDT_144M_PRETENDED 0x1000
102
103/* error returns for fd_cmd() */
104#define FD_FAILED -1
105#define FD_NOT_VALID -2
106#define FDC_ERRMAX 100 /* do not log more */
107
108#define NUMTYPES 14
109#define NUMDENS (NUMTYPES - 6)
110
111/* These defines (-1) must match index for fd_types */
112#define F_TAPE_TYPE 0x020 /* bit for fd_types to indicate tape */
113#define NO_TYPE 0 /* must match NO_TYPE in ft.c */
114#define FD_1720 1
115#define FD_1480 2
116#define FD_1440 3
117#define FD_1200 4
118#define FD_820 5
119#define FD_800 6
120#define FD_720 7
121#define FD_360 8
122
123#define FD_1480in5_25 9
124#define FD_1440in5_25 10
125#define FD_820in5_25 11
126#define FD_800in5_25 12
127#define FD_720in5_25 13
128#define FD_360in5_25 14
129
130
131static struct fd_type fd_types[NUMTYPES] =
132{
133{ 21,2,0xFF,0x04,82,3444,1,FDC_500KBPS,2,0x0C,2 }, /* 1.72M in HD 3.5in */
134{ 18,2,0xFF,0x1B,82,2952,1,FDC_500KBPS,2,0x6C,1 }, /* 1.48M in HD 3.5in */
135{ 18,2,0xFF,0x1B,80,2880,1,FDC_500KBPS,2,0x6C,1 }, /* 1.44M in HD 3.5in */
136{ 15,2,0xFF,0x1B,80,2400,1,FDC_500KBPS,2,0x54,1 }, /* 1.2M in HD 5.25/3.5 */
137{ 10,2,0xFF,0x10,82,1640,1,FDC_250KBPS,2,0x2E,1 }, /* 820K in HD 3.5in */
138{ 10,2,0xFF,0x10,80,1600,1,FDC_250KBPS,2,0x2E,1 }, /* 800K in HD 3.5in */
139{ 9,2,0xFF,0x20,80,1440,1,FDC_250KBPS,2,0x50,1 }, /* 720K in HD 3.5in */
140{ 9,2,0xFF,0x2A,40, 720,1,FDC_250KBPS,2,0x50,1 }, /* 360K in DD 5.25in */
141
142{ 18,2,0xFF,0x02,82,2952,1,FDC_500KBPS,2,0x02,2 }, /* 1.48M in HD 5.25in */
143{ 18,2,0xFF,0x02,80,2880,1,FDC_500KBPS,2,0x02,2 }, /* 1.44M in HD 5.25in */
144{ 10,2,0xFF,0x10,82,1640,1,FDC_300KBPS,2,0x2E,1 }, /* 820K in HD 5.25in */
145{ 10,2,0xFF,0x10,80,1600,1,FDC_300KBPS,2,0x2E,1 }, /* 800K in HD 5.25in */
146{ 9,2,0xFF,0x20,80,1440,1,FDC_300KBPS,2,0x50,1 }, /* 720K in HD 5.25in */
147{ 9,2,0xFF,0x23,40, 720,2,FDC_300KBPS,2,0x50,1 }, /* 360K in HD 5.25in */
148};
149
150#define DRVS_PER_CTLR 2 /* 2 floppies */
151
152/***********************************************************************\
153* Per controller structure. *
154\***********************************************************************/
155static devclass_t fdc_devclass;
156
157/***********************************************************************\
158* Per drive structure. *
159* N per controller (DRVS_PER_CTLR) *
160\***********************************************************************/
161struct fd_data {
162 struct fdc_data *fdc; /* pointer to controller structure */
163 int fdsu; /* this units number on this controller */
164 int type; /* Drive type (FD_1440...) */
165 struct fd_type *ft; /* pointer to the type descriptor */
166 int flags;
167#define FD_OPEN 0x01 /* it's open */
168#define FD_ACTIVE 0x02 /* it's active */
169#define FD_MOTOR 0x04 /* motor should be on */
170#define FD_MOTOR_WAIT 0x08 /* motor coming up */
171 int skip;
172 int hddrv;
173#define FD_NO_TRACK -2
174 int track; /* where we think the head is */
175 int options; /* user configurable options, see ioctl_fd.h */
176 struct callout_handle toffhandle;
177 struct callout_handle tohandle;
178 struct devstat device_stats;
179 device_t dev;
180 fdu_t fdu;
181};
182static devclass_t fd_devclass;
183
184/***********************************************************************\
185* Throughout this file the following conventions will be used: *
186* fd is a pointer to the fd_data struct for the drive in question *
187* fdc is a pointer to the fdc_data struct for the controller *
188* fdu is the floppy drive unit number *
189* fdcu is the floppy controller unit number *
190* fdsu is the floppy drive unit number on that controller. (sub-unit) *
191\***********************************************************************/
192
193#ifdef FDC_YE
194#include "card.h"
195static int yeattach(struct isa_device *);
196#endif
197
198/* needed for ft driver, thus exported */
199int in_fdc(struct fdc_data *);
200int out_fdc(struct fdc_data *, int);
201
202/* internal functions */
203static void fdc_add_device(device_t, const char *, int);
204static void fdc_intr(void *);
205static void set_motor(struct fdc_data *, int, int);
206# define TURNON 1
207# define TURNOFF 0
208static timeout_t fd_turnoff;
209static timeout_t fd_motor_on;
210static void fd_turnon(struct fd_data *);
211static void fdc_reset(fdc_p);
212static int fd_in(struct fdc_data *, int *);
213static void fdstart(struct fdc_data *);
214static timeout_t fd_iotimeout;
215static timeout_t fd_pseudointr;
216static int fdstate(struct fdc_data *);
217static int retrier(struct fdc_data *);
218static int fdformat(dev_t, struct fd_formb *, struct proc *);
219
220static int enable_fifo(fdc_p fdc);
221
222static int fifo_threshold = 8; /* XXX: should be accessible via sysctl */
223
224
225#define DEVIDLE 0
226#define FINDWORK 1
227#define DOSEEK 2
228#define SEEKCOMPLETE 3
229#define IOCOMPLETE 4
230#define RECALCOMPLETE 5
231#define STARTRECAL 6
232#define RESETCTLR 7
233#define SEEKWAIT 8
234#define RECALWAIT 9
235#define MOTORWAIT 10
236#define IOTIMEDOUT 11
237#define RESETCOMPLETE 12
238#ifdef FDC_YE
239#define PIOREAD 13
240#endif
241
242#ifdef FDC_DEBUG
243static char const * const fdstates[] =
244{
245"DEVIDLE",
246"FINDWORK",
247"DOSEEK",
248"SEEKCOMPLETE",
249"IOCOMPLETE",
250"RECALCOMPLETE",
251"STARTRECAL",
252"RESETCTLR",
253"SEEKWAIT",
254"RECALWAIT",
255"MOTORWAIT",
256"IOTIMEDOUT",
257"RESETCOMPLETE",
258#ifdef FDC_YE
259"PIOREAD",
260#endif
261};
262
263/* CAUTION: fd_debug causes huge amounts of logging output */
264static int volatile fd_debug = 0;
265#define TRACE0(arg) if(fd_debug) printf(arg)
266#define TRACE1(arg1, arg2) if(fd_debug) printf(arg1, arg2)
267#else /* FDC_DEBUG */
268#define TRACE0(arg)
269#define TRACE1(arg1, arg2)
270#endif /* FDC_DEBUG */
271
272static void
273fdout_wr(fdc_p fdc, u_int8_t v)
274{
275 bus_space_write_1(fdc->portt, fdc->porth, FDOUT+fdc->port_off, v);
276}
277
278static u_int8_t
279fdsts_rd(fdc_p fdc)
280{
281 return bus_space_read_1(fdc->portt, fdc->porth, FDSTS+fdc->port_off);
282}
283
284static void
285fddata_wr(fdc_p fdc, u_int8_t v)
286{
287 bus_space_write_1(fdc->portt, fdc->porth, FDDATA+fdc->port_off, v);
288}
289
290static u_int8_t
291fddata_rd(fdc_p fdc)
292{
293 return bus_space_read_1(fdc->portt, fdc->porth, FDDATA+fdc->port_off);
294}
295
296static void
297fdctl_wr(fdc_p fdc, u_int8_t v)
298{
299 if (fdc->flags & FDC_ISPNP)
300 bus_space_write_1(fdc->ctlt, fdc->ctlh, 0, v);
301 else
302 bus_space_write_1(fdc->portt, fdc->porth, FDCTL, v);
303}
304
305#if 0
306
307static u_int8_t
308fdin_rd(fdc_p fdc)
309{
310 return bus_space_read_1(fdc->portt, fdc->porth, FDIN);
311}
312
313#endif
314
315#ifdef FDC_YE
316#if NCARD > 0
317#include <sys/select.h>
318#include <sys/module.h>
319#include <pccard/cardinfo.h>
320#include <pccard/driver.h>
321#include <pccard/slot.h>
322
323/*
324 * PC-Card (PCMCIA) specific code.
325 */
326static int yeinit(struct pccard_devinfo *); /* init device */
327static void yeunload(struct pccard_devinfo *); /* Disable driver */
328static int yeintr(struct pccard_devinfo *); /* Interrupt handler */
329
330PCCARD_MODULE(fdc, yeinit, yeunload, yeintr, 0, bio_imask);
331
332/*
333 * this is the secret PIO data port (offset from base)
334 */
335#define FDC_YE_DATAPORT 6
336
337/*
338 * Initialize the device - called from Slot manager.
339 */
340static int yeinit(struct pccard_devinfo *devi)
341{
342 fdc_p fdc = &fdc_data[devi->isahd.id_unit];
343
344 fdc->baseport = devi->isahd.id_iobase;
345 /*
346 * reset controller
347 */
348 fdout_wr(fdc, 0);
349 DELAY(100);
350 fdout_wr(fdc, FDO_FRST);
351
352 /*
353 * wire into system
354 */
355 if (yeattach(&devi->isahd) == 0)
356 return(ENXIO);
357
358 return(0);
359}
360
361/*
362 * yeunload - unload the driver and clear the table.
363 * XXX TODO:
364 * This is usually called when the card is ejected, but
365 * can be caused by a modunload of a controller driver.
366 * The idea is to reset the driver's view of the device
367 * and ensure that any driver entry points such as
368 * read and write do not hang.
369 */
370static void yeunload(struct pccard_devinfo *devi)
371{
372 if (fd_data[devi->isahd.id_unit].type == NO_TYPE)
373 return;
374
375 /*
376 * this prevents Fdopen() and fdstrategy() from attempting
377 * to access unloaded controller
378 */
379 fd_data[devi->isahd.id_unit].type = NO_TYPE;
380
381 printf("fdc%d: unload\n", devi->isahd.id_unit);
382}
383
384/*
385 * yeintr - Shared interrupt called from
386 * front end of PC-Card handler.
387 */
388static int yeintr(struct pccard_devinfo *devi)
389{
390 fdintr((fdcu_t)devi->isahd.id_unit);
391 return(1);
392}
393#endif /* NCARD > 0 */
394#endif /* FDC_YE */
395
396static d_open_t Fdopen; /* NOTE, not fdopen */
397static d_close_t fdclose;
398static d_ioctl_t fdioctl;
399static d_strategy_t fdstrategy;
400
401#define CDEV_MAJOR 9
402#define BDEV_MAJOR 2
403
404static struct cdevsw fd_cdevsw = {
405 /* open */ Fdopen,
406 /* close */ fdclose,
407 /* read */ physread,
408 /* write */ physwrite,
409 /* ioctl */ fdioctl,
410 /* poll */ nopoll,
411 /* mmap */ nommap,
412 /* strategy */ fdstrategy,
413 /* name */ "fd",
414 /* maj */ CDEV_MAJOR,
415 /* dump */ nodump,
416 /* psize */ nopsize,
417 /* flags */ D_DISK,
418 /* bmaj */ BDEV_MAJOR
419};
420
421static int
422fdc_err(struct fdc_data *fdc, const char *s)
423{
424 fdc->fdc_errs++;
425 if (s) {
426 if (fdc->fdc_errs < FDC_ERRMAX) {
427 device_print_prettyname(fdc->fdc_dev);
428 printf("%s", s);
429 } else if (fdc->fdc_errs == FDC_ERRMAX) {
430 device_print_prettyname(fdc->fdc_dev);
431 printf("too many errors, not logging any more\n");
432 }
433 }
434
435 return FD_FAILED;
436}
437
438/*
439 * fd_cmd: Send a command to the chip. Takes a varargs with this structure:
440 * Unit number,
441 * # of output bytes, output bytes as ints ...,
442 * # of input bytes, input bytes as ints ...
443 */
444static int
445fd_cmd(struct fdc_data *fdc, int n_out, ...)
446{
447 u_char cmd;
448 int n_in;
449 int n;
450 va_list ap;
451
452 va_start(ap, n_out);
453 cmd = (u_char)(va_arg(ap, int));
454 va_end(ap);
455 va_start(ap, n_out);
456 for (n = 0; n < n_out; n++)
457 {
458 if (out_fdc(fdc, va_arg(ap, int)) < 0)
459 {
460 char msg[50];
461 snprintf(msg, sizeof(msg),
462 "cmd %x failed at out byte %d of %d\n",
463 cmd, n + 1, n_out);
464 return fdc_err(fdc, msg);
465 }
466 }
467 n_in = va_arg(ap, int);
468 for (n = 0; n < n_in; n++)
469 {
470 int *ptr = va_arg(ap, int *);
471 if (fd_in(fdc, ptr) < 0)
472 {
473 char msg[50];
474 snprintf(msg, sizeof(msg),
475 "cmd %02x failed at in byte %d of %d\n",
476 cmd, n + 1, n_in);
477 return fdc_err(fdc, msg);
478 }
479 }
480
481 return 0;
482}
483
484static int
485enable_fifo(fdc_p fdc)
486{
487 int i, j;
488
489 if ((fdc->flags & FDC_HAS_FIFO) == 0) {
490
491 /*
492 * XXX:
493 * Cannot use fd_cmd the normal way here, since
494 * this might be an invalid command. Thus we send the
495 * first byte, and check for an early turn of data directon.
496 */
497
498 if (out_fdc(fdc, I8207X_CONFIGURE) < 0)
499 return fdc_err(fdc, "Enable FIFO failed\n");
500
501 /* If command is invalid, return */
502 j = 100000;
503 while ((i = fdsts_rd(fdc) & (NE7_DIO | NE7_RQM))
504 != NE7_RQM && j-- > 0)
505 if (i == (NE7_DIO | NE7_RQM)) {
506 fdc_reset(fdc);
507 return FD_FAILED;
508 }
509 if (j<0 ||
510 fd_cmd(fdc, 3,
511 0, (fifo_threshold - 1) & 0xf, 0, 0) < 0) {
512 fdc_reset(fdc);
513 return fdc_err(fdc, "Enable FIFO failed\n");
514 }
515 fdc->flags |= FDC_HAS_FIFO;
516 return 0;
517 }
518 if (fd_cmd(fdc, 4,
519 I8207X_CONFIGURE, 0, (fifo_threshold - 1) & 0xf, 0, 0) < 0)
520 return fdc_err(fdc, "Re-enable FIFO failed\n");
521 return 0;
522}
523
524static int
525fd_sense_drive_status(fdc_p fdc, int *st3p)
526{
527 int st3;
528
529 if (fd_cmd(fdc, 2, NE7CMD_SENSED, fdc->fdu, 1, &st3))
530 {
531 return fdc_err(fdc, "Sense Drive Status failed\n");
532 }
533 if (st3p)
534 *st3p = st3;
535
536 return 0;
537}
538
539static int
540fd_sense_int(fdc_p fdc, int *st0p, int *cylp)
541{
542 int cyl, st0, ret;
543
544 ret = fd_cmd(fdc, 1, NE7CMD_SENSEI, 1, &st0);
545 if (ret) {
546 (void)fdc_err(fdc,
547 "sense intr err reading stat reg 0\n");
548 return ret;
549 }
550
551 if (st0p)
552 *st0p = st0;
553
554 if ((st0 & NE7_ST0_IC) == NE7_ST0_IC_IV) {
555 /*
556 * There doesn't seem to have been an interrupt.
557 */
558 return FD_NOT_VALID;
559 }
560
561 if (fd_in(fdc, &cyl) < 0) {
562 return fdc_err(fdc, "can't get cyl num\n");
563 }
564
565 if (cylp)
566 *cylp = cyl;
567
568 return 0;
569}
570
571
572static int
573fd_read_status(fdc_p fdc, int fdsu)
574{
575 int i, ret;
576
577 for (i = 0; i < 7; i++) {
578 /*
579 * XXX types are poorly chosen. Only bytes can by read
580 * from the hardware, but fdc->status[] wants u_ints and
581 * fd_in() gives ints.
582 */
583 int status;
584
585 ret = fd_in(fdc, &status);
586 fdc->status[i] = status;
587 if (ret != 0)
588 break;
589 }
590
591 if (ret == 0)
592 fdc->flags |= FDC_STAT_VALID;
593 else
594 fdc->flags &= ~FDC_STAT_VALID;
595
596 return ret;
597}
598
599/****************************************************************************/
600/* autoconfiguration stuff */
601/****************************************************************************/
602
603static struct isa_pnp_id fdc_ids[] = {
604 {0x0007d041, "PC standard floppy disk controller"}, /* PNP0700 */
605 {0x0107d041, "Standard floppy controller supporting MS Device Bay Spec"}, /* PNP0701 */
606 {0}
607};
608
609
610/*
611 * fdc controller section.
612 */
613static int
614fdc_probe(device_t dev)
615{
616 int error, ispnp, ic_type;
617 struct fdc_data *fdc;
618
619 /* Check pnp ids */
620 error = ISA_PNP_PROBE(device_get_parent(dev), dev, fdc_ids);
621 if (error == ENXIO)
622 return ENXIO;
623 ispnp = (error == 0);
624
625 fdc = device_get_softc(dev);
626 bzero(fdc, sizeof *fdc);
627 fdc->fdc_dev = dev;
628 fdc->rid_ioport = fdc->rid_irq = fdc->rid_drq = 0;
629 fdc->res_ioport = fdc->res_irq = fdc->res_drq = 0;
630
631 fdc->res_ioport = bus_alloc_resource(dev, SYS_RES_IOPORT,
632 &fdc->rid_ioport, 0ul, ~0ul,
633 ispnp ? 1 : IO_FDCSIZE,
634 RF_ACTIVE);
635 if (fdc->res_ioport == 0) {
636 device_printf(dev, "cannot reserve I/O port range\n");
637 error = ENXIO;
638 goto out;
639 }
640 fdc->portt = rman_get_bustag(fdc->res_ioport);
641 fdc->porth = rman_get_bushandle(fdc->res_ioport);
642
643 if (ispnp) {
644 /*
645 * Some bios' report the device at 0x3f2-0x3f5,0x3f7
646 * and some at 0x3f0-0x3f5,0x3f7. We detect the former
647 * by checking the size and adjust the port address
648 * accordingly.
649 */
650 if (bus_get_resource_count(dev, SYS_RES_IOPORT, 0) == 4)
651 fdc->port_off = -2;
652 fdc->flags |= FDC_ISPNP;
653 fdc->rid_ctl = 1;
654 fdc->res_ctl = bus_alloc_resource(dev, SYS_RES_IOPORT,
655 &fdc->rid_ctl, 0ul, ~0ul,
656 1, RF_ACTIVE);
657 if (fdc->res_ctl == 0) {
658 device_printf(dev, "cannot reserve I/O port range\n");
659 error = ENXIO;
660 goto out;
661 }
662 fdc->ctlt = rman_get_bustag(fdc->res_ctl);
663 fdc->ctlh = rman_get_bushandle(fdc->res_ctl);
664 }
665
666 fdc->res_irq = bus_alloc_resource(dev, SYS_RES_IRQ,
667 &fdc->rid_irq, 0ul, ~0ul, 1,
668 RF_ACTIVE);
669 if (fdc->res_irq == 0) {
670 device_printf(dev, "cannot reserve interrupt line\n");
671 error = ENXIO;
672 goto out;
673 }
674 fdc->res_drq = bus_alloc_resource(dev, SYS_RES_DRQ,
675 &fdc->rid_drq, 0ul, ~0ul, 1,
676 RF_ACTIVE);
677 if (fdc->res_drq == 0) {
678 device_printf(dev, "cannot reserve DMA request line\n");
679 error = ENXIO;
680 goto out;
681 }
682 fdc->dmachan = fdc->res_drq->r_start;
683 error = BUS_SETUP_INTR(device_get_parent(dev), dev, fdc->res_irq,
684 INTR_TYPE_BIO, fdc_intr, fdc, &fdc->fdc_intr);
685
686 /* First - lets reset the floppy controller */
687 fdout_wr(fdc, 0);
688 DELAY(100);
689 fdout_wr(fdc, FDO_FRST);
690
691 /* see if it can handle a command */
692 if (fd_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240),
693 NE7_SPEC_2(2, 0), 0)) {
694 error = ENXIO;
695 goto out;
696 }
697
698 if (fd_cmd(fdc, 1, NE7CMD_VERSION, 1, &ic_type) == 0) {
699 ic_type = (u_char)ic_type;
700 switch (ic_type) {
701 case 0x80:
702 device_set_desc(dev, "NEC 765 or clone");
703 fdc->fdct = FDC_NE765;
704 break;
705 case 0x81:
706 device_set_desc(dev, "Intel 82077 or clone");
707 fdc->fdct = FDC_I82077;
708 break;
709 case 0x90:
710 device_set_desc(dev, "NEC 72065B or clone");
711 fdc->fdct = FDC_NE72065;
712 break;
713 default:
714 device_set_desc(dev, "generic floppy controller");
715 fdc->fdct = FDC_UNKNOWN;
716 break;
717 }
718 }
719
720#ifdef FDC_YE
721 /*
722 * don't succeed on probe; wait
723 * for PCCARD subsystem to do it
724 */
725 if (dev->id_flags & FDC_IS_PCMCIA)
726 return(0);
727#endif
728 return (0);
729
730out:
731 if (fdc->fdc_intr)
732 BUS_TEARDOWN_INTR(device_get_parent(dev), dev, fdc->res_irq,
733 fdc->fdc_intr);
734 if (fdc->res_irq != 0) {
735 bus_deactivate_resource(dev, SYS_RES_IRQ, fdc->rid_irq,
736 fdc->res_irq);
737 bus_release_resource(dev, SYS_RES_IRQ, fdc->rid_irq,
738 fdc->res_irq);
739 }
740 if (fdc->res_ctl != 0) {
741 bus_deactivate_resource(dev, SYS_RES_IOPORT, fdc->rid_ctl,
742 fdc->res_ctl);
743 bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ctl,
744 fdc->res_ctl);
745 }
746 if (fdc->res_ioport != 0) {
747 bus_deactivate_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport,
748 fdc->res_ioport);
749 bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport,
750 fdc->res_ioport);
751 }
752 if (fdc->res_drq != 0) {
753 bus_deactivate_resource(dev, SYS_RES_DRQ, fdc->rid_drq,
754 fdc->res_drq);
755 bus_release_resource(dev, SYS_RES_DRQ, fdc->rid_drq,
756 fdc->res_drq);
757 }
758 return (error);
759}
760
761/*
762 * Aped dfr@freebsd.org's isa_add_device().
763 */
764static void
765fdc_add_device(device_t dev, const char *name, int unit)
766{
767 int disabled, *ivar;
768 device_t child;
769
770 ivar = malloc(sizeof *ivar, M_DEVBUF /* XXX */, M_NOWAIT);
771 if (ivar == 0)
772 return;
773 if (resource_int_value(name, unit, "drive", ivar) != 0)
774 *ivar = 0;
775 child = device_add_child(dev, name, unit);
776 device_set_ivars(child, ivar);
777 if (child == 0)
778 return;
779 if (resource_int_value(name, unit, "disabled", &disabled) == 0
780 && disabled != 0)
781 device_disable(child);
782}
783
784static int
785fdc_attach(device_t dev)
786{
787 struct fdc_data *fdc = device_get_softc(dev);
788 fdcu_t fdcu = device_get_unit(dev);
789 int i;
790
791 for (i = resource_query_string(-1, "at", device_get_nameunit(dev));
792 i != -1;
793 i = resource_query_string(i, "at", device_get_nameunit(dev)))
794 fdc_add_device(dev, resource_query_name(i),
795 resource_query_unit(i));
796
797 fdc->fdcu = fdcu;
798 fdc->flags |= FDC_ATTACHED;
799
800 /* Acquire the DMA channel forever, The driver will do the rest */
801 /* XXX should integrate with rman */
802 isa_dma_acquire(fdc->dmachan);
803 isa_dmainit(fdc->dmachan, 128 << 3 /* XXX max secsize */);
804 fdc->state = DEVIDLE;
805
806 /* reset controller, turn motor off, clear fdout mirror reg */
807 fdout_wr(fdc, ((fdc->fdout = 0)));
808 bufq_init(&fdc->head);
809
810 /*
811 * Probe and attach any children as were configured above.
812 */
813 return (bus_generic_attach(dev));
814}
815
816static int
817fdc_print_child(device_t me, device_t child)
818{
819 int retval = 0;
820
821 retval += bus_print_child_header(me, child);
822 retval += printf(" on %s drive %d\n", device_get_nameunit(me),
823 *(int *)device_get_ivars(child));
824
825 return (retval);
826}
827
828static device_method_t fdc_methods[] = {
829 /* Device interface */
830 DEVMETHOD(device_probe, fdc_probe),
831 DEVMETHOD(device_attach, fdc_attach),
832 DEVMETHOD(device_detach, bus_generic_detach),
833 DEVMETHOD(device_shutdown, bus_generic_shutdown),
834 DEVMETHOD(device_suspend, bus_generic_suspend),
835 DEVMETHOD(device_resume, bus_generic_resume),
836
837 /* Bus interface */
838 DEVMETHOD(bus_print_child, fdc_print_child),
839 /* Our children never use any other bus interface methods. */
840
841 { 0, 0 }
842};
843
844static driver_t fdc_driver = {
845 "fdc",
846 fdc_methods,
847 sizeof(struct fdc_data)
848};
849
850DRIVER_MODULE(fdc, isa, fdc_driver, fdc_devclass, 0, 0);
851
852/******************************************************************/
853/*
854 * devices attached to the controller section.
855 */
856static int
857fd_probe(device_t dev)
858{
859 int i;
860 u_int fdt, st0, st3;
861 struct fd_data *fd;
862 struct fdc_data *fdc;
863 fdsu_t fdsu;
864 static int fd_fifo = 0;
865
866 fdsu = *(int *)device_get_ivars(dev); /* xxx cheat a bit... */
867 fd = device_get_softc(dev);
868 fdc = device_get_softc(device_get_parent(dev));
869
870 bzero(fd, sizeof *fd);
871 fd->dev = dev;
872 fd->fdc = fdc;
873 fd->fdsu = fdsu;
874 fd->fdu = device_get_unit(dev);
875
876#ifdef __i386__
877 /* look up what bios thinks we have */
878 switch (fd->fdu) {
879 case 0:
880 if (device_get_flags(fdc->fdc_dev) & FDC_PRETEND_D0)
881 fdt = RTCFDT_144M | RTCFDT_144M_PRETENDED;
882 else
883 fdt = (rtcin(RTC_FDISKETTE) & 0xf0);
884 break;
885 case 1:
886 fdt = ((rtcin(RTC_FDISKETTE) << 4) & 0xf0);
887 break;
888 default:
889 fdt = RTCFDT_NONE;
890 break;
891 }
892#else
893 fdt = RTCFDT_144M; /* XXX probably */
894#endif
895
896 /* is there a unit? */
897 if (fdt == RTCFDT_NONE)
898 return (ENXIO);
899
900 /* select it */
901 set_motor(fdc, fdsu, TURNON);
902 DELAY(1000000); /* 1 sec */
903
904 /* XXX This doesn't work before the first set_motor() */
905 if (fd_fifo == 0 && fdc->fdct != FDC_NE765 && fdc->fdct != FDC_UNKNOWN
906 && enable_fifo(fdc) == 0) {
907 device_print_prettyname(device_get_parent(dev));
908 printf("FIFO enabled, %d bytes threshold\n", fifo_threshold);
909 }
910 fd_fifo = 1;
911
912 if ((fd_cmd(fdc, 2, NE7CMD_SENSED, fdsu, 1, &st3) == 0)
913 && (st3 & NE7_ST3_T0)) {
914 /* if at track 0, first seek inwards */
915 /* seek some steps: */
916 fd_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0);
917 DELAY(300000); /* ...wait a moment... */
918 fd_sense_int(fdc, 0, 0); /* make ctrlr happy */
919 }
920
921 /* If we're at track 0 first seek inwards. */
922 if ((fd_sense_drive_status(fdc, &st3) == 0) && (st3 & NE7_ST3_T0)) {
923 /* Seek some steps... */
924 if (fd_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) {
925 /* ...wait a moment... */
926 DELAY(300000);
927 /* make ctrlr happy: */
928 fd_sense_int(fdc, 0, 0);
929 }
930 }
931
932 for (i = 0; i < 2; i++) {
933 /*
934 * we must recalibrate twice, just in case the
935 * heads have been beyond cylinder 76, since most
936 * FDCs still barf when attempting to recalibrate
937 * more than 77 steps
938 */
939 /* go back to 0: */
940 if (fd_cmd(fdc, 2, NE7CMD_RECAL, fdsu, 0) == 0) {
941 /* a second being enough for full stroke seek*/
942 DELAY(i == 0 ? 1000000 : 300000);
943
944 /* anything responding? */
945 if (fd_sense_int(fdc, &st0, 0) == 0 &&
946 (st0 & NE7_ST0_EC) == 0)
947 break; /* already probed succesfully */
948 }
949 }
950
951 set_motor(fdc, fdsu, TURNOFF);
952
953 if (st0 & NE7_ST0_EC) /* no track 0 -> no drive present */
954 return (ENXIO);
955
956 fd->track = FD_NO_TRACK;
957 fd->fdc = fdc;
958 fd->fdsu = fdsu;
959 fd->options = 0;
960 callout_handle_init(&fd->toffhandle);
961 callout_handle_init(&fd->tohandle);
962
963 switch (fdt) {
964 case RTCFDT_12M:
965 device_set_desc(dev, "1200-KB 5.25\" drive");
966 fd->type = FD_1200;
967 break;
968 case RTCFDT_144M | RTCFDT_144M_PRETENDED:
969 device_set_desc(dev, "config-pretended 1440-MB 3.5\" drive");
970 fdt = RTCFDT_144M;
971 fd->type = FD_1440;
972 case RTCFDT_144M:
973 device_set_desc(dev, "1440-KB 3.5\" drive");
974 fd->type = FD_1440;
975 break;
976 case RTCFDT_288M:
977 case RTCFDT_288M_1:
978 device_set_desc(dev, "2880-KB 3.5\" drive (in 1440-KB mode)");
979 fd->type = FD_1440;
980 break;
981 case RTCFDT_360K:
982 device_set_desc(dev, "360-KB 5.25\" drive");
983 fd->type = FD_360;
984 break;
985 case RTCFDT_720K:
986 printf("720-KB 3.5\" drive");
987 fd->type = FD_720;
988 break;
989 default:
990 return (ENXIO);
991 }
992 return (0);
993}
994
995static int
996fd_attach(device_t dev)
997{
998 struct fd_data *fd;
999#if 0
1000 int i;
1001 int mynor;
1002 int typemynor;
1003 int typesize;
1004#endif
1005
1006 fd = device_get_softc(dev);
1007
1008 cdevsw_add(&fd_cdevsw); /* XXX */
1009 make_dev(&fd_cdevsw, (fd->fdu << 6),
1010 UID_ROOT, GID_OPERATOR, 0640, "rfd%d", fd->fdu);
1011
1012#if 0
1013 /* Other make_dev() go here. */
1014#endif
1015
1016 /*
1017 * Export the drive to the devstat interface.
1018 */
1019 devstat_add_entry(&fd->device_stats, device_get_name(dev),
1020 device_get_unit(dev), 512, DEVSTAT_NO_ORDERED_TAGS,
1021 DEVSTAT_TYPE_FLOPPY | DEVSTAT_TYPE_IF_OTHER,
1022 DEVSTAT_PRIORITY_FD);
1023 return (0);
1024}
1025
1026static device_method_t fd_methods[] = {
1027 /* Device interface */
1028 DEVMETHOD(device_probe, fd_probe),
1029 DEVMETHOD(device_attach, fd_attach),
1030 DEVMETHOD(device_detach, bus_generic_detach),
1031 DEVMETHOD(device_shutdown, bus_generic_shutdown),
1032 DEVMETHOD(device_suspend, bus_generic_suspend), /* XXX */
1033 DEVMETHOD(device_resume, bus_generic_resume), /* XXX */
1034
1035 { 0, 0 }
1036};
1037
1038static driver_t fd_driver = {
1039 "fd",
1040 fd_methods,
1041 sizeof(struct fd_data)
1042};
1043
1044DRIVER_MODULE(fd, fdc, fd_driver, fd_devclass, 0, 0);
1045
1046/******************************************************************/
1047
1048#ifdef FDC_YE
1049/*
1050 * this is a subset of fdattach() optimized for the Y-E Data
1051 * PCMCIA floppy drive.
1052 */
1053static int yeattach(struct isa_device *dev)
1054{
1055 fdcu_t fdcu = dev->id_unit;
1056 fdc_p fdc = fdc_data + fdcu;
1057 fdsu_t fdsu = 0; /* assume 1 drive per YE controller */
1058 fdu_t fdu;
1059 fd_p fd;
1060 int st0, st3, i;
1061 fdc->fdcu = fdcu;
1062 /*
1063 * the FDC_PCMCIA flag is used to to indicate special PIO is used
1064 * instead of DMA
1065 */
1066 fdc->flags = FDC_ATTACHED|FDC_PCMCIA;
1067 fdc->state = DEVIDLE;
1068 /* reset controller, turn motor off, clear fdout mirror reg */
1069 fdout_wr(fdc, ((fdc->fdout = 0)));
1070 bufq_init(&fdc->head);
1071 /*
1072 * assume 2 drives/ "normal" controller
1073 */
1074 fdu = fdcu * 2;
1075 if (fdu >= NFD) {
1076 printf("fdu %d >= NFD\n",fdu);
1077 return(0);
1078 };
1079 fd = &fd_data[fdu];
1080
1081 set_motor(fdcu, fdsu, TURNON);
1082 DELAY(1000000); /* 1 sec */
1083 fdc->fdct = FDC_NE765;
1084
1085 if ((fd_cmd(fdcu, 2, NE7CMD_SENSED, fdsu, 1, &st3) == 0) &&
1086 (st3 & NE7_ST3_T0)) {
1087 /* if at track 0, first seek inwards */
1088 /* seek some steps: */
1089 (void)fd_cmd(fdcu, 3, NE7CMD_SEEK, fdsu, 10, 0);
1090 DELAY(300000); /* ...wait a moment... */
1091 (void)fd_sense_int(fdc, 0, 0); /* make ctrlr happy */
1092 }
1093
1094 /* If we're at track 0 first seek inwards. */
1095 if ((fd_sense_drive_status(fdc, &st3) == 0) && (st3 & NE7_ST3_T0)) {
1096 /* Seek some steps... */
1097 if (fd_cmd(fdcu, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) {
1098 /* ...wait a moment... */
1099 DELAY(300000);
1100 /* make ctrlr happy: */
1101 (void)fd_sense_int(fdc, 0, 0);
1102 }
1103 }
1104
1105 for(i = 0; i < 2; i++) {
1106 /*
1107 * we must recalibrate twice, just in case the
1108 * heads have been beyond cylinder 76, since most
1109 * FDCs still barf when attempting to recalibrate
1110 * more than 77 steps
1111 */
1112 /* go back to 0: */
1113 if (fd_cmd(fdcu, 2, NE7CMD_RECAL, fdsu, 0) == 0) {
1114 /* a second being enough for full stroke seek*/
1115 DELAY(i == 0? 1000000: 300000);
1116
1117 /* anything responding? */
1118 if (fd_sense_int(fdc, &st0, 0) == 0 &&
1119 (st0 & NE7_ST0_EC) == 0)
1120 break; /* already probed succesfully */
1121 }
1122 }
1123
1124 set_motor(fdcu, fdsu, TURNOFF);
1125
1126 if (st0 & NE7_ST0_EC) /* no track 0 -> no drive present */
1127 return(0);
1128
1129 fd->track = FD_NO_TRACK;
1130 fd->fdc = fdc;
1131 fd->fdsu = fdsu;
1132 fd->options = 0;
1133 printf("fdc%d: 1.44MB 3.5in PCMCIA\n", fdcu);
1134 fd->type = FD_1440;
1135
1136 return (1);
1137}
1138#endif
1139
1140/****************************************************************************/
1141/* motor control stuff */
1142/* remember to not deselect the drive we're working on */
1143/****************************************************************************/
1144static void
1145set_motor(struct fdc_data *fdc, int fdsu, int turnon)
1146{
1147 int fdout = fdc->fdout;
1148 int needspecify = 0;
1149
1150 if(turnon) {
1151 fdout &= ~FDO_FDSEL;
1152 fdout |= (FDO_MOEN0 << fdsu) + fdsu;
1153 } else
1154 fdout &= ~(FDO_MOEN0 << fdsu);
1155
1156 if(!turnon
1157 && (fdout & (FDO_MOEN0+FDO_MOEN1+FDO_MOEN2+FDO_MOEN3)) == 0)
1158 /* gonna turn off the last drive, put FDC to bed */
1159 fdout &= ~ (FDO_FRST|FDO_FDMAEN);
1160 else {
1161 /* make sure controller is selected and specified */
1162 if((fdout & (FDO_FRST|FDO_FDMAEN)) == 0)
1163 needspecify = 1;
1164 fdout |= (FDO_FRST|FDO_FDMAEN);
1165 }
1166
1167 fdout_wr(fdc, fdout);
1168 fdc->fdout = fdout;
1169 TRACE1("[0x%x->FDOUT]", fdout);
1170
1171 if (needspecify) {
1172 /*
1173 * XXX
1174 * special case: since we have just woken up the FDC
1175 * from its sleep, we silently assume the command will
1176 * be accepted, and do not test for a timeout
1177 */
1178 (void)fd_cmd(fdc, 3, NE7CMD_SPECIFY,
1179 NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0),
1180 0);
1181 if (fdc->flags & FDC_HAS_FIFO)
1182 (void) enable_fifo(fdc);
1183 }
1184}
1185
1186static void
1187fd_turnoff(void *xfd)
1188{
1189 int s;
1190 fd_p fd = xfd;
1191
1192 TRACE1("[fd%d: turnoff]", fd->fdu);
1193
1194 /*
1195 * Don't turn off the motor yet if the drive is active.
1196 * XXX shouldn't even schedule turnoff until drive is inactive
1197 * and nothing is queued on it.
1198 */
1199 if (fd->fdc->state != DEVIDLE && fd->fdc->fdu == fd->fdu) {
1200 fd->toffhandle = timeout(fd_turnoff, fd, 4 * hz);
1201 return;
1202 }
1203
1204 s = splbio();
1205 fd->flags &= ~FD_MOTOR;
1206 set_motor(fd->fdc, fd->fdsu, TURNOFF);
1207 splx(s);
1208}
1209
1210static void
1211fd_motor_on(void *xfd)
1212{
1213 int s;
1214 fd_p fd = xfd;
1215
1216 s = splbio();
1217 fd->flags &= ~FD_MOTOR_WAIT;
1218 if((fd->fdc->fd == fd) && (fd->fdc->state == MOTORWAIT))
1219 {
1220 fdc_intr(fd->fdc);
1221 }
1222 splx(s);
1223}
1224
1225static void
1226fd_turnon(fd_p fd)
1227{
1228 if(!(fd->flags & FD_MOTOR))
1229 {
1230 fd->flags |= (FD_MOTOR + FD_MOTOR_WAIT);
1231 set_motor(fd->fdc, fd->fdsu, TURNON);
1232 timeout(fd_motor_on, fd, hz); /* in 1 sec its ok */
1233 }
1234}
1235
1236static void
1237fdc_reset(fdc_p fdc)
1238{
1239 /* Try a reset, keep motor on */
1240 fdout_wr(fdc, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
1241 TRACE1("[0x%x->FDOUT]", fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
1242 DELAY(100);
1243 /* enable FDC, but defer interrupts a moment */
1244 fdout_wr(fdc, fdc->fdout & ~FDO_FDMAEN);
1245 TRACE1("[0x%x->FDOUT]", fdc->fdout & ~FDO_FDMAEN);
1246 DELAY(100);
1247 fdout_wr(fdc, fdc->fdout);
1248 TRACE1("[0x%x->FDOUT]", fdc->fdout);
1249
1250 /* XXX after a reset, silently believe the FDC will accept commands */
1251 (void)fd_cmd(fdc, 3, NE7CMD_SPECIFY,
1252 NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0),
1253 0);
1254 if (fdc->flags & FDC_HAS_FIFO)
1255 (void) enable_fifo(fdc);
1256}
1257
1258/****************************************************************************/
1259/* fdc in/out */
1260/****************************************************************************/
1261int
1262in_fdc(struct fdc_data *fdc)
1263{
1264 int i, j = 100000;
1265 while ((i = fdsts_rd(fdc) & (NE7_DIO|NE7_RQM))
1266 != (NE7_DIO|NE7_RQM) && j-- > 0)
1267 if (i == NE7_RQM)
1268 return fdc_err(fdc, "ready for output in input\n");
1269 if (j <= 0)
1270 return fdc_err(fdc, bootverbose? "input ready timeout\n": 0);
1271#ifdef FDC_DEBUG
1272 i = fddata_rd(fdc);
1273 TRACE1("[FDDATA->0x%x]", (unsigned char)i);
1274 return(i);
1275#else /* !FDC_DEBUG */
1276 return fddata_rd(fdc);
1277#endif /* FDC_DEBUG */
1278}
1279
1280/*
1281 * fd_in: Like in_fdc, but allows you to see if it worked.
1282 */
1283static int
1284fd_in(struct fdc_data *fdc, int *ptr)
1285{
1286 int i, j = 100000;
1287 while ((i = fdsts_rd(fdc) & (NE7_DIO|NE7_RQM))
1288 != (NE7_DIO|NE7_RQM) && j-- > 0)
1289 if (i == NE7_RQM)
1290 return fdc_err(fdc, "ready for output in input\n");
1291 if (j <= 0)
1292 return fdc_err(fdc, bootverbose? "input ready timeout\n": 0);
1293#ifdef FDC_DEBUG
1294 i = fddata_rd(fdc);
1295 TRACE1("[FDDATA->0x%x]", (unsigned char)i);
1296 *ptr = i;
1297 return 0;
1298#else /* !FDC_DEBUG */
1299 i = fddata_rd(fdc);
1300 if (ptr)
1301 *ptr = i;
1302 return 0;
1303#endif /* FDC_DEBUG */
1304}
1305
1306int
1307out_fdc(struct fdc_data *fdc, int x)
1308{
1309 int i;
1310
1311 /* Check that the direction bit is set */
1312 i = 100000;
1313 while ((fdsts_rd(fdc) & NE7_DIO) && i-- > 0);
1314 if (i <= 0) return fdc_err(fdc, "direction bit not set\n");
1315
1316 /* Check that the floppy controller is ready for a command */
1317 i = 100000;
1318 while ((fdsts_rd(fdc) & NE7_RQM) == 0 && i-- > 0);
1319 if (i <= 0)
1320 return fdc_err(fdc, bootverbose? "output ready timeout\n": 0);
1321
1322 /* Send the command and return */
1323 fddata_wr(fdc, x);
1324 TRACE1("[0x%x->FDDATA]", x);
1325 return (0);
1326}
1327
1328/****************************************************************************/
1329/* fdopen/fdclose */
1330/****************************************************************************/
1331int
1332Fdopen(dev_t dev, int flags, int mode, struct proc *p)
1333{
1334 fdu_t fdu = FDUNIT(minor(dev));
1335 int type = FDTYPE(minor(dev));
1336 fd_p fd;
1337 fdc_p fdc;
1338
1339 /* check bounds */
1340 if ((fd = devclass_get_softc(fd_devclass, fdu)) == 0)
1341 return (ENXIO);
1342 fdc = fd->fdc;
1343 if ((fdc == NULL) || (fd->type == NO_TYPE))
1344 return (ENXIO);
1345 if (type > NUMDENS)
1346 return (ENXIO);
1347 if (type == 0)
1348 type = fd->type;
1349 else {
1350 /*
1351 * For each type of basic drive, make sure we are trying
1352 * to open a type it can do,
1353 */
1354 if (type != fd->type) {
1355 switch (fd->type) {
1356 case FD_360:
1357 return (ENXIO);
1358 case FD_720:
1359 if ( type != FD_820
1360 && type != FD_800
1361 )
1362 return (ENXIO);
1363 break;
1364 case FD_1200:
1365 switch (type) {
1366 case FD_1480:
1367 type = FD_1480in5_25;
1368 break;
1369 case FD_1440:
1370 type = FD_1440in5_25;
1371 break;
1372 case FD_820:
1373 type = FD_820in5_25;
1374 break;
1375 case FD_800:
1376 type = FD_800in5_25;
1377 break;
1378 case FD_720:
1379 type = FD_720in5_25;
1380 break;
1381 case FD_360:
1382 type = FD_360in5_25;
1383 break;
1384 default:
1385 return(ENXIO);
1386 }
1387 break;
1388 case FD_1440:
1389 if ( type != FD_1720
1390 && type != FD_1480
1391 && type != FD_1200
1392 && type != FD_820
1393 && type != FD_800
1394 && type != FD_720
1395 )
1396 return(ENXIO);
1397 break;
1398 }
1399 }
1400 }
1401 fd->ft = fd_types + type - 1;
1402 fd->flags |= FD_OPEN;
1403 device_busy(fd->dev);
1404 device_busy(fd->fdc->fdc_dev);
1405 return 0;
1406}
1407
1408int
1409fdclose(dev_t dev, int flags, int mode, struct proc *p)
1410{
1411 fdu_t fdu = FDUNIT(minor(dev));
1412 struct fd_data *fd;
1413
1414 fd = devclass_get_softc(fd_devclass, fdu);
1415 fd->flags &= ~FD_OPEN;
1416 fd->options &= ~FDOPT_NORETRY;
1417
1418 return (0);
1419}
1420
1421/****************************************************************************/
1422/* fdstrategy */
1423/****************************************************************************/
1424void
1425fdstrategy(struct buf *bp)
1426{
1427 unsigned nblocks, blknum, cando;
1428 int s;
1429 fdu_t fdu;
1430 fdc_p fdc;
1431 fd_p fd;
1432 size_t fdblk;
1433
1434 fdu = FDUNIT(minor(bp->b_dev));
1435 fd = devclass_get_softc(fd_devclass, fdu);
1436 if (fd == 0)
1437 panic("fdstrategy: buf for nonexistent device (%#lx, %#lx)",
1438 (u_long)major(bp->b_dev), (u_long)minor(bp->b_dev));
1439 fdc = fd->fdc;
1440#ifdef FDC_YE
1441 if (fd->type == NO_TYPE) {
1442 bp->b_error = ENXIO;
1443 bp->b_flags |= B_ERROR;
1444 /*
1445 * I _refuse_ to use a goto
1446 */
1447 biodone(bp);
1448 return;
1449 };
1450#endif
1451
1452 fdblk = 128 << (fd->ft->secsize);
1453 if (!(bp->b_flags & B_FORMAT)) {
1454 if (bp->b_blkno < 0) {
1455 printf(
1456 "fd%d: fdstrat: bad request blkno = %lu, bcount = %ld\n",
1457 fdu, (u_long)bp->b_blkno, bp->b_bcount);
1458 bp->b_error = EINVAL;
1459 bp->b_flags |= B_ERROR;
1460 goto bad;
1461 }
1462 if ((bp->b_bcount % fdblk) != 0) {
1463 bp->b_error = EINVAL;
1464 bp->b_flags |= B_ERROR;
1465 goto bad;
1466 }
1467 }
1468
1469 /*
1470 * Set up block calculations.
1471 */
1472 if (bp->b_blkno > 20000000) {
1473 /*
1474 * Reject unreasonably high block number, prevent the
1475 * multiplication below from overflowing.
1476 */
1477 bp->b_error = EINVAL;
1478 bp->b_flags |= B_ERROR;
1479 goto bad;
1480 }
1481 blknum = (unsigned) bp->b_blkno * DEV_BSIZE/fdblk;
1482 nblocks = fd->ft->size;
1483 bp->b_resid = 0;
1484 if (blknum + (bp->b_bcount / fdblk) > nblocks) {
1485 if (blknum <= nblocks) {
1486 cando = (nblocks - blknum) * fdblk;
1487 bp->b_resid = bp->b_bcount - cando;
1488 if (cando == 0)
1489 goto bad; /* not actually bad but EOF */
1490 } else {
1491 bp->b_error = EINVAL;
1492 bp->b_flags |= B_ERROR;
1493 goto bad;
1494 }
1495 }
1496 bp->b_pblkno = bp->b_blkno;
1497 s = splbio();
1498 bufqdisksort(&fdc->head, bp);
1499 untimeout(fd_turnoff, fd, fd->toffhandle); /* a good idea */
1500
1501 /* Tell devstat we are starting on the transaction */
1502 devstat_start_transaction(&fd->device_stats);
1503
1504 fdstart(fdc);
1505 splx(s);
1506 return;
1507
1508bad:
1509 biodone(bp);
1510}
1511
1512/***************************************************************\
1513* fdstart *
1514* We have just queued something.. if the controller is not busy *
1515* then simulate the case where it has just finished a command *
1516* So that it (the interrupt routine) looks on the queue for more*
1517* work to do and picks up what we just added. *
1518* If the controller is already busy, we need do nothing, as it *
1519* will pick up our work when the present work completes *
1520\***************************************************************/
1521static void
1522fdstart(struct fdc_data *fdc)
1523{
1524 int s;
1525
1526 s = splbio();
1527 if(fdc->state == DEVIDLE)
1528 {
1529 fdc_intr(fdc);
1530 }
1531 splx(s);
1532}
1533
1534static void
1535fd_iotimeout(void *xfdc)
1536{
1537 fdc_p fdc;
1538 int s;
1539
1540 fdc = xfdc;
1541 TRACE1("fd%d[fd_iotimeout()]", fdc->fdu);
1542
1543 /*
1544 * Due to IBM's brain-dead design, the FDC has a faked ready
1545 * signal, hardwired to ready == true. Thus, any command
1546 * issued if there's no diskette in the drive will _never_
1547 * complete, and must be aborted by resetting the FDC.
1548 * Many thanks, Big Blue!
1549 * The FDC must not be reset directly, since that would
1550 * interfere with the state machine. Instead, pretend that
1551 * the command completed but was invalid. The state machine
1552 * will reset the FDC and retry once.
1553 */
1554 s = splbio();
1555 fdc->status[0] = NE7_ST0_IC_IV;
1556 fdc->flags &= ~FDC_STAT_VALID;
1557 fdc->state = IOTIMEDOUT;
1558 fdc_intr(fdc);
1559 splx(s);
1560}
1561
1562/* just ensure it has the right spl */
1563static void
1564fd_pseudointr(void *xfdc)
1565{
1566 int s;
1567
1568 s = splbio();
1569 fdc_intr(xfdc);
1570 splx(s);
1571}
1572
1573/***********************************************************************\
1574* fdintr *
1575* keep calling the state machine until it returns a 0 *
1576* ALWAYS called at SPLBIO *
1577\***********************************************************************/
1578static void
1579fdc_intr(void *xfdc)
1580{
1581 fdc_p fdc = xfdc;
1582 while(fdstate(fdc))
1583 ;
1584}
1585
1586#ifdef FDC_YE
1587/*
1588 * magic pseudo-DMA initialization for YE FDC. Sets count and
1589 * direction
1590 */
1591#define SET_BCDR(wr,cnt,port) outb(port,(((cnt)-1) & 0xff)); \
1592 outb(port+1,((wr ? 0x80 : 0) | ((((cnt)-1) >> 8) & 0x7f)))
1593
1594/*
1595 * fdcpio(): perform programmed IO read/write for YE PCMCIA floppy
1596 */
1597static int fdcpio(fdcu_t fdcu, long flags, caddr_t addr, u_int count)
1598{
1599 u_char *cptr = (u_char *)addr;
1600 fdc_p fdc = &fdc_data[fdcu];
1601 int io = fdc->baseport;
1602
1603 if (flags & B_READ) {
1604 if (fdc->state != PIOREAD) {
1605 fdc->state = PIOREAD;
1606 return(0);
1607 };
1608 SET_BCDR(0,count,io);
1609 insb(io+FDC_YE_DATAPORT,cptr,count);
1610 } else {
1611 outsb(io+FDC_YE_DATAPORT,cptr,count);
1612 SET_BCDR(0,count,io);
1613 };
1614 return(1);
1615}
1616#endif /* FDC_YE */
1617
1618/***********************************************************************\
1619* The controller state machine. *
1620* if it returns a non zero value, it should be called again immediatly *
1621\***********************************************************************/
1622static int
1623fdstate(fdc_p fdc)
1624{
1625 int read, format, head, i, sec = 0, sectrac, st0, cyl, st3;
1626 unsigned blknum = 0, b_cylinder = 0;
1627 fdu_t fdu = fdc->fdu;
1628 fd_p fd;
1629 register struct buf *bp;
1630 struct fd_formb *finfo = NULL;
1631 size_t fdblk;
1632
1633 bp = fdc->bp;
1634 if (bp == NULL) {
1635 bp = bufq_first(&fdc->head);
1636 if (bp != NULL) {
1637 bufq_remove(&fdc->head, bp);
1638 fdc->bp = bp;
1639 }
1640 }
1641 if (bp == NULL) {
1642 /***********************************************\
1643 * nothing left for this controller to do *
1644 * Force into the IDLE state, *
1645 \***********************************************/
1646 fdc->state = DEVIDLE;
1647 if (fdc->fd) {
1648 device_print_prettyname(fdc->fdc_dev);
1649 printf("unexpected valid fd pointer\n");
1650 fdc->fd = (fd_p) 0;
1651 fdc->fdu = -1;
1652 }
1653 TRACE1("[fdc%d IDLE]", fdc->fdcu);
1654 return (0);
1655 }
1656 fdu = FDUNIT(minor(bp->b_dev));
1657 fd = devclass_get_softc(fd_devclass, fdu);
1658 fdblk = 128 << fd->ft->secsize;
1659 if (fdc->fd && (fd != fdc->fd)) {
1660 device_print_prettyname(fd->dev);
1661 printf("confused fd pointers\n");
1662 }
1663 read = bp->b_flags & B_READ;
1664 format = bp->b_flags & B_FORMAT;
1665 if (format) {
1666 finfo = (struct fd_formb *)bp->b_data;
1667 fd->skip = (char *)&(finfo->fd_formb_cylno(0))
1668 - (char *)finfo;
1669 }
1670 if (fdc->state == DOSEEK || fdc->state == SEEKCOMPLETE) {
1671 blknum = (unsigned) bp->b_pblkno * DEV_BSIZE/fdblk +
1672 fd->skip/fdblk;
1673 b_cylinder = blknum / (fd->ft->sectrac * fd->ft->heads);
1674 }
1675 TRACE1("fd%d", fdu);
1676 TRACE1("[%s]", fdstates[fdc->state]);
1677 TRACE1("(0x%x)", fd->flags);
1678 untimeout(fd_turnoff, fd, fd->toffhandle);
1679 fd->toffhandle = timeout(fd_turnoff, fd, 4 * hz);
1680 switch (fdc->state)
1681 {
1682 case DEVIDLE:
1683 case FINDWORK: /* we have found new work */
1684 fdc->retry = 0;
1685 fd->skip = 0;
1686 fdc->fd = fd;
1687 fdc->fdu = fdu;
1688 fdctl_wr(fdc, fd->ft->trans);
1689 TRACE1("[0x%x->FDCTL]", fd->ft->trans);
1690 /*******************************************************\
1691 * If the next drive has a motor startup pending, then *
1692 * it will start up in its own good time *
1693 \*******************************************************/
1694 if(fd->flags & FD_MOTOR_WAIT) {
1695 fdc->state = MOTORWAIT;
1696 return (0); /* come back later */
1697 }
1698 /*******************************************************\
1699 * Maybe if it's not starting, it SHOULD be starting *
1700 \*******************************************************/
1701 if (!(fd->flags & FD_MOTOR))
1702 {
1703 fdc->state = MOTORWAIT;
1704 fd_turnon(fd);
1705 return (0);
1706 }
1707 else /* at least make sure we are selected */
1708 {
1709 set_motor(fdc, fd->fdsu, TURNON);
1710 }
1711 if (fdc->flags & FDC_NEEDS_RESET) {
1712 fdc->state = RESETCTLR;
1713 fdc->flags &= ~FDC_NEEDS_RESET;
1714 } else
1715 fdc->state = DOSEEK;
1716 break;
1717 case DOSEEK:
1718 if (b_cylinder == (unsigned)fd->track)
1719 {
1720 fdc->state = SEEKCOMPLETE;
1721 break;
1722 }
1723 if (fd_cmd(fdc, 3, NE7CMD_SEEK,
1724 fd->fdsu, b_cylinder * fd->ft->steptrac,
1725 0))
1726 {
1727 /*
1728 * seek command not accepted, looks like
1729 * the FDC went off to the Saints...
1730 */
1731 fdc->retry = 6; /* try a reset */
1732 return(retrier(fdc));
1733 }
1734 fd->track = FD_NO_TRACK;
1735 fdc->state = SEEKWAIT;
1736 return(0); /* will return later */
1737 case SEEKWAIT:
1738 /* allow heads to settle */
1739 timeout(fd_pseudointr, fdc, hz / 16);
1740 fdc->state = SEEKCOMPLETE;
1741 return(0); /* will return later */
1742 case SEEKCOMPLETE : /* SEEK DONE, START DMA */
1743 /* Make sure seek really happened*/
1744 if(fd->track == FD_NO_TRACK) {
1745 int descyl = b_cylinder * fd->ft->steptrac;
1746 do {
1747 /*
1748 * This might be a "ready changed" interrupt,
1749 * which cannot really happen since the
1750 * RDY pin is hardwired to + 5 volts. This
1751 * generally indicates a "bouncing" intr
1752 * line, so do one of the following:
1753 *
1754 * When running on an enhanced FDC that is
1755 * known to not go stuck after responding
1756 * with INVALID, fetch all interrupt states
1757 * until seeing either an INVALID or a
1758 * real interrupt condition.
1759 *
1760 * When running on a dumb old NE765, give
1761 * up immediately. The controller will
1762 * provide up to four dummy RC interrupt
1763 * conditions right after reset (for the
1764 * corresponding four drives), so this is
1765 * our only chance to get notice that it
1766 * was not the FDC that caused the interrupt.
1767 */
1768 if (fd_sense_int(fdc, &st0, &cyl)
1769 == FD_NOT_VALID)
1770 return 0;
1771 if(fdc->fdct == FDC_NE765
1772 && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
1773 return 0; /* hope for a real intr */
1774 } while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
1775
1776 if (0 == descyl) {
1777 int failed = 0;
1778 /*
1779 * seek to cyl 0 requested; make sure we are
1780 * really there
1781 */
1782 if (fd_sense_drive_status(fdc, &st3))
1783 failed = 1;
1784 if ((st3 & NE7_ST3_T0) == 0) {
1785 printf(
1786 "fd%d: Seek to cyl 0, but not really there (ST3 = %b)\n",
1787 fdu, st3, NE7_ST3BITS);
1788 failed = 1;
1789 }
1790
1791 if (failed) {
1792 if(fdc->retry < 3)
1793 fdc->retry = 3;
1794 return (retrier(fdc));
1795 }
1796 }
1797
1798 if (cyl != descyl) {
1799 printf(
1800 "fd%d: Seek to cyl %d failed; am at cyl %d (ST0 = 0x%x)\n",
1801 fdu, descyl, cyl, st0);
1802 if (fdc->retry < 3)
1803 fdc->retry = 3;
1804 return (retrier(fdc));
1805 }
1806 }
1807
1808 fd->track = b_cylinder;
1809#ifdef FDC_YE
1810 if (!(fdc->flags & FDC_PCMCIA))
1811#endif
1812 isa_dmastart(bp->b_flags, bp->b_data+fd->skip,
1813 format ? bp->b_bcount : fdblk, fdc->dmachan);
1814 sectrac = fd->ft->sectrac;
1815 sec = blknum % (sectrac * fd->ft->heads);
1816 head = sec / sectrac;
1817 sec = sec % sectrac + 1;
1818 fd->hddrv = ((head&1)<<2)+fdu;
1819
1820 if(format || !read)
1821 {
1822 /* make sure the drive is writable */
1823 if(fd_sense_drive_status(fdc, &st3) != 0)
1824 {
1825 /* stuck controller? */
1826 isa_dmadone(bp->b_flags, bp->b_data + fd->skip,
1827 format ? bp->b_bcount : fdblk,
1828 fdc->dmachan);
1829 fdc->retry = 6; /* reset the beast */
1830 return (retrier(fdc));
1831 }
1832 if(st3 & NE7_ST3_WP)
1833 {
1834 /*
1835 * XXX YES! this is ugly.
1836 * in order to force the current operation
1837 * to fail, we will have to fake an FDC
1838 * error - all error handling is done
1839 * by the retrier()
1840 */
1841 fdc->status[0] = NE7_ST0_IC_AT;
1842 fdc->status[1] = NE7_ST1_NW;
1843 fdc->status[2] = 0;
1844 fdc->status[3] = fd->track;
1845 fdc->status[4] = head;
1846 fdc->status[5] = sec;
1847 fdc->retry = 8; /* break out immediately */
1848 fdc->state = IOTIMEDOUT; /* not really... */
1849 return (1);
1850 }
1851 }
1852
1853 if (format) {
1854#ifdef FDC_YE
1855 if (fdc->flags & FDC_PCMCIA)
1856 (void)fdcpio(fdcu,bp->b_flags,
1857 bp->b_data+fd->skip,
1858 bp->b_bcount);
1859#endif
1860 /* formatting */
1861 if(fd_cmd(fdc, 6, NE7CMD_FORMAT, head << 2 | fdu,
1862 finfo->fd_formb_secshift,
1863 finfo->fd_formb_nsecs,
1864 finfo->fd_formb_gaplen,
1865 finfo->fd_formb_fillbyte, 0)) {
1866 /* controller fell over */
1867 isa_dmadone(bp->b_flags, bp->b_data + fd->skip,
1868 format ? bp->b_bcount : fdblk,
1869 fdc->dmachan);
1870 fdc->retry = 6;
1871 return (retrier(fdc));
1872 }
1873 } else {
1874#ifdef FDC_YE
1875 if (fdc->flags & FDC_PCMCIA) {
1876 /*
1877 * this seems to be necessary even when
1878 * reading data
1879 */
1880 SET_BCDR(1,fdblk,fdc->baseport);
1881
1882 /*
1883 * perform the write pseudo-DMA before
1884 * the WRITE command is sent
1885 */
1886 if (!read)
1887 (void)fdcpio(fdcu,bp->b_flags,
1888 bp->b_data+fd->skip,
1889 fdblk);
1890 }
1891#endif
1892 if (fd_cmd(fdc, 9,
1893 (read ? NE7CMD_READ : NE7CMD_WRITE),
1894 head << 2 | fdu, /* head & unit */
1895 fd->track, /* track */
1896 head,
1897 sec, /* sector + 1 */
1898 fd->ft->secsize, /* sector size */
1899 sectrac, /* sectors/track */
1900 fd->ft->gap, /* gap size */
1901 fd->ft->datalen, /* data length */
1902 0)) {
1903 /* the beast is sleeping again */
1904 isa_dmadone(bp->b_flags, bp->b_data + fd->skip,
1905 format ? bp->b_bcount : fdblk,
1906 fdc->dmachan);
1907 fdc->retry = 6;
1908 return (retrier(fdc));
1909 }
1910 }
1911#ifdef FDC_YE
1912 if (fdc->flags & FDC_PCMCIA)
1913 /*
1914 * if this is a read, then simply await interrupt
1915 * before performing PIO
1916 */
1917 if (read && !fdcpio(fdcu,bp->b_flags,
1918 bp->b_data+fd->skip,fdblk)) {
1919 fd->tohandle = timeout(fd_iotimeout,
1920 (caddr_t)fdcu, hz);
1921 return(0); /* will return later */
1922 };
1923
1924 /*
1925 * write (or format) operation will fall through and
1926 * await completion interrupt
1927 */
1928#endif
1929 fdc->state = IOCOMPLETE;
1930 fd->tohandle = timeout(fd_iotimeout, fdc, hz);
1931 return (0); /* will return later */
1932#ifdef FDC_YE
1933 case PIOREAD:
1934 /*
1935 * actually perform the PIO read. The IOCOMPLETE case
1936 * removes the timeout for us.
1937 */
1938 (void)fdcpio(fdcu,bp->b_flags,bp->b_data+fd->skip,fdblk);
1939 fdc->state = IOCOMPLETE;
1940 /* FALLTHROUGH */
1941#endif
1942 case IOCOMPLETE: /* IO DONE, post-analyze */
1943 untimeout(fd_iotimeout, fdc, fd->tohandle);
1944
1945 if (fd_read_status(fdc, fd->fdsu)) {
1946 isa_dmadone(bp->b_flags, bp->b_data + fd->skip,
1947 format ? bp->b_bcount : fdblk,
1948 fdc->dmachan);
1949 if (fdc->retry < 6)
1950 fdc->retry = 6; /* force a reset */
1951 return (retrier(fdc));
1952 }
1953
1954 fdc->state = IOTIMEDOUT;
1955
1956 /* FALLTHROUGH */
1957
1958 case IOTIMEDOUT:
1959#ifdef FDC_YE
1960 if (!(fdc->flags & FDC_PCMCIA))
1961#endif
1962 isa_dmadone(bp->b_flags, bp->b_data + fd->skip,
1963 format ? bp->b_bcount : fdblk, fdc->dmachan);
1964 if (fdc->status[0] & NE7_ST0_IC) {
1965 if ((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
1966 && fdc->status[1] & NE7_ST1_OR) {
1967 /*
1968 * DMA overrun. Someone hogged the bus
1969 * and didn't release it in time for the
1970 * next FDC transfer.
1971 * Just restart it, don't increment retry
1972 * count. (vak)
1973 */
1974 fdc->state = SEEKCOMPLETE;
1975 return (1);
1976 }
1977 else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_IV
1978 && fdc->retry < 6)
1979 fdc->retry = 6; /* force a reset */
1980 else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
1981 && fdc->status[2] & NE7_ST2_WC
1982 && fdc->retry < 3)
1983 fdc->retry = 3; /* force recalibrate */
1984 return (retrier(fdc));
1985 }
1986 /* All OK */
1987 fd->skip += fdblk;
1988 if (!format && fd->skip < bp->b_bcount - bp->b_resid) {
1989 /* set up next transfer */
1990 fdc->state = DOSEEK;
1991 } else {
1992 /* ALL DONE */
1993 fd->skip = 0;
1994 fdc->bp = NULL;
1995 devstat_end_transaction_buf(&fd->device_stats, bp);
1996 biodone(bp);
1997 fdc->fd = (fd_p) 0;
1998 fdc->fdu = -1;
1999 fdc->state = FINDWORK;
2000 }
2001 return (1);
2002 case RESETCTLR:
2003 fdc_reset(fdc);
2004 fdc->retry++;
2005 fdc->state = RESETCOMPLETE;
2006 return (0);
2007 case RESETCOMPLETE:
2008 /*
2009 * Discard all the results from the reset so that they
2010 * can't cause an unexpected interrupt later.
2011 */
2012 for (i = 0; i < 4; i++)
2013 (void)fd_sense_int(fdc, &st0, &cyl);
2014 fdc->state = STARTRECAL;
2015 /* Fall through. */
2016 case STARTRECAL:
2017 if(fd_cmd(fdc, 2, NE7CMD_RECAL, fdu, 0)) {
2018 /* arrgl */
2019 fdc->retry = 6;
2020 return (retrier(fdc));
2021 }
2022 fdc->state = RECALWAIT;
2023 return (0); /* will return later */
2024 case RECALWAIT:
2025 /* allow heads to settle */
2026 timeout(fd_pseudointr, fdc, hz / 8);
2027 fdc->state = RECALCOMPLETE;
2028 return (0); /* will return later */
2029 case RECALCOMPLETE:
2030 do {
2031 /*
2032 * See SEEKCOMPLETE for a comment on this:
2033 */
2034 if (fd_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
2035 return 0;
2036 if(fdc->fdct == FDC_NE765
2037 && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
2038 return 0; /* hope for a real intr */
2039 } while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
2040 if ((st0 & NE7_ST0_IC) != NE7_ST0_IC_NT || cyl != 0)
2041 {
2042 if(fdc->retry > 3)
2043 /*
2044 * a recalibrate from beyond cylinder 77
2045 * will "fail" due to the FDC limitations;
2046 * since people used to complain much about
2047 * the failure message, try not logging
2048 * this one if it seems to be the first
2049 * time in a line
2050 */
2051 printf("fd%d: recal failed ST0 %b cyl %d\n",
2052 fdu, st0, NE7_ST0BITS, cyl);
2053 if(fdc->retry < 3) fdc->retry = 3;
2054 return (retrier(fdc));
2055 }
2056 fd->track = 0;
2057 /* Seek (probably) necessary */
2058 fdc->state = DOSEEK;
2059 return (1); /* will return immediatly */
2060 case MOTORWAIT:
2061 if(fd->flags & FD_MOTOR_WAIT)
2062 {
2063 return (0); /* time's not up yet */
2064 }
2065 if (fdc->flags & FDC_NEEDS_RESET) {
2066 fdc->state = RESETCTLR;
2067 fdc->flags &= ~FDC_NEEDS_RESET;
2068 } else {
2069 /*
2070 * If all motors were off, then the controller was
2071 * reset, so it has lost track of the current
2072 * cylinder. Recalibrate to handle this case.
2073 * But first, discard the results of the reset.
2074 */
2075 fdc->state = RESETCOMPLETE;
2076 }
2077 return (1); /* will return immediatly */
2078 default:
2079 device_print_prettyname(fdc->fdc_dev);
2080 printf("unexpected FD int->");
2081 if (fd_read_status(fdc, fd->fdsu) == 0)
2082 printf("FDC status :%x %x %x %x %x %x %x ",
2083 fdc->status[0],
2084 fdc->status[1],
2085 fdc->status[2],
2086 fdc->status[3],
2087 fdc->status[4],
2088 fdc->status[5],
2089 fdc->status[6] );
2090 else
2091 printf("No status available ");
2092 if (fd_sense_int(fdc, &st0, &cyl) != 0)
2093 {
2094 printf("[controller is dead now]\n");
2095 return (0);
2096 }
2097 printf("ST0 = %x, PCN = %x\n", st0, cyl);
2098 return (0);
2099 }
2100 /*XXX confusing: some branches return immediately, others end up here*/
2101 return (1); /* Come back immediatly to new state */
2102}
2103
2104static int
2105retrier(struct fdc_data *fdc)
2106{
2107 register struct buf *bp;
2108 struct fd_data *fd;
2109 int fdu;
2110
2111 bp = fdc->bp;
2112
2113 /* XXX shouldn't this be cached somewhere? */
2114 fdu = FDUNIT(minor(bp->b_dev));
2115 fd = devclass_get_softc(fd_devclass, fdu);
2116 if (fd->options & FDOPT_NORETRY)
2117 goto fail;
2118
2119 switch (fdc->retry) {
2120 case 0: case 1: case 2:
2121 fdc->state = SEEKCOMPLETE;
2122 break;
2123 case 3: case 4: case 5:
2124 fdc->state = STARTRECAL;
2125 break;
2126 case 6:
2127 fdc->state = RESETCTLR;
2128 break;
2129 case 7:
2130 break;
2131 default:
2132 fail:
2133 {
2134 dev_t sav_b_dev = bp->b_dev;
2135 /* Trick diskerr */
2136 bp->b_dev = makedev(major(bp->b_dev),
2137 (FDUNIT(minor(bp->b_dev))<<3)|RAW_PART);
2138 diskerr(bp, "hard error", LOG_PRINTF,
2139 fdc->fd->skip / DEV_BSIZE,
2140 (struct disklabel *)NULL);
2141 bp->b_dev = sav_b_dev;
2142 if (fdc->flags & FDC_STAT_VALID)
2143 {
2144 printf(
2145 " (ST0 %b ST1 %b ST2 %b cyl %u hd %u sec %u)\n",
2146 fdc->status[0], NE7_ST0BITS,
2147 fdc->status[1], NE7_ST1BITS,
2148 fdc->status[2], NE7_ST2BITS,
2149 fdc->status[3], fdc->status[4],
2150 fdc->status[5]);
2151 }
2152 else
2153 printf(" (No status)\n");
2154 }
2155 bp->b_flags |= B_ERROR;
2156 bp->b_error = EIO;
2157 bp->b_resid += bp->b_bcount - fdc->fd->skip;
2158 fdc->bp = NULL;
2159 fdc->fd->skip = 0;
2160 devstat_end_transaction_buf(&fdc->fd->device_stats, bp);
2161 biodone(bp);
2162 fdc->state = FINDWORK;
2163 fdc->flags |= FDC_NEEDS_RESET;
2164 fdc->fd = (fd_p) 0;
2165 fdc->fdu = -1;
2166 return (1);
2167 }
2168 fdc->retry++;
2169 return (1);
2170}
2171
2172static int
2173fdformat(dev, finfo, p)
2174 dev_t dev;
2175 struct fd_formb *finfo;
2176 struct proc *p;
2177{
2178 fdu_t fdu;
2179 fd_p fd;
2180
2181 struct buf *bp;
2182 int rv = 0, s;
2183 size_t fdblk;
2184
2185 fdu = FDUNIT(minor(dev));
2186 fd = devclass_get_softc(fd_devclass, fdu);
2187 fdblk = 128 << fd->ft->secsize;
2188
2189 /* set up a buffer header for fdstrategy() */
2190 bp = (struct buf *)malloc(sizeof(struct buf), M_TEMP, M_NOWAIT);
2191 if(bp == 0)
2192 return ENOBUFS;
2193 /*
2194 * keep the process from being swapped
2195 */
2196 PHOLD(p);
2197 bzero((void *)bp, sizeof(struct buf));
2198 BUF_LOCKINIT(bp);
2199 BUF_LOCK(bp, LK_EXCLUSIVE);
2200 bp->b_flags = B_PHYS | B_FORMAT;
2201
2202 /*
2203 * calculate a fake blkno, so fdstrategy() would initiate a
2204 * seek to the requested cylinder
2205 */
2206 bp->b_blkno = (finfo->cyl * (fd->ft->sectrac * fd->ft->heads)
2207 + finfo->head * fd->ft->sectrac) * fdblk / DEV_BSIZE;
2208
2209 bp->b_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs;
2210 bp->b_data = (caddr_t)finfo;
2211
2212 /* now do the format */
2213 bp->b_dev = dev;
2214 BUF_STRATEGY(bp, 0);
2215
2216 /* ...and wait for it to complete */
2217 s = splbio();
2218 while(!(bp->b_flags & B_DONE)) {
2219 rv = tsleep((caddr_t)bp, PRIBIO, "fdform", 20 * hz);
2220 if (rv == EWOULDBLOCK)
2221 break;
2222 }
2223 splx(s);
2224
2225 if (rv == EWOULDBLOCK) {
2226 /* timed out */
2227 rv = EIO;
2228 biodone(bp);
2229 }
2230 if (bp->b_flags & B_ERROR)
2231 rv = bp->b_error;
2232 /*
2233 * allow the process to be swapped
2234 */
2235 PRELE(p);
2236 BUF_UNLOCK(bp);
2237 BUF_LOCKFREE(bp);
2238 free(bp, M_TEMP);
2239 return rv;
2240}
2241
2242/*
2243 * TODO: don't allocate buffer on stack.
2244 */
2245
2246static int
2247fdioctl(dev, cmd, addr, flag, p)
2248 dev_t dev;
2249 u_long cmd;
2250 caddr_t addr;
2251 int flag;
2252 struct proc *p;
2253{
2254 fdu_t fdu = FDUNIT(minor(dev));
2255 fd_p fd = devclass_get_softc(fd_devclass, fdu);
2256 size_t fdblk;
2257
2258 struct fd_type *fdt;
2259 struct disklabel *dl;
2260 char buffer[DEV_BSIZE];
2261 int error = 0;
2262
2263 fdblk = 128 << fd->ft->secsize;
2264
2265 switch (cmd) {
2266 case DIOCGDINFO:
2267 bzero(buffer, sizeof (buffer));
2268 dl = (struct disklabel *)buffer;
2269 dl->d_secsize = fdblk;
2270 fdt = fd->ft;
2271 dl->d_secpercyl = fdt->size / fdt->tracks;
2272 dl->d_type = DTYPE_FLOPPY;
2273
2274 if (readdisklabel(dkmodpart(dev, RAW_PART), dl)
2275 == NULL)
2276 error = 0;
2277 else
2278 error = EINVAL;
2279
2280 *(struct disklabel *)addr = *dl;
2281 break;
2282
2283 case DIOCSDINFO:
2284 if ((flag & FWRITE) == 0)
2285 error = EBADF;
2286 break;
2287
2288 case DIOCWLABEL:
2289 if ((flag & FWRITE) == 0)
2290 error = EBADF;
2291 break;
2292
2293 case DIOCWDINFO:
2294 if ((flag & FWRITE) == 0) {
2295 error = EBADF;
2296 break;
2297 }
2298
2299 dl = (struct disklabel *)addr;
2300
2301 if ((error = setdisklabel((struct disklabel *)buffer, dl,
2302 (u_long)0)) != 0)
2303 break;
2304
2305 error = writedisklabel(dev, (struct disklabel *)buffer);
2306 break;
2307 case FD_FORM:
2308 if ((flag & FWRITE) == 0)
2309 error = EBADF; /* must be opened for writing */
2310 else if (((struct fd_formb *)addr)->format_version !=
2311 FD_FORMAT_VERSION)
2312 error = EINVAL; /* wrong version of formatting prog */
2313 else
2314 error = fdformat(dev, (struct fd_formb *)addr, p);
2315 break;
2316
2317 case FD_GTYPE: /* get drive type */
2318 *(struct fd_type *)addr = *fd->ft;
2319 break;
2320
2321 case FD_STYPE: /* set drive type */
2322 /* this is considered harmful; only allow for superuser */
2323 if (suser(p) != 0)
2324 return EPERM;
2325 *fd->ft = *(struct fd_type *)addr;
2326 break;
2327
2328 case FD_GOPTS: /* get drive options */
2329 *(int *)addr = fd->options;
2330 break;
2331
2332 case FD_SOPTS: /* set drive options */
2333 fd->options = *(int *)addr;
2334 break;
2335
2336 default:
2337 error = ENOTTY;
2338 break;
2339 }
2340 return (error);
2341}
2342
2343/*
2344 * Hello emacs, these are the
2345 * Local Variables:
2346 * c-indent-level: 8
2347 * c-continued-statement-offset: 8
2348 * c-continued-brace-offset: 0
2349 * c-brace-offset: -8
2350 * c-brace-imaginary-offset: 0
2351 * c-argdecl-indent: 8
2352 * c-label-offset: -8
2353 * c++-hanging-braces: 1
2354 * c++-access-specifier-offset: -8
2355 * c++-empty-arglist-indent: 8
2356 * c++-friend-offset: 0
2357 * End:
2358 */