xref: /dragonfly/sys/dev/netif/ic/if_ic.c (revision 3170ffd7)
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 #include <sys/thread2.h>
47 
48 #include <net/if.h>
49 #include <net/ifq_var.h>
50 #include <net/if_types.h>
51 #include <net/netisr.h>
52 
53 #endif
54 #include <sys/mbuf.h>
55 #include <sys/socket.h>
56 #include <net/netisr.h>
57 #include <net/route.h>
58 #include <netinet/in.h>
59 #include <netinet/in_systm.h>
60 #include <netinet/in_var.h>
61 #include <netinet/ip.h>
62 #include <netinet/if_ether.h>
63 
64 #include <net/bpf.h>
65 
66 #include <bus/iicbus/iiconf.h>
67 #include <bus/iicbus/iicbus.h>
68 
69 #include "iicbus_if.h"
70 
71 #define PCF_MASTER_ADDRESS 0xaa
72 
73 #define ICHDRLEN	sizeof(uint32_t)
74 #define ICMTU		1500		/* default mtu */
75 
76 struct ic_softc {
77 	struct ifnet ic_if;
78 
79 	u_char ic_addr;			/* peer I2C address */
80 
81 	int ic_sending;
82 
83 	char *ic_obuf;
84 	char *ic_ifbuf;
85 	char *ic_cp;
86 
87 	int ic_xfercnt;
88 
89 	int ic_iferrs;
90 };
91 
92 static devclass_t ic_devclass;
93 
94 static int icprobe(device_t);
95 static int icattach(device_t);
96 
97 static int icioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
98 static int icoutput(struct ifnet *, struct mbuf *, struct sockaddr *,
99 		struct rtentry *);
100 
101 static void icintr(device_t, int, char *);
102 
103 static device_method_t ic_methods[] = {
104 	/* device interface */
105 	DEVMETHOD(device_probe,		icprobe),
106 	DEVMETHOD(device_attach,	icattach),
107 
108 	/* iicbus interface */
109 	DEVMETHOD(iicbus_intr,		icintr),
110 
111 	DEVMETHOD_END
112 };
113 
114 static driver_t ic_driver = {
115 	"ic",
116 	ic_methods,
117 	sizeof(struct ic_softc),
118 };
119 
120 /*
121  * icprobe()
122  */
123 static int
124 icprobe(device_t dev)
125 {
126 	return (0);
127 }
128 
129 /*
130  * icattach()
131  */
132 static int
133 icattach(device_t dev)
134 {
135 	struct ic_softc *sc = (struct ic_softc *)device_get_softc(dev);
136 	struct ifnet *ifp = &sc->ic_if;
137 
138 	sc->ic_addr = PCF_MASTER_ADDRESS;	/* XXX only PCF masters */
139 
140 	ifp->if_softc = sc;
141 	if_initname(ifp, "ic", device_get_unit(dev));
142 	ifp->if_mtu = ICMTU;
143 	ifp->if_flags = IFF_SIMPLEX | IFF_POINTOPOINT | IFF_MULTICAST;
144 	ifp->if_ioctl = icioctl;
145 	ifp->if_output = icoutput;
146 	ifp->if_type = IFT_PARA;
147 	ifp->if_hdrlen = 0;
148 	ifp->if_addrlen = 0;
149 	ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
150 
151 	if_attach(ifp, NULL);
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, struct ucred *cr)
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)) && (ifp->if_flags & IFF_RUNNING)) {
185 
186 	    /* XXX disable PCF */
187 	    ifp->if_flags &= ~IFF_RUNNING;
188 
189 	    /* IFF_UP is not set, try to release the bus anyway */
190 	    iicbus_release_bus(parent, icdev);
191 	    break;
192 	}
193 	if (((ifp->if_flags & IFF_UP)) && (!(ifp->if_flags & IFF_RUNNING))) {
194 
195 	    if ((error = iicbus_request_bus(parent, icdev, IIC_WAIT|IIC_INTR)))
196 		return (error);
197 
198 	    sc->ic_obuf = kmalloc(sc->ic_if.if_mtu + ICHDRLEN,
199 				  M_DEVBUF, M_WAITOK);
200 
201 	    sc->ic_ifbuf = kmalloc(sc->ic_if.if_mtu + ICHDRLEN,
202 				  M_DEVBUF, M_WAITOK);
203 
204 	    iicbus_reset(parent, IIC_FASTEST, 0, NULL);
205 
206 	    ifp->if_flags |= IFF_RUNNING;
207 	}
208 	break;
209 
210     case SIOCSIFMTU:
211 	/* save previous buffers */
212 	iptr = sc->ic_ifbuf;
213 	optr = sc->ic_obuf;
214 
215 	/* allocate input buffer */
216 	sc->ic_ifbuf = kmalloc(ifr->ifr_mtu+ICHDRLEN, M_DEVBUF, M_WAITOK);
217 
218 	/* allocate output buffer */
219 	sc->ic_obuf = kmalloc(ifr->ifr_mtu+ICHDRLEN, M_DEVBUF, M_WAITOK);
220 
221 	if (iptr)
222 	    kfree(iptr,M_DEVBUF);
223 
224 	if (optr)
225 	    kfree(optr,M_DEVBUF);
226 
227 	sc->ic_if.if_mtu = ifr->ifr_mtu;
228 	break;
229 
230     case SIOCGIFMTU:
231 	ifr->ifr_mtu = sc->ic_if.if_mtu;
232 	break;
233 
234     case SIOCADDMULTI:
235     case SIOCDELMULTI:
236 	if (ifr == NULL) {
237 	    return EAFNOSUPPORT;		/* XXX */
238 	}
239 	switch (ifr->ifr_addr.sa_family) {
240 
241 	case AF_INET:
242 	    break;
243 
244 	default:
245 	    return EAFNOSUPPORT;
246 	}
247 	break;
248 
249     default:
250 	return EINVAL;
251     }
252     return 0;
253 }
254 
255 /*
256  * icintr()
257  */
258 static void
259 icintr (device_t dev, int event, char *ptr)
260 {
261 	struct ic_softc *sc = (struct ic_softc *)device_get_softc(dev);
262 	int unit = device_get_unit(dev);
263 	int len;
264 	struct mbuf *top;
265 
266 	crit_enter();
267 
268 	switch (event) {
269 
270 	case INTR_GENERAL:
271 	case INTR_START:
272 		sc->ic_cp = sc->ic_ifbuf;
273 		sc->ic_xfercnt = 0;
274 		break;
275 
276 	case INTR_STOP:
277 
278 	  /* if any error occured during transfert,
279 	   * drop the packet */
280 	  if (sc->ic_iferrs)
281 	    goto err;
282 
283 	  if ((len = sc->ic_xfercnt) == 0)
284 		break;					/* ignore */
285 
286 	  if (len <= ICHDRLEN)
287 	    goto err;
288 
289 	  len -= ICHDRLEN;
290 	  IFNET_STAT_INC(&sc->ic_if, ipackets, 1);
291 	  IFNET_STAT_INC(&sc->ic_if, ibytes, len);
292 
293 	  BPF_TAP(&sc->ic_if, sc->ic_ifbuf, len + ICHDRLEN);
294 
295 	  top = m_devget(sc->ic_ifbuf + ICHDRLEN, len, 0, &sc->ic_if, 0);
296 
297 	  if (top)
298 	    netisr_queue(NETISR_IP, top);
299 	  break;
300 
301 	err:
302 	  kprintf("ic%d: errors (%d)!\n", unit, sc->ic_iferrs);
303 
304 	  sc->ic_iferrs = 0;			/* reset error count */
305 	  IFNET_STAT_INC(&sc->ic_if, ierrors, 1);
306 
307 	  break;
308 
309 	case INTR_RECEIVE:
310 		if (sc->ic_xfercnt >= sc->ic_if.if_mtu+ICHDRLEN) {
311 			sc->ic_iferrs ++;
312 
313 		} else {
314 			*sc->ic_cp++ = *ptr;
315 			sc->ic_xfercnt ++;
316 		}
317 		break;
318 
319 	case INTR_NOACK:			/* xfer terminated by master */
320 		break;
321 
322 	case INTR_TRANSMIT:
323 		*ptr = 0xff;					/* XXX */
324 	  	break;
325 
326 	case INTR_ERROR:
327 		sc->ic_iferrs ++;
328 		break;
329 
330 	default:
331 		panic("%s: unknown event (%d)!", __func__, event);
332 	}
333 
334 	crit_exit();
335 }
336 
337 /*
338  * icoutput()
339  */
340 static int
341 icoutput(struct ifnet *ifp, struct mbuf *m,
342 	struct sockaddr *dst, struct rtentry *rt)
343 {
344 	device_t icdev = devclass_get_device(ic_devclass, ifp->if_dunit);
345 	device_t parent = device_get_parent(icdev);
346 	struct ic_softc *sc = (struct ic_softc *)device_get_softc(icdev);
347 	int len, sent;
348 	struct mbuf *mm;
349 	u_char *cp;
350 	uint32_t hdr = dst->sa_family;
351 
352 	ifp->if_flags |= IFF_RUNNING;
353 
354 	crit_enter();
355 
356 	/* already sending? */
357 	if (sc->ic_sending) {
358 		IFNET_STAT_INC(ifp, oerrors, 1);
359 		goto error;
360 	}
361 
362 	/* insert header */
363 	bcopy ((char *)&hdr, sc->ic_obuf, ICHDRLEN);
364 
365 	cp = sc->ic_obuf + ICHDRLEN;
366 	len = 0;
367 	mm = m;
368 	do {
369 		if (len + mm->m_len > sc->ic_if.if_mtu) {
370 			/* packet to large */
371 			IFNET_STAT_INC(ifp, oerrors, 1);
372 			goto error;
373 		}
374 
375 		bcopy(mtod(mm,char *), cp, mm->m_len);
376 		cp += mm->m_len;
377 		len += mm->m_len;
378 
379 	} while ((mm = mm->m_next));
380 
381 	if (ifp->if_bpf) {
382 		bpf_gettoken();
383 		if (ifp->if_bpf)
384 			bpf_ptap(ifp->if_bpf, m, &hdr, ICHDRLEN);
385 		bpf_reltoken();
386 	}
387 
388 	sc->ic_sending = 1;
389 
390 	m_freem(m);
391 
392 	crit_exit();
393 
394 	/* send the packet */
395 	if (iicbus_block_write(parent, sc->ic_addr, sc->ic_obuf,
396 				len + ICHDRLEN, &sent))
397 
398 		IFNET_STAT_INC(ifp, oerrors, 1);
399 	else {
400 		IFNET_STAT_INC(ifp, opackets, 1);
401 		IFNET_STAT_INC(ifp, obytes, len);
402 	}
403 
404 	sc->ic_sending = 0;
405 
406 	return (0);
407 
408 error:
409 	m_freem(m);
410 	crit_exit();
411 
412 	return(0);
413 }
414 
415 DRIVER_MODULE(if_ic, iicbus, ic_driver, ic_devclass, NULL, NULL);
416 MODULE_DEPEND(if_ic, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER);
417 MODULE_VERSION(if_ic, 1);
418