1 /* 2 * Copyright (c) 1990 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Don Ahn. 7 * 8 * Libretto PCMCIA floppy support by David Horwitt (dhorwitt@ucsd.edu) 9 * aided by the Linux floppy driver modifications from David Bateman 10 * (dbateman@eng.uts.edu.au). 11 * 12 * Copyright (c) 1993, 1994 by 13 * jc@irbs.UUCP (John Capo) 14 * vak@zebub.msk.su (Serge Vakulenko) 15 * ache@astral.msk.su (Andrew A. Chernov) 16 * 17 * Copyright (c) 1993, 1994, 1995 by 18 * joerg_wunsch@uriah.sax.de (Joerg Wunsch) 19 * dufault@hda.com (Peter Dufault) 20 * 21 * Copyright (c) 2001 Joerg Wunsch, 22 * joerg_wunsch@uriah.heep.sax.de (Joerg Wunsch) 23 * 24 * Redistribution and use in source and binary forms, with or without 25 * modification, are permitted provided that the following conditions 26 * are met: 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 * 4. Neither the name of the University nor the names of its contributors 33 * may be used to endorse or promote products derived from this software 34 * without specific prior written permission. 35 * 36 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 37 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 39 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 40 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 41 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 42 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 44 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 45 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 46 * SUCH DAMAGE. 47 * 48 * from: @(#)fd.c 7.4 (Berkeley) 5/25/91 49 */ 50 51 #include <sys/cdefs.h> 52 __FBSDID("$FreeBSD$"); 53 54 #include "opt_fdc.h" 55 #include "card.h" 56 57 #include <sys/param.h> 58 #include <sys/systm.h> 59 #include <sys/bio.h> 60 #include <sys/bus.h> 61 #include <sys/conf.h> 62 #include <sys/devicestat.h> 63 #include <sys/disk.h> 64 #include <sys/fcntl.h> 65 #include <sys/fdcio.h> 66 #include <sys/filio.h> 67 #include <sys/kernel.h> 68 #include <sys/lock.h> 69 #include <sys/malloc.h> 70 #include <sys/module.h> 71 #include <sys/mutex.h> 72 #include <sys/proc.h> 73 #include <sys/syslog.h> 74 75 #include <machine/bus.h> 76 #include <sys/rman.h> 77 78 #include <machine/clock.h> 79 #include <machine/resource.h> 80 #include <machine/stdarg.h> 81 82 #include <isa/isavar.h> 83 #include <isa/isareg.h> 84 #include <dev/fdc/fdcreg.h> 85 #include <isa/rtc.h> 86 87 enum fdc_type 88 { 89 FDC_NE765, FDC_ENHANCED, FDC_UNKNOWN = -1 90 }; 91 92 enum fdc_states { 93 DEVIDLE, 94 FINDWORK, 95 DOSEEK, 96 SEEKCOMPLETE , 97 IOCOMPLETE, 98 RECALCOMPLETE, 99 STARTRECAL, 100 RESETCTLR, 101 SEEKWAIT, 102 RECALWAIT, 103 MOTORWAIT, 104 IOTIMEDOUT, 105 RESETCOMPLETE, 106 PIOREAD 107 }; 108 109 #ifdef FDC_DEBUG 110 static char const * const fdstates[] = { 111 "DEVIDLE", 112 "FINDWORK", 113 "DOSEEK", 114 "SEEKCOMPLETE", 115 "IOCOMPLETE", 116 "RECALCOMPLETE", 117 "STARTRECAL", 118 "RESETCTLR", 119 "SEEKWAIT", 120 "RECALWAIT", 121 "MOTORWAIT", 122 "IOTIMEDOUT", 123 "RESETCOMPLETE", 124 "PIOREAD" 125 }; 126 #endif 127 128 /* 129 * Per controller structure (softc). 130 */ 131 struct fdc_data 132 { 133 int fdcu; /* our unit number */ 134 int dmachan; 135 int dmacnt; 136 int flags; 137 #define FDC_ATTACHED 0x01 138 #define FDC_STAT_VALID 0x08 139 #define FDC_HAS_FIFO 0x10 140 #define FDC_NEEDS_RESET 0x20 141 #define FDC_NODMA 0x40 142 #define FDC_ISPNP 0x80 143 #define FDC_ISPCMCIA 0x100 144 struct fd_data *fd; 145 int fdu; /* the active drive */ 146 enum fdc_states state; 147 int retry; 148 int fdout; /* mirror of the w/o digital output reg */ 149 u_int status[7]; /* copy of the registers */ 150 enum fdc_type fdct; /* chip version of FDC */ 151 int fdc_errs; /* number of logged errors */ 152 int dma_overruns; /* number of DMA overruns */ 153 struct bio_queue_head head; 154 struct bio *bp; /* active buffer */ 155 struct resource *res_ioport, *res_ctl, *res_irq, *res_drq; 156 int rid_ioport, rid_ctl, rid_irq, rid_drq; 157 int port_off; 158 bus_space_tag_t portt; 159 bus_space_handle_t porth; 160 bus_space_tag_t ctlt; 161 bus_space_handle_t ctlh; 162 void *fdc_intr; 163 struct device *fdc_dev; 164 void (*fdctl_wr)(struct fdc_data *fdc, u_int8_t v); 165 }; 166 167 #define FDBIO_FORMAT BIO_CMD2 168 169 typedef int fdu_t; 170 typedef int fdcu_t; 171 typedef int fdsu_t; 172 typedef struct fd_data *fd_p; 173 typedef struct fdc_data *fdc_p; 174 typedef enum fdc_type fdc_t; 175 176 /* 177 * fdc maintains a set (1!) of ivars per child of each controller. 178 */ 179 enum fdc_device_ivars { 180 FDC_IVAR_FDUNIT, 181 }; 182 183 /* 184 * Simple access macros for the ivars. 185 */ 186 #define FDC_ACCESSOR(A, B, T) \ 187 static __inline T fdc_get_ ## A(device_t dev) \ 188 { \ 189 uintptr_t v; \ 190 BUS_READ_IVAR(device_get_parent(dev), dev, FDC_IVAR_ ## B, &v); \ 191 return (T) v; \ 192 } 193 FDC_ACCESSOR(fdunit, FDUNIT, int) 194 195 /* configuration flags for fdc */ 196 #define FDC_NO_FIFO (1 << 2) /* do not enable FIFO */ 197 198 /* error returns for fd_cmd() */ 199 #define FD_FAILED -1 200 #define FD_NOT_VALID -2 201 #define FDC_ERRMAX 100 /* do not log more */ 202 /* 203 * Stop retrying after this many DMA overruns. Since each retry takes 204 * one revolution, with 300 rpm., 25 retries take approximately 5 205 * seconds which the read attempt will block in case the DMA overrun 206 * is persistent. 207 */ 208 #define FDC_DMAOV_MAX 25 209 210 /* 211 * Timeout value for the PIO loops to wait until the FDC main status 212 * register matches our expectations (request for master, direction 213 * bit). This is supposed to be a number of microseconds, although 214 * timing might actually not be very accurate. 215 * 216 * Timeouts of 100 msec are believed to be required for some broken 217 * (old) hardware. 218 */ 219 #define FDSTS_TIMEOUT 100000 220 221 /* 222 * Number of subdevices that can be used for different density types. 223 */ 224 #define NUMDENS 16 225 226 #define FDBIO_RDSECTID BIO_CMD1 227 228 /* 229 * List of native drive densities. Order must match enum fd_drivetype 230 * in <sys/fdcio.h>. Upon attaching the drive, each of the 231 * programmable subdevices is initialized with the native density 232 * definition. 233 */ 234 static struct fd_type fd_native_types[] = 235 { 236 { 0 }, /* FDT_NONE */ 237 { 9,2,0xFF,0x2A,40, 720,FDC_250KBPS,2,0x50,1,0,FL_MFM }, /* FDT_360K */ 238 { 15,2,0xFF,0x1B,80,2400,FDC_500KBPS,2,0x54,1,0,FL_MFM }, /* FDT_12M */ 239 { 9,2,0xFF,0x20,80,1440,FDC_250KBPS,2,0x50,1,0,FL_MFM }, /* FDT_720K */ 240 { 18,2,0xFF,0x1B,80,2880,FDC_500KBPS,2,0x6C,1,0,FL_MFM }, /* FDT_144M */ 241 #if 0 /* we currently don't handle 2.88 MB */ 242 { 36,2,0xFF,0x1B,80,5760,FDC_1MBPS, 2,0x4C,1,1,FL_MFM|FL_PERPND } /*FDT_288M*/ 243 #else 244 { 18,2,0xFF,0x1B,80,2880,FDC_500KBPS,2,0x6C,1,0,FL_MFM }, /* FDT_144M */ 245 #endif 246 }; 247 248 /* 249 * 360 KB 5.25" and 720 KB 3.5" drives don't have automatic density 250 * selection, they just start out with their native density (or lose). 251 * So 1.2 MB 5.25", 1.44 MB 3.5", and 2.88 MB 3.5" drives have their 252 * respective lists of densities to search for. 253 */ 254 static struct fd_type fd_searchlist_12m[] = { 255 { 15,2,0xFF,0x1B,80,2400,FDC_500KBPS,2,0x54,1,0,FL_MFM }, /* 1.2M */ 256 { 9,2,0xFF,0x23,40, 720,FDC_300KBPS,2,0x50,1,0,FL_MFM|FL_2STEP }, /* 360K */ 257 { 9,2,0xFF,0x20,80,1440,FDC_300KBPS,2,0x50,1,0,FL_MFM }, /* 720K */ 258 }; 259 260 static struct fd_type fd_searchlist_144m[] = { 261 { 18,2,0xFF,0x1B,80,2880,FDC_500KBPS,2,0x6C,1,0,FL_MFM }, /* 1.44M */ 262 { 9,2,0xFF,0x20,80,1440,FDC_250KBPS,2,0x50,1,0,FL_MFM }, /* 720K */ 263 }; 264 265 /* We search for 1.44M first since this is the most common case. */ 266 static struct fd_type fd_searchlist_288m[] = { 267 { 18,2,0xFF,0x1B,80,2880,FDC_500KBPS,2,0x6C,1,0,FL_MFM }, /* 1.44M */ 268 #if 0 269 { 36,2,0xFF,0x1B,80,5760,FDC_1MBPS, 2,0x4C,1,1,FL_MFM|FL_PERPND } /* 2.88M */ 270 #endif 271 { 9,2,0xFF,0x20,80,1440,FDC_250KBPS,2,0x50,1,0,FL_MFM }, /* 720K */ 272 }; 273 274 #define MAX_SEC_SIZE (128 << 3) 275 #define MAX_CYLINDER 85 /* some people really stress their drives 276 * up to cyl 82 */ 277 #define MAX_HEAD 1 278 279 static devclass_t fdc_devclass; 280 281 /* 282 * Per drive structure (softc). 283 */ 284 struct fd_data { 285 struct fdc_data *fdc; /* pointer to controller structure */ 286 int fdsu; /* this units number on this controller */ 287 enum fd_drivetype type; /* drive type */ 288 struct fd_type *ft; /* pointer to current type descriptor */ 289 struct fd_type fts[NUMDENS]; /* type descriptors */ 290 int flags; 291 #define FD_OPEN 0x01 /* it's open */ 292 #define FD_NONBLOCK 0x02 /* O_NONBLOCK set */ 293 #define FD_ACTIVE 0x04 /* it's active */ 294 #define FD_MOTOR 0x08 /* motor should be on */ 295 #define FD_MOTOR_WAIT 0x10 /* motor coming up */ 296 #define FD_UA 0x20 /* force unit attention */ 297 int skip; 298 int hddrv; 299 #define FD_NO_TRACK -2 300 int track; /* where we think the head is */ 301 int options; /* user configurable options, see fdcio.h */ 302 struct callout_handle toffhandle; 303 struct callout_handle tohandle; 304 struct devstat *device_stats; 305 struct cdev *masterdev; 306 device_t dev; 307 fdu_t fdu; 308 }; 309 310 struct fdc_ivars { 311 int fdunit; 312 }; 313 static devclass_t fd_devclass; 314 315 /* configuration flags for fd */ 316 #define FD_TYPEMASK 0x0f /* drive type, matches enum 317 * fd_drivetype; on i386 machines, if 318 * given as 0, use RTC type for fd0 319 * and fd1 */ 320 #define FD_DTYPE(flags) ((flags) & FD_TYPEMASK) 321 #define FD_NO_CHLINE 0x10 /* drive does not support changeline 322 * aka. unit attention */ 323 #define FD_NO_PROBE 0x20 /* don't probe drive (seek test), just 324 * assume it is there */ 325 326 /* 327 * Throughout this file the following conventions will be used: 328 * 329 * fd is a pointer to the fd_data struct for the drive in question 330 * fdc is a pointer to the fdc_data struct for the controller 331 * fdu is the floppy drive unit number 332 * fdcu is the floppy controller unit number 333 * fdsu is the floppy drive unit number on that controller. (sub-unit) 334 */ 335 336 /* 337 * Function declarations, same (chaotic) order as they appear in the 338 * file. Re-ordering is too late now, it would only obfuscate the 339 * diffs against old and offspring versions (like the PC98 one). 340 * 341 * Anyone adding functions here, please keep this sequence the same 342 * as below -- makes locating a particular function in the body much 343 * easier. 344 */ 345 static void fdout_wr(fdc_p, u_int8_t); 346 static u_int8_t fdsts_rd(fdc_p); 347 static void fddata_wr(fdc_p, u_int8_t); 348 static u_int8_t fddata_rd(fdc_p); 349 static void fdctl_wr_isa(fdc_p, u_int8_t); 350 #if NCARD > 0 351 static void fdctl_wr_pcmcia(fdc_p, u_int8_t); 352 #endif 353 #if 0 354 static u_int8_t fdin_rd(fdc_p); 355 #endif 356 static int fdc_err(struct fdc_data *, const char *); 357 static int fd_cmd(struct fdc_data *, int, ...); 358 static int enable_fifo(fdc_p fdc); 359 static int fd_sense_drive_status(fdc_p, int *); 360 static int fd_sense_int(fdc_p, int *, int *); 361 static int fd_read_status(fdc_p); 362 static int fdc_alloc_resources(struct fdc_data *); 363 static void fdc_release_resources(struct fdc_data *); 364 static int fdc_read_ivar(device_t, device_t, int, uintptr_t *); 365 static int fdc_probe(device_t); 366 #if NCARD > 0 367 static int fdc_pccard_probe(device_t); 368 #endif 369 static int fdc_detach(device_t dev); 370 static void fdc_add_child(device_t, const char *, int); 371 static int fdc_attach(device_t); 372 static int fdc_print_child(device_t, device_t); 373 static int fd_probe(device_t); 374 static int fd_attach(device_t); 375 static int fd_detach(device_t); 376 static void set_motor(struct fdc_data *, int, int); 377 # define TURNON 1 378 # define TURNOFF 0 379 static timeout_t fd_turnoff; 380 static timeout_t fd_motor_on; 381 static void fd_turnon(struct fd_data *); 382 static void fdc_reset(fdc_p); 383 static int fd_in(struct fdc_data *, int *); 384 static int out_fdc(struct fdc_data *, int); 385 /* 386 * The open function is named fdopen() to avoid confusion with fdopen() 387 * in fd(4). The difference is now only meaningful for debuggers. 388 */ 389 static d_open_t fdopen; 390 static d_close_t fdclose; 391 static d_strategy_t fdstrategy; 392 static void fdstart(struct fdc_data *); 393 static timeout_t fd_iotimeout; 394 static timeout_t fd_pseudointr; 395 static driver_intr_t fdc_intr; 396 static int fdcpio(fdc_p, long, caddr_t, u_int); 397 static int fdautoselect(struct cdev *); 398 static int fdstate(struct fdc_data *); 399 static int retrier(struct fdc_data *); 400 static void fdbiodone(struct bio *); 401 static int fdmisccmd(struct cdev *, u_int, void *); 402 static d_ioctl_t fdioctl; 403 404 static int fifo_threshold = 8; /* XXX: should be accessible via sysctl */ 405 406 #ifdef FDC_DEBUG 407 /* CAUTION: fd_debug causes huge amounts of logging output */ 408 static int volatile fd_debug = 0; 409 #define TRACE0(arg) do { if (fd_debug) printf(arg); } while (0) 410 #define TRACE1(arg1, arg2) do { if (fd_debug) printf(arg1, arg2); } while (0) 411 #else /* FDC_DEBUG */ 412 #define TRACE0(arg) do { } while (0) 413 #define TRACE1(arg1, arg2) do { } while (0) 414 #endif /* FDC_DEBUG */ 415 416 /* 417 * Bus space handling (access to low-level IO). 418 */ 419 static void 420 fdout_wr(fdc_p fdc, u_int8_t v) 421 { 422 bus_space_write_1(fdc->portt, fdc->porth, FDOUT+fdc->port_off, v); 423 } 424 425 static u_int8_t 426 fdsts_rd(fdc_p fdc) 427 { 428 return bus_space_read_1(fdc->portt, fdc->porth, FDSTS+fdc->port_off); 429 } 430 431 static void 432 fddata_wr(fdc_p fdc, u_int8_t v) 433 { 434 bus_space_write_1(fdc->portt, fdc->porth, FDDATA+fdc->port_off, v); 435 } 436 437 static u_int8_t 438 fddata_rd(fdc_p fdc) 439 { 440 return bus_space_read_1(fdc->portt, fdc->porth, FDDATA+fdc->port_off); 441 } 442 443 static void 444 fdctl_wr_isa(fdc_p fdc, u_int8_t v) 445 { 446 bus_space_write_1(fdc->ctlt, fdc->ctlh, 0, v); 447 } 448 449 #if NCARD > 0 450 static void 451 fdctl_wr_pcmcia(fdc_p fdc, u_int8_t v) 452 { 453 bus_space_write_1(fdc->portt, fdc->porth, FDCTL+fdc->port_off, v); 454 } 455 #endif 456 457 static u_int8_t 458 fdin_rd(fdc_p fdc) 459 { 460 return bus_space_read_1(fdc->portt, fdc->porth, FDIN); 461 } 462 463 static struct cdevsw fd_cdevsw = { 464 .d_version = D_VERSION, 465 .d_open = fdopen, 466 .d_close = fdclose, 467 .d_read = physread, 468 .d_write = physwrite, 469 .d_ioctl = fdioctl, 470 .d_strategy = fdstrategy, 471 .d_name = "fd", 472 .d_flags = D_DISK | D_NEEDGIANT, 473 }; 474 475 /* 476 * Auxiliary functions. Well, some only. Others are scattered 477 * throughout the entire file. 478 */ 479 static int 480 fdc_err(struct fdc_data *fdc, const char *s) 481 { 482 fdc->fdc_errs++; 483 if (s) { 484 if (fdc->fdc_errs < FDC_ERRMAX) 485 device_printf(fdc->fdc_dev, "%s", s); 486 else if (fdc->fdc_errs == FDC_ERRMAX) 487 device_printf(fdc->fdc_dev, "too many errors, not " 488 "logging any more\n"); 489 } 490 491 return FD_FAILED; 492 } 493 494 /* 495 * fd_cmd: Send a command to the chip. Takes a varargs with this structure: 496 * Unit number, 497 * # of output bytes, output bytes as ints ..., 498 * # of input bytes, input bytes as ints ... 499 */ 500 static int 501 fd_cmd(struct fdc_data *fdc, int n_out, ...) 502 { 503 u_char cmd; 504 int n_in; 505 int n; 506 va_list ap; 507 508 va_start(ap, n_out); 509 cmd = (u_char)(va_arg(ap, int)); 510 va_end(ap); 511 va_start(ap, n_out); 512 for (n = 0; n < n_out; n++) 513 { 514 if (out_fdc(fdc, va_arg(ap, int)) < 0) 515 { 516 char msg[50]; 517 snprintf(msg, sizeof(msg), 518 "cmd %x failed at out byte %d of %d\n", 519 cmd, n + 1, n_out); 520 return fdc_err(fdc, msg); 521 } 522 } 523 n_in = va_arg(ap, int); 524 for (n = 0; n < n_in; n++) 525 { 526 int *ptr = va_arg(ap, int *); 527 if (fd_in(fdc, ptr) < 0) 528 { 529 char msg[50]; 530 snprintf(msg, sizeof(msg), 531 "cmd %02x failed at in byte %d of %d\n", 532 cmd, n + 1, n_in); 533 return fdc_err(fdc, msg); 534 } 535 } 536 537 return 0; 538 } 539 540 static int 541 enable_fifo(fdc_p fdc) 542 { 543 int i, j; 544 545 if ((fdc->flags & FDC_HAS_FIFO) == 0) { 546 547 /* 548 * Cannot use fd_cmd the normal way here, since 549 * this might be an invalid command. Thus we send the 550 * first byte, and check for an early turn of data directon. 551 */ 552 553 if (out_fdc(fdc, I8207X_CONFIGURE) < 0) 554 return fdc_err(fdc, "Enable FIFO failed\n"); 555 556 /* If command is invalid, return */ 557 j = FDSTS_TIMEOUT; 558 while ((i = fdsts_rd(fdc) & (NE7_DIO | NE7_RQM)) 559 != NE7_RQM && j-- > 0) { 560 if (i == (NE7_DIO | NE7_RQM)) { 561 fdc_reset(fdc); 562 return FD_FAILED; 563 } 564 DELAY(1); 565 } 566 if (j<0 || 567 fd_cmd(fdc, 3, 568 0, (fifo_threshold - 1) & 0xf, 0, 0) < 0) { 569 fdc_reset(fdc); 570 return fdc_err(fdc, "Enable FIFO failed\n"); 571 } 572 fdc->flags |= FDC_HAS_FIFO; 573 return 0; 574 } 575 if (fd_cmd(fdc, 4, 576 I8207X_CONFIGURE, 0, (fifo_threshold - 1) & 0xf, 0, 0) < 0) 577 return fdc_err(fdc, "Re-enable FIFO failed\n"); 578 return 0; 579 } 580 581 static int 582 fd_sense_drive_status(fdc_p fdc, int *st3p) 583 { 584 int st3; 585 586 if (fd_cmd(fdc, 2, NE7CMD_SENSED, fdc->fdu, 1, &st3)) 587 { 588 return fdc_err(fdc, "Sense Drive Status failed\n"); 589 } 590 if (st3p) 591 *st3p = st3; 592 593 return 0; 594 } 595 596 static int 597 fd_sense_int(fdc_p fdc, int *st0p, int *cylp) 598 { 599 int cyl, st0, ret; 600 601 ret = fd_cmd(fdc, 1, NE7CMD_SENSEI, 1, &st0); 602 if (ret) { 603 (void)fdc_err(fdc, 604 "sense intr err reading stat reg 0\n"); 605 return ret; 606 } 607 608 if (st0p) 609 *st0p = st0; 610 611 if ((st0 & NE7_ST0_IC) == NE7_ST0_IC_IV) { 612 /* 613 * There doesn't seem to have been an interrupt. 614 */ 615 return FD_NOT_VALID; 616 } 617 618 if (fd_in(fdc, &cyl) < 0) { 619 return fdc_err(fdc, "can't get cyl num\n"); 620 } 621 622 if (cylp) 623 *cylp = cyl; 624 625 return 0; 626 } 627 628 629 static int 630 fd_read_status(fdc_p fdc) 631 { 632 int i, ret; 633 634 for (i = ret = 0; i < 7; i++) { 635 /* 636 * XXX types are poorly chosen. Only bytes can be read 637 * from the hardware, but fdc->status[] wants u_ints and 638 * fd_in() gives ints. 639 */ 640 int status; 641 642 ret = fd_in(fdc, &status); 643 fdc->status[i] = status; 644 if (ret != 0) 645 break; 646 } 647 648 if (ret == 0) 649 fdc->flags |= FDC_STAT_VALID; 650 else 651 fdc->flags &= ~FDC_STAT_VALID; 652 653 return ret; 654 } 655 656 static int 657 fdc_alloc_resources(struct fdc_data *fdc) 658 { 659 device_t dev; 660 int ispnp, ispcmcia, nports; 661 662 dev = fdc->fdc_dev; 663 ispnp = (fdc->flags & FDC_ISPNP) != 0; 664 ispcmcia = (fdc->flags & FDC_ISPCMCIA) != 0; 665 fdc->rid_ioport = fdc->rid_irq = fdc->rid_drq = 0; 666 fdc->res_ioport = fdc->res_irq = fdc->res_drq = 0; 667 fdc->rid_ctl = 1; 668 669 /* 670 * On standard ISA, we don't just use an 8 port range 671 * (e.g. 0x3f0-0x3f7) since that covers an IDE control 672 * register at 0x3f6. 673 * 674 * Isn't PC hardware wonderful. 675 * 676 * The Y-E Data PCMCIA FDC doesn't have this problem, it 677 * uses the register with offset 6 for pseudo-DMA, and the 678 * one with offset 7 as control register. 679 */ 680 nports = ispcmcia ? 8 : (ispnp ? 1 : 6); 681 682 /* 683 * Some ACPI BIOSen have _CRS objects for the floppy device that 684 * split the I/O port resource into several resources. We detect 685 * this case by checking if there are more than 2 IOPORT resources. 686 * If so, we use the resource with the smallest start address as 687 * the port RID and the largest start address as the control RID. 688 */ 689 if (bus_get_resource_count(dev, SYS_RES_IOPORT, 2) != 0) { 690 u_long min_start, max_start, tmp; 691 int i; 692 693 /* Find the min/max start addresses and their RIDs. */ 694 max_start = 0ul; 695 min_start = ~0ul; 696 for (i = 0; bus_get_resource_count(dev, SYS_RES_IOPORT, i) > 0; 697 i++) { 698 tmp = bus_get_resource_start(dev, SYS_RES_IOPORT, i); 699 KASSERT(tmp != 0, ("bogus resource")); 700 if (tmp < min_start) { 701 min_start = tmp; 702 fdc->rid_ioport = i; 703 } 704 if (tmp > max_start) { 705 max_start = tmp; 706 fdc->rid_ctl = i; 707 } 708 } 709 if (min_start + 7 != max_start) { 710 device_printf(dev, "I/O to control range incorrect\n"); 711 return (ENXIO); 712 } 713 } 714 715 fdc->res_ioport = bus_alloc_resource(dev, SYS_RES_IOPORT, 716 &fdc->rid_ioport, 0ul, ~0ul, 717 nports, RF_ACTIVE); 718 if (fdc->res_ioport == 0) { 719 device_printf(dev, "cannot reserve I/O port range (%d ports)\n", 720 nports); 721 return ENXIO; 722 } 723 fdc->portt = rman_get_bustag(fdc->res_ioport); 724 fdc->porth = rman_get_bushandle(fdc->res_ioport); 725 726 if (!ispcmcia) { 727 /* 728 * Some BIOSen report the device at 0x3f2-0x3f5,0x3f7 729 * and some at 0x3f0-0x3f5,0x3f7. We detect the former 730 * by checking the size and adjust the port address 731 * accordingly. 732 */ 733 if (bus_get_resource_count(dev, SYS_RES_IOPORT, 0) == 4) 734 fdc->port_off = -2; 735 736 /* 737 * Register the control port range as rid 1 if it 738 * isn't there already. Most PnP BIOSen will have 739 * already done this but non-PnP configurations don't. 740 * 741 * And some (!!) report 0x3f2-0x3f5 and completely 742 * leave out the control register! It seems that some 743 * non-antique controller chips have a different 744 * method of programming the transfer speed which 745 * doesn't require the control register, but it's 746 * mighty bogus as the chip still responds to the 747 * address for the control register. 748 */ 749 if (bus_get_resource_count(dev, SYS_RES_IOPORT, 1) == 0) { 750 u_long ctlstart; 751 752 /* Find the control port, usually 0x3f7 */ 753 ctlstart = rman_get_start(fdc->res_ioport) + 754 fdc->port_off + 7; 755 756 bus_set_resource(dev, SYS_RES_IOPORT, 1, ctlstart, 1); 757 } 758 759 /* 760 * Now (finally!) allocate the control port. 761 */ 762 fdc->res_ctl = bus_alloc_resource_any(dev, SYS_RES_IOPORT, 763 &fdc->rid_ctl, RF_ACTIVE); 764 if (fdc->res_ctl == 0) { 765 device_printf(dev, 766 "cannot reserve control I/O port range (control port)\n"); 767 return ENXIO; 768 } 769 fdc->ctlt = rman_get_bustag(fdc->res_ctl); 770 fdc->ctlh = rman_get_bushandle(fdc->res_ctl); 771 } 772 773 fdc->res_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, 774 &fdc->rid_irq, 775 RF_ACTIVE | RF_SHAREABLE); 776 if (fdc->res_irq == 0) { 777 device_printf(dev, "cannot reserve interrupt line\n"); 778 return ENXIO; 779 } 780 781 if ((fdc->flags & FDC_NODMA) == 0) { 782 fdc->res_drq = bus_alloc_resource_any(dev, SYS_RES_DRQ, 783 &fdc->rid_drq, RF_ACTIVE | RF_SHAREABLE); 784 if (fdc->res_drq == 0) { 785 device_printf(dev, "cannot reserve DMA request line\n"); 786 fdc->flags |= FDC_NODMA; 787 } else 788 fdc->dmachan = rman_get_start(fdc->res_drq); 789 } 790 791 return 0; 792 } 793 794 static void 795 fdc_release_resources(struct fdc_data *fdc) 796 { 797 device_t dev; 798 799 dev = fdc->fdc_dev; 800 if (fdc->res_irq != 0) { 801 bus_deactivate_resource(dev, SYS_RES_IRQ, fdc->rid_irq, 802 fdc->res_irq); 803 bus_release_resource(dev, SYS_RES_IRQ, fdc->rid_irq, 804 fdc->res_irq); 805 } 806 if (fdc->res_ctl != 0) { 807 bus_deactivate_resource(dev, SYS_RES_IOPORT, fdc->rid_ctl, 808 fdc->res_ctl); 809 bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ctl, 810 fdc->res_ctl); 811 } 812 if (fdc->res_ioport != 0) { 813 bus_deactivate_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport, 814 fdc->res_ioport); 815 bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport, 816 fdc->res_ioport); 817 } 818 if (fdc->res_drq != 0) { 819 bus_deactivate_resource(dev, SYS_RES_DRQ, fdc->rid_drq, 820 fdc->res_drq); 821 bus_release_resource(dev, SYS_RES_DRQ, fdc->rid_drq, 822 fdc->res_drq); 823 } 824 } 825 826 /* 827 * Configuration/initialization stuff, per controller. 828 */ 829 830 static struct isa_pnp_id fdc_ids[] = { 831 {0x0007d041, "PC standard floppy disk controller"}, /* PNP0700 */ 832 {0x0107d041, "Standard floppy controller supporting MS Device Bay Spec"}, /* PNP0701 */ 833 {0} 834 }; 835 836 static int 837 fdc_read_ivar(device_t dev, device_t child, int which, uintptr_t *result) 838 { 839 struct fdc_ivars *ivars = device_get_ivars(child); 840 841 switch (which) { 842 case FDC_IVAR_FDUNIT: 843 *result = ivars->fdunit; 844 break; 845 default: 846 return ENOENT; 847 } 848 return 0; 849 } 850 851 static int 852 fdc_probe(device_t dev) 853 { 854 int error, ic_type; 855 struct fdc_data *fdc; 856 857 fdc = device_get_softc(dev); 858 bzero(fdc, sizeof *fdc); 859 fdc->fdc_dev = dev; 860 fdc->fdctl_wr = fdctl_wr_isa; 861 862 /* Check pnp ids */ 863 error = ISA_PNP_PROBE(device_get_parent(dev), dev, fdc_ids); 864 if (error == ENXIO) 865 return ENXIO; 866 if (error == 0) 867 fdc->flags |= FDC_ISPNP; 868 869 /* Attempt to allocate our resources for the duration of the probe */ 870 error = fdc_alloc_resources(fdc); 871 if (error) 872 goto out; 873 874 /* First - lets reset the floppy controller */ 875 fdout_wr(fdc, 0); 876 DELAY(100); 877 fdout_wr(fdc, FDO_FRST); 878 879 /* see if it can handle a command */ 880 if (fd_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240), 881 NE7_SPEC_2(2, 0), 0)) { 882 error = ENXIO; 883 goto out; 884 } 885 886 if (fd_cmd(fdc, 1, NE7CMD_VERSION, 1, &ic_type) == 0) { 887 ic_type = (u_char)ic_type; 888 switch (ic_type) { 889 case 0x80: 890 device_set_desc(dev, "NEC 765 or clone"); 891 fdc->fdct = FDC_NE765; 892 break; 893 case 0x81: /* not mentioned in any hardware doc */ 894 case 0x90: 895 device_set_desc(dev, 896 "Enhanced floppy controller (i82077, NE72065 or clone)"); 897 fdc->fdct = FDC_ENHANCED; 898 break; 899 default: 900 device_set_desc(dev, "Generic floppy controller"); 901 fdc->fdct = FDC_UNKNOWN; 902 break; 903 } 904 } 905 906 out: 907 fdc_release_resources(fdc); 908 return (error); 909 } 910 911 #if NCARD > 0 912 913 static int 914 fdc_pccard_probe(device_t dev) 915 { 916 int error; 917 struct fdc_data *fdc; 918 919 fdc = device_get_softc(dev); 920 bzero(fdc, sizeof *fdc); 921 fdc->fdc_dev = dev; 922 fdc->fdctl_wr = fdctl_wr_pcmcia; 923 924 fdc->flags |= FDC_ISPCMCIA | FDC_NODMA; 925 926 /* Attempt to allocate our resources for the duration of the probe */ 927 error = fdc_alloc_resources(fdc); 928 if (error) 929 goto out; 930 931 /* First - lets reset the floppy controller */ 932 fdout_wr(fdc, 0); 933 DELAY(100); 934 fdout_wr(fdc, FDO_FRST); 935 936 /* see if it can handle a command */ 937 if (fd_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240), 938 NE7_SPEC_2(2, 0), 0)) { 939 error = ENXIO; 940 goto out; 941 } 942 943 device_set_desc(dev, "Y-E Data PCMCIA floppy"); 944 fdc->fdct = FDC_NE765; 945 946 out: 947 fdc_release_resources(fdc); 948 return (error); 949 } 950 951 #endif /* NCARD > 0 */ 952 953 static int 954 fdc_detach(device_t dev) 955 { 956 struct fdc_data *fdc; 957 int error; 958 959 fdc = device_get_softc(dev); 960 961 /* have our children detached first */ 962 if ((error = bus_generic_detach(dev))) 963 return (error); 964 965 /* reset controller, turn motor off */ 966 fdout_wr(fdc, 0); 967 968 if ((fdc->flags & FDC_ATTACHED) == 0) { 969 device_printf(dev, "already unloaded\n"); 970 return (0); 971 } 972 fdc->flags &= ~FDC_ATTACHED; 973 974 BUS_TEARDOWN_INTR(device_get_parent(dev), dev, fdc->res_irq, 975 fdc->fdc_intr); 976 fdc_release_resources(fdc); 977 device_printf(dev, "unload\n"); 978 return (0); 979 } 980 981 /* 982 * Add a child device to the fdc controller. It will then be probed etc. 983 */ 984 static void 985 fdc_add_child(device_t dev, const char *name, int unit) 986 { 987 int flags; 988 struct fdc_ivars *ivar; 989 device_t child; 990 991 ivar = malloc(sizeof *ivar, M_DEVBUF /* XXX */, M_NOWAIT | M_ZERO); 992 if (ivar == NULL) 993 return; 994 if (resource_int_value(name, unit, "drive", &ivar->fdunit) != 0) 995 ivar->fdunit = 0; 996 child = device_add_child(dev, name, unit); 997 if (child == NULL) { 998 free(ivar, M_DEVBUF); 999 return; 1000 } 1001 device_set_ivars(child, ivar); 1002 if (resource_int_value(name, unit, "flags", &flags) == 0) 1003 device_set_flags(child, flags); 1004 if (resource_disabled(name, unit)) 1005 device_disable(child); 1006 } 1007 1008 static int 1009 fdc_attach(device_t dev) 1010 { 1011 struct fdc_data *fdc; 1012 const char *name, *dname; 1013 int i, error, dunit; 1014 1015 fdc = device_get_softc(dev); 1016 error = fdc_alloc_resources(fdc); 1017 if (error) { 1018 device_printf(dev, "cannot re-acquire resources\n"); 1019 return error; 1020 } 1021 error = BUS_SETUP_INTR(device_get_parent(dev), dev, fdc->res_irq, 1022 INTR_TYPE_BIO | INTR_ENTROPY, fdc_intr, fdc, 1023 &fdc->fdc_intr); 1024 if (error) { 1025 device_printf(dev, "cannot setup interrupt\n"); 1026 return error; 1027 } 1028 fdc->fdcu = device_get_unit(dev); 1029 fdc->flags |= FDC_ATTACHED | FDC_NEEDS_RESET; 1030 1031 fdc->state = DEVIDLE; 1032 1033 /* reset controller, turn motor off, clear fdout mirror reg */ 1034 fdout_wr(fdc, fdc->fdout = 0); 1035 bioq_init(&fdc->head); 1036 1037 /* 1038 * Probe and attach any children. We should probably detect 1039 * devices from the BIOS unless overridden. 1040 */ 1041 name = device_get_nameunit(dev); 1042 i = 0; 1043 while ((resource_find_match(&i, &dname, &dunit, "at", name)) == 0) 1044 fdc_add_child(dev, dname, dunit); 1045 1046 if ((error = bus_generic_attach(dev)) != 0) 1047 return (error); 1048 1049 return (0); 1050 } 1051 1052 static int 1053 fdc_print_child(device_t me, device_t child) 1054 { 1055 int retval = 0, flags; 1056 1057 retval += bus_print_child_header(me, child); 1058 retval += printf(" on %s drive %d", device_get_nameunit(me), 1059 fdc_get_fdunit(child)); 1060 if ((flags = device_get_flags(me)) != 0) 1061 retval += printf(" flags %#x", flags); 1062 retval += printf("\n"); 1063 1064 return (retval); 1065 } 1066 1067 static device_method_t fdc_methods[] = { 1068 /* Device interface */ 1069 DEVMETHOD(device_probe, fdc_probe), 1070 DEVMETHOD(device_attach, fdc_attach), 1071 DEVMETHOD(device_detach, fdc_detach), 1072 DEVMETHOD(device_shutdown, bus_generic_shutdown), 1073 DEVMETHOD(device_suspend, bus_generic_suspend), 1074 DEVMETHOD(device_resume, bus_generic_resume), 1075 1076 /* Bus interface */ 1077 DEVMETHOD(bus_print_child, fdc_print_child), 1078 DEVMETHOD(bus_read_ivar, fdc_read_ivar), 1079 /* Our children never use any other bus interface methods. */ 1080 1081 { 0, 0 } 1082 }; 1083 1084 static driver_t fdc_driver = { 1085 "fdc", 1086 fdc_methods, 1087 sizeof(struct fdc_data) 1088 }; 1089 1090 DRIVER_MODULE(fdc, isa, fdc_driver, fdc_devclass, 0, 0); 1091 DRIVER_MODULE(fdc, acpi, fdc_driver, fdc_devclass, 0, 0); 1092 1093 #if NCARD > 0 1094 1095 static device_method_t fdc_pccard_methods[] = { 1096 /* Device interface */ 1097 DEVMETHOD(device_probe, fdc_pccard_probe), 1098 DEVMETHOD(device_attach, fdc_attach), 1099 DEVMETHOD(device_detach, fdc_detach), 1100 DEVMETHOD(device_shutdown, bus_generic_shutdown), 1101 DEVMETHOD(device_suspend, bus_generic_suspend), 1102 DEVMETHOD(device_resume, bus_generic_resume), 1103 1104 /* Bus interface */ 1105 DEVMETHOD(bus_print_child, fdc_print_child), 1106 DEVMETHOD(bus_read_ivar, fdc_read_ivar), 1107 /* Our children never use any other bus interface methods. */ 1108 1109 { 0, 0 } 1110 }; 1111 1112 static driver_t fdc_pccard_driver = { 1113 "fdc", 1114 fdc_pccard_methods, 1115 sizeof(struct fdc_data) 1116 }; 1117 1118 DRIVER_MODULE(fdc, pccard, fdc_pccard_driver, fdc_devclass, 0, 0); 1119 1120 #endif /* NCARD > 0 */ 1121 1122 1123 /* 1124 * Configuration/initialization, per drive. 1125 */ 1126 static int 1127 fd_probe(device_t dev) 1128 { 1129 int i; 1130 u_int st0, st3; 1131 struct fd_data *fd; 1132 struct fdc_data *fdc; 1133 fdsu_t fdsu; 1134 int flags; 1135 1136 fdsu = *(int *)device_get_ivars(dev); /* xxx cheat a bit... */ 1137 fd = device_get_softc(dev); 1138 fdc = device_get_softc(device_get_parent(dev)); 1139 flags = device_get_flags(dev); 1140 1141 bzero(fd, sizeof *fd); 1142 fd->dev = dev; 1143 fd->fdc = fdc; 1144 fd->fdsu = fdsu; 1145 fd->fdu = device_get_unit(dev); 1146 fd->flags = FD_UA; /* make sure fdautoselect() will be called */ 1147 1148 fd->type = FD_DTYPE(flags); 1149 /* 1150 * XXX I think using __i386__ is wrong here since we actually want to probe 1151 * for the machine type, not the CPU type (so non-PC arch's like the PC98 will 1152 * fail the probe). However, for whatever reason, testing for _MACHINE_ARCH 1153 * == i386 breaks the test on FreeBSD/Alpha. 1154 */ 1155 #if defined(__i386__) || defined(__amd64__) 1156 if (fd->type == FDT_NONE && (fd->fdu == 0 || fd->fdu == 1)) { 1157 /* Look up what the BIOS thinks we have. */ 1158 if (fd->fdu == 0) { 1159 if ((fdc->flags & FDC_ISPCMCIA)) 1160 /* 1161 * Somewhat special. No need to force the 1162 * user to set device flags, since the Y-E 1163 * Data PCMCIA floppy is always a 1.44 MB 1164 * device. 1165 */ 1166 fd->type = FDT_144M; 1167 else 1168 fd->type = (rtcin(RTC_FDISKETTE) & 0xf0) >> 4; 1169 } else { 1170 fd->type = rtcin(RTC_FDISKETTE) & 0x0f; 1171 } 1172 if (fd->type == FDT_288M_1) 1173 fd->type = FDT_288M; 1174 } 1175 #endif /* __i386__ || __amd64__ */ 1176 /* is there a unit? */ 1177 if (fd->type == FDT_NONE) 1178 return (ENXIO); 1179 1180 /* select it */ 1181 set_motor(fdc, fdsu, TURNON); 1182 fdc_reset(fdc); /* XXX reset, then unreset, etc. */ 1183 DELAY(1000000); /* 1 sec */ 1184 1185 /* XXX This doesn't work before the first set_motor() */ 1186 if ((fdc->flags & FDC_HAS_FIFO) == 0 && 1187 fdc->fdct == FDC_ENHANCED && 1188 (device_get_flags(fdc->fdc_dev) & FDC_NO_FIFO) == 0 && 1189 enable_fifo(fdc) == 0) { 1190 device_printf(device_get_parent(dev), 1191 "FIFO enabled, %d bytes threshold\n", fifo_threshold); 1192 } 1193 1194 if ((flags & FD_NO_PROBE) == 0) { 1195 /* If we're at track 0 first seek inwards. */ 1196 if ((fd_sense_drive_status(fdc, &st3) == 0) && 1197 (st3 & NE7_ST3_T0)) { 1198 /* Seek some steps... */ 1199 if (fd_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) { 1200 /* ...wait a moment... */ 1201 DELAY(300000); 1202 /* make ctrlr happy: */ 1203 fd_sense_int(fdc, 0, 0); 1204 } 1205 } 1206 1207 for (i = 0; i < 2; i++) { 1208 /* 1209 * we must recalibrate twice, just in case the 1210 * heads have been beyond cylinder 76, since 1211 * most FDCs still barf when attempting to 1212 * recalibrate more than 77 steps 1213 */ 1214 /* go back to 0: */ 1215 if (fd_cmd(fdc, 2, NE7CMD_RECAL, fdsu, 0) == 0) { 1216 /* a second being enough for full stroke seek*/ 1217 DELAY(i == 0 ? 1000000 : 300000); 1218 1219 /* anything responding? */ 1220 if (fd_sense_int(fdc, &st0, 0) == 0 && 1221 (st0 & NE7_ST0_EC) == 0) 1222 break; /* already probed succesfully */ 1223 } 1224 } 1225 } 1226 1227 set_motor(fdc, fdsu, TURNOFF); 1228 1229 if ((flags & FD_NO_PROBE) == 0 && 1230 (st0 & NE7_ST0_EC) != 0) /* no track 0 -> no drive present */ 1231 return (ENXIO); 1232 1233 switch (fd->type) { 1234 case FDT_12M: 1235 device_set_desc(dev, "1200-KB 5.25\" drive"); 1236 fd->type = FDT_12M; 1237 break; 1238 case FDT_144M: 1239 device_set_desc(dev, "1440-KB 3.5\" drive"); 1240 fd->type = FDT_144M; 1241 break; 1242 case FDT_288M: 1243 device_set_desc(dev, "2880-KB 3.5\" drive (in 1440-KB mode)"); 1244 fd->type = FDT_288M; 1245 break; 1246 case FDT_360K: 1247 device_set_desc(dev, "360-KB 5.25\" drive"); 1248 fd->type = FDT_360K; 1249 break; 1250 case FDT_720K: 1251 device_set_desc(dev, "720-KB 3.5\" drive"); 1252 fd->type = FDT_720K; 1253 break; 1254 default: 1255 return (ENXIO); 1256 } 1257 fd->track = FD_NO_TRACK; 1258 fd->fdc = fdc; 1259 fd->fdsu = fdsu; 1260 fd->options = 0; 1261 callout_handle_init(&fd->toffhandle); 1262 callout_handle_init(&fd->tohandle); 1263 1264 /* initialize densities for subdevices */ 1265 for (i = 0; i < NUMDENS; i++) 1266 memcpy(fd->fts + i, fd_native_types + fd->type, 1267 sizeof(struct fd_type)); 1268 return (0); 1269 } 1270 1271 static int 1272 fd_attach(device_t dev) 1273 { 1274 struct fd_data *fd; 1275 1276 fd = device_get_softc(dev); 1277 fd->masterdev = make_dev(&fd_cdevsw, fd->fdu, 1278 UID_ROOT, GID_OPERATOR, 0640, "fd%d", fd->fdu); 1279 fd->masterdev->si_drv1 = fd; 1280 fd->device_stats = devstat_new_entry(device_get_name(dev), 1281 device_get_unit(dev), 0, DEVSTAT_NO_ORDERED_TAGS, 1282 DEVSTAT_TYPE_FLOPPY | DEVSTAT_TYPE_IF_OTHER, 1283 DEVSTAT_PRIORITY_FD); 1284 return (0); 1285 } 1286 1287 static int 1288 fd_detach(device_t dev) 1289 { 1290 struct fd_data *fd; 1291 1292 fd = device_get_softc(dev); 1293 untimeout(fd_turnoff, fd, fd->toffhandle); 1294 devstat_remove_entry(fd->device_stats); 1295 destroy_dev(fd->masterdev); 1296 1297 return (0); 1298 } 1299 1300 static device_method_t fd_methods[] = { 1301 /* Device interface */ 1302 DEVMETHOD(device_probe, fd_probe), 1303 DEVMETHOD(device_attach, fd_attach), 1304 DEVMETHOD(device_detach, fd_detach), 1305 DEVMETHOD(device_shutdown, bus_generic_shutdown), 1306 DEVMETHOD(device_suspend, bus_generic_suspend), /* XXX */ 1307 DEVMETHOD(device_resume, bus_generic_resume), /* XXX */ 1308 1309 { 0, 0 } 1310 }; 1311 1312 static driver_t fd_driver = { 1313 "fd", 1314 fd_methods, 1315 sizeof(struct fd_data) 1316 }; 1317 1318 DRIVER_MODULE(fd, fdc, fd_driver, fd_devclass, 0, 0); 1319 1320 /* 1321 * More auxiliary functions. 1322 */ 1323 /* 1324 * Motor control stuff. 1325 * Remember to not deselect the drive we're working on. 1326 */ 1327 static void 1328 set_motor(struct fdc_data *fdc, int fdsu, int turnon) 1329 { 1330 int fdout; 1331 1332 fdout = fdc->fdout; 1333 if (turnon) { 1334 fdout &= ~FDO_FDSEL; 1335 fdout |= (FDO_MOEN0 << fdsu) | FDO_FDMAEN | FDO_FRST | fdsu; 1336 } else 1337 fdout &= ~(FDO_MOEN0 << fdsu); 1338 fdc->fdout = fdout; 1339 fdout_wr(fdc, fdout); 1340 TRACE1("[0x%x->FDOUT]", fdout); 1341 } 1342 1343 static void 1344 fd_turnoff(void *xfd) 1345 { 1346 int s; 1347 fd_p fd = xfd; 1348 1349 TRACE1("[fd%d: turnoff]", fd->fdu); 1350 1351 s = splbio(); 1352 /* 1353 * Don't turn off the motor yet if the drive is active. 1354 * 1355 * If we got here, this could only mean we missed an interrupt. 1356 * This can e. g. happen on the Y-E Date PCMCIA floppy controller 1357 * after a controller reset. Just schedule a pseudo-interrupt 1358 * so the state machine gets re-entered. 1359 */ 1360 if (fd->fdc->state != DEVIDLE && fd->fdc->fdu == fd->fdu) { 1361 fdc_intr(fd->fdc); 1362 splx(s); 1363 return; 1364 } 1365 1366 fd->flags &= ~FD_MOTOR; 1367 set_motor(fd->fdc, fd->fdsu, TURNOFF); 1368 splx(s); 1369 } 1370 1371 static void 1372 fd_motor_on(void *xfd) 1373 { 1374 int s; 1375 fd_p fd = xfd; 1376 1377 s = splbio(); 1378 fd->flags &= ~FD_MOTOR_WAIT; 1379 if((fd->fdc->fd == fd) && (fd->fdc->state == MOTORWAIT)) 1380 { 1381 fdc_intr(fd->fdc); 1382 } 1383 splx(s); 1384 } 1385 1386 static void 1387 fd_turnon(fd_p fd) 1388 { 1389 if(!(fd->flags & FD_MOTOR)) 1390 { 1391 fd->flags |= (FD_MOTOR + FD_MOTOR_WAIT); 1392 set_motor(fd->fdc, fd->fdsu, TURNON); 1393 timeout(fd_motor_on, fd, hz); /* in 1 sec its ok */ 1394 } 1395 } 1396 1397 static void 1398 fdc_reset(fdc_p fdc) 1399 { 1400 /* Try a reset, keep motor on */ 1401 fdout_wr(fdc, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN)); 1402 TRACE1("[0x%x->FDOUT]", fdc->fdout & ~(FDO_FRST|FDO_FDMAEN)); 1403 DELAY(100); 1404 /* enable FDC, but defer interrupts a moment */ 1405 fdout_wr(fdc, fdc->fdout & ~FDO_FDMAEN); 1406 TRACE1("[0x%x->FDOUT]", fdc->fdout & ~FDO_FDMAEN); 1407 DELAY(100); 1408 fdout_wr(fdc, fdc->fdout); 1409 TRACE1("[0x%x->FDOUT]", fdc->fdout); 1410 1411 /* XXX after a reset, silently believe the FDC will accept commands */ 1412 (void)fd_cmd(fdc, 3, NE7CMD_SPECIFY, 1413 NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0), 1414 0); 1415 if (fdc->flags & FDC_HAS_FIFO) 1416 (void) enable_fifo(fdc); 1417 } 1418 1419 /* 1420 * FDC IO functions, take care of the main status register, timeout 1421 * in case the desired status bits are never set. 1422 * 1423 * These PIO loops initially start out with short delays between 1424 * each iteration in the expectation that the required condition 1425 * is usually met quickly, so it can be handled immediately. After 1426 * about 1 ms, stepping is increased to achieve a better timing 1427 * accuracy in the calls to DELAY(). 1428 */ 1429 static int 1430 fd_in(struct fdc_data *fdc, int *ptr) 1431 { 1432 int i, j, step; 1433 1434 for (j = 0, step = 1; 1435 (i = fdsts_rd(fdc) & (NE7_DIO|NE7_RQM)) != (NE7_DIO|NE7_RQM) && 1436 j < FDSTS_TIMEOUT; 1437 j += step) { 1438 if (i == NE7_RQM) 1439 return (fdc_err(fdc, "ready for output in input\n")); 1440 if (j == 1000) 1441 step = 1000; 1442 DELAY(step); 1443 } 1444 if (j >= FDSTS_TIMEOUT) 1445 return (fdc_err(fdc, bootverbose? "input ready timeout\n": 0)); 1446 #ifdef FDC_DEBUG 1447 i = fddata_rd(fdc); 1448 TRACE1("[FDDATA->0x%x]", (unsigned char)i); 1449 *ptr = i; 1450 return (0); 1451 #else /* !FDC_DEBUG */ 1452 i = fddata_rd(fdc); 1453 if (ptr) 1454 *ptr = i; 1455 return (0); 1456 #endif /* FDC_DEBUG */ 1457 } 1458 1459 static int 1460 out_fdc(struct fdc_data *fdc, int x) 1461 { 1462 int i, j, step; 1463 1464 for (j = 0, step = 1; 1465 (i = fdsts_rd(fdc) & (NE7_DIO|NE7_RQM)) != NE7_RQM && 1466 j < FDSTS_TIMEOUT; 1467 j += step) { 1468 if (i == (NE7_DIO|NE7_RQM)) 1469 return (fdc_err(fdc, "ready for input in output\n")); 1470 if (j == 1000) 1471 step = 1000; 1472 DELAY(step); 1473 } 1474 if (j >= FDSTS_TIMEOUT) 1475 return (fdc_err(fdc, bootverbose? "output ready timeout\n": 0)); 1476 1477 /* Send the command and return */ 1478 fddata_wr(fdc, x); 1479 TRACE1("[0x%x->FDDATA]", x); 1480 return (0); 1481 } 1482 1483 /* 1484 * Block device driver interface functions (interspersed with even more 1485 * auxiliary functions). 1486 */ 1487 static int 1488 fdopen(struct cdev *dev, int flags, int mode, struct thread *td) 1489 { 1490 fd_p fd; 1491 fdc_p fdc; 1492 int rv, unitattn, dflags; 1493 1494 fd = dev->si_drv1; 1495 if (fd == NULL) 1496 return (ENXIO); 1497 fdc = fd->fdc; 1498 if ((fdc == NULL) || (fd->type == FDT_NONE)) 1499 return (ENXIO); 1500 dflags = device_get_flags(fd->dev); 1501 /* 1502 * This is a bit bogus. It's still possible that e. g. a 1503 * descriptor gets inherited to a child, but then it's at 1504 * least for the same subdevice. By checking FD_OPEN here, we 1505 * can ensure that a device isn't attempted to be opened with 1506 * different densities at the same time where the second open 1507 * could clobber the settings from the first one. 1508 */ 1509 if (fd->flags & FD_OPEN) 1510 return (EBUSY); 1511 1512 if (flags & FNONBLOCK) { 1513 /* 1514 * Unfortunately, physio(9) discards its ioflag 1515 * argument, thus preventing us from seeing the 1516 * IO_NDELAY bit. So we need to keep track 1517 * ourselves. 1518 */ 1519 fd->flags |= FD_NONBLOCK; 1520 fd->ft = 0; 1521 } else { 1522 /* 1523 * Figure out a unit attention condition. 1524 * 1525 * If UA has been forced, proceed. 1526 * 1527 * If the drive has no changeline support, 1528 * or if the drive parameters have been lost 1529 * due to previous non-blocking access, 1530 * assume a forced UA condition. 1531 * 1532 * If motor is off, turn it on for a moment 1533 * and select our drive, in order to read the 1534 * UA hardware signal. 1535 * 1536 * If motor is on, and our drive is currently 1537 * selected, just read the hardware bit. 1538 * 1539 * If motor is on, but active for another 1540 * drive on that controller, we are lost. We 1541 * cannot risk to deselect the other drive, so 1542 * we just assume a forced UA condition to be 1543 * on the safe side. 1544 */ 1545 unitattn = 0; 1546 if ((dflags & FD_NO_CHLINE) != 0 || 1547 (fd->flags & FD_UA) != 0 || 1548 fd->ft == 0) { 1549 unitattn = 1; 1550 fd->flags &= ~FD_UA; 1551 } else if (fdc->fdout & (FDO_MOEN0 | FDO_MOEN1 | 1552 FDO_MOEN2 | FDO_MOEN3)) { 1553 if ((fdc->fdout & FDO_FDSEL) == fd->fdsu) 1554 unitattn = fdin_rd(fdc) & FDI_DCHG; 1555 else 1556 unitattn = 1; 1557 } else { 1558 set_motor(fdc, fd->fdsu, TURNON); 1559 unitattn = fdin_rd(fdc) & FDI_DCHG; 1560 set_motor(fdc, fd->fdsu, TURNOFF); 1561 } 1562 if (unitattn && (rv = fdautoselect(dev)) != 0) 1563 return (rv); 1564 } 1565 fd->flags |= FD_OPEN; 1566 1567 if ((fdc->flags & FDC_NODMA) == 0) { 1568 if (fdc->dmacnt++ == 0) { 1569 isa_dma_acquire(fdc->dmachan); 1570 isa_dmainit(fdc->dmachan, MAX_SEC_SIZE); 1571 } 1572 } 1573 1574 /* 1575 * Clearing the DMA overrun counter at open time is a bit messy. 1576 * Since we're only managing one counter per controller, opening 1577 * the second drive could mess it up. Anyway, if the DMA overrun 1578 * condition is really persistent, it will eventually time out 1579 * still. OTOH, clearing it here will ensure we'll at least start 1580 * trying again after a previous (maybe even long ago) failure. 1581 * Also, this is merely a stop-gap measure only that should not 1582 * happen during normal operation, so we can tolerate it to be a 1583 * bit sloppy about this. 1584 */ 1585 fdc->dma_overruns = 0; 1586 1587 return 0; 1588 } 1589 1590 static int 1591 fdclose(struct cdev *dev, int flags, int mode, struct thread *td) 1592 { 1593 struct fd_data *fd; 1594 fdc_p fdc; 1595 1596 fd = dev->si_drv1; 1597 fdc = fd->fdc; 1598 fd->flags &= ~(FD_OPEN | FD_NONBLOCK); 1599 fd->options &= ~(FDOPT_NORETRY | FDOPT_NOERRLOG | FDOPT_NOERROR); 1600 1601 if ((fdc->flags & FDC_NODMA) == 0) 1602 if (--fdc->dmacnt == 0) 1603 isa_dma_release(fdc->dmachan); 1604 1605 return (0); 1606 } 1607 1608 static void 1609 fdstrategy(struct bio *bp) 1610 { 1611 long blknum, nblocks; 1612 int s; 1613 fdu_t fdu; 1614 fdc_p fdc; 1615 fd_p fd; 1616 size_t fdblk; 1617 1618 fd = bp->bio_dev->si_drv1; 1619 fdu = fd->fdu; 1620 fdc = fd->fdc; 1621 bp->bio_resid = bp->bio_bcount; 1622 if (fd->type == FDT_NONE || fd->ft == 0) { 1623 if (fd->type != FDT_NONE && (fd->flags & FD_NONBLOCK)) 1624 bp->bio_error = EAGAIN; 1625 else 1626 bp->bio_error = ENXIO; 1627 bp->bio_flags |= BIO_ERROR; 1628 goto bad; 1629 } 1630 fdblk = 128 << (fd->ft->secsize); 1631 if (bp->bio_cmd != FDBIO_FORMAT && bp->bio_cmd != FDBIO_RDSECTID) { 1632 if (fd->flags & FD_NONBLOCK) { 1633 bp->bio_error = EAGAIN; 1634 bp->bio_flags |= BIO_ERROR; 1635 goto bad; 1636 } 1637 if (bp->bio_offset < 0) { 1638 printf( 1639 "fd%d: fdstrat: bad request offset = %ju, bcount = %ld\n", 1640 fdu, (intmax_t)bp->bio_offset, bp->bio_bcount); 1641 bp->bio_error = EINVAL; 1642 bp->bio_flags |= BIO_ERROR; 1643 goto bad; 1644 } 1645 if ((bp->bio_bcount % fdblk) != 0) { 1646 bp->bio_error = EINVAL; 1647 bp->bio_flags |= BIO_ERROR; 1648 goto bad; 1649 } 1650 } 1651 1652 /* 1653 * Set up block calculations. 1654 */ 1655 if (bp->bio_offset >= ((off_t)128 << fd->ft->secsize) * fd->ft->size) { 1656 bp->bio_error = EINVAL; 1657 bp->bio_flags |= BIO_ERROR; 1658 goto bad; 1659 } 1660 blknum = bp->bio_offset / fdblk; 1661 nblocks = fd->ft->size; 1662 if (blknum + bp->bio_bcount / fdblk > nblocks) { 1663 if (blknum >= nblocks) { 1664 if (bp->bio_cmd != BIO_READ) { 1665 bp->bio_error = ENOSPC; 1666 bp->bio_flags |= BIO_ERROR; 1667 } 1668 goto bad; /* not always bad, but EOF */ 1669 } 1670 bp->bio_bcount = (nblocks - blknum) * fdblk; 1671 } 1672 bp->bio_pblkno = blknum; 1673 s = splbio(); 1674 bioq_disksort(&fdc->head, bp); 1675 untimeout(fd_turnoff, fd, fd->toffhandle); /* a good idea */ 1676 devstat_start_transaction_bio(fd->device_stats, bp); 1677 device_busy(fd->dev); 1678 fdstart(fdc); 1679 splx(s); 1680 return; 1681 1682 bad: 1683 biodone(bp); 1684 } 1685 1686 /* 1687 * fdstart 1688 * 1689 * We have just queued something. If the controller is not busy 1690 * then simulate the case where it has just finished a command 1691 * So that it (the interrupt routine) looks on the queue for more 1692 * work to do and picks up what we just added. 1693 * 1694 * If the controller is already busy, we need do nothing, as it 1695 * will pick up our work when the present work completes. 1696 */ 1697 static void 1698 fdstart(struct fdc_data *fdc) 1699 { 1700 int s; 1701 1702 s = splbio(); 1703 if(fdc->state == DEVIDLE) 1704 { 1705 fdc_intr(fdc); 1706 } 1707 splx(s); 1708 } 1709 1710 static void 1711 fd_iotimeout(void *xfdc) 1712 { 1713 fdc_p fdc; 1714 int s; 1715 1716 fdc = xfdc; 1717 TRACE1("fd%d[fd_iotimeout()]", fdc->fdu); 1718 1719 /* 1720 * Due to IBM's brain-dead design, the FDC has a faked ready 1721 * signal, hardwired to ready == true. Thus, any command 1722 * issued if there's no diskette in the drive will _never_ 1723 * complete, and must be aborted by resetting the FDC. 1724 * Many thanks, Big Blue! 1725 * The FDC must not be reset directly, since that would 1726 * interfere with the state machine. Instead, pretend that 1727 * the command completed but was invalid. The state machine 1728 * will reset the FDC and retry once. 1729 */ 1730 s = splbio(); 1731 fdc->status[0] = NE7_ST0_IC_IV; 1732 fdc->flags &= ~FDC_STAT_VALID; 1733 fdc->state = IOTIMEDOUT; 1734 fdc_intr(fdc); 1735 splx(s); 1736 } 1737 1738 /* Just ensure it has the right spl. */ 1739 static void 1740 fd_pseudointr(void *xfdc) 1741 { 1742 int s; 1743 1744 s = splbio(); 1745 fdc_intr(xfdc); 1746 splx(s); 1747 } 1748 1749 /* 1750 * fdc_intr 1751 * 1752 * Keep calling the state machine until it returns a 0. 1753 * Always called at splbio. 1754 */ 1755 static void 1756 fdc_intr(void *xfdc) 1757 { 1758 fdc_p fdc = xfdc; 1759 while(fdstate(fdc)) 1760 ; 1761 } 1762 1763 /* 1764 * Magic pseudo-DMA initialization for YE FDC. Sets count and 1765 * direction. 1766 */ 1767 #define SET_BCDR(fdc,wr,cnt,port) \ 1768 bus_space_write_1(fdc->portt, fdc->porth, fdc->port_off + port, \ 1769 ((cnt)-1) & 0xff); \ 1770 bus_space_write_1(fdc->portt, fdc->porth, fdc->port_off + port + 1, \ 1771 ((wr ? 0x80 : 0) | ((((cnt)-1) >> 8) & 0x7f))); 1772 1773 /* 1774 * fdcpio(): perform programmed IO read/write for YE PCMCIA floppy. 1775 */ 1776 static int 1777 fdcpio(fdc_p fdc, long flags, caddr_t addr, u_int count) 1778 { 1779 u_char *cptr = (u_char *)addr; 1780 1781 if (flags == BIO_READ) { 1782 if (fdc->state != PIOREAD) { 1783 fdc->state = PIOREAD; 1784 return(0); 1785 } 1786 SET_BCDR(fdc, 0, count, 0); 1787 bus_space_read_multi_1(fdc->portt, fdc->porth, fdc->port_off + 1788 FDC_YE_DATAPORT, cptr, count); 1789 } else { 1790 bus_space_write_multi_1(fdc->portt, fdc->porth, fdc->port_off + 1791 FDC_YE_DATAPORT, cptr, count); 1792 SET_BCDR(fdc, 0, count, 0); 1793 } 1794 return(1); 1795 } 1796 1797 /* 1798 * Try figuring out the density of the media present in our device. 1799 */ 1800 static int 1801 fdautoselect(struct cdev *dev) 1802 { 1803 fd_p fd; 1804 struct fd_type *fdtp; 1805 struct fdc_readid id; 1806 int i, n, oopts, rv; 1807 1808 fd = dev->si_drv1; 1809 1810 switch (fd->type) { 1811 default: 1812 return (ENXIO); 1813 1814 case FDT_360K: 1815 case FDT_720K: 1816 /* no autoselection on those drives */ 1817 fd->ft = fd_native_types + fd->type; 1818 return (0); 1819 1820 case FDT_12M: 1821 fdtp = fd_searchlist_12m; 1822 n = sizeof fd_searchlist_12m / sizeof(struct fd_type); 1823 break; 1824 1825 case FDT_144M: 1826 fdtp = fd_searchlist_144m; 1827 n = sizeof fd_searchlist_144m / sizeof(struct fd_type); 1828 break; 1829 1830 case FDT_288M: 1831 fdtp = fd_searchlist_288m; 1832 n = sizeof fd_searchlist_288m / sizeof(struct fd_type); 1833 break; 1834 } 1835 1836 /* 1837 * Try reading sector ID fields, first at cylinder 0, head 0, 1838 * then at cylinder 2, head N. We don't probe cylinder 1, 1839 * since for 5.25in DD media in a HD drive, there are no data 1840 * to read (2 step pulses per media cylinder required). For 1841 * two-sided media, the second probe always goes to head 1, so 1842 * we can tell them apart from single-sided media. As a 1843 * side-effect this means that single-sided media should be 1844 * mentioned in the search list after two-sided media of an 1845 * otherwise identical density. Media with a different number 1846 * of sectors per track but otherwise identical parameters 1847 * cannot be distinguished at all. 1848 * 1849 * If we successfully read an ID field on both cylinders where 1850 * the recorded values match our expectation, we are done. 1851 * Otherwise, we try the next density entry from the table. 1852 * 1853 * Stepping to cylinder 2 has the side-effect of clearing the 1854 * unit attention bit. 1855 */ 1856 oopts = fd->options; 1857 fd->options |= FDOPT_NOERRLOG | FDOPT_NORETRY; 1858 for (i = 0; i < n; i++, fdtp++) { 1859 fd->ft = fdtp; 1860 1861 id.cyl = id.head = 0; 1862 rv = fdmisccmd(dev, FDBIO_RDSECTID, &id); 1863 if (rv != 0) 1864 continue; 1865 if (id.cyl != 0 || id.head != 0 || 1866 id.secshift != fdtp->secsize) 1867 continue; 1868 id.cyl = 2; 1869 id.head = fd->ft->heads - 1; 1870 rv = fdmisccmd(dev, FDBIO_RDSECTID, &id); 1871 if (id.cyl != 2 || id.head != fdtp->heads - 1 || 1872 id.secshift != fdtp->secsize) 1873 continue; 1874 if (rv == 0) 1875 break; 1876 } 1877 1878 fd->options = oopts; 1879 if (i == n) { 1880 if (bootverbose) 1881 device_printf(fd->dev, "autoselection failed\n"); 1882 fd->ft = 0; 1883 return (EIO); 1884 } else { 1885 if (bootverbose) 1886 device_printf(fd->dev, "autoselected %d KB medium\n", 1887 fd->ft->size / 2); 1888 return (0); 1889 } 1890 } 1891 1892 1893 /* 1894 * The controller state machine. 1895 * 1896 * If it returns a non zero value, it should be called again immediately. 1897 */ 1898 static int 1899 fdstate(fdc_p fdc) 1900 { 1901 struct fdc_readid *idp; 1902 int read, format, rdsectid, cylinder, head, i, sec = 0, sectrac; 1903 int st0, cyl, st3, idf, ne7cmd, mfm, steptrac; 1904 unsigned long blknum; 1905 fdu_t fdu = fdc->fdu; 1906 fd_p fd; 1907 register struct bio *bp; 1908 struct fd_formb *finfo = NULL; 1909 size_t fdblk; 1910 1911 bp = fdc->bp; 1912 if (bp == NULL) { 1913 bp = bioq_first(&fdc->head); 1914 if (bp != NULL) { 1915 bioq_remove(&fdc->head, bp); 1916 fdc->bp = bp; 1917 } 1918 } 1919 if (bp == NULL) { 1920 /* 1921 * Nothing left for this controller to do, 1922 * force into the IDLE state. 1923 */ 1924 fdc->state = DEVIDLE; 1925 if (fdc->fd) { 1926 device_printf(fdc->fdc_dev, 1927 "unexpected valid fd pointer\n"); 1928 fdc->fd = (fd_p) 0; 1929 fdc->fdu = -1; 1930 } 1931 TRACE1("[fdc%d IDLE]", fdc->fdcu); 1932 return (0); 1933 } 1934 fd = bp->bio_dev->si_drv1; 1935 fdu = fd->fdu; 1936 fdblk = 128 << fd->ft->secsize; 1937 if (fdc->fd && (fd != fdc->fd)) 1938 device_printf(fd->dev, "confused fd pointers\n"); 1939 read = bp->bio_cmd == BIO_READ; 1940 mfm = (fd->ft->flags & FL_MFM)? NE7CMD_MFM: 0; 1941 steptrac = (fd->ft->flags & FL_2STEP)? 2: 1; 1942 if (read) 1943 idf = ISADMA_READ; 1944 else 1945 idf = ISADMA_WRITE; 1946 format = bp->bio_cmd == FDBIO_FORMAT; 1947 rdsectid = bp->bio_cmd == FDBIO_RDSECTID; 1948 if (format) 1949 finfo = (struct fd_formb *)bp->bio_data; 1950 TRACE1("fd%d", fdu); 1951 TRACE1("[%s]", fdstates[fdc->state]); 1952 TRACE1("(0x%x)", fd->flags); 1953 untimeout(fd_turnoff, fd, fd->toffhandle); 1954 fd->toffhandle = timeout(fd_turnoff, fd, 4 * hz); 1955 switch (fdc->state) 1956 { 1957 case DEVIDLE: 1958 case FINDWORK: /* we have found new work */ 1959 fdc->retry = 0; 1960 fd->skip = 0; 1961 fdc->fd = fd; 1962 fdc->fdu = fdu; 1963 fdc->fdctl_wr(fdc, fd->ft->trans); 1964 TRACE1("[0x%x->FDCTL]", fd->ft->trans); 1965 /* 1966 * If the next drive has a motor startup pending, then 1967 * it will start up in its own good time. 1968 */ 1969 if(fd->flags & FD_MOTOR_WAIT) { 1970 fdc->state = MOTORWAIT; 1971 return (0); /* will return later */ 1972 } 1973 /* 1974 * Maybe if it's not starting, it SHOULD be starting. 1975 */ 1976 if (!(fd->flags & FD_MOTOR)) 1977 { 1978 fdc->state = MOTORWAIT; 1979 fd_turnon(fd); 1980 return (0); /* will return later */ 1981 } 1982 else /* at least make sure we are selected */ 1983 { 1984 set_motor(fdc, fd->fdsu, TURNON); 1985 } 1986 if (fdc->flags & FDC_NEEDS_RESET) { 1987 fdc->state = RESETCTLR; 1988 fdc->flags &= ~FDC_NEEDS_RESET; 1989 } else 1990 fdc->state = DOSEEK; 1991 return (1); /* will return immediately */ 1992 1993 case DOSEEK: 1994 blknum = bp->bio_pblkno + fd->skip / fdblk; 1995 cylinder = blknum / (fd->ft->sectrac * fd->ft->heads); 1996 if (cylinder == fd->track) 1997 { 1998 fdc->state = SEEKCOMPLETE; 1999 return (1); /* will return immediately */ 2000 } 2001 if (fd_cmd(fdc, 3, NE7CMD_SEEK, 2002 fd->fdsu, cylinder * steptrac, 0)) 2003 { 2004 /* 2005 * Seek command not accepted, looks like 2006 * the FDC went off to the Saints... 2007 */ 2008 fdc->retry = 6; /* try a reset */ 2009 return(retrier(fdc)); 2010 } 2011 fd->track = FD_NO_TRACK; 2012 fdc->state = SEEKWAIT; 2013 return(0); /* will return later */ 2014 2015 case SEEKWAIT: 2016 /* allow heads to settle */ 2017 timeout(fd_pseudointr, fdc, hz / 16); 2018 fdc->state = SEEKCOMPLETE; 2019 return(0); /* will return later */ 2020 2021 case SEEKCOMPLETE : /* seek done, start DMA */ 2022 blknum = bp->bio_pblkno + fd->skip / fdblk; 2023 cylinder = blknum / (fd->ft->sectrac * fd->ft->heads); 2024 2025 /* Make sure seek really happened. */ 2026 if(fd->track == FD_NO_TRACK) { 2027 int descyl = cylinder * steptrac; 2028 do { 2029 /* 2030 * This might be a "ready changed" interrupt, 2031 * which cannot really happen since the 2032 * RDY pin is hardwired to + 5 volts. This 2033 * generally indicates a "bouncing" intr 2034 * line, so do one of the following: 2035 * 2036 * When running on an enhanced FDC that is 2037 * known to not go stuck after responding 2038 * with INVALID, fetch all interrupt states 2039 * until seeing either an INVALID or a 2040 * real interrupt condition. 2041 * 2042 * When running on a dumb old NE765, give 2043 * up immediately. The controller will 2044 * provide up to four dummy RC interrupt 2045 * conditions right after reset (for the 2046 * corresponding four drives), so this is 2047 * our only chance to get notice that it 2048 * was not the FDC that caused the interrupt. 2049 */ 2050 if (fd_sense_int(fdc, &st0, &cyl) 2051 == FD_NOT_VALID) 2052 return (0); /* will return later */ 2053 if(fdc->fdct == FDC_NE765 2054 && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC) 2055 return (0); /* hope for a real intr */ 2056 } while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC); 2057 2058 if (0 == descyl) { 2059 int failed = 0; 2060 /* 2061 * seek to cyl 0 requested; make sure we are 2062 * really there 2063 */ 2064 if (fd_sense_drive_status(fdc, &st3)) 2065 failed = 1; 2066 if ((st3 & NE7_ST3_T0) == 0) { 2067 printf( 2068 "fd%d: Seek to cyl 0, but not really there (ST3 = %b)\n", 2069 fdu, st3, NE7_ST3BITS); 2070 failed = 1; 2071 } 2072 2073 if (failed) { 2074 if(fdc->retry < 3) 2075 fdc->retry = 3; 2076 return (retrier(fdc)); 2077 } 2078 } 2079 2080 if (cyl != descyl) { 2081 printf( 2082 "fd%d: Seek to cyl %d failed; am at cyl %d (ST0 = 0x%x)\n", 2083 fdu, descyl, cyl, st0); 2084 if (fdc->retry < 3) 2085 fdc->retry = 3; 2086 return (retrier(fdc)); 2087 } 2088 } 2089 2090 fd->track = cylinder; 2091 if (format) 2092 fd->skip = (char *)&(finfo->fd_formb_cylno(0)) 2093 - (char *)finfo; 2094 if (!rdsectid && !(fdc->flags & FDC_NODMA)) 2095 isa_dmastart(idf, bp->bio_data+fd->skip, 2096 format ? bp->bio_bcount : fdblk, fdc->dmachan); 2097 blknum = bp->bio_pblkno + fd->skip / fdblk; 2098 sectrac = fd->ft->sectrac; 2099 sec = blknum % (sectrac * fd->ft->heads); 2100 head = sec / sectrac; 2101 sec = sec % sectrac + 1; 2102 if (head != 0 && fd->ft->offset_side2 != 0) 2103 sec += fd->ft->offset_side2; 2104 fd->hddrv = ((head&1)<<2)+fdu; 2105 2106 if(format || !(read || rdsectid)) 2107 { 2108 /* make sure the drive is writable */ 2109 if(fd_sense_drive_status(fdc, &st3) != 0) 2110 { 2111 /* stuck controller? */ 2112 if (!(fdc->flags & FDC_NODMA)) 2113 isa_dmadone(idf, 2114 bp->bio_data + fd->skip, 2115 format ? bp->bio_bcount : fdblk, 2116 fdc->dmachan); 2117 fdc->retry = 6; /* reset the beast */ 2118 return (retrier(fdc)); 2119 } 2120 if(st3 & NE7_ST3_WP) 2121 { 2122 /* 2123 * XXX YES! this is ugly. 2124 * in order to force the current operation 2125 * to fail, we will have to fake an FDC 2126 * error - all error handling is done 2127 * by the retrier() 2128 */ 2129 fdc->status[0] = NE7_ST0_IC_AT; 2130 fdc->status[1] = NE7_ST1_NW; 2131 fdc->status[2] = 0; 2132 fdc->status[3] = fd->track; 2133 fdc->status[4] = head; 2134 fdc->status[5] = sec; 2135 fdc->retry = 8; /* break out immediately */ 2136 fdc->state = IOTIMEDOUT; /* not really... */ 2137 return (1); /* will return immediately */ 2138 } 2139 } 2140 2141 if (format) { 2142 ne7cmd = NE7CMD_FORMAT | mfm; 2143 if (fdc->flags & FDC_NODMA) { 2144 /* 2145 * This seems to be necessary for 2146 * whatever obscure reason; if we omit 2147 * it, we end up filling the sector ID 2148 * fields of the newly formatted track 2149 * entirely with garbage, causing 2150 * `wrong cylinder' errors all over 2151 * the place when trying to read them 2152 * back. 2153 * 2154 * Umpf. 2155 */ 2156 SET_BCDR(fdc, 1, bp->bio_bcount, 0); 2157 2158 (void)fdcpio(fdc,bp->bio_cmd, 2159 bp->bio_data+fd->skip, 2160 bp->bio_bcount); 2161 2162 } 2163 /* formatting */ 2164 if(fd_cmd(fdc, 6, ne7cmd, head << 2 | fdu, 2165 finfo->fd_formb_secshift, 2166 finfo->fd_formb_nsecs, 2167 finfo->fd_formb_gaplen, 2168 finfo->fd_formb_fillbyte, 0)) { 2169 /* controller fell over */ 2170 if (!(fdc->flags & FDC_NODMA)) 2171 isa_dmadone(idf, 2172 bp->bio_data + fd->skip, 2173 format ? bp->bio_bcount : fdblk, 2174 fdc->dmachan); 2175 fdc->retry = 6; 2176 return (retrier(fdc)); 2177 } 2178 } else if (rdsectid) { 2179 ne7cmd = NE7CMD_READID | mfm; 2180 if (fd_cmd(fdc, 2, ne7cmd, head << 2 | fdu, 0)) { 2181 /* controller jamming */ 2182 fdc->retry = 6; 2183 return (retrier(fdc)); 2184 } 2185 } else { 2186 /* read or write operation */ 2187 ne7cmd = (read ? NE7CMD_READ | NE7CMD_SK : NE7CMD_WRITE) | mfm; 2188 if (fdc->flags & FDC_NODMA) { 2189 /* 2190 * This seems to be necessary even when 2191 * reading data. 2192 */ 2193 SET_BCDR(fdc, 1, fdblk, 0); 2194 2195 /* 2196 * Perform the write pseudo-DMA before 2197 * the WRITE command is sent. 2198 */ 2199 if (!read) 2200 (void)fdcpio(fdc,bp->bio_cmd, 2201 bp->bio_data+fd->skip, 2202 fdblk); 2203 } 2204 if (fd_cmd(fdc, 9, 2205 ne7cmd, 2206 head << 2 | fdu, /* head & unit */ 2207 fd->track, /* track */ 2208 head, 2209 sec, /* sector + 1 */ 2210 fd->ft->secsize, /* sector size */ 2211 sectrac, /* sectors/track */ 2212 fd->ft->gap, /* gap size */ 2213 fd->ft->datalen, /* data length */ 2214 0)) { 2215 /* the beast is sleeping again */ 2216 if (!(fdc->flags & FDC_NODMA)) 2217 isa_dmadone(idf, 2218 bp->bio_data + fd->skip, 2219 format ? bp->bio_bcount : fdblk, 2220 fdc->dmachan); 2221 fdc->retry = 6; 2222 return (retrier(fdc)); 2223 } 2224 } 2225 if (!rdsectid && (fdc->flags & FDC_NODMA)) 2226 /* 2227 * If this is a read, then simply await interrupt 2228 * before performing PIO. 2229 */ 2230 if (read && !fdcpio(fdc,bp->bio_cmd, 2231 bp->bio_data+fd->skip,fdblk)) { 2232 fd->tohandle = timeout(fd_iotimeout, fdc, hz); 2233 return(0); /* will return later */ 2234 } 2235 2236 /* 2237 * Write (or format) operation will fall through and 2238 * await completion interrupt. 2239 */ 2240 fdc->state = IOCOMPLETE; 2241 fd->tohandle = timeout(fd_iotimeout, fdc, hz); 2242 return (0); /* will return later */ 2243 2244 case PIOREAD: 2245 /* 2246 * Actually perform the PIO read. The IOCOMPLETE case 2247 * removes the timeout for us. 2248 */ 2249 (void)fdcpio(fdc,bp->bio_cmd,bp->bio_data+fd->skip,fdblk); 2250 fdc->state = IOCOMPLETE; 2251 /* FALLTHROUGH */ 2252 case IOCOMPLETE: /* IO done, post-analyze */ 2253 untimeout(fd_iotimeout, fdc, fd->tohandle); 2254 2255 if (fd_read_status(fdc)) { 2256 if (!rdsectid && !(fdc->flags & FDC_NODMA)) 2257 isa_dmadone(idf, bp->bio_data + fd->skip, 2258 format ? bp->bio_bcount : fdblk, 2259 fdc->dmachan); 2260 if (fdc->retry < 6) 2261 fdc->retry = 6; /* force a reset */ 2262 return (retrier(fdc)); 2263 } 2264 2265 fdc->state = IOTIMEDOUT; 2266 2267 /* FALLTHROUGH */ 2268 case IOTIMEDOUT: 2269 if (!rdsectid && !(fdc->flags & FDC_NODMA)) 2270 isa_dmadone(idf, bp->bio_data + fd->skip, 2271 format ? bp->bio_bcount : fdblk, fdc->dmachan); 2272 if (fdc->status[0] & NE7_ST0_IC) { 2273 if ((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT 2274 && fdc->status[1] & NE7_ST1_OR) { 2275 /* 2276 * DMA overrun. Someone hogged the bus and 2277 * didn't release it in time for the next 2278 * FDC transfer. 2279 * 2280 * We normally restart this without bumping 2281 * the retry counter. However, in case 2282 * something is seriously messed up (like 2283 * broken hardware), we rather limit the 2284 * number of retries so the IO operation 2285 * doesn't block indefinately. 2286 */ 2287 if (fdc->dma_overruns++ < FDC_DMAOV_MAX) { 2288 fdc->state = SEEKCOMPLETE; 2289 return (1);/* will return immediately */ 2290 } /* else fall through */ 2291 } 2292 if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_IV 2293 && fdc->retry < 6) 2294 fdc->retry = 6; /* force a reset */ 2295 else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT 2296 && fdc->status[2] & NE7_ST2_WC 2297 && fdc->retry < 3) 2298 fdc->retry = 3; /* force recalibrate */ 2299 return (retrier(fdc)); 2300 } 2301 /* All OK */ 2302 if (rdsectid) { 2303 /* copy out ID field contents */ 2304 idp = (struct fdc_readid *)bp->bio_data; 2305 idp->cyl = fdc->status[3]; 2306 idp->head = fdc->status[4]; 2307 idp->sec = fdc->status[5]; 2308 idp->secshift = fdc->status[6]; 2309 } 2310 /* Operation successful, retry DMA overruns again next time. */ 2311 fdc->dma_overruns = 0; 2312 fd->skip += fdblk; 2313 if (!rdsectid && !format && fd->skip < bp->bio_bcount) { 2314 /* set up next transfer */ 2315 fdc->state = DOSEEK; 2316 } else { 2317 /* ALL DONE */ 2318 fd->skip = 0; 2319 bp->bio_resid = 0; 2320 fdc->bp = NULL; 2321 device_unbusy(fd->dev); 2322 biofinish(bp, fd->device_stats, 0); 2323 fdc->fd = (fd_p) 0; 2324 fdc->fdu = -1; 2325 fdc->state = FINDWORK; 2326 } 2327 return (1); /* will return immediately */ 2328 2329 case RESETCTLR: 2330 fdc_reset(fdc); 2331 fdc->retry++; 2332 fdc->state = RESETCOMPLETE; 2333 return (0); /* will return later */ 2334 2335 case RESETCOMPLETE: 2336 /* 2337 * Discard all the results from the reset so that they 2338 * can't cause an unexpected interrupt later. 2339 */ 2340 for (i = 0; i < 4; i++) 2341 (void)fd_sense_int(fdc, &st0, &cyl); 2342 fdc->state = STARTRECAL; 2343 /* FALLTHROUGH */ 2344 case STARTRECAL: 2345 if(fd_cmd(fdc, 2, NE7CMD_RECAL, fdu, 0)) { 2346 /* arrgl */ 2347 fdc->retry = 6; 2348 return (retrier(fdc)); 2349 } 2350 fdc->state = RECALWAIT; 2351 return (0); /* will return later */ 2352 2353 case RECALWAIT: 2354 /* allow heads to settle */ 2355 timeout(fd_pseudointr, fdc, hz / 8); 2356 fdc->state = RECALCOMPLETE; 2357 return (0); /* will return later */ 2358 2359 case RECALCOMPLETE: 2360 do { 2361 /* 2362 * See SEEKCOMPLETE for a comment on this: 2363 */ 2364 if (fd_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID) 2365 return (0); /* will return later */ 2366 if(fdc->fdct == FDC_NE765 2367 && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC) 2368 return (0); /* hope for a real intr */ 2369 } while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC); 2370 if ((st0 & NE7_ST0_IC) != NE7_ST0_IC_NT || cyl != 0) 2371 { 2372 if(fdc->retry > 3) 2373 /* 2374 * A recalibrate from beyond cylinder 77 2375 * will "fail" due to the FDC limitations; 2376 * since people used to complain much about 2377 * the failure message, try not logging 2378 * this one if it seems to be the first 2379 * time in a line. 2380 */ 2381 printf("fd%d: recal failed ST0 %b cyl %d\n", 2382 fdu, st0, NE7_ST0BITS, cyl); 2383 if(fdc->retry < 3) fdc->retry = 3; 2384 return (retrier(fdc)); 2385 } 2386 fd->track = 0; 2387 /* Seek (probably) necessary */ 2388 fdc->state = DOSEEK; 2389 return (1); /* will return immediately */ 2390 2391 case MOTORWAIT: 2392 if(fd->flags & FD_MOTOR_WAIT) 2393 { 2394 return (0); /* time's not up yet */ 2395 } 2396 if (fdc->flags & FDC_NEEDS_RESET) { 2397 fdc->state = RESETCTLR; 2398 fdc->flags &= ~FDC_NEEDS_RESET; 2399 } else 2400 fdc->state = DOSEEK; 2401 return (1); /* will return immediately */ 2402 2403 default: 2404 device_printf(fdc->fdc_dev, "unexpected FD int->"); 2405 if (fd_read_status(fdc) == 0) 2406 printf("FDC status :%x %x %x %x %x %x %x ", 2407 fdc->status[0], 2408 fdc->status[1], 2409 fdc->status[2], 2410 fdc->status[3], 2411 fdc->status[4], 2412 fdc->status[5], 2413 fdc->status[6] ); 2414 else 2415 printf("No status available "); 2416 if (fd_sense_int(fdc, &st0, &cyl) != 0) 2417 { 2418 printf("[controller is dead now]\n"); 2419 return (0); /* will return later */ 2420 } 2421 printf("ST0 = %x, PCN = %x\n", st0, cyl); 2422 return (0); /* will return later */ 2423 } 2424 /* noone should ever get here */ 2425 } 2426 2427 static int 2428 retrier(struct fdc_data *fdc) 2429 { 2430 struct bio *bp; 2431 struct fd_data *fd; 2432 int fdu; 2433 2434 bp = fdc->bp; 2435 2436 /* XXX shouldn't this be cached somewhere? */ 2437 fd = bp->bio_dev->si_drv1; 2438 fdu = fd->fdu; 2439 if (fd->options & FDOPT_NORETRY) 2440 goto fail; 2441 2442 switch (fdc->retry) { 2443 case 0: case 1: case 2: 2444 fdc->state = SEEKCOMPLETE; 2445 break; 2446 case 3: case 4: case 5: 2447 fdc->state = STARTRECAL; 2448 break; 2449 case 6: 2450 fdc->state = RESETCTLR; 2451 break; 2452 case 7: 2453 break; 2454 default: 2455 fail: 2456 if ((fd->options & FDOPT_NOERRLOG) == 0) { 2457 disk_err(bp, "hard error", 2458 fdc->fd->skip / DEV_BSIZE, 0); 2459 if (fdc->flags & FDC_STAT_VALID) { 2460 printf( 2461 " (ST0 %b ST1 %b ST2 %b cyl %u hd %u sec %u)\n", 2462 fdc->status[0], NE7_ST0BITS, 2463 fdc->status[1], NE7_ST1BITS, 2464 fdc->status[2], NE7_ST2BITS, 2465 fdc->status[3], fdc->status[4], 2466 fdc->status[5]); 2467 } 2468 else 2469 printf(" (No status)\n"); 2470 } 2471 if ((fd->options & FDOPT_NOERROR) == 0) { 2472 bp->bio_flags |= BIO_ERROR; 2473 bp->bio_error = EIO; 2474 bp->bio_resid = bp->bio_bcount - fdc->fd->skip; 2475 } else 2476 bp->bio_resid = 0; 2477 fdc->bp = NULL; 2478 fdc->fd->skip = 0; 2479 device_unbusy(fd->dev); 2480 biofinish(bp, fdc->fd->device_stats, 0); 2481 fdc->state = FINDWORK; 2482 fdc->flags |= FDC_NEEDS_RESET; 2483 fdc->fd = (fd_p) 0; 2484 fdc->fdu = -1; 2485 return (1); 2486 } 2487 fdc->retry++; 2488 return (1); 2489 } 2490 2491 static void 2492 fdbiodone(struct bio *bp) 2493 { 2494 wakeup(bp); 2495 } 2496 2497 static int 2498 fdmisccmd(struct cdev *dev, u_int cmd, void *data) 2499 { 2500 fdu_t fdu; 2501 fd_p fd; 2502 struct bio *bp; 2503 struct fd_formb *finfo; 2504 struct fdc_readid *idfield; 2505 size_t fdblk; 2506 int error; 2507 2508 fd = dev->si_drv1; 2509 fdu = fd->fdu; 2510 fdblk = 128 << fd->ft->secsize; 2511 finfo = (struct fd_formb *)data; 2512 idfield = (struct fdc_readid *)data; 2513 2514 bp = malloc(sizeof(struct bio), M_TEMP, M_WAITOK | M_ZERO); 2515 2516 /* 2517 * Set up a bio request for fdstrategy(). bio_offset is faked 2518 * so that fdstrategy() will seek to the the requested 2519 * cylinder, and use the desired head. 2520 */ 2521 bp->bio_cmd = cmd; 2522 if (cmd == FDBIO_FORMAT) { 2523 bp->bio_offset = 2524 (finfo->cyl * (fd->ft->sectrac * fd->ft->heads) + 2525 finfo->head * fd->ft->sectrac) * fdblk; 2526 bp->bio_bcount = sizeof(struct fd_idfield_data) * 2527 finfo->fd_formb_nsecs; 2528 } else if (cmd == FDBIO_RDSECTID) { 2529 bp->bio_offset = 2530 (idfield->cyl * (fd->ft->sectrac * fd->ft->heads) + 2531 idfield->head * fd->ft->sectrac) * fdblk; 2532 bp->bio_bcount = sizeof(struct fdc_readid); 2533 } else 2534 panic("wrong cmd in fdmisccmd()"); 2535 bp->bio_data = data; 2536 bp->bio_dev = dev; 2537 bp->bio_done = fdbiodone; 2538 bp->bio_flags = 0; 2539 2540 /* Now run the command. */ 2541 fdstrategy(bp); 2542 error = biowait(bp, "fdcmd"); 2543 2544 free(bp, M_TEMP); 2545 return (error); 2546 } 2547 2548 static int 2549 fdioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td) 2550 { 2551 fdu_t fdu; 2552 fd_p fd; 2553 struct fdc_status *fsp; 2554 struct fdc_readid *rid; 2555 int error; 2556 2557 fd = dev->si_drv1; 2558 fdu = fd->fdu; 2559 2560 /* 2561 * First, handle everything that could be done with 2562 * FD_NONBLOCK still being set. 2563 */ 2564 switch (cmd) { 2565 2566 case DIOCGMEDIASIZE: 2567 if (fd->ft == 0) 2568 return ((fd->flags & FD_NONBLOCK) ? EAGAIN : ENXIO); 2569 *(off_t *)addr = (128 << (fd->ft->secsize)) * fd->ft->size; 2570 return (0); 2571 2572 case DIOCGSECTORSIZE: 2573 if (fd->ft == 0) 2574 return ((fd->flags & FD_NONBLOCK) ? EAGAIN : ENXIO); 2575 *(u_int *)addr = 128 << (fd->ft->secsize); 2576 return (0); 2577 2578 case FIONBIO: 2579 if (*(int *)addr != 0) 2580 fd->flags |= FD_NONBLOCK; 2581 else { 2582 if (fd->ft == 0) { 2583 /* 2584 * No drive type has been selected yet, 2585 * cannot turn FNONBLOCK off. 2586 */ 2587 return (EINVAL); 2588 } 2589 fd->flags &= ~FD_NONBLOCK; 2590 } 2591 return (0); 2592 2593 case FIOASYNC: 2594 /* keep the generic fcntl() code happy */ 2595 return (0); 2596 2597 case FD_GTYPE: /* get drive type */ 2598 if (fd->ft == 0) 2599 /* no type known yet, return the native type */ 2600 *(struct fd_type *)addr = fd_native_types[fd->type]; 2601 else 2602 *(struct fd_type *)addr = *fd->ft; 2603 return (0); 2604 2605 case FD_STYPE: /* set drive type */ 2606 /* 2607 * Allow setting drive type temporarily iff 2608 * currently unset. Used for fdformat so any 2609 * user can set it, and then start formatting. 2610 */ 2611 if (fd->ft) 2612 return (EINVAL); /* already set */ 2613 fd->fts[0] = *(struct fd_type *)addr; 2614 fd->ft = &fd->fts[0]; 2615 fd->flags |= FD_UA; 2616 return (0); 2617 2618 case FD_GOPTS: /* get drive options */ 2619 *(int *)addr = fd->options + FDOPT_AUTOSEL; 2620 return (0); 2621 2622 case FD_SOPTS: /* set drive options */ 2623 fd->options = *(int *)addr & ~FDOPT_AUTOSEL; 2624 return (0); 2625 2626 #ifdef FDC_DEBUG 2627 case FD_DEBUG: 2628 if ((fd_debug != 0) != (*(int *)addr != 0)) { 2629 fd_debug = (*(int *)addr != 0); 2630 printf("fd%d: debugging turned %s\n", 2631 fd->fdu, fd_debug ? "on" : "off"); 2632 } 2633 return (0); 2634 #endif 2635 2636 case FD_CLRERR: 2637 if (suser(td) != 0) 2638 return (EPERM); 2639 fd->fdc->fdc_errs = 0; 2640 return (0); 2641 2642 case FD_GSTAT: 2643 fsp = (struct fdc_status *)addr; 2644 if ((fd->fdc->flags & FDC_STAT_VALID) == 0) 2645 return (EINVAL); 2646 memcpy(fsp->status, fd->fdc->status, 7 * sizeof(u_int)); 2647 return (0); 2648 2649 case FD_GDTYPE: 2650 *(enum fd_drivetype *)addr = fd->type; 2651 return (0); 2652 } 2653 2654 /* 2655 * Now handle everything else. Make sure we have a valid 2656 * drive type. 2657 */ 2658 if (fd->flags & FD_NONBLOCK) 2659 return (EAGAIN); 2660 if (fd->ft == 0) 2661 return (ENXIO); 2662 error = 0; 2663 2664 switch (cmd) { 2665 2666 case FD_FORM: 2667 if ((flag & FWRITE) == 0) 2668 return (EBADF); /* must be opened for writing */ 2669 if (((struct fd_formb *)addr)->format_version != 2670 FD_FORMAT_VERSION) 2671 return (EINVAL); /* wrong version of formatting prog */ 2672 error = fdmisccmd(dev, FDBIO_FORMAT, addr); 2673 break; 2674 2675 case FD_GTYPE: /* get drive type */ 2676 *(struct fd_type *)addr = *fd->ft; 2677 break; 2678 2679 case FD_STYPE: /* set drive type */ 2680 /* this is considered harmful; only allow for superuser */ 2681 if (suser(td) != 0) 2682 return (EPERM); 2683 *fd->ft = *(struct fd_type *)addr; 2684 break; 2685 2686 case FD_GOPTS: /* get drive options */ 2687 *(int *)addr = fd->options; 2688 break; 2689 2690 case FD_SOPTS: /* set drive options */ 2691 fd->options = *(int *)addr; 2692 break; 2693 2694 #ifdef FDC_DEBUG 2695 case FD_DEBUG: 2696 if ((fd_debug != 0) != (*(int *)addr != 0)) { 2697 fd_debug = (*(int *)addr != 0); 2698 printf("fd%d: debugging turned %s\n", 2699 fd->fdu, fd_debug ? "on" : "off"); 2700 } 2701 break; 2702 #endif 2703 2704 case FD_CLRERR: 2705 if (suser(td) != 0) 2706 return (EPERM); 2707 fd->fdc->fdc_errs = 0; 2708 break; 2709 2710 case FD_GSTAT: 2711 fsp = (struct fdc_status *)addr; 2712 if ((fd->fdc->flags & FDC_STAT_VALID) == 0) 2713 return (EINVAL); 2714 memcpy(fsp->status, fd->fdc->status, 7 * sizeof(u_int)); 2715 break; 2716 2717 case FD_READID: 2718 rid = (struct fdc_readid *)addr; 2719 if (rid->cyl > MAX_CYLINDER || rid->head > MAX_HEAD) 2720 return (EINVAL); 2721 error = fdmisccmd(dev, FDBIO_RDSECTID, addr); 2722 break; 2723 2724 default: 2725 error = ENOTTY; 2726 break; 2727 } 2728 return (error); 2729 } 2730