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