1 /* $NetBSD: lpt.c,v 1.22 2002/10/23 09:10:52 jdolecek 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 CFATTACH_DECL(lp, sizeof(struct lpt_softc), 131 lpmatch, lpattach, NULL, NULL); 132 133 extern struct cfdriver lp_cd; 134 135 const struct cdevsw lp_cdevsw = { 136 lpopen, lpclose, noread, lpwrite, lpioctl, 137 nostop, notty, nopoll, nommap, nokqfilter, 138 }; 139 140 /*ARGSUSED*/ 141 static int 142 lpmatch(pdp, cfp, auxp) 143 struct device *pdp; 144 struct cfdata *cfp; 145 void *auxp; 146 { 147 static int lpt_matched = 0; 148 149 /* Match at most 1 lpt unit */ 150 if (strcmp((char *)auxp, "lpt") || lpt_matched) 151 return 0; 152 lpt_matched = 1; 153 return (1); 154 } 155 156 /*ARGSUSED*/ 157 static void 158 lpattach(pdp, dp, auxp) 159 struct device *pdp, *dp; 160 void *auxp; 161 { 162 struct lpt_softc *sc = (void *)dp; 163 164 sc->sc_state = 0; 165 166 if (intr_establish(0, USER_VEC, 0, (hw_ifun_t)lpthwintr, sc) == NULL) 167 printf("lptattach: Can't establish interrupt\n"); 168 ym2149_strobe(1); 169 170 printf("\n"); 171 172 callout_init(&sc->sc_wakeup_ch); 173 } 174 175 /* 176 * Reset the printer, then wait until it's selected and not busy. 177 */ 178 int 179 lpopen(dev, flag, mode, p) 180 dev_t dev; 181 int flag, mode; 182 struct proc *p; 183 { 184 int unit = LPTUNIT(dev); 185 u_char flags = LPTFLAGS(dev); 186 struct lpt_softc *sc; 187 int error; 188 int spin; 189 int sps; 190 191 if (unit >= lp_cd.cd_ndevs) 192 return ENXIO; 193 sc = lp_cd.cd_devs[unit]; 194 if (!sc) 195 return ENXIO; 196 197 #ifdef DIAGNOSTIC 198 if (sc->sc_state) 199 printf("%s: stat=0x%x not zero\n", sc->sc_dev.dv_xname, 200 sc->sc_state); 201 #endif 202 203 if (sc->sc_state) 204 return EBUSY; 205 206 sc->sc_state = LPT_INIT; 207 sc->sc_flags = flags; 208 lprintf("%s: open: flags=0x%x\n", sc->sc_dev.dv_xname, flags); 209 210 /* wait till ready (printer running diagnostics) */ 211 for (spin = 0; NOT_READY(); spin += STEP) { 212 if (spin >= TIMEOUT) { 213 sc->sc_state = 0; 214 return EBUSY; 215 } 216 217 /* wait 1/4 second, give up if we get a signal */ 218 if ((error = tsleep((caddr_t)sc, LPTPRI | PCATCH, "lptopen", 219 STEP)) != EWOULDBLOCK) { 220 sc->sc_state = 0; 221 return error; 222 } 223 } 224 225 sc->sc_inbuf = geteblk(LPT_BSIZE); 226 sc->sc_count = 0; 227 sc->sc_state = LPT_OPEN; 228 229 if ((sc->sc_flags & LPT_NOINTR) == 0) { 230 lptwakeup(sc); 231 232 sps = splhigh(); 233 MFP->mf_imrb |= IB_PBSY; 234 MFP->mf_ierb |= IB_PBSY; 235 splx(sps); 236 } 237 238 lprintf("%s: opened\n", sc->sc_dev.dv_xname); 239 return 0; 240 } 241 242 void 243 lptwakeup(arg) 244 void *arg; 245 { 246 struct lpt_softc *sc = arg; 247 248 lptpseudointr(sc); 249 250 callout_reset(&sc->sc_wakeup_ch, STEP, lptwakeup, sc); 251 } 252 253 /* 254 * Close the device, and free the local line buffer. 255 */ 256 int 257 lpclose(dev, flag, mode, p) 258 dev_t dev; 259 int flag; 260 int mode; 261 struct proc *p; 262 { 263 int unit = LPTUNIT(dev); 264 struct lpt_softc *sc = lp_cd.cd_devs[unit]; 265 int sps; 266 267 if (sc->sc_count) 268 (void) pushbytes(sc); 269 270 if ((sc->sc_flags & LPT_NOINTR) == 0) { 271 callout_stop(&sc->sc_wakeup_ch); 272 273 sps = splhigh(); 274 MFP->mf_ierb &= ~IB_PBSY; 275 MFP->mf_imrb &= ~IB_PBSY; 276 splx(sps); 277 } 278 279 sc->sc_state = 0; 280 brelse(sc->sc_inbuf); 281 282 lprintf("%s: closed\n", sc->sc_dev.dv_xname); 283 return 0; 284 } 285 286 int 287 pushbytes(sc) 288 struct lpt_softc *sc; 289 { 290 int error; 291 292 if (sc->sc_flags & LPT_NOINTR) { 293 int spin, tic; 294 295 while (sc->sc_count > 0) { 296 spin = 0; 297 while (NOT_READY()) { 298 if (++spin < sc->sc_spinmax) 299 continue; 300 tic = 0; 301 /* adapt busy-wait algorithm */ 302 sc->sc_spinmax++; 303 while (NOT_READY()) { 304 /* exponential backoff */ 305 tic = tic + tic + 1; 306 if (tic > TIMEOUT) 307 tic = TIMEOUT; 308 error = tsleep((caddr_t)sc, 309 LPTPRI | PCATCH, "lptpsh", tic); 310 if (error != EWOULDBLOCK) 311 return error; 312 } 313 break; 314 } 315 316 ym2149_write_ioport(YM_IOB, *sc->sc_cp++); 317 ym2149_strobe(0); 318 sc->sc_count--; 319 ym2149_strobe(1); 320 321 /* adapt busy-wait algorithm */ 322 if (spin*2 + 16 < sc->sc_spinmax) 323 sc->sc_spinmax--; 324 } 325 } else { 326 while (sc->sc_count > 0) { 327 /* if the printer is ready for a char, give it one */ 328 if ((sc->sc_state & LPT_OBUSY) == 0) { 329 lprintf("%s: write %d\n", sc->sc_dev.dv_xname, 330 sc->sc_count); 331 (void) lptpseudointr(sc); 332 } 333 if ((error = tsleep((caddr_t)sc, LPTPRI | PCATCH, 334 "lptwrite2", 0)) != 0) 335 return error; 336 } 337 } 338 return 0; 339 } 340 341 /* 342 * Copy a line from user space to a local buffer, then call putc to get the 343 * chars moved to the output queue. 344 */ 345 int 346 lpwrite(dev, uio, flags) 347 dev_t dev; 348 struct uio *uio; 349 int flags; 350 { 351 struct lpt_softc *sc = lp_cd.cd_devs[LPTUNIT(dev)]; 352 size_t n; 353 int error = 0; 354 355 while ((n = min(LPT_BSIZE, uio->uio_resid)) > 0) { 356 uiomove(sc->sc_cp = sc->sc_inbuf->b_data, n, uio); 357 sc->sc_count = n; 358 error = pushbytes(sc); 359 if (error) { 360 /* 361 * Return accurate residual if interrupted or timed 362 * out. 363 */ 364 uio->uio_resid += sc->sc_count; 365 sc->sc_count = 0; 366 return error; 367 } 368 } 369 return 0; 370 } 371 372 /* 373 * Handle printer interrupts which occur when the printer is ready to accept 374 * another char. 375 */ 376 int 377 lptintr(sc) 378 struct lpt_softc *sc; 379 { 380 /* is printer online and ready for output */ 381 if (NOT_READY()) 382 return 0; 383 384 if (sc->sc_count) { 385 386 /* send char */ 387 ym2149_write_ioport(YM_IOB, *sc->sc_cp++); 388 ym2149_strobe(0); 389 sc->sc_count--; 390 ym2149_strobe(1); 391 sc->sc_state |= LPT_OBUSY; 392 } else 393 sc->sc_state &= ~LPT_OBUSY; 394 395 if (sc->sc_count == 0) { 396 /* none, wake up the top half to get more */ 397 wakeup((caddr_t)sc); 398 } 399 400 return 1; 401 } 402 403 static void 404 lptpseudointr(sc) 405 struct lpt_softc *sc; 406 { 407 int s; 408 409 s = spltty(); 410 lptintr(sc); 411 splx(s); 412 } 413 414 int 415 lpthwintr(sc, sr) 416 struct lpt_softc *sc; 417 int sr; 418 { 419 if (!BASEPRI(sr)) 420 add_sicallback((si_farg)lptpseudointr, sc, 0); 421 else lptpseudointr(sc); 422 return 1; 423 } 424 425 int 426 lpioctl(dev, cmd, data, flag, p) 427 dev_t dev; 428 u_long cmd; 429 caddr_t data; 430 int flag; 431 struct proc *p; 432 { 433 int error = 0; 434 435 switch (cmd) { 436 default: 437 error = ENODEV; 438 } 439 440 return error; 441 } 442