xref: /freebsd/sys/dev/ppbus/if_plip.c (revision a34c6aeb850016c35e415e12bf3c3443c0a8734d)
146f3ff79SMike Smith /*-
246f3ff79SMike Smith  * Copyright (c) 1997 Poul-Henning Kamp
346f3ff79SMike Smith  * All rights reserved.
446f3ff79SMike Smith  *
546f3ff79SMike Smith  * Redistribution and use in source and binary forms, with or without
646f3ff79SMike Smith  * modification, are permitted provided that the following conditions
746f3ff79SMike Smith  * are met:
846f3ff79SMike Smith  * 1. Redistributions of source code must retain the above copyright
946f3ff79SMike Smith  *    notice, this list of conditions and the following disclaimer.
1046f3ff79SMike Smith  * 2. Redistributions in binary form must reproduce the above copyright
1146f3ff79SMike Smith  *    notice, this list of conditions and the following disclaimer in the
1246f3ff79SMike Smith  *    documentation and/or other materials provided with the distribution.
1346f3ff79SMike Smith  *
1446f3ff79SMike Smith  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1546f3ff79SMike Smith  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1646f3ff79SMike Smith  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1746f3ff79SMike Smith  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1846f3ff79SMike Smith  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1946f3ff79SMike Smith  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2046f3ff79SMike Smith  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2146f3ff79SMike Smith  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2246f3ff79SMike Smith  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2346f3ff79SMike Smith  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2446f3ff79SMike Smith  * SUCH DAMAGE.
2546f3ff79SMike Smith  *
2646f3ff79SMike Smith  *	From Id: lpt.c,v 1.55.2.1 1996/11/12 09:08:38 phk Exp
2746f3ff79SMike Smith  */
2846f3ff79SMike Smith 
29aad970f1SDavid E. O'Brien #include <sys/cdefs.h>
30aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$");
31aad970f1SDavid E. O'Brien 
3246f3ff79SMike Smith /*
3346f3ff79SMike Smith  * Parallel port TCP/IP interfaces added.  I looked at the driver from
3446f3ff79SMike Smith  * MACH but this is a complete rewrite, and btw. incompatible, and it
3546f3ff79SMike Smith  * should perform better too.  I have never run the MACH driver though.
3646f3ff79SMike Smith  *
3746f3ff79SMike Smith  * This driver sends two bytes (0x08, 0x00) in front of each packet,
3846f3ff79SMike Smith  * to allow us to distinguish another format later.
3946f3ff79SMike Smith  *
40d64ada50SJens Schweikhardt  * Now added a Linux/Crynwr compatibility mode which is enabled using
4146f3ff79SMike Smith  * IF_LINK0 - Tim Wilkinson.
4246f3ff79SMike Smith  *
4346f3ff79SMike Smith  * TODO:
4446f3ff79SMike Smith  *    Make HDLC/PPP mode, use IF_LLC1 to enable.
4546f3ff79SMike Smith  *
4646f3ff79SMike Smith  * Connect the two computers using a Laplink parallel cable to use this
4746f3ff79SMike Smith  * feature:
4846f3ff79SMike Smith  *
4946f3ff79SMike Smith  *      +----------------------------------------+
5046f3ff79SMike Smith  * 	|A-name	A-End	B-End	Descr.	Port/Bit |
5146f3ff79SMike Smith  *      +----------------------------------------+
5246f3ff79SMike Smith  *	|DATA0	2	15	Data	0/0x01   |
5346f3ff79SMike Smith  *	|-ERROR	15	2	   	1/0x08   |
5446f3ff79SMike Smith  *      +----------------------------------------+
5546f3ff79SMike Smith  *	|DATA1	3	13	Data	0/0x02	 |
5646f3ff79SMike Smith  *	|+SLCT	13	3	   	1/0x10   |
5746f3ff79SMike Smith  *      +----------------------------------------+
5846f3ff79SMike Smith  *	|DATA2	4	12	Data	0/0x04   |
5946f3ff79SMike Smith  *	|+PE	12	4	   	1/0x20   |
6046f3ff79SMike Smith  *      +----------------------------------------+
6146f3ff79SMike Smith  *	|DATA3	5	10	Strobe	0/0x08   |
6246f3ff79SMike Smith  *	|-ACK	10	5	   	1/0x40   |
6346f3ff79SMike Smith  *      +----------------------------------------+
6446f3ff79SMike Smith  *	|DATA4	6	11	Data	0/0x10   |
6546f3ff79SMike Smith  *	|BUSY	11	6	   	1/~0x80  |
6646f3ff79SMike Smith  *      +----------------------------------------+
6746f3ff79SMike Smith  *	|GND	18-25	18-25	GND	-        |
6846f3ff79SMike Smith  *      +----------------------------------------+
6946f3ff79SMike Smith  *
7046f3ff79SMike Smith  * Expect transfer-rates up to 75 kbyte/sec.
7146f3ff79SMike Smith  *
7246f3ff79SMike Smith  * If GCC could correctly grok
7346f3ff79SMike Smith  *	register int port asm("edx")
7446f3ff79SMike Smith  * the code would be cleaner
7546f3ff79SMike Smith  *
7646f3ff79SMike Smith  * Poul-Henning Kamp <phk@freebsd.org>
7746f3ff79SMike Smith  */
7846f3ff79SMike Smith 
7946f3ff79SMike Smith /*
8046f3ff79SMike Smith  * Update for ppbus, PLIP support only - Nicolas Souchu
8146f3ff79SMike Smith  */
820f210c92SNicolas Souchu 
830f210c92SNicolas Souchu #include "opt_plip.h"
8446f3ff79SMike Smith 
8546f3ff79SMike Smith #include <sys/param.h>
8646f3ff79SMike Smith #include <sys/systm.h>
870f210c92SNicolas Souchu #include <sys/module.h>
880f210c92SNicolas Souchu #include <sys/bus.h>
8946f3ff79SMike Smith #include <sys/mbuf.h>
9046f3ff79SMike Smith #include <sys/socket.h>
9146f3ff79SMike Smith #include <sys/sockio.h>
9246f3ff79SMike Smith #include <sys/kernel.h>
9346f3ff79SMike Smith #include <sys/malloc.h>
9446f3ff79SMike Smith 
950f210c92SNicolas Souchu #include <machine/bus.h>
960f210c92SNicolas Souchu #include <machine/resource.h>
970f210c92SNicolas Souchu #include <sys/rman.h>
980f210c92SNicolas Souchu 
9946f3ff79SMike Smith #include <net/if.h>
10046f3ff79SMike Smith #include <net/if_types.h>
10146f3ff79SMike Smith #include <net/netisr.h>
102279aa3d4SKip Macy #include <net/route.h>
10346f3ff79SMike Smith 
10446f3ff79SMike Smith #include <netinet/in.h>
10546f3ff79SMike Smith #include <netinet/in_var.h>
10646f3ff79SMike Smith 
10746f3ff79SMike Smith #include <net/bpf.h>
10846f3ff79SMike Smith 
10946f3ff79SMike Smith #include <dev/ppbus/ppbconf.h>
1100f210c92SNicolas Souchu #include "ppbus_if.h"
1110f210c92SNicolas Souchu #include <dev/ppbus/ppbio.h>
1127b7bf77eSNicolas Souchu 
11346f3ff79SMike Smith #ifndef LPMTU			/* MTU for the lp# interfaces */
11446f3ff79SMike Smith #define	LPMTU		1500
11546f3ff79SMike Smith #endif
11646f3ff79SMike Smith 
11746f3ff79SMike Smith #ifndef LPMAXSPIN1		/* DELAY factor for the lp# interfaces */
11846f3ff79SMike Smith #define	LPMAXSPIN1	8000   /* Spinning for remote intr to happen */
11946f3ff79SMike Smith #endif
12046f3ff79SMike Smith 
12146f3ff79SMike Smith #ifndef LPMAXSPIN2		/* DELAY factor for the lp# interfaces */
12246f3ff79SMike Smith #define	LPMAXSPIN2	500	/* Spinning for remote handshake to happen */
12346f3ff79SMike Smith #endif
12446f3ff79SMike Smith 
12546f3ff79SMike Smith #ifndef LPMAXERRS		/* Max errors before !RUNNING */
12646f3ff79SMike Smith #define	LPMAXERRS	100
12746f3ff79SMike Smith #endif
12846f3ff79SMike Smith 
12946f3ff79SMike Smith #define	CLPIPHDRLEN	14	/* We send dummy ethernet addresses (two) + packet type in front of packet */
13046f3ff79SMike Smith #define	CLPIP_SHAKE	0x80	/* This bit toggles between nibble reception */
13146f3ff79SMike Smith #define	MLPIPHDRLEN	CLPIPHDRLEN
13246f3ff79SMike Smith 
13346f3ff79SMike Smith #define	LPIPHDRLEN	2	/* We send 0x08, 0x00 in front of packet */
13446f3ff79SMike Smith #define	LPIP_SHAKE	0x40	/* This bit toggles between nibble reception */
13546f3ff79SMike Smith #if !defined(MLPIPHDRLEN) || LPIPHDRLEN > MLPIPHDRLEN
13646f3ff79SMike Smith #define	MLPIPHDRLEN	LPIPHDRLEN
13746f3ff79SMike Smith #endif
13846f3ff79SMike Smith 
13946f3ff79SMike Smith #define	LPIPTBLSIZE	256	/* Size of octet translation table */
14046f3ff79SMike Smith 
14146f3ff79SMike Smith #define	lprintf		if (lptflag) printf
14220240fa3SNicolas Souchu 
14320240fa3SNicolas Souchu #ifdef PLIP_DEBUG
14446f3ff79SMike Smith static int volatile lptflag = 1;
14520240fa3SNicolas Souchu #else
14620240fa3SNicolas Souchu static int volatile lptflag = 0;
14746f3ff79SMike Smith #endif
14846f3ff79SMike Smith 
1490f210c92SNicolas Souchu struct lp_data {
150fc74a9f9SBrooks Davis 	struct  ifnet	*sc_ifp;
151ae6b868aSJohn Baldwin 	device_t	sc_dev;
15246f3ff79SMike Smith 	u_char		*sc_ifbuf;
15346f3ff79SMike Smith 	int		sc_iferrs;
1540f210c92SNicolas Souchu 
1550f210c92SNicolas Souchu 	struct resource *res_irq;
1562067d312SJohn Baldwin 	void		*sc_intr_cookie;
15746f3ff79SMike Smith };
15846f3ff79SMike Smith 
1592067d312SJohn Baldwin static struct mtx lp_tables_lock;
1602067d312SJohn Baldwin MTX_SYSINIT(lp_tables, &lp_tables_lock, "plip tables", MTX_DEF);
1612067d312SJohn Baldwin 
16246f3ff79SMike Smith /* Tables for the lp# interface */
16346f3ff79SMike Smith static u_char *txmith;
16446f3ff79SMike Smith #define	txmitl (txmith + (1 * LPIPTBLSIZE))
16546f3ff79SMike Smith #define	trecvh (txmith + (2 * LPIPTBLSIZE))
16646f3ff79SMike Smith #define	trecvl (txmith + (3 * LPIPTBLSIZE))
16746f3ff79SMike Smith 
16846f3ff79SMike Smith static u_char *ctxmith;
16946f3ff79SMike Smith #define	ctxmitl (ctxmith + (1 * LPIPTBLSIZE))
17046f3ff79SMike Smith #define	ctrecvh (ctxmith + (2 * LPIPTBLSIZE))
17146f3ff79SMike Smith #define	ctrecvl (ctxmith + (3 * LPIPTBLSIZE))
17246f3ff79SMike Smith 
17346f3ff79SMike Smith /* Functions for the lp# interface */
17446f3ff79SMike Smith static int lpinittables(void);
17546f3ff79SMike Smith static int lpioctl(struct ifnet *, u_long, caddr_t);
17646f3ff79SMike Smith static int lpoutput(struct ifnet *, struct mbuf *, struct sockaddr *,
177279aa3d4SKip Macy        struct route *);
1782067d312SJohn Baldwin static void lpstop(struct lp_data *);
1790f210c92SNicolas Souchu static void lp_intr(void *);
1802067d312SJohn Baldwin static int lp_module_handler(module_t, int, void *);
18146f3ff79SMike Smith 
1820f210c92SNicolas Souchu #define	DEVTOSOFTC(dev) \
1830f210c92SNicolas Souchu 	((struct lp_data *)device_get_softc(dev))
18446f3ff79SMike Smith 
1850f210c92SNicolas Souchu static devclass_t lp_devclass;
1860f210c92SNicolas Souchu 
1872067d312SJohn Baldwin static int
1882067d312SJohn Baldwin lp_module_handler(module_t mod, int what, void *arg)
1892067d312SJohn Baldwin {
1902067d312SJohn Baldwin 
1912067d312SJohn Baldwin 	switch (what) {
1922067d312SJohn Baldwin 	case MOD_UNLOAD:
1932067d312SJohn Baldwin 		mtx_lock(&lp_tables_lock);
1942067d312SJohn Baldwin 		if (txmith != NULL) {
1952067d312SJohn Baldwin 			free(txmith, M_DEVBUF);
1962067d312SJohn Baldwin 			txmith = NULL;
1972067d312SJohn Baldwin 		}
1982067d312SJohn Baldwin 		if (ctxmith != NULL) {
1992067d312SJohn Baldwin 			free(ctxmith, M_DEVBUF);
2002067d312SJohn Baldwin 			ctxmith = NULL;
2012067d312SJohn Baldwin 		}
2022067d312SJohn Baldwin 		mtx_unlock(&lp_tables_lock);
2032067d312SJohn Baldwin 		break;
2042067d312SJohn Baldwin 	case MOD_LOAD:
2052067d312SJohn Baldwin 	case MOD_QUIESCE:
2062067d312SJohn Baldwin 		break;
2072067d312SJohn Baldwin 	default:
2082067d312SJohn Baldwin 		return (EOPNOTSUPP);
2092067d312SJohn Baldwin 	}
2102067d312SJohn Baldwin 	return (0);
2112067d312SJohn Baldwin }
2122067d312SJohn Baldwin 
2130f063508SPeter Wemm static void
2140f063508SPeter Wemm lp_identify(driver_t *driver, device_t parent)
2150f063508SPeter Wemm {
216a5c7e3bbSGuido van Rooij 	device_t dev;
2170f210c92SNicolas Souchu 
21866de9771SJohn Baldwin 	dev = device_find_child(parent, "plip", -1);
219a5c7e3bbSGuido van Rooij 	if (!dev)
220338cad62SBernd Walter 		BUS_ADD_CHILD(parent, 0, "plip", -1);
2210f063508SPeter Wemm }
222c26864e0SJohn Baldwin 
2230f210c92SNicolas Souchu static int
2240f210c92SNicolas Souchu lp_probe(device_t dev)
22546f3ff79SMike Smith {
22646f3ff79SMike Smith 
2270f210c92SNicolas Souchu 	device_set_desc(dev, "PLIP network interface");
22846f3ff79SMike Smith 
2290f210c92SNicolas Souchu 	return (0);
23046f3ff79SMike Smith }
23146f3ff79SMike Smith 
23246f3ff79SMike Smith static int
2330f210c92SNicolas Souchu lp_attach(device_t dev)
23446f3ff79SMike Smith {
2350f210c92SNicolas Souchu 	struct lp_data *lp = DEVTOSOFTC(dev);
236fc74a9f9SBrooks Davis 	struct ifnet *ifp;
2372067d312SJohn Baldwin 	int error, rid = 0;
238ca3d3795SJohn Baldwin 
239ae6b868aSJohn Baldwin 	lp->sc_dev = dev;
240ae6b868aSJohn Baldwin 
241ca3d3795SJohn Baldwin 	/*
242ca3d3795SJohn Baldwin 	 * Reserve the interrupt resource.  If we don't have one, the
243ca3d3795SJohn Baldwin 	 * attach fails.
244ca3d3795SJohn Baldwin 	 */
245ca3d3795SJohn Baldwin 	lp->res_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
246ca3d3795SJohn Baldwin 	    RF_SHAREABLE);
247ca3d3795SJohn Baldwin 	if (lp->res_irq == 0) {
248ca3d3795SJohn Baldwin 		device_printf(dev, "cannot reserve interrupt, failed.\n");
249ca3d3795SJohn Baldwin 		return (ENXIO);
250ca3d3795SJohn Baldwin 	}
251fc74a9f9SBrooks Davis 
252fc74a9f9SBrooks Davis 	ifp = lp->sc_ifp = if_alloc(IFT_PARA);
253fc74a9f9SBrooks Davis 	if (ifp == NULL) {
254fc74a9f9SBrooks Davis 		return (ENOSPC);
255fc74a9f9SBrooks Davis 	}
25646f3ff79SMike Smith 
2570f210c92SNicolas Souchu 	ifp->if_softc = lp;
2589bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
25946f3ff79SMike Smith 	ifp->if_mtu = LPMTU;
2602067d312SJohn Baldwin 	ifp->if_flags = IFF_SIMPLEX | IFF_POINTOPOINT | IFF_MULTICAST;
26146f3ff79SMike Smith 	ifp->if_ioctl = lpioctl;
26246f3ff79SMike Smith 	ifp->if_output = lpoutput;
26346f3ff79SMike Smith 	ifp->if_hdrlen = 0;
26446f3ff79SMike Smith 	ifp->if_addrlen = 0;
265e50d35e6SMaxim Sobolev 	ifp->if_snd.ifq_maxlen = ifqmaxlen;
26646f3ff79SMike Smith 	if_attach(ifp);
26746f3ff79SMike Smith 
2688f84257eSDag-Erling Smørgrav 	bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
26946f3ff79SMike Smith 
2702067d312SJohn Baldwin 	/*
2712067d312SJohn Baldwin 	 * Attach our interrupt handler.  It is only called while we
2722067d312SJohn Baldwin 	 * own the ppbus.
2732067d312SJohn Baldwin 	 */
2742067d312SJohn Baldwin 	error = bus_setup_intr(dev, lp->res_irq, INTR_TYPE_NET | INTR_MPSAFE,
2752067d312SJohn Baldwin 	    NULL, lp_intr, lp, &lp->sc_intr_cookie);
2762067d312SJohn Baldwin 	if (error) {
2772067d312SJohn Baldwin 		bpfdetach(ifp);
2782067d312SJohn Baldwin 		if_detach(ifp);
2792067d312SJohn Baldwin 		bus_release_resource(dev, SYS_RES_IRQ, 0, lp->res_irq);
2802067d312SJohn Baldwin 		device_printf(dev, "Unable to register interrupt handler\n");
2812067d312SJohn Baldwin 		return (error);
2822067d312SJohn Baldwin 	}
2832067d312SJohn Baldwin 
2840f210c92SNicolas Souchu 	return (0);
28546f3ff79SMike Smith }
2862067d312SJohn Baldwin 
2872067d312SJohn Baldwin static int
2882067d312SJohn Baldwin lp_detach(device_t dev)
2892067d312SJohn Baldwin {
2902067d312SJohn Baldwin 	struct lp_data *sc = device_get_softc(dev);
2912067d312SJohn Baldwin 	device_t ppbus = device_get_parent(dev);
2922067d312SJohn Baldwin 
2932067d312SJohn Baldwin 	ppb_lock(ppbus);
2942067d312SJohn Baldwin 	lpstop(sc);
2952067d312SJohn Baldwin 	ppb_unlock(ppbus);
2962067d312SJohn Baldwin 	bpfdetach(sc->sc_ifp);
2972067d312SJohn Baldwin 	if_detach(sc->sc_ifp);
2982067d312SJohn Baldwin 	bus_teardown_intr(dev, sc->res_irq, sc->sc_intr_cookie);
2992067d312SJohn Baldwin 	bus_release_resource(dev, SYS_RES_IRQ, 0, sc->res_irq);
3002067d312SJohn Baldwin 	return (0);
3012067d312SJohn Baldwin }
3022067d312SJohn Baldwin 
30346f3ff79SMike Smith /*
30446f3ff79SMike Smith  * Build the translation tables for the LPIP (BSD unix) protocol.
30546f3ff79SMike Smith  * We don't want to calculate these nasties in our tight loop, so we
30646f3ff79SMike Smith  * precalculate them when we initialize.
30746f3ff79SMike Smith  */
30846f3ff79SMike Smith static int
30946f3ff79SMike Smith lpinittables(void)
31046f3ff79SMike Smith {
31146f3ff79SMike Smith 	int i;
31246f3ff79SMike Smith 
3132067d312SJohn Baldwin 	mtx_lock(&lp_tables_lock);
314c26864e0SJohn Baldwin 	if (txmith == NULL)
31546f3ff79SMike Smith 		txmith = malloc(4 * LPIPTBLSIZE, M_DEVBUF, M_NOWAIT);
31646f3ff79SMike Smith 
3172067d312SJohn Baldwin 	if (txmith == NULL) {
3182067d312SJohn Baldwin 		mtx_unlock(&lp_tables_lock);
319c26864e0SJohn Baldwin 		return (1);
3202067d312SJohn Baldwin 	}
32146f3ff79SMike Smith 
322c26864e0SJohn Baldwin 	if (ctxmith == NULL)
32346f3ff79SMike Smith 		ctxmith = malloc(4 * LPIPTBLSIZE, M_DEVBUF, M_NOWAIT);
32446f3ff79SMike Smith 
3252067d312SJohn Baldwin 	if (ctxmith == NULL) {
3262067d312SJohn Baldwin 		mtx_unlock(&lp_tables_lock);
327c26864e0SJohn Baldwin 		return (1);
3282067d312SJohn Baldwin 	}
32946f3ff79SMike Smith 
33046f3ff79SMike Smith 	for (i = 0; i < LPIPTBLSIZE; i++) {
33146f3ff79SMike Smith 		ctxmith[i] = (i & 0xF0) >> 4;
33246f3ff79SMike Smith 		ctxmitl[i] = 0x10 | (i & 0x0F);
33346f3ff79SMike Smith 		ctrecvh[i] = (i & 0x78) << 1;
33446f3ff79SMike Smith 		ctrecvl[i] = (i & 0x78) >> 3;
33546f3ff79SMike Smith 	}
33646f3ff79SMike Smith 
33746f3ff79SMike Smith 	for (i = 0; i < LPIPTBLSIZE; i++) {
33846f3ff79SMike Smith 		txmith[i] = ((i & 0x80) >> 3) | ((i & 0x70) >> 4) | 0x08;
33946f3ff79SMike Smith 		txmitl[i] = ((i & 0x08) << 1) | (i & 0x07);
34046f3ff79SMike Smith 		trecvh[i] = ((~i) & 0x80) | ((i & 0x38) << 1);
34146f3ff79SMike Smith 		trecvl[i] = (((~i) & 0x80) >> 4) | ((i & 0x38) >> 3);
34246f3ff79SMike Smith 	}
3432067d312SJohn Baldwin 	mtx_unlock(&lp_tables_lock);
34446f3ff79SMike Smith 
345c26864e0SJohn Baldwin 	return (0);
34646f3ff79SMike Smith }
34746f3ff79SMike Smith 
3482067d312SJohn Baldwin static void
3492067d312SJohn Baldwin lpstop(struct lp_data *sc)
3502067d312SJohn Baldwin {
3512067d312SJohn Baldwin 	device_t ppbus = device_get_parent(sc->sc_dev);
3522067d312SJohn Baldwin 
3532067d312SJohn Baldwin 	ppb_assert_locked(ppbus);
3542067d312SJohn Baldwin 	ppb_wctr(ppbus, 0x00);
3552067d312SJohn Baldwin 	sc->sc_ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
3562067d312SJohn Baldwin 	free(sc->sc_ifbuf, M_DEVBUF);
3572067d312SJohn Baldwin 	sc->sc_ifbuf = NULL;
3582067d312SJohn Baldwin 
3592067d312SJohn Baldwin 	/* IFF_UP is not set, try to release the bus anyway */
3602067d312SJohn Baldwin 	ppb_release_bus(ppbus, sc->sc_dev);
3612067d312SJohn Baldwin }
3622067d312SJohn Baldwin 
3632067d312SJohn Baldwin static int
3642067d312SJohn Baldwin lpinit_locked(struct ifnet *ifp)
3652067d312SJohn Baldwin {
3662067d312SJohn Baldwin 	struct lp_data *sc = ifp->if_softc;
3672067d312SJohn Baldwin 	device_t dev = sc->sc_dev;
3682067d312SJohn Baldwin 	device_t ppbus = device_get_parent(dev);
3692067d312SJohn Baldwin 	int error;
3702067d312SJohn Baldwin 
3712067d312SJohn Baldwin 	ppb_assert_locked(ppbus);
3722067d312SJohn Baldwin 	error = ppb_request_bus(ppbus, dev, PPB_DONTWAIT);
3732067d312SJohn Baldwin 	if (error)
3742067d312SJohn Baldwin 		return (error);
3752067d312SJohn Baldwin 
3762067d312SJohn Baldwin 	/* Now IFF_UP means that we own the bus */
3772067d312SJohn Baldwin 	ppb_set_mode(ppbus, PPB_COMPATIBLE);
3782067d312SJohn Baldwin 
3792067d312SJohn Baldwin 	if (lpinittables()) {
3802067d312SJohn Baldwin 		ppb_release_bus(ppbus, dev);
3812067d312SJohn Baldwin 		return (ENOBUFS);
3822067d312SJohn Baldwin 	}
3832067d312SJohn Baldwin 
3842067d312SJohn Baldwin 	sc->sc_ifbuf = malloc(sc->sc_ifp->if_mtu + MLPIPHDRLEN,
3852067d312SJohn Baldwin 	    M_DEVBUF, M_NOWAIT);
3862067d312SJohn Baldwin 	if (sc->sc_ifbuf == NULL) {
3872067d312SJohn Baldwin 		ppb_release_bus(ppbus, dev);
3882067d312SJohn Baldwin 		return (ENOBUFS);
3892067d312SJohn Baldwin 	}
3902067d312SJohn Baldwin 
3912067d312SJohn Baldwin 	ppb_wctr(ppbus, IRQENABLE);
3922067d312SJohn Baldwin 
3932067d312SJohn Baldwin 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
3942067d312SJohn Baldwin 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
3952067d312SJohn Baldwin 	return (0);
3962067d312SJohn Baldwin }
3972067d312SJohn Baldwin 
39846f3ff79SMike Smith /*
39946f3ff79SMike Smith  * Process an ioctl request.
40046f3ff79SMike Smith  */
40146f3ff79SMike Smith static int
40246f3ff79SMike Smith lpioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
40346f3ff79SMike Smith {
404ae6b868aSJohn Baldwin 	struct lp_data *sc = ifp->if_softc;
405ae6b868aSJohn Baldwin 	device_t dev = sc->sc_dev;
4060f210c92SNicolas Souchu 	device_t ppbus = device_get_parent(dev);
40746f3ff79SMike Smith 	struct ifaddr *ifa = (struct ifaddr *)data;
40846f3ff79SMike Smith 	struct ifreq *ifr = (struct ifreq *)data;
40946f3ff79SMike Smith 	u_char *ptr;
41046f3ff79SMike Smith 	int error;
41146f3ff79SMike Smith 
41246f3ff79SMike Smith 	switch (cmd) {
41346f3ff79SMike Smith 	case SIOCSIFDSTADDR:
41446f3ff79SMike Smith 	case SIOCAIFADDR:
41546f3ff79SMike Smith 	case SIOCSIFADDR:
41646f3ff79SMike Smith 		if (ifa->ifa_addr->sa_family != AF_INET)
417c26864e0SJohn Baldwin 			return (EAFNOSUPPORT);
41846f3ff79SMike Smith 
41946f3ff79SMike Smith 		ifp->if_flags |= IFF_UP;
42046f3ff79SMike Smith 		/* FALLTHROUGH */
42146f3ff79SMike Smith 	case SIOCSIFFLAGS:
4222067d312SJohn Baldwin 		error = 0;
4232067d312SJohn Baldwin 		ppb_lock(ppbus);
42413f4c340SRobert Watson 		if ((!(ifp->if_flags & IFF_UP)) &&
4252067d312SJohn Baldwin 		    (ifp->if_drv_flags & IFF_DRV_RUNNING))
4262067d312SJohn Baldwin 			lpstop(sc);
4272067d312SJohn Baldwin 		else if (((ifp->if_flags & IFF_UP)) &&
4282067d312SJohn Baldwin 		    (!(ifp->if_drv_flags & IFF_DRV_RUNNING)))
4292067d312SJohn Baldwin 			error = lpinit_locked(ifp);
4302067d312SJohn Baldwin 		ppb_unlock(ppbus);
43146f3ff79SMike Smith 		return (error);
43246f3ff79SMike Smith 
43346f3ff79SMike Smith 	case SIOCSIFMTU:
4342067d312SJohn Baldwin 		ppb_lock(ppbus);
4352067d312SJohn Baldwin 		if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
4362067d312SJohn Baldwin 			ptr = malloc(ifr->ifr_mtu + MLPIPHDRLEN, M_DEVBUF,
437c26864e0SJohn Baldwin 			    M_NOWAIT);
4382067d312SJohn Baldwin 			if (ptr == NULL) {
4392067d312SJohn Baldwin 				ppb_unlock(ppbus);
440c26864e0SJohn Baldwin 				return (ENOBUFS);
44146f3ff79SMike Smith 			}
4422067d312SJohn Baldwin 			if (sc->sc_ifbuf)
4432067d312SJohn Baldwin 				free(sc->sc_ifbuf, M_DEVBUF);
4442067d312SJohn Baldwin 			sc->sc_ifbuf = ptr;
4452067d312SJohn Baldwin 		}
446fc74a9f9SBrooks Davis 		sc->sc_ifp->if_mtu = ifr->ifr_mtu;
4472067d312SJohn Baldwin 		ppb_unlock(ppbus);
44846f3ff79SMike Smith 		break;
44946f3ff79SMike Smith 
45046f3ff79SMike Smith 	case SIOCGIFMTU:
451fc74a9f9SBrooks Davis 		ifr->ifr_mtu = sc->sc_ifp->if_mtu;
45246f3ff79SMike Smith 		break;
45346f3ff79SMike Smith 
45446f3ff79SMike Smith 	case SIOCADDMULTI:
45546f3ff79SMike Smith 	case SIOCDELMULTI:
45646f3ff79SMike Smith 		if (ifr == 0) {
457c26864e0SJohn Baldwin 			return (EAFNOSUPPORT);		/* XXX */
45846f3ff79SMike Smith 		}
45946f3ff79SMike Smith 		switch (ifr->ifr_addr.sa_family) {
46046f3ff79SMike Smith 		case AF_INET:
46146f3ff79SMike Smith 			break;
46246f3ff79SMike Smith 		default:
463c26864e0SJohn Baldwin 			return (EAFNOSUPPORT);
46446f3ff79SMike Smith 		}
46546f3ff79SMike Smith 		break;
46646f3ff79SMike Smith 
46780015f13SMike Smith 	case SIOCGIFMEDIA:
46880015f13SMike Smith 		/*
46980015f13SMike Smith 		 * No ifmedia support at this stage; maybe use it
47080015f13SMike Smith 		 * in future for eg. protocol selection.
47180015f13SMike Smith 		 */
472c26864e0SJohn Baldwin 		return (EINVAL);
47380015f13SMike Smith 
47446f3ff79SMike Smith 	default:
475162886e2SBruce Evans 		lprintf("LP:ioctl(0x%lx)\n", cmd);
476c26864e0SJohn Baldwin 		return (EINVAL);
47746f3ff79SMike Smith 	}
478c26864e0SJohn Baldwin 	return (0);
47946f3ff79SMike Smith }
48046f3ff79SMike Smith 
48146f3ff79SMike Smith static __inline int
4820f210c92SNicolas Souchu clpoutbyte(u_char byte, int spin, device_t ppbus)
48346f3ff79SMike Smith {
484c26864e0SJohn Baldwin 
4850f210c92SNicolas Souchu 	ppb_wdtr(ppbus, ctxmitl[byte]);
4860f210c92SNicolas Souchu 	while (ppb_rstr(ppbus) & CLPIP_SHAKE)
48746f3ff79SMike Smith 		if (--spin == 0) {
488c26864e0SJohn Baldwin 			return (1);
48946f3ff79SMike Smith 		}
4900f210c92SNicolas Souchu 	ppb_wdtr(ppbus, ctxmith[byte]);
4910f210c92SNicolas Souchu 	while (!(ppb_rstr(ppbus) & CLPIP_SHAKE))
49246f3ff79SMike Smith 		if (--spin == 0) {
493c26864e0SJohn Baldwin 			return (1);
49446f3ff79SMike Smith 		}
495c26864e0SJohn Baldwin 	return (0);
49646f3ff79SMike Smith }
49746f3ff79SMike Smith 
49846f3ff79SMike Smith static __inline int
4990f210c92SNicolas Souchu clpinbyte(int spin, device_t ppbus)
50046f3ff79SMike Smith {
50124063475SNicolas Souchu 	u_char c, cl;
50246f3ff79SMike Smith 
5030f210c92SNicolas Souchu 	while ((ppb_rstr(ppbus) & CLPIP_SHAKE))
50446f3ff79SMike Smith 		if (!--spin) {
505c26864e0SJohn Baldwin 			return (-1);
50646f3ff79SMike Smith 		}
5070f210c92SNicolas Souchu 	cl = ppb_rstr(ppbus);
5080f210c92SNicolas Souchu 	ppb_wdtr(ppbus, 0x10);
50946f3ff79SMike Smith 
5100f210c92SNicolas Souchu 	while (!(ppb_rstr(ppbus) & CLPIP_SHAKE))
51146f3ff79SMike Smith 		if (!--spin) {
512c26864e0SJohn Baldwin 			return (-1);
51346f3ff79SMike Smith 		}
5140f210c92SNicolas Souchu 	c = ppb_rstr(ppbus);
5150f210c92SNicolas Souchu 	ppb_wdtr(ppbus, 0x00);
51646f3ff79SMike Smith 
51746f3ff79SMike Smith 	return (ctrecvl[cl] | ctrecvh[c]);
51846f3ff79SMike Smith }
51946f3ff79SMike Smith 
5208f84257eSDag-Erling Smørgrav static void
5218f84257eSDag-Erling Smørgrav lptap(struct ifnet *ifp, struct mbuf *m)
5228f84257eSDag-Erling Smørgrav {
5238f84257eSDag-Erling Smørgrav 	u_int32_t af = AF_INET;
524c26864e0SJohn Baldwin 
52555834910SJohn Baldwin 	bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
5268f84257eSDag-Erling Smørgrav }
5278f84257eSDag-Erling Smørgrav 
52846f3ff79SMike Smith static void
5290f210c92SNicolas Souchu lp_intr(void *arg)
53046f3ff79SMike Smith {
5312067d312SJohn Baldwin 	struct lp_data *sc = arg;
5322067d312SJohn Baldwin 	device_t ppbus = device_get_parent(sc->sc_dev);
5332067d312SJohn Baldwin 	int len, j;
53446f3ff79SMike Smith 	u_char *bp;
53546f3ff79SMike Smith 	u_char c, cl;
53646f3ff79SMike Smith 	struct mbuf *top;
53746f3ff79SMike Smith 
5382067d312SJohn Baldwin 	ppb_assert_locked(ppbus);
539fc74a9f9SBrooks Davis 	if (sc->sc_ifp->if_flags & IFF_LINK0) {
54046f3ff79SMike Smith 
54146f3ff79SMike Smith 		/* Ack. the request */
5420f210c92SNicolas Souchu 		ppb_wdtr(ppbus, 0x01);
54346f3ff79SMike Smith 
54446f3ff79SMike Smith 		/* Get the packet length */
5450f210c92SNicolas Souchu 		j = clpinbyte(LPMAXSPIN2, ppbus);
54646f3ff79SMike Smith 		if (j == -1)
54746f3ff79SMike Smith 			goto err;
54846f3ff79SMike Smith 		len = j;
5490f210c92SNicolas Souchu 		j = clpinbyte(LPMAXSPIN2, ppbus);
55046f3ff79SMike Smith 		if (j == -1)
55146f3ff79SMike Smith 			goto err;
55246f3ff79SMike Smith 		len = len + (j << 8);
553fc74a9f9SBrooks Davis 		if (len > sc->sc_ifp->if_mtu + MLPIPHDRLEN)
55446f3ff79SMike Smith 			goto err;
55546f3ff79SMike Smith 
55646f3ff79SMike Smith 		bp = sc->sc_ifbuf;
55746f3ff79SMike Smith 
55846f3ff79SMike Smith 		while (len--) {
5590f210c92SNicolas Souchu 			j = clpinbyte(LPMAXSPIN2, ppbus);
56046f3ff79SMike Smith 			if (j == -1) {
56146f3ff79SMike Smith 				goto err;
56246f3ff79SMike Smith 			}
56346f3ff79SMike Smith 			*bp++ = j;
56446f3ff79SMike Smith 		}
565c26864e0SJohn Baldwin 
56646f3ff79SMike Smith 		/* Get and ignore checksum */
5670f210c92SNicolas Souchu 		j = clpinbyte(LPMAXSPIN2, ppbus);
56846f3ff79SMike Smith 		if (j == -1) {
56946f3ff79SMike Smith 			goto err;
57046f3ff79SMike Smith 		}
57146f3ff79SMike Smith 
57246f3ff79SMike Smith 		len = bp - sc->sc_ifbuf;
57346f3ff79SMike Smith 		if (len <= CLPIPHDRLEN)
57446f3ff79SMike Smith 			goto err;
57546f3ff79SMike Smith 
57646f3ff79SMike Smith 		sc->sc_iferrs = 0;
57746f3ff79SMike Smith 
57846f3ff79SMike Smith 		len -= CLPIPHDRLEN;
579fc74a9f9SBrooks Davis 		sc->sc_ifp->if_ipackets++;
580fc74a9f9SBrooks Davis 		sc->sc_ifp->if_ibytes += len;
581c26864e0SJohn Baldwin 		top = m_devget(sc->sc_ifbuf + CLPIPHDRLEN, len, 0, sc->sc_ifp,
582c26864e0SJohn Baldwin 		    0);
58346f3ff79SMike Smith 		if (top) {
5842067d312SJohn Baldwin 			ppb_unlock(ppbus);
5850dea849aSJohn Baldwin 			if (bpf_peers_present(sc->sc_ifp->if_bpf))
586fc74a9f9SBrooks Davis 				lptap(sc->sc_ifp, top);
587c26864e0SJohn Baldwin 
588*a34c6aebSBjoern A. Zeeb 			M_SETFIB(top, sc->sc_ifp->if_fib);
589*a34c6aebSBjoern A. Zeeb 
590c26864e0SJohn Baldwin 			/* mbuf is free'd on failure. */
591c26864e0SJohn Baldwin 			netisr_queue(NETISR_IP, top);
5922067d312SJohn Baldwin 			ppb_lock(ppbus);
593df5e1987SJonathan Lemon 		}
5942067d312SJohn Baldwin 		return;
59546f3ff79SMike Smith 	}
5960f210c92SNicolas Souchu 	while ((ppb_rstr(ppbus) & LPIP_SHAKE)) {
597fc74a9f9SBrooks Davis 		len = sc->sc_ifp->if_mtu + LPIPHDRLEN;
59846f3ff79SMike Smith 		bp  = sc->sc_ifbuf;
59946f3ff79SMike Smith 		while (len--) {
60046f3ff79SMike Smith 
6010f210c92SNicolas Souchu 			cl = ppb_rstr(ppbus);
6020f210c92SNicolas Souchu 			ppb_wdtr(ppbus, 8);
60346f3ff79SMike Smith 
60446f3ff79SMike Smith 			j = LPMAXSPIN2;
6050f210c92SNicolas Souchu 			while ((ppb_rstr(ppbus) & LPIP_SHAKE))
606c26864e0SJohn Baldwin 				if (!--j)
607c26864e0SJohn Baldwin 					goto err;
60846f3ff79SMike Smith 
6090f210c92SNicolas Souchu 			c = ppb_rstr(ppbus);
6100f210c92SNicolas Souchu 			ppb_wdtr(ppbus, 0);
61146f3ff79SMike Smith 
61246f3ff79SMike Smith 			*bp++= trecvh[cl] | trecvl[c];
61346f3ff79SMike Smith 
61446f3ff79SMike Smith 			j = LPMAXSPIN2;
6150f210c92SNicolas Souchu 			while (!((cl = ppb_rstr(ppbus)) & LPIP_SHAKE)) {
61646f3ff79SMike Smith 				if (cl != c &&
6170f210c92SNicolas Souchu 				    (((cl = ppb_rstr(ppbus)) ^ 0xb8) & 0xf8) ==
61846f3ff79SMike Smith 				    (c & 0xf8))
61946f3ff79SMike Smith 					goto end;
620c26864e0SJohn Baldwin 				if (!--j)
621c26864e0SJohn Baldwin 					goto err;
62246f3ff79SMike Smith 			}
62346f3ff79SMike Smith 		}
62446f3ff79SMike Smith 
62546f3ff79SMike Smith 	end:
62646f3ff79SMike Smith 		len = bp - sc->sc_ifbuf;
62746f3ff79SMike Smith 		if (len <= LPIPHDRLEN)
62846f3ff79SMike Smith 			goto err;
62946f3ff79SMike Smith 
63046f3ff79SMike Smith 		sc->sc_iferrs = 0;
63146f3ff79SMike Smith 
63246f3ff79SMike Smith 		len -= LPIPHDRLEN;
633fc74a9f9SBrooks Davis 		sc->sc_ifp->if_ipackets++;
634fc74a9f9SBrooks Davis 		sc->sc_ifp->if_ibytes += len;
635c26864e0SJohn Baldwin 		top = m_devget(sc->sc_ifbuf + LPIPHDRLEN, len, 0, sc->sc_ifp,
636c26864e0SJohn Baldwin 		    0);
63746f3ff79SMike Smith 		if (top) {
6382067d312SJohn Baldwin 			ppb_unlock(ppbus);
6390dea849aSJohn Baldwin 			if (bpf_peers_present(sc->sc_ifp->if_bpf))
640fc74a9f9SBrooks Davis 				lptap(sc->sc_ifp, top);
641c26864e0SJohn Baldwin 
642*a34c6aebSBjoern A. Zeeb 			M_SETFIB(top, sc->sc_ifp->if_fib);
643*a34c6aebSBjoern A. Zeeb 
644c26864e0SJohn Baldwin 			/* mbuf is free'd on failure. */
645c26864e0SJohn Baldwin 			netisr_queue(NETISR_IP, top);
6462067d312SJohn Baldwin 			ppb_lock(ppbus);
64746f3ff79SMike Smith 		}
648df5e1987SJonathan Lemon 	}
6492067d312SJohn Baldwin 	return;
65046f3ff79SMike Smith 
65146f3ff79SMike Smith err:
6520f210c92SNicolas Souchu 	ppb_wdtr(ppbus, 0);
65346f3ff79SMike Smith 	lprintf("R");
654fc74a9f9SBrooks Davis 	sc->sc_ifp->if_ierrors++;
65546f3ff79SMike Smith 	sc->sc_iferrs++;
65646f3ff79SMike Smith 
65746f3ff79SMike Smith 	/*
65846f3ff79SMike Smith 	 * We are not able to send receive anything for now,
65946f3ff79SMike Smith 	 * so stop wasting our time
66046f3ff79SMike Smith 	 */
66146f3ff79SMike Smith 	if (sc->sc_iferrs > LPMAXERRS) {
662ae6b868aSJohn Baldwin 		if_printf(sc->sc_ifp, "Too many errors, Going off-line.\n");
6630f210c92SNicolas Souchu 		ppb_wctr(ppbus, 0x00);
66413f4c340SRobert Watson 		sc->sc_ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
66546f3ff79SMike Smith 		sc->sc_iferrs = 0;
66646f3ff79SMike Smith 	}
66746f3ff79SMike Smith }
66846f3ff79SMike Smith 
66946f3ff79SMike Smith static __inline int
6700f210c92SNicolas Souchu lpoutbyte(u_char byte, int spin, device_t ppbus)
67146f3ff79SMike Smith {
672c26864e0SJohn Baldwin 
6730f210c92SNicolas Souchu 	ppb_wdtr(ppbus, txmith[byte]);
6740f210c92SNicolas Souchu 	while (!(ppb_rstr(ppbus) & LPIP_SHAKE))
67546f3ff79SMike Smith 		if (--spin == 0)
676c26864e0SJohn Baldwin 			return (1);
6770f210c92SNicolas Souchu 	ppb_wdtr(ppbus, txmitl[byte]);
6780f210c92SNicolas Souchu 	while (ppb_rstr(ppbus) & LPIP_SHAKE)
67946f3ff79SMike Smith 		if (--spin == 0)
680c26864e0SJohn Baldwin 			return (1);
681c26864e0SJohn Baldwin 	return (0);
68246f3ff79SMike Smith }
68346f3ff79SMike Smith 
68446f3ff79SMike Smith static int
685c26864e0SJohn Baldwin lpoutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
686279aa3d4SKip Macy     struct route *ro)
68746f3ff79SMike Smith {
688ae6b868aSJohn Baldwin 	struct lp_data *sc = ifp->if_softc;
689ae6b868aSJohn Baldwin 	device_t dev = sc->sc_dev;
6900f210c92SNicolas Souchu 	device_t ppbus = device_get_parent(dev);
6912067d312SJohn Baldwin 	int err;
69246f3ff79SMike Smith 	struct mbuf *mm;
69346f3ff79SMike Smith 	u_char *cp = "\0\0";
69446f3ff79SMike Smith 	u_char chksum = 0;
69546f3ff79SMike Smith 	int count = 0;
6968f84257eSDag-Erling Smørgrav 	int i, len, spin;
69746f3ff79SMike Smith 
69846f3ff79SMike Smith 	/* We need a sensible value if we abort */
69946f3ff79SMike Smith 	cp++;
7002067d312SJohn Baldwin 	ppb_lock(ppbus);
7012067d312SJohn Baldwin 	ifp->if_drv_flags |= IFF_DRV_OACTIVE;
70246f3ff79SMike Smith 
70346f3ff79SMike Smith 	err = 1;		/* assume we're aborting because of an error */
70446f3ff79SMike Smith 
70546f3ff79SMike Smith 	/* Suspend (on laptops) or receive-errors might have taken us offline */
7060f210c92SNicolas Souchu 	ppb_wctr(ppbus, IRQENABLE);
70746f3ff79SMike Smith 
70846f3ff79SMike Smith 	if (ifp->if_flags & IFF_LINK0) {
7090f210c92SNicolas Souchu 		if (!(ppb_rstr(ppbus) & CLPIP_SHAKE)) {
71046f3ff79SMike Smith 			lprintf("&");
7112067d312SJohn Baldwin 			lp_intr(sc);
71246f3ff79SMike Smith 		}
71346f3ff79SMike Smith 
71446f3ff79SMike Smith 		/* Alert other end to pending packet */
71546f3ff79SMike Smith 		spin = LPMAXSPIN1;
7160f210c92SNicolas Souchu 		ppb_wdtr(ppbus, 0x08);
7170f210c92SNicolas Souchu 		while ((ppb_rstr(ppbus) & 0x08) == 0)
71846f3ff79SMike Smith 			if (--spin == 0) {
71946f3ff79SMike Smith 				goto nend;
72046f3ff79SMike Smith 			}
72146f3ff79SMike Smith 
72246f3ff79SMike Smith 		/* Calculate length of packet, then send that */
72346f3ff79SMike Smith 
72446f3ff79SMike Smith 		count += 14;		/* Ethernet header len */
72546f3ff79SMike Smith 
72646f3ff79SMike Smith 		mm = m;
72746f3ff79SMike Smith 		for (mm = m; mm; mm = mm->m_next) {
72846f3ff79SMike Smith 			count += mm->m_len;
72946f3ff79SMike Smith 		}
7300f210c92SNicolas Souchu 		if (clpoutbyte(count & 0xFF, LPMAXSPIN1, ppbus))
73146f3ff79SMike Smith 			goto nend;
7320f210c92SNicolas Souchu 		if (clpoutbyte((count >> 8) & 0xFF, LPMAXSPIN1, ppbus))
73346f3ff79SMike Smith 			goto nend;
73446f3ff79SMike Smith 
73546f3ff79SMike Smith 		/* Send dummy ethernet header */
73646f3ff79SMike Smith 		for (i = 0; i < 12; i++) {
7370f210c92SNicolas Souchu 			if (clpoutbyte(i, LPMAXSPIN1, ppbus))
73846f3ff79SMike Smith 				goto nend;
73946f3ff79SMike Smith 			chksum += i;
74046f3ff79SMike Smith 		}
74146f3ff79SMike Smith 
7420f210c92SNicolas Souchu 		if (clpoutbyte(0x08, LPMAXSPIN1, ppbus))
74346f3ff79SMike Smith 			goto nend;
7440f210c92SNicolas Souchu 		if (clpoutbyte(0x00, LPMAXSPIN1, ppbus))
74546f3ff79SMike Smith 			goto nend;
74646f3ff79SMike Smith 		chksum += 0x08 + 0x00;		/* Add into checksum */
74746f3ff79SMike Smith 
74846f3ff79SMike Smith 		mm = m;
74946f3ff79SMike Smith 		do {
75046f3ff79SMike Smith 			cp = mtod(mm, u_char *);
7518f84257eSDag-Erling Smørgrav 			len = mm->m_len;
7528f84257eSDag-Erling Smørgrav 			while (len--) {
75346f3ff79SMike Smith 				chksum += *cp;
7540f210c92SNicolas Souchu 				if (clpoutbyte(*cp++, LPMAXSPIN2, ppbus))
75546f3ff79SMike Smith 					goto nend;
75646f3ff79SMike Smith 			}
75746f3ff79SMike Smith 		} while ((mm = mm->m_next));
75846f3ff79SMike Smith 
75946f3ff79SMike Smith 		/* Send checksum */
7600f210c92SNicolas Souchu 		if (clpoutbyte(chksum, LPMAXSPIN2, ppbus))
76146f3ff79SMike Smith 			goto nend;
76246f3ff79SMike Smith 
76346f3ff79SMike Smith 		/* Go quiescent */
7640f210c92SNicolas Souchu 		ppb_wdtr(ppbus, 0);
76546f3ff79SMike Smith 
76646f3ff79SMike Smith 		err = 0;			/* No errors */
76746f3ff79SMike Smith 
76846f3ff79SMike Smith 	nend:
7692067d312SJohn Baldwin 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
77046f3ff79SMike Smith 		if (err)  {			/* if we didn't timeout... */
77146f3ff79SMike Smith 			ifp->if_oerrors++;
77246f3ff79SMike Smith 			lprintf("X");
77346f3ff79SMike Smith 		} else {
77446f3ff79SMike Smith 			ifp->if_opackets++;
77546f3ff79SMike Smith 			ifp->if_obytes += m->m_pkthdr.len;
7760dea849aSJohn Baldwin 			if (bpf_peers_present(ifp->if_bpf))
7778f84257eSDag-Erling Smørgrav 				lptap(ifp, m);
77846f3ff79SMike Smith 		}
77946f3ff79SMike Smith 
78046f3ff79SMike Smith 		m_freem(m);
78146f3ff79SMike Smith 
7820f210c92SNicolas Souchu 		if (!(ppb_rstr(ppbus) & CLPIP_SHAKE)) {
78346f3ff79SMike Smith 			lprintf("^");
7842067d312SJohn Baldwin 			lp_intr(sc);
78546f3ff79SMike Smith 		}
7862067d312SJohn Baldwin 		ppb_unlock(ppbus);
787c26864e0SJohn Baldwin 		return (0);
78846f3ff79SMike Smith 	}
78946f3ff79SMike Smith 
7900f210c92SNicolas Souchu 	if (ppb_rstr(ppbus) & LPIP_SHAKE) {
79146f3ff79SMike Smith 		lprintf("&");
7922067d312SJohn Baldwin 		lp_intr(sc);
79346f3ff79SMike Smith 	}
79446f3ff79SMike Smith 
7950f210c92SNicolas Souchu 	if (lpoutbyte(0x08, LPMAXSPIN1, ppbus))
79646f3ff79SMike Smith 		goto end;
7970f210c92SNicolas Souchu 	if (lpoutbyte(0x00, LPMAXSPIN2, ppbus))
79846f3ff79SMike Smith 		goto end;
79946f3ff79SMike Smith 
80046f3ff79SMike Smith 	mm = m;
80146f3ff79SMike Smith 	do {
80246f3ff79SMike Smith 		cp = mtod(mm, u_char *);
8038f84257eSDag-Erling Smørgrav 		len = mm->m_len;
8048f84257eSDag-Erling Smørgrav 		while (len--)
8050f210c92SNicolas Souchu 			if (lpoutbyte(*cp++, LPMAXSPIN2, ppbus))
80646f3ff79SMike Smith 				goto end;
80746f3ff79SMike Smith 	} while ((mm = mm->m_next));
80846f3ff79SMike Smith 
80946f3ff79SMike Smith 	err = 0;			/* no errors were encountered */
81046f3ff79SMike Smith 
81146f3ff79SMike Smith end:
81246f3ff79SMike Smith 	--cp;
8130f210c92SNicolas Souchu 	ppb_wdtr(ppbus, txmitl[*cp] ^ 0x17);
81446f3ff79SMike Smith 
8152067d312SJohn Baldwin 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
81646f3ff79SMike Smith 	if (err)  {			/* if we didn't timeout... */
81746f3ff79SMike Smith 		ifp->if_oerrors++;
81846f3ff79SMike Smith 		lprintf("X");
81946f3ff79SMike Smith 	} else {
82046f3ff79SMike Smith 		ifp->if_opackets++;
82146f3ff79SMike Smith 		ifp->if_obytes += m->m_pkthdr.len;
8220dea849aSJohn Baldwin 		if (bpf_peers_present(ifp->if_bpf))
8238f84257eSDag-Erling Smørgrav 			lptap(ifp, m);
82446f3ff79SMike Smith 	}
82546f3ff79SMike Smith 
82646f3ff79SMike Smith 	m_freem(m);
82746f3ff79SMike Smith 
8280f210c92SNicolas Souchu 	if (ppb_rstr(ppbus) & LPIP_SHAKE) {
82946f3ff79SMike Smith 		lprintf("^");
8302067d312SJohn Baldwin 		lp_intr(sc);
83146f3ff79SMike Smith 	}
83246f3ff79SMike Smith 
8332067d312SJohn Baldwin 	ppb_unlock(ppbus);
834c26864e0SJohn Baldwin 	return (0);
83546f3ff79SMike Smith }
8360f210c92SNicolas Souchu 
8370f063508SPeter Wemm static device_method_t lp_methods[] = {
8380f063508SPeter Wemm   	/* device interface */
8390f063508SPeter Wemm 	DEVMETHOD(device_identify,	lp_identify),
8400f063508SPeter Wemm 	DEVMETHOD(device_probe,		lp_probe),
8410f063508SPeter Wemm 	DEVMETHOD(device_attach,	lp_attach),
8422067d312SJohn Baldwin 	DEVMETHOD(device_detach,	lp_detach),
8430f210c92SNicolas Souchu 
8440f063508SPeter Wemm 	{ 0, 0 }
8450f063508SPeter Wemm };
8460f063508SPeter Wemm 
8470f063508SPeter Wemm static driver_t lp_driver = {
8480f063508SPeter Wemm 	"plip",
8490f063508SPeter Wemm 	lp_methods,
8500f063508SPeter Wemm 	sizeof(struct lp_data),
8510f063508SPeter Wemm };
8520f063508SPeter Wemm 
8532067d312SJohn Baldwin DRIVER_MODULE(plip, ppbus, lp_driver, lp_devclass, lp_module_handler, 0);
854f5fd5611SRuslan Ermilov MODULE_DEPEND(plip, ppbus, 1, 1, 1);
855