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.2 1998/10/31 11:31:07 nsouch 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 DRIVER_TYPE_MISC, 118 sizeof(struct ic_softc), 119 }; 120 121 /* 122 * icprobe() 123 */ 124 static int 125 icprobe(device_t dev) 126 { 127 return (0); 128 } 129 130 /* 131 * icattach() 132 */ 133 static int 134 icattach(device_t dev) 135 { 136 struct ic_softc *sc = (struct ic_softc *)device_get_softc(dev); 137 struct ifnet *ifp = &sc->ic_if; 138 139 sc->ic_addr = iicbus_get_addr(dev); 140 141 ifp->if_softc = sc; 142 ifp->if_name = "ic"; 143 ifp->if_unit = device_get_unit(dev); 144 ifp->if_mtu = ICMTU; 145 ifp->if_flags = IFF_SIMPLEX | IFF_POINTOPOINT | IFF_MULTICAST; 146 ifp->if_ioctl = icioctl; 147 ifp->if_output = icoutput; 148 ifp->if_type = IFT_PARA; 149 ifp->if_hdrlen = 0; 150 ifp->if_addrlen = 0; 151 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN; 152 153 if_attach(ifp); 154 155 #if NBPFILTER > 0 156 bpfattach(ifp, DLT_NULL, ICHDRLEN); 157 #endif 158 159 return (0); 160 } 161 162 /* 163 * iciotcl() 164 */ 165 static int 166 icioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 167 { 168 device_t icdev = devclass_get_device(ic_devclass, ifp->if_unit); 169 device_t parent = device_get_parent(icdev); 170 struct ic_softc *sc = (struct ic_softc *)device_get_softc(icdev); 171 172 struct ifaddr *ifa = (struct ifaddr *)data; 173 struct ifreq *ifr = (struct ifreq *)data; 174 175 u_char *iptr, *optr; 176 int error; 177 178 switch (cmd) { 179 180 case SIOCSIFDSTADDR: 181 case SIOCAIFADDR: 182 case SIOCSIFADDR: 183 if (ifa->ifa_addr->sa_family != AF_INET) 184 return EAFNOSUPPORT; 185 ifp->if_flags |= IFF_UP; 186 /* FALLTHROUGH */ 187 case SIOCSIFFLAGS: 188 if ((!(ifp->if_flags & IFF_UP)) && (ifp->if_flags & IFF_RUNNING)) { 189 190 /* XXX disable PCF */ 191 ifp->if_flags &= ~IFF_RUNNING; 192 193 /* IFF_UP is not set, try to release the bus anyway */ 194 iicbus_release_bus(parent, icdev); 195 break; 196 } 197 if (((ifp->if_flags & IFF_UP)) && (!(ifp->if_flags & IFF_RUNNING))) { 198 199 if ((error = iicbus_request_bus(parent, icdev, IIC_WAIT|IIC_INTR))) 200 return (error); 201 202 sc->ic_obuf = malloc(sc->ic_if.if_mtu + ICHDRLEN, 203 M_DEVBUF, M_WAITOK); 204 if (!sc->ic_obuf) { 205 iicbus_release_bus(parent, icdev); 206 return ENOBUFS; 207 } 208 209 sc->ic_ifbuf = malloc(sc->ic_if.if_mtu + ICHDRLEN, 210 M_DEVBUF, M_WAITOK); 211 if (!sc->ic_ifbuf) { 212 iicbus_release_bus(parent, icdev); 213 return ENOBUFS; 214 } 215 216 iicbus_reset(parent, IIC_FASTEST, 0, NULL); 217 218 ifp->if_flags |= IFF_RUNNING; 219 } 220 break; 221 222 case SIOCSIFMTU: 223 /* save previous buffers */ 224 iptr = sc->ic_ifbuf; 225 optr = sc->ic_obuf; 226 227 /* allocate input buffer */ 228 sc->ic_ifbuf = malloc(ifr->ifr_mtu+ICHDRLEN, M_DEVBUF, M_NOWAIT); 229 if (!sc->ic_ifbuf) { 230 231 sc->ic_ifbuf = iptr; 232 sc->ic_obuf = optr; 233 234 return ENOBUFS; 235 } 236 237 /* allocate output buffer */ 238 sc->ic_ifbuf = malloc(ifr->ifr_mtu+ICHDRLEN, M_DEVBUF, M_NOWAIT); 239 if (!sc->ic_obuf) { 240 241 free(sc->ic_ifbuf,M_DEVBUF); 242 243 sc->ic_ifbuf = iptr; 244 sc->ic_obuf = optr; 245 246 return ENOBUFS; 247 } 248 249 if (iptr) 250 free(iptr,M_DEVBUF); 251 252 if (optr) 253 free(optr,M_DEVBUF); 254 255 sc->ic_if.if_mtu = ifr->ifr_mtu; 256 break; 257 258 case SIOCGIFMTU: 259 ifr->ifr_mtu = sc->ic_if.if_mtu; 260 break; 261 262 case SIOCADDMULTI: 263 case SIOCDELMULTI: 264 if (ifr == 0) { 265 return EAFNOSUPPORT; /* XXX */ 266 } 267 switch (ifr->ifr_addr.sa_family) { 268 269 case AF_INET: 270 break; 271 272 default: 273 return EAFNOSUPPORT; 274 } 275 break; 276 277 default: 278 return EINVAL; 279 } 280 return 0; 281 } 282 283 /* 284 * icintr() 285 */ 286 static void 287 icintr (device_t dev, int event, char *ptr) 288 { 289 struct ic_softc *sc = (struct ic_softc *)device_get_softc(dev); 290 int unit = device_get_unit(dev); 291 int s, len; 292 struct mbuf *top; 293 294 s = splhigh(); 295 296 switch (event) { 297 298 case INTR_GENERAL: 299 case INTR_START: 300 sc->ic_cp = sc->ic_ifbuf; 301 sc->ic_xfercnt = 0; 302 break; 303 304 case INTR_STOP: 305 306 /* if any error occured during transfert, 307 * drop the packet */ 308 if (sc->ic_iferrs) 309 goto err; 310 311 if ((len = sc->ic_xfercnt) == 0) 312 break; /* ignore */ 313 314 if (len <= ICHDRLEN) 315 goto err; 316 317 if (IF_QFULL(&ipintrq)) { 318 IF_DROP(&ipintrq); 319 break; 320 } 321 322 len -= ICHDRLEN; 323 sc->ic_if.if_ipackets ++; 324 sc->ic_if.if_ibytes += len; 325 326 #if NBPFILTER > 0 327 if (sc->ic_if.if_bpf) 328 bpf_tap(&sc->ic_if, sc->ic_ifbuf, len + ICHDRLEN); 329 #endif 330 331 top = m_devget(sc->ic_ifbuf + ICHDRLEN, len, 0, &sc->ic_if, 0); 332 333 if (top) { 334 IF_ENQUEUE(&ipintrq, top); 335 schednetisr(NETISR_IP); 336 } 337 break; 338 339 err: 340 printf("ic%d: errors (%d)!\n", unit, sc->ic_iferrs); 341 342 sc->ic_iferrs = 0; /* reset error count */ 343 sc->ic_if.if_ierrors ++; 344 345 break; 346 347 case INTR_RECEIVE: 348 if (sc->ic_xfercnt >= sc->ic_if.if_mtu+ICHDRLEN) { 349 sc->ic_iferrs ++; 350 351 } else { 352 *sc->ic_cp++ = *ptr; 353 sc->ic_xfercnt ++; 354 } 355 break; 356 357 case INTR_NOACK: /* xfer terminated by master */ 358 break; 359 360 case INTR_TRANSMIT: 361 *ptr = 0xff; /* XXX */ 362 break; 363 364 case INTR_ERROR: 365 sc->ic_iferrs ++; 366 break; 367 368 default: 369 panic("%s: unknown event (%d)!", __FUNCTION__, event); 370 } 371 372 splx(s); 373 return; 374 } 375 376 /* 377 * icoutput() 378 */ 379 static int 380 icoutput(struct ifnet *ifp, struct mbuf *m, 381 struct sockaddr *dst, struct rtentry *rt) 382 { 383 device_t icdev = devclass_get_device(ic_devclass, ifp->if_unit); 384 device_t parent = device_get_parent(icdev); 385 struct ic_softc *sc = (struct ic_softc *)device_get_softc(icdev); 386 387 int s, len, sent; 388 struct mbuf *mm; 389 u_char *cp; 390 u_int hdr = dst->sa_family; 391 392 ifp->if_flags |= IFF_RUNNING; 393 394 s = splhigh(); 395 396 /* already sending? */ 397 if (sc->ic_sending) { 398 ifp->if_oerrors ++; 399 goto error; 400 } 401 402 /* insert header */ 403 bcopy ((char *)&hdr, sc->ic_obuf, ICHDRLEN); 404 405 cp = sc->ic_obuf + ICHDRLEN; 406 len = 0; 407 mm = m; 408 do { 409 if (len + mm->m_len > sc->ic_if.if_mtu) { 410 /* packet to large */ 411 ifp->if_oerrors ++; 412 goto error; 413 } 414 415 bcopy(mtod(mm,char *), cp, mm->m_len); 416 cp += mm->m_len; 417 len += mm->m_len; 418 419 } while ((mm = mm->m_next)); 420 421 #if NBPFILTER > 0 422 if (ifp->if_bpf) { 423 struct mbuf m0, *n = m; 424 425 /* 426 * We need to prepend the address family as 427 * a four byte field. Cons up a dummy header 428 * to pacify bpf. This is safe because bpf 429 * will only read from the mbuf (i.e., it won't 430 * try to free it or keep a pointer a to it). 431 */ 432 m0.m_next = m; 433 m0.m_len = sizeof(u_int); 434 m0.m_data = (char *)&hdr; 435 n = &m0; 436 437 bpf_mtap(ifp, n); 438 } 439 #endif 440 441 sc->ic_sending = 1; 442 443 m_freem(m); 444 splx(s); 445 446 /* send the packet */ 447 if (iicbus_block_write(parent, sc->ic_addr, sc->ic_obuf, 448 len + ICHDRLEN, &sent)) 449 450 ifp->if_oerrors ++; 451 else { 452 ifp->if_opackets ++; 453 ifp->if_obytes += len; 454 } 455 456 sc->ic_sending = 0; 457 458 return (0); 459 460 error: 461 m_freem(m); 462 splx(s); 463 464 return(0); 465 } 466 467 DRIVER_MODULE(ic, iicbus, ic_driver, ic_devclass, 0, 0); 468