xref: /freebsd/sys/dev/iicbus/if_ic.c (revision 2e3f49888ec8851bafb22011533217487764fdb0)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 /*
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/lock.h>
42 #include <sys/module.h>
43 #include <sys/mutex.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_var.h>
50 #include <net/if_types.h>
51 #include <net/netisr.h>
52 
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 	if_t ic_ifp;
74 	device_t ic_dev;
75 
76 	u_char ic_addr;			/* peer I2C address */
77 
78 	int ic_flags;
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 	struct mtx ic_lock;
89 };
90 
91 #define	IC_SENDING		0x0001
92 #define	IC_OBUF_BUSY		0x0002
93 #define	IC_IFBUF_BUSY		0x0004
94 #define	IC_BUFFERS_BUSY		(IC_OBUF_BUSY | IC_IFBUF_BUSY)
95 #define	IC_BUFFER_WAITER	0x0004
96 
97 static int icprobe(device_t);
98 static int icattach(device_t);
99 
100 static int icioctl(if_t, u_long, caddr_t);
101 static int icoutput(if_t, struct mbuf *, const struct sockaddr *,
102                struct route *);
103 
104 static int icintr(device_t, int, char *);
105 
106 static device_method_t ic_methods[] = {
107 	/* device interface */
108 	DEVMETHOD(device_probe,		icprobe),
109 	DEVMETHOD(device_attach,	icattach),
110 
111 	/* iicbus interface */
112 	DEVMETHOD(iicbus_intr,		icintr),
113 
114 	{ 0, 0 }
115 };
116 
117 static driver_t ic_driver = {
118 	"ic",
119 	ic_methods,
120 	sizeof(struct ic_softc),
121 };
122 
123 static void
124 ic_alloc_buffers(struct ic_softc *sc, int mtu)
125 {
126 	char *obuf, *ifbuf;
127 
128 	obuf = malloc(mtu + ICHDRLEN, M_DEVBUF, M_WAITOK);
129 	ifbuf = malloc(mtu + ICHDRLEN, M_DEVBUF, M_WAITOK);
130 
131 	mtx_lock(&sc->ic_lock);
132 	while (sc->ic_flags & IC_BUFFERS_BUSY) {
133 		sc->ic_flags |= IC_BUFFER_WAITER;
134 		mtx_sleep(sc, &sc->ic_lock, 0, "icalloc", 0);
135 		sc->ic_flags &= ~IC_BUFFER_WAITER;
136 	}
137 
138 	free(sc->ic_obuf, M_DEVBUF);
139 	free(sc->ic_ifbuf, M_DEVBUF);
140 	sc->ic_obuf = obuf;
141 	sc->ic_ifbuf = ifbuf;
142 	if_setmtu(sc->ic_ifp, mtu);
143 	mtx_unlock(&sc->ic_lock);
144 }
145 
146 /*
147  * icprobe()
148  */
149 static int
150 icprobe(device_t dev)
151 {
152 	return (BUS_PROBE_NOWILDCARD);
153 }
154 
155 /*
156  * icattach()
157  */
158 static int
159 icattach(device_t dev)
160 {
161 	struct ic_softc *sc = (struct ic_softc *)device_get_softc(dev);
162 	if_t ifp;
163 
164 	ifp = sc->ic_ifp = if_alloc(IFT_PARA);
165 	if (ifp == NULL)
166 		return (ENOSPC);
167 
168 	mtx_init(&sc->ic_lock, device_get_nameunit(dev), MTX_NETWORK_LOCK,
169 	    MTX_DEF);
170 	sc->ic_addr = PCF_MASTER_ADDRESS;	/* XXX only PCF masters */
171 	sc->ic_dev = dev;
172 
173 	if_setsoftc(ifp, sc);
174 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
175 	if_setflags(ifp, IFF_SIMPLEX | IFF_POINTOPOINT | IFF_MULTICAST);
176 	if_setioctlfn(ifp, icioctl);
177 	if_setoutputfn(ifp, icoutput);
178 	if_setifheaderlen(ifp, 0);
179 	if_setsendqlen(ifp, ifqmaxlen);
180 
181 	ic_alloc_buffers(sc, ICMTU);
182 
183 	if_attach(ifp);
184 
185 	bpfattach(ifp, DLT_NULL, ICHDRLEN);
186 
187 	return (0);
188 }
189 
190 /*
191  * iciotcl()
192  */
193 static int
194 icioctl(if_t ifp, u_long cmd, caddr_t data)
195 {
196 	struct ic_softc *sc = if_getsoftc(ifp);
197 	device_t icdev = sc->ic_dev;
198 	device_t parent = device_get_parent(icdev);
199 	struct ifaddr *ifa = (struct ifaddr *)data;
200 	struct ifreq *ifr = (struct ifreq *)data;
201 	int error;
202 
203 	switch (cmd) {
204 
205 	case SIOCAIFADDR:
206 	case SIOCSIFADDR:
207 		if (ifa->ifa_addr->sa_family != AF_INET)
208 			return (EAFNOSUPPORT);
209 		mtx_lock(&sc->ic_lock);
210 		if_setflagbits(ifp, IFF_UP, 0);
211 		goto locked;
212 	case SIOCSIFFLAGS:
213 		mtx_lock(&sc->ic_lock);
214 	locked:
215 		if ((!(if_getflags(ifp) & IFF_UP)) &&
216 		    (if_getdrvflags(ifp) & IFF_DRV_RUNNING)) {
217 
218 			/* XXX disable PCF */
219 			if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
220 			mtx_unlock(&sc->ic_lock);
221 
222 			/* IFF_UP is not set, try to release the bus anyway */
223 			iicbus_release_bus(parent, icdev);
224 			break;
225 		}
226 		if (((if_getflags(ifp) & IFF_UP)) &&
227 		    (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING))) {
228 			mtx_unlock(&sc->ic_lock);
229 			if ((error = iicbus_request_bus(parent, icdev,
230 			    IIC_WAIT | IIC_INTR)))
231 				return (error);
232 			mtx_lock(&sc->ic_lock);
233 			iicbus_reset(parent, IIC_FASTEST, 0, NULL);
234 			if_setdrvflagbits(ifp, IFF_DRV_RUNNING, 0);
235 		}
236 		mtx_unlock(&sc->ic_lock);
237 		break;
238 
239 	case SIOCSIFMTU:
240 		ic_alloc_buffers(sc, ifr->ifr_mtu);
241 		break;
242 
243 	case SIOCGIFMTU:
244 		mtx_lock(&sc->ic_lock);
245 		ifr->ifr_mtu = if_getmtu(sc->ic_ifp);
246 		mtx_unlock(&sc->ic_lock);
247 		break;
248 
249 	case SIOCADDMULTI:
250 	case SIOCDELMULTI:
251 		if (ifr == NULL)
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 int
270 icintr(device_t dev, int event, char *ptr)
271 {
272 	struct ic_softc *sc = (struct ic_softc *)device_get_softc(dev);
273 	struct mbuf *top;
274 	int len;
275 
276 	mtx_lock(&sc->ic_lock);
277 
278 	switch (event) {
279 
280 	case INTR_GENERAL:
281 	case INTR_START:
282 		sc->ic_cp = sc->ic_ifbuf;
283 		sc->ic_xfercnt = 0;
284 		sc->ic_flags |= IC_IFBUF_BUSY;
285 		break;
286 
287 	case INTR_STOP:
288 
289 		/* if any error occurred during transfert,
290 		 * drop the packet */
291 		sc->ic_flags &= ~IC_IFBUF_BUSY;
292 		if ((sc->ic_flags & (IC_BUFFERS_BUSY | IC_BUFFER_WAITER)) ==
293 		    IC_BUFFER_WAITER)
294 			wakeup(&sc);
295 		if (sc->ic_iferrs)
296 			goto err;
297 		if ((len = sc->ic_xfercnt) == 0)
298 			break;					/* ignore */
299 		if (len <= ICHDRLEN)
300 			goto err;
301 		len -= ICHDRLEN;
302 		if_inc_counter(sc->ic_ifp, IFCOUNTER_IPACKETS, 1);
303 		if_inc_counter(sc->ic_ifp, IFCOUNTER_IBYTES, len);
304 		BPF_TAP(sc->ic_ifp, sc->ic_ifbuf, len + ICHDRLEN);
305 		top = m_devget(sc->ic_ifbuf + ICHDRLEN, len, 0, sc->ic_ifp, 0);
306 		if (top) {
307 			struct epoch_tracker et;
308 
309 			mtx_unlock(&sc->ic_lock);
310 			M_SETFIB(top, if_getfib(sc->ic_ifp));
311 			NET_EPOCH_ENTER(et);
312 			netisr_dispatch(NETISR_IP, top);
313 			NET_EPOCH_EXIT(et);
314 			mtx_lock(&sc->ic_lock);
315 		}
316 		break;
317 	err:
318 		if_printf(sc->ic_ifp, "errors (%d)!\n", sc->ic_iferrs);
319 		sc->ic_iferrs = 0;			/* reset error count */
320 		if_inc_counter(sc->ic_ifp, IFCOUNTER_IERRORS, 1);
321 		break;
322 
323 	case INTR_RECEIVE:
324 		if (sc->ic_xfercnt >= if_getmtu(sc->ic_ifp) + ICHDRLEN) {
325 			sc->ic_iferrs++;
326 		} else {
327 			*sc->ic_cp++ = *ptr;
328 			sc->ic_xfercnt++;
329 		}
330 		break;
331 
332 	case INTR_NOACK:			/* xfer terminated by master */
333 		break;
334 
335 	case INTR_TRANSMIT:
336 		*ptr = 0xff;					/* XXX */
337 	  	break;
338 
339 	case INTR_ERROR:
340 		sc->ic_iferrs++;
341 		break;
342 
343 	default:
344 		panic("%s: unknown event (%d)!", __func__, event);
345 	}
346 
347 	mtx_unlock(&sc->ic_lock);
348 	return (0);
349 }
350 
351 /*
352  * icoutput()
353  */
354 static int
355 icoutput(if_t ifp, struct mbuf *m, const struct sockaddr *dst,
356     struct route *ro)
357 {
358 	struct ic_softc *sc = if_getsoftc(ifp);
359 	device_t icdev = sc->ic_dev;
360 	device_t parent = device_get_parent(icdev);
361 	int len, sent;
362 	struct mbuf *mm;
363 	u_char *cp;
364 	u_int32_t hdr;
365 
366 	/* BPF writes need to be handled specially. */
367 	if (dst->sa_family == AF_UNSPEC)
368 		bcopy(dst->sa_data, &hdr, sizeof(hdr));
369 	else
370 		hdr = RO_GET_FAMILY(ro, dst);
371 
372 	mtx_lock(&sc->ic_lock);
373 	if_setdrvflagbits(ifp, IFF_DRV_RUNNING, 0);
374 
375 	/* already sending? */
376 	if (sc->ic_flags & IC_SENDING) {
377 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
378 		goto error;
379 	}
380 
381 	/* insert header */
382 	bcopy ((char *)&hdr, sc->ic_obuf, ICHDRLEN);
383 
384 	cp = sc->ic_obuf + ICHDRLEN;
385 	len = 0;
386 	mm = m;
387 	do {
388 		if (len + mm->m_len > if_getmtu(sc->ic_ifp)) {
389 			/* packet too large */
390 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
391 			goto error;
392 		}
393 
394 		bcopy(mtod(mm,char *), cp, mm->m_len);
395 		cp += mm->m_len;
396 		len += mm->m_len;
397 
398 	} while ((mm = mm->m_next));
399 
400 	BPF_MTAP2(ifp, &hdr, sizeof(hdr), m);
401 
402 	sc->ic_flags |= (IC_SENDING | IC_OBUF_BUSY);
403 
404 	m_freem(m);
405 	mtx_unlock(&sc->ic_lock);
406 
407 	/* send the packet */
408 	if (iicbus_block_write(parent, sc->ic_addr, sc->ic_obuf,
409 				len + ICHDRLEN, &sent))
410 
411 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
412 	else {
413 		if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
414 		if_inc_counter(ifp, IFCOUNTER_OBYTES, len);
415 	}
416 
417 	mtx_lock(&sc->ic_lock);
418 	sc->ic_flags &= ~(IC_SENDING | IC_OBUF_BUSY);
419 	if ((sc->ic_flags & (IC_BUFFERS_BUSY | IC_BUFFER_WAITER)) ==
420 	    IC_BUFFER_WAITER)
421 		wakeup(&sc);
422 	mtx_unlock(&sc->ic_lock);
423 
424 	return (0);
425 
426 error:
427 	m_freem(m);
428 	mtx_unlock(&sc->ic_lock);
429 
430 	return(0);
431 }
432 
433 DRIVER_MODULE(ic, iicbus, ic_driver, 0, 0);
434 MODULE_DEPEND(ic, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER);
435 MODULE_VERSION(ic, 1);
436