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 * Copyright (c) 2001 Joerg Wunsch,
22 * joerg_wunsch@uriah.heep.sax.de (Joerg Wunsch)
24 * Redistribution and use in source and binary forms, with or without
25 * modification, are permitted provided that the following conditions
27 * 1. Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the following disclaimer.
29 * 2. Redistributions in binary form must reproduce the above copyright
30 * notice, this list of conditions and the following disclaimer in the
31 * documentation and/or other materials provided with the distribution.
32 * 3. All advertising materials mentioning features or use of this software
33 * must display the following acknowledgement:
34 * This product includes software developed by the University of
35 * California, Berkeley and its contributors.
36 * 4. Neither the name of the University nor the names of its contributors
37 * may be used to endorse or promote products derived from this software
38 * without specific prior written permission.
40 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * from: @(#)fd.c 7.4 (Berkeley) 5/25/91
59 #include <sys/param.h>
60 #include <sys/systm.h>
64 #include <sys/devicestat.h>
65 #include <sys/disklabel.h>
66 #include <sys/fcntl.h>
67 #include <sys/fdcio.h>
68 #include <sys/filio.h>
69 #include <sys/kernel.h>
71 #include <sys/malloc.h>
72 #include <sys/module.h>
73 #include <sys/mutex.h>
75 #include <sys/syslog.h>
77 #include <machine/bus.h>
80 #include <machine/clock.h>
81 #include <machine/resource.h>
82 #include <machine/stdarg.h>
84 #include <isa/isavar.h>
85 #include <isa/isareg.h>
86 #include <isa/fdreg.h>
91 FDC_NE765, FDC_ENHANCED, FDC_UNKNOWN = -1
112 static char const * const fdstates[] = {
131 * Per controller structure (softc).
135 int fdcu; /* our unit number */
138 #define FDC_ATTACHED 0x01
139 #define FDC_STAT_VALID 0x08
140 #define FDC_HAS_FIFO 0x10
141 #define FDC_NEEDS_RESET 0x20
142 #define FDC_NODMA 0x40
143 #define FDC_ISPNP 0x80
144 #define FDC_ISPCMCIA 0x100
146 int fdu; /* the active drive */
147 enum fdc_states state;
149 int fdout; /* mirror of the w/o digital output reg */
150 u_int status[7]; /* copy of the registers */
151 enum fdc_type fdct; /* chip version of FDC */
152 int fdc_errs; /* number of logged errors */
153 int dma_overruns; /* number of DMA overruns */
154 struct bio_queue_head head;
155 struct bio *bp; /* active buffer */
156 struct resource *res_ioport, *res_ctl, *res_irq, *res_drq;
157 int rid_ioport, rid_ctl, rid_irq, rid_drq;
159 bus_space_tag_t portt;
160 bus_space_handle_t porth;
161 bus_space_tag_t ctlt;
162 bus_space_handle_t ctlh;
164 struct device *fdc_dev;
165 void (*fdctl_wr)(struct fdc_data *fdc, u_int8_t v);
171 typedef struct fd_data *fd_p;
172 typedef struct fdc_data *fdc_p;
173 typedef enum fdc_type fdc_t;
175 #define FDUNIT(s) (((s) >> 6) & 3)
176 #define FDNUMTOUNIT(n) (((n) & 3) << 6)
177 #define FDTYPE(s) ((s) & 0x3f)
180 * fdc maintains a set (1!) of ivars per child of each controller.
182 enum fdc_device_ivars {
187 * Simple access macros for the ivars.
189 #define FDC_ACCESSOR(A, B, T) \
190 static __inline T fdc_get_ ## A(device_t dev) \
193 BUS_READ_IVAR(device_get_parent(dev), dev, FDC_IVAR_ ## B, &v); \
196 FDC_ACCESSOR(fdunit, FDUNIT, int)
198 /* configuration flags for fdc */
199 #define FDC_NO_FIFO (1 << 2) /* do not enable FIFO */
201 /* error returns for fd_cmd() */
203 #define FD_NOT_VALID -2
204 #define FDC_ERRMAX 100 /* do not log more */
206 * Stop retrying after this many DMA overruns. Since each retry takes
207 * one revolution, with 300 rpm., 25 retries take approximately 5
208 * seconds which the read attempt will block in case the DMA overrun
211 #define FDC_DMAOV_MAX 25
214 * Number of subdevices that can be used for different density types.
215 * By now, the lower 6 bit of the minor number are reserved for this,
216 * allowing for up to 64 subdevices, but we only use 16 out of this.
217 * Density #0 is used for automatic format detection, the other
218 * densities are available as programmable densities (for assignment
220 * The upper 2 bits of the minor number are reserved for the subunit
221 * (drive #) per controller.
225 #define BIO_RDSECTID BIO_CMD1
228 * List of native drive densities. Order must match enum fd_drivetype
229 * in <sys/fdcio.h>. Upon attaching the drive, each of the
230 * programmable subdevices is initialized with the native density
233 static struct fd_type fd_native_types[] =
235 { 0 }, /* FDT_NONE */
236 { 9,2,0xFF,0x2A,40, 720,FDC_250KBPS,2,0x50,1,0,FL_MFM }, /* FDT_360K */
237 { 15,2,0xFF,0x1B,80,2400,FDC_500KBPS,2,0x54,1,0,FL_MFM }, /* FDT_12M */
238 { 9,2,0xFF,0x20,80,1440,FDC_250KBPS,2,0x50,1,0,FL_MFM }, /* FDT_720K */
239 { 18,2,0xFF,0x1B,80,2880,FDC_500KBPS,2,0x6C,1,0,FL_MFM }, /* FDT_144M */
240 #if 0 /* we currently don't handle 2.88 MB */
241 { 36,2,0xFF,0x1B,80,5760,FDC_1MBPS, 2,0x4C,1,1,FL_MFM|FL_PERPND } /*FDT_288M*/
243 { 18,2,0xFF,0x1B,80,2880,FDC_500KBPS,2,0x6C,1,0,FL_MFM }, /* FDT_144M */
248 * 360 KB 5.25" and 720 KB 3.5" drives don't have automatic density
249 * selection, they just start out with their native density (or lose).
250 * So 1.2 MB 5.25", 1.44 MB 3.5", and 2.88 MB 3.5" drives have their
251 * respective lists of densities to search for.
253 static struct fd_type fd_searchlist_12m[] = {
254 { 15,2,0xFF,0x1B,80,2400,FDC_500KBPS,2,0x54,1,0,FL_MFM }, /* 1.2M */
255 { 9,2,0xFF,0x23,40, 720,FDC_300KBPS,2,0x50,1,0,FL_MFM|FL_2STEP }, /* 360K */
256 { 9,2,0xFF,0x20,80,1440,FDC_300KBPS,2,0x50,1,0,FL_MFM }, /* 720K */
259 static struct fd_type fd_searchlist_144m[] = {
260 { 18,2,0xFF,0x1B,80,2880,FDC_500KBPS,2,0x6C,1,0,FL_MFM }, /* 1.44M */
261 { 9,2,0xFF,0x20,80,1440,FDC_250KBPS,2,0x50,1,0,FL_MFM }, /* 720K */
264 /* We search for 1.44M first since this is the most common case. */
265 static struct fd_type fd_searchlist_288m[] = {
266 { 18,2,0xFF,0x1B,80,2880,FDC_500KBPS,2,0x6C,1,0,FL_MFM }, /* 1.44M */
268 { 36,2,0xFF,0x1B,80,5760,FDC_1MBPS, 2,0x4C,1,1,FL_MFM|FL_PERPND } /* 2.88M */
270 { 9,2,0xFF,0x20,80,1440,FDC_250KBPS,2,0x50,1,0,FL_MFM }, /* 720K */
273 #define MAX_SEC_SIZE (128 << 3)
274 #define MAX_CYLINDER 85 /* some people really stress their drives
278 static devclass_t fdc_devclass;
281 * Per drive structure (softc).
284 struct fdc_data *fdc; /* pointer to controller structure */
285 int fdsu; /* this units number on this controller */
286 enum fd_drivetype type; /* drive type */
287 struct fd_type *ft; /* pointer to current type descriptor */
288 struct fd_type fts[NUMDENS]; /* type descriptors */
290 #define FD_OPEN 0x01 /* it's open */
291 #define FD_NONBLOCK 0x02 /* O_NONBLOCK set */
292 #define FD_ACTIVE 0x04 /* it's active */
293 #define FD_MOTOR 0x08 /* motor should be on */
294 #define FD_MOTOR_WAIT 0x10 /* motor coming up */
295 #define FD_UA 0x20 /* force unit attention */
298 #define FD_NO_TRACK -2
299 int track; /* where we think the head is */
300 int options; /* user configurable options, see fdcio.h */
301 struct callout_handle toffhandle;
302 struct callout_handle tohandle;
303 struct devstat device_stats;
304 eventhandler_tag clonetag;
306 dev_t clonedevs[NUMDENS - 1];
314 static devclass_t fd_devclass;
316 /* configuration flags for fd */
317 #define FD_TYPEMASK 0x0f /* drive type, matches enum
318 * fd_drivetype; on i386 machines, if
319 * given as 0, use RTC type for fd0
321 #define FD_DTYPE(flags) ((flags) & FD_TYPEMASK)
322 #define FD_NO_CHLINE 0x10 /* drive does not support changeline
323 * aka. unit attention */
324 #define FD_NO_PROBE 0x20 /* don't probe drive (seek test), just
325 * assume it is there */
328 * Throughout this file the following conventions will be used:
330 * fd is a pointer to the fd_data struct for the drive in question
331 * fdc is a pointer to the fdc_data struct for the controller
332 * fdu is the floppy drive unit number
333 * fdcu is the floppy controller unit number
334 * fdsu is the floppy drive unit number on that controller. (sub-unit)
338 * Function declarations, same (chaotic) order as they appear in the
339 * file. Re-ordering is too late now, it would only obfuscate the
340 * diffs against old and offspring versions (like the PC98 one).
342 * Anyone adding functions here, please keep this sequence the same
343 * as below -- makes locating a particular function in the body much
346 static void fdout_wr(fdc_p, u_int8_t);
347 static u_int8_t fdsts_rd(fdc_p);
348 static void fddata_wr(fdc_p, u_int8_t);
349 static u_int8_t fddata_rd(fdc_p);
350 static void fdctl_wr_isa(fdc_p, u_int8_t);
352 static void fdctl_wr_pcmcia(fdc_p, u_int8_t);
355 static u_int8_t fdin_rd(fdc_p);
357 static int fdc_err(struct fdc_data *, const char *);
358 static int fd_cmd(struct fdc_data *, int, ...);
359 static int enable_fifo(fdc_p fdc);
360 static int fd_sense_drive_status(fdc_p, int *);
361 static int fd_sense_int(fdc_p, int *, int *);
362 static int fd_read_status(fdc_p);
363 static int fdc_alloc_resources(struct fdc_data *);
364 static void fdc_release_resources(struct fdc_data *);
365 static int fdc_read_ivar(device_t, device_t, int, uintptr_t *);
366 static int fdc_probe(device_t);
368 static int fdc_pccard_probe(device_t);
370 static int fdc_detach(device_t dev);
371 static void fdc_add_child(device_t, const char *, int);
372 static int fdc_attach(device_t);
373 static int fdc_print_child(device_t, device_t);
374 static void fd_clone (void *, char *, int, dev_t *);
375 static int fd_probe(device_t);
376 static int fd_attach(device_t);
377 static int fd_detach(device_t);
378 static void set_motor(struct fdc_data *, int, int);
381 static timeout_t fd_turnoff;
382 static timeout_t fd_motor_on;
383 static void fd_turnon(struct fd_data *);
384 static void fdc_reset(fdc_p);
385 static int fd_in(struct fdc_data *, int *);
386 static int out_fdc(struct fdc_data *, int);
388 * The open function is named Fdopen() to avoid confusion with fdopen()
389 * in fd(4). The difference is now only meaningful for debuggers.
391 static d_open_t Fdopen;
392 static d_close_t fdclose;
393 static d_strategy_t fdstrategy;
394 static void fdstart(struct fdc_data *);
395 static timeout_t fd_iotimeout;
396 static timeout_t fd_pseudointr;
397 static driver_intr_t fdc_intr;
398 static int fdcpio(fdc_p, long, caddr_t, u_int);
399 static int fdautoselect(dev_t);
400 static int fdstate(struct fdc_data *);
401 static int retrier(struct fdc_data *);
402 static void fdbiodone(struct bio *);
403 static int fdmisccmd(dev_t, u_int, void *);
404 static d_ioctl_t fdioctl;
406 static int fifo_threshold = 8; /* XXX: should be accessible via sysctl */
409 /* CAUTION: fd_debug causes huge amounts of logging output */
410 static int volatile fd_debug = 0;
411 #define TRACE0(arg) do { if (fd_debug) printf(arg); } while (0)
412 #define TRACE1(arg1, arg2) do { if (fd_debug) printf(arg1, arg2); } while (0)
413 #else /* FDC_DEBUG */
414 #define TRACE0(arg) do { } while (0)
415 #define TRACE1(arg1, arg2) do { } while (0)
416 #endif /* FDC_DEBUG */
419 * Bus space handling (access to low-level IO).
422 fdout_wr(fdc_p fdc, u_int8_t v)
424 bus_space_write_1(fdc->portt, fdc->porth, FDOUT+fdc->port_off, v);
430 return bus_space_read_1(fdc->portt, fdc->porth, FDSTS+fdc->port_off);
434 fddata_wr(fdc_p fdc, u_int8_t v)
436 bus_space_write_1(fdc->portt, fdc->porth, FDDATA+fdc->port_off, v);
442 return bus_space_read_1(fdc->portt, fdc->porth, FDDATA+fdc->port_off);
446 fdctl_wr_isa(fdc_p fdc, u_int8_t v)
448 bus_space_write_1(fdc->ctlt, fdc->ctlh, 0, v);
453 fdctl_wr_pcmcia(fdc_p fdc, u_int8_t v)
455 bus_space_write_1(fdc->portt, fdc->porth, FDCTL+fdc->port_off, v);
462 return bus_space_read_1(fdc->portt, fdc->porth, FDIN);
466 static struct cdevsw fd_cdevsw = {
470 /* write */ physwrite,
474 /* strategy */ fdstrategy,
476 /* maj */ CDEV_MAJOR,
483 * Auxiliary functions. Well, some only. Others are scattered
484 * throughout the entire file.
487 fdc_err(struct fdc_data *fdc, const char *s)
491 if (fdc->fdc_errs < FDC_ERRMAX)
492 device_printf(fdc->fdc_dev, "%s", s);
493 else if (fdc->fdc_errs == FDC_ERRMAX)
494 device_printf(fdc->fdc_dev, "too many errors, not "
495 "logging any more\n");
502 * fd_cmd: Send a command to the chip. Takes a varargs with this structure:
504 * # of output bytes, output bytes as ints ...,
505 * # of input bytes, input bytes as ints ...
508 fd_cmd(struct fdc_data *fdc, int n_out, ...)
516 cmd = (u_char)(va_arg(ap, int));
519 for (n = 0; n < n_out; n++)
521 if (out_fdc(fdc, va_arg(ap, int)) < 0)
524 snprintf(msg, sizeof(msg),
525 "cmd %x failed at out byte %d of %d\n",
527 return fdc_err(fdc, msg);
530 n_in = va_arg(ap, int);
531 for (n = 0; n < n_in; n++)
533 int *ptr = va_arg(ap, int *);
534 if (fd_in(fdc, ptr) < 0)
537 snprintf(msg, sizeof(msg),
538 "cmd %02x failed at in byte %d of %d\n",
540 return fdc_err(fdc, msg);
548 enable_fifo(fdc_p fdc)
552 if ((fdc->flags & FDC_HAS_FIFO) == 0) {
555 * Cannot use fd_cmd the normal way here, since
556 * this might be an invalid command. Thus we send the
557 * first byte, and check for an early turn of data directon.
560 if (out_fdc(fdc, I8207X_CONFIGURE) < 0)
561 return fdc_err(fdc, "Enable FIFO failed\n");
563 /* If command is invalid, return */
565 while ((i = fdsts_rd(fdc) & (NE7_DIO | NE7_RQM))
566 != NE7_RQM && j-- > 0)
567 if (i == (NE7_DIO | NE7_RQM)) {
573 0, (fifo_threshold - 1) & 0xf, 0, 0) < 0) {
575 return fdc_err(fdc, "Enable FIFO failed\n");
577 fdc->flags |= FDC_HAS_FIFO;
581 I8207X_CONFIGURE, 0, (fifo_threshold - 1) & 0xf, 0, 0) < 0)
582 return fdc_err(fdc, "Re-enable FIFO failed\n");
587 fd_sense_drive_status(fdc_p fdc, int *st3p)
591 if (fd_cmd(fdc, 2, NE7CMD_SENSED, fdc->fdu, 1, &st3))
593 return fdc_err(fdc, "Sense Drive Status failed\n");
602 fd_sense_int(fdc_p fdc, int *st0p, int *cylp)
606 ret = fd_cmd(fdc, 1, NE7CMD_SENSEI, 1, &st0);
609 "sense intr err reading stat reg 0\n");
616 if ((st0 & NE7_ST0_IC) == NE7_ST0_IC_IV) {
618 * There doesn't seem to have been an interrupt.
623 if (fd_in(fdc, &cyl) < 0) {
624 return fdc_err(fdc, "can't get cyl num\n");
635 fd_read_status(fdc_p fdc)
639 for (i = ret = 0; i < 7; i++) {
641 * XXX types are poorly chosen. Only bytes can be read
642 * from the hardware, but fdc->status[] wants u_ints and
643 * fd_in() gives ints.
647 ret = fd_in(fdc, &status);
648 fdc->status[i] = status;
654 fdc->flags |= FDC_STAT_VALID;
656 fdc->flags &= ~FDC_STAT_VALID;
662 fdc_alloc_resources(struct fdc_data *fdc)
665 int ispnp, ispcmcia, nports;
668 ispnp = (fdc->flags & FDC_ISPNP) != 0;
669 ispcmcia = (fdc->flags & FDC_ISPCMCIA) != 0;
670 fdc->rid_ioport = fdc->rid_irq = fdc->rid_drq = 0;
671 fdc->res_ioport = fdc->res_irq = fdc->res_drq = 0;
674 * On standard ISA, we don't just use an 8 port range
675 * (e.g. 0x3f0-0x3f7) since that covers an IDE control
678 * Isn't PC hardware wonderful.
680 * The Y-E Data PCMCIA FDC doesn't have this problem, it
681 * uses the register with offset 6 for pseudo-DMA, and the
682 * one with offset 7 as control register.
684 nports = ispcmcia ? 8 : (ispnp ? 1 : 6);
685 fdc->res_ioport = bus_alloc_resource(dev, SYS_RES_IOPORT,
686 &fdc->rid_ioport, 0ul, ~0ul,
688 if (fdc->res_ioport == 0) {
689 device_printf(dev, "cannot reserve I/O port range (%d ports)\n",
693 fdc->portt = rman_get_bustag(fdc->res_ioport);
694 fdc->porth = rman_get_bushandle(fdc->res_ioport);
698 * Some BIOSen report the device at 0x3f2-0x3f5,0x3f7
699 * and some at 0x3f0-0x3f5,0x3f7. We detect the former
700 * by checking the size and adjust the port address
703 if (bus_get_resource_count(dev, SYS_RES_IOPORT, 0) == 4)
707 * Register the control port range as rid 1 if it
708 * isn't there already. Most PnP BIOSen will have
709 * already done this but non-PnP configurations don't.
711 * And some (!!) report 0x3f2-0x3f5 and completely
712 * leave out the control register! It seems that some
713 * non-antique controller chips have a different
714 * method of programming the transfer speed which
715 * doesn't require the control register, but it's
716 * mighty bogus as the chip still responds to the
717 * address for the control register.
719 if (bus_get_resource_count(dev, SYS_RES_IOPORT, 1) == 0) {
722 /* Find the control port, usually 0x3f7 */
723 ctlstart = rman_get_start(fdc->res_ioport) +
726 bus_set_resource(dev, SYS_RES_IOPORT, 1, ctlstart, 1);
730 * Now (finally!) allocate the control port.
733 fdc->res_ctl = bus_alloc_resource(dev, SYS_RES_IOPORT,
735 0ul, ~0ul, 1, RF_ACTIVE);
736 if (fdc->res_ctl == 0) {
738 "cannot reserve control I/O port range (control port)\n");
741 fdc->ctlt = rman_get_bustag(fdc->res_ctl);
742 fdc->ctlh = rman_get_bushandle(fdc->res_ctl);
745 fdc->res_irq = bus_alloc_resource(dev, SYS_RES_IRQ,
746 &fdc->rid_irq, 0ul, ~0ul, 1,
748 if (fdc->res_irq == 0) {
749 device_printf(dev, "cannot reserve interrupt line\n");
753 if ((fdc->flags & FDC_NODMA) == 0) {
754 fdc->res_drq = bus_alloc_resource(dev, SYS_RES_DRQ,
755 &fdc->rid_drq, 0ul, ~0ul, 1,
757 if (fdc->res_drq == 0) {
758 device_printf(dev, "cannot reserve DMA request line\n");
761 fdc->dmachan = fdc->res_drq->r_start;
768 fdc_release_resources(struct fdc_data *fdc)
773 if (fdc->res_irq != 0) {
774 bus_deactivate_resource(dev, SYS_RES_IRQ, fdc->rid_irq,
776 bus_release_resource(dev, SYS_RES_IRQ, fdc->rid_irq,
779 if (fdc->res_ctl != 0) {
780 bus_deactivate_resource(dev, SYS_RES_IOPORT, fdc->rid_ctl,
782 bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ctl,
785 if (fdc->res_ioport != 0) {
786 bus_deactivate_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport,
788 bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport,
791 if (fdc->res_drq != 0) {
792 bus_deactivate_resource(dev, SYS_RES_DRQ, fdc->rid_drq,
794 bus_release_resource(dev, SYS_RES_DRQ, fdc->rid_drq,
800 * Configuration/initialization stuff, per controller.
803 static struct isa_pnp_id fdc_ids[] = {
804 {0x0007d041, "PC standard floppy disk controller"}, /* PNP0700 */
805 {0x0107d041, "Standard floppy controller supporting MS Device Bay Spec"}, /* PNP0701 */
810 fdc_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
812 struct fdc_ivars *ivars = device_get_ivars(child);
815 case FDC_IVAR_FDUNIT:
816 *result = ivars->fdunit;
825 fdc_probe(device_t dev)
828 struct fdc_data *fdc;
830 fdc = device_get_softc(dev);
831 bzero(fdc, sizeof *fdc);
833 fdc->fdctl_wr = fdctl_wr_isa;
836 error = ISA_PNP_PROBE(device_get_parent(dev), dev, fdc_ids);
840 fdc->flags |= FDC_ISPNP;
842 /* Attempt to allocate our resources for the duration of the probe */
843 error = fdc_alloc_resources(fdc);
847 /* First - lets reset the floppy controller */
850 fdout_wr(fdc, FDO_FRST);
852 /* see if it can handle a command */
853 if (fd_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240),
854 NE7_SPEC_2(2, 0), 0)) {
859 if (fd_cmd(fdc, 1, NE7CMD_VERSION, 1, &ic_type) == 0) {
860 ic_type = (u_char)ic_type;
863 device_set_desc(dev, "NEC 765 or clone");
864 fdc->fdct = FDC_NE765;
866 case 0x81: /* not mentioned in any hardware doc */
869 "enhanced floppy controller (i82077, NE72065 or clone)");
870 fdc->fdct = FDC_ENHANCED;
873 device_set_desc(dev, "generic floppy controller");
874 fdc->fdct = FDC_UNKNOWN;
880 fdc_release_resources(fdc);
887 fdc_pccard_probe(device_t dev)
890 struct fdc_data *fdc;
892 fdc = device_get_softc(dev);
893 bzero(fdc, sizeof *fdc);
895 fdc->fdctl_wr = fdctl_wr_pcmcia;
897 fdc->flags |= FDC_ISPCMCIA | FDC_NODMA;
899 /* Attempt to allocate our resources for the duration of the probe */
900 error = fdc_alloc_resources(fdc);
904 /* First - lets reset the floppy controller */
907 fdout_wr(fdc, FDO_FRST);
909 /* see if it can handle a command */
910 if (fd_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240),
911 NE7_SPEC_2(2, 0), 0)) {
916 device_set_desc(dev, "Y-E Data PCMCIA floppy");
917 fdc->fdct = FDC_NE765;
920 fdc_release_resources(fdc);
924 #endif /* NCARD > 0 */
927 fdc_detach(device_t dev)
929 struct fdc_data *fdc;
932 fdc = device_get_softc(dev);
934 /* have our children detached first */
935 if ((error = bus_generic_detach(dev)))
938 /* reset controller, turn motor off */
941 if ((fdc->flags & FDC_NODMA) == 0)
942 isa_dma_release(fdc->dmachan);
944 if ((fdc->flags & FDC_ATTACHED) == 0) {
945 device_printf(dev, "already unloaded\n");
948 fdc->flags &= ~FDC_ATTACHED;
950 BUS_TEARDOWN_INTR(device_get_parent(dev), dev, fdc->res_irq,
952 fdc_release_resources(fdc);
953 device_printf(dev, "unload\n");
958 * Add a child device to the fdc controller. It will then be probed etc.
961 fdc_add_child(device_t dev, const char *name, int unit)
964 struct fdc_ivars *ivar;
967 ivar = malloc(sizeof *ivar, M_DEVBUF /* XXX */, M_NOWAIT | M_ZERO);
970 if (resource_int_value(name, unit, "drive", &ivar->fdunit) != 0)
972 child = device_add_child(dev, name, unit);
975 device_set_ivars(child, ivar);
976 if (resource_int_value(name, unit, "flags", &flags) == 0)
977 device_set_flags(child, flags);
978 if (resource_int_value(name, unit, "disabled", &disabled) == 0
980 device_disable(child);
984 fdc_attach(device_t dev)
986 struct fdc_data *fdc;
987 const char *name, *dname;
990 fdc = device_get_softc(dev);
991 error = fdc_alloc_resources(fdc);
993 device_printf(dev, "cannot re-acquire resources\n");
996 error = BUS_SETUP_INTR(device_get_parent(dev), dev, fdc->res_irq,
997 INTR_TYPE_BIO | INTR_ENTROPY, fdc_intr, fdc,
1000 device_printf(dev, "cannot setup interrupt\n");
1003 fdc->fdcu = device_get_unit(dev);
1004 fdc->flags |= FDC_ATTACHED | FDC_NEEDS_RESET;
1006 if ((fdc->flags & FDC_NODMA) == 0) {
1008 * Acquire the DMA channel forever, the driver will do
1010 * XXX should integrate with rman
1012 isa_dma_acquire(fdc->dmachan);
1013 isa_dmainit(fdc->dmachan, MAX_SEC_SIZE);
1015 fdc->state = DEVIDLE;
1017 /* reset controller, turn motor off, clear fdout mirror reg */
1018 fdout_wr(fdc, fdc->fdout = 0);
1019 bioq_init(&fdc->head);
1022 * Probe and attach any children. We should probably detect
1023 * devices from the BIOS unless overridden.
1025 name = device_get_nameunit(dev);
1027 while ((resource_find_match(&i, &dname, &dunit, "at", name)) == 0)
1028 fdc_add_child(dev, dname, dunit);
1030 if ((error = bus_generic_attach(dev)) != 0)
1037 fdc_print_child(device_t me, device_t child)
1039 int retval = 0, flags;
1041 retval += bus_print_child_header(me, child);
1042 retval += printf(" on %s drive %d", device_get_nameunit(me),
1043 fdc_get_fdunit(child));
1044 if ((flags = device_get_flags(me)) != 0)
1045 retval += printf(" flags %#x", flags);
1046 retval += printf("\n");
1051 static device_method_t fdc_methods[] = {
1052 /* Device interface */
1053 DEVMETHOD(device_probe, fdc_probe),
1054 DEVMETHOD(device_attach, fdc_attach),
1055 DEVMETHOD(device_detach, fdc_detach),
1056 DEVMETHOD(device_shutdown, bus_generic_shutdown),
1057 DEVMETHOD(device_suspend, bus_generic_suspend),
1058 DEVMETHOD(device_resume, bus_generic_resume),
1061 DEVMETHOD(bus_print_child, fdc_print_child),
1062 DEVMETHOD(bus_read_ivar, fdc_read_ivar),
1063 /* Our children never use any other bus interface methods. */
1068 static driver_t fdc_driver = {
1071 sizeof(struct fdc_data)
1074 DRIVER_MODULE(fdc, isa, fdc_driver, fdc_devclass, 0, 0);
1075 DRIVER_MODULE(fdc, acpi, fdc_driver, fdc_devclass, 0, 0);
1079 static device_method_t fdc_pccard_methods[] = {
1080 /* Device interface */
1081 DEVMETHOD(device_probe, fdc_pccard_probe),
1082 DEVMETHOD(device_attach, fdc_attach),
1083 DEVMETHOD(device_detach, fdc_detach),
1084 DEVMETHOD(device_shutdown, bus_generic_shutdown),
1085 DEVMETHOD(device_suspend, bus_generic_suspend),
1086 DEVMETHOD(device_resume, bus_generic_resume),
1089 DEVMETHOD(bus_print_child, fdc_print_child),
1090 DEVMETHOD(bus_read_ivar, fdc_read_ivar),
1091 /* Our children never use any other bus interface methods. */
1096 static driver_t fdc_pccard_driver = {
1099 sizeof(struct fdc_data)
1102 DRIVER_MODULE(fdc, pccard, fdc_pccard_driver, fdc_devclass, 0, 0);
1104 #endif /* NCARD > 0 */
1107 * Create a clone device upon request by devfs.
1110 fd_clone(void *arg, char *name, int namelen, dev_t *dev)
1117 fd = (struct fd_data *)arg;
1120 if (dev_stdclone(name, &n, "fd", &u) != 2)
1123 if (l == 1 && *n >= 'a' && *n <= 'h') {
1125 * Trailing letters a through h denote
1126 * pseudo-partitions. We don't support true
1127 * (UFS-style) partitions, so we just implement them
1128 * as symlinks if someone asks us nicely.
1130 *dev = make_dev_alias(fd->masterdev, name);
1133 if (l >= 2 && l <= 5 && *n == '.') {
1135 * Trailing numbers, preceded by a dot, denote
1136 * subdevices for different densities. Historically,
1137 * they have been named by density (like fd0.1440),
1138 * but we allow arbitrary numbers between 1 and 4
1139 * digits, so fd0.1 through fd0.15 are possible as
1142 for (i = 1; i < l; i++)
1143 if (n[i] < '0' || n[i] > '9')
1145 for (i = 0; i < NUMDENS - 1; i++)
1146 if (fd->clonedevs[i] == NODEV) {
1147 *dev = make_dev(&fd_cdevsw,
1148 FDNUMTOUNIT(u) + i + 1,
1149 UID_ROOT, GID_OPERATOR, 0640,
1151 fd->clonedevs[i] = *dev;
1158 * Configuration/initialization, per drive.
1161 fd_probe(device_t dev)
1166 struct fdc_data *fdc;
1170 fdsu = *(int *)device_get_ivars(dev); /* xxx cheat a bit... */
1171 fd = device_get_softc(dev);
1172 fdc = device_get_softc(device_get_parent(dev));
1173 flags = device_get_flags(dev);
1175 bzero(fd, sizeof *fd);
1179 fd->fdu = device_get_unit(dev);
1180 fd->flags = FD_UA; /* make sure fdautoselect() will be called */
1182 fd->type = FD_DTYPE(flags);
1184 * XXX I think using __i386__ is wrong here since we actually want to probe
1185 * for the machine type, not the CPU type (so non-PC arch's like the PC98 will
1186 * fail the probe). However, for whatever reason, testing for _MACHINE_ARCH
1187 * == i386 breaks the test on FreeBSD/Alpha.
1190 if (fd->type == FDT_NONE && (fd->fdu == 0 || fd->fdu == 1)) {
1191 /* Look up what the BIOS thinks we have. */
1193 if ((fdc->flags & FDC_ISPCMCIA))
1195 * Somewhat special. No need to force the
1196 * user to set device flags, since the Y-E
1197 * Data PCMCIA floppy is always a 1.44 MB
1200 fd->type = FDT_144M;
1202 fd->type = (rtcin(RTC_FDISKETTE) & 0xf0) >> 4;
1204 fd->type = rtcin(RTC_FDISKETTE) & 0x0f;
1206 if (fd->type == FDT_288M_1)
1207 fd->type = FDT_288M;
1209 #endif /* __i386__ */
1210 /* is there a unit? */
1211 if (fd->type == FDT_NONE)
1215 set_motor(fdc, fdsu, TURNON);
1216 fdc_reset(fdc); /* XXX reset, then unreset, etc. */
1217 DELAY(1000000); /* 1 sec */
1219 /* XXX This doesn't work before the first set_motor() */
1220 if ((fdc->flags & FDC_HAS_FIFO) == 0 &&
1221 fdc->fdct == FDC_ENHANCED &&
1222 (device_get_flags(fdc->fdc_dev) & FDC_NO_FIFO) == 0 &&
1223 enable_fifo(fdc) == 0) {
1224 device_printf(device_get_parent(dev),
1225 "FIFO enabled, %d bytes threshold\n", fifo_threshold);
1228 if ((flags & FD_NO_PROBE) == 0) {
1229 /* If we're at track 0 first seek inwards. */
1230 if ((fd_sense_drive_status(fdc, &st3) == 0) &&
1231 (st3 & NE7_ST3_T0)) {
1232 /* Seek some steps... */
1233 if (fd_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) {
1234 /* ...wait a moment... */
1236 /* make ctrlr happy: */
1237 fd_sense_int(fdc, 0, 0);
1241 for (i = 0; i < 2; i++) {
1243 * we must recalibrate twice, just in case the
1244 * heads have been beyond cylinder 76, since
1245 * most FDCs still barf when attempting to
1246 * recalibrate more than 77 steps
1249 if (fd_cmd(fdc, 2, NE7CMD_RECAL, fdsu, 0) == 0) {
1250 /* a second being enough for full stroke seek*/
1251 DELAY(i == 0 ? 1000000 : 300000);
1253 /* anything responding? */
1254 if (fd_sense_int(fdc, &st0, 0) == 0 &&
1255 (st0 & NE7_ST0_EC) == 0)
1256 break; /* already probed succesfully */
1261 set_motor(fdc, fdsu, TURNOFF);
1263 if ((flags & FD_NO_PROBE) == 0 &&
1264 (st0 & NE7_ST0_EC) != 0) /* no track 0 -> no drive present */
1269 device_set_desc(dev, "1200-KB 5.25\" drive");
1273 device_set_desc(dev, "1440-KB 3.5\" drive");
1274 fd->type = FDT_144M;
1277 device_set_desc(dev, "2880-KB 3.5\" drive (in 1440-KB mode)");
1278 fd->type = FDT_288M;
1281 device_set_desc(dev, "360-KB 5.25\" drive");
1282 fd->type = FDT_360K;
1285 device_set_desc(dev, "720-KB 3.5\" drive");
1286 fd->type = FDT_720K;
1291 fd->track = FD_NO_TRACK;
1295 callout_handle_init(&fd->toffhandle);
1296 callout_handle_init(&fd->tohandle);
1298 /* initialize densities for subdevices */
1299 for (i = 0; i < NUMDENS; i++)
1300 memcpy(fd->fts + i, fd_native_types + fd->type,
1301 sizeof(struct fd_type));
1306 fd_attach(device_t dev)
1309 static int cdevsw_add_done;
1312 if (!cdevsw_add_done) {
1313 cdevsw_add(&fd_cdevsw); /* XXX */
1314 cdevsw_add_done = 1;
1316 fd = device_get_softc(dev);
1317 fd->clonetag = EVENTHANDLER_REGISTER(dev_clone, fd_clone, fd, 1000);
1318 fd->masterdev = make_dev(&fd_cdevsw, fd->fdu << 6,
1319 UID_ROOT, GID_OPERATOR, 0640, "fd%d", fd->fdu);
1320 for (i = 0; i < NUMDENS - 1; i++)
1321 fd->clonedevs[i] = NODEV;
1322 devstat_add_entry(&fd->device_stats, device_get_name(dev),
1323 device_get_unit(dev), 0, DEVSTAT_NO_ORDERED_TAGS,
1324 DEVSTAT_TYPE_FLOPPY | DEVSTAT_TYPE_IF_OTHER,
1325 DEVSTAT_PRIORITY_FD);
1330 fd_detach(device_t dev)
1335 fd = device_get_softc(dev);
1336 untimeout(fd_turnoff, fd, fd->toffhandle);
1337 devstat_remove_entry(&fd->device_stats);
1338 destroy_dev(fd->masterdev);
1339 for (i = 0; i < NUMDENS - 1; i++)
1340 if (fd->clonedevs[i] != NODEV)
1341 destroy_dev(fd->clonedevs[i]);
1342 cdevsw_remove(&fd_cdevsw);
1343 EVENTHANDLER_DEREGISTER(dev_clone, fd->clonetag);
1348 static device_method_t fd_methods[] = {
1349 /* Device interface */
1350 DEVMETHOD(device_probe, fd_probe),
1351 DEVMETHOD(device_attach, fd_attach),
1352 DEVMETHOD(device_detach, fd_detach),
1353 DEVMETHOD(device_shutdown, bus_generic_shutdown),
1354 DEVMETHOD(device_suspend, bus_generic_suspend), /* XXX */
1355 DEVMETHOD(device_resume, bus_generic_resume), /* XXX */
1360 static driver_t fd_driver = {
1363 sizeof(struct fd_data)
1366 DRIVER_MODULE(fd, fdc, fd_driver, fd_devclass, 0, 0);
1369 * More auxiliary functions.
1372 * Motor control stuff.
1373 * Remember to not deselect the drive we're working on.
1376 set_motor(struct fdc_data *fdc, int fdsu, int turnon)
1382 fdout &= ~FDO_FDSEL;
1383 fdout |= (FDO_MOEN0 << fdsu) | FDO_FDMAEN | FDO_FRST | fdsu;
1385 fdout &= ~(FDO_MOEN0 << fdsu);
1387 fdout_wr(fdc, fdout);
1388 TRACE1("[0x%x->FDOUT]", fdout);
1392 fd_turnoff(void *xfd)
1397 TRACE1("[fd%d: turnoff]", fd->fdu);
1401 * Don't turn off the motor yet if the drive is active.
1403 * If we got here, this could only mean we missed an interrupt.
1404 * This can e. g. happen on the Y-E Date PCMCIA floppy controller
1405 * after a controller reset. Just schedule a pseudo-interrupt
1406 * so the state machine gets re-entered.
1408 if (fd->fdc->state != DEVIDLE && fd->fdc->fdu == fd->fdu) {
1414 fd->flags &= ~FD_MOTOR;
1415 set_motor(fd->fdc, fd->fdsu, TURNOFF);
1420 fd_motor_on(void *xfd)
1426 fd->flags &= ~FD_MOTOR_WAIT;
1427 if((fd->fdc->fd == fd) && (fd->fdc->state == MOTORWAIT))
1437 if(!(fd->flags & FD_MOTOR))
1439 fd->flags |= (FD_MOTOR + FD_MOTOR_WAIT);
1440 set_motor(fd->fdc, fd->fdsu, TURNON);
1441 timeout(fd_motor_on, fd, hz); /* in 1 sec its ok */
1446 fdc_reset(fdc_p fdc)
1448 /* Try a reset, keep motor on */
1449 fdout_wr(fdc, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
1450 TRACE1("[0x%x->FDOUT]", fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
1452 /* enable FDC, but defer interrupts a moment */
1453 fdout_wr(fdc, fdc->fdout & ~FDO_FDMAEN);
1454 TRACE1("[0x%x->FDOUT]", fdc->fdout & ~FDO_FDMAEN);
1456 fdout_wr(fdc, fdc->fdout);
1457 TRACE1("[0x%x->FDOUT]", fdc->fdout);
1459 /* XXX after a reset, silently believe the FDC will accept commands */
1460 (void)fd_cmd(fdc, 3, NE7CMD_SPECIFY,
1461 NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0),
1463 if (fdc->flags & FDC_HAS_FIFO)
1464 (void) enable_fifo(fdc);
1468 * FDC IO functions, take care of the main status register, timeout
1469 * in case the desired status bits are never set.
1472 fd_in(struct fdc_data *fdc, int *ptr)
1475 while ((i = fdsts_rd(fdc) & (NE7_DIO|NE7_RQM))
1476 != (NE7_DIO|NE7_RQM) && j-- > 0)
1478 return fdc_err(fdc, "ready for output in input\n");
1480 return fdc_err(fdc, bootverbose? "input ready timeout\n": 0);
1483 TRACE1("[FDDATA->0x%x]", (unsigned char)i);
1486 #else /* !FDC_DEBUG */
1491 #endif /* FDC_DEBUG */
1495 out_fdc(struct fdc_data *fdc, int x)
1499 /* Check that the direction bit is set */
1501 while ((fdsts_rd(fdc) & NE7_DIO) && i-- > 0);
1502 if (i <= 0) return fdc_err(fdc, "direction bit not set\n");
1504 /* Check that the floppy controller is ready for a command */
1506 while ((fdsts_rd(fdc) & NE7_RQM) == 0 && i-- > 0);
1508 return fdc_err(fdc, bootverbose? "output ready timeout\n": 0);
1510 /* Send the command and return */
1512 TRACE1("[0x%x->FDDATA]", x);
1517 * Block device driver interface functions (interspersed with even more
1518 * auxiliary functions).
1521 Fdopen(dev_t dev, int flags, int mode, struct thread *td)
1523 fdu_t fdu = FDUNIT(minor(dev));
1524 int type = FDTYPE(minor(dev));
1527 int rv, unitattn, dflags;
1529 if ((fd = devclass_get_softc(fd_devclass, fdu)) == 0)
1532 if ((fdc == NULL) || (fd->type == FDT_NONE))
1536 dflags = device_get_flags(fd->dev);
1538 * This is a bit bogus. It's still possible that e. g. a
1539 * descriptor gets inherited to a child, but then it's at
1540 * least for the same subdevice. By checking FD_OPEN here, we
1541 * can ensure that a device isn't attempted to be opened with
1542 * different densities at the same time where the second open
1543 * could clobber the settings from the first one.
1545 if (fd->flags & FD_OPEN)
1549 if (flags & FNONBLOCK) {
1551 * Unfortunately, physio(9) discards its ioflag
1552 * argument, thus preventing us from seeing the
1553 * IO_NDELAY bit. So we need to keep track
1556 fd->flags |= FD_NONBLOCK;
1560 * Figure out a unit attention condition.
1562 * If UA has been forced, proceed.
1564 * If motor is off, turn it on for a moment
1565 * and select our drive, in order to read the
1566 * UA hardware signal.
1568 * If motor is on, and our drive is currently
1569 * selected, just read the hardware bit.
1571 * If motor is on, but active for another
1572 * drive on that controller, we are lost. We
1573 * cannot risk to deselect the other drive, so
1574 * we just assume a forced UA condition to be
1578 if ((dflags & FD_NO_CHLINE) != 0 ||
1579 (fd->flags & FD_UA) != 0) {
1581 fd->flags &= ~FD_UA;
1582 } else if (fdc->fdout & (FDO_MOEN0 | FDO_MOEN1 |
1583 FDO_MOEN2 | FDO_MOEN3)) {
1584 if ((fdc->fdout & FDO_FDSEL) == fd->fdsu)
1585 unitattn = fdin_rd(fdc) & FDI_DCHG;
1589 set_motor(fdc, fd->fdsu, TURNON);
1590 unitattn = fdin_rd(fdc) & FDI_DCHG;
1591 set_motor(fdc, fd->fdsu, TURNOFF);
1593 if (unitattn && (rv = fdautoselect(dev)) != 0)
1597 fd->ft = fd->fts + type;
1599 fd->flags |= FD_OPEN;
1601 * Clearing the DMA overrun counter at open time is a bit messy.
1602 * Since we're only managing one counter per controller, opening
1603 * the second drive could mess it up. Anyway, if the DMA overrun
1604 * condition is really persistent, it will eventually time out
1605 * still. OTOH, clearing it here will ensure we'll at least start
1606 * trying again after a previous (maybe even long ago) failure.
1607 * Also, this is merely a stop-gap measure only that should not
1608 * happen during normal operation, so we can tolerate it to be a
1609 * bit sloppy about this.
1611 fdc->dma_overruns = 0;
1617 fdclose(dev_t dev, int flags, int mode, struct thread *td)
1619 fdu_t fdu = FDUNIT(minor(dev));
1622 fd = devclass_get_softc(fd_devclass, fdu);
1623 fd->flags &= ~(FD_OPEN | FD_NONBLOCK);
1624 fd->options &= ~(FDOPT_NORETRY | FDOPT_NOERRLOG | FDOPT_NOERROR);
1630 fdstrategy(struct bio *bp)
1632 long blknum, nblocks;
1639 fdu = FDUNIT(minor(bp->bio_dev));
1640 fd = devclass_get_softc(fd_devclass, fdu);
1642 panic("fdstrategy: buf for nonexistent device (%#lx, %#lx)",
1643 (u_long)major(bp->bio_dev), (u_long)minor(bp->bio_dev));
1645 if (fd->type == FDT_NONE || fd->ft == 0) {
1646 bp->bio_error = ENXIO;
1647 bp->bio_flags |= BIO_ERROR;
1650 fdblk = 128 << (fd->ft->secsize);
1651 if (bp->bio_cmd != BIO_FORMAT && bp->bio_cmd != BIO_RDSECTID) {
1652 if (fd->flags & FD_NONBLOCK) {
1653 bp->bio_error = EAGAIN;
1654 bp->bio_flags |= BIO_ERROR;
1657 if (bp->bio_blkno < 0) {
1659 "fd%d: fdstrat: bad request blkno = %lu, bcount = %ld\n",
1660 fdu, (u_long)bp->bio_blkno, bp->bio_bcount);
1661 bp->bio_error = EINVAL;
1662 bp->bio_flags |= BIO_ERROR;
1665 if ((bp->bio_bcount % fdblk) != 0) {
1666 bp->bio_error = EINVAL;
1667 bp->bio_flags |= BIO_ERROR;
1673 * Set up block calculations.
1675 if (bp->bio_blkno > 20000000) {
1677 * Reject unreasonably high block number, prevent the
1678 * multiplication below from overflowing.
1680 bp->bio_error = EINVAL;
1681 bp->bio_flags |= BIO_ERROR;
1684 blknum = bp->bio_blkno * DEV_BSIZE / fdblk;
1685 nblocks = fd->ft->size;
1686 if (blknum + bp->bio_bcount / fdblk > nblocks) {
1687 if (blknum >= nblocks) {
1688 if (bp->bio_cmd == BIO_READ)
1689 bp->bio_resid = bp->bio_bcount;
1691 bp->bio_error = ENOSPC;
1692 bp->bio_flags |= BIO_ERROR;
1694 goto bad; /* not always bad, but EOF */
1696 bp->bio_bcount = (nblocks - blknum) * fdblk;
1698 bp->bio_pblkno = bp->bio_blkno;
1700 bioqdisksort(&fdc->head, bp);
1701 untimeout(fd_turnoff, fd, fd->toffhandle); /* a good idea */
1702 devstat_start_transaction(&fd->device_stats);
1703 device_busy(fd->dev);
1715 * We have just queued something. If the controller is not busy
1716 * then simulate the case where it has just finished a command
1717 * So that it (the interrupt routine) looks on the queue for more
1718 * work to do and picks up what we just added.
1720 * If the controller is already busy, we need do nothing, as it
1721 * will pick up our work when the present work completes.
1724 fdstart(struct fdc_data *fdc)
1729 if(fdc->state == DEVIDLE)
1737 fd_iotimeout(void *xfdc)
1743 TRACE1("fd%d[fd_iotimeout()]", fdc->fdu);
1746 * Due to IBM's brain-dead design, the FDC has a faked ready
1747 * signal, hardwired to ready == true. Thus, any command
1748 * issued if there's no diskette in the drive will _never_
1749 * complete, and must be aborted by resetting the FDC.
1750 * Many thanks, Big Blue!
1751 * The FDC must not be reset directly, since that would
1752 * interfere with the state machine. Instead, pretend that
1753 * the command completed but was invalid. The state machine
1754 * will reset the FDC and retry once.
1757 fdc->status[0] = NE7_ST0_IC_IV;
1758 fdc->flags &= ~FDC_STAT_VALID;
1759 fdc->state = IOTIMEDOUT;
1764 /* Just ensure it has the right spl. */
1766 fd_pseudointr(void *xfdc)
1778 * Keep calling the state machine until it returns a 0.
1779 * Always called at splbio.
1782 fdc_intr(void *xfdc)
1790 * Magic pseudo-DMA initialization for YE FDC. Sets count and
1793 #define SET_BCDR(fdc,wr,cnt,port) \
1794 bus_space_write_1(fdc->portt, fdc->porth, fdc->port_off + port, \
1795 ((cnt)-1) & 0xff); \
1796 bus_space_write_1(fdc->portt, fdc->porth, fdc->port_off + port + 1, \
1797 ((wr ? 0x80 : 0) | ((((cnt)-1) >> 8) & 0x7f)));
1800 * fdcpio(): perform programmed IO read/write for YE PCMCIA floppy.
1803 fdcpio(fdc_p fdc, long flags, caddr_t addr, u_int count)
1805 u_char *cptr = (u_char *)addr;
1807 if (flags == BIO_READ) {
1808 if (fdc->state != PIOREAD) {
1809 fdc->state = PIOREAD;
1812 SET_BCDR(fdc, 0, count, 0);
1813 bus_space_read_multi_1(fdc->portt, fdc->porth, fdc->port_off +
1814 FDC_YE_DATAPORT, cptr, count);
1816 bus_space_write_multi_1(fdc->portt, fdc->porth, fdc->port_off +
1817 FDC_YE_DATAPORT, cptr, count);
1818 SET_BCDR(fdc, 0, count, 0);
1824 * Try figuring out the density of the media present in our device.
1827 fdautoselect(dev_t dev)
1831 struct fd_type *fdtp;
1832 struct fdc_readid id;
1833 int i, n, oopts, rv;
1835 fdu = FDUNIT(minor(dev));
1836 fd = devclass_get_softc(fd_devclass, fdu);
1844 /* no autoselection on those drives */
1845 fd->ft = fd_native_types + fd->type;
1849 fdtp = fd_searchlist_12m;
1850 n = sizeof fd_searchlist_12m / sizeof(struct fd_type);
1854 fdtp = fd_searchlist_144m;
1855 n = sizeof fd_searchlist_144m / sizeof(struct fd_type);
1859 fdtp = fd_searchlist_288m;
1860 n = sizeof fd_searchlist_288m / sizeof(struct fd_type);
1865 * Try reading sector ID fields, first at cylinder 0, head 0,
1866 * then at cylinder 2, head N. We don't probe cylinder 1,
1867 * since for 5.25in DD media in a HD drive, there are no data
1868 * to read (2 step pulses per media cylinder required). For
1869 * two-sided media, the second probe always goes to head 1, so
1870 * we can tell them apart from single-sided media. As a
1871 * side-effect this means that single-sided media should be
1872 * mentioned in the search list after two-sided media of an
1873 * otherwise identical density. Media with a different number
1874 * of sectors per track but otherwise identical parameters
1875 * cannot be distinguished at all.
1877 * If we successfully read an ID field on both cylinders where
1878 * the recorded values match our expectation, we are done.
1879 * Otherwise, we try the next density entry from the table.
1881 * Stepping to cylinder 2 has the side-effect of clearing the
1882 * unit attention bit.
1884 oopts = fd->options;
1885 fd->options |= FDOPT_NOERRLOG | FDOPT_NORETRY;
1886 for (i = 0; i < n; i++, fdtp++) {
1889 id.cyl = id.head = 0;
1890 rv = fdmisccmd(dev, BIO_RDSECTID, &id);
1893 if (id.cyl != 0 || id.head != 0 ||
1894 id.secshift != fdtp->secsize)
1897 id.head = fd->ft->heads - 1;
1898 rv = fdmisccmd(dev, BIO_RDSECTID, &id);
1899 if (id.cyl != 2 || id.head != fdtp->heads - 1 ||
1900 id.secshift != fdtp->secsize)
1906 fd->options = oopts;
1908 device_printf(fd->dev, "autoselection failed\n");
1912 device_printf(fd->dev, "autoselected %d KB medium\n",
1920 * The controller state machine.
1922 * If it returns a non zero value, it should be called again immediately.
1927 struct fdc_readid *idp;
1928 int read, format, rdsectid, cylinder, head, i, sec = 0, sectrac;
1929 int st0, cyl, st3, idf, ne7cmd, mfm, steptrac;
1930 unsigned long blknum;
1931 fdu_t fdu = fdc->fdu;
1933 register struct bio *bp;
1934 struct fd_formb *finfo = NULL;
1939 bp = bioq_first(&fdc->head);
1941 bioq_remove(&fdc->head, bp);
1947 * Nothing left for this controller to do,
1948 * force into the IDLE state.
1950 fdc->state = DEVIDLE;
1952 device_printf(fdc->fdc_dev,
1953 "unexpected valid fd pointer\n");
1957 TRACE1("[fdc%d IDLE]", fdc->fdcu);
1960 fdu = FDUNIT(minor(bp->bio_dev));
1961 fd = devclass_get_softc(fd_devclass, fdu);
1962 fdblk = 128 << fd->ft->secsize;
1963 if (fdc->fd && (fd != fdc->fd))
1964 device_printf(fd->dev, "confused fd pointers\n");
1965 read = bp->bio_cmd == BIO_READ;
1966 mfm = (fd->ft->flags & FL_MFM)? NE7CMD_MFM: 0;
1967 steptrac = (fd->ft->flags & FL_2STEP)? 2: 1;
1972 format = bp->bio_cmd == BIO_FORMAT;
1973 rdsectid = bp->bio_cmd == BIO_RDSECTID;
1975 finfo = (struct fd_formb *)bp->bio_data;
1976 TRACE1("fd%d", fdu);
1977 TRACE1("[%s]", fdstates[fdc->state]);
1978 TRACE1("(0x%x)", fd->flags);
1979 untimeout(fd_turnoff, fd, fd->toffhandle);
1980 fd->toffhandle = timeout(fd_turnoff, fd, 4 * hz);
1984 case FINDWORK: /* we have found new work */
1989 fdc->fdctl_wr(fdc, fd->ft->trans);
1990 TRACE1("[0x%x->FDCTL]", fd->ft->trans);
1992 * If the next drive has a motor startup pending, then
1993 * it will start up in its own good time.
1995 if(fd->flags & FD_MOTOR_WAIT) {
1996 fdc->state = MOTORWAIT;
1997 return (0); /* will return later */
2000 * Maybe if it's not starting, it SHOULD be starting.
2002 if (!(fd->flags & FD_MOTOR))
2004 fdc->state = MOTORWAIT;
2006 return (0); /* will return later */
2008 else /* at least make sure we are selected */
2010 set_motor(fdc, fd->fdsu, TURNON);
2012 if (fdc->flags & FDC_NEEDS_RESET) {
2013 fdc->state = RESETCTLR;
2014 fdc->flags &= ~FDC_NEEDS_RESET;
2016 fdc->state = DOSEEK;
2017 return (1); /* will return immediately */
2020 blknum = bp->bio_pblkno + fd->skip / fdblk;
2021 cylinder = blknum / (fd->ft->sectrac * fd->ft->heads);
2022 if (cylinder == fd->track)
2024 fdc->state = SEEKCOMPLETE;
2025 return (1); /* will return immediately */
2027 if (fd_cmd(fdc, 3, NE7CMD_SEEK,
2028 fd->fdsu, cylinder * steptrac, 0))
2031 * Seek command not accepted, looks like
2032 * the FDC went off to the Saints...
2034 fdc->retry = 6; /* try a reset */
2035 return(retrier(fdc));
2037 fd->track = FD_NO_TRACK;
2038 fdc->state = SEEKWAIT;
2039 return(0); /* will return later */
2042 /* allow heads to settle */
2043 timeout(fd_pseudointr, fdc, hz / 16);
2044 fdc->state = SEEKCOMPLETE;
2045 return(0); /* will return later */
2047 case SEEKCOMPLETE : /* seek done, start DMA */
2048 blknum = bp->bio_pblkno + fd->skip / fdblk;
2049 cylinder = blknum / (fd->ft->sectrac * fd->ft->heads);
2051 /* Make sure seek really happened. */
2052 if(fd->track == FD_NO_TRACK) {
2053 int descyl = cylinder * steptrac;
2056 * This might be a "ready changed" interrupt,
2057 * which cannot really happen since the
2058 * RDY pin is hardwired to + 5 volts. This
2059 * generally indicates a "bouncing" intr
2060 * line, so do one of the following:
2062 * When running on an enhanced FDC that is
2063 * known to not go stuck after responding
2064 * with INVALID, fetch all interrupt states
2065 * until seeing either an INVALID or a
2066 * real interrupt condition.
2068 * When running on a dumb old NE765, give
2069 * up immediately. The controller will
2070 * provide up to four dummy RC interrupt
2071 * conditions right after reset (for the
2072 * corresponding four drives), so this is
2073 * our only chance to get notice that it
2074 * was not the FDC that caused the interrupt.
2076 if (fd_sense_int(fdc, &st0, &cyl)
2078 return (0); /* will return later */
2079 if(fdc->fdct == FDC_NE765
2080 && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
2081 return (0); /* hope for a real intr */
2082 } while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
2087 * seek to cyl 0 requested; make sure we are
2090 if (fd_sense_drive_status(fdc, &st3))
2092 if ((st3 & NE7_ST3_T0) == 0) {
2094 "fd%d: Seek to cyl 0, but not really there (ST3 = %b)\n",
2095 fdu, st3, NE7_ST3BITS);
2102 return (retrier(fdc));
2106 if (cyl != descyl) {
2108 "fd%d: Seek to cyl %d failed; am at cyl %d (ST0 = 0x%x)\n",
2109 fdu, descyl, cyl, st0);
2112 return (retrier(fdc));
2116 fd->track = cylinder;
2118 fd->skip = (char *)&(finfo->fd_formb_cylno(0))
2120 if (!rdsectid && !(fdc->flags & FDC_NODMA))
2121 isa_dmastart(idf, bp->bio_data+fd->skip,
2122 format ? bp->bio_bcount : fdblk, fdc->dmachan);
2123 blknum = bp->bio_pblkno + fd->skip / fdblk;
2124 sectrac = fd->ft->sectrac;
2125 sec = blknum % (sectrac * fd->ft->heads);
2126 head = sec / sectrac;
2127 sec = sec % sectrac + 1;
2128 if (head != 0 && fd->ft->offset_side2 != 0)
2129 sec += fd->ft->offset_side2;
2130 fd->hddrv = ((head&1)<<2)+fdu;
2132 if(format || !(read || rdsectid))
2134 /* make sure the drive is writable */
2135 if(fd_sense_drive_status(fdc, &st3) != 0)
2137 /* stuck controller? */
2138 if (!(fdc->flags & FDC_NODMA))
2140 bp->bio_data + fd->skip,
2141 format ? bp->bio_bcount : fdblk,
2143 fdc->retry = 6; /* reset the beast */
2144 return (retrier(fdc));
2146 if(st3 & NE7_ST3_WP)
2149 * XXX YES! this is ugly.
2150 * in order to force the current operation
2151 * to fail, we will have to fake an FDC
2152 * error - all error handling is done
2155 fdc->status[0] = NE7_ST0_IC_AT;
2156 fdc->status[1] = NE7_ST1_NW;
2158 fdc->status[3] = fd->track;
2159 fdc->status[4] = head;
2160 fdc->status[5] = sec;
2161 fdc->retry = 8; /* break out immediately */
2162 fdc->state = IOTIMEDOUT; /* not really... */
2163 return (1); /* will return immediately */
2168 ne7cmd = NE7CMD_FORMAT | mfm;
2169 if (fdc->flags & FDC_NODMA) {
2171 * This seems to be necessary for
2172 * whatever obscure reason; if we omit
2173 * it, we end up filling the sector ID
2174 * fields of the newly formatted track
2175 * entirely with garbage, causing
2176 * `wrong cylinder' errors all over
2177 * the place when trying to read them
2182 SET_BCDR(fdc, 1, bp->bio_bcount, 0);
2184 (void)fdcpio(fdc,bp->bio_cmd,
2185 bp->bio_data+fd->skip,
2190 if(fd_cmd(fdc, 6, ne7cmd, head << 2 | fdu,
2191 finfo->fd_formb_secshift,
2192 finfo->fd_formb_nsecs,
2193 finfo->fd_formb_gaplen,
2194 finfo->fd_formb_fillbyte, 0)) {
2195 /* controller fell over */
2196 if (!(fdc->flags & FDC_NODMA))
2198 bp->bio_data + fd->skip,
2199 format ? bp->bio_bcount : fdblk,
2202 return (retrier(fdc));
2204 } else if (rdsectid) {
2205 ne7cmd = NE7CMD_READID | mfm;
2206 if (fd_cmd(fdc, 2, ne7cmd, head << 2 | fdu, 0)) {
2207 /* controller jamming */
2209 return (retrier(fdc));
2212 /* read or write operation */
2213 ne7cmd = (read ? NE7CMD_READ | NE7CMD_SK : NE7CMD_WRITE) | mfm;
2214 if (fdc->flags & FDC_NODMA) {
2216 * This seems to be necessary even when
2219 SET_BCDR(fdc, 1, fdblk, 0);
2222 * Perform the write pseudo-DMA before
2223 * the WRITE command is sent.
2226 (void)fdcpio(fdc,bp->bio_cmd,
2227 bp->bio_data+fd->skip,
2232 head << 2 | fdu, /* head & unit */
2233 fd->track, /* track */
2235 sec, /* sector + 1 */
2236 fd->ft->secsize, /* sector size */
2237 sectrac, /* sectors/track */
2238 fd->ft->gap, /* gap size */
2239 fd->ft->datalen, /* data length */
2241 /* the beast is sleeping again */
2242 if (!(fdc->flags & FDC_NODMA))
2244 bp->bio_data + fd->skip,
2245 format ? bp->bio_bcount : fdblk,
2248 return (retrier(fdc));
2251 if (!rdsectid && (fdc->flags & FDC_NODMA))
2253 * If this is a read, then simply await interrupt
2254 * before performing PIO.
2256 if (read && !fdcpio(fdc,bp->bio_cmd,
2257 bp->bio_data+fd->skip,fdblk)) {
2258 fd->tohandle = timeout(fd_iotimeout, fdc, hz);
2259 return(0); /* will return later */
2263 * Write (or format) operation will fall through and
2264 * await completion interrupt.
2266 fdc->state = IOCOMPLETE;
2267 fd->tohandle = timeout(fd_iotimeout, fdc, hz);
2268 return (0); /* will return later */
2272 * Actually perform the PIO read. The IOCOMPLETE case
2273 * removes the timeout for us.
2275 (void)fdcpio(fdc,bp->bio_cmd,bp->bio_data+fd->skip,fdblk);
2276 fdc->state = IOCOMPLETE;
2278 case IOCOMPLETE: /* IO done, post-analyze */
2279 untimeout(fd_iotimeout, fdc, fd->tohandle);
2281 if (fd_read_status(fdc)) {
2282 if (!rdsectid && !(fdc->flags & FDC_NODMA))
2283 isa_dmadone(idf, bp->bio_data + fd->skip,
2284 format ? bp->bio_bcount : fdblk,
2287 fdc->retry = 6; /* force a reset */
2288 return (retrier(fdc));
2291 fdc->state = IOTIMEDOUT;
2295 if (!rdsectid && !(fdc->flags & FDC_NODMA))
2296 isa_dmadone(idf, bp->bio_data + fd->skip,
2297 format ? bp->bio_bcount : fdblk, fdc->dmachan);
2298 if (fdc->status[0] & NE7_ST0_IC) {
2299 if ((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
2300 && fdc->status[1] & NE7_ST1_OR) {
2302 * DMA overrun. Someone hogged the bus and
2303 * didn't release it in time for the next
2306 * We normally restart this without bumping
2307 * the retry counter. However, in case
2308 * something is seriously messed up (like
2309 * broken hardware), we rather limit the
2310 * number of retries so the IO operation
2311 * doesn't block indefinately.
2313 if (fdc->dma_overruns++ < FDC_DMAOV_MAX) {
2314 fdc->state = SEEKCOMPLETE;
2315 return (1);/* will return immediately */
2316 } /* else fall through */
2318 if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_IV
2320 fdc->retry = 6; /* force a reset */
2321 else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
2322 && fdc->status[2] & NE7_ST2_WC
2324 fdc->retry = 3; /* force recalibrate */
2325 return (retrier(fdc));
2329 /* copy out ID field contents */
2330 idp = (struct fdc_readid *)bp->bio_data;
2331 idp->cyl = fdc->status[3];
2332 idp->head = fdc->status[4];
2333 idp->sec = fdc->status[5];
2334 idp->secshift = fdc->status[6];
2336 /* Operation successful, retry DMA overruns again next time. */
2337 fdc->dma_overruns = 0;
2339 if (!rdsectid && !format && fd->skip < bp->bio_bcount) {
2340 /* set up next transfer */
2341 fdc->state = DOSEEK;
2347 device_unbusy(fd->dev);
2348 biofinish(bp, &fd->device_stats, 0);
2351 fdc->state = FINDWORK;
2353 return (1); /* will return immediately */
2358 fdc->state = RESETCOMPLETE;
2359 return (0); /* will return later */
2363 * Discard all the results from the reset so that they
2364 * can't cause an unexpected interrupt later.
2366 for (i = 0; i < 4; i++)
2367 (void)fd_sense_int(fdc, &st0, &cyl);
2368 fdc->state = STARTRECAL;
2371 if(fd_cmd(fdc, 2, NE7CMD_RECAL, fdu, 0)) {
2374 return (retrier(fdc));
2376 fdc->state = RECALWAIT;
2377 return (0); /* will return later */
2380 /* allow heads to settle */
2381 timeout(fd_pseudointr, fdc, hz / 8);
2382 fdc->state = RECALCOMPLETE;
2383 return (0); /* will return later */
2388 * See SEEKCOMPLETE for a comment on this:
2390 if (fd_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
2391 return (0); /* will return later */
2392 if(fdc->fdct == FDC_NE765
2393 && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
2394 return (0); /* hope for a real intr */
2395 } while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
2396 if ((st0 & NE7_ST0_IC) != NE7_ST0_IC_NT || cyl != 0)
2400 * A recalibrate from beyond cylinder 77
2401 * will "fail" due to the FDC limitations;
2402 * since people used to complain much about
2403 * the failure message, try not logging
2404 * this one if it seems to be the first
2407 printf("fd%d: recal failed ST0 %b cyl %d\n",
2408 fdu, st0, NE7_ST0BITS, cyl);
2409 if(fdc->retry < 3) fdc->retry = 3;
2410 return (retrier(fdc));
2413 /* Seek (probably) necessary */
2414 fdc->state = DOSEEK;
2415 return (1); /* will return immediately */
2418 if(fd->flags & FD_MOTOR_WAIT)
2420 return (0); /* time's not up yet */
2422 if (fdc->flags & FDC_NEEDS_RESET) {
2423 fdc->state = RESETCTLR;
2424 fdc->flags &= ~FDC_NEEDS_RESET;
2426 fdc->state = DOSEEK;
2427 return (1); /* will return immediately */
2430 device_printf(fdc->fdc_dev, "unexpected FD int->");
2431 if (fd_read_status(fdc) == 0)
2432 printf("FDC status :%x %x %x %x %x %x %x ",
2441 printf("No status available ");
2442 if (fd_sense_int(fdc, &st0, &cyl) != 0)
2444 printf("[controller is dead now]\n");
2445 return (0); /* will return later */
2447 printf("ST0 = %x, PCN = %x\n", st0, cyl);
2448 return (0); /* will return later */
2450 /* noone should ever get here */
2454 retrier(struct fdc_data *fdc)
2462 /* XXX shouldn't this be cached somewhere? */
2463 fdu = FDUNIT(minor(bp->bio_dev));
2464 fd = devclass_get_softc(fd_devclass, fdu);
2465 if (fd->options & FDOPT_NORETRY)
2468 switch (fdc->retry) {
2469 case 0: case 1: case 2:
2470 fdc->state = SEEKCOMPLETE;
2472 case 3: case 4: case 5:
2473 fdc->state = STARTRECAL;
2476 fdc->state = RESETCTLR;
2482 if ((fd->options & FDOPT_NOERRLOG) == 0) {
2483 diskerr(bp, "hard error", fdc->fd->skip / DEV_BSIZE,
2484 (struct disklabel *)NULL);
2485 if (fdc->flags & FDC_STAT_VALID) {
2487 " (ST0 %b ST1 %b ST2 %b cyl %u hd %u sec %u)\n",
2488 fdc->status[0], NE7_ST0BITS,
2489 fdc->status[1], NE7_ST1BITS,
2490 fdc->status[2], NE7_ST2BITS,
2491 fdc->status[3], fdc->status[4],
2495 printf(" (No status)\n");
2497 if ((fd->options & FDOPT_NOERROR) == 0) {
2498 bp->bio_flags |= BIO_ERROR;
2499 bp->bio_error = EIO;
2500 bp->bio_resid = bp->bio_bcount - fdc->fd->skip;
2505 device_unbusy(fd->dev);
2506 biofinish(bp, &fdc->fd->device_stats, 0);
2507 fdc->state = FINDWORK;
2508 fdc->flags |= FDC_NEEDS_RESET;
2518 fdbiodone(struct bio *bp)
2524 fdmisccmd(dev_t dev, u_int cmd, void *data)
2529 struct fd_formb *finfo;
2530 struct fdc_readid *idfield;
2533 fdu = FDUNIT(minor(dev));
2534 fd = devclass_get_softc(fd_devclass, fdu);
2535 fdblk = 128 << fd->ft->secsize;
2536 finfo = (struct fd_formb *)data;
2537 idfield = (struct fdc_readid *)data;
2539 bp = malloc(sizeof(struct bio), M_TEMP, M_ZERO);
2542 * Set up a bio request for fdstrategy(). bio_blkno is faked
2543 * so that fdstrategy() will seek to the the requested
2544 * cylinder, and use the desired head.
2547 if (cmd == BIO_FORMAT) {
2549 (finfo->cyl * (fd->ft->sectrac * fd->ft->heads) +
2550 finfo->head * fd->ft->sectrac) *
2552 bp->bio_bcount = sizeof(struct fd_idfield_data) *
2553 finfo->fd_formb_nsecs;
2554 } else if (cmd == BIO_RDSECTID) {
2556 (idfield->cyl * (fd->ft->sectrac * fd->ft->heads) +
2557 idfield->head * fd->ft->sectrac) *
2559 bp->bio_bcount = sizeof(struct fdc_readid);
2561 panic("wrong cmd in fdmisccmd()");
2562 bp->bio_data = data;
2564 bp->bio_done = fdbiodone;
2568 * Now run the command. The wait loop is a version of bufwait()
2569 * adapted for struct bio instead of struct buf and specialized
2570 * for the current context.
2573 while ((bp->bio_flags & BIO_DONE) == 0)
2574 tsleep(bp, PRIBIO, "fdcmd", 0);
2577 return (bp->bio_flags & BIO_ERROR ? bp->bio_error : 0);
2581 fdioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
2585 struct fd_type *fdt;
2586 struct disklabel *lp;
2587 struct fdc_status *fsp;
2588 struct fdc_readid *rid;
2592 fdu = FDUNIT(minor(dev));
2593 type = FDTYPE(minor(dev));
2594 fd = devclass_get_softc(fd_devclass, fdu);
2597 * First, handle everything that could be done with
2598 * FD_NONBLOCK still being set.
2602 if (*(int *)addr != 0)
2603 fd->flags |= FD_NONBLOCK;
2607 * No drive type has been selected yet,
2608 * cannot turn FNONBLOCK off.
2612 fd->flags &= ~FD_NONBLOCK;
2617 /* keep the generic fcntl() code happy */
2620 case FD_GTYPE: /* get drive type */
2622 /* no type known yet, return the native type */
2623 *(struct fd_type *)addr = fd_native_types[fd->type];
2625 *(struct fd_type *)addr = *fd->ft;
2628 case FD_STYPE: /* set drive type */
2631 * Allow setting drive type temporarily iff
2632 * currently unset. Used for fdformat so any
2633 * user can set it, and then start formatting.
2636 return (EINVAL); /* already set */
2638 *fd->ft = *(struct fd_type *)addr;
2642 * Set density definition permanently. Only
2643 * allow for superuser.
2645 if (suser_td(td) != 0)
2647 fd->fts[type] = *(struct fd_type *)addr;
2651 case FD_GOPTS: /* get drive options */
2652 *(int *)addr = fd->options + (type == 0? FDOPT_AUTOSEL: 0);
2655 case FD_SOPTS: /* set drive options */
2656 fd->options = *(int *)addr & ~FDOPT_AUTOSEL;
2661 if ((fd_debug != 0) != (*(int *)addr != 0)) {
2662 fd_debug = (*(int *)addr != 0);
2663 printf("fd%d: debugging turned %s\n",
2664 fd->fdu, fd_debug ? "on" : "off");
2670 if (suser_td(td) != 0)
2672 fd->fdc->fdc_errs = 0;
2676 fsp = (struct fdc_status *)addr;
2677 if ((fd->fdc->flags & FDC_STAT_VALID) == 0)
2679 memcpy(fsp->status, fd->fdc->status, 7 * sizeof(u_int));
2683 *(enum fd_drivetype *)addr = fd->type;
2688 * Now handle everything else. Make sure we have a valid
2691 if (fd->flags & FD_NONBLOCK)
2695 fdblk = 128 << fd->ft->secsize;
2700 lp = malloc(sizeof(*lp), M_TEMP, M_ZERO);
2701 lp->d_secsize = fdblk;
2703 lp->d_secpercyl = fdt->size / fdt->tracks;
2704 lp->d_type = DTYPE_FLOPPY;
2705 if (readdisklabel(dkmodpart(dev, RAW_PART), lp) != NULL)
2708 *(struct disklabel *)addr = *lp;
2713 if ((flag & FWRITE) == 0)
2716 * XXX perhaps should call setdisklabel() to do error checking
2717 * although there is nowhere to "set" the result. Perhaps
2718 * should always just fail.
2723 if ((flag & FWRITE) == 0)
2728 if ((flag & FWRITE) == 0)
2730 lp = malloc(DEV_BSIZE, M_TEMP, M_ZERO);
2731 error = setdisklabel(lp, (struct disklabel *)addr, (u_long)0);
2733 error = writedisklabel(dev, lp);
2738 if ((flag & FWRITE) == 0)
2739 return (EBADF); /* must be opened for writing */
2740 if (((struct fd_formb *)addr)->format_version !=
2742 return (EINVAL); /* wrong version of formatting prog */
2743 error = fdmisccmd(dev, BIO_FORMAT, addr);
2746 case FD_GTYPE: /* get drive type */
2747 *(struct fd_type *)addr = *fd->ft;
2750 case FD_STYPE: /* set drive type */
2751 /* this is considered harmful; only allow for superuser */
2752 if (suser_td(td) != 0)
2754 *fd->ft = *(struct fd_type *)addr;
2757 case FD_GOPTS: /* get drive options */
2758 *(int *)addr = fd->options;
2761 case FD_SOPTS: /* set drive options */
2762 fd->options = *(int *)addr;
2767 if ((fd_debug != 0) != (*(int *)addr != 0)) {
2768 fd_debug = (*(int *)addr != 0);
2769 printf("fd%d: debugging turned %s\n",
2770 fd->fdu, fd_debug ? "on" : "off");
2776 if (suser_td(td) != 0)
2778 fd->fdc->fdc_errs = 0;
2782 fsp = (struct fdc_status *)addr;
2783 if ((fd->fdc->flags & FDC_STAT_VALID) == 0)
2785 memcpy(fsp->status, fd->fdc->status, 7 * sizeof(u_int));
2789 rid = (struct fdc_readid *)addr;
2790 if (rid->cyl > MAX_CYLINDER || rid->head > MAX_HEAD)
2792 error = fdmisccmd(dev, BIO_RDSECTID, addr);