1 /* $OpenBSD: lpt.c,v 1.8 2011/09/17 08:36:06 miod Exp $ */ 2 /* $NetBSD: lpt.c,v 1.42 1996/10/21 22:41:14 thorpej Exp $ */ 3 4 /* 5 * Copyright (c) 1993, 1994 Charles Hannum. 6 * Copyright (c) 1990 William F. Jolitz, TeleMuse 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This software is a component of "386BSD" developed by 20 * William F. Jolitz, TeleMuse. 21 * 4. Neither the name of the developer nor the name "386BSD" 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THIS SOFTWARE IS A COMPONENT OF 386BSD DEVELOPED BY WILLIAM F. JOLITZ 26 * AND IS INTENDED FOR RESEARCH AND EDUCATIONAL PURPOSES ONLY. THIS 27 * SOFTWARE SHOULD NOT BE CONSIDERED TO BE A COMMERCIAL PRODUCT. 28 * THE DEVELOPER URGES THAT USERS WHO REQUIRE A COMMERCIAL PRODUCT 29 * NOT MAKE USE OF THIS WORK. 30 * 31 * FOR USERS WHO WISH TO UNDERSTAND THE 386BSD SYSTEM DEVELOPED 32 * BY WILLIAM F. JOLITZ, WE RECOMMEND THE USER STUDY WRITTEN 33 * REFERENCES SUCH AS THE "PORTING UNIX TO THE 386" SERIES 34 * (BEGINNING JANUARY 1991 "DR. DOBBS JOURNAL", USA AND BEGINNING 35 * JUNE 1991 "UNIX MAGAZIN", GERMANY) BY WILLIAM F. JOLITZ AND 36 * LYNNE GREER JOLITZ, AS WELL AS OTHER BOOKS ON UNIX AND THE 37 * ON-LINE 386BSD USER MANUAL BEFORE USE. A BOOK DISCUSSING THE INTERNALS 38 * OF 386BSD ENTITLED "386BSD FROM THE INSIDE OUT" WILL BE AVAILABLE LATE 1992. 39 * 40 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPER ``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 DEVELOPER 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 53 /* 54 * Device Driver for AT parallel printer port 55 */ 56 57 #include <sys/param.h> 58 #include <sys/systm.h> 59 #include <sys/proc.h> 60 #include <sys/buf.h> 61 #include <sys/kernel.h> 62 #include <sys/uio.h> 63 #include <sys/device.h> 64 #include <sys/conf.h> 65 #include <sys/syslog.h> 66 67 #include <machine/bus.h> 68 #include <machine/intr.h> 69 70 #include <dev/ic/lptreg.h> 71 #include <dev/ic/lptvar.h> 72 73 #include "lpt.h" 74 75 #define TIMEOUT hz*16 /* wait up to 16 seconds for a ready */ 76 #define STEP hz/4 77 78 #define LPTPRI (PZERO+8) 79 #define LPT_BSIZE 1024 80 81 #if !defined(DEBUG) || !defined(notdef) 82 #define LPRINTF(a) 83 #else 84 #define LPRINTF(a) if (lptdebug) printf a 85 int lptdebug = 1; 86 #endif 87 88 /* XXX does not belong here */ 89 cdev_decl(lpt); 90 91 struct cfdriver lpt_cd = { 92 NULL, "lpt", DV_TTY 93 }; 94 95 #define LPTUNIT(s) (minor(s) & 0x1f) 96 #define LPTFLAGS(s) (minor(s) & 0xe0) 97 98 #define LPS_INVERT (LPS_SELECT|LPS_NERR|LPS_NBSY|LPS_NACK) 99 #define LPS_MASK (LPS_SELECT|LPS_NERR|LPS_NBSY|LPS_NACK|LPS_NOPAPER) 100 #define NOT_READY() \ 101 ((bus_space_read_1(iot, ioh, lpt_status) ^ LPS_INVERT) & LPS_MASK) 102 #define NOT_READY_ERR() \ 103 lpt_not_ready(bus_space_read_1(iot, ioh, lpt_status), sc) 104 105 int lpt_not_ready(u_int8_t, struct lpt_softc *); 106 void lptwakeup(void *arg); 107 int lptpushbytes(struct lpt_softc *); 108 109 /* 110 * Internal routine to lptprobe to do port tests of one byte value. 111 */ 112 int 113 lpt_port_test(iot, ioh, base, off, data, mask) 114 bus_space_tag_t iot; 115 bus_space_handle_t ioh; 116 bus_addr_t base; 117 bus_size_t off; 118 u_int8_t data, mask; 119 { 120 int timeout; 121 u_int8_t temp; 122 123 data &= mask; 124 bus_space_write_1(iot, ioh, off, data); 125 timeout = 1000; 126 do { 127 delay(10); 128 temp = bus_space_read_1(iot, ioh, off) & mask; 129 } while (temp != data && --timeout); 130 LPRINTF(("lpt: port=0x%x out=0x%x in=0x%x timeout=%d\n", base + off, 131 data, temp, timeout)); 132 return (temp == data); 133 } 134 135 void 136 lpt_attach_common(sc) 137 struct lpt_softc *sc; 138 { 139 printf("\n"); 140 141 bus_space_write_1(sc->sc_iot, sc->sc_ioh, lpt_control, LPC_NINIT); 142 143 timeout_set(&sc->sc_wakeup_tmo, lptwakeup, sc); 144 } 145 146 /* 147 * Reset the printer, then wait until it's selected and not busy. 148 */ 149 int 150 lptopen(dev, flag, mode, p) 151 dev_t dev; 152 int flag; 153 int mode; 154 struct proc *p; 155 { 156 int unit = LPTUNIT(dev); 157 u_int8_t flags = LPTFLAGS(dev); 158 struct lpt_softc *sc; 159 bus_space_tag_t iot; 160 bus_space_handle_t ioh; 161 u_int8_t control; 162 int error; 163 int spin; 164 165 if (unit >= lpt_cd.cd_ndevs) 166 return ENXIO; 167 sc = lpt_cd.cd_devs[unit]; 168 if (!sc) 169 return ENXIO; 170 171 sc->sc_flags = (sc->sc_flags & LPT_POLLED) | flags; 172 if ((sc->sc_flags & (LPT_POLLED|LPT_NOINTR)) == LPT_POLLED) 173 return ENXIO; 174 175 #ifdef DIAGNOSTIC 176 if (sc->sc_state) 177 printf("%s: stat=0x%x not zero\n", sc->sc_dev.dv_xname, 178 sc->sc_state); 179 #endif 180 181 if (sc->sc_state) 182 return EBUSY; 183 184 sc->sc_state = LPT_INIT; 185 LPRINTF(("%s: open: flags=0x%x\n", sc->sc_dev.dv_xname, flags)); 186 iot = sc->sc_iot; 187 ioh = sc->sc_ioh; 188 189 if ((flags & LPT_NOPRIME) == 0) { 190 /* assert INIT for 100 usec to start up printer */ 191 bus_space_write_1(iot, ioh, lpt_control, LPC_SELECT); 192 delay(100); 193 } 194 195 control = LPC_SELECT | LPC_NINIT; 196 bus_space_write_1(iot, ioh, lpt_control, control); 197 198 /* wait till ready (printer running diagnostics) */ 199 for (spin = 0; NOT_READY_ERR(); spin += STEP) { 200 if (spin >= TIMEOUT) { 201 sc->sc_state = 0; 202 return EBUSY; 203 } 204 205 /* wait 1/4 second, give up if we get a signal */ 206 error = tsleep((caddr_t)sc, LPTPRI | PCATCH, "lptopen", STEP); 207 if (error != EWOULDBLOCK) { 208 sc->sc_state = 0; 209 return error; 210 } 211 } 212 213 if ((flags & LPT_NOINTR) == 0) 214 control |= LPC_IENABLE; 215 if (flags & LPT_AUTOLF) 216 control |= LPC_AUTOLF; 217 sc->sc_control = control; 218 bus_space_write_1(iot, ioh, lpt_control, control); 219 220 sc->sc_inbuf = geteblk(LPT_BSIZE); 221 sc->sc_count = 0; 222 sc->sc_state = LPT_OPEN; 223 224 if ((sc->sc_flags & LPT_NOINTR) == 0) 225 lptwakeup(sc); 226 227 LPRINTF(("%s: opened\n", sc->sc_dev.dv_xname)); 228 return 0; 229 } 230 231 int 232 lpt_not_ready(status, sc) 233 u_int8_t status; 234 struct lpt_softc *sc; 235 { 236 u_int8_t new; 237 238 status = (status ^ LPS_INVERT) & LPS_MASK; 239 new = status & ~sc->sc_laststatus; 240 sc->sc_laststatus = status; 241 242 if (new & LPS_SELECT) 243 log(LOG_NOTICE, "%s: offline\n", sc->sc_dev.dv_xname); 244 else if (new & LPS_NOPAPER) 245 log(LOG_NOTICE, "%s: out of paper\n", sc->sc_dev.dv_xname); 246 else if (new & LPS_NERR) 247 log(LOG_NOTICE, "%s: output error\n", sc->sc_dev.dv_xname); 248 249 return status; 250 } 251 252 void 253 lptwakeup(arg) 254 void *arg; 255 { 256 struct lpt_softc *sc = arg; 257 int s; 258 259 s = spltty(); 260 lptintr(sc); 261 splx(s); 262 263 timeout_add(&sc->sc_wakeup_tmo, STEP); 264 } 265 266 /* 267 * Close the device, and free the local line buffer. 268 */ 269 int 270 lptclose(dev, flag, mode, p) 271 dev_t dev; 272 int flag; 273 int mode; 274 struct proc *p; 275 { 276 int unit = LPTUNIT(dev); 277 struct lpt_softc *sc = lpt_cd.cd_devs[unit]; 278 bus_space_tag_t iot = sc->sc_iot; 279 bus_space_handle_t ioh = sc->sc_ioh; 280 281 if (sc->sc_count) 282 (void) lptpushbytes(sc); 283 284 if ((sc->sc_flags & LPT_NOINTR) == 0) 285 timeout_del(&sc->sc_wakeup_tmo); 286 287 bus_space_write_1(iot, ioh, lpt_control, LPC_NINIT); 288 sc->sc_state = 0; 289 bus_space_write_1(iot, ioh, lpt_control, LPC_NINIT); 290 brelse(sc->sc_inbuf); 291 292 LPRINTF(("%s: closed\n", sc->sc_dev.dv_xname)); 293 return 0; 294 } 295 296 int 297 lptpushbytes(sc) 298 struct lpt_softc *sc; 299 { 300 bus_space_tag_t iot = sc->sc_iot; 301 bus_space_handle_t ioh = sc->sc_ioh; 302 int error; 303 304 if (sc->sc_flags & LPT_NOINTR) { 305 int spin, tic; 306 u_int8_t control = sc->sc_control; 307 308 while (sc->sc_count > 0) { 309 spin = 0; 310 while (NOT_READY()) { 311 if (++spin < sc->sc_spinmax) 312 continue; 313 tic = 0; 314 /* adapt busy-wait algorithm */ 315 sc->sc_spinmax++; 316 while (NOT_READY_ERR()) { 317 /* exponential backoff */ 318 tic = tic + tic + 1; 319 if (tic > TIMEOUT) 320 tic = TIMEOUT; 321 error = tsleep((caddr_t)sc, 322 LPTPRI | PCATCH, "lptpsh", tic); 323 if (error != EWOULDBLOCK) 324 return error; 325 } 326 break; 327 } 328 329 bus_space_write_1(iot, ioh, lpt_data, *sc->sc_cp++); 330 bus_space_write_1(iot, ioh, lpt_control, 331 control | LPC_STROBE); 332 sc->sc_count--; 333 bus_space_write_1(iot, ioh, lpt_control, control); 334 335 /* adapt busy-wait algorithm */ 336 if (spin*2 + 16 < sc->sc_spinmax) 337 sc->sc_spinmax--; 338 } 339 } else { 340 int s; 341 342 while (sc->sc_count > 0) { 343 /* if the printer is ready for a char, give it one */ 344 if ((sc->sc_state & LPT_OBUSY) == 0) { 345 LPRINTF(("%s: write %d\n", sc->sc_dev.dv_xname, 346 sc->sc_count)); 347 s = spltty(); 348 (void) lptintr(sc); 349 splx(s); 350 } 351 error = tsleep((caddr_t)sc, LPTPRI | PCATCH, 352 "lptwrite2", 0); 353 if (error) 354 return error; 355 } 356 } 357 return 0; 358 } 359 360 /* 361 * Copy a line from user space to a local buffer, then call putc to get the 362 * chars moved to the output queue. 363 */ 364 int 365 lptwrite(dev, uio, flags) 366 dev_t dev; 367 struct uio *uio; 368 int flags; 369 { 370 struct lpt_softc *sc = lpt_cd.cd_devs[LPTUNIT(dev)]; 371 size_t n; 372 int error = 0; 373 374 while ((n = min(LPT_BSIZE, uio->uio_resid)) != 0) { 375 error = uiomove(sc->sc_cp = sc->sc_inbuf->b_data, n, uio); 376 if (error != 0) 377 return error; 378 sc->sc_count = n; 379 error = lptpushbytes(sc); 380 if (error) { 381 /* 382 * Return accurate residual if interrupted or timed 383 * out. 384 */ 385 uio->uio_resid += sc->sc_count; 386 sc->sc_count = 0; 387 return error; 388 } 389 } 390 return 0; 391 } 392 393 /* 394 * Handle printer interrupts which occur when the printer is ready to accept 395 * another char. 396 */ 397 int 398 lptintr(arg) 399 void *arg; 400 { 401 struct lpt_softc *sc = arg; 402 bus_space_tag_t iot = sc->sc_iot; 403 bus_space_handle_t ioh = sc->sc_ioh; 404 405 if (((sc->sc_state & LPT_OPEN) == 0 && sc->sc_count == 0) || 406 (sc->sc_flags & LPT_NOINTR)) 407 return 0; 408 409 /* is printer online and ready for output */ 410 if (NOT_READY() && NOT_READY_ERR()) 411 return -1; 412 413 if (sc->sc_count) { 414 u_int8_t control = sc->sc_control; 415 /* send char */ 416 bus_space_write_1(iot, ioh, lpt_data, *sc->sc_cp++); 417 delay (50); 418 bus_space_write_1(iot, ioh, lpt_control, control | LPC_STROBE); 419 sc->sc_count--; 420 bus_space_write_1(iot, ioh, lpt_control, control); 421 sc->sc_state |= LPT_OBUSY; 422 } else 423 sc->sc_state &= ~LPT_OBUSY; 424 425 if (sc->sc_count == 0) { 426 /* none, wake up the top half to get more */ 427 wakeup((caddr_t)sc); 428 } 429 430 return 1; 431 } 432