2 * Copyright (c) 1997 Poul-Henning Kamp
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * From Id: lpt.c,v 1.55.2.1 1996/11/12 09:08:38 phk Exp
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
33 * Parallel port TCP/IP interfaces added. I looked at the driver from
34 * MACH but this is a complete rewrite, and btw. incompatible, and it
35 * should perform better too. I have never run the MACH driver though.
37 * This driver sends two bytes (0x08, 0x00) in front of each packet,
38 * to allow us to distinguish another format later.
40 * Now added a Linux/Crynwr compatibility mode which is enabled using
41 * IF_LINK0 - Tim Wilkinson.
44 * Make HDLC/PPP mode, use IF_LLC1 to enable.
46 * Connect the two computers using a Laplink parallel cable to use this
49 * +----------------------------------------+
50 * |A-name A-End B-End Descr. Port/Bit |
51 * +----------------------------------------+
52 * |DATA0 2 15 Data 0/0x01 |
53 * |-ERROR 15 2 1/0x08 |
54 * +----------------------------------------+
55 * |DATA1 3 13 Data 0/0x02 |
56 * |+SLCT 13 3 1/0x10 |
57 * +----------------------------------------+
58 * |DATA2 4 12 Data 0/0x04 |
60 * +----------------------------------------+
61 * |DATA3 5 10 Strobe 0/0x08 |
63 * +----------------------------------------+
64 * |DATA4 6 11 Data 0/0x10 |
65 * |BUSY 11 6 1/~0x80 |
66 * +----------------------------------------+
67 * |GND 18-25 18-25 GND - |
68 * +----------------------------------------+
70 * Expect transfer-rates up to 75 kbyte/sec.
72 * If GCC could correctly grok
73 * register int port asm("edx")
74 * the code would be cleaner
76 * Poul-Henning Kamp <phk@freebsd.org>
80 * Update for ppbus, PLIP support only - Nicolas Souchu
85 #include <sys/param.h>
86 #include <sys/systm.h>
87 #include <sys/module.h>
90 #include <sys/socket.h>
91 #include <sys/sockio.h>
92 #include <sys/kernel.h>
93 #include <sys/malloc.h>
95 #include <machine/bus.h>
96 #include <machine/resource.h>
100 #include <net/if_types.h>
101 #include <net/netisr.h>
102 #include <net/route.h>
104 #include <netinet/in.h>
105 #include <netinet/in_var.h>
109 #include <dev/ppbus/ppbconf.h>
110 #include "ppbus_if.h"
111 #include <dev/ppbus/ppbio.h>
113 #ifndef LPMTU /* MTU for the lp# interfaces */
117 #ifndef LPMAXSPIN1 /* DELAY factor for the lp# interfaces */
118 #define LPMAXSPIN1 8000 /* Spinning for remote intr to happen */
121 #ifndef LPMAXSPIN2 /* DELAY factor for the lp# interfaces */
122 #define LPMAXSPIN2 500 /* Spinning for remote handshake to happen */
125 #ifndef LPMAXERRS /* Max errors before !RUNNING */
126 #define LPMAXERRS 100
129 #define CLPIPHDRLEN 14 /* We send dummy ethernet addresses (two) + packet type in front of packet */
130 #define CLPIP_SHAKE 0x80 /* This bit toggles between nibble reception */
131 #define MLPIPHDRLEN CLPIPHDRLEN
133 #define LPIPHDRLEN 2 /* We send 0x08, 0x00 in front of packet */
134 #define LPIP_SHAKE 0x40 /* This bit toggles between nibble reception */
135 #if !defined(MLPIPHDRLEN) || LPIPHDRLEN > MLPIPHDRLEN
136 #define MLPIPHDRLEN LPIPHDRLEN
139 #define LPIPTBLSIZE 256 /* Size of octet translation table */
141 #define lprintf if (lptflag) printf
144 static int volatile lptflag = 1;
146 static int volatile lptflag = 0;
150 struct ifnet *sc_ifp;
155 struct resource *res_irq;
156 void *sc_intr_cookie;
159 static struct mtx lp_tables_lock;
160 MTX_SYSINIT(lp_tables, &lp_tables_lock, "plip tables", MTX_DEF);
162 /* 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))
168 static u_char *ctxmith;
169 #define ctxmitl (ctxmith + (1 * LPIPTBLSIZE))
170 #define ctrecvh (ctxmith + (2 * LPIPTBLSIZE))
171 #define ctrecvl (ctxmith + (3 * LPIPTBLSIZE))
173 /* Functions for the lp# interface */
174 static int lpinittables(void);
175 static int lpioctl(struct ifnet *, u_long, caddr_t);
176 static int lpoutput(struct ifnet *, struct mbuf *, const struct sockaddr *,
178 static void lpstop(struct lp_data *);
179 static void lp_intr(void *);
180 static int lp_module_handler(module_t, int, void *);
182 #define DEVTOSOFTC(dev) \
183 ((struct lp_data *)device_get_softc(dev))
185 static devclass_t lp_devclass;
188 lp_module_handler(module_t mod, int what, void *arg)
193 mtx_lock(&lp_tables_lock);
194 if (txmith != NULL) {
195 free(txmith, M_DEVBUF);
198 if (ctxmith != NULL) {
199 free(ctxmith, M_DEVBUF);
202 mtx_unlock(&lp_tables_lock);
214 lp_identify(driver_t *driver, device_t parent)
218 dev = device_find_child(parent, "plip", -1);
220 BUS_ADD_CHILD(parent, 0, "plip", -1);
224 lp_probe(device_t dev)
227 device_set_desc(dev, "PLIP network interface");
233 lp_attach(device_t dev)
235 struct lp_data *lp = DEVTOSOFTC(dev);
242 * Reserve the interrupt resource. If we don't have one, the
245 lp->res_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
247 if (lp->res_irq == 0) {
248 device_printf(dev, "cannot reserve interrupt, failed.\n");
252 ifp = lp->sc_ifp = if_alloc(IFT_PARA);
258 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
260 ifp->if_flags = IFF_SIMPLEX | IFF_POINTOPOINT | IFF_MULTICAST;
261 ifp->if_ioctl = lpioctl;
262 ifp->if_output = lpoutput;
265 ifp->if_snd.ifq_maxlen = ifqmaxlen;
268 bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
271 * Attach our interrupt handler. It is only called while we
274 error = bus_setup_intr(dev, lp->res_irq, INTR_TYPE_NET | INTR_MPSAFE,
275 NULL, lp_intr, lp, &lp->sc_intr_cookie);
279 bus_release_resource(dev, SYS_RES_IRQ, 0, lp->res_irq);
280 device_printf(dev, "Unable to register interrupt handler\n");
288 lp_detach(device_t dev)
290 struct lp_data *sc = device_get_softc(dev);
291 device_t ppbus = device_get_parent(dev);
296 bpfdetach(sc->sc_ifp);
297 if_detach(sc->sc_ifp);
298 bus_teardown_intr(dev, sc->res_irq, sc->sc_intr_cookie);
299 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->res_irq);
304 * Build the translation tables for the LPIP (BSD unix) protocol.
305 * We don't want to calculate these nasties in our tight loop, so we
306 * precalculate them when we initialize.
313 mtx_lock(&lp_tables_lock);
315 txmith = malloc(4 * LPIPTBLSIZE, M_DEVBUF, M_NOWAIT);
317 if (txmith == NULL) {
318 mtx_unlock(&lp_tables_lock);
323 ctxmith = malloc(4 * LPIPTBLSIZE, M_DEVBUF, M_NOWAIT);
325 if (ctxmith == NULL) {
326 mtx_unlock(&lp_tables_lock);
330 for (i = 0; i < LPIPTBLSIZE; i++) {
331 ctxmith[i] = (i & 0xF0) >> 4;
332 ctxmitl[i] = 0x10 | (i & 0x0F);
333 ctrecvh[i] = (i & 0x78) << 1;
334 ctrecvl[i] = (i & 0x78) >> 3;
337 for (i = 0; i < LPIPTBLSIZE; i++) {
338 txmith[i] = ((i & 0x80) >> 3) | ((i & 0x70) >> 4) | 0x08;
339 txmitl[i] = ((i & 0x08) << 1) | (i & 0x07);
340 trecvh[i] = ((~i) & 0x80) | ((i & 0x38) << 1);
341 trecvl[i] = (((~i) & 0x80) >> 4) | ((i & 0x38) >> 3);
343 mtx_unlock(&lp_tables_lock);
349 lpstop(struct lp_data *sc)
351 device_t ppbus = device_get_parent(sc->sc_dev);
353 ppb_assert_locked(ppbus);
354 ppb_wctr(ppbus, 0x00);
355 sc->sc_ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
356 free(sc->sc_ifbuf, M_DEVBUF);
359 /* IFF_UP is not set, try to release the bus anyway */
360 ppb_release_bus(ppbus, sc->sc_dev);
364 lpinit_locked(struct ifnet *ifp)
366 struct lp_data *sc = ifp->if_softc;
367 device_t dev = sc->sc_dev;
368 device_t ppbus = device_get_parent(dev);
371 ppb_assert_locked(ppbus);
372 error = ppb_request_bus(ppbus, dev, PPB_DONTWAIT);
376 /* Now IFF_UP means that we own the bus */
377 ppb_set_mode(ppbus, PPB_COMPATIBLE);
379 if (lpinittables()) {
380 ppb_release_bus(ppbus, dev);
384 sc->sc_ifbuf = malloc(sc->sc_ifp->if_mtu + MLPIPHDRLEN,
386 if (sc->sc_ifbuf == NULL) {
387 ppb_release_bus(ppbus, dev);
391 ppb_wctr(ppbus, IRQENABLE);
393 ifp->if_drv_flags |= IFF_DRV_RUNNING;
394 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
399 * Process an ioctl request.
402 lpioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
404 struct lp_data *sc = ifp->if_softc;
405 device_t dev = sc->sc_dev;
406 device_t ppbus = device_get_parent(dev);
407 struct ifaddr *ifa = (struct ifaddr *)data;
408 struct ifreq *ifr = (struct ifreq *)data;
415 if (ifa->ifa_addr->sa_family != AF_INET)
416 return (EAFNOSUPPORT);
418 ifp->if_flags |= IFF_UP;
423 if ((!(ifp->if_flags & IFF_UP)) &&
424 (ifp->if_drv_flags & IFF_DRV_RUNNING))
426 else if (((ifp->if_flags & IFF_UP)) &&
427 (!(ifp->if_drv_flags & IFF_DRV_RUNNING)))
428 error = lpinit_locked(ifp);
434 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
435 ptr = malloc(ifr->ifr_mtu + MLPIPHDRLEN, M_DEVBUF,
442 free(sc->sc_ifbuf, M_DEVBUF);
445 sc->sc_ifp->if_mtu = ifr->ifr_mtu;
450 ifr->ifr_mtu = sc->sc_ifp->if_mtu;
456 return (EAFNOSUPPORT); /* XXX */
458 switch (ifr->ifr_addr.sa_family) {
462 return (EAFNOSUPPORT);
468 * No ifmedia support at this stage; maybe use it
469 * in future for eg. protocol selection.
474 lprintf("LP:ioctl(0x%lx)\n", cmd);
481 clpoutbyte(u_char byte, int spin, device_t ppbus)
484 ppb_wdtr(ppbus, ctxmitl[byte]);
485 while (ppb_rstr(ppbus) & CLPIP_SHAKE)
489 ppb_wdtr(ppbus, ctxmith[byte]);
490 while (!(ppb_rstr(ppbus) & CLPIP_SHAKE))
498 clpinbyte(int spin, device_t ppbus)
502 while ((ppb_rstr(ppbus) & CLPIP_SHAKE))
506 cl = ppb_rstr(ppbus);
507 ppb_wdtr(ppbus, 0x10);
509 while (!(ppb_rstr(ppbus) & CLPIP_SHAKE))
514 ppb_wdtr(ppbus, 0x00);
516 return (ctrecvl[cl] | ctrecvh[c]);
520 lptap(struct ifnet *ifp, struct mbuf *m)
522 u_int32_t af = AF_INET;
524 bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
530 struct lp_data *sc = arg;
531 device_t ppbus = device_get_parent(sc->sc_dev);
537 ppb_assert_locked(ppbus);
538 if (sc->sc_ifp->if_flags & IFF_LINK0) {
540 /* Ack. the request */
541 ppb_wdtr(ppbus, 0x01);
543 /* Get the packet length */
544 j = clpinbyte(LPMAXSPIN2, ppbus);
548 j = clpinbyte(LPMAXSPIN2, ppbus);
551 len = len + (j << 8);
552 if (len > sc->sc_ifp->if_mtu + MLPIPHDRLEN)
558 j = clpinbyte(LPMAXSPIN2, ppbus);
565 /* Get and ignore checksum */
566 j = clpinbyte(LPMAXSPIN2, ppbus);
571 len = bp - sc->sc_ifbuf;
572 if (len <= CLPIPHDRLEN)
578 sc->sc_ifp->if_ipackets++;
579 sc->sc_ifp->if_ibytes += len;
580 top = m_devget(sc->sc_ifbuf + CLPIPHDRLEN, len, 0, sc->sc_ifp,
584 if (bpf_peers_present(sc->sc_ifp->if_bpf))
585 lptap(sc->sc_ifp, top);
587 M_SETFIB(top, sc->sc_ifp->if_fib);
589 /* mbuf is free'd on failure. */
590 netisr_queue(NETISR_IP, top);
595 while ((ppb_rstr(ppbus) & LPIP_SHAKE)) {
596 len = sc->sc_ifp->if_mtu + LPIPHDRLEN;
600 cl = ppb_rstr(ppbus);
604 while ((ppb_rstr(ppbus) & LPIP_SHAKE))
611 *bp++= trecvh[cl] | trecvl[c];
614 while (!((cl = ppb_rstr(ppbus)) & LPIP_SHAKE)) {
616 (((cl = ppb_rstr(ppbus)) ^ 0xb8) & 0xf8) ==
625 len = bp - sc->sc_ifbuf;
626 if (len <= LPIPHDRLEN)
632 sc->sc_ifp->if_ipackets++;
633 sc->sc_ifp->if_ibytes += len;
634 top = m_devget(sc->sc_ifbuf + LPIPHDRLEN, len, 0, sc->sc_ifp,
638 if (bpf_peers_present(sc->sc_ifp->if_bpf))
639 lptap(sc->sc_ifp, top);
641 M_SETFIB(top, sc->sc_ifp->if_fib);
643 /* mbuf is free'd on failure. */
644 netisr_queue(NETISR_IP, top);
653 sc->sc_ifp->if_ierrors++;
657 * We are not able to send receive anything for now,
658 * so stop wasting our time
660 if (sc->sc_iferrs > LPMAXERRS) {
661 if_printf(sc->sc_ifp, "Too many errors, Going off-line.\n");
662 ppb_wctr(ppbus, 0x00);
663 sc->sc_ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
669 lpoutbyte(u_char byte, int spin, device_t ppbus)
672 ppb_wdtr(ppbus, txmith[byte]);
673 while (!(ppb_rstr(ppbus) & LPIP_SHAKE))
676 ppb_wdtr(ppbus, txmitl[byte]);
677 while (ppb_rstr(ppbus) & LPIP_SHAKE)
684 lpoutput(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
687 struct lp_data *sc = ifp->if_softc;
688 device_t dev = sc->sc_dev;
689 device_t ppbus = device_get_parent(dev);
697 /* We need a sensible value if we abort */
700 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
702 err = 1; /* assume we're aborting because of an error */
704 /* Suspend (on laptops) or receive-errors might have taken us offline */
705 ppb_wctr(ppbus, IRQENABLE);
707 if (ifp->if_flags & IFF_LINK0) {
708 if (!(ppb_rstr(ppbus) & CLPIP_SHAKE)) {
713 /* Alert other end to pending packet */
715 ppb_wdtr(ppbus, 0x08);
716 while ((ppb_rstr(ppbus) & 0x08) == 0)
721 /* Calculate length of packet, then send that */
723 count += 14; /* Ethernet header len */
726 for (mm = m; mm; mm = mm->m_next) {
729 if (clpoutbyte(count & 0xFF, LPMAXSPIN1, ppbus))
731 if (clpoutbyte((count >> 8) & 0xFF, LPMAXSPIN1, ppbus))
734 /* Send dummy ethernet header */
735 for (i = 0; i < 12; i++) {
736 if (clpoutbyte(i, LPMAXSPIN1, ppbus))
741 if (clpoutbyte(0x08, LPMAXSPIN1, ppbus))
743 if (clpoutbyte(0x00, LPMAXSPIN1, ppbus))
745 chksum += 0x08 + 0x00; /* Add into checksum */
749 cp = mtod(mm, u_char *);
753 if (clpoutbyte(*cp++, LPMAXSPIN2, ppbus))
756 } while ((mm = mm->m_next));
759 if (clpoutbyte(chksum, LPMAXSPIN2, ppbus))
765 err = 0; /* No errors */
768 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
769 if (err) { /* if we didn't timeout... */
774 ifp->if_obytes += m->m_pkthdr.len;
775 if (bpf_peers_present(ifp->if_bpf))
781 if (!(ppb_rstr(ppbus) & CLPIP_SHAKE)) {
789 if (ppb_rstr(ppbus) & LPIP_SHAKE) {
794 if (lpoutbyte(0x08, LPMAXSPIN1, ppbus))
796 if (lpoutbyte(0x00, LPMAXSPIN2, ppbus))
801 cp = mtod(mm, u_char *);
804 if (lpoutbyte(*cp++, LPMAXSPIN2, ppbus))
806 } while ((mm = mm->m_next));
808 err = 0; /* no errors were encountered */
812 ppb_wdtr(ppbus, txmitl[*cp] ^ 0x17);
814 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
815 if (err) { /* if we didn't timeout... */
820 ifp->if_obytes += m->m_pkthdr.len;
821 if (bpf_peers_present(ifp->if_bpf))
827 if (ppb_rstr(ppbus) & LPIP_SHAKE) {
836 static device_method_t lp_methods[] = {
837 /* device interface */
838 DEVMETHOD(device_identify, lp_identify),
839 DEVMETHOD(device_probe, lp_probe),
840 DEVMETHOD(device_attach, lp_attach),
841 DEVMETHOD(device_detach, lp_detach),
846 static driver_t lp_driver = {
849 sizeof(struct lp_data),
852 DRIVER_MODULE(plip, ppbus, lp_driver, lp_devclass, lp_module_handler, 0);
853 MODULE_DEPEND(plip, ppbus, 1, 1, 1);