xref: /freebsd/sys/dev/ppbus/if_plip.c (revision a05a680469a7ac77b195021fed74e3aa58152dd7)
146f3ff79SMike Smith /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
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>
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>
10076039bc8SGleb Smirnoff #include <net/if_var.h>
10146f3ff79SMike Smith #include <net/if_types.h>
10246f3ff79SMike Smith #include <net/netisr.h>
103279aa3d4SKip Macy #include <net/route.h>
10446f3ff79SMike Smith 
10546f3ff79SMike Smith #include <netinet/in.h>
10646f3ff79SMike Smith #include <netinet/in_var.h>
10746f3ff79SMike Smith 
10846f3ff79SMike Smith #include <net/bpf.h>
10946f3ff79SMike Smith 
11046f3ff79SMike Smith #include <dev/ppbus/ppbconf.h>
1110f210c92SNicolas Souchu #include "ppbus_if.h"
1120f210c92SNicolas Souchu #include <dev/ppbus/ppbio.h>
1137b7bf77eSNicolas Souchu 
11446f3ff79SMike Smith #ifndef LPMTU			/* MTU for the lp# interfaces */
11546f3ff79SMike Smith #define	LPMTU		1500
11646f3ff79SMike Smith #endif
11746f3ff79SMike Smith 
11846f3ff79SMike Smith #ifndef LPMAXSPIN1		/* DELAY factor for the lp# interfaces */
11946f3ff79SMike Smith #define	LPMAXSPIN1	8000   /* Spinning for remote intr to happen */
12046f3ff79SMike Smith #endif
12146f3ff79SMike Smith 
12246f3ff79SMike Smith #ifndef LPMAXSPIN2		/* DELAY factor for the lp# interfaces */
12346f3ff79SMike Smith #define	LPMAXSPIN2	500	/* Spinning for remote handshake to happen */
12446f3ff79SMike Smith #endif
12546f3ff79SMike Smith 
12646f3ff79SMike Smith #ifndef LPMAXERRS		/* Max errors before !RUNNING */
12746f3ff79SMike Smith #define	LPMAXERRS	100
12846f3ff79SMike Smith #endif
12946f3ff79SMike Smith 
13046f3ff79SMike Smith #define	CLPIPHDRLEN	14	/* We send dummy ethernet addresses (two) + packet type in front of packet */
13146f3ff79SMike Smith #define	CLPIP_SHAKE	0x80	/* This bit toggles between nibble reception */
13246f3ff79SMike Smith #define	MLPIPHDRLEN	CLPIPHDRLEN
13346f3ff79SMike Smith 
13446f3ff79SMike Smith #define	LPIPHDRLEN	2	/* We send 0x08, 0x00 in front of packet */
13546f3ff79SMike Smith #define	LPIP_SHAKE	0x40	/* This bit toggles between nibble reception */
13646f3ff79SMike Smith #if !defined(MLPIPHDRLEN) || LPIPHDRLEN > MLPIPHDRLEN
13746f3ff79SMike Smith #define	MLPIPHDRLEN	LPIPHDRLEN
13846f3ff79SMike Smith #endif
13946f3ff79SMike Smith 
14046f3ff79SMike Smith #define	LPIPTBLSIZE	256	/* Size of octet translation table */
14146f3ff79SMike Smith 
14246f3ff79SMike Smith #define	lprintf		if (lptflag) printf
14320240fa3SNicolas Souchu 
14420240fa3SNicolas Souchu #ifdef PLIP_DEBUG
14546f3ff79SMike Smith static int volatile lptflag = 1;
14620240fa3SNicolas Souchu #else
14720240fa3SNicolas Souchu static int volatile lptflag = 0;
14846f3ff79SMike Smith #endif
14946f3ff79SMike Smith 
1500f210c92SNicolas Souchu struct lp_data {
151fc74a9f9SBrooks Davis 	struct  ifnet	*sc_ifp;
152ae6b868aSJohn Baldwin 	device_t	sc_dev;
15346f3ff79SMike Smith 	u_char		*sc_ifbuf;
15446f3ff79SMike Smith 	int		sc_iferrs;
1550f210c92SNicolas Souchu 
1560f210c92SNicolas Souchu 	struct resource *res_irq;
1572067d312SJohn Baldwin 	void		*sc_intr_cookie;
15846f3ff79SMike Smith };
15946f3ff79SMike Smith 
1602067d312SJohn Baldwin static struct mtx lp_tables_lock;
1612067d312SJohn Baldwin MTX_SYSINIT(lp_tables, &lp_tables_lock, "plip tables", MTX_DEF);
1622067d312SJohn Baldwin 
16346f3ff79SMike Smith /* Tables for the lp# interface */
16446f3ff79SMike Smith static u_char *txmith;
16546f3ff79SMike Smith #define	txmitl (txmith + (1 * LPIPTBLSIZE))
16646f3ff79SMike Smith #define	trecvh (txmith + (2 * LPIPTBLSIZE))
16746f3ff79SMike Smith #define	trecvl (txmith + (3 * LPIPTBLSIZE))
16846f3ff79SMike Smith 
16946f3ff79SMike Smith static u_char *ctxmith;
17046f3ff79SMike Smith #define	ctxmitl (ctxmith + (1 * LPIPTBLSIZE))
17146f3ff79SMike Smith #define	ctrecvh (ctxmith + (2 * LPIPTBLSIZE))
17246f3ff79SMike Smith #define	ctrecvl (ctxmith + (3 * LPIPTBLSIZE))
17346f3ff79SMike Smith 
17446f3ff79SMike Smith /* Functions for the lp# interface */
17546f3ff79SMike Smith static int lpinittables(void);
1762568cd2aSJustin Hibbits static int lpioctl(if_t, u_long, caddr_t);
1772568cd2aSJustin Hibbits static int lpoutput(if_t, struct mbuf *, const struct sockaddr *,
178279aa3d4SKip Macy        struct route *);
1792067d312SJohn Baldwin static void lpstop(struct lp_data *);
1800f210c92SNicolas Souchu static void lp_intr(void *);
1812067d312SJohn Baldwin static int lp_module_handler(module_t, int, void *);
18246f3ff79SMike Smith 
1830f210c92SNicolas Souchu #define	DEVTOSOFTC(dev) \
1840f210c92SNicolas Souchu 	((struct lp_data *)device_get_softc(dev))
18546f3ff79SMike Smith 
1862067d312SJohn Baldwin static int
1872067d312SJohn Baldwin lp_module_handler(module_t mod, int what, void *arg)
1882067d312SJohn Baldwin {
1892067d312SJohn Baldwin 
1902067d312SJohn Baldwin 	switch (what) {
1912067d312SJohn Baldwin 	case MOD_UNLOAD:
1922067d312SJohn Baldwin 		mtx_lock(&lp_tables_lock);
1932067d312SJohn Baldwin 		if (txmith != NULL) {
1942067d312SJohn Baldwin 			free(txmith, M_DEVBUF);
1952067d312SJohn Baldwin 			txmith = NULL;
1962067d312SJohn Baldwin 		}
1972067d312SJohn Baldwin 		if (ctxmith != NULL) {
1982067d312SJohn Baldwin 			free(ctxmith, M_DEVBUF);
1992067d312SJohn Baldwin 			ctxmith = NULL;
2002067d312SJohn Baldwin 		}
2012067d312SJohn Baldwin 		mtx_unlock(&lp_tables_lock);
2022067d312SJohn Baldwin 		break;
2032067d312SJohn Baldwin 	case MOD_LOAD:
2042067d312SJohn Baldwin 	case MOD_QUIESCE:
2052067d312SJohn Baldwin 		break;
2062067d312SJohn Baldwin 	default:
2072067d312SJohn Baldwin 		return (EOPNOTSUPP);
2082067d312SJohn Baldwin 	}
2092067d312SJohn Baldwin 	return (0);
2102067d312SJohn Baldwin }
2112067d312SJohn Baldwin 
2120f063508SPeter Wemm static void
2130f063508SPeter Wemm lp_identify(driver_t *driver, device_t parent)
2140f063508SPeter Wemm {
215a5c7e3bbSGuido van Rooij 	device_t dev;
2160f210c92SNicolas Souchu 
21766de9771SJohn Baldwin 	dev = device_find_child(parent, "plip", -1);
218a5c7e3bbSGuido van Rooij 	if (!dev)
219*a05a6804SWarner Losh 		BUS_ADD_CHILD(parent, 0, "plip", DEVICE_UNIT_ANY);
2200f063508SPeter Wemm }
221c26864e0SJohn Baldwin 
2220f210c92SNicolas Souchu static int
2230f210c92SNicolas Souchu lp_probe(device_t dev)
22446f3ff79SMike Smith {
22546f3ff79SMike Smith 
2260f210c92SNicolas Souchu 	device_set_desc(dev, "PLIP network interface");
22746f3ff79SMike Smith 
2280f210c92SNicolas Souchu 	return (0);
22946f3ff79SMike Smith }
23046f3ff79SMike Smith 
23146f3ff79SMike Smith static int
2320f210c92SNicolas Souchu lp_attach(device_t dev)
23346f3ff79SMike Smith {
2340f210c92SNicolas Souchu 	struct lp_data *lp = DEVTOSOFTC(dev);
2352568cd2aSJustin Hibbits 	if_t ifp;
2362067d312SJohn Baldwin 	int error, rid = 0;
237ca3d3795SJohn Baldwin 
238ae6b868aSJohn Baldwin 	lp->sc_dev = dev;
239ae6b868aSJohn Baldwin 
240ca3d3795SJohn Baldwin 	/*
241ca3d3795SJohn Baldwin 	 * Reserve the interrupt resource.  If we don't have one, the
242ca3d3795SJohn Baldwin 	 * attach fails.
243ca3d3795SJohn Baldwin 	 */
244ca3d3795SJohn Baldwin 	lp->res_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
245ca3d3795SJohn Baldwin 	    RF_SHAREABLE);
2464d24901aSPedro F. Giffuni 	if (lp->res_irq == NULL) {
247ca3d3795SJohn Baldwin 		device_printf(dev, "cannot reserve interrupt, failed.\n");
248ca3d3795SJohn Baldwin 		return (ENXIO);
249ca3d3795SJohn Baldwin 	}
250fc74a9f9SBrooks Davis 
251fc74a9f9SBrooks Davis 	ifp = lp->sc_ifp = if_alloc(IFT_PARA);
2522568cd2aSJustin Hibbits 	if_setsoftc(ifp, lp);
2539bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
2542568cd2aSJustin Hibbits 	if_setmtu(ifp, LPMTU);
2552568cd2aSJustin Hibbits 	if_setflags(ifp, IFF_SIMPLEX | IFF_POINTOPOINT | IFF_MULTICAST);
2562568cd2aSJustin Hibbits 	if_setioctlfn(ifp, lpioctl);
2572568cd2aSJustin Hibbits 	if_setoutputfn(ifp, lpoutput);
2582568cd2aSJustin Hibbits 	if_setsendqlen(ifp, ifqmaxlen);
25946f3ff79SMike Smith 	if_attach(ifp);
26046f3ff79SMike Smith 
2618f84257eSDag-Erling Smørgrav 	bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
26246f3ff79SMike Smith 
2632067d312SJohn Baldwin 	/*
2642067d312SJohn Baldwin 	 * Attach our interrupt handler.  It is only called while we
2652067d312SJohn Baldwin 	 * own the ppbus.
2662067d312SJohn Baldwin 	 */
2672067d312SJohn Baldwin 	error = bus_setup_intr(dev, lp->res_irq, INTR_TYPE_NET | INTR_MPSAFE,
2682067d312SJohn Baldwin 	    NULL, lp_intr, lp, &lp->sc_intr_cookie);
2692067d312SJohn Baldwin 	if (error) {
2702067d312SJohn Baldwin 		bpfdetach(ifp);
2712067d312SJohn Baldwin 		if_detach(ifp);
2722067d312SJohn Baldwin 		bus_release_resource(dev, SYS_RES_IRQ, 0, lp->res_irq);
2732067d312SJohn Baldwin 		device_printf(dev, "Unable to register interrupt handler\n");
2742067d312SJohn Baldwin 		return (error);
2752067d312SJohn Baldwin 	}
2762067d312SJohn Baldwin 
2770f210c92SNicolas Souchu 	return (0);
27846f3ff79SMike Smith }
2792067d312SJohn Baldwin 
2802067d312SJohn Baldwin static int
2812067d312SJohn Baldwin lp_detach(device_t dev)
2822067d312SJohn Baldwin {
2832067d312SJohn Baldwin 	struct lp_data *sc = device_get_softc(dev);
2842067d312SJohn Baldwin 	device_t ppbus = device_get_parent(dev);
2852067d312SJohn Baldwin 
2862067d312SJohn Baldwin 	ppb_lock(ppbus);
2872067d312SJohn Baldwin 	lpstop(sc);
2882067d312SJohn Baldwin 	ppb_unlock(ppbus);
2892067d312SJohn Baldwin 	bpfdetach(sc->sc_ifp);
2902067d312SJohn Baldwin 	if_detach(sc->sc_ifp);
2912067d312SJohn Baldwin 	bus_teardown_intr(dev, sc->res_irq, sc->sc_intr_cookie);
2922067d312SJohn Baldwin 	bus_release_resource(dev, SYS_RES_IRQ, 0, sc->res_irq);
2932067d312SJohn Baldwin 	return (0);
2942067d312SJohn Baldwin }
2952067d312SJohn Baldwin 
29646f3ff79SMike Smith /*
29746f3ff79SMike Smith  * Build the translation tables for the LPIP (BSD unix) protocol.
29846f3ff79SMike Smith  * We don't want to calculate these nasties in our tight loop, so we
29946f3ff79SMike Smith  * precalculate them when we initialize.
30046f3ff79SMike Smith  */
30146f3ff79SMike Smith static int
30246f3ff79SMike Smith lpinittables(void)
30346f3ff79SMike Smith {
30446f3ff79SMike Smith 	int i;
30546f3ff79SMike Smith 
3062067d312SJohn Baldwin 	mtx_lock(&lp_tables_lock);
307c26864e0SJohn Baldwin 	if (txmith == NULL)
30846f3ff79SMike Smith 		txmith = malloc(4 * LPIPTBLSIZE, M_DEVBUF, M_NOWAIT);
30946f3ff79SMike Smith 
3102067d312SJohn Baldwin 	if (txmith == NULL) {
3112067d312SJohn Baldwin 		mtx_unlock(&lp_tables_lock);
312c26864e0SJohn Baldwin 		return (1);
3132067d312SJohn Baldwin 	}
31446f3ff79SMike Smith 
315c26864e0SJohn Baldwin 	if (ctxmith == NULL)
31646f3ff79SMike Smith 		ctxmith = malloc(4 * LPIPTBLSIZE, M_DEVBUF, M_NOWAIT);
31746f3ff79SMike Smith 
3182067d312SJohn Baldwin 	if (ctxmith == NULL) {
3192067d312SJohn Baldwin 		mtx_unlock(&lp_tables_lock);
320c26864e0SJohn Baldwin 		return (1);
3212067d312SJohn Baldwin 	}
32246f3ff79SMike Smith 
32346f3ff79SMike Smith 	for (i = 0; i < LPIPTBLSIZE; i++) {
32446f3ff79SMike Smith 		ctxmith[i] = (i & 0xF0) >> 4;
32546f3ff79SMike Smith 		ctxmitl[i] = 0x10 | (i & 0x0F);
32646f3ff79SMike Smith 		ctrecvh[i] = (i & 0x78) << 1;
32746f3ff79SMike Smith 		ctrecvl[i] = (i & 0x78) >> 3;
32846f3ff79SMike Smith 	}
32946f3ff79SMike Smith 
33046f3ff79SMike Smith 	for (i = 0; i < LPIPTBLSIZE; i++) {
33146f3ff79SMike Smith 		txmith[i] = ((i & 0x80) >> 3) | ((i & 0x70) >> 4) | 0x08;
33246f3ff79SMike Smith 		txmitl[i] = ((i & 0x08) << 1) | (i & 0x07);
33346f3ff79SMike Smith 		trecvh[i] = ((~i) & 0x80) | ((i & 0x38) << 1);
33446f3ff79SMike Smith 		trecvl[i] = (((~i) & 0x80) >> 4) | ((i & 0x38) >> 3);
33546f3ff79SMike Smith 	}
3362067d312SJohn Baldwin 	mtx_unlock(&lp_tables_lock);
33746f3ff79SMike Smith 
338c26864e0SJohn Baldwin 	return (0);
33946f3ff79SMike Smith }
34046f3ff79SMike Smith 
3412067d312SJohn Baldwin static void
3422067d312SJohn Baldwin lpstop(struct lp_data *sc)
3432067d312SJohn Baldwin {
3442067d312SJohn Baldwin 	device_t ppbus = device_get_parent(sc->sc_dev);
3452067d312SJohn Baldwin 
3462067d312SJohn Baldwin 	ppb_assert_locked(ppbus);
3472067d312SJohn Baldwin 	ppb_wctr(ppbus, 0x00);
3482568cd2aSJustin Hibbits 	if_setdrvflagbits(sc->sc_ifp, 0, (IFF_DRV_RUNNING | IFF_DRV_OACTIVE));
3492067d312SJohn Baldwin 	free(sc->sc_ifbuf, M_DEVBUF);
3502067d312SJohn Baldwin 	sc->sc_ifbuf = NULL;
3512067d312SJohn Baldwin 
3522067d312SJohn Baldwin 	/* IFF_UP is not set, try to release the bus anyway */
3532067d312SJohn Baldwin 	ppb_release_bus(ppbus, sc->sc_dev);
3542067d312SJohn Baldwin }
3552067d312SJohn Baldwin 
3562067d312SJohn Baldwin static int
3572568cd2aSJustin Hibbits lpinit_locked(if_t ifp)
3582067d312SJohn Baldwin {
3592568cd2aSJustin Hibbits 	struct lp_data *sc = if_getsoftc(ifp);
3602067d312SJohn Baldwin 	device_t dev = sc->sc_dev;
3612067d312SJohn Baldwin 	device_t ppbus = device_get_parent(dev);
3622067d312SJohn Baldwin 	int error;
3632067d312SJohn Baldwin 
3642067d312SJohn Baldwin 	ppb_assert_locked(ppbus);
3652067d312SJohn Baldwin 	error = ppb_request_bus(ppbus, dev, PPB_DONTWAIT);
3662067d312SJohn Baldwin 	if (error)
3672067d312SJohn Baldwin 		return (error);
3682067d312SJohn Baldwin 
3692067d312SJohn Baldwin 	/* Now IFF_UP means that we own the bus */
3702067d312SJohn Baldwin 	ppb_set_mode(ppbus, PPB_COMPATIBLE);
3712067d312SJohn Baldwin 
3722067d312SJohn Baldwin 	if (lpinittables()) {
3732067d312SJohn Baldwin 		ppb_release_bus(ppbus, dev);
3742067d312SJohn Baldwin 		return (ENOBUFS);
3752067d312SJohn Baldwin 	}
3762067d312SJohn Baldwin 
3772568cd2aSJustin Hibbits 	sc->sc_ifbuf = malloc(if_getmtu(sc->sc_ifp) + MLPIPHDRLEN,
3782067d312SJohn Baldwin 	    M_DEVBUF, M_NOWAIT);
3792067d312SJohn Baldwin 	if (sc->sc_ifbuf == NULL) {
3802067d312SJohn Baldwin 		ppb_release_bus(ppbus, dev);
3812067d312SJohn Baldwin 		return (ENOBUFS);
3822067d312SJohn Baldwin 	}
3832067d312SJohn Baldwin 
3842067d312SJohn Baldwin 	ppb_wctr(ppbus, IRQENABLE);
3852067d312SJohn Baldwin 
3862568cd2aSJustin Hibbits 	if_setdrvflagbits(ifp, IFF_DRV_RUNNING, 0);
3872568cd2aSJustin Hibbits 	if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
3882067d312SJohn Baldwin 	return (0);
3892067d312SJohn Baldwin }
3902067d312SJohn Baldwin 
39146f3ff79SMike Smith /*
39246f3ff79SMike Smith  * Process an ioctl request.
39346f3ff79SMike Smith  */
39446f3ff79SMike Smith static int
3952568cd2aSJustin Hibbits lpioctl(if_t ifp, u_long cmd, caddr_t data)
39646f3ff79SMike Smith {
3972568cd2aSJustin Hibbits 	struct lp_data *sc = if_getsoftc(ifp);
398ae6b868aSJohn Baldwin 	device_t dev = sc->sc_dev;
3990f210c92SNicolas Souchu 	device_t ppbus = device_get_parent(dev);
40046f3ff79SMike Smith 	struct ifaddr *ifa = (struct ifaddr *)data;
40146f3ff79SMike Smith 	struct ifreq *ifr = (struct ifreq *)data;
40246f3ff79SMike Smith 	u_char *ptr;
40346f3ff79SMike Smith 	int error;
40446f3ff79SMike Smith 
40546f3ff79SMike Smith 	switch (cmd) {
40646f3ff79SMike Smith 	case SIOCAIFADDR:
40746f3ff79SMike Smith 	case SIOCSIFADDR:
40846f3ff79SMike Smith 		if (ifa->ifa_addr->sa_family != AF_INET)
409c26864e0SJohn Baldwin 			return (EAFNOSUPPORT);
41046f3ff79SMike Smith 
4112568cd2aSJustin Hibbits 		if_setflagbits(ifp, IFF_UP, 0);
41246f3ff79SMike Smith 		/* FALLTHROUGH */
41346f3ff79SMike Smith 	case SIOCSIFFLAGS:
4142067d312SJohn Baldwin 		error = 0;
4152067d312SJohn Baldwin 		ppb_lock(ppbus);
4162568cd2aSJustin Hibbits 		if ((!(if_getflags(ifp) & IFF_UP)) &&
4172568cd2aSJustin Hibbits 		    (if_getdrvflags(ifp) & IFF_DRV_RUNNING))
4182067d312SJohn Baldwin 			lpstop(sc);
4192568cd2aSJustin Hibbits 		else if (((if_getflags(ifp) & IFF_UP)) &&
4202568cd2aSJustin Hibbits 		    (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING)))
4212067d312SJohn Baldwin 			error = lpinit_locked(ifp);
4222067d312SJohn Baldwin 		ppb_unlock(ppbus);
42346f3ff79SMike Smith 		return (error);
42446f3ff79SMike Smith 
42546f3ff79SMike Smith 	case SIOCSIFMTU:
4262067d312SJohn Baldwin 		ppb_lock(ppbus);
4272568cd2aSJustin Hibbits 		if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
4282067d312SJohn Baldwin 			ptr = malloc(ifr->ifr_mtu + MLPIPHDRLEN, M_DEVBUF,
429c26864e0SJohn Baldwin 			    M_NOWAIT);
4302067d312SJohn Baldwin 			if (ptr == NULL) {
4312067d312SJohn Baldwin 				ppb_unlock(ppbus);
432c26864e0SJohn Baldwin 				return (ENOBUFS);
43346f3ff79SMike Smith 			}
4342067d312SJohn Baldwin 			if (sc->sc_ifbuf)
4352067d312SJohn Baldwin 				free(sc->sc_ifbuf, M_DEVBUF);
4362067d312SJohn Baldwin 			sc->sc_ifbuf = ptr;
4372067d312SJohn Baldwin 		}
4382568cd2aSJustin Hibbits 		if_setmtu(ifp, ifr->ifr_mtu);
4392067d312SJohn Baldwin 		ppb_unlock(ppbus);
44046f3ff79SMike Smith 		break;
44146f3ff79SMike Smith 
44246f3ff79SMike Smith 	case SIOCGIFMTU:
4432568cd2aSJustin Hibbits 		ifr->ifr_mtu = if_getmtu(sc->sc_ifp);
44446f3ff79SMike Smith 		break;
44546f3ff79SMike Smith 
44646f3ff79SMike Smith 	case SIOCADDMULTI:
44746f3ff79SMike Smith 	case SIOCDELMULTI:
4484d24901aSPedro F. Giffuni 		if (ifr == NULL) {
449c26864e0SJohn Baldwin 			return (EAFNOSUPPORT);		/* XXX */
45046f3ff79SMike Smith 		}
45146f3ff79SMike Smith 		switch (ifr->ifr_addr.sa_family) {
45246f3ff79SMike Smith 		case AF_INET:
45346f3ff79SMike Smith 			break;
45446f3ff79SMike Smith 		default:
455c26864e0SJohn Baldwin 			return (EAFNOSUPPORT);
45646f3ff79SMike Smith 		}
45746f3ff79SMike Smith 		break;
45846f3ff79SMike Smith 
45980015f13SMike Smith 	case SIOCGIFMEDIA:
46080015f13SMike Smith 		/*
46180015f13SMike Smith 		 * No ifmedia support at this stage; maybe use it
46280015f13SMike Smith 		 * in future for eg. protocol selection.
46380015f13SMike Smith 		 */
464c26864e0SJohn Baldwin 		return (EINVAL);
46580015f13SMike Smith 
46646f3ff79SMike Smith 	default:
467162886e2SBruce Evans 		lprintf("LP:ioctl(0x%lx)\n", cmd);
468c26864e0SJohn Baldwin 		return (EINVAL);
46946f3ff79SMike Smith 	}
470c26864e0SJohn Baldwin 	return (0);
47146f3ff79SMike Smith }
47246f3ff79SMike Smith 
47346f3ff79SMike Smith static __inline int
4740f210c92SNicolas Souchu clpoutbyte(u_char byte, int spin, device_t ppbus)
47546f3ff79SMike Smith {
476c26864e0SJohn Baldwin 
4770f210c92SNicolas Souchu 	ppb_wdtr(ppbus, ctxmitl[byte]);
4780f210c92SNicolas Souchu 	while (ppb_rstr(ppbus) & CLPIP_SHAKE)
47946f3ff79SMike Smith 		if (--spin == 0) {
480c26864e0SJohn Baldwin 			return (1);
48146f3ff79SMike Smith 		}
4820f210c92SNicolas Souchu 	ppb_wdtr(ppbus, ctxmith[byte]);
4830f210c92SNicolas Souchu 	while (!(ppb_rstr(ppbus) & CLPIP_SHAKE))
48446f3ff79SMike Smith 		if (--spin == 0) {
485c26864e0SJohn Baldwin 			return (1);
48646f3ff79SMike Smith 		}
487c26864e0SJohn Baldwin 	return (0);
48846f3ff79SMike Smith }
48946f3ff79SMike Smith 
49046f3ff79SMike Smith static __inline int
4910f210c92SNicolas Souchu clpinbyte(int spin, device_t ppbus)
49246f3ff79SMike Smith {
49324063475SNicolas Souchu 	u_char c, cl;
49446f3ff79SMike Smith 
4950f210c92SNicolas Souchu 	while ((ppb_rstr(ppbus) & CLPIP_SHAKE))
49646f3ff79SMike Smith 		if (!--spin) {
497c26864e0SJohn Baldwin 			return (-1);
49846f3ff79SMike Smith 		}
4990f210c92SNicolas Souchu 	cl = ppb_rstr(ppbus);
5000f210c92SNicolas Souchu 	ppb_wdtr(ppbus, 0x10);
50146f3ff79SMike Smith 
5020f210c92SNicolas Souchu 	while (!(ppb_rstr(ppbus) & CLPIP_SHAKE))
50346f3ff79SMike Smith 		if (!--spin) {
504c26864e0SJohn Baldwin 			return (-1);
50546f3ff79SMike Smith 		}
5060f210c92SNicolas Souchu 	c = ppb_rstr(ppbus);
5070f210c92SNicolas Souchu 	ppb_wdtr(ppbus, 0x00);
50846f3ff79SMike Smith 
50946f3ff79SMike Smith 	return (ctrecvl[cl] | ctrecvh[c]);
51046f3ff79SMike Smith }
51146f3ff79SMike Smith 
5128f84257eSDag-Erling Smørgrav static void
5132568cd2aSJustin Hibbits lptap(if_t ifp, struct mbuf *m)
5148f84257eSDag-Erling Smørgrav {
5158f84257eSDag-Erling Smørgrav 	u_int32_t af = AF_INET;
516c26864e0SJohn Baldwin 
5172568cd2aSJustin Hibbits 	bpf_mtap2_if(ifp, &af, sizeof(af), m);
5188f84257eSDag-Erling Smørgrav }
5198f84257eSDag-Erling Smørgrav 
52046f3ff79SMike Smith static void
5210f210c92SNicolas Souchu lp_intr(void *arg)
52246f3ff79SMike Smith {
5232067d312SJohn Baldwin 	struct lp_data *sc = arg;
5242067d312SJohn Baldwin 	device_t ppbus = device_get_parent(sc->sc_dev);
5252067d312SJohn Baldwin 	int len, j;
52646f3ff79SMike Smith 	u_char *bp;
52746f3ff79SMike Smith 	u_char c, cl;
52846f3ff79SMike Smith 	struct mbuf *top;
52946f3ff79SMike Smith 
5302067d312SJohn Baldwin 	ppb_assert_locked(ppbus);
5312568cd2aSJustin Hibbits 	if (if_getflags(sc->sc_ifp) & IFF_LINK0) {
53246f3ff79SMike Smith 		/* Ack. the request */
5330f210c92SNicolas Souchu 		ppb_wdtr(ppbus, 0x01);
53446f3ff79SMike Smith 
53546f3ff79SMike Smith 		/* Get the packet length */
5360f210c92SNicolas Souchu 		j = clpinbyte(LPMAXSPIN2, ppbus);
53746f3ff79SMike Smith 		if (j == -1)
53846f3ff79SMike Smith 			goto err;
53946f3ff79SMike Smith 		len = j;
5400f210c92SNicolas Souchu 		j = clpinbyte(LPMAXSPIN2, ppbus);
54146f3ff79SMike Smith 		if (j == -1)
54246f3ff79SMike Smith 			goto err;
54346f3ff79SMike Smith 		len = len + (j << 8);
5442568cd2aSJustin Hibbits 		if (len > if_getmtu(sc->sc_ifp) + MLPIPHDRLEN)
54546f3ff79SMike Smith 			goto err;
54646f3ff79SMike Smith 
54746f3ff79SMike Smith 		bp = sc->sc_ifbuf;
54846f3ff79SMike Smith 
54946f3ff79SMike Smith 		while (len--) {
5500f210c92SNicolas Souchu 			j = clpinbyte(LPMAXSPIN2, ppbus);
55146f3ff79SMike Smith 			if (j == -1) {
55246f3ff79SMike Smith 				goto err;
55346f3ff79SMike Smith 			}
55446f3ff79SMike Smith 			*bp++ = j;
55546f3ff79SMike Smith 		}
556c26864e0SJohn Baldwin 
55746f3ff79SMike Smith 		/* Get and ignore checksum */
5580f210c92SNicolas Souchu 		j = clpinbyte(LPMAXSPIN2, ppbus);
55946f3ff79SMike Smith 		if (j == -1) {
56046f3ff79SMike Smith 			goto err;
56146f3ff79SMike Smith 		}
56246f3ff79SMike Smith 
56346f3ff79SMike Smith 		len = bp - sc->sc_ifbuf;
56446f3ff79SMike Smith 		if (len <= CLPIPHDRLEN)
56546f3ff79SMike Smith 			goto err;
56646f3ff79SMike Smith 
56746f3ff79SMike Smith 		sc->sc_iferrs = 0;
56846f3ff79SMike Smith 
56946f3ff79SMike Smith 		len -= CLPIPHDRLEN;
570c8dfaf38SGleb Smirnoff 		if_inc_counter(sc->sc_ifp, IFCOUNTER_IPACKETS, 1);
571c8dfaf38SGleb Smirnoff 		if_inc_counter(sc->sc_ifp, IFCOUNTER_IBYTES, len);
572c26864e0SJohn Baldwin 		top = m_devget(sc->sc_ifbuf + CLPIPHDRLEN, len, 0, sc->sc_ifp,
573c26864e0SJohn Baldwin 		    0);
57446f3ff79SMike Smith 		if (top) {
5752067d312SJohn Baldwin 			ppb_unlock(ppbus);
576fc74a9f9SBrooks Davis 			lptap(sc->sc_ifp, top);
577c26864e0SJohn Baldwin 
5782568cd2aSJustin Hibbits 			M_SETFIB(top, if_getfib(sc->sc_ifp));
579a34c6aebSBjoern A. Zeeb 
580c26864e0SJohn Baldwin 			/* mbuf is free'd on failure. */
581c26864e0SJohn Baldwin 			netisr_queue(NETISR_IP, top);
5822067d312SJohn Baldwin 			ppb_lock(ppbus);
583df5e1987SJonathan Lemon 		}
5842067d312SJohn Baldwin 		return;
58546f3ff79SMike Smith 	}
5860f210c92SNicolas Souchu 	while ((ppb_rstr(ppbus) & LPIP_SHAKE)) {
5872568cd2aSJustin Hibbits 		len = if_getmtu(sc->sc_ifp) + LPIPHDRLEN;
58846f3ff79SMike Smith 		bp  = sc->sc_ifbuf;
58946f3ff79SMike Smith 		while (len--) {
5900f210c92SNicolas Souchu 			cl = ppb_rstr(ppbus);
5910f210c92SNicolas Souchu 			ppb_wdtr(ppbus, 8);
59246f3ff79SMike Smith 
59346f3ff79SMike Smith 			j = LPMAXSPIN2;
5940f210c92SNicolas Souchu 			while ((ppb_rstr(ppbus) & LPIP_SHAKE))
595c26864e0SJohn Baldwin 				if (!--j)
596c26864e0SJohn Baldwin 					goto err;
59746f3ff79SMike Smith 
5980f210c92SNicolas Souchu 			c = ppb_rstr(ppbus);
5990f210c92SNicolas Souchu 			ppb_wdtr(ppbus, 0);
60046f3ff79SMike Smith 
60146f3ff79SMike Smith 			*bp++= trecvh[cl] | trecvl[c];
60246f3ff79SMike Smith 
60346f3ff79SMike Smith 			j = LPMAXSPIN2;
6040f210c92SNicolas Souchu 			while (!((cl = ppb_rstr(ppbus)) & LPIP_SHAKE)) {
60546f3ff79SMike Smith 				if (cl != c &&
6060f210c92SNicolas Souchu 				    (((cl = ppb_rstr(ppbus)) ^ 0xb8) & 0xf8) ==
60746f3ff79SMike Smith 				    (c & 0xf8))
60846f3ff79SMike Smith 					goto end;
609c26864e0SJohn Baldwin 				if (!--j)
610c26864e0SJohn Baldwin 					goto err;
61146f3ff79SMike Smith 			}
61246f3ff79SMike Smith 		}
61346f3ff79SMike Smith 
61446f3ff79SMike Smith 	end:
61546f3ff79SMike Smith 		len = bp - sc->sc_ifbuf;
61646f3ff79SMike Smith 		if (len <= LPIPHDRLEN)
61746f3ff79SMike Smith 			goto err;
61846f3ff79SMike Smith 
61946f3ff79SMike Smith 		sc->sc_iferrs = 0;
62046f3ff79SMike Smith 
62146f3ff79SMike Smith 		len -= LPIPHDRLEN;
622c8dfaf38SGleb Smirnoff 		if_inc_counter(sc->sc_ifp, IFCOUNTER_IPACKETS, 1);
623c8dfaf38SGleb Smirnoff 		if_inc_counter(sc->sc_ifp, IFCOUNTER_IBYTES, len);
624c26864e0SJohn Baldwin 		top = m_devget(sc->sc_ifbuf + LPIPHDRLEN, len, 0, sc->sc_ifp,
625c26864e0SJohn Baldwin 		    0);
62646f3ff79SMike Smith 		if (top) {
6272067d312SJohn Baldwin 			ppb_unlock(ppbus);
628fc74a9f9SBrooks Davis 			lptap(sc->sc_ifp, top);
629c26864e0SJohn Baldwin 
6302568cd2aSJustin Hibbits 			M_SETFIB(top, if_getfib(sc->sc_ifp));
631a34c6aebSBjoern A. Zeeb 
632c26864e0SJohn Baldwin 			/* mbuf is free'd on failure. */
633c26864e0SJohn Baldwin 			netisr_queue(NETISR_IP, top);
6342067d312SJohn Baldwin 			ppb_lock(ppbus);
63546f3ff79SMike Smith 		}
636df5e1987SJonathan Lemon 	}
6372067d312SJohn Baldwin 	return;
63846f3ff79SMike Smith 
63946f3ff79SMike Smith err:
6400f210c92SNicolas Souchu 	ppb_wdtr(ppbus, 0);
64146f3ff79SMike Smith 	lprintf("R");
642c8dfaf38SGleb Smirnoff 	if_inc_counter(sc->sc_ifp, IFCOUNTER_IERRORS, 1);
64346f3ff79SMike Smith 	sc->sc_iferrs++;
64446f3ff79SMike Smith 
64546f3ff79SMike Smith 	/*
64646f3ff79SMike Smith 	 * We are not able to send receive anything for now,
64746f3ff79SMike Smith 	 * so stop wasting our time
64846f3ff79SMike Smith 	 */
64946f3ff79SMike Smith 	if (sc->sc_iferrs > LPMAXERRS) {
650ae6b868aSJohn Baldwin 		if_printf(sc->sc_ifp, "Too many errors, Going off-line.\n");
6510f210c92SNicolas Souchu 		ppb_wctr(ppbus, 0x00);
6522568cd2aSJustin Hibbits 		if_setdrvflagbits(sc->sc_ifp, 0, IFF_DRV_RUNNING);
65346f3ff79SMike Smith 		sc->sc_iferrs = 0;
65446f3ff79SMike Smith 	}
65546f3ff79SMike Smith }
65646f3ff79SMike Smith 
65746f3ff79SMike Smith static __inline int
6580f210c92SNicolas Souchu lpoutbyte(u_char byte, int spin, device_t ppbus)
65946f3ff79SMike Smith {
660c26864e0SJohn Baldwin 
6610f210c92SNicolas Souchu 	ppb_wdtr(ppbus, txmith[byte]);
6620f210c92SNicolas Souchu 	while (!(ppb_rstr(ppbus) & LPIP_SHAKE))
66346f3ff79SMike Smith 		if (--spin == 0)
664c26864e0SJohn Baldwin 			return (1);
6650f210c92SNicolas Souchu 	ppb_wdtr(ppbus, txmitl[byte]);
6660f210c92SNicolas Souchu 	while (ppb_rstr(ppbus) & LPIP_SHAKE)
66746f3ff79SMike Smith 		if (--spin == 0)
668c26864e0SJohn Baldwin 			return (1);
669c26864e0SJohn Baldwin 	return (0);
67046f3ff79SMike Smith }
67146f3ff79SMike Smith 
67246f3ff79SMike Smith static int
6732568cd2aSJustin Hibbits lpoutput(if_t ifp, struct mbuf *m, const struct sockaddr *dst,
674279aa3d4SKip Macy     struct route *ro)
67546f3ff79SMike Smith {
6762568cd2aSJustin Hibbits 	struct lp_data *sc = if_getsoftc(ifp);
677ae6b868aSJohn Baldwin 	device_t dev = sc->sc_dev;
6780f210c92SNicolas Souchu 	device_t ppbus = device_get_parent(dev);
6792067d312SJohn Baldwin 	int err;
68046f3ff79SMike Smith 	struct mbuf *mm;
68146f3ff79SMike Smith 	u_char *cp = "\0\0";
68246f3ff79SMike Smith 	u_char chksum = 0;
68346f3ff79SMike Smith 	int count = 0;
6848f84257eSDag-Erling Smørgrav 	int i, len, spin;
68546f3ff79SMike Smith 
68646f3ff79SMike Smith 	/* We need a sensible value if we abort */
68746f3ff79SMike Smith 	cp++;
6882067d312SJohn Baldwin 	ppb_lock(ppbus);
6892568cd2aSJustin Hibbits 	if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, 0);
69046f3ff79SMike Smith 
69146f3ff79SMike Smith 	err = 1;		/* assume we're aborting because of an error */
69246f3ff79SMike Smith 
69346f3ff79SMike Smith 	/* Suspend (on laptops) or receive-errors might have taken us offline */
6940f210c92SNicolas Souchu 	ppb_wctr(ppbus, IRQENABLE);
69546f3ff79SMike Smith 
6962568cd2aSJustin Hibbits 	if (if_getflags(ifp) & IFF_LINK0) {
6970f210c92SNicolas Souchu 		if (!(ppb_rstr(ppbus) & CLPIP_SHAKE)) {
69846f3ff79SMike Smith 			lprintf("&");
6992067d312SJohn Baldwin 			lp_intr(sc);
70046f3ff79SMike Smith 		}
70146f3ff79SMike Smith 
70246f3ff79SMike Smith 		/* Alert other end to pending packet */
70346f3ff79SMike Smith 		spin = LPMAXSPIN1;
7040f210c92SNicolas Souchu 		ppb_wdtr(ppbus, 0x08);
7050f210c92SNicolas Souchu 		while ((ppb_rstr(ppbus) & 0x08) == 0)
70646f3ff79SMike Smith 			if (--spin == 0) {
70746f3ff79SMike Smith 				goto nend;
70846f3ff79SMike Smith 			}
70946f3ff79SMike Smith 
71046f3ff79SMike Smith 		/* Calculate length of packet, then send that */
71146f3ff79SMike Smith 
71246f3ff79SMike Smith 		count += 14;		/* Ethernet header len */
71346f3ff79SMike Smith 
71446f3ff79SMike Smith 		mm = m;
71546f3ff79SMike Smith 		for (mm = m; mm; mm = mm->m_next) {
71646f3ff79SMike Smith 			count += mm->m_len;
71746f3ff79SMike Smith 		}
7180f210c92SNicolas Souchu 		if (clpoutbyte(count & 0xFF, LPMAXSPIN1, ppbus))
71946f3ff79SMike Smith 			goto nend;
7200f210c92SNicolas Souchu 		if (clpoutbyte((count >> 8) & 0xFF, LPMAXSPIN1, ppbus))
72146f3ff79SMike Smith 			goto nend;
72246f3ff79SMike Smith 
72346f3ff79SMike Smith 		/* Send dummy ethernet header */
72446f3ff79SMike Smith 		for (i = 0; i < 12; i++) {
7250f210c92SNicolas Souchu 			if (clpoutbyte(i, LPMAXSPIN1, ppbus))
72646f3ff79SMike Smith 				goto nend;
72746f3ff79SMike Smith 			chksum += i;
72846f3ff79SMike Smith 		}
72946f3ff79SMike Smith 
7300f210c92SNicolas Souchu 		if (clpoutbyte(0x08, LPMAXSPIN1, ppbus))
73146f3ff79SMike Smith 			goto nend;
7320f210c92SNicolas Souchu 		if (clpoutbyte(0x00, LPMAXSPIN1, ppbus))
73346f3ff79SMike Smith 			goto nend;
73446f3ff79SMike Smith 		chksum += 0x08 + 0x00;		/* Add into checksum */
73546f3ff79SMike Smith 
73646f3ff79SMike Smith 		mm = m;
73746f3ff79SMike Smith 		do {
73846f3ff79SMike Smith 			cp = mtod(mm, u_char *);
7398f84257eSDag-Erling Smørgrav 			len = mm->m_len;
7408f84257eSDag-Erling Smørgrav 			while (len--) {
74146f3ff79SMike Smith 				chksum += *cp;
7420f210c92SNicolas Souchu 				if (clpoutbyte(*cp++, LPMAXSPIN2, ppbus))
74346f3ff79SMike Smith 					goto nend;
74446f3ff79SMike Smith 			}
74546f3ff79SMike Smith 		} while ((mm = mm->m_next));
74646f3ff79SMike Smith 
74746f3ff79SMike Smith 		/* Send checksum */
7480f210c92SNicolas Souchu 		if (clpoutbyte(chksum, LPMAXSPIN2, ppbus))
74946f3ff79SMike Smith 			goto nend;
75046f3ff79SMike Smith 
75146f3ff79SMike Smith 		/* Go quiescent */
7520f210c92SNicolas Souchu 		ppb_wdtr(ppbus, 0);
75346f3ff79SMike Smith 
75446f3ff79SMike Smith 		err = 0;			/* No errors */
75546f3ff79SMike Smith 
75646f3ff79SMike Smith 	nend:
7572568cd2aSJustin Hibbits 		if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
75846f3ff79SMike Smith 		if (err)  {			/* if we didn't timeout... */
759c8dfaf38SGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
76046f3ff79SMike Smith 			lprintf("X");
76146f3ff79SMike Smith 		} else {
762c8dfaf38SGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
763c8dfaf38SGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len);
7648f84257eSDag-Erling Smørgrav 			lptap(ifp, m);
76546f3ff79SMike Smith 		}
76646f3ff79SMike Smith 
76746f3ff79SMike Smith 		m_freem(m);
76846f3ff79SMike Smith 
7690f210c92SNicolas Souchu 		if (!(ppb_rstr(ppbus) & CLPIP_SHAKE)) {
77046f3ff79SMike Smith 			lprintf("^");
7712067d312SJohn Baldwin 			lp_intr(sc);
77246f3ff79SMike Smith 		}
7732067d312SJohn Baldwin 		ppb_unlock(ppbus);
774c26864e0SJohn Baldwin 		return (0);
77546f3ff79SMike Smith 	}
77646f3ff79SMike Smith 
7770f210c92SNicolas Souchu 	if (ppb_rstr(ppbus) & LPIP_SHAKE) {
77846f3ff79SMike Smith 		lprintf("&");
7792067d312SJohn Baldwin 		lp_intr(sc);
78046f3ff79SMike Smith 	}
78146f3ff79SMike Smith 
7820f210c92SNicolas Souchu 	if (lpoutbyte(0x08, LPMAXSPIN1, ppbus))
78346f3ff79SMike Smith 		goto end;
7840f210c92SNicolas Souchu 	if (lpoutbyte(0x00, LPMAXSPIN2, ppbus))
78546f3ff79SMike Smith 		goto end;
78646f3ff79SMike Smith 
78746f3ff79SMike Smith 	mm = m;
78846f3ff79SMike Smith 	do {
78946f3ff79SMike Smith 		cp = mtod(mm, u_char *);
7908f84257eSDag-Erling Smørgrav 		len = mm->m_len;
7918f84257eSDag-Erling Smørgrav 		while (len--)
7920f210c92SNicolas Souchu 			if (lpoutbyte(*cp++, LPMAXSPIN2, ppbus))
79346f3ff79SMike Smith 				goto end;
79446f3ff79SMike Smith 	} while ((mm = mm->m_next));
79546f3ff79SMike Smith 
79646f3ff79SMike Smith 	err = 0;			/* no errors were encountered */
79746f3ff79SMike Smith 
79846f3ff79SMike Smith end:
79946f3ff79SMike Smith 	--cp;
8000f210c92SNicolas Souchu 	ppb_wdtr(ppbus, txmitl[*cp] ^ 0x17);
80146f3ff79SMike Smith 
8022568cd2aSJustin Hibbits 	if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
80346f3ff79SMike Smith 	if (err)  {			/* if we didn't timeout... */
804c8dfaf38SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
80546f3ff79SMike Smith 		lprintf("X");
80646f3ff79SMike Smith 	} else {
807c8dfaf38SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
808c8dfaf38SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len);
8098f84257eSDag-Erling Smørgrav 		lptap(ifp, m);
81046f3ff79SMike Smith 	}
81146f3ff79SMike Smith 
81246f3ff79SMike Smith 	m_freem(m);
81346f3ff79SMike Smith 
8140f210c92SNicolas Souchu 	if (ppb_rstr(ppbus) & LPIP_SHAKE) {
81546f3ff79SMike Smith 		lprintf("^");
8162067d312SJohn Baldwin 		lp_intr(sc);
81746f3ff79SMike Smith 	}
81846f3ff79SMike Smith 
8192067d312SJohn Baldwin 	ppb_unlock(ppbus);
820c26864e0SJohn Baldwin 	return (0);
82146f3ff79SMike Smith }
8220f210c92SNicolas Souchu 
8230f063508SPeter Wemm static device_method_t lp_methods[] = {
8240f063508SPeter Wemm   	/* device interface */
8250f063508SPeter Wemm 	DEVMETHOD(device_identify,	lp_identify),
8260f063508SPeter Wemm 	DEVMETHOD(device_probe,		lp_probe),
8270f063508SPeter Wemm 	DEVMETHOD(device_attach,	lp_attach),
8282067d312SJohn Baldwin 	DEVMETHOD(device_detach,	lp_detach),
8290f063508SPeter Wemm 	{ 0, 0 }
8300f063508SPeter Wemm };
8310f063508SPeter Wemm 
8320f063508SPeter Wemm static driver_t lp_driver = {
8330f063508SPeter Wemm 	"plip",
8340f063508SPeter Wemm 	lp_methods,
8350f063508SPeter Wemm 	sizeof(struct lp_data),
8360f063508SPeter Wemm };
8370f063508SPeter Wemm 
8386d588090SJohn Baldwin DRIVER_MODULE(plip, ppbus, lp_driver, lp_module_handler, NULL);
839f5fd5611SRuslan Ermilov MODULE_DEPEND(plip, ppbus, 1, 1, 1);
840