CVS commit: [bouyer-socketcan] src/sys/kern
Module Name:src Committed By: bouyer Date: Thu May 25 18:52:24 UTC 2017 Modified Files: src/sys/kern [bouyer-socketcan]: uipc_socket.c Log Message: Allow any user to bind to CAN sockets. Maybe a better security model is needed. To generate a diff of this commit: cvs rdiff -u -r1.252 -r1.252.2.1 src/sys/kern/uipc_socket.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/sys/kern/uipc_socket.c diff -u src/sys/kern/uipc_socket.c:1.252 src/sys/kern/uipc_socket.c:1.252.2.1 --- src/sys/kern/uipc_socket.c:1.252 Thu Oct 13 19:10:23 2016 +++ src/sys/kern/uipc_socket.c Thu May 25 18:52:24 2017 @@ -1,4 +1,4 @@ -/* $NetBSD: uipc_socket.c,v 1.252 2016/10/13 19:10:23 uwe Exp $ */ +/* $NetBSD: uipc_socket.c,v 1.252.2.1 2017/05/25 18:52:24 bouyer Exp $ */ /*- * Copyright (c) 2002, 2007, 2008, 2009 The NetBSD Foundation, Inc. @@ -71,7 +71,7 @@ */ #include -__KERNEL_RCSID(0, "$NetBSD: uipc_socket.c,v 1.252 2016/10/13 19:10:23 uwe Exp $"); +__KERNEL_RCSID(0, "$NetBSD: uipc_socket.c,v 1.252.2.1 2017/05/25 18:52:24 bouyer Exp $"); #ifdef _KERNEL_OPT #include "opt_compat_netbsd.h" @@ -433,9 +433,9 @@ socket_listener_cb(kauth_cred_t cred, ka } case KAUTH_REQ_NETWORK_SOCKET_OPEN: - /* We allow "raw" routing/bluetooth sockets to anyone. */ + /* We allow "raw" routing/bluetooth/CAN sockets to anyone. */ if ((u_long)arg1 == PF_ROUTE || (u_long)arg1 == PF_OROUTE - || (u_long)arg1 == PF_BLUETOOTH) { + || (u_long)arg1 == PF_BLUETOOTH || (u_long)arg1 == PF_CAN) { result = KAUTH_RESULT_ALLOW; } else { /* Privileged, let secmodel handle this. */
CVS commit: [bouyer-socketcan] src/sys/arch/arm/allwinner
Module Name:src Committed By: bouyer Date: Thu May 25 18:26:36 UTC 2017 Modified Files: src/sys/arch/arm/allwinner [bouyer-socketcan]: awin_can.c Log Message: abort may clear sc_m_transmit but leave OACTIVE, so unconditionally clear OACTIVE in TX interrupt. To generate a diff of this commit: cvs rdiff -u -r1.1.2.7 -r1.1.2.8 src/sys/arch/arm/allwinner/awin_can.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/sys/arch/arm/allwinner/awin_can.c diff -u src/sys/arch/arm/allwinner/awin_can.c:1.1.2.7 src/sys/arch/arm/allwinner/awin_can.c:1.1.2.8 --- src/sys/arch/arm/allwinner/awin_can.c:1.1.2.7 Thu May 25 18:23:15 2017 +++ src/sys/arch/arm/allwinner/awin_can.c Thu May 25 18:26:36 2017 @@ -1,4 +1,4 @@ -/* $NetBSD: awin_can.c,v 1.1.2.7 2017/05/25 18:23:15 bouyer Exp $ */ +/* $NetBSD: awin_can.c,v 1.1.2.8 2017/05/25 18:26:36 bouyer Exp $ */ /*- * Copyright (c) 2017 The NetBSD Foundation, Inc. @@ -36,7 +36,7 @@ #include -__KERNEL_RCSID(1, "$NetBSD: awin_can.c,v 1.1.2.7 2017/05/25 18:23:15 bouyer Exp $"); +__KERNEL_RCSID(1, "$NetBSD: awin_can.c,v 1.1.2.8 2017/05/25 18:26:36 bouyer Exp $"); #include #include @@ -297,9 +297,8 @@ awin_can_tx_intr(struct awin_can_softc * can_input(ifp, m); /* loopback */ sc->sc_m_transmit = NULL; ifp->if_timer = 0; - ifp->if_flags &= ~IFF_OACTIVE; } - + ifp->if_flags &= ~IFF_OACTIVE; if_schedule_deferred_start(ifp); }
CVS commit: [bouyer-socketcan] src/sys/arch/arm/allwinner
Module Name:src Committed By: bouyer Date: Thu May 25 18:23:16 UTC 2017 Modified Files: src/sys/arch/arm/allwinner [bouyer-socketcan]: awin_can.c Log Message: Do not call bpf_mtap() from interrupt context. Mostly from Ryota Ozaki: on receive, use bpf_mtap_softint(). On send, call awin_can_ifstart() through if_schedule_deferred_start(). To generate a diff of this commit: cvs rdiff -u -r1.1.2.6 -r1.1.2.7 src/sys/arch/arm/allwinner/awin_can.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/sys/arch/arm/allwinner/awin_can.c diff -u src/sys/arch/arm/allwinner/awin_can.c:1.1.2.6 src/sys/arch/arm/allwinner/awin_can.c:1.1.2.7 --- src/sys/arch/arm/allwinner/awin_can.c:1.1.2.6 Mon May 22 16:11:23 2017 +++ src/sys/arch/arm/allwinner/awin_can.c Thu May 25 18:23:15 2017 @@ -1,4 +1,4 @@ -/* $NetBSD: awin_can.c,v 1.1.2.6 2017/05/22 16:11:23 bouyer Exp $ */ +/* $NetBSD: awin_can.c,v 1.1.2.7 2017/05/25 18:23:15 bouyer Exp $ */ /*- * Copyright (c) 2017 The NetBSD Foundation, Inc. @@ -36,7 +36,7 @@ #include -__KERNEL_RCSID(1, "$NetBSD: awin_can.c,v 1.1.2.6 2017/05/22 16:11:23 bouyer Exp $"); +__KERNEL_RCSID(1, "$NetBSD: awin_can.c,v 1.1.2.7 2017/05/25 18:23:15 bouyer Exp $"); #include #include @@ -50,6 +50,7 @@ __KERNEL_RCSID(1, "$NetBSD: awin_can.c,v #include #include +#include #ifdef CAN #include @@ -209,6 +210,8 @@ awin_can_attach(device_t parent, device_ * Attach the interface. */ can_ifattach(ifp); + if_deferred_start_init(ifp, NULL); + bpf_mtap_softint_init(ifp); rnd_attach_source(>sc_rnd_source, device_xname(self), RND_TYPE_NET, RND_FLAG_DEFAULT); #ifdef MBUFTRACE @@ -275,7 +278,7 @@ awin_can_rx_intr(struct awin_can_softc * ifp->if_ipackets++; ifp->if_ibytes += m->m_len; m_set_rcvif(m, ifp); - can_bpf_mtap(ifp, m); + can_bpf_mtap(ifp, m, 1); can_input(ifp, m); } @@ -284,10 +287,6 @@ awin_can_tx_intr(struct awin_can_softc * { struct ifnet * const ifp = sc->sc_ifp; struct mbuf *m; - struct can_frame *cf; - int regd; - uint32_t reg0val; - int i; KASSERT(mutex_owned(>sc_intr_lock)); if ((m = sc->sc_m_transmit) != NULL) { @@ -298,58 +297,10 @@ awin_can_tx_intr(struct awin_can_softc * can_input(ifp, m); /* loopback */ sc->sc_m_transmit = NULL; ifp->if_timer = 0; - } - - IF_DEQUEUE(>if_snd, m); - - if (m == NULL) { ifp->if_flags &= ~IFF_OACTIVE; - return; - } - MCLAIM(m, ifp->if_mowner); - sc->sc_m_transmit = m; - - KASSERT((m->m_flags & M_PKTHDR) != 0); - KASSERT(m->m_len == m->m_pkthdr.len); - - cf = mtod(m, struct can_frame *); - reg0val = cf->can_dlc & AWIN_CAN_TXBUF0_DL; - if (cf->can_id & CAN_RTR_FLAG) - reg0val |= AWIN_CAN_TXBUF0_RTR; - - if (cf->can_id & CAN_EFF_FLAG) { - reg0val |= AWIN_CAN_TXBUF0_EFF; - awin_can_write(sc, AWIN_CAN_TXBUF1_REG, - (cf->can_id >> 21) & 0xff); - awin_can_write(sc, AWIN_CAN_TXBUF2_REG, - (cf->can_id >> 13) & 0xff); - awin_can_write(sc, AWIN_CAN_TXBUF3_REG, - (cf->can_id >> 5) & 0xff); - awin_can_write(sc, AWIN_CAN_TXBUF4_REG, - (cf->can_id << 3) & 0xf8); - regd = AWIN_CAN_TXBUF5_REG; - } else { - awin_can_write(sc, AWIN_CAN_TXBUF1_REG, - (cf->can_id >> 3) & 0xff); - awin_can_write(sc, AWIN_CAN_TXBUF2_REG, - (cf->can_id << 5) & 0xe0); - regd = AWIN_CAN_TXBUF3_REG; } - for (i = 0; i < cf->can_dlc; i++) { - awin_can_write(sc, regd + i * 4, cf->data[i]); - } - awin_can_write(sc, AWIN_CAN_TXBUF0_REG, reg0val); - - if (sc->sc_linkmodes & CAN_LINKMODE_LOOPBACK) { - awin_can_write(sc, AWIN_CAN_CMD_REG, - AWIN_CAN_CMD_TANS_REQ | AWIN_CAN_CMD_SELF_REQ); - } else { - awin_can_write(sc, AWIN_CAN_CMD_REG, AWIN_CAN_CMD_TANS_REQ); - } - ifp->if_flags |= IFF_OACTIVE; - ifp->if_timer = 5; - can_bpf_mtap(ifp, m); + if_schedule_deferred_start(ifp); } static int @@ -446,10 +397,66 @@ void awin_can_ifstart(struct ifnet *ifp) { struct awin_can_softc * const sc = ifp->if_softc; + struct mbuf *m; + struct can_frame *cf; + int regd; + uint32_t reg0val; + int i; mutex_enter(>sc_intr_lock); - KASSERT((ifp->if_flags & IFF_OACTIVE) == 0); - awin_can_tx_intr(sc); + if (ifp->if_flags & IFF_OACTIVE) + goto out; + + IF_DEQUEUE(>if_snd, m); + + if (m == NULL) + goto out; + + MCLAIM(m, ifp->if_mowner); + sc->sc_m_transmit = m; + + KASSERT((m->m_flags & M_PKTHDR) != 0); + KASSERT(m->m_len == m->m_pkthdr.len); + + cf = mtod(m, struct can_frame *); + reg0val = cf->can_dlc & AWIN_CAN_TXBUF0_DL; + if (cf->can_id & CAN_RTR_FLAG) + reg0val |= AWIN_CAN_TXBUF0_RTR; + + if (cf->can_id & CAN_EFF_FLAG) { + reg0val |= AWIN_CAN_TXBUF0_EFF; + awin_can_write(sc, AWIN_CAN_TXBUF1_REG, + (cf->can_id >> 21) & 0xff); + awin_can_write(sc, AWIN_CAN_TXBUF2_REG, + (cf->can_id >> 13) & 0xff); + awin_can_write(sc, AWIN_CAN_TXBUF3_REG, + (cf->can_id >> 5) & 0xff); + awin_can_write(sc, AWIN_CAN_TXBUF4_REG, + (cf->can_id << 3) & 0xf8); + regd =
CVS commit: [bouyer-socketcan] src/sys/netcan
Module Name:src Committed By: bouyer Date: Thu May 25 18:21:01 UTC 2017 Modified Files: src/sys/netcan [bouyer-socketcan]: can.c can_var.h if_canloop.c Log Message: Allow can_bpf_mtap() to call bpf_mtap_softint() when needed. To generate a diff of this commit: cvs rdiff -u -r1.1.2.14 -r1.1.2.15 src/sys/netcan/can.c cvs rdiff -u -r1.1.2.8 -r1.1.2.9 src/sys/netcan/can_var.h cvs rdiff -u -r1.1.2.7 -r1.1.2.8 src/sys/netcan/if_canloop.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/sys/netcan/can.c diff -u src/sys/netcan/can.c:1.1.2.14 src/sys/netcan/can.c:1.1.2.15 --- src/sys/netcan/can.c:1.1.2.14 Mon May 22 16:11:23 2017 +++ src/sys/netcan/can.c Thu May 25 18:21:00 2017 @@ -1,4 +1,4 @@ -/* $NetBSD: can.c,v 1.1.2.14 2017/05/22 16:11:23 bouyer Exp $ */ +/* $NetBSD: can.c,v 1.1.2.15 2017/05/25 18:21:00 bouyer Exp $ */ /*- * Copyright (c) 2003, 2017 The NetBSD Foundation, Inc. @@ -30,7 +30,7 @@ */ #include -__KERNEL_RCSID(0, "$NetBSD: can.c,v 1.1.2.14 2017/05/22 16:11:23 bouyer Exp $"); +__KERNEL_RCSID(0, "$NetBSD: can.c,v 1.1.2.15 2017/05/25 18:21:00 bouyer Exp $"); #include #include @@ -416,7 +416,7 @@ canintr(void) } void -can_bpf_mtap(struct ifnet *ifp, struct mbuf *m) +can_bpf_mtap(struct ifnet *ifp, struct mbuf *m, bool do_softint) { /* bpf wants the CAN id in network byte order */ struct can_frame *cf; @@ -425,7 +425,10 @@ can_bpf_mtap(struct ifnet *ifp, struct m cf = mtod(m, struct can_frame *); oid = cf->can_id; cf->can_id = htonl(oid); - bpf_mtap(ifp, m); + if (do_softint) + bpf_mtap_softint(ifp, m); + else + bpf_mtap(ifp, m); cf->can_id = oid; } Index: src/sys/netcan/can_var.h diff -u src/sys/netcan/can_var.h:1.1.2.8 src/sys/netcan/can_var.h:1.1.2.9 --- src/sys/netcan/can_var.h:1.1.2.8 Mon May 22 16:11:23 2017 +++ src/sys/netcan/can_var.h Thu May 25 18:21:00 2017 @@ -1,4 +1,4 @@ -/* $NetBSD: can_var.h,v 1.1.2.8 2017/05/22 16:11:23 bouyer Exp $ */ +/* $NetBSD: can_var.h,v 1.1.2.9 2017/05/25 18:21:00 bouyer Exp $ */ /*- * Copyright (c) 2003, 2017 The NetBSD Foundation, Inc. @@ -68,7 +68,7 @@ void *can_ctlinput(int, struct sockaddr int can_ctloutput(int, struct socket *, struct sockopt *); void can_init(void); void canintr(void); -void can_bpf_mtap(struct ifnet *, struct mbuf *); +void can_bpf_mtap(struct ifnet *, struct mbuf *, bool); #endif Index: src/sys/netcan/if_canloop.c diff -u src/sys/netcan/if_canloop.c:1.1.2.7 src/sys/netcan/if_canloop.c:1.1.2.8 --- src/sys/netcan/if_canloop.c:1.1.2.7 Mon May 22 16:11:23 2017 +++ src/sys/netcan/if_canloop.c Thu May 25 18:21:00 2017 @@ -1,4 +1,4 @@ -/* $NetBSD: if_canloop.c,v 1.1.2.7 2017/05/22 16:11:23 bouyer Exp $ */ +/* $NetBSD: if_canloop.c,v 1.1.2.8 2017/05/25 18:21:00 bouyer Exp $ */ /*- * Copyright (c) 2017 The NetBSD Foundation, Inc. @@ -35,7 +35,7 @@ */ #include -__KERNEL_RCSID(0, "$NetBSD: if_canloop.c,v 1.1.2.7 2017/05/22 16:11:23 bouyer Exp $"); +__KERNEL_RCSID(0, "$NetBSD: if_canloop.c,v 1.1.2.8 2017/05/25 18:21:00 bouyer Exp $"); #ifdef _KERNEL_OPT #include "opt_can.h" @@ -162,7 +162,7 @@ canloop_ifstart(struct ifnet *ifp) panic("canloop_output: no header mbuf"); m_set_rcvif(m, ifp); if (ifp->if_flags & IFF_LOOPBACK) - can_bpf_mtap(ifp, m); + can_bpf_mtap(ifp, m, 0); pktlen = m->m_pkthdr.len; ifp->if_opackets++;
CVS commit: [bouyer-socketcan] src/sys
Module Name:src Committed By: bouyer Date: Mon May 22 16:11:23 UTC 2017 Modified Files: src/sys/arch/arm/allwinner [bouyer-socketcan]: awin_can.c src/sys/netcan [bouyer-socketcan]: can.c can_var.h if_canloop.c Log Message: factor out if_attach() and bpf_attach() calls in can_ifattach(). Introduce can_ifdetach(). Introduce can_bpf_mtap(), which converts the can_id to network byte order (as required by tcpdump/wireshark). Thanks to Guy Harris for poiting this requirement. To generate a diff of this commit: cvs rdiff -u -r1.1.2.5 -r1.1.2.6 src/sys/arch/arm/allwinner/awin_can.c cvs rdiff -u -r1.1.2.13 -r1.1.2.14 src/sys/netcan/can.c cvs rdiff -u -r1.1.2.7 -r1.1.2.8 src/sys/netcan/can_var.h cvs rdiff -u -r1.1.2.6 -r1.1.2.7 src/sys/netcan/if_canloop.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/sys/arch/arm/allwinner/awin_can.c diff -u src/sys/arch/arm/allwinner/awin_can.c:1.1.2.5 src/sys/arch/arm/allwinner/awin_can.c:1.1.2.6 --- src/sys/arch/arm/allwinner/awin_can.c:1.1.2.5 Fri Apr 21 13:08:55 2017 +++ src/sys/arch/arm/allwinner/awin_can.c Mon May 22 16:11:23 2017 @@ -1,4 +1,4 @@ -/* $NetBSD: awin_can.c,v 1.1.2.5 2017/04/21 13:08:55 bouyer Exp $ */ +/* $NetBSD: awin_can.c,v 1.1.2.6 2017/05/22 16:11:23 bouyer Exp $ */ /*- * Copyright (c) 2017 The NetBSD Foundation, Inc. @@ -36,7 +36,7 @@ #include -__KERNEL_RCSID(1, "$NetBSD: awin_can.c,v 1.1.2.5 2017/04/21 13:08:55 bouyer Exp $"); +__KERNEL_RCSID(1, "$NetBSD: awin_can.c,v 1.1.2.6 2017/05/22 16:11:23 bouyer Exp $"); #include #include @@ -55,7 +55,6 @@ __KERNEL_RCSID(1, "$NetBSD: awin_can.c,v #include #include #endif -#include #include #include @@ -209,9 +208,7 @@ awin_can_attach(device_t parent, device_ /* * Attach the interface. */ - if_attach(ifp); can_ifattach(ifp); - bpf_attach(ifp, DLT_CAN_SOCKETCAN, 0); rnd_attach_source(>sc_rnd_source, device_xname(self), RND_TYPE_NET, RND_FLAG_DEFAULT); #ifdef MBUFTRACE @@ -278,7 +275,7 @@ awin_can_rx_intr(struct awin_can_softc * ifp->if_ipackets++; ifp->if_ibytes += m->m_len; m_set_rcvif(m, ifp); - bpf_mtap(ifp, m); + can_bpf_mtap(ifp, m); can_input(ifp, m); } @@ -352,7 +349,7 @@ awin_can_tx_intr(struct awin_can_softc * } ifp->if_flags |= IFF_OACTIVE; ifp->if_timer = 5; - bpf_mtap(ifp, m); + can_bpf_mtap(ifp, m); } static int Index: src/sys/netcan/can.c diff -u src/sys/netcan/can.c:1.1.2.13 src/sys/netcan/can.c:1.1.2.14 --- src/sys/netcan/can.c:1.1.2.13 Sun Apr 23 21:05:09 2017 +++ src/sys/netcan/can.c Mon May 22 16:11:23 2017 @@ -1,4 +1,4 @@ -/* $NetBSD: can.c,v 1.1.2.13 2017/04/23 21:05:09 bouyer Exp $ */ +/* $NetBSD: can.c,v 1.1.2.14 2017/05/22 16:11:23 bouyer Exp $ */ /*- * Copyright (c) 2003, 2017 The NetBSD Foundation, Inc. @@ -30,7 +30,7 @@ */ #include -__KERNEL_RCSID(0, "$NetBSD: can.c,v 1.1.2.13 2017/04/23 21:05:09 bouyer Exp $"); +__KERNEL_RCSID(0, "$NetBSD: can.c,v 1.1.2.14 2017/05/22 16:11:23 bouyer Exp $"); #include #include @@ -48,6 +48,7 @@ __KERNEL_RCSID(0, "$NetBSD: can.c,v 1.1. #include #include #include +#include #include #include @@ -192,7 +193,9 @@ can_purgeif(struct socket *so, struct if } void -can_ifattach(struct ifnet *ifp) { +can_ifattach(struct ifnet *ifp) +{ + if_attach(ifp); ifp->if_mtu = sizeof(struct can_frame); ifp->if_type = IFT_OTHER; ifp->if_hdrlen = 0; @@ -201,6 +204,14 @@ can_ifattach(struct ifnet *ifp) { ifp->if_output = NULL; /* unused */ IFQ_SET_READY(>if_snd); if_alloc_sadl(ifp); + bpf_attach(ifp, DLT_CAN_SOCKETCAN, 0); +} + +void +can_ifdetach(struct ifnet *ifp) +{ + bpf_detach(ifp); + if_detach(ifp); } void @@ -404,6 +415,20 @@ canintr(void) mutex_exit(softnet_lock); } +void +can_bpf_mtap(struct ifnet *ifp, struct mbuf *m) +{ + /* bpf wants the CAN id in network byte order */ + struct can_frame *cf; + canid_t oid; + + cf = mtod(m, struct can_frame *); + oid = cf->can_id; + cf->can_id = htonl(oid); + bpf_mtap(ifp, m); + cf->can_id = oid; +} + static int can_attach(struct socket *so, int proto) { Index: src/sys/netcan/can_var.h diff -u src/sys/netcan/can_var.h:1.1.2.7 src/sys/netcan/can_var.h:1.1.2.8 --- src/sys/netcan/can_var.h:1.1.2.7 Tue Apr 18 21:29:40 2017 +++ src/sys/netcan/can_var.h Mon May 22 16:11:23 2017 @@ -1,4 +1,4 @@ -/* $NetBSD: can_var.h,v 1.1.2.7 2017/04/18 21:29:40 bouyer Exp $ */ +/* $NetBSD: can_var.h,v 1.1.2.8 2017/05/22 16:11:23 bouyer Exp $ */ /*- * Copyright (c) 2003, 2017 The NetBSD Foundation, Inc. @@ -60,6 +60,7 @@ extern struct domain candomain; extern const struct pr_usrreqs can_usrreqs; void can_ifattach(struct ifnet *); +void can_ifdetach(struct ifnet *); void can_ifinit_timings(struct canif_softc *); void can_mbuf_tag_clean(struct mbuf *); void can_input(struct ifnet *, struct mbuf *); @@ -67,6 +68,7 @@ void *can_ctlinput(int, struct sockaddr int can_ctloutput(int, struct
CVS commit: [bouyer-socketcan] src/sys/dev
Module Name:src Committed By: bouyer Date: Wed Apr 26 11:45:23 UTC 2017 Modified Files: src/sys/dev [bouyer-socketcan]: audio.c Log Message: Sync with HEAD To generate a diff of this commit: cvs rdiff -u -r1.291.2.2 -r1.291.2.3 src/sys/dev/audio.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/sys/dev/audio.c diff -u src/sys/dev/audio.c:1.291.2.2 src/sys/dev/audio.c:1.291.2.3 --- src/sys/dev/audio.c:1.291.2.2 Wed Apr 26 10:10:04 2017 +++ src/sys/dev/audio.c Wed Apr 26 11:45:23 2017 @@ -1,4 +1,4 @@ -/* $NetBSD: audio.c,v 1.291.2.2 2017/04/26 10:10:04 bouyer Exp $ */ +/* $NetBSD: audio.c,v 1.291.2.3 2017/04/26 11:45:23 bouyer Exp $ */ /*- * Copyright (c) 2016 Nathanial Sloss@@ -148,7 +148,7 @@ */ #include -__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.291.2.2 2017/04/26 10:10:04 bouyer Exp $"); +__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.291.2.3 2017/04/26 11:45:23 bouyer Exp $"); #include "audio.h" #if NAUDIO > 0 @@ -649,8 +649,6 @@ bad_rec: "failed\n", __func__); } - sc->sc_pr.blksize = vc->sc_mpr.blksize; - sc->sc_rr.blksize = vc->sc_mrr.blksize; sc->sc_sih_rd = softint_establish(SOFTINT_SERIAL | SOFTINT_MPSAFE, audio_softintr_rd, sc); sc->sc_sih_wr = softint_establish(SOFTINT_SERIAL | SOFTINT_MPSAFE, @@ -1972,6 +1970,8 @@ audio_initbufs(struct audio_softc *sc, s if (vc == NULL) { chan = SIMPLEQ_FIRST(>sc_audiochan); vc = chan->vc; + sc->sc_pr.blksize = vc->sc_mrr.blksize; + sc->sc_rr.blksize = vc->sc_mrr.blksize; } DPRINTF(("audio_initbufs: mode=0x%x\n", vc->sc_mode)); @@ -1987,6 +1987,8 @@ audio_initbufs(struct audio_softc *sc, s return error; } } + if (vc == SIMPLEQ_FIRST(>sc_audiochan)->vc) + sc->sc_rr.blksize = vc->sc_mrr.blksize; if (audio_can_playback(sc) || (vc->sc_open & AUOPEN_WRITE)) { audio_init_ringbuffer(sc, >sc_mpr, @@ -2000,6 +2002,8 @@ audio_initbufs(struct audio_softc *sc, s return error; } } + if (vc == SIMPLEQ_FIRST(>sc_audiochan)->vc) + sc->sc_pr.blksize = vc->sc_mpr.blksize; #ifdef AUDIO_INTR_TIME #define double u_long @@ -2142,9 +2146,9 @@ audio_open(dev_t dev, struct audio_softc return error; } } + audio_initbufs(sc, NULL); audio_init_ringbuffer(sc, >sc_pr, AUMODE_PLAY); audio_init_ringbuffer(sc, >sc_rr, AUMODE_RECORD); - audio_initbufs(sc, NULL); sc->schedule_wih = false; sc->schedule_rih = false; sc->sc_eof = 0; @@ -2177,8 +2181,8 @@ audio_open(dev_t dev, struct audio_softc mode |= AUMODE_PLAY | AUMODE_PLAY_ALL; } - vc->sc_mrr.blksize = sc->sc_rr.blksize; vc->sc_mpr.blksize = sc->sc_pr.blksize; + vc->sc_mrr.blksize = sc->sc_rr.blksize; /* * Multiplex device: /dev/audio (MU-Law) and /dev/sound (linear) @@ -3583,7 +3587,7 @@ audio_pint(void *v) struct audio_softc *sc; struct audio_chan *chan; struct virtual_channel *vc; - int blksize; + int blksize, cc, used; sc = v; chan = SIMPLEQ_FIRST(>sc_audiochan); @@ -3602,10 +3606,16 @@ audio_pint(void *v) vc->sc_mpr.s.outp, blksize); if (audio_stream_get_used(>sc_pr.s) < blksize) { - audio_fill_silence(>sc_pparams, vc->sc_mpr.s.inp, - vc->sc_mpr.blksize); - vc->sc_mpr.s.inp = audio_stream_add_inp(>sc_mpr.s, - vc->sc_mpr.s.inp, blksize); + used = blksize; + while (used > 0) { + cc = sc->sc_pr.s.end - sc->sc_pr.s.inp; + if (cc > used) +cc = used; + audio_fill_silence(>sc_pparams, vc->sc_mpr.s.inp, cc); + vc->sc_mpr.s.inp = audio_stream_add_inp(>sc_mpr.s, + vc->sc_mpr.s.inp, cc); + used -= cc; + } goto wake_mix; } @@ -3786,8 +3796,10 @@ audio_mix(void *v) inp = cb->s.inp; cc = blksize - (inp - cb->s.start) % blksize; if (sc->sc_writeme == false) - audio_pint_silence(sc, cb, inp, cc, vc); - cb->s.inp = audio_stream_add_inp(>s, cb->s.inp, blksize); + audio_fill_silence(>sc_mpr.s.param, inp, cc); + else + cc = blksize; + cb->s.inp = audio_stream_add_inp(>s, cb->s.inp, cc); mutex_exit(sc->sc_intr_lock); kpreempt_disable(); @@ -4090,9 +4102,6 @@ audio_set_vchan_defaults(struct audio_so if (error == 0) error = audiosetinfo(sc, , true, vc); - sc->sc_pr.blksize = vc->sc_mpr.blksize; - sc->sc_rr.blksize = vc->sc_mrr.blksize; - return error; } @@ -4432,7 +4441,6 @@ audiosetinfo(struct audio_softc *sc, str int error; int np, nr; unsigned int blks; - int oldpblksize, oldrblksize; u_int gain; bool rbus, pbus; bool cleared, modechange, pausechange; @@ -4511,9 +4519,6 @@ audiosetinfo(struct audio_softc *sc, str if (np > 0 && (error = audio_check_params())) return error; - oldpblksize = vc->sc_mpr.blksize; - oldrblksize = vc->sc_mrr.blksize; - setmode = 0; if (nr > 0) { if (!cleared) { @@ -4698,50 +4703,6 @@ audiosetinfo(struct audio_softc *sc, str pausechange = true; } - if (SPECIFIED(ai->blocksize)) { - int pblksize, rblksize; - - /* Block size specified
CVS commit: [bouyer-socketcan] src/sys/dev
Module Name:src Committed By: bouyer Date: Wed Apr 26 10:10:05 UTC 2017 Modified Files: src/sys/dev [bouyer-socketcan]: audio.c Log Message: SYnc with HEAD To generate a diff of this commit: cvs rdiff -u -r1.291.2.1 -r1.291.2.2 src/sys/dev/audio.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/sys/dev/audio.c diff -u src/sys/dev/audio.c:1.291.2.1 src/sys/dev/audio.c:1.291.2.2 --- src/sys/dev/audio.c:1.291.2.1 Fri Apr 21 16:53:44 2017 +++ src/sys/dev/audio.c Wed Apr 26 10:10:04 2017 @@ -1,4 +1,4 @@ -/* $NetBSD: audio.c,v 1.291.2.1 2017/04/21 16:53:44 bouyer Exp $ */ +/* $NetBSD: audio.c,v 1.291.2.2 2017/04/26 10:10:04 bouyer Exp $ */ /*- * Copyright (c) 2016 Nathanial Sloss@@ -148,7 +148,7 @@ */ #include -__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.291.2.1 2017/04/21 16:53:44 bouyer Exp $"); +__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.291.2.2 2017/04/26 10:10:04 bouyer Exp $"); #include "audio.h" #if NAUDIO > 0 @@ -3598,12 +3598,17 @@ audio_pint(void *v) cv_broadcast(>sc_wchan); } - if (audio_stream_get_used(>sc_pr.s) < blksize) - goto wake_mix; - vc->sc_mpr.s.outp = audio_stream_add_outp(>sc_mpr.s, vc->sc_mpr.s.outp, blksize); + if (audio_stream_get_used(>sc_pr.s) < blksize) { + audio_fill_silence(>sc_pparams, vc->sc_mpr.s.inp, + vc->sc_mpr.blksize); + vc->sc_mpr.s.inp = audio_stream_add_inp(>sc_mpr.s, + vc->sc_mpr.s.inp, blksize); + goto wake_mix; + } + mix_write(sc); wake_mix:
CVS commit: [bouyer-socketcan] src/sys/netcan
Module Name:src Committed By: bouyer Date: Mon Apr 24 13:38:33 UTC 2017 Modified Files: src/sys/netcan [bouyer-socketcan]: if_canloop.c Log Message: Use bpf_mtap() not bpf_mtap_af(). Now files produced by tcpdump can be decoded by wireshark. To generate a diff of this commit: cvs rdiff -u -r1.1.2.5 -r1.1.2.6 src/sys/netcan/if_canloop.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/sys/netcan/if_canloop.c diff -u src/sys/netcan/if_canloop.c:1.1.2.5 src/sys/netcan/if_canloop.c:1.1.2.6 --- src/sys/netcan/if_canloop.c:1.1.2.5 Wed Apr 19 22:17:56 2017 +++ src/sys/netcan/if_canloop.c Mon Apr 24 13:38:33 2017 @@ -1,4 +1,4 @@ -/* $NetBSD: if_canloop.c,v 1.1.2.5 2017/04/19 22:17:56 bouyer Exp $ */ +/* $NetBSD: if_canloop.c,v 1.1.2.6 2017/04/24 13:38:33 bouyer Exp $ */ /*- * Copyright (c) 2017 The NetBSD Foundation, Inc. @@ -35,7 +35,7 @@ */ #include -__KERNEL_RCSID(0, "$NetBSD: if_canloop.c,v 1.1.2.5 2017/04/19 22:17:56 bouyer Exp $"); +__KERNEL_RCSID(0, "$NetBSD: if_canloop.c,v 1.1.2.6 2017/04/24 13:38:33 bouyer Exp $"); #ifdef _KERNEL_OPT #include "opt_can.h" @@ -166,7 +166,7 @@ canloop_ifstart(struct ifnet *ifp) panic("canloop_output: no header mbuf"); m_set_rcvif(m, ifp); if (ifp->if_flags & IFF_LOOPBACK) - bpf_mtap_af(ifp, AF_CAN, m); + bpf_mtap(ifp, m); pktlen = m->m_pkthdr.len; ifp->if_opackets++;
CVS commit: [bouyer-socketcan] src/sys/netcan
Module Name:src Committed By: bouyer Date: Sun Apr 23 21:05:09 UTC 2017 Modified Files: src/sys/netcan [bouyer-socketcan]: can.c can_pcb.c can_pcb.h Log Message: Add locking and refcounting to canpcb. Store the canpcb in the in the mbuf tag on send instead of the socket's address. This should protect against a race where the socket cloud be closed before we get back the mbuf from the adapter's driver. To generate a diff of this commit: cvs rdiff -u -r1.1.2.12 -r1.1.2.13 src/sys/netcan/can.c cvs rdiff -u -r1.1.2.3 -r1.1.2.4 src/sys/netcan/can_pcb.c \ src/sys/netcan/can_pcb.h Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/sys/netcan/can.c diff -u src/sys/netcan/can.c:1.1.2.12 src/sys/netcan/can.c:1.1.2.13 --- src/sys/netcan/can.c:1.1.2.12 Thu Apr 20 17:29:10 2017 +++ src/sys/netcan/can.c Sun Apr 23 21:05:09 2017 @@ -1,4 +1,4 @@ -/* $NetBSD: can.c,v 1.1.2.12 2017/04/20 17:29:10 bouyer Exp $ */ +/* $NetBSD: can.c,v 1.1.2.13 2017/04/23 21:05:09 bouyer Exp $ */ /*- * Copyright (c) 2003, 2017 The NetBSD Foundation, Inc. @@ -30,7 +30,7 @@ */ #include -__KERNEL_RCSID(0, "$NetBSD: can.c,v 1.1.2.12 2017/04/20 17:29:10 bouyer Exp $"); +__KERNEL_RCSID(0, "$NetBSD: can.c,v 1.1.2.13 2017/04/23 21:05:09 bouyer Exp $"); #include #include @@ -235,7 +235,10 @@ can_output(struct mbuf *m, struct canpcb ifp->if_oerrors++; return ENOMEM; } - *(struct socket **)(sotag + 1) = canp->canp_socket; + mutex_enter(>canp_mtx); + canp_ref(canp); + mutex_exit(>canp_mtx); + *(struct canpcb **)(sotag + 1) = canp; m_tag_prepend(m, sotag); if (m->m_len <= ifp->if_mtu) { @@ -302,7 +305,6 @@ canintr(void) struct sockaddr_can from; struct canpcb *canp; struct m_tag *sotag; - struct socket *so; struct canpcb *sender_canp; mutex_enter(softnet_lock); @@ -319,13 +321,13 @@ canintr(void) #endif sotag = m_tag_find(m, PACKET_TAG_SO, NULL); if (sotag) { - so = *(struct socket **)(sotag + 1); - sender_canp = sotocanpcb(so); + sender_canp = *(struct canpcb **)(sotag + 1); m_tag_delete(m, sotag); + KASSERT(sender_canp != NULL); /* if the sender doesn't want loopback, don't do it */ - if (sender_canp && - (sender_canp->canp_flags & CANP_NO_LOOPBACK) != 0) { + if ((sender_canp->canp_flags & CANP_NO_LOOPBACK) != 0) { m_freem(m); +canp_unref(sender_canp); continue; } } else { @@ -340,19 +342,31 @@ canintr(void) TAILQ_FOREACH(canp, _queue, canp_queue) { struct mbuf *mc; + mutex_enter(>canp_mtx); + /* skip if we're detached */ + if (canp->canp_state == CANP_DETACHED) { +mutex_exit(>canp_mtx); +continue; + } + /* don't loop back to sockets on other interfaces */ if (canp->canp_ifp != NULL && canp->canp_ifp->if_index != rcv_ifindex) { +mutex_exit(>canp_mtx); continue; } /* don't loop back to myself if I don't want it */ if (canp == sender_canp && - (canp->canp_flags & CANP_RECEIVE_OWN) == 0) + (canp->canp_flags & CANP_RECEIVE_OWN) == 0) { +mutex_exit(>canp_mtx); continue; + } /* skip if the accept filter doen't match this pkt */ - if (!can_pcbfilter(canp, m)) + if (!can_pcbfilter(canp, m)) { +mutex_exit(>canp_mtx); continue; + } if (TAILQ_NEXT(canp, canp_queue) != NULL) { /* @@ -375,9 +389,13 @@ canintr(void) m_freem(mc); } else sorwakeup(canp->canp_socket); + mutex_exit(>canp_mtx); if (m == NULL) break; } + if (sender_canp) { + canp_unref(sender_canp); + } /* If it didn't go anywhere just delete it */ if (m) { m_freem(m); @@ -492,7 +510,6 @@ can_disconnect(struct socket *so) /*soisdisconnected(so);*/ so->so_state &= ~SS_ISCONNECTED; /* XXX */ can_pcbdisconnect(canp); - can_pcbstate(canp, CANP_BOUND); /* XXX */ return 0; } @@ -877,7 +894,9 @@ can_raw_setop(struct canpcb *canp, struc int nfilters = sopt->sopt_size / sizeof(struct can_filter); if (sopt->sopt_size % sizeof(struct can_filter) != 0) return EINVAL; + mutex_enter(>canp_mtx); error = can_pcbsetfilter(canp, sopt->sopt_data, nfilters); + mutex_exit(>canp_mtx); break; } default: Index: src/sys/netcan/can_pcb.c diff -u src/sys/netcan/can_pcb.c:1.1.2.3 src/sys/netcan/can_pcb.c:1.1.2.4 --- src/sys/netcan/can_pcb.c:1.1.2.3 Sun Feb 5 19:44:53 2017 +++ src/sys/netcan/can_pcb.c Sun Apr 23 21:05:09 2017 @@ -1,4 +1,4 @@ -/* $NetBSD: can_pcb.c,v 1.1.2.3 2017/02/05 19:44:53 bouyer Exp $ */ +/* $NetBSD: can_pcb.c,v 1.1.2.4 2017/04/23 21:05:09 bouyer Exp $ */ /*- * Copyright (c) 2003, 2017 The NetBSD Foundation, Inc. @@ -30,7 +30,7 @@ */ #include -__KERNEL_RCSID(0, "$NetBSD: can_pcb.c,v 1.1.2.3 2017/02/05 19:44:53 bouyer Exp $"); +__KERNEL_RCSID(0, "$NetBSD: can_pcb.c,v 1.1.2.4 2017/04/23 21:05:09 bouyer Exp $"); #include #include @@ -106,12 +106,14 @@ can_pcballoc(struct socket *so, void
CVS commit: [bouyer-socketcan] src/sys/rump/librump/rumpkern
Module Name:src Committed By: bouyer Date: Sun Apr 23 11:17:25 UTC 2017 Modified Files: src/sys/rump/librump/rumpkern [bouyer-socketcan]: threads.c Log Message: Sync with HEAD To generate a diff of this commit: cvs rdiff -u -r1.24.4.1 -r1.24.4.2 src/sys/rump/librump/rumpkern/threads.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/sys/rump/librump/rumpkern/threads.c diff -u src/sys/rump/librump/rumpkern/threads.c:1.24.4.1 src/sys/rump/librump/rumpkern/threads.c:1.24.4.2 --- src/sys/rump/librump/rumpkern/threads.c:1.24.4.1 Fri Apr 21 16:54:07 2017 +++ src/sys/rump/librump/rumpkern/threads.c Sun Apr 23 11:17:25 2017 @@ -1,4 +1,4 @@ -/* $NetBSD: threads.c,v 1.24.4.1 2017/04/21 16:54:07 bouyer Exp $ */ +/* $NetBSD: threads.c,v 1.24.4.2 2017/04/23 11:17:25 bouyer Exp $ */ /* * Copyright (c) 2007-2009 Antti Kantee. All Rights Reserved. @@ -29,7 +29,7 @@ */ #include -__KERNEL_RCSID(0, "$NetBSD: threads.c,v 1.24.4.1 2017/04/21 16:54:07 bouyer Exp $"); +__KERNEL_RCSID(0, "$NetBSD: threads.c,v 1.24.4.2 2017/04/23 11:17:25 bouyer Exp $"); #include #include @@ -294,7 +294,7 @@ lwpbouncer(void *arg) int lwp_create(struct lwp *l1, struct proc *p2, vaddr_t uaddr, int flags, void *stack, size_t stacksize, void (*func)(void *), void *arg, -struct lwp **newlwpp, int sclass, const sigmask_t *sigmask, +struct lwp **newlwpp, int sclass, const sigset_t *sigmask, const stack_t *sigstk) { struct thrdesc *td;
CVS commit: [bouyer-socketcan] src/sys/arch/arm/allwinner
Module Name:src Committed By: bouyer Date: Fri Apr 21 13:08:55 UTC 2017 Modified Files: src/sys/arch/arm/allwinner [bouyer-socketcan]: awin_can.c Log Message: can4linux code, and experiments, shows that the CAN clock frequency is AWIN_REF_FREQ, not AWIN_REF_FREQ / 2 as the A20 doc suggests. Always set the mbuf len to CAN_MTU, this is what linux does. Set accept filters and clear errors counters on ifconfig up. We can now send and receive CAN frames. To generate a diff of this commit: cvs rdiff -u -r1.1.2.4 -r1.1.2.5 src/sys/arch/arm/allwinner/awin_can.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/sys/arch/arm/allwinner/awin_can.c diff -u src/sys/arch/arm/allwinner/awin_can.c:1.1.2.4 src/sys/arch/arm/allwinner/awin_can.c:1.1.2.5 --- src/sys/arch/arm/allwinner/awin_can.c:1.1.2.4 Thu Apr 20 17:30:52 2017 +++ src/sys/arch/arm/allwinner/awin_can.c Fri Apr 21 13:08:55 2017 @@ -1,4 +1,4 @@ -/* $NetBSD: awin_can.c,v 1.1.2.4 2017/04/20 17:30:52 bouyer Exp $ */ +/* $NetBSD: awin_can.c,v 1.1.2.5 2017/04/21 13:08:55 bouyer Exp $ */ /*- * Copyright (c) 2017 The NetBSD Foundation, Inc. @@ -36,7 +36,7 @@ #include -__KERNEL_RCSID(1, "$NetBSD: awin_can.c,v 1.1.2.4 2017/04/20 17:30:52 bouyer Exp $"); +__KERNEL_RCSID(1, "$NetBSD: awin_can.c,v 1.1.2.5 2017/04/21 13:08:55 bouyer Exp $"); #include #include @@ -168,7 +168,7 @@ awin_can_attach(device_t parent, device_ sc->sc_timecaps.cltc_brp_min = 1; sc->sc_timecaps.cltc_brp_max = 64; sc->sc_timecaps.cltc_brp_inc = 1; - sc->sc_timecaps.cltc_clock_freq = AWIN_REF_FREQ / 2; + sc->sc_timecaps.cltc_clock_freq = AWIN_REF_FREQ; sc->sc_timecaps.cltc_linkmode_caps = CAN_LINKMODE_3SAMPLES | CAN_LINKMODE_LISTENONLY | CAN_LINKMODE_LOOPBACK; @@ -274,13 +274,11 @@ awin_can_rx_intr(struct awin_can_softc * } } awin_can_write(sc, AWIN_CAN_CMD_REG, AWIN_CAN_CMD_REL_RX_BUF); - m->m_len = m->m_pkthdr.len = - offsetof(struct can_frame, data[0]) + cf->can_dlc; + m->m_len = m->m_pkthdr.len = CAN_MTU; ifp->if_ipackets++; ifp->if_ibytes += m->m_len; m_set_rcvif(m, ifp); bpf_mtap(ifp, m); - printf("can_input1\n"); can_input(ifp, m); } @@ -300,7 +298,6 @@ awin_can_tx_intr(struct awin_can_softc * ifp->if_opackets++; can_mbuf_tag_clean(m); m_set_rcvif(m, ifp); - printf("can_input2\n"); can_input(ifp, m); /* loopback */ sc->sc_m_transmit = NULL; ifp->if_timer = 0; @@ -503,6 +500,14 @@ awin_can_ifup(struct awin_can_softc * co awin_can_write(sc, AWIN_CAN_BUS_TIME_REG, reg); + /* set filters to accept all frames */ + awin_can_write(sc, AWIN_CAN_ACPC, 0x); + awin_can_write(sc, AWIN_CAN_ACPM, 0x); + + /* clear errors counter */ + awin_can_write(sc, AWIN_CAN_REC_REG, 0); + + /* leave reset mode and enable interrupts */ awin_can_exit_reset(sc); awin_can_write(sc, AWIN_CAN_INTE_REG, AWIN_CAN_INT_TX_FLAG | AWIN_CAN_INT_RX_FLAG | AWIN_CAN_INT_ALLERRS);
CVS commit: [bouyer-socketcan] src/sys/arch/arm/allwinner
Module Name:src Committed By: bouyer Date: Thu Apr 20 17:30:52 UTC 2017 Modified Files: src/sys/arch/arm/allwinner [bouyer-socketcan]: awin_can.c Log Message: Print some informations for error interrupts. Fix some registers values. Now we can transmit frames. Receive still doesn't work. To generate a diff of this commit: cvs rdiff -u -r1.1.2.3 -r1.1.2.4 src/sys/arch/arm/allwinner/awin_can.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/sys/arch/arm/allwinner/awin_can.c diff -u src/sys/arch/arm/allwinner/awin_can.c:1.1.2.3 src/sys/arch/arm/allwinner/awin_can.c:1.1.2.4 --- src/sys/arch/arm/allwinner/awin_can.c:1.1.2.3 Thu Apr 20 13:00:52 2017 +++ src/sys/arch/arm/allwinner/awin_can.c Thu Apr 20 17:30:52 2017 @@ -1,4 +1,4 @@ -/* $NetBSD: awin_can.c,v 1.1.2.3 2017/04/20 13:00:52 bouyer Exp $ */ +/* $NetBSD: awin_can.c,v 1.1.2.4 2017/04/20 17:30:52 bouyer Exp $ */ /*- * Copyright (c) 2017 The NetBSD Foundation, Inc. @@ -36,7 +36,7 @@ #include -__KERNEL_RCSID(1, "$NetBSD: awin_can.c,v 1.1.2.3 2017/04/20 13:00:52 bouyer Exp $"); +__KERNEL_RCSID(1, "$NetBSD: awin_can.c,v 1.1.2.4 2017/04/20 17:30:52 bouyer Exp $"); #include #include @@ -303,6 +303,7 @@ awin_can_tx_intr(struct awin_can_softc * printf("can_input2\n"); can_input(ifp, m); /* loopback */ sc->sc_m_transmit = NULL; + ifp->if_timer = 0; } IF_DEQUEUE(>if_snd, m); @@ -382,13 +383,17 @@ awin_can_err_intr(struct awin_can_softc struct ifnet * const ifp = sc->sc_ifp; KASSERT(mutex_owned(>sc_intr_lock)); int txerr = 0; + uint32_t reg; if (irq & AWIN_CAN_INT_DATA_OR) { ifp->if_ierrors++; awin_can_write(sc, AWIN_CAN_CMD_REG, AWIN_CAN_CMD_CLR_OR); } if (irq & AWIN_CAN_INT_ERR) { - /* XXX todo */ + reg = awin_can_read(sc, AWIN_CAN_REC_REG); + printf("%s: ERR interrupt status 0x%x counters 0x%x\n", + device_xname(sc->sc_dev), sts, reg); + } if (irq & AWIN_CAN_INT_BERR) { if (sts & AWIN_CAN_STA_TX) @@ -397,7 +402,8 @@ awin_can_err_intr(struct awin_can_softc ifp->if_ierrors++; } if (irq & AWIN_CAN_INT_ERR_PASSIVE) { - /* XXX todo */ + printf("%s: PASSV interrupt status 0x%x\n", + device_xname(sc->sc_dev), sts); } if (irq & AWIN_CAN_INT_ARB_LOST) { txerr++; @@ -459,7 +465,7 @@ awin_can_ifup(struct awin_can_softc * co uint32_t reg; /* setup timings and mode - has to be done in reset */ - reg = 0; + reg = AWIN_CAN_MODSEL_RST; if (sc->sc_linkmodes & CAN_LINKMODE_LISTENONLY) reg |= AWIN_CAN_MODSEL_LST_ONLY;
CVS commit: [bouyer-socketcan] src/sys/netcan
Module Name:src Committed By: bouyer Date: Thu Apr 20 17:29:10 UTC 2017 Modified Files: src/sys/netcan [bouyer-socketcan]: can.c Log Message: If a packet was sent using sendto, the socket pointed to by the tag may be unbound. Check for this in canintr(). XXX possibly the socket can completely dissapear before we get here. This needs to be revisited. To generate a diff of this commit: cvs rdiff -u -r1.1.2.11 -r1.1.2.12 src/sys/netcan/can.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/sys/netcan/can.c diff -u src/sys/netcan/can.c:1.1.2.11 src/sys/netcan/can.c:1.1.2.12 --- src/sys/netcan/can.c:1.1.2.11 Thu Apr 20 12:59:11 2017 +++ src/sys/netcan/can.c Thu Apr 20 17:29:10 2017 @@ -1,4 +1,4 @@ -/* $NetBSD: can.c,v 1.1.2.11 2017/04/20 12:59:11 bouyer Exp $ */ +/* $NetBSD: can.c,v 1.1.2.12 2017/04/20 17:29:10 bouyer Exp $ */ /*- * Copyright (c) 2003, 2017 The NetBSD Foundation, Inc. @@ -30,7 +30,7 @@ */ #include -__KERNEL_RCSID(0, "$NetBSD: can.c,v 1.1.2.11 2017/04/20 12:59:11 bouyer Exp $"); +__KERNEL_RCSID(0, "$NetBSD: can.c,v 1.1.2.12 2017/04/20 17:29:10 bouyer Exp $"); #include #include @@ -323,7 +323,8 @@ canintr(void) sender_canp = sotocanpcb(so); m_tag_delete(m, sotag); /* if the sender doesn't want loopback, don't do it */ - if (sender_canp->canp_flags & CANP_NO_LOOPBACK) { + if (sender_canp && + (sender_canp->canp_flags & CANP_NO_LOOPBACK) != 0) { m_freem(m); continue; }
CVS commit: [bouyer-socketcan] src/sys/arch/arm/allwinner
Module Name:src Committed By: bouyer Date: Thu Apr 20 13:00:52 UTC 2017 Modified Files: src/sys/arch/arm/allwinner [bouyer-socketcan]: awin_can.c Log Message: Handle interface errors. Use a watchdog for transmit. To generate a diff of this commit: cvs rdiff -u -r1.1.2.2 -r1.1.2.3 src/sys/arch/arm/allwinner/awin_can.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/sys/arch/arm/allwinner/awin_can.c diff -u src/sys/arch/arm/allwinner/awin_can.c:1.1.2.2 src/sys/arch/arm/allwinner/awin_can.c:1.1.2.3 --- src/sys/arch/arm/allwinner/awin_can.c:1.1.2.2 Wed Apr 19 17:54:18 2017 +++ src/sys/arch/arm/allwinner/awin_can.c Thu Apr 20 13:00:52 2017 @@ -1,4 +1,4 @@ -/* $NetBSD: awin_can.c,v 1.1.2.2 2017/04/19 17:54:18 bouyer Exp $ */ +/* $NetBSD: awin_can.c,v 1.1.2.3 2017/04/20 13:00:52 bouyer Exp $ */ /*- * Copyright (c) 2017 The NetBSD Foundation, Inc. @@ -36,7 +36,7 @@ #include -__KERNEL_RCSID(1, "$NetBSD: awin_can.c,v 1.1.2.2 2017/04/19 17:54:18 bouyer Exp $"); +__KERNEL_RCSID(1, "$NetBSD: awin_can.c,v 1.1.2.3 2017/04/20 13:00:52 bouyer Exp $"); #include #include @@ -60,6 +60,15 @@ __KERNEL_RCSID(1, "$NetBSD: awin_can.c,v #include #include +/* shortcut for all error interrupts */ +#define AWIN_CAN_INT_ALLERRS (\ + AWIN_CAN_INT_BERR | \ + AWIN_CAN_INT_ARB_LOST | \ + AWIN_CAN_INT_ERR_PASSIVE | \ + AWIN_CAN_INT_DATA_OR | \ + AWIN_CAN_INT_ERR \ +) + struct awin_can_softc { struct canif_softc sc_cansc; bus_space_tag_t sc_bst; @@ -164,6 +173,8 @@ awin_can_attach(device_t parent, device_ CAN_LINKMODE_3SAMPLES | CAN_LINKMODE_LISTENONLY | CAN_LINKMODE_LOOPBACK; can_ifinit_timings(>sc_cansc); + sc->sc_timings.clt_prop = 0; + sc->sc_timings.clt_sjw = 1; aprint_naive("\n"); aprint_normal(": CAN bus controller\n"); @@ -222,6 +233,7 @@ awin_can_rx_intr(struct awin_can_softc * int dlc; int regd, i; + KASSERT(mutex_owned(>sc_intr_lock)); reg0v = awin_can_read(sc, AWIN_CAN_TXBUF0_REG); dlc = reg0v & AWIN_CAN_TXBUF0_DL; @@ -268,6 +280,7 @@ awin_can_rx_intr(struct awin_can_softc * ifp->if_ibytes += m->m_len; m_set_rcvif(m, ifp); bpf_mtap(ifp, m); + printf("can_input1\n"); can_input(ifp, m); } @@ -281,11 +294,13 @@ awin_can_tx_intr(struct awin_can_softc * uint32_t reg0val; int i; + KASSERT(mutex_owned(>sc_intr_lock)); if ((m = sc->sc_m_transmit) != NULL) { ifp->if_obytes += m->m_len; ifp->if_opackets++; can_mbuf_tag_clean(m); m_set_rcvif(m, ifp); + printf("can_input2\n"); can_input(ifp, m); /* loopback */ sc->sc_m_transmit = NULL; } @@ -331,11 +346,68 @@ awin_can_tx_intr(struct awin_can_softc * } awin_can_write(sc, AWIN_CAN_TXBUF0_REG, reg0val); - awin_can_write(sc, AWIN_CAN_CMD_REG, AWIN_CAN_CMD_TANS_REQ); + if (sc->sc_linkmodes & CAN_LINKMODE_LOOPBACK) { + awin_can_write(sc, AWIN_CAN_CMD_REG, + AWIN_CAN_CMD_TANS_REQ | AWIN_CAN_CMD_SELF_REQ); + } else { + awin_can_write(sc, AWIN_CAN_CMD_REG, AWIN_CAN_CMD_TANS_REQ); + } ifp->if_flags |= IFF_OACTIVE; + ifp->if_timer = 5; bpf_mtap(ifp, m); } +static int +awin_can_tx_abort(struct awin_can_softc *sc) +{ + KASSERT(mutex_owned(>sc_intr_lock)); + if (sc->sc_m_transmit) { + m_freem(sc->sc_m_transmit); + sc->sc_m_transmit = NULL; + sc->sc_ifp->if_timer = 0; + /* + * the transmit abort will trigger a TX interrupt + * which will restart the queue or cleae OACTIVE, + * as appropriate + */ + awin_can_write(sc, AWIN_CAN_CMD_REG, AWIN_CAN_CMD_ABT_REQ); + return 1; + } + return 0; +} + +static void +awin_can_err_intr(struct awin_can_softc *sc, uint32_t irq, uint32_t sts) +{ + struct ifnet * const ifp = sc->sc_ifp; + KASSERT(mutex_owned(>sc_intr_lock)); + int txerr = 0; + + if (irq & AWIN_CAN_INT_DATA_OR) { + ifp->if_ierrors++; + awin_can_write(sc, AWIN_CAN_CMD_REG, AWIN_CAN_CMD_CLR_OR); + } + if (irq & AWIN_CAN_INT_ERR) { + /* XXX todo */ + } + if (irq & AWIN_CAN_INT_BERR) { + if (sts & AWIN_CAN_STA_TX) + txerr++; + if (sts & AWIN_CAN_STA_RX) + ifp->if_ierrors++; + } + if (irq & AWIN_CAN_INT_ERR_PASSIVE) { + /* XXX todo */ + } + if (irq & AWIN_CAN_INT_ARB_LOST) { + txerr++; + } + if (txerr) { + ifp->if_oerrors += txerr; + (void) awin_can_tx_abort(sc); + } +} + int awin_can_intr(void *arg) { @@ -358,6 +430,9 @@ awin_can_intr(void *arg) sts = awin_can_read(sc, AWIN_CAN_STA_REG); } } + if (irq & AWIN_CAN_INT_ALLERRS) { + awin_can_err_intr(sc, irq, sts); + } awin_can_write(sc, AWIN_CAN_INT_REG, irq); rnd_add_uint32(>sc_rnd_source, irq); @@ -424,7 +499,7 @@ awin_can_ifup(struct awin_can_softc * co awin_can_exit_reset(sc); awin_can_write(sc, AWIN_CAN_INTE_REG, - AWIN_CAN_INT_TX_FLAG | AWIN_CAN_INT_RX_FLAG); + AWIN_CAN_INT_TX_FLAG | AWIN_CAN_INT_RX_FLAG | AWIN_CAN_INT_ALLERRS); sc->sc_ifp->if_flags |= IFF_RUNNING; return 0; } @@ -433,6 +508,7 @@ static void awin_can_ifdown(struct
CVS commit: [bouyer-socketcan] src/sys/netcan
Module Name:src Committed By: bouyer Date: Thu Apr 20 12:59:11 UTC 2017 Modified Files: src/sys/netcan [bouyer-socketcan]: can.c Log Message: Fix LINKMODE Refuse to send packets if LISTENONLY is set Don't forget to unbind in error case. To generate a diff of this commit: cvs rdiff -u -r1.1.2.10 -r1.1.2.11 src/sys/netcan/can.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/sys/netcan/can.c diff -u src/sys/netcan/can.c:1.1.2.10 src/sys/netcan/can.c:1.1.2.11 --- src/sys/netcan/can.c:1.1.2.10 Wed Apr 19 22:19:12 2017 +++ src/sys/netcan/can.c Thu Apr 20 12:59:11 2017 @@ -1,4 +1,4 @@ -/* $NetBSD: can.c,v 1.1.2.10 2017/04/19 22:19:12 bouyer Exp $ */ +/* $NetBSD: can.c,v 1.1.2.11 2017/04/20 12:59:11 bouyer Exp $ */ /*- * Copyright (c) 2003, 2017 The NetBSD Foundation, Inc. @@ -30,7 +30,7 @@ */ #include -__KERNEL_RCSID(0, "$NetBSD: can.c,v 1.1.2.10 2017/04/19 22:19:12 bouyer Exp $"); +__KERNEL_RCSID(0, "$NetBSD: can.c,v 1.1.2.11 2017/04/20 12:59:11 bouyer Exp $"); #include #include @@ -146,7 +146,7 @@ can_set_netlink(struct ifnet *ifp, struc case CANCLINKMODE: if (ifd->ifd_len != sizeof(uint32_t)) return EINVAL; - error = copyout(ifd->ifd_data, , ifd->ifd_len); + error = copyin(ifd->ifd_data, , ifd->ifd_len); if (error) return error; if ((mode & csc->csc_timecaps.cltc_linkmode_caps) != mode) @@ -215,6 +215,7 @@ can_output(struct mbuf *m, struct canpcb { struct ifnet *ifp; struct m_tag *sotag; + struct canif_softc *csc; if (canp == NULL) { printf("can_output: no pcb\n"); @@ -224,6 +225,11 @@ can_output(struct mbuf *m, struct canpcb if (ifp == 0) { return EDESTADDRREQ; } + csc = ifp->if_softc; + if (csc && (csc->csc_linkmodes & CAN_LINKMODE_LISTENONLY)) { + return ENETUNREACH; + } + sotag = m_tag_get(PACKET_TAG_SO, sizeof(struct socket *), PR_NOWAIT); if (sotag == NULL) { ifp->if_oerrors++; @@ -602,8 +608,6 @@ can_send(struct socket *so, struct mbuf } } error = can_output(m, canp); - if (error) - goto err; if (nam) { struct sockaddr_can lscan; memset(, 0, sizeof(lscan)); @@ -611,6 +615,8 @@ can_send(struct socket *so, struct mbuf lscan.can_len = sizeof(lscan); can_pcbbind(canp, , l); } + if (error) + goto err; return 0; err:
CVS commit: [bouyer-socketcan] src/sys/netcan
Module Name:src Committed By: bouyer Date: Wed Apr 19 22:19:12 UTC 2017 Modified Files: src/sys/netcan [bouyer-socketcan]: can.c Log Message: can_mbuf_tag_clean(): make sure we won't free the tag we will reuse. Avoid mbuf leak in error paths. To generate a diff of this commit: cvs rdiff -u -r1.1.2.9 -r1.1.2.10 src/sys/netcan/can.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/sys/netcan/can.c diff -u src/sys/netcan/can.c:1.1.2.9 src/sys/netcan/can.c:1.1.2.10 --- src/sys/netcan/can.c:1.1.2.9 Wed Apr 19 17:52:37 2017 +++ src/sys/netcan/can.c Wed Apr 19 22:19:12 2017 @@ -1,4 +1,4 @@ -/* $NetBSD: can.c,v 1.1.2.9 2017/04/19 17:52:37 bouyer Exp $ */ +/* $NetBSD: can.c,v 1.1.2.10 2017/04/19 22:19:12 bouyer Exp $ */ /*- * Copyright (c) 2003, 2017 The NetBSD Foundation, Inc. @@ -30,7 +30,7 @@ */ #include -__KERNEL_RCSID(0, "$NetBSD: can.c,v 1.1.2.9 2017/04/19 17:52:37 bouyer Exp $"); +__KERNEL_RCSID(0, "$NetBSD: can.c,v 1.1.2.10 2017/04/19 22:19:12 bouyer Exp $"); #include #include @@ -214,24 +214,20 @@ static int can_output(struct mbuf *m, struct canpcb *canp) { struct ifnet *ifp; - int error = 0; struct m_tag *sotag; if (canp == NULL) { printf("can_output: no pcb\n"); - error = EINVAL; - return error; + return EINVAL; } ifp = canp->canp_ifp; if (ifp == 0) { - error = EDESTADDRREQ; - goto bad; + return EDESTADDRREQ; } sotag = m_tag_get(PACKET_TAG_SO, sizeof(struct socket *), PR_NOWAIT); if (sotag == NULL) { ifp->if_oerrors++; - error = ENOMEM; - goto bad; + return ENOMEM; } *(struct socket **)(sotag + 1) = canp->canp_socket; m_tag_prepend(m, sotag); @@ -240,10 +236,7 @@ can_output(struct mbuf *m, struct canpcb can_output_cnt++; return ifq_enqueue(ifp, m); } else - error = EMSGSIZE; -bad: - m_freem(m); - return (error); + return EMSGSIZE; } /* @@ -255,6 +248,9 @@ can_mbuf_tag_clean(struct mbuf *m) struct m_tag *sotag; sotag = m_tag_find(m, PACKET_TAG_SO, NULL); + if (sotag) + m_tag_unlink(m, sotag); + m_tag_delete_nonpersistent(m); if (sotag) m_tag_prepend(m, sotag); @@ -285,9 +281,9 @@ can_input(struct ifnet *ifp, struct mbuf } else { IF_ENQUEUE(inq, m); IFQ_UNLOCK(inq); - schednetisr(NETISR_CAN); ifp->if_ipackets++; ifp->if_ibytes += m->m_pkthdr.len; + schednetisr(NETISR_CAN); } } @@ -356,7 +352,7 @@ canintr(void) * we can't be sure we won't need * the original mbuf later so copy */ -mc = m_copym(m, 0, M_COPYALL, M_NOWAIT); +mc = m_copypacket(m, M_NOWAIT); if (mc == NULL) { /* deliver this mbuf and abort */ mc = m; @@ -574,11 +570,15 @@ can_send(struct socket *so, struct mbuf int s; if (control && control->m_len) { - return EINVAL; + m_freem(control); + error = EINVAL; + goto err; } if (m->m_len > sizeof(struct can_frame) || - m->m_len < offsetof(struct can_frame, can_dlc)) - return EINVAL; + m->m_len < offsetof(struct can_frame, can_dlc)) { + error = EINVAL; + goto err; + } /* we expect all data in the first mbuf */ KASSERT((m->m_flags & M_PKTHDR) != 0); @@ -586,20 +586,24 @@ can_send(struct socket *so, struct mbuf if (nam) { if ((so->so_state & SS_ISCONNECTED) != 0) { - return EISCONN; + error = EISCONN; + goto err; } s = splnet(); error = can_pcbbind(canp, (struct sockaddr_can *)nam, l); if (error) { splx(s); - return error; + goto err; } } else { if ((so->so_state & SS_ISCONNECTED) == 0) { - return EDESTADDRREQ; + error = EDESTADDRREQ; + goto err; } } error = can_output(m, canp); + if (error) + goto err; if (nam) { struct sockaddr_can lscan; memset(, 0, sizeof(lscan)); @@ -607,6 +611,10 @@ can_send(struct socket *so, struct mbuf lscan.can_len = sizeof(lscan); can_pcbbind(canp, , l); } + return 0; + +err: + m_freem(m); return error; }
CVS commit: [bouyer-socketcan] src/sys/netcan
Module Name:src Committed By: bouyer Date: Wed Apr 19 22:17:57 UTC 2017 Modified Files: src/sys/netcan [bouyer-socketcan]: if_canloop.c Log Message: Set rcvif before bpf_mtap To generate a diff of this commit: cvs rdiff -u -r1.1.2.4 -r1.1.2.5 src/sys/netcan/if_canloop.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/sys/netcan/if_canloop.c diff -u src/sys/netcan/if_canloop.c:1.1.2.4 src/sys/netcan/if_canloop.c:1.1.2.5 --- src/sys/netcan/if_canloop.c:1.1.2.4 Sun Feb 5 17:37:10 2017 +++ src/sys/netcan/if_canloop.c Wed Apr 19 22:17:56 2017 @@ -1,4 +1,4 @@ -/* $NetBSD: if_canloop.c,v 1.1.2.4 2017/02/05 17:37:10 bouyer Exp $ */ +/* $NetBSD: if_canloop.c,v 1.1.2.5 2017/04/19 22:17:56 bouyer Exp $ */ /*- * Copyright (c) 2017 The NetBSD Foundation, Inc. @@ -35,7 +35,7 @@ */ #include -__KERNEL_RCSID(0, "$NetBSD: if_canloop.c,v 1.1.2.4 2017/02/05 17:37:10 bouyer Exp $"); +__KERNEL_RCSID(0, "$NetBSD: if_canloop.c,v 1.1.2.5 2017/04/19 22:17:56 bouyer Exp $"); #ifdef _KERNEL_OPT #include "opt_can.h" @@ -164,9 +164,9 @@ canloop_ifstart(struct ifnet *ifp) if ((m->m_flags & M_PKTHDR) == 0) panic("canloop_output: no header mbuf"); + m_set_rcvif(m, ifp); if (ifp->if_flags & IFF_LOOPBACK) bpf_mtap_af(ifp, AF_CAN, m); - m_set_rcvif(m, ifp); pktlen = m->m_pkthdr.len; ifp->if_opackets++; @@ -178,7 +178,6 @@ canloop_ifstart(struct ifnet *ifp) #else printf("%s: can't handle CAN packet\n", ifp->if_xname); m_freem(m); - error = EAFNOSUPPORT; #endif }
CVS commit: [bouyer-socketcan] src/sys/arch/arm/allwinner
Module Name:src Committed By: bouyer Date: Wed Apr 19 17:54:18 UTC 2017 Modified Files: src/sys/arch/arm/allwinner [bouyer-socketcan]: awin_can.c Log Message: Fix the reset code clear the IFF_UP flag if we can't get the interface running. To generate a diff of this commit: cvs rdiff -u -r1.1.2.1 -r1.1.2.2 src/sys/arch/arm/allwinner/awin_can.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/sys/arch/arm/allwinner/awin_can.c diff -u src/sys/arch/arm/allwinner/awin_can.c:1.1.2.1 src/sys/arch/arm/allwinner/awin_can.c:1.1.2.2 --- src/sys/arch/arm/allwinner/awin_can.c:1.1.2.1 Tue Apr 18 21:30:38 2017 +++ src/sys/arch/arm/allwinner/awin_can.c Wed Apr 19 17:54:18 2017 @@ -1,4 +1,4 @@ -/* $NetBSD: awin_can.c,v 1.1.2.1 2017/04/18 21:30:38 bouyer Exp $ */ +/* $NetBSD: awin_can.c,v 1.1.2.2 2017/04/19 17:54:18 bouyer Exp $ */ /*- * Copyright (c) 2017 The NetBSD Foundation, Inc. @@ -36,7 +36,7 @@ #include -__KERNEL_RCSID(1, "$NetBSD: awin_can.c,v 1.1.2.1 2017/04/18 21:30:38 bouyer Exp $"); +__KERNEL_RCSID(1, "$NetBSD: awin_can.c,v 1.1.2.2 2017/04/19 17:54:18 bouyer Exp $"); #include #include @@ -119,8 +119,9 @@ awin_can_match(device_t parent, cfdata_t KASSERT(cf->cf_loc[AWINIOCF_PORT] == AWINIOCF_PORT_DEFAULT || cf->cf_loc[AWINIOCF_PORT] == loc->loc_port); - if (!awin_gpio_pinset_available(pinset)) + if (!awin_gpio_pinset_available(pinset)) { return 0; + } return 1; } @@ -465,6 +466,9 @@ awin_can_ifioctl(struct ifnet *ifp, u_lo if ((ifp->if_flags & IFF_UP) != 0 && (ifp->if_flags & IFF_RUNNING) == 0) { error = awin_can_ifup(sc); +if (error) { + ifp->if_flags &= ~IFF_UP; +} } else if ((ifp->if_flags & IFF_UP) == 0 && (ifp->if_flags & IFF_RUNNING) != 0) { awin_can_ifdown(sc); @@ -493,7 +497,7 @@ awin_can_enter_reset(struct awin_can_sof val |= AWIN_CAN_MODSEL_RST; awin_can_write(sc, AWIN_CAN_MODSEL_REG, val); val = awin_can_read(sc, AWIN_CAN_MODSEL_REG); - if (val & AWIN_CAN_MODSEL_REG) + if (val & AWIN_CAN_MODSEL_RST) return; } printf("%s: couldn't enter reset mode\n", device_xname(sc->sc_dev)); @@ -510,7 +514,7 @@ awin_can_exit_reset(struct awin_can_soft val &= ~AWIN_CAN_MODSEL_RST; awin_can_write(sc, AWIN_CAN_MODSEL_REG, val); val = awin_can_read(sc, AWIN_CAN_MODSEL_REG); - if ((val & AWIN_CAN_MODSEL_REG) == 0) + if ((val & AWIN_CAN_MODSEL_RST) == 0) return; } printf("%s: couldn't leave reset mode\n", device_xname(sc->sc_dev));
CVS commit: [bouyer-socketcan] src/sys/arch/arm/allwinner
Module Name:src Committed By: bouyer Date: Wed Apr 19 17:53:32 UTC 2017 Modified Files: src/sys/arch/arm/allwinner [bouyer-socketcan]: awin_io.c Log Message: Add awincan To generate a diff of this commit: cvs rdiff -u -r1.46 -r1.46.4.1 src/sys/arch/arm/allwinner/awin_io.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/sys/arch/arm/allwinner/awin_io.c diff -u src/sys/arch/arm/allwinner/awin_io.c:1.46 src/sys/arch/arm/allwinner/awin_io.c:1.46.4.1 --- src/sys/arch/arm/allwinner/awin_io.c:1.46 Wed May 11 18:33:40 2016 +++ src/sys/arch/arm/allwinner/awin_io.c Wed Apr 19 17:53:32 2017 @@ -31,7 +31,7 @@ #include -__KERNEL_RCSID(1, "$NetBSD: awin_io.c,v 1.46 2016/05/11 18:33:40 bouyer Exp $"); +__KERNEL_RCSID(1, "$NetBSD: awin_io.c,v 1.46.4.1 2017/04/19 17:53:32 bouyer Exp $"); #include #include @@ -192,6 +192,7 @@ static const struct awin_locators awin_l { "awinir", OFFANDSIZE(A31_CIR), NOPORT, AWIN_A31_IRQ_CIR, A31 }, { "awinir", OFFANDSIZE(A80_CIR), NOPORT, AWIN_A80_IRQ_R_CIR, A80 }, { "awinlradc", OFFANDSIZE(LRADC), NOPORT, AWIN_IRQ_LRADC, A20 }, + { "awincan", OFFANDSIZE(CAN), NOPORT, AWIN_IRQ_CAN, A20 }, }; static int
CVS commit: [bouyer-socketcan] src/sys/netcan
Module Name:src Committed By: bouyer Date: Wed Apr 19 17:52:38 UTC 2017 Modified Files: src/sys/netcan [bouyer-socketcan]: can.c Log Message: Call if_alloc_sadl() for the interface, so that getifaddrs() will find them. To generate a diff of this commit: cvs rdiff -u -r1.1.2.8 -r1.1.2.9 src/sys/netcan/can.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/sys/netcan/can.c diff -u src/sys/netcan/can.c:1.1.2.8 src/sys/netcan/can.c:1.1.2.9 --- src/sys/netcan/can.c:1.1.2.8 Tue Apr 18 20:37:38 2017 +++ src/sys/netcan/can.c Wed Apr 19 17:52:37 2017 @@ -1,4 +1,4 @@ -/* $NetBSD: can.c,v 1.1.2.8 2017/04/18 20:37:38 bouyer Exp $ */ +/* $NetBSD: can.c,v 1.1.2.9 2017/04/19 17:52:37 bouyer Exp $ */ /*- * Copyright (c) 2003, 2017 The NetBSD Foundation, Inc. @@ -30,7 +30,7 @@ */ #include -__KERNEL_RCSID(0, "$NetBSD: can.c,v 1.1.2.8 2017/04/18 20:37:38 bouyer Exp $"); +__KERNEL_RCSID(0, "$NetBSD: can.c,v 1.1.2.9 2017/04/19 17:52:37 bouyer Exp $"); #include #include @@ -200,6 +200,7 @@ can_ifattach(struct ifnet *ifp) { ifp->if_dlt = DLT_CAN_SOCKETCAN; ifp->if_output = NULL; /* unused */ IFQ_SET_READY(>if_snd); + if_alloc_sadl(ifp); } void
CVS commit: [bouyer-socketcan] src/sys/arch/evbarm/conf
Module Name:src Committed By: bouyer Date: Tue Apr 18 21:31:48 UTC 2017 Modified Files: src/sys/arch/evbarm/conf [bouyer-socketcan]: CUBIEBOARD GENERIC.common Log Message: Include options CAN and canloop pseudo-device by default. Add awincan0 device to CUBIEBOARD To generate a diff of this commit: cvs rdiff -u -r1.49 -r1.49.2.1 src/sys/arch/evbarm/conf/CUBIEBOARD cvs rdiff -u -r1.14 -r1.14.2.1 src/sys/arch/evbarm/conf/GENERIC.common Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/sys/arch/evbarm/conf/CUBIEBOARD diff -u src/sys/arch/evbarm/conf/CUBIEBOARD:1.49 src/sys/arch/evbarm/conf/CUBIEBOARD:1.49.2.1 --- src/sys/arch/evbarm/conf/CUBIEBOARD:1.49 Tue Dec 13 20:42:17 2016 +++ src/sys/arch/evbarm/conf/CUBIEBOARD Tue Apr 18 21:31:48 2017 @@ -1,5 +1,5 @@ # -# $NetBSD: CUBIEBOARD,v 1.49 2016/12/13 20:42:17 christos Exp $ +# $NetBSD: CUBIEBOARD,v 1.49.2.1 2017/04/18 21:31:48 bouyer Exp $ # # CUBIEBOARD -- Allwinner A10/A20 Eval Board Kernel # @@ -185,6 +185,9 @@ awe0 at awinio0 port ? # 10/100/1000 Ethernet (A10/A20) awge0 at awinio0 port ? +# CAN bus +awincan0 at awinio0 port ? + include "dev/usb/usbdevices.config" midi* at midibus? Index: src/sys/arch/evbarm/conf/GENERIC.common diff -u src/sys/arch/evbarm/conf/GENERIC.common:1.14 src/sys/arch/evbarm/conf/GENERIC.common:1.14.2.1 --- src/sys/arch/evbarm/conf/GENERIC.common:1.14 Sat Nov 26 13:59:45 2016 +++ src/sys/arch/evbarm/conf/GENERIC.common Tue Apr 18 21:31:48 2017 @@ -1,5 +1,5 @@ # -# $NetBSD: GENERIC.common,v 1.14 2016/11/26 13:59:45 christos Exp $ +# $NetBSD: GENERIC.common,v 1.14.2.1 2017/04/18 21:31:48 bouyer Exp $ # # GENERIC evbarm kernel config (template) # @@ -46,6 +46,7 @@ options IPSEC # IP security #options PPP_DEFLATE # Deflate compression support for PPP #options PPP_FILTER # Active filter support for PPP (requires bpf) #options TCP_DEBUG # Record last TCP_NDEBUG packets with SO_DEBUG +options CAN # CAN bus support # JIT compiler for bpfilter #options SLJIT @@ -148,6 +149,7 @@ pseudo-device drvctl # driver control # network pseudo-devices pseudo-device bpfilter # Berkeley packet filter pseudo-device loop # network loopback +pseudo-device canloop # CAN loopback pseudo-device bridge # simple inter-network bridging pseudo-device vlan # IEEE 802.1q encapsulation pseudo-device ppp # Point-to-Point Protocol
CVS commit: [bouyer-socketcan] src/sys/arch/arm/allwinner
Module Name:src Committed By: bouyer Date: Tue Apr 18 21:30:38 UTC 2017 Modified Files: src/sys/arch/arm/allwinner [bouyer-socketcan]: files.awin Added Files: src/sys/arch/arm/allwinner [bouyer-socketcan]: awin_can.c Log Message: First draft of a driver for the CAN controller found in the Allwinner A20 SoC. To generate a diff of this commit: cvs rdiff -u -r0 -r1.1.2.1 src/sys/arch/arm/allwinner/awin_can.c cvs rdiff -u -r1.36 -r1.36.4.1 src/sys/arch/arm/allwinner/files.awin Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/sys/arch/arm/allwinner/files.awin diff -u src/sys/arch/arm/allwinner/files.awin:1.36 src/sys/arch/arm/allwinner/files.awin:1.36.4.1 --- src/sys/arch/arm/allwinner/files.awin:1.36 Fri May 27 20:01:49 2016 +++ src/sys/arch/arm/allwinner/files.awin Tue Apr 18 21:30:38 2017 @@ -1,4 +1,4 @@ -# $NetBSD: files.awin,v 1.36 2016/05/27 20:01:49 bouyer Exp $ +# $NetBSD: files.awin,v 1.36.4.1 2017/04/18 21:30:38 bouyer Exp $ # # Configuration info for Allwinner ARM Peripherals # @@ -110,6 +110,11 @@ file arch/arm/allwinner/awin_eth.c awin attach awge at awinio with awin_gige file arch/arm/allwinner/awin_gige.c awin_gige +# A20 CAN +device awincan { } : ifnet +attach awincan at awinio with awin_can +file arch/arm/allwinner/awin_can.c awin_can + # USB2 OTG Controller attach motg at awinio with awin_otg file arch/arm/allwinner/awin_otg.c awin_otg Added files: Index: src/sys/arch/arm/allwinner/awin_can.c diff -u /dev/null src/sys/arch/arm/allwinner/awin_can.c:1.1.2.1 --- /dev/null Tue Apr 18 21:30:38 2017 +++ src/sys/arch/arm/allwinner/awin_can.c Tue Apr 18 21:30:38 2017 @@ -0,0 +1,517 @@ +/* $NetBSD: awin_can.c,v 1.1.2.1 2017/04/18 21:30:38 bouyer Exp $ */ + +/*- + * Copyright (c) 2017 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Manuel Bouyer. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + *notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + *notice, this list of conditions and the following disclaimer in the + *documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + + +#include "locators.h" +#include "opt_can.h" + + +#include + +__KERNEL_RCSID(1, "$NetBSD: awin_can.c,v 1.1.2.1 2017/04/18 21:30:38 bouyer Exp $"); + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#ifdef CAN +#include +#include +#endif +#include + +#include +#include + +struct awin_can_softc { + struct canif_softc sc_cansc; + bus_space_tag_t sc_bst; + bus_space_handle_t sc_bsh; + kmutex_t sc_intr_lock; + void *sc_ih; + struct ifnet *sc_ifp; + krndsource_t sc_rnd_source; /* random source */ + struct mbuf *sc_m_transmit; /* mbuf being transmitted */ +}; +#define sc_dev sc_cansc.csc_dev +#define sc_timecaps sc_cansc.csc_timecaps +#define sc_timings sc_cansc.csc_timings +#define sc_linkmodes sc_cansc.csc_linkmodes + +static int awin_can_match(device_t, cfdata_t, void *); +static void awin_can_attach(device_t, device_t, void *); + +static int awin_can_intr(void *); + +static void awin_can_ifstart(struct ifnet *); +static int awin_can_ifioctl(struct ifnet *, u_long, void *); +static void awin_can_ifwatchdog(struct ifnet *); + +static void awin_can_enter_reset(struct awin_can_softc *); +static void awin_can_exit_reset(struct awin_can_softc *); + +CFATTACH_DECL_NEW(awin_can, sizeof(struct awin_can_softc), + awin_can_match, awin_can_attach, NULL, NULL); + +static const struct awin_gpio_pinset awin_can_pinsets[2] = { +[0] = { 'A', AWIN_PIO_PA_CAN_FUNC, AWIN_PIO_PA_CAN_PINS }, +[1] = { 'H', AWIN_PIO_PH_CAN_FUNC, AWIN_PIO_PH_CAN_PINS }, +}; + +static inline uint32_t +awin_can_read(struct awin_can_softc *sc, bus_size_t o) +{ +
CVS commit: [bouyer-socketcan] src/sys/netcan
Module Name:src Committed By: bouyer Date: Tue Apr 18 21:29:40 UTC 2017 Modified Files: src/sys/netcan [bouyer-socketcan]: can_var.h Log Message: include socketvar.h, so avoid a warning on struct sockopt * To generate a diff of this commit: cvs rdiff -u -r1.1.2.6 -r1.1.2.7 src/sys/netcan/can_var.h Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/sys/netcan/can_var.h diff -u src/sys/netcan/can_var.h:1.1.2.6 src/sys/netcan/can_var.h:1.1.2.7 --- src/sys/netcan/can_var.h:1.1.2.6 Tue Apr 18 20:37:38 2017 +++ src/sys/netcan/can_var.h Tue Apr 18 21:29:40 2017 @@ -1,4 +1,4 @@ -/* $NetBSD: can_var.h,v 1.1.2.6 2017/04/18 20:37:38 bouyer Exp $ */ +/* $NetBSD: can_var.h,v 1.1.2.7 2017/04/18 21:29:40 bouyer Exp $ */ /*- * Copyright (c) 2003, 2017 The NetBSD Foundation, Inc. @@ -41,6 +41,7 @@ struct can_ifreq { }; #ifdef _KERNEL +#include /* * common structure for CAN interface drivers. Should be at the start of
CVS commit: [bouyer-socketcan] src/sys/netcan
Module Name:src Committed By: bouyer Date: Tue Apr 18 20:48:07 UTC 2017 Modified Files: src/sys/netcan [bouyer-socketcan]: files.netcan Log Message: if_canloop.c is in netcan/ To generate a diff of this commit: cvs rdiff -u -r1.1.2.1 -r1.1.2.2 src/sys/netcan/files.netcan Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/sys/netcan/files.netcan diff -u src/sys/netcan/files.netcan:1.1.2.1 src/sys/netcan/files.netcan:1.1.2.2 --- src/sys/netcan/files.netcan:1.1.2.1 Sun Jan 15 20:27:33 2017 +++ src/sys/netcan/files.netcan Tue Apr 18 20:48:07 2017 @@ -1,8 +1,8 @@ -# $NetBSD: files.netcan,v 1.1.2.1 2017/01/15 20:27:33 bouyer Exp $ +# $NetBSD: files.netcan,v 1.1.2.2 2017/04/18 20:48:07 bouyer Exp $ defflag opt_can.h CAN # ISO-15765 CAN network stack file netcan/can.c can file netcan/can_proto.c can file netcan/can_pcb.c can -file net/if_canloop.c canloop +file netcan/if_canloop.c canloop
CVS commit: [bouyer-socketcan] src/sys/netcan
Module Name:src Committed By: bouyer Date: Tue Apr 18 20:37:38 UTC 2017 Modified Files: src/sys/netcan [bouyer-socketcan]: can.c can_var.h Log Message: Add can_ifinit_timings() helper function, which initialise timing parameters to known invalid values. To generate a diff of this commit: cvs rdiff -u -r1.1.2.7 -r1.1.2.8 src/sys/netcan/can.c cvs rdiff -u -r1.1.2.5 -r1.1.2.6 src/sys/netcan/can_var.h Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/sys/netcan/can.c diff -u src/sys/netcan/can.c:1.1.2.7 src/sys/netcan/can.c:1.1.2.8 --- src/sys/netcan/can.c:1.1.2.7 Mon Apr 17 20:32:27 2017 +++ src/sys/netcan/can.c Tue Apr 18 20:37:38 2017 @@ -1,4 +1,4 @@ -/* $NetBSD: can.c,v 1.1.2.7 2017/04/17 20:32:27 bouyer Exp $ */ +/* $NetBSD: can.c,v 1.1.2.8 2017/04/18 20:37:38 bouyer Exp $ */ /*- * Copyright (c) 2003, 2017 The NetBSD Foundation, Inc. @@ -30,7 +30,7 @@ */ #include -__KERNEL_RCSID(0, "$NetBSD: can.c,v 1.1.2.7 2017/04/17 20:32:27 bouyer Exp $"); +__KERNEL_RCSID(0, "$NetBSD: can.c,v 1.1.2.8 2017/04/18 20:37:38 bouyer Exp $"); #include #include @@ -202,6 +202,13 @@ can_ifattach(struct ifnet *ifp) { IFQ_SET_READY(>if_snd); } +void +can_ifinit_timings(struct canif_softc *csc) +{ + /* uninitialized parameters is all-one */ + memset(>csc_timings, 0xff, sizeof(struct can_link_timings)); +} + static int can_output(struct mbuf *m, struct canpcb *canp) { Index: src/sys/netcan/can_var.h diff -u src/sys/netcan/can_var.h:1.1.2.5 src/sys/netcan/can_var.h:1.1.2.6 --- src/sys/netcan/can_var.h:1.1.2.5 Mon Apr 17 20:32:27 2017 +++ src/sys/netcan/can_var.h Tue Apr 18 20:37:38 2017 @@ -1,4 +1,4 @@ -/* $NetBSD: can_var.h,v 1.1.2.5 2017/04/17 20:32:27 bouyer Exp $ */ +/* $NetBSD: can_var.h,v 1.1.2.6 2017/04/18 20:37:38 bouyer Exp $ */ /*- * Copyright (c) 2003, 2017 The NetBSD Foundation, Inc. @@ -59,6 +59,7 @@ extern struct domain candomain; extern const struct pr_usrreqs can_usrreqs; void can_ifattach(struct ifnet *); +void can_ifinit_timings(struct canif_softc *); void can_mbuf_tag_clean(struct mbuf *); void can_input(struct ifnet *, struct mbuf *); void *can_ctlinput(int, struct sockaddr *, void *);
CVS commit: [bouyer-socketcan] src/sys/netcan
Module Name:src Committed By: bouyer Date: Tue Apr 18 20:36:27 UTC 2017 Modified Files: src/sys/netcan [bouyer-socketcan]: can_link.h Log Message: Remove clt_clock_freq from user-supplied timing informations, it is defined by the brp. To generate a diff of this commit: cvs rdiff -u -r1.1.2.1 -r1.1.2.2 src/sys/netcan/can_link.h Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/sys/netcan/can_link.h diff -u src/sys/netcan/can_link.h:1.1.2.1 src/sys/netcan/can_link.h:1.1.2.2 --- src/sys/netcan/can_link.h:1.1.2.1 Mon Apr 17 20:32:27 2017 +++ src/sys/netcan/can_link.h Tue Apr 18 20:36:27 2017 @@ -1,4 +1,4 @@ -/* $NetBSD: can_link.h,v 1.1.2.1 2017/04/17 20:32:27 bouyer Exp $ */ +/* $NetBSD: can_link.h,v 1.1.2.2 2017/04/18 20:36:27 bouyer Exp $ */ /*- * Copyright (c) 2017 The NetBSD Foundation, Inc. @@ -55,7 +55,6 @@ struct can_link_timecaps { /* get/set timing parameters */ struct can_link_timings { - uint32_t clt_clock_freq; /* prescaler input clock, in hz */ uint32_t clt_brp; /* prescaler value */ uint32_t clt_prop; /* Propagation segment in tq */ uint32_t clt_ps1; /* Phase segment 1 in tq */
CVS commit: [bouyer-socketcan] src/sys/arch/arm/allwinner
Module Name:src Committed By: bouyer Date: Mon Apr 17 20:41:55 UTC 2017 Modified Files: src/sys/arch/arm/allwinner [bouyer-socketcan]: awin_reg.h Log Message: Add CAN registers To generate a diff of this commit: cvs rdiff -u -r1.90 -r1.90.2.1 src/sys/arch/arm/allwinner/awin_reg.h Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/sys/arch/arm/allwinner/awin_reg.h diff -u src/sys/arch/arm/allwinner/awin_reg.h:1.90 src/sys/arch/arm/allwinner/awin_reg.h:1.90.2.1 --- src/sys/arch/arm/allwinner/awin_reg.h:1.90 Mon Dec 26 16:20:17 2016 +++ src/sys/arch/arm/allwinner/awin_reg.h Mon Apr 17 20:41:55 2017 @@ -1,4 +1,4 @@ -/* $NetBSD: awin_reg.h,v 1.90 2016/12/26 16:20:17 rjs Exp $ */ +/* $NetBSD: awin_reg.h,v 1.90.2.1 2017/04/17 20:41:55 bouyer Exp $ */ /*- * Copyright (c) 2013 The NetBSD Foundation, Inc. @@ -3071,4 +3071,120 @@ struct awin_a31_dma_desc { #define AWIN_LRADC_DATA0_REG 0x0c #define AWIN_LRADC_DATA1_REG 0x10 +/* CAN mode select register */ +#define AWIN_CAN_MODSEL_REG 0x00 +#define AWIN_CAN_MODSEL_SLEEP __BIT(4) +#define AWIN_CAN_MODSEL_ACP_FLT_MOD __BIT(3) +#define AWIN_CAN_MODSEL_LB_MOD __BIT(2) +#define AWIN_CAN_MODSEL_LST_ONLY __BIT(1) +#define AWIN_CAN_MODSEL_RST __BIT(0) + +/* CAN command register */ +#define AWIN_CAN_CMD_REG 0x04 +#define AWIN_CAN_CMD_BUS_OFF __BIT(5) +#define AWIN_CAN_CMD_SELF_REQ __BIT(4) +#define AWIN_CAN_CMD_CLR_OR __BIT(3) +#define AWIN_CAN_CMD_REL_RX_BUF __BIT(2) +#define AWIN_CAN_CMD_ABT_REQ __BIT(1) +#define AWIN_CAN_CMD_TANS_REQ __BIT(0) + +/* CAN status register */ +#define AWIN_CAN_STA_REG 0x08 +#define AWIN_CAN_STA_ERR_CODE __BITS(23,22) +#define AWIN_CAN_STA_ERR_CODE_BIT 0 +#define AWIN_CAN_STA_ERR_CODE_FORM 1 +#define AWIN_CAN_STA_ERR_CODE_STUFF 2 +#define AWIN_CAN_STA_ERR_CODE_OTHER 3 +#define AWIN_CAN_STA_ERR_DIR _BIT(21) +#define AWIN_CAN_STA_ERR_SEG_CODE __BITS(20,16) +#define AWIN_CAN_STA_ARB_LOST __BITS(12,8) +#define AWIN_CAN_STA_BUS __BIT(7) +#define AWIN_CAN_STA_ERR __BIT(6) +#define AWIN_CAN_STA_TX __BIT(5) +#define AWIN_CAN_STA_RX __BIT(4) +#define AWIN_CAN_STA_TX_OVER __BIT(3) +#define AWIN_CAN_STA_TX_RDY __BIT(2) +#define AWIN_CAN_STA_DATA_OR __BIT(1) +#define AWIN_CAN_STA_RX_RDY __BIT(0) + +/* CAN interrupt register */ +#define AWIN_CAN_INT_REG 0x0c +#define AWIN_CAN_INT_BERR __BIT(7) +#define AWIN_CAN_INT_ARB_LOST __BIT(6) +#define AWIN_CAN_INT_ERR_PASSIVE __BIT(5) +#define AWIN_CAN_INT_WAKEUP __BIT(4) +#define AWIN_CAN_INT_DATA_OR __BIT(3) +#define AWIN_CAN_INT_ERR __BIT(2) +#define AWIN_CAN_INT_TX_FLAG __BIT(1) +#define AWIN_CAN_INT_RX_FLAG __BIT(0) + +/* CAN interrupt enable register */ +#define AWIN_CAN_INTE_REG 0x10 + +/* CAN bus timing register */ +#define AWIN_CAN_BUS_TIME_REG 0x14 +#define AWIN_CAN_BUS_TIME_SAM __BIT(23) +#define AWIN_CAN_BUS_TIME_PHSEG2 __BITS(22,20) +#define AWIN_CAN_BUS_TIME_PHSEG1 __BITS(19,16) +#define AWIN_CAN_BUS_TIME_SJW __BITS(15,14) +#define AWIN_CAN_BUS_TIME_TQ_BRP __BITS(9,0) + +/* CAN tx error warning limit register */ +#define AWIN_CAN_EWL_REG 0x18 +#define AWIN_CAN_EWL_ERR_WRN_LMT __BITS(7,0) + +/* CAN error counter register */ +#define AWIN_CAN_REC_REG 0x1c +#define AWIN_CAN_REC_RX_ERR_CNT __BITS(23,16) +#define AWIN_CAN_REC_TX_ERR_CNT __BITS(7,0) + +/* CAN receive message register */ +#define AWIN_CAN_RMSGC_REG 0x20 +#define AWIN_CAN_RMSGC_RX_MSG_CNT __BITS(7,0) + +/* CAN receive buffer start address register */ +#define AWIN_CAN_RSADDR_REG 0x24 +#define AWIN_CAN_RSADDR_RX_BUF_SADDR __BITS(5,0) + +/* CAN rx/tx message buffer 0 register */ +#define AWIN_CAN_TXBUF0_REG 0x40 +#define AWIN_CAN_TXBUF0_EFF __BIT(7) +#define AWIN_CAN_TXBUF0_RTR __BIT(6) +#define AWIN_CAN_TXBUF0_DL __BITS(3,0) + +/* CAN rx/tx message buffer registers */ +#define AWIN_CAN_TXBUF1_REG 0x44 +#define AWIN_CAN_TXBUF2_REG 0x48 +#define AWIN_CAN_TXBUF3_REG 0x4c +#define AWIN_CAN_TXBUF4_REG 0x50 +#define AWIN_CAN_TXBUF5_REG 0x54 +#define AWIN_CAN_TXBUF6_REG 0x58 +#define AWIN_CAN_TXBUF7_REG 0x5c +#define AWIN_CAN_TXBUF8_REG 0x60 +#define AWIN_CAN_TXBUF9_REG 0x64 +#define AWIN_CAN_TXBUF10_REG 0x68 +#define AWIN_CAN_TXBUF11_REG 0x6c +#define AWIN_CAN_TXBUF12_REG 0x70 + +/* CAN acceptance code 0 register */ +#define AWIN_CAN_ACPC 0x40 + +/* CAN acceptance mask 0 register */ +#define AWIN_CAN_ACPM 0x44 + +/* CAN transmit buffer for read back registers */ +#define AWIN_CAN_RBUF_RBACK0 0x180 +#define AWIN_CAN_RBUF_RBACK1 0x184 +#define AWIN_CAN_RBUF_RBACK2 0x188 +#define AWIN_CAN_RBUF_RBACK3 0x18c +#define AWIN_CAN_RBUF_RBACK4 0x190 +#define AWIN_CAN_RBUF_RBACK5 0x194 +#define AWIN_CAN_RBUF_RBACK6 0x198 +#define AWIN_CAN_RBUF_RBACK7 0x19c +#define AWIN_CAN_RBUF_RBACK8 0x1a0 +#define AWIN_CAN_RBUF_RBACK9 0x1a4 +#define AWIN_CAN_RBUF_RBACK10 0x1a8 +#define AWIN_CAN_RBUF_RBACK11 0x1ac +#define AWIN_CAN_RBUF_RBACK12 0x1b0 + #endif /* _ARM_ALLWINNER_AWIN_REG_H_ */
CVS commit: [bouyer-socketcan] src/sys/netcan
Module Name:src Committed By: bouyer Date: Mon Apr 17 20:32:27 UTC 2017 Modified Files: src/sys/netcan [bouyer-socketcan]: Makefile can.c can.h can_var.h Added Files: src/sys/netcan [bouyer-socketcan]: can_link.h Log Message: Add infranstructure to configure timings from userland on a can interface. This uses the SIOCGDRVSPEC/SIOCSDRVSPEC ioctls. Compile-tested only. To generate a diff of this commit: cvs rdiff -u -r1.1.2.1 -r1.1.2.2 src/sys/netcan/Makefile cvs rdiff -u -r1.1.2.6 -r1.1.2.7 src/sys/netcan/can.c cvs rdiff -u -r1.1.2.3 -r1.1.2.4 src/sys/netcan/can.h cvs rdiff -u -r0 -r1.1.2.1 src/sys/netcan/can_link.h cvs rdiff -u -r1.1.2.4 -r1.1.2.5 src/sys/netcan/can_var.h Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/sys/netcan/Makefile diff -u src/sys/netcan/Makefile:1.1.2.1 src/sys/netcan/Makefile:1.1.2.2 --- src/sys/netcan/Makefile:1.1.2.1 Sun Jan 15 20:27:33 2017 +++ src/sys/netcan/Makefile Mon Apr 17 20:32:27 2017 @@ -1,8 +1,8 @@ -# $NetBSD: Makefile,v 1.1.2.1 2017/01/15 20:27:33 bouyer Exp $ +# $NetBSD: Makefile,v 1.1.2.2 2017/04/17 20:32:27 bouyer Exp $ KDIR= /sys/netcan INCSDIR= /usr/include/netcan -INCS= can.h +INCS= can.h can_link.h .include Index: src/sys/netcan/can.c diff -u src/sys/netcan/can.c:1.1.2.6 src/sys/netcan/can.c:1.1.2.7 --- src/sys/netcan/can.c:1.1.2.6 Sun Feb 5 19:44:53 2017 +++ src/sys/netcan/can.c Mon Apr 17 20:32:27 2017 @@ -1,4 +1,4 @@ -/* $NetBSD: can.c,v 1.1.2.6 2017/02/05 19:44:53 bouyer Exp $ */ +/* $NetBSD: can.c,v 1.1.2.7 2017/04/17 20:32:27 bouyer Exp $ */ /*- * Copyright (c) 2003, 2017 The NetBSD Foundation, Inc. @@ -30,7 +30,7 @@ */ #include -__KERNEL_RCSID(0, "$NetBSD: can.c,v 1.1.2.6 2017/02/05 19:44:53 bouyer Exp $"); +__KERNEL_RCSID(0, "$NetBSD: can.c,v 1.1.2.7 2017/04/17 20:32:27 bouyer Exp $"); #include #include @@ -90,6 +90,77 @@ can_init(void) /* * Generic control operations (ioctl's). */ +static int +can_get_netlink(struct ifnet *ifp, struct ifdrv *ifd) +{ + struct canif_softc *csc = ifp->if_softc; + + if (ifp->if_dlt != DLT_CAN_SOCKETCAN || csc == NULL) + return EOPNOTSUPP; + + switch(ifd->ifd_cmd) { + case CANGLINKTIMECAP: + if (ifd->ifd_len != sizeof(struct can_link_timecaps)) + return EINVAL; + return copyout(>csc_timecaps, ifd->ifd_data, ifd->ifd_len); + case CANGLINKTIMINGS: + if (ifd->ifd_len != sizeof(struct can_link_timings)) + return EINVAL; + return copyout(>csc_timings, ifd->ifd_data, ifd->ifd_len); + case CANGLINKMODE: + if (ifd->ifd_len != sizeof(uint32_t)) + return EINVAL; + return copyout(>csc_linkmodes, ifd->ifd_data, ifd->ifd_len); + } + return EOPNOTSUPP; +} + +static int +can_set_netlink(struct ifnet *ifp, struct ifdrv *ifd) +{ + struct canif_softc *csc = ifp->if_softc; + uint32_t mode; + int error; + + if (ifp->if_dlt != DLT_CAN_SOCKETCAN || csc == NULL) + return EOPNOTSUPP; + + error = kauth_authorize_network(curlwp->l_cred, + KAUTH_NETWORK_INTERFACE, + KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, + (void *)SIOCSDRVSPEC, NULL); + if (error != 0) + return error; + + if ((ifp->if_flags & IFF_UP) != 0) { + return EBUSY; + } + + switch(ifd->ifd_cmd) { + case CANSLINKTIMINGS: + if (ifd->ifd_len != sizeof(struct can_link_timings)) + return EINVAL; + return copyin(ifd->ifd_data, >csc_timings, ifd->ifd_len); + + case CANSLINKMODE: + case CANCLINKMODE: + if (ifd->ifd_len != sizeof(uint32_t)) + return EINVAL; + error = copyout(ifd->ifd_data, , ifd->ifd_len); + if (error) + return error; + if ((mode & csc->csc_timecaps.cltc_linkmode_caps) != mode) + return EINVAL; + /* XXX locking */ + if (ifd->ifd_cmd == CANSLINKMODE) + csc->csc_linkmodes |= mode; + else + csc->csc_linkmodes &= ~mode; + return 0; + } + return EOPNOTSUPP; +} + /* ARGSUSED */ static int can_control(struct socket *so, u_long cmd, void *data, struct ifnet *ifp) @@ -98,12 +169,16 @@ can_control(struct socket *so, u_long cm struct can_ifreq *cfr = (struct can_ifreq *)data; int error = 0; #endif - + if (ifp == NULL) + return (EOPNOTSUPP); switch (cmd) { - + case SIOCGDRVSPEC: + return can_get_netlink(ifp, (struct ifdrv *) data); + case SIOCSDRVSPEC: + return can_set_netlink(ifp, (struct ifdrv *) data); default: - if (ifp == 0 || ifp->if_ioctl == 0) + if (ifp->if_ioctl == 0) return (EOPNOTSUPP); return ((*ifp->if_ioctl)(ifp, cmd, data)); } Index: src/sys/netcan/can.h diff -u src/sys/netcan/can.h:1.1.2.3 src/sys/netcan/can.h:1.1.2.4 --- src/sys/netcan/can.h:1.1.2.3 Mon Jan 16 18:03:38 2017 +++ src/sys/netcan/can.h Mon Apr 17 20:32:27 2017 @@ -1,4 +1,4 @@ -/* $NetBSD: can.h,v 1.1.2.3 2017/01/16 18:03:38 bouyer Exp $ */ +/* $NetBSD: can.h,v 1.1.2.4 2017/04/17 20:32:27 bouyer Exp $ */ /*- * Copyright (c) 2003, 2017 The NetBSD Foundation, Inc. @@ -118,12 +118,6 @@ struct can_filter { #define CAN_INV_FILTER 0x2000U #ifdef
CVS commit: [bouyer-socketcan] src/sys/netcan
Module Name:src Committed By: bouyer Date: Sun Feb 5 19:44:53 UTC 2017 Modified Files: src/sys/netcan [bouyer-socketcan]: can.c can_pcb.c Log Message: more sanity checks on the mbuf we get. To generate a diff of this commit: cvs rdiff -u -r1.1.2.5 -r1.1.2.6 src/sys/netcan/can.c cvs rdiff -u -r1.1.2.2 -r1.1.2.3 src/sys/netcan/can_pcb.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/sys/netcan/can.c diff -u src/sys/netcan/can.c:1.1.2.5 src/sys/netcan/can.c:1.1.2.6 --- src/sys/netcan/can.c:1.1.2.5 Sun Feb 5 17:37:10 2017 +++ src/sys/netcan/can.c Sun Feb 5 19:44:53 2017 @@ -1,4 +1,4 @@ -/* $NetBSD: can.c,v 1.1.2.5 2017/02/05 17:37:10 bouyer Exp $ */ +/* $NetBSD: can.c,v 1.1.2.6 2017/02/05 19:44:53 bouyer Exp $ */ /*- * Copyright (c) 2003, 2017 The NetBSD Foundation, Inc. @@ -30,7 +30,7 @@ */ #include -__KERNEL_RCSID(0, "$NetBSD: can.c,v 1.1.2.5 2017/02/05 17:37:10 bouyer Exp $"); +__KERNEL_RCSID(0, "$NetBSD: can.c,v 1.1.2.6 2017/02/05 19:44:53 bouyer Exp $"); #include #include @@ -493,6 +493,13 @@ can_send(struct socket *so, struct mbuf if (control && control->m_len) { return EINVAL; } + if (m->m_len > sizeof(struct can_frame) || + m->m_len < offsetof(struct can_frame, can_dlc)) + return EINVAL; + + /* we expect all data in the first mbuf */ + KASSERT((m->m_flags & M_PKTHDR) != 0); + KASSERT(m->m_len == m->m_pkthdr.len); if (nam) { if ((so->so_state & SS_ISCONNECTED) != 0) { Index: src/sys/netcan/can_pcb.c diff -u src/sys/netcan/can_pcb.c:1.1.2.2 src/sys/netcan/can_pcb.c:1.1.2.3 --- src/sys/netcan/can_pcb.c:1.1.2.2 Sun Feb 5 10:56:12 2017 +++ src/sys/netcan/can_pcb.c Sun Feb 5 19:44:53 2017 @@ -1,4 +1,4 @@ -/* $NetBSD: can_pcb.c,v 1.1.2.2 2017/02/05 10:56:12 bouyer Exp $ */ +/* $NetBSD: can_pcb.c,v 1.1.2.3 2017/02/05 19:44:53 bouyer Exp $ */ /*- * Copyright (c) 2003, 2017 The NetBSD Foundation, Inc. @@ -30,7 +30,7 @@ */ #include -__KERNEL_RCSID(0, "$NetBSD: can_pcb.c,v 1.1.2.2 2017/02/05 10:56:12 bouyer Exp $"); +__KERNEL_RCSID(0, "$NetBSD: can_pcb.c,v 1.1.2.3 2017/02/05 19:44:53 bouyer Exp $"); #include #include @@ -332,6 +332,9 @@ can_pcbfilter(struct canpcb *canp, struc struct can_frame *fmp; struct can_filter *fip; + KASSERT((m->m_flags & M_PKTHDR) != 0); + KASSERT(m->m_len == m->m_pkthdr.len); + fmp = mtod(m, struct can_frame *); for (i = 0; i < canp->canp_nfilters; i++) { fip = >canp_filters[i];
CVS commit: [bouyer-socketcan] src/sys/netcan
Module Name:src Committed By: bouyer Date: Sun Feb 5 17:37:10 UTC 2017 Modified Files: src/sys/netcan [bouyer-socketcan]: can.c can_var.h if_canloop.c Log Message: Introduce can_ifattach(), for common setups of can interfaces. Convert output to a ifq and change canloop_output to canloop_ifstart To generate a diff of this commit: cvs rdiff -u -r1.1.2.4 -r1.1.2.5 src/sys/netcan/can.c cvs rdiff -u -r1.1.2.3 -r1.1.2.4 src/sys/netcan/can_var.h \ src/sys/netcan/if_canloop.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/sys/netcan/can.c diff -u src/sys/netcan/can.c:1.1.2.4 src/sys/netcan/can.c:1.1.2.5 --- src/sys/netcan/can.c:1.1.2.4 Sun Feb 5 11:45:11 2017 +++ src/sys/netcan/can.c Sun Feb 5 17:37:10 2017 @@ -1,4 +1,4 @@ -/* $NetBSD: can.c,v 1.1.2.4 2017/02/05 11:45:11 bouyer Exp $ */ +/* $NetBSD: can.c,v 1.1.2.5 2017/02/05 17:37:10 bouyer Exp $ */ /*- * Copyright (c) 2003, 2017 The NetBSD Foundation, Inc. @@ -30,7 +30,7 @@ */ #include -__KERNEL_RCSID(0, "$NetBSD: can.c,v 1.1.2.4 2017/02/05 11:45:11 bouyer Exp $"); +__KERNEL_RCSID(0, "$NetBSD: can.c,v 1.1.2.5 2017/02/05 17:37:10 bouyer Exp $"); #include #include @@ -45,6 +45,7 @@ __KERNEL_RCSID(0, "$NetBSD: can.c,v 1.1. #include #include +#include #include #include @@ -115,6 +116,17 @@ can_purgeif(struct socket *so, struct if return 0; } +void +can_ifattach(struct ifnet *ifp) { + ifp->if_mtu = sizeof(struct can_frame); + ifp->if_type = IFT_OTHER; + ifp->if_hdrlen = 0; + ifp->if_addrlen = 0; + ifp->if_dlt = DLT_CAN_SOCKETCAN; + ifp->if_output = NULL; /* unused */ + IFQ_SET_READY(>if_snd); +} + static int can_output(struct mbuf *m, struct canpcb *canp) { @@ -143,8 +155,7 @@ can_output(struct mbuf *m, struct canpcb if (m->m_len <= ifp->if_mtu) { can_output_cnt++; - error = (*ifp->if_output)(ifp, m, NULL, 0); - return error; + return ifq_enqueue(ifp, m); } else error = EMSGSIZE; bad: Index: src/sys/netcan/can_var.h diff -u src/sys/netcan/can_var.h:1.1.2.3 src/sys/netcan/can_var.h:1.1.2.4 --- src/sys/netcan/can_var.h:1.1.2.3 Sun Feb 5 11:45:11 2017 +++ src/sys/netcan/can_var.h Sun Feb 5 17:37:10 2017 @@ -1,4 +1,4 @@ -/* $NetBSD: can_var.h,v 1.1.2.3 2017/02/05 11:45:11 bouyer Exp $ */ +/* $NetBSD: can_var.h,v 1.1.2.4 2017/02/05 17:37:10 bouyer Exp $ */ /*- * Copyright (c) 2003, 2017 The NetBSD Foundation, Inc. @@ -45,6 +45,7 @@ extern struct domain candomain; extern const struct pr_usrreqs can_usrreqs; +void can_ifattach(struct ifnet *); void can_mbuf_tag_clean(struct mbuf *); void can_input(struct ifnet *, struct mbuf *); void *can_ctlinput(int, struct sockaddr *, void *); Index: src/sys/netcan/if_canloop.c diff -u src/sys/netcan/if_canloop.c:1.1.2.3 src/sys/netcan/if_canloop.c:1.1.2.4 --- src/sys/netcan/if_canloop.c:1.1.2.3 Sun Feb 5 11:45:11 2017 +++ src/sys/netcan/if_canloop.c Sun Feb 5 17:37:10 2017 @@ -1,4 +1,4 @@ -/* $NetBSD: if_canloop.c,v 1.1.2.3 2017/02/05 11:45:11 bouyer Exp $ */ +/* $NetBSD: if_canloop.c,v 1.1.2.4 2017/02/05 17:37:10 bouyer Exp $ */ /*- * Copyright (c) 2017 The NetBSD Foundation, Inc. @@ -35,7 +35,7 @@ */ #include -__KERNEL_RCSID(0, "$NetBSD: if_canloop.c,v 1.1.2.3 2017/02/05 11:45:11 bouyer Exp $"); +__KERNEL_RCSID(0, "$NetBSD: if_canloop.c,v 1.1.2.4 2017/02/05 17:37:10 bouyer Exp $"); #ifdef _KERNEL_OPT #include "opt_can.h" @@ -69,8 +69,7 @@ void canloopinit(void); static int canloop_clone_create(struct if_clone *, int); static int canloop_clone_destroy(struct ifnet *); static int canloop_ioctl(struct ifnet *, u_long, void *); -static int canloop_output(struct ifnet *, - struct mbuf *, const struct sockaddr *, const struct rtentry *); +static void canloop_ifstart(struct ifnet *); static int canloop_count; @@ -113,19 +112,13 @@ canloop_clone_create(struct if_clone *if if_initname(ifp, ifc->ifc_name, unit); - ifp->if_mtu = sizeof(struct can_frame); ifp->if_flags = IFF_LOOPBACK | IFF_RUNNING; ifp->if_extflags = IFEF_OUTPUT_MPSAFE; ifp->if_ioctl = canloop_ioctl; - ifp->if_output = canloop_output; - ifp->if_type = IFT_OTHER; - ifp->if_hdrlen = 0; - ifp->if_addrlen = 0; - ifp->if_dlt = DLT_CAN_SOCKETCAN; - IFQ_SET_READY(>if_snd); + ifp->if_start = canloop_ifstart; if_attach(ifp); - if_alloc_sadl(ifp); - bpf_attach(ifp, DLT_CAN_SOCKETCAN, sizeof(u_int)); + can_ifattach(ifp); + bpf_attach(ifp, DLT_CAN_SOCKETCAN, 0); #ifdef MBUFTRACE ifp->if_mowner = malloc(sizeof(struct mowner), M_DEVBUF, M_WAITOK | M_ZERO); @@ -156,41 +149,42 @@ canloop_clone_destroy(struct ifnet *ifp) return (0); } -static int -canloop_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst, -const struct rtentry *rt) +static void +canloop_ifstart(struct ifnet *ifp) { - int error = 0; size_t pktlen; - - MCLAIM(m, ifp->if_mowner); + struct mbuf *m; KERNEL_LOCK(1, NULL); - - if ((m->m_flags & M_PKTHDR) ==
CVS commit: [bouyer-socketcan] src/sys/netcan
Module Name:src Committed By: bouyer Date: Sun Feb 5 11:45:11 UTC 2017 Modified Files: src/sys/netcan [bouyer-socketcan]: can.c can_var.h if_canloop.c Log Message: Centralize mbuf tag cleanup, it will be used by real interface drivers too. To generate a diff of this commit: cvs rdiff -u -r1.1.2.3 -r1.1.2.4 src/sys/netcan/can.c cvs rdiff -u -r1.1.2.2 -r1.1.2.3 src/sys/netcan/can_var.h \ src/sys/netcan/if_canloop.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/sys/netcan/can.c diff -u src/sys/netcan/can.c:1.1.2.3 src/sys/netcan/can.c:1.1.2.4 --- src/sys/netcan/can.c:1.1.2.3 Sun Feb 5 10:56:12 2017 +++ src/sys/netcan/can.c Sun Feb 5 11:45:11 2017 @@ -1,4 +1,4 @@ -/* $NetBSD: can.c,v 1.1.2.3 2017/02/05 10:56:12 bouyer Exp $ */ +/* $NetBSD: can.c,v 1.1.2.4 2017/02/05 11:45:11 bouyer Exp $ */ /*- * Copyright (c) 2003, 2017 The NetBSD Foundation, Inc. @@ -30,7 +30,7 @@ */ #include -__KERNEL_RCSID(0, "$NetBSD: can.c,v 1.1.2.3 2017/02/05 10:56:12 bouyer Exp $"); +__KERNEL_RCSID(0, "$NetBSD: can.c,v 1.1.2.4 2017/02/05 11:45:11 bouyer Exp $"); #include #include @@ -153,6 +153,20 @@ bad: } /* + * cleanup mbuf tag, keeping the PACKET_TAG_SO tag + */ +void +can_mbuf_tag_clean(struct mbuf *m) +{ + struct m_tag *sotag; + + sotag = m_tag_find(m, PACKET_TAG_SO, NULL); + m_tag_delete_nonpersistent(m); + if (sotag) + m_tag_prepend(m, sotag); +} + +/* * Process a received CAN frame * the packet is in the mbuf chain m with * the CAN header. Index: src/sys/netcan/can_var.h diff -u src/sys/netcan/can_var.h:1.1.2.2 src/sys/netcan/can_var.h:1.1.2.3 --- src/sys/netcan/can_var.h:1.1.2.2 Mon Jan 16 18:03:38 2017 +++ src/sys/netcan/can_var.h Sun Feb 5 11:45:11 2017 @@ -1,4 +1,4 @@ -/* $NetBSD: can_var.h,v 1.1.2.2 2017/01/16 18:03:38 bouyer Exp $ */ +/* $NetBSD: can_var.h,v 1.1.2.3 2017/02/05 11:45:11 bouyer Exp $ */ /*- * Copyright (c) 2003, 2017 The NetBSD Foundation, Inc. @@ -45,6 +45,7 @@ extern struct domain candomain; extern const struct pr_usrreqs can_usrreqs; +void can_mbuf_tag_clean(struct mbuf *); void can_input(struct ifnet *, struct mbuf *); void *can_ctlinput(int, struct sockaddr *, void *); int can_ctloutput(int, struct socket *, struct sockopt *); Index: src/sys/netcan/if_canloop.c diff -u src/sys/netcan/if_canloop.c:1.1.2.2 src/sys/netcan/if_canloop.c:1.1.2.3 --- src/sys/netcan/if_canloop.c:1.1.2.2 Mon Jan 16 18:03:38 2017 +++ src/sys/netcan/if_canloop.c Sun Feb 5 11:45:11 2017 @@ -1,4 +1,4 @@ -/* $NetBSD: if_canloop.c,v 1.1.2.2 2017/01/16 18:03:38 bouyer Exp $ */ +/* $NetBSD: if_canloop.c,v 1.1.2.3 2017/02/05 11:45:11 bouyer Exp $ */ /*- * Copyright (c) 2017 The NetBSD Foundation, Inc. @@ -35,7 +35,7 @@ */ #include -__KERNEL_RCSID(0, "$NetBSD: if_canloop.c,v 1.1.2.2 2017/01/16 18:03:38 bouyer Exp $"); +__KERNEL_RCSID(0, "$NetBSD: if_canloop.c,v 1.1.2.3 2017/02/05 11:45:11 bouyer Exp $"); #ifdef _KERNEL_OPT #include "opt_can.h" @@ -83,7 +83,7 @@ canloopattach(int n) /* * Nothing to do here, initialization is handled by the - * module initialization code in canloopnit() below). + * module initialization code in canloopinit() below). */ } @@ -162,7 +162,6 @@ canloop_output(struct ifnet *ifp, struct { int error = 0; size_t pktlen; - struct m_tag *sotag; MCLAIM(m, ifp->if_mowner); @@ -178,12 +177,8 @@ canloop_output(struct ifnet *ifp, struct ifp->if_opackets++; ifp->if_obytes += pktlen; - /* we have to preserve the socket tag */ - sotag = m_tag_find(m, PACKET_TAG_SO, NULL); - m_tag_delete_nonpersistent(m); - if (sotag) - m_tag_prepend(m, sotag); #ifdef CAN + can_mbuf_tag_clean(m); can_input(ifp, m); #else printf("%s: can't handle CAN packet\n", ifp->if_xname);
CVS commit: [bouyer-socketcan] src/sys/netcan
Module Name:src Committed By: bouyer Date: Mon Jan 16 18:03:38 UTC 2017 Modified Files: src/sys/netcan [bouyer-socketcan]: can.c can.h can_pcb.h can_proto.c can_var.h if_canloop.c Log Message: Use PACKET_TAG_SO to store the sender's struct socket pointer, and use it to implement socket options CAN_RAW_LOOPBACK and CAN_RAW_RECV_OWN_MSGS. To generate a diff of this commit: cvs rdiff -u -r1.1.2.1 -r1.1.2.2 src/sys/netcan/can.c \ src/sys/netcan/can_pcb.h src/sys/netcan/can_proto.c \ src/sys/netcan/can_var.h src/sys/netcan/if_canloop.c cvs rdiff -u -r1.1.2.2 -r1.1.2.3 src/sys/netcan/can.h Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/sys/netcan/can.c diff -u src/sys/netcan/can.c:1.1.2.1 src/sys/netcan/can.c:1.1.2.2 --- src/sys/netcan/can.c:1.1.2.1 Sun Jan 15 20:27:33 2017 +++ src/sys/netcan/can.c Mon Jan 16 18:03:38 2017 @@ -1,4 +1,4 @@ -/* $NetBSD: can.c,v 1.1.2.1 2017/01/15 20:27:33 bouyer Exp $ */ +/* $NetBSD: can.c,v 1.1.2.2 2017/01/16 18:03:38 bouyer Exp $ */ /*- * Copyright (c) 2003, 2017 The NetBSD Foundation, Inc. @@ -30,12 +30,13 @@ */ #include -__KERNEL_RCSID(0, "$NetBSD: can.c,v 1.1.2.1 2017/01/15 20:27:33 bouyer Exp $"); +__KERNEL_RCSID(0, "$NetBSD: can.c,v 1.1.2.2 2017/01/16 18:03:38 bouyer Exp $"); #include #include #include #include +#include #include #include #include @@ -119,25 +120,35 @@ can_output(struct mbuf *m, struct canpcb { struct ifnet *ifp; int error = 0; + struct m_tag *sotag; - if (canp == 0) { + if (canp == NULL) { printf("can_output: no pcb\n"); error = EINVAL; - goto done; + return error; } ifp = canp->canp_ifp; if (ifp == 0) { error = EDESTADDRREQ; - goto done; + goto bad; + } + sotag = m_tag_get(PACKET_TAG_SO, sizeof(struct socket *), PR_NOWAIT); + if (sotag == NULL) { + ifp->if_oerrors++; + error = ENOMEM; + goto bad; } + *(struct socket **)(sotag + 1) = canp->canp_socket; + m_tag_prepend(m, sotag); + if (m->m_len <= ifp->if_mtu) { can_output_cnt++; error = (*ifp->if_output)(ifp, m, NULL, 0); - goto done; - } else error = EMSGSIZE; - + return error; + } else + error = EMSGSIZE; +bad: m_freem(m); -done: return (error); } @@ -180,6 +191,9 @@ canintr(void) struct sockaddr_can from; struct canpcb *canp; + struct m_tag *sotag; + struct socket *so; + struct canpcb *sender_canp; mutex_enter(softnet_lock); for (;;) { @@ -187,9 +201,22 @@ canintr(void) IF_DEQUEUE(, m); IFQ_UNLOCK(); - if (m == 0) /* no more queued packets */ + if (m == NULL) /* no more queued packets */ break; + sotag = m_tag_find(m, PACKET_TAG_SO, NULL); + if (sotag) { + so = *(struct socket **)(sotag + 1); + sender_canp = sotocanpcb(so); + m_tag_delete(m, sotag); + /* if the sender doesn't want loopback, don't do it */ + if (sender_canp->canp_flags & CANP_NO_LOOPBACK) { +m_freem(m); +continue; + } + } else { + sender_canp = NULL; + } memset(, 0, sizeof(struct sockaddr_can)); rcv_ifindex = m->m_pkthdr.rcvif_index; #if 0 @@ -201,10 +228,17 @@ canintr(void) TAILQ_FOREACH(canp, _queue, canp_queue) { struct mbuf *mc; + + /* don't loop back to sockets on other interfaces */ if (canp->canp_ifp != NULL && canp->canp_ifp->if_index != rcv_ifindex) { continue; } + /* don't loop back to myself if I don't want it */ + if (canp == sender_canp && + (canp->canp_flags & CANP_RECEIVE_OWN) == 0) +continue; + if (TAILQ_NEXT(canp, canp_queue) != NULL) { /* * we can't be sure we won't need @@ -653,6 +687,97 @@ can_ctlinput(int cmd, struct sockaddr *s } #endif +static int +can_raw_getop(struct canpcb *canp, struct sockopt *sopt) +{ + int optval = 0; + int error; + + switch (sopt->sopt_name) { + case CAN_RAW_LOOPBACK: + optval = (canp->canp_flags & CANP_NO_LOOPBACK) ? 0 : 1; + error = sockopt_set(sopt, , sizeof(optval)); + break; + case CAN_RAW_RECV_OWN_MSGS: + optval = (canp->canp_flags & CANP_RECEIVE_OWN) ? 1 : 0; + error = sockopt_set(sopt, , sizeof(optval)); + break; + default: + error = ENOPROTOOPT; + break; + } + return error; +} + +static int +can_raw_setop(struct canpcb *canp, struct sockopt *sopt) +{ + int optval = 0; + int error; + + switch (sopt->sopt_name) { + case CAN_RAW_LOOPBACK: + error = sockopt_getint(sopt, ); + if (error == 0) { + if (optval) { +canp->canp_flags &= ~CANP_NO_LOOPBACK; + } else { +canp->canp_flags |= CANP_NO_LOOPBACK; + } + } + break; + case CAN_RAW_RECV_OWN_MSGS: + error = sockopt_getint(sopt, ); + if (error == 0) { + if (optval) { +canp->canp_flags |= CANP_RECEIVE_OWN; + } else { +canp->canp_flags &= ~CANP_RECEIVE_OWN; + } + } + break; + default: + error = ENOPROTOOPT; + break; + } + return error; +} + +/* + * Called by getsockopt and setsockopt. + * + */ +int +can_ctloutput(int op, struct socket *so, struct sockopt
CVS commit: [bouyer-socketcan] src/sys/sys
Module Name:src Committed By: bouyer Date: Mon Jan 16 18:02:00 UTC 2017 Modified Files: src/sys/sys [bouyer-socketcan]: mbuf.h Log Message: Add PACKET_TAG_SO tag type, so store a pointer the sender struct socket. To generate a diff of this commit: cvs rdiff -u -r1.167 -r1.167.2.1 src/sys/sys/mbuf.h Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/sys/sys/mbuf.h diff -u src/sys/sys/mbuf.h:1.167 src/sys/sys/mbuf.h:1.167.2.1 --- src/sys/sys/mbuf.h:1.167 Tue Oct 4 14:13:21 2016 +++ src/sys/sys/mbuf.h Mon Jan 16 18:02:00 2017 @@ -1,4 +1,4 @@ -/* $NetBSD: mbuf.h,v 1.167 2016/10/04 14:13:21 christos Exp $ */ +/* $NetBSD: mbuf.h,v 1.167.2.1 2017/01/16 18:02:00 bouyer Exp $ */ /*- * Copyright (c) 1996, 1997, 1999, 2001, 2007 The NetBSD Foundation, Inc. @@ -897,6 +897,7 @@ struct m_tag *m_tag_next(struct mbuf *, #define PACKET_TAG_VLAN1 /* VLAN ID */ #define PACKET_TAG_ENCAP 2 /* encapsulation data */ #define PACKET_TAG_ESP3 /* ESP information */ +#define PACKET_TAG_SO4 /* sending socket pointer */ #define PACKET_TAG_PF11 /* packet filter */ #define PACKET_TAG_ALTQ_QID 12 /* ALTQ queue id */
CVS commit: [bouyer-socketcan] src/sys/netcan
Module Name:src Committed By: bouyer Date: Sun Jan 15 21:01:34 UTC 2017 Modified Files: src/sys/netcan [bouyer-socketcan]: can.h Log Message: Align can_frame.data to 8 bytes, per SocketCAN spec. To generate a diff of this commit: cvs rdiff -u -r1.1.2.1 -r1.1.2.2 src/sys/netcan/can.h Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/sys/netcan/can.h diff -u src/sys/netcan/can.h:1.1.2.1 src/sys/netcan/can.h:1.1.2.2 --- src/sys/netcan/can.h:1.1.2.1 Sun Jan 15 20:27:33 2017 +++ src/sys/netcan/can.h Sun Jan 15 21:01:34 2017 @@ -1,4 +1,4 @@ -/* $NetBSD: can.h,v 1.1.2.1 2017/01/15 20:27:33 bouyer Exp $ */ +/* $NetBSD: can.h,v 1.1.2.2 2017/01/15 21:01:34 bouyer Exp $ */ /*- * Copyright (c) 2003, 2017 The NetBSD Foundation, Inc. @@ -68,7 +68,7 @@ struct can_frame { uint8_t __pad; uint8_t __res0; uint8_t __res1; - uint8_t data[CAN_MAX_DLEN]; + uint8_t data[CAN_MAX_DLEN] __aligned(8); }; #define CAN_MTU (sizeof(struct can_frame))
CVS commit: [bouyer-socketcan] src/sys
Module Name:src Committed By: bouyer Date: Sun Jan 15 20:27:34 UTC 2017 Modified Files: src/sys [bouyer-socketcan]: Makefile src/sys/conf [bouyer-socketcan]: files src/sys/net [bouyer-socketcan]: netisr.h netisr_dispatch.h src/sys/rump/include/opt [bouyer-socketcan]: opt_rumpkernel.h src/sys/rump/include/rump [bouyer-socketcan]: rumpdefs.h src/sys/rump/net [bouyer-socketcan]: Makefile.rumpnetcomp src/sys/sys [bouyer-socketcan]: socket.h Added Files: src/sys/netcan [bouyer-socketcan]: Makefile can.c can.h can_pcb.c can_pcb.h can_proto.c can_var.h files.netcan if_canloop.c src/sys/rump/net/lib/libnetcan [bouyer-socketcan]: Makefile NETCAN.ioconf netcan_component.c Log Message: Initial commit of a CAN socket layer, compatible with linux SoccketCAN (but incomplete). Based on work from Robert Swindells. To generate a diff of this commit: cvs rdiff -u -r1.79 -r1.79.22.1 src/sys/Makefile cvs rdiff -u -r1.1168 -r1.1168.2.1 src/sys/conf/files cvs rdiff -u -r1.44 -r1.44.4.1 src/sys/net/netisr.h cvs rdiff -u -r1.18 -r1.18.12.1 src/sys/net/netisr_dispatch.h cvs rdiff -u -r0 -r1.1.2.1 src/sys/netcan/Makefile src/sys/netcan/can.c \ src/sys/netcan/can.h src/sys/netcan/can_pcb.c src/sys/netcan/can_pcb.h \ src/sys/netcan/can_proto.c src/sys/netcan/can_var.h \ src/sys/netcan/files.netcan src/sys/netcan/if_canloop.c cvs rdiff -u -r1.5 -r1.5.4.1 src/sys/rump/include/opt/opt_rumpkernel.h cvs rdiff -u -r1.36 -r1.36.4.1 src/sys/rump/include/rump/rumpdefs.h cvs rdiff -u -r1.16 -r1.16.2.1 src/sys/rump/net/Makefile.rumpnetcomp cvs rdiff -u -r0 -r1.1.2.1 src/sys/rump/net/lib/libnetcan/Makefile \ src/sys/rump/net/lib/libnetcan/NETCAN.ioconf \ src/sys/rump/net/lib/libnetcan/netcan_component.c cvs rdiff -u -r1.120 -r1.120.2.1 src/sys/sys/socket.h Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. Modified files: Index: src/sys/Makefile diff -u src/sys/Makefile:1.79 src/sys/Makefile:1.79.22.1 --- src/sys/Makefile:1.79 Fri Mar 1 18:25:27 2013 +++ src/sys/Makefile Sun Jan 15 20:27:33 2017 @@ -1,9 +1,9 @@ -# $NetBSD: Makefile,v 1.79 2013/03/01 18:25:27 joerg Exp $ +# $NetBSD: Makefile,v 1.79.22.1 2017/01/15 20:27:33 bouyer Exp $ .include SUBDIR= altq arch compat dev fs miscfs \ - net net80211 netatalk netbt netipsec netinet netinet6 \ + net net80211 netatalk netbt netcan netipsec netinet netinet6 \ netisdn netkey netmpls netnatm netsmb \ nfs opencrypto sys ufs uvm Index: src/sys/conf/files diff -u src/sys/conf/files:1.1168 src/sys/conf/files:1.1168.2.1 --- src/sys/conf/files:1.1168 Mon Dec 26 23:12:33 2016 +++ src/sys/conf/files Sun Jan 15 20:27:33 2017 @@ -1,4 +1,4 @@ -# $NetBSD: files,v 1.1168 2016/12/26 23:12:33 pgoyette Exp $ +# $NetBSD: files,v 1.1168.2.1 2017/01/15 20:27:33 bouyer Exp $ # @(#)files.newconf 7.5 (Berkeley) 5/10/93 version 20150846 @@ -229,6 +229,7 @@ file net/bpfjit.c sljit & bpfjit include "net80211/files.net80211" include "netatalk/files.netatalk" include "netbt/files.netbt" +include "netcan/files.netcan" include "netinet/files.netinet" include "netinet6/files.netinet6" include "netipsec/files.netipsec" @@ -1435,6 +1436,7 @@ defpseudo stf: ifnet defpseudodev tap: ifnet, ether, arp defpseudo carp: ifnet, ether, arp defpseudodev etherip: ifnet, ether, arp +defpseudo canloop: ifnet defpseudo sequencer defpseudo clockctl Index: src/sys/net/netisr.h diff -u src/sys/net/netisr.h:1.44 src/sys/net/netisr.h:1.44.4.1 --- src/sys/net/netisr.h:1.44 Mon May 25 08:29:01 2015 +++ src/sys/net/netisr.h Sun Jan 15 20:27:33 2017 @@ -1,4 +1,4 @@ -/* $NetBSD: netisr.h,v 1.44 2015/05/25 08:29:01 ozaki-r Exp $ */ +/* $NetBSD: netisr.h,v 1.44.4.1 2017/01/15 20:27:33 bouyer Exp $ */ /* * Copyright (c) 1980, 1986, 1989, 1993 @@ -53,6 +53,7 @@ #include "opt_atalk.h" #include "opt_mpls.h" #include "opt_natm.h" +#include "opt_can.h" #include "arp.h" #endif /* defined(_KERNEL_OPT) */ @@ -90,6 +91,10 @@ #ifdef NETATALK #include #endif +#ifdef CAN +#include +#include +#endif #endif /* !defined(_LOCORE) */ #endif /* defined(_KERNEL) */ @@ -109,6 +114,7 @@ #define NETISR_NATM 27 /* same as AF_NATM */ #define NETISR_ARP 28 /* same as AF_ARP */ #define NETISR_MPLS 33 /* same as AF_MPLS */ +#define NETISR_CAN 35 /* same as AF_CAN */ #define NETISR_MAX AF_MAX #if !defined(_LOCORE) && defined(_KERNEL) Index: src/sys/net/netisr_dispatch.h diff -u src/sys/net/netisr_dispatch.h:1.18 src/sys/net/netisr_dispatch.h:1.18.12.1 --- src/sys/net/netisr_dispatch.h:1.18 Thu Jun 5 23:48:16 2014 +++ src/sys/net/netisr_dispatch.h Sun Jan 15 20:27:33 2017 @@ -1,4 +1,4 @@ -/* $NetBSD: netisr_dispatch.h,v 1.18 2014/06/05 23:48:16 rmind Exp $ */ +/* $NetBSD: netisr_dispatch.h,v 1.18.12.1 2017/01/15 20:27:33 bouyer Exp $ */ #ifndef _NET_NETISR_DISPATCH_H_ #define _NET_NETISR_DISPATCH_H_ @@ -41,5