xref: /freebsd/sys/dev/ppbus/if_plip.c (revision 6d5880906ecc768646ba7b0cbaba4babc27a44d8)
146f3ff79SMike Smith /*-
2718cf2ccSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3718cf2ccSPedro F. Giffuni  *
446f3ff79SMike Smith  * Copyright (c) 1997 Poul-Henning Kamp
546f3ff79SMike Smith  * All rights reserved.
646f3ff79SMike Smith  *
746f3ff79SMike Smith  * Redistribution and use in source and binary forms, with or without
846f3ff79SMike Smith  * modification, are permitted provided that the following conditions
946f3ff79SMike Smith  * are met:
1046f3ff79SMike Smith  * 1. Redistributions of source code must retain the above copyright
1146f3ff79SMike Smith  *    notice, this list of conditions and the following disclaimer.
1246f3ff79SMike Smith  * 2. Redistributions in binary form must reproduce the above copyright
1346f3ff79SMike Smith  *    notice, this list of conditions and the following disclaimer in the
1446f3ff79SMike Smith  *    documentation and/or other materials provided with the distribution.
1546f3ff79SMike Smith  *
1646f3ff79SMike Smith  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1746f3ff79SMike Smith  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1846f3ff79SMike Smith  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1946f3ff79SMike Smith  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2046f3ff79SMike Smith  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2146f3ff79SMike Smith  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2246f3ff79SMike Smith  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2346f3ff79SMike Smith  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2446f3ff79SMike Smith  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2546f3ff79SMike Smith  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2646f3ff79SMike Smith  * SUCH DAMAGE.
2746f3ff79SMike Smith  *
2846f3ff79SMike Smith  *	From Id: lpt.c,v 1.55.2.1 1996/11/12 09:08:38 phk Exp
2946f3ff79SMike Smith  */
3046f3ff79SMike Smith 
31aad970f1SDavid E. O'Brien #include <sys/cdefs.h>
32aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$");
33aad970f1SDavid E. O'Brien 
3446f3ff79SMike Smith /*
3546f3ff79SMike Smith  * Parallel port TCP/IP interfaces added.  I looked at the driver from
3646f3ff79SMike Smith  * MACH but this is a complete rewrite, and btw. incompatible, and it
3746f3ff79SMike Smith  * should perform better too.  I have never run the MACH driver though.
3846f3ff79SMike Smith  *
3946f3ff79SMike Smith  * This driver sends two bytes (0x08, 0x00) in front of each packet,
4046f3ff79SMike Smith  * to allow us to distinguish another format later.
4146f3ff79SMike Smith  *
42d64ada50SJens Schweikhardt  * Now added a Linux/Crynwr compatibility mode which is enabled using
4346f3ff79SMike Smith  * IF_LINK0 - Tim Wilkinson.
4446f3ff79SMike Smith  *
4546f3ff79SMike Smith  * TODO:
4646f3ff79SMike Smith  *    Make HDLC/PPP mode, use IF_LLC1 to enable.
4746f3ff79SMike Smith  *
4846f3ff79SMike Smith  * Connect the two computers using a Laplink parallel cable to use this
4946f3ff79SMike Smith  * feature:
5046f3ff79SMike Smith  *
5146f3ff79SMike Smith  *      +----------------------------------------+
5246f3ff79SMike Smith  * 	|A-name	A-End	B-End	Descr.	Port/Bit |
5346f3ff79SMike Smith  *      +----------------------------------------+
5446f3ff79SMike Smith  *	|DATA0	2	15	Data	0/0x01   |
5546f3ff79SMike Smith  *	|-ERROR	15	2	   	1/0x08   |
5646f3ff79SMike Smith  *      +----------------------------------------+
5746f3ff79SMike Smith  *	|DATA1	3	13	Data	0/0x02	 |
5846f3ff79SMike Smith  *	|+SLCT	13	3	   	1/0x10   |
5946f3ff79SMike Smith  *      +----------------------------------------+
6046f3ff79SMike Smith  *	|DATA2	4	12	Data	0/0x04   |
6146f3ff79SMike Smith  *	|+PE	12	4	   	1/0x20   |
6246f3ff79SMike Smith  *      +----------------------------------------+
6346f3ff79SMike Smith  *	|DATA3	5	10	Strobe	0/0x08   |
6446f3ff79SMike Smith  *	|-ACK	10	5	   	1/0x40   |
6546f3ff79SMike Smith  *      +----------------------------------------+
6646f3ff79SMike Smith  *	|DATA4	6	11	Data	0/0x10   |
6746f3ff79SMike Smith  *	|BUSY	11	6	   	1/~0x80  |
6846f3ff79SMike Smith  *      +----------------------------------------+
6946f3ff79SMike Smith  *	|GND	18-25	18-25	GND	-        |
7046f3ff79SMike Smith  *      +----------------------------------------+
7146f3ff79SMike Smith  *
7246f3ff79SMike Smith  * Expect transfer-rates up to 75 kbyte/sec.
7346f3ff79SMike Smith  *
7446f3ff79SMike Smith  * If GCC could correctly grok
7546f3ff79SMike Smith  *	register int port asm("edx")
7646f3ff79SMike Smith  * the code would be cleaner
7746f3ff79SMike Smith  *
7846f3ff79SMike Smith  * Poul-Henning Kamp <phk@freebsd.org>
7946f3ff79SMike Smith  */
8046f3ff79SMike Smith 
8146f3ff79SMike Smith /*
8246f3ff79SMike Smith  * Update for ppbus, PLIP support only - Nicolas Souchu
8346f3ff79SMike Smith  */
840f210c92SNicolas Souchu 
850f210c92SNicolas Souchu #include "opt_plip.h"
8646f3ff79SMike Smith 
8746f3ff79SMike Smith #include <sys/param.h>
8846f3ff79SMike Smith #include <sys/systm.h>
890f210c92SNicolas Souchu #include <sys/module.h>
900f210c92SNicolas Souchu #include <sys/bus.h>
9146f3ff79SMike Smith #include <sys/mbuf.h>
9246f3ff79SMike Smith #include <sys/socket.h>
9346f3ff79SMike Smith #include <sys/sockio.h>
9446f3ff79SMike Smith #include <sys/kernel.h>
9546f3ff79SMike Smith #include <sys/malloc.h>
9646f3ff79SMike Smith 
970f210c92SNicolas Souchu #include <machine/bus.h>
980f210c92SNicolas Souchu #include <machine/resource.h>
990f210c92SNicolas Souchu #include <sys/rman.h>
1000f210c92SNicolas Souchu 
10146f3ff79SMike Smith #include <net/if.h>
10276039bc8SGleb Smirnoff #include <net/if_var.h>
10346f3ff79SMike Smith #include <net/if_types.h>
10446f3ff79SMike Smith #include <net/netisr.h>
105279aa3d4SKip Macy #include <net/route.h>
10646f3ff79SMike Smith 
10746f3ff79SMike Smith #include <netinet/in.h>
10846f3ff79SMike Smith #include <netinet/in_var.h>
10946f3ff79SMike Smith 
11046f3ff79SMike Smith #include <net/bpf.h>
11146f3ff79SMike Smith 
11246f3ff79SMike Smith #include <dev/ppbus/ppbconf.h>
1130f210c92SNicolas Souchu #include "ppbus_if.h"
1140f210c92SNicolas Souchu #include <dev/ppbus/ppbio.h>
1157b7bf77eSNicolas Souchu 
11646f3ff79SMike Smith #ifndef LPMTU			/* MTU for the lp# interfaces */
11746f3ff79SMike Smith #define	LPMTU		1500
11846f3ff79SMike Smith #endif
11946f3ff79SMike Smith 
12046f3ff79SMike Smith #ifndef LPMAXSPIN1		/* DELAY factor for the lp# interfaces */
12146f3ff79SMike Smith #define	LPMAXSPIN1	8000   /* Spinning for remote intr to happen */
12246f3ff79SMike Smith #endif
12346f3ff79SMike Smith 
12446f3ff79SMike Smith #ifndef LPMAXSPIN2		/* DELAY factor for the lp# interfaces */
12546f3ff79SMike Smith #define	LPMAXSPIN2	500	/* Spinning for remote handshake to happen */
12646f3ff79SMike Smith #endif
12746f3ff79SMike Smith 
12846f3ff79SMike Smith #ifndef LPMAXERRS		/* Max errors before !RUNNING */
12946f3ff79SMike Smith #define	LPMAXERRS	100
13046f3ff79SMike Smith #endif
13146f3ff79SMike Smith 
13246f3ff79SMike Smith #define	CLPIPHDRLEN	14	/* We send dummy ethernet addresses (two) + packet type in front of packet */
13346f3ff79SMike Smith #define	CLPIP_SHAKE	0x80	/* This bit toggles between nibble reception */
13446f3ff79SMike Smith #define	MLPIPHDRLEN	CLPIPHDRLEN
13546f3ff79SMike Smith 
13646f3ff79SMike Smith #define	LPIPHDRLEN	2	/* We send 0x08, 0x00 in front of packet */
13746f3ff79SMike Smith #define	LPIP_SHAKE	0x40	/* This bit toggles between nibble reception */
13846f3ff79SMike Smith #if !defined(MLPIPHDRLEN) || LPIPHDRLEN > MLPIPHDRLEN
13946f3ff79SMike Smith #define	MLPIPHDRLEN	LPIPHDRLEN
14046f3ff79SMike Smith #endif
14146f3ff79SMike Smith 
14246f3ff79SMike Smith #define	LPIPTBLSIZE	256	/* Size of octet translation table */
14346f3ff79SMike Smith 
14446f3ff79SMike Smith #define	lprintf		if (lptflag) printf
14520240fa3SNicolas Souchu 
14620240fa3SNicolas Souchu #ifdef PLIP_DEBUG
14746f3ff79SMike Smith static int volatile lptflag = 1;
14820240fa3SNicolas Souchu #else
14920240fa3SNicolas Souchu static int volatile lptflag = 0;
15046f3ff79SMike Smith #endif
15146f3ff79SMike Smith 
1520f210c92SNicolas Souchu struct lp_data {
153fc74a9f9SBrooks Davis 	struct  ifnet	*sc_ifp;
154ae6b868aSJohn Baldwin 	device_t	sc_dev;
15546f3ff79SMike Smith 	u_char		*sc_ifbuf;
15646f3ff79SMike Smith 	int		sc_iferrs;
1570f210c92SNicolas Souchu 
1580f210c92SNicolas Souchu 	struct resource *res_irq;
1592067d312SJohn Baldwin 	void		*sc_intr_cookie;
16046f3ff79SMike Smith };
16146f3ff79SMike Smith 
1622067d312SJohn Baldwin static struct mtx lp_tables_lock;
1632067d312SJohn Baldwin MTX_SYSINIT(lp_tables, &lp_tables_lock, "plip tables", MTX_DEF);
1642067d312SJohn Baldwin 
16546f3ff79SMike Smith /* Tables for the lp# interface */
16646f3ff79SMike Smith static u_char *txmith;
16746f3ff79SMike Smith #define	txmitl (txmith + (1 * LPIPTBLSIZE))
16846f3ff79SMike Smith #define	trecvh (txmith + (2 * LPIPTBLSIZE))
16946f3ff79SMike Smith #define	trecvl (txmith + (3 * LPIPTBLSIZE))
17046f3ff79SMike Smith 
17146f3ff79SMike Smith static u_char *ctxmith;
17246f3ff79SMike Smith #define	ctxmitl (ctxmith + (1 * LPIPTBLSIZE))
17346f3ff79SMike Smith #define	ctrecvh (ctxmith + (2 * LPIPTBLSIZE))
17446f3ff79SMike Smith #define	ctrecvl (ctxmith + (3 * LPIPTBLSIZE))
17546f3ff79SMike Smith 
17646f3ff79SMike Smith /* Functions for the lp# interface */
17746f3ff79SMike Smith static int lpinittables(void);
17846f3ff79SMike Smith static int lpioctl(struct ifnet *, u_long, caddr_t);
17947e8d432SGleb Smirnoff static int lpoutput(struct ifnet *, struct mbuf *, const struct sockaddr *,
180279aa3d4SKip Macy        struct route *);
1812067d312SJohn Baldwin static void lpstop(struct lp_data *);
1820f210c92SNicolas Souchu static void lp_intr(void *);
1832067d312SJohn Baldwin static int lp_module_handler(module_t, int, void *);
18446f3ff79SMike Smith 
1850f210c92SNicolas Souchu #define	DEVTOSOFTC(dev) \
1860f210c92SNicolas Souchu 	((struct lp_data *)device_get_softc(dev))
18746f3ff79SMike Smith 
1882067d312SJohn Baldwin static int
1892067d312SJohn Baldwin lp_module_handler(module_t mod, int what, void *arg)
1902067d312SJohn Baldwin {
1912067d312SJohn Baldwin 
1922067d312SJohn Baldwin 	switch (what) {
1932067d312SJohn Baldwin 	case MOD_UNLOAD:
1942067d312SJohn Baldwin 		mtx_lock(&lp_tables_lock);
1952067d312SJohn Baldwin 		if (txmith != NULL) {
1962067d312SJohn Baldwin 			free(txmith, M_DEVBUF);
1972067d312SJohn Baldwin 			txmith = NULL;
1982067d312SJohn Baldwin 		}
1992067d312SJohn Baldwin 		if (ctxmith != NULL) {
2002067d312SJohn Baldwin 			free(ctxmith, M_DEVBUF);
2012067d312SJohn Baldwin 			ctxmith = NULL;
2022067d312SJohn Baldwin 		}
2032067d312SJohn Baldwin 		mtx_unlock(&lp_tables_lock);
2042067d312SJohn Baldwin 		break;
2052067d312SJohn Baldwin 	case MOD_LOAD:
2062067d312SJohn Baldwin 	case MOD_QUIESCE:
2072067d312SJohn Baldwin 		break;
2082067d312SJohn Baldwin 	default:
2092067d312SJohn Baldwin 		return (EOPNOTSUPP);
2102067d312SJohn Baldwin 	}
2112067d312SJohn Baldwin 	return (0);
2122067d312SJohn Baldwin }
2132067d312SJohn Baldwin 
2140f063508SPeter Wemm static void
2150f063508SPeter Wemm lp_identify(driver_t *driver, device_t parent)
2160f063508SPeter Wemm {
217a5c7e3bbSGuido van Rooij 	device_t dev;
2180f210c92SNicolas Souchu 
21966de9771SJohn Baldwin 	dev = device_find_child(parent, "plip", -1);
220a5c7e3bbSGuido van Rooij 	if (!dev)
221338cad62SBernd Walter 		BUS_ADD_CHILD(parent, 0, "plip", -1);
2220f063508SPeter Wemm }
223c26864e0SJohn Baldwin 
2240f210c92SNicolas Souchu static int
2250f210c92SNicolas Souchu lp_probe(device_t dev)
22646f3ff79SMike Smith {
22746f3ff79SMike Smith 
2280f210c92SNicolas Souchu 	device_set_desc(dev, "PLIP network interface");
22946f3ff79SMike Smith 
2300f210c92SNicolas Souchu 	return (0);
23146f3ff79SMike Smith }
23246f3ff79SMike Smith 
23346f3ff79SMike Smith static int
2340f210c92SNicolas Souchu lp_attach(device_t dev)
23546f3ff79SMike Smith {
2360f210c92SNicolas Souchu 	struct lp_data *lp = DEVTOSOFTC(dev);
237fc74a9f9SBrooks Davis 	struct ifnet *ifp;
2382067d312SJohn Baldwin 	int error, rid = 0;
239ca3d3795SJohn Baldwin 
240ae6b868aSJohn Baldwin 	lp->sc_dev = dev;
241ae6b868aSJohn Baldwin 
242ca3d3795SJohn Baldwin 	/*
243ca3d3795SJohn Baldwin 	 * Reserve the interrupt resource.  If we don't have one, the
244ca3d3795SJohn Baldwin 	 * attach fails.
245ca3d3795SJohn Baldwin 	 */
246ca3d3795SJohn Baldwin 	lp->res_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
247ca3d3795SJohn Baldwin 	    RF_SHAREABLE);
2484d24901aSPedro F. Giffuni 	if (lp->res_irq == NULL) {
249ca3d3795SJohn Baldwin 		device_printf(dev, "cannot reserve interrupt, failed.\n");
250ca3d3795SJohn Baldwin 		return (ENXIO);
251ca3d3795SJohn Baldwin 	}
252fc74a9f9SBrooks Davis 
253fc74a9f9SBrooks Davis 	ifp = lp->sc_ifp = if_alloc(IFT_PARA);
254fc74a9f9SBrooks Davis 	if (ifp == NULL) {
255fc74a9f9SBrooks Davis 		return (ENOSPC);
256fc74a9f9SBrooks Davis 	}
25746f3ff79SMike Smith 
2580f210c92SNicolas Souchu 	ifp->if_softc = lp;
2599bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
26046f3ff79SMike Smith 	ifp->if_mtu = LPMTU;
2612067d312SJohn Baldwin 	ifp->if_flags = IFF_SIMPLEX | IFF_POINTOPOINT | IFF_MULTICAST;
26246f3ff79SMike Smith 	ifp->if_ioctl = lpioctl;
26346f3ff79SMike Smith 	ifp->if_output = lpoutput;
26446f3ff79SMike Smith 	ifp->if_hdrlen = 0;
26546f3ff79SMike Smith 	ifp->if_addrlen = 0;
266e50d35e6SMaxim Sobolev 	ifp->if_snd.ifq_maxlen = ifqmaxlen;
26746f3ff79SMike Smith 	if_attach(ifp);
26846f3ff79SMike Smith 
2698f84257eSDag-Erling Smørgrav 	bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
27046f3ff79SMike Smith 
2712067d312SJohn Baldwin 	/*
2722067d312SJohn Baldwin 	 * Attach our interrupt handler.  It is only called while we
2732067d312SJohn Baldwin 	 * own the ppbus.
2742067d312SJohn Baldwin 	 */
2752067d312SJohn Baldwin 	error = bus_setup_intr(dev, lp->res_irq, INTR_TYPE_NET | INTR_MPSAFE,
2762067d312SJohn Baldwin 	    NULL, lp_intr, lp, &lp->sc_intr_cookie);
2772067d312SJohn Baldwin 	if (error) {
2782067d312SJohn Baldwin 		bpfdetach(ifp);
2792067d312SJohn Baldwin 		if_detach(ifp);
2802067d312SJohn Baldwin 		bus_release_resource(dev, SYS_RES_IRQ, 0, lp->res_irq);
2812067d312SJohn Baldwin 		device_printf(dev, "Unable to register interrupt handler\n");
2822067d312SJohn Baldwin 		return (error);
2832067d312SJohn Baldwin 	}
2842067d312SJohn Baldwin 
2850f210c92SNicolas Souchu 	return (0);
28646f3ff79SMike Smith }
2872067d312SJohn Baldwin 
2882067d312SJohn Baldwin static int
2892067d312SJohn Baldwin lp_detach(device_t dev)
2902067d312SJohn Baldwin {
2912067d312SJohn Baldwin 	struct lp_data *sc = device_get_softc(dev);
2922067d312SJohn Baldwin 	device_t ppbus = device_get_parent(dev);
2932067d312SJohn Baldwin 
2942067d312SJohn Baldwin 	ppb_lock(ppbus);
2952067d312SJohn Baldwin 	lpstop(sc);
2962067d312SJohn Baldwin 	ppb_unlock(ppbus);
2972067d312SJohn Baldwin 	bpfdetach(sc->sc_ifp);
2982067d312SJohn Baldwin 	if_detach(sc->sc_ifp);
2992067d312SJohn Baldwin 	bus_teardown_intr(dev, sc->res_irq, sc->sc_intr_cookie);
3002067d312SJohn Baldwin 	bus_release_resource(dev, SYS_RES_IRQ, 0, sc->res_irq);
3012067d312SJohn Baldwin 	return (0);
3022067d312SJohn Baldwin }
3032067d312SJohn Baldwin 
30446f3ff79SMike Smith /*
30546f3ff79SMike Smith  * Build the translation tables for the LPIP (BSD unix) protocol.
30646f3ff79SMike Smith  * We don't want to calculate these nasties in our tight loop, so we
30746f3ff79SMike Smith  * precalculate them when we initialize.
30846f3ff79SMike Smith  */
30946f3ff79SMike Smith static int
31046f3ff79SMike Smith lpinittables(void)
31146f3ff79SMike Smith {
31246f3ff79SMike Smith 	int i;
31346f3ff79SMike Smith 
3142067d312SJohn Baldwin 	mtx_lock(&lp_tables_lock);
315c26864e0SJohn Baldwin 	if (txmith == NULL)
31646f3ff79SMike Smith 		txmith = malloc(4 * LPIPTBLSIZE, M_DEVBUF, M_NOWAIT);
31746f3ff79SMike Smith 
3182067d312SJohn Baldwin 	if (txmith == NULL) {
3192067d312SJohn Baldwin 		mtx_unlock(&lp_tables_lock);
320c26864e0SJohn Baldwin 		return (1);
3212067d312SJohn Baldwin 	}
32246f3ff79SMike Smith 
323c26864e0SJohn Baldwin 	if (ctxmith == NULL)
32446f3ff79SMike Smith 		ctxmith = malloc(4 * LPIPTBLSIZE, M_DEVBUF, M_NOWAIT);
32546f3ff79SMike Smith 
3262067d312SJohn Baldwin 	if (ctxmith == NULL) {
3272067d312SJohn Baldwin 		mtx_unlock(&lp_tables_lock);
328c26864e0SJohn Baldwin 		return (1);
3292067d312SJohn Baldwin 	}
33046f3ff79SMike Smith 
33146f3ff79SMike Smith 	for (i = 0; i < LPIPTBLSIZE; i++) {
33246f3ff79SMike Smith 		ctxmith[i] = (i & 0xF0) >> 4;
33346f3ff79SMike Smith 		ctxmitl[i] = 0x10 | (i & 0x0F);
33446f3ff79SMike Smith 		ctrecvh[i] = (i & 0x78) << 1;
33546f3ff79SMike Smith 		ctrecvl[i] = (i & 0x78) >> 3;
33646f3ff79SMike Smith 	}
33746f3ff79SMike Smith 
33846f3ff79SMike Smith 	for (i = 0; i < LPIPTBLSIZE; i++) {
33946f3ff79SMike Smith 		txmith[i] = ((i & 0x80) >> 3) | ((i & 0x70) >> 4) | 0x08;
34046f3ff79SMike Smith 		txmitl[i] = ((i & 0x08) << 1) | (i & 0x07);
34146f3ff79SMike Smith 		trecvh[i] = ((~i) & 0x80) | ((i & 0x38) << 1);
34246f3ff79SMike Smith 		trecvl[i] = (((~i) & 0x80) >> 4) | ((i & 0x38) >> 3);
34346f3ff79SMike Smith 	}
3442067d312SJohn Baldwin 	mtx_unlock(&lp_tables_lock);
34546f3ff79SMike Smith 
346c26864e0SJohn Baldwin 	return (0);
34746f3ff79SMike Smith }
34846f3ff79SMike Smith 
3492067d312SJohn Baldwin static void
3502067d312SJohn Baldwin lpstop(struct lp_data *sc)
3512067d312SJohn Baldwin {
3522067d312SJohn Baldwin 	device_t ppbus = device_get_parent(sc->sc_dev);
3532067d312SJohn Baldwin 
3542067d312SJohn Baldwin 	ppb_assert_locked(ppbus);
3552067d312SJohn Baldwin 	ppb_wctr(ppbus, 0x00);
3562067d312SJohn Baldwin 	sc->sc_ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
3572067d312SJohn Baldwin 	free(sc->sc_ifbuf, M_DEVBUF);
3582067d312SJohn Baldwin 	sc->sc_ifbuf = NULL;
3592067d312SJohn Baldwin 
3602067d312SJohn Baldwin 	/* IFF_UP is not set, try to release the bus anyway */
3612067d312SJohn Baldwin 	ppb_release_bus(ppbus, sc->sc_dev);
3622067d312SJohn Baldwin }
3632067d312SJohn Baldwin 
3642067d312SJohn Baldwin static int
3652067d312SJohn Baldwin lpinit_locked(struct ifnet *ifp)
3662067d312SJohn Baldwin {
3672067d312SJohn Baldwin 	struct lp_data *sc = ifp->if_softc;
3682067d312SJohn Baldwin 	device_t dev = sc->sc_dev;
3692067d312SJohn Baldwin 	device_t ppbus = device_get_parent(dev);
3702067d312SJohn Baldwin 	int error;
3712067d312SJohn Baldwin 
3722067d312SJohn Baldwin 	ppb_assert_locked(ppbus);
3732067d312SJohn Baldwin 	error = ppb_request_bus(ppbus, dev, PPB_DONTWAIT);
3742067d312SJohn Baldwin 	if (error)
3752067d312SJohn Baldwin 		return (error);
3762067d312SJohn Baldwin 
3772067d312SJohn Baldwin 	/* Now IFF_UP means that we own the bus */
3782067d312SJohn Baldwin 	ppb_set_mode(ppbus, PPB_COMPATIBLE);
3792067d312SJohn Baldwin 
3802067d312SJohn Baldwin 	if (lpinittables()) {
3812067d312SJohn Baldwin 		ppb_release_bus(ppbus, dev);
3822067d312SJohn Baldwin 		return (ENOBUFS);
3832067d312SJohn Baldwin 	}
3842067d312SJohn Baldwin 
3852067d312SJohn Baldwin 	sc->sc_ifbuf = malloc(sc->sc_ifp->if_mtu + MLPIPHDRLEN,
3862067d312SJohn Baldwin 	    M_DEVBUF, M_NOWAIT);
3872067d312SJohn Baldwin 	if (sc->sc_ifbuf == NULL) {
3882067d312SJohn Baldwin 		ppb_release_bus(ppbus, dev);
3892067d312SJohn Baldwin 		return (ENOBUFS);
3902067d312SJohn Baldwin 	}
3912067d312SJohn Baldwin 
3922067d312SJohn Baldwin 	ppb_wctr(ppbus, IRQENABLE);
3932067d312SJohn Baldwin 
3942067d312SJohn Baldwin 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
3952067d312SJohn Baldwin 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
3962067d312SJohn Baldwin 	return (0);
3972067d312SJohn Baldwin }
3982067d312SJohn Baldwin 
39946f3ff79SMike Smith /*
40046f3ff79SMike Smith  * Process an ioctl request.
40146f3ff79SMike Smith  */
40246f3ff79SMike Smith static int
40346f3ff79SMike Smith lpioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
40446f3ff79SMike Smith {
405ae6b868aSJohn Baldwin 	struct lp_data *sc = ifp->if_softc;
406ae6b868aSJohn Baldwin 	device_t dev = sc->sc_dev;
4070f210c92SNicolas Souchu 	device_t ppbus = device_get_parent(dev);
40846f3ff79SMike Smith 	struct ifaddr *ifa = (struct ifaddr *)data;
40946f3ff79SMike Smith 	struct ifreq *ifr = (struct ifreq *)data;
41046f3ff79SMike Smith 	u_char *ptr;
41146f3ff79SMike Smith 	int error;
41246f3ff79SMike Smith 
41346f3ff79SMike Smith 	switch (cmd) {
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:
4564d24901aSPedro F. Giffuni 		if (ifr == NULL) {
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 		/* Ack. the request */
5410f210c92SNicolas Souchu 		ppb_wdtr(ppbus, 0x01);
54246f3ff79SMike Smith 
54346f3ff79SMike Smith 		/* Get the packet length */
5440f210c92SNicolas Souchu 		j = clpinbyte(LPMAXSPIN2, ppbus);
54546f3ff79SMike Smith 		if (j == -1)
54646f3ff79SMike Smith 			goto err;
54746f3ff79SMike Smith 		len = j;
5480f210c92SNicolas Souchu 		j = clpinbyte(LPMAXSPIN2, ppbus);
54946f3ff79SMike Smith 		if (j == -1)
55046f3ff79SMike Smith 			goto err;
55146f3ff79SMike Smith 		len = len + (j << 8);
552fc74a9f9SBrooks Davis 		if (len > sc->sc_ifp->if_mtu + MLPIPHDRLEN)
55346f3ff79SMike Smith 			goto err;
55446f3ff79SMike Smith 
55546f3ff79SMike Smith 		bp = sc->sc_ifbuf;
55646f3ff79SMike Smith 
55746f3ff79SMike Smith 		while (len--) {
5580f210c92SNicolas Souchu 			j = clpinbyte(LPMAXSPIN2, ppbus);
55946f3ff79SMike Smith 			if (j == -1) {
56046f3ff79SMike Smith 				goto err;
56146f3ff79SMike Smith 			}
56246f3ff79SMike Smith 			*bp++ = j;
56346f3ff79SMike Smith 		}
564c26864e0SJohn Baldwin 
56546f3ff79SMike Smith 		/* Get and ignore checksum */
5660f210c92SNicolas Souchu 		j = clpinbyte(LPMAXSPIN2, ppbus);
56746f3ff79SMike Smith 		if (j == -1) {
56846f3ff79SMike Smith 			goto err;
56946f3ff79SMike Smith 		}
57046f3ff79SMike Smith 
57146f3ff79SMike Smith 		len = bp - sc->sc_ifbuf;
57246f3ff79SMike Smith 		if (len <= CLPIPHDRLEN)
57346f3ff79SMike Smith 			goto err;
57446f3ff79SMike Smith 
57546f3ff79SMike Smith 		sc->sc_iferrs = 0;
57646f3ff79SMike Smith 
57746f3ff79SMike Smith 		len -= CLPIPHDRLEN;
578c8dfaf38SGleb Smirnoff 		if_inc_counter(sc->sc_ifp, IFCOUNTER_IPACKETS, 1);
579c8dfaf38SGleb Smirnoff 		if_inc_counter(sc->sc_ifp, IFCOUNTER_IBYTES, len);
580c26864e0SJohn Baldwin 		top = m_devget(sc->sc_ifbuf + CLPIPHDRLEN, len, 0, sc->sc_ifp,
581c26864e0SJohn Baldwin 		    0);
58246f3ff79SMike Smith 		if (top) {
5832067d312SJohn Baldwin 			ppb_unlock(ppbus);
5840dea849aSJohn Baldwin 			if (bpf_peers_present(sc->sc_ifp->if_bpf))
585fc74a9f9SBrooks Davis 				lptap(sc->sc_ifp, top);
586c26864e0SJohn Baldwin 
587a34c6aebSBjoern A. Zeeb 			M_SETFIB(top, sc->sc_ifp->if_fib);
588a34c6aebSBjoern A. Zeeb 
589c26864e0SJohn Baldwin 			/* mbuf is free'd on failure. */
590c26864e0SJohn Baldwin 			netisr_queue(NETISR_IP, top);
5912067d312SJohn Baldwin 			ppb_lock(ppbus);
592df5e1987SJonathan Lemon 		}
5932067d312SJohn Baldwin 		return;
59446f3ff79SMike Smith 	}
5950f210c92SNicolas Souchu 	while ((ppb_rstr(ppbus) & LPIP_SHAKE)) {
596fc74a9f9SBrooks Davis 		len = sc->sc_ifp->if_mtu + LPIPHDRLEN;
59746f3ff79SMike Smith 		bp  = sc->sc_ifbuf;
59846f3ff79SMike Smith 		while (len--) {
5990f210c92SNicolas Souchu 			cl = ppb_rstr(ppbus);
6000f210c92SNicolas Souchu 			ppb_wdtr(ppbus, 8);
60146f3ff79SMike Smith 
60246f3ff79SMike Smith 			j = LPMAXSPIN2;
6030f210c92SNicolas Souchu 			while ((ppb_rstr(ppbus) & LPIP_SHAKE))
604c26864e0SJohn Baldwin 				if (!--j)
605c26864e0SJohn Baldwin 					goto err;
60646f3ff79SMike Smith 
6070f210c92SNicolas Souchu 			c = ppb_rstr(ppbus);
6080f210c92SNicolas Souchu 			ppb_wdtr(ppbus, 0);
60946f3ff79SMike Smith 
61046f3ff79SMike Smith 			*bp++= trecvh[cl] | trecvl[c];
61146f3ff79SMike Smith 
61246f3ff79SMike Smith 			j = LPMAXSPIN2;
6130f210c92SNicolas Souchu 			while (!((cl = ppb_rstr(ppbus)) & LPIP_SHAKE)) {
61446f3ff79SMike Smith 				if (cl != c &&
6150f210c92SNicolas Souchu 				    (((cl = ppb_rstr(ppbus)) ^ 0xb8) & 0xf8) ==
61646f3ff79SMike Smith 				    (c & 0xf8))
61746f3ff79SMike Smith 					goto end;
618c26864e0SJohn Baldwin 				if (!--j)
619c26864e0SJohn Baldwin 					goto err;
62046f3ff79SMike Smith 			}
62146f3ff79SMike Smith 		}
62246f3ff79SMike Smith 
62346f3ff79SMike Smith 	end:
62446f3ff79SMike Smith 		len = bp - sc->sc_ifbuf;
62546f3ff79SMike Smith 		if (len <= LPIPHDRLEN)
62646f3ff79SMike Smith 			goto err;
62746f3ff79SMike Smith 
62846f3ff79SMike Smith 		sc->sc_iferrs = 0;
62946f3ff79SMike Smith 
63046f3ff79SMike Smith 		len -= LPIPHDRLEN;
631c8dfaf38SGleb Smirnoff 		if_inc_counter(sc->sc_ifp, IFCOUNTER_IPACKETS, 1);
632c8dfaf38SGleb Smirnoff 		if_inc_counter(sc->sc_ifp, IFCOUNTER_IBYTES, len);
633c26864e0SJohn Baldwin 		top = m_devget(sc->sc_ifbuf + LPIPHDRLEN, len, 0, sc->sc_ifp,
634c26864e0SJohn Baldwin 		    0);
63546f3ff79SMike Smith 		if (top) {
6362067d312SJohn Baldwin 			ppb_unlock(ppbus);
6370dea849aSJohn Baldwin 			if (bpf_peers_present(sc->sc_ifp->if_bpf))
638fc74a9f9SBrooks Davis 				lptap(sc->sc_ifp, top);
639c26864e0SJohn Baldwin 
640a34c6aebSBjoern A. Zeeb 			M_SETFIB(top, sc->sc_ifp->if_fib);
641a34c6aebSBjoern A. Zeeb 
642c26864e0SJohn Baldwin 			/* mbuf is free'd on failure. */
643c26864e0SJohn Baldwin 			netisr_queue(NETISR_IP, top);
6442067d312SJohn Baldwin 			ppb_lock(ppbus);
64546f3ff79SMike Smith 		}
646df5e1987SJonathan Lemon 	}
6472067d312SJohn Baldwin 	return;
64846f3ff79SMike Smith 
64946f3ff79SMike Smith err:
6500f210c92SNicolas Souchu 	ppb_wdtr(ppbus, 0);
65146f3ff79SMike Smith 	lprintf("R");
652c8dfaf38SGleb Smirnoff 	if_inc_counter(sc->sc_ifp, IFCOUNTER_IERRORS, 1);
65346f3ff79SMike Smith 	sc->sc_iferrs++;
65446f3ff79SMike Smith 
65546f3ff79SMike Smith 	/*
65646f3ff79SMike Smith 	 * We are not able to send receive anything for now,
65746f3ff79SMike Smith 	 * so stop wasting our time
65846f3ff79SMike Smith 	 */
65946f3ff79SMike Smith 	if (sc->sc_iferrs > LPMAXERRS) {
660ae6b868aSJohn Baldwin 		if_printf(sc->sc_ifp, "Too many errors, Going off-line.\n");
6610f210c92SNicolas Souchu 		ppb_wctr(ppbus, 0x00);
66213f4c340SRobert Watson 		sc->sc_ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
66346f3ff79SMike Smith 		sc->sc_iferrs = 0;
66446f3ff79SMike Smith 	}
66546f3ff79SMike Smith }
66646f3ff79SMike Smith 
66746f3ff79SMike Smith static __inline int
6680f210c92SNicolas Souchu lpoutbyte(u_char byte, int spin, device_t ppbus)
66946f3ff79SMike Smith {
670c26864e0SJohn Baldwin 
6710f210c92SNicolas Souchu 	ppb_wdtr(ppbus, txmith[byte]);
6720f210c92SNicolas Souchu 	while (!(ppb_rstr(ppbus) & LPIP_SHAKE))
67346f3ff79SMike Smith 		if (--spin == 0)
674c26864e0SJohn Baldwin 			return (1);
6750f210c92SNicolas Souchu 	ppb_wdtr(ppbus, txmitl[byte]);
6760f210c92SNicolas Souchu 	while (ppb_rstr(ppbus) & LPIP_SHAKE)
67746f3ff79SMike Smith 		if (--spin == 0)
678c26864e0SJohn Baldwin 			return (1);
679c26864e0SJohn Baldwin 	return (0);
68046f3ff79SMike Smith }
68146f3ff79SMike Smith 
68246f3ff79SMike Smith static int
68347e8d432SGleb Smirnoff lpoutput(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
684279aa3d4SKip Macy     struct route *ro)
68546f3ff79SMike Smith {
686ae6b868aSJohn Baldwin 	struct lp_data *sc = ifp->if_softc;
687ae6b868aSJohn Baldwin 	device_t dev = sc->sc_dev;
6880f210c92SNicolas Souchu 	device_t ppbus = device_get_parent(dev);
6892067d312SJohn Baldwin 	int err;
69046f3ff79SMike Smith 	struct mbuf *mm;
69146f3ff79SMike Smith 	u_char *cp = "\0\0";
69246f3ff79SMike Smith 	u_char chksum = 0;
69346f3ff79SMike Smith 	int count = 0;
6948f84257eSDag-Erling Smørgrav 	int i, len, spin;
69546f3ff79SMike Smith 
69646f3ff79SMike Smith 	/* We need a sensible value if we abort */
69746f3ff79SMike Smith 	cp++;
6982067d312SJohn Baldwin 	ppb_lock(ppbus);
6992067d312SJohn Baldwin 	ifp->if_drv_flags |= IFF_DRV_OACTIVE;
70046f3ff79SMike Smith 
70146f3ff79SMike Smith 	err = 1;		/* assume we're aborting because of an error */
70246f3ff79SMike Smith 
70346f3ff79SMike Smith 	/* Suspend (on laptops) or receive-errors might have taken us offline */
7040f210c92SNicolas Souchu 	ppb_wctr(ppbus, IRQENABLE);
70546f3ff79SMike Smith 
70646f3ff79SMike Smith 	if (ifp->if_flags & IFF_LINK0) {
7070f210c92SNicolas Souchu 		if (!(ppb_rstr(ppbus) & CLPIP_SHAKE)) {
70846f3ff79SMike Smith 			lprintf("&");
7092067d312SJohn Baldwin 			lp_intr(sc);
71046f3ff79SMike Smith 		}
71146f3ff79SMike Smith 
71246f3ff79SMike Smith 		/* Alert other end to pending packet */
71346f3ff79SMike Smith 		spin = LPMAXSPIN1;
7140f210c92SNicolas Souchu 		ppb_wdtr(ppbus, 0x08);
7150f210c92SNicolas Souchu 		while ((ppb_rstr(ppbus) & 0x08) == 0)
71646f3ff79SMike Smith 			if (--spin == 0) {
71746f3ff79SMike Smith 				goto nend;
71846f3ff79SMike Smith 			}
71946f3ff79SMike Smith 
72046f3ff79SMike Smith 		/* Calculate length of packet, then send that */
72146f3ff79SMike Smith 
72246f3ff79SMike Smith 		count += 14;		/* Ethernet header len */
72346f3ff79SMike Smith 
72446f3ff79SMike Smith 		mm = m;
72546f3ff79SMike Smith 		for (mm = m; mm; mm = mm->m_next) {
72646f3ff79SMike Smith 			count += mm->m_len;
72746f3ff79SMike Smith 		}
7280f210c92SNicolas Souchu 		if (clpoutbyte(count & 0xFF, LPMAXSPIN1, ppbus))
72946f3ff79SMike Smith 			goto nend;
7300f210c92SNicolas Souchu 		if (clpoutbyte((count >> 8) & 0xFF, LPMAXSPIN1, ppbus))
73146f3ff79SMike Smith 			goto nend;
73246f3ff79SMike Smith 
73346f3ff79SMike Smith 		/* Send dummy ethernet header */
73446f3ff79SMike Smith 		for (i = 0; i < 12; i++) {
7350f210c92SNicolas Souchu 			if (clpoutbyte(i, LPMAXSPIN1, ppbus))
73646f3ff79SMike Smith 				goto nend;
73746f3ff79SMike Smith 			chksum += i;
73846f3ff79SMike Smith 		}
73946f3ff79SMike Smith 
7400f210c92SNicolas Souchu 		if (clpoutbyte(0x08, LPMAXSPIN1, ppbus))
74146f3ff79SMike Smith 			goto nend;
7420f210c92SNicolas Souchu 		if (clpoutbyte(0x00, LPMAXSPIN1, ppbus))
74346f3ff79SMike Smith 			goto nend;
74446f3ff79SMike Smith 		chksum += 0x08 + 0x00;		/* Add into checksum */
74546f3ff79SMike Smith 
74646f3ff79SMike Smith 		mm = m;
74746f3ff79SMike Smith 		do {
74846f3ff79SMike Smith 			cp = mtod(mm, u_char *);
7498f84257eSDag-Erling Smørgrav 			len = mm->m_len;
7508f84257eSDag-Erling Smørgrav 			while (len--) {
75146f3ff79SMike Smith 				chksum += *cp;
7520f210c92SNicolas Souchu 				if (clpoutbyte(*cp++, LPMAXSPIN2, ppbus))
75346f3ff79SMike Smith 					goto nend;
75446f3ff79SMike Smith 			}
75546f3ff79SMike Smith 		} while ((mm = mm->m_next));
75646f3ff79SMike Smith 
75746f3ff79SMike Smith 		/* Send checksum */
7580f210c92SNicolas Souchu 		if (clpoutbyte(chksum, LPMAXSPIN2, ppbus))
75946f3ff79SMike Smith 			goto nend;
76046f3ff79SMike Smith 
76146f3ff79SMike Smith 		/* Go quiescent */
7620f210c92SNicolas Souchu 		ppb_wdtr(ppbus, 0);
76346f3ff79SMike Smith 
76446f3ff79SMike Smith 		err = 0;			/* No errors */
76546f3ff79SMike Smith 
76646f3ff79SMike Smith 	nend:
7672067d312SJohn Baldwin 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
76846f3ff79SMike Smith 		if (err)  {			/* if we didn't timeout... */
769c8dfaf38SGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
77046f3ff79SMike Smith 			lprintf("X");
77146f3ff79SMike Smith 		} else {
772c8dfaf38SGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
773c8dfaf38SGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len);
7740dea849aSJohn Baldwin 			if (bpf_peers_present(ifp->if_bpf))
7758f84257eSDag-Erling Smørgrav 				lptap(ifp, m);
77646f3ff79SMike Smith 		}
77746f3ff79SMike Smith 
77846f3ff79SMike Smith 		m_freem(m);
77946f3ff79SMike Smith 
7800f210c92SNicolas Souchu 		if (!(ppb_rstr(ppbus) & CLPIP_SHAKE)) {
78146f3ff79SMike Smith 			lprintf("^");
7822067d312SJohn Baldwin 			lp_intr(sc);
78346f3ff79SMike Smith 		}
7842067d312SJohn Baldwin 		ppb_unlock(ppbus);
785c26864e0SJohn Baldwin 		return (0);
78646f3ff79SMike Smith 	}
78746f3ff79SMike Smith 
7880f210c92SNicolas Souchu 	if (ppb_rstr(ppbus) & LPIP_SHAKE) {
78946f3ff79SMike Smith 		lprintf("&");
7902067d312SJohn Baldwin 		lp_intr(sc);
79146f3ff79SMike Smith 	}
79246f3ff79SMike Smith 
7930f210c92SNicolas Souchu 	if (lpoutbyte(0x08, LPMAXSPIN1, ppbus))
79446f3ff79SMike Smith 		goto end;
7950f210c92SNicolas Souchu 	if (lpoutbyte(0x00, LPMAXSPIN2, ppbus))
79646f3ff79SMike Smith 		goto end;
79746f3ff79SMike Smith 
79846f3ff79SMike Smith 	mm = m;
79946f3ff79SMike Smith 	do {
80046f3ff79SMike Smith 		cp = mtod(mm, u_char *);
8018f84257eSDag-Erling Smørgrav 		len = mm->m_len;
8028f84257eSDag-Erling Smørgrav 		while (len--)
8030f210c92SNicolas Souchu 			if (lpoutbyte(*cp++, LPMAXSPIN2, ppbus))
80446f3ff79SMike Smith 				goto end;
80546f3ff79SMike Smith 	} while ((mm = mm->m_next));
80646f3ff79SMike Smith 
80746f3ff79SMike Smith 	err = 0;			/* no errors were encountered */
80846f3ff79SMike Smith 
80946f3ff79SMike Smith end:
81046f3ff79SMike Smith 	--cp;
8110f210c92SNicolas Souchu 	ppb_wdtr(ppbus, txmitl[*cp] ^ 0x17);
81246f3ff79SMike Smith 
8132067d312SJohn Baldwin 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
81446f3ff79SMike Smith 	if (err)  {			/* if we didn't timeout... */
815c8dfaf38SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
81646f3ff79SMike Smith 		lprintf("X");
81746f3ff79SMike Smith 	} else {
818c8dfaf38SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
819c8dfaf38SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len);
8200dea849aSJohn Baldwin 		if (bpf_peers_present(ifp->if_bpf))
8218f84257eSDag-Erling Smørgrav 			lptap(ifp, m);
82246f3ff79SMike Smith 	}
82346f3ff79SMike Smith 
82446f3ff79SMike Smith 	m_freem(m);
82546f3ff79SMike Smith 
8260f210c92SNicolas Souchu 	if (ppb_rstr(ppbus) & LPIP_SHAKE) {
82746f3ff79SMike Smith 		lprintf("^");
8282067d312SJohn Baldwin 		lp_intr(sc);
82946f3ff79SMike Smith 	}
83046f3ff79SMike Smith 
8312067d312SJohn Baldwin 	ppb_unlock(ppbus);
832c26864e0SJohn Baldwin 	return (0);
83346f3ff79SMike Smith }
8340f210c92SNicolas Souchu 
8350f063508SPeter Wemm static device_method_t lp_methods[] = {
8360f063508SPeter Wemm   	/* device interface */
8370f063508SPeter Wemm 	DEVMETHOD(device_identify,	lp_identify),
8380f063508SPeter Wemm 	DEVMETHOD(device_probe,		lp_probe),
8390f063508SPeter Wemm 	DEVMETHOD(device_attach,	lp_attach),
8402067d312SJohn Baldwin 	DEVMETHOD(device_detach,	lp_detach),
8410f063508SPeter Wemm 	{ 0, 0 }
8420f063508SPeter Wemm };
8430f063508SPeter Wemm 
8440f063508SPeter Wemm static driver_t lp_driver = {
8450f063508SPeter Wemm 	"plip",
8460f063508SPeter Wemm 	lp_methods,
8470f063508SPeter Wemm 	sizeof(struct lp_data),
8480f063508SPeter Wemm };
8490f063508SPeter Wemm 
850*6d588090SJohn Baldwin DRIVER_MODULE(plip, ppbus, lp_driver, lp_module_handler, NULL);
851f5fd5611SRuslan Ermilov MODULE_DEPEND(plip, ppbus, 1, 1, 1);
852