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 */ 52 53 #include <sys/cdefs.h> 54 __FBSDID("$FreeBSD$"); 55 56 /* 57 * Device Driver for AT parallel printer port 58 * Written by William Jolitz 12/18/90 59 */ 60 61 /* 62 * Updated for ppbus by Nicolas Souchu 63 * [Mon Jul 28 1997] 64 */ 65 66 #include "opt_lpt.h" 67 68 #include <sys/param.h> 69 #include <sys/systm.h> 70 #include <sys/module.h> 71 #include <sys/bus.h> 72 #include <sys/conf.h> 73 #include <sys/kernel.h> 74 #include <sys/uio.h> 75 #include <sys/syslog.h> 76 #include <sys/malloc.h> 77 78 #include <machine/bus.h> 79 #include <machine/resource.h> 80 #include <sys/rman.h> 81 82 #include <dev/ppbus/lptio.h> 83 #include <dev/ppbus/ppbconf.h> 84 #include <dev/ppbus/ppb_1284.h> 85 #include <dev/ppbus/lpt.h> 86 #include "ppbus_if.h" 87 #include <dev/ppbus/ppbio.h> 88 89 #ifndef LPT_DEBUG 90 #define lprintf(args) 91 #else 92 #define lprintf(args) \ 93 do { \ 94 if (lptflag) \ 95 printf args; \ 96 } while (0) 97 static int volatile lptflag = 1; 98 #endif 99 100 #define LPINITRDY 4 /* wait up to 4 seconds for a ready */ 101 #define LPTOUTINITIAL 10 /* initial timeout to wait for ready 1/10 s */ 102 #define LPTOUTMAX 1 /* maximal timeout 1 s */ 103 #define LPPRI (PZERO+8) 104 #define BUFSIZE 1024 105 #define BUFSTATSIZE 32 106 107 struct lpt_data { 108 device_t dev; 109 struct cdev *cdev; 110 struct cdev *cdev_bypass; 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_POS_INIT 0x04 /* if we are a postive init signal */ 117 #define LP_POS_ACK 0x08 /* if we are a positive going ack */ 118 #define LP_NO_PRIME 0x10 /* don't prime the printer at all */ 119 #define LP_PRIMEOPEN 0x20 /* prime on every open */ 120 #define LP_AUTOLF 0x40 /* tell printer to do an automatic lf */ 121 #define LP_BYPASS 0x80 /* bypass printer ready checks */ 122 void *sc_inbuf; 123 void *sc_statbuf; 124 short sc_xfercnt ; 125 char sc_primed; 126 char *sc_cp ; 127 u_short sc_irq ; /* IRQ status of port */ 128 #define LP_HAS_IRQ 0x01 /* we have an irq available */ 129 #define LP_USE_IRQ 0x02 /* we are using our irq */ 130 #define LP_ENABLE_IRQ 0x04 /* enable IRQ on open */ 131 #define LP_ENABLE_EXT 0x10 /* we shall use advanced mode when possible */ 132 u_char sc_backoff ; /* time to call lptout() again */ 133 134 struct resource *intr_resource; /* interrupt resource */ 135 void *intr_cookie; /* interrupt registration cookie */ 136 }; 137 138 #define LPT_NAME "lpt" /* our official name */ 139 140 static timeout_t lptout; 141 static int lpt_port_test(device_t dev, u_char data, u_char mask); 142 static int lpt_detect(device_t dev); 143 144 #define DEVTOSOFTC(dev) \ 145 ((struct lpt_data *)device_get_softc(dev)) 146 147 static void lptintr(device_t dev); 148 static void lpt_intr(void *arg); /* without spls */ 149 150 static devclass_t lpt_devclass; 151 152 153 /* bits for state */ 154 #define OPEN (1<<0) /* device is open */ 155 #define ASLP (1<<1) /* awaiting draining of printer */ 156 #define EERROR (1<<2) /* error was received from printer */ 157 #define OBUSY (1<<3) /* printer is busy doing output */ 158 #define LPTOUT (1<<4) /* timeout while not selected */ 159 #define TOUT (1<<5) /* timeout while not selected */ 160 #define LPTINIT (1<<6) /* waiting to initialize for open */ 161 #define INTERRUPTED (1<<7) /* write call was interrupted */ 162 #define HAVEBUS (1<<8) /* the driver owns the bus */ 163 164 /* status masks to interrogate printer status */ 165 #define RDY_MASK (LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR) /* ready ? */ 166 #define LP_READY (LPS_SEL|LPS_NBSY|LPS_NERR) 167 168 /* Printer Ready condition - from lpa.c */ 169 /* Only used in polling code */ 170 #define LPS_INVERT (LPS_NBSY | LPS_NACK | LPS_SEL | LPS_NERR) 171 #define LPS_MASK (LPS_NBSY | LPS_NACK | LPS_OUT | LPS_SEL | LPS_NERR) 172 #define NOT_READY(ppbus) ((ppb_rstr(ppbus)^LPS_INVERT)&LPS_MASK) 173 174 #define MAX_SLEEP (hz*5) /* Timeout while waiting for device ready */ 175 #define MAX_SPIN 20 /* Max delay for device ready in usecs */ 176 177 178 static d_open_t lptopen; 179 static d_close_t lptclose; 180 static d_write_t lptwrite; 181 static d_read_t lptread; 182 static d_ioctl_t lptioctl; 183 184 static struct cdevsw lpt_cdevsw = { 185 .d_version = D_VERSION, 186 .d_flags = D_NEEDGIANT, 187 .d_open = lptopen, 188 .d_close = lptclose, 189 .d_read = lptread, 190 .d_write = lptwrite, 191 .d_ioctl = lptioctl, 192 .d_name = LPT_NAME, 193 }; 194 195 static int 196 lpt_request_ppbus(device_t dev, int how) 197 { 198 device_t ppbus = device_get_parent(dev); 199 struct lpt_data *sc = DEVTOSOFTC(dev); 200 int error; 201 202 if (sc->sc_state & HAVEBUS) 203 return (0); 204 205 /* we have the bus only if the request succeded */ 206 if ((error = ppb_request_bus(ppbus, dev, how)) == 0) 207 sc->sc_state |= HAVEBUS; 208 209 return (error); 210 } 211 212 static int 213 lpt_release_ppbus(device_t dev) 214 { 215 device_t ppbus = device_get_parent(dev); 216 struct lpt_data *sc = DEVTOSOFTC(dev); 217 int error = 0; 218 219 if ((error = ppb_release_bus(ppbus, dev)) == 0) 220 sc->sc_state &= ~HAVEBUS; 221 222 return (error); 223 } 224 225 /* 226 * Internal routine to lptprobe to do port tests of one byte value 227 */ 228 static int 229 lpt_port_test(device_t ppbus, u_char data, u_char mask) 230 { 231 int temp, timeout; 232 233 data = data & mask; 234 ppb_wdtr(ppbus, data); 235 timeout = 10000; 236 do { 237 DELAY(10); 238 temp = ppb_rdtr(ppbus) & mask; 239 } 240 while (temp != data && --timeout); 241 lprintf(("out=%x\tin=%x\ttout=%d\n", data, temp, timeout)); 242 return (temp == data); 243 } 244 245 /* 246 * Probe simplified by replacing multiple loops with a hardcoded 247 * test pattern - 1999/02/08 des@freebsd.org 248 * 249 * New lpt port probe Geoff Rehmet - Rhodes University - 14/2/94 250 * Based partially on Rod Grimes' printer probe 251 * 252 * Logic: 253 * 1) If no port address was given, use the bios detected ports 254 * and autodetect what ports the printers are on. 255 * 2) Otherwise, probe the data port at the address given, 256 * using the method in Rod Grimes' port probe. 257 * (Much code ripped off directly from Rod's probe.) 258 * 259 * Comments from Rod's probe: 260 * Logic: 261 * 1) You should be able to write to and read back the same value 262 * to the data port. Do an alternating zeros, alternating ones, 263 * walking zero, and walking one test to check for stuck bits. 264 * 265 * 2) You should be able to write to and read back the same value 266 * to the control port lower 5 bits, the upper 3 bits are reserved 267 * per the IBM PC technical reference manauls and different boards 268 * do different things with them. Do an alternating zeros, alternating 269 * ones, walking zero, and walking one test to check for stuck bits. 270 * 271 * Some printers drag the strobe line down when the are powered off 272 * so this bit has been masked out of the control port test. 273 * 274 * XXX Some printers may not like a fast pulse on init or strobe, I 275 * don't know at this point, if that becomes a problem these bits 276 * should be turned off in the mask byte for the control port test. 277 * 278 * We are finally left with a mask of 0x14, due to some printers 279 * being adamant about holding other bits high ........ 280 * 281 * Before probing the control port, we write a 0 to the data port - 282 * If not, some printers chuck out garbage when the strobe line 283 * gets toggled. 284 * 285 * 3) Set the data and control ports to a value of 0 286 * 287 * This probe routine has been tested on Epson Lx-800, HP LJ3P, 288 * Epson FX-1170 and C.Itoh 8510RM 289 * printers. 290 * Quick exit on fail added. 291 */ 292 static int 293 lpt_detect(device_t dev) 294 { 295 device_t ppbus = device_get_parent(dev); 296 297 static u_char testbyte[18] = { 298 0x55, /* alternating zeros */ 299 0xaa, /* alternating ones */ 300 0xfe, 0xfd, 0xfb, 0xf7, 301 0xef, 0xdf, 0xbf, 0x7f, /* walking zero */ 302 0x01, 0x02, 0x04, 0x08, 303 0x10, 0x20, 0x40, 0x80 /* walking one */ 304 }; 305 int i, error, status; 306 307 status = 1; /* assume success */ 308 309 if ((error = lpt_request_ppbus(dev, PPB_DONTWAIT))) { 310 printf(LPT_NAME ": cannot alloc ppbus (%d)!\n", error); 311 status = 0; 312 goto end_probe; 313 } 314 315 for (i = 0; i < 18 && status; i++) 316 if (!lpt_port_test(ppbus, testbyte[i], 0xff)) { 317 status = 0; 318 goto end_probe; 319 } 320 321 end_probe: 322 /* write 0's to control and data ports */ 323 ppb_wdtr(ppbus, 0); 324 ppb_wctr(ppbus, 0); 325 326 lpt_release_ppbus(dev); 327 328 return (status); 329 } 330 331 static void 332 lpt_identify(driver_t *driver, device_t parent) 333 { 334 335 device_t dev; 336 337 dev = device_find_child(parent, LPT_NAME, -1); 338 if (!dev) 339 BUS_ADD_CHILD(parent, 0, LPT_NAME, -1); 340 } 341 342 /* 343 * lpt_probe() 344 */ 345 static int 346 lpt_probe(device_t dev) 347 { 348 349 if (!lpt_detect(dev)) 350 return (ENXIO); 351 352 device_set_desc(dev, "Printer"); 353 354 return (0); 355 } 356 357 static int 358 lpt_attach(device_t dev) 359 { 360 device_t ppbus = device_get_parent(dev); 361 struct lpt_data *sc = DEVTOSOFTC(dev); 362 int rid = 0, unit = device_get_unit(dev); 363 int error; 364 365 sc->sc_primed = 0; /* not primed yet */ 366 367 if ((error = lpt_request_ppbus(dev, PPB_DONTWAIT))) { 368 printf(LPT_NAME ": cannot alloc ppbus (%d)!\n", error); 369 return (0); 370 } 371 372 ppb_wctr(ppbus, LPC_NINIT); 373 374 /* check if we can use interrupt, should be done by ppc stuff */ 375 lprintf(("oldirq %x\n", sc->sc_irq)); 376 377 /* declare our interrupt handler */ 378 sc->intr_resource = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 379 RF_SHAREABLE); 380 if (sc->intr_resource) { 381 sc->sc_irq = LP_HAS_IRQ | LP_USE_IRQ | LP_ENABLE_IRQ; 382 device_printf(dev, "Interrupt-driven port\n"); 383 } else { 384 sc->sc_irq = 0; 385 device_printf(dev, "Polled port\n"); 386 } 387 lprintf(("irq %x\n", sc->sc_irq)); 388 389 lpt_release_ppbus(dev); 390 391 sc->dev = dev; 392 sc->cdev = make_dev(&lpt_cdevsw, unit, 393 UID_ROOT, GID_WHEEL, 0600, LPT_NAME "%d", unit); 394 sc->cdev->si_drv1 = sc; 395 sc->cdev->si_drv2 = 0; 396 sc->cdev_bypass = make_dev(&lpt_cdevsw, unit, 397 UID_ROOT, GID_WHEEL, 0600, LPT_NAME "%d.ctl", unit); 398 sc->cdev_bypass->si_drv1 = sc; 399 sc->cdev_bypass->si_drv2 = (void *)LP_BYPASS; 400 return (0); 401 } 402 403 static int 404 lpt_detach(device_t dev) 405 { 406 struct lpt_data *sc = DEVTOSOFTC(dev); 407 408 destroy_dev(sc->cdev); 409 destroy_dev(sc->cdev_bypass); 410 lpt_release_ppbus(dev); 411 if (sc->intr_resource != 0) { 412 BUS_TEARDOWN_INTR(device_get_parent(dev), dev, 413 sc->intr_resource, sc->intr_cookie); 414 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->intr_resource); 415 } 416 417 return (0); 418 } 419 420 static void 421 lptout(void *arg) 422 { 423 device_t dev = (device_t)arg; 424 struct lpt_data *sc = DEVTOSOFTC(dev); 425 #ifdef LPT_DEBUG 426 device_t ppbus = device_get_parent(dev); 427 #endif 428 429 lprintf(("T %x ", ppb_rstr(ppbus))); 430 if (sc->sc_state & OPEN) { 431 sc->sc_backoff++; 432 if (sc->sc_backoff > hz/LPTOUTMAX) 433 sc->sc_backoff = sc->sc_backoff > hz/LPTOUTMAX; 434 timeout(lptout, (caddr_t)dev, sc->sc_backoff); 435 } else 436 sc->sc_state &= ~TOUT; 437 438 if (sc->sc_state & EERROR) 439 sc->sc_state &= ~EERROR; 440 441 /* 442 * Avoid possible hangs due to missed interrupts 443 */ 444 if (sc->sc_xfercnt) { 445 lptintr(dev); 446 } else { 447 sc->sc_state &= ~OBUSY; 448 wakeup(dev); 449 } 450 } 451 452 /* 453 * lptopen -- reset the printer, then wait until it's selected and not busy. 454 * If LP_BYPASS flag is selected, then we do not try to select the 455 * printer -- this is just used for passing ioctls. 456 */ 457 458 static int 459 lptopen(struct cdev *dev, int flags, int fmt, struct thread *td) 460 { 461 int s; 462 int trys, err; 463 struct lpt_data *sc = dev->si_drv1; 464 device_t lptdev = sc->dev; 465 device_t ppbus = device_get_parent(lptdev); 466 467 if (!sc) 468 return (ENXIO); 469 470 if (sc->sc_state) { 471 lprintf((LPT_NAME ": still open %x\n", sc->sc_state)); 472 return(EBUSY); 473 } else 474 sc->sc_state |= LPTINIT; 475 476 sc->sc_flags = (uintptr_t)dev->si_drv2; 477 478 /* Check for open with BYPASS flag set. */ 479 if (sc->sc_flags & LP_BYPASS) { 480 sc->sc_state = OPEN; 481 return(0); 482 } 483 484 /* request the ppbus only if we don't have it already */ 485 if ((err = lpt_request_ppbus(lptdev, PPB_WAIT|PPB_INTR)) != 0) { 486 /* give it a chance to try later */ 487 sc->sc_state = 0; 488 return (err); 489 } 490 491 s = spltty(); 492 lprintf((LPT_NAME " flags 0x%x\n", sc->sc_flags)); 493 494 /* set IRQ status according to ENABLE_IRQ flag 495 */ 496 if (sc->sc_irq & LP_ENABLE_IRQ) 497 sc->sc_irq |= LP_USE_IRQ; 498 else 499 sc->sc_irq &= ~LP_USE_IRQ; 500 501 /* init printer */ 502 if ((sc->sc_flags & LP_NO_PRIME) == 0) { 503 if ((sc->sc_flags & LP_PRIMEOPEN) || sc->sc_primed == 0) { 504 ppb_wctr(ppbus, 0); 505 sc->sc_primed++; 506 DELAY(500); 507 } 508 } 509 510 ppb_wctr(ppbus, LPC_SEL|LPC_NINIT); 511 512 /* wait till ready (printer running diagnostics) */ 513 trys = 0; 514 do { 515 /* ran out of waiting for the printer */ 516 if (trys++ >= LPINITRDY*4) { 517 splx(s); 518 sc->sc_state = 0; 519 lprintf(("status %x\n", ppb_rstr(ppbus))); 520 521 lpt_release_ppbus(lptdev); 522 return (EBUSY); 523 } 524 525 /* wait 1/4 second, give up if we get a signal */ 526 if (tsleep(lptdev, LPPRI|PCATCH, "lptinit", hz/4) != 527 EWOULDBLOCK) { 528 sc->sc_state = 0; 529 splx(s); 530 531 lpt_release_ppbus(lptdev); 532 return (EBUSY); 533 } 534 535 /* is printer online and ready for output */ 536 } while ((ppb_rstr(ppbus) & 537 (LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR)) != 538 (LPS_SEL|LPS_NBSY|LPS_NERR)); 539 540 sc->sc_control = LPC_SEL|LPC_NINIT; 541 if (sc->sc_flags & LP_AUTOLF) 542 sc->sc_control |= LPC_AUTOL; 543 544 /* enable interrupt if interrupt-driven */ 545 if (sc->sc_irq & LP_USE_IRQ) 546 sc->sc_control |= LPC_ENA; 547 548 ppb_wctr(ppbus, sc->sc_control); 549 550 sc->sc_state = OPEN; 551 sc->sc_inbuf = malloc(BUFSIZE, M_DEVBUF, M_WAITOK); 552 sc->sc_statbuf = malloc(BUFSTATSIZE, M_DEVBUF, M_WAITOK); 553 sc->sc_xfercnt = 0; 554 splx(s); 555 556 /* release the ppbus */ 557 lpt_release_ppbus(lptdev); 558 559 /* only use timeout if using interrupt */ 560 lprintf(("irq %x\n", sc->sc_irq)); 561 if (sc->sc_irq & LP_USE_IRQ) { 562 sc->sc_state |= TOUT; 563 timeout(lptout, (caddr_t)lptdev, 564 (sc->sc_backoff = hz/LPTOUTINITIAL)); 565 } 566 567 lprintf(("opened.\n")); 568 return(0); 569 } 570 571 /* 572 * lptclose -- close the device, free the local line buffer. 573 * 574 * Check for interrupted write call added. 575 */ 576 577 static int 578 lptclose(struct cdev *dev, int flags, int fmt, struct thread *td) 579 { 580 struct lpt_data *sc = dev->si_drv1; 581 device_t lptdev = sc->dev; 582 device_t ppbus = device_get_parent(lptdev); 583 int err; 584 585 if (sc->sc_flags & LP_BYPASS) 586 goto end_close; 587 588 if ((err = lpt_request_ppbus(lptdev, PPB_WAIT|PPB_INTR)) != 0) 589 return (err); 590 591 sc->sc_state &= ~OPEN; 592 593 /* if the last write was interrupted, don't complete it */ 594 if ((!(sc->sc_state & INTERRUPTED)) && (sc->sc_irq & LP_USE_IRQ)) 595 while ((ppb_rstr(ppbus) & 596 (LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR)) != 597 (LPS_SEL|LPS_NBSY|LPS_NERR) || sc->sc_xfercnt) 598 /* wait 1/4 second, give up if we get a signal */ 599 if (tsleep(lptdev, LPPRI|PCATCH, 600 "lpclose", hz) != EWOULDBLOCK) 601 break; 602 603 ppb_wctr(ppbus, LPC_NINIT); 604 free(sc->sc_inbuf, M_DEVBUF); 605 free(sc->sc_statbuf, M_DEVBUF); 606 607 end_close: 608 /* release the bus anyway 609 * unregistration of interrupt forced by release 610 */ 611 lpt_release_ppbus(lptdev); 612 613 sc->sc_state = 0; 614 sc->sc_xfercnt = 0; 615 lprintf(("closed.\n")); 616 return(0); 617 } 618 619 /* 620 * lpt_pushbytes() 621 * Workhorse for actually spinning and writing bytes to printer 622 * Derived from lpa.c 623 * Originally by ? 624 * 625 * This code is only used when we are polling the port 626 */ 627 static int 628 lpt_pushbytes(device_t dev) 629 { 630 struct lpt_data *sc = DEVTOSOFTC(dev); 631 device_t ppbus = device_get_parent(dev); 632 int spin, err, tic; 633 char ch; 634 635 lprintf(("p")); 636 /* loop for every character .. */ 637 while (sc->sc_xfercnt > 0) { 638 /* printer data */ 639 ch = *(sc->sc_cp); 640 sc->sc_cp++; 641 sc->sc_xfercnt--; 642 643 /* 644 * Wait for printer ready. 645 * Loop 20 usecs testing BUSY bit, then sleep 646 * for exponentially increasing timeout. (vak) 647 */ 648 for (spin = 0; NOT_READY(ppbus) && spin < MAX_SPIN; ++spin) 649 DELAY(1); /* XXX delay is NOT this accurate! */ 650 if (spin >= MAX_SPIN) { 651 tic = 0; 652 while (NOT_READY(ppbus)) { 653 /* 654 * Now sleep, every cycle a 655 * little longer .. 656 */ 657 tic = tic + tic + 1; 658 /* 659 * But no more than 10 seconds. (vak) 660 */ 661 if (tic > MAX_SLEEP) 662 tic = MAX_SLEEP; 663 err = tsleep(dev, LPPRI, 664 LPT_NAME "poll", tic); 665 if (err != EWOULDBLOCK) { 666 return (err); 667 } 668 } 669 } 670 671 /* output data */ 672 ppb_wdtr(ppbus, ch); 673 /* strobe */ 674 ppb_wctr(ppbus, sc->sc_control|LPC_STB); 675 ppb_wctr(ppbus, sc->sc_control); 676 677 } 678 return(0); 679 } 680 681 /* 682 * lptread --retrieve printer status in IEEE1284 NIBBLE mode 683 */ 684 685 static int 686 lptread(struct cdev *dev, struct uio *uio, int ioflag) 687 { 688 struct lpt_data *sc = dev->si_drv1; 689 device_t lptdev = sc->dev; 690 device_t ppbus = device_get_parent(lptdev); 691 int error = 0, len; 692 693 if (sc->sc_flags & LP_BYPASS) { 694 /* we can't do reads in bypass mode */ 695 return (EPERM); 696 } 697 698 if ((error = ppb_1284_negociate(ppbus, PPB_NIBBLE, 0))) 699 return (error); 700 701 /* read data in an other buffer, read/write may be simultaneous */ 702 len = 0; 703 while (uio->uio_resid) { 704 if ((error = ppb_1284_read(ppbus, PPB_NIBBLE, 705 sc->sc_statbuf, min(BUFSTATSIZE, 706 uio->uio_resid), &len))) { 707 goto error; 708 } 709 710 if (!len) 711 goto error; /* no more data */ 712 713 if ((error = uiomove(sc->sc_statbuf, len, uio))) 714 goto error; 715 } 716 717 error: 718 ppb_1284_terminate(ppbus); 719 return (error); 720 } 721 722 /* 723 * lptwrite --copy a line from user space to a local buffer, then call 724 * putc to get the chars moved to the output queue. 725 * 726 * Flagging of interrupted write added. 727 */ 728 729 static int 730 lptwrite(struct cdev *dev, struct uio *uio, int ioflag) 731 { 732 register unsigned n; 733 int err; 734 struct lpt_data *sc = dev->si_drv1; 735 device_t lptdev = sc->dev; 736 device_t ppbus = device_get_parent(lptdev); 737 738 if (sc->sc_flags & LP_BYPASS) { 739 /* we can't do writes in bypass mode */ 740 return(EPERM); 741 } 742 743 /* request the ppbus only if we don't have it already */ 744 /* XXX interrupt registration?! */ 745 if ((err = lpt_request_ppbus(lptdev, PPB_WAIT|PPB_INTR)) != 0) 746 return (err); 747 748 /* if interrupts are working, register the handler */ 749 if (sc->sc_irq & LP_USE_IRQ) { 750 /* register our interrupt handler */ 751 err = bus_setup_intr(lptdev, sc->intr_resource, 752 INTR_TYPE_TTY, NULL, lpt_intr, lptdev, 753 &sc->intr_cookie); 754 if (err) { 755 device_printf(lptdev, "handler registration failed, polled mode.\n"); 756 sc->sc_irq &= ~LP_USE_IRQ; 757 } 758 } 759 760 sc->sc_state &= ~INTERRUPTED; 761 while ((n = min(BUFSIZE, uio->uio_resid)) != 0) { 762 sc->sc_cp = sc->sc_inbuf; 763 uiomove(sc->sc_cp, n, uio); 764 sc->sc_xfercnt = n ; 765 766 if (sc->sc_irq & LP_ENABLE_EXT) { 767 /* try any extended mode */ 768 err = ppb_write(ppbus, sc->sc_cp, 769 sc->sc_xfercnt, 0); 770 switch (err) { 771 case 0: 772 /* if not all data was sent, we could rely 773 * on polling for the last bytes */ 774 sc->sc_xfercnt = 0; 775 break; 776 case EINTR: 777 sc->sc_state |= INTERRUPTED; 778 return(err); 779 case EINVAL: 780 /* advanced mode not avail */ 781 log(LOG_NOTICE, 782 "%s: advanced mode not avail, polling\n", 783 device_get_nameunit(sc->dev)); 784 break; 785 default: 786 return(err); 787 } 788 } else while ((sc->sc_xfercnt > 0)&&(sc->sc_irq & LP_USE_IRQ)) { 789 lprintf(("i")); 790 /* if the printer is ready for a char, */ 791 /* give it one */ 792 if ((sc->sc_state & OBUSY) == 0){ 793 lprintf(("\nC %d. ", sc->sc_xfercnt)); 794 lptintr(lptdev); 795 } 796 lprintf(("W ")); 797 if (sc->sc_state & OBUSY) 798 if ((err = tsleep(lptdev, 799 LPPRI|PCATCH, LPT_NAME "write", 0))) { 800 sc->sc_state |= INTERRUPTED; 801 return(err); 802 } 803 } 804 805 /* check to see if we must do a polled write */ 806 if (!(sc->sc_irq & LP_USE_IRQ) && (sc->sc_xfercnt)) { 807 lprintf(("p")); 808 809 err = lpt_pushbytes(lptdev); 810 811 if (err) 812 return(err); 813 } 814 } 815 816 /* we have not been interrupted, release the ppbus */ 817 lpt_release_ppbus(lptdev); 818 819 return(0); 820 } 821 822 /* 823 * lpt_intr -- handle printer interrupts which occur when the printer is 824 * ready to accept another char. 825 * 826 * do checking for interrupted write call. 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) 864 return; 865 } 866 867 /* 868 * No more data waiting for printer. 869 * Wakeup is not done if write call was not interrupted. 870 */ 871 sc->sc_state &= ~OBUSY; 872 873 if (!(sc->sc_state & INTERRUPTED)) 874 wakeup(lptdev); 875 lprintf(("w ")); 876 return; 877 } else { /* check for error */ 878 if (((sts & (LPS_NERR | LPS_OUT) ) != LPS_NERR) && 879 (sc->sc_state & OPEN)) 880 sc->sc_state |= EERROR; 881 /* lptout() will jump in and try to restart. */ 882 } 883 lprintf(("sts %x ", sts)); 884 } 885 886 static void 887 lptintr(device_t dev) 888 { 889 /* call the interrupt at required spl level */ 890 int s = spltty(); 891 892 lpt_intr(dev); 893 894 splx(s); 895 return; 896 } 897 898 static int 899 lptioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags, struct thread *td) 900 { 901 int error = 0; 902 struct lpt_data *sc = dev->si_drv1; 903 u_char old_sc_irq; /* old printer IRQ status */ 904 905 switch (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*)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, "%s: switched to %s %s mode\n", 942 device_get_nameunit(sc->dev), 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 static device_method_t lpt_methods[] = { 958 /* device interface */ 959 DEVMETHOD(device_identify, lpt_identify), 960 DEVMETHOD(device_probe, lpt_probe), 961 DEVMETHOD(device_attach, lpt_attach), 962 DEVMETHOD(device_detach, lpt_detach), 963 964 { 0, 0 } 965 }; 966 967 static driver_t lpt_driver = { 968 LPT_NAME, 969 lpt_methods, 970 sizeof(struct lpt_data), 971 }; 972 973 DRIVER_MODULE(lpt, ppbus, lpt_driver, lpt_devclass, 0, 0); 974 MODULE_DEPEND(lpt, ppbus, 1, 1, 1); 975