lpt.c revision 1.29
1/* $NetBSD: lpt.c,v 1.29 2008/03/07 17:15:51 cube 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/cdefs.h> 59__KERNEL_RCSID(0, "$NetBSD: lpt.c,v 1.29 2008/03/07 17:15:51 cube Exp $"); 60 61#include <sys/param.h> 62#include <sys/systm.h> 63#include <sys/callout.h> 64#include <sys/proc.h> 65#include <sys/user.h> 66#include <sys/buf.h> 67#include <sys/kernel.h> 68#include <sys/ioctl.h> 69#include <sys/uio.h> 70#include <sys/device.h> 71#include <sys/conf.h> 72#include <sys/syslog.h> 73 74#include <machine/cpu.h> 75#include <machine/iomap.h> 76#include <machine/mfp.h> 77 78#include <atari/dev/ym2149reg.h> 79#include <atari/atari/intr.h> 80 81#define TIMEOUT hz*16 /* wait up to 16 seconds for a ready */ 82#define STEP hz/4 83 84#define LPTPRI (PZERO+8) 85#define LPT_BSIZE 1024 86 87#if !defined(DEBUG) || !defined(notdef) 88#define lprintf if (0) aprint_error_dev 89#else 90#define lprintf if (lptdebug) aprint_error_dev 91int lptdebug = 1; 92#endif 93 94struct lpt_softc { 95 device_t sc_dev; 96 struct callout sc_wakeup_ch; 97 size_t sc_count; 98 struct buf *sc_inbuf; 99 u_char *sc_cp; 100 int sc_spinmax; 101 u_char sc_state; 102#define LPT_OPEN 0x01 /* device is open */ 103#define LPT_OBUSY 0x02 /* printer is busy doing output */ 104#define LPT_INIT 0x04 /* waiting to initialize for open */ 105 u_char sc_flags; 106#define LPT_AUTOLF 0x20 /* automatic LF on CR XXX: LWP - not yet... */ 107#define LPT_NOINTR 0x40 /* do not use interrupt */ 108}; 109 110#define LPTUNIT(s) (minor(s) & 0x1f) 111#define LPTFLAGS(s) (minor(s) & 0xe0) 112#define NOT_READY() (MFP->mf_gpip & IO_PBSY) 113 114/* {b,c}devsw[] function prototypes */ 115dev_type_open(lpopen); 116dev_type_close(lpclose); 117dev_type_write(lpwrite); 118dev_type_ioctl(lpioctl); 119 120static void lptwakeup (void *arg); 121static int pushbytes (struct lpt_softc *); 122static void lptpseudointr (struct lpt_softc *); 123int lptintr (struct lpt_softc *); 124int lpthwintr (struct lpt_softc *, int); 125 126 127/* 128 * Autoconfig stuff 129 */ 130static void lpattach (device_t, device_t, void *); 131static int lpmatch (device_t, cfdata_t , void *); 132 133CFATTACH_DECL_NEW(lp, sizeof(struct lpt_softc), 134 lpmatch, lpattach, NULL, NULL); 135 136extern struct cfdriver lp_cd; 137 138const struct cdevsw lp_cdevsw = { 139 lpopen, lpclose, noread, lpwrite, lpioctl, 140 nostop, notty, nopoll, nommap, nokqfilter, 141}; 142 143/*ARGSUSED*/ 144static int 145lpmatch(device_t pdp, cfdata_t cfp, 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*/ 157static void 158lpattach(device_t pdp, device_t dp, void *auxp) 159{ 160 struct lpt_softc *sc = device_private(dp); 161 162 sc->sc_dev = dp; 163 sc->sc_state = 0; 164 165 aprint_normal("\n"); 166 167 if (intr_establish(0, USER_VEC, 0, (hw_ifun_t)lpthwintr, sc) == NULL) 168 aprint_error_dev(dp, "Can't establish interrupt\n"); 169 ym2149_strobe(1); 170 171 callout_init(&sc->sc_wakeup_ch, 0); 172} 173 174/* 175 * Reset the printer, then wait until it's selected and not busy. 176 */ 177int 178lpopen(dev_t dev, int flag, int mode, struct lwp *l) 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 = device_private(lp_cd.cd_devs[unit]); 190 if (!sc) 191 return ENXIO; 192 193#ifdef DIAGNOSTIC 194 if (sc->sc_state) 195 aprint_verbose_dev(sc->sc_dev, "stat=0x%x not zero\n", 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(sc->sc_dev, "open: flags=0x%x\n", 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((void *)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(sc->sc_dev, "opened\n"); 235 return 0; 236} 237 238void 239lptwakeup(void *arg) 240{ 241 struct lpt_softc *sc = arg; 242 243 lptpseudointr(sc); 244 245 callout_reset(&sc->sc_wakeup_ch, STEP, lptwakeup, sc); 246} 247 248/* 249 * Close the device, and free the local line buffer. 250 */ 251int 252lpclose(dev_t dev, int flag, int mode, struct lwp *l) 253{ 254 int unit = LPTUNIT(dev); 255 struct lpt_softc *sc = device_private(lp_cd.cd_devs[unit]); 256 int sps; 257 258 if (sc->sc_count) 259 (void) pushbytes(sc); 260 261 if ((sc->sc_flags & LPT_NOINTR) == 0) { 262 callout_stop(&sc->sc_wakeup_ch); 263 264 sps = splhigh(); 265 MFP->mf_ierb &= ~IB_PBSY; 266 MFP->mf_imrb &= ~IB_PBSY; 267 splx(sps); 268 } 269 270 sc->sc_state = 0; 271 brelse(sc->sc_inbuf, 0); 272 273 lprintf(sc->sc_dev, "closed\n"); 274 return 0; 275} 276 277int 278pushbytes(struct lpt_softc *sc) 279{ 280 int error; 281 282 if (sc->sc_flags & LPT_NOINTR) { 283 int spin, tic; 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()) { 294 /* exponential backoff */ 295 tic = tic + tic + 1; 296 if (tic > TIMEOUT) 297 tic = TIMEOUT; 298 error = tsleep((void *)sc, 299 LPTPRI | PCATCH, "lptpsh", tic); 300 if (error != EWOULDBLOCK) 301 return error; 302 } 303 break; 304 } 305 306 ym2149_write_ioport(YM_IOB, *sc->sc_cp++); 307 ym2149_strobe(0); 308 sc->sc_count--; 309 ym2149_strobe(1); 310 311 /* adapt busy-wait algorithm */ 312 if (spin*2 + 16 < sc->sc_spinmax) 313 sc->sc_spinmax--; 314 } 315 } else { 316 while (sc->sc_count > 0) { 317 /* if the printer is ready for a char, give it one */ 318 if ((sc->sc_state & LPT_OBUSY) == 0) { 319 lprintf(sc->sc_dev, "write %d\n", 320 sc->sc_count); 321 (void) lptpseudointr(sc); 322 } 323 if ((error = tsleep((void *)sc, LPTPRI | PCATCH, 324 "lptwrite2", 0)) != 0) 325 return error; 326 } 327 } 328 return 0; 329} 330 331/* 332 * Copy a line from user space to a local buffer, then call putc to get the 333 * chars moved to the output queue. 334 */ 335int 336lpwrite(dev_t dev, struct uio *uio, int flags) 337{ 338 struct lpt_softc *sc = device_private(lp_cd.cd_devs[LPTUNIT(dev)]); 339 size_t n; 340 int error = 0; 341 342 while ((n = min(LPT_BSIZE, uio->uio_resid)) > 0) { 343 uiomove(sc->sc_cp = sc->sc_inbuf->b_data, n, uio); 344 sc->sc_count = n; 345 error = pushbytes(sc); 346 if (error) { 347 /* 348 * Return accurate residual if interrupted or timed 349 * out. 350 */ 351 uio->uio_resid += sc->sc_count; 352 sc->sc_count = 0; 353 return error; 354 } 355 } 356 return 0; 357} 358 359/* 360 * Handle printer interrupts which occur when the printer is ready to accept 361 * another char. 362 */ 363int 364lptintr(struct lpt_softc *sc) 365{ 366 /* is printer online and ready for output */ 367 if (NOT_READY()) 368 return 0; 369 370 if (sc->sc_count) { 371 372 /* send char */ 373 ym2149_write_ioport(YM_IOB, *sc->sc_cp++); 374 ym2149_strobe(0); 375 sc->sc_count--; 376 ym2149_strobe(1); 377 sc->sc_state |= LPT_OBUSY; 378 } else 379 sc->sc_state &= ~LPT_OBUSY; 380 381 if (sc->sc_count == 0) { 382 /* none, wake up the top half to get more */ 383 wakeup((void *)sc); 384 } 385 386 return 1; 387} 388 389static void 390lptpseudointr(struct lpt_softc *sc) 391{ 392 int s; 393 394 s = spltty(); 395 lptintr(sc); 396 splx(s); 397} 398 399int 400lpthwintr(struct lpt_softc *sc, int sr) 401{ 402 if (!BASEPRI(sr)) 403 add_sicallback((si_farg)lptpseudointr, sc, 0); 404 else lptpseudointr(sc); 405 return 1; 406} 407 408int 409lpioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l) 410{ 411 int error = 0; 412 413 switch (cmd) { 414 default: 415 error = ENODEV; 416 } 417 418 return error; 419} 420