1 /* $NetBSD: lptvar.h,v 1.25 1994/11/18 22:03:28 mycroft Exp $ */ 2 3 /* 4 * Copyright (c) 1993, 1994 Charles Hannum. 5 * Copyright (c) 1990 William F. Jolitz, TeleMuse 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This software is a component of "386BSD" developed by 19 * William F. Jolitz, TeleMuse. 20 * 4. Neither the name of the developer nor the name "386BSD" 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS A COMPONENT OF 386BSD DEVELOPED BY WILLIAM F. JOLITZ 25 * AND IS INTENDED FOR RESEARCH AND EDUCATIONAL PURPOSES ONLY. THIS 26 * SOFTWARE SHOULD NOT BE CONSIDERED TO BE A COMMERCIAL PRODUCT. 27 * THE DEVELOPER URGES THAT USERS WHO REQUIRE A COMMERCIAL PRODUCT 28 * NOT MAKE USE OF THIS WORK. 29 * 30 * FOR USERS WHO WISH TO UNDERSTAND THE 386BSD SYSTEM DEVELOPED 31 * BY WILLIAM F. JOLITZ, WE RECOMMEND THE USER STUDY WRITTEN 32 * REFERENCES SUCH AS THE "PORTING UNIX TO THE 386" SERIES 33 * (BEGINNING JANUARY 1991 "DR. DOBBS JOURNAL", USA AND BEGINNING 34 * JUNE 1991 "UNIX MAGAZIN", GERMANY) BY WILLIAM F. JOLITZ AND 35 * LYNNE GREER JOLITZ, AS WELL AS OTHER BOOKS ON UNIX AND THE 36 * ON-LINE 386BSD USER MANUAL BEFORE USE. A BOOK DISCUSSING THE INTERNALS 37 * OF 386BSD ENTITLED "386BSD FROM THE INSIDE OUT" WILL BE AVAILABLE LATE 1992. 38 * 39 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPER ``AS IS'' AND 40 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 42 * ARE DISCLAIMED. IN NO EVENT SHALL THE DEVELOPER BE LIABLE 43 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 44 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 45 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 46 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 47 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 48 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 49 * SUCH DAMAGE. 50 */ 51 52 /* 53 * Device Driver for AT parallel printer port 54 */ 55 56 #include <sys/param.h> 57 #include <sys/systm.h> 58 #include <sys/proc.h> 59 #include <sys/user.h> 60 #include <sys/buf.h> 61 #include <sys/kernel.h> 62 #include <sys/ioctl.h> 63 #include <sys/uio.h> 64 #include <sys/device.h> 65 #include <sys/syslog.h> 66 67 #include <machine/cpu.h> 68 #include <machine/pio.h> 69 70 #include <i386/isa/isavar.h> 71 #include <i386/isa/lptreg.h> 72 73 #define TIMEOUT hz*16 /* wait up to 16 seconds for a ready */ 74 #define STEP hz/4 75 76 #define LPTPRI (PZERO+8) 77 #define LPT_BSIZE 1024 78 79 #if !defined(DEBUG) || !defined(notdef) 80 #define lprintf 81 #else 82 #define lprintf if (lptdebug) printf 83 int lptdebug = 1; 84 #endif 85 86 struct lpt_softc { 87 struct device sc_dev; 88 struct intrhand sc_ih; 89 90 size_t sc_count; 91 struct buf *sc_inbuf; 92 u_char *sc_cp; 93 int sc_spinmax; 94 int sc_iobase; 95 int sc_irq; 96 u_char sc_state; 97 #define LPT_OPEN 0x01 /* device is open */ 98 #define LPT_OBUSY 0x02 /* printer is busy doing output */ 99 #define LPT_INIT 0x04 /* waiting to initialize for open */ 100 u_char sc_flags; 101 #define LPT_AUTOLF 0x20 /* automatic LF on CR */ 102 #define LPT_NOPRIME 0x40 /* don't prime on open */ 103 #define LPT_NOINTR 0x80 /* do not use interrupt */ 104 u_char sc_control; 105 u_char sc_laststatus; 106 }; 107 108 int lptprobe __P((struct device *, void *, void *)); 109 void lptattach __P((struct device *, struct device *, void *)); 110 int lptintr __P((struct lpt_softc *)); 111 112 struct cfdriver lptcd = { 113 NULL, "lpt", lptprobe, lptattach, DV_TTY, sizeof(struct lpt_softc) 114 }; 115 116 #define LPTUNIT(s) (minor(s) & 0x1f) 117 #define LPTFLAGS(s) (minor(s) & 0xe0) 118 119 #define LPS_INVERT (LPS_SELECT|LPS_NERR|LPS_NBSY|LPS_NACK) 120 #define LPS_MASK (LPS_SELECT|LPS_NERR|LPS_NBSY|LPS_NACK|LPS_NOPAPER) 121 #define NOT_READY() ((inb(iobase + lpt_status) ^ LPS_INVERT) & LPS_MASK) 122 #define NOT_READY_ERR() not_ready(inb(iobase + lpt_status), sc) 123 static int not_ready __P((u_char, struct lpt_softc *)); 124 125 static void lptwakeup __P((void *arg)); 126 static int pushbytes __P((struct lpt_softc *)); 127 128 /* 129 * Internal routine to lptprobe to do port tests of one byte value. 130 */ 131 int 132 lpt_port_test(port, data, mask) 133 int port; 134 u_char data, mask; 135 { 136 int timeout; 137 u_char temp; 138 139 data &= mask; 140 outb(port, data); 141 timeout = 1000; 142 do { 143 delay(10); 144 temp = inb(port) & mask; 145 } while (temp != data && --timeout); 146 lprintf("lpt: port=0x%x out=0x%x in=0x%x timeout=%d\n", port, data, 147 temp, timeout); 148 return (temp == data); 149 } 150 151 /* 152 * Logic: 153 * 1) You should be able to write to and read back the same value 154 * to the data port. Do an alternating zeros, alternating ones, 155 * walking zero, and walking one test to check for stuck bits. 156 * 157 * 2) You should be able to write to and read back the same value 158 * to the control port lower 5 bits, the upper 3 bits are reserved 159 * per the IBM PC technical reference manauls and different boards 160 * do different things with them. Do an alternating zeros, alternating 161 * ones, walking zero, and walking one test to check for stuck bits. 162 * 163 * Some printers drag the strobe line down when the are powered off 164 * so this bit has been masked out of the control port test. 165 * 166 * XXX Some printers may not like a fast pulse on init or strobe, I 167 * don't know at this point, if that becomes a problem these bits 168 * should be turned off in the mask byte for the control port test. 169 * 170 * 3) Set the data and control ports to a value of 0 171 */ 172 int 173 lptprobe(parent, match, aux) 174 struct device *parent; 175 void *match, *aux; 176 { 177 struct isa_attach_args *ia = aux; 178 int iobase = ia->ia_iobase; 179 int port; 180 u_char mask, data; 181 int i; 182 183 #ifdef DEBUG 184 #define ABORT do {printf("lptprobe: mask %x data %x failed\n", mask, data); \ 185 return 0;} while (0) 186 #else 187 #define ABORT return 0 188 #endif 189 190 port = iobase + lpt_data; 191 mask = 0xff; 192 193 for (;;) { 194 data = 0x55; /* Alternating zeros */ 195 if (!lpt_port_test(port, data, mask)) 196 ABORT; 197 198 data = 0xaa; /* Alternating ones */ 199 if (!lpt_port_test(port, data, mask)) 200 ABORT; 201 202 for (i = 0; i < CHAR_BIT; i++) { /* Walking zero */ 203 data = ~(1 << i); 204 if (!lpt_port_test(port, data, mask)) 205 ABORT; 206 } 207 208 for (i = 0; i < CHAR_BIT; i++) { /* Walking one */ 209 data = (1 << i); 210 if (!lpt_port_test(port, data, mask)) 211 ABORT; 212 } 213 214 if (port == iobase + lpt_data) { 215 port = iobase + lpt_control; 216 #if 1 /* XXX */ 217 mask = 0x04; 218 #else 219 mask = 0x1e; 220 #endif 221 } else 222 break; 223 } 224 outb(iobase + lpt_data, 0); 225 outb(iobase + lpt_control, 0); 226 227 ia->ia_iosize = LPT_NPORTS; 228 ia->ia_msize = 0; 229 return 1; 230 } 231 232 void 233 lptattach(parent, self, aux) 234 struct device *parent, *self; 235 void *aux; 236 { 237 struct lpt_softc *sc = (void *)self; 238 struct isa_attach_args *ia = aux; 239 int iobase = ia->ia_iobase; 240 241 if (ia->ia_irq != IRQUNK) 242 printf("\n"); 243 else 244 printf(": polled\n"); 245 246 sc->sc_iobase = iobase; 247 sc->sc_irq = ia->ia_irq; 248 sc->sc_state = 0; 249 outb(iobase + lpt_control, LPC_NINIT); 250 251 if (ia->ia_irq != IRQUNK) { 252 sc->sc_ih.ih_fun = lptintr; 253 sc->sc_ih.ih_arg = sc; 254 sc->sc_ih.ih_level = IPL_NONE; 255 intr_establish(ia->ia_irq, &sc->sc_ih); 256 } 257 } 258 259 /* 260 * Reset the printer, then wait until it's selected and not busy. 261 */ 262 int 263 lptopen(dev, flag) 264 dev_t dev; 265 int flag; 266 { 267 int unit = LPTUNIT(dev); 268 u_char flags = LPTFLAGS(dev); 269 struct lpt_softc *sc; 270 int iobase; 271 u_char control; 272 int error; 273 int spin; 274 275 if (unit >= lptcd.cd_ndevs) 276 return ENXIO; 277 sc = lptcd.cd_devs[unit]; 278 if (!sc) 279 return ENXIO; 280 281 if (sc->sc_irq == IRQUNK && (flags & LPT_NOINTR) == 0) 282 return ENXIO; 283 284 #ifdef DIAGNOSTIC 285 if (sc->sc_state) 286 printf("%s: stat=0x%x not zero\n", sc->sc_dev.dv_xname, 287 sc->sc_state); 288 #endif 289 290 if (sc->sc_state) 291 return EBUSY; 292 293 sc->sc_state = LPT_INIT; 294 sc->sc_flags = flags; 295 lprintf("%s: open: flags=0x%x\n", sc->sc_dev.dv_xname, flags); 296 iobase = sc->sc_iobase; 297 298 if ((flags & LPT_NOPRIME) == 0) { 299 /* assert INIT for 100 usec to start up printer */ 300 outb(iobase + lpt_control, LPC_SELECT); 301 delay(100); 302 } 303 304 control = LPC_SELECT | LPC_NINIT; 305 outb(iobase + lpt_control, control); 306 307 /* wait till ready (printer running diagnostics) */ 308 for (spin = 0; NOT_READY_ERR(); spin += STEP) { 309 if (spin >= TIMEOUT) { 310 sc->sc_state = 0; 311 return EBUSY; 312 } 313 314 /* wait 1/4 second, give up if we get a signal */ 315 if (error = tsleep((caddr_t)sc, LPTPRI | PCATCH, "lptopen", 316 STEP) != EWOULDBLOCK) { 317 sc->sc_state = 0; 318 return error; 319 } 320 } 321 322 if ((flags & LPT_NOINTR) == 0) 323 control |= LPC_IENABLE; 324 if (flags & LPT_AUTOLF) 325 control |= LPC_AUTOLF; 326 sc->sc_control = control; 327 outb(iobase + lpt_control, control); 328 329 sc->sc_inbuf = geteblk(LPT_BSIZE); 330 sc->sc_count = 0; 331 sc->sc_state = LPT_OPEN; 332 333 if ((sc->sc_flags & LPT_NOINTR) == 0) 334 lptwakeup(sc); 335 336 lprintf("%s: opened\n", sc->sc_dev.dv_xname); 337 return 0; 338 } 339 340 int 341 not_ready(status, sc) 342 u_char status; 343 struct lpt_softc *sc; 344 { 345 u_char new; 346 347 status = (status ^ LPS_INVERT) & LPS_MASK; 348 new = status & ~sc->sc_laststatus; 349 sc->sc_laststatus = status; 350 351 if (new & LPS_NOPAPER) 352 log(LOG_NOTICE, "%s: out of paper\n", sc->sc_dev.dv_xname); 353 else if (new & LPS_SELECT) 354 log(LOG_NOTICE, "%s: offline\n", sc->sc_dev.dv_xname); 355 else if (new & LPS_NERR) 356 log(LOG_NOTICE, "%s: output error\n", sc->sc_dev.dv_xname); 357 358 return status; 359 } 360 361 void 362 lptwakeup(arg) 363 void *arg; 364 { 365 struct lpt_softc *sc = arg; 366 int s; 367 368 s = spltty(); 369 lptintr(sc); 370 splx(s); 371 372 timeout(lptwakeup, sc, STEP); 373 } 374 375 /* 376 * Close the device, and free the local line buffer. 377 */ 378 lptclose(dev, flag) 379 dev_t dev; 380 int flag; 381 { 382 int unit = LPTUNIT(dev); 383 struct lpt_softc *sc = lptcd.cd_devs[unit]; 384 int iobase = sc->sc_iobase; 385 386 if (sc->sc_count) 387 (void) pushbytes(sc); 388 389 if ((sc->sc_flags & LPT_NOINTR) == 0) 390 untimeout(lptwakeup, sc); 391 392 outb(iobase + lpt_control, LPC_NINIT); 393 sc->sc_state = 0; 394 outb(iobase + lpt_control, LPC_NINIT); 395 brelse(sc->sc_inbuf); 396 397 lprintf("%s: closed\n", sc->sc_dev.dv_xname); 398 return 0; 399 } 400 401 int 402 pushbytes(sc) 403 struct lpt_softc *sc; 404 { 405 int iobase = sc->sc_iobase; 406 int error; 407 408 if (sc->sc_flags & LPT_NOINTR) { 409 int spin, tic; 410 u_char control = sc->sc_control; 411 412 while (sc->sc_count > 0) { 413 spin = 0; 414 while (NOT_READY()) { 415 if (++spin < sc->sc_spinmax) 416 continue; 417 tic = 0; 418 /* adapt busy-wait algorithm */ 419 sc->sc_spinmax++; 420 while (NOT_READY_ERR()) { 421 /* exponential backoff */ 422 tic = tic + tic + 1; 423 if (tic > TIMEOUT) 424 tic = TIMEOUT; 425 error = tsleep((caddr_t)sc, 426 LPTPRI | PCATCH, "lptpsh", tic); 427 if (error != EWOULDBLOCK) 428 return error; 429 } 430 break; 431 } 432 433 outb(iobase + lpt_data, *sc->sc_cp++); 434 outb(iobase + lpt_control, control | LPC_STROBE); 435 sc->sc_count--; 436 outb(iobase + lpt_control, control); 437 438 /* adapt busy-wait algorithm */ 439 if (spin*2 + 16 < sc->sc_spinmax) 440 sc->sc_spinmax--; 441 } 442 } else { 443 int s; 444 445 while (sc->sc_count > 0) { 446 /* if the printer is ready for a char, give it one */ 447 if ((sc->sc_state & LPT_OBUSY) == 0) { 448 lprintf("%s: write %d\n", sc->sc_dev.dv_xname, 449 sc->sc_count); 450 s = spltty(); 451 (void) lptintr(sc); 452 splx(s); 453 } 454 if (error = tsleep((caddr_t)sc, LPTPRI | PCATCH, 455 "lptwrite2", 0)) 456 return error; 457 } 458 } 459 return 0; 460 } 461 462 /* 463 * Copy a line from user space to a local buffer, then call putc to get the 464 * chars moved to the output queue. 465 */ 466 lptwrite(dev, uio) 467 dev_t dev; 468 struct uio *uio; 469 { 470 struct lpt_softc *sc = lptcd.cd_devs[LPTUNIT(dev)]; 471 size_t n; 472 int error = 0; 473 474 while (n = min(LPT_BSIZE, uio->uio_resid)) { 475 uiomove(sc->sc_cp = sc->sc_inbuf->b_data, n, uio); 476 sc->sc_count = n; 477 error = pushbytes(sc); 478 if (error) { 479 /* 480 * Return accurate residual if interrupted or timed 481 * out. 482 */ 483 uio->uio_resid += sc->sc_count; 484 sc->sc_count = 0; 485 return error; 486 } 487 } 488 return 0; 489 } 490 491 /* 492 * Handle printer interrupts which occur when the printer is ready to accept 493 * another char. 494 */ 495 int 496 lptintr(sc) 497 struct lpt_softc *sc; 498 { 499 int iobase = sc->sc_iobase; 500 501 #if 0 502 if ((sc->sc_state & LPT_OPEN) == 0) 503 return 0; 504 #endif 505 506 /* is printer online and ready for output */ 507 if (NOT_READY() && NOT_READY_ERR()) 508 return 0; 509 510 if (sc->sc_count) { 511 u_char control = sc->sc_control; 512 /* send char */ 513 outb(iobase + lpt_data, *sc->sc_cp++); 514 outb(iobase + lpt_control, control | LPC_STROBE); 515 sc->sc_count--; 516 outb(iobase + lpt_control, control); 517 sc->sc_state |= LPT_OBUSY; 518 } else 519 sc->sc_state &= ~LPT_OBUSY; 520 521 if (sc->sc_count == 0) { 522 /* none, wake up the top half to get more */ 523 wakeup((caddr_t)sc); 524 } 525 526 return 1; 527 } 528 529 int 530 lptioctl(dev, cmd, data, flag) 531 dev_t dev; 532 u_long cmd; 533 caddr_t data; 534 int flag; 535 { 536 int error = 0; 537 538 switch (cmd) { 539 default: 540 error = ENODEV; 541 } 542 543 return error; 544 } 545