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 * $Id: if_ic.c,v 1.3 1998/12/07 21:58:16 archie 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 47 #include <net/if.h> 48 #include <net/if_types.h> 49 #include <net/netisr.h> 50 51 #endif /* KERNEL */ 52 #include <sys/mbuf.h> 53 #include <sys/socket.h> 54 #include <net/netisr.h> 55 #include <net/route.h> 56 #include <netinet/in.h> 57 #include <netinet/in_systm.h> 58 #include <netinet/in_var.h> 59 #include <netinet/ip.h> 60 #include <netinet/if_ether.h> 61 62 #include "bpfilter.h" 63 64 #if NBPFILTER > 0 65 #include <net/bpf.h> 66 #endif 67 68 #include <dev/iicbus/iiconf.h> 69 #include <dev/iicbus/iicbus.h> 70 71 #include "iicbus_if.h" 72 73 #define ICHDRLEN sizeof(u_int) 74 #define ICMTU 1500 /* default mtu */ 75 76 struct ic_softc { 77 struct ifnet ic_if; 78 79 u_char ic_addr; /* peer I2C address */ 80 81 int ic_sending; 82 83 char *ic_obuf; 84 char *ic_ifbuf; 85 char *ic_cp; 86 87 int ic_xfercnt; 88 89 int ic_iferrs; 90 }; 91 92 static devclass_t ic_devclass; 93 94 static int icprobe(device_t); 95 static int icattach(device_t); 96 97 static int icioctl(struct ifnet *, u_long, caddr_t); 98 static int icoutput(struct ifnet *, struct mbuf *, struct sockaddr *, 99 struct rtentry *); 100 101 static void icintr(device_t, int, char *); 102 103 static device_method_t ic_methods[] = { 104 /* device interface */ 105 DEVMETHOD(device_probe, icprobe), 106 DEVMETHOD(device_attach, icattach), 107 108 /* iicbus interface */ 109 DEVMETHOD(iicbus_intr, icintr), 110 111 { 0, 0 } 112 }; 113 114 static driver_t ic_driver = { 115 "ic", 116 ic_methods, 117 sizeof(struct ic_softc), 118 }; 119 120 /* 121 * icprobe() 122 */ 123 static int 124 icprobe(device_t dev) 125 { 126 return (0); 127 } 128 129 /* 130 * icattach() 131 */ 132 static int 133 icattach(device_t dev) 134 { 135 struct ic_softc *sc = (struct ic_softc *)device_get_softc(dev); 136 struct ifnet *ifp = &sc->ic_if; 137 138 sc->ic_addr = iicbus_get_addr(dev); 139 140 ifp->if_softc = sc; 141 ifp->if_name = "ic"; 142 ifp->if_unit = device_get_unit(dev); 143 ifp->if_mtu = ICMTU; 144 ifp->if_flags = IFF_SIMPLEX | IFF_POINTOPOINT | IFF_MULTICAST; 145 ifp->if_ioctl = icioctl; 146 ifp->if_output = icoutput; 147 ifp->if_type = IFT_PARA; 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 #if NBPFILTER > 0 155 bpfattach(ifp, DLT_NULL, ICHDRLEN); 156 #endif 157 158 return (0); 159 } 160 161 /* 162 * iciotcl() 163 */ 164 static int 165 icioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 166 { 167 device_t icdev = devclass_get_device(ic_devclass, ifp->if_unit); 168 device_t parent = device_get_parent(icdev); 169 struct ic_softc *sc = (struct ic_softc *)device_get_softc(icdev); 170 171 struct ifaddr *ifa = (struct ifaddr *)data; 172 struct ifreq *ifr = (struct ifreq *)data; 173 174 u_char *iptr, *optr; 175 int error; 176 177 switch (cmd) { 178 179 case SIOCSIFDSTADDR: 180 case SIOCAIFADDR: 181 case SIOCSIFADDR: 182 if (ifa->ifa_addr->sa_family != AF_INET) 183 return EAFNOSUPPORT; 184 ifp->if_flags |= IFF_UP; 185 /* FALLTHROUGH */ 186 case SIOCSIFFLAGS: 187 if ((!(ifp->if_flags & IFF_UP)) && (ifp->if_flags & IFF_RUNNING)) { 188 189 /* XXX disable PCF */ 190 ifp->if_flags &= ~IFF_RUNNING; 191 192 /* IFF_UP is not set, try to release the bus anyway */ 193 iicbus_release_bus(parent, icdev); 194 break; 195 } 196 if (((ifp->if_flags & IFF_UP)) && (!(ifp->if_flags & IFF_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_if.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_if.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_flags |= IFF_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_if.if_mtu = ifr->ifr_mtu; 255 break; 256 257 case SIOCGIFMTU: 258 ifr->ifr_mtu = sc->ic_if.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 if (IF_QFULL(&ipintrq)) { 317 IF_DROP(&ipintrq); 318 break; 319 } 320 321 len -= ICHDRLEN; 322 sc->ic_if.if_ipackets ++; 323 sc->ic_if.if_ibytes += len; 324 325 #if NBPFILTER > 0 326 if (sc->ic_if.if_bpf) 327 bpf_tap(&sc->ic_if, sc->ic_ifbuf, len + ICHDRLEN); 328 #endif 329 330 top = m_devget(sc->ic_ifbuf + ICHDRLEN, len, 0, &sc->ic_if, 0); 331 332 if (top) { 333 IF_ENQUEUE(&ipintrq, top); 334 schednetisr(NETISR_IP); 335 } 336 break; 337 338 err: 339 printf("ic%d: errors (%d)!\n", unit, sc->ic_iferrs); 340 341 sc->ic_iferrs = 0; /* reset error count */ 342 sc->ic_if.if_ierrors ++; 343 344 break; 345 346 case INTR_RECEIVE: 347 if (sc->ic_xfercnt >= sc->ic_if.if_mtu+ICHDRLEN) { 348 sc->ic_iferrs ++; 349 350 } else { 351 *sc->ic_cp++ = *ptr; 352 sc->ic_xfercnt ++; 353 } 354 break; 355 356 case INTR_NOACK: /* xfer terminated by master */ 357 break; 358 359 case INTR_TRANSMIT: 360 *ptr = 0xff; /* XXX */ 361 break; 362 363 case INTR_ERROR: 364 sc->ic_iferrs ++; 365 break; 366 367 default: 368 panic("%s: unknown event (%d)!", __FUNCTION__, event); 369 } 370 371 splx(s); 372 return; 373 } 374 375 /* 376 * icoutput() 377 */ 378 static int 379 icoutput(struct ifnet *ifp, struct mbuf *m, 380 struct sockaddr *dst, struct rtentry *rt) 381 { 382 device_t icdev = devclass_get_device(ic_devclass, ifp->if_unit); 383 device_t parent = device_get_parent(icdev); 384 struct ic_softc *sc = (struct ic_softc *)device_get_softc(icdev); 385 386 int s, len, sent; 387 struct mbuf *mm; 388 u_char *cp; 389 u_int hdr = dst->sa_family; 390 391 ifp->if_flags |= IFF_RUNNING; 392 393 s = splhigh(); 394 395 /* already sending? */ 396 if (sc->ic_sending) { 397 ifp->if_oerrors ++; 398 goto error; 399 } 400 401 /* insert header */ 402 bcopy ((char *)&hdr, sc->ic_obuf, ICHDRLEN); 403 404 cp = sc->ic_obuf + ICHDRLEN; 405 len = 0; 406 mm = m; 407 do { 408 if (len + mm->m_len > sc->ic_if.if_mtu) { 409 /* packet to large */ 410 ifp->if_oerrors ++; 411 goto error; 412 } 413 414 bcopy(mtod(mm,char *), cp, mm->m_len); 415 cp += mm->m_len; 416 len += mm->m_len; 417 418 } while ((mm = mm->m_next)); 419 420 #if NBPFILTER > 0 421 if (ifp->if_bpf) { 422 struct mbuf m0, *n = m; 423 424 /* 425 * We need to prepend the address family as 426 * a four byte field. Cons up a dummy header 427 * to pacify bpf. This is safe because bpf 428 * will only read from the mbuf (i.e., it won't 429 * try to free it or keep a pointer a to it). 430 */ 431 m0.m_next = m; 432 m0.m_len = sizeof(u_int); 433 m0.m_data = (char *)&hdr; 434 n = &m0; 435 436 bpf_mtap(ifp, n); 437 } 438 #endif 439 440 sc->ic_sending = 1; 441 442 m_freem(m); 443 splx(s); 444 445 /* send the packet */ 446 if (iicbus_block_write(parent, sc->ic_addr, sc->ic_obuf, 447 len + ICHDRLEN, &sent)) 448 449 ifp->if_oerrors ++; 450 else { 451 ifp->if_opackets ++; 452 ifp->if_obytes += len; 453 } 454 455 sc->ic_sending = 0; 456 457 return (0); 458 459 error: 460 m_freem(m); 461 splx(s); 462 463 return(0); 464 } 465 466 DRIVER_MODULE(ic, iicbus, ic_driver, ic_devclass, 0, 0); 467