olpt.c revision 58142
1/*
2 * Copyright (c) 1990 William F. Jolitz, TeleMuse
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This software is a component of "386BSD" developed by
16 *	William F. Jolitz, TeleMuse.
17 * 4. Neither the name of the developer nor the name "386BSD"
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS A COMPONENT OF 386BSD DEVELOPED BY WILLIAM F. JOLITZ
22 * AND IS INTENDED FOR RESEARCH AND EDUCATIONAL PURPOSES ONLY. THIS
23 * SOFTWARE SHOULD NOT BE CONSIDERED TO BE A COMMERCIAL PRODUCT.
24 * THE DEVELOPER URGES THAT USERS WHO REQUIRE A COMMERCIAL PRODUCT
25 * NOT MAKE USE OF THIS WORK.
26 *
27 * FOR USERS WHO WISH TO UNDERSTAND THE 386BSD SYSTEM DEVELOPED
28 * BY WILLIAM F. JOLITZ, WE RECOMMEND THE USER STUDY WRITTEN
29 * REFERENCES SUCH AS THE  "PORTING UNIX TO THE 386" SERIES
30 * (BEGINNING JANUARY 1991 "DR. DOBBS JOURNAL", USA AND BEGINNING
31 * JUNE 1991 "UNIX MAGAZIN", GERMANY) BY WILLIAM F. JOLITZ AND
32 * LYNNE GREER JOLITZ, AS WELL AS OTHER BOOKS ON UNIX AND THE
33 * ON-LINE 386BSD USER MANUAL BEFORE USE. A BOOK DISCUSSING THE INTERNALS
34 * OF 386BSD ENTITLED "386BSD FROM THE INSIDE OUT" WILL BE AVAILABLE LATE 1992.
35 *
36 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPER ``AS IS'' AND
37 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
39 * ARE DISCLAIMED.  IN NO EVENT SHALL THE DEVELOPER BE LIABLE
40 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
41 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
42 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
44 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
45 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46 * SUCH DAMAGE.
47 *
48 *	from: unknown origin, 386BSD 0.1
49 * $FreeBSD: head/sys/pc98/cbus/olpt.c 58142 2000-03-16 12:06:29Z nyan $
50 */
51
52/*
53 * Device Driver for AT parallel printer port
54 * Written by William Jolitz 12/18/90
55 */
56
57/*
58 * Parallel port TCP/IP interfaces added.  I looked at the driver from
59 * MACH but this is a complete rewrite, and btw. incompatible, and it
60 * should perform better too.  I have never run the MACH driver though.
61 *
62 * This driver sends two bytes (0x08, 0x00) in front of each packet,
63 * to allow us to distinguish another format later.
64 *
65 * Now added an Linux/Crynwr compatibility mode which is enabled using
66 * IF_LINK0 - Tim Wilkinson.
67 *
68 * TODO:
69 *    Make HDLC/PPP mode, use IF_LLC1 to enable.
70 *
71 * Connect the two computers using a Laplink parallel cable to use this
72 * feature:
73 *
74 *      +----------------------------------------+
75 * 	|A-name	A-End	B-End	Descr.	Port/Bit |
76 *      +----------------------------------------+
77 *	|DATA0	2	15	Data	0/0x01   |
78 *	|-ERROR	15	2	   	1/0x08   |
79 *      +----------------------------------------+
80 *	|DATA1	3	13	Data	0/0x02	 |
81 *	|+SLCT	13	3	   	1/0x10   |
82 *      +----------------------------------------+
83 *	|DATA2	4	12	Data	0/0x04   |
84 *	|+PE	12	4	   	1/0x20   |
85 *      +----------------------------------------+
86 *	|DATA3	5	10	Strobe	0/0x08   |
87 *	|-ACK	10	5	   	1/0x40   |
88 *      +----------------------------------------+
89 *	|DATA4	6	11	Data	0/0x10   |
90 *	|BUSY	11	6	   	1/~0x80  |
91 *      +----------------------------------------+
92 *	|GND	18-25	18-25	GND	-        |
93 *      +----------------------------------------+
94 *
95 * Expect transfer-rates up to 75 kbyte/sec.
96 *
97 * If GCC could correctly grok
98 *	register int port asm("edx")
99 * the code would be cleaner
100 *
101 * Poul-Henning Kamp <phk@freebsd.org>
102 */
103
104#include "olpt.h"
105#include "opt_inet.h"
106#ifdef PC98
107#undef INET	/* PLIP is not supported for old PC-98 */
108#define LPT_DRVINIT_AT_ATTACH	/* avoid conflicting with lpt on ppbus */
109#endif
110
111#include <sys/param.h>
112#include <sys/systm.h>
113#include <sys/conf.h>
114#include <sys/buf.h>
115#include <sys/kernel.h>
116#include <sys/uio.h>
117#include <sys/syslog.h>
118#include <machine/clock.h>
119#include <machine/lpt.h>
120
121#include <i386/isa/isa_device.h>
122#include <i386/isa/lptreg.h>
123
124#ifdef INET
125#include <sys/malloc.h>
126#include <sys/mbuf.h>
127#include <sys/socket.h>
128#include <sys/sockio.h>
129
130#include <net/if.h>
131#include <net/if_types.h>
132#include <net/netisr.h>
133#include <netinet/in.h>
134#include <netinet/in_var.h>
135#include <net/bpf.h>
136#endif /* INET */
137
138
139#define	LPINITRDY	4	/* wait up to 4 seconds for a ready */
140#define	LPTOUTINITIAL	10	/* initial timeout to wait for ready 1/10 s */
141#define	LPTOUTMAX	1	/* maximal timeout 1 s */
142#define	LPPRI		(PZERO+8)
143#define	BUFSIZE		1024
144
145#ifdef INET
146#ifndef LPMTU			/* MTU for the lp# interfaces */
147#define	LPMTU	1500
148#endif
149
150#ifndef LPMAXSPIN1		/* DELAY factor for the lp# interfaces */
151#define	LPMAXSPIN1	8000   /* Spinning for remote intr to happen */
152#endif
153
154#ifndef LPMAXSPIN2		/* DELAY factor for the lp# interfaces */
155#define	LPMAXSPIN2	500	/* Spinning for remote handshake to happen */
156#endif
157
158#ifndef LPMAXERRS		/* Max errors before !RUNNING */
159#define	LPMAXERRS	100
160#endif
161
162#define CLPIPHDRLEN	14	/* We send dummy ethernet addresses (two) + packet type in front of packet */
163#define	CLPIP_SHAKE	0x80	/* This bit toggles between nibble reception */
164#define MLPIPHDRLEN	CLPIPHDRLEN
165
166#define LPIPHDRLEN	2	/* We send 0x08, 0x00 in front of packet */
167#define	LPIP_SHAKE	0x40	/* This bit toggles between nibble reception */
168#if !defined(MLPIPHDRLEN) || LPIPHDRLEN > MLPIPHDRLEN
169#define MLPIPHDRLEN	LPIPHDRLEN
170#endif
171
172#define	LPIPTBLSIZE	256	/* Size of octet translation table */
173
174#endif /* INET */
175
176#ifndef PC98
177/* BIOS printer list - used by BIOS probe*/
178#define	BIOS_LPT_PORTS	0x408
179#define	BIOS_PORTS	(short *)(KERNBASE+BIOS_LPT_PORTS)
180#define	BIOS_MAX_LPT	4
181#endif
182
183
184#ifndef DEBUG
185#define lprintf(args)
186#else
187#define lprintf(args)	do {				\
188				if (lptflag)		\
189					printf args;	\
190			} while (0)
191static int volatile lptflag = 1;
192#endif
193
194#define	LPTUNIT(s)	((s)&0x03)
195#define	LPTFLAGS(s)	((s)&0xfc)
196
197static struct lpt_softc {
198	int	sc_port;
199	short	sc_state;
200	/* default case: negative prime, negative ack, handshake strobe,
201	   prime once */
202	u_char	sc_control;
203	char	sc_flags;
204#define LP_POS_INIT	0x04	/* if we are a postive init signal */
205#define LP_POS_ACK	0x08	/* if we are a positive going ack */
206#define LP_NO_PRIME	0x10	/* don't prime the printer at all */
207#define LP_PRIMEOPEN	0x20	/* prime on every open */
208#define LP_AUTOLF	0x40	/* tell printer to do an automatic lf */
209#define LP_BYPASS	0x80	/* bypass  printer ready checks */
210	struct	buf *sc_inbuf;
211	short	sc_xfercnt ;
212	char	sc_primed;
213	char	*sc_cp ;
214	u_char	sc_irq ;	/* IRQ status of port */
215#define LP_HAS_IRQ	0x01	/* we have an irq available */
216#define LP_USE_IRQ	0x02	/* we are using our irq */
217#define LP_ENABLE_IRQ	0x04	/* enable IRQ on open */
218	u_char	sc_backoff ;	/* time to call lptout() again */
219
220#ifdef INET
221	struct  ifnet	sc_if;
222	u_char		*sc_ifbuf;
223	int		sc_iferrs;
224#endif
225} lpt_sc[NOLPT] ;
226
227/* bits for state */
228#define	OPEN		(1<<0)	/* device is open */
229#define	ASLP		(1<<1)	/* awaiting draining of printer */
230#define	ERROR		(1<<2)	/* error was received from printer */
231#define	OBUSY		(1<<3)	/* printer is busy doing output */
232#define LPTOUT		(1<<4)	/* timeout while not selected */
233#define TOUT		(1<<5)	/* timeout while not selected */
234#define INIT		(1<<6)	/* waiting to initialize for open */
235#define INTERRUPTED	(1<<7)	/* write call was interrupted */
236
237
238/* status masks to interrogate printer status */
239#define RDY_MASK	(LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR)	/* ready ? */
240#define LP_READY	(LPS_SEL|LPS_NBSY|LPS_NERR)
241
242/* Printer Ready condition  - from lpa.c */
243/* Only used in polling code */
244#ifdef PC98
245#define	NOT_READY(x)	((inb(x) & LPS_NBSY) != LPS_NBSY)
246#else	/* IBM-PC */
247#define	LPS_INVERT	(LPS_NBSY | LPS_NACK |           LPS_SEL | LPS_NERR)
248#define	LPS_MASK	(LPS_NBSY | LPS_NACK | LPS_OUT | LPS_SEL | LPS_NERR)
249#define	NOT_READY(x)	((inb(x)^LPS_INVERT)&LPS_MASK)
250#endif
251
252#define	MAX_SLEEP	(hz*5)	/* Timeout while waiting for device ready */
253#define	MAX_SPIN	20	/* Max delay for device ready in usecs */
254
255static timeout_t lptout;
256static int	lptprobe (struct isa_device *dvp);
257static int	lptattach (struct isa_device *isdp);
258static ointhand2_t	lptintr;
259static void 	lpt_drvinit(void *unused);
260
261#ifdef INET
262
263/* Tables for the lp# interface */
264static u_char *txmith;
265#define txmitl (txmith+(1*LPIPTBLSIZE))
266#define trecvh (txmith+(2*LPIPTBLSIZE))
267#define trecvl (txmith+(3*LPIPTBLSIZE))
268
269static u_char *ctxmith;
270#define ctxmitl (ctxmith+(1*LPIPTBLSIZE))
271#define ctrecvh (ctxmith+(2*LPIPTBLSIZE))
272#define ctrecvl (ctxmith+(3*LPIPTBLSIZE))
273
274/* Functions for the lp# interface */
275static void lpattach(struct lpt_softc *,int);
276#ifndef PC98
277static int lpinittables(void);
278#endif
279static int lpioctl(struct ifnet *, u_long, caddr_t);
280static int lpoutput(struct ifnet *, struct mbuf *, struct sockaddr *,
281	struct rtentry *);
282static void lpintr(int);
283#endif /* INET */
284
285struct	isa_driver olptdriver = {
286	lptprobe, lptattach, "olpt"
287};
288
289static	d_open_t	lptopen;
290static	d_close_t	lptclose;
291static	d_write_t	lptwrite;
292static	d_ioctl_t	lptioctl;
293
294#define CDEV_MAJOR 16
295static struct cdevsw lpt_cdevsw = {
296	/* open */	lptopen,
297	/* close */	lptclose,
298	/* read */	noread,
299	/* write */	lptwrite,
300	/* ioctl */	lptioctl,
301	/* poll */	nopoll,
302	/* mmap */	nommap,
303	/* strategy */	nostrategy,
304	/* name */	"lpt",
305	/* maj */	CDEV_MAJOR,
306	/* dump */	nodump,
307	/* psize */	nopsize,
308	/* flags */	0,
309	/* bmaj */	-1
310};
311
312#ifndef PC98
313/*
314 * Internal routine to lptprobe to do port tests of one byte value
315 */
316static int
317lpt_port_test (int port, u_char data, u_char mask)
318{
319	int	temp, timeout;
320
321	data = data & mask;
322	outb(port, data);
323	timeout = 10000;
324	do {
325		DELAY(10);
326		temp = inb(port) & mask;
327	}
328	while (temp != data && --timeout);
329	lprintf(("Port 0x%x\tout=%x\tin=%x\ttout=%d\n",
330		port, data, temp, timeout));
331	return (temp == data);
332}
333#endif /* PC98 */
334
335/*
336 * New lpt port probe Geoff Rehmet - Rhodes University - 14/2/94
337 * Based partially on Rod Grimes' printer probe
338 *
339 * Logic:
340 *	1) If no port address was given, use the bios detected ports
341 *	   and autodetect what ports the printers are on.
342 *	2) Otherwise, probe the data port at the address given,
343 *	   using the method in Rod Grimes' port probe.
344 *	   (Much code ripped off directly from Rod's probe.)
345 *
346 * Comments from Rod's probe:
347 * Logic:
348 *	1) You should be able to write to and read back the same value
349 *	   to the data port.  Do an alternating zeros, alternating ones,
350 *	   walking zero, and walking one test to check for stuck bits.
351 *
352 *	2) You should be able to write to and read back the same value
353 *	   to the control port lower 5 bits, the upper 3 bits are reserved
354 *	   per the IBM PC technical reference manauls and different boards
355 *	   do different things with them.  Do an alternating zeros, alternating
356 *	   ones, walking zero, and walking one test to check for stuck bits.
357 *
358 *	   Some printers drag the strobe line down when the are powered off
359 * 	   so this bit has been masked out of the control port test.
360 *
361 *	   XXX Some printers may not like a fast pulse on init or strobe, I
362 *	   don't know at this point, if that becomes a problem these bits
363 *	   should be turned off in the mask byte for the control port test.
364 *
365 *	   We are finally left with a mask of 0x14, due to some printers
366 *	   being adamant about holding other bits high ........
367 *
368 *	   Before probing the control port, we write a 0 to the data port -
369 *	   If not, some printers chuck out garbage when the strobe line
370 *	   gets toggled.
371 *
372 *	3) Set the data and control ports to a value of 0
373 *
374 *	This probe routine has been tested on Epson Lx-800, HP LJ3P,
375 *	Epson FX-1170 and C.Itoh 8510RM
376 *	printers.
377 *	Quick exit on fail added.
378 */
379
380int
381lptprobe(struct isa_device *dvp)
382{
383#ifdef PC98
384#define PC98_OLD_LPT 0x40
385#define PC98_IEEE_1284_FUNCTION 0x149
386	unsigned int pc98_ieee_mode, tmp;
387
388	if (dvp->id_iobase == PC98_OLD_LPT) {
389		tmp = inb(PC98_IEEE_1284_FUNCTION);
390		pc98_ieee_mode = tmp;
391		if ((tmp & 0x10) == 0x10) {
392			outb(PC98_IEEE_1284_FUNCTION, tmp & ~0x10);
393			tmp = inb(PC98_IEEE_1284_FUNCTION);
394			if ((tmp & 0x10) != 0x10) {
395				outb(PC98_IEEE_1284_FUNCTION, pc98_ieee_mode);
396				return 0;
397			}
398		}
399	}
400	return 8;
401#else
402	int		port;
403	static short	next_bios_lpt = 0;
404	int		status;
405	static u_char	testbyte[18] = {
406		0x55,			/* alternating zeros */
407		0xaa,			/* alternating ones */
408		0xfe, 0xfd, 0xfb, 0xf7,
409		0xef, 0xdf, 0xbf, 0x7f,	/* walking zero */
410		0x01, 0x02, 0x04, 0x08,
411		0x10, 0x20, 0x40, 0x80	/* walking one */
412	};
413	int		i;
414
415	/*
416	 * Make sure there is some way for lptopen to see that
417	 * the port is not configured
418	 * This 0 will remain if the port isn't attached
419	 */
420	(lpt_sc + dvp->id_unit)->sc_port = 0;
421
422	status = IO_LPTSIZE;
423	/* If port not specified, use bios list */
424	if(dvp->id_iobase < 0) {	/* port? */
425		if((next_bios_lpt < BIOS_MAX_LPT) &&
426				(*(BIOS_PORTS+next_bios_lpt) != 0) ) {
427			dvp->id_iobase = *(BIOS_PORTS+next_bios_lpt++);
428			goto end_probe;
429		} else
430			return (0);
431	}
432
433	/* Port was explicitly specified */
434	/* This allows probing of ports unknown to the BIOS */
435	port = dvp->id_iobase + lpt_data;
436	for (i = 0; i < 18; i++) {
437		if (!lpt_port_test(port, testbyte[i], 0xff)) {
438			status = 0;
439			goto end_probe;
440		}
441	}
442
443end_probe:
444	/* write 0's to control and data ports */
445	outb(dvp->id_iobase+lpt_data, 0);
446	outb(dvp->id_iobase+lpt_control, 0);
447
448	return (status);
449#endif
450}
451
452/* XXX Todo - try and detect if interrupt is working */
453int
454lptattach(struct isa_device *isdp)
455{
456	struct	lpt_softc	*sc;
457	int	unit;
458
459	isdp->id_ointr = lptintr;
460	unit = isdp->id_unit;
461	sc = lpt_sc + unit;
462	sc->sc_port = isdp->id_iobase;
463	sc->sc_primed = 0;	/* not primed yet */
464#ifdef PC98
465	outb(sc->sc_port+lpt_pstb_ctrl,	LPC_DIS_PSTB);	/* PSTB disable */
466	outb(sc->sc_port+lpt_control,	LPC_MODE8255);	/* 8255 mode set */
467	outb(sc->sc_port+lpt_control,	LPC_NIRQ8);	/* IRQ8 inactive */
468	outb(sc->sc_port+lpt_control,	LPC_NPSTB);	/* PSTB inactive */
469	outb(sc->sc_port+lpt_pstb_ctrl,	LPC_EN_PSTB);	/* PSTB enable */
470#else
471	outb(sc->sc_port+lpt_control, LPC_NINIT);
472#endif
473
474	/* check if we can use interrupt */
475	lprintf(("oldirq %x\n", sc->sc_irq));
476	if (isdp->id_irq) {
477		sc->sc_irq = LP_HAS_IRQ | LP_USE_IRQ | LP_ENABLE_IRQ;
478		printf("lpt%d: Interrupt-driven port\n", unit);
479#ifdef INET
480		lpattach(sc, unit);
481#endif
482	} else {
483		sc->sc_irq = 0;
484		lprintf(("lpt%d: Polled port\n", unit));
485	}
486	lprintf(("irq %x\n", sc->sc_irq));
487
488	/* XXX what to do about the flags in the minor number? */
489	make_dev(&lpt_cdevsw, unit, UID_ROOT, GID_WHEEL, 0600, "lpt%d", unit);
490	make_dev(&lpt_cdevsw, unit | LP_BYPASS,
491			UID_ROOT, GID_WHEEL, 0600, "lpctl%d", unit);
492
493#ifdef LPT_DRVINIT_AT_ATTACH
494	lpt_drvinit(NULL);
495#endif
496	return (1);
497}
498
499/*
500 * lptopen -- reset the printer, then wait until it's selected and not busy.
501 *	If LP_BYPASS flag is selected, then we do not try to select the
502 *	printer -- this is just used for passing ioctls.
503 */
504
505static	int
506lptopen (dev_t dev, int flags, int fmt, struct proc *p)
507{
508	struct lpt_softc *sc;
509	int s;
510#ifdef PC98
511	int port;
512#else
513	int trys, port;
514#endif
515	u_int unit = LPTUNIT(minor(dev));
516
517	sc = lpt_sc + unit;
518	if ((unit >= NOLPT) || (sc->sc_port == 0))
519		return (ENXIO);
520
521#ifdef INET
522	if (sc->sc_if.if_flags & IFF_UP)
523		return(EBUSY);
524#endif
525
526	if (sc->sc_state) {
527		lprintf(("lp: still open %x\n", sc->sc_state));
528		return(EBUSY);
529	} else
530		sc->sc_state |= INIT;
531
532	sc->sc_flags = LPTFLAGS(minor(dev));
533
534	/* Check for open with BYPASS flag set. */
535	if (sc->sc_flags & LP_BYPASS) {
536		sc->sc_state = OPEN;
537		return(0);
538	}
539
540	s = spltty();
541	lprintf(("lp flags 0x%x\n", sc->sc_flags));
542	port = sc->sc_port;
543
544	/* set IRQ status according to ENABLE_IRQ flag */
545	if (sc->sc_irq & LP_ENABLE_IRQ)
546		sc->sc_irq |= LP_USE_IRQ;
547	else
548		sc->sc_irq &= ~LP_USE_IRQ;
549
550	/* init printer */
551#ifndef PC98
552	if ((sc->sc_flags & LP_NO_PRIME) == 0) {
553		if((sc->sc_flags & LP_PRIMEOPEN) || sc->sc_primed == 0) {
554			outb(port+lpt_control, 0);
555			sc->sc_primed++;
556			DELAY(500);
557		}
558	}
559
560	outb (port+lpt_control, LPC_SEL|LPC_NINIT);
561
562	/* wait till ready (printer running diagnostics) */
563	trys = 0;
564	do {
565		/* ran out of waiting for the printer */
566		if (trys++ >= LPINITRDY*4) {
567			splx(s);
568			sc->sc_state = 0;
569			lprintf(("status %x\n", inb(port+lpt_status)));
570			return (EBUSY);
571		}
572
573		/* wait 1/4 second, give up if we get a signal */
574		if (tsleep ((caddr_t)sc, LPPRI|PCATCH, "lptinit", hz/4) !=
575		    EWOULDBLOCK) {
576			sc->sc_state = 0;
577			splx(s);
578			return (EBUSY);
579		}
580
581		/* is printer online and ready for output */
582	} while ((inb(port+lpt_status) & (LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR)) !=
583		 (LPS_SEL|LPS_NBSY|LPS_NERR));
584
585	sc->sc_control = LPC_SEL|LPC_NINIT;
586	if (sc->sc_flags & LP_AUTOLF)
587		sc->sc_control |= LPC_AUTOL;
588
589	/* enable interrupt if interrupt-driven */
590	if (sc->sc_irq & LP_USE_IRQ)
591		sc->sc_control |= LPC_ENA;
592
593	outb(port+lpt_control, sc->sc_control);
594#endif
595
596	sc->sc_state = OPEN;
597	sc->sc_inbuf = geteblk(BUFSIZE);
598	sc->sc_xfercnt = 0;
599	splx(s);
600
601	/* only use timeout if using interrupt */
602	lprintf(("irq %x\n", sc->sc_irq));
603	if (sc->sc_irq & LP_USE_IRQ) {
604		sc->sc_state |= TOUT;
605		timeout (lptout, (caddr_t)sc,
606			 (sc->sc_backoff = hz/LPTOUTINITIAL));
607	}
608
609	lprintf(("opened.\n"));
610	return(0);
611}
612
613static void
614lptout (void *arg)
615{
616	struct lpt_softc *sc = arg;
617	int pl;
618
619	lprintf(("T %x ", inb(sc->sc_port+lpt_status)));
620	if (sc->sc_state & OPEN) {
621		sc->sc_backoff++;
622		if (sc->sc_backoff > hz/LPTOUTMAX)
623			sc->sc_backoff = sc->sc_backoff > hz/LPTOUTMAX;
624		timeout (lptout, (caddr_t)sc, sc->sc_backoff);
625	} else
626		sc->sc_state &= ~TOUT;
627
628	if (sc->sc_state & ERROR)
629		sc->sc_state &= ~ERROR;
630
631	/*
632	 * Avoid possible hangs do to missed interrupts
633	 */
634	if (sc->sc_xfercnt) {
635		pl = spltty();
636		lptintr(sc - lpt_sc);
637		splx(pl);
638	} else {
639		sc->sc_state &= ~OBUSY;
640		wakeup((caddr_t)sc);
641	}
642}
643
644/*
645 * lptclose -- close the device, free the local line buffer.
646 *
647 * Check for interrupted write call added.
648 */
649
650static	int
651lptclose(dev_t dev, int flags, int fmt, struct proc *p)
652{
653	struct lpt_softc *sc = lpt_sc + LPTUNIT(minor(dev));
654#ifndef PC98
655	int port = sc->sc_port;
656#endif
657
658	if(sc->sc_flags & LP_BYPASS)
659		goto end_close;
660
661	sc->sc_state &= ~OPEN;
662
663#ifndef PC98
664	/* if the last write was interrupted, don't complete it */
665	if((!(sc->sc_state  & INTERRUPTED)) && (sc->sc_irq & LP_USE_IRQ))
666		while ((inb(port+lpt_status) & (LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR)) !=
667			(LPS_SEL|LPS_NBSY|LPS_NERR) || sc->sc_xfercnt)
668			/* wait 1/4 second, give up if we get a signal */
669			if (tsleep ((caddr_t)sc, LPPRI|PCATCH,
670				"lpclose", hz) != EWOULDBLOCK)
671				break;
672
673	outb(sc->sc_port+lpt_control, LPC_NINIT);
674#endif
675	brelse(sc->sc_inbuf);
676
677end_close:
678	sc->sc_state = 0;
679	sc->sc_xfercnt = 0;
680	lprintf(("closed.\n"));
681	return(0);
682}
683
684/*
685 * pushbytes()
686 *	Workhorse for actually spinning and writing bytes to printer
687 *	Derived from lpa.c
688 *	Originally by ?
689 *
690 *	This code is only used when we are polling the port
691 */
692static int
693pushbytes(struct lpt_softc * sc)
694{
695	int spin, err, tic;
696	char ch;
697	int port = sc->sc_port;
698
699	lprintf(("p"));
700	/* loop for every character .. */
701	while (sc->sc_xfercnt > 0) {
702		/* printer data */
703		ch = *(sc->sc_cp);
704		sc->sc_cp++;
705		sc->sc_xfercnt--;
706
707		/*
708		 * Wait for printer ready.
709		 * Loop 20 usecs testing BUSY bit, then sleep
710		 * for exponentially increasing timeout. (vak)
711		 */
712		for (spin=0; NOT_READY(port+lpt_status) && spin<MAX_SPIN; ++spin)
713			DELAY(1);	/* XXX delay is NOT this accurate! */
714		if (spin >= MAX_SPIN) {
715			tic = 0;
716			while (NOT_READY(port+lpt_status)) {
717				/*
718				 * Now sleep, every cycle a
719				 * little longer ..
720				 */
721				tic = tic + tic + 1;
722				/*
723				 * But no more than 10 seconds. (vak)
724				 */
725				if (tic > MAX_SLEEP)
726					tic = MAX_SLEEP;
727				err = tsleep((caddr_t)sc, LPPRI,
728					"lptpoll", tic);
729				if (err != EWOULDBLOCK) {
730					return (err);
731				}
732			}
733		}
734
735		/* output data */
736		outb(port+lpt_data, ch);
737#ifdef PC98
738		DELAY(1);
739		outb(port+lpt_control, LPC_PSTB);
740		DELAY(1);
741		outb(port+lpt_control, LPC_NPSTB);
742#else
743		/* strobe */
744		outb(port+lpt_control, sc->sc_control|LPC_STB);
745		outb(port+lpt_control, sc->sc_control);
746#endif
747
748	}
749	return(0);
750}
751
752/*
753 * lptwrite --copy a line from user space to a local buffer, then call
754 * putc to get the chars moved to the output queue.
755 *
756 * Flagging of interrupted write added.
757 */
758
759static	int
760lptwrite(dev_t dev, struct uio * uio, int ioflag)
761{
762	register unsigned n;
763	int pl, err;
764	struct lpt_softc *sc = lpt_sc + LPTUNIT(minor(dev));
765
766	if(sc->sc_flags & LP_BYPASS) {
767		/* we can't do writes in bypass mode */
768		return(EPERM);
769	}
770
771	sc->sc_state &= ~INTERRUPTED;
772	while ((n = min(BUFSIZE, uio->uio_resid)) != 0) {
773		sc->sc_cp = sc->sc_inbuf->b_data ;
774		uiomove(sc->sc_cp, n, uio);
775		sc->sc_xfercnt = n ;
776		while ((sc->sc_xfercnt > 0)&&(sc->sc_irq & LP_USE_IRQ)) {
777			lprintf(("i"));
778			/* if the printer is ready for a char, */
779			/* give it one */
780			if ((sc->sc_state & OBUSY) == 0){
781				lprintf(("\nC %d. ", sc->sc_xfercnt));
782				pl = spltty();
783				lptintr(sc - lpt_sc);
784				(void) splx(pl);
785			}
786			lprintf(("W "));
787			if (sc->sc_state & OBUSY)
788				if ((err = tsleep ((caddr_t)sc,
789					 LPPRI|PCATCH, "lpwrite", 0))) {
790					sc->sc_state |= INTERRUPTED;
791					return(err);
792				}
793		}
794		/* check to see if we must do a polled write */
795		if(!(sc->sc_irq & LP_USE_IRQ) && (sc->sc_xfercnt)) {
796			lprintf(("p"));
797			if((err = pushbytes(sc)))
798				return(err);
799		}
800	}
801	return(0);
802}
803
804/*
805 * lptintr -- handle printer interrupts which occur when the printer is
806 * ready to accept another char.
807 *
808 * do checking for interrupted write call.
809 */
810
811static void
812lptintr(int unit)
813{
814#if defined(INET) || !defined(PC98)
815	struct lpt_softc *sc = lpt_sc + unit;
816#endif
817#ifndef PC98
818	int port = sc->sc_port, sts;
819	int i;
820#endif
821
822#ifdef INET
823	if(sc->sc_if.if_flags & IFF_UP) {
824	    lpintr(unit);
825	    return;
826	}
827#endif /* INET */
828
829#ifndef PC98
830	/*
831	 * Is printer online and ready for output?
832	 *
833	 * Avoid falling back to lptout() too quickly.  First spin-loop
834	 * to see if the printer will become ready ``really soon now''.
835	 */
836	for (i = 0;
837	     i < 100 &&
838	     ((sts=inb(port+lpt_status)) & RDY_MASK) != LP_READY;
839	     i++) ;
840	if ((sts & RDY_MASK) == LP_READY) {
841		sc->sc_state = (sc->sc_state | OBUSY) & ~ERROR;
842		sc->sc_backoff = hz/LPTOUTINITIAL;
843
844		if (sc->sc_xfercnt) {
845			/* send char */
846			/*lprintf(("%x ", *sc->sc_cp)); */
847			outb(port+lpt_data, *sc->sc_cp++) ;
848			outb(port+lpt_control, sc->sc_control|LPC_STB);
849			/* DELAY(X) */
850			outb(port+lpt_control, sc->sc_control);
851
852			/* any more data for printer */
853			if(--(sc->sc_xfercnt) > 0) return;
854		}
855
856		/*
857		 * No more data waiting for printer.
858		 * Wakeup is not done if write call was interrupted.
859		 */
860		sc->sc_state &= ~OBUSY;
861		if(!(sc->sc_state & INTERRUPTED))
862			wakeup((caddr_t)sc);
863		lprintf(("w "));
864		return;
865	} else	{	/* check for error */
866		if(((sts & (LPS_NERR | LPS_OUT) ) != LPS_NERR) &&
867				(sc->sc_state & OPEN))
868			sc->sc_state |= ERROR;
869		/* lptout() will jump in and try to restart. */
870	}
871#endif
872	lprintf(("sts %x ", sts));
873}
874
875static	int
876lptioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct proc *p)
877{
878	int	error = 0;
879        struct	lpt_softc *sc;
880        u_int	unit = LPTUNIT(minor(dev));
881	u_char	old_sc_irq;	/* old printer IRQ status */
882
883        sc = lpt_sc + unit;
884
885	switch (cmd) {
886	case LPT_IRQ :
887		if(sc->sc_irq & LP_HAS_IRQ) {
888			/*
889			 * NOTE:
890			 * If the IRQ status is changed,
891			 * this will only be visible on the
892			 * next open.
893			 *
894			 * If interrupt status changes,
895			 * this gets syslog'd.
896			 */
897			old_sc_irq = sc->sc_irq;
898			if(*(int*)data == 0)
899				sc->sc_irq &= (~LP_ENABLE_IRQ);
900			else
901				sc->sc_irq |= LP_ENABLE_IRQ;
902			if (old_sc_irq != sc->sc_irq )
903				log(LOG_NOTICE, "lpt%c switched to %s mode\n",
904					(char)unit+'0',
905					(sc->sc_irq & LP_ENABLE_IRQ)?
906					"interrupt-driven":"polled");
907		} else /* polled port */
908			error = EOPNOTSUPP;
909		break;
910	default:
911		error = ENODEV;
912	}
913
914	return(error);
915}
916
917#ifdef INET
918
919static void
920lpattach (struct lpt_softc *sc, int unit)
921{
922	struct ifnet *ifp = &sc->sc_if;
923
924	ifp->if_softc = sc;
925	ifp->if_name = "lp";
926	ifp->if_unit = unit;
927	ifp->if_mtu = LPMTU;
928	ifp->if_flags = IFF_SIMPLEX | IFF_POINTOPOINT | IFF_MULTICAST;
929	ifp->if_ioctl = lpioctl;
930	ifp->if_output = lpoutput;
931	ifp->if_type = IFT_PARA;
932	ifp->if_hdrlen = 0;
933	ifp->if_addrlen = 0;
934	ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
935	if_attach(ifp);
936	printf("lp%d: TCP/IP capable interface\n", unit);
937
938	bpfattach(ifp, DLT_NULL, LPIPHDRLEN);
939}
940
941#ifndef PC98
942/*
943 * Build the translation tables for the LPIP (BSD unix) protocol.
944 * We don't want to calculate these nasties in our tight loop, so we
945 * precalculate them when we initialize.
946 */
947static int
948lpinittables (void)
949{
950    int i;
951
952    if (!txmith)
953	txmith = malloc(4*LPIPTBLSIZE, M_DEVBUF, M_NOWAIT);
954
955    if (!txmith)
956	return 1;
957
958    if (!ctxmith)
959	ctxmith = malloc(4*LPIPTBLSIZE, M_DEVBUF, M_NOWAIT);
960
961    if (!ctxmith)
962	return 1;
963
964    for (i=0; i < LPIPTBLSIZE; i++) {
965	ctxmith[i] = (i & 0xF0) >> 4;
966	ctxmitl[i] = 0x10 | (i & 0x0F);
967	ctrecvh[i] = (i & 0x78) << 1;
968	ctrecvl[i] = (i & 0x78) >> 3;
969    }
970
971    for (i=0; i < LPIPTBLSIZE; i++) {
972	txmith[i] = ((i & 0x80) >> 3) | ((i & 0x70) >> 4) | 0x08;
973	txmitl[i] = ((i & 0x08) << 1) | (i & 0x07);
974	trecvh[i] = ((~i) & 0x80) | ((i & 0x38) << 1);
975	trecvl[i] = (((~i) & 0x80) >> 4) | ((i & 0x38) >> 3);
976    }
977
978    return 0;
979}
980#endif /* PC98 */
981
982/*
983 * Process an ioctl request.
984 */
985
986static int
987lpioctl (struct ifnet *ifp, u_long cmd, caddr_t data)
988{
989    struct lpt_softc *sc = lpt_sc + ifp->if_unit;
990    struct ifaddr *ifa = (struct ifaddr *)data;
991    struct ifreq *ifr = (struct ifreq *)data;
992    u_char *ptr;
993
994    switch (cmd) {
995
996    case SIOCSIFDSTADDR:
997    case SIOCAIFADDR:
998    case SIOCSIFADDR:
999	if (ifa->ifa_addr->sa_family != AF_INET)
1000	    return EAFNOSUPPORT;
1001	ifp->if_flags |= IFF_UP;
1002	/* FALLTHROUGH */
1003    case SIOCSIFFLAGS:
1004	if ((!(ifp->if_flags & IFF_UP)) && (ifp->if_flags & IFF_RUNNING)) {
1005	    outb(sc->sc_port + lpt_control, 0x00);
1006	    ifp->if_flags &= ~IFF_RUNNING;
1007	    break;
1008	}
1009#ifdef PC98
1010	/* XXX */
1011	return ENOBUFS;
1012#else
1013	if (((ifp->if_flags & IFF_UP)) && (!(ifp->if_flags & IFF_RUNNING))) {
1014	    if (lpinittables())
1015		return ENOBUFS;
1016	    sc->sc_ifbuf = malloc(sc->sc_if.if_mtu + MLPIPHDRLEN,
1017				  M_DEVBUF, M_WAITOK);
1018	    if (!sc->sc_ifbuf)
1019		return ENOBUFS;
1020
1021	    outb(sc->sc_port + lpt_control, LPC_ENA);
1022	    ifp->if_flags |= IFF_RUNNING;
1023	}
1024	break;
1025#endif
1026    case SIOCSIFMTU:
1027	ptr = sc->sc_ifbuf;
1028	sc->sc_ifbuf = malloc(ifr->ifr_mtu+MLPIPHDRLEN, M_DEVBUF, M_NOWAIT);
1029	if (!sc->sc_ifbuf) {
1030	    sc->sc_ifbuf = ptr;
1031	    return ENOBUFS;
1032	}
1033	if (ptr)
1034	    free(ptr,M_DEVBUF);
1035	sc->sc_if.if_mtu = ifr->ifr_mtu;
1036	break;
1037
1038    case SIOCGIFMTU:
1039	ifr->ifr_mtu = sc->sc_if.if_mtu;
1040	break;
1041
1042    case SIOCADDMULTI:
1043    case SIOCDELMULTI:
1044	if (ifr == 0) {
1045	    return EAFNOSUPPORT;		/* XXX */
1046	}
1047	switch (ifr->ifr_addr.sa_family) {
1048
1049#ifdef INET
1050	case AF_INET:
1051	    break;
1052#endif
1053
1054	default:
1055	    return EAFNOSUPPORT;
1056	}
1057	break;
1058
1059    default:
1060	lprintf(("LP:ioctl(0x%lx)\n", cmd));
1061	return EINVAL;
1062    }
1063    return 0;
1064}
1065
1066static __inline int
1067clpoutbyte (u_char byte, int spin, int data_port, int status_port)
1068{
1069	outb(data_port, ctxmitl[byte]);
1070	while (inb(status_port) & CLPIP_SHAKE)
1071		if (--spin == 0) {
1072			return 1;
1073		}
1074	outb(data_port, ctxmith[byte]);
1075	while (!(inb(status_port) & CLPIP_SHAKE))
1076		if (--spin == 0) {
1077			return 1;
1078		}
1079	return 0;
1080}
1081
1082static __inline int
1083clpinbyte (int spin, int data_port, int status_port)
1084{
1085	int c, cl;
1086
1087	while((inb(status_port) & CLPIP_SHAKE))
1088	    if(!--spin) {
1089		return -1;
1090	    }
1091	cl = inb(status_port);
1092	outb(data_port, 0x10);
1093
1094	while(!(inb(status_port) & CLPIP_SHAKE))
1095	    if(!--spin) {
1096		return -1;
1097	    }
1098	c = inb(status_port);
1099	outb(data_port, 0x00);
1100
1101	return (ctrecvl[cl] | ctrecvh[c]);
1102}
1103
1104static void
1105lpintr (int unit)
1106{
1107	struct   lpt_softc *sc = lpt_sc + unit;
1108	register int lpt_data_port = sc->sc_port + lpt_data;
1109	register int lpt_stat_port = sc->sc_port + lpt_status;
1110		 int lpt_ctrl_port = sc->sc_port + lpt_control;
1111	int len, s, j;
1112	u_char *bp;
1113	u_char c, cl;
1114	struct mbuf *top;
1115
1116	s = splhigh();
1117
1118	if (sc->sc_if.if_flags & IFF_LINK0) {
1119
1120	    /* Ack. the request */
1121	    outb(lpt_data_port, 0x01);
1122
1123	    /* Get the packet length */
1124	    j = clpinbyte(LPMAXSPIN2, lpt_data_port, lpt_stat_port);
1125	    if (j == -1)
1126		goto err;
1127	    len = j;
1128	    j = clpinbyte(LPMAXSPIN2, lpt_data_port, lpt_stat_port);
1129	    if (j == -1)
1130		goto err;
1131	    len = len + (j << 8);
1132	    if (len > sc->sc_if.if_mtu + MLPIPHDRLEN)
1133		goto err;
1134
1135	    bp  = sc->sc_ifbuf;
1136
1137	    while (len--) {
1138	        j = clpinbyte(LPMAXSPIN2, lpt_data_port, lpt_stat_port);
1139	        if (j == -1) {
1140		    goto err;
1141	        }
1142	        *bp++ = j;
1143	    }
1144	    /* Get and ignore checksum */
1145	    j = clpinbyte(LPMAXSPIN2, lpt_data_port, lpt_stat_port);
1146	    if (j == -1) {
1147	        goto err;
1148	    }
1149
1150	    len = bp - sc->sc_ifbuf;
1151	    if (len <= CLPIPHDRLEN)
1152	        goto err;
1153
1154	    sc->sc_iferrs = 0;
1155
1156	    if (IF_QFULL(&ipintrq)) {
1157	        lprintf(("DROP"));
1158	        IF_DROP(&ipintrq);
1159		goto done;
1160	    }
1161	    len -= CLPIPHDRLEN;
1162	    sc->sc_if.if_ipackets++;
1163	    sc->sc_if.if_ibytes += len;
1164	    top = m_devget(sc->sc_ifbuf + CLPIPHDRLEN, len, 0, &sc->sc_if, 0);
1165	    if (top) {
1166	        IF_ENQUEUE(&ipintrq, top);
1167	        schednetisr(NETISR_IP);
1168	    }
1169	    goto done;
1170	}
1171	while ((inb(lpt_stat_port) & LPIP_SHAKE)) {
1172	    len = sc->sc_if.if_mtu + LPIPHDRLEN;
1173	    bp  = sc->sc_ifbuf;
1174	    while (len--) {
1175
1176		cl = inb(lpt_stat_port);
1177		outb(lpt_data_port, 8);
1178
1179		j = LPMAXSPIN2;
1180		while((inb(lpt_stat_port) & LPIP_SHAKE))
1181		    if(!--j) goto err;
1182
1183		c = inb(lpt_stat_port);
1184		outb(lpt_data_port, 0);
1185
1186		*bp++= trecvh[cl] | trecvl[c];
1187
1188		j = LPMAXSPIN2;
1189		while (!((cl=inb(lpt_stat_port)) & LPIP_SHAKE)) {
1190		    if (cl != c &&
1191			(((cl = inb(lpt_stat_port)) ^ 0xb8) & 0xf8) ==
1192			  (c & 0xf8))
1193			goto end;
1194		    if (!--j) goto err;
1195		}
1196	    }
1197
1198	end:
1199	    len = bp - sc->sc_ifbuf;
1200	    if (len <= LPIPHDRLEN)
1201		goto err;
1202
1203	    sc->sc_iferrs = 0;
1204
1205	    if (IF_QFULL(&ipintrq)) {
1206		lprintf(("DROP"));
1207		IF_DROP(&ipintrq);
1208		goto done;
1209	    }
1210	    if (sc->sc_if.if_bpf) {
1211		bpf_tap(&sc->sc_if, sc->sc_ifbuf, len);
1212	    }
1213	    len -= LPIPHDRLEN;
1214	    sc->sc_if.if_ipackets++;
1215	    sc->sc_if.if_ibytes += len;
1216	    top = m_devget(sc->sc_ifbuf + LPIPHDRLEN, len, 0, &sc->sc_if, 0);
1217	    if (top) {
1218		    IF_ENQUEUE(&ipintrq, top);
1219		    schednetisr(NETISR_IP);
1220	    }
1221	}
1222	goto done;
1223
1224    err:
1225	outb(lpt_data_port, 0);
1226	lprintf(("R"));
1227	sc->sc_if.if_ierrors++;
1228	sc->sc_iferrs++;
1229
1230	/*
1231	 * We are not able to send receive anything for now,
1232	 * so stop wasting our time
1233	 */
1234	if (sc->sc_iferrs > LPMAXERRS) {
1235	    printf("lp%d: Too many errors, Going off-line.\n", unit);
1236	    outb(lpt_ctrl_port, 0x00);
1237	    sc->sc_if.if_flags &= ~IFF_RUNNING;
1238	    sc->sc_iferrs=0;
1239	}
1240
1241    done:
1242	splx(s);
1243	return;
1244}
1245
1246static __inline int
1247lpoutbyte (u_char byte, int spin, int data_port, int status_port)
1248{
1249    outb(data_port, txmith[byte]);
1250    while (!(inb(status_port) & LPIP_SHAKE))
1251	if (--spin == 0)
1252		return 1;
1253    outb(data_port, txmitl[byte]);
1254    while (inb(status_port) & LPIP_SHAKE)
1255	if (--spin == 0)
1256		return 1;
1257    return 0;
1258}
1259
1260static int
1261lpoutput (struct ifnet *ifp, struct mbuf *m,
1262	  struct sockaddr *dst, struct rtentry *rt)
1263{
1264    register int lpt_data_port = lpt_sc[ifp->if_unit].sc_port + lpt_data;
1265    register int lpt_stat_port = lpt_sc[ifp->if_unit].sc_port + lpt_status;
1266#ifndef PC98
1267	     int lpt_ctrl_port = lpt_sc[ifp->if_unit].sc_port + lpt_control;
1268#endif
1269
1270    int s, err;
1271    struct mbuf *mm;
1272    u_char *cp = "\0\0";
1273    u_char chksum = 0;
1274    int count = 0;
1275    int i;
1276    int spin;
1277
1278    /* We need a sensible value if we abort */
1279    cp++;
1280    ifp->if_flags |= IFF_RUNNING;
1281
1282    err = 1;			/* assume we're aborting because of an error */
1283
1284    s = splhigh();
1285
1286#ifndef PC98
1287    /* Suspend (on laptops) or receive-errors might have taken us offline */
1288    outb(lpt_ctrl_port, LPC_ENA);
1289#endif
1290
1291    if (ifp->if_flags & IFF_LINK0) {
1292
1293	if (!(inb(lpt_stat_port) & CLPIP_SHAKE)) {
1294	    lprintf(("&"));
1295	    lptintr(ifp->if_unit);
1296	}
1297
1298	/* Alert other end to pending packet */
1299	spin = LPMAXSPIN1;
1300	outb(lpt_data_port, 0x08);
1301	while ((inb(lpt_stat_port) & 0x08) == 0)
1302		if (--spin == 0) {
1303			goto nend;
1304		}
1305
1306	/* Calculate length of packet, then send that */
1307
1308	count += 14;		/* Ethernet header len */
1309
1310	mm = m;
1311	for (mm = m; mm; mm = mm->m_next) {
1312		count += mm->m_len;
1313	}
1314	if (clpoutbyte(count & 0xFF, LPMAXSPIN1, lpt_data_port, lpt_stat_port))
1315		goto nend;
1316	if (clpoutbyte((count >> 8) & 0xFF, LPMAXSPIN1, lpt_data_port, lpt_stat_port))
1317		goto nend;
1318
1319	/* Send dummy ethernet header */
1320	for (i = 0; i < 12; i++) {
1321		if (clpoutbyte(i, LPMAXSPIN1, lpt_data_port, lpt_stat_port))
1322			goto nend;
1323		chksum += i;
1324	}
1325
1326	if (clpoutbyte(0x08, LPMAXSPIN1, lpt_data_port, lpt_stat_port))
1327		goto nend;
1328	if (clpoutbyte(0x00, LPMAXSPIN1, lpt_data_port, lpt_stat_port))
1329		goto nend;
1330	chksum += 0x08 + 0x00;		/* Add into checksum */
1331
1332	mm = m;
1333	do {
1334		cp = mtod(mm, u_char *);
1335		while (mm->m_len--) {
1336			chksum += *cp;
1337			if (clpoutbyte(*cp++, LPMAXSPIN2, lpt_data_port, lpt_stat_port))
1338				goto nend;
1339		}
1340	} while ((mm = mm->m_next));
1341
1342	/* Send checksum */
1343	if (clpoutbyte(chksum, LPMAXSPIN2, lpt_data_port, lpt_stat_port))
1344		goto nend;
1345
1346	/* Go quiescent */
1347	outb(lpt_data_port, 0);
1348
1349	err = 0;			/* No errors */
1350
1351	nend:
1352	if (err)  {				/* if we didn't timeout... */
1353		ifp->if_oerrors++;
1354		lprintf(("X"));
1355	} else {
1356		ifp->if_opackets++;
1357		ifp->if_obytes += m->m_pkthdr.len;
1358	}
1359
1360	m_freem(m);
1361
1362	if (!(inb(lpt_stat_port) & CLPIP_SHAKE)) {
1363		lprintf(("^"));
1364		lptintr(ifp->if_unit);
1365	}
1366	(void) splx(s);
1367	return 0;
1368    }
1369
1370    if (inb(lpt_stat_port) & LPIP_SHAKE) {
1371        lprintf(("&"));
1372        lptintr(ifp->if_unit);
1373    }
1374
1375    if (lpoutbyte(0x08, LPMAXSPIN1, lpt_data_port, lpt_stat_port))
1376        goto end;
1377    if (lpoutbyte(0x00, LPMAXSPIN2, lpt_data_port, lpt_stat_port))
1378        goto end;
1379
1380    mm = m;
1381    do {
1382        cp = mtod(mm,u_char *);
1383        while (mm->m_len--)
1384	    if (lpoutbyte(*cp++, LPMAXSPIN2, lpt_data_port, lpt_stat_port))
1385	        goto end;
1386    } while ((mm = mm->m_next));
1387
1388    err = 0;				/* no errors were encountered */
1389
1390    end:
1391    --cp;
1392    outb(lpt_data_port, txmitl[*cp] ^ 0x17);
1393
1394    if (err)  {				/* if we didn't timeout... */
1395	ifp->if_oerrors++;
1396        lprintf(("X"));
1397    } else {
1398	ifp->if_opackets++;
1399	ifp->if_obytes += m->m_pkthdr.len;
1400	if (ifp->if_bpf) {
1401	    /*
1402	     * We need to prepend the packet type as
1403	     * a two byte field.  Cons up a dummy header
1404	     * to pacify bpf.  This is safe because bpf
1405	     * will only read from the mbuf (i.e., it won't
1406	     * try to free it or keep a pointer to it).
1407	     */
1408	    struct mbuf m0;
1409	    u_short hdr = 0x800;
1410
1411	    m0.m_next = m;
1412	    m0.m_len = 2;
1413	    m0.m_data = (char *)&hdr;
1414
1415	    bpf_mtap(ifp, &m0);
1416	}
1417    }
1418
1419    m_freem(m);
1420
1421    if (inb(lpt_stat_port) & LPIP_SHAKE) {
1422	lprintf(("^"));
1423	lptintr(ifp->if_unit);
1424    }
1425
1426    (void) splx(s);
1427    return 0;
1428}
1429
1430#endif /* INET */
1431
1432static int lpt_devsw_installed;
1433
1434static void 	lpt_drvinit(void *unused)
1435{
1436
1437	if( ! lpt_devsw_installed ) {
1438		cdevsw_add(&lpt_cdevsw);
1439		lpt_devsw_installed = 1;
1440    	}
1441}
1442#ifndef LPT_DRVINIT_AT_ATTACH
1443SYSINIT(lptdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,lpt_drvinit,NULL)
1444#endif
1445