]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/iicbus/if_ic.c
This commit was generated by cvs2svn to compensate for changes in r149511,
[FreeBSD/FreeBSD.git] / 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$");
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_int32_t)
70 #define ICMTU           1500            /* default mtu */
71
72 struct ic_softc {
73         struct ifnet *ic_ifp;
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;
133
134         ifp = sc->ic_ifp = if_alloc(IFT_PARA);
135         if (ifp == NULL) {
136                 return (ENOSPC);
137         }
138
139         sc->ic_addr = PCF_MASTER_ADDRESS;       /* XXX only PCF masters */
140
141         ifp->if_softc = sc;
142         if_initname(ifp, device_get_name(dev), device_get_unit(dev));
143         ifp->if_mtu = ICMTU;
144         ifp->if_flags = IFF_SIMPLEX | IFF_POINTOPOINT | IFF_MULTICAST |
145             IFF_NEEDSGIANT;
146         ifp->if_ioctl = icioctl;
147         ifp->if_output = icoutput;
148         ifp->if_hdrlen = 0;
149         ifp->if_addrlen = 0;
150         ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
151
152         if_attach(ifp);
153
154         bpfattach(ifp, DLT_NULL, ICHDRLEN);
155
156         return (0);
157 }
158
159 /*
160  * iciotcl()
161  */
162 static int
163 icioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
164 {
165     device_t icdev = devclass_get_device(ic_devclass, ifp->if_dunit);
166     device_t parent = device_get_parent(icdev);
167     struct ic_softc *sc = (struct ic_softc *)device_get_softc(icdev);
168
169     struct ifaddr *ifa = (struct ifaddr *)data;
170     struct ifreq *ifr = (struct ifreq *)data;
171
172     u_char *iptr, *optr;
173     int error;
174
175     switch (cmd) {
176
177     case SIOCSIFDSTADDR:
178     case SIOCAIFADDR:
179     case SIOCSIFADDR:
180         if (ifa->ifa_addr->sa_family != AF_INET)
181             return EAFNOSUPPORT;
182         ifp->if_flags |= IFF_UP;
183         /* FALLTHROUGH */
184     case SIOCSIFFLAGS:
185         if ((!(ifp->if_flags & IFF_UP)) &&
186           (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
187
188             /* XXX disable PCF */
189             ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
190
191             /* IFF_UP is not set, try to release the bus anyway */
192             iicbus_release_bus(parent, icdev);
193             break;
194         }
195         if (((ifp->if_flags & IFF_UP)) &&
196           (!(ifp->if_drv_flags & IFF_DRV_RUNNING))) {
197
198             if ((error = iicbus_request_bus(parent, icdev, IIC_WAIT|IIC_INTR)))
199                 return (error);
200
201             sc->ic_obuf = malloc(sc->ic_ifp->if_mtu + ICHDRLEN,
202                                   M_DEVBUF, M_WAITOK);
203             if (!sc->ic_obuf) {
204                 iicbus_release_bus(parent, icdev);
205                 return ENOBUFS;
206             }
207
208             sc->ic_ifbuf = malloc(sc->ic_ifp->if_mtu + ICHDRLEN,
209                                   M_DEVBUF, M_WAITOK);
210             if (!sc->ic_ifbuf) {
211                 iicbus_release_bus(parent, icdev);
212                 return ENOBUFS;
213             }
214
215             iicbus_reset(parent, IIC_FASTEST, 0, NULL);
216
217             ifp->if_drv_flags |= IFF_DRV_RUNNING;
218         }
219         break;
220
221     case SIOCSIFMTU:
222         /* save previous buffers */
223         iptr = sc->ic_ifbuf;
224         optr = sc->ic_obuf;
225
226         /* allocate input buffer */
227         sc->ic_ifbuf = malloc(ifr->ifr_mtu+ICHDRLEN, M_DEVBUF, M_NOWAIT);
228         if (!sc->ic_ifbuf) {
229
230             sc->ic_ifbuf = iptr;
231             sc->ic_obuf = optr;
232
233             return ENOBUFS;
234         }
235
236         /* allocate output buffer */
237         sc->ic_ifbuf = malloc(ifr->ifr_mtu+ICHDRLEN, M_DEVBUF, M_NOWAIT);
238         if (!sc->ic_obuf) {
239
240             free(sc->ic_ifbuf,M_DEVBUF);
241
242             sc->ic_ifbuf = iptr;
243             sc->ic_obuf = optr;
244
245             return ENOBUFS;
246         }
247
248         if (iptr)
249             free(iptr,M_DEVBUF);
250
251         if (optr)
252             free(optr,M_DEVBUF);
253
254         sc->ic_ifp->if_mtu = ifr->ifr_mtu;
255         break;
256
257     case SIOCGIFMTU:
258         ifr->ifr_mtu = sc->ic_ifp->if_mtu;
259         break;
260
261     case SIOCADDMULTI:
262     case SIOCDELMULTI:
263         if (ifr == 0) {
264             return EAFNOSUPPORT;                /* XXX */
265         }
266         switch (ifr->ifr_addr.sa_family) {
267
268         case AF_INET:
269             break;
270
271         default:
272             return EAFNOSUPPORT;
273         }
274         break;
275
276     default:
277         return EINVAL;
278     }
279     return 0;
280 }
281
282 /*
283  * icintr()
284  */
285 static void
286 icintr (device_t dev, int event, char *ptr)
287 {
288         struct ic_softc *sc = (struct ic_softc *)device_get_softc(dev);
289         int unit = device_get_unit(dev);
290         int s, len;
291         struct mbuf *top;
292         
293         s = splhigh();
294
295         switch (event) {
296
297         case INTR_GENERAL:
298         case INTR_START:
299                 sc->ic_cp = sc->ic_ifbuf;
300                 sc->ic_xfercnt = 0;
301                 break;
302
303         case INTR_STOP:
304
305           /* if any error occured during transfert,
306            * drop the packet */
307           if (sc->ic_iferrs)
308             goto err;
309
310           if ((len = sc->ic_xfercnt) == 0)
311                 break;                                  /* ignore */
312
313           if (len <= ICHDRLEN)
314             goto err;
315
316           len -= ICHDRLEN;
317           sc->ic_ifp->if_ipackets ++;
318           sc->ic_ifp->if_ibytes += len;
319
320           BPF_TAP(sc->ic_ifp, sc->ic_ifbuf, len + ICHDRLEN);
321
322           top = m_devget(sc->ic_ifbuf + ICHDRLEN, len, 0, sc->ic_ifp, 0);
323
324           if (top)
325             netisr_dispatch(NETISR_IP, top);
326           break;
327
328         err:
329           printf("ic%d: errors (%d)!\n", unit, sc->ic_iferrs);
330
331           sc->ic_iferrs = 0;                    /* reset error count */
332           sc->ic_ifp->if_ierrors ++;
333
334           break;
335
336         case INTR_RECEIVE:
337                 if (sc->ic_xfercnt >= sc->ic_ifp->if_mtu+ICHDRLEN) {
338                         sc->ic_iferrs ++;
339
340                 } else {
341                         *sc->ic_cp++ = *ptr;
342                         sc->ic_xfercnt ++;
343                 }
344                 break;
345
346         case INTR_NOACK:                        /* xfer terminated by master */
347                 break;
348
349         case INTR_TRANSMIT:
350                 *ptr = 0xff;                                    /* XXX */
351                 break;
352
353         case INTR_ERROR:
354                 sc->ic_iferrs ++;
355                 break;
356
357         default:
358                 panic("%s: unknown event (%d)!", __func__, event);
359         }
360
361         splx(s);
362         return;
363 }
364
365 /*
366  * icoutput()
367  */
368 static int
369 icoutput(struct ifnet *ifp, struct mbuf *m,
370         struct sockaddr *dst, struct rtentry *rt)
371 {
372         device_t icdev = devclass_get_device(ic_devclass, ifp->if_dunit);
373         device_t parent = device_get_parent(icdev);
374         struct ic_softc *sc = (struct ic_softc *)device_get_softc(icdev);
375
376         int s, len, sent;
377         struct mbuf *mm;
378         u_char *cp;
379         u_int32_t hdr;
380
381         /* BPF writes need to be handled specially. */ 
382         if (dst->sa_family == AF_UNSPEC)
383                 bcopy(dst->sa_data, &hdr, sizeof(hdr));
384         else 
385                 hdr = dst->sa_family;
386
387         ifp->if_drv_flags |= IFF_DRV_RUNNING;
388
389         s = splhigh();
390
391         /* already sending? */
392         if (sc->ic_sending) {
393                 ifp->if_oerrors ++;
394                 goto error;
395         }
396                 
397         /* insert header */
398         bcopy ((char *)&hdr, sc->ic_obuf, ICHDRLEN);
399
400         cp = sc->ic_obuf + ICHDRLEN;
401         len = 0;
402         mm = m;
403         do {
404                 if (len + mm->m_len > sc->ic_ifp->if_mtu) {
405                         /* packet to large */
406                         ifp->if_oerrors ++;
407                         goto error;
408                 }
409                         
410                 bcopy(mtod(mm,char *), cp, mm->m_len);
411                 cp += mm->m_len;
412                 len += mm->m_len;
413
414         } while ((mm = mm->m_next));
415
416         BPF_MTAP2(ifp, &hdr, sizeof(hdr), m);
417
418         sc->ic_sending = 1;
419
420         m_freem(m);
421         splx(s);
422
423         /* send the packet */
424         if (iicbus_block_write(parent, sc->ic_addr, sc->ic_obuf,
425                                 len + ICHDRLEN, &sent))
426
427                 ifp->if_oerrors ++;
428         else {
429                 ifp->if_opackets ++;
430                 ifp->if_obytes += len;
431         }
432
433         sc->ic_sending = 0;
434
435         return (0);
436
437 error:
438         m_freem(m);
439         splx(s);
440
441         return(0);
442 }
443
444 DRIVER_MODULE(ic, iicbus, ic_driver, ic_devclass, 0, 0);
445 MODULE_DEPEND(ic, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER);
446 MODULE_VERSION(ic, 1);