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