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