1 /* $NetBSD: par.c,v 1.13 2002/09/06 13:18:43 gehenna Exp $ */ 2 3 /* 4 * Copyright (c) 1982, 1990 The Regents of the University of California. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * @(#)ppi.c 7.3 (Berkeley) 12/16/90 36 */ 37 38 /* 39 * parallel port interface 40 */ 41 42 #include <sys/param.h> 43 #include <sys/errno.h> 44 #include <sys/uio.h> 45 #include <sys/device.h> 46 #include <sys/malloc.h> 47 #include <sys/file.h> 48 #include <sys/systm.h> 49 #include <sys/callout.h> 50 #include <sys/proc.h> 51 #include <sys/conf.h> 52 53 #include <machine/bus.h> 54 #include <machine/cpu.h> 55 #include <machine/parioctl.h> 56 57 #include <arch/x68k/dev/intiovar.h> 58 59 struct par_softc { 60 struct device sc_dev; 61 62 bus_space_tag_t sc_bst; 63 bus_space_handle_t sc_bsh; 64 int sc_flags; 65 struct parparam sc_param; 66 #define sc_burst sc_param.burst 67 #define sc_timo sc_param.timo 68 #define sc_delay sc_param.delay 69 struct callout sc_timo_ch; 70 struct callout sc_start_ch; 71 } ; 72 73 /* par registers */ 74 #define PAR_DATA 1 75 #define PAR_STROBE 3 76 77 /* sc_flags values */ 78 #define PARF_ALIVE 0x01 79 #define PARF_OPEN 0x02 80 #define PARF_UIO 0x04 81 #define PARF_TIMO 0x08 82 #define PARF_DELAY 0x10 83 #define PARF_OREAD 0x40 /* no support */ 84 #define PARF_OWRITE 0x80 85 86 87 void partimo __P((void *)); 88 void parstart __P((void *);) 89 void parintr __P((void *)); 90 int parrw __P((dev_t, struct uio *)); 91 int parhztoms __P((int)); 92 int parmstohz __P((int)); 93 int parsendch __P((struct par_softc*, u_char)); 94 int parsend __P((struct par_softc*, u_char *, int)); 95 96 static struct callout intr_callout = CALLOUT_INITIALIZER; 97 98 #define UNIT(x) minor(x) 99 100 #ifdef DEBUG 101 #define PDB_FOLLOW 0x01 102 #define PDB_IO 0x02 103 #define PDB_INTERRUPT 0x04 104 #define PDB_NOCHECK 0x80 105 #ifdef PARDEBUG 106 int pardebug = PDB_FOLLOW | PDB_IO | PDB_INTERRUPT; 107 #else 108 int pardebug = 0; 109 #endif 110 #endif 111 112 int parmatch __P((struct device *, struct cfdata *, void *)); 113 void parattach __P((struct device *, struct device *, void *)); 114 115 struct cfattach par_ca = { 116 sizeof(struct par_softc), parmatch, parattach 117 }; 118 119 extern struct cfdriver par_cd; 120 121 dev_type_open(paropen); 122 dev_type_close(parclose); 123 dev_type_write(parwrite); 124 dev_type_ioctl(parioctl); 125 126 const struct cdevsw par_cdevsw = { 127 paropen, parclose, noread, parwrite, parioctl, 128 nostop, notty, nopoll, nommap, 129 }; 130 131 int 132 parmatch(pdp, cfp, aux) 133 struct device *pdp; 134 struct cfdata *cfp; 135 void *aux; 136 { 137 struct intio_attach_args *ia = aux; 138 139 /* X680x0 has only one parallel port */ 140 if (strcmp(ia->ia_name, "par") || cfp->cf_unit > 0) 141 return 0; 142 143 if (ia->ia_addr == INTIOCF_ADDR_DEFAULT) 144 ia->ia_addr = 0xe8c000; 145 ia->ia_size = 0x2000; 146 if (intio_map_allocate_region (pdp, ia, INTIO_MAP_TESTONLY)) 147 return 0; 148 if (ia->ia_intr == INTIOCF_INTR_DEFAULT) 149 ia->ia_intr = 99; 150 #if DIAGNOSTIC 151 if (ia->ia_intr != 99) 152 return 0; 153 #endif 154 155 return 1; 156 } 157 158 void 159 parattach(pdp, dp, aux) 160 struct device *pdp, *dp; 161 void *aux; 162 { 163 register struct par_softc *sc = (struct par_softc *)dp; 164 struct intio_attach_args *ia = aux; 165 int r; 166 167 sc->sc_flags = PARF_ALIVE; 168 printf(": parallel port (write only, interrupt)\n"); 169 ia->ia_size = 0x2000; 170 r = intio_map_allocate_region (pdp, ia, INTIO_MAP_ALLOCATE); 171 #ifdef DIAGNOSTIC 172 if (r) 173 panic ("IO map for PAR corruption??"); 174 #endif 175 sc->sc_bst = ia->ia_bst; 176 r = bus_space_map (sc->sc_bst, 177 ia->ia_addr, ia->ia_size, 178 BUS_SPACE_MAP_SHIFTED, 179 &sc->sc_bsh); 180 #ifdef DIAGNOSTIC 181 if (r) 182 panic ("Cannot map IO space for PAR."); 183 #endif 184 185 intio_set_sicilian_intr(intio_get_sicilian_intr() & 186 ~SICILIAN_INTR_PAR); 187 188 intio_intr_establish(ia->ia_intr, "par", 189 (intio_intr_handler_t) parintr, (void*) 1); 190 191 callout_init(&sc->sc_timo_ch); 192 callout_init(&sc->sc_start_ch); 193 } 194 195 int 196 paropen(dev, flags, mode, p) 197 dev_t dev; 198 int flags, mode; 199 struct proc *p; 200 { 201 register int unit = UNIT(dev); 202 register struct par_softc *sc; 203 204 if (unit != 0) 205 return(ENXIO); 206 sc = par_cd.cd_devs[unit]; 207 if (!(sc->sc_flags & PARF_ALIVE)) 208 return(ENXIO); 209 if (sc->sc_flags & PARF_OPEN) 210 return(EBUSY); 211 /* X680x0 can't read */ 212 if ((flags & FREAD) == FREAD) 213 return (EINVAL); 214 215 sc->sc_flags |= PARF_OPEN; 216 217 sc->sc_flags |= PARF_OWRITE; 218 219 sc->sc_burst = PAR_BURST; 220 sc->sc_timo = parmstohz(PAR_TIMO); 221 sc->sc_delay = parmstohz(PAR_DELAY); 222 223 return(0); 224 } 225 226 int 227 parclose(dev, flags, mode, p) 228 dev_t dev; 229 int flags, mode; 230 struct proc *p; 231 { 232 int unit = UNIT(dev); 233 int s; 234 struct par_softc *sc = par_cd.cd_devs[unit]; 235 236 sc->sc_flags &= ~(PARF_OPEN|PARF_OWRITE); 237 238 /* don't allow interrupts any longer */ 239 s = spl1(); 240 intio_set_sicilian_intr(intio_get_sicilian_intr() & 241 ~SICILIAN_INTR_PAR); 242 splx(s); 243 244 return (0); 245 } 246 247 void 248 parstart(arg) 249 void *arg; 250 { 251 struct par_softc *sc = arg; 252 #ifdef DEBUG 253 if (pardebug & PDB_FOLLOW) 254 printf("parstart(%x)\n", sc->sc_dev.dv_unit); 255 #endif 256 sc->sc_flags &= ~PARF_DELAY; 257 wakeup(sc); 258 } 259 260 void 261 partimo(arg) 262 void *arg; 263 { 264 struct par_softc *sc = arg; 265 #ifdef DEBUG 266 if (pardebug & PDB_FOLLOW) 267 printf("partimo(%x)\n", sc->sc_dev.dv_unit); 268 #endif 269 sc->sc_flags &= ~(PARF_UIO|PARF_TIMO); 270 wakeup(sc); 271 } 272 273 int 274 parwrite(dev, uio, flag) 275 dev_t dev; 276 struct uio *uio; 277 int flag; 278 { 279 280 #ifdef DEBUG 281 if (pardebug & PDB_FOLLOW) 282 printf("parwrite(%x, %p)\n", dev, uio); 283 #endif 284 return (parrw(dev, uio)); 285 } 286 287 int 288 parrw(dev, uio) 289 dev_t dev; 290 register struct uio *uio; 291 { 292 int unit = UNIT(dev); 293 register struct par_softc *sc = par_cd.cd_devs[unit]; 294 register int s, len, cnt; 295 register char *cp; 296 int error = 0; 297 int buflen; 298 char *buf; 299 300 if (!!(sc->sc_flags & PARF_OREAD) ^ (uio->uio_rw == UIO_READ)) 301 return EINVAL; 302 303 if (uio->uio_resid == 0) 304 return(0); 305 306 buflen = min(sc->sc_burst, uio->uio_resid); 307 buf = (char *)malloc(buflen, M_DEVBUF, M_WAITOK); 308 sc->sc_flags |= PARF_UIO; 309 if (sc->sc_timo > 0) { 310 sc->sc_flags |= PARF_TIMO; 311 callout_reset(&sc->sc_timo_ch, sc->sc_timo, partimo, sc); 312 } 313 while (uio->uio_resid > 0) { 314 len = min(buflen, uio->uio_resid); 315 cp = buf; 316 if (uio->uio_rw == UIO_WRITE) { 317 error = uiomove(cp, len, uio); 318 if (error) 319 break; 320 } 321 again: 322 s = spl1(); 323 /* 324 * Check if we timed out during sleep or uiomove 325 */ 326 (void) spllowersoftclock(); 327 if ((sc->sc_flags & PARF_UIO) == 0) { 328 #ifdef DEBUG 329 if (pardebug & PDB_IO) 330 printf("parrw: uiomove/sleep timo, flags %x\n", 331 sc->sc_flags); 332 #endif 333 if (sc->sc_flags & PARF_TIMO) { 334 callout_stop(&sc->sc_timo_ch); 335 sc->sc_flags &= ~PARF_TIMO; 336 } 337 splx(s); 338 break; 339 } 340 splx(s); 341 /* 342 * Perform the operation 343 */ 344 cnt = parsend(sc, cp, len); 345 if (cnt < 0) { 346 error = -cnt; 347 break; 348 } 349 350 s = splsoftclock(); 351 /* 352 * Operation timeout (or non-blocking), quit now. 353 */ 354 if ((sc->sc_flags & PARF_UIO) == 0) { 355 #ifdef DEBUG 356 if (pardebug & PDB_IO) 357 printf("parrw: timeout/done\n"); 358 #endif 359 splx(s); 360 break; 361 } 362 /* 363 * Implement inter-read delay 364 */ 365 if (sc->sc_delay > 0) { 366 sc->sc_flags |= PARF_DELAY; 367 callout_reset(&sc->sc_start_ch, sc->sc_delay, 368 parstart, sc); 369 error = tsleep(sc, PCATCH|(PZERO-1), "par-cdelay", 0); 370 if (error) { 371 splx(s); 372 break; 373 } 374 } 375 splx(s); 376 /* 377 * Must not call uiomove again til we've used all data 378 * that we already grabbed. 379 */ 380 if (uio->uio_rw == UIO_WRITE && cnt != len) { 381 cp += cnt; 382 len -= cnt; 383 cnt = 0; 384 goto again; 385 } 386 } 387 s = splsoftclock(); 388 if (sc->sc_flags & PARF_TIMO) { 389 callout_stop(&sc->sc_timo_ch); 390 sc->sc_flags &= ~PARF_TIMO; 391 } 392 if (sc->sc_flags & PARF_DELAY) { 393 callout_stop(&sc->sc_start_ch); 394 sc->sc_flags &= ~PARF_DELAY; 395 } 396 splx(s); 397 /* 398 * Adjust for those chars that we uiomove'ed but never wrote 399 */ 400 if (uio->uio_rw == UIO_WRITE && cnt != len) { 401 uio->uio_resid += (len - cnt); 402 #ifdef DEBUG 403 if (pardebug & PDB_IO) 404 printf("parrw: short write, adjust by %d\n", 405 len-cnt); 406 #endif 407 } 408 free(buf, M_DEVBUF); 409 #ifdef DEBUG 410 if (pardebug & (PDB_FOLLOW|PDB_IO)) 411 printf("parrw: return %d, resid %d\n", error, uio->uio_resid); 412 #endif 413 return (error); 414 } 415 416 int 417 parioctl(dev, cmd, data, flag, p) 418 dev_t dev; 419 u_long cmd; 420 caddr_t data; 421 int flag; 422 struct proc *p; 423 { 424 struct par_softc *sc = par_cd.cd_devs[UNIT(dev)]; 425 struct parparam *pp, *upp; 426 int error = 0; 427 428 switch (cmd) { 429 case PARIOCGPARAM: 430 pp = &sc->sc_param; 431 upp = (struct parparam *)data; 432 upp->burst = pp->burst; 433 upp->timo = parhztoms(pp->timo); 434 upp->delay = parhztoms(pp->delay); 435 break; 436 437 case PARIOCSPARAM: 438 pp = &sc->sc_param; 439 upp = (struct parparam *)data; 440 if (upp->burst < PAR_BURST_MIN || upp->burst > PAR_BURST_MAX || 441 upp->delay < PAR_DELAY_MIN || upp->delay > PAR_DELAY_MAX) 442 return(EINVAL); 443 pp->burst = upp->burst; 444 pp->timo = parmstohz(upp->timo); 445 pp->delay = parmstohz(upp->delay); 446 break; 447 448 default: 449 return(EINVAL); 450 } 451 return (error); 452 } 453 454 int 455 parhztoms(h) 456 int h; 457 { 458 extern int hz; 459 register int m = h; 460 461 if (m > 0) 462 m = m * 1000 / hz; 463 return(m); 464 } 465 466 int 467 parmstohz(m) 468 int m; 469 { 470 extern int hz; 471 register int h = m; 472 473 if (h > 0) { 474 h = h * hz / 1000; 475 if (h == 0) 476 h = 1000 / hz; 477 } 478 return(h); 479 } 480 481 /* stuff below here if for interrupt driven output of data thru 482 the parallel port. */ 483 484 int partimeout_pending; 485 int parsend_pending; 486 487 void 488 parintr(arg) 489 void *arg; 490 { 491 int s, mask; 492 493 mask = (int)arg; 494 s = splclock(); 495 496 intio_set_sicilian_intr(intio_get_sicilian_intr() & 497 ~SICILIAN_INTR_PAR); 498 499 #ifdef DEBUG 500 if (pardebug & PDB_INTERRUPT) 501 printf ("parintr %d(%s)\n", mask, mask ? "FLG" : "tout"); 502 #endif 503 /* if invoked from timeout handler, mask will be 0, 504 * if from interrupt, it will contain the cia-icr mask, 505 * which is != 0 506 */ 507 if (mask) { 508 if (partimeout_pending) 509 callout_stop(&intr_callout); 510 if (parsend_pending) 511 parsend_pending = 0; 512 } 513 514 /* either way, there won't be a timeout pending any longer */ 515 partimeout_pending = 0; 516 517 wakeup(parintr); 518 splx (s); 519 } 520 521 int 522 parsendch(sc, ch) 523 struct par_softc *sc; 524 u_char ch; 525 { 526 int error = 0; 527 int s; 528 529 /* if either offline, busy or out of paper, wait for that 530 condition to clear */ 531 s = spl1(); 532 while (!error 533 && (parsend_pending 534 || !(intio_get_sicilian_intr() & SICILIAN_STAT_PAR))) 535 { 536 extern int hz; 537 538 /* wait a second, and try again */ 539 callout_reset(&intr_callout, hz, parintr, 0); 540 partimeout_pending = 1; 541 /* this is essentially a flipflop to have us wait for the 542 first character being transmitted when trying to transmit 543 the second, etc. */ 544 parsend_pending = 0; 545 /* it's quite important that a parallel putc can be 546 interrupted, given the possibility to lock a printer 547 in an offline condition.. */ 548 if ((error = tsleep (parintr, PCATCH|(PZERO-1), "parsendch", 0))) { 549 #ifdef DEBUG 550 if (pardebug & PDB_INTERRUPT) 551 printf ("parsendch interrupted, error = %d\n", error); 552 #endif 553 if (partimeout_pending) 554 callout_stop(&intr_callout); 555 556 partimeout_pending = 0; 557 } 558 } 559 560 if (!error) { 561 #ifdef DEBUG 562 if (pardebug & PDB_INTERRUPT) 563 printf ("#%d", ch); 564 #endif 565 bus_space_write_1 (sc->sc_bst, sc->sc_bsh, PAR_DATA, ch); 566 DELAY(1); /* (DELAY(1) == 1us) > 0.5us */ 567 bus_space_write_1 (sc->sc_bst, sc->sc_bsh, PAR_STROBE, 0); 568 intio_set_sicilian_intr (intio_get_sicilian_intr() | 569 SICILIAN_INTR_PAR); 570 DELAY(1); 571 bus_space_write_1 (sc->sc_bst, sc->sc_bsh, PAR_STROBE, 1); 572 parsend_pending = 1; 573 } 574 575 splx (s); 576 577 return error; 578 } 579 580 581 int 582 parsend(sc, buf, len) 583 struct par_softc *sc; 584 u_char *buf; 585 int len; 586 { 587 int err, orig_len = len; 588 589 for (; len; len--, buf++) 590 if ((err = parsendch (sc, *buf))) 591 return err < 0 ? -EINTR : -err; 592 593 /* either all or nothing.. */ 594 return orig_len; 595 } 596