if_ic.c revision 38775
1/*-
2 * Copyright (c) 1998 Nicolas Souchu
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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
24 * SUCH DAMAGE.
25 *
26 *	$Id: if_ic.c,v 1.1.1.11 1998/08/13 17:10:42 son Exp $
27 */
28
29/*
30 * I2C bus IP driver
31 */
32
33#ifdef KERNEL
34#include <sys/param.h>
35#include <sys/systm.h>
36#include <sys/proc.h>
37#include <sys/mbuf.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>
43#include <sys/bus.h>
44#include <sys/time.h>
45#include <sys/malloc.h>
46
47#include <net/if.h>
48#include <net/if_types.h>
49#include <net/netisr.h>
50
51#endif /* KERNEL */
52#include <sys/mbuf.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>
61
62#include "bpfilter.h"
63
64#if NBPFILTER > 0
65#include <net/bpf.h>
66#endif
67
68#include <dev/iicbus/iiconf.h>
69#include <dev/iicbus/iicbus.h>
70
71#include "iicbus_if.h"
72
73#define ICHDRLEN	sizeof(u_int)
74#define ICMTU		1500		/* default mtu */
75
76struct ic_softc {
77	struct ifnet ic_if;
78
79	u_char ic_addr;			/* peer I2C address */
80
81	int ic_sending;
82
83	char *ic_obuf;
84	char *ic_ifbuf;
85	char *ic_cp;
86
87	int ic_xfercnt;
88
89	int ic_iferrs;
90};
91
92static devclass_t ic_devclass;
93
94static int icprobe(device_t);
95static int icattach(device_t);
96
97static int icioctl(struct ifnet *, u_long, caddr_t);
98static int icoutput(struct ifnet *, struct mbuf *, struct sockaddr *,
99		struct rtentry *);
100
101static void icintr(device_t, int, char *);
102
103static device_method_t ic_methods[] = {
104	/* device interface */
105	DEVMETHOD(device_probe,		icprobe),
106	DEVMETHOD(device_attach,	icattach),
107
108	/* iicbus interface */
109	DEVMETHOD(iicbus_intr,		icintr),
110
111	{ 0, 0 }
112};
113
114static driver_t ic_driver = {
115	"ic",
116	ic_methods,
117	DRIVER_TYPE_MISC,
118	sizeof(struct ic_softc),
119};
120
121/*
122 * icprobe()
123 */
124static int
125icprobe(device_t dev)
126{
127	return (0);
128}
129
130/*
131 * icattach()
132 */
133static int
134icattach(device_t dev)
135{
136	struct ic_softc *sc = (struct ic_softc *)device_get_softc(dev);
137	struct ifnet *ifp = &sc->ic_if;
138
139	sc->ic_addr = iicbus_get_addr(dev);
140
141	ifp->if_softc = sc;
142	ifp->if_name = "ic";
143	ifp->if_unit = device_get_unit(dev);
144	ifp->if_mtu = ICMTU;
145	ifp->if_flags = IFF_SIMPLEX | IFF_POINTOPOINT | IFF_MULTICAST;
146	ifp->if_ioctl = icioctl;
147	ifp->if_output = icoutput;
148	ifp->if_type = IFT_PARA;
149	ifp->if_hdrlen = 0;
150	ifp->if_addrlen = 0;
151	ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
152
153	if_attach(ifp);
154
155#if NBPFILTER > 0
156	bpfattach(ifp, DLT_NULL, ICHDRLEN);
157#endif
158
159	return (0);
160}
161
162/*
163 * iciotcl()
164 */
165static int
166icioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
167{
168    device_t icdev = devclass_get_device(ic_devclass, ifp->if_unit);
169    device_t parent = device_get_parent(icdev);
170    struct ic_softc *sc = (struct ic_softc *)device_get_softc(icdev);
171
172    struct ifaddr *ifa = (struct ifaddr *)data;
173    struct ifreq *ifr = (struct ifreq *)data;
174
175    u_char *iptr, *optr;
176    int error;
177
178    switch (cmd) {
179
180    case SIOCSIFDSTADDR:
181    case SIOCAIFADDR:
182    case SIOCSIFADDR:
183	if (ifa->ifa_addr->sa_family != AF_INET)
184	    return EAFNOSUPPORT;
185	ifp->if_flags |= IFF_UP;
186	/* FALLTHROUGH */
187    case SIOCSIFFLAGS:
188	if ((!(ifp->if_flags & IFF_UP)) && (ifp->if_flags & IFF_RUNNING)) {
189
190	    /* XXX disable PCF */
191	    ifp->if_flags &= ~IFF_RUNNING;
192
193	    /* IFF_UP is not set, try to release the bus anyway */
194	    iicbus_release_bus(parent, icdev);
195	    break;
196	}
197	if (((ifp->if_flags & IFF_UP)) && (!(ifp->if_flags & IFF_RUNNING))) {
198
199	    if ((error = iicbus_request_bus(parent, icdev, IIC_WAIT|IIC_INTR)))
200		return (error);
201
202	    sc->ic_obuf = malloc(sc->ic_if.if_mtu + ICHDRLEN,
203				  M_DEVBUF, M_WAITOK);
204	    if (!sc->ic_obuf) {
205		iicbus_release_bus(parent, icdev);
206		return ENOBUFS;
207	    }
208
209	    sc->ic_ifbuf = malloc(sc->ic_if.if_mtu + ICHDRLEN,
210				  M_DEVBUF, M_WAITOK);
211	    if (!sc->ic_ifbuf) {
212		iicbus_release_bus(parent, icdev);
213		return ENOBUFS;
214	    }
215
216	    iicbus_reset(parent, IIC_FASTEST);
217
218	    ifp->if_flags |= IFF_RUNNING;
219	}
220	break;
221
222    case SIOCSIFMTU:
223	/* save previous buffers */
224	iptr = sc->ic_ifbuf;
225	optr = sc->ic_obuf;
226
227	/* allocate input buffer */
228	sc->ic_ifbuf = malloc(ifr->ifr_mtu+ICHDRLEN, M_DEVBUF, M_NOWAIT);
229	if (!sc->ic_ifbuf) {
230
231	    sc->ic_ifbuf = iptr;
232	    sc->ic_obuf = optr;
233
234	    return ENOBUFS;
235	}
236
237	/* allocate output buffer */
238	sc->ic_ifbuf = malloc(ifr->ifr_mtu+ICHDRLEN, M_DEVBUF, M_NOWAIT);
239	if (!sc->ic_obuf) {
240
241	    free(sc->ic_ifbuf,M_DEVBUF);
242
243	    sc->ic_ifbuf = iptr;
244	    sc->ic_obuf = optr;
245
246	    return ENOBUFS;
247	}
248
249	if (iptr)
250	    free(iptr,M_DEVBUF);
251
252	if (optr)
253	    free(optr,M_DEVBUF);
254
255	sc->ic_if.if_mtu = ifr->ifr_mtu;
256	break;
257
258    case SIOCGIFMTU:
259	ifr->ifr_mtu = sc->ic_if.if_mtu;
260	break;
261
262    case SIOCADDMULTI:
263    case SIOCDELMULTI:
264	if (ifr == 0) {
265	    return EAFNOSUPPORT;		/* XXX */
266	}
267	switch (ifr->ifr_addr.sa_family) {
268
269	case AF_INET:
270	    break;
271
272	default:
273	    return EAFNOSUPPORT;
274	}
275	break;
276
277    default:
278	return EINVAL;
279    }
280    return 0;
281}
282
283/*
284 * icintr()
285 */
286static void
287icintr (device_t dev, int event, char *ptr)
288{
289	struct ic_softc *sc = (struct ic_softc *)device_get_softc(dev);
290	int unit = device_get_unit(dev);
291	int s, len;
292	struct mbuf *top;
293	int i;
294
295	s = splhigh();
296
297	switch (event) {
298
299	case INTR_GENERAL:
300	case INTR_START:
301		sc->ic_cp = sc->ic_ifbuf;
302		sc->ic_xfercnt = 0;
303		break;
304
305	case INTR_STOP:
306
307	  /* if any error occured during transfert,
308	   * drop the packet */
309	  if (sc->ic_iferrs)
310	    goto err;
311
312	  if ((len = sc->ic_xfercnt) == 0)
313		break;					/* ignore */
314
315	  if (len <= ICHDRLEN)
316	    goto err;
317
318	  if (IF_QFULL(&ipintrq)) {
319	    IF_DROP(&ipintrq);
320	    break;
321	  }
322
323	  len -= ICHDRLEN;
324	  sc->ic_if.if_ipackets ++;
325	  sc->ic_if.if_ibytes += len;
326
327#if NBPFILTER > 0
328	if (sc->ic_if.if_bpf)
329		bpf_tap(&sc->ic_if, sc->ic_ifbuf, len + ICHDRLEN);
330#endif
331
332	  top = m_devget(sc->ic_ifbuf + ICHDRLEN, len, 0, &sc->ic_if, 0);
333
334	  if (top) {
335	    IF_ENQUEUE(&ipintrq, top);
336	    schednetisr(NETISR_IP);
337	  }
338	  break;
339
340	err:
341	  printf("ic%d: errors (%d)!\n", unit, sc->ic_iferrs);
342
343	  sc->ic_iferrs = 0;			/* reset error count */
344	  sc->ic_if.if_ierrors ++;
345
346	  break;
347
348	case INTR_RECEIVE:
349		if (sc->ic_xfercnt >= sc->ic_if.if_mtu+ICHDRLEN) {
350			sc->ic_iferrs ++;
351
352		} else {
353			*sc->ic_cp++ = *ptr;
354			sc->ic_xfercnt ++;
355		}
356		break;
357
358	case INTR_NOACK:			/* xfer terminated by master */
359		break;
360
361	case INTR_TRANSMIT:
362		*ptr = 0xff;					/* XXX */
363	  	break;
364
365	case INTR_ERROR:
366		sc->ic_iferrs ++;
367		break;
368
369	default:
370		panic("%s: unknown event (%d)!", __FUNCTION__, event);
371	}
372
373	splx(s);
374	return;
375}
376
377/*
378 * icoutput()
379 */
380static int
381icoutput(struct ifnet *ifp, struct mbuf *m,
382	struct sockaddr *dst, struct rtentry *rt)
383{
384	device_t icdev = devclass_get_device(ic_devclass, ifp->if_unit);
385	device_t parent = device_get_parent(icdev);
386	struct ic_softc *sc = (struct ic_softc *)device_get_softc(icdev);
387
388	int s, len, sent;
389	struct mbuf *mm;
390	u_char *cp;
391	u_int hdr = dst->sa_family;
392
393	ifp->if_flags |= IFF_RUNNING;
394
395	s = splhigh();
396
397	/* already sending? */
398	if (sc->ic_sending) {
399		ifp->if_oerrors ++;
400		goto error;
401	}
402
403	/* insert header */
404	bcopy ((char *)&hdr, sc->ic_obuf, ICHDRLEN);
405
406	cp = sc->ic_obuf + ICHDRLEN;
407	len = 0;
408	mm = m;
409	do {
410		if (len + mm->m_len > sc->ic_if.if_mtu) {
411			/* packet to large */
412			ifp->if_oerrors ++;
413			goto error;
414		}
415
416		bcopy(mtod(mm,char *), cp, mm->m_len);
417		cp += mm->m_len;
418		len += mm->m_len;
419
420	} while ((mm = mm->m_next));
421
422#if NBPFILTER > 0
423	if (ifp->if_bpf) {
424		struct mbuf m0, *n = m;
425
426		/*
427		 * We need to prepend the address family as
428		 * a four byte field.  Cons up a dummy header
429		 * to pacify bpf.  This is safe because bpf
430		 * will only read from the mbuf (i.e., it won't
431		 * try to free it or keep a pointer a to it).
432		 */
433		m0.m_next = m;
434		m0.m_len = sizeof(u_int);
435		m0.m_data = (char *)&hdr;
436		n = &m0;
437
438		bpf_mtap(ifp, n);
439	}
440#endif
441
442	sc->ic_sending = 1;
443
444	m_freem(m);
445	splx(s);
446
447	/* send the packet */
448	if (iicbus_block_write(parent, sc->ic_addr, sc->ic_obuf,
449				len + ICHDRLEN, &sent))
450
451		ifp->if_oerrors ++;
452	else {
453		ifp->if_opackets ++;
454		ifp->if_obytes += len;
455	}
456
457	sc->ic_sending = 0;
458
459	return (0);
460
461error:
462	m_freem(m);
463	splx(s);
464
465	return(0);
466}
467
468DRIVER_MODULE(ic, iicbus, ic_driver, ic_devclass, 0, 0);
469