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