1 /* $NetBSD: lpt.c,v 1.56 2000/03/23 07:01:31 thorpej 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/param.h> 57 #include <sys/systm.h> 58 #include <sys/proc.h> 59 #include <sys/user.h> 60 #include <sys/malloc.h> 61 #include <sys/kernel.h> 62 #include <sys/ioctl.h> 63 #include <sys/uio.h> 64 #include <sys/device.h> 65 #include <sys/conf.h> 66 #include <sys/syslog.h> 67 68 #include <machine/bus.h> 69 #include <machine/intr.h> 70 71 #include <dev/ic/lptreg.h> 72 #include <dev/ic/lptvar.h> 73 74 #define TIMEOUT hz*16 /* wait up to 16 seconds for a ready */ 75 #define STEP hz/4 76 77 #define LPTPRI (PZERO+8) 78 #define LPT_BSIZE 1024 79 80 #define LPTDEBUG 81 82 #ifndef LPTDEBUG 83 #define LPRINTF(a) 84 #else 85 #define LPRINTF(a) if (lptdebug) printf a 86 int lptdebug = 0; 87 #endif 88 89 /* XXX does not belong here */ 90 cdev_decl(lpt); 91 92 extern struct cfdriver lpt_cd; 93 94 #define LPTUNIT(s) (minor(s) & 0x1f) 95 #define LPTFLAGS(s) (minor(s) & 0xe0) 96 97 void 98 lpt_attach_subr(sc) 99 struct lpt_softc *sc; 100 { 101 bus_space_tag_t iot; 102 bus_space_handle_t ioh; 103 104 sc->sc_state = 0; 105 106 iot = sc->sc_iot; 107 ioh = sc->sc_ioh; 108 109 bus_space_write_1(iot, ioh, lpt_control, LPC_NINIT); 110 111 callout_init(&sc->sc_wakeup_ch); 112 113 sc->sc_dev_ok = 1; 114 } 115 116 /* 117 * Reset the printer, then wait until it's selected and not busy. 118 */ 119 int 120 lptopen(dev, flag, mode, p) 121 dev_t dev; 122 int flag; 123 int mode; 124 struct proc *p; 125 { 126 int unit = LPTUNIT(dev); 127 u_char flags = LPTFLAGS(dev); 128 struct lpt_softc *sc; 129 bus_space_tag_t iot; 130 bus_space_handle_t ioh; 131 u_char control; 132 int error; 133 int spin; 134 135 if (unit >= lpt_cd.cd_ndevs) 136 return ENXIO; 137 sc = lpt_cd.cd_devs[unit]; 138 if (!sc || !sc->sc_dev_ok) 139 return ENXIO; 140 141 #if 0 /* XXX what to do? */ 142 if (sc->sc_irq == IRQUNK && (flags & LPT_NOINTR) == 0) 143 return ENXIO; 144 #endif 145 146 #ifdef DIAGNOSTIC 147 if (sc->sc_state) 148 printf("%s: stat=0x%x not zero\n", sc->sc_dev.dv_xname, 149 sc->sc_state); 150 #endif 151 152 if (sc->sc_state) 153 return EBUSY; 154 155 sc->sc_state = LPT_INIT; 156 sc->sc_flags = flags; 157 LPRINTF(("%s: open: flags=0x%x\n", sc->sc_dev.dv_xname, 158 (unsigned)flags)); 159 iot = sc->sc_iot; 160 ioh = sc->sc_ioh; 161 162 if ((flags & LPT_NOPRIME) == 0) { 163 /* assert INIT for 100 usec to start up printer */ 164 bus_space_write_1(iot, ioh, lpt_control, LPC_SELECT); 165 delay(100); 166 } 167 168 control = LPC_SELECT | LPC_NINIT; 169 bus_space_write_1(iot, ioh, lpt_control, control); 170 171 /* wait till ready (printer running diagnostics) */ 172 for (spin = 0; NOT_READY_ERR(); spin += STEP) { 173 if (spin >= TIMEOUT) { 174 sc->sc_state = 0; 175 return EBUSY; 176 } 177 178 /* wait 1/4 second, give up if we get a signal */ 179 error = tsleep((caddr_t)sc, LPTPRI | PCATCH, "lptopen", STEP); 180 if (error != EWOULDBLOCK) { 181 sc->sc_state = 0; 182 return error; 183 } 184 } 185 186 if ((flags & LPT_NOINTR) == 0) 187 control |= LPC_IENABLE; 188 if (flags & LPT_AUTOLF) 189 control |= LPC_AUTOLF; 190 sc->sc_control = control; 191 bus_space_write_1(iot, ioh, lpt_control, control); 192 193 sc->sc_inbuf = malloc(LPT_BSIZE, M_DEVBUF, M_WAITOK); 194 sc->sc_count = 0; 195 sc->sc_state = LPT_OPEN; 196 197 if ((sc->sc_flags & LPT_NOINTR) == 0) 198 lptwakeup(sc); 199 200 LPRINTF(("%s: opened\n", sc->sc_dev.dv_xname)); 201 return 0; 202 } 203 204 int 205 lptnotready(status, sc) 206 u_char status; 207 struct lpt_softc *sc; 208 { 209 u_char new; 210 211 status = (status ^ LPS_INVERT) & LPS_MASK; 212 new = status & ~sc->sc_laststatus; 213 sc->sc_laststatus = status; 214 215 if (sc->sc_state & LPT_OPEN) { 216 if (new & LPS_SELECT) 217 log(LOG_NOTICE, 218 "%s: offline\n", sc->sc_dev.dv_xname); 219 else if (new & LPS_NOPAPER) 220 log(LOG_NOTICE, 221 "%s: out of paper\n", sc->sc_dev.dv_xname); 222 else if (new & LPS_NERR) 223 log(LOG_NOTICE, 224 "%s: output error\n", sc->sc_dev.dv_xname); 225 } 226 227 return status; 228 } 229 230 void 231 lptwakeup(arg) 232 void *arg; 233 { 234 struct lpt_softc *sc = arg; 235 int s; 236 237 s = spllpt(); 238 lptintr(sc); 239 splx(s); 240 241 callout_reset(&sc->sc_wakeup_ch, STEP, lptwakeup, sc); 242 } 243 244 /* 245 * Close the device, and free the local line buffer. 246 */ 247 int 248 lptclose(dev, flag, mode, p) 249 dev_t dev; 250 int flag; 251 int mode; 252 struct proc *p; 253 { 254 int unit = LPTUNIT(dev); 255 struct lpt_softc *sc = lpt_cd.cd_devs[unit]; 256 bus_space_tag_t iot = sc->sc_iot; 257 bus_space_handle_t ioh = sc->sc_ioh; 258 259 if (sc->sc_count) 260 (void) lptpushbytes(sc); 261 262 if ((sc->sc_flags & LPT_NOINTR) == 0) 263 callout_stop(&sc->sc_wakeup_ch); 264 265 bus_space_write_1(iot, ioh, lpt_control, LPC_NINIT); 266 sc->sc_state = 0; 267 bus_space_write_1(iot, ioh, lpt_control, LPC_NINIT); 268 free(sc->sc_inbuf, M_DEVBUF); 269 270 LPRINTF(("%s: closed\n", sc->sc_dev.dv_xname)); 271 return 0; 272 } 273 274 int 275 lptpushbytes(sc) 276 struct lpt_softc *sc; 277 { 278 bus_space_tag_t iot = sc->sc_iot; 279 bus_space_handle_t ioh = sc->sc_ioh; 280 int error; 281 282 if (sc->sc_flags & LPT_NOINTR) { 283 int spin, tic; 284 u_char control = sc->sc_control; 285 286 while (sc->sc_count > 0) { 287 spin = 0; 288 while (NOT_READY()) { 289 if (++spin < sc->sc_spinmax) 290 continue; 291 tic = 0; 292 /* adapt busy-wait algorithm */ 293 sc->sc_spinmax++; 294 while (NOT_READY_ERR()) { 295 /* exponential backoff */ 296 tic = tic + tic + 1; 297 if (tic > TIMEOUT) 298 tic = TIMEOUT; 299 error = tsleep((caddr_t)sc, 300 LPTPRI | PCATCH, "lptpsh", tic); 301 if (error != EWOULDBLOCK) 302 return error; 303 } 304 break; 305 } 306 307 bus_space_write_1(iot, ioh, lpt_data, *sc->sc_cp++); 308 bus_space_write_1(iot, ioh, lpt_control, 309 control | LPC_STROBE); 310 sc->sc_count--; 311 bus_space_write_1(iot, ioh, lpt_control, control); 312 313 /* adapt busy-wait algorithm */ 314 if (spin*2 + 16 < sc->sc_spinmax) 315 sc->sc_spinmax--; 316 } 317 } else { 318 int s; 319 320 while (sc->sc_count > 0) { 321 /* if the printer is ready for a char, give it one */ 322 if ((sc->sc_state & LPT_OBUSY) == 0) { 323 LPRINTF(("%s: write %lu\n", sc->sc_dev.dv_xname, 324 (u_long)sc->sc_count)); 325 s = spllpt(); 326 (void) lptintr(sc); 327 splx(s); 328 } 329 error = tsleep((caddr_t)sc, LPTPRI | PCATCH, 330 "lptwrite2", 0); 331 if (error) 332 return error; 333 } 334 } 335 return 0; 336 } 337 338 /* 339 * Copy a line from user space to a local buffer, then call putc to get the 340 * chars moved to the output queue. 341 */ 342 int 343 lptwrite(dev, uio, flags) 344 dev_t dev; 345 struct uio *uio; 346 int flags; 347 { 348 struct lpt_softc *sc = lpt_cd.cd_devs[LPTUNIT(dev)]; 349 size_t n; 350 int error = 0; 351 352 while ((n = min(LPT_BSIZE, uio->uio_resid)) != 0) { 353 uiomove(sc->sc_cp = sc->sc_inbuf, n, uio); 354 sc->sc_count = n; 355 error = lptpushbytes(sc); 356 if (error) { 357 /* 358 * Return accurate residual if interrupted or timed 359 * out. 360 */ 361 uio->uio_resid += sc->sc_count; 362 sc->sc_count = 0; 363 return error; 364 } 365 } 366 return 0; 367 } 368 369 /* 370 * Handle printer interrupts which occur when the printer is ready to accept 371 * another char. 372 */ 373 int 374 lptintr(arg) 375 void *arg; 376 { 377 struct lpt_softc *sc = arg; 378 bus_space_tag_t iot = sc->sc_iot; 379 bus_space_handle_t ioh = sc->sc_ioh; 380 381 #if 0 382 if ((sc->sc_state & LPT_OPEN) == 0) 383 return 0; 384 #endif 385 386 /* is printer online and ready for output */ 387 if (NOT_READY() && NOT_READY_ERR()) 388 return 0; 389 390 if (sc->sc_count) { 391 u_char control = sc->sc_control; 392 /* send char */ 393 bus_space_write_1(iot, ioh, lpt_data, *sc->sc_cp++); 394 DELAY(1); 395 bus_space_write_1(iot, ioh, lpt_control, control | LPC_STROBE); 396 DELAY(1); 397 sc->sc_count--; 398 bus_space_write_1(iot, ioh, lpt_control, control); 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, cmd, data, flag, p) 413 dev_t dev; 414 u_long cmd; 415 caddr_t data; 416 int flag; 417 struct proc *p; 418 { 419 int error = 0; 420 421 switch (cmd) { 422 default: 423 error = ENODEV; 424 } 425 426 return error; 427 } 428