2 * Copyright (c) 1990 The Regents of the University of California.
5 * This code is derived from software contributed to Berkeley by
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).
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)
17 * Copyright (c) 1993, 1994, 1995 by
18 * joerg_wunsch@uriah.sax.de (Joerg Wunsch)
19 * dufault@hda.com (Peter Dufault)
21 * Redistribution and use in source and binary forms, with or without
22 * modification, are permitted provided that the following conditions
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.
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
49 * from: @(#)fd.c 7.4 (Berkeley) 5/25/91
56 #include <sys/param.h>
57 #include <sys/systm.h>
58 #include <sys/kernel.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>
68 #include <sys/syslog.h>
71 #include <machine/bus.h>
74 #include <machine/clock.h>
75 #include <machine/ioctl_fd.h>
76 #include <machine/resource.h>
77 #include <machine/stdarg.h>
79 #include <isa/isavar.h>
81 #include <pc98/pc98/pc98.h>
82 #include <pc98/pc98/pc98_machdep.h>
83 #include <pc98/pc98/epsonio.h>
84 #include <pc98/pc98/fdreg.h>
87 #include <isa/isareg.h>
88 #include <isa/fdreg.h>
93 /* misuse a flag to identify format operation */
94 #define B_FORMAT B_XXX
96 /* configuration flags */
97 #define FDC_PRETEND_D0 (1 << 0) /* pretend drive 0 to be there */
98 #define FDC_NO_FIFO (1 << 2) /* do not enable FIFO */
100 /* internally used only, not really from CMOS: */
101 #define RTCFDT_144M_PRETENDED 0x1000
103 /* error returns for fd_cmd() */
105 #define FD_NOT_VALID -2
106 #define FDC_ERRMAX 100 /* do not log more */
110 #define NUMDENS NUMTYPES
113 #define NUMDENS (NUMTYPES - 7)
116 /* These defines (-1) must match index for fd_types */
117 #define F_TAPE_TYPE 0x020 /* bit for fd_types to indicate tape */
118 #define NO_TYPE 0 /* must match NO_TYPE in ft.c */
134 #define FDT_NONE 0 /* none present */
135 #define FDT_12M 1 /* 1M/640K FDD */
136 #define FDT_144M 2 /* 1.44M/1M/640K FDD */
138 #define FD_1480in5_25 11
139 #define FD_1440in5_25 12
140 #define FD_820in5_25 13
141 #define FD_800in5_25 14
142 #define FD_720in5_25 15
143 #define FD_360in5_25 16
144 #define FD_640in5_25 17
148 static struct fd_type fd_types[NUMTYPES] =
151 { 21,2,0xFF,0x04,82,3444,1,2,2,0x0C,2 }, /* 1.72M in 3mode */
152 { 18,2,0xFF,0x1B,82,2952,1,2,2,0x54,1 }, /* 1.48M in 3mode */
153 { 18,2,0xFF,0x1B,80,2880,1,2,2,0x54,1 }, /* 1.44M in 3mode */
154 { 15,2,0xFF,0x1B,80,2400,1,0,2,0x54,1 }, /* 1.2M */
155 { 10,2,0xFF,0x10,82,1640,1,1,2,0x30,1 }, /* 820K */
156 { 10,2,0xFF,0x10,80,1600,1,1,2,0x30,1 }, /* 800K */
157 { 9,2,0xFF,0x20,80,1440,1,1,2,0x50,1 }, /* 720K */
158 { 9,2,0xFF,0x20,40, 720,2,1,2,0x50,1 }, /* 360K */
159 { 8,2,0xFF,0x2A,80,1280,1,1,2,0x50,1 }, /* 640K */
160 { 8,3,0xFF,0x35,77,1232,1,0,2,0x74,1 }, /* 1.23M 1024/sec */
162 { 8,3,0xFF,0x35,80,1280,1,0,2,0x74,1 }, /* 1.28M 1024/sec */
163 { 9,3,0xFF,0x35,82,1476,1,0,2,0x47,1 }, /* 1.48M 1024/sec 9sec */
165 { 10,3,0xFF,0x1B,82,1640,1,2,2,0x54,1 }, /* 1.64M in 3mode - Reserve */
168 { 21,2,0xFF,0x04,82,3444,1,FDC_500KBPS,2,0x0C,2 }, /* 1.72M in HD 3.5in */
169 { 18,2,0xFF,0x1B,82,2952,1,FDC_500KBPS,2,0x6C,1 }, /* 1.48M in HD 3.5in */
170 { 18,2,0xFF,0x1B,80,2880,1,FDC_500KBPS,2,0x6C,1 }, /* 1.44M in HD 3.5in */
171 { 15,2,0xFF,0x1B,80,2400,1,FDC_500KBPS,2,0x54,1 }, /* 1.2M in HD 5.25/3.5 */
172 { 10,2,0xFF,0x10,82,1640,1,FDC_250KBPS,2,0x2E,1 }, /* 820K in HD 3.5in */
173 { 10,2,0xFF,0x10,80,1600,1,FDC_250KBPS,2,0x2E,1 }, /* 800K in HD 3.5in */
174 { 9,2,0xFF,0x20,80,1440,1,FDC_250KBPS,2,0x50,1 }, /* 720K in HD 3.5in */
175 { 9,2,0xFF,0x2A,40, 720,1,FDC_250KBPS,2,0x50,1 }, /* 360K in DD 5.25in */
176 { 8,2,0xFF,0x2A,80,1280,1,FDC_250KBPS,2,0x50,1 }, /* 640K in DD 5.25in */
177 { 8,3,0xFF,0x35,77,1232,1,FDC_500KBPS,2,0x74,1 }, /* 1.23M in HD 5.25in */
179 { 18,2,0xFF,0x02,82,2952,1,FDC_500KBPS,2,0x02,2 }, /* 1.48M in HD 5.25in */
180 { 18,2,0xFF,0x02,80,2880,1,FDC_500KBPS,2,0x02,2 }, /* 1.44M in HD 5.25in */
181 { 10,2,0xFF,0x10,82,1640,1,FDC_300KBPS,2,0x2E,1 }, /* 820K in HD 5.25in */
182 { 10,2,0xFF,0x10,80,1600,1,FDC_300KBPS,2,0x2E,1 }, /* 800K in HD 5.25in */
183 { 9,2,0xFF,0x20,80,1440,1,FDC_300KBPS,2,0x50,1 }, /* 720K in HD 5.25in */
184 { 9,2,0xFF,0x23,40, 720,2,FDC_300KBPS,2,0x50,1 }, /* 360K in HD 5.25in */
185 { 8,2,0xFF,0x2A,80,1280,1,FDC_300KBPS,2,0x50,1 }, /* 640K in HD 5.25in */
190 #define DRVS_PER_CTLR 4 /* 4 floppies */
192 #define DRVS_PER_CTLR 2 /* 2 floppies */
195 /***********************************************************************\
196 * Per controller structure. *
197 \***********************************************************************/
198 static devclass_t fdc_devclass;
200 /***********************************************************************\
201 * Per drive structure. *
202 * N per controller (DRVS_PER_CTLR) *
203 \***********************************************************************/
205 struct fdc_data *fdc; /* pointer to controller structure */
206 int fdsu; /* this units number on this controller */
207 int type; /* Drive type (FD_1440...) */
208 struct fd_type *ft; /* pointer to the type descriptor */
210 #define FD_OPEN 0x01 /* it's open */
211 #define FD_ACTIVE 0x02 /* it's active */
212 #define FD_MOTOR 0x04 /* motor should be on */
213 #define FD_MOTOR_WAIT 0x08 /* motor coming up */
216 #define FD_NO_TRACK -2
217 int track; /* where we think the head is */
218 int options; /* user configurable options, see ioctl_fd.h */
219 struct callout_handle toffhandle;
220 struct callout_handle tohandle;
221 struct devstat device_stats;
232 static devclass_t fd_devclass;
235 typedef unsigned int nrd_t;
237 #define P_NRD_ADDRH 0xc24
238 #define P_NRD_ADDRM 0xc22
239 #define P_NRD_ADDRL 0xc20
240 #define P_NRD_CHECK 0xc20
241 #define P_NRD_DATA 0xc26
242 #define P_NRD_LED 0xc36
243 #define B_NRD_CHK 0x80
244 #define B_NRD_LED 0x40
245 #define A_NRD_INFO 0x2
246 #define A_NRD_BASE 0x400
247 #define NRD_STATUS 0x0
248 #define NRD_ST0_HD 0x04
250 static fdu_t nrdu=-1;
252 static nrd_t nrdblkn=0;
253 static nrd_t nrdaddr=0x0;
255 #define nrd_check_ready() ({ \
256 (epson_inb(P_NRD_CHECK) & B_NRD_CHK) ? 0 : 1; \
258 #define nrd_LED_on() epson_outb(P_NRD_LED, B_NRD_LED)
259 #define nrd_LED_off() epson_outb(P_NRD_LED, ~B_NRD_LED)
260 #define nrd_trac() ((int)(nrd_info(nrdaddr) & 0xff))
261 #define nrd_head() ((int)((nrd_info(nrdaddr) >> 8) & 0xff))
262 #define nrd_sec() ((int)(nrd_info(nrdaddr + 2) & 0xff))
263 #define nrd_secsize() ((int)((nrd_info(A_NRD_INFO) >> 8) & 0xff))
264 #define nrd_addrset(p) nrd_addr((nrd_t)((nrd_t)p+A_NRD_BASE))
270 epson_outb(P_NRD_ADDRH, (u_char)((addr >> 16) & 0x1f));
271 epson_outb(P_NRD_ADDRM, (u_char)((addr >> 8) & 0xff));
272 epson_outb(P_NRD_ADDRL, (u_char)(addr & 0xff));
275 static inline u_short
283 tmp = (short)inw(P_NRD_DATA);
285 return ((u_short)tmp);
287 #endif /* EPSON_NRDISK */
289 /***********************************************************************\
290 * Throughout this file the following conventions will be used: *
291 * fd is a pointer to the fd_data struct for the drive in question *
292 * fdc is a pointer to the fdc_data struct for the controller *
293 * fdu is the floppy drive unit number *
294 * fdcu is the floppy controller unit number *
295 * fdsu is the floppy drive unit number on that controller. (sub-unit) *
296 \***********************************************************************/
298 /* needed for ft driver, thus exported */
299 int in_fdc(struct fdc_data *);
300 int out_fdc(struct fdc_data *, int);
302 /* internal functions */
303 static void fdc_intr(void *);
304 static void set_motor(struct fdc_data *, int, int);
307 static timeout_t fd_turnoff;
308 static timeout_t fd_motor_on;
309 static void fd_turnon(struct fd_data *);
310 static void fdc_reset(fdc_p);
311 static int fd_in(struct fdc_data *, int *);
312 static void fdstart(struct fdc_data *);
313 static timeout_t fd_iotimeout;
314 static timeout_t fd_pseudointr;
315 static int fdstate(struct fdc_data *);
316 static int retrier(struct fdc_data *);
317 static int fdformat(dev_t, struct fd_formb *, struct proc *);
319 static int enable_fifo(fdc_p fdc);
321 static int fifo_threshold = 8; /* XXX: should be accessible via sysctl */
327 #define SEEKCOMPLETE 3
329 #define RECALCOMPLETE 5
335 #define IOTIMEDOUT 11
336 #define RESETCOMPLETE 12
340 static char const * const fdstates[] =
358 /* CAUTION: fd_debug causes huge amounts of logging output */
359 static int volatile fd_debug = 0;
360 #define TRACE0(arg) if(fd_debug) printf(arg)
361 #define TRACE1(arg1, arg2) if(fd_debug) printf(arg1, arg2)
362 #else /* FDC_DEBUG */
364 #define TRACE1(arg1, arg2)
365 #endif /* FDC_DEBUG */
368 fdout_wr(fdc_p fdc, u_int8_t v)
370 bus_space_write_1(fdc->portt, fdc->porth, FDOUT+fdc->port_off, v);
376 return bus_space_read_1(fdc->portt, fdc->porth, FDSTS+fdc->port_off);
380 fddata_wr(fdc_p fdc, u_int8_t v)
382 bus_space_write_1(fdc->portt, fdc->porth, FDDATA+fdc->port_off, v);
388 return bus_space_read_1(fdc->portt, fdc->porth, FDDATA+fdc->port_off);
393 fdctl_wr(fdc_p fdc, u_int8_t v)
395 bus_space_write_1(fdc->ctlt, fdc->ctlh, 0, v);
404 return bus_space_read_1(fdc->portt, fdc->porth, FDIN);
411 #include <sys/select.h>
412 #include <sys/module.h>
413 #include <pccard/cardinfo.h>
414 #include <pccard/driver.h>
415 #include <pccard/slot.h>
418 * PC-Card (PCMCIA) specific code.
420 static int yeinit(struct pccard_devinfo *); /* init device */
421 static void yeunload(struct pccard_devinfo *); /* Disable driver */
422 static int yeintr(struct pccard_devinfo *); /* Interrupt handler */
424 PCCARD_MODULE(fdc, yeinit, yeunload, yeintr, 0, bio_imask);
427 * Initialize the device - called from Slot manager.
429 static int yeinit(struct pccard_devinfo *devi)
431 fdc_p fdc = &fdc_data[devi->isahd.id_unit];
433 fdc->baseport = devi->isahd.id_iobase;
439 fdout_wr(fdc, FDO_FRST);
444 if (yeattach(&devi->isahd) == 0)
451 * yeunload - unload the driver and clear the table.
453 * This is usually called when the card is ejected, but
454 * can be caused by a modunload of a controller driver.
455 * The idea is to reset the driver's view of the device
456 * and ensure that any driver entry points such as
457 * read and write do not hang.
459 static void yeunload(struct pccard_devinfo *devi)
461 if (fd_data[devi->isahd.id_unit].type == NO_TYPE)
465 * this prevents Fdopen() and fdstrategy() from attempting
466 * to access unloaded controller
468 fd_data[devi->isahd.id_unit].type = NO_TYPE;
470 printf("fdc%d: unload\n", devi->isahd.id_unit);
474 * yeintr - Shared interrupt called from
475 * front end of PC-Card handler.
477 static int yeintr(struct pccard_devinfo *devi)
479 fdintr((fdcu_t)devi->isahd.id_unit);
482 #endif /* NCARD > 0 */
485 static d_open_t Fdopen; /* NOTE, not fdopen */
486 static d_close_t fdclose;
487 static d_ioctl_t fdioctl;
488 static d_strategy_t fdstrategy;
493 static struct cdevsw fd_cdevsw = {
497 /* write */ physwrite,
501 /* strategy */ fdstrategy,
503 /* maj */ CDEV_MAJOR,
507 /* bmaj */ BDEV_MAJOR
511 fdc_err(struct fdc_data *fdc, const char *s)
515 if (fdc->fdc_errs < FDC_ERRMAX)
516 device_printf(fdc->fdc_dev, "%s", s);
517 else if (fdc->fdc_errs == FDC_ERRMAX)
518 device_printf(fdc->fdc_dev, "too many errors, not "
519 "logging any more\n");
526 * fd_cmd: Send a command to the chip. Takes a varargs with this structure:
528 * # of output bytes, output bytes as ints ...,
529 * # of input bytes, input bytes as ints ...
532 fd_cmd(struct fdc_data *fdc, int n_out, ...)
540 cmd = (u_char)(va_arg(ap, int));
543 for (n = 0; n < n_out; n++)
545 if (out_fdc(fdc, va_arg(ap, int)) < 0)
548 snprintf(msg, sizeof(msg),
549 "cmd %x failed at out byte %d of %d\n",
551 return fdc_err(fdc, msg);
554 n_in = va_arg(ap, int);
555 for (n = 0; n < n_in; n++)
557 int *ptr = va_arg(ap, int *);
558 if (fd_in(fdc, ptr) < 0)
561 snprintf(msg, sizeof(msg),
562 "cmd %02x failed at in byte %d of %d\n",
564 return fdc_err(fdc, msg);
572 enable_fifo(fdc_p fdc)
576 if ((fdc->flags & FDC_HAS_FIFO) == 0) {
580 * Cannot use fd_cmd the normal way here, since
581 * this might be an invalid command. Thus we send the
582 * first byte, and check for an early turn of data directon.
585 if (out_fdc(fdc, I8207X_CONFIGURE) < 0)
586 return fdc_err(fdc, "Enable FIFO failed\n");
588 /* If command is invalid, return */
590 while ((i = fdsts_rd(fdc) & (NE7_DIO | NE7_RQM))
591 != NE7_RQM && j-- > 0)
592 if (i == (NE7_DIO | NE7_RQM)) {
598 0, (fifo_threshold - 1) & 0xf, 0, 0) < 0) {
600 return fdc_err(fdc, "Enable FIFO failed\n");
602 fdc->flags |= FDC_HAS_FIFO;
606 I8207X_CONFIGURE, 0, (fifo_threshold - 1) & 0xf, 0, 0) < 0)
607 return fdc_err(fdc, "Re-enable FIFO failed\n");
612 fd_sense_drive_status(fdc_p fdc, int *st3p)
616 if (fd_cmd(fdc, 2, NE7CMD_SENSED, fdc->fdu, 1, &st3))
618 return fdc_err(fdc, "Sense Drive Status failed\n");
627 fd_sense_int(fdc_p fdc, int *st0p, int *cylp)
632 if (fdc->fdu == nrdu) {
633 if (fdc->fd->track >= 0) nrdaddr = (fdc->fd->track + 1) * 8;
635 *st0p = nrd_head() ? NRD_ST0_HD : NRD_STATUS;
639 #endif /* EPSON_NRDISK */
640 ret = fd_cmd(fdc, 1, NE7CMD_SENSEI, 1, &st0);
643 "sense intr err reading stat reg 0\n");
650 if ((st0 & NE7_ST0_IC) == NE7_ST0_IC_IV) {
652 * There doesn't seem to have been an interrupt.
657 if (fd_in(fdc, &cyl) < 0) {
658 return fdc_err(fdc, "can't get cyl num\n");
666 #endif /* EPSON_NRDISK */
672 fd_read_status(fdc_p fdc, int fdsu)
676 for (i = 0; i < 7; i++) {
678 * XXX types are poorly chosen. Only bytes can by read
679 * from the hardware, but fdc->status[] wants u_ints and
680 * fd_in() gives ints.
685 if (fdc->fdu == nrdu) {
687 case 0: fdc->status[i] = nrd_head()
688 ? NRD_ST0_HD : NRD_STATUS; break;
689 case 1: fdc->status[i] = NRD_STATUS; break;
690 case 2: fdc->status[i] = NRD_STATUS; break;
691 case 3: fdc->status[i] = nrd_trac(); break;
692 case 4: fdc->status[i] = nrd_head(); break;
693 case 5: fdc->status[i] = nrdsec; break;
694 case 6: fdc->status[i] = nrd_secsize(); break;
699 #endif /* EPSON_NRDISK */
700 ret = fd_in(fdc, &status);
701 fdc->status[i] = status;
706 #endif /* EPSON_NRDISK */
710 fdc->flags |= FDC_STAT_VALID;
712 fdc->flags &= ~FDC_STAT_VALID;
717 /****************************************************************************/
718 /* autoconfiguration stuff */
719 /****************************************************************************/
721 static int pc98_trans = 0; /* 0 : HD , 1 : DD , 2 : 1.44 */
722 static int pc98_trans_prev = 0;
724 static void set_density(fdc_p fdc)
726 /* always motor on */
727 outb(IO_FDPORT, (pc98_trans != 1 ? FDP_FDDEXC : 0) | FDP_PORTEXC);
729 fdout_wr(fdc, FDO_RST | FDO_DMAE);
730 /* in the case of note W, always inhibit 100ms timer */
733 static int pc98_fd_check_ready(fdu_t fdu)
735 fd_p fd = devclass_get_softc(fd_devclass, fdu);
736 struct fdc_data *fdc = fd->fdc;
741 if (nrd_check_ready()) return 0;
745 while (retry++ < 30000) {
746 set_motor(fdc, fd->fdsu, TURNON);
747 out_fdc(fdc, NE7CMD_SENSED); /* Sense Drive Status */
749 out_fdc(fdc, fdu); /* Drive number */
751 if ((in_fdc(fdc) & NE7_ST3_RD)){
752 fdout_wr(fdc, FDO_DMAE | FDO_MTON);
762 fdc_alloc_resources(struct fdc_data *fdc)
768 ispnp = fdc->fdc_ispnp;
769 fdc->rid_ioport = fdc->rid_irq = fdc->rid_drq = 0;
770 fdc->res_ioport = fdc->res_irq = fdc->res_drq = 0;
773 fdc->res_ioport = bus_alloc_resource(dev, SYS_RES_IOPORT,
774 &fdc->rid_ioport, 0ul, ~0ul,
775 ispnp ? 1 : IO_FDCSIZE,
779 * We don't just use an 8 port range (e.g. 0x3f0-0x3f7) since that
780 * covers an IDE control register at 0x3f6.
781 * Isn't PC hardware wonderful.
783 fdc->res_ioport = bus_alloc_resource(dev, SYS_RES_IOPORT,
784 &fdc->rid_ioport, 0ul, ~0ul,
785 ispnp ? 1 : 6, RF_ACTIVE);
787 if (fdc->res_ioport == 0) {
788 device_printf(dev, "cannot reserve I/O port range\n");
791 fdc->portt = rman_get_bustag(fdc->res_ioport);
792 fdc->porth = rman_get_bushandle(fdc->res_ioport);
796 * Some BIOSen report the device at 0x3f2-0x3f5,0x3f7 and some at
797 * 0x3f0-0x3f5,0x3f7. We detect the former by checking the size
798 * and adjust the port address accordingly.
800 if (bus_get_resource_count(dev, SYS_RES_IOPORT, 0) == 4)
804 * Register the control port range as rid 1 if it isn't there
805 * already. Most PnP BIOSen will have already done this but
806 * non-PnP configurations don't.
808 * And some (!!) report 0x3f2-0x3f5 and completely leave out the
809 * control register! It seems that some non-antique controller chips
810 * have a different method of programming the transfer speed which
811 * doesn't require the control register, but it's mighty bogus as the
812 * chip still responds to the address for the control register.
814 if (bus_get_resource_count(dev, SYS_RES_IOPORT, 1) == 0) {
817 /* Find the control port, usually 0x3f7 */
818 ctlstart = rman_get_start(fdc->res_ioport) + fdc->port_off + 7;
820 bus_set_resource(dev, SYS_RES_IOPORT, 1, ctlstart, 1);
824 * Now (finally!) allocate the control port.
827 fdc->res_ctl = bus_alloc_resource(dev, SYS_RES_IOPORT, &fdc->rid_ctl,
828 0ul, ~0ul, 1, RF_ACTIVE);
829 if (fdc->res_ctl == 0) {
830 device_printf(dev, "cannot reserve control I/O port range\n");
833 fdc->ctlt = rman_get_bustag(fdc->res_ctl);
834 fdc->ctlh = rman_get_bushandle(fdc->res_ctl);
837 fdc->res_irq = bus_alloc_resource(dev, SYS_RES_IRQ,
838 &fdc->rid_irq, 0ul, ~0ul, 1,
840 if (fdc->res_irq == 0) {
841 device_printf(dev, "cannot reserve interrupt line\n");
844 fdc->res_drq = bus_alloc_resource(dev, SYS_RES_DRQ,
845 &fdc->rid_drq, 0ul, ~0ul, 1,
847 if (fdc->res_drq == 0) {
848 device_printf(dev, "cannot reserve DMA request line\n");
851 fdc->dmachan = fdc->res_drq->r_start;
857 fdc_release_resources(struct fdc_data *fdc)
862 if (fdc->res_irq != 0) {
863 bus_deactivate_resource(dev, SYS_RES_IRQ, fdc->rid_irq,
865 bus_release_resource(dev, SYS_RES_IRQ, fdc->rid_irq,
869 if (fdc->res_ctl != 0) {
870 bus_deactivate_resource(dev, SYS_RES_IOPORT, fdc->rid_ctl,
872 bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ctl,
876 if (fdc->res_ioport != 0) {
877 bus_deactivate_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport,
879 bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport,
882 if (fdc->res_drq != 0) {
883 bus_deactivate_resource(dev, SYS_RES_DRQ, fdc->rid_drq,
885 bus_release_resource(dev, SYS_RES_DRQ, fdc->rid_drq,
890 /****************************************************************************/
891 /* autoconfiguration stuff */
892 /****************************************************************************/
894 static struct isa_pnp_id fdc_ids[] = {
895 {0x0007d041, "PC standard floppy disk controller"}, /* PNP0700 */
896 {0x0107d041, "Standard floppy controller supporting MS Device Bay Spec"}, /* PNP0701 */
901 fdc_read_ivar(device_t dev, device_t child, int which, u_long *result)
903 struct fdc_ivars *ivars = device_get_ivars(child);
906 case FDC_IVAR_FDUNIT:
907 *result = ivars->fdunit;
916 * fdc controller section.
919 fdc_probe(device_t dev)
922 struct fdc_data *fdc;
924 fdc = device_get_softc(dev);
925 bzero(fdc, sizeof *fdc);
929 error = ISA_PNP_PROBE(device_get_parent(dev), dev, fdc_ids);
932 fdc->fdc_ispnp = (error == 0);
934 /* Attempt to allocate our resources for the duration of the probe */
935 error = fdc_alloc_resources(fdc);
940 /* First - lets reset the floppy controller */
943 fdout_wr(fdc, FDO_FRST);
946 /* see if it can handle a command */
948 if (fd_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(4, 240),
949 NE7_SPEC_2(2, 0), 0)) {
954 if (fd_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240),
955 NE7_SPEC_2(2, 0), 0)) {
962 if (fd_cmd(fdc, 1, NE7CMD_VERSION, 1, &ic_type) == 0) {
963 ic_type = (u_char)ic_type;
966 device_set_desc(dev, "NEC 765 or clone");
967 fdc->fdct = FDC_NE765;
970 device_set_desc(dev, "Intel 82077 or clone");
971 fdc->fdct = FDC_I82077;
974 device_set_desc(dev, "NEC 72065B or clone");
975 fdc->fdct = FDC_NE72065;
978 device_set_desc(dev, "generic floppy controller");
979 fdc->fdct = FDC_UNKNOWN;
986 fdc_release_resources(fdc);
991 * Add a child device to the fdc controller. It will then be probed etc.
994 fdc_add_child(device_t dev, const char *name, int unit)
997 struct fdc_ivars *ivar;
1000 ivar = malloc(sizeof *ivar, M_DEVBUF /* XXX */, M_NOWAIT);
1003 bzero(ivar, sizeof *ivar);
1004 if (resource_int_value(name, unit, "drive", &ivar->fdunit) != 0)
1006 child = device_add_child(dev, name, unit);
1009 device_set_ivars(child, ivar);
1010 if (resource_int_value(name, unit, "disabled", &disabled) == 0
1012 device_disable(child);
1016 fdc_attach(device_t dev)
1018 struct fdc_data *fdc;
1021 fdc = device_get_softc(dev);
1022 error = fdc_alloc_resources(fdc);
1024 device_printf(dev, "cannot re-aquire resources\n");
1027 error = BUS_SETUP_INTR(device_get_parent(dev), dev, fdc->res_irq,
1028 INTR_TYPE_BIO, fdc_intr, fdc, &fdc->fdc_intr);
1030 device_printf(dev, "cannot setup interrupt\n");
1033 fdc->fdcu = device_get_unit(dev);
1034 fdc->flags |= FDC_ATTACHED;
1036 /* Acquire the DMA channel forever, The driver will do the rest */
1037 /* XXX should integrate with rman */
1038 isa_dma_acquire(fdc->dmachan);
1039 isa_dmainit(fdc->dmachan, 128 << 3 /* XXX max secsize */);
1040 fdc->state = DEVIDLE;
1043 /* reset controller, turn motor off, clear fdout mirror reg */
1046 /* reset controller, turn motor off, clear fdout mirror reg */
1047 fdout_wr(fdc, ((fdc->fdout = 0)));
1049 bufq_init(&fdc->head);
1052 * Probe and attach any children. We should probably detect
1053 * devices from the BIOS unless overridden.
1055 for (i = resource_query_string(-1, "at", device_get_nameunit(dev));
1057 i = resource_query_string(i, "at", device_get_nameunit(dev)))
1058 fdc_add_child(dev, resource_query_name(i),
1059 resource_query_unit(i));
1061 return (bus_generic_attach(dev));
1065 fdc_print_child(device_t me, device_t child)
1069 retval += bus_print_child_header(me, child);
1070 retval += printf(" on %s drive %d\n", device_get_nameunit(me),
1071 fdc_get_fdunit(child));
1076 static device_method_t fdc_methods[] = {
1077 /* Device interface */
1078 DEVMETHOD(device_probe, fdc_probe),
1079 DEVMETHOD(device_attach, fdc_attach),
1080 DEVMETHOD(device_detach, bus_generic_detach),
1081 DEVMETHOD(device_shutdown, bus_generic_shutdown),
1082 DEVMETHOD(device_suspend, bus_generic_suspend),
1083 DEVMETHOD(device_resume, bus_generic_resume),
1086 DEVMETHOD(bus_print_child, fdc_print_child),
1087 DEVMETHOD(bus_read_ivar, fdc_read_ivar),
1088 /* Our children never use any other bus interface methods. */
1093 static driver_t fdc_driver = {
1096 sizeof(struct fdc_data)
1099 DRIVER_MODULE(fdc, isa, fdc_driver, fdc_devclass, 0, 0);
1101 /******************************************************************/
1103 * devices attached to the controller section.
1106 fd_probe(device_t dev)
1109 u_int fdt, st0, st3;
1111 struct fdc_data *fdc;
1113 static int fd_fifo = 0;
1115 fdsu = *(int *)device_get_ivars(dev); /* xxx cheat a bit... */
1116 fd = device_get_softc(dev);
1117 fdc = device_get_softc(device_get_parent(dev));
1119 bzero(fd, sizeof *fd);
1123 fd->fdu = device_get_unit(dev);
1126 /* look up what bios thinks we have */
1128 case 0: case 1: case 2: case 3:
1129 if ((PC98_SYSTEM_PARAMETER(0x5ae) >> fd->fdu) & 0x01)
1132 else if ((PC98_SYSTEM_PARAMETER(0x55c) >> fd->fdu) & 0x01) {
1134 switch (epson_machine_id) {
1135 case 0x20: case 0x27:
1136 if ((PC98_SYSTEM_PARAMETER(0x488) >> fd->fdu) & 0x01) {
1137 if (nrd_check_ready()) {
1146 #else /* !EPSON_NRDISK */
1147 else if ((PC98_SYSTEM_PARAMETER(0x55c) >> fd->fdu) & 0x01) {
1149 switch (epson_machine_id) {
1150 case 0x20: case 0x27:
1151 if ((PC98_SYSTEM_PARAMETER(0x488) >> fd->fdu) & 0x01)
1155 #endif /* EPSON_NRDISK */
1165 /* look up what bios thinks we have */
1168 if (device_get_flags(fdc->fdc_dev) & FDC_PRETEND_D0)
1169 fdt = RTCFDT_144M | RTCFDT_144M_PRETENDED;
1171 fdt = (rtcin(RTC_FDISKETTE) & 0xf0);
1174 fdt = ((rtcin(RTC_FDISKETTE) << 4) & 0xf0);
1181 fdt = RTCFDT_144M; /* XXX probably */
1185 /* is there a unit? */
1187 if (fdt == FDT_NONE)
1190 if (fdt == RTCFDT_NONE)
1196 set_motor(fdc, fdsu, TURNON);
1197 DELAY(1000000); /* 1 sec */
1199 /* XXX This doesn't work before the first set_motor() */
1200 if (fd_fifo == 0 && fdc->fdct != FDC_NE765 && fdc->fdct != FDC_UNKNOWN
1201 && (device_get_flags(fdc->fdc_dev) & FDC_NO_FIFO) == 0
1202 && enable_fifo(fdc) == 0) {
1203 device_printf(device_get_parent(dev),
1204 "FIFO enabled, %d bytes threshold\n", fifo_threshold);
1208 if ((fd_cmd(fdc, 2, NE7CMD_SENSED, fdsu, 1, &st3) == 0)
1209 && (st3 & NE7_ST3_T0)) {
1210 /* if at track 0, first seek inwards */
1211 /* seek some steps: */
1212 fd_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0);
1213 DELAY(300000); /* ...wait a moment... */
1214 fd_sense_int(fdc, 0, 0); /* make ctrlr happy */
1217 /* If we're at track 0 first seek inwards. */
1218 if ((fd_sense_drive_status(fdc, &st3) == 0) && (st3 & NE7_ST3_T0)) {
1219 /* Seek some steps... */
1220 if (fd_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) {
1221 /* ...wait a moment... */
1223 /* make ctrlr happy: */
1224 fd_sense_int(fdc, 0, 0);
1228 for (i = 0; i < 2; i++) {
1230 * we must recalibrate twice, just in case the
1231 * heads have been beyond cylinder 76, since most
1232 * FDCs still barf when attempting to recalibrate
1233 * more than 77 steps
1236 if (fd_cmd(fdc, 2, NE7CMD_RECAL, fdsu, 0) == 0) {
1237 /* a second being enough for full stroke seek*/
1238 DELAY(i == 0 ? 1000000 : 300000);
1240 /* anything responding? */
1241 if (fd_sense_int(fdc, &st0, 0) == 0 &&
1242 (st0 & NE7_ST0_EC) == 0)
1243 break; /* already probed succesfully */
1247 set_motor(fdc, fdsu, TURNOFF);
1249 if (st0 & NE7_ST0_EC) /* no track 0 -> no drive present */
1253 fd->track = FD_NO_TRACK;
1257 callout_handle_init(&fd->toffhandle);
1258 callout_handle_init(&fd->tohandle);
1263 /* Check 3mode I/F */
1265 outb(0x4be, (fd->fdu << 5) | 0x10);
1266 if (!(inb(0x4be) & 0x01)) {
1267 device_set_desc(dev, "1.44M FDD");
1272 printf("Warning: can't control 3mode I/F, "
1273 "fallback to 2mode.\n"
1278 if (fd->fdu == nrdu) {
1279 device_set_desc(dev, "EPSON RAM DRIVE");
1283 device_set_desc(dev, "1M/640K FDD");
1293 device_set_desc(dev, "1200-KB 5.25\" drive");
1296 case RTCFDT_144M | RTCFDT_144M_PRETENDED:
1297 device_set_desc(dev, "config-pretended 1440-MB 3.5\" drive");
1301 device_set_desc(dev, "1440-KB 3.5\" drive");
1306 device_set_desc(dev, "2880-KB 3.5\" drive (in 1440-KB mode)");
1310 device_set_desc(dev, "360-KB 5.25\" drive");
1314 printf("720-KB 3.5\" drive");
1325 fd_attach(device_t dev)
1335 fd = device_get_softc(dev);
1337 cdevsw_add(&fd_cdevsw); /* XXX */
1338 make_dev(&fd_cdevsw, (fd->fdu << 6),
1339 UID_ROOT, GID_OPERATOR, 0640, "rfd%d", fd->fdu);
1342 /* Other make_dev() go here. */
1346 * Export the drive to the devstat interface.
1348 devstat_add_entry(&fd->device_stats, device_get_name(dev),
1349 device_get_unit(dev), 512, DEVSTAT_NO_ORDERED_TAGS,
1350 DEVSTAT_TYPE_FLOPPY | DEVSTAT_TYPE_IF_OTHER,
1351 DEVSTAT_PRIORITY_FD);
1355 static device_method_t fd_methods[] = {
1356 /* Device interface */
1357 DEVMETHOD(device_probe, fd_probe),
1358 DEVMETHOD(device_attach, fd_attach),
1359 DEVMETHOD(device_detach, bus_generic_detach),
1360 DEVMETHOD(device_shutdown, bus_generic_shutdown),
1361 DEVMETHOD(device_suspend, bus_generic_suspend), /* XXX */
1362 DEVMETHOD(device_resume, bus_generic_resume), /* XXX */
1367 static driver_t fd_driver = {
1370 sizeof(struct fd_data)
1373 DRIVER_MODULE(fd, fdc, fd_driver, fd_devclass, 0, 0);
1375 /******************************************************************/
1379 * this is a subset of fdattach() optimized for the Y-E Data
1380 * PCMCIA floppy drive.
1382 static int yeattach(struct isa_device *dev)
1384 fdcu_t fdcu = dev->id_unit;
1385 fdc_p fdc = fdc_data + fdcu;
1386 fdsu_t fdsu = 0; /* assume 1 drive per YE controller */
1392 * the FDC_NODMA flag is used to to indicate special PIO is used
1395 fdc->flags = FDC_ATTACHED|FDC_NODMA;
1396 fdc->state = DEVIDLE;
1397 /* reset controller, turn motor off, clear fdout mirror reg */
1398 fdout_wr(fdc, ((fdc->fdout = 0)));
1399 bufq_init(&fdc->head);
1401 * assume 2 drives/ "normal" controller
1405 printf("fdu %d >= NFD\n",fdu);
1410 set_motor(fdcu, fdsu, TURNON);
1411 DELAY(1000000); /* 1 sec */
1412 fdc->fdct = FDC_NE765;
1414 if ((fd_cmd(fdcu, 2, NE7CMD_SENSED, fdsu, 1, &st3) == 0) &&
1415 (st3 & NE7_ST3_T0)) {
1416 /* if at track 0, first seek inwards */
1417 /* seek some steps: */
1418 (void)fd_cmd(fdcu, 3, NE7CMD_SEEK, fdsu, 10, 0);
1419 DELAY(300000); /* ...wait a moment... */
1420 (void)fd_sense_int(fdc, 0, 0); /* make ctrlr happy */
1423 /* If we're at track 0 first seek inwards. */
1424 if ((fd_sense_drive_status(fdc, &st3) == 0) && (st3 & NE7_ST3_T0)) {
1425 /* Seek some steps... */
1426 if (fd_cmd(fdcu, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) {
1427 /* ...wait a moment... */
1429 /* make ctrlr happy: */
1430 (void)fd_sense_int(fdc, 0, 0);
1434 for(i = 0; i < 2; i++) {
1436 * we must recalibrate twice, just in case the
1437 * heads have been beyond cylinder 76, since most
1438 * FDCs still barf when attempting to recalibrate
1439 * more than 77 steps
1442 if (fd_cmd(fdcu, 2, NE7CMD_RECAL, fdsu, 0) == 0) {
1443 /* a second being enough for full stroke seek*/
1444 DELAY(i == 0? 1000000: 300000);
1446 /* anything responding? */
1447 if (fd_sense_int(fdc, &st0, 0) == 0 &&
1448 (st0 & NE7_ST0_EC) == 0)
1449 break; /* already probed succesfully */
1453 set_motor(fdcu, fdsu, TURNOFF);
1455 if (st0 & NE7_ST0_EC) /* no track 0 -> no drive present */
1458 fd->track = FD_NO_TRACK;
1462 printf("fdc%d: 1.44MB 3.5in PCMCIA\n", fdcu);
1469 /****************************************************************************/
1470 /* motor control stuff */
1471 /* remember to not deselect the drive we're working on */
1472 /****************************************************************************/
1474 set_motor(struct fdc_data *fdc, int fdsu, int turnon)
1476 int fdout = fdc->fdout;
1477 int needspecify = 0;
1480 outb(IO_FDPORT, (pc98_trans != 1 ? FDP_FDDEXC : 0)|FDP_PORTEXC);
1482 fdout = FDO_DMAE|FDO_MTON;
1485 fdout &= ~FDO_FDSEL;
1486 fdout |= (FDO_MOEN0 << fdsu) + fdsu;
1488 fdout &= ~(FDO_MOEN0 << fdsu);
1491 && (fdout & (FDO_MOEN0+FDO_MOEN1+FDO_MOEN2+FDO_MOEN3)) == 0)
1492 /* gonna turn off the last drive, put FDC to bed */
1493 fdout &= ~ (FDO_FRST|FDO_FDMAEN);
1495 /* make sure controller is selected and specified */
1496 if((fdout & (FDO_FRST|FDO_FDMAEN)) == 0)
1498 fdout |= (FDO_FRST|FDO_FDMAEN);
1502 fdout_wr(fdc, fdout);
1504 TRACE1("[0x%x->FDOUT]", fdout);
1509 * special case: since we have just woken up the FDC
1510 * from its sleep, we silently assume the command will
1511 * be accepted, and do not test for a timeout
1514 (void)fd_cmd(fdc, 3, NE7CMD_SPECIFY,
1515 NE7_SPEC_1(4, 240), NE7_SPEC_2(2, 0),
1518 (void)fd_cmd(fdc, 3, NE7CMD_SPECIFY,
1519 NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0),
1522 if (fdc->flags & FDC_HAS_FIFO)
1523 (void) enable_fifo(fdc);
1528 fd_turnoff(void *xfd)
1533 TRACE1("[fd%d: turnoff]", fd->fdu);
1536 * Don't turn off the motor yet if the drive is active.
1537 * XXX shouldn't even schedule turnoff until drive is inactive
1538 * and nothing is queued on it.
1540 if (fd->fdc->state != DEVIDLE && fd->fdc->fdu == fd->fdu) {
1541 fd->toffhandle = timeout(fd_turnoff, fd, 4 * hz);
1546 fd->flags &= ~FD_MOTOR;
1547 set_motor(fd->fdc, fd->fdsu, TURNOFF);
1552 fd_motor_on(void *xfd)
1558 fd->flags &= ~FD_MOTOR_WAIT;
1559 if((fd->fdc->fd == fd) && (fd->fdc->state == MOTORWAIT))
1569 if(!(fd->flags & FD_MOTOR))
1571 fd->flags |= (FD_MOTOR + FD_MOTOR_WAIT);
1572 set_motor(fd->fdc, fd->fdsu, TURNON);
1573 timeout(fd_motor_on, fd, hz); /* in 1 sec its ok */
1578 fdc_reset(fdc_p fdc)
1580 /* Try a reset, keep motor on */
1583 if (pc98_machine_type & M_EPSON_PC98)
1584 fdout_wr(fdc, 0xe8);
1586 fdout_wr(fdc, 0xd8);
1588 fdout_wr(fdc, 0x18);
1591 fdout_wr(fdc, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
1592 TRACE1("[0x%x->FDOUT]", fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
1594 /* enable FDC, but defer interrupts a moment */
1595 fdout_wr(fdc, fdc->fdout & ~FDO_FDMAEN);
1596 TRACE1("[0x%x->FDOUT]", fdc->fdout & ~FDO_FDMAEN);
1598 fdout_wr(fdc, fdc->fdout);
1599 TRACE1("[0x%x->FDOUT]", fdc->fdout);
1602 /* XXX after a reset, silently believe the FDC will accept commands */
1604 (void)fd_cmd(fdc, 3, NE7CMD_SPECIFY,
1605 NE7_SPEC_1(4, 240), NE7_SPEC_2(2, 0),
1608 (void)fd_cmd(fdc, 3, NE7CMD_SPECIFY,
1609 NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0),
1612 if (fdc->flags & FDC_HAS_FIFO)
1613 (void) enable_fifo(fdc);
1616 /****************************************************************************/
1618 /****************************************************************************/
1620 in_fdc(struct fdc_data *fdc)
1623 while ((i = fdsts_rd(fdc) & (NE7_DIO|NE7_RQM))
1624 != (NE7_DIO|NE7_RQM) && j-- > 0)
1626 return fdc_err(fdc, "ready for output in input\n");
1628 return fdc_err(fdc, bootverbose? "input ready timeout\n": 0);
1631 TRACE1("[FDDATA->0x%x]", (unsigned char)i);
1633 #else /* !FDC_DEBUG */
1634 return fddata_rd(fdc);
1635 #endif /* FDC_DEBUG */
1639 * fd_in: Like in_fdc, but allows you to see if it worked.
1642 fd_in(struct fdc_data *fdc, int *ptr)
1645 while ((i = fdsts_rd(fdc) & (NE7_DIO|NE7_RQM))
1646 != (NE7_DIO|NE7_RQM) && j-- > 0)
1648 return fdc_err(fdc, "ready for output in input\n");
1650 return fdc_err(fdc, bootverbose? "input ready timeout\n": 0);
1653 TRACE1("[FDDATA->0x%x]", (unsigned char)i);
1656 #else /* !FDC_DEBUG */
1661 #endif /* FDC_DEBUG */
1665 out_fdc(struct fdc_data *fdc, int x)
1669 /* Check that the direction bit is set */
1671 while ((fdsts_rd(fdc) & NE7_DIO) && i-- > 0);
1672 if (i <= 0) return fdc_err(fdc, "direction bit not set\n");
1674 /* Check that the floppy controller is ready for a command */
1676 while ((fdsts_rd(fdc) & NE7_RQM) == 0 && i-- > 0);
1678 return fdc_err(fdc, bootverbose? "output ready timeout\n": 0);
1680 /* Send the command and return */
1682 TRACE1("[0x%x->FDDATA]", x);
1686 /****************************************************************************/
1687 /* fdopen/fdclose */
1688 /****************************************************************************/
1690 Fdopen(dev_t dev, int flags, int mode, struct proc *p)
1692 fdu_t fdu = FDUNIT(minor(dev));
1693 int type = FDTYPE(minor(dev));
1698 if ((fd = devclass_get_softc(fd_devclass, fdu)) == 0)
1701 if ((fdc == NULL) || (fd->type == NO_TYPE))
1707 type = FD_1200; /* XXX backward compatibility */
1709 ; /* XXX any types are OK for PC-98 */
1711 if (pc98_fd_check_ready(fdu) == -1)
1718 * For each type of basic drive, make sure we are trying
1719 * to open a type it can do,
1721 if (type != fd->type) {
1735 type = FD_1480in5_25;
1738 type = FD_1440in5_25;
1743 type = FD_820in5_25;
1746 type = FD_800in5_25;
1749 type = FD_720in5_25;
1752 type = FD_640in5_25;
1755 type = FD_360in5_25;
1762 if ( type != FD_1720
1776 fd->ft = fd_types + type - 1;
1777 fd->flags |= FD_OPEN;
1778 device_busy(fd->dev);
1779 device_busy(fd->fdc->fdc_dev);
1784 fdclose(dev_t dev, int flags, int mode, struct proc *p)
1786 fdu_t fdu = FDUNIT(minor(dev));
1789 fd = devclass_get_softc(fd_devclass, fdu);
1790 fd->flags &= ~FD_OPEN;
1791 fd->options &= ~FDOPT_NORETRY;
1796 /****************************************************************************/
1798 /****************************************************************************/
1800 fdstrategy(struct buf *bp)
1802 unsigned nblocks, blknum, cando;
1809 fdu = FDUNIT(minor(bp->b_dev));
1810 fd = devclass_get_softc(fd_devclass, fdu);
1812 panic("fdstrategy: buf for nonexistent device (%#lx, %#lx)",
1813 (u_long)major(bp->b_dev), (u_long)minor(bp->b_dev));
1815 if (fd->type == NO_TYPE) {
1816 bp->b_error = ENXIO;
1817 bp->b_flags |= B_ERROR;
1821 fdblk = 128 << (fd->ft->secsize);
1822 if (!(bp->b_flags & B_FORMAT)) {
1823 if (bp->b_blkno < 0) {
1825 "fd%d: fdstrat: bad request blkno = %lu, bcount = %ld\n",
1826 fdu, (u_long)bp->b_blkno, bp->b_bcount);
1827 bp->b_error = EINVAL;
1828 bp->b_flags |= B_ERROR;
1831 if ((bp->b_bcount % fdblk) != 0) {
1832 bp->b_error = EINVAL;
1833 bp->b_flags |= B_ERROR;
1839 * Set up block calculations.
1841 if (bp->b_blkno > 20000000) {
1843 * Reject unreasonably high block number, prevent the
1844 * multiplication below from overflowing.
1846 bp->b_error = EINVAL;
1847 bp->b_flags |= B_ERROR;
1850 blknum = (unsigned) bp->b_blkno * DEV_BSIZE/fdblk;
1851 nblocks = fd->ft->size;
1853 if (blknum + (bp->b_bcount / fdblk) > nblocks) {
1854 if (blknum <= nblocks) {
1855 cando = (nblocks - blknum) * fdblk;
1856 bp->b_resid = bp->b_bcount - cando;
1858 goto bad; /* not actually bad but EOF */
1860 bp->b_error = EINVAL;
1861 bp->b_flags |= B_ERROR;
1865 bp->b_pblkno = bp->b_blkno;
1867 bufqdisksort(&fdc->head, bp);
1868 untimeout(fd_turnoff, fd, fd->toffhandle); /* a good idea */
1870 /* Tell devstat we are starting on the transaction */
1871 devstat_start_transaction(&fd->device_stats);
1881 /***************************************************************\
1883 * We have just queued something.. if the controller is not busy *
1884 * then simulate the case where it has just finished a command *
1885 * So that it (the interrupt routine) looks on the queue for more*
1886 * work to do and picks up what we just added. *
1887 * If the controller is already busy, we need do nothing, as it *
1888 * will pick up our work when the present work completes *
1889 \***************************************************************/
1891 fdstart(struct fdc_data *fdc)
1896 if(fdc->state == DEVIDLE)
1904 fd_iotimeout(void *xfdc)
1910 TRACE1("fd%d[fd_iotimeout()]", fdc->fdu);
1913 * Due to IBM's brain-dead design, the FDC has a faked ready
1914 * signal, hardwired to ready == true. Thus, any command
1915 * issued if there's no diskette in the drive will _never_
1916 * complete, and must be aborted by resetting the FDC.
1917 * Many thanks, Big Blue!
1918 * The FDC must not be reset directly, since that would
1919 * interfere with the state machine. Instead, pretend that
1920 * the command completed but was invalid. The state machine
1921 * will reset the FDC and retry once.
1924 fdc->status[0] = NE7_ST0_IC_IV;
1925 fdc->flags &= ~FDC_STAT_VALID;
1926 fdc->state = IOTIMEDOUT;
1931 /* just ensure it has the right spl */
1933 fd_pseudointr(void *xfdc)
1942 /***********************************************************************\
1944 * keep calling the state machine until it returns a 0 *
1945 * ALWAYS called at SPLBIO *
1946 \***********************************************************************/
1948 fdc_intr(void *xfdc)
1956 * magic pseudo-DMA initialization for YE FDC. Sets count and
1959 #define SET_BCDR(fdc,wr,cnt,port) \
1960 bus_space_write_1(fdc->portt, fdc->porth, fdc->port_off + port, \
1961 ((cnt)-1) & 0xff); \
1962 bus_space_write_1(fdc->portt, fdc->porth, fdc->port_off + port + 1, \
1963 ((wr ? 0x80 : 0) | ((((cnt)-1) >> 8) & 0x7f)));
1966 * fdcpio(): perform programmed IO read/write for YE PCMCIA floppy
1968 static int fdcpio(fdc_p fdc, long flags, caddr_t addr, u_int count)
1970 u_char *cptr = (u_char *)addr;
1972 if (flags & B_READ) {
1973 if (fdc->state != PIOREAD) {
1974 fdc->state = PIOREAD;
1977 SET_BCDR(fdc, 0, count, 0);
1978 bus_space_read_multi_1(fdc->portt, fdc->porth, fdc->port_off +
1979 FDC_YE_DATAPORT, cptr, count);
1981 bus_space_write_multi_1(fdc->portt, fdc->porth, fdc->port_off +
1982 FDC_YE_DATAPORT, cptr, count);
1983 SET_BCDR(fdc, 0, count, 0);
1988 /***********************************************************************\
1989 * The controller state machine. *
1990 * if it returns a non zero value, it should be called again immediatly *
1991 \***********************************************************************/
1995 int read, format, head, i, sec = 0, sectrac, st0, cyl, st3;
1996 unsigned blknum = 0, b_cylinder = 0;
1997 fdu_t fdu = fdc->fdu;
1999 register struct buf *bp;
2000 struct fd_formb *finfo = NULL;
2005 bp = bufq_first(&fdc->head);
2007 bufq_remove(&fdc->head, bp);
2012 /***********************************************\
2013 * nothing left for this controller to do *
2014 * Force into the IDLE state, *
2015 \***********************************************/
2016 fdc->state = DEVIDLE;
2018 device_printf(fdc->fdc_dev,
2019 "unexpected valid fd pointer\n");
2023 TRACE1("[fdc%d IDLE]", fdc->fdcu);
2026 fdu = FDUNIT(minor(bp->b_dev));
2027 fd = devclass_get_softc(fd_devclass, fdu);
2028 fdblk = 128 << fd->ft->secsize;
2029 if (fdc->fd && (fd != fdc->fd))
2030 device_printf(fd->dev, "confused fd pointers\n");
2031 read = bp->b_flags & B_READ;
2032 format = bp->b_flags & B_FORMAT;
2034 finfo = (struct fd_formb *)bp->b_data;
2035 fd->skip = (char *)&(finfo->fd_formb_cylno(0))
2038 if (fdc->state == DOSEEK || fdc->state == SEEKCOMPLETE) {
2039 blknum = (unsigned) bp->b_pblkno * DEV_BSIZE/fdblk +
2041 b_cylinder = blknum / (fd->ft->sectrac * fd->ft->heads);
2043 TRACE1("fd%d", fdu);
2044 TRACE1("[%s]", fdstates[fdc->state]);
2045 TRACE1("(0x%x)", fd->flags);
2046 untimeout(fd_turnoff, fd, fd->toffhandle);
2047 fd->toffhandle = timeout(fd_turnoff, fd, 4 * hz);
2051 case FINDWORK: /* we have found new work */
2057 pc98_trans = fd->ft->trans;
2058 if (pc98_trans_prev != pc98_trans) {
2061 for (i = 0; i < 10; i++) {
2065 pc98_trans_prev = pc98_trans;
2067 if (pc98_trans != fd->pc98_trans) {
2068 if (fd->type == FD_1440) {
2069 outb(0x4be, (fdu << 5) | 0x10 | (pc98_trans >> 1));
2073 fd->pc98_trans = pc98_trans;
2076 fdctl_wr(fdc, fd->ft->trans);
2078 TRACE1("[0x%x->FDCTL]", fd->ft->trans);
2079 /*******************************************************\
2080 * If the next drive has a motor startup pending, then *
2081 * it will start up in its own good time *
2082 \*******************************************************/
2083 if(fd->flags & FD_MOTOR_WAIT) {
2084 fdc->state = MOTORWAIT;
2085 return (0); /* come back later */
2087 /*******************************************************\
2088 * Maybe if it's not starting, it SHOULD be starting *
2089 \*******************************************************/
2092 if (!(fd->flags & FD_MOTOR))
2094 fdc->state = MOTORWAIT;
2098 else /* at least make sure we are selected */
2100 set_motor(fdcu, fd->fdsu, TURNON);
2103 #else /* !EPSON_NRDISK */
2104 if (!(fd->flags & FD_MOTOR))
2106 fdc->state = MOTORWAIT;
2110 else /* at least make sure we are selected */
2112 set_motor(fdc, fd->fdsu, TURNON);
2115 if (fdc->flags & FDC_NEEDS_RESET) {
2116 fdc->state = RESETCTLR;
2117 fdc->flags &= ~FDC_NEEDS_RESET;
2119 fdc->state = DOSEEK;
2122 if (b_cylinder == (unsigned)fd->track)
2124 fdc->state = SEEKCOMPLETE;
2128 pc98_fd_check_ready(fdu);
2130 if (fd_cmd(fdc, 3, NE7CMD_SEEK,
2131 fd->fdsu, b_cylinder * fd->ft->steptrac,
2135 * seek command not accepted, looks like
2136 * the FDC went off to the Saints...
2138 fdc->retry = 6; /* try a reset */
2139 return(retrier(fdc));
2141 fd->track = FD_NO_TRACK;
2142 fdc->state = SEEKWAIT;
2143 return(0); /* will return later */
2145 /* allow heads to settle */
2146 timeout(fd_pseudointr, fdc, hz / 16);
2147 fdc->state = SEEKCOMPLETE;
2148 return(0); /* will return later */
2149 case SEEKCOMPLETE : /* SEEK DONE, START DMA */
2150 /* Make sure seek really happened*/
2151 if(fd->track == FD_NO_TRACK) {
2152 int descyl = b_cylinder * fd->ft->steptrac;
2155 * This might be a "ready changed" interrupt,
2156 * which cannot really happen since the
2157 * RDY pin is hardwired to + 5 volts. This
2158 * generally indicates a "bouncing" intr
2159 * line, so do one of the following:
2161 * When running on an enhanced FDC that is
2162 * known to not go stuck after responding
2163 * with INVALID, fetch all interrupt states
2164 * until seeing either an INVALID or a
2165 * real interrupt condition.
2167 * When running on a dumb old NE765, give
2168 * up immediately. The controller will
2169 * provide up to four dummy RC interrupt
2170 * conditions right after reset (for the
2171 * corresponding four drives), so this is
2172 * our only chance to get notice that it
2173 * was not the FDC that caused the interrupt.
2175 if (fd_sense_int(fdc, &st0, &cyl)
2178 if(fdc->fdct == FDC_NE765
2179 && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
2180 return 0; /* hope for a real intr */
2181 } while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
2186 * seek to cyl 0 requested; make sure we are
2189 if (fd_sense_drive_status(fdc, &st3))
2192 if (fdu == nrdu) st3 = NE7_ST3_T0;
2193 #endif /* EPSON_NRDISK */
2194 if ((st3 & NE7_ST3_T0) == 0) {
2196 "fd%d: Seek to cyl 0, but not really there (ST3 = %b)\n",
2197 fdu, st3, NE7_ST3BITS);
2204 return (retrier(fdc));
2208 if (fdu == nrdu) cyl = descyl;
2211 if (cyl != descyl) {
2213 "fd%d: Seek to cyl %d failed; am at cyl %d (ST0 = 0x%x)\n",
2214 fdu, descyl, cyl, st0);
2217 return (retrier(fdc));
2221 fd->track = b_cylinder;
2224 #endif /* EPSON_NRDISK */
2225 if (!(fdc->flags & FDC_NODMA))
2226 isa_dmastart(bp->b_flags, bp->b_data+fd->skip,
2227 format ? bp->b_bcount : fdblk, fdc->dmachan);
2228 sectrac = fd->ft->sectrac;
2229 sec = blknum % (sectrac * fd->ft->heads);
2230 head = sec / sectrac;
2231 sec = sec % sectrac + 1;
2232 fd->hddrv = ((head&1)<<2)+fdu;
2236 /* make sure the drive is writable */
2237 if(fd_sense_drive_status(fdc, &st3) != 0)
2239 /* stuck controller? */
2240 isa_dmadone(bp->b_flags, bp->b_data + fd->skip,
2241 format ? bp->b_bcount : fdblk,
2243 fdc->retry = 6; /* reset the beast */
2244 return (retrier(fdc));
2246 if(st3 & NE7_ST3_WP)
2249 * XXX YES! this is ugly.
2250 * in order to force the current operation
2251 * to fail, we will have to fake an FDC
2252 * error - all error handling is done
2255 fdc->status[0] = NE7_ST0_IC_AT;
2256 fdc->status[1] = NE7_ST1_NW;
2258 fdc->status[3] = fd->track;
2259 fdc->status[4] = head;
2260 fdc->status[5] = sec;
2261 fdc->retry = 8; /* break out immediately */
2262 fdc->state = IOTIMEDOUT; /* not really... */
2268 if (fdc->flags & FDC_NODMA)
2269 (void)fdcpio(fdc,bp->b_flags,
2270 bp->b_data+fd->skip,
2273 if(fd_cmd(fdc, 6, NE7CMD_FORMAT, head << 2 | fdu,
2274 finfo->fd_formb_secshift,
2275 finfo->fd_formb_nsecs,
2276 finfo->fd_formb_gaplen,
2277 finfo->fd_formb_fillbyte, 0)) {
2278 /* controller fell over */
2279 isa_dmadone(bp->b_flags, bp->b_data + fd->skip,
2280 format ? bp->b_bcount : fdblk,
2283 return (retrier(fdc));
2286 if (fdc->flags & FDC_NODMA) {
2288 * this seems to be necessary even when
2291 SET_BCDR(fdc, 1, fdblk, 0);
2294 * perform the write pseudo-DMA before
2295 * the WRITE command is sent
2298 (void)fdcpio(fdc,bp->b_flags,
2299 bp->b_data+fd->skip,
2303 (read ? NE7CMD_READ : NE7CMD_WRITE),
2304 head << 2 | fdu, /* head & unit */
2305 fd->track, /* track */
2307 sec, /* sector + 1 */
2308 fd->ft->secsize, /* sector size */
2309 sectrac, /* sectors/track */
2310 fd->ft->gap, /* gap size */
2311 fd->ft->datalen, /* data length */
2313 /* the beast is sleeping again */
2314 isa_dmadone(bp->b_flags, bp->b_data + fd->skip,
2315 format ? bp->b_bcount : fdblk,
2318 return (retrier(fdc));
2321 if (fdc->flags & FDC_NODMA)
2323 * if this is a read, then simply await interrupt
2324 * before performing PIO
2326 if (read && !fdcpio(fdc,bp->b_flags,
2327 bp->b_data+fd->skip,fdblk)) {
2328 fd->tohandle = timeout(fd_iotimeout, fdc, hz);
2329 return(0); /* will return later */
2333 * write (or format) operation will fall through and
2334 * await completion interrupt
2336 fdc->state = IOCOMPLETE;
2337 fd->tohandle = timeout(fd_iotimeout, fdc, hz);
2338 return (0); /* will return later */
2342 nrdblkn = (nrd_t)((unsigned long)bp->b_blkno*DEV_BSIZE/fdblk
2345 nrd_addrset(fdblk * nrdblkn);
2346 while (!nrd_check_ready()) DELAY(1);
2347 if (read) epson_insw(P_NRD_DATA,
2348 bp->b_data + fd->skip,
2349 fdblk / sizeof(short));
2350 else epson_outsw(P_NRD_DATA,
2351 bp->b_data + fd->skip,
2352 (format ? bp->b_bcount : fdblk)
2355 blknum = (unsigned long)bp->b_blkno*DEV_BSIZE/fdblk
2357 sectrac = fd->ft->sectrac;
2358 sec = blknum % (sectrac * fd->ft->heads);
2359 head = sec / sectrac;
2360 sec = sec % sectrac + 1;
2361 fd->hddrv = ((head&1)<<2)+fdu;
2363 if (nrdsec++ >= nrd_sec())
2364 nrdaddr = (nrd_t)(fd->track * 8 + head * 4);
2366 fdc->state = IOCOMPLETE;
2371 * actually perform the PIO read. The IOCOMPLETE case
2372 * removes the timeout for us.
2374 (void)fdcpio(fdc,bp->b_flags,bp->b_data+fd->skip,fdblk);
2375 fdc->state = IOCOMPLETE;
2377 case IOCOMPLETE: /* IO DONE, post-analyze */
2380 untimeout(fd_iotimeout, fdc, fd->tohandle);
2382 untimeout(fd_iotimeout, fdc, fd->tohandle);
2385 if (fd_read_status(fdc, fd->fdsu)) {
2386 isa_dmadone(bp->b_flags, bp->b_data + fd->skip,
2387 format ? bp->b_bcount : fdblk,
2390 fdc->retry = 6; /* force a reset */
2391 return (retrier(fdc));
2394 fdc->state = IOTIMEDOUT;
2401 #endif /* EPSON_NRDISK */
2402 if (!(fdc->flags & FDC_NODMA))
2403 isa_dmadone(bp->b_flags, bp->b_data + fd->skip,
2404 format ? bp->b_bcount : fdblk, fdc->dmachan);
2408 #endif /* EPSON_NRDISK */
2409 if (fdc->status[0] & NE7_ST0_IC) {
2410 if ((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
2411 && fdc->status[1] & NE7_ST1_OR) {
2413 * DMA overrun. Someone hogged the bus
2414 * and didn't release it in time for the
2415 * next FDC transfer.
2416 * Just restart it, don't increment retry
2419 fdc->state = SEEKCOMPLETE;
2422 else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_IV
2424 fdc->retry = 6; /* force a reset */
2425 else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
2426 && fdc->status[2] & NE7_ST2_WC
2428 fdc->retry = 3; /* force recalibrate */
2429 return (retrier(fdc));
2433 if (!format && fd->skip < bp->b_bcount - bp->b_resid) {
2434 /* set up next transfer */
2435 fdc->state = DOSEEK;
2440 devstat_end_transaction_buf(&fd->device_stats, bp);
2444 fdc->state = FINDWORK;
2450 fdc->state = RESETCOMPLETE;
2454 * Discard all the results from the reset so that they
2455 * can't cause an unexpected interrupt later.
2457 for (i = 0; i < 4; i++)
2458 (void)fd_sense_int(fdc, &st0, &cyl);
2459 fdc->state = STARTRECAL;
2463 pc98_fd_check_ready(fdu);
2465 if(fd_cmd(fdc, 2, NE7CMD_RECAL, fdu, 0)) {
2468 return (retrier(fdc));
2470 fdc->state = RECALWAIT;
2471 return (0); /* will return later */
2473 /* allow heads to settle */
2474 timeout(fd_pseudointr, fdc, hz / 8);
2475 fdc->state = RECALCOMPLETE;
2476 return (0); /* will return later */
2480 * See SEEKCOMPLETE for a comment on this:
2482 if (fd_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
2484 if(fdc->fdct == FDC_NE765
2485 && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
2486 return 0; /* hope for a real intr */
2487 } while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
2490 st0 = NE7_ST0_IC_NT;
2494 if ((st0 & NE7_ST0_IC) != NE7_ST0_IC_NT || cyl != 0)
2498 * a recalibrate from beyond cylinder 77
2499 * will "fail" due to the FDC limitations;
2500 * since people used to complain much about
2501 * the failure message, try not logging
2502 * this one if it seems to be the first
2505 printf("fd%d: recal failed ST0 %b cyl %d\n",
2506 fdu, st0, NE7_ST0BITS, cyl);
2507 if(fdc->retry < 3) fdc->retry = 3;
2508 return (retrier(fdc));
2511 /* Seek (probably) necessary */
2512 fdc->state = DOSEEK;
2513 return (1); /* will return immediatly */
2515 if(fd->flags & FD_MOTOR_WAIT)
2517 return (0); /* time's not up yet */
2519 if (fdc->flags & FDC_NEEDS_RESET) {
2520 fdc->state = RESETCTLR;
2521 fdc->flags &= ~FDC_NEEDS_RESET;
2524 * If all motors were off, then the controller was
2525 * reset, so it has lost track of the current
2526 * cylinder. Recalibrate to handle this case.
2527 * But first, discard the results of the reset.
2529 fdc->state = RESETCOMPLETE;
2531 return (1); /* will return immediatly */
2533 device_printf(fdc->fdc_dev, "unexpected FD int->");
2534 if (fd_read_status(fdc, fd->fdsu) == 0)
2535 printf("FDC status :%x %x %x %x %x %x %x ",
2544 printf("No status available ");
2545 if (fd_sense_int(fdc, &st0, &cyl) != 0)
2547 printf("[controller is dead now]\n");
2550 printf("ST0 = %x, PCN = %x\n", st0, cyl);
2553 /*XXX confusing: some branches return immediately, others end up here*/
2554 return (1); /* Come back immediatly to new state */
2558 retrier(struct fdc_data *fdc)
2560 register struct buf *bp;
2566 /* XXX shouldn't this be cached somewhere? */
2567 fdu = FDUNIT(minor(bp->b_dev));
2568 fd = devclass_get_softc(fd_devclass, fdu);
2569 if (fd->options & FDOPT_NORETRY)
2572 switch (fdc->retry) {
2573 case 0: case 1: case 2:
2574 fdc->state = SEEKCOMPLETE;
2576 case 3: case 4: case 5:
2577 fdc->state = STARTRECAL;
2580 fdc->state = RESETCTLR;
2587 dev_t sav_b_dev = bp->b_dev;
2589 bp->b_dev = makedev(major(bp->b_dev),
2590 (FDUNIT(minor(bp->b_dev))<<3)|RAW_PART);
2591 diskerr(bp, "hard error", LOG_PRINTF,
2592 fdc->fd->skip / DEV_BSIZE,
2593 (struct disklabel *)NULL);
2594 bp->b_dev = sav_b_dev;
2595 if (fdc->flags & FDC_STAT_VALID)
2598 " (ST0 %b ST1 %b ST2 %b cyl %u hd %u sec %u)\n",
2599 fdc->status[0], NE7_ST0BITS,
2600 fdc->status[1], NE7_ST1BITS,
2601 fdc->status[2], NE7_ST2BITS,
2602 fdc->status[3], fdc->status[4],
2606 printf(" (No status)\n");
2608 bp->b_flags |= B_ERROR;
2610 bp->b_resid += bp->b_bcount - fdc->fd->skip;
2613 devstat_end_transaction_buf(&fdc->fd->device_stats, bp);
2615 fdc->state = FINDWORK;
2616 fdc->flags |= FDC_NEEDS_RESET;
2626 fdformat(dev, finfo, p)
2628 struct fd_formb *finfo;
2638 fdu = FDUNIT(minor(dev));
2639 fd = devclass_get_softc(fd_devclass, fdu);
2640 fdblk = 128 << fd->ft->secsize;
2642 /* set up a buffer header for fdstrategy() */
2643 bp = (struct buf *)malloc(sizeof(struct buf), M_TEMP, M_NOWAIT);
2647 * keep the process from being swapped
2650 bzero((void *)bp, sizeof(struct buf));
2652 BUF_LOCK(bp, LK_EXCLUSIVE);
2653 bp->b_flags = B_PHYS | B_FORMAT;
2656 * calculate a fake blkno, so fdstrategy() would initiate a
2657 * seek to the requested cylinder
2659 bp->b_blkno = (finfo->cyl * (fd->ft->sectrac * fd->ft->heads)
2660 + finfo->head * fd->ft->sectrac) * fdblk / DEV_BSIZE;
2662 bp->b_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs;
2663 bp->b_data = (caddr_t)finfo;
2665 /* now do the format */
2667 BUF_STRATEGY(bp, 0);
2669 /* ...and wait for it to complete */
2671 while(!(bp->b_flags & B_DONE)) {
2672 rv = tsleep((caddr_t)bp, PRIBIO, "fdform", 20 * hz);
2673 if (rv == EWOULDBLOCK)
2678 if (rv == EWOULDBLOCK) {
2683 if (bp->b_flags & B_ERROR)
2686 * allow the process to be swapped
2696 * TODO: don't allocate buffer on stack.
2700 fdioctl(dev, cmd, addr, flag, p)
2707 fdu_t fdu = FDUNIT(minor(dev));
2708 fd_p fd = devclass_get_softc(fd_devclass, fdu);
2711 struct fd_type *fdt;
2712 struct disklabel *dl;
2713 char buffer[DEV_BSIZE];
2716 fdblk = 128 << fd->ft->secsize;
2719 pc98_fd_check_ready(fdu);
2723 bzero(buffer, sizeof (buffer));
2724 dl = (struct disklabel *)buffer;
2725 dl->d_secsize = fdblk;
2727 dl->d_secpercyl = fdt->size / fdt->tracks;
2728 dl->d_type = DTYPE_FLOPPY;
2730 if (readdisklabel(dkmodpart(dev, RAW_PART), dl)
2736 *(struct disklabel *)addr = *dl;
2740 if ((flag & FWRITE) == 0)
2745 if ((flag & FWRITE) == 0)
2750 if ((flag & FWRITE) == 0) {
2755 dl = (struct disklabel *)addr;
2757 if ((error = setdisklabel((struct disklabel *)buffer, dl,
2761 error = writedisklabel(dev, (struct disklabel *)buffer);
2764 if ((flag & FWRITE) == 0)
2765 error = EBADF; /* must be opened for writing */
2766 else if (((struct fd_formb *)addr)->format_version !=
2768 error = EINVAL; /* wrong version of formatting prog */
2770 error = fdformat(dev, (struct fd_formb *)addr, p);
2773 case FD_GTYPE: /* get drive type */
2774 *(struct fd_type *)addr = *fd->ft;
2777 case FD_STYPE: /* set drive type */
2778 /* this is considered harmful; only allow for superuser */
2781 *fd->ft = *(struct fd_type *)addr;
2784 case FD_GOPTS: /* get drive options */
2785 *(int *)addr = fd->options;
2788 case FD_SOPTS: /* set drive options */
2789 fd->options = *(int *)addr;
2800 * Hello emacs, these are the
2803 * c-continued-statement-offset: 8
2804 * c-continued-brace-offset: 0
2805 * c-brace-offset: -8
2806 * c-brace-imaginary-offset: 0
2807 * c-argdecl-indent: 8
2808 * c-label-offset: -8
2809 * c++-hanging-braces: 1
2810 * c++-access-specifier-offset: -8
2811 * c++-empty-arglist-indent: 8
2812 * c++-friend-offset: 0