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 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/proc.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 #include <sys/thread2.h>
46
47 #include <net/if.h>
48 #include <net/ifq_var.h>
49 #include <net/if_types.h>
50 #include <net/netisr.h>
51
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 <bus/iicbus/iiconf.h>
62 #include <bus/iicbus/iicbus.h>
63
64 #include "iicbus_if.h"
65
66 #define PCF_MASTER_ADDRESS 0xaa
67
68 #define ICHDRLEN sizeof(uint32_t)
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, struct ucred *);
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 DEVMETHOD_END
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
icprobe(device_t dev)119 icprobe(device_t dev)
120 {
121 return (0);
122 }
123
124 /*
125 * icattach()
126 */
127 static int
icattach(device_t dev)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 if_initname(ifp, "ic", device_get_unit(dev));
137 ifp->if_mtu = ICMTU;
138 ifp->if_flags = IFF_SIMPLEX | IFF_POINTOPOINT | IFF_MULTICAST;
139 ifp->if_ioctl = icioctl;
140 ifp->if_output = icoutput;
141 ifp->if_type = IFT_PARA;
142 ifp->if_hdrlen = 0;
143 ifp->if_addrlen = 0;
144 ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
145
146 if_attach(ifp, NULL);
147
148 bpfattach(ifp, DLT_NULL, ICHDRLEN);
149
150 return (0);
151 }
152
153 /*
154 * iciotcl()
155 */
156 static int
icioctl(struct ifnet * ifp,u_long cmd,caddr_t data,struct ucred * cr)157 icioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
158 {
159 device_t icdev = devclass_get_device(ic_devclass, ifp->if_dunit);
160 device_t parent = device_get_parent(icdev);
161 struct ic_softc *sc = (struct ic_softc *)device_get_softc(icdev);
162
163 struct ifaddr *ifa = (struct ifaddr *)data;
164 struct ifreq *ifr = (struct ifreq *)data;
165
166 u_char *iptr, *optr;
167 int error;
168
169 switch (cmd) {
170
171 case SIOCSIFDSTADDR:
172 case SIOCAIFADDR:
173 case SIOCSIFADDR:
174 if (ifa->ifa_addr->sa_family != AF_INET)
175 return EAFNOSUPPORT;
176 ifp->if_flags |= IFF_UP;
177 /* FALLTHROUGH */
178 case SIOCSIFFLAGS:
179 if ((!(ifp->if_flags & IFF_UP)) && (ifp->if_flags & IFF_RUNNING)) {
180
181 /* XXX disable PCF */
182 ifp->if_flags &= ~IFF_RUNNING;
183
184 /* IFF_UP is not set, try to release the bus anyway */
185 iicbus_release_bus(parent, icdev);
186 break;
187 }
188 if (((ifp->if_flags & IFF_UP)) && (!(ifp->if_flags & IFF_RUNNING))) {
189
190 if ((error = iicbus_request_bus(parent, icdev, IIC_WAIT|IIC_INTR)))
191 return (error);
192
193 sc->ic_obuf = kmalloc(sc->ic_if.if_mtu + ICHDRLEN,
194 M_DEVBUF, M_WAITOK);
195
196 sc->ic_ifbuf = kmalloc(sc->ic_if.if_mtu + ICHDRLEN,
197 M_DEVBUF, M_WAITOK);
198
199 iicbus_reset(parent, IIC_FASTEST, 0, NULL);
200
201 ifp->if_flags |= IFF_RUNNING;
202 }
203 break;
204
205 case SIOCSIFMTU:
206 /* save previous buffers */
207 iptr = sc->ic_ifbuf;
208 optr = sc->ic_obuf;
209
210 /* allocate input buffer */
211 sc->ic_ifbuf = kmalloc(ifr->ifr_mtu+ICHDRLEN, M_DEVBUF, M_WAITOK);
212
213 /* allocate output buffer */
214 sc->ic_obuf = kmalloc(ifr->ifr_mtu+ICHDRLEN, M_DEVBUF, M_WAITOK);
215
216 if (iptr)
217 kfree(iptr,M_DEVBUF);
218
219 if (optr)
220 kfree(optr,M_DEVBUF);
221
222 sc->ic_if.if_mtu = ifr->ifr_mtu;
223 break;
224
225 case SIOCGIFMTU:
226 ifr->ifr_mtu = sc->ic_if.if_mtu;
227 break;
228
229 case SIOCADDMULTI:
230 case SIOCDELMULTI:
231 if (ifr == NULL) {
232 return EAFNOSUPPORT; /* XXX */
233 }
234 switch (ifr->ifr_addr.sa_family) {
235
236 case AF_INET:
237 break;
238
239 default:
240 return EAFNOSUPPORT;
241 }
242 break;
243
244 default:
245 return EINVAL;
246 }
247 return 0;
248 }
249
250 /*
251 * icintr()
252 */
253 static void
icintr(device_t dev,int event,char * ptr)254 icintr (device_t dev, int event, char *ptr)
255 {
256 struct ic_softc *sc = (struct ic_softc *)device_get_softc(dev);
257 int unit = device_get_unit(dev);
258 int len;
259 struct mbuf *top;
260
261 crit_enter();
262
263 switch (event) {
264
265 case INTR_GENERAL:
266 case INTR_START:
267 sc->ic_cp = sc->ic_ifbuf;
268 sc->ic_xfercnt = 0;
269 break;
270
271 case INTR_STOP:
272
273 /* if any error occured during transfert,
274 * drop the packet */
275 if (sc->ic_iferrs)
276 goto err;
277
278 if ((len = sc->ic_xfercnt) == 0)
279 break; /* ignore */
280
281 if (len <= ICHDRLEN)
282 goto err;
283
284 len -= ICHDRLEN;
285 IFNET_STAT_INC(&sc->ic_if, ipackets, 1);
286 IFNET_STAT_INC(&sc->ic_if, ibytes, len);
287
288 BPF_TAP(&sc->ic_if, sc->ic_ifbuf, len + ICHDRLEN);
289
290 top = m_devget(sc->ic_ifbuf + ICHDRLEN, len, 0, &sc->ic_if);
291
292 if (top)
293 netisr_queue(NETISR_IP, top);
294 break;
295
296 err:
297 kprintf("ic%d: errors (%d)!\n", unit, sc->ic_iferrs);
298
299 sc->ic_iferrs = 0; /* reset error count */
300 IFNET_STAT_INC(&sc->ic_if, ierrors, 1);
301
302 break;
303
304 case INTR_RECEIVE:
305 if (sc->ic_xfercnt >= sc->ic_if.if_mtu+ICHDRLEN) {
306 sc->ic_iferrs ++;
307
308 } else {
309 *sc->ic_cp++ = *ptr;
310 sc->ic_xfercnt ++;
311 }
312 break;
313
314 case INTR_NOACK: /* xfer terminated by master */
315 break;
316
317 case INTR_TRANSMIT:
318 *ptr = 0xff; /* XXX */
319 break;
320
321 case INTR_ERROR:
322 sc->ic_iferrs ++;
323 break;
324
325 default:
326 panic("%s: unknown event (%d)!", __func__, event);
327 }
328
329 crit_exit();
330 }
331
332 /*
333 * icoutput()
334 */
335 static int
icoutput(struct ifnet * ifp,struct mbuf * m,struct sockaddr * dst,struct rtentry * rt)336 icoutput(struct ifnet *ifp, struct mbuf *m,
337 struct sockaddr *dst, struct rtentry *rt)
338 {
339 device_t icdev = devclass_get_device(ic_devclass, ifp->if_dunit);
340 device_t parent = device_get_parent(icdev);
341 struct ic_softc *sc = (struct ic_softc *)device_get_softc(icdev);
342 int len, sent;
343 struct mbuf *mm;
344 u_char *cp;
345 uint32_t hdr = dst->sa_family;
346
347 ifp->if_flags |= IFF_RUNNING;
348
349 crit_enter();
350
351 /* already sending? */
352 if (sc->ic_sending) {
353 IFNET_STAT_INC(ifp, oerrors, 1);
354 goto error;
355 }
356
357 /* insert header */
358 bcopy ((char *)&hdr, sc->ic_obuf, ICHDRLEN);
359
360 cp = sc->ic_obuf + ICHDRLEN;
361 len = 0;
362 mm = m;
363 do {
364 if (len + mm->m_len > sc->ic_if.if_mtu) {
365 /* packet to large */
366 IFNET_STAT_INC(ifp, oerrors, 1);
367 goto error;
368 }
369
370 bcopy(mtod(mm,char *), cp, mm->m_len);
371 cp += mm->m_len;
372 len += mm->m_len;
373
374 } while ((mm = mm->m_next));
375
376 if (ifp->if_bpf) {
377 bpf_gettoken();
378 if (ifp->if_bpf)
379 bpf_ptap(ifp->if_bpf, m, &hdr, ICHDRLEN);
380 bpf_reltoken();
381 }
382
383 sc->ic_sending = 1;
384
385 m_freem(m);
386
387 crit_exit();
388
389 /* send the packet */
390 if (iicbus_block_write(parent, sc->ic_addr, sc->ic_obuf,
391 len + ICHDRLEN, &sent))
392
393 IFNET_STAT_INC(ifp, oerrors, 1);
394 else {
395 IFNET_STAT_INC(ifp, opackets, 1);
396 IFNET_STAT_INC(ifp, obytes, len);
397 }
398
399 sc->ic_sending = 0;
400
401 return (0);
402
403 error:
404 m_freem(m);
405 crit_exit();
406
407 return(0);
408 }
409
410 DRIVER_MODULE(if_ic, iicbus, ic_driver, ic_devclass, NULL, NULL);
411 MODULE_DEPEND(if_ic, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER);
412 MODULE_VERSION(if_ic, 1);
413