1 /* $NetBSD: lpt.c,v 1.18 2001/01/16 21:13:09 thomas Exp $ */ 2 3 /* 4 * Copyright (c) 1996 Leo Weppelman 5 * Copyright (c) 1993, 1994 Charles M. 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 PROVIDED BY THE DEVELOPER ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE DEVELOPER BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 */ 37 38 /* 39 * Device Driver originally written for AT parallel printer port. Now 40 * drives the printer port on the YM2149. 41 * 42 * THIS SOFTWARE IS A COMPONENT OF 386BSD DEVELOPED BY WILLIAM F. JOLITZ 43 * AND IS INTENDED FOR RESEARCH AND EDUCATIONAL PURPOSES ONLY. THIS 44 * SOFTWARE SHOULD NOT BE CONSIDERED TO BE A COMMERCIAL PRODUCT. 45 * THE DEVELOPER URGES THAT USERS WHO REQUIRE A COMMERCIAL PRODUCT 46 * NOT MAKE USE OF THIS WORK. 47 * 48 * FOR USERS WHO WISH TO UNDERSTAND THE 386BSD SYSTEM DEVELOPED 49 * BY WILLIAM F. JOLITZ, WE RECOMMEND THE USER STUDY WRITTEN 50 * REFERENCES SUCH AS THE "PORTING UNIX TO THE 386" SERIES 51 * (BEGINNING JANUARY 1991 "DR. DOBBS JOURNAL", USA AND BEGINNING 52 * JUNE 1991 "UNIX MAGAZIN", GERMANY) BY WILLIAM F. JOLITZ AND 53 * LYNNE GREER JOLITZ, AS WELL AS OTHER BOOKS ON UNIX AND THE 54 * ON-LINE 386BSD USER MANUAL BEFORE USE. A BOOK DISCUSSING THE INTERNALS 55 * OF 386BSD ENTITLED "386BSD FROM THE INSIDE OUT" WILL BE AVAILABLE LATE 1992. 56 */ 57 58 #include <sys/param.h> 59 #include <sys/systm.h> 60 #include <sys/callout.h> 61 #include <sys/proc.h> 62 #include <sys/user.h> 63 #include <sys/buf.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/cpu.h> 72 #include <machine/iomap.h> 73 #include <machine/mfp.h> 74 75 #include <atari/dev/ym2149reg.h> 76 #include <atari/atari/intr.h> 77 78 #define TIMEOUT hz*16 /* wait up to 16 seconds for a ready */ 79 #define STEP hz/4 80 81 #define LPTPRI (PZERO+8) 82 #define LPT_BSIZE 1024 83 84 #if !defined(DEBUG) || !defined(notdef) 85 #define lprintf if (0) printf 86 #else 87 #define lprintf if (lptdebug) printf 88 int lptdebug = 1; 89 #endif 90 91 struct lpt_softc { 92 struct device sc_dev; 93 struct callout sc_wakeup_ch; 94 size_t sc_count; 95 struct buf *sc_inbuf; 96 u_char *sc_cp; 97 int sc_spinmax; 98 u_char sc_state; 99 #define LPT_OPEN 0x01 /* device is open */ 100 #define LPT_OBUSY 0x02 /* printer is busy doing output */ 101 #define LPT_INIT 0x04 /* waiting to initialize for open */ 102 u_char sc_flags; 103 #define LPT_AUTOLF 0x20 /* automatic LF on CR XXX: LWP - not yet... */ 104 #define LPT_NOINTR 0x40 /* do not use interrupt */ 105 }; 106 107 #define LPTUNIT(s) (minor(s) & 0x1f) 108 #define LPTFLAGS(s) (minor(s) & 0xe0) 109 #define NOT_READY() (MFP->mf_gpip & IO_PBSY) 110 111 /* {b,c}devsw[] function prototypes */ 112 dev_type_open(lpopen); 113 dev_type_close(lpclose); 114 dev_type_write(lpwrite); 115 dev_type_ioctl(lpioctl); 116 117 static void lptwakeup __P((void *arg)); 118 static int pushbytes __P((struct lpt_softc *)); 119 static void lptpseudointr __P((struct lpt_softc *)); 120 int lptintr __P((struct lpt_softc *)); 121 int lpthwintr __P((struct lpt_softc *, int)); 122 123 124 /* 125 * Autoconfig stuff 126 */ 127 static void lpattach __P((struct device *, struct device *, void *)); 128 static int lpmatch __P((struct device *, struct cfdata *, void *)); 129 130 struct cfattach lp_ca = { 131 sizeof(struct lpt_softc), lpmatch, lpattach 132 }; 133 134 extern struct cfdriver lp_cd; 135 136 /*ARGSUSED*/ 137 static int 138 lpmatch(pdp, cfp, auxp) 139 struct device *pdp; 140 struct cfdata *cfp; 141 void *auxp; 142 { 143 static int lpt_matched = 0; 144 145 /* Match at most 1 lpt unit */ 146 if (strcmp((char *)auxp, "lpt") || lpt_matched) 147 return 0; 148 lpt_matched = 1; 149 return (1); 150 } 151 152 /*ARGSUSED*/ 153 static void 154 lpattach(pdp, dp, auxp) 155 struct device *pdp, *dp; 156 void *auxp; 157 { 158 struct lpt_softc *sc = (void *)dp; 159 160 sc->sc_state = 0; 161 162 if (intr_establish(0, USER_VEC, 0, (hw_ifun_t)lpthwintr, sc) == NULL) 163 printf("lptattach: Can't establish interrupt\n"); 164 ym2149_strobe(1); 165 166 printf("\n"); 167 168 callout_init(&sc->sc_wakeup_ch); 169 } 170 171 /* 172 * Reset the printer, then wait until it's selected and not busy. 173 */ 174 int 175 lpopen(dev, flag, mode, p) 176 dev_t dev; 177 int flag, mode; 178 struct proc *p; 179 { 180 int unit = LPTUNIT(dev); 181 u_char flags = LPTFLAGS(dev); 182 struct lpt_softc *sc; 183 int error; 184 int spin; 185 int sps; 186 187 if (unit >= lp_cd.cd_ndevs) 188 return ENXIO; 189 sc = lp_cd.cd_devs[unit]; 190 if (!sc) 191 return ENXIO; 192 193 #ifdef DIAGNOSTIC 194 if (sc->sc_state) 195 printf("%s: stat=0x%x not zero\n", sc->sc_dev.dv_xname, 196 sc->sc_state); 197 #endif 198 199 if (sc->sc_state) 200 return EBUSY; 201 202 sc->sc_state = LPT_INIT; 203 sc->sc_flags = flags; 204 lprintf("%s: open: flags=0x%x\n", sc->sc_dev.dv_xname, flags); 205 206 /* wait till ready (printer running diagnostics) */ 207 for (spin = 0; NOT_READY(); spin += STEP) { 208 if (spin >= TIMEOUT) { 209 sc->sc_state = 0; 210 return EBUSY; 211 } 212 213 /* wait 1/4 second, give up if we get a signal */ 214 if ((error = tsleep((caddr_t)sc, LPTPRI | PCATCH, "lptopen", 215 STEP)) != EWOULDBLOCK) { 216 sc->sc_state = 0; 217 return error; 218 } 219 } 220 221 sc->sc_inbuf = geteblk(LPT_BSIZE); 222 sc->sc_count = 0; 223 sc->sc_state = LPT_OPEN; 224 225 if ((sc->sc_flags & LPT_NOINTR) == 0) { 226 lptwakeup(sc); 227 228 sps = splhigh(); 229 MFP->mf_imrb |= IB_PBSY; 230 MFP->mf_ierb |= IB_PBSY; 231 splx(sps); 232 } 233 234 lprintf("%s: opened\n", sc->sc_dev.dv_xname); 235 return 0; 236 } 237 238 void 239 lptwakeup(arg) 240 void *arg; 241 { 242 struct lpt_softc *sc = arg; 243 244 lptpseudointr(sc); 245 246 callout_reset(&sc->sc_wakeup_ch, STEP, lptwakeup, sc); 247 } 248 249 /* 250 * Close the device, and free the local line buffer. 251 */ 252 int 253 lpclose(dev, flag, mode, p) 254 dev_t dev; 255 int flag; 256 int mode; 257 struct proc *p; 258 { 259 int unit = LPTUNIT(dev); 260 struct lpt_softc *sc = lp_cd.cd_devs[unit]; 261 int sps; 262 263 if (sc->sc_count) 264 (void) pushbytes(sc); 265 266 if ((sc->sc_flags & LPT_NOINTR) == 0) { 267 callout_stop(&sc->sc_wakeup_ch); 268 269 sps = splhigh(); 270 MFP->mf_ierb &= ~IB_PBSY; 271 MFP->mf_imrb &= ~IB_PBSY; 272 splx(sps); 273 } 274 275 sc->sc_state = 0; 276 brelse(sc->sc_inbuf); 277 278 lprintf("%s: closed\n", sc->sc_dev.dv_xname); 279 return 0; 280 } 281 282 int 283 pushbytes(sc) 284 struct lpt_softc *sc; 285 { 286 int error; 287 288 if (sc->sc_flags & LPT_NOINTR) { 289 int spin, tic; 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()) { 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 ym2149_write_ioport(YM_IOB, *sc->sc_cp++); 313 ym2149_strobe(0); 314 sc->sc_count--; 315 ym2149_strobe(1); 316 317 /* adapt busy-wait algorithm */ 318 if (spin*2 + 16 < sc->sc_spinmax) 319 sc->sc_spinmax--; 320 } 321 } else { 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 %d\n", sc->sc_dev.dv_xname, 326 sc->sc_count); 327 (void) lptpseudointr(sc); 328 } 329 if ((error = tsleep((caddr_t)sc, LPTPRI | PCATCH, 330 "lptwrite2", 0)) != 0) 331 return error; 332 } 333 } 334 return 0; 335 } 336 337 /* 338 * Copy a line from user space to a local buffer, then call putc to get the 339 * chars moved to the output queue. 340 */ 341 int 342 lpwrite(dev, uio, flags) 343 dev_t dev; 344 struct uio *uio; 345 int flags; 346 { 347 struct lpt_softc *sc = lp_cd.cd_devs[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->b_data, n, uio); 353 sc->sc_count = n; 354 error = pushbytes(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(sc) 374 struct lpt_softc *sc; 375 { 376 /* is printer online and ready for output */ 377 if (NOT_READY()) 378 return 0; 379 380 if (sc->sc_count) { 381 382 /* send char */ 383 ym2149_write_ioport(YM_IOB, *sc->sc_cp++); 384 ym2149_strobe(0); 385 sc->sc_count--; 386 ym2149_strobe(1); 387 sc->sc_state |= LPT_OBUSY; 388 } else 389 sc->sc_state &= ~LPT_OBUSY; 390 391 if (sc->sc_count == 0) { 392 /* none, wake up the top half to get more */ 393 wakeup((caddr_t)sc); 394 } 395 396 return 1; 397 } 398 399 static void 400 lptpseudointr(sc) 401 struct lpt_softc *sc; 402 { 403 int s; 404 405 s = spltty(); 406 lptintr(sc); 407 splx(s); 408 } 409 410 int 411 lpthwintr(sc, sr) 412 struct lpt_softc *sc; 413 int sr; 414 { 415 if (!BASEPRI(sr)) 416 add_sicallback((si_farg)lptpseudointr, sc, 0); 417 else lptpseudointr(sc); 418 return 1; 419 } 420 421 int 422 lpioctl(dev, cmd, data, flag, p) 423 dev_t dev; 424 u_long cmd; 425 caddr_t data; 426 int flag; 427 struct proc *p; 428 { 429 int error = 0; 430 431 switch (cmd) { 432 default: 433 error = ENODEV; 434 } 435 436 return error; 437 } 438