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>
56fdf94d1aSNicolas Souchu /*
57fdf94d1aSNicolas Souchu * Device Driver for AT parallel printer port
58fdf94d1aSNicolas Souchu * Written by William Jolitz 12/18/90
59fdf94d1aSNicolas Souchu */
60fdf94d1aSNicolas Souchu
61fdf94d1aSNicolas Souchu /*
62fdf94d1aSNicolas Souchu * Updated for ppbus by Nicolas Souchu
63fdf94d1aSNicolas Souchu * [Mon Jul 28 1997]
64fdf94d1aSNicolas Souchu */
65fdf94d1aSNicolas Souchu
660f210c92SNicolas Souchu #include "opt_lpt.h"
670f210c92SNicolas Souchu
68fdf94d1aSNicolas Souchu #include <sys/param.h>
69fdf94d1aSNicolas Souchu #include <sys/systm.h>
700f210c92SNicolas Souchu #include <sys/module.h>
710f210c92SNicolas Souchu #include <sys/bus.h>
72fdf94d1aSNicolas Souchu #include <sys/conf.h>
73fdf94d1aSNicolas Souchu #include <sys/kernel.h>
74fdf94d1aSNicolas Souchu #include <sys/uio.h>
75fdf94d1aSNicolas Souchu #include <sys/syslog.h>
765856c958SPoul-Henning Kamp #include <sys/malloc.h>
77fdf94d1aSNicolas Souchu
780f210c92SNicolas Souchu #include <machine/bus.h>
790f210c92SNicolas Souchu #include <machine/resource.h>
800f210c92SNicolas Souchu #include <sys/rman.h>
810f210c92SNicolas Souchu
8274cf6eb6SDavid E. O'Brien #include <dev/ppbus/lptio.h>
83fdf94d1aSNicolas Souchu #include <dev/ppbus/ppbconf.h>
84fdf94d1aSNicolas Souchu #include <dev/ppbus/ppb_1284.h>
85fdf94d1aSNicolas Souchu #include <dev/ppbus/lpt.h>
860f210c92SNicolas Souchu #include "ppbus_if.h"
870f210c92SNicolas Souchu #include <dev/ppbus/ppbio.h>
88fdf94d1aSNicolas Souchu
89fdf94d1aSNicolas Souchu #ifndef LPT_DEBUG
90fdf94d1aSNicolas Souchu #define lprintf(args)
91fdf94d1aSNicolas Souchu #else
92fdf94d1aSNicolas Souchu #define lprintf(args) \
93fdf94d1aSNicolas Souchu do { \
94fdf94d1aSNicolas Souchu if (lptflag) \
95fdf94d1aSNicolas Souchu printf args; \
96fdf94d1aSNicolas Souchu } while (0)
97fdf94d1aSNicolas Souchu static int volatile lptflag = 1;
98fdf94d1aSNicolas Souchu #endif
99fdf94d1aSNicolas Souchu
100fdf94d1aSNicolas Souchu #define LPINITRDY 4 /* wait up to 4 seconds for a ready */
101fdf94d1aSNicolas Souchu #define LPTOUTINITIAL 10 /* initial timeout to wait for ready 1/10 s */
102fdf94d1aSNicolas Souchu #define LPTOUTMAX 1 /* maximal timeout 1 s */
103fdf94d1aSNicolas Souchu #define LPPRI (PZERO+8)
104fdf94d1aSNicolas Souchu #define BUFSIZE 1024
105fdf94d1aSNicolas Souchu #define BUFSTATSIZE 32
106fdf94d1aSNicolas Souchu
10717b5923dSNicolas Souchu struct lpt_data {
1082067d312SJohn Baldwin device_t sc_dev;
1092067d312SJohn Baldwin struct cdev *sc_cdev;
1102067d312SJohn Baldwin struct cdev *sc_cdev_bypass;
11117b5923dSNicolas Souchu short sc_state;
11217b5923dSNicolas Souchu /* default case: negative prime, negative ack, handshake strobe,
11317b5923dSNicolas Souchu prime once */
11417b5923dSNicolas Souchu u_char sc_control;
11517b5923dSNicolas Souchu char sc_flags;
116453130d9SPedro F. Giffuni #define LP_POS_INIT 0x04 /* if we are a positive init signal */
11717b5923dSNicolas Souchu #define LP_POS_ACK 0x08 /* if we are a positive going ack */
11817b5923dSNicolas Souchu #define LP_NO_PRIME 0x10 /* don't prime the printer at all */
11917b5923dSNicolas Souchu #define LP_PRIMEOPEN 0x20 /* prime on every open */
12017b5923dSNicolas Souchu #define LP_AUTOLF 0x40 /* tell printer to do an automatic lf */
12117b5923dSNicolas Souchu #define LP_BYPASS 0x80 /* bypass printer ready checks */
1225856c958SPoul-Henning Kamp void *sc_inbuf;
1235856c958SPoul-Henning Kamp void *sc_statbuf;
12417b5923dSNicolas Souchu short sc_xfercnt ;
12517b5923dSNicolas Souchu char sc_primed;
12617b5923dSNicolas Souchu char *sc_cp ;
12717b5923dSNicolas Souchu u_short sc_irq ; /* IRQ status of port */
12817b5923dSNicolas Souchu #define LP_HAS_IRQ 0x01 /* we have an irq available */
12917b5923dSNicolas Souchu #define LP_USE_IRQ 0x02 /* we are using our irq */
13017b5923dSNicolas Souchu #define LP_ENABLE_IRQ 0x04 /* enable IRQ on open */
13117b5923dSNicolas Souchu #define LP_ENABLE_EXT 0x10 /* we shall use advanced mode when possible */
13217b5923dSNicolas Souchu u_char sc_backoff ; /* time to call lptout() again */
1332067d312SJohn Baldwin struct callout sc_timer;
13417b5923dSNicolas Souchu
1352067d312SJohn Baldwin struct resource *sc_intr_resource; /* interrupt resource */
1362067d312SJohn Baldwin void *sc_intr_cookie; /* interrupt cookie */
1370f210c92SNicolas Souchu };
138fdf94d1aSNicolas Souchu
139fdf94d1aSNicolas Souchu #define LPT_NAME "lpt" /* our official name */
140fdf94d1aSNicolas Souchu
1415773ac11SJohn Baldwin static callout_func_t lptout;
1420f210c92SNicolas Souchu static int lpt_port_test(device_t dev, u_char data, u_char mask);
1430f210c92SNicolas Souchu static int lpt_detect(device_t dev);
144fdf94d1aSNicolas Souchu
1450f210c92SNicolas Souchu #define DEVTOSOFTC(dev) \
1460f210c92SNicolas Souchu ((struct lpt_data *)device_get_softc(dev))
147fdf94d1aSNicolas Souchu
1482067d312SJohn Baldwin static void lptintr(void *arg);
149fdf94d1aSNicolas Souchu
150fdf94d1aSNicolas Souchu /* bits for state */
151fdf94d1aSNicolas Souchu #define OPEN (1<<0) /* device is open */
152fdf94d1aSNicolas Souchu #define ASLP (1<<1) /* awaiting draining of printer */
153fdf94d1aSNicolas Souchu #define EERROR (1<<2) /* error was received from printer */
154fdf94d1aSNicolas Souchu #define OBUSY (1<<3) /* printer is busy doing output */
155fdf94d1aSNicolas Souchu #define LPTOUT (1<<4) /* timeout while not selected */
156fdf94d1aSNicolas Souchu #define TOUT (1<<5) /* timeout while not selected */
157fdf94d1aSNicolas Souchu #define LPTINIT (1<<6) /* waiting to initialize for open */
158fdf94d1aSNicolas Souchu #define INTERRUPTED (1<<7) /* write call was interrupted */
159fdf94d1aSNicolas Souchu #define HAVEBUS (1<<8) /* the driver owns the bus */
160fdf94d1aSNicolas Souchu
161fdf94d1aSNicolas Souchu /* status masks to interrogate printer status */
162fdf94d1aSNicolas Souchu #define RDY_MASK (LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR) /* ready ? */
163fdf94d1aSNicolas Souchu #define LP_READY (LPS_SEL|LPS_NBSY|LPS_NERR)
164fdf94d1aSNicolas Souchu
165fdf94d1aSNicolas Souchu /* Printer Ready condition - from lpa.c */
166fdf94d1aSNicolas Souchu /* Only used in polling code */
167fdf94d1aSNicolas Souchu #define LPS_INVERT (LPS_NBSY | LPS_NACK | LPS_SEL | LPS_NERR)
168fdf94d1aSNicolas Souchu #define LPS_MASK (LPS_NBSY | LPS_NACK | LPS_OUT | LPS_SEL | LPS_NERR)
1690f210c92SNicolas Souchu #define NOT_READY(ppbus) ((ppb_rstr(ppbus)^LPS_INVERT)&LPS_MASK)
170fdf94d1aSNicolas Souchu
171fdf94d1aSNicolas Souchu #define MAX_SLEEP (hz*5) /* Timeout while waiting for device ready */
172fdf94d1aSNicolas Souchu #define MAX_SPIN 20 /* Max delay for device ready in usecs */
173fdf94d1aSNicolas Souchu
174fdf94d1aSNicolas Souchu static d_open_t lptopen;
175fdf94d1aSNicolas Souchu static d_close_t lptclose;
176fdf94d1aSNicolas Souchu static d_write_t lptwrite;
177fdf94d1aSNicolas Souchu static d_read_t lptread;
178fdf94d1aSNicolas Souchu static d_ioctl_t lptioctl;
179fdf94d1aSNicolas Souchu
1804e2f199eSPoul-Henning Kamp static struct cdevsw lpt_cdevsw = {
181dc08ffecSPoul-Henning Kamp .d_version = D_VERSION,
1827ac40f5fSPoul-Henning Kamp .d_open = lptopen,
1837ac40f5fSPoul-Henning Kamp .d_close = lptclose,
1847ac40f5fSPoul-Henning Kamp .d_read = lptread,
1857ac40f5fSPoul-Henning Kamp .d_write = lptwrite,
1867ac40f5fSPoul-Henning Kamp .d_ioctl = lptioctl,
1877ac40f5fSPoul-Henning Kamp .d_name = LPT_NAME,
1884e2f199eSPoul-Henning Kamp };
189fdf94d1aSNicolas Souchu
190fdf94d1aSNicolas Souchu static int
lpt_request_ppbus(device_t dev,int how)1910f210c92SNicolas Souchu lpt_request_ppbus(device_t dev, int how)
192fdf94d1aSNicolas Souchu {
1930f210c92SNicolas Souchu device_t ppbus = device_get_parent(dev);
1940f210c92SNicolas Souchu struct lpt_data *sc = DEVTOSOFTC(dev);
195fdf94d1aSNicolas Souchu int error;
196fdf94d1aSNicolas Souchu
1972067d312SJohn Baldwin /*
1982067d312SJohn Baldwin * We might already have the bus for a write(2) after an interrupted
1992067d312SJohn Baldwin * write(2) call.
2002067d312SJohn Baldwin */
2012067d312SJohn Baldwin ppb_assert_locked(ppbus);
202fdf94d1aSNicolas Souchu if (sc->sc_state & HAVEBUS)
203fdf94d1aSNicolas Souchu return (0);
204fdf94d1aSNicolas Souchu
2052067d312SJohn Baldwin error = ppb_request_bus(ppbus, dev, how);
2062067d312SJohn Baldwin if (error == 0)
207fdf94d1aSNicolas Souchu sc->sc_state |= HAVEBUS;
208fdf94d1aSNicolas Souchu return (error);
209fdf94d1aSNicolas Souchu }
210fdf94d1aSNicolas Souchu
211fdf94d1aSNicolas Souchu static int
lpt_release_ppbus(device_t dev)2120f210c92SNicolas Souchu lpt_release_ppbus(device_t dev)
213fdf94d1aSNicolas Souchu {
2140f210c92SNicolas Souchu device_t ppbus = device_get_parent(dev);
2150f210c92SNicolas Souchu struct lpt_data *sc = DEVTOSOFTC(dev);
2160f210c92SNicolas Souchu int error = 0;
2170f210c92SNicolas Souchu
2182067d312SJohn Baldwin ppb_assert_locked(ppbus);
2192067d312SJohn Baldwin if (sc->sc_state & HAVEBUS) {
2202067d312SJohn Baldwin error = ppb_release_bus(ppbus, dev);
2212067d312SJohn Baldwin if (error == 0)
222fdf94d1aSNicolas Souchu sc->sc_state &= ~HAVEBUS;
2232067d312SJohn Baldwin }
2240f210c92SNicolas Souchu return (error);
225fdf94d1aSNicolas Souchu }
226fdf94d1aSNicolas Souchu
227fdf94d1aSNicolas Souchu /*
228fdf94d1aSNicolas Souchu * Internal routine to lptprobe to do port tests of one byte value
229fdf94d1aSNicolas Souchu */
230fdf94d1aSNicolas Souchu static int
lpt_port_test(device_t ppbus,u_char data,u_char mask)2310f210c92SNicolas Souchu lpt_port_test(device_t ppbus, u_char data, u_char mask)
232fdf94d1aSNicolas Souchu {
233fdf94d1aSNicolas Souchu int temp, timeout;
234fdf94d1aSNicolas Souchu
235fdf94d1aSNicolas Souchu data = data & mask;
2360f210c92SNicolas Souchu ppb_wdtr(ppbus, data);
237fdf94d1aSNicolas Souchu timeout = 10000;
238fdf94d1aSNicolas Souchu do {
239fdf94d1aSNicolas Souchu DELAY(10);
2400f210c92SNicolas Souchu temp = ppb_rdtr(ppbus) & mask;
241fdf94d1aSNicolas Souchu }
242fdf94d1aSNicolas Souchu while (temp != data && --timeout);
243fdf94d1aSNicolas Souchu lprintf(("out=%x\tin=%x\ttout=%d\n", data, temp, timeout));
244fdf94d1aSNicolas Souchu return (temp == data);
245fdf94d1aSNicolas Souchu }
246fdf94d1aSNicolas Souchu
247fdf94d1aSNicolas Souchu /*
248fdf94d1aSNicolas Souchu * Probe simplified by replacing multiple loops with a hardcoded
249fdf94d1aSNicolas Souchu * test pattern - 1999/02/08 des@freebsd.org
250fdf94d1aSNicolas Souchu *
251fdf94d1aSNicolas Souchu * New lpt port probe Geoff Rehmet - Rhodes University - 14/2/94
252fdf94d1aSNicolas Souchu * Based partially on Rod Grimes' printer probe
253fdf94d1aSNicolas Souchu *
254fdf94d1aSNicolas Souchu * Logic:
255fdf94d1aSNicolas Souchu * 1) If no port address was given, use the bios detected ports
256fdf94d1aSNicolas Souchu * and autodetect what ports the printers are on.
257fdf94d1aSNicolas Souchu * 2) Otherwise, probe the data port at the address given,
258fdf94d1aSNicolas Souchu * using the method in Rod Grimes' port probe.
259fdf94d1aSNicolas Souchu * (Much code ripped off directly from Rod's probe.)
260fdf94d1aSNicolas Souchu *
261fdf94d1aSNicolas Souchu * Comments from Rod's probe:
262fdf94d1aSNicolas Souchu * Logic:
263fdf94d1aSNicolas Souchu * 1) You should be able to write to and read back the same value
264fdf94d1aSNicolas Souchu * to the data port. Do an alternating zeros, alternating ones,
265fdf94d1aSNicolas Souchu * walking zero, and walking one test to check for stuck bits.
266fdf94d1aSNicolas Souchu *
267fdf94d1aSNicolas Souchu * 2) You should be able to write to and read back the same value
268fdf94d1aSNicolas Souchu * to the control port lower 5 bits, the upper 3 bits are reserved
2695aa0576bSEd Maste * per the IBM PC technical reference manuals and different boards
270fdf94d1aSNicolas Souchu * do different things with them. Do an alternating zeros, alternating
271fdf94d1aSNicolas Souchu * ones, walking zero, and walking one test to check for stuck bits.
272fdf94d1aSNicolas Souchu *
273fdf94d1aSNicolas Souchu * Some printers drag the strobe line down when the are powered off
274fdf94d1aSNicolas Souchu * so this bit has been masked out of the control port test.
275fdf94d1aSNicolas Souchu *
276fdf94d1aSNicolas Souchu * XXX Some printers may not like a fast pulse on init or strobe, I
277fdf94d1aSNicolas Souchu * don't know at this point, if that becomes a problem these bits
278fdf94d1aSNicolas Souchu * should be turned off in the mask byte for the control port test.
279fdf94d1aSNicolas Souchu *
280fdf94d1aSNicolas Souchu * We are finally left with a mask of 0x14, due to some printers
281fdf94d1aSNicolas Souchu * being adamant about holding other bits high ........
282fdf94d1aSNicolas Souchu *
283fdf94d1aSNicolas Souchu * Before probing the control port, we write a 0 to the data port -
284fdf94d1aSNicolas Souchu * If not, some printers chuck out garbage when the strobe line
285fdf94d1aSNicolas Souchu * gets toggled.
286fdf94d1aSNicolas Souchu *
287fdf94d1aSNicolas Souchu * 3) Set the data and control ports to a value of 0
288fdf94d1aSNicolas Souchu *
289fdf94d1aSNicolas Souchu * This probe routine has been tested on Epson Lx-800, HP LJ3P,
290fdf94d1aSNicolas Souchu * Epson FX-1170 and C.Itoh 8510RM
291fdf94d1aSNicolas Souchu * printers.
292fdf94d1aSNicolas Souchu * Quick exit on fail added.
293fdf94d1aSNicolas Souchu */
294fdf94d1aSNicolas Souchu static int
lpt_detect(device_t dev)2950f210c92SNicolas Souchu lpt_detect(device_t dev)
296fdf94d1aSNicolas Souchu {
2970f210c92SNicolas Souchu device_t ppbus = device_get_parent(dev);
2980f210c92SNicolas Souchu
299fdf94d1aSNicolas Souchu static u_char testbyte[18] = {
300fdf94d1aSNicolas Souchu 0x55, /* alternating zeros */
301fdf94d1aSNicolas Souchu 0xaa, /* alternating ones */
302fdf94d1aSNicolas Souchu 0xfe, 0xfd, 0xfb, 0xf7,
303fdf94d1aSNicolas Souchu 0xef, 0xdf, 0xbf, 0x7f, /* walking zero */
304fdf94d1aSNicolas Souchu 0x01, 0x02, 0x04, 0x08,
305fdf94d1aSNicolas Souchu 0x10, 0x20, 0x40, 0x80 /* walking one */
306fdf94d1aSNicolas Souchu };
307fdf94d1aSNicolas Souchu int i, error, status;
308fdf94d1aSNicolas Souchu
309fdf94d1aSNicolas Souchu status = 1; /* assume success */
310fdf94d1aSNicolas Souchu
3112067d312SJohn Baldwin ppb_lock(ppbus);
3120f210c92SNicolas Souchu if ((error = lpt_request_ppbus(dev, PPB_DONTWAIT))) {
3132067d312SJohn Baldwin ppb_unlock(ppbus);
3142067d312SJohn Baldwin device_printf(dev, "cannot alloc ppbus (%d)!\n", error);
3152067d312SJohn Baldwin return (0);
316fdf94d1aSNicolas Souchu }
317fdf94d1aSNicolas Souchu
318fdf94d1aSNicolas Souchu for (i = 0; i < 18 && status; i++)
3190f210c92SNicolas Souchu if (!lpt_port_test(ppbus, testbyte[i], 0xff)) {
320fdf94d1aSNicolas Souchu status = 0;
3212067d312SJohn Baldwin break;
322fdf94d1aSNicolas Souchu }
323fdf94d1aSNicolas Souchu
324fdf94d1aSNicolas Souchu /* write 0's to control and data ports */
3250f210c92SNicolas Souchu ppb_wdtr(ppbus, 0);
3260f210c92SNicolas Souchu ppb_wctr(ppbus, 0);
327fdf94d1aSNicolas Souchu
3280f210c92SNicolas Souchu lpt_release_ppbus(dev);
3292067d312SJohn Baldwin ppb_unlock(ppbus);
330fdf94d1aSNicolas Souchu
331fdf94d1aSNicolas Souchu return (status);
332fdf94d1aSNicolas Souchu }
333fdf94d1aSNicolas Souchu
3340f063508SPeter Wemm static void
lpt_identify(driver_t * driver,device_t parent)3350f063508SPeter Wemm lpt_identify(driver_t *driver, device_t parent)
3360f063508SPeter Wemm {
3370f063508SPeter Wemm
338a5c7e3bbSGuido van Rooij device_t dev;
339a5c7e3bbSGuido van Rooij
34057fb5e60SJohn Baldwin dev = device_find_child(parent, LPT_NAME, -1);
341a5c7e3bbSGuido van Rooij if (!dev)
342338cad62SBernd Walter BUS_ADD_CHILD(parent, 0, LPT_NAME, -1);
3430f063508SPeter Wemm }
3440f063508SPeter Wemm
345fdf94d1aSNicolas Souchu /*
3460f210c92SNicolas Souchu * lpt_probe()
347fdf94d1aSNicolas Souchu */
3480f210c92SNicolas Souchu static int
lpt_probe(device_t dev)3490f210c92SNicolas Souchu lpt_probe(device_t dev)
350fdf94d1aSNicolas Souchu {
351fdf94d1aSNicolas Souchu
3520f210c92SNicolas Souchu if (!lpt_detect(dev))
3530f210c92SNicolas Souchu return (ENXIO);
3540f210c92SNicolas Souchu
3550f210c92SNicolas Souchu device_set_desc(dev, "Printer");
3560f210c92SNicolas Souchu
357fdf94d1aSNicolas Souchu return (0);
358fdf94d1aSNicolas Souchu }
359fdf94d1aSNicolas Souchu
360fdf94d1aSNicolas Souchu static int
lpt_attach(device_t dev)3610f210c92SNicolas Souchu lpt_attach(device_t dev)
362fdf94d1aSNicolas Souchu {
3630f210c92SNicolas Souchu device_t ppbus = device_get_parent(dev);
3640f210c92SNicolas Souchu struct lpt_data *sc = DEVTOSOFTC(dev);
365ca3d3795SJohn Baldwin int rid = 0, unit = device_get_unit(dev);
366fdf94d1aSNicolas Souchu int error;
367fdf94d1aSNicolas Souchu
368fdf94d1aSNicolas Souchu sc->sc_primed = 0; /* not primed yet */
3692067d312SJohn Baldwin ppb_init_callout(ppbus, &sc->sc_timer, 0);
370fdf94d1aSNicolas Souchu
3712067d312SJohn Baldwin ppb_lock(ppbus);
3720f210c92SNicolas Souchu if ((error = lpt_request_ppbus(dev, PPB_DONTWAIT))) {
3732067d312SJohn Baldwin ppb_unlock(ppbus);
3742067d312SJohn Baldwin device_printf(dev, "cannot alloc ppbus (%d)!\n", error);
375fdf94d1aSNicolas Souchu return (0);
376fdf94d1aSNicolas Souchu }
377fdf94d1aSNicolas Souchu
3780f210c92SNicolas Souchu ppb_wctr(ppbus, LPC_NINIT);
3792067d312SJohn Baldwin lpt_release_ppbus(dev);
380192204feSJohn Baldwin ppb_unlock(ppbus);
3810f210c92SNicolas Souchu
3820f210c92SNicolas Souchu /* declare our interrupt handler */
3832067d312SJohn Baldwin sc->sc_intr_resource = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
384ca3d3795SJohn Baldwin RF_SHAREABLE);
3852067d312SJohn Baldwin if (sc->sc_intr_resource) {
3862067d312SJohn Baldwin error = bus_setup_intr(dev, sc->sc_intr_resource,
3872067d312SJohn Baldwin INTR_TYPE_TTY | INTR_MPSAFE, NULL, lptintr, sc,
3882067d312SJohn Baldwin &sc->sc_intr_cookie);
3892067d312SJohn Baldwin if (error) {
3902067d312SJohn Baldwin bus_release_resource(dev, SYS_RES_IRQ, rid,
3912067d312SJohn Baldwin sc->sc_intr_resource);
3922067d312SJohn Baldwin device_printf(dev,
3932067d312SJohn Baldwin "Unable to register interrupt handler\n");
3942067d312SJohn Baldwin return (error);
3952067d312SJohn Baldwin }
396fdf94d1aSNicolas Souchu sc->sc_irq = LP_HAS_IRQ | LP_USE_IRQ | LP_ENABLE_IRQ;
3970f210c92SNicolas Souchu device_printf(dev, "Interrupt-driven port\n");
398fdf94d1aSNicolas Souchu } else {
399fdf94d1aSNicolas Souchu sc->sc_irq = 0;
4000f210c92SNicolas Souchu device_printf(dev, "Polled port\n");
401fdf94d1aSNicolas Souchu }
402ca3d3795SJohn Baldwin lprintf(("irq %x\n", sc->sc_irq));
403fdf94d1aSNicolas Souchu
4042067d312SJohn Baldwin sc->sc_inbuf = malloc(BUFSIZE, M_DEVBUF, M_WAITOK);
4052067d312SJohn Baldwin sc->sc_statbuf = malloc(BUFSTATSIZE, M_DEVBUF, M_WAITOK);
4062067d312SJohn Baldwin sc->sc_dev = dev;
4072067d312SJohn Baldwin sc->sc_cdev = make_dev(&lpt_cdevsw, unit,
4080f210c92SNicolas Souchu UID_ROOT, GID_WHEEL, 0600, LPT_NAME "%d", unit);
4092067d312SJohn Baldwin sc->sc_cdev->si_drv1 = sc;
4102067d312SJohn Baldwin sc->sc_cdev->si_drv2 = 0;
4112067d312SJohn Baldwin sc->sc_cdev_bypass = make_dev(&lpt_cdevsw, unit,
4120f210c92SNicolas Souchu UID_ROOT, GID_WHEEL, 0600, LPT_NAME "%d.ctl", unit);
4132067d312SJohn Baldwin sc->sc_cdev_bypass->si_drv1 = sc;
4142067d312SJohn Baldwin sc->sc_cdev_bypass->si_drv2 = (void *)LP_BYPASS;
4150f210c92SNicolas Souchu return (0);
416fdf94d1aSNicolas Souchu }
417fdf94d1aSNicolas Souchu
418858a52f4SMitsuru IWASAKI static int
lpt_detach(device_t dev)419858a52f4SMitsuru IWASAKI lpt_detach(device_t dev)
420858a52f4SMitsuru IWASAKI {
421858a52f4SMitsuru IWASAKI struct lpt_data *sc = DEVTOSOFTC(dev);
4222067d312SJohn Baldwin device_t ppbus = device_get_parent(dev);
423858a52f4SMitsuru IWASAKI
4242067d312SJohn Baldwin destroy_dev(sc->sc_cdev);
4252067d312SJohn Baldwin destroy_dev(sc->sc_cdev_bypass);
4262067d312SJohn Baldwin ppb_lock(ppbus);
427858a52f4SMitsuru IWASAKI lpt_release_ppbus(dev);
4282067d312SJohn Baldwin ppb_unlock(ppbus);
4292067d312SJohn Baldwin callout_drain(&sc->sc_timer);
4302067d312SJohn Baldwin if (sc->sc_intr_resource != NULL) {
4312067d312SJohn Baldwin bus_teardown_intr(dev, sc->sc_intr_resource,
4322067d312SJohn Baldwin sc->sc_intr_cookie);
4332067d312SJohn Baldwin bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_intr_resource);
434858a52f4SMitsuru IWASAKI }
4352067d312SJohn Baldwin free(sc->sc_inbuf, M_DEVBUF);
4362067d312SJohn Baldwin free(sc->sc_statbuf, M_DEVBUF);
437858a52f4SMitsuru IWASAKI
438858a52f4SMitsuru IWASAKI return (0);
439858a52f4SMitsuru IWASAKI }
440858a52f4SMitsuru IWASAKI
441fdf94d1aSNicolas Souchu static void
lptout(void * arg)442fdf94d1aSNicolas Souchu lptout(void *arg)
443fdf94d1aSNicolas Souchu {
4442067d312SJohn Baldwin struct lpt_data *sc = arg;
4452067d312SJohn Baldwin device_t dev = sc->sc_dev;
4468fcf690bSJohn Baldwin device_t ppbus __unused;
447fdf94d1aSNicolas Souchu
448b373cc42SAttilio Rao ppbus = device_get_parent(dev);
4492067d312SJohn Baldwin ppb_assert_locked(ppbus);
4500f210c92SNicolas Souchu lprintf(("T %x ", ppb_rstr(ppbus)));
451fdf94d1aSNicolas Souchu if (sc->sc_state & OPEN) {
452fdf94d1aSNicolas Souchu sc->sc_backoff++;
453fdf94d1aSNicolas Souchu if (sc->sc_backoff > hz/LPTOUTMAX)
454432e1942SJohn Baldwin sc->sc_backoff = hz/LPTOUTMAX;
4552067d312SJohn Baldwin callout_reset(&sc->sc_timer, sc->sc_backoff, lptout, sc);
456fdf94d1aSNicolas Souchu } else
457fdf94d1aSNicolas Souchu sc->sc_state &= ~TOUT;
458fdf94d1aSNicolas Souchu
459fdf94d1aSNicolas Souchu if (sc->sc_state & EERROR)
460fdf94d1aSNicolas Souchu sc->sc_state &= ~EERROR;
461fdf94d1aSNicolas Souchu
462fdf94d1aSNicolas Souchu /*
4630f210c92SNicolas Souchu * Avoid possible hangs due to missed interrupts
464fdf94d1aSNicolas Souchu */
465fdf94d1aSNicolas Souchu if (sc->sc_xfercnt) {
4662067d312SJohn Baldwin lptintr(sc);
467fdf94d1aSNicolas Souchu } else {
468fdf94d1aSNicolas Souchu sc->sc_state &= ~OBUSY;
469521f364bSDag-Erling Smørgrav wakeup(dev);
470fdf94d1aSNicolas Souchu }
471fdf94d1aSNicolas Souchu }
472fdf94d1aSNicolas Souchu
473fdf94d1aSNicolas Souchu /*
474fdf94d1aSNicolas Souchu * lptopen -- reset the printer, then wait until it's selected and not busy.
475fdf94d1aSNicolas Souchu * If LP_BYPASS flag is selected, then we do not try to select the
476fdf94d1aSNicolas Souchu * printer -- this is just used for passing ioctls.
477fdf94d1aSNicolas Souchu */
478fdf94d1aSNicolas Souchu
479fdf94d1aSNicolas Souchu static int
lptopen(struct cdev * dev,int flags,int fmt,struct thread * td)48089c9c53dSPoul-Henning Kamp lptopen(struct cdev *dev, int flags, int fmt, struct thread *td)
481fdf94d1aSNicolas Souchu {
482fdf94d1aSNicolas Souchu int trys, err;
483ae6b868aSJohn Baldwin struct lpt_data *sc = dev->si_drv1;
484ef1be3b0SChristian Brueffer device_t lptdev;
485ef1be3b0SChristian Brueffer device_t ppbus;
486fdf94d1aSNicolas Souchu
4870f210c92SNicolas Souchu if (!sc)
488fdf94d1aSNicolas Souchu return (ENXIO);
489fdf94d1aSNicolas Souchu
490ef1be3b0SChristian Brueffer lptdev = sc->sc_dev;
491ef1be3b0SChristian Brueffer ppbus = device_get_parent(lptdev);
492ef1be3b0SChristian Brueffer
4932067d312SJohn Baldwin ppb_lock(ppbus);
494fdf94d1aSNicolas Souchu if (sc->sc_state) {
4952067d312SJohn Baldwin lprintf(("%s: still open %x\n", device_get_nameunit(lptdev),
4962067d312SJohn Baldwin sc->sc_state));
4972067d312SJohn Baldwin ppb_unlock(ppbus);
498fdf94d1aSNicolas Souchu return(EBUSY);
499fdf94d1aSNicolas Souchu } else
500fdf94d1aSNicolas Souchu sc->sc_state |= LPTINIT;
501fdf94d1aSNicolas Souchu
502f32b2a16SJohn Baldwin sc->sc_flags = (uintptr_t)dev->si_drv2;
503fdf94d1aSNicolas Souchu
504fdf94d1aSNicolas Souchu /* Check for open with BYPASS flag set. */
505fdf94d1aSNicolas Souchu if (sc->sc_flags & LP_BYPASS) {
506fdf94d1aSNicolas Souchu sc->sc_state = OPEN;
5072067d312SJohn Baldwin ppb_unlock(ppbus);
508fdf94d1aSNicolas Souchu return(0);
509fdf94d1aSNicolas Souchu }
510fdf94d1aSNicolas Souchu
511fdf94d1aSNicolas Souchu /* request the ppbus only if we don't have it already */
5120f210c92SNicolas Souchu if ((err = lpt_request_ppbus(lptdev, PPB_WAIT|PPB_INTR)) != 0) {
5130f210c92SNicolas Souchu /* give it a chance to try later */
5140f210c92SNicolas Souchu sc->sc_state = 0;
5152067d312SJohn Baldwin ppb_unlock(ppbus);
516fdf94d1aSNicolas Souchu return (err);
5170f210c92SNicolas Souchu }
518fdf94d1aSNicolas Souchu
5192067d312SJohn Baldwin lprintf(("%s flags 0x%x\n", device_get_nameunit(lptdev),
5202067d312SJohn Baldwin sc->sc_flags));
521fdf94d1aSNicolas Souchu
5220f210c92SNicolas Souchu /* set IRQ status according to ENABLE_IRQ flag
5230f210c92SNicolas Souchu */
524fdf94d1aSNicolas Souchu if (sc->sc_irq & LP_ENABLE_IRQ)
525fdf94d1aSNicolas Souchu sc->sc_irq |= LP_USE_IRQ;
526fdf94d1aSNicolas Souchu else
527fdf94d1aSNicolas Souchu sc->sc_irq &= ~LP_USE_IRQ;
528fdf94d1aSNicolas Souchu
529fdf94d1aSNicolas Souchu /* init printer */
530fdf94d1aSNicolas Souchu if ((sc->sc_flags & LP_NO_PRIME) == 0) {
531fdf94d1aSNicolas Souchu if ((sc->sc_flags & LP_PRIMEOPEN) || sc->sc_primed == 0) {
5320f210c92SNicolas Souchu ppb_wctr(ppbus, 0);
533fdf94d1aSNicolas Souchu sc->sc_primed++;
534fdf94d1aSNicolas Souchu DELAY(500);
535fdf94d1aSNicolas Souchu }
536fdf94d1aSNicolas Souchu }
537fdf94d1aSNicolas Souchu
5380f210c92SNicolas Souchu ppb_wctr(ppbus, LPC_SEL|LPC_NINIT);
539fdf94d1aSNicolas Souchu
540fdf94d1aSNicolas Souchu /* wait till ready (printer running diagnostics) */
541fdf94d1aSNicolas Souchu trys = 0;
542fdf94d1aSNicolas Souchu do {
543fdf94d1aSNicolas Souchu /* ran out of waiting for the printer */
544fdf94d1aSNicolas Souchu if (trys++ >= LPINITRDY*4) {
5450f210c92SNicolas Souchu lprintf(("status %x\n", ppb_rstr(ppbus)));
546fdf94d1aSNicolas Souchu
5470f210c92SNicolas Souchu lpt_release_ppbus(lptdev);
54884f9a0e5SJohn Baldwin sc->sc_state = 0;
5492067d312SJohn Baldwin ppb_unlock(ppbus);
550fdf94d1aSNicolas Souchu return (EBUSY);
551fdf94d1aSNicolas Souchu }
552fdf94d1aSNicolas Souchu
553fdf94d1aSNicolas Souchu /* wait 1/4 second, give up if we get a signal */
5542067d312SJohn Baldwin if (ppb_sleep(ppbus, lptdev, LPPRI | PCATCH, "lptinit",
5552067d312SJohn Baldwin hz / 4) != EWOULDBLOCK) {
5560f210c92SNicolas Souchu lpt_release_ppbus(lptdev);
55784f9a0e5SJohn Baldwin sc->sc_state = 0;
5582067d312SJohn Baldwin ppb_unlock(ppbus);
559fdf94d1aSNicolas Souchu return (EBUSY);
560fdf94d1aSNicolas Souchu }
561fdf94d1aSNicolas Souchu
562fdf94d1aSNicolas Souchu /* is printer online and ready for output */
5630f210c92SNicolas Souchu } while ((ppb_rstr(ppbus) &
564fdf94d1aSNicolas Souchu (LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR)) !=
565fdf94d1aSNicolas Souchu (LPS_SEL|LPS_NBSY|LPS_NERR));
566fdf94d1aSNicolas Souchu
567fdf94d1aSNicolas Souchu sc->sc_control = LPC_SEL|LPC_NINIT;
568fdf94d1aSNicolas Souchu if (sc->sc_flags & LP_AUTOLF)
569fdf94d1aSNicolas Souchu sc->sc_control |= LPC_AUTOL;
570fdf94d1aSNicolas Souchu
571fdf94d1aSNicolas Souchu /* enable interrupt if interrupt-driven */
572fdf94d1aSNicolas Souchu if (sc->sc_irq & LP_USE_IRQ)
573fdf94d1aSNicolas Souchu sc->sc_control |= LPC_ENA;
574fdf94d1aSNicolas Souchu
5750f210c92SNicolas Souchu ppb_wctr(ppbus, sc->sc_control);
576fdf94d1aSNicolas Souchu
57784f9a0e5SJohn Baldwin sc->sc_state &= ~LPTINIT;
57884f9a0e5SJohn Baldwin sc->sc_state |= OPEN;
579fdf94d1aSNicolas Souchu sc->sc_xfercnt = 0;
580fdf94d1aSNicolas Souchu
581fdf94d1aSNicolas Souchu /* only use timeout if using interrupt */
582fdf94d1aSNicolas Souchu lprintf(("irq %x\n", sc->sc_irq));
583fdf94d1aSNicolas Souchu if (sc->sc_irq & LP_USE_IRQ) {
584fdf94d1aSNicolas Souchu sc->sc_state |= TOUT;
5852067d312SJohn Baldwin sc->sc_backoff = hz / LPTOUTINITIAL;
5862067d312SJohn Baldwin callout_reset(&sc->sc_timer, sc->sc_backoff, lptout, sc);
587fdf94d1aSNicolas Souchu }
588fdf94d1aSNicolas Souchu
5892067d312SJohn Baldwin /* release the ppbus */
5902067d312SJohn Baldwin lpt_release_ppbus(lptdev);
5912067d312SJohn Baldwin ppb_unlock(ppbus);
5922067d312SJohn Baldwin
593fdf94d1aSNicolas Souchu lprintf(("opened.\n"));
594fdf94d1aSNicolas Souchu return(0);
595fdf94d1aSNicolas Souchu }
596fdf94d1aSNicolas Souchu
597fdf94d1aSNicolas Souchu /*
598fdf94d1aSNicolas Souchu * lptclose -- close the device, free the local line buffer.
599fdf94d1aSNicolas Souchu *
600fdf94d1aSNicolas Souchu * Check for interrupted write call added.
601fdf94d1aSNicolas Souchu */
602fdf94d1aSNicolas Souchu
603fdf94d1aSNicolas Souchu static int
lptclose(struct cdev * dev,int flags,int fmt,struct thread * td)60489c9c53dSPoul-Henning Kamp lptclose(struct cdev *dev, int flags, int fmt, struct thread *td)
605fdf94d1aSNicolas Souchu {
606ae6b868aSJohn Baldwin struct lpt_data *sc = dev->si_drv1;
6072067d312SJohn Baldwin device_t lptdev = sc->sc_dev;
6080f210c92SNicolas Souchu device_t ppbus = device_get_parent(lptdev);
609fdf94d1aSNicolas Souchu int err;
610fdf94d1aSNicolas Souchu
6112067d312SJohn Baldwin ppb_lock(ppbus);
61284f9a0e5SJohn Baldwin if (sc->sc_flags & LP_BYPASS)
613fdf94d1aSNicolas Souchu goto end_close;
614fdf94d1aSNicolas Souchu
6152067d312SJohn Baldwin if ((err = lpt_request_ppbus(lptdev, PPB_WAIT|PPB_INTR)) != 0) {
6162067d312SJohn Baldwin ppb_unlock(ppbus);
617fdf94d1aSNicolas Souchu return (err);
6182067d312SJohn Baldwin }
619fdf94d1aSNicolas Souchu
620fdf94d1aSNicolas Souchu /* if the last write was interrupted, don't complete it */
621fdf94d1aSNicolas Souchu if ((!(sc->sc_state & INTERRUPTED)) && (sc->sc_irq & LP_USE_IRQ))
6220f210c92SNicolas Souchu while ((ppb_rstr(ppbus) &
623fdf94d1aSNicolas Souchu (LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR)) !=
624fdf94d1aSNicolas Souchu (LPS_SEL|LPS_NBSY|LPS_NERR) || sc->sc_xfercnt)
6259599da5dSJohn Baldwin /* wait 1 second, give up if we get a signal */
6262067d312SJohn Baldwin if (ppb_sleep(ppbus, lptdev, LPPRI | PCATCH, "lpclose",
6272067d312SJohn Baldwin hz) != EWOULDBLOCK)
628fdf94d1aSNicolas Souchu break;
629fdf94d1aSNicolas Souchu
6302067d312SJohn Baldwin sc->sc_state &= ~OPEN;
6312067d312SJohn Baldwin callout_stop(&sc->sc_timer);
6320f210c92SNicolas Souchu ppb_wctr(ppbus, LPC_NINIT);
633fdf94d1aSNicolas Souchu
6342067d312SJohn Baldwin /*
6350f210c92SNicolas Souchu * unregistration of interrupt forced by release
6360f210c92SNicolas Souchu */
6370f210c92SNicolas Souchu lpt_release_ppbus(lptdev);
638fdf94d1aSNicolas Souchu
6392067d312SJohn Baldwin end_close:
64084f9a0e5SJohn Baldwin sc->sc_state = 0;
64184f9a0e5SJohn Baldwin sc->sc_xfercnt = 0;
64284f9a0e5SJohn Baldwin ppb_unlock(ppbus);
643fdf94d1aSNicolas Souchu lprintf(("closed.\n"));
644fdf94d1aSNicolas Souchu return(0);
645fdf94d1aSNicolas Souchu }
646fdf94d1aSNicolas Souchu
647fdf94d1aSNicolas Souchu /*
648fdf94d1aSNicolas Souchu * lpt_pushbytes()
649fdf94d1aSNicolas Souchu * Workhorse for actually spinning and writing bytes to printer
650fdf94d1aSNicolas Souchu * Derived from lpa.c
651fdf94d1aSNicolas Souchu * Originally by ?
652fdf94d1aSNicolas Souchu *
653fdf94d1aSNicolas Souchu * This code is only used when we are polling the port
654fdf94d1aSNicolas Souchu */
655fdf94d1aSNicolas Souchu static int
lpt_pushbytes(struct lpt_data * sc)6562067d312SJohn Baldwin lpt_pushbytes(struct lpt_data *sc)
657fdf94d1aSNicolas Souchu {
6582067d312SJohn Baldwin device_t dev = sc->sc_dev;
6590f210c92SNicolas Souchu device_t ppbus = device_get_parent(dev);
660fdf94d1aSNicolas Souchu int spin, err, tic;
661fdf94d1aSNicolas Souchu char ch;
662fdf94d1aSNicolas Souchu
6632067d312SJohn Baldwin ppb_assert_locked(ppbus);
664fdf94d1aSNicolas Souchu lprintf(("p"));
665fdf94d1aSNicolas Souchu /* loop for every character .. */
666fdf94d1aSNicolas Souchu while (sc->sc_xfercnt > 0) {
667fdf94d1aSNicolas Souchu /* printer data */
668fdf94d1aSNicolas Souchu ch = *(sc->sc_cp);
669fdf94d1aSNicolas Souchu sc->sc_cp++;
670fdf94d1aSNicolas Souchu sc->sc_xfercnt--;
671fdf94d1aSNicolas Souchu
672fdf94d1aSNicolas Souchu /*
673fdf94d1aSNicolas Souchu * Wait for printer ready.
674fdf94d1aSNicolas Souchu * Loop 20 usecs testing BUSY bit, then sleep
675fdf94d1aSNicolas Souchu * for exponentially increasing timeout. (vak)
676fdf94d1aSNicolas Souchu */
6770f210c92SNicolas Souchu for (spin = 0; NOT_READY(ppbus) && spin < MAX_SPIN; ++spin)
678fdf94d1aSNicolas Souchu DELAY(1); /* XXX delay is NOT this accurate! */
679fdf94d1aSNicolas Souchu if (spin >= MAX_SPIN) {
680fdf94d1aSNicolas Souchu tic = 0;
6810f210c92SNicolas Souchu while (NOT_READY(ppbus)) {
682fdf94d1aSNicolas Souchu /*
683fdf94d1aSNicolas Souchu * Now sleep, every cycle a
684fdf94d1aSNicolas Souchu * little longer ..
685fdf94d1aSNicolas Souchu */
686fdf94d1aSNicolas Souchu tic = tic + tic + 1;
687fdf94d1aSNicolas Souchu /*
688fdf94d1aSNicolas Souchu * But no more than 10 seconds. (vak)
689fdf94d1aSNicolas Souchu */
690fdf94d1aSNicolas Souchu if (tic > MAX_SLEEP)
691fdf94d1aSNicolas Souchu tic = MAX_SLEEP;
6922067d312SJohn Baldwin err = ppb_sleep(ppbus, dev, LPPRI,
693fdf94d1aSNicolas Souchu LPT_NAME "poll", tic);
694fdf94d1aSNicolas Souchu if (err != EWOULDBLOCK) {
695fdf94d1aSNicolas Souchu return (err);
696fdf94d1aSNicolas Souchu }
697fdf94d1aSNicolas Souchu }
698fdf94d1aSNicolas Souchu }
699fdf94d1aSNicolas Souchu
700fdf94d1aSNicolas Souchu /* output data */
7010f210c92SNicolas Souchu ppb_wdtr(ppbus, ch);
702fdf94d1aSNicolas Souchu /* strobe */
7030f210c92SNicolas Souchu ppb_wctr(ppbus, sc->sc_control|LPC_STB);
7040f210c92SNicolas Souchu ppb_wctr(ppbus, sc->sc_control);
705fdf94d1aSNicolas Souchu }
706fdf94d1aSNicolas Souchu return(0);
707fdf94d1aSNicolas Souchu }
708fdf94d1aSNicolas Souchu
709fdf94d1aSNicolas Souchu /*
710fdf94d1aSNicolas Souchu * lptread --retrieve printer status in IEEE1284 NIBBLE mode
711fdf94d1aSNicolas Souchu */
712fdf94d1aSNicolas Souchu
713fdf94d1aSNicolas Souchu static int
lptread(struct cdev * dev,struct uio * uio,int ioflag)71489c9c53dSPoul-Henning Kamp lptread(struct cdev *dev, struct uio *uio, int ioflag)
715fdf94d1aSNicolas Souchu {
716ae6b868aSJohn Baldwin struct lpt_data *sc = dev->si_drv1;
7172067d312SJohn Baldwin device_t lptdev = sc->sc_dev;
7180f210c92SNicolas Souchu device_t ppbus = device_get_parent(lptdev);
719fdf94d1aSNicolas Souchu int error = 0, len;
720fdf94d1aSNicolas Souchu
7213841bf81SBrian Feldman if (sc->sc_flags & LP_BYPASS) {
7223841bf81SBrian Feldman /* we can't do reads in bypass mode */
7233841bf81SBrian Feldman return (EPERM);
7243841bf81SBrian Feldman }
7253841bf81SBrian Feldman
7262067d312SJohn Baldwin ppb_lock(ppbus);
7272067d312SJohn Baldwin if ((error = ppb_1284_negociate(ppbus, PPB_NIBBLE, 0))) {
7282067d312SJohn Baldwin ppb_unlock(ppbus);
729fdf94d1aSNicolas Souchu return (error);
7302067d312SJohn Baldwin }
731fdf94d1aSNicolas Souchu
732fdf94d1aSNicolas Souchu /* read data in an other buffer, read/write may be simultaneous */
733fdf94d1aSNicolas Souchu len = 0;
734fdf94d1aSNicolas Souchu while (uio->uio_resid) {
7350f210c92SNicolas Souchu if ((error = ppb_1284_read(ppbus, PPB_NIBBLE,
7365856c958SPoul-Henning Kamp sc->sc_statbuf, min(BUFSTATSIZE,
737fdf94d1aSNicolas Souchu uio->uio_resid), &len))) {
738fdf94d1aSNicolas Souchu goto error;
739fdf94d1aSNicolas Souchu }
740fdf94d1aSNicolas Souchu
741fdf94d1aSNicolas Souchu if (!len)
742fdf94d1aSNicolas Souchu goto error; /* no more data */
743fdf94d1aSNicolas Souchu
7442067d312SJohn Baldwin ppb_unlock(ppbus);
7452067d312SJohn Baldwin error = uiomove(sc->sc_statbuf, len, uio);
7462067d312SJohn Baldwin ppb_lock(ppbus);
7472067d312SJohn Baldwin if (error)
748fdf94d1aSNicolas Souchu goto error;
749fdf94d1aSNicolas Souchu }
750fdf94d1aSNicolas Souchu
751fdf94d1aSNicolas Souchu error:
7520f210c92SNicolas Souchu ppb_1284_terminate(ppbus);
7532067d312SJohn Baldwin ppb_unlock(ppbus);
754fdf94d1aSNicolas Souchu return (error);
755fdf94d1aSNicolas Souchu }
756fdf94d1aSNicolas Souchu
757fdf94d1aSNicolas Souchu /*
758fdf94d1aSNicolas Souchu * lptwrite --copy a line from user space to a local buffer, then call
759fdf94d1aSNicolas Souchu * putc to get the chars moved to the output queue.
760fdf94d1aSNicolas Souchu *
761fdf94d1aSNicolas Souchu * Flagging of interrupted write added.
762fdf94d1aSNicolas Souchu */
763fdf94d1aSNicolas Souchu
764fdf94d1aSNicolas Souchu static int
lptwrite(struct cdev * dev,struct uio * uio,int ioflag)76589c9c53dSPoul-Henning Kamp lptwrite(struct cdev *dev, struct uio *uio, int ioflag)
766fdf94d1aSNicolas Souchu {
767fdf94d1aSNicolas Souchu register unsigned n;
7680f210c92SNicolas Souchu int err;
769ae6b868aSJohn Baldwin struct lpt_data *sc = dev->si_drv1;
7702067d312SJohn Baldwin device_t lptdev = sc->sc_dev;
7710f210c92SNicolas Souchu device_t ppbus = device_get_parent(lptdev);
772fdf94d1aSNicolas Souchu
773fdf94d1aSNicolas Souchu if (sc->sc_flags & LP_BYPASS) {
774fdf94d1aSNicolas Souchu /* we can't do writes in bypass mode */
775fdf94d1aSNicolas Souchu return (EPERM);
776fdf94d1aSNicolas Souchu }
777fdf94d1aSNicolas Souchu
778fdf94d1aSNicolas Souchu /* request the ppbus only if we don't have it already */
7792067d312SJohn Baldwin ppb_lock(ppbus);
7802067d312SJohn Baldwin if ((err = lpt_request_ppbus(lptdev, PPB_WAIT|PPB_INTR)) != 0) {
7812067d312SJohn Baldwin ppb_unlock(ppbus);
782fdf94d1aSNicolas Souchu return (err);
7830f210c92SNicolas Souchu }
7840f210c92SNicolas Souchu
785fdf94d1aSNicolas Souchu sc->sc_state &= ~INTERRUPTED;
786fdf94d1aSNicolas Souchu while ((n = min(BUFSIZE, uio->uio_resid)) != 0) {
7875856c958SPoul-Henning Kamp sc->sc_cp = sc->sc_inbuf;
7882067d312SJohn Baldwin ppb_unlock(ppbus);
7892067d312SJohn Baldwin err = uiomove(sc->sc_cp, n, uio);
7902067d312SJohn Baldwin ppb_lock(ppbus);
7912067d312SJohn Baldwin if (err)
7922067d312SJohn Baldwin break;
793fdf94d1aSNicolas Souchu sc->sc_xfercnt = n;
794fdf94d1aSNicolas Souchu
795fdf94d1aSNicolas Souchu if (sc->sc_irq & LP_ENABLE_EXT) {
796fdf94d1aSNicolas Souchu /* try any extended mode */
7970f210c92SNicolas Souchu err = ppb_write(ppbus, sc->sc_cp,
798fdf94d1aSNicolas Souchu sc->sc_xfercnt, 0);
799fdf94d1aSNicolas Souchu switch (err) {
800fdf94d1aSNicolas Souchu case 0:
801fdf94d1aSNicolas Souchu /* if not all data was sent, we could rely
802fdf94d1aSNicolas Souchu * on polling for the last bytes */
803fdf94d1aSNicolas Souchu sc->sc_xfercnt = 0;
804fdf94d1aSNicolas Souchu break;
805fdf94d1aSNicolas Souchu case EINTR:
806fdf94d1aSNicolas Souchu sc->sc_state |= INTERRUPTED;
8072067d312SJohn Baldwin ppb_unlock(ppbus);
808fdf94d1aSNicolas Souchu return (err);
809fdf94d1aSNicolas Souchu case EINVAL:
810fdf94d1aSNicolas Souchu /* advanced mode not avail */
811ae6b868aSJohn Baldwin log(LOG_NOTICE,
812ae6b868aSJohn Baldwin "%s: advanced mode not avail, polling\n",
8132067d312SJohn Baldwin device_get_nameunit(sc->sc_dev));
814fdf94d1aSNicolas Souchu break;
815fdf94d1aSNicolas Souchu default:
8162067d312SJohn Baldwin ppb_unlock(ppbus);
817fdf94d1aSNicolas Souchu return (err);
818fdf94d1aSNicolas Souchu }
819fdf94d1aSNicolas Souchu } else while ((sc->sc_xfercnt > 0)&&(sc->sc_irq & LP_USE_IRQ)) {
820fdf94d1aSNicolas Souchu lprintf(("i"));
821fdf94d1aSNicolas Souchu /* if the printer is ready for a char, */
822fdf94d1aSNicolas Souchu /* give it one */
823fdf94d1aSNicolas Souchu if ((sc->sc_state & OBUSY) == 0){
824fdf94d1aSNicolas Souchu lprintf(("\nC %d. ", sc->sc_xfercnt));
8252067d312SJohn Baldwin lptintr(sc);
826fdf94d1aSNicolas Souchu }
827fdf94d1aSNicolas Souchu lprintf(("W "));
828fdf94d1aSNicolas Souchu if (sc->sc_state & OBUSY)
8292067d312SJohn Baldwin if ((err = ppb_sleep(ppbus, lptdev,
830fdf94d1aSNicolas Souchu LPPRI|PCATCH, LPT_NAME "write", 0))) {
831fdf94d1aSNicolas Souchu sc->sc_state |= INTERRUPTED;
8322067d312SJohn Baldwin ppb_unlock(ppbus);
833fdf94d1aSNicolas Souchu return(err);
834fdf94d1aSNicolas Souchu }
835fdf94d1aSNicolas Souchu }
836fdf94d1aSNicolas Souchu
837fdf94d1aSNicolas Souchu /* check to see if we must do a polled write */
838fdf94d1aSNicolas Souchu if (!(sc->sc_irq & LP_USE_IRQ) && (sc->sc_xfercnt)) {
839fdf94d1aSNicolas Souchu lprintf(("p"));
840fdf94d1aSNicolas Souchu
8412067d312SJohn Baldwin err = lpt_pushbytes(sc);
842fdf94d1aSNicolas Souchu
8432067d312SJohn Baldwin if (err) {
8442067d312SJohn Baldwin ppb_unlock(ppbus);
845fdf94d1aSNicolas Souchu return (err);
846fdf94d1aSNicolas Souchu }
847fdf94d1aSNicolas Souchu }
8482067d312SJohn Baldwin }
849fdf94d1aSNicolas Souchu
850fdf94d1aSNicolas Souchu /* we have not been interrupted, release the ppbus */
8510f210c92SNicolas Souchu lpt_release_ppbus(lptdev);
8522067d312SJohn Baldwin ppb_unlock(ppbus);
853fdf94d1aSNicolas Souchu
8542067d312SJohn Baldwin return (err);
855fdf94d1aSNicolas Souchu }
856fdf94d1aSNicolas Souchu
857fdf94d1aSNicolas Souchu /*
8582067d312SJohn Baldwin * lptintr -- handle printer interrupts which occur when the printer is
859fdf94d1aSNicolas Souchu * ready to accept another char.
860fdf94d1aSNicolas Souchu *
861fdf94d1aSNicolas Souchu * do checking for interrupted write call.
862fdf94d1aSNicolas Souchu */
863fdf94d1aSNicolas Souchu static void
lptintr(void * arg)8642067d312SJohn Baldwin lptintr(void *arg)
865fdf94d1aSNicolas Souchu {
8662067d312SJohn Baldwin struct lpt_data *sc = arg;
8672067d312SJohn Baldwin device_t lptdev = sc->sc_dev;
8680f210c92SNicolas Souchu device_t ppbus = device_get_parent(lptdev);
869150630c4SDoug Rabson int sts = 0;
870fdf94d1aSNicolas Souchu int i;
871fdf94d1aSNicolas Souchu
872fdf94d1aSNicolas Souchu /*
873fdf94d1aSNicolas Souchu * Is printer online and ready for output?
874fdf94d1aSNicolas Souchu *
875fdf94d1aSNicolas Souchu * Avoid falling back to lptout() too quickly. First spin-loop
876fdf94d1aSNicolas Souchu * to see if the printer will become ready ``really soon now''.
877fdf94d1aSNicolas Souchu */
878fdf94d1aSNicolas Souchu for (i = 0; i < 100 &&
8790f210c92SNicolas Souchu ((sts=ppb_rstr(ppbus)) & RDY_MASK) != LP_READY; i++) ;
880fdf94d1aSNicolas Souchu
881fdf94d1aSNicolas Souchu if ((sts & RDY_MASK) == LP_READY) {
882fdf94d1aSNicolas Souchu sc->sc_state = (sc->sc_state | OBUSY) & ~EERROR;
883fdf94d1aSNicolas Souchu sc->sc_backoff = hz / LPTOUTINITIAL;
884fdf94d1aSNicolas Souchu
885fdf94d1aSNicolas Souchu if (sc->sc_xfercnt) {
886fdf94d1aSNicolas Souchu /* send char */
887fdf94d1aSNicolas Souchu /*lprintf(("%x ", *sc->sc_cp)); */
8880f210c92SNicolas Souchu ppb_wdtr(ppbus, *sc->sc_cp++) ;
8890f210c92SNicolas Souchu ppb_wctr(ppbus, sc->sc_control|LPC_STB);
890fdf94d1aSNicolas Souchu /* DELAY(X) */
8910f210c92SNicolas Souchu ppb_wctr(ppbus, sc->sc_control);
892fdf94d1aSNicolas Souchu
893fdf94d1aSNicolas Souchu /* any more data for printer */
894284c87f6SJohn Baldwin if (--(sc->sc_xfercnt) > 0)
895284c87f6SJohn Baldwin return;
896fdf94d1aSNicolas Souchu }
897fdf94d1aSNicolas Souchu
898fdf94d1aSNicolas Souchu /*
899fdf94d1aSNicolas Souchu * No more data waiting for printer.
900249a9688SNicolas Souchu * Wakeup is not done if write call was not interrupted.
901fdf94d1aSNicolas Souchu */
902fdf94d1aSNicolas Souchu sc->sc_state &= ~OBUSY;
903fdf94d1aSNicolas Souchu
904fdf94d1aSNicolas Souchu if (!(sc->sc_state & INTERRUPTED))
905521f364bSDag-Erling Smørgrav wakeup(lptdev);
906fdf94d1aSNicolas Souchu lprintf(("w "));
907fdf94d1aSNicolas Souchu return;
908fdf94d1aSNicolas Souchu } else { /* check for error */
909fdf94d1aSNicolas Souchu if (((sts & (LPS_NERR | LPS_OUT) ) != LPS_NERR) &&
910fdf94d1aSNicolas Souchu (sc->sc_state & OPEN))
911fdf94d1aSNicolas Souchu sc->sc_state |= EERROR;
912fdf94d1aSNicolas Souchu /* lptout() will jump in and try to restart. */
913fdf94d1aSNicolas Souchu }
914fdf94d1aSNicolas Souchu lprintf(("sts %x ", sts));
915fdf94d1aSNicolas Souchu }
916fdf94d1aSNicolas Souchu
917fdf94d1aSNicolas Souchu static int
lptioctl(struct cdev * dev,u_long cmd,caddr_t data,int flags,struct thread * td)91889c9c53dSPoul-Henning Kamp lptioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags, struct thread *td)
919fdf94d1aSNicolas Souchu {
920fdf94d1aSNicolas Souchu int error = 0;
921ae6b868aSJohn Baldwin struct lpt_data *sc = dev->si_drv1;
9222067d312SJohn Baldwin device_t ppbus;
923fdf94d1aSNicolas Souchu u_char old_sc_irq; /* old printer IRQ status */
924fdf94d1aSNicolas Souchu
925fdf94d1aSNicolas Souchu switch (cmd) {
926fdf94d1aSNicolas Souchu case LPT_IRQ :
9272067d312SJohn Baldwin ppbus = device_get_parent(sc->sc_dev);
9282067d312SJohn Baldwin ppb_lock(ppbus);
929fdf94d1aSNicolas Souchu if (sc->sc_irq & LP_HAS_IRQ) {
930fdf94d1aSNicolas Souchu /*
931fdf94d1aSNicolas Souchu * NOTE:
932fdf94d1aSNicolas Souchu * If the IRQ status is changed,
933fdf94d1aSNicolas Souchu * this will only be visible on the
934fdf94d1aSNicolas Souchu * next open.
935fdf94d1aSNicolas Souchu *
936fdf94d1aSNicolas Souchu * If interrupt status changes,
937fdf94d1aSNicolas Souchu * this gets syslog'd.
938fdf94d1aSNicolas Souchu */
939fdf94d1aSNicolas Souchu old_sc_irq = sc->sc_irq;
940fdf94d1aSNicolas Souchu switch (*(int*)data) {
941fdf94d1aSNicolas Souchu case 0:
942fdf94d1aSNicolas Souchu sc->sc_irq &= (~LP_ENABLE_IRQ);
943fdf94d1aSNicolas Souchu break;
944fdf94d1aSNicolas Souchu case 1:
945fdf94d1aSNicolas Souchu sc->sc_irq &= (~LP_ENABLE_EXT);
946fdf94d1aSNicolas Souchu sc->sc_irq |= LP_ENABLE_IRQ;
947fdf94d1aSNicolas Souchu break;
948fdf94d1aSNicolas Souchu case 2:
949fdf94d1aSNicolas Souchu /* classic irq based transfer and advanced
950fdf94d1aSNicolas Souchu * modes are in conflict
951fdf94d1aSNicolas Souchu */
952fdf94d1aSNicolas Souchu sc->sc_irq &= (~LP_ENABLE_IRQ);
953fdf94d1aSNicolas Souchu sc->sc_irq |= LP_ENABLE_EXT;
954fdf94d1aSNicolas Souchu break;
955fdf94d1aSNicolas Souchu case 3:
956fdf94d1aSNicolas Souchu sc->sc_irq &= (~LP_ENABLE_EXT);
957fdf94d1aSNicolas Souchu break;
958fdf94d1aSNicolas Souchu default:
959fdf94d1aSNicolas Souchu break;
960fdf94d1aSNicolas Souchu }
961fdf94d1aSNicolas Souchu
962fdf94d1aSNicolas Souchu if (old_sc_irq != sc->sc_irq )
963ae6b868aSJohn Baldwin log(LOG_NOTICE, "%s: switched to %s %s mode\n",
9642067d312SJohn Baldwin device_get_nameunit(sc->sc_dev),
965fdf94d1aSNicolas Souchu (sc->sc_irq & LP_ENABLE_IRQ)?
966fdf94d1aSNicolas Souchu "interrupt-driven":"polled",
967fdf94d1aSNicolas Souchu (sc->sc_irq & LP_ENABLE_EXT)?
968fdf94d1aSNicolas Souchu "extended":"standard");
969fdf94d1aSNicolas Souchu } else /* polled port */
970fdf94d1aSNicolas Souchu error = EOPNOTSUPP;
9712067d312SJohn Baldwin ppb_unlock(ppbus);
972fdf94d1aSNicolas Souchu break;
973fdf94d1aSNicolas Souchu default:
974fdf94d1aSNicolas Souchu error = ENODEV;
975fdf94d1aSNicolas Souchu }
976fdf94d1aSNicolas Souchu
977fdf94d1aSNicolas Souchu return(error);
978fdf94d1aSNicolas Souchu }
9790f210c92SNicolas Souchu
9800f063508SPeter Wemm static device_method_t lpt_methods[] = {
9810f063508SPeter Wemm /* device interface */
9820f063508SPeter Wemm DEVMETHOD(device_identify, lpt_identify),
9830f063508SPeter Wemm DEVMETHOD(device_probe, lpt_probe),
9840f063508SPeter Wemm DEVMETHOD(device_attach, lpt_attach),
985858a52f4SMitsuru IWASAKI DEVMETHOD(device_detach, lpt_detach),
9860f063508SPeter Wemm { 0, 0 }
9870f063508SPeter Wemm };
9880f063508SPeter Wemm
9890f063508SPeter Wemm static driver_t lpt_driver = {
9900f063508SPeter Wemm LPT_NAME,
9910f063508SPeter Wemm lpt_methods,
9920f063508SPeter Wemm sizeof(struct lpt_data),
9930f063508SPeter Wemm };
9940f063508SPeter Wemm
9956d588090SJohn Baldwin DRIVER_MODULE(lpt, ppbus, lpt_driver, 0, 0);
996f5fd5611SRuslan Ermilov MODULE_DEPEND(lpt, ppbus, 1, 1, 1);
997