xref: /freebsd/sys/dev/iicbus/if_ic.c (revision 7bd6fde3)
1 /*-
2  * Copyright (c) 1998, 2001 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 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 /*
31  * I2C bus IP driver
32  */
33 
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 #include <sys/mbuf.h>
51 #include <sys/socket.h>
52 #include <net/netisr.h>
53 #include <net/route.h>
54 #include <netinet/in.h>
55 #include <netinet/in_systm.h>
56 #include <netinet/in_var.h>
57 #include <netinet/ip.h>
58 #include <netinet/if_ether.h>
59 
60 #include <net/bpf.h>
61 
62 #include <dev/iicbus/iiconf.h>
63 #include <dev/iicbus/iicbus.h>
64 
65 #include "iicbus_if.h"
66 
67 #define PCF_MASTER_ADDRESS 0xaa
68 
69 #define ICHDRLEN	sizeof(u_int32_t)
70 #define ICMTU		1500		/* default mtu */
71 
72 struct ic_softc {
73 	struct ifnet *ic_ifp;
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;
133 
134 	ifp = sc->ic_ifp = if_alloc(IFT_PARA);
135 	if (ifp == NULL)
136 		return (ENOSPC);
137 
138 	sc->ic_addr = PCF_MASTER_ADDRESS;	/* XXX only PCF masters */
139 
140 	ifp->if_softc = sc;
141 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
142 	ifp->if_mtu = ICMTU;
143 	ifp->if_flags = IFF_SIMPLEX | IFF_POINTOPOINT | IFF_MULTICAST |
144 	    IFF_NEEDSGIANT;
145 	ifp->if_ioctl = icioctl;
146 	ifp->if_output = icoutput;
147 	ifp->if_hdrlen = 0;
148 	ifp->if_addrlen = 0;
149 	ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
150 
151 	if_attach(ifp);
152 
153 	bpfattach(ifp, DLT_NULL, ICHDRLEN);
154 
155 	return (0);
156 }
157 
158 /*
159  * iciotcl()
160  */
161 static int
162 icioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
163 {
164 	device_t icdev = devclass_get_device(ic_devclass, ifp->if_dunit);
165 	device_t parent = device_get_parent(icdev);
166 	struct ic_softc *sc = (struct ic_softc *)device_get_softc(icdev);
167 
168 	struct ifaddr *ifa = (struct ifaddr *)data;
169 	struct ifreq *ifr = (struct ifreq *)data;
170 
171 	u_char *iptr, *optr;
172 	int error;
173 
174 	switch (cmd) {
175 
176 	case SIOCSIFDSTADDR:
177 	case SIOCAIFADDR:
178 	case SIOCSIFADDR:
179 		if (ifa->ifa_addr->sa_family != AF_INET)
180 			return (EAFNOSUPPORT);
181 		ifp->if_flags |= IFF_UP;
182 		/* FALLTHROUGH */
183 	case SIOCSIFFLAGS:
184 		if ((!(ifp->if_flags & IFF_UP)) &&
185 		    (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
186 
187 			/* XXX disable PCF */
188 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
189 
190 			/* IFF_UP is not set, try to release the bus anyway */
191 			iicbus_release_bus(parent, icdev);
192 			break;
193 		}
194 		if (((ifp->if_flags & IFF_UP)) &&
195 		    (!(ifp->if_drv_flags & IFF_DRV_RUNNING))) {
196 			if ((error = iicbus_request_bus(parent, icdev,
197 			    IIC_WAIT | IIC_INTR)))
198 				return (error);
199 			sc->ic_obuf = malloc(sc->ic_ifp->if_mtu + ICHDRLEN,
200 			    M_DEVBUF, M_WAITOK);
201 			if (!sc->ic_obuf) {
202 				iicbus_release_bus(parent, icdev);
203 				return (ENOBUFS);
204 			}
205 			sc->ic_ifbuf = malloc(sc->ic_ifp->if_mtu + ICHDRLEN,
206 			    M_DEVBUF, M_WAITOK);
207 			if (!sc->ic_ifbuf) {
208 				iicbus_release_bus(parent, icdev);
209 				return (ENOBUFS);
210 			}
211 			iicbus_reset(parent, IIC_FASTEST, 0, NULL);
212 			ifp->if_drv_flags |= IFF_DRV_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 			sc->ic_ifbuf = iptr;
225 			sc->ic_obuf = optr;
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 			free(sc->ic_ifbuf,M_DEVBUF);
233 			sc->ic_ifbuf = iptr;
234 			sc->ic_obuf = optr;
235 			return (ENOBUFS);
236 		}
237 
238 		if (iptr)
239 			free(iptr,M_DEVBUF);
240 		if (optr)
241 			free(optr,M_DEVBUF);
242 		sc->ic_ifp->if_mtu = ifr->ifr_mtu;
243 		break;
244 
245 	case SIOCGIFMTU:
246 		ifr->ifr_mtu = sc->ic_ifp->if_mtu;
247 		break;
248 
249 	case SIOCADDMULTI:
250 	case SIOCDELMULTI:
251 		if (ifr == 0)
252 			return (EAFNOSUPPORT);		/* XXX */
253 		switch (ifr->ifr_addr.sa_family) {
254 		case AF_INET:
255 			break;
256 		default:
257 			return (EAFNOSUPPORT);
258 		}
259 		break;
260 	default:
261 		return (EINVAL);
262 	}
263 	return (0);
264 }
265 
266 /*
267  * icintr()
268  */
269 static void
270 icintr(device_t dev, int event, char *ptr)
271 {
272 	struct ic_softc *sc = (struct ic_softc *)device_get_softc(dev);
273 	int unit = device_get_unit(dev);
274 	int s, len;
275 	struct mbuf *top;
276 
277 	s = splhigh();
278 
279 	switch (event) {
280 
281 	case INTR_GENERAL:
282 	case INTR_START:
283 		sc->ic_cp = sc->ic_ifbuf;
284 		sc->ic_xfercnt = 0;
285 		break;
286 
287 	case INTR_STOP:
288 
289 		/* if any error occured during transfert,
290 		 * drop the packet */
291 		if (sc->ic_iferrs)
292 			goto err;
293 		if ((len = sc->ic_xfercnt) == 0)
294 			break;					/* ignore */
295 		if (len <= ICHDRLEN)
296 			goto err;
297 		len -= ICHDRLEN;
298 		sc->ic_ifp->if_ipackets++;
299 		sc->ic_ifp->if_ibytes += len;
300 		BPF_TAP(sc->ic_ifp, sc->ic_ifbuf, len + ICHDRLEN);
301 		top = m_devget(sc->ic_ifbuf + ICHDRLEN, len, 0, sc->ic_ifp, 0);
302 		if (top)
303 			netisr_dispatch(NETISR_IP, top);
304 		break;
305 	err:
306 		printf("ic%d: errors (%d)!\n", unit, sc->ic_iferrs);
307 		sc->ic_iferrs = 0;			/* reset error count */
308 		sc->ic_ifp->if_ierrors++;
309 		break;
310 
311 	case INTR_RECEIVE:
312 		if (sc->ic_xfercnt >= sc->ic_ifp->if_mtu+ICHDRLEN) {
313 			sc->ic_iferrs++;
314 		} else {
315 			*sc->ic_cp++ = *ptr;
316 			sc->ic_xfercnt++;
317 		}
318 		break;
319 
320 	case INTR_NOACK:			/* xfer terminated by master */
321 		break;
322 
323 	case INTR_TRANSMIT:
324 		*ptr = 0xff;					/* XXX */
325 	  	break;
326 
327 	case INTR_ERROR:
328 		sc->ic_iferrs++;
329 		break;
330 
331 	default:
332 		panic("%s: unknown event (%d)!", __func__, event);
333 	}
334 
335 	splx(s);
336 	return;
337 }
338 
339 /*
340  * icoutput()
341  */
342 static int
343 icoutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
344     struct rtentry *rt)
345 {
346 	device_t icdev = devclass_get_device(ic_devclass, ifp->if_dunit);
347 	device_t parent = device_get_parent(icdev);
348 	struct ic_softc *sc = (struct ic_softc *)device_get_softc(icdev);
349 
350 	int s, len, sent;
351 	struct mbuf *mm;
352 	u_char *cp;
353 	u_int32_t hdr;
354 
355 	/* BPF writes need to be handled specially. */
356 	if (dst->sa_family == AF_UNSPEC)
357 		bcopy(dst->sa_data, &hdr, sizeof(hdr));
358 	else
359 		hdr = dst->sa_family;
360 
361 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
362 
363 	s = splhigh();
364 
365 	/* already sending? */
366 	if (sc->ic_sending) {
367 		ifp->if_oerrors++;
368 		goto error;
369 	}
370 
371 	/* insert header */
372 	bcopy ((char *)&hdr, sc->ic_obuf, ICHDRLEN);
373 
374 	cp = sc->ic_obuf + ICHDRLEN;
375 	len = 0;
376 	mm = m;
377 	do {
378 		if (len + mm->m_len > sc->ic_ifp->if_mtu) {
379 			/* packet to large */
380 			ifp->if_oerrors++;
381 			goto error;
382 		}
383 
384 		bcopy(mtod(mm,char *), cp, mm->m_len);
385 		cp += mm->m_len;
386 		len += mm->m_len;
387 
388 	} while ((mm = mm->m_next));
389 
390 	BPF_MTAP2(ifp, &hdr, sizeof(hdr), m);
391 
392 	sc->ic_sending = 1;
393 
394 	m_freem(m);
395 	splx(s);
396 
397 	/* send the packet */
398 	if (iicbus_block_write(parent, sc->ic_addr, sc->ic_obuf,
399 				len + ICHDRLEN, &sent))
400 
401 		ifp->if_oerrors++;
402 	else {
403 		ifp->if_opackets++;
404 		ifp->if_obytes += len;
405 	}
406 
407 	sc->ic_sending = 0;
408 
409 	return (0);
410 
411 error:
412 	m_freem(m);
413 	splx(s);
414 
415 	return(0);
416 }
417 
418 DRIVER_MODULE(ic, iicbus, ic_driver, ic_devclass, 0, 0);
419 MODULE_DEPEND(ic, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER);
420 MODULE_VERSION(ic, 1);
421