2 * Copyright (c) 1998 Nicolas Souchu
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
26 * $FreeBSD: src/sys/dev/iicbus/if_ic.c,v 1.8 1999/12/29 04:35:39 peter Exp $
27 * $DragonFly: src/sys/dev/netif/ic/if_ic.c,v 1.17 2006/12/23 00:26:20 swildner Exp $
35 #include <sys/param.h>
36 #include <sys/systm.h>
39 #include <sys/socket.h>
40 #include <sys/filio.h>
41 #include <sys/sockio.h>
42 #include <sys/kernel.h>
43 #include <sys/module.h>
46 #include <sys/malloc.h>
47 #include <sys/thread2.h>
50 #include <net/ifq_var.h>
51 #include <net/if_types.h>
52 #include <net/netisr.h>
56 #include <sys/socket.h>
57 #include <net/netisr.h>
58 #include <net/route.h>
59 #include <netinet/in.h>
60 #include <netinet/in_systm.h>
61 #include <netinet/in_var.h>
62 #include <netinet/ip.h>
63 #include <netinet/if_ether.h>
67 #include <bus/iicbus/iiconf.h>
68 #include <bus/iicbus/iicbus.h>
70 #include "iicbus_if.h"
72 #define ICHDRLEN sizeof(uint32_t)
73 #define ICMTU 1500 /* default mtu */
78 u_char ic_addr
; /* peer I2C address */
91 static devclass_t ic_devclass
;
93 static int icprobe(device_t
);
94 static int icattach(device_t
);
96 static int icioctl(struct ifnet
*, u_long
, caddr_t
, struct ucred
*);
97 static int icoutput(struct ifnet
*, struct mbuf
*, struct sockaddr
*,
100 static void icintr(device_t
, int, char *);
102 static device_method_t ic_methods
[] = {
103 /* device interface */
104 DEVMETHOD(device_probe
, icprobe
),
105 DEVMETHOD(device_attach
, icattach
),
107 /* iicbus interface */
108 DEVMETHOD(iicbus_intr
, icintr
),
113 static driver_t ic_driver
= {
116 sizeof(struct ic_softc
),
123 icprobe(device_t dev
)
132 icattach(device_t dev
)
134 struct ic_softc
*sc
= (struct ic_softc
*)device_get_softc(dev
);
135 struct ifnet
*ifp
= &sc
->ic_if
;
137 sc
->ic_addr
= iicbus_get_addr(dev
);
140 if_initname(ifp
, "ic", device_get_unit(dev
));
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
;
148 ifq_set_maxlen(&ifp
->if_snd
, IFQ_MAXLEN
);
150 if_attach(ifp
, NULL
);
152 bpfattach(ifp
, DLT_NULL
, ICHDRLEN
);
161 icioctl(struct ifnet
*ifp
, u_long cmd
, caddr_t data
, struct ucred
*cr
)
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
);
167 struct ifaddr
*ifa
= (struct ifaddr
*)data
;
168 struct ifreq
*ifr
= (struct ifreq
*)data
;
178 if (ifa
->ifa_addr
->sa_family
!= AF_INET
)
180 ifp
->if_flags
|= IFF_UP
;
183 if ((!(ifp
->if_flags
& IFF_UP
)) && (ifp
->if_flags
& IFF_RUNNING
)) {
185 /* XXX disable PCF */
186 ifp
->if_flags
&= ~IFF_RUNNING
;
188 /* IFF_UP is not set, try to release the bus anyway */
189 iicbus_release_bus(parent
, icdev
);
192 if (((ifp
->if_flags
& IFF_UP
)) && (!(ifp
->if_flags
& IFF_RUNNING
))) {
194 if ((error
= iicbus_request_bus(parent
, icdev
, IIC_WAIT
|IIC_INTR
)))
197 sc
->ic_obuf
= kmalloc(sc
->ic_if
.if_mtu
+ ICHDRLEN
,
200 iicbus_release_bus(parent
, icdev
);
204 sc
->ic_ifbuf
= kmalloc(sc
->ic_if
.if_mtu
+ ICHDRLEN
,
207 iicbus_release_bus(parent
, icdev
);
211 iicbus_reset(parent
, IIC_FASTEST
, 0, NULL
);
213 ifp
->if_flags
|= IFF_RUNNING
;
218 /* save previous buffers */
222 /* allocate input buffer */
223 sc
->ic_ifbuf
= kmalloc(ifr
->ifr_mtu
+ICHDRLEN
, M_DEVBUF
, M_WAITOK
);
232 /* allocate output buffer */
233 sc
->ic_ifbuf
= kmalloc(ifr
->ifr_mtu
+ICHDRLEN
, M_DEVBUF
, M_WAITOK
);
236 kfree(sc
->ic_ifbuf
,M_DEVBUF
);
245 kfree(iptr
,M_DEVBUF
);
248 kfree(optr
,M_DEVBUF
);
250 sc
->ic_if
.if_mtu
= ifr
->ifr_mtu
;
254 ifr
->ifr_mtu
= sc
->ic_if
.if_mtu
;
260 return EAFNOSUPPORT
; /* XXX */
262 switch (ifr
->ifr_addr
.sa_family
) {
282 icintr (device_t dev
, int event
, char *ptr
)
284 struct ic_softc
*sc
= (struct ic_softc
*)device_get_softc(dev
);
285 int unit
= device_get_unit(dev
);
295 sc
->ic_cp
= sc
->ic_ifbuf
;
301 /* if any error occured during transfert,
306 if ((len
= sc
->ic_xfercnt
) == 0)
313 sc
->ic_if
.if_ipackets
++;
314 sc
->ic_if
.if_ibytes
+= len
;
316 BPF_TAP(&sc
->ic_if
, sc
->ic_ifbuf
, len
+ ICHDRLEN
);
318 top
= m_devget(sc
->ic_ifbuf
+ ICHDRLEN
, len
, 0, &sc
->ic_if
, 0);
321 netisr_dispatch(NETISR_IP
, top
);
325 kprintf("ic%d: errors (%d)!\n", unit
, sc
->ic_iferrs
);
327 sc
->ic_iferrs
= 0; /* reset error count */
328 sc
->ic_if
.if_ierrors
++;
333 if (sc
->ic_xfercnt
>= sc
->ic_if
.if_mtu
+ICHDRLEN
) {
342 case INTR_NOACK
: /* xfer terminated by master */
346 *ptr
= 0xff; /* XXX */
354 panic("%s: unknown event (%d)!", __func__
, event
);
364 icoutput(struct ifnet
*ifp
, struct mbuf
*m
,
365 struct sockaddr
*dst
, struct rtentry
*rt
)
367 device_t icdev
= devclass_get_device(ic_devclass
, ifp
->if_dunit
);
368 device_t parent
= device_get_parent(icdev
);
369 struct ic_softc
*sc
= (struct ic_softc
*)device_get_softc(icdev
);
373 uint32_t hdr
= dst
->sa_family
;
375 ifp
->if_flags
|= IFF_RUNNING
;
379 /* already sending? */
380 if (sc
->ic_sending
) {
386 bcopy ((char *)&hdr
, sc
->ic_obuf
, ICHDRLEN
);
388 cp
= sc
->ic_obuf
+ ICHDRLEN
;
392 if (len
+ mm
->m_len
> sc
->ic_if
.if_mtu
) {
393 /* packet to large */
398 bcopy(mtod(mm
,char *), cp
, mm
->m_len
);
402 } while ((mm
= mm
->m_next
));
405 bpf_ptap(ifp
->if_bpf
, m
, &hdr
, ICHDRLEN
);
413 /* send the packet */
414 if (iicbus_block_write(parent
, sc
->ic_addr
, sc
->ic_obuf
,
415 len
+ ICHDRLEN
, &sent
))
420 ifp
->if_obytes
+= len
;
434 DECLARE_DUMMY_MODULE(if_ic
);
435 DRIVER_MODULE(if_ic
, iicbus
, ic_driver
, ic_devclass
, 0, 0);