FreeBSD/Linux Kernel Cross Reference
sys/dev/iicbus/if_ic.c
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: releng/5.2/sys/dev/iicbus/if_ic.c 121816 2003-10-31 18:32:15Z brooks $");
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_int)
70 #define ICMTU 1500 /* default mtu */
71
72 struct ic_softc {
73 struct ifnet ic_if;
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 = &sc->ic_if;
133
134 sc->ic_addr = PCF_MASTER_ADDRESS; /* XXX only PCF masters */
135
136 ifp->if_softc = sc;
137 if_initname(ifp, device_get_name(dev), 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_dunit);
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 BPF_TAP(&sc->ic_if, sc->ic_ifbuf, len + ICHDRLEN);
314
315 top = m_devget(sc->ic_ifbuf + ICHDRLEN, len, 0, &sc->ic_if, 0);
316
317 if (top)
318 netisr_dispatch(NETISR_IP, top);
319 break;
320
321 err:
322 printf("ic%d: errors (%d)!\n", unit, sc->ic_iferrs);
323
324 sc->ic_iferrs = 0; /* reset error count */
325 sc->ic_if.if_ierrors ++;
326
327 break;
328
329 case INTR_RECEIVE:
330 if (sc->ic_xfercnt >= sc->ic_if.if_mtu+ICHDRLEN) {
331 sc->ic_iferrs ++;
332
333 } else {
334 *sc->ic_cp++ = *ptr;
335 sc->ic_xfercnt ++;
336 }
337 break;
338
339 case INTR_NOACK: /* xfer terminated by master */
340 break;
341
342 case INTR_TRANSMIT:
343 *ptr = 0xff; /* XXX */
344 break;
345
346 case INTR_ERROR:
347 sc->ic_iferrs ++;
348 break;
349
350 default:
351 panic("%s: unknown event (%d)!", __func__, event);
352 }
353
354 splx(s);
355 return;
356 }
357
358 /*
359 * icoutput()
360 */
361 static int
362 icoutput(struct ifnet *ifp, struct mbuf *m,
363 struct sockaddr *dst, struct rtentry *rt)
364 {
365 device_t icdev = devclass_get_device(ic_devclass, ifp->if_dunit);
366 device_t parent = device_get_parent(icdev);
367 struct ic_softc *sc = (struct ic_softc *)device_get_softc(icdev);
368
369 int s, len, sent;
370 struct mbuf *mm;
371 u_char *cp;
372 u_int hdr = dst->sa_family;
373
374 ifp->if_flags |= IFF_RUNNING;
375
376 s = splhigh();
377
378 /* already sending? */
379 if (sc->ic_sending) {
380 ifp->if_oerrors ++;
381 goto error;
382 }
383
384 /* insert header */
385 bcopy ((char *)&hdr, sc->ic_obuf, ICHDRLEN);
386
387 cp = sc->ic_obuf + ICHDRLEN;
388 len = 0;
389 mm = m;
390 do {
391 if (len + mm->m_len > sc->ic_if.if_mtu) {
392 /* packet to large */
393 ifp->if_oerrors ++;
394 goto error;
395 }
396
397 bcopy(mtod(mm,char *), cp, mm->m_len);
398 cp += mm->m_len;
399 len += mm->m_len;
400
401 } while ((mm = mm->m_next));
402
403 if (ifp->if_bpf) {
404 struct mbuf m0, *n = m;
405
406 /*
407 * We need to prepend the address family as
408 * a four byte field. Cons up a dummy header
409 * to pacify bpf. This is safe because bpf
410 * will only read from the mbuf (i.e., it won't
411 * try to free it or keep a pointer a to it).
412 */
413 m0.m_next = m;
414 m0.m_len = sizeof(u_int);
415 m0.m_data = (char *)&hdr;
416 n = &m0;
417
418 BPF_MTAP(ifp, n);
419 }
420
421 sc->ic_sending = 1;
422
423 m_freem(m);
424 splx(s);
425
426 /* send the packet */
427 if (iicbus_block_write(parent, sc->ic_addr, sc->ic_obuf,
428 len + ICHDRLEN, &sent))
429
430 ifp->if_oerrors ++;
431 else {
432 ifp->if_opackets ++;
433 ifp->if_obytes += len;
434 }
435
436 sc->ic_sending = 0;
437
438 return (0);
439
440 error:
441 m_freem(m);
442 splx(s);
443
444 return(0);
445 }
446
447 DRIVER_MODULE(ic, iicbus, ic_driver, ic_devclass, 0, 0);
448 MODULE_DEPEND(ic, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER);
449 MODULE_VERSION(ic, 1);
Cache object: ad08bae647a25a3e681c36c018d93962
|