2 * Copyright (c) 1998 Nicolas Souchu
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 * $FreeBSD: src/sys/dev/iicbus/if_ic.c,v 1.8 1999/12/29 04:35:39 peter Exp $
27 * $DragonFly: src/sys/dev/netif/ic/if_ic.c,v 1.3 2003/08/07 21:17:02 dillon Exp $
35 #include <sys/param.h>
36 #include <sys/systm.h>
39 #include <sys/socket.h>
40 #include <sys/filio.h>
41 #include <sys/sockio.h>
42 #include <sys/kernel.h>
43 #include <sys/module.h>
46 #include <sys/malloc.h>
49 #include <net/if_types.h>
50 #include <net/netisr.h>
54 #include <sys/socket.h>
55 #include <net/netisr.h>
56 #include <net/route.h>
57 #include <netinet/in.h>
58 #include <netinet/in_systm.h>
59 #include <netinet/in_var.h>
60 #include <netinet/ip.h>
61 #include <netinet/if_ether.h>
65 #include <bus/iicbus/iiconf.h>
66 #include <bus/iicbus/iicbus.h>
68 #include "iicbus_if.h"
70 #define ICHDRLEN sizeof(u_int)
71 #define ICMTU 1500 /* default mtu */
76 u_char ic_addr; /* peer I2C address */
89 static devclass_t ic_devclass;
91 static int icprobe(device_t);
92 static int icattach(device_t);
94 static int icioctl(struct ifnet *, u_long, caddr_t);
95 static int icoutput(struct ifnet *, struct mbuf *, struct sockaddr *,
98 static void icintr(device_t, int, char *);
100 static device_method_t ic_methods[] = {
101 /* device interface */
102 DEVMETHOD(device_probe, icprobe),
103 DEVMETHOD(device_attach, icattach),
105 /* iicbus interface */
106 DEVMETHOD(iicbus_intr, icintr),
111 static driver_t ic_driver = {
114 sizeof(struct ic_softc),
121 icprobe(device_t dev)
130 icattach(device_t dev)
132 struct ic_softc *sc = (struct ic_softc *)device_get_softc(dev);
133 struct ifnet *ifp = &sc->ic_if;
135 sc->ic_addr = iicbus_get_addr(dev);
139 ifp->if_unit = device_get_unit(dev);
141 ifp->if_flags = IFF_SIMPLEX | IFF_POINTOPOINT | IFF_MULTICAST;
142 ifp->if_ioctl = icioctl;
143 ifp->if_output = icoutput;
144 ifp->if_type = IFT_PARA;
147 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
151 bpfattach(ifp, DLT_NULL, ICHDRLEN);
160 icioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
162 device_t icdev = devclass_get_device(ic_devclass, ifp->if_unit);
163 device_t parent = device_get_parent(icdev);
164 struct ic_softc *sc = (struct ic_softc *)device_get_softc(icdev);
166 struct ifaddr *ifa = (struct ifaddr *)data;
167 struct ifreq *ifr = (struct ifreq *)data;
177 if (ifa->ifa_addr->sa_family != AF_INET)
179 ifp->if_flags |= IFF_UP;
182 if ((!(ifp->if_flags & IFF_UP)) && (ifp->if_flags & IFF_RUNNING)) {
184 /* XXX disable PCF */
185 ifp->if_flags &= ~IFF_RUNNING;
187 /* IFF_UP is not set, try to release the bus anyway */
188 iicbus_release_bus(parent, icdev);
191 if (((ifp->if_flags & IFF_UP)) && (!(ifp->if_flags & IFF_RUNNING))) {
193 if ((error = iicbus_request_bus(parent, icdev, IIC_WAIT|IIC_INTR)))
196 sc->ic_obuf = malloc(sc->ic_if.if_mtu + ICHDRLEN,
199 iicbus_release_bus(parent, icdev);
203 sc->ic_ifbuf = malloc(sc->ic_if.if_mtu + ICHDRLEN,
206 iicbus_release_bus(parent, icdev);
210 iicbus_reset(parent, IIC_FASTEST, 0, NULL);
212 ifp->if_flags |= IFF_RUNNING;
217 /* save previous buffers */
221 /* allocate input buffer */
222 sc->ic_ifbuf = malloc(ifr->ifr_mtu+ICHDRLEN, M_DEVBUF, M_NOWAIT);
231 /* allocate output buffer */
232 sc->ic_ifbuf = malloc(ifr->ifr_mtu+ICHDRLEN, M_DEVBUF, M_NOWAIT);
235 free(sc->ic_ifbuf,M_DEVBUF);
249 sc->ic_if.if_mtu = ifr->ifr_mtu;
253 ifr->ifr_mtu = sc->ic_if.if_mtu;
259 return EAFNOSUPPORT; /* XXX */
261 switch (ifr->ifr_addr.sa_family) {
281 icintr (device_t dev, int event, char *ptr)
283 struct ic_softc *sc = (struct ic_softc *)device_get_softc(dev);
284 int unit = device_get_unit(dev);
294 sc->ic_cp = sc->ic_ifbuf;
300 /* if any error occured during transfert,
305 if ((len = sc->ic_xfercnt) == 0)
311 if (IF_QFULL(&ipintrq)) {
317 sc->ic_if.if_ipackets ++;
318 sc->ic_if.if_ibytes += len;
320 if (sc->ic_if.if_bpf)
321 bpf_tap(&sc->ic_if, sc->ic_ifbuf, len + ICHDRLEN);
323 top = m_devget(sc->ic_ifbuf + ICHDRLEN, len, 0, &sc->ic_if, 0);
326 IF_ENQUEUE(&ipintrq, top);
327 schednetisr(NETISR_IP);
332 printf("ic%d: errors (%d)!\n", unit, sc->ic_iferrs);
334 sc->ic_iferrs = 0; /* reset error count */
335 sc->ic_if.if_ierrors ++;
340 if (sc->ic_xfercnt >= sc->ic_if.if_mtu+ICHDRLEN) {
349 case INTR_NOACK: /* xfer terminated by master */
353 *ptr = 0xff; /* XXX */
361 panic("%s: unknown event (%d)!", __FUNCTION__, event);
372 icoutput(struct ifnet *ifp, struct mbuf *m,
373 struct sockaddr *dst, struct rtentry *rt)
375 device_t icdev = devclass_get_device(ic_devclass, ifp->if_unit);
376 device_t parent = device_get_parent(icdev);
377 struct ic_softc *sc = (struct ic_softc *)device_get_softc(icdev);
382 u_int hdr = dst->sa_family;
384 ifp->if_flags |= IFF_RUNNING;
388 /* already sending? */
389 if (sc->ic_sending) {
395 bcopy ((char *)&hdr, sc->ic_obuf, ICHDRLEN);
397 cp = sc->ic_obuf + ICHDRLEN;
401 if (len + mm->m_len > sc->ic_if.if_mtu) {
402 /* packet to large */
407 bcopy(mtod(mm,char *), cp, mm->m_len);
411 } while ((mm = mm->m_next));
414 struct mbuf m0, *n = m;
417 * We need to prepend the address family as
418 * a four byte field. Cons up a dummy header
419 * to pacify bpf. This is safe because bpf
420 * will only read from the mbuf (i.e., it won't
421 * try to free it or keep a pointer a to it).
424 m0.m_len = sizeof(u_int);
425 m0.m_data = (char *)&hdr;
436 /* send the packet */
437 if (iicbus_block_write(parent, sc->ic_addr, sc->ic_obuf,
438 len + ICHDRLEN, &sent))
443 ifp->if_obytes += len;
457 DRIVER_MODULE(ic, iicbus, ic_driver, ic_devclass, 0, 0);