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