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