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 #define LPTUNIT(s) ((s)&0x03) 108 #define LPTFLAGS(s) ((s)&0xfc) 109 110 struct lpt_data { 111 112 short sc_state; 113 /* default case: negative prime, negative ack, handshake strobe, 114 prime once */ 115 u_char sc_control; 116 char sc_flags; 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 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 cdevsw lpt_cdevsw = { 193 .d_version = D_VERSION, 194 .d_flags = D_NEEDGIANT, 195 .d_open = lptopen, 196 .d_close = lptclose, 197 .d_read = lptread, 198 .d_write = lptwrite, 199 .d_ioctl = lptioctl, 200 .d_name = LPT_NAME, 201 }; 202 203 static int 204 lpt_request_ppbus(device_t dev, int how) 205 { 206 device_t ppbus = device_get_parent(dev); 207 struct lpt_data *sc = DEVTOSOFTC(dev); 208 int error; 209 210 if (sc->sc_state & HAVEBUS) 211 return (0); 212 213 /* we have the bus only if the request succeded */ 214 if ((error = ppb_request_bus(ppbus, dev, how)) == 0) 215 sc->sc_state |= HAVEBUS; 216 217 return (error); 218 } 219 220 static int 221 lpt_release_ppbus(device_t dev) 222 { 223 device_t ppbus = device_get_parent(dev); 224 struct lpt_data *sc = DEVTOSOFTC(dev); 225 int error = 0; 226 227 if ((error = ppb_release_bus(ppbus, dev)) == 0) 228 sc->sc_state &= ~HAVEBUS; 229 230 return (error); 231 } 232 233 /* 234 * Internal routine to lptprobe to do port tests of one byte value 235 */ 236 static int 237 lpt_port_test(device_t ppbus, u_char data, u_char mask) 238 { 239 int temp, timeout; 240 241 data = data & mask; 242 ppb_wdtr(ppbus, data); 243 timeout = 10000; 244 do { 245 DELAY(10); 246 temp = ppb_rdtr(ppbus) & mask; 247 } 248 while (temp != data && --timeout); 249 lprintf(("out=%x\tin=%x\ttout=%d\n", data, temp, timeout)); 250 return (temp == data); 251 } 252 253 /* 254 * Probe simplified by replacing multiple loops with a hardcoded 255 * test pattern - 1999/02/08 des@freebsd.org 256 * 257 * New lpt port probe Geoff Rehmet - Rhodes University - 14/2/94 258 * Based partially on Rod Grimes' printer probe 259 * 260 * Logic: 261 * 1) If no port address was given, use the bios detected ports 262 * and autodetect what ports the printers are on. 263 * 2) Otherwise, probe the data port at the address given, 264 * using the method in Rod Grimes' port probe. 265 * (Much code ripped off directly from Rod's probe.) 266 * 267 * Comments from Rod's probe: 268 * Logic: 269 * 1) You should be able to write to and read back the same value 270 * to the data port. Do an alternating zeros, alternating ones, 271 * walking zero, and walking one test to check for stuck bits. 272 * 273 * 2) You should be able to write to and read back the same value 274 * to the control port lower 5 bits, the upper 3 bits are reserved 275 * per the IBM PC technical reference manauls and different boards 276 * do different things with them. Do an alternating zeros, alternating 277 * ones, walking zero, and walking one test to check for stuck bits. 278 * 279 * Some printers drag the strobe line down when the are powered off 280 * so this bit has been masked out of the control port test. 281 * 282 * XXX Some printers may not like a fast pulse on init or strobe, I 283 * don't know at this point, if that becomes a problem these bits 284 * should be turned off in the mask byte for the control port test. 285 * 286 * We are finally left with a mask of 0x14, due to some printers 287 * being adamant about holding other bits high ........ 288 * 289 * Before probing the control port, we write a 0 to the data port - 290 * If not, some printers chuck out garbage when the strobe line 291 * gets toggled. 292 * 293 * 3) Set the data and control ports to a value of 0 294 * 295 * This probe routine has been tested on Epson Lx-800, HP LJ3P, 296 * Epson FX-1170 and C.Itoh 8510RM 297 * printers. 298 * Quick exit on fail added. 299 */ 300 static int 301 lpt_detect(device_t dev) 302 { 303 device_t ppbus = device_get_parent(dev); 304 305 static u_char testbyte[18] = { 306 0x55, /* alternating zeros */ 307 0xaa, /* alternating ones */ 308 0xfe, 0xfd, 0xfb, 0xf7, 309 0xef, 0xdf, 0xbf, 0x7f, /* walking zero */ 310 0x01, 0x02, 0x04, 0x08, 311 0x10, 0x20, 0x40, 0x80 /* walking one */ 312 }; 313 int i, error, status; 314 315 status = 1; /* assume success */ 316 317 if ((error = lpt_request_ppbus(dev, PPB_DONTWAIT))) { 318 printf(LPT_NAME ": cannot alloc ppbus (%d)!\n", error); 319 status = 0; 320 goto end_probe; 321 } 322 323 for (i = 0; i < 18 && status; i++) 324 if (!lpt_port_test(ppbus, testbyte[i], 0xff)) { 325 status = 0; 326 goto end_probe; 327 } 328 329 end_probe: 330 /* write 0's to control and data ports */ 331 ppb_wdtr(ppbus, 0); 332 ppb_wctr(ppbus, 0); 333 334 lpt_release_ppbus(dev); 335 336 return (status); 337 } 338 339 static void 340 lpt_identify(driver_t *driver, device_t parent) 341 { 342 343 device_t dev; 344 345 dev = device_find_child(parent, LPT_NAME, 0); 346 if (!dev) 347 BUS_ADD_CHILD(parent, 0, LPT_NAME, -1); 348 } 349 350 /* 351 * lpt_probe() 352 */ 353 static int 354 lpt_probe(device_t dev) 355 { 356 struct lpt_data *sc; 357 358 sc = DEVTOSOFTC(dev); 359 360 /* 361 * Now, try to detect the printer. 362 */ 363 if (!lpt_detect(dev)) 364 return (ENXIO); 365 366 device_set_desc(dev, "Printer"); 367 368 return (0); 369 } 370 371 static int 372 lpt_attach(device_t dev) 373 { 374 device_t ppbus = device_get_parent(dev); 375 struct lpt_data *sc = DEVTOSOFTC(dev); 376 int zero = 0, unit = device_get_unit(dev); 377 int error; 378 intptr_t irq; 379 380 sc->sc_primed = 0; /* not primed yet */ 381 382 if ((error = lpt_request_ppbus(dev, PPB_DONTWAIT))) { 383 printf(LPT_NAME ": cannot alloc ppbus (%d)!\n", error); 384 return (0); 385 } 386 387 ppb_wctr(ppbus, LPC_NINIT); 388 389 /* check if we can use interrupt, should be done by ppc stuff */ 390 lprintf(("oldirq %x\n", sc->sc_irq)); 391 392 /* retrieve the ppbus irq */ 393 BUS_READ_IVAR(ppbus, dev, PPBUS_IVAR_IRQ, &irq); 394 395 if (irq > 0) { 396 /* declare our interrupt handler */ 397 sc->intr_resource = bus_alloc_resource(dev, SYS_RES_IRQ, 398 &zero, irq, irq, 1, RF_SHAREABLE); 399 } 400 if (sc->intr_resource) { 401 sc->sc_irq = LP_HAS_IRQ | LP_USE_IRQ | LP_ENABLE_IRQ; 402 device_printf(dev, "Interrupt-driven port\n"); 403 } else { 404 sc->sc_irq = 0; 405 device_printf(dev, "Polled port\n"); 406 } 407 lprintf(("irq %x %x\n", (int)irq, sc->sc_irq)); 408 409 lpt_release_ppbus(dev); 410 411 make_dev(&lpt_cdevsw, unit, 412 UID_ROOT, GID_WHEEL, 0600, LPT_NAME "%d", unit); 413 make_dev(&lpt_cdevsw, unit | LP_BYPASS, 414 UID_ROOT, GID_WHEEL, 0600, LPT_NAME "%d.ctl", unit); 415 return (0); 416 } 417 418 static void 419 lptout(void *arg) 420 { 421 device_t dev = (device_t)arg; 422 struct lpt_data *sc = DEVTOSOFTC(dev); 423 #ifdef LPT_DEBUG 424 device_t ppbus = device_get_parent(dev); 425 #endif 426 427 lprintf(("T %x ", ppb_rstr(ppbus))); 428 if (sc->sc_state & OPEN) { 429 sc->sc_backoff++; 430 if (sc->sc_backoff > hz/LPTOUTMAX) 431 sc->sc_backoff = sc->sc_backoff > hz/LPTOUTMAX; 432 timeout(lptout, (caddr_t)dev, sc->sc_backoff); 433 } else 434 sc->sc_state &= ~TOUT; 435 436 if (sc->sc_state & EERROR) 437 sc->sc_state &= ~EERROR; 438 439 /* 440 * Avoid possible hangs due to missed interrupts 441 */ 442 if (sc->sc_xfercnt) { 443 lptintr(dev); 444 } else { 445 sc->sc_state &= ~OBUSY; 446 wakeup(dev); 447 } 448 } 449 450 /* 451 * lptopen -- reset the printer, then wait until it's selected and not busy. 452 * If LP_BYPASS flag is selected, then we do not try to select the 453 * printer -- this is just used for passing ioctls. 454 */ 455 456 static int 457 lptopen(struct cdev *dev, int flags, int fmt, struct thread *td) 458 { 459 int s; 460 int trys, err; 461 u_int unit = LPTUNIT(minor(dev)); 462 struct lpt_data *sc = UNITOSOFTC(unit); 463 device_t lptdev = UNITODEVICE(unit); 464 device_t ppbus = device_get_parent(lptdev); 465 466 if (!sc) 467 return (ENXIO); 468 469 if (sc->sc_state) { 470 lprintf((LPT_NAME ": still open %x\n", sc->sc_state)); 471 return(EBUSY); 472 } else 473 sc->sc_state |= LPTINIT; 474 475 sc->sc_flags = LPTFLAGS(minor(dev)); 476 477 /* Check for open with BYPASS flag set. */ 478 if (sc->sc_flags & LP_BYPASS) { 479 sc->sc_state = OPEN; 480 return(0); 481 } 482 483 /* request the ppbus only if we don't have it already */ 484 if ((err = lpt_request_ppbus(lptdev, PPB_WAIT|PPB_INTR)) != 0) { 485 /* give it a chance to try later */ 486 sc->sc_state = 0; 487 return (err); 488 } 489 490 s = spltty(); 491 lprintf((LPT_NAME " flags 0x%x\n", sc->sc_flags)); 492 493 /* set IRQ status according to ENABLE_IRQ flag 494 */ 495 if (sc->sc_irq & LP_ENABLE_IRQ) 496 sc->sc_irq |= LP_USE_IRQ; 497 else 498 sc->sc_irq &= ~LP_USE_IRQ; 499 500 /* init printer */ 501 if ((sc->sc_flags & LP_NO_PRIME) == 0) { 502 if((sc->sc_flags & LP_PRIMEOPEN) || sc->sc_primed == 0) { 503 ppb_wctr(ppbus, 0); 504 sc->sc_primed++; 505 DELAY(500); 506 } 507 } 508 509 ppb_wctr(ppbus, LPC_SEL|LPC_NINIT); 510 511 /* wait till ready (printer running diagnostics) */ 512 trys = 0; 513 do { 514 /* ran out of waiting for the printer */ 515 if (trys++ >= LPINITRDY*4) { 516 splx(s); 517 sc->sc_state = 0; 518 lprintf(("status %x\n", ppb_rstr(ppbus))); 519 520 lpt_release_ppbus(lptdev); 521 return (EBUSY); 522 } 523 524 /* wait 1/4 second, give up if we get a signal */ 525 if (tsleep(lptdev, LPPRI|PCATCH, "lptinit", hz/4) != 526 EWOULDBLOCK) { 527 sc->sc_state = 0; 528 splx(s); 529 530 lpt_release_ppbus(lptdev); 531 return (EBUSY); 532 } 533 534 /* is printer online and ready for output */ 535 } while ((ppb_rstr(ppbus) & 536 (LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR)) != 537 (LPS_SEL|LPS_NBSY|LPS_NERR)); 538 539 sc->sc_control = LPC_SEL|LPC_NINIT; 540 if (sc->sc_flags & LP_AUTOLF) 541 sc->sc_control |= LPC_AUTOL; 542 543 /* enable interrupt if interrupt-driven */ 544 if (sc->sc_irq & LP_USE_IRQ) 545 sc->sc_control |= LPC_ENA; 546 547 ppb_wctr(ppbus, sc->sc_control); 548 549 sc->sc_state = OPEN; 550 sc->sc_inbuf = malloc(BUFSIZE, M_DEVBUF, M_WAITOK); 551 sc->sc_statbuf = malloc(BUFSTATSIZE, M_DEVBUF, M_WAITOK); 552 sc->sc_xfercnt = 0; 553 splx(s); 554 555 /* release the ppbus */ 556 lpt_release_ppbus(lptdev); 557 558 /* only use timeout if using interrupt */ 559 lprintf(("irq %x\n", sc->sc_irq)); 560 if (sc->sc_irq & LP_USE_IRQ) { 561 sc->sc_state |= TOUT; 562 timeout(lptout, (caddr_t)lptdev, 563 (sc->sc_backoff = hz/LPTOUTINITIAL)); 564 } 565 566 lprintf(("opened.\n")); 567 return(0); 568 } 569 570 /* 571 * lptclose -- close the device, free the local line buffer. 572 * 573 * Check for interrupted write call added. 574 */ 575 576 static int 577 lptclose(struct cdev *dev, int flags, int fmt, struct thread *td) 578 { 579 u_int unit = LPTUNIT(minor(dev)); 580 struct lpt_data *sc = UNITOSOFTC(unit); 581 device_t lptdev = UNITODEVICE(unit); 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 u_int unit = LPTUNIT(minor(dev)); 689 struct lpt_data *sc = UNITOSOFTC(unit); 690 device_t lptdev = UNITODEVICE(unit); 691 device_t ppbus = device_get_parent(lptdev); 692 int error = 0, len; 693 694 if (sc->sc_flags & LP_BYPASS) { 695 /* we can't do reads in bypass mode */ 696 return (EPERM); 697 } 698 699 if ((error = ppb_1284_negociate(ppbus, PPB_NIBBLE, 0))) 700 return (error); 701 702 /* read data in an other buffer, read/write may be simultaneous */ 703 len = 0; 704 while (uio->uio_resid) { 705 if ((error = ppb_1284_read(ppbus, PPB_NIBBLE, 706 sc->sc_statbuf, min(BUFSTATSIZE, 707 uio->uio_resid), &len))) { 708 goto error; 709 } 710 711 if (!len) 712 goto error; /* no more data */ 713 714 if ((error = uiomove(sc->sc_statbuf, len, uio))) 715 goto error; 716 } 717 718 error: 719 ppb_1284_terminate(ppbus); 720 return (error); 721 } 722 723 /* 724 * lptwrite --copy a line from user space to a local buffer, then call 725 * putc to get the chars moved to the output queue. 726 * 727 * Flagging of interrupted write added. 728 */ 729 730 static int 731 lptwrite(struct cdev *dev, struct uio *uio, int ioflag) 732 { 733 register unsigned n; 734 int err; 735 u_int unit = LPTUNIT(minor(dev)); 736 struct lpt_data *sc = UNITOSOFTC(unit); 737 device_t lptdev = UNITODEVICE(unit); 738 device_t ppbus = device_get_parent(lptdev); 739 740 if(sc->sc_flags & LP_BYPASS) { 741 /* we can't do writes in bypass mode */ 742 return(EPERM); 743 } 744 745 /* request the ppbus only if we don't have it already */ 746 /* XXX interrupt registration?! */ 747 if ((err = lpt_request_ppbus(lptdev, PPB_WAIT|PPB_INTR)) != 0) 748 return (err); 749 750 /* if interrupts are working, register the handler */ 751 if (sc->sc_irq & LP_USE_IRQ) { 752 /* register our interrupt handler */ 753 err = BUS_SETUP_INTR(ppbus, lptdev, sc->intr_resource, 754 INTR_TYPE_TTY, lpt_intr, lptdev, 755 &sc->intr_cookie); 756 if (err) { 757 device_printf(lptdev, "handler registration failed, polled mode.\n"); 758 sc->sc_irq &= ~LP_USE_IRQ; 759 } 760 } 761 762 sc->sc_state &= ~INTERRUPTED; 763 while ((n = min(BUFSIZE, uio->uio_resid)) != 0) { 764 sc->sc_cp = sc->sc_inbuf; 765 uiomove(sc->sc_cp, n, uio); 766 sc->sc_xfercnt = n ; 767 768 if (sc->sc_irq & LP_ENABLE_EXT) { 769 /* try any extended mode */ 770 err = ppb_write(ppbus, sc->sc_cp, 771 sc->sc_xfercnt, 0); 772 switch (err) { 773 case 0: 774 /* if not all data was sent, we could rely 775 * on polling for the last bytes */ 776 sc->sc_xfercnt = 0; 777 break; 778 case EINTR: 779 sc->sc_state |= INTERRUPTED; 780 return(err); 781 case EINVAL: 782 /* advanced mode not avail */ 783 log(LOG_NOTICE, LPT_NAME "%d: advanced mode not avail, polling\n", unit); 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 829 static void 830 lpt_intr(void *arg) 831 { 832 device_t lptdev = (device_t)arg; 833 device_t ppbus = device_get_parent(lptdev); 834 struct lpt_data *sc = DEVTOSOFTC(lptdev); 835 int sts = 0; 836 int i; 837 838 /* we must own the bus to use it */ 839 if ((sc->sc_state & HAVEBUS) == 0) 840 return; 841 842 /* 843 * Is printer online and ready for output? 844 * 845 * Avoid falling back to lptout() too quickly. First spin-loop 846 * to see if the printer will become ready ``really soon now''. 847 */ 848 for (i = 0; i < 100 && 849 ((sts=ppb_rstr(ppbus)) & RDY_MASK) != LP_READY; i++) ; 850 851 if ((sts & RDY_MASK) == LP_READY) { 852 sc->sc_state = (sc->sc_state | OBUSY) & ~EERROR; 853 sc->sc_backoff = hz/LPTOUTINITIAL; 854 855 if (sc->sc_xfercnt) { 856 /* send char */ 857 /*lprintf(("%x ", *sc->sc_cp)); */ 858 ppb_wdtr(ppbus, *sc->sc_cp++) ; 859 ppb_wctr(ppbus, sc->sc_control|LPC_STB); 860 /* DELAY(X) */ 861 ppb_wctr(ppbus, sc->sc_control); 862 863 /* any more data for printer */ 864 if(--(sc->sc_xfercnt) > 0) 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 u_int unit = LPTUNIT(minor(dev)); 903 struct lpt_data *sc = UNITOSOFTC(unit); 904 u_char old_sc_irq; /* old printer IRQ status */ 905 906 switch (cmd) { 907 case LPT_IRQ : 908 if(sc->sc_irq & LP_HAS_IRQ) { 909 /* 910 * NOTE: 911 * If the IRQ status is changed, 912 * this will only be visible on the 913 * next open. 914 * 915 * If interrupt status changes, 916 * this gets syslog'd. 917 */ 918 old_sc_irq = sc->sc_irq; 919 switch(*(int*)data) { 920 case 0: 921 sc->sc_irq &= (~LP_ENABLE_IRQ); 922 break; 923 case 1: 924 sc->sc_irq &= (~LP_ENABLE_EXT); 925 sc->sc_irq |= LP_ENABLE_IRQ; 926 break; 927 case 2: 928 /* classic irq based transfer and advanced 929 * modes are in conflict 930 */ 931 sc->sc_irq &= (~LP_ENABLE_IRQ); 932 sc->sc_irq |= LP_ENABLE_EXT; 933 break; 934 case 3: 935 sc->sc_irq &= (~LP_ENABLE_EXT); 936 break; 937 default: 938 break; 939 } 940 941 if (old_sc_irq != sc->sc_irq ) 942 log(LOG_NOTICE, LPT_NAME "%d: switched to %s %s mode\n", 943 unit, 944 (sc->sc_irq & LP_ENABLE_IRQ)? 945 "interrupt-driven":"polled", 946 (sc->sc_irq & LP_ENABLE_EXT)? 947 "extended":"standard"); 948 } else /* polled port */ 949 error = EOPNOTSUPP; 950 break; 951 default: 952 error = ENODEV; 953 } 954 955 return(error); 956 } 957 958 static device_method_t lpt_methods[] = { 959 /* device interface */ 960 DEVMETHOD(device_identify, lpt_identify), 961 DEVMETHOD(device_probe, lpt_probe), 962 DEVMETHOD(device_attach, lpt_attach), 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