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: lptvar.h,v 1.8 1993/09/28 21:26:59 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 sc->sc_state = 0; 225 outb(sc->sc_port+lpt_control, LPC_NINIT); 226 return (1); 227 } 228 229 /* 230 * lptopen -- reset the printer, then wait until it's selected and not busy. 231 */ 232 233 lptopen(dev, flag) 234 dev_t dev; 235 int flag; 236 { 237 struct lpt_softc *sc = lpt_sc + LPTUNIT(minor(dev)); 238 int s; 239 int trys, port; 240 241 if (sc->sc_state) { 242 lprintf("lp: 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 lprintf ("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((caddr_t)sc, 274 LPPRI|PCATCH, "lptinit", hz/4) != EWOULDBLOCK) { 275 sc->sc_state = 0; 276 splx(s); 277 return (EBUSY); 278 } 279 280 /* is printer online and ready for output */ 281 } while ((inb(port+lpt_status) & (LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR)) != 282 (LPS_SEL|LPS_NBSY|LPS_NERR)); 283 284 if(sc->sc_flags&LP_AUTOLF) { 285 outb(port+lpt_control, LPC_SEL|LPC_NINIT|LPC_ENA|LPC_AUTOL); 286 sc->sc_control = LPC_SEL|LPC_NINIT|LPC_ENA|LPC_AUTOL; 287 } else { 288 outb(port+lpt_control, LPC_SEL|LPC_NINIT|LPC_ENA); 289 sc->sc_control = LPC_SEL|LPC_NINIT|LPC_ENA; 290 } 291 292 sc->sc_state = OPEN | TOUT; 293 sc->sc_inbuf = geteblk(BUFSIZE); 294 sc->sc_xfercnt = 0; 295 splx(s); 296 timeout((timeout_t)lptout, (caddr_t)sc, hz/2); 297 lprintf("opened.\n"); 298 return(0); 299 } 300 301 lptout (sc) 302 struct lpt_softc *sc; 303 { int pl; 304 305 lprintf ("T %x ", inb(sc->sc_port+lpt_status)); 306 if (sc->sc_state&OPEN) 307 timeout((timeout_t)lptout, (caddr_t)sc, hz/2); 308 else sc->sc_state &= ~TOUT; 309 310 if (sc->sc_state & ERROR) 311 sc->sc_state &= ~ERROR; 312 313 /* 314 * Avoid possible hangs do to missed interrupts 315 */ 316 if (sc->sc_xfercnt) { 317 pl = spltty(); 318 lptintr(sc - lpt_sc); 319 splx(pl); 320 } else { 321 sc->sc_state &= ~OBUSY; 322 wakeup((caddr_t)sc); 323 } 324 } 325 326 /* 327 * lptclose -- close the device, free the local line buffer. 328 */ 329 330 lptclose(dev, flag) 331 int flag; 332 { 333 struct lpt_softc *sc = lpt_sc + LPTUNIT(minor(dev)); 334 int port = sc->sc_port; 335 336 sc->sc_state &= ~OPEN; 337 while ((inb(port+lpt_status) & (LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR)) != 338 (LPS_SEL|LPS_NBSY|LPS_NERR) || sc->sc_xfercnt) 339 /* wait 1/4 second, give up if we get a signal */ 340 if (tsleep((caddr_t)sc, 341 LPPRI|PCATCH, "lpclose", hz) != EWOULDBLOCK) 342 break; 343 344 sc->sc_state = 0; 345 sc->sc_xfercnt = 0; 346 outb(sc->sc_port+lpt_control, LPC_NINIT); 347 brelse(sc->sc_inbuf); 348 lprintf("closed.\n"); 349 return(0); 350 } 351 352 /* 353 * lptwrite --copy a line from user space to a local buffer, then call 354 * putc to get the chars moved to the output queue. 355 */ 356 357 lptwrite(dev, uio) 358 dev_t dev; 359 struct uio *uio; 360 { 361 register unsigned n; 362 int pl, err; 363 struct lpt_softc *sc = lpt_sc + LPTUNIT(minor(dev)); 364 365 while (n = MIN(BUFSIZE, uio->uio_resid)) { 366 sc->sc_cp = sc->sc_inbuf->b_un.b_addr ; 367 uiomove(sc->sc_cp, n, uio); 368 sc->sc_xfercnt = n ; 369 while (sc->sc_xfercnt > 0) { 370 /* if the printer is ready for a char, give it one */ 371 if ((sc->sc_state & OBUSY) == 0){ 372 lprintf("\nC %d. ", sc->sc_xfercnt); 373 pl = spltty(); 374 lptintr(sc - lpt_sc); 375 (void) splx(pl); 376 } 377 lprintf("W "); 378 if (err = tsleep((caddr_t)sc, 379 LPPRI|PCATCH, "lpwrite", 0)) 380 return(err); 381 } 382 } 383 return(0); 384 } 385 386 /* 387 * lptintr -- handle printer interrupts which occur when the printer is 388 * ready to accept another char. 389 */ 390 391 lptintr(unit) 392 { 393 struct lpt_softc *sc = lpt_sc + unit; 394 int port = sc->sc_port,sts; 395 396 sts = inb(port+lpt_status); 397 398 if (!sc->sc_state) { 399 printf ("lpt%d: stray interrupt sts=0x%02x\n", unit, sts); 400 return; 401 } 402 403 /* is printer online and ready for output */ 404 if ((sts & (LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR/*|LPS_NACK*/)) == 405 (LPS_SEL|LPS_NBSY|LPS_NERR)) { 406 /* is this a false interrupt ? */ 407 if ((sc->sc_state & OBUSY) 408 && (sts & LPS_NACK) == 0) return; 409 sc->sc_state |= OBUSY; sc->sc_state &= ~ERROR; 410 411 if (sc->sc_xfercnt) { 412 /* send char */ 413 /*lprintf("%x ", *sc->sc_cp); */ 414 outb(port+lpt_data, *sc->sc_cp++) ; sc->sc_xfercnt-- ; 415 outb(port+lpt_control, sc->sc_control|LPC_STB); 416 /* DELAY(X) */ 417 outb(port+lpt_control, sc->sc_control); 418 } 419 420 /* any more bytes for the printer? */ 421 if (sc->sc_xfercnt > 0) return; 422 423 /* none, wake up the top half to get more */ 424 sc->sc_state &= ~OBUSY; 425 wakeup((caddr_t)sc); 426 lprintf("w "); 427 return; 428 } else sc->sc_state |= ERROR; 429 lprintf("sts %x ", sts); 430 } 431 432 int 433 lptioctl(dev_t dev, int cmd, caddr_t data, int flag) 434 { 435 int error; 436 437 error = 0; 438 switch (cmd) { 439 #ifdef THISISASAMPLE 440 case XXX: 441 dothis; andthis; andthat; 442 error=x; 443 break; 444 #endif /* THISISASAMPLE */ 445 default: 446 error = ENODEV; 447 } 448 449 return(error); 450 } 451 452 #endif /* NLPT */ 453