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