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