1 /* 2 * Copyright (c) 1990 William F. Jolitz, TeleMuse 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This software is a component of "386BSD" developed by 16 * William F. Jolitz, TeleMuse. 17 * 4. Neither the name of the developer nor the name "386BSD" 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS A COMPONENT OF 386BSD DEVELOPED BY WILLIAM F. JOLITZ 22 * AND IS INTENDED FOR RESEARCH AND EDUCATIONAL PURPOSES ONLY. THIS 23 * SOFTWARE SHOULD NOT BE CONSIDERED TO BE A COMMERCIAL PRODUCT. 24 * THE DEVELOPER URGES THAT USERS WHO REQUIRE A COMMERCIAL PRODUCT 25 * NOT MAKE USE OF THIS WORK. 26 * 27 * FOR USERS WHO WISH TO UNDERSTAND THE 386BSD SYSTEM DEVELOPED 28 * BY WILLIAM F. JOLITZ, WE RECOMMEND THE USER STUDY WRITTEN 29 * REFERENCES SUCH AS THE "PORTING UNIX TO THE 386" SERIES 30 * (BEGINNING JANUARY 1991 "DR. DOBBS JOURNAL", USA AND BEGINNING 31 * JUNE 1991 "UNIX MAGAZIN", GERMANY) BY WILLIAM F. JOLITZ AND 32 * LYNNE GREER JOLITZ, AS WELL AS OTHER BOOKS ON UNIX AND THE 33 * ON-LINE 386BSD USER MANUAL BEFORE USE. A BOOK DISCUSSING THE INTERNALS 34 * OF 386BSD ENTITLED "386BSD FROM THE INSIDE OUT" WILL BE AVAILABLE LATE 1992. 35 * 36 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPER ``AS IS'' AND 37 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 39 * ARE DISCLAIMED. IN NO EVENT SHALL THE DEVELOPER BE LIABLE 40 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 41 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 42 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 44 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 45 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 46 * SUCH DAMAGE. 47 * 48 * from: unknown origin, 386BSD 0.1 49 * From Id: lpt.c,v 1.55.2.1 1996/11/12 09:08:38 phk Exp 50 * From Id: nlpt.c,v 1.14 1999/02/08 13:55:43 des Exp 51 * $FreeBSD: src/sys/dev/ppbus/lpt.c,v 1.15.2.3 2000/07/07 00:30:40 obrien Exp $ 52 */ 53 54 /* 55 * Device Driver for AT parallel printer port 56 * Written by William Jolitz 12/18/90 57 */ 58 59 /* 60 * Updated for ppbus by Nicolas Souchu 61 * [Mon Jul 28 1997] 62 */ 63 64 #include "opt_lpt.h" 65 66 #include <sys/param.h> 67 #include <sys/systm.h> 68 #include <sys/module.h> 69 #include <sys/bus.h> 70 #include <sys/conf.h> 71 #include <sys/device.h> 72 #include <sys/kernel.h> 73 #include <sys/uio.h> 74 #include <sys/syslog.h> 75 #include <sys/thread2.h> 76 #include <sys/malloc.h> 77 #include <sys/rman.h> 78 79 #include <machine/clock.h> 80 81 #include "lptio.h" 82 #include <bus/ppbus/ppbconf.h> 83 #include <bus/ppbus/ppb_1284.h> 84 #include "lpt.h" 85 #include "ppbus_if.h" 86 #include <bus/ppbus/ppbio.h> 87 88 MALLOC_DEFINE(M_LPT, "lpt", "LPT buffers"); 89 90 #ifndef LPT_DEBUG 91 #define lprintf(args) 92 #else 93 #define lprintf(args) \ 94 do { \ 95 if (lptflag) \ 96 kprintf args; \ 97 } while (0) 98 static int volatile lptflag = 1; 99 #endif 100 101 #define LPINITRDY 4 /* wait up to 4 seconds for a ready */ 102 #define LPTOUTINITIAL 10 /* initial timeout to wait for ready 1/10 s */ 103 #define LPTOUTMAX 1 /* maximal timeout 1 s */ 104 #define BUFSIZE 1024 105 #define BUFSTATSIZE 32 106 107 #define LPTUNIT(s) ((s)&0x03) 108 #define LPTFLAGS(s) ((s)&0xfc) 109 110 struct lpt_data { 111 short sc_state; 112 /* default case: negative prime, negative ack, handshake strobe, 113 prime once */ 114 u_char sc_control; 115 char sc_flags; 116 #define LP_UNITMASK 0x03 /* up to 4 units */ 117 #define LP_POS_INIT 0x04 /* if we are a postive init signal */ 118 #define LP_POS_ACK 0x08 /* if we are a positive going ack */ 119 #define LP_NO_PRIME 0x10 /* don't prime the printer at all */ 120 #define LP_PRIMEOPEN 0x20 /* prime on every open */ 121 #define LP_AUTOLF 0x40 /* tell printer to do an automatic lf */ 122 #define LP_BYPASS 0x80 /* bypass printer ready checks */ 123 void *sc_inbuf; 124 void *sc_statbuf; 125 short sc_xfercnt ; 126 char sc_primed; 127 char *sc_cp ; 128 u_short sc_irq ; /* IRQ status of port */ 129 #define LP_HAS_IRQ 0x01 /* we have an irq available */ 130 #define LP_USE_IRQ 0x02 /* we are using our irq */ 131 #define LP_ENABLE_IRQ 0x04 /* enable IRQ on open */ 132 #define LP_ENABLE_EXT 0x10 /* we shall use advanced mode when possible */ 133 u_char sc_backoff ; /* time to call lptout() again */ 134 135 struct resource *intr_resource; /* interrupt resource */ 136 void *intr_cookie; /* interrupt registration cookie */ 137 struct callout sc_callout; 138 }; 139 140 #define LPT_NAME "lpt" /* our official name */ 141 142 static timeout_t lptout; 143 static int lpt_port_test(device_t dev, u_char data, u_char mask); 144 static int lpt_detect(device_t dev); 145 146 #define DEVTOSOFTC(dev) \ 147 ((struct lpt_data *)device_get_softc(dev)) 148 #define UNITOSOFTC(unit) \ 149 ((struct lpt_data *)devclass_get_softc(lpt_devclass, (unit))) 150 #define UNITODEVICE(unit) \ 151 (devclass_get_device(lpt_devclass, (unit))) 152 153 static void lptintr(device_t dev); 154 static void lpt_intr(void *arg); /* without spls */ 155 156 static devclass_t lpt_devclass; 157 158 159 /* bits for state */ 160 #define OPEN (1<<0) /* device is open */ 161 #define ASLP (1<<1) /* awaiting draining of printer */ 162 #define EERROR (1<<2) /* error was received from printer */ 163 #define OBUSY (1<<3) /* printer is busy doing output */ 164 #define LPTOUT (1<<4) /* timeout while not selected */ 165 #define TOUT (1<<5) /* timeout while not selected */ 166 #define LPTINIT (1<<6) /* waiting to initialize for open */ 167 #define INTERRUPTED (1<<7) /* write call was interrupted */ 168 169 #define HAVEBUS (1<<8) /* the driver owns the bus */ 170 171 172 /* status masks to interrogate printer status */ 173 #define RDY_MASK (LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR) /* ready ? */ 174 #define LP_READY (LPS_SEL|LPS_NBSY|LPS_NERR) 175 176 /* Printer Ready condition - from lpa.c */ 177 /* Only used in polling code */ 178 #define LPS_INVERT (LPS_NBSY | LPS_NACK | LPS_SEL | LPS_NERR) 179 #define LPS_MASK (LPS_NBSY | LPS_NACK | LPS_OUT | LPS_SEL | LPS_NERR) 180 #define NOT_READY(ppbus) ((ppb_rstr(ppbus)^LPS_INVERT)&LPS_MASK) 181 182 #define MAX_SLEEP (hz*5) /* Timeout while waiting for device ready */ 183 #define MAX_SPIN 20 /* Max delay for device ready in usecs */ 184 185 186 static d_open_t lptopen; 187 static d_close_t lptclose; 188 static d_write_t lptwrite; 189 static d_read_t lptread; 190 static d_ioctl_t lptioctl; 191 192 static struct dev_ops lpt_ops = { 193 { LPT_NAME, 0, 0 }, 194 .d_open = lptopen, 195 .d_close = lptclose, 196 .d_read = lptread, 197 .d_write = lptwrite, 198 .d_ioctl = lptioctl, 199 }; 200 201 static int 202 lpt_request_ppbus(device_t dev, int how) 203 { 204 device_t ppbus = device_get_parent(dev); 205 struct lpt_data *sc = DEVTOSOFTC(dev); 206 int error; 207 208 if (sc->sc_state & HAVEBUS) 209 return (0); 210 211 /* we have the bus only if the request succeded */ 212 if ((error = ppb_request_bus(ppbus, dev, how)) == 0) 213 sc->sc_state |= HAVEBUS; 214 215 return (error); 216 } 217 218 static int 219 lpt_release_ppbus(device_t dev) 220 { 221 device_t ppbus = device_get_parent(dev); 222 struct lpt_data *sc = DEVTOSOFTC(dev); 223 int error = 0; 224 225 if ((error = ppb_release_bus(ppbus, dev)) == 0) 226 sc->sc_state &= ~HAVEBUS; 227 228 return (error); 229 } 230 231 /* 232 * Internal routine to lptprobe to do port tests of one byte value 233 */ 234 static int 235 lpt_port_test(device_t ppbus, u_char data, u_char mask) 236 { 237 int temp, timeout; 238 239 data = data & mask; 240 ppb_wdtr(ppbus, data); 241 timeout = 10000; 242 do { 243 DELAY(10); 244 temp = ppb_rdtr(ppbus) & mask; 245 } 246 while (temp != data && --timeout); 247 lprintf(("out=%x\tin=%x\ttout=%d\n", data, temp, timeout)); 248 return (temp == data); 249 } 250 251 /* 252 * Probe simplified by replacing multiple loops with a hardcoded 253 * test pattern - 1999/02/08 des@freebsd.org 254 * 255 * New lpt port probe Geoff Rehmet - Rhodes University - 14/2/94 256 * Based partially on Rod Grimes' printer probe 257 * 258 * Logic: 259 * 1) If no port address was given, use the bios detected ports 260 * and autodetect what ports the printers are on. 261 * 2) Otherwise, probe the data port at the address given, 262 * using the method in Rod Grimes' port probe. 263 * (Much code ripped off directly from Rod's probe.) 264 * 265 * Comments from Rod's probe: 266 * Logic: 267 * 1) You should be able to write to and read back the same value 268 * to the data port. Do an alternating zeros, alternating ones, 269 * walking zero, and walking one test to check for stuck bits. 270 * 271 * 2) You should be able to write to and read back the same value 272 * to the control port lower 5 bits, the upper 3 bits are reserved 273 * per the IBM PC technical reference manauls and different boards 274 * do different things with them. Do an alternating zeros, alternating 275 * ones, walking zero, and walking one test to check for stuck bits. 276 * 277 * Some printers drag the strobe line down when the are powered off 278 * so this bit has been masked out of the control port test. 279 * 280 * XXX Some printers may not like a fast pulse on init or strobe, I 281 * don't know at this point, if that becomes a problem these bits 282 * should be turned off in the mask byte for the control port test. 283 * 284 * We are finally left with a mask of 0x14, due to some printers 285 * being adamant about holding other bits high ........ 286 * 287 * Before probing the control port, we write a 0 to the data port - 288 * If not, some printers chuck out garbage when the strobe line 289 * gets toggled. 290 * 291 * 3) Set the data and control ports to a value of 0 292 * 293 * This probe routine has been tested on Epson Lx-800, HP LJ3P, 294 * Epson FX-1170 and C.Itoh 8510RM 295 * printers. 296 * Quick exit on fail added. 297 */ 298 static int 299 lpt_detect(device_t dev) 300 { 301 device_t ppbus = device_get_parent(dev); 302 303 static u_char testbyte[18] = { 304 0x55, /* alternating zeros */ 305 0xaa, /* alternating ones */ 306 0xfe, 0xfd, 0xfb, 0xf7, 307 0xef, 0xdf, 0xbf, 0x7f, /* walking zero */ 308 0x01, 0x02, 0x04, 0x08, 309 0x10, 0x20, 0x40, 0x80 /* walking one */ 310 }; 311 int i, error, status; 312 313 status = 1; /* assume success */ 314 315 if ((error = lpt_request_ppbus(dev, PPB_DONTWAIT))) { 316 kprintf(LPT_NAME ": cannot alloc ppbus (%d)!\n", error); 317 status = 0; 318 goto end_probe; 319 } 320 321 for (i = 0; i < 18 && status; i++) 322 if (!lpt_port_test(ppbus, testbyte[i], 0xff)) { 323 status = 0; 324 goto end_probe; 325 } 326 327 end_probe: 328 /* write 0's to control and data ports */ 329 ppb_wdtr(ppbus, 0); 330 ppb_wctr(ppbus, 0); 331 332 lpt_release_ppbus(dev); 333 334 return (status); 335 } 336 337 /* 338 * lpt_probe() 339 */ 340 static int 341 lpt_probe(device_t dev) 342 { 343 struct lpt_data *sc; 344 345 sc = DEVTOSOFTC(dev); 346 bzero(sc, sizeof(struct lpt_data)); 347 348 /* 349 * Now, try to detect the printer. 350 */ 351 if (!lpt_detect(dev)) 352 return (ENXIO); 353 354 device_set_desc(dev, "Printer"); 355 356 return (0); 357 } 358 359 static int 360 lpt_attach(device_t dev) 361 { 362 device_t ppbus = device_get_parent(dev); 363 struct lpt_data *sc = DEVTOSOFTC(dev); 364 int zero = 0, unit = device_get_unit(dev); 365 int error; 366 uintptr_t irq; 367 368 sc->sc_primed = 0; /* not primed yet */ 369 callout_init(&sc->sc_callout); 370 371 if ((error = lpt_request_ppbus(dev, PPB_DONTWAIT))) { 372 kprintf(LPT_NAME ": cannot alloc ppbus (%d)!\n", error); 373 return (0); 374 } 375 376 ppb_wctr(ppbus, LPC_NINIT); 377 378 /* check if we can use interrupt, should be done by ppc stuff */ 379 lprintf(("oldirq %x\n", sc->sc_irq)); 380 381 /* retrieve the ppbus irq */ 382 BUS_READ_IVAR(ppbus, dev, PPBUS_IVAR_IRQ, &irq); 383 384 if (irq > 0) { 385 /* declare our interrupt handler */ 386 sc->intr_resource = bus_alloc_resource(dev, SYS_RES_IRQ, 387 &zero, irq, irq, 1, RF_SHAREABLE); 388 } 389 if (sc->intr_resource) { 390 sc->sc_irq = LP_HAS_IRQ | LP_USE_IRQ | LP_ENABLE_IRQ; 391 device_printf(dev, "Interrupt-driven port\n"); 392 } else { 393 sc->sc_irq = 0; 394 device_printf(dev, "Polled port\n"); 395 } 396 lprintf(("irq %x %x\n", irq, sc->sc_irq)); 397 398 lpt_release_ppbus(dev); 399 400 make_dev(&lpt_ops, unit, UID_ROOT, GID_WHEEL, 401 0600, LPT_NAME "%d", unit); 402 make_dev(&lpt_ops, unit | LP_BYPASS, UID_ROOT, GID_WHEEL, 403 0600, LPT_NAME "%d.ctl", unit); 404 return (0); 405 } 406 407 static void 408 lptout(void *arg) 409 { 410 device_t dev = (device_t)arg; 411 struct lpt_data *sc = DEVTOSOFTC(dev); 412 #ifdef LPT_DEBUG 413 device_t ppbus = device_get_parent(dev); 414 #endif 415 416 lprintf(("T %x ", ppb_rstr(ppbus))); 417 if (sc->sc_state & OPEN) { 418 sc->sc_backoff++; 419 if (sc->sc_backoff > hz/LPTOUTMAX) 420 sc->sc_backoff = sc->sc_backoff > hz/LPTOUTMAX; 421 callout_reset(&sc->sc_callout, sc->sc_backoff, lptout, dev); 422 } else { 423 sc->sc_state &= ~TOUT; 424 } 425 426 if (sc->sc_state & EERROR) 427 sc->sc_state &= ~EERROR; 428 429 /* 430 * Avoid possible hangs due to missed interrupts 431 */ 432 if (sc->sc_xfercnt) { 433 lptintr(dev); 434 } else { 435 sc->sc_state &= ~OBUSY; 436 wakeup((caddr_t)dev); 437 } 438 } 439 440 /* 441 * lptopen -- reset the printer, then wait until it's selected and not busy. 442 * If LP_BYPASS flag is selected, then we do not try to select the 443 * printer -- this is just used for passing ioctls. 444 */ 445 446 static int 447 lptopen(struct dev_open_args *ap) 448 { 449 cdev_t dev = ap->a_head.a_dev; 450 int trys, err; 451 u_int unit = LPTUNIT(minor(dev)); 452 struct lpt_data *sc = UNITOSOFTC(unit); 453 device_t lptdev = UNITODEVICE(unit); 454 device_t ppbus = device_get_parent(lptdev); 455 456 if (!sc) 457 return (ENXIO); 458 459 if (sc->sc_state) { 460 lprintf((LPT_NAME ": still open %x\n", sc->sc_state)); 461 return(EBUSY); 462 } else 463 sc->sc_state |= LPTINIT; 464 465 sc->sc_flags = LPTFLAGS(minor(dev)); 466 467 /* Check for open with BYPASS flag set. */ 468 if (sc->sc_flags & LP_BYPASS) { 469 sc->sc_state = OPEN; 470 return(0); 471 } 472 473 /* request the ppbus only if we don't have it already */ 474 if ((err = lpt_request_ppbus(lptdev, PPB_WAIT|PPB_INTR)) != 0) { 475 /* give it a chance to try later */ 476 sc->sc_state = 0; 477 return (err); 478 } 479 480 crit_enter(); 481 lprintf((LPT_NAME " flags 0x%x\n", sc->sc_flags)); 482 483 /* set IRQ status according to ENABLE_IRQ flag 484 */ 485 if (sc->sc_irq & LP_ENABLE_IRQ) 486 sc->sc_irq |= LP_USE_IRQ; 487 else 488 sc->sc_irq &= ~LP_USE_IRQ; 489 490 /* init printer */ 491 if ((sc->sc_flags & LP_NO_PRIME) == 0) { 492 if((sc->sc_flags & LP_PRIMEOPEN) || sc->sc_primed == 0) { 493 ppb_wctr(ppbus, 0); 494 sc->sc_primed++; 495 DELAY(500); 496 } 497 } 498 499 ppb_wctr(ppbus, LPC_SEL|LPC_NINIT); 500 501 /* wait till ready (printer running diagnostics) */ 502 trys = 0; 503 do { 504 /* ran out of waiting for the printer */ 505 if (trys++ >= LPINITRDY*4) { 506 crit_exit(); 507 sc->sc_state = 0; 508 lprintf(("status %x\n", ppb_rstr(ppbus))); 509 510 lpt_release_ppbus(lptdev); 511 return (EBUSY); 512 } 513 514 /* wait 1/4 second, give up if we get a signal */ 515 if (tsleep((caddr_t)lptdev, PCATCH, "lptinit", hz/4) != 516 EWOULDBLOCK) { 517 sc->sc_state = 0; 518 crit_exit(); 519 520 lpt_release_ppbus(lptdev); 521 return (EBUSY); 522 } 523 524 /* is printer online and ready for output */ 525 } while ((ppb_rstr(ppbus) & 526 (LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR)) != 527 (LPS_SEL|LPS_NBSY|LPS_NERR)); 528 529 sc->sc_control = LPC_SEL|LPC_NINIT; 530 if (sc->sc_flags & LP_AUTOLF) 531 sc->sc_control |= LPC_AUTOL; 532 533 /* enable interrupt if interrupt-driven */ 534 if (sc->sc_irq & LP_USE_IRQ) 535 sc->sc_control |= LPC_ENA; 536 537 ppb_wctr(ppbus, sc->sc_control); 538 539 sc->sc_state = OPEN; 540 sc->sc_inbuf = kmalloc(BUFSIZE, M_LPT, M_WAITOK); 541 sc->sc_statbuf = kmalloc(BUFSTATSIZE, M_LPT, M_WAITOK); 542 sc->sc_xfercnt = 0; 543 544 crit_exit(); 545 546 /* release the ppbus */ 547 lpt_release_ppbus(lptdev); 548 549 /* only use timeout if using interrupt */ 550 lprintf(("irq %x\n", sc->sc_irq)); 551 if (sc->sc_irq & LP_USE_IRQ) { 552 sc->sc_state |= TOUT; 553 sc->sc_backoff = hz / LPTOUTINITIAL; 554 callout_reset(&sc->sc_callout, sc->sc_backoff, lptout, lptdev); 555 } 556 557 lprintf(("opened.\n")); 558 return(0); 559 } 560 561 /* 562 * lptclose -- close the device, free the local line buffer. 563 * 564 * Check for interrupted write call added. 565 */ 566 567 static int 568 lptclose(struct dev_close_args *ap) 569 { 570 cdev_t dev = ap->a_head.a_dev; 571 u_int unit = LPTUNIT(minor(dev)); 572 struct lpt_data *sc = UNITOSOFTC(unit); 573 device_t lptdev = UNITODEVICE(unit); 574 device_t ppbus = device_get_parent(lptdev); 575 int err; 576 577 if(sc->sc_flags & LP_BYPASS) 578 goto end_close; 579 580 if ((err = lpt_request_ppbus(lptdev, PPB_WAIT|PPB_INTR)) != 0) 581 return (err); 582 583 sc->sc_state &= ~OPEN; 584 585 /* if the last write was interrupted, don't complete it */ 586 if((!(sc->sc_state & INTERRUPTED)) && (sc->sc_irq & LP_USE_IRQ)) { 587 while ((ppb_rstr(ppbus) & 588 (LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR)) != 589 (LPS_SEL|LPS_NBSY|LPS_NERR) || sc->sc_xfercnt) { 590 /* wait 1/4 second, give up if we get a signal */ 591 if (tsleep((caddr_t)lptdev, PCATCH, 592 "lpclose", hz) != EWOULDBLOCK) { 593 break; 594 } 595 } 596 } 597 callout_stop(&sc->sc_callout); 598 ppb_wctr(ppbus, LPC_NINIT); 599 kfree(sc->sc_inbuf, M_LPT); 600 kfree(sc->sc_statbuf, M_LPT); 601 602 end_close: 603 /* release the bus anyway 604 * unregistration of interrupt forced by release 605 */ 606 lpt_release_ppbus(lptdev); 607 608 sc->sc_state = 0; 609 sc->sc_xfercnt = 0; 610 lprintf(("closed.\n")); 611 return(0); 612 } 613 614 /* 615 * lpt_pushbytes() 616 * Workhorse for actually spinning and writing bytes to printer 617 * Derived from lpa.c 618 * Originally by ? 619 * 620 * This code is only used when we are polling the port 621 */ 622 static int 623 lpt_pushbytes(device_t dev) 624 { 625 struct lpt_data *sc = DEVTOSOFTC(dev); 626 device_t ppbus = device_get_parent(dev); 627 int spin, err, tic; 628 char ch; 629 630 lprintf(("p")); 631 /* loop for every character .. */ 632 while (sc->sc_xfercnt > 0) { 633 /* printer data */ 634 ch = *(sc->sc_cp); 635 sc->sc_cp++; 636 sc->sc_xfercnt--; 637 638 /* 639 * Wait for printer ready. 640 * Loop 20 usecs testing BUSY bit, then sleep 641 * for exponentially increasing timeout. (vak) 642 */ 643 for (spin = 0; NOT_READY(ppbus) && spin < MAX_SPIN; ++spin) 644 DELAY(1); /* XXX delay is NOT this accurate! */ 645 if (spin >= MAX_SPIN) { 646 tic = 0; 647 while (NOT_READY(ppbus)) { 648 /* 649 * Now sleep, every cycle a 650 * little longer .. 651 */ 652 tic = tic + tic + 1; 653 /* 654 * But no more than 10 seconds. (vak) 655 */ 656 if (tic > MAX_SLEEP) 657 tic = MAX_SLEEP; 658 err = tsleep((caddr_t)dev, 0, 659 LPT_NAME "poll", tic); 660 if (err != EWOULDBLOCK) { 661 return (err); 662 } 663 } 664 } 665 666 /* output data */ 667 ppb_wdtr(ppbus, ch); 668 /* strobe */ 669 ppb_wctr(ppbus, sc->sc_control|LPC_STB); 670 ppb_wctr(ppbus, sc->sc_control); 671 672 } 673 return(0); 674 } 675 676 /* 677 * lptread --retrieve printer status in IEEE1284 NIBBLE mode 678 */ 679 680 static int 681 lptread(struct dev_read_args *ap) 682 { 683 cdev_t dev = ap->a_head.a_dev; 684 struct uio *uio = ap->a_uio; 685 u_int unit = LPTUNIT(minor(dev)); 686 struct lpt_data *sc = UNITOSOFTC(unit); 687 device_t lptdev = UNITODEVICE(unit); 688 device_t ppbus = device_get_parent(lptdev); 689 int error = 0, len; 690 691 if (sc->sc_flags & LP_BYPASS) { 692 /* we can't do reads in bypass mode */ 693 return (EPERM); 694 } 695 696 if ((error = ppb_1284_negociate(ppbus, PPB_NIBBLE, 0))) 697 return (error); 698 699 /* read data in an other buffer, read/write may be simultaneous */ 700 len = 0; 701 while (uio->uio_resid) { 702 error = ppb_1284_read(ppbus, PPB_NIBBLE, sc->sc_statbuf, 703 (int)szmin(BUFSTATSIZE, uio->uio_resid), 704 &len); 705 if (error) 706 goto error; 707 708 if (!len) 709 goto error; /* no more data */ 710 711 if ((error = uiomove(sc->sc_statbuf, (size_t)len, uio))) 712 goto error; 713 } 714 715 error: 716 ppb_1284_terminate(ppbus); 717 return (error); 718 } 719 720 /* 721 * lptwrite --copy a line from user space to a local buffer, then call 722 * putc to get the chars moved to the output queue. 723 * 724 * Flagging of interrupted write added. 725 */ 726 727 static int 728 lptwrite(struct dev_write_args *ap) 729 { 730 cdev_t dev = ap->a_head.a_dev; 731 struct uio *uio = ap->a_uio; 732 unsigned n; 733 int err; 734 u_int unit = LPTUNIT(minor(dev)); 735 struct lpt_data *sc = UNITOSOFTC(unit); 736 device_t lptdev = UNITODEVICE(unit); 737 device_t ppbus = device_get_parent(lptdev); 738 739 if(sc->sc_flags & LP_BYPASS) { 740 /* we can't do writes in bypass mode */ 741 return(EPERM); 742 } 743 744 /* request the ppbus only if we don't have it already */ 745 /* XXX interrupt registration?! */ 746 if ((err = lpt_request_ppbus(lptdev, PPB_WAIT|PPB_INTR)) != 0) 747 return (err); 748 749 /* if interrupts are working, register the handler */ 750 if (sc->sc_irq & LP_USE_IRQ) { 751 /* register our interrupt handler */ 752 err = BUS_SETUP_INTR(ppbus, lptdev, sc->intr_resource, 753 0, lpt_intr, lptdev, 754 &sc->intr_cookie, NULL); 755 if (err) { 756 device_printf(lptdev, "handler registration failed, polled mode.\n"); 757 sc->sc_irq &= ~LP_USE_IRQ; 758 } 759 } 760 761 sc->sc_state &= ~INTERRUPTED; 762 while ((n = (unsigned)szmin(BUFSIZE, uio->uio_resid)) != 0) { 763 sc->sc_cp = sc->sc_inbuf; 764 uiomove(sc->sc_cp, (size_t)n, uio); 765 sc->sc_xfercnt = n ; 766 767 if (sc->sc_irq & LP_ENABLE_EXT) { 768 /* try any extended mode */ 769 err = ppb_write(ppbus, sc->sc_cp, 770 sc->sc_xfercnt, 0); 771 switch (err) { 772 case 0: 773 /* if not all data was sent, we could rely 774 * on polling for the last bytes */ 775 sc->sc_xfercnt = 0; 776 break; 777 case EINTR: 778 sc->sc_state |= INTERRUPTED; 779 return(err); 780 case EINVAL: 781 /* advanced mode not avail */ 782 log(LOG_NOTICE, LPT_NAME "%d: advanced mode not avail, polling\n", unit); 783 break; 784 default: 785 return(err); 786 } 787 } else while ((sc->sc_xfercnt > 0)&&(sc->sc_irq & LP_USE_IRQ)) { 788 lprintf(("i")); 789 /* if the printer is ready for a char, */ 790 /* give it one */ 791 if ((sc->sc_state & OBUSY) == 0){ 792 lprintf(("\nC %d. ", sc->sc_xfercnt)); 793 lptintr(lptdev); 794 } 795 lprintf(("W ")); 796 if (sc->sc_state & OBUSY) 797 if ((err = tsleep((caddr_t)lptdev, 798 PCATCH, LPT_NAME "write", 0))) { 799 sc->sc_state |= INTERRUPTED; 800 return(err); 801 } 802 } 803 804 /* check to see if we must do a polled write */ 805 if(!(sc->sc_irq & LP_USE_IRQ) && (sc->sc_xfercnt)) { 806 lprintf(("p")); 807 808 err = lpt_pushbytes(lptdev); 809 810 if (err) 811 return(err); 812 } 813 } 814 815 /* we have not been interrupted, release the ppbus */ 816 lpt_release_ppbus(lptdev); 817 818 return(0); 819 } 820 821 /* 822 * lpt_intr -- handle printer interrupts which occur when the printer is 823 * ready to accept another char. 824 * 825 * do checking for interrupted write call. 826 */ 827 828 static void 829 lpt_intr(void *arg) 830 { 831 device_t lptdev = (device_t)arg; 832 device_t ppbus = device_get_parent(lptdev); 833 struct lpt_data *sc = DEVTOSOFTC(lptdev); 834 int sts = 0; 835 int i; 836 837 /* we must own the bus to use it */ 838 if ((sc->sc_state & HAVEBUS) == 0) 839 return; 840 841 /* 842 * Is printer online and ready for output? 843 * 844 * Avoid falling back to lptout() too quickly. First spin-loop 845 * to see if the printer will become ready ``really soon now''. 846 */ 847 for (i = 0; i < 100 && 848 ((sts=ppb_rstr(ppbus)) & RDY_MASK) != LP_READY; i++) ; 849 850 if ((sts & RDY_MASK) == LP_READY) { 851 sc->sc_state = (sc->sc_state | OBUSY) & ~EERROR; 852 sc->sc_backoff = hz/LPTOUTINITIAL; 853 854 if (sc->sc_xfercnt) { 855 /* send char */ 856 /*lprintf(("%x ", *sc->sc_cp)); */ 857 ppb_wdtr(ppbus, *sc->sc_cp++) ; 858 ppb_wctr(ppbus, sc->sc_control|LPC_STB); 859 /* DELAY(X) */ 860 ppb_wctr(ppbus, sc->sc_control); 861 862 /* any more data for printer */ 863 if(--(sc->sc_xfercnt) > 0) return; 864 } 865 866 /* 867 * No more data waiting for printer. 868 * Wakeup is not done if write call was not interrupted. 869 */ 870 sc->sc_state &= ~OBUSY; 871 872 if(!(sc->sc_state & INTERRUPTED)) 873 wakeup((caddr_t)lptdev); 874 lprintf(("w ")); 875 return; 876 } else { /* check for error */ 877 if(((sts & (LPS_NERR | LPS_OUT) ) != LPS_NERR) && 878 (sc->sc_state & OPEN)) 879 sc->sc_state |= EERROR; 880 /* lptout() will jump in and try to restart. */ 881 } 882 lprintf(("sts %x ", sts)); 883 } 884 885 static void 886 lptintr(device_t dev) 887 { 888 /* call the interrupt at required spl level */ 889 crit_enter(); 890 891 lpt_intr(dev); 892 893 crit_exit(); 894 } 895 896 static int 897 lptioctl(struct dev_ioctl_args *ap) 898 { 899 cdev_t dev = ap->a_head.a_dev; 900 int error = 0; 901 u_int unit = LPTUNIT(minor(dev)); 902 struct lpt_data *sc = UNITOSOFTC(unit); 903 u_char old_sc_irq; /* old printer IRQ status */ 904 905 switch (ap->a_cmd) { 906 case LPT_IRQ : 907 if(sc->sc_irq & LP_HAS_IRQ) { 908 /* 909 * NOTE: 910 * If the IRQ status is changed, 911 * this will only be visible on the 912 * next open. 913 * 914 * If interrupt status changes, 915 * this gets syslog'd. 916 */ 917 old_sc_irq = sc->sc_irq; 918 switch(*(int*)ap->a_data) { 919 case 0: 920 sc->sc_irq &= (~LP_ENABLE_IRQ); 921 break; 922 case 1: 923 sc->sc_irq &= (~LP_ENABLE_EXT); 924 sc->sc_irq |= LP_ENABLE_IRQ; 925 break; 926 case 2: 927 /* classic irq based transfer and advanced 928 * modes are in conflict 929 */ 930 sc->sc_irq &= (~LP_ENABLE_IRQ); 931 sc->sc_irq |= LP_ENABLE_EXT; 932 break; 933 case 3: 934 sc->sc_irq &= (~LP_ENABLE_EXT); 935 break; 936 default: 937 break; 938 } 939 940 if (old_sc_irq != sc->sc_irq ) 941 log(LOG_NOTICE, LPT_NAME "%d: switched to %s %s mode\n", 942 unit, 943 (sc->sc_irq & LP_ENABLE_IRQ)? 944 "interrupt-driven":"polled", 945 (sc->sc_irq & LP_ENABLE_EXT)? 946 "extended":"standard"); 947 } else /* polled port */ 948 error = EOPNOTSUPP; 949 break; 950 default: 951 error = ENODEV; 952 } 953 954 return(error); 955 } 956 957 /* 958 * Because lpt is a static device that always exists under a ppbus device, 959 * and not scanned by the ppbus device, we need an identify function to 960 * install its device. 961 */ 962 static device_method_t lpt_methods[] = { 963 /* device interface */ 964 DEVMETHOD(device_identify, bus_generic_identify), 965 DEVMETHOD(device_probe, lpt_probe), 966 DEVMETHOD(device_attach, lpt_attach), 967 968 { 0, 0 } 969 }; 970 971 static driver_t lpt_driver = { 972 LPT_NAME, 973 lpt_methods, 974 sizeof(struct lpt_data), 975 }; 976 977 DRIVER_MODULE(lpt, ppbus, lpt_driver, lpt_devclass, 0, 0); 978 979