Home | History | Annotate | Line # | Download | only in ppbus
if_plip.c revision 1.33
      1 /* $NetBSD: if_plip.c,v 1.33 2018/11/15 10:56:30 maxv Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1997 Poul-Henning Kamp
      5  * Copyright (c) 2003, 2004 Gary Thorpe <gathorpe (at) users.sourceforge.net>
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27  * SUCH DAMAGE.
     28  *
     29  *	From Id: lpt.c,v 1.55.2.1 1996/11/12 09:08:38 phk Exp
     30  * FreeBSD: src/sys/dev/ppbus/if_plip.c,v 1.19.2.1 2000/05/24 00:20:57 n_hibma Exp
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 __KERNEL_RCSID(0, "$NetBSD: if_plip.c,v 1.33 2018/11/15 10:56:30 maxv Exp $");
     35 
     36 /*
     37  * Parallel port TCP/IP interfaces added.  I looked at the driver from
     38  * MACH but this is a complete rewrite, and btw. incompatible, and it
     39  * should perform better too.  I have never run the MACH driver though.
     40  *
     41  * This driver sends two bytes (0x08, 0x00) in front of each packet,
     42  * to allow us to distinguish another format later.
     43  *
     44  * Now added a Linux/Crynwr compatibility mode which is enabled using
     45  * IF_LINK0 - Tim Wilkinson.
     46  *
     47  * TODO:
     48  *    Make HDLC/PPP mode, use IF_LLC1 to enable.
     49  *
     50  * Connect the two computers using a Laplink parallel cable to use this
     51  * feature:
     52  *
     53  *      +----------------------------------------+
     54  * 	|A-name	A-End	B-End	Descr.	Port/Bit |
     55  *      +----------------------------------------+
     56  *	|DATA0	2	15	Data	0/0x01   |
     57  *	|-ERROR	15	2	   	1/0x08   |
     58  *      +----------------------------------------+
     59  *	|DATA1	3	13	Data	0/0x02	 |
     60  *	|+SLCT	13	3	   	1/0x10   |
     61  *      +----------------------------------------+
     62  *	|DATA2	4	12	Data	0/0x04   |
     63  *	|+PE	12	4	   	1/0x20   |
     64  *      +----------------------------------------+
     65  *	|DATA3	5	10	Strobe	0/0x08   |
     66  *	|-ACK	10	5	   	1/0x40   |
     67  *      +----------------------------------------+
     68  *	|DATA4	6	11	Data	0/0x10   |
     69  *	|BUSY	11	6	   	1/~0x80  |
     70  *      +----------------------------------------+
     71  *	|GND	18-25	18-25	GND	-        |
     72  *      +----------------------------------------+
     73  *
     74  * Expect transfer-rates up to 75 kbyte/sec.
     75  *
     76  * If GCC could correctly grok
     77  *	register int port __asm("edx")
     78  * the code would be cleaner
     79  *
     80  * Poul-Henning Kamp <phk (at) freebsd.org>
     81  */
     82 
     83 /*
     84  * Update for ppbus, PLIP support only - Nicolas Souchu
     85  */
     86 
     87 #include "opt_inet.h"
     88 #include "opt_plip.h"
     89 
     90 #include <sys/systm.h>
     91 #include <sys/param.h>
     92 #include <sys/proc.h>
     93 #include <sys/types.h>
     94 #include <sys/device.h>
     95 #include <sys/ioctl.h>
     96 #include <sys/malloc.h>
     97 #include <sys/mbuf.h>
     98 
     99 #include <net/if.h>
    100 #include <net/if_types.h>
    101 #include <net/netisr.h>
    102 
    103 #include <sys/time.h>
    104 #include <net/bpf.h>
    105 
    106 #ifdef INET
    107 #include <netinet/in_var.h>
    108 /* #include <netinet/in.h> */
    109 #else
    110 #error Cannot config lp/plip without inet
    111 #endif
    112 
    113 #include <dev/ppbus/ppbus_base.h>
    114 #include <dev/ppbus/ppbus_device.h>
    115 #include <dev/ppbus/ppbus_io.h>
    116 #include <dev/ppbus/ppbus_var.h>
    117 
    118 #include <machine/types.h>
    119 #include <sys/intr.h>
    120 
    121 #ifndef LPMTU			/* MTU for the lp# interfaces */
    122 #define	LPMTU		1500
    123 #endif
    124 
    125 #ifndef LPMAXSPIN1		/* DELAY factor for the lp# interfaces */
    126 #define	LPMAXSPIN1	8000	/* Spinning for remote intr to happen */
    127 #endif
    128 
    129 #ifndef LPMAXSPIN2		/* DELAY factor for the lp# interfaces */
    130 #define	LPMAXSPIN2	500	/* Spinning for remote handshake to happen */
    131 #endif
    132 
    133 #ifndef LPMAXERRS		/* Max errors before !RUNNING */
    134 #define	LPMAXERRS	100
    135 #endif
    136 
    137 #ifndef LPMAXRTRY
    138 #define LPMAXRTRY	100	/* If channel busy, retry LPMAXRTRY
    139 					consecutive times */
    140 #endif
    141 
    142 #define CLPIPHDRLEN	14	/* We send dummy ethernet addresses (two) + packet type in front of packet */
    143 #define	CLPIP_SHAKE	0x80	/* This bit toggles between nibble reception */
    144 #define MLPIPHDRLEN	CLPIPHDRLEN
    145 
    146 #define LPIPHDRLEN	2	/* We send 0x08, 0x00 in front of packet */
    147 #define	LPIP_SHAKE	0x40	/* This bit toggles between nibble reception */
    148 #if !defined(MLPIPHDRLEN) || LPIPHDRLEN > MLPIPHDRLEN
    149 #define MLPIPHDRLEN	LPIPHDRLEN
    150 #endif
    151 
    152 #define	LPIPTBLSIZE	256	/* Size of octet translation table */
    153 
    154 #define LP_PRINTF	if (lpflag) printf
    155 
    156 #ifdef PLIP_DEBUG
    157 static int volatile lpflag = 1;
    158 #else
    159 static int volatile lpflag = 0;
    160 #endif
    161 
    162 /* Tx/Rsv tables for the lp interface */
    163 static u_char *txmith;
    164 #define txmitl (txmith+(1*LPIPTBLSIZE))
    165 #define trecvh (txmith+(2*LPIPTBLSIZE))
    166 #define trecvl (txmith+(3*LPIPTBLSIZE))
    167 static u_char *ctxmith;
    168 #define ctxmitl (ctxmith+(1*LPIPTBLSIZE))
    169 #define ctrecvh (ctxmith+(2*LPIPTBLSIZE))
    170 #define ctrecvl (ctxmith+(3*LPIPTBLSIZE))
    171 static uint16_t lp_count = 0;
    172 
    173 /* Autoconf functions */
    174 static int lp_probe(device_t, cfdata_t, void *);
    175 static void lp_attach(device_t, device_t, void *);
    176 static int lp_detach(device_t, int);
    177 
    178 /* Soft config data */
    179 struct lp_softc {
    180 	struct ppbus_device_softc ppbus_dev;
    181 	struct ifnet sc_if;
    182 	u_char *sc_ifbuf;
    183 	unsigned short sc_iferrs;
    184 	unsigned short sc_xmit_rtry;
    185 	u_int8_t sc_dev_ok; /* Zero means ok */
    186 };
    187 
    188 /* Autoconf structure */
    189 CFATTACH_DECL_NEW(plip, sizeof(struct lp_softc), lp_probe, lp_attach, lp_detach,
    190 	NULL);
    191 
    192 /* Functions for the lp interface */
    193 static void lpinittables(void);
    194 static void lpfreetables(void);
    195 static int lpioctl(struct ifnet *, u_long, void *);
    196 static int lpoutput(struct ifnet *, struct mbuf *, const struct sockaddr *,
    197     const struct rtentry *);
    198 static void lpstart(struct ifnet *);
    199 static void lp_intr(void *);
    200 
    201 
    202 static int
    203 lp_probe(device_t parent, cfdata_t match, void *aux)
    204 {
    205 	struct ppbus_attach_args * args = aux;
    206 
    207 	/* Fail if ppbus is not interrupt capable */
    208 	if (args->capabilities & PPBUS_HAS_INTR)
    209 		return 1;
    210 
    211 	printf("%s(%s): not an interrupt-driven port.\n", __func__,
    212 		device_xname(parent));
    213 	return 0;
    214 }
    215 
    216 static void
    217 lp_attach(device_t parent, device_t self, void *aux)
    218 {
    219 	struct lp_softc * lp = device_private(self);
    220 	struct ifnet * ifp = &lp->sc_if;
    221 
    222 	lp->ppbus_dev.sc_dev = self;
    223 	lp->sc_dev_ok = 0;
    224 	lp->sc_ifbuf = NULL;
    225 	lp->sc_iferrs = 0;
    226 	lp->sc_xmit_rtry = 0;
    227 
    228 	ifp->if_softc = lp;
    229 	strlcpy(ifp->if_xname, device_xname(self), IFNAMSIZ);
    230 	ifp->if_mtu = LPMTU;
    231 	ifp->if_flags = IFF_SIMPLEX | IFF_POINTOPOINT | IFF_MULTICAST;
    232 	ifp->if_ioctl = lpioctl;
    233 	ifp->if_output = lpoutput;
    234 	ifp->if_start = lpstart;
    235 	ifp->if_type = IFT_PARA;
    236 	ifp->if_hdrlen = 0;
    237 	ifp->if_addrlen = 0;
    238 	ifp->if_dlt = DLT_NULL;
    239 	IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
    240 	IFQ_SET_READY(&ifp->if_snd);
    241 	if_attach(ifp);
    242 	if_alloc_sadl(ifp);
    243 
    244 	bpf_attach(ifp, DLT_NULL, sizeof(u_int32_t));
    245 
    246 	if (lp_count++ == 0)
    247 		lpinittables();
    248 	printf("\n");
    249 }
    250 
    251 static int
    252 lp_detach(device_t self, int flags)
    253 {
    254 	int error = 0;
    255 	struct lp_softc * lp = device_private(self);
    256 	device_t ppbus = device_parent(self);
    257 
    258 	if (lp->sc_dev_ok) {
    259 		if (!(flags & DETACH_QUIET))
    260 			LP_PRINTF("%s(%s): device not properly attached! "
    261 				"Skipping detach....\n", __func__,
    262 				device_xname(self));
    263 		return error;
    264 	}
    265 
    266 	/* If interface is up, bring it down and release ppbus */
    267 	if (lp->sc_if.if_flags & IFF_RUNNING) {
    268 		ppbus_wctr(ppbus, 0x00);
    269 		if_detach(&lp->sc_if);
    270 		error = ppbus_remove_handler(ppbus, lp_intr);
    271 		if (error) {
    272 			if (!(flags & DETACH_QUIET))
    273 				LP_PRINTF("%s(%s): unable to remove interrupt "
    274 					"callback.\n", __func__,
    275 					device_xname(self));
    276 			if (!(flags & DETACH_FORCE))
    277 				return error;
    278 		}
    279 		error = ppbus_release_bus(ppbus, self, 0, 0);
    280 		if (error) {
    281 			if (!(flags & DETACH_QUIET))
    282 				LP_PRINTF("%s(%s): error releasing bus %s.\n",
    283 					__func__, device_xname(self),
    284 					device_xname(ppbus));
    285 			if (!(flags & DETACH_FORCE))
    286 				return error;
    287 		}
    288 	}
    289 
    290 	if (lp->sc_ifbuf)
    291 		free(lp->sc_ifbuf, M_DEVBUF);
    292 
    293 	if (--lp_count == 0)
    294 		lpfreetables();
    295 	return error;
    296 }
    297 
    298 /*
    299  * Build the translation tables for the LPIP (BSD unix) protocol.
    300  * We don't want to calculate these nasties in our tight loop, so we
    301  * precalculate them when we initialize.
    302  */
    303 static void
    304 lpinittables(void)
    305 {
    306 	int i;
    307 
    308 	if (!txmith)
    309 		txmith = malloc(4*LPIPTBLSIZE, M_DEVBUF, M_WAITOK);
    310 
    311 	if (!ctxmith)
    312 		ctxmith = malloc(4*LPIPTBLSIZE, M_DEVBUF, M_WAITOK);
    313 
    314 	for (i = 0; i < LPIPTBLSIZE; i++) {
    315 		ctxmith[i] = (i & 0xF0) >> 4;
    316 		ctxmitl[i] = 0x10 | (i & 0x0F);
    317 		ctrecvh[i] = (i & 0x78) << 1;
    318 		ctrecvl[i] = (i & 0x78) >> 3;
    319 	}
    320 
    321 	for (i = 0; i < LPIPTBLSIZE; i++) {
    322 		txmith[i] = ((i & 0x80) >> 3) | ((i & 0x70) >> 4) | 0x08;
    323 		txmitl[i] = ((i & 0x08) << 1) | (i & 0x07);
    324 		trecvh[i] = ((~i) & 0x80) | ((i & 0x38) << 1);
    325 		trecvl[i] = (((~i) & 0x80) >> 4) | ((i & 0x38) >> 3);
    326 	}
    327 }
    328 
    329 /* Free translation tables */
    330 static void
    331 lpfreetables(void)
    332 {
    333 	if (txmith)
    334 		free(txmith, M_DEVBUF);
    335 	if (ctxmith)
    336 		free(ctxmith, M_DEVBUF);
    337 	txmith = ctxmith = NULL;
    338 }
    339 
    340 
    341 /* Process an ioctl request. */
    342 static int
    343 lpioctl(struct ifnet *ifp, u_long cmd, void *data)
    344 {
    345 	struct lp_softc * sc = ifp->if_softc;
    346 	device_t dev = sc->ppbus_dev.sc_dev;
    347 	device_t ppbus = device_parent(dev);
    348 	struct ifaddr * ifa = (struct ifaddr *)data;
    349 	struct ifreq * ifr = (struct ifreq *)data;
    350 	u_char * ptr;
    351 	int error, s;
    352 
    353 	error = 0;
    354 	s = splnet();
    355 
    356 	if (sc->sc_dev_ok) {
    357 		LP_PRINTF("%s(%s): device not properly attached!", __func__,
    358 			device_xname(dev));
    359 		error = ENODEV;
    360 		goto end;
    361 	}
    362 
    363 	switch (cmd) {
    364 
    365 	case SIOCSIFDSTADDR:
    366 		if (ifa->ifa_addr->sa_family != AF_INET)
    367 			error = EAFNOSUPPORT;
    368 		break;
    369 
    370 	case SIOCINITIFADDR:
    371 		if (ifa->ifa_addr->sa_family != AF_INET) {
    372 			error = EAFNOSUPPORT;
    373 			break;
    374 		}
    375 		ifp->if_flags |= IFF_UP;
    376 	/* FALLTHROUGH */
    377 	case SIOCSIFFLAGS:
    378 		if (cmd == SIOCSIFFLAGS) {
    379 			if ((error = ifioctl_common(ifp, cmd, data)) != 0)
    380 				break;
    381 		}
    382 		if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) == IFF_UP) {
    383 			if ((error = ppbus_request_bus(ppbus, dev, 0, 0)))
    384 				break;
    385 			error = ppbus_set_mode(ppbus, PPBUS_COMPATIBLE, 0);
    386 			if (error)
    387 				break;
    388 
    389 			error = ppbus_add_handler(ppbus, lp_intr, dev);
    390 			if (error) {
    391 				LP_PRINTF("%s(%s): unable to register interrupt"
    392 					" callback.\n", __func__,
    393 					device_xname(dev));
    394 				ppbus_release_bus(ppbus, dev, 0, 0);
    395 				break;
    396 			}
    397 
    398 			/* Allocate a buffer if necessary */
    399 			if (sc->sc_ifbuf == NULL) {
    400 				sc->sc_ifbuf = malloc(sc->sc_if.if_mtu +
    401 					MLPIPHDRLEN, M_DEVBUF, M_NOWAIT);
    402 				if (!sc->sc_ifbuf) {
    403 					error = ENOBUFS;
    404 					ppbus_release_bus(ppbus, dev, 0, 0);
    405 					break;
    406 				}
    407 			}
    408 
    409 			ppbus_wctr(ppbus, IRQENABLE);
    410 			ifp->if_flags |= IFF_RUNNING;
    411 		}
    412 		if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) == IFF_RUNNING) {
    413 			ppbus_remove_handler(ppbus, lp_intr);
    414 			error = ppbus_release_bus(ppbus, dev, 0, 0);
    415 			ifp->if_flags &= ~IFF_RUNNING;
    416 		}
    417 		/* Go quiescent */
    418 		ppbus_wdtr(ppbus, 0);
    419 		break;
    420 
    421 	case SIOCSIFMTU:
    422 		if (sc->sc_if.if_mtu == ifr->ifr_mtu)
    423 			break;
    424 		ptr = sc->sc_ifbuf;
    425 		sc->sc_ifbuf = malloc(ifr->ifr_mtu+MLPIPHDRLEN, M_DEVBUF,
    426 			M_NOWAIT);
    427 		if (!sc->sc_ifbuf) {
    428 			sc->sc_ifbuf = ptr;
    429 			error = ENOBUFS;
    430 			break;
    431 		}
    432 		if (ptr)
    433 			free(ptr,M_DEVBUF);
    434 		/*FALLTHROUGH*/
    435 	case SIOCGIFMTU:
    436 		if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET)
    437 			error = 0;
    438 		break;
    439 
    440 	case SIOCADDMULTI:
    441 	case SIOCDELMULTI:
    442 		if (ifr == NULL) {
    443 			error = EAFNOSUPPORT;		/* XXX */
    444 			break;
    445 		}
    446 		switch (ifreq_getaddr(cmd, ifr)->sa_family) {
    447 		case AF_INET:
    448 			break;
    449 		default:
    450 			splx(s);
    451 			return EAFNOSUPPORT;
    452 		}
    453 		break;
    454 
    455 	case SIOCGIFMEDIA:
    456 		/*
    457 		 * No ifmedia support at this stage; maybe use it
    458 		 * in future for eg. protocol selection.
    459 		 */
    460 	default:
    461 		LP_PRINTF("LP:ioctl(0x%lx)\n", cmd);
    462 		error = ifioctl_common(ifp, cmd, data);
    463 	}
    464 
    465 end:
    466 	splx(s);
    467 	return error;
    468 }
    469 
    470 static inline int
    471 clpoutbyte(u_char byte, int spin, device_t ppbus)
    472 {
    473 	int s = spin;
    474 	ppbus_wdtr(ppbus, ctxmitl[byte]);
    475 	while (ppbus_rstr(ppbus) & CLPIP_SHAKE) {
    476 		if (--s == 0) {
    477 			return 1;
    478 		}
    479 	}
    480 	s = spin;
    481 	ppbus_wdtr(ppbus, ctxmith[byte]);
    482 	while (!(ppbus_rstr(ppbus) & CLPIP_SHAKE)) {
    483 		if (--s == 0) {
    484 			return 1;
    485 		}
    486 	}
    487 	return 0;
    488 }
    489 
    490 static inline int
    491 clpinbyte(int spin, device_t ppbus)
    492 {
    493 	u_char c, cl;
    494 	int s = spin;
    495 
    496 	while (ppbus_rstr(ppbus) & CLPIP_SHAKE) {
    497 		if (!--s) {
    498 			return -1;
    499 		}
    500 	}
    501 	cl = ppbus_rstr(ppbus);
    502 	ppbus_wdtr(ppbus, 0x10);
    503 
    504 	s = spin;
    505 	while (!(ppbus_rstr(ppbus) & CLPIP_SHAKE)) {
    506 		if (!--s) {
    507 			return -1;
    508 		}
    509 	}
    510 	c = ppbus_rstr(ppbus);
    511 	ppbus_wdtr(ppbus, 0x00);
    512 
    513 	return (ctrecvl[cl] | ctrecvh[c]);
    514 }
    515 
    516 static void
    517 lptap(struct ifnet *ifp, struct mbuf *m, u_int direction)
    518 {
    519 	/*
    520 	 * Send a packet through bpf. We need to prepend the address family
    521 	 * as a four byte field. Cons up a dummy header to pacify bpf. This
    522 	 * is safe because bpf will only read from the mbuf (i.e., it won't
    523 	 * try to free it or keep a pointer to it).
    524 	 */
    525 	u_int32_t af = AF_INET;
    526 	struct mbuf m0;
    527 
    528 	m0.m_type = MT_DATA;
    529 	m0.m_next = m;
    530 	m0.m_nextpkt = NULL;
    531 	m0.m_owner = NULL;
    532 	m0.m_len = sizeof(u_int32_t);
    533 	m0.m_data = (char *)&af;
    534 	m0.m_flags = 0;
    535 	bpf_mtap(ifp, &m0, direction);
    536 }
    537 
    538 /* Soft interrupt handler called by hardware interrupt handler */
    539 static void
    540 lp_intr(void *arg)
    541 {
    542 	device_t dev = (device_t)arg;
    543         device_t ppbus = device_parent(dev);
    544 	struct lp_softc * sc = device_private(dev);
    545 	struct ifnet * ifp = &sc->sc_if;
    546 	struct mbuf *top;
    547 	int len, s, j;
    548 	u_char *bp;
    549 	u_char c, cl;
    550 
    551 	s = splnet();
    552 
    553 	/* Do nothing if device not properly attached */
    554 	if (sc->sc_dev_ok) {
    555 		LP_PRINTF("%s(%s): device not properly attached!", __func__,
    556 			device_xname(dev));
    557 		goto done;
    558 	}
    559 
    560 	/* Do nothing if interface is not up */
    561 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
    562 		goto done;
    563 
    564 	/* If other side is no longer transmitting, do nothing */
    565 	if (!(ppbus_rstr(ppbus) & LPIP_SHAKE))
    566 		goto done;
    567 
    568 	/* Disable interrupts until we finish */
    569 	ppbus_wctr(ppbus, ~IRQENABLE);
    570 
    571 	top = NULL;
    572 	bp = sc->sc_ifbuf;
    573 	/* Linux/crynwyr protocol receiving */
    574 	if (ifp->if_flags & IFF_LINK0) {
    575 		/* Ack. the request */
    576 		ppbus_wdtr(ppbus, 0x01);
    577 
    578 		/* Get the packet length */
    579 		j = clpinbyte(LPMAXSPIN2, ppbus);
    580 		if (j == -1)
    581 			goto err;
    582 		len = j;
    583 		j = clpinbyte(LPMAXSPIN2, ppbus);
    584 		if (j == -1)
    585 			goto err;
    586 		len = len + (j << 8);
    587 		if (len > ifp->if_mtu + MLPIPHDRLEN)
    588 			goto err;
    589 
    590 		while (len--) {
    591 			j = clpinbyte(LPMAXSPIN2, ppbus);
    592 			if (j == -1) {
    593 				goto err;
    594 			}
    595 			*bp++ = j;
    596 		}
    597 		/* Get and ignore checksum */
    598 		j = clpinbyte(LPMAXSPIN2, ppbus);
    599 		if (j == -1) {
    600 			goto err;
    601 		}
    602 
    603 		/* Return to idle state */
    604 		ppbus_wdtr(ppbus, 0);
    605 		len = bp - sc->sc_ifbuf;
    606 		if (len <= CLPIPHDRLEN)
    607 			goto err;
    608 		len -= CLPIPHDRLEN;
    609 		top = m_devget(sc->sc_ifbuf + CLPIPHDRLEN, len, 0, ifp);
    610 	}
    611 	/* FreeBSD protocol receiving */
    612 	else {
    613 		len = ifp->if_mtu + LPIPHDRLEN;
    614 		while (len--) {
    615 			cl = ppbus_rstr(ppbus);
    616 			ppbus_wdtr(ppbus, 0x08);
    617 
    618 			j = LPMAXSPIN2;
    619 			while ((ppbus_rstr(ppbus) & LPIP_SHAKE)) {
    620 				if (!--j)
    621 					goto err;
    622 			}
    623 
    624 			c = ppbus_rstr(ppbus);
    625 			ppbus_wdtr(ppbus, 0);
    626 
    627 			*bp++= trecvh[cl] | trecvl[c];
    628 
    629 			j = LPMAXSPIN2;
    630 			while (!((cl = ppbus_rstr(ppbus)) & LPIP_SHAKE)) {
    631 				if (cl != c &&
    632 					(((cl = ppbus_rstr(ppbus)) ^ 0xb8) &
    633 					0xf8) == (c & 0xf8))
    634 					goto end;
    635 				if (!--j)
    636 					goto err;
    637 			}
    638 		}
    639 
    640 end:
    641 		len = bp - sc->sc_ifbuf;
    642 		if (len <= LPIPHDRLEN)
    643 			goto err;
    644 		len -= LPIPHDRLEN;
    645 		top = m_devget(sc->sc_ifbuf + LPIPHDRLEN, len, 0, ifp);
    646 	}
    647 
    648 	if (top == NULL) {
    649 		ifp->if_iqdrops++;
    650 		goto err;
    651 	}
    652 	if (ifp->if_bpf) {
    653 		lptap(ifp, top, BPF_D_IN);
    654 	}
    655 	if (__predict_false(!pktq_enqueue(ip_pktq, top, 0))) {
    656 		ifp->if_iqdrops++;
    657 		m_freem(top);
    658 		goto err;
    659 	}
    660 	ifp->if_ipackets++;
    661 	ifp->if_ibytes += len;
    662 	sc->sc_iferrs = 0;
    663 
    664 	goto done;
    665 
    666 err:
    667 	/* Return to idle state */
    668 	ppbus_wdtr(ppbus, 0);
    669 	ifp->if_ierrors++;
    670 	sc->sc_iferrs++;
    671 	LP_PRINTF("R");
    672 	/* Disable interface if there are too many errors */
    673 	if (sc->sc_iferrs > LPMAXERRS) {
    674 		aprint_error_dev(dev, "Too many consecutive errors, going off-line.\n");
    675 		ppbus_wctr(ppbus, ~IRQENABLE);
    676 		if_down(ifp);
    677 		sc->sc_iferrs = 0;
    678 	}
    679 
    680 done:
    681 	/* Re-enable interrupts */
    682 	ppbus_wctr(ppbus, IRQENABLE);
    683 	/* If interface is not active, send some packets */
    684 	if ((ifp->if_flags & IFF_OACTIVE) == 0)
    685 		lpstart(ifp);
    686 	splx(s);
    687 	return;
    688 }
    689 
    690 static inline int
    691 lpoutbyte(u_char byte, int spin, device_t ppbus)
    692 {
    693 	int s = spin;
    694 	ppbus_wdtr(ppbus, txmith[byte]);
    695 	while (!(ppbus_rstr(ppbus) & LPIP_SHAKE)) {
    696 		if (--s == 0)
    697 			return 1;
    698 	}
    699 	s = spin;
    700 	ppbus_wdtr(ppbus, txmitl[byte]);
    701 	while (ppbus_rstr(ppbus) & LPIP_SHAKE) {
    702 		if (--s == 0)
    703 			return 1;
    704 	}
    705 	return 0;
    706 }
    707 
    708 /* Queue a packet for delivery */
    709 static int
    710 lpoutput(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
    711     const struct rtentry *rt)
    712 {
    713 	struct lp_softc * sc = ifp->if_softc;
    714 	device_t dev = sc->ppbus_dev.sc_dev;
    715 	device_t ppbus = device_parent(dev);
    716 	int err;
    717 	int s;
    718 
    719 	s = splnet();
    720 
    721 	if (sc->sc_dev_ok) {
    722 		LP_PRINTF("%s(%s): device not properly attached!", __func__,
    723 			device_xname(dev));
    724 		err = ENODEV;
    725 		goto endoutput;
    726 	}
    727 
    728 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
    729 		err = ENETDOWN;
    730 		goto endoutput;
    731 	}
    732 
    733 	/* Only support INET */
    734 	if (dst->sa_family != AF_INET) {
    735 		LP_PRINTF("%s: af%d not supported\n", ifp->if_xname,
    736 		    dst->sa_family);
    737 		ifp->if_noproto++;
    738 		err = EAFNOSUPPORT;
    739 		goto endoutput;
    740 	}
    741 
    742 	IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family);
    743 	IFQ_ENQUEUE(&ifp->if_snd, m, err);
    744 	if (err == 0) {
    745 		if ((ifp->if_flags & IFF_OACTIVE) == 0)
    746 			lpstart(ifp);
    747 	} else {
    748 		ifp->if_oerrors++;
    749 		sc->sc_iferrs++;
    750 		LP_PRINTF("Q");
    751 
    752 		/* Disable interface if there are too many errors */
    753 		if (sc->sc_iferrs > LPMAXERRS) {
    754 			aprint_error_dev(dev, "Too many errors, going off-line.\n");
    755 			ppbus_wctr(ppbus, ~IRQENABLE);
    756 			if_down(ifp);
    757 			sc->sc_iferrs = 0;
    758 		}
    759 	}
    760 
    761 endoutput:
    762 	if ((err != 0) && (err != ENOBUFS))
    763 		m_freem(m);
    764 	splx(s);
    765 	return err;
    766 }
    767 
    768 /* Send routine: send packets over PLIP cable. Call at splnet(). */
    769 void
    770 lpstart(struct ifnet * ifp)
    771 {
    772 	struct lp_softc * lp = ifp->if_softc;
    773 	device_t dev = lp->ppbus_dev.sc_dev;
    774 	device_t ppbus = device_parent(dev);
    775 	struct mbuf * mm;
    776 	struct mbuf * m;
    777 	u_char * cp;
    778 	int err, i, len, spin, count;
    779 	u_char str, chksum;
    780 
    781 	if (lp->sc_dev_ok) {
    782 		LP_PRINTF("%s(%s): device not properly attached!", __func__,
    783 			device_xname(dev));
    784 		return;
    785 	}
    786 
    787 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
    788 		return;
    789 	}
    790 
    791 	ifp->if_flags |= IFF_OACTIVE;
    792 
    793 	/* Go quiescent */
    794 	ppbus_wdtr(ppbus, 0);
    795 
    796 	/* Output loop */
    797 	for (;;) {
    798 		/* Check if there are packets to send */
    799 		if (IFQ_IS_EMPTY(&ifp->if_snd)) {
    800 			goto final;
    801 		}
    802 		/* Try to send a packet, dequeue it later if successful */
    803 		IFQ_POLL(&ifp->if_snd, m);
    804 		if (m == NULL)
    805 			goto final;
    806 
    807 		str = ppbus_rstr(ppbus);
    808 		/* Wait until other side is not transmitting */
    809 		if ((str & LPIP_SHAKE) ||
    810 			((ifp->if_flags & IFF_LINK0) && !(str & CLPIP_SHAKE))) {
    811 			LP_PRINTF("&");
    812 			if (++lp->sc_xmit_rtry > LPMAXRTRY) {
    813 				aprint_error_dev(dev, "Too many retries while channel "
    814 					"busy, going off-line.\n");
    815 				ppbus_wctr(ppbus, ~IRQENABLE);
    816 				if_down(ifp);
    817 				lp->sc_xmit_rtry = 0;
    818 			}
    819 			goto final;
    820 		}
    821 		lp->sc_xmit_rtry = 0;
    822 
    823 		/* Disable interrupt generation */
    824 		ppbus_wctr(ppbus, ~IRQENABLE);
    825 
    826 		err = 1;
    827 
    828 		/* Output packet for Linux/crynwyr compatible protocol */
    829 		if (ifp->if_flags & IFF_LINK0) {
    830 			/* Calculate packet length */
    831 			count = 14;		/* Ethernet header len */
    832 			for (mm = m; mm; mm = mm->m_next) {
    833 				count += mm->m_len;
    834 			}
    835 
    836 			/* Alert other end to pending packet */
    837 			spin = LPMAXSPIN1;
    838 			ppbus_wdtr(ppbus, 0x08);
    839 			while ((ppbus_rstr(ppbus) & 0x08) == 0) {
    840 				if (--spin == 0) {
    841 					goto nend;
    842 				}
    843 			}
    844 
    845 			if (clpoutbyte(count & 0xFF, LPMAXSPIN1, ppbus))
    846 				goto nend;
    847 			if (clpoutbyte((count >> 8) & 0xFF, LPMAXSPIN1, ppbus))
    848 				goto nend;
    849 
    850 			/* Send dummy ethernet header */
    851 			chksum = 0;
    852 			for (i = 0; i < 12; i++) {
    853 				if (clpoutbyte(i, LPMAXSPIN1, ppbus))
    854 					goto nend;
    855 				chksum += i;
    856 			}
    857 
    858 			if (clpoutbyte(0x08, LPMAXSPIN1, ppbus))
    859 				goto nend;
    860 			if (clpoutbyte(0x00, LPMAXSPIN1, ppbus))
    861 				goto nend;
    862 			chksum += 0x08 + 0x00;		/* Add into checksum */
    863 
    864 			mm = m;
    865 			do {
    866 				cp = mtod(mm, u_char *);
    867 				len = mm->m_len;
    868 				while (len--) {
    869 					if (clpoutbyte(*cp, LPMAXSPIN2, ppbus))
    870 						goto nend;
    871 					chksum += *cp++;
    872 				}
    873 			} while ((mm = mm->m_next));
    874 
    875 			/* Send checksum */
    876 			if (clpoutbyte(chksum, LPMAXSPIN2, ppbus))
    877 				goto nend;
    878 
    879 			/* No errors */
    880 			err = 0;
    881 			/* Go quiescent */
    882 			ppbus_wdtr(ppbus, 0);
    883 		}
    884 		/* Output packet for FreeBSD compatible protocol */
    885 		else {
    886 			/* We need a sensible value if we abort */
    887 			cp = NULL;
    888 
    889 			if (lpoutbyte(0x08, LPMAXSPIN1, ppbus))
    890 				goto end;
    891 			if (lpoutbyte(0x00, LPMAXSPIN2, ppbus))
    892 				goto end;
    893 
    894 			mm = m;
    895 			do {
    896 				cp = mtod(mm,u_char *);
    897 				len = mm->m_len;
    898 				while (len--)
    899 					if (lpoutbyte(*cp++, LPMAXSPIN2, ppbus))
    900 						goto end;
    901 			} while ((mm = mm->m_next));
    902 
    903 			/* no errors were encountered */
    904 			err = 0;
    905 
    906 end:
    907 			if (cp)
    908 				ppbus_wdtr(ppbus, txmitl[*(--cp)] ^ 0x17);
    909 			else
    910 				ppbus_wdtr(ppbus, txmitl['\0'] ^ 0x17);
    911 		}
    912 
    913 nend:
    914 		/* Re-enable interrupt generation */
    915 		ppbus_wctr(ppbus, IRQENABLE);
    916 
    917 		if (err) {
    918 			/* Go quiescent */
    919 			ppbus_wdtr(ppbus, 0);
    920 
    921 			ifp->if_oerrors++;
    922 			lp->sc_iferrs++;
    923 			LP_PRINTF("X");
    924 
    925 			/* Disable interface if there are too many errors */
    926 			if (lp->sc_iferrs > LPMAXERRS) {
    927 				aprint_error_dev(dev, "Too many errors, going off-line.\n");
    928 				ppbus_wctr(ppbus, ~IRQENABLE);
    929 				if_down(ifp);
    930 				lp->sc_iferrs = 0;
    931 				goto final;
    932 			}
    933 		} else {
    934 			/* Dequeue packet on success */
    935 			IFQ_DEQUEUE(&ifp->if_snd, m);
    936 			if(ifp->if_bpf)
    937 				lptap(ifp, m, BPF_D_OUT);
    938 			ifp->if_opackets++;
    939 			ifp->if_obytes += m->m_pkthdr.len;
    940 			m_freem(m);
    941 		}
    942 	}
    943 
    944 final:
    945 	ifp->if_flags &= ~IFF_OACTIVE;
    946 	return;
    947 }
    948