xref: /freebsd/sys/dev/iicbus/if_ic.c (revision d9a42747950146bf03cda7f6e25d219253f8a57a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 1998, 2001 Nicolas Souchu
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 /*
33  * I2C bus IP driver
34  */
35 
36 #include <sys/param.h>
37 #include <sys/systm.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/lock.h>
44 #include <sys/module.h>
45 #include <sys/mutex.h>
46 #include <sys/bus.h>
47 #include <sys/time.h>
48 #include <sys/malloc.h>
49 
50 #include <net/if.h>
51 #include <net/if_var.h>
52 #include <net/if_types.h>
53 #include <net/netisr.h>
54 
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 PCF_MASTER_ADDRESS 0xaa
70 
71 #define ICHDRLEN	sizeof(u_int32_t)
72 #define ICMTU		1500		/* default mtu */
73 
74 struct ic_softc {
75 	if_t ic_ifp;
76 	device_t ic_dev;
77 
78 	u_char ic_addr;			/* peer I2C address */
79 
80 	int ic_flags;
81 
82 	char *ic_obuf;
83 	char *ic_ifbuf;
84 	char *ic_cp;
85 
86 	int ic_xfercnt;
87 
88 	int ic_iferrs;
89 
90 	struct mtx ic_lock;
91 };
92 
93 #define	IC_SENDING		0x0001
94 #define	IC_OBUF_BUSY		0x0002
95 #define	IC_IFBUF_BUSY		0x0004
96 #define	IC_BUFFERS_BUSY		(IC_OBUF_BUSY | IC_IFBUF_BUSY)
97 #define	IC_BUFFER_WAITER	0x0004
98 
99 static int icprobe(device_t);
100 static int icattach(device_t);
101 
102 static int icioctl(if_t, u_long, caddr_t);
103 static int icoutput(if_t, struct mbuf *, const struct sockaddr *,
104                struct route *);
105 
106 static int icintr(device_t, int, char *);
107 
108 static device_method_t ic_methods[] = {
109 	/* device interface */
110 	DEVMETHOD(device_probe,		icprobe),
111 	DEVMETHOD(device_attach,	icattach),
112 
113 	/* iicbus interface */
114 	DEVMETHOD(iicbus_intr,		icintr),
115 
116 	{ 0, 0 }
117 };
118 
119 static driver_t ic_driver = {
120 	"ic",
121 	ic_methods,
122 	sizeof(struct ic_softc),
123 };
124 
125 static void
126 ic_alloc_buffers(struct ic_softc *sc, int mtu)
127 {
128 	char *obuf, *ifbuf;
129 
130 	obuf = malloc(mtu + ICHDRLEN, M_DEVBUF, M_WAITOK);
131 	ifbuf = malloc(mtu + ICHDRLEN, M_DEVBUF, M_WAITOK);
132 
133 	mtx_lock(&sc->ic_lock);
134 	while (sc->ic_flags & IC_BUFFERS_BUSY) {
135 		sc->ic_flags |= IC_BUFFER_WAITER;
136 		mtx_sleep(sc, &sc->ic_lock, 0, "icalloc", 0);
137 		sc->ic_flags &= ~IC_BUFFER_WAITER;
138 	}
139 
140 	free(sc->ic_obuf, M_DEVBUF);
141 	free(sc->ic_ifbuf, M_DEVBUF);
142 	sc->ic_obuf = obuf;
143 	sc->ic_ifbuf = ifbuf;
144 	if_setmtu(sc->ic_ifp, mtu);
145 	mtx_unlock(&sc->ic_lock);
146 }
147 
148 /*
149  * icprobe()
150  */
151 static int
152 icprobe(device_t dev)
153 {
154 	return (BUS_PROBE_NOWILDCARD);
155 }
156 
157 /*
158  * icattach()
159  */
160 static int
161 icattach(device_t dev)
162 {
163 	struct ic_softc *sc = (struct ic_softc *)device_get_softc(dev);
164 	if_t ifp;
165 
166 	ifp = sc->ic_ifp = if_alloc(IFT_PARA);
167 	if (ifp == NULL)
168 		return (ENOSPC);
169 
170 	mtx_init(&sc->ic_lock, device_get_nameunit(dev), MTX_NETWORK_LOCK,
171 	    MTX_DEF);
172 	sc->ic_addr = PCF_MASTER_ADDRESS;	/* XXX only PCF masters */
173 	sc->ic_dev = dev;
174 
175 	if_setsoftc(ifp, sc);
176 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
177 	if_setflags(ifp, IFF_SIMPLEX | IFF_POINTOPOINT | IFF_MULTICAST);
178 	if_setioctlfn(ifp, icioctl);
179 	if_setoutputfn(ifp, icoutput);
180 	if_setifheaderlen(ifp, 0);
181 	if_setsendqlen(ifp, ifqmaxlen);
182 
183 	ic_alloc_buffers(sc, ICMTU);
184 
185 	if_attach(ifp);
186 
187 	bpfattach(ifp, DLT_NULL, ICHDRLEN);
188 
189 	return (0);
190 }
191 
192 /*
193  * iciotcl()
194  */
195 static int
196 icioctl(if_t ifp, u_long cmd, caddr_t data)
197 {
198 	struct ic_softc *sc = if_getsoftc(ifp);
199 	device_t icdev = sc->ic_dev;
200 	device_t parent = device_get_parent(icdev);
201 	struct ifaddr *ifa = (struct ifaddr *)data;
202 	struct ifreq *ifr = (struct ifreq *)data;
203 	int error;
204 
205 	switch (cmd) {
206 
207 	case SIOCAIFADDR:
208 	case SIOCSIFADDR:
209 		if (ifa->ifa_addr->sa_family != AF_INET)
210 			return (EAFNOSUPPORT);
211 		mtx_lock(&sc->ic_lock);
212 		if_setflagbits(ifp, IFF_UP, 0);
213 		goto locked;
214 	case SIOCSIFFLAGS:
215 		mtx_lock(&sc->ic_lock);
216 	locked:
217 		if ((!(if_getflags(ifp) & IFF_UP)) &&
218 		    (if_getdrvflags(ifp) & IFF_DRV_RUNNING)) {
219 
220 			/* XXX disable PCF */
221 			if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
222 			mtx_unlock(&sc->ic_lock);
223 
224 			/* IFF_UP is not set, try to release the bus anyway */
225 			iicbus_release_bus(parent, icdev);
226 			break;
227 		}
228 		if (((if_getflags(ifp) & IFF_UP)) &&
229 		    (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING))) {
230 			mtx_unlock(&sc->ic_lock);
231 			if ((error = iicbus_request_bus(parent, icdev,
232 			    IIC_WAIT | IIC_INTR)))
233 				return (error);
234 			mtx_lock(&sc->ic_lock);
235 			iicbus_reset(parent, IIC_FASTEST, 0, NULL);
236 			if_setdrvflagbits(ifp, IFF_DRV_RUNNING, 0);
237 		}
238 		mtx_unlock(&sc->ic_lock);
239 		break;
240 
241 	case SIOCSIFMTU:
242 		ic_alloc_buffers(sc, ifr->ifr_mtu);
243 		break;
244 
245 	case SIOCGIFMTU:
246 		mtx_lock(&sc->ic_lock);
247 		ifr->ifr_mtu = if_getmtu(sc->ic_ifp);
248 		mtx_unlock(&sc->ic_lock);
249 		break;
250 
251 	case SIOCADDMULTI:
252 	case SIOCDELMULTI:
253 		if (ifr == NULL)
254 			return (EAFNOSUPPORT);		/* XXX */
255 		switch (ifr->ifr_addr.sa_family) {
256 		case AF_INET:
257 			break;
258 		default:
259 			return (EAFNOSUPPORT);
260 		}
261 		break;
262 	default:
263 		return (EINVAL);
264 	}
265 	return (0);
266 }
267 
268 /*
269  * icintr()
270  */
271 static int
272 icintr(device_t dev, int event, char *ptr)
273 {
274 	struct ic_softc *sc = (struct ic_softc *)device_get_softc(dev);
275 	struct mbuf *top;
276 	int len;
277 
278 	mtx_lock(&sc->ic_lock);
279 
280 	switch (event) {
281 
282 	case INTR_GENERAL:
283 	case INTR_START:
284 		sc->ic_cp = sc->ic_ifbuf;
285 		sc->ic_xfercnt = 0;
286 		sc->ic_flags |= IC_IFBUF_BUSY;
287 		break;
288 
289 	case INTR_STOP:
290 
291 		/* if any error occurred during transfert,
292 		 * drop the packet */
293 		sc->ic_flags &= ~IC_IFBUF_BUSY;
294 		if ((sc->ic_flags & (IC_BUFFERS_BUSY | IC_BUFFER_WAITER)) ==
295 		    IC_BUFFER_WAITER)
296 			wakeup(&sc);
297 		if (sc->ic_iferrs)
298 			goto err;
299 		if ((len = sc->ic_xfercnt) == 0)
300 			break;					/* ignore */
301 		if (len <= ICHDRLEN)
302 			goto err;
303 		len -= ICHDRLEN;
304 		if_inc_counter(sc->ic_ifp, IFCOUNTER_IPACKETS, 1);
305 		if_inc_counter(sc->ic_ifp, IFCOUNTER_IBYTES, len);
306 		BPF_TAP(sc->ic_ifp, sc->ic_ifbuf, len + ICHDRLEN);
307 		top = m_devget(sc->ic_ifbuf + ICHDRLEN, len, 0, sc->ic_ifp, 0);
308 		if (top) {
309 			struct epoch_tracker et;
310 
311 			mtx_unlock(&sc->ic_lock);
312 			M_SETFIB(top, if_getfib(sc->ic_ifp));
313 			NET_EPOCH_ENTER(et);
314 			netisr_dispatch(NETISR_IP, top);
315 			NET_EPOCH_EXIT(et);
316 			mtx_lock(&sc->ic_lock);
317 		}
318 		break;
319 	err:
320 		if_printf(sc->ic_ifp, "errors (%d)!\n", sc->ic_iferrs);
321 		sc->ic_iferrs = 0;			/* reset error count */
322 		if_inc_counter(sc->ic_ifp, IFCOUNTER_IERRORS, 1);
323 		break;
324 
325 	case INTR_RECEIVE:
326 		if (sc->ic_xfercnt >= if_getmtu(sc->ic_ifp) + ICHDRLEN) {
327 			sc->ic_iferrs++;
328 		} else {
329 			*sc->ic_cp++ = *ptr;
330 			sc->ic_xfercnt++;
331 		}
332 		break;
333 
334 	case INTR_NOACK:			/* xfer terminated by master */
335 		break;
336 
337 	case INTR_TRANSMIT:
338 		*ptr = 0xff;					/* XXX */
339 	  	break;
340 
341 	case INTR_ERROR:
342 		sc->ic_iferrs++;
343 		break;
344 
345 	default:
346 		panic("%s: unknown event (%d)!", __func__, event);
347 	}
348 
349 	mtx_unlock(&sc->ic_lock);
350 	return (0);
351 }
352 
353 /*
354  * icoutput()
355  */
356 static int
357 icoutput(if_t ifp, struct mbuf *m, const struct sockaddr *dst,
358     struct route *ro)
359 {
360 	struct ic_softc *sc = if_getsoftc(ifp);
361 	device_t icdev = sc->ic_dev;
362 	device_t parent = device_get_parent(icdev);
363 	int len, sent;
364 	struct mbuf *mm;
365 	u_char *cp;
366 	u_int32_t hdr;
367 
368 	/* BPF writes need to be handled specially. */
369 	if (dst->sa_family == AF_UNSPEC)
370 		bcopy(dst->sa_data, &hdr, sizeof(hdr));
371 	else
372 		hdr = RO_GET_FAMILY(ro, dst);
373 
374 	mtx_lock(&sc->ic_lock);
375 	if_setdrvflagbits(ifp, IFF_DRV_RUNNING, 0);
376 
377 	/* already sending? */
378 	if (sc->ic_flags & IC_SENDING) {
379 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
380 		goto error;
381 	}
382 
383 	/* insert header */
384 	bcopy ((char *)&hdr, sc->ic_obuf, ICHDRLEN);
385 
386 	cp = sc->ic_obuf + ICHDRLEN;
387 	len = 0;
388 	mm = m;
389 	do {
390 		if (len + mm->m_len > if_getmtu(sc->ic_ifp)) {
391 			/* packet too large */
392 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
393 			goto error;
394 		}
395 
396 		bcopy(mtod(mm,char *), cp, mm->m_len);
397 		cp += mm->m_len;
398 		len += mm->m_len;
399 
400 	} while ((mm = mm->m_next));
401 
402 	BPF_MTAP2(ifp, &hdr, sizeof(hdr), m);
403 
404 	sc->ic_flags |= (IC_SENDING | IC_OBUF_BUSY);
405 
406 	m_freem(m);
407 	mtx_unlock(&sc->ic_lock);
408 
409 	/* send the packet */
410 	if (iicbus_block_write(parent, sc->ic_addr, sc->ic_obuf,
411 				len + ICHDRLEN, &sent))
412 
413 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
414 	else {
415 		if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
416 		if_inc_counter(ifp, IFCOUNTER_OBYTES, len);
417 	}
418 
419 	mtx_lock(&sc->ic_lock);
420 	sc->ic_flags &= ~(IC_SENDING | IC_OBUF_BUSY);
421 	if ((sc->ic_flags & (IC_BUFFERS_BUSY | IC_BUFFER_WAITER)) ==
422 	    IC_BUFFER_WAITER)
423 		wakeup(&sc);
424 	mtx_unlock(&sc->ic_lock);
425 
426 	return (0);
427 
428 error:
429 	m_freem(m);
430 	mtx_unlock(&sc->ic_lock);
431 
432 	return(0);
433 }
434 
435 DRIVER_MODULE(ic, iicbus, ic_driver, 0, 0);
436 MODULE_DEPEND(ic, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER);
437 MODULE_VERSION(ic, 1);
438