1 /* $OpenBSD: lpt.c,v 1.9 2012/01/11 16:22:33 dhill 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(bus_space_tag_t iot, bus_space_handle_t ioh, bus_addr_t base, 114 bus_size_t off, u_int8_t data, u_int8_t mask) 115 { 116 int timeout; 117 u_int8_t temp; 118 119 data &= mask; 120 bus_space_write_1(iot, ioh, off, data); 121 timeout = 1000; 122 do { 123 delay(10); 124 temp = bus_space_read_1(iot, ioh, off) & mask; 125 } while (temp != data && --timeout); 126 LPRINTF(("lpt: port=0x%x out=0x%x in=0x%x timeout=%d\n", base + off, 127 data, temp, timeout)); 128 return (temp == data); 129 } 130 131 void 132 lpt_attach_common(struct lpt_softc *sc) 133 { 134 printf("\n"); 135 136 bus_space_write_1(sc->sc_iot, sc->sc_ioh, lpt_control, LPC_NINIT); 137 138 timeout_set(&sc->sc_wakeup_tmo, lptwakeup, sc); 139 } 140 141 /* 142 * Reset the printer, then wait until it's selected and not busy. 143 */ 144 int 145 lptopen(dev_t dev, int flag, int mode, struct proc *p) 146 { 147 int unit = LPTUNIT(dev); 148 u_int8_t flags = LPTFLAGS(dev); 149 struct lpt_softc *sc; 150 bus_space_tag_t iot; 151 bus_space_handle_t ioh; 152 u_int8_t control; 153 int error; 154 int spin; 155 156 if (unit >= lpt_cd.cd_ndevs) 157 return ENXIO; 158 sc = lpt_cd.cd_devs[unit]; 159 if (!sc) 160 return ENXIO; 161 162 sc->sc_flags = (sc->sc_flags & LPT_POLLED) | flags; 163 if ((sc->sc_flags & (LPT_POLLED|LPT_NOINTR)) == LPT_POLLED) 164 return ENXIO; 165 166 #ifdef DIAGNOSTIC 167 if (sc->sc_state) 168 printf("%s: stat=0x%x not zero\n", sc->sc_dev.dv_xname, 169 sc->sc_state); 170 #endif 171 172 if (sc->sc_state) 173 return EBUSY; 174 175 sc->sc_state = LPT_INIT; 176 LPRINTF(("%s: open: flags=0x%x\n", sc->sc_dev.dv_xname, flags)); 177 iot = sc->sc_iot; 178 ioh = sc->sc_ioh; 179 180 if ((flags & LPT_NOPRIME) == 0) { 181 /* assert INIT for 100 usec to start up printer */ 182 bus_space_write_1(iot, ioh, lpt_control, LPC_SELECT); 183 delay(100); 184 } 185 186 control = LPC_SELECT | LPC_NINIT; 187 bus_space_write_1(iot, ioh, lpt_control, control); 188 189 /* wait till ready (printer running diagnostics) */ 190 for (spin = 0; NOT_READY_ERR(); spin += STEP) { 191 if (spin >= TIMEOUT) { 192 sc->sc_state = 0; 193 return EBUSY; 194 } 195 196 /* wait 1/4 second, give up if we get a signal */ 197 error = tsleep((caddr_t)sc, LPTPRI | PCATCH, "lptopen", STEP); 198 if (error != EWOULDBLOCK) { 199 sc->sc_state = 0; 200 return error; 201 } 202 } 203 204 if ((flags & LPT_NOINTR) == 0) 205 control |= LPC_IENABLE; 206 if (flags & LPT_AUTOLF) 207 control |= LPC_AUTOLF; 208 sc->sc_control = control; 209 bus_space_write_1(iot, ioh, lpt_control, control); 210 211 sc->sc_inbuf = geteblk(LPT_BSIZE); 212 sc->sc_count = 0; 213 sc->sc_state = LPT_OPEN; 214 215 if ((sc->sc_flags & LPT_NOINTR) == 0) 216 lptwakeup(sc); 217 218 LPRINTF(("%s: opened\n", sc->sc_dev.dv_xname)); 219 return 0; 220 } 221 222 int 223 lpt_not_ready(u_int8_t status, struct lpt_softc *sc) 224 { 225 u_int8_t new; 226 227 status = (status ^ LPS_INVERT) & LPS_MASK; 228 new = status & ~sc->sc_laststatus; 229 sc->sc_laststatus = status; 230 231 if (new & LPS_SELECT) 232 log(LOG_NOTICE, "%s: offline\n", sc->sc_dev.dv_xname); 233 else if (new & LPS_NOPAPER) 234 log(LOG_NOTICE, "%s: out of paper\n", sc->sc_dev.dv_xname); 235 else if (new & LPS_NERR) 236 log(LOG_NOTICE, "%s: output error\n", sc->sc_dev.dv_xname); 237 238 return status; 239 } 240 241 void 242 lptwakeup(void *arg) 243 { 244 struct lpt_softc *sc = arg; 245 int s; 246 247 s = spltty(); 248 lptintr(sc); 249 splx(s); 250 251 timeout_add(&sc->sc_wakeup_tmo, STEP); 252 } 253 254 /* 255 * Close the device, and free the local line buffer. 256 */ 257 int 258 lptclose(dev_t dev, int flag, int mode, struct proc *p) 259 { 260 int unit = LPTUNIT(dev); 261 struct lpt_softc *sc = lpt_cd.cd_devs[unit]; 262 bus_space_tag_t iot = sc->sc_iot; 263 bus_space_handle_t ioh = sc->sc_ioh; 264 265 if (sc->sc_count) 266 (void) lptpushbytes(sc); 267 268 if ((sc->sc_flags & LPT_NOINTR) == 0) 269 timeout_del(&sc->sc_wakeup_tmo); 270 271 bus_space_write_1(iot, ioh, lpt_control, LPC_NINIT); 272 sc->sc_state = 0; 273 bus_space_write_1(iot, ioh, lpt_control, LPC_NINIT); 274 brelse(sc->sc_inbuf); 275 276 LPRINTF(("%s: closed\n", sc->sc_dev.dv_xname)); 277 return 0; 278 } 279 280 int 281 lptpushbytes(struct lpt_softc *sc) 282 { 283 bus_space_tag_t iot = sc->sc_iot; 284 bus_space_handle_t ioh = sc->sc_ioh; 285 int error; 286 287 if (sc->sc_flags & LPT_NOINTR) { 288 int spin, tic; 289 u_int8_t control = sc->sc_control; 290 291 while (sc->sc_count > 0) { 292 spin = 0; 293 while (NOT_READY()) { 294 if (++spin < sc->sc_spinmax) 295 continue; 296 tic = 0; 297 /* adapt busy-wait algorithm */ 298 sc->sc_spinmax++; 299 while (NOT_READY_ERR()) { 300 /* exponential backoff */ 301 tic = tic + tic + 1; 302 if (tic > TIMEOUT) 303 tic = TIMEOUT; 304 error = tsleep((caddr_t)sc, 305 LPTPRI | PCATCH, "lptpsh", tic); 306 if (error != EWOULDBLOCK) 307 return error; 308 } 309 break; 310 } 311 312 bus_space_write_1(iot, ioh, lpt_data, *sc->sc_cp++); 313 bus_space_write_1(iot, ioh, lpt_control, 314 control | LPC_STROBE); 315 sc->sc_count--; 316 bus_space_write_1(iot, ioh, lpt_control, control); 317 318 /* adapt busy-wait algorithm */ 319 if (spin*2 + 16 < sc->sc_spinmax) 320 sc->sc_spinmax--; 321 } 322 } else { 323 int s; 324 325 while (sc->sc_count > 0) { 326 /* if the printer is ready for a char, give it one */ 327 if ((sc->sc_state & LPT_OBUSY) == 0) { 328 LPRINTF(("%s: write %d\n", sc->sc_dev.dv_xname, 329 sc->sc_count)); 330 s = spltty(); 331 (void) lptintr(sc); 332 splx(s); 333 } 334 error = tsleep((caddr_t)sc, LPTPRI | PCATCH, 335 "lptwrite2", 0); 336 if (error) 337 return error; 338 } 339 } 340 return 0; 341 } 342 343 /* 344 * Copy a line from user space to a local buffer, then call putc to get the 345 * chars moved to the output queue. 346 */ 347 int 348 lptwrite(dev_t dev, struct uio *uio, int flags) 349 { 350 struct lpt_softc *sc = lpt_cd.cd_devs[LPTUNIT(dev)]; 351 size_t n; 352 int error = 0; 353 354 while ((n = min(LPT_BSIZE, uio->uio_resid)) != 0) { 355 error = uiomove(sc->sc_cp = sc->sc_inbuf->b_data, n, uio); 356 if (error != 0) 357 return error; 358 sc->sc_count = n; 359 error = lptpushbytes(sc); 360 if (error) { 361 /* 362 * Return accurate residual if interrupted or timed 363 * out. 364 */ 365 uio->uio_resid += sc->sc_count; 366 sc->sc_count = 0; 367 return error; 368 } 369 } 370 return 0; 371 } 372 373 /* 374 * Handle printer interrupts which occur when the printer is ready to accept 375 * another char. 376 */ 377 int 378 lptintr(void *arg) 379 { 380 struct lpt_softc *sc = arg; 381 bus_space_tag_t iot = sc->sc_iot; 382 bus_space_handle_t ioh = sc->sc_ioh; 383 384 if (((sc->sc_state & LPT_OPEN) == 0 && sc->sc_count == 0) || 385 (sc->sc_flags & LPT_NOINTR)) 386 return 0; 387 388 /* is printer online and ready for output */ 389 if (NOT_READY() && NOT_READY_ERR()) 390 return -1; 391 392 if (sc->sc_count) { 393 u_int8_t control = sc->sc_control; 394 /* send char */ 395 bus_space_write_1(iot, ioh, lpt_data, *sc->sc_cp++); 396 delay (50); 397 bus_space_write_1(iot, ioh, lpt_control, control | LPC_STROBE); 398 sc->sc_count--; 399 bus_space_write_1(iot, ioh, lpt_control, control); 400 sc->sc_state |= LPT_OBUSY; 401 } else 402 sc->sc_state &= ~LPT_OBUSY; 403 404 if (sc->sc_count == 0) { 405 /* none, wake up the top half to get more */ 406 wakeup((caddr_t)sc); 407 } 408 409 return 1; 410 } 411