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