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