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 * $FreeBSD: releng/5.0/sys/dev/iicbus/if_ic.c 106937 2002-11-14 23:54:55Z sam $
27 */
28
29 /*
30 * I2C bus IP driver
31 */
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/mbuf.h>
36 #include <sys/socket.h>
37 #include <sys/filio.h>
38 #include <sys/sockio.h>
39 #include <sys/kernel.h>
40 #include <sys/module.h>
41 #include <sys/bus.h>
42 #include <sys/time.h>
43 #include <sys/malloc.h>
44
45 #include <net/if.h>
46 #include <net/if_types.h>
47 #include <net/netisr.h>
48
49 #include <sys/mbuf.h>
50 #include <sys/socket.h>
51 #include <net/netisr.h>
52 #include <net/route.h>
53 #include <netinet/in.h>
54 #include <netinet/in_systm.h>
55 #include <netinet/in_var.h>
56 #include <netinet/ip.h>
57 #include <netinet/if_ether.h>
58
59 #include <net/bpf.h>
60
61 #include <dev/iicbus/iiconf.h>
62 #include <dev/iicbus/iicbus.h>
63
64 #include "iicbus_if.h"
65
66 #define PCF_MASTER_ADDRESS 0xaa
67
68 #define ICHDRLEN sizeof(u_int)
69 #define ICMTU 1500 /* default mtu */
70
71 struct ic_softc {
72 struct ifnet ic_if;
73
74 u_char ic_addr; /* peer I2C address */
75
76 int ic_sending;
77
78 char *ic_obuf;
79 char *ic_ifbuf;
80 char *ic_cp;
81
82 int ic_xfercnt;
83
84 int ic_iferrs;
85 };
86
87 static devclass_t ic_devclass;
88
89 static int icprobe(device_t);
90 static int icattach(device_t);
91
92 static int icioctl(struct ifnet *, u_long, caddr_t);
93 static int icoutput(struct ifnet *, struct mbuf *, struct sockaddr *,
94 struct rtentry *);
95
96 static void icintr(device_t, int, char *);
97
98 static device_method_t ic_methods[] = {
99 /* device interface */
100 DEVMETHOD(device_probe, icprobe),
101 DEVMETHOD(device_attach, icattach),
102
103 /* iicbus interface */
104 DEVMETHOD(iicbus_intr, icintr),
105
106 { 0, 0 }
107 };
108
109 static driver_t ic_driver = {
110 "ic",
111 ic_methods,
112 sizeof(struct ic_softc),
113 };
114
115 /*
116 * icprobe()
117 */
118 static int
119 icprobe(device_t dev)
120 {
121 return (0);
122 }
123
124 /*
125 * icattach()
126 */
127 static int
128 icattach(device_t dev)
129 {
130 struct ic_softc *sc = (struct ic_softc *)device_get_softc(dev);
131 struct ifnet *ifp = &sc->ic_if;
132
133 sc->ic_addr = PCF_MASTER_ADDRESS; /* XXX only PCF masters */
134
135 ifp->if_softc = sc;
136 ifp->if_name = "ic";
137 ifp->if_unit = 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_unit);
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 if (IF_HANDOFF(&ipintrq, top, NULL))
319 schednetisr(NETISR_IP);
320 }
321 break;
322
323 err:
324 printf("ic%d: errors (%d)!\n", unit, sc->ic_iferrs);
325
326 sc->ic_iferrs = 0; /* reset error count */
327 sc->ic_if.if_ierrors ++;
328
329 break;
330
331 case INTR_RECEIVE:
332 if (sc->ic_xfercnt >= sc->ic_if.if_mtu+ICHDRLEN) {
333 sc->ic_iferrs ++;
334
335 } else {
336 *sc->ic_cp++ = *ptr;
337 sc->ic_xfercnt ++;
338 }
339 break;
340
341 case INTR_NOACK: /* xfer terminated by master */
342 break;
343
344 case INTR_TRANSMIT:
345 *ptr = 0xff; /* XXX */
346 break;
347
348 case INTR_ERROR:
349 sc->ic_iferrs ++;
350 break;
351
352 default:
353 panic("%s: unknown event (%d)!", __func__, event);
354 }
355
356 splx(s);
357 return;
358 }
359
360 /*
361 * icoutput()
362 */
363 static int
364 icoutput(struct ifnet *ifp, struct mbuf *m,
365 struct sockaddr *dst, struct rtentry *rt)
366 {
367 device_t icdev = devclass_get_device(ic_devclass, ifp->if_unit);
368 device_t parent = device_get_parent(icdev);
369 struct ic_softc *sc = (struct ic_softc *)device_get_softc(icdev);
370
371 int s, len, sent;
372 struct mbuf *mm;
373 u_char *cp;
374 u_int hdr = dst->sa_family;
375
376 ifp->if_flags |= IFF_RUNNING;
377
378 s = splhigh();
379
380 /* already sending? */
381 if (sc->ic_sending) {
382 ifp->if_oerrors ++;
383 goto error;
384 }
385
386 /* insert header */
387 bcopy ((char *)&hdr, sc->ic_obuf, ICHDRLEN);
388
389 cp = sc->ic_obuf + ICHDRLEN;
390 len = 0;
391 mm = m;
392 do {
393 if (len + mm->m_len > sc->ic_if.if_mtu) {
394 /* packet to large */
395 ifp->if_oerrors ++;
396 goto error;
397 }
398
399 bcopy(mtod(mm,char *), cp, mm->m_len);
400 cp += mm->m_len;
401 len += mm->m_len;
402
403 } while ((mm = mm->m_next));
404
405 if (ifp->if_bpf) {
406 struct mbuf m0, *n = m;
407
408 /*
409 * We need to prepend the address family as
410 * a four byte field. Cons up a dummy header
411 * to pacify bpf. This is safe because bpf
412 * will only read from the mbuf (i.e., it won't
413 * try to free it or keep a pointer a to it).
414 */
415 m0.m_next = m;
416 m0.m_len = sizeof(u_int);
417 m0.m_data = (char *)&hdr;
418 n = &m0;
419
420 BPF_MTAP(ifp, n);
421 }
422
423 sc->ic_sending = 1;
424
425 m_freem(m);
426 splx(s);
427
428 /* send the packet */
429 if (iicbus_block_write(parent, sc->ic_addr, sc->ic_obuf,
430 len + ICHDRLEN, &sent))
431
432 ifp->if_oerrors ++;
433 else {
434 ifp->if_opackets ++;
435 ifp->if_obytes += len;
436 }
437
438 sc->ic_sending = 0;
439
440 return (0);
441
442 error:
443 m_freem(m);
444 splx(s);
445
446 return(0);
447 }
448
449 DRIVER_MODULE(ic, iicbus, ic_driver, ic_devclass, 0, 0);
450 MODULE_DEPEND(ic, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER);
451 MODULE_VERSION(ic, 1);
Cache object: 8d5895d462abeb88094396a8ca81aad7
|