1098ca2bdSWarner Losh /*- 2df57947fSPedro F. Giffuni * SPDX-License-Identifier: BSD-4-Clause 3df57947fSPedro F. Giffuni * 4fdf94d1aSNicolas Souchu * Copyright (c) 1990 William F. Jolitz, TeleMuse 5fdf94d1aSNicolas Souchu * All rights reserved. 6fdf94d1aSNicolas Souchu * 7fdf94d1aSNicolas Souchu * Redistribution and use in source and binary forms, with or without 8fdf94d1aSNicolas Souchu * modification, are permitted provided that the following conditions 9fdf94d1aSNicolas Souchu * are met: 10fdf94d1aSNicolas Souchu * 1. Redistributions of source code must retain the above copyright 11fdf94d1aSNicolas Souchu * notice, this list of conditions and the following disclaimer. 12fdf94d1aSNicolas Souchu * 2. Redistributions in binary form must reproduce the above copyright 13fdf94d1aSNicolas Souchu * notice, this list of conditions and the following disclaimer in the 14fdf94d1aSNicolas Souchu * documentation and/or other materials provided with the distribution. 15fdf94d1aSNicolas Souchu * 3. All advertising materials mentioning features or use of this software 16fdf94d1aSNicolas Souchu * must display the following acknowledgement: 17fdf94d1aSNicolas Souchu * This software is a component of "386BSD" developed by 18fdf94d1aSNicolas Souchu * William F. Jolitz, TeleMuse. 19fdf94d1aSNicolas Souchu * 4. Neither the name of the developer nor the name "386BSD" 20fdf94d1aSNicolas Souchu * may be used to endorse or promote products derived from this software 21fdf94d1aSNicolas Souchu * without specific prior written permission. 22fdf94d1aSNicolas Souchu * 23fdf94d1aSNicolas Souchu * THIS SOFTWARE IS A COMPONENT OF 386BSD DEVELOPED BY WILLIAM F. JOLITZ 24fdf94d1aSNicolas Souchu * AND IS INTENDED FOR RESEARCH AND EDUCATIONAL PURPOSES ONLY. THIS 25fdf94d1aSNicolas Souchu * SOFTWARE SHOULD NOT BE CONSIDERED TO BE A COMMERCIAL PRODUCT. 26fdf94d1aSNicolas Souchu * THE DEVELOPER URGES THAT USERS WHO REQUIRE A COMMERCIAL PRODUCT 27fdf94d1aSNicolas Souchu * NOT MAKE USE OF THIS WORK. 28fdf94d1aSNicolas Souchu * 29fdf94d1aSNicolas Souchu * FOR USERS WHO WISH TO UNDERSTAND THE 386BSD SYSTEM DEVELOPED 30fdf94d1aSNicolas Souchu * BY WILLIAM F. JOLITZ, WE RECOMMEND THE USER STUDY WRITTEN 31fdf94d1aSNicolas Souchu * REFERENCES SUCH AS THE "PORTING UNIX TO THE 386" SERIES 32fdf94d1aSNicolas Souchu * (BEGINNING JANUARY 1991 "DR. DOBBS JOURNAL", USA AND BEGINNING 33fdf94d1aSNicolas Souchu * JUNE 1991 "UNIX MAGAZIN", GERMANY) BY WILLIAM F. JOLITZ AND 34fdf94d1aSNicolas Souchu * LYNNE GREER JOLITZ, AS WELL AS OTHER BOOKS ON UNIX AND THE 35fdf94d1aSNicolas Souchu * ON-LINE 386BSD USER MANUAL BEFORE USE. A BOOK DISCUSSING THE INTERNALS 36fdf94d1aSNicolas Souchu * OF 386BSD ENTITLED "386BSD FROM THE INSIDE OUT" WILL BE AVAILABLE LATE 1992. 37fdf94d1aSNicolas Souchu * 38fdf94d1aSNicolas Souchu * THIS SOFTWARE IS PROVIDED BY THE DEVELOPER ``AS IS'' AND 39fdf94d1aSNicolas Souchu * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 40fdf94d1aSNicolas Souchu * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 41fdf94d1aSNicolas Souchu * ARE DISCLAIMED. IN NO EVENT SHALL THE DEVELOPER BE LIABLE 42fdf94d1aSNicolas Souchu * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 43fdf94d1aSNicolas Souchu * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 44fdf94d1aSNicolas Souchu * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 45fdf94d1aSNicolas Souchu * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 46fdf94d1aSNicolas Souchu * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 47fdf94d1aSNicolas Souchu * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 48fdf94d1aSNicolas Souchu * SUCH DAMAGE. 49fdf94d1aSNicolas Souchu * 50fdf94d1aSNicolas Souchu * from: unknown origin, 386BSD 0.1 51fdf94d1aSNicolas Souchu * From Id: lpt.c,v 1.55.2.1 1996/11/12 09:08:38 phk Exp 52fdf94d1aSNicolas Souchu * From Id: nlpt.c,v 1.14 1999/02/08 13:55:43 des Exp 53fdf94d1aSNicolas Souchu */ 54fdf94d1aSNicolas Souchu 55aad970f1SDavid E. O'Brien #include <sys/cdefs.h> 56aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$"); 57aad970f1SDavid E. O'Brien 58fdf94d1aSNicolas Souchu /* 59fdf94d1aSNicolas Souchu * Device Driver for AT parallel printer port 60fdf94d1aSNicolas Souchu * Written by William Jolitz 12/18/90 61fdf94d1aSNicolas Souchu */ 62fdf94d1aSNicolas Souchu 63fdf94d1aSNicolas Souchu /* 64fdf94d1aSNicolas Souchu * Updated for ppbus by Nicolas Souchu 65fdf94d1aSNicolas Souchu * [Mon Jul 28 1997] 66fdf94d1aSNicolas Souchu */ 67fdf94d1aSNicolas Souchu 680f210c92SNicolas Souchu #include "opt_lpt.h" 690f210c92SNicolas Souchu 70fdf94d1aSNicolas Souchu #include <sys/param.h> 71fdf94d1aSNicolas Souchu #include <sys/systm.h> 720f210c92SNicolas Souchu #include <sys/module.h> 730f210c92SNicolas Souchu #include <sys/bus.h> 74fdf94d1aSNicolas Souchu #include <sys/conf.h> 75fdf94d1aSNicolas Souchu #include <sys/kernel.h> 76fdf94d1aSNicolas Souchu #include <sys/uio.h> 77fdf94d1aSNicolas Souchu #include <sys/syslog.h> 785856c958SPoul-Henning Kamp #include <sys/malloc.h> 79fdf94d1aSNicolas Souchu 800f210c92SNicolas Souchu #include <machine/bus.h> 810f210c92SNicolas Souchu #include <machine/resource.h> 820f210c92SNicolas Souchu #include <sys/rman.h> 830f210c92SNicolas Souchu 8474cf6eb6SDavid E. O'Brien #include <dev/ppbus/lptio.h> 85fdf94d1aSNicolas Souchu #include <dev/ppbus/ppbconf.h> 86fdf94d1aSNicolas Souchu #include <dev/ppbus/ppb_1284.h> 87fdf94d1aSNicolas Souchu #include <dev/ppbus/lpt.h> 880f210c92SNicolas Souchu #include "ppbus_if.h" 890f210c92SNicolas Souchu #include <dev/ppbus/ppbio.h> 90fdf94d1aSNicolas Souchu 91fdf94d1aSNicolas Souchu #ifndef LPT_DEBUG 92fdf94d1aSNicolas Souchu #define lprintf(args) 93fdf94d1aSNicolas Souchu #else 94fdf94d1aSNicolas Souchu #define lprintf(args) \ 95fdf94d1aSNicolas Souchu do { \ 96fdf94d1aSNicolas Souchu if (lptflag) \ 97fdf94d1aSNicolas Souchu printf args; \ 98fdf94d1aSNicolas Souchu } while (0) 99fdf94d1aSNicolas Souchu static int volatile lptflag = 1; 100fdf94d1aSNicolas Souchu #endif 101fdf94d1aSNicolas Souchu 102fdf94d1aSNicolas Souchu #define LPINITRDY 4 /* wait up to 4 seconds for a ready */ 103fdf94d1aSNicolas Souchu #define LPTOUTINITIAL 10 /* initial timeout to wait for ready 1/10 s */ 104fdf94d1aSNicolas Souchu #define LPTOUTMAX 1 /* maximal timeout 1 s */ 105fdf94d1aSNicolas Souchu #define LPPRI (PZERO+8) 106fdf94d1aSNicolas Souchu #define BUFSIZE 1024 107fdf94d1aSNicolas Souchu #define BUFSTATSIZE 32 108fdf94d1aSNicolas Souchu 10917b5923dSNicolas Souchu struct lpt_data { 1102067d312SJohn Baldwin device_t sc_dev; 1112067d312SJohn Baldwin struct cdev *sc_cdev; 1122067d312SJohn Baldwin struct cdev *sc_cdev_bypass; 11317b5923dSNicolas Souchu short sc_state; 11417b5923dSNicolas Souchu /* default case: negative prime, negative ack, handshake strobe, 11517b5923dSNicolas Souchu prime once */ 11617b5923dSNicolas Souchu u_char sc_control; 11717b5923dSNicolas Souchu char sc_flags; 118453130d9SPedro F. Giffuni #define LP_POS_INIT 0x04 /* if we are a positive init signal */ 11917b5923dSNicolas Souchu #define LP_POS_ACK 0x08 /* if we are a positive going ack */ 12017b5923dSNicolas Souchu #define LP_NO_PRIME 0x10 /* don't prime the printer at all */ 12117b5923dSNicolas Souchu #define LP_PRIMEOPEN 0x20 /* prime on every open */ 12217b5923dSNicolas Souchu #define LP_AUTOLF 0x40 /* tell printer to do an automatic lf */ 12317b5923dSNicolas Souchu #define LP_BYPASS 0x80 /* bypass printer ready checks */ 1245856c958SPoul-Henning Kamp void *sc_inbuf; 1255856c958SPoul-Henning Kamp void *sc_statbuf; 12617b5923dSNicolas Souchu short sc_xfercnt ; 12717b5923dSNicolas Souchu char sc_primed; 12817b5923dSNicolas Souchu char *sc_cp ; 12917b5923dSNicolas Souchu u_short sc_irq ; /* IRQ status of port */ 13017b5923dSNicolas Souchu #define LP_HAS_IRQ 0x01 /* we have an irq available */ 13117b5923dSNicolas Souchu #define LP_USE_IRQ 0x02 /* we are using our irq */ 13217b5923dSNicolas Souchu #define LP_ENABLE_IRQ 0x04 /* enable IRQ on open */ 13317b5923dSNicolas Souchu #define LP_ENABLE_EXT 0x10 /* we shall use advanced mode when possible */ 13417b5923dSNicolas Souchu u_char sc_backoff ; /* time to call lptout() again */ 1352067d312SJohn Baldwin struct callout sc_timer; 13617b5923dSNicolas Souchu 1372067d312SJohn Baldwin struct resource *sc_intr_resource; /* interrupt resource */ 1382067d312SJohn Baldwin void *sc_intr_cookie; /* interrupt cookie */ 1390f210c92SNicolas Souchu }; 140fdf94d1aSNicolas Souchu 141fdf94d1aSNicolas Souchu #define LPT_NAME "lpt" /* our official name */ 142fdf94d1aSNicolas Souchu 1435773ac11SJohn Baldwin static callout_func_t lptout; 1440f210c92SNicolas Souchu static int lpt_port_test(device_t dev, u_char data, u_char mask); 1450f210c92SNicolas Souchu static int lpt_detect(device_t dev); 146fdf94d1aSNicolas Souchu 1470f210c92SNicolas Souchu #define DEVTOSOFTC(dev) \ 1480f210c92SNicolas Souchu ((struct lpt_data *)device_get_softc(dev)) 149fdf94d1aSNicolas Souchu 1502067d312SJohn Baldwin static void lptintr(void *arg); 151fdf94d1aSNicolas Souchu 1520f210c92SNicolas Souchu static devclass_t lpt_devclass; 153fdf94d1aSNicolas Souchu 154fdf94d1aSNicolas Souchu /* bits for state */ 155fdf94d1aSNicolas Souchu #define OPEN (1<<0) /* device is open */ 156fdf94d1aSNicolas Souchu #define ASLP (1<<1) /* awaiting draining of printer */ 157fdf94d1aSNicolas Souchu #define EERROR (1<<2) /* error was received from printer */ 158fdf94d1aSNicolas Souchu #define OBUSY (1<<3) /* printer is busy doing output */ 159fdf94d1aSNicolas Souchu #define LPTOUT (1<<4) /* timeout while not selected */ 160fdf94d1aSNicolas Souchu #define TOUT (1<<5) /* timeout while not selected */ 161fdf94d1aSNicolas Souchu #define LPTINIT (1<<6) /* waiting to initialize for open */ 162fdf94d1aSNicolas Souchu #define INTERRUPTED (1<<7) /* write call was interrupted */ 163fdf94d1aSNicolas Souchu #define HAVEBUS (1<<8) /* the driver owns the bus */ 164fdf94d1aSNicolas Souchu 165fdf94d1aSNicolas Souchu /* status masks to interrogate printer status */ 166fdf94d1aSNicolas Souchu #define RDY_MASK (LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR) /* ready ? */ 167fdf94d1aSNicolas Souchu #define LP_READY (LPS_SEL|LPS_NBSY|LPS_NERR) 168fdf94d1aSNicolas Souchu 169fdf94d1aSNicolas Souchu /* Printer Ready condition - from lpa.c */ 170fdf94d1aSNicolas Souchu /* Only used in polling code */ 171fdf94d1aSNicolas Souchu #define LPS_INVERT (LPS_NBSY | LPS_NACK | LPS_SEL | LPS_NERR) 172fdf94d1aSNicolas Souchu #define LPS_MASK (LPS_NBSY | LPS_NACK | LPS_OUT | LPS_SEL | LPS_NERR) 1730f210c92SNicolas Souchu #define NOT_READY(ppbus) ((ppb_rstr(ppbus)^LPS_INVERT)&LPS_MASK) 174fdf94d1aSNicolas Souchu 175fdf94d1aSNicolas Souchu #define MAX_SLEEP (hz*5) /* Timeout while waiting for device ready */ 176fdf94d1aSNicolas Souchu #define MAX_SPIN 20 /* Max delay for device ready in usecs */ 177fdf94d1aSNicolas Souchu 178fdf94d1aSNicolas Souchu static d_open_t lptopen; 179fdf94d1aSNicolas Souchu static d_close_t lptclose; 180fdf94d1aSNicolas Souchu static d_write_t lptwrite; 181fdf94d1aSNicolas Souchu static d_read_t lptread; 182fdf94d1aSNicolas Souchu static d_ioctl_t lptioctl; 183fdf94d1aSNicolas Souchu 1844e2f199eSPoul-Henning Kamp static struct cdevsw lpt_cdevsw = { 185dc08ffecSPoul-Henning Kamp .d_version = D_VERSION, 1867ac40f5fSPoul-Henning Kamp .d_open = lptopen, 1877ac40f5fSPoul-Henning Kamp .d_close = lptclose, 1887ac40f5fSPoul-Henning Kamp .d_read = lptread, 1897ac40f5fSPoul-Henning Kamp .d_write = lptwrite, 1907ac40f5fSPoul-Henning Kamp .d_ioctl = lptioctl, 1917ac40f5fSPoul-Henning Kamp .d_name = LPT_NAME, 1924e2f199eSPoul-Henning Kamp }; 193fdf94d1aSNicolas Souchu 194fdf94d1aSNicolas Souchu static int 1950f210c92SNicolas Souchu lpt_request_ppbus(device_t dev, int how) 196fdf94d1aSNicolas Souchu { 1970f210c92SNicolas Souchu device_t ppbus = device_get_parent(dev); 1980f210c92SNicolas Souchu struct lpt_data *sc = DEVTOSOFTC(dev); 199fdf94d1aSNicolas Souchu int error; 200fdf94d1aSNicolas Souchu 2012067d312SJohn Baldwin /* 2022067d312SJohn Baldwin * We might already have the bus for a write(2) after an interrupted 2032067d312SJohn Baldwin * write(2) call. 2042067d312SJohn Baldwin */ 2052067d312SJohn Baldwin ppb_assert_locked(ppbus); 206fdf94d1aSNicolas Souchu if (sc->sc_state & HAVEBUS) 207fdf94d1aSNicolas Souchu return (0); 208fdf94d1aSNicolas Souchu 2092067d312SJohn Baldwin error = ppb_request_bus(ppbus, dev, how); 2102067d312SJohn Baldwin if (error == 0) 211fdf94d1aSNicolas Souchu sc->sc_state |= HAVEBUS; 212fdf94d1aSNicolas Souchu return (error); 213fdf94d1aSNicolas Souchu } 214fdf94d1aSNicolas Souchu 215fdf94d1aSNicolas Souchu static int 2160f210c92SNicolas Souchu lpt_release_ppbus(device_t dev) 217fdf94d1aSNicolas Souchu { 2180f210c92SNicolas Souchu device_t ppbus = device_get_parent(dev); 2190f210c92SNicolas Souchu struct lpt_data *sc = DEVTOSOFTC(dev); 2200f210c92SNicolas Souchu int error = 0; 2210f210c92SNicolas Souchu 2222067d312SJohn Baldwin ppb_assert_locked(ppbus); 2232067d312SJohn Baldwin if (sc->sc_state & HAVEBUS) { 2242067d312SJohn Baldwin error = ppb_release_bus(ppbus, dev); 2252067d312SJohn Baldwin if (error == 0) 226fdf94d1aSNicolas Souchu sc->sc_state &= ~HAVEBUS; 2272067d312SJohn Baldwin } 2280f210c92SNicolas Souchu return (error); 229fdf94d1aSNicolas Souchu } 230fdf94d1aSNicolas Souchu 231fdf94d1aSNicolas Souchu /* 232fdf94d1aSNicolas Souchu * Internal routine to lptprobe to do port tests of one byte value 233fdf94d1aSNicolas Souchu */ 234fdf94d1aSNicolas Souchu static int 2350f210c92SNicolas Souchu lpt_port_test(device_t ppbus, u_char data, u_char mask) 236fdf94d1aSNicolas Souchu { 237fdf94d1aSNicolas Souchu int temp, timeout; 238fdf94d1aSNicolas Souchu 239fdf94d1aSNicolas Souchu data = data & mask; 2400f210c92SNicolas Souchu ppb_wdtr(ppbus, data); 241fdf94d1aSNicolas Souchu timeout = 10000; 242fdf94d1aSNicolas Souchu do { 243fdf94d1aSNicolas Souchu DELAY(10); 2440f210c92SNicolas Souchu temp = ppb_rdtr(ppbus) & mask; 245fdf94d1aSNicolas Souchu } 246fdf94d1aSNicolas Souchu while (temp != data && --timeout); 247fdf94d1aSNicolas Souchu lprintf(("out=%x\tin=%x\ttout=%d\n", data, temp, timeout)); 248fdf94d1aSNicolas Souchu return (temp == data); 249fdf94d1aSNicolas Souchu } 250fdf94d1aSNicolas Souchu 251fdf94d1aSNicolas Souchu /* 252fdf94d1aSNicolas Souchu * Probe simplified by replacing multiple loops with a hardcoded 253fdf94d1aSNicolas Souchu * test pattern - 1999/02/08 des@freebsd.org 254fdf94d1aSNicolas Souchu * 255fdf94d1aSNicolas Souchu * New lpt port probe Geoff Rehmet - Rhodes University - 14/2/94 256fdf94d1aSNicolas Souchu * Based partially on Rod Grimes' printer probe 257fdf94d1aSNicolas Souchu * 258fdf94d1aSNicolas Souchu * Logic: 259fdf94d1aSNicolas Souchu * 1) If no port address was given, use the bios detected ports 260fdf94d1aSNicolas Souchu * and autodetect what ports the printers are on. 261fdf94d1aSNicolas Souchu * 2) Otherwise, probe the data port at the address given, 262fdf94d1aSNicolas Souchu * using the method in Rod Grimes' port probe. 263fdf94d1aSNicolas Souchu * (Much code ripped off directly from Rod's probe.) 264fdf94d1aSNicolas Souchu * 265fdf94d1aSNicolas Souchu * Comments from Rod's probe: 266fdf94d1aSNicolas Souchu * Logic: 267fdf94d1aSNicolas Souchu * 1) You should be able to write to and read back the same value 268fdf94d1aSNicolas Souchu * to the data port. Do an alternating zeros, alternating ones, 269fdf94d1aSNicolas Souchu * walking zero, and walking one test to check for stuck bits. 270fdf94d1aSNicolas Souchu * 271fdf94d1aSNicolas Souchu * 2) You should be able to write to and read back the same value 272fdf94d1aSNicolas Souchu * to the control port lower 5 bits, the upper 3 bits are reserved 2735aa0576bSEd Maste * per the IBM PC technical reference manuals and different boards 274fdf94d1aSNicolas Souchu * do different things with them. Do an alternating zeros, alternating 275fdf94d1aSNicolas Souchu * ones, walking zero, and walking one test to check for stuck bits. 276fdf94d1aSNicolas Souchu * 277fdf94d1aSNicolas Souchu * Some printers drag the strobe line down when the are powered off 278fdf94d1aSNicolas Souchu * so this bit has been masked out of the control port test. 279fdf94d1aSNicolas Souchu * 280fdf94d1aSNicolas Souchu * XXX Some printers may not like a fast pulse on init or strobe, I 281fdf94d1aSNicolas Souchu * don't know at this point, if that becomes a problem these bits 282fdf94d1aSNicolas Souchu * should be turned off in the mask byte for the control port test. 283fdf94d1aSNicolas Souchu * 284fdf94d1aSNicolas Souchu * We are finally left with a mask of 0x14, due to some printers 285fdf94d1aSNicolas Souchu * being adamant about holding other bits high ........ 286fdf94d1aSNicolas Souchu * 287fdf94d1aSNicolas Souchu * Before probing the control port, we write a 0 to the data port - 288fdf94d1aSNicolas Souchu * If not, some printers chuck out garbage when the strobe line 289fdf94d1aSNicolas Souchu * gets toggled. 290fdf94d1aSNicolas Souchu * 291fdf94d1aSNicolas Souchu * 3) Set the data and control ports to a value of 0 292fdf94d1aSNicolas Souchu * 293fdf94d1aSNicolas Souchu * This probe routine has been tested on Epson Lx-800, HP LJ3P, 294fdf94d1aSNicolas Souchu * Epson FX-1170 and C.Itoh 8510RM 295fdf94d1aSNicolas Souchu * printers. 296fdf94d1aSNicolas Souchu * Quick exit on fail added. 297fdf94d1aSNicolas Souchu */ 298fdf94d1aSNicolas Souchu static int 2990f210c92SNicolas Souchu lpt_detect(device_t dev) 300fdf94d1aSNicolas Souchu { 3010f210c92SNicolas Souchu device_t ppbus = device_get_parent(dev); 3020f210c92SNicolas Souchu 303fdf94d1aSNicolas Souchu static u_char testbyte[18] = { 304fdf94d1aSNicolas Souchu 0x55, /* alternating zeros */ 305fdf94d1aSNicolas Souchu 0xaa, /* alternating ones */ 306fdf94d1aSNicolas Souchu 0xfe, 0xfd, 0xfb, 0xf7, 307fdf94d1aSNicolas Souchu 0xef, 0xdf, 0xbf, 0x7f, /* walking zero */ 308fdf94d1aSNicolas Souchu 0x01, 0x02, 0x04, 0x08, 309fdf94d1aSNicolas Souchu 0x10, 0x20, 0x40, 0x80 /* walking one */ 310fdf94d1aSNicolas Souchu }; 311fdf94d1aSNicolas Souchu int i, error, status; 312fdf94d1aSNicolas Souchu 313fdf94d1aSNicolas Souchu status = 1; /* assume success */ 314fdf94d1aSNicolas Souchu 3152067d312SJohn Baldwin ppb_lock(ppbus); 3160f210c92SNicolas Souchu if ((error = lpt_request_ppbus(dev, PPB_DONTWAIT))) { 3172067d312SJohn Baldwin ppb_unlock(ppbus); 3182067d312SJohn Baldwin device_printf(dev, "cannot alloc ppbus (%d)!\n", error); 3192067d312SJohn Baldwin return (0); 320fdf94d1aSNicolas Souchu } 321fdf94d1aSNicolas Souchu 322fdf94d1aSNicolas Souchu for (i = 0; i < 18 && status; i++) 3230f210c92SNicolas Souchu if (!lpt_port_test(ppbus, testbyte[i], 0xff)) { 324fdf94d1aSNicolas Souchu status = 0; 3252067d312SJohn Baldwin break; 326fdf94d1aSNicolas Souchu } 327fdf94d1aSNicolas Souchu 328fdf94d1aSNicolas Souchu /* write 0's to control and data ports */ 3290f210c92SNicolas Souchu ppb_wdtr(ppbus, 0); 3300f210c92SNicolas Souchu ppb_wctr(ppbus, 0); 331fdf94d1aSNicolas Souchu 3320f210c92SNicolas Souchu lpt_release_ppbus(dev); 3332067d312SJohn Baldwin ppb_unlock(ppbus); 334fdf94d1aSNicolas Souchu 335fdf94d1aSNicolas Souchu return (status); 336fdf94d1aSNicolas Souchu } 337fdf94d1aSNicolas Souchu 3380f063508SPeter Wemm static void 3390f063508SPeter Wemm lpt_identify(driver_t *driver, device_t parent) 3400f063508SPeter Wemm { 3410f063508SPeter Wemm 342a5c7e3bbSGuido van Rooij device_t dev; 343a5c7e3bbSGuido van Rooij 34457fb5e60SJohn Baldwin dev = device_find_child(parent, LPT_NAME, -1); 345a5c7e3bbSGuido van Rooij if (!dev) 346338cad62SBernd Walter BUS_ADD_CHILD(parent, 0, LPT_NAME, -1); 3470f063508SPeter Wemm } 3480f063508SPeter Wemm 349fdf94d1aSNicolas Souchu /* 3500f210c92SNicolas Souchu * lpt_probe() 351fdf94d1aSNicolas Souchu */ 3520f210c92SNicolas Souchu static int 3530f210c92SNicolas Souchu lpt_probe(device_t dev) 354fdf94d1aSNicolas Souchu { 355fdf94d1aSNicolas Souchu 3560f210c92SNicolas Souchu if (!lpt_detect(dev)) 3570f210c92SNicolas Souchu return (ENXIO); 3580f210c92SNicolas Souchu 3590f210c92SNicolas Souchu device_set_desc(dev, "Printer"); 3600f210c92SNicolas Souchu 361fdf94d1aSNicolas Souchu return (0); 362fdf94d1aSNicolas Souchu } 363fdf94d1aSNicolas Souchu 364fdf94d1aSNicolas Souchu static int 3650f210c92SNicolas Souchu lpt_attach(device_t dev) 366fdf94d1aSNicolas Souchu { 3670f210c92SNicolas Souchu device_t ppbus = device_get_parent(dev); 3680f210c92SNicolas Souchu struct lpt_data *sc = DEVTOSOFTC(dev); 369ca3d3795SJohn Baldwin int rid = 0, unit = device_get_unit(dev); 370fdf94d1aSNicolas Souchu int error; 371fdf94d1aSNicolas Souchu 372fdf94d1aSNicolas Souchu sc->sc_primed = 0; /* not primed yet */ 3732067d312SJohn Baldwin ppb_init_callout(ppbus, &sc->sc_timer, 0); 374fdf94d1aSNicolas Souchu 3752067d312SJohn Baldwin ppb_lock(ppbus); 3760f210c92SNicolas Souchu if ((error = lpt_request_ppbus(dev, PPB_DONTWAIT))) { 3772067d312SJohn Baldwin ppb_unlock(ppbus); 3782067d312SJohn Baldwin device_printf(dev, "cannot alloc ppbus (%d)!\n", error); 379fdf94d1aSNicolas Souchu return (0); 380fdf94d1aSNicolas Souchu } 381fdf94d1aSNicolas Souchu 3820f210c92SNicolas Souchu ppb_wctr(ppbus, LPC_NINIT); 3832067d312SJohn Baldwin lpt_release_ppbus(dev); 384192204feSJohn Baldwin ppb_unlock(ppbus); 3850f210c92SNicolas Souchu 3860f210c92SNicolas Souchu /* declare our interrupt handler */ 3872067d312SJohn Baldwin sc->sc_intr_resource = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 388ca3d3795SJohn Baldwin RF_SHAREABLE); 3892067d312SJohn Baldwin if (sc->sc_intr_resource) { 3902067d312SJohn Baldwin error = bus_setup_intr(dev, sc->sc_intr_resource, 3912067d312SJohn Baldwin INTR_TYPE_TTY | INTR_MPSAFE, NULL, lptintr, sc, 3922067d312SJohn Baldwin &sc->sc_intr_cookie); 3932067d312SJohn Baldwin if (error) { 3942067d312SJohn Baldwin bus_release_resource(dev, SYS_RES_IRQ, rid, 3952067d312SJohn Baldwin sc->sc_intr_resource); 3962067d312SJohn Baldwin device_printf(dev, 3972067d312SJohn Baldwin "Unable to register interrupt handler\n"); 3982067d312SJohn Baldwin return (error); 3992067d312SJohn Baldwin } 400fdf94d1aSNicolas Souchu sc->sc_irq = LP_HAS_IRQ | LP_USE_IRQ | LP_ENABLE_IRQ; 4010f210c92SNicolas Souchu device_printf(dev, "Interrupt-driven port\n"); 402fdf94d1aSNicolas Souchu } else { 403fdf94d1aSNicolas Souchu sc->sc_irq = 0; 4040f210c92SNicolas Souchu device_printf(dev, "Polled port\n"); 405fdf94d1aSNicolas Souchu } 406ca3d3795SJohn Baldwin lprintf(("irq %x\n", sc->sc_irq)); 407fdf94d1aSNicolas Souchu 4082067d312SJohn Baldwin sc->sc_inbuf = malloc(BUFSIZE, M_DEVBUF, M_WAITOK); 4092067d312SJohn Baldwin sc->sc_statbuf = malloc(BUFSTATSIZE, M_DEVBUF, M_WAITOK); 4102067d312SJohn Baldwin sc->sc_dev = dev; 4112067d312SJohn Baldwin sc->sc_cdev = make_dev(&lpt_cdevsw, unit, 4120f210c92SNicolas Souchu UID_ROOT, GID_WHEEL, 0600, LPT_NAME "%d", unit); 4132067d312SJohn Baldwin sc->sc_cdev->si_drv1 = sc; 4142067d312SJohn Baldwin sc->sc_cdev->si_drv2 = 0; 4152067d312SJohn Baldwin sc->sc_cdev_bypass = make_dev(&lpt_cdevsw, unit, 4160f210c92SNicolas Souchu UID_ROOT, GID_WHEEL, 0600, LPT_NAME "%d.ctl", unit); 4172067d312SJohn Baldwin sc->sc_cdev_bypass->si_drv1 = sc; 4182067d312SJohn Baldwin sc->sc_cdev_bypass->si_drv2 = (void *)LP_BYPASS; 4190f210c92SNicolas Souchu return (0); 420fdf94d1aSNicolas Souchu } 421fdf94d1aSNicolas Souchu 422858a52f4SMitsuru IWASAKI static int 423858a52f4SMitsuru IWASAKI lpt_detach(device_t dev) 424858a52f4SMitsuru IWASAKI { 425858a52f4SMitsuru IWASAKI struct lpt_data *sc = DEVTOSOFTC(dev); 4262067d312SJohn Baldwin device_t ppbus = device_get_parent(dev); 427858a52f4SMitsuru IWASAKI 4282067d312SJohn Baldwin destroy_dev(sc->sc_cdev); 4292067d312SJohn Baldwin destroy_dev(sc->sc_cdev_bypass); 4302067d312SJohn Baldwin ppb_lock(ppbus); 431858a52f4SMitsuru IWASAKI lpt_release_ppbus(dev); 4322067d312SJohn Baldwin ppb_unlock(ppbus); 4332067d312SJohn Baldwin callout_drain(&sc->sc_timer); 4342067d312SJohn Baldwin if (sc->sc_intr_resource != NULL) { 4352067d312SJohn Baldwin bus_teardown_intr(dev, sc->sc_intr_resource, 4362067d312SJohn Baldwin sc->sc_intr_cookie); 4372067d312SJohn Baldwin bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_intr_resource); 438858a52f4SMitsuru IWASAKI } 4392067d312SJohn Baldwin free(sc->sc_inbuf, M_DEVBUF); 4402067d312SJohn Baldwin free(sc->sc_statbuf, M_DEVBUF); 441858a52f4SMitsuru IWASAKI 442858a52f4SMitsuru IWASAKI return (0); 443858a52f4SMitsuru IWASAKI } 444858a52f4SMitsuru IWASAKI 445fdf94d1aSNicolas Souchu static void 446fdf94d1aSNicolas Souchu lptout(void *arg) 447fdf94d1aSNicolas Souchu { 4482067d312SJohn Baldwin struct lpt_data *sc = arg; 4492067d312SJohn Baldwin device_t dev = sc->sc_dev; 4508fcf690bSJohn Baldwin device_t ppbus __unused; 451fdf94d1aSNicolas Souchu 452b373cc42SAttilio Rao ppbus = device_get_parent(dev); 4532067d312SJohn Baldwin ppb_assert_locked(ppbus); 4540f210c92SNicolas Souchu lprintf(("T %x ", ppb_rstr(ppbus))); 455fdf94d1aSNicolas Souchu if (sc->sc_state & OPEN) { 456fdf94d1aSNicolas Souchu sc->sc_backoff++; 457fdf94d1aSNicolas Souchu if (sc->sc_backoff > hz/LPTOUTMAX) 458432e1942SJohn Baldwin sc->sc_backoff = hz/LPTOUTMAX; 4592067d312SJohn Baldwin callout_reset(&sc->sc_timer, sc->sc_backoff, lptout, sc); 460fdf94d1aSNicolas Souchu } else 461fdf94d1aSNicolas Souchu sc->sc_state &= ~TOUT; 462fdf94d1aSNicolas Souchu 463fdf94d1aSNicolas Souchu if (sc->sc_state & EERROR) 464fdf94d1aSNicolas Souchu sc->sc_state &= ~EERROR; 465fdf94d1aSNicolas Souchu 466fdf94d1aSNicolas Souchu /* 4670f210c92SNicolas Souchu * Avoid possible hangs due to missed interrupts 468fdf94d1aSNicolas Souchu */ 469fdf94d1aSNicolas Souchu if (sc->sc_xfercnt) { 4702067d312SJohn Baldwin lptintr(sc); 471fdf94d1aSNicolas Souchu } else { 472fdf94d1aSNicolas Souchu sc->sc_state &= ~OBUSY; 473521f364bSDag-Erling Smørgrav wakeup(dev); 474fdf94d1aSNicolas Souchu } 475fdf94d1aSNicolas Souchu } 476fdf94d1aSNicolas Souchu 477fdf94d1aSNicolas Souchu /* 478fdf94d1aSNicolas Souchu * lptopen -- reset the printer, then wait until it's selected and not busy. 479fdf94d1aSNicolas Souchu * If LP_BYPASS flag is selected, then we do not try to select the 480fdf94d1aSNicolas Souchu * printer -- this is just used for passing ioctls. 481fdf94d1aSNicolas Souchu */ 482fdf94d1aSNicolas Souchu 483fdf94d1aSNicolas Souchu static int 48489c9c53dSPoul-Henning Kamp lptopen(struct cdev *dev, int flags, int fmt, struct thread *td) 485fdf94d1aSNicolas Souchu { 486fdf94d1aSNicolas Souchu int trys, err; 487ae6b868aSJohn Baldwin struct lpt_data *sc = dev->si_drv1; 488ef1be3b0SChristian Brueffer device_t lptdev; 489ef1be3b0SChristian Brueffer device_t ppbus; 490fdf94d1aSNicolas Souchu 4910f210c92SNicolas Souchu if (!sc) 492fdf94d1aSNicolas Souchu return (ENXIO); 493fdf94d1aSNicolas Souchu 494ef1be3b0SChristian Brueffer lptdev = sc->sc_dev; 495ef1be3b0SChristian Brueffer ppbus = device_get_parent(lptdev); 496ef1be3b0SChristian Brueffer 4972067d312SJohn Baldwin ppb_lock(ppbus); 498fdf94d1aSNicolas Souchu if (sc->sc_state) { 4992067d312SJohn Baldwin lprintf(("%s: still open %x\n", device_get_nameunit(lptdev), 5002067d312SJohn Baldwin sc->sc_state)); 5012067d312SJohn Baldwin ppb_unlock(ppbus); 502fdf94d1aSNicolas Souchu return(EBUSY); 503fdf94d1aSNicolas Souchu } else 504fdf94d1aSNicolas Souchu sc->sc_state |= LPTINIT; 505fdf94d1aSNicolas Souchu 506f32b2a16SJohn Baldwin sc->sc_flags = (uintptr_t)dev->si_drv2; 507fdf94d1aSNicolas Souchu 508fdf94d1aSNicolas Souchu /* Check for open with BYPASS flag set. */ 509fdf94d1aSNicolas Souchu if (sc->sc_flags & LP_BYPASS) { 510fdf94d1aSNicolas Souchu sc->sc_state = OPEN; 5112067d312SJohn Baldwin ppb_unlock(ppbus); 512fdf94d1aSNicolas Souchu return(0); 513fdf94d1aSNicolas Souchu } 514fdf94d1aSNicolas Souchu 515fdf94d1aSNicolas Souchu /* request the ppbus only if we don't have it already */ 5160f210c92SNicolas Souchu if ((err = lpt_request_ppbus(lptdev, PPB_WAIT|PPB_INTR)) != 0) { 5170f210c92SNicolas Souchu /* give it a chance to try later */ 5180f210c92SNicolas Souchu sc->sc_state = 0; 5192067d312SJohn Baldwin ppb_unlock(ppbus); 520fdf94d1aSNicolas Souchu return (err); 5210f210c92SNicolas Souchu } 522fdf94d1aSNicolas Souchu 5232067d312SJohn Baldwin lprintf(("%s flags 0x%x\n", device_get_nameunit(lptdev), 5242067d312SJohn Baldwin sc->sc_flags)); 525fdf94d1aSNicolas Souchu 5260f210c92SNicolas Souchu /* set IRQ status according to ENABLE_IRQ flag 5270f210c92SNicolas Souchu */ 528fdf94d1aSNicolas Souchu if (sc->sc_irq & LP_ENABLE_IRQ) 529fdf94d1aSNicolas Souchu sc->sc_irq |= LP_USE_IRQ; 530fdf94d1aSNicolas Souchu else 531fdf94d1aSNicolas Souchu sc->sc_irq &= ~LP_USE_IRQ; 532fdf94d1aSNicolas Souchu 533fdf94d1aSNicolas Souchu /* init printer */ 534fdf94d1aSNicolas Souchu if ((sc->sc_flags & LP_NO_PRIME) == 0) { 535fdf94d1aSNicolas Souchu if ((sc->sc_flags & LP_PRIMEOPEN) || sc->sc_primed == 0) { 5360f210c92SNicolas Souchu ppb_wctr(ppbus, 0); 537fdf94d1aSNicolas Souchu sc->sc_primed++; 538fdf94d1aSNicolas Souchu DELAY(500); 539fdf94d1aSNicolas Souchu } 540fdf94d1aSNicolas Souchu } 541fdf94d1aSNicolas Souchu 5420f210c92SNicolas Souchu ppb_wctr(ppbus, LPC_SEL|LPC_NINIT); 543fdf94d1aSNicolas Souchu 544fdf94d1aSNicolas Souchu /* wait till ready (printer running diagnostics) */ 545fdf94d1aSNicolas Souchu trys = 0; 546fdf94d1aSNicolas Souchu do { 547fdf94d1aSNicolas Souchu /* ran out of waiting for the printer */ 548fdf94d1aSNicolas Souchu if (trys++ >= LPINITRDY*4) { 5490f210c92SNicolas Souchu lprintf(("status %x\n", ppb_rstr(ppbus))); 550fdf94d1aSNicolas Souchu 5510f210c92SNicolas Souchu lpt_release_ppbus(lptdev); 55284f9a0e5SJohn Baldwin sc->sc_state = 0; 5532067d312SJohn Baldwin ppb_unlock(ppbus); 554fdf94d1aSNicolas Souchu return (EBUSY); 555fdf94d1aSNicolas Souchu } 556fdf94d1aSNicolas Souchu 557fdf94d1aSNicolas Souchu /* wait 1/4 second, give up if we get a signal */ 5582067d312SJohn Baldwin if (ppb_sleep(ppbus, lptdev, LPPRI | PCATCH, "lptinit", 5592067d312SJohn Baldwin hz / 4) != EWOULDBLOCK) { 5600f210c92SNicolas Souchu lpt_release_ppbus(lptdev); 56184f9a0e5SJohn Baldwin sc->sc_state = 0; 5622067d312SJohn Baldwin ppb_unlock(ppbus); 563fdf94d1aSNicolas Souchu return (EBUSY); 564fdf94d1aSNicolas Souchu } 565fdf94d1aSNicolas Souchu 566fdf94d1aSNicolas Souchu /* is printer online and ready for output */ 5670f210c92SNicolas Souchu } while ((ppb_rstr(ppbus) & 568fdf94d1aSNicolas Souchu (LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR)) != 569fdf94d1aSNicolas Souchu (LPS_SEL|LPS_NBSY|LPS_NERR)); 570fdf94d1aSNicolas Souchu 571fdf94d1aSNicolas Souchu sc->sc_control = LPC_SEL|LPC_NINIT; 572fdf94d1aSNicolas Souchu if (sc->sc_flags & LP_AUTOLF) 573fdf94d1aSNicolas Souchu sc->sc_control |= LPC_AUTOL; 574fdf94d1aSNicolas Souchu 575fdf94d1aSNicolas Souchu /* enable interrupt if interrupt-driven */ 576fdf94d1aSNicolas Souchu if (sc->sc_irq & LP_USE_IRQ) 577fdf94d1aSNicolas Souchu sc->sc_control |= LPC_ENA; 578fdf94d1aSNicolas Souchu 5790f210c92SNicolas Souchu ppb_wctr(ppbus, sc->sc_control); 580fdf94d1aSNicolas Souchu 58184f9a0e5SJohn Baldwin sc->sc_state &= ~LPTINIT; 58284f9a0e5SJohn Baldwin sc->sc_state |= OPEN; 583fdf94d1aSNicolas Souchu sc->sc_xfercnt = 0; 584fdf94d1aSNicolas Souchu 585fdf94d1aSNicolas Souchu /* only use timeout if using interrupt */ 586fdf94d1aSNicolas Souchu lprintf(("irq %x\n", sc->sc_irq)); 587fdf94d1aSNicolas Souchu if (sc->sc_irq & LP_USE_IRQ) { 588fdf94d1aSNicolas Souchu sc->sc_state |= TOUT; 5892067d312SJohn Baldwin sc->sc_backoff = hz / LPTOUTINITIAL; 5902067d312SJohn Baldwin callout_reset(&sc->sc_timer, sc->sc_backoff, lptout, sc); 591fdf94d1aSNicolas Souchu } 592fdf94d1aSNicolas Souchu 5932067d312SJohn Baldwin /* release the ppbus */ 5942067d312SJohn Baldwin lpt_release_ppbus(lptdev); 5952067d312SJohn Baldwin ppb_unlock(ppbus); 5962067d312SJohn Baldwin 597fdf94d1aSNicolas Souchu lprintf(("opened.\n")); 598fdf94d1aSNicolas Souchu return(0); 599fdf94d1aSNicolas Souchu } 600fdf94d1aSNicolas Souchu 601fdf94d1aSNicolas Souchu /* 602fdf94d1aSNicolas Souchu * lptclose -- close the device, free the local line buffer. 603fdf94d1aSNicolas Souchu * 604fdf94d1aSNicolas Souchu * Check for interrupted write call added. 605fdf94d1aSNicolas Souchu */ 606fdf94d1aSNicolas Souchu 607fdf94d1aSNicolas Souchu static int 60889c9c53dSPoul-Henning Kamp lptclose(struct cdev *dev, int flags, int fmt, struct thread *td) 609fdf94d1aSNicolas Souchu { 610ae6b868aSJohn Baldwin struct lpt_data *sc = dev->si_drv1; 6112067d312SJohn Baldwin device_t lptdev = sc->sc_dev; 6120f210c92SNicolas Souchu device_t ppbus = device_get_parent(lptdev); 613fdf94d1aSNicolas Souchu int err; 614fdf94d1aSNicolas Souchu 6152067d312SJohn Baldwin ppb_lock(ppbus); 61684f9a0e5SJohn Baldwin if (sc->sc_flags & LP_BYPASS) 617fdf94d1aSNicolas Souchu goto end_close; 618fdf94d1aSNicolas Souchu 6192067d312SJohn Baldwin if ((err = lpt_request_ppbus(lptdev, PPB_WAIT|PPB_INTR)) != 0) { 6202067d312SJohn Baldwin ppb_unlock(ppbus); 621fdf94d1aSNicolas Souchu return (err); 6222067d312SJohn Baldwin } 623fdf94d1aSNicolas Souchu 624fdf94d1aSNicolas Souchu /* if the last write was interrupted, don't complete it */ 625fdf94d1aSNicolas Souchu if ((!(sc->sc_state & INTERRUPTED)) && (sc->sc_irq & LP_USE_IRQ)) 6260f210c92SNicolas Souchu while ((ppb_rstr(ppbus) & 627fdf94d1aSNicolas Souchu (LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR)) != 628fdf94d1aSNicolas Souchu (LPS_SEL|LPS_NBSY|LPS_NERR) || sc->sc_xfercnt) 6299599da5dSJohn Baldwin /* wait 1 second, give up if we get a signal */ 6302067d312SJohn Baldwin if (ppb_sleep(ppbus, lptdev, LPPRI | PCATCH, "lpclose", 6312067d312SJohn Baldwin hz) != EWOULDBLOCK) 632fdf94d1aSNicolas Souchu break; 633fdf94d1aSNicolas Souchu 6342067d312SJohn Baldwin sc->sc_state &= ~OPEN; 6352067d312SJohn Baldwin callout_stop(&sc->sc_timer); 6360f210c92SNicolas Souchu ppb_wctr(ppbus, LPC_NINIT); 637fdf94d1aSNicolas Souchu 6382067d312SJohn Baldwin /* 6390f210c92SNicolas Souchu * unregistration of interrupt forced by release 6400f210c92SNicolas Souchu */ 6410f210c92SNicolas Souchu lpt_release_ppbus(lptdev); 642fdf94d1aSNicolas Souchu 6432067d312SJohn Baldwin end_close: 64484f9a0e5SJohn Baldwin sc->sc_state = 0; 64584f9a0e5SJohn Baldwin sc->sc_xfercnt = 0; 64684f9a0e5SJohn Baldwin ppb_unlock(ppbus); 647fdf94d1aSNicolas Souchu lprintf(("closed.\n")); 648fdf94d1aSNicolas Souchu return(0); 649fdf94d1aSNicolas Souchu } 650fdf94d1aSNicolas Souchu 651fdf94d1aSNicolas Souchu /* 652fdf94d1aSNicolas Souchu * lpt_pushbytes() 653fdf94d1aSNicolas Souchu * Workhorse for actually spinning and writing bytes to printer 654fdf94d1aSNicolas Souchu * Derived from lpa.c 655fdf94d1aSNicolas Souchu * Originally by ? 656fdf94d1aSNicolas Souchu * 657fdf94d1aSNicolas Souchu * This code is only used when we are polling the port 658fdf94d1aSNicolas Souchu */ 659fdf94d1aSNicolas Souchu static int 6602067d312SJohn Baldwin lpt_pushbytes(struct lpt_data *sc) 661fdf94d1aSNicolas Souchu { 6622067d312SJohn Baldwin device_t dev = sc->sc_dev; 6630f210c92SNicolas Souchu device_t ppbus = device_get_parent(dev); 664fdf94d1aSNicolas Souchu int spin, err, tic; 665fdf94d1aSNicolas Souchu char ch; 666fdf94d1aSNicolas Souchu 6672067d312SJohn Baldwin ppb_assert_locked(ppbus); 668fdf94d1aSNicolas Souchu lprintf(("p")); 669fdf94d1aSNicolas Souchu /* loop for every character .. */ 670fdf94d1aSNicolas Souchu while (sc->sc_xfercnt > 0) { 671fdf94d1aSNicolas Souchu /* printer data */ 672fdf94d1aSNicolas Souchu ch = *(sc->sc_cp); 673fdf94d1aSNicolas Souchu sc->sc_cp++; 674fdf94d1aSNicolas Souchu sc->sc_xfercnt--; 675fdf94d1aSNicolas Souchu 676fdf94d1aSNicolas Souchu /* 677fdf94d1aSNicolas Souchu * Wait for printer ready. 678fdf94d1aSNicolas Souchu * Loop 20 usecs testing BUSY bit, then sleep 679fdf94d1aSNicolas Souchu * for exponentially increasing timeout. (vak) 680fdf94d1aSNicolas Souchu */ 6810f210c92SNicolas Souchu for (spin = 0; NOT_READY(ppbus) && spin < MAX_SPIN; ++spin) 682fdf94d1aSNicolas Souchu DELAY(1); /* XXX delay is NOT this accurate! */ 683fdf94d1aSNicolas Souchu if (spin >= MAX_SPIN) { 684fdf94d1aSNicolas Souchu tic = 0; 6850f210c92SNicolas Souchu while (NOT_READY(ppbus)) { 686fdf94d1aSNicolas Souchu /* 687fdf94d1aSNicolas Souchu * Now sleep, every cycle a 688fdf94d1aSNicolas Souchu * little longer .. 689fdf94d1aSNicolas Souchu */ 690fdf94d1aSNicolas Souchu tic = tic + tic + 1; 691fdf94d1aSNicolas Souchu /* 692fdf94d1aSNicolas Souchu * But no more than 10 seconds. (vak) 693fdf94d1aSNicolas Souchu */ 694fdf94d1aSNicolas Souchu if (tic > MAX_SLEEP) 695fdf94d1aSNicolas Souchu tic = MAX_SLEEP; 6962067d312SJohn Baldwin err = ppb_sleep(ppbus, dev, LPPRI, 697fdf94d1aSNicolas Souchu LPT_NAME "poll", tic); 698fdf94d1aSNicolas Souchu if (err != EWOULDBLOCK) { 699fdf94d1aSNicolas Souchu return (err); 700fdf94d1aSNicolas Souchu } 701fdf94d1aSNicolas Souchu } 702fdf94d1aSNicolas Souchu } 703fdf94d1aSNicolas Souchu 704fdf94d1aSNicolas Souchu /* output data */ 7050f210c92SNicolas Souchu ppb_wdtr(ppbus, ch); 706fdf94d1aSNicolas Souchu /* strobe */ 7070f210c92SNicolas Souchu ppb_wctr(ppbus, sc->sc_control|LPC_STB); 7080f210c92SNicolas Souchu ppb_wctr(ppbus, sc->sc_control); 709fdf94d1aSNicolas Souchu } 710fdf94d1aSNicolas Souchu return(0); 711fdf94d1aSNicolas Souchu } 712fdf94d1aSNicolas Souchu 713fdf94d1aSNicolas Souchu /* 714fdf94d1aSNicolas Souchu * lptread --retrieve printer status in IEEE1284 NIBBLE mode 715fdf94d1aSNicolas Souchu */ 716fdf94d1aSNicolas Souchu 717fdf94d1aSNicolas Souchu static int 71889c9c53dSPoul-Henning Kamp lptread(struct cdev *dev, struct uio *uio, int ioflag) 719fdf94d1aSNicolas Souchu { 720ae6b868aSJohn Baldwin struct lpt_data *sc = dev->si_drv1; 7212067d312SJohn Baldwin device_t lptdev = sc->sc_dev; 7220f210c92SNicolas Souchu device_t ppbus = device_get_parent(lptdev); 723fdf94d1aSNicolas Souchu int error = 0, len; 724fdf94d1aSNicolas Souchu 7253841bf81SBrian Feldman if (sc->sc_flags & LP_BYPASS) { 7263841bf81SBrian Feldman /* we can't do reads in bypass mode */ 7273841bf81SBrian Feldman return (EPERM); 7283841bf81SBrian Feldman } 7293841bf81SBrian Feldman 7302067d312SJohn Baldwin ppb_lock(ppbus); 7312067d312SJohn Baldwin if ((error = ppb_1284_negociate(ppbus, PPB_NIBBLE, 0))) { 7322067d312SJohn Baldwin ppb_unlock(ppbus); 733fdf94d1aSNicolas Souchu return (error); 7342067d312SJohn Baldwin } 735fdf94d1aSNicolas Souchu 736fdf94d1aSNicolas Souchu /* read data in an other buffer, read/write may be simultaneous */ 737fdf94d1aSNicolas Souchu len = 0; 738fdf94d1aSNicolas Souchu while (uio->uio_resid) { 7390f210c92SNicolas Souchu if ((error = ppb_1284_read(ppbus, PPB_NIBBLE, 7405856c958SPoul-Henning Kamp sc->sc_statbuf, min(BUFSTATSIZE, 741fdf94d1aSNicolas Souchu uio->uio_resid), &len))) { 742fdf94d1aSNicolas Souchu goto error; 743fdf94d1aSNicolas Souchu } 744fdf94d1aSNicolas Souchu 745fdf94d1aSNicolas Souchu if (!len) 746fdf94d1aSNicolas Souchu goto error; /* no more data */ 747fdf94d1aSNicolas Souchu 7482067d312SJohn Baldwin ppb_unlock(ppbus); 7492067d312SJohn Baldwin error = uiomove(sc->sc_statbuf, len, uio); 7502067d312SJohn Baldwin ppb_lock(ppbus); 7512067d312SJohn Baldwin if (error) 752fdf94d1aSNicolas Souchu goto error; 753fdf94d1aSNicolas Souchu } 754fdf94d1aSNicolas Souchu 755fdf94d1aSNicolas Souchu error: 7560f210c92SNicolas Souchu ppb_1284_terminate(ppbus); 7572067d312SJohn Baldwin ppb_unlock(ppbus); 758fdf94d1aSNicolas Souchu return (error); 759fdf94d1aSNicolas Souchu } 760fdf94d1aSNicolas Souchu 761fdf94d1aSNicolas Souchu /* 762fdf94d1aSNicolas Souchu * lptwrite --copy a line from user space to a local buffer, then call 763fdf94d1aSNicolas Souchu * putc to get the chars moved to the output queue. 764fdf94d1aSNicolas Souchu * 765fdf94d1aSNicolas Souchu * Flagging of interrupted write added. 766fdf94d1aSNicolas Souchu */ 767fdf94d1aSNicolas Souchu 768fdf94d1aSNicolas Souchu static int 76989c9c53dSPoul-Henning Kamp lptwrite(struct cdev *dev, struct uio *uio, int ioflag) 770fdf94d1aSNicolas Souchu { 771fdf94d1aSNicolas Souchu register unsigned n; 7720f210c92SNicolas Souchu int err; 773ae6b868aSJohn Baldwin struct lpt_data *sc = dev->si_drv1; 7742067d312SJohn Baldwin device_t lptdev = sc->sc_dev; 7750f210c92SNicolas Souchu device_t ppbus = device_get_parent(lptdev); 776fdf94d1aSNicolas Souchu 777fdf94d1aSNicolas Souchu if (sc->sc_flags & LP_BYPASS) { 778fdf94d1aSNicolas Souchu /* we can't do writes in bypass mode */ 779fdf94d1aSNicolas Souchu return (EPERM); 780fdf94d1aSNicolas Souchu } 781fdf94d1aSNicolas Souchu 782fdf94d1aSNicolas Souchu /* request the ppbus only if we don't have it already */ 7832067d312SJohn Baldwin ppb_lock(ppbus); 7842067d312SJohn Baldwin if ((err = lpt_request_ppbus(lptdev, PPB_WAIT|PPB_INTR)) != 0) { 7852067d312SJohn Baldwin ppb_unlock(ppbus); 786fdf94d1aSNicolas Souchu return (err); 7870f210c92SNicolas Souchu } 7880f210c92SNicolas Souchu 789fdf94d1aSNicolas Souchu sc->sc_state &= ~INTERRUPTED; 790fdf94d1aSNicolas Souchu while ((n = min(BUFSIZE, uio->uio_resid)) != 0) { 7915856c958SPoul-Henning Kamp sc->sc_cp = sc->sc_inbuf; 7922067d312SJohn Baldwin ppb_unlock(ppbus); 7932067d312SJohn Baldwin err = uiomove(sc->sc_cp, n, uio); 7942067d312SJohn Baldwin ppb_lock(ppbus); 7952067d312SJohn Baldwin if (err) 7962067d312SJohn Baldwin break; 797fdf94d1aSNicolas Souchu sc->sc_xfercnt = n; 798fdf94d1aSNicolas Souchu 799fdf94d1aSNicolas Souchu if (sc->sc_irq & LP_ENABLE_EXT) { 800fdf94d1aSNicolas Souchu /* try any extended mode */ 8010f210c92SNicolas Souchu err = ppb_write(ppbus, sc->sc_cp, 802fdf94d1aSNicolas Souchu sc->sc_xfercnt, 0); 803fdf94d1aSNicolas Souchu switch (err) { 804fdf94d1aSNicolas Souchu case 0: 805fdf94d1aSNicolas Souchu /* if not all data was sent, we could rely 806fdf94d1aSNicolas Souchu * on polling for the last bytes */ 807fdf94d1aSNicolas Souchu sc->sc_xfercnt = 0; 808fdf94d1aSNicolas Souchu break; 809fdf94d1aSNicolas Souchu case EINTR: 810fdf94d1aSNicolas Souchu sc->sc_state |= INTERRUPTED; 8112067d312SJohn Baldwin ppb_unlock(ppbus); 812fdf94d1aSNicolas Souchu return (err); 813fdf94d1aSNicolas Souchu case EINVAL: 814fdf94d1aSNicolas Souchu /* advanced mode not avail */ 815ae6b868aSJohn Baldwin log(LOG_NOTICE, 816ae6b868aSJohn Baldwin "%s: advanced mode not avail, polling\n", 8172067d312SJohn Baldwin device_get_nameunit(sc->sc_dev)); 818fdf94d1aSNicolas Souchu break; 819fdf94d1aSNicolas Souchu default: 8202067d312SJohn Baldwin ppb_unlock(ppbus); 821fdf94d1aSNicolas Souchu return (err); 822fdf94d1aSNicolas Souchu } 823fdf94d1aSNicolas Souchu } else while ((sc->sc_xfercnt > 0)&&(sc->sc_irq & LP_USE_IRQ)) { 824fdf94d1aSNicolas Souchu lprintf(("i")); 825fdf94d1aSNicolas Souchu /* if the printer is ready for a char, */ 826fdf94d1aSNicolas Souchu /* give it one */ 827fdf94d1aSNicolas Souchu if ((sc->sc_state & OBUSY) == 0){ 828fdf94d1aSNicolas Souchu lprintf(("\nC %d. ", sc->sc_xfercnt)); 8292067d312SJohn Baldwin lptintr(sc); 830fdf94d1aSNicolas Souchu } 831fdf94d1aSNicolas Souchu lprintf(("W ")); 832fdf94d1aSNicolas Souchu if (sc->sc_state & OBUSY) 8332067d312SJohn Baldwin if ((err = ppb_sleep(ppbus, lptdev, 834fdf94d1aSNicolas Souchu LPPRI|PCATCH, LPT_NAME "write", 0))) { 835fdf94d1aSNicolas Souchu sc->sc_state |= INTERRUPTED; 8362067d312SJohn Baldwin ppb_unlock(ppbus); 837fdf94d1aSNicolas Souchu return(err); 838fdf94d1aSNicolas Souchu } 839fdf94d1aSNicolas Souchu } 840fdf94d1aSNicolas Souchu 841fdf94d1aSNicolas Souchu /* check to see if we must do a polled write */ 842fdf94d1aSNicolas Souchu if (!(sc->sc_irq & LP_USE_IRQ) && (sc->sc_xfercnt)) { 843fdf94d1aSNicolas Souchu lprintf(("p")); 844fdf94d1aSNicolas Souchu 8452067d312SJohn Baldwin err = lpt_pushbytes(sc); 846fdf94d1aSNicolas Souchu 8472067d312SJohn Baldwin if (err) { 8482067d312SJohn Baldwin ppb_unlock(ppbus); 849fdf94d1aSNicolas Souchu return (err); 850fdf94d1aSNicolas Souchu } 851fdf94d1aSNicolas Souchu } 8522067d312SJohn Baldwin } 853fdf94d1aSNicolas Souchu 854fdf94d1aSNicolas Souchu /* we have not been interrupted, release the ppbus */ 8550f210c92SNicolas Souchu lpt_release_ppbus(lptdev); 8562067d312SJohn Baldwin ppb_unlock(ppbus); 857fdf94d1aSNicolas Souchu 8582067d312SJohn Baldwin return (err); 859fdf94d1aSNicolas Souchu } 860fdf94d1aSNicolas Souchu 861fdf94d1aSNicolas Souchu /* 8622067d312SJohn Baldwin * lptintr -- handle printer interrupts which occur when the printer is 863fdf94d1aSNicolas Souchu * ready to accept another char. 864fdf94d1aSNicolas Souchu * 865fdf94d1aSNicolas Souchu * do checking for interrupted write call. 866fdf94d1aSNicolas Souchu */ 867fdf94d1aSNicolas Souchu static void 8682067d312SJohn Baldwin lptintr(void *arg) 869fdf94d1aSNicolas Souchu { 8702067d312SJohn Baldwin struct lpt_data *sc = arg; 8712067d312SJohn Baldwin device_t lptdev = sc->sc_dev; 8720f210c92SNicolas Souchu device_t ppbus = device_get_parent(lptdev); 873150630c4SDoug Rabson int sts = 0; 874fdf94d1aSNicolas Souchu int i; 875fdf94d1aSNicolas Souchu 876fdf94d1aSNicolas Souchu /* 877fdf94d1aSNicolas Souchu * Is printer online and ready for output? 878fdf94d1aSNicolas Souchu * 879fdf94d1aSNicolas Souchu * Avoid falling back to lptout() too quickly. First spin-loop 880fdf94d1aSNicolas Souchu * to see if the printer will become ready ``really soon now''. 881fdf94d1aSNicolas Souchu */ 882fdf94d1aSNicolas Souchu for (i = 0; i < 100 && 8830f210c92SNicolas Souchu ((sts=ppb_rstr(ppbus)) & RDY_MASK) != LP_READY; i++) ; 884fdf94d1aSNicolas Souchu 885fdf94d1aSNicolas Souchu if ((sts & RDY_MASK) == LP_READY) { 886fdf94d1aSNicolas Souchu sc->sc_state = (sc->sc_state | OBUSY) & ~EERROR; 887fdf94d1aSNicolas Souchu sc->sc_backoff = hz / LPTOUTINITIAL; 888fdf94d1aSNicolas Souchu 889fdf94d1aSNicolas Souchu if (sc->sc_xfercnt) { 890fdf94d1aSNicolas Souchu /* send char */ 891fdf94d1aSNicolas Souchu /*lprintf(("%x ", *sc->sc_cp)); */ 8920f210c92SNicolas Souchu ppb_wdtr(ppbus, *sc->sc_cp++) ; 8930f210c92SNicolas Souchu ppb_wctr(ppbus, sc->sc_control|LPC_STB); 894fdf94d1aSNicolas Souchu /* DELAY(X) */ 8950f210c92SNicolas Souchu ppb_wctr(ppbus, sc->sc_control); 896fdf94d1aSNicolas Souchu 897fdf94d1aSNicolas Souchu /* any more data for printer */ 898284c87f6SJohn Baldwin if (--(sc->sc_xfercnt) > 0) 899284c87f6SJohn Baldwin return; 900fdf94d1aSNicolas Souchu } 901fdf94d1aSNicolas Souchu 902fdf94d1aSNicolas Souchu /* 903fdf94d1aSNicolas Souchu * No more data waiting for printer. 904249a9688SNicolas Souchu * Wakeup is not done if write call was not interrupted. 905fdf94d1aSNicolas Souchu */ 906fdf94d1aSNicolas Souchu sc->sc_state &= ~OBUSY; 907fdf94d1aSNicolas Souchu 908fdf94d1aSNicolas Souchu if (!(sc->sc_state & INTERRUPTED)) 909521f364bSDag-Erling Smørgrav wakeup(lptdev); 910fdf94d1aSNicolas Souchu lprintf(("w ")); 911fdf94d1aSNicolas Souchu return; 912fdf94d1aSNicolas Souchu } else { /* check for error */ 913fdf94d1aSNicolas Souchu if (((sts & (LPS_NERR | LPS_OUT) ) != LPS_NERR) && 914fdf94d1aSNicolas Souchu (sc->sc_state & OPEN)) 915fdf94d1aSNicolas Souchu sc->sc_state |= EERROR; 916fdf94d1aSNicolas Souchu /* lptout() will jump in and try to restart. */ 917fdf94d1aSNicolas Souchu } 918fdf94d1aSNicolas Souchu lprintf(("sts %x ", sts)); 919fdf94d1aSNicolas Souchu } 920fdf94d1aSNicolas Souchu 921fdf94d1aSNicolas Souchu static int 92289c9c53dSPoul-Henning Kamp lptioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags, struct thread *td) 923fdf94d1aSNicolas Souchu { 924fdf94d1aSNicolas Souchu int error = 0; 925ae6b868aSJohn Baldwin struct lpt_data *sc = dev->si_drv1; 9262067d312SJohn Baldwin device_t ppbus; 927fdf94d1aSNicolas Souchu u_char old_sc_irq; /* old printer IRQ status */ 928fdf94d1aSNicolas Souchu 929fdf94d1aSNicolas Souchu switch (cmd) { 930fdf94d1aSNicolas Souchu case LPT_IRQ : 9312067d312SJohn Baldwin ppbus = device_get_parent(sc->sc_dev); 9322067d312SJohn Baldwin ppb_lock(ppbus); 933fdf94d1aSNicolas Souchu if (sc->sc_irq & LP_HAS_IRQ) { 934fdf94d1aSNicolas Souchu /* 935fdf94d1aSNicolas Souchu * NOTE: 936fdf94d1aSNicolas Souchu * If the IRQ status is changed, 937fdf94d1aSNicolas Souchu * this will only be visible on the 938fdf94d1aSNicolas Souchu * next open. 939fdf94d1aSNicolas Souchu * 940fdf94d1aSNicolas Souchu * If interrupt status changes, 941fdf94d1aSNicolas Souchu * this gets syslog'd. 942fdf94d1aSNicolas Souchu */ 943fdf94d1aSNicolas Souchu old_sc_irq = sc->sc_irq; 944fdf94d1aSNicolas Souchu switch (*(int*)data) { 945fdf94d1aSNicolas Souchu case 0: 946fdf94d1aSNicolas Souchu sc->sc_irq &= (~LP_ENABLE_IRQ); 947fdf94d1aSNicolas Souchu break; 948fdf94d1aSNicolas Souchu case 1: 949fdf94d1aSNicolas Souchu sc->sc_irq &= (~LP_ENABLE_EXT); 950fdf94d1aSNicolas Souchu sc->sc_irq |= LP_ENABLE_IRQ; 951fdf94d1aSNicolas Souchu break; 952fdf94d1aSNicolas Souchu case 2: 953fdf94d1aSNicolas Souchu /* classic irq based transfer and advanced 954fdf94d1aSNicolas Souchu * modes are in conflict 955fdf94d1aSNicolas Souchu */ 956fdf94d1aSNicolas Souchu sc->sc_irq &= (~LP_ENABLE_IRQ); 957fdf94d1aSNicolas Souchu sc->sc_irq |= LP_ENABLE_EXT; 958fdf94d1aSNicolas Souchu break; 959fdf94d1aSNicolas Souchu case 3: 960fdf94d1aSNicolas Souchu sc->sc_irq &= (~LP_ENABLE_EXT); 961fdf94d1aSNicolas Souchu break; 962fdf94d1aSNicolas Souchu default: 963fdf94d1aSNicolas Souchu break; 964fdf94d1aSNicolas Souchu } 965fdf94d1aSNicolas Souchu 966fdf94d1aSNicolas Souchu if (old_sc_irq != sc->sc_irq ) 967ae6b868aSJohn Baldwin log(LOG_NOTICE, "%s: switched to %s %s mode\n", 9682067d312SJohn Baldwin device_get_nameunit(sc->sc_dev), 969fdf94d1aSNicolas Souchu (sc->sc_irq & LP_ENABLE_IRQ)? 970fdf94d1aSNicolas Souchu "interrupt-driven":"polled", 971fdf94d1aSNicolas Souchu (sc->sc_irq & LP_ENABLE_EXT)? 972fdf94d1aSNicolas Souchu "extended":"standard"); 973fdf94d1aSNicolas Souchu } else /* polled port */ 974fdf94d1aSNicolas Souchu error = EOPNOTSUPP; 9752067d312SJohn Baldwin ppb_unlock(ppbus); 976fdf94d1aSNicolas Souchu break; 977fdf94d1aSNicolas Souchu default: 978fdf94d1aSNicolas Souchu error = ENODEV; 979fdf94d1aSNicolas Souchu } 980fdf94d1aSNicolas Souchu 981fdf94d1aSNicolas Souchu return(error); 982fdf94d1aSNicolas Souchu } 9830f210c92SNicolas Souchu 9840f063508SPeter Wemm static device_method_t lpt_methods[] = { 9850f063508SPeter Wemm /* device interface */ 9860f063508SPeter Wemm DEVMETHOD(device_identify, lpt_identify), 9870f063508SPeter Wemm DEVMETHOD(device_probe, lpt_probe), 9880f063508SPeter Wemm DEVMETHOD(device_attach, lpt_attach), 989858a52f4SMitsuru IWASAKI DEVMETHOD(device_detach, lpt_detach), 9900f063508SPeter Wemm { 0, 0 } 9910f063508SPeter Wemm }; 9920f063508SPeter Wemm 9930f063508SPeter Wemm static driver_t lpt_driver = { 9940f063508SPeter Wemm LPT_NAME, 9950f063508SPeter Wemm lpt_methods, 9960f063508SPeter Wemm sizeof(struct lpt_data), 9970f063508SPeter Wemm }; 9980f063508SPeter Wemm 9990f210c92SNicolas Souchu DRIVER_MODULE(lpt, ppbus, lpt_driver, lpt_devclass, 0, 0); 1000f5fd5611SRuslan Ermilov MODULE_DEPEND(lpt, ppbus, 1, 1, 1); 1001