1 /* 2 * Copyright (c) 1990 William F. Jolitz, TeleMuse 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This software is a component of "386BSD" developed by 16 * William F. Jolitz, TeleMuse. 17 * 4. Neither the name of the developer nor the name "386BSD" 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS A COMPONENT OF 386BSD DEVELOPED BY WILLIAM F. JOLITZ 22 * AND IS INTENDED FOR RESEARCH AND EDUCATIONAL PURPOSES ONLY. THIS 23 * SOFTWARE SHOULD NOT BE CONSIDERED TO BE A COMMERCIAL PRODUCT. 24 * THE DEVELOPER URGES THAT USERS WHO REQUIRE A COMMERCIAL PRODUCT 25 * NOT MAKE USE OF THIS WORK. 26 * 27 * FOR USERS WHO WISH TO UNDERSTAND THE 386BSD SYSTEM DEVELOPED 28 * BY WILLIAM F. JOLITZ, WE RECOMMEND THE USER STUDY WRITTEN 29 * REFERENCES SUCH AS THE "PORTING UNIX TO THE 386" SERIES 30 * (BEGINNING JANUARY 1991 "DR. DOBBS JOURNAL", USA AND BEGINNING 31 * JUNE 1991 "UNIX MAGAZIN", GERMANY) BY WILLIAM F. JOLITZ AND 32 * LYNNE GREER JOLITZ, AS WELL AS OTHER BOOKS ON UNIX AND THE 33 * ON-LINE 386BSD USER MANUAL BEFORE USE. A BOOK DISCUSSING THE INTERNALS 34 * OF 386BSD ENTITLED "386BSD FROM THE INSIDE OUT" WILL BE AVAILABLE LATE 1992. 35 * 36 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPER ``AS IS'' AND 37 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 39 * ARE DISCLAIMED. IN NO EVENT SHALL THE DEVELOPER BE LIABLE 40 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 41 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 42 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 44 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 45 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 46 * SUCH DAMAGE. 47 * 48 * $Id: lpt.c,v 1.5 1993/06/05 22:58:31 cgd Exp $ 49 */ 50 51 /* 52 * Device Driver for AT parallel printer port 53 * Written by William Jolitz 12/18/90 54 * 55 * Hacked heavily by Rod Grimes and Eric Haug... 56 */ 57 58 #include "lpt.h" 59 #if NLPT > 0 60 61 #include "param.h" 62 #include "systm.h" 63 #include "proc.h" 64 #include "user.h" 65 #include "buf.h" 66 #include "kernel.h" 67 #include "ioctl.h" 68 #include "tty.h" 69 #include "uio.h" 70 71 #include "i386/isa/isa.h" 72 #include "i386/isa/isa_device.h" 73 #include "i386/isa/lptreg.h" 74 75 #define LPINITRDY 4 /* wait up to 4 seconds for a ready */ 76 #define LPTOUTTIME 4 /* wait up to 4 seconds for a ready */ 77 #define LPPRI (PZERO+8) 78 #define BUFSIZE 1024 79 80 #ifndef DEBUG 81 #define lprintf 82 #else 83 #define lprintf if (lptflag) printf 84 int lptflag = 1; 85 #endif 86 87 int lptout(); 88 #ifdef DEBUG 89 int lptflag = 1; 90 #endif 91 92 int lptprobe(), lptattach(), lptintr(); 93 94 struct isa_driver lptdriver = { 95 lptprobe, lptattach, "lpt" 96 }; 97 98 #define LPTUNIT(s) (((s)>>6)&0x3) 99 #define LPTFLAGS(s) ((s)&0x3f) 100 101 struct lpt_softc { 102 short sc_port; 103 short sc_state; 104 /* default case: negative prime, negative ack, handshake strobe, 105 prime once */ 106 u_char sc_control; 107 char sc_flags; 108 #define LP_POS_INIT 0x01 /* if we are a postive init signal */ 109 #define LP_POS_ACK 0x02 /* if we are a positive going ack */ 110 #define LP_NO_PRIME 0x04 /* don't prime the printer at all */ 111 #define LP_PRIMEOPEN 0x08 /* prime on every open */ 112 #define LP_AUTOLF 0x10 /* tell printer to do an automatic lf */ 113 #define LP_BYPASS 0x20 /* bypass printer ready checks */ 114 struct buf *sc_inbuf; 115 short sc_xfercnt ; 116 char sc_primed; 117 char *sc_cp ; 118 } lpt_sc[NLPT] ; 119 120 /* bits for state */ 121 #define OPEN (1<<0) /* device is open */ 122 #define ASLP (1<<1) /* awaiting draining of printer */ 123 #define ERROR (1<<2) /* error was received from printer */ 124 #define OBUSY (1<<3) /* printer is busy doing output */ 125 #define LPTOUT (1<<4) /* timeout while not selected */ 126 #define TOUT (1<<5) /* timeout while not selected */ 127 #define INIT (1<<6) /* waiting to initialize for open */ 128 129 /* 130 * Internal routine to lptprobe to do port tests of one byte value 131 */ 132 int 133 lpt_port_test(short port, u_char data, u_char mask) 134 { 135 int temp, timeout; 136 137 data = data & mask; 138 outb(port, data); 139 timeout = 100; 140 do 141 temp = inb(port) & mask; 142 while (temp != data && --timeout); 143 lprintf("Port 0x%x\tout=%x\tin=%x\n", port, data, temp); 144 return (temp == data); 145 } 146 147 /* 148 * New lptprobe routine written by Rodney W. Grimes, 3/25/1993 149 * 150 * Logic: 151 * 1) You should be able to write to and read back the same value 152 * to the data port. Do an alternating zeros, alternating ones, 153 * walking zero, and walking one test to check for stuck bits. 154 * 155 * 2) You should be able to write to and read back the same value 156 * to the control port lower 5 bits, the upper 3 bits are reserved 157 * per the IBM PC technical reference manauls and different boards 158 * do different things with them. Do an alternating zeros, alternating 159 * ones, walking zero, and walking one test to check for stuck bits. 160 * 161 * Some printers drag the strobe line down when the are powered off 162 * so this bit has been masked out of the control port test. 163 * 164 * XXX Some printers may not like a fast pulse on init or strobe, I 165 * don't know at this point, if that becomes a problem these bits 166 * should be turned off in the mask byte for the control port test. 167 * 168 * 3) Set the data and control ports to a value of 0 169 */ 170 171 int 172 lptprobe(struct isa_device *dvp) 173 { 174 int status; 175 short port; 176 u_char data; 177 u_char mask; 178 int i; 179 180 status = IO_LPTSIZE; 181 182 port = dvp->id_iobase + lpt_data; 183 mask = 0xff; 184 while (mask != 0) 185 { 186 data = 0x55; /* Alternating zeros */ 187 if (!lpt_port_test(port, data, mask)) status = 0; 188 189 data = 0xaa; /* Alternating ones */ 190 if (!lpt_port_test(port, data, mask)) status = 0; 191 192 for (i = 0; i < 8; i++) /* Walking zero */ 193 { 194 data = ~(1 << i); 195 if (!lpt_port_test(port, data, mask)) status = 0; 196 } 197 198 for (i = 0; i < 8; i++) /* Walking one */ 199 { 200 data = (1 << i); 201 if (!lpt_port_test(port, data, mask)) status = 0; 202 } 203 204 if (port == dvp->id_iobase + lpt_data) 205 { 206 port = dvp->id_iobase + lpt_control; 207 mask = 0x1e; 208 } 209 else 210 mask = 0; 211 } 212 outb(dvp->id_iobase+lpt_data, 0); 213 outb(dvp->id_iobase+lpt_control, 0); 214 return (status); 215 } 216 217 lptattach(isdp) 218 struct isa_device *isdp; 219 { 220 struct lpt_softc *sc; 221 222 sc = lpt_sc + isdp->id_unit; 223 sc->sc_port = isdp->id_iobase; 224 outb(sc->sc_port+lpt_control, LPC_NINIT); 225 return (1); 226 } 227 228 /* 229 * lptopen -- reset the printer, then wait until it's selected and not busy. 230 */ 231 232 lptopen(dev, flag) 233 dev_t dev; 234 int flag; 235 { 236 struct lpt_softc *sc = lpt_sc + LPTUNIT(minor(dev)); 237 int s; 238 int trys, port; 239 240 if (sc->sc_state) { 241 lprintf("lp: still open\n") ; 242 printf("still open %x\n", sc->sc_state); 243 return(EBUSY); 244 } else sc->sc_state |= INIT; 245 246 s = spltty(); 247 sc->sc_flags = LPTFLAGS(minor(dev)); 248 lprintf("lp flags 0x%x\n", sc->sc_flags); 249 port = sc->sc_port; 250 251 /* init printer */ 252 if((sc->sc_flags & LP_NO_PRIME) == 0) { 253 if((sc->sc_flags & LP_PRIMEOPEN) || sc->sc_primed == 0) { 254 outb(port+lpt_control, 0); 255 sc->sc_primed++; 256 DELAY(500); 257 } 258 } 259 outb(port+lpt_control, LPC_SEL|LPC_NINIT); 260 261 /* wait till ready (printer running diagnostics) */ 262 trys = 0; 263 do { 264 /* ran out of waiting for the printer */ 265 if (trys++ >= LPINITRDY*4) { 266 splx(s); 267 sc->sc_state = 0; 268 printf ("status %x\n", inb(port+lpt_status) ); 269 return (EBUSY); 270 } 271 272 /* wait 1/4 second, give up if we get a signal */ 273 if (tsleep (sc, LPPRI|PCATCH, "lptinit", hz/4) != EWOULDBLOCK) { 274 sc->sc_state = 0; 275 splx(s); 276 return (EBUSY); 277 } 278 279 /* is printer online and ready for output */ 280 } while ((inb(port+lpt_status) & (LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR)) != 281 (LPS_SEL|LPS_NBSY|LPS_NERR)); 282 283 if(sc->sc_flags&LP_AUTOLF) { 284 outb(port+lpt_control, LPC_SEL|LPC_NINIT|LPC_ENA|LPC_AUTOL); 285 sc->sc_control = LPC_SEL|LPC_NINIT|LPC_ENA|LPC_AUTOL; 286 } else { 287 outb(port+lpt_control, LPC_SEL|LPC_NINIT|LPC_ENA); 288 sc->sc_control = LPC_SEL|LPC_NINIT|LPC_ENA; 289 } 290 291 sc->sc_state = OPEN | TOUT; 292 sc->sc_inbuf = geteblk(BUFSIZE); 293 sc->sc_xfercnt = 0; 294 splx(s); 295 timeout (lptout, sc, hz/2); 296 lprintf("opened.\n"); 297 return(0); 298 } 299 300 lptout (sc) 301 struct lpt_softc *sc; 302 { int pl; 303 304 lprintf ("T %x ", inb(sc->sc_port+lpt_status)); 305 if (sc->sc_state&OPEN) 306 timeout (lptout, sc, hz/2); 307 else sc->sc_state &= ~TOUT; 308 309 if (sc->sc_state & ERROR) 310 sc->sc_state &= ~ERROR; 311 312 /* 313 * Avoid possible hangs do to missed interrupts 314 */ 315 if (sc->sc_xfercnt) { 316 pl = spltty(); 317 lptintr(sc - lpt_sc); 318 splx(pl); 319 } else { 320 sc->sc_state &= ~OBUSY; 321 wakeup((caddr_t)sc); 322 } 323 } 324 325 /* 326 * lptclose -- close the device, free the local line buffer. 327 */ 328 329 lptclose(dev, flag) 330 int flag; 331 { 332 struct lpt_softc *sc = lpt_sc + LPTUNIT(minor(dev)); 333 int port = sc->sc_port; 334 335 sc->sc_state &= ~OPEN; 336 while ((inb(port+lpt_status) & (LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR)) != 337 (LPS_SEL|LPS_NBSY|LPS_NERR) || sc->sc_xfercnt) 338 /* wait 1/4 second, give up if we get a signal */ 339 if (tsleep (sc, LPPRI|PCATCH, "lpclose", hz) != EWOULDBLOCK) 340 break; 341 342 sc->sc_state = 0; 343 sc->sc_xfercnt = 0; 344 outb(sc->sc_port+lpt_control, LPC_NINIT); 345 brelse(sc->sc_inbuf); 346 lprintf("closed.\n"); 347 return(0); 348 } 349 350 /* 351 * lptwrite --copy a line from user space to a local buffer, then call 352 * putc to get the chars moved to the output queue. 353 */ 354 355 lptwrite(dev, uio) 356 dev_t dev; 357 struct uio *uio; 358 { 359 register unsigned n; 360 int pl, err; 361 struct lpt_softc *sc = lpt_sc + LPTUNIT(minor(dev)); 362 363 while (n = MIN(BUFSIZE, uio->uio_resid)) { 364 sc->sc_cp = sc->sc_inbuf->b_un.b_addr ; 365 uiomove(sc->sc_cp, n, uio); 366 sc->sc_xfercnt = n ; 367 while (sc->sc_xfercnt > 0) { 368 /* if the printer is ready for a char, give it one */ 369 if ((sc->sc_state & OBUSY) == 0){ 370 lprintf("\nC %d. ", sc->sc_xfercnt); 371 pl = spltty(); 372 lptintr(sc - lpt_sc); 373 (void) splx(pl); 374 } 375 lprintf("W "); 376 if (err = tsleep (sc, LPPRI|PCATCH, "lpwrite", 0)) 377 return(err); 378 } 379 } 380 return(0); 381 } 382 383 /* 384 * lptintr -- handle printer interrupts which occur when the printer is 385 * ready to accept another char. 386 */ 387 388 lptintr(unit) 389 { 390 struct lpt_softc *sc = lpt_sc + unit; 391 int port = sc->sc_port,sts; 392 393 /* is printer online and ready for output */ 394 if (((sts=inb(port+lpt_status)) & (LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR/*|LPS_NACK*/)) == 395 (LPS_SEL|LPS_NBSY|LPS_NERR)) { 396 /* is this a false interrupt ? */ 397 if ((sc->sc_state & OBUSY) 398 && (sts & LPS_NACK) == 0) return; 399 sc->sc_state |= OBUSY; sc->sc_state &= ~ERROR; 400 401 if (sc->sc_xfercnt) { 402 /* send char */ 403 /*lprintf("%x ", *sc->sc_cp); */ 404 outb(port+lpt_data, *sc->sc_cp++) ; sc->sc_xfercnt-- ; 405 outb(port+lpt_control, sc->sc_control|LPC_STB); 406 /* DELAY(X) */ 407 outb(port+lpt_control, sc->sc_control); 408 } 409 410 /* any more bytes for the printer? */ 411 if (sc->sc_xfercnt > 0) return; 412 413 /* none, wake up the top half to get more */ 414 sc->sc_state &= ~OBUSY; 415 wakeup((caddr_t)sc); 416 lprintf("w "); 417 return; 418 } else sc->sc_state |= ERROR; 419 lprintf("sts %x ", sts); 420 } 421 422 int 423 lptioctl(dev_t dev, int cmd, caddr_t data, int flag) 424 { 425 int error; 426 427 error = 0; 428 switch (cmd) { 429 #ifdef THISISASAMPLE 430 case XXX: 431 dothis; andthis; andthat; 432 error=x; 433 break; 434 #endif /* THISISASAMPLE */ 435 default: 436 error = ENODEV; 437 } 438 439 return(error); 440 } 441 442 #endif /* NLPT */ 443