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/lock.h> 42 #include <sys/module.h> 43 #include <sys/mutex.h> 44 #include <sys/bus.h> 45 #include <sys/time.h> 46 #include <sys/malloc.h> 47 48 #include <net/if.h> 49 #include <net/if_types.h> 50 #include <net/netisr.h> 51 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 <net/bpf.h> 63 64 #include <dev/iicbus/iiconf.h> 65 #include <dev/iicbus/iicbus.h> 66 67 #include "iicbus_if.h" 68 69 #define PCF_MASTER_ADDRESS 0xaa 70 71 #define ICHDRLEN sizeof(u_int32_t) 72 #define ICMTU 1500 /* default mtu */ 73 74 struct ic_softc { 75 struct ifnet *ic_ifp; 76 device_t ic_dev; 77 78 u_char ic_addr; /* peer I2C address */ 79 80 int ic_flags; 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 struct mtx ic_lock; 91 }; 92 93 #define IC_SENDING 0x0001 94 #define IC_OBUF_BUSY 0x0002 95 #define IC_IFBUF_BUSY 0x0004 96 #define IC_BUFFERS_BUSY (IC_OBUF_BUSY | IC_IFBUF_BUSY) 97 #define IC_BUFFER_WAITER 0x0004 98 99 static devclass_t ic_devclass; 100 101 static int icprobe(device_t); 102 static int icattach(device_t); 103 104 static int icioctl(struct ifnet *, u_long, caddr_t); 105 static int icoutput(struct ifnet *, struct mbuf *, struct sockaddr *, 106 struct route *); 107 108 static int icintr(device_t, int, char *); 109 110 static device_method_t ic_methods[] = { 111 /* device interface */ 112 DEVMETHOD(device_probe, icprobe), 113 DEVMETHOD(device_attach, icattach), 114 115 /* iicbus interface */ 116 DEVMETHOD(iicbus_intr, icintr), 117 118 { 0, 0 } 119 }; 120 121 static driver_t ic_driver = { 122 "ic", 123 ic_methods, 124 sizeof(struct ic_softc), 125 }; 126 127 static void 128 ic_alloc_buffers(struct ic_softc *sc, int mtu) 129 { 130 char *obuf, *ifbuf; 131 132 obuf = malloc(mtu + ICHDRLEN, M_DEVBUF, M_WAITOK); 133 ifbuf = malloc(mtu + ICHDRLEN, M_DEVBUF, M_WAITOK); 134 135 mtx_lock(&sc->ic_lock); 136 while (sc->ic_flags & IC_BUFFERS_BUSY) { 137 sc->ic_flags |= IC_BUFFER_WAITER; 138 mtx_sleep(sc, &sc->ic_lock, 0, "icalloc", 0); 139 sc->ic_flags &= ~IC_BUFFER_WAITER; 140 } 141 142 free(sc->ic_obuf, M_DEVBUF); 143 free(sc->ic_ifbuf, M_DEVBUF); 144 sc->ic_obuf = obuf; 145 sc->ic_ifbuf = ifbuf; 146 sc->ic_ifp->if_mtu = mtu; 147 mtx_unlock(&sc->ic_lock); 148 } 149 150 /* 151 * icprobe() 152 */ 153 static int 154 icprobe(device_t dev) 155 { 156 return (BUS_PROBE_NOWILDCARD); 157 } 158 159 /* 160 * icattach() 161 */ 162 static int 163 icattach(device_t dev) 164 { 165 struct ic_softc *sc = (struct ic_softc *)device_get_softc(dev); 166 struct ifnet *ifp; 167 168 ifp = sc->ic_ifp = if_alloc(IFT_PARA); 169 if (ifp == NULL) 170 return (ENOSPC); 171 172 mtx_init(&sc->ic_lock, device_get_nameunit(dev), MTX_NETWORK_LOCK, 173 MTX_DEF); 174 sc->ic_addr = PCF_MASTER_ADDRESS; /* XXX only PCF masters */ 175 sc->ic_dev = dev; 176 177 ifp->if_softc = sc; 178 if_initname(ifp, device_get_name(dev), device_get_unit(dev)); 179 ifp->if_flags = IFF_SIMPLEX | IFF_POINTOPOINT | IFF_MULTICAST; 180 ifp->if_ioctl = icioctl; 181 ifp->if_output = icoutput; 182 ifp->if_hdrlen = 0; 183 ifp->if_addrlen = 0; 184 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN; 185 186 ic_alloc_buffers(sc, ICMTU); 187 188 if_attach(ifp); 189 190 bpfattach(ifp, DLT_NULL, ICHDRLEN); 191 192 return (0); 193 } 194 195 /* 196 * iciotcl() 197 */ 198 static int 199 icioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 200 { 201 struct ic_softc *sc = ifp->if_softc; 202 device_t icdev = sc->ic_dev; 203 device_t parent = device_get_parent(icdev); 204 struct ifaddr *ifa = (struct ifaddr *)data; 205 struct ifreq *ifr = (struct ifreq *)data; 206 int error; 207 208 switch (cmd) { 209 210 case SIOCSIFDSTADDR: 211 case SIOCAIFADDR: 212 case SIOCSIFADDR: 213 if (ifa->ifa_addr->sa_family != AF_INET) 214 return (EAFNOSUPPORT); 215 mtx_lock(&sc->ic_lock); 216 ifp->if_flags |= IFF_UP; 217 goto locked; 218 case SIOCSIFFLAGS: 219 mtx_lock(&sc->ic_lock); 220 locked: 221 if ((!(ifp->if_flags & IFF_UP)) && 222 (ifp->if_drv_flags & IFF_DRV_RUNNING)) { 223 224 /* XXX disable PCF */ 225 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 226 mtx_unlock(&sc->ic_lock); 227 228 /* IFF_UP is not set, try to release the bus anyway */ 229 iicbus_release_bus(parent, icdev); 230 break; 231 } 232 if (((ifp->if_flags & IFF_UP)) && 233 (!(ifp->if_drv_flags & IFF_DRV_RUNNING))) { 234 mtx_unlock(&sc->ic_lock); 235 if ((error = iicbus_request_bus(parent, icdev, 236 IIC_WAIT | IIC_INTR))) 237 return (error); 238 mtx_lock(&sc->ic_lock); 239 iicbus_reset(parent, IIC_FASTEST, 0, NULL); 240 ifp->if_drv_flags |= IFF_DRV_RUNNING; 241 } 242 mtx_unlock(&sc->ic_lock); 243 break; 244 245 case SIOCSIFMTU: 246 ic_alloc_buffers(sc, ifr->ifr_mtu); 247 break; 248 249 case SIOCGIFMTU: 250 mtx_lock(&sc->ic_lock); 251 ifr->ifr_mtu = sc->ic_ifp->if_mtu; 252 mtx_unlock(&sc->ic_lock); 253 break; 254 255 case SIOCADDMULTI: 256 case SIOCDELMULTI: 257 if (ifr == 0) 258 return (EAFNOSUPPORT); /* XXX */ 259 switch (ifr->ifr_addr.sa_family) { 260 case AF_INET: 261 break; 262 default: 263 return (EAFNOSUPPORT); 264 } 265 break; 266 default: 267 return (EINVAL); 268 } 269 return (0); 270 } 271 272 /* 273 * icintr() 274 */ 275 static int 276 icintr(device_t dev, int event, char *ptr) 277 { 278 struct ic_softc *sc = (struct ic_softc *)device_get_softc(dev); 279 struct mbuf *top; 280 int len; 281 282 mtx_lock(&sc->ic_lock); 283 284 switch (event) { 285 286 case INTR_GENERAL: 287 case INTR_START: 288 sc->ic_cp = sc->ic_ifbuf; 289 sc->ic_xfercnt = 0; 290 sc->ic_flags |= IC_IFBUF_BUSY; 291 break; 292 293 case INTR_STOP: 294 295 /* if any error occured during transfert, 296 * drop the packet */ 297 sc->ic_flags &= ~IC_IFBUF_BUSY; 298 if ((sc->ic_flags & (IC_BUFFERS_BUSY | IC_BUFFER_WAITER)) == 299 IC_BUFFER_WAITER) 300 wakeup(&sc); 301 if (sc->ic_iferrs) 302 goto err; 303 if ((len = sc->ic_xfercnt) == 0) 304 break; /* ignore */ 305 if (len <= ICHDRLEN) 306 goto err; 307 len -= ICHDRLEN; 308 sc->ic_ifp->if_ipackets++; 309 sc->ic_ifp->if_ibytes += len; 310 BPF_TAP(sc->ic_ifp, sc->ic_ifbuf, len + ICHDRLEN); 311 top = m_devget(sc->ic_ifbuf + ICHDRLEN, len, 0, sc->ic_ifp, 0); 312 if (top) { 313 mtx_unlock(&sc->ic_lock); 314 netisr_dispatch(NETISR_IP, top); 315 mtx_lock(&sc->ic_lock); 316 } 317 break; 318 err: 319 if_printf(sc->ic_ifp, "errors (%d)!\n", sc->ic_iferrs); 320 sc->ic_iferrs = 0; /* reset error count */ 321 sc->ic_ifp->if_ierrors++; 322 break; 323 324 case INTR_RECEIVE: 325 if (sc->ic_xfercnt >= sc->ic_ifp->if_mtu + ICHDRLEN) { 326 sc->ic_iferrs++; 327 } else { 328 *sc->ic_cp++ = *ptr; 329 sc->ic_xfercnt++; 330 } 331 break; 332 333 case INTR_NOACK: /* xfer terminated by master */ 334 break; 335 336 case INTR_TRANSMIT: 337 *ptr = 0xff; /* XXX */ 338 break; 339 340 case INTR_ERROR: 341 sc->ic_iferrs++; 342 break; 343 344 default: 345 panic("%s: unknown event (%d)!", __func__, event); 346 } 347 348 mtx_unlock(&sc->ic_lock); 349 return (0); 350 } 351 352 /* 353 * icoutput() 354 */ 355 static int 356 icoutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst, 357 struct route *ro) 358 { 359 struct ic_softc *sc = ifp->if_softc; 360 device_t icdev = sc->ic_dev; 361 device_t parent = device_get_parent(icdev); 362 int len, sent; 363 struct mbuf *mm; 364 u_char *cp; 365 u_int32_t hdr; 366 367 /* BPF writes need to be handled specially. */ 368 if (dst->sa_family == AF_UNSPEC) 369 bcopy(dst->sa_data, &hdr, sizeof(hdr)); 370 else 371 hdr = dst->sa_family; 372 373 mtx_lock(&sc->ic_lock); 374 ifp->if_drv_flags |= IFF_DRV_RUNNING; 375 376 /* already sending? */ 377 if (sc->ic_flags & IC_SENDING) { 378 ifp->if_oerrors++; 379 goto error; 380 } 381 382 /* insert header */ 383 bcopy ((char *)&hdr, sc->ic_obuf, ICHDRLEN); 384 385 cp = sc->ic_obuf + ICHDRLEN; 386 len = 0; 387 mm = m; 388 do { 389 if (len + mm->m_len > sc->ic_ifp->if_mtu) { 390 /* packet too large */ 391 ifp->if_oerrors++; 392 goto error; 393 } 394 395 bcopy(mtod(mm,char *), cp, mm->m_len); 396 cp += mm->m_len; 397 len += mm->m_len; 398 399 } while ((mm = mm->m_next)); 400 401 BPF_MTAP2(ifp, &hdr, sizeof(hdr), m); 402 403 sc->ic_flags |= (IC_SENDING | IC_OBUF_BUSY); 404 405 m_freem(m); 406 mtx_unlock(&sc->ic_lock); 407 408 /* send the packet */ 409 if (iicbus_block_write(parent, sc->ic_addr, sc->ic_obuf, 410 len + ICHDRLEN, &sent)) 411 412 ifp->if_oerrors++; 413 else { 414 ifp->if_opackets++; 415 ifp->if_obytes += len; 416 } 417 418 mtx_lock(&sc->ic_lock); 419 sc->ic_flags &= ~(IC_SENDING | IC_OBUF_BUSY); 420 if ((sc->ic_flags & (IC_BUFFERS_BUSY | IC_BUFFER_WAITER)) == 421 IC_BUFFER_WAITER) 422 wakeup(&sc); 423 mtx_unlock(&sc->ic_lock); 424 425 return (0); 426 427 error: 428 m_freem(m); 429 mtx_unlock(&sc->ic_lock); 430 431 return(0); 432 } 433 434 DRIVER_MODULE(ic, iicbus, ic_driver, ic_devclass, 0, 0); 435 MODULE_DEPEND(ic, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER); 436 MODULE_VERSION(ic, 1); 437