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 $
34 #include <sys/param.h>
35 #include <sys/systm.h>
38 #include <sys/socket.h>
39 #include <sys/filio.h>
40 #include <sys/sockio.h>
41 #include <sys/kernel.h>
42 #include <sys/module.h>
45 #include <sys/malloc.h>
48 #include <net/if_types.h>
49 #include <net/netisr.h>
53 #include <sys/socket.h>
54 #include <net/netisr.h>
55 #include <net/route.h>
56 #include <netinet/in.h>
57 #include <netinet/in_systm.h>
58 #include <netinet/in_var.h>
59 #include <netinet/ip.h>
60 #include <netinet/if_ether.h>
64 #include <dev/iicbus/iiconf.h>
65 #include <dev/iicbus/iicbus.h>
67 #include "iicbus_if.h"
69 #define ICHDRLEN sizeof(u_int)
70 #define ICMTU 1500 /* default mtu */
75 u_char ic_addr; /* peer I2C address */
88 static devclass_t ic_devclass;
90 static int icprobe(device_t);
91 static int icattach(device_t);
93 static int icioctl(struct ifnet *, u_long, caddr_t);
94 static int icoutput(struct ifnet *, struct mbuf *, struct sockaddr *,
97 static void icintr(device_t, int, char *);
99 static device_method_t ic_methods[] = {
100 /* device interface */
101 DEVMETHOD(device_probe, icprobe),
102 DEVMETHOD(device_attach, icattach),
104 /* iicbus interface */
105 DEVMETHOD(iicbus_intr, icintr),
110 static driver_t ic_driver = {
113 sizeof(struct ic_softc),
120 icprobe(device_t dev)
129 icattach(device_t dev)
131 struct ic_softc *sc = (struct ic_softc *)device_get_softc(dev);
132 struct ifnet *ifp = &sc->ic_if;
134 sc->ic_addr = iicbus_get_addr(dev);
138 ifp->if_unit = device_get_unit(dev);
140 ifp->if_flags = IFF_SIMPLEX | IFF_POINTOPOINT | IFF_MULTICAST;
141 ifp->if_ioctl = icioctl;
142 ifp->if_output = icoutput;
143 ifp->if_type = IFT_PARA;
146 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
150 bpfattach(ifp, DLT_NULL, ICHDRLEN);
159 icioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
161 device_t icdev = devclass_get_device(ic_devclass, ifp->if_unit);
162 device_t parent = device_get_parent(icdev);
163 struct ic_softc *sc = (struct ic_softc *)device_get_softc(icdev);
165 struct ifaddr *ifa = (struct ifaddr *)data;
166 struct ifreq *ifr = (struct ifreq *)data;
176 if (ifa->ifa_addr->sa_family != AF_INET)
178 ifp->if_flags |= IFF_UP;
181 if ((!(ifp->if_flags & IFF_UP)) && (ifp->if_flags & IFF_RUNNING)) {
183 /* XXX disable PCF */
184 ifp->if_flags &= ~IFF_RUNNING;
186 /* IFF_UP is not set, try to release the bus anyway */
187 iicbus_release_bus(parent, icdev);
190 if (((ifp->if_flags & IFF_UP)) && (!(ifp->if_flags & IFF_RUNNING))) {
192 if ((error = iicbus_request_bus(parent, icdev, IIC_WAIT|IIC_INTR)))
195 sc->ic_obuf = malloc(sc->ic_if.if_mtu + ICHDRLEN,
198 iicbus_release_bus(parent, icdev);
202 sc->ic_ifbuf = malloc(sc->ic_if.if_mtu + ICHDRLEN,
205 iicbus_release_bus(parent, icdev);
209 iicbus_reset(parent, IIC_FASTEST, 0, NULL);
211 ifp->if_flags |= IFF_RUNNING;
216 /* save previous buffers */
220 /* allocate input buffer */
221 sc->ic_ifbuf = malloc(ifr->ifr_mtu+ICHDRLEN, M_DEVBUF, M_NOWAIT);
230 /* allocate output buffer */
231 sc->ic_ifbuf = malloc(ifr->ifr_mtu+ICHDRLEN, M_DEVBUF, M_NOWAIT);
234 free(sc->ic_ifbuf,M_DEVBUF);
248 sc->ic_if.if_mtu = ifr->ifr_mtu;
252 ifr->ifr_mtu = sc->ic_if.if_mtu;
258 return EAFNOSUPPORT; /* XXX */
260 switch (ifr->ifr_addr.sa_family) {
280 icintr (device_t dev, int event, char *ptr)
282 struct ic_softc *sc = (struct ic_softc *)device_get_softc(dev);
283 int unit = device_get_unit(dev);
293 sc->ic_cp = sc->ic_ifbuf;
299 /* if any error occured during transfert,
304 if ((len = sc->ic_xfercnt) == 0)
310 if (IF_QFULL(&ipintrq)) {
316 sc->ic_if.if_ipackets ++;
317 sc->ic_if.if_ibytes += len;
319 if (sc->ic_if.if_bpf)
320 bpf_tap(&sc->ic_if, sc->ic_ifbuf, len + ICHDRLEN);
322 top = m_devget(sc->ic_ifbuf + ICHDRLEN, len, 0, &sc->ic_if, 0);
325 IF_ENQUEUE(&ipintrq, top);
326 schednetisr(NETISR_IP);
331 printf("ic%d: errors (%d)!\n", unit, sc->ic_iferrs);
333 sc->ic_iferrs = 0; /* reset error count */
334 sc->ic_if.if_ierrors ++;
339 if (sc->ic_xfercnt >= sc->ic_if.if_mtu+ICHDRLEN) {
348 case INTR_NOACK: /* xfer terminated by master */
352 *ptr = 0xff; /* XXX */
360 panic("%s: unknown event (%d)!", __FUNCTION__, event);
371 icoutput(struct ifnet *ifp, struct mbuf *m,
372 struct sockaddr *dst, struct rtentry *rt)
374 device_t icdev = devclass_get_device(ic_devclass, ifp->if_unit);
375 device_t parent = device_get_parent(icdev);
376 struct ic_softc *sc = (struct ic_softc *)device_get_softc(icdev);
381 u_int hdr = dst->sa_family;
383 ifp->if_flags |= IFF_RUNNING;
387 /* already sending? */
388 if (sc->ic_sending) {
394 bcopy ((char *)&hdr, sc->ic_obuf, ICHDRLEN);
396 cp = sc->ic_obuf + ICHDRLEN;
400 if (len + mm->m_len > sc->ic_if.if_mtu) {
401 /* packet to large */
406 bcopy(mtod(mm,char *), cp, mm->m_len);
410 } while ((mm = mm->m_next));
413 struct mbuf m0, *n = m;
416 * We need to prepend the address family as
417 * a four byte field. Cons up a dummy header
418 * to pacify bpf. This is safe because bpf
419 * will only read from the mbuf (i.e., it won't
420 * try to free it or keep a pointer a to it).
423 m0.m_len = sizeof(u_int);
424 m0.m_data = (char *)&hdr;
435 /* send the packet */
436 if (iicbus_block_write(parent, sc->ic_addr, sc->ic_obuf,
437 len + ICHDRLEN, &sent))
442 ifp->if_obytes += len;
456 DRIVER_MODULE(ic, iicbus, ic_driver, ic_devclass, 0, 0);