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